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 {
CloudPrefImpl(const int32_t userId,const std::string & bundleName,const std::string & tableName)22 CloudPrefImpl::CloudPrefImpl(const int32_t userId, const std::string& bundleName, const std::string& tableName)
23 {
24 /* the file name varies from different userId and bundle name */
25 std::string userIdDir = CLOUDFILE_DIR + std::to_string(userId);
26 if (access(userIdDir.c_str(), F_OK) != 0) {
27 if (mkdir(userIdDir.c_str(), S_IRWXU | S_IRWXG | S_IXOTH) != 0) {
28 LOGE("CloudPrefImpl: mkdir failed");
29 }
30 }
31
32 std::string bundleDir = userIdDir + "/" + bundleName;
33 if (access(bundleDir.c_str(), F_OK) != 0) {
34 if (mkdir(bundleDir.c_str(), S_IRWXU | S_IRWXG | S_IXOTH) != 0) {
35 LOGE("CloudPrefImpl: mkdir failed");
36 }
37 }
38 fileName_ = bundleDir + "/" + tableName;
39 int32_t errCode = 0;
40 pref_ = NativePreferences::PreferencesHelper::GetPreferences(fileName_, errCode);
41 if (!pref_) {
42 LOGE("CloudPrefImpl: Preference get null, errcode: %{public}d", errCode);
43 }
44 }
45
CloudPrefImpl(const std::string & fileName)46 CloudPrefImpl::CloudPrefImpl(const std::string& fileName)
47 {
48 int32_t errCode = 0;
49 fileName_ = fileName;
50 pref_ = NativePreferences::PreferencesHelper::GetPreferences(fileName_, errCode);
51 if (!pref_) {
52 LOGE("CloudPrefImpl: Preference get null, errcode: %{public}d", errCode);
53 }
54 }
55
SetString(const std::string & key,const std::string & value)56 void CloudPrefImpl::SetString(const std::string& key, const std::string& value)
57 {
58 pref_->PutString(key, value);
59 pref_->Flush();
60 }
61
GetString(const std::string & key,std::string & value)62 void CloudPrefImpl::GetString(const std::string& key, std::string &value)
63 {
64 value = pref_->GetString(key, "");
65 }
66
SetLong(const std::string & key,const int64_t value)67 void CloudPrefImpl::SetLong(const std::string& key, const int64_t value)
68 {
69 pref_->PutLong(key, value);
70 pref_->Flush();
71 }
72
GetLong(const std::string & key,int64_t & value)73 void CloudPrefImpl::GetLong(const std::string& key, int64_t &value)
74 {
75 value = pref_->GetLong(key, 0);
76 }
77
SetInt(const std::string & key,const int value)78 void CloudPrefImpl::SetInt(const std::string& key, const int value)
79 {
80 pref_->PutInt(key, value);
81 pref_->Flush();
82 }
83
GetInt(const std::string & key,int32_t & value)84 void CloudPrefImpl::GetInt(const std::string& key, int32_t &value)
85 {
86 value = pref_->GetInt(key, 0);
87 }
88
SetBool(const std::string & key,const bool & value)89 void CloudPrefImpl::SetBool(const std::string& key, const bool& value)
90 {
91 pref_->PutBool(key, value);
92 pref_->Flush();
93 }
94
GetBool(const std::string & key,bool & value)95 void CloudPrefImpl::GetBool(const std::string& key, bool& value)
96 {
97 value = pref_->GetBool(key, false);
98 }
99
Clear()100 void CloudPrefImpl::Clear()
101 {
102 pref_->Clear();
103 NativePreferences::PreferencesHelper::DeletePreferences(fileName_);
104 }
105
Delete(const std::string & key)106 void CloudPrefImpl::Delete(const std::string& key)
107 {
108 pref_->Delete(key);
109 pref_->FlushSync();
110 }
111 } // namespace OHOS::FileManagement::CloudSync