1cb7eb8c9Sopenharmony_ci/*
2cb7eb8c9Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3cb7eb8c9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cb7eb8c9Sopenharmony_ci * you may not use this file except in compliance with the License.
5cb7eb8c9Sopenharmony_ci * You may obtain a copy of the License at
6cb7eb8c9Sopenharmony_ci *
7cb7eb8c9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cb7eb8c9Sopenharmony_ci *
9cb7eb8c9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cb7eb8c9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cb7eb8c9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb7eb8c9Sopenharmony_ci * See the License for the specific language governing permissions and
13cb7eb8c9Sopenharmony_ci * limitations under the License.
14cb7eb8c9Sopenharmony_ci */
15cb7eb8c9Sopenharmony_ci
16cb7eb8c9Sopenharmony_ci#include "cloud_pref_impl.h"
17cb7eb8c9Sopenharmony_ci#include <sys/stat.h>
18cb7eb8c9Sopenharmony_ci#include <unistd.h>
19cb7eb8c9Sopenharmony_ci#include "utils_log.h"
20cb7eb8c9Sopenharmony_ci
21cb7eb8c9Sopenharmony_cinamespace OHOS::FileManagement::CloudSync {
22cb7eb8c9Sopenharmony_cinamespace {
23cb7eb8c9Sopenharmony_ci    static const uint32_t STAT_MODE_DIR = 0771;
24cb7eb8c9Sopenharmony_ci}
25cb7eb8c9Sopenharmony_ci
26cb7eb8c9Sopenharmony_ciCloudPrefImpl::CloudPrefImpl(const int32_t userId, const std::string& bundleName, const std::string& tableName)
27cb7eb8c9Sopenharmony_ci{
28cb7eb8c9Sopenharmony_ci    /* the file name varies from different userId and bundle name */
29cb7eb8c9Sopenharmony_ci    std::string userIdDir = "/data/service/el2/" + std::to_string(userId) + "/hmdfs/cloudfile_manager";
30cb7eb8c9Sopenharmony_ci    if (access(userIdDir.c_str(), F_OK) != 0) {
31cb7eb8c9Sopenharmony_ci        if (mkdir(userIdDir.c_str(), STAT_MODE_DIR) != 0) {
32cb7eb8c9Sopenharmony_ci            LOGE("CloudPrefImpl: mkdir failed");
33cb7eb8c9Sopenharmony_ci        }
34cb7eb8c9Sopenharmony_ci    }
35cb7eb8c9Sopenharmony_ci
36cb7eb8c9Sopenharmony_ci    std::string bundleDir = userIdDir + "/" + bundleName;
37cb7eb8c9Sopenharmony_ci    if (access(bundleDir.c_str(), F_OK) != 0) {
38cb7eb8c9Sopenharmony_ci        if (mkdir(bundleDir.c_str(), STAT_MODE_DIR) != 0) {
39cb7eb8c9Sopenharmony_ci            LOGE("CloudPrefImpl: mkdir failed");
40cb7eb8c9Sopenharmony_ci        }
41cb7eb8c9Sopenharmony_ci    }
42cb7eb8c9Sopenharmony_ci    fileName_ = bundleDir + "/" + tableName;
43cb7eb8c9Sopenharmony_ci    int32_t errCode = 0;
44cb7eb8c9Sopenharmony_ci    pref_ = NativePreferences::PreferencesHelper::GetPreferences(fileName_, errCode);
45cb7eb8c9Sopenharmony_ci    if (!pref_) {
46cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: Preference get null, errcode: %{public}d", errCode);
47cb7eb8c9Sopenharmony_ci    }
48cb7eb8c9Sopenharmony_ci}
49cb7eb8c9Sopenharmony_ci
50cb7eb8c9Sopenharmony_ciCloudPrefImpl::CloudPrefImpl(const std::string& fileName)
51cb7eb8c9Sopenharmony_ci{
52cb7eb8c9Sopenharmony_ci    int32_t errCode = 0;
53cb7eb8c9Sopenharmony_ci    fileName_ = fileName;
54cb7eb8c9Sopenharmony_ci    pref_ = NativePreferences::PreferencesHelper::GetPreferences(fileName_, errCode);
55cb7eb8c9Sopenharmony_ci    if (!pref_) {
56cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: Preference get null, errcode: %{public}d", errCode);
57cb7eb8c9Sopenharmony_ci    }
58cb7eb8c9Sopenharmony_ci}
59cb7eb8c9Sopenharmony_ci
60cb7eb8c9Sopenharmony_civoid CloudPrefImpl::SetString(const std::string& key, const std::string& value)
61cb7eb8c9Sopenharmony_ci{
62cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
63cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
64cb7eb8c9Sopenharmony_ci        return;
65cb7eb8c9Sopenharmony_ci    }
66cb7eb8c9Sopenharmony_ci    pref_->PutString(key, value);
67cb7eb8c9Sopenharmony_ci    int ret = pref_->FlushSync();
68cb7eb8c9Sopenharmony_ci    if (ret) {
69cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: SetString has error, ret = %{public}d", ret);
70cb7eb8c9Sopenharmony_ci    }
71cb7eb8c9Sopenharmony_ci}
72cb7eb8c9Sopenharmony_ci
73cb7eb8c9Sopenharmony_civoid CloudPrefImpl::GetString(const std::string& key, std::string &value)
74cb7eb8c9Sopenharmony_ci{
75cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
76cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
77cb7eb8c9Sopenharmony_ci        return;
78cb7eb8c9Sopenharmony_ci    }
79cb7eb8c9Sopenharmony_ci    value = pref_->GetString(key, "");
80cb7eb8c9Sopenharmony_ci}
81cb7eb8c9Sopenharmony_ci
82cb7eb8c9Sopenharmony_civoid CloudPrefImpl::SetLong(const std::string& key, const int64_t value)
83cb7eb8c9Sopenharmony_ci{
84cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
85cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
86cb7eb8c9Sopenharmony_ci        return;
87cb7eb8c9Sopenharmony_ci    }
88cb7eb8c9Sopenharmony_ci    pref_->PutLong(key, value);
89cb7eb8c9Sopenharmony_ci    int ret = pref_->FlushSync();
90cb7eb8c9Sopenharmony_ci    if (ret) {
91cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: SetLong has error, ret = %{public}d", ret);
92cb7eb8c9Sopenharmony_ci    }
93cb7eb8c9Sopenharmony_ci}
94cb7eb8c9Sopenharmony_ci
95cb7eb8c9Sopenharmony_civoid CloudPrefImpl::GetLong(const std::string& key, int64_t &value)
96cb7eb8c9Sopenharmony_ci{
97cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
98cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
99cb7eb8c9Sopenharmony_ci        return;
100cb7eb8c9Sopenharmony_ci    }
101cb7eb8c9Sopenharmony_ci    value = pref_->GetLong(key, 0);
102cb7eb8c9Sopenharmony_ci}
103cb7eb8c9Sopenharmony_ci
104cb7eb8c9Sopenharmony_civoid CloudPrefImpl::SetInt(const std::string& key, const int value)
105cb7eb8c9Sopenharmony_ci{
106cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
107cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
108cb7eb8c9Sopenharmony_ci        return;
109cb7eb8c9Sopenharmony_ci    }
110cb7eb8c9Sopenharmony_ci    pref_->PutInt(key, value);
111cb7eb8c9Sopenharmony_ci    int ret = pref_->FlushSync();
112cb7eb8c9Sopenharmony_ci    if (ret) {
113cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: SetInt has error, ret = %{public}d", ret);
114cb7eb8c9Sopenharmony_ci    }
115cb7eb8c9Sopenharmony_ci}
116cb7eb8c9Sopenharmony_ci
117cb7eb8c9Sopenharmony_civoid CloudPrefImpl::GetInt(const std::string& key, int32_t &value)
118cb7eb8c9Sopenharmony_ci{
119cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
120cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
121cb7eb8c9Sopenharmony_ci        return;
122cb7eb8c9Sopenharmony_ci    }
123cb7eb8c9Sopenharmony_ci    value = pref_->GetInt(key, 0);
124cb7eb8c9Sopenharmony_ci}
125cb7eb8c9Sopenharmony_ci
126cb7eb8c9Sopenharmony_civoid CloudPrefImpl::SetBool(const std::string& key, const bool& value)
127cb7eb8c9Sopenharmony_ci{
128cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
129cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
130cb7eb8c9Sopenharmony_ci        return;
131cb7eb8c9Sopenharmony_ci    }
132cb7eb8c9Sopenharmony_ci    pref_->PutBool(key, value);
133cb7eb8c9Sopenharmony_ci    int ret = pref_->FlushSync();
134cb7eb8c9Sopenharmony_ci    if (ret) {
135cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: SetBool has error, ret = %{public}d", ret);
136cb7eb8c9Sopenharmony_ci    }
137cb7eb8c9Sopenharmony_ci}
138cb7eb8c9Sopenharmony_ci
139cb7eb8c9Sopenharmony_civoid CloudPrefImpl::GetBool(const std::string& key, bool& value)
140cb7eb8c9Sopenharmony_ci{
141cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
142cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
143cb7eb8c9Sopenharmony_ci        return;
144cb7eb8c9Sopenharmony_ci    }
145cb7eb8c9Sopenharmony_ci    value = pref_->GetBool(key, false);
146cb7eb8c9Sopenharmony_ci}
147cb7eb8c9Sopenharmony_ci
148cb7eb8c9Sopenharmony_civoid CloudPrefImpl::Clear()
149cb7eb8c9Sopenharmony_ci{
150cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
151cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
152cb7eb8c9Sopenharmony_ci        return;
153cb7eb8c9Sopenharmony_ci    }
154cb7eb8c9Sopenharmony_ci    pref_->Clear();
155cb7eb8c9Sopenharmony_ci    NativePreferences::PreferencesHelper::DeletePreferences(fileName_);
156cb7eb8c9Sopenharmony_ci}
157cb7eb8c9Sopenharmony_ci
158cb7eb8c9Sopenharmony_civoid CloudPrefImpl::Delete(const std::string& key)
159cb7eb8c9Sopenharmony_ci{
160cb7eb8c9Sopenharmony_ci    if (pref_ == nullptr) {
161cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: perf_ is null");
162cb7eb8c9Sopenharmony_ci        return;
163cb7eb8c9Sopenharmony_ci    }
164cb7eb8c9Sopenharmony_ci    pref_->Delete(key);
165cb7eb8c9Sopenharmony_ci    int ret = pref_->FlushSync();
166cb7eb8c9Sopenharmony_ci    if (ret) {
167cb7eb8c9Sopenharmony_ci        LOGE("CloudPrefImpl: Delete has error, ret = %{public}d", ret);
168cb7eb8c9Sopenharmony_ci    }
169cb7eb8c9Sopenharmony_ci}
170cb7eb8c9Sopenharmony_ci} // namespace OHOS::FileManagement::CloudSync