1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <dirent.h>
17 #include <unistd.h>
18 #include "constant.h"
19 
20 namespace OHOS {
21 namespace DistributedKv {
22 // the Key Prefix for Meta data of KvStore.
23 const std::string KvStoreMetaRow::KEY_PREFIX = "KvStoreMetaData";
24 
25 const std::string SecretMetaRow::KEY_PREFIX = "SecretKey";
26 
27 /* version for distributed kv data service. */
28 const std::string Constant::VERSION = "1";
29 
30 /* meta name for distributed kv data service. */
31 const std::string Constant::META_DIR_NAME = "Meta";
32 
33 /* name for distributed kv data service. */
34 const std::string Constant::SERVICE_NAME = "mdds";
35 
36 /* root path for distributed kv data service. */
37 const std::string Constant::ROOT_PATH = "/data/misc_de/0";
38 
39 /* root path for distributeddata service and system services. */
40 const std::string Constant::ROOT_PATH_DE = "/data/misc_de/0";
41 
42 /* root path for self-developed and non-self-developed app. */
43 const std::string Constant::ROOT_PATH_CE = "/data/misc_ce/0";
44 
45 // the max length for key is 1024.
46 const size_t Constant::MAX_KEY_LENGTH = 1024;
47 
48 // the max length for StoreId is 128.
49 const size_t Constant::MAX_STORE_ID_LENGTH = 128;
50 
51 // the max length for value is 4M.
52 const size_t Constant::MAX_VALUE_LENGTH = 4 * 1024 * 1024;
53 
54 // the max batch for putBatch is 128.
55 const size_t Constant::MAX_BATCH_SIZE = 128;
56 
57 // the max capacity for ipc is 800K.
58 const size_t Constant::MAX_IPC_CAPACITY = 800 * 1024;
59 
60 // the default mode is 0755, stands for r/w/x for user, r/x for group, r/x for others.
61 const mode_t Constant::DEFAULT_MODE = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
62 
63 // the mode for dir is 0755, r/w/x for user, r/-/x for group, r/-/x for others.
64 const mode_t Constant::DEFAULT_MODE_DIR = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
65 
66 // the mode for file is 0600, r/w/- for user, -/-/- for group, -/-/- for others.
67 const mode_t Constant::DEFAULT_MODE_FILE = S_IRUSR | S_IWUSR;
68 
69 // Size threshold of switching to large data is a little smaller than MAX_IPC_CAPACITY.
70 const int Constant::SWITCH_RAW_DATA_SIZE = 700 * 1024;
71 
72 const int Constant::MAX_OPEN_KVSTORES = 16;
73 
74 // default group id for synchronization.
75 const std::string Constant::DEFAULT_GROUP_ID = "default";
76 
77 // true indicates the ownership of distributed data is DEVICE, otherwise, ACCOUNT
78 const bool Constant::STOREID_ONLY_FLAG = true;
79 
80 // service meta db name.
81 const std::string Constant::SERVICE_META_DB_NAME = "service_meta";
82 
83 const std::string Constant::KEY_SEPARATOR = "###";
84 
85 const std::string Constant::ROOT_KEY_GENERATED = "RootKeyGenerated";
86 
GetKeyFor(const std::string & key)87 std::vector<uint8_t> KvStoreMetaRow::GetKeyFor(const std::string &key)
88 {
89     std::string str = Constant::Concatenate({ KvStoreMetaRow::KEY_PREFIX, Constant::KEY_SEPARATOR, key });
90     return std::vector<uint8_t>(str.begin(), str.end());
91 }
92 
GetKeyFor(const std::string & key)93 std::vector<uint8_t> SecretMetaRow::GetKeyFor(const std::string &key)
94 {
95     std::string str = Constant::Concatenate({ SecretMetaRow::KEY_PREFIX, Constant::KEY_SEPARATOR, key});
96     return std::vector<uint8_t>(str.begin(), str.end());
97 }
98 
Concatenate(std::initializer_list<std::string> stringList)99 std::string Constant::Concatenate(std::initializer_list<std::string> stringList)
100 {
101     std::string result;
102     size_t result_size = 0;
103     for (const std::string &str : stringList) {
104         result_size += str.size();
105     }
106     result.reserve(result_size);
107     for (const std::string &str : stringList) {
108         result.append(str.data(), str.size());
109     }
110     return result;
111 }
112 
GetDefaultDeviceAccountId()113 std::string Constant::GetDefaultDeviceAccountId()
114 {
115     return "0";
116 }
117 
GetDefaultHarmonyAccountName()118 std::string Constant::GetDefaultHarmonyAccountName()
119 {
120     return "default";
121 }
122 }  // namespace DistributedKv
123 }  // namespace OHOS
124