• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define MLOG_TAG "MediaLibraryBackupUtils"
16 
17 #include "backup_database_helper.h"
18 #include "backup_database_utils.h"
19 #include "backup_file_utils.h"
20 #include "media_file_utils.h"
21 #include "media_log.h"
22 
23 namespace OHOS::Media {
Init(int32_t sceneCode,bool shouldIncludeSd,const std::string & prefix)24 void BackupDatabaseHelper::Init(int32_t sceneCode, bool shouldIncludeSd, const std::string &prefix)
25 {
26     CHECK_AND_RETURN_INFO_LOG(sceneCode == DUAL_FRAME_CLONE_RESTORE_ID,
27         "sceneCode is dual clone restore");
28     std::vector<int32_t> dbTypeList;
29     if (shouldIncludeSd) {
30         dbTypeList = { DbType::PHOTO_CACHE, DbType::VIDEO_CACHE, DbType::PHOTO_SD_CACHE, DbType::VIDEO_SD_CACHE };
31     } else {
32         dbTypeList = { DbType::PHOTO_CACHE, DbType::VIDEO_CACHE };
33     }
34     InitDb(dbTypeList, prefix);
35 }
36 
InitDb(int32_t dbType,const std::string & prefix)37 void BackupDatabaseHelper::InitDb(int32_t dbType, const std::string &prefix)
38 {
39     CHECK_AND_RETURN_LOG(!HasDb(dbType), "Db %{public}d already exists", dbType);
40     CHECK_AND_RETURN_LOG(DB_INFO_MAP.count(dbType) != 0, "No such db type: %{public}d", dbType);
41     DbInfo dbInfo = DB_INFO_MAP.at(dbType);
42     std::string dbFullPath = prefix + dbInfo.path;
43     CHECK_AND_RETURN_LOG(MediaFileUtils::IsFileExists(dbFullPath),
44         "Db not exist, type: %{public}d, path: %{public}s", dbType,
45         BackupFileUtils::GarbleFilePath(dbFullPath, DEFAULT_RESTORE_ID).c_str());
46 
47     int32_t errCode = BackupDatabaseUtils::InitDb(dbInfo.rdbStore, dbInfo.name, dbFullPath, BUNDLE_NAME, false);
48     CHECK_AND_RETURN_LOG(dbInfo.rdbStore != nullptr,
49         "Init db failed, type: %{public}d, errCode: %{public}d", dbType, errCode);
50     dbInfoMap_[dbType] = dbInfo;
51     MEDIA_INFO_LOG("Init db succeeded, type: %{public}d, current size: %{public}zu", dbType, dbInfoMap_.size());
52 }
53 
InitDb(const std::vector<int32_t> & dbTypeList,const std::string & prefix)54 void BackupDatabaseHelper::InitDb(const std::vector<int32_t> &dbTypeList, const std::string &prefix)
55 {
56     for (auto dbType : dbTypeList) {
57         InitDb(dbType, prefix);
58     }
59 }
60 
AddDb(int32_t dbType,std::shared_ptr<NativeRdb::RdbStore> rdbStore)61 void BackupDatabaseHelper::AddDb(int32_t dbType, std::shared_ptr<NativeRdb::RdbStore> rdbStore)
62 {
63     if (HasDb(dbType)) {
64         MEDIA_WARN_LOG("Db %{public}d already exists", dbType);
65         return;
66     }
67     dbInfoMap_[dbType] = DbInfo(rdbStore);
68     MEDIA_INFO_LOG("Add db succeeded, type: %{public}d, current size: %{public}zu", dbType, dbInfoMap_.size());
69 }
70 
IsFileExist(int32_t sceneCode,const FileInfo & fileInfo,int32_t & dbType,int32_t & dbStatus,int32_t & fileStatus)71 void BackupDatabaseHelper::IsFileExist(int32_t sceneCode, const FileInfo &fileInfo, int32_t &dbType, int32_t &dbStatus,
72     int32_t &fileStatus)
73 {
74     FileQueryInfo fileQueryInfo;
75     GetFileQueryInfo(sceneCode, fileInfo, fileQueryInfo);
76     dbType = fileQueryInfo.dbType;
77     if (!HasDb(fileQueryInfo.dbType)) {
78         dbStatus = E_DB_FAIL;
79         return;
80     }
81     DbInfo dbInfo = dbInfoMap_.at(fileQueryInfo.dbType);
82     std::string querySql = "SELECT count(1) as count FROM " + fileQueryInfo.tableName + " WHERE " +
83         fileQueryInfo.columnName + " = ?";
84     std::vector<std::string> queryArgs = { fileQueryInfo.path };
85     auto resultSet = BackupDatabaseUtils::GetQueryResultSet(dbInfo.rdbStore, querySql, queryArgs);
86     if (resultSet == nullptr || resultSet->GoToFirstRow() != E_OK) {
87         dbStatus = E_HAS_DB_ERROR;
88         return;
89     }
90     dbStatus = E_OK;
91     fileStatus = GetInt32Val(CUSTOM_COUNT, resultSet) > 0 ? E_OK : E_NO_SUCH_FILE;
92 }
93 
HasDb(int32_t dbType)94 bool BackupDatabaseHelper::HasDb(int32_t dbType)
95 {
96     return dbInfoMap_.count(dbType) > 0;
97 }
98 
GetFileQueryInfo(int32_t sceneCode,const FileInfo & fileInfo,FileQueryInfo & fileQueryInfo)99 void BackupDatabaseHelper::GetFileQueryInfo(int32_t sceneCode, const FileInfo &fileInfo, FileQueryInfo &fileQueryInfo)
100 {
101     if (sceneCode == UPGRADE_RESTORE_ID) {
102         fileQueryInfo = FileQueryInfo(DbType::EXTERNAL, "files", "_data", fileInfo.oldPath);
103         return;
104     }
105     int32_t dbType;
106     if (fileInfo.isInternal) {
107         dbType = fileInfo.fileType == DUAL_MEDIA_TYPE::IMAGE_TYPE ? DbType::PHOTO_CACHE : DbType::VIDEO_CACHE;
108     } else {
109         dbType = fileInfo.fileType == DUAL_MEDIA_TYPE::IMAGE_TYPE ? DbType::PHOTO_SD_CACHE : DbType::VIDEO_SD_CACHE;
110     }
111     std::string tableName = fileInfo.fileSize >= TAR_FILE_LIMIT ? "normal_file" : "small_file";
112     fileQueryInfo = FileQueryInfo(dbType, tableName, "filepath", fileInfo.oldPath);
113 }
114 } // namespace OHOS::Media