1 /*
2 * Copyright (c) 2024 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 #include "database_backup_task.h"
16 #include "clouddisk_rdbstore.h"
17 #include "parameters.h"
18 #include "utils_log.h"
19
20 namespace OHOS {
21 namespace FileManagement {
22 namespace CloudSync {
23 using namespace std;
24 using namespace OHOS::FileManagement::CloudDisk;
25 static const std::string FILEMANAGER_KEY = "persist.kernel.bundle_name.filemanager";
26 static const uint32_t STAT_MODE_DIR = 0771;
DatabaseBackupTask(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)27 DatabaseBackupTask::DatabaseBackupTask(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
28 : CycleTask("database_backup_task", {system::GetParameter(FILEMANAGER_KEY, "")}, ONE_DAY, dataSyncManager)
29 {
30 }
31
RunTaskForBundle(int32_t userId,std::string bundleName)32 int32_t DatabaseBackupTask::RunTaskForBundle(int32_t userId, std::string bundleName)
33 {
34 LOGI("%{public}s backup database entry", bundleName.c_str());
35 auto directoryName = "/data/service/el2/" + to_string(userId_) +
36 "/hmdfs/cloudfile_manager/" + bundleName + "/backup";
37 if (access(directoryName.c_str(), F_OK) != 0) {
38 if (errno == ENOENT) {
39 if (mkdir(directoryName.c_str(), STAT_MODE_DIR) != 0) {
40 LOGE("mkdir backup failed :%{public}s err:%{public}d", GetAnonyString(directoryName).c_str(), errno);
41 return errno;
42 }
43 } else {
44 LOGE("access backup dir failed :%{public}s err:%{public}d", GetAnonyString(directoryName).c_str(), errno);
45 return errno;
46 }
47 }
48 auto fileName = directoryName + "/clouddisk_backup.db";
49 std::thread backup([fileName, userId, bundleName] {
50 auto cloudDiskRdbStore = make_shared<CloudDisk::CloudDiskRdbStore>(bundleName, userId);
51 auto rdb = cloudDiskRdbStore->GetRaw();
52 if (!rdb) {
53 LOGE("clouddisk rdb init fail");
54 return;
55 }
56 auto ret = rdb->Backup(fileName);
57 if (ret != 0) {
58 LOGE("clouddisk backup failed, ret %{public}d", ret);
59 }
60 });
61 backup.detach();
62 return E_OK;
63 }
64 } // namespace CloudSync
65 } // namespace FileManagement
66 } // namespace OHOS