1 /*
2 * Copyright (c) 2023 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 "cloud_pref_impl.h"
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include "utils_log.h"
20
21 namespace OHOS::FileManagement::CloudSync {
22 namespace {
23 static const uint32_t STAT_MODE_DIR = 0771;
24 }
25
CloudPrefImpl(const int32_t userId,const std::string & bundleName,const std::string & tableName)26 CloudPrefImpl::CloudPrefImpl(const int32_t userId, const std::string& bundleName, const std::string& tableName)
27 {
28 /* the file name varies from different userId and bundle name */
29 std::string userIdDir = "/data/service/el2/" + std::to_string(userId) + "/hmdfs/cloudfile_manager";
30 if (access(userIdDir.c_str(), F_OK) != 0) {
31 if (mkdir(userIdDir.c_str(), STAT_MODE_DIR) != 0) {
32 LOGE("CloudPrefImpl: mkdir failed");
33 }
34 }
35
36 std::string bundleDir = userIdDir + "/" + bundleName;
37 if (access(bundleDir.c_str(), F_OK) != 0) {
38 if (mkdir(bundleDir.c_str(), STAT_MODE_DIR) != 0) {
39 LOGE("CloudPrefImpl: mkdir failed");
40 }
41 }
42 fileName_ = bundleDir + "/" + tableName;
43 int32_t errCode = 0;
44 pref_ = NativePreferences::PreferencesHelper::GetPreferences(fileName_, errCode);
45 if (!pref_) {
46 LOGE("CloudPrefImpl: Preference get null, errcode: %{public}d", errCode);
47 }
48 }
49
CloudPrefImpl(const std::string & fileName)50 CloudPrefImpl::CloudPrefImpl(const std::string& fileName)
51 {
52 int32_t errCode = 0;
53 fileName_ = fileName;
54 pref_ = NativePreferences::PreferencesHelper::GetPreferences(fileName_, errCode);
55 if (!pref_) {
56 LOGE("CloudPrefImpl: Preference get null, errcode: %{public}d", errCode);
57 }
58 }
59
SetString(const std::string & key,const std::string & value)60 void CloudPrefImpl::SetString(const std::string& key, const std::string& value)
61 {
62 if (pref_ == nullptr) {
63 LOGE("CloudPrefImpl: perf_ is null");
64 return;
65 }
66 pref_->PutString(key, value);
67 pref_->Flush();
68 }
69
GetString(const std::string & key,std::string & value)70 void CloudPrefImpl::GetString(const std::string& key, std::string &value)
71 {
72 if (pref_ == nullptr) {
73 LOGE("CloudPrefImpl: perf_ is null");
74 return;
75 }
76 value = pref_->GetString(key, "");
77 }
78
SetLong(const std::string & key,const int64_t value)79 void CloudPrefImpl::SetLong(const std::string& key, const int64_t value)
80 {
81 if (pref_ == nullptr) {
82 LOGE("CloudPrefImpl: perf_ is null");
83 return;
84 }
85 pref_->PutLong(key, value);
86 pref_->Flush();
87 }
88
GetLong(const std::string & key,int64_t & value)89 void CloudPrefImpl::GetLong(const std::string& key, int64_t &value)
90 {
91 if (pref_ == nullptr) {
92 LOGE("CloudPrefImpl: perf_ is null");
93 return;
94 }
95 value = pref_->GetLong(key, 0);
96 }
97
SetInt(const std::string & key,const int value)98 void CloudPrefImpl::SetInt(const std::string& key, const int value)
99 {
100 if (pref_ == nullptr) {
101 LOGE("CloudPrefImpl: perf_ is null");
102 return;
103 }
104 pref_->PutInt(key, value);
105 pref_->Flush();
106 }
107
GetInt(const std::string & key,int32_t & value)108 void CloudPrefImpl::GetInt(const std::string& key, int32_t &value)
109 {
110 if (pref_ == nullptr) {
111 LOGE("CloudPrefImpl: perf_ is null");
112 return;
113 }
114 value = pref_->GetInt(key, 0);
115 }
116
SetBool(const std::string & key,const bool & value)117 void CloudPrefImpl::SetBool(const std::string& key, const bool& value)
118 {
119 if (pref_ == nullptr) {
120 LOGE("CloudPrefImpl: perf_ is null");
121 return;
122 }
123 pref_->PutBool(key, value);
124 pref_->Flush();
125 }
126
GetBool(const std::string & key,bool & value)127 void CloudPrefImpl::GetBool(const std::string& key, bool& value)
128 {
129 if (pref_ == nullptr) {
130 LOGE("CloudPrefImpl: perf_ is null");
131 return;
132 }
133 value = pref_->GetBool(key, false);
134 }
135
Clear()136 void CloudPrefImpl::Clear()
137 {
138 if (pref_ == nullptr) {
139 LOGE("CloudPrefImpl: perf_ is null");
140 return;
141 }
142 pref_->Clear();
143 NativePreferences::PreferencesHelper::DeletePreferences(fileName_);
144 }
145
Delete(const std::string & key)146 void CloudPrefImpl::Delete(const std::string& key)
147 {
148 if (pref_ == nullptr) {
149 LOGE("CloudPrefImpl: perf_ is null");
150 return;
151 }
152 pref_->Delete(key);
153 pref_->FlushSync();
154 }
155 } // namespace OHOS::FileManagement::CloudSync