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 #ifndef OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H 17 #define OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H 18 19 #include "data_sync_notifier.h" 20 #include "medialibrary_db_const.h" 21 #include "medialibrary_type_const.h" 22 23 #include "cloud_pref_impl.h" 24 #include "file_data_convertor.h" 25 #include "gallery_sysevent.h" 26 #include "rdb_data_handler.h" 27 namespace OHOS { 28 namespace FileManagement { 29 namespace CloudSync { 30 31 class FileDataHandler : public RdbDataHandler, public GallerySyncStatContainer, 32 public GalleryCheckStatContainer { 33 public: 34 friend class GalleryDataSyncer; 35 enum Clean { 36 NOT_NEED_CLEAN = 0, 37 NEED_CLEAN, 38 }; 39 FileDataHandler(int32_t userId, const std::string &bundleName, 40 std::shared_ptr<NativeRdb::RdbStore> rdb, std::shared_ptr<bool> stopFlag); 41 virtual ~FileDataHandler() = default; 42 43 /* download */ 44 void GetFetchCondition(FetchCondition &cond) override; 45 virtual int32_t OnFetchRecords(std::shared_ptr<std::vector<DriveKit::DKRecord>> &records, 46 OnFetchParams ¶ms) override; 47 virtual int32_t GetRetryRecords(std::vector<DriveKit::DKRecordId> &records) override; 48 virtual int32_t GetCheckRecords(std::vector<DriveKit::DKRecordId> &checkRecords, 49 const std::shared_ptr<std::vector<DriveKit::DKRecord>> &records) override; 50 virtual int32_t GetAssetsToDownload(std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload) override; 51 virtual int32_t GetThumbToDownload(std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload) override; 52 int32_t GetDownloadAsset(std::string cloudId, 53 std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload) override; 54 int32_t HandleRecord(std::shared_ptr<std::vector<DriveKit::DKRecord>> &records, OnFetchParams ¶ms, 55 std::vector<std::string> &recordIds, const std::shared_ptr<NativeRdb::ResultSet> &resultSet, 56 const std::map<std::string, int> &recordIdRowIdMap); 57 58 /*clean*/ 59 int32_t Clean(const int action) override; 60 int32_t MarkClean(const int32_t action); 61 int32_t CleanRemainRecord(); 62 63 /* upload */ 64 int32_t GetCreatedRecords(std::vector<DriveKit::DKRecord> &records) override; 65 int32_t GetDeletedRecords(std::vector<DriveKit::DKRecord> &records) override; 66 int32_t GetMetaModifiedRecords(std::vector<DriveKit::DKRecord> &records) override; 67 int32_t GetFileModifiedRecords(std::vector<DriveKit::DKRecord> &records) override; 68 69 /* callback */ 70 int32_t OnCreateRecords(const std::map<DriveKit::DKRecordId, 71 DriveKit::DKRecordOperResult> &map) override; 72 int32_t OnDeleteRecords(const std::map<DriveKit::DKRecordId, 73 DriveKit::DKRecordOperResult> &map) override; 74 int32_t OnModifyMdirtyRecords(const std::map<DriveKit::DKRecordId, 75 DriveKit::DKRecordOperResult> &map) override; 76 int32_t OnModifyFdirtyRecords(const std::map<DriveKit::DKRecordId, 77 DriveKit::DKRecordOperResult> &map) override; 78 int32_t OnDownloadSuccess(const DriveKit::DKDownloadAsset &asset) override; 79 int32_t OnDownloadAssets(const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &resultMap) override; 80 int32_t OnDownloadAssets(const DriveKit::DKDownloadAsset &asset) override; 81 void PeriodicUpdataFiles(uint32_t &timeId); 82 void StopUpdataFiles(uint32_t &timeId); 83 84 int32_t OnDownloadAssetsFailure(const std::vector<DriveKit::DKDownloadAsset> &assets) override; 85 86 /* optimize storage */ 87 int32_t OptimizeStorage(const int32_t agingDays); 88 std::shared_ptr<NativeRdb::ResultSet> GetAgingFile(const int64_t agingTime, int32_t &rowCount); 89 int32_t UpdateAgingFile(const std::string cloudId); 90 int32_t FileAgingDelete(const int64_t agingTime, const int64_t deleteSize); 91 92 void UpdateAlbumInternal(); 93 void UpdateAllAlbums(); 94 95 /* reset */ 96 void Reset(); 97 98 private: 99 int32_t OnCreateRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry, 100 const std::unordered_map<std::string, LocalInfo> &localMap); 101 int32_t CheckRecordData(DriveKit::DKRecordData &data, std::string &path); 102 int32_t OnDeleteRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry); 103 int32_t OnMdirtyRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry, 104 const std::unordered_map<std::string, LocalInfo> &localMap); 105 int32_t OnFdirtyRecordSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry, 106 const std::unordered_map<std::string, LocalInfo> &localMap); 107 108 bool IsTimeChanged(const DriveKit::DKRecord &record, const std::unordered_map<std::string, LocalInfo> &localMap, 109 const std::string &path, const std::string &type); 110 int32_t GetLocalInfo(const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &map, 111 std::unordered_map<std::string, LocalInfo> &infoMap, const std::string &type); 112 int32_t BuildInfoMap(const std::shared_ptr<NativeRdb::ResultSet> resultSet, 113 std::unordered_map<std::string, LocalInfo> &cloudMap, const std::string &type); 114 int64_t UTCTimeSeconds(); 115 116 int32_t EraseLocalInfo(std::vector<DriveKit::DKRecord> &records); 117 int32_t UpdateMediaFilePath(DriveKit::DKRecord &record, NativeRdb::ResultSet &local); 118 119 /* data calculate*/ 120 int32_t CompensateFilePath(DriveKit::DKRecord &record); 121 int32_t CalculateFilePath(DriveKit::DKRecord &record, std::string &filePath); 122 int32_t GetMediaType(DriveKit::DKRecord &record, int32_t &mediaType); 123 int32_t GetAssetUniqueId(int32_t &type); 124 int32_t CreateAssetPathById(DriveKit::DKRecord &record, int32_t &uniqueId, 125 int32_t &mediaType, std::string &filePath); 126 int32_t CreateAssetBucket(int32_t &uniqueId, int32_t &bucketNum); 127 std::string GetFileExtension(DriveKit::DKRecord &record); 128 int32_t CreateAssetRealName(int32_t &fileId, int32_t &mediaType, 129 const std::string &extension, std::string &name); 130 131 /* album map */ 132 int32_t BindAlbums(std::vector<DriveKit::DKRecord> &records); 133 int32_t BindAlbumChanges(std::vector<DriveKit::DKRecord> &records); 134 int32_t UpdateLocalAlbumMap(const std::string &cloudId); 135 136 int32_t GetAlbumCloudIds(std::vector<int32_t> &localIds, std::vector<std::string> &cloudIds); 137 int32_t GetAlbumIdsFromResultSet(const std::shared_ptr<NativeRdb::ResultSet> resultSet, std::vector<int32_t> &ids); 138 int32_t GetAlbumIdsFromResultSet(const std::shared_ptr<NativeRdb::ResultSet> resultSet, 139 std::vector<int32_t> &add, std::vector<int32_t> &rm); 140 141 /* data check */ 142 int32_t CheckDataConsistency(NativeRdb::ResultSet &resultSet); 143 144 int32_t CheckAssetConsistency(NativeRdb::ResultSet &resultSet); 145 int32_t CheckContentConsistency(NativeRdb::ResultSet &resultSet); 146 int32_t CheckThumbConsistency(NativeRdb::ResultSet &resultSet, const std::string &suffix); 147 148 int32_t CheckStatusConsistency(NativeRdb::ResultSet &resultSet); 149 int32_t CheckDirtyConsistency(NativeRdb::ResultSet &resultSet); 150 int32_t CheckPositionConsistency(NativeRdb::ResultSet &resultSet); 151 int32_t CheckSyncStatusConsistency(NativeRdb::ResultSet &resultSet); 152 153 static inline const std::string TABLE_NAME = Media::PhotoColumn::PHOTOS_TABLE; 154 static inline const int32_t LIMIT_SIZE = 5; 155 static inline const int32_t MODIFY_BATCH_NUM = 20; 156 static inline const int32_t DELETE_BATCH_NUM = 20; 157 static inline const int32_t DOWNLOAD_LIMIT_SIZE = 200; 158 DriveKit::DKRecordType recordType_ = "media"; 159 DriveKit::DKFieldKeyArray desiredKeys_; 160 DriveKit::DKFieldKeyArray checkedKeys_ = {"version", "id"}; 161 std::vector<std::string> modifyFailSet_; 162 std::vector<std::string> createFailSet_; 163 std::vector<NativeRdb::ValueObject> retrySet_; 164 165 /* identifier */ 166 int32_t userId_; 167 std::string bundleName_; 168 169 /*clean*/ 170 FileDataConvertor cleanConvertor_ = { userId_, bundleName_, FILE_CLEAN }; 171 int32_t ClearCloudInfo(const std::string &cloudId); 172 int32_t CleanAllCloudInfo(); 173 int32_t CleanNotDirtyData(bool isReamin = false); 174 int32_t CleanNotPureCloudRecord(const int32_t action); 175 int32_t CleanPureCloudRecord(bool isReamin = false); 176 int32_t DeleteDentryFile(void); 177 int32_t GetFilePathAndId(NativeRdb::AbsRdbPredicates cleanPredicates, 178 std::vector<NativeRdb::ValueObject> &deleteFileId, 179 std::vector<std::string> &filePaths); 180 void RemoveCloudRecord(std::shared_ptr<std::vector<std::string>> filePaths); 181 void RemoveBothRecord(std::shared_ptr<std::vector<std::string>> filePaths); 182 183 /* err handle */ 184 void HandleCreateConvertErr(int32_t err, NativeRdb::ResultSet &resultSet); 185 void HandleFdirtyConvertErr(int32_t err, NativeRdb::ResultSet &resultSet); 186 187 /* create */ 188 FileDataConvertor createConvertor_ = { 189 userId_, bundleName_, FILE_CREATE, 190 std::bind(&FileDataHandler::HandleCreateConvertErr, this, std::placeholders::_1, std::placeholders::_2) 191 }; 192 193 /* delete */ 194 FileDataConvertor deleteConvertor_ = { userId_, bundleName_, FILE_DELETE }; 195 196 /* update */ 197 FileDataConvertor mdirtyConvertor_ = { userId_, bundleName_, FILE_METADATA_MODIFY }; 198 FileDataConvertor fdirtyConvertor_ = { 199 userId_, bundleName_, FILE_DATA_MODIFY, 200 std::bind(&FileDataHandler::HandleFdirtyConvertErr, this, std::placeholders::_1, std::placeholders::_2) 201 }; 202 203 /* file Conflict */ 204 static inline const std::string CON_SUFFIX = "_1"; 205 std::string ConflictRenameThumb(NativeRdb::ResultSet &resultSet, 206 std::string fullPath, 207 std::string &tmpPath, 208 std::string &newPath); 209 int32_t ConflictRename(NativeRdb::ResultSet &resultSet, std::string &fullPath, std::string &relativePath); 210 int32_t ConflictRenamePath(NativeRdb::ResultSet &resultSet, 211 std::string &fullPath, 212 std::string &rdbPath, 213 std::string &localPath, 214 std::string &newLocalPath); 215 int32_t ConflictDataMerge(DriveKit::DKRecord &record, std::string &fullPath, bool upflag); 216 int32_t ConflictHandler(NativeRdb::ResultSet &resultSet, 217 const DriveKit::DKRecord &record, 218 int64_t isize, 219 bool &modifyPathflag); 220 int32_t ConflictDifferent(NativeRdb::ResultSet &resultSet, 221 const DriveKit::DKRecord &record, 222 std::string &fullPath, 223 std::string &relativePath); 224 int32_t ConflictMerge(NativeRdb::ResultSet &resultSet, 225 DriveKit::DKRecord &record, 226 std::string &fullPath, 227 bool &comflag, 228 int64_t &imetaModified); 229 int32_t GetConflictData(const DriveKit::DKRecord &record, 230 std::string &fullPath, 231 int64_t &isize, 232 int64_t &imetaModified, 233 std::string &relativePath); 234 int32_t PullRecordConflict(DriveKit::DKRecord &record, bool &comflag); 235 236 /* pull operations */ 237 std::tuple<std::shared_ptr<NativeRdb::ResultSet>, std::map<std::string, int>> QueryLocalByCloudId( 238 const std::vector<std::string> &recordIds); 239 int32_t PullRecordInsert(DriveKit::DKRecord &record, OnFetchParams ¶ms); 240 int32_t PullRecordUpdate(DriveKit::DKRecord &record, NativeRdb::ResultSet &local, 241 OnFetchParams ¶ms); 242 void RemoveThmParentPath(const std::string &filePath); 243 int32_t PullRecordDelete(DriveKit::DKRecord &record, NativeRdb::ResultSet &local); 244 int32_t SetRetry(const std::string &recordId); 245 int32_t SetRetry(std::vector<NativeRdb::ValueObject> &retryList); 246 int32_t RecycleFile(const std::string &recordId); 247 void AppendToDownload(NativeRdb::ResultSet &local, 248 const std::string &fieldKey, 249 std::vector<DriveKit::DKDownloadAsset> &assetsToDownload); 250 void AppendToDownload(const DriveKit::DKRecord &record, 251 const std::string &fieldKey, 252 std::vector<DriveKit::DKDownloadAsset> &assetsToDownload); 253 int AddCloudThumbs(const DriveKit::DKRecord &record); 254 int DentryInsertThumb(const std::string &fullPath, 255 const std::string &recordId, 256 uint64_t size, 257 uint64_t mtime, 258 const std::string &type); 259 int DentryRemoveThumb(const std::string &downloadPath); 260 bool ThumbsAtLocal(const DriveKit::DKRecord &record); 261 int32_t UpdateAssetInPhotoMap(const DriveKit::DKRecord &record, int32_t fileId); 262 int32_t InsertAssetToPhotoMap(const DriveKit::DKRecord &record, OnFetchParams ¶ms); 263 int32_t DeleteAssetInPhotoMap(int32_t fileId); 264 int32_t GetAlbumIdFromCloudId(const std::string &recordId); 265 int32_t BatchGetFileIdFromCloudId(const std::vector<NativeRdb::ValueObject> &recordIds, std::vector<int> &fileIds); 266 int32_t QueryWithBatchCloudId(std::vector<int> &fileIds, std::vector<std::string> &thmStrVec); 267 void QueryAndInsertMap(int32_t albumId, int32_t fileId); 268 void QueryAndDeleteMap(int32_t fileId, const std::set<int> &cloudMapIds); 269 int32_t BatchInsertAssetMaps(OnFetchParams ¶ms); 270 void UpdateThmVec(); 271 void UpdateLcdVec(); 272 273 /* db result to record */ 274 FileDataConvertor localConvertor_ = { userId_, bundleName_, FILE_DOWNLOAD }; 275 std::mutex rdbMutex_; 276 std::mutex thmMutex_; 277 std::mutex lcdMutex_; 278 std::mutex cleanMutex_; 279 std::vector<NativeRdb::ValueObject> thmVec_; 280 std::vector<NativeRdb::ValueObject> lcdVec_; 281 int32_t waitCount_{0}; 282 uint32_t timeId_; 283 }; 284 } // namespace CloudSync 285 } // namespace FileManagement 286 } // namespace OHOS 287 #endif // OHOS_CLOUD_SYNC_SERVICE_FILE_DATA_HANDLER_H 288