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     int ret = pref_->FlushSync();
68     if (ret) {
69         LOGE("CloudPrefImpl: SetString has error, ret = %{public}d", ret);
70     }
71 }
72 
GetString(const std::string& key, std::string &value)73 void CloudPrefImpl::GetString(const std::string& key, std::string &value)
74 {
75     if (pref_ == nullptr) {
76         LOGE("CloudPrefImpl: perf_ is null");
77         return;
78     }
79     value = pref_->GetString(key, "");
80 }
81 
SetLong(const std::string& key, const int64_t value)82 void CloudPrefImpl::SetLong(const std::string& key, const int64_t value)
83 {
84     if (pref_ == nullptr) {
85         LOGE("CloudPrefImpl: perf_ is null");
86         return;
87     }
88     pref_->PutLong(key, value);
89     int ret = pref_->FlushSync();
90     if (ret) {
91         LOGE("CloudPrefImpl: SetLong has error, ret = %{public}d", ret);
92     }
93 }
94 
GetLong(const std::string& key, int64_t &value)95 void CloudPrefImpl::GetLong(const std::string& key, int64_t &value)
96 {
97     if (pref_ == nullptr) {
98         LOGE("CloudPrefImpl: perf_ is null");
99         return;
100     }
101     value = pref_->GetLong(key, 0);
102 }
103 
SetInt(const std::string& key, const int value)104 void CloudPrefImpl::SetInt(const std::string& key, const int value)
105 {
106     if (pref_ == nullptr) {
107         LOGE("CloudPrefImpl: perf_ is null");
108         return;
109     }
110     pref_->PutInt(key, value);
111     int ret = pref_->FlushSync();
112     if (ret) {
113         LOGE("CloudPrefImpl: SetInt has error, ret = %{public}d", ret);
114     }
115 }
116 
GetInt(const std::string& key, int32_t &value)117 void CloudPrefImpl::GetInt(const std::string& key, int32_t &value)
118 {
119     if (pref_ == nullptr) {
120         LOGE("CloudPrefImpl: perf_ is null");
121         return;
122     }
123     value = pref_->GetInt(key, 0);
124 }
125 
SetBool(const std::string& key, const bool& value)126 void CloudPrefImpl::SetBool(const std::string& key, const bool& value)
127 {
128     if (pref_ == nullptr) {
129         LOGE("CloudPrefImpl: perf_ is null");
130         return;
131     }
132     pref_->PutBool(key, value);
133     int ret = pref_->FlushSync();
134     if (ret) {
135         LOGE("CloudPrefImpl: SetBool has error, ret = %{public}d", ret);
136     }
137 }
138 
GetBool(const std::string& key, bool& value)139 void CloudPrefImpl::GetBool(const std::string& key, bool& value)
140 {
141     if (pref_ == nullptr) {
142         LOGE("CloudPrefImpl: perf_ is null");
143         return;
144     }
145     value = pref_->GetBool(key, false);
146 }
147 
Clear()148 void CloudPrefImpl::Clear()
149 {
150     if (pref_ == nullptr) {
151         LOGE("CloudPrefImpl: perf_ is null");
152         return;
153     }
154     pref_->Clear();
155     NativePreferences::PreferencesHelper::DeletePreferences(fileName_);
156 }
157 
Delete(const std::string& key)158 void CloudPrefImpl::Delete(const std::string& key)
159 {
160     if (pref_ == nullptr) {
161         LOGE("CloudPrefImpl: perf_ is null");
162         return;
163     }
164     pref_->Delete(key);
165     int ret = pref_->FlushSync();
166     if (ret) {
167         LOGE("CloudPrefImpl: Delete has error, ret = %{public}d", ret);
168     }
169 }
170 } // namespace OHOS::FileManagement::CloudSync