diff --git a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h index 75003fc184d0aef505ebca3f514ba3a246355694..01dc02212b978ecffb0c3f8950514be10796be52 100644 --- a/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h +++ b/services/cloudsyncservice/include/data_sync/gallery_data_sync/file_data_handler.h @@ -92,7 +92,14 @@ public: int32_t OptimizeStorage(const int32_t agingDays); std::shared_ptr GetAgingFile(const int64_t agingTime, int32_t &rowCount); int32_t UpdateAgingFile(const std::string cloudId); + int32_t GetAgingFileInfo(std::shared_ptr results, + std::string &path, std::string &cloudId, int64_t &size); + int32_t AgingDentryInsert(std::string file_name, std::string path, std::string cloudId, + std::string relativePath, int64_t size); int32_t FileAgingDelete(const int64_t agingTime, const int64_t deleteSize); + int32_t RollBackUpdateAgingFile(const std::string cloudId); + int32_t RollBackAgingDentryInsert(std::string fileName, std::string path, std::string cloudId, + std::string relativePath, int64_t size); void UpdateAlbumInternal(); diff --git a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp index 8c7b43ee04898a6f7ac81dd2f9ae38f45fec8bdf..485aa36fed8831a702ac7df18d1a81bc51b9d971 100644 --- a/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp +++ b/services/cloudsyncservice/src/data_sync/gallery_data_sync/file_data_handler.cpp @@ -4400,6 +4400,49 @@ int32_t FileDataHandler::UpdateAgingFile(const string cloudId) return E_OK; } +int32_t FileDataHandler::GetAgingFileInfo(std::shared_ptr results, + string &path, string &cloudId, int64_t &size) +{ + int ret = DataConvertor::GetString(PhotoColumn::MEDIA_FILE_PATH, path, *results); + if (ret != E_OK) { + LOGE("get path error"); + return E_RDB; + } + ret = DataConvertor::GetString(PhotoColumn::PHOTO_CLOUD_ID, cloudId, *results); + if (ret != E_OK) { + LOGE("get cloudId error"); + return E_RDB; + } + ret = DataConvertor::GetLong(PhotoColumn::MEDIA_SIZE, size, *results); + if (ret != E_OK) { + LOGE("get size error"); + return E_RDB; + } + return E_OK; +} + +int32_t FileDataHandler::AgingDentryInsert(string fileName, string path, + string cloudId, string relativePath, int64_t size) +{ + DKRecord record; + if (GetDentryPathName(path, relativePath, fileName) != E_OK) { + LOGE("split to dentry path failed, path:%s", path.c_str()); + return E_INVAL_ARG; + } + int64_t mtime = static_cast(record.GetEditedTime()) / MILLISECOND_TO_SECOND; + string recordId = MetaFileMgr::RecordIdToCloudId(cloudId); + auto mFile = MetaFileMgr::GetInstance().GetMetaFile(userId_, relativePath); + MetaBase mBaseLookup(fileName); + MetaBase mBase(fileName, recordId); + mBase.size = static_cast(size); + mBase.mtime = static_cast(mtime); + if (mFile->DoLookup(mBaseLookup) == E_OK) { + LOGE("dentry exist when insert, do update instead"); + return mFile->DoUpdate(mBase); + } + return mFile->DoCreate(mBase); +} + int32_t FileDataHandler::FileAgingDelete(const int64_t agingTime, const int64_t deleteSize) { int rowCount = 0; @@ -4414,33 +4457,33 @@ int32_t FileDataHandler::FileAgingDelete(const int64_t agingTime, const int64_t return E_OK; } while (results->GoToNextRow() == 0) { + string fileName; string path; - int64_t size; string cloudId; - int ret = DataConvertor::GetString(PhotoColumn::MEDIA_FILE_PATH, path, *results); + string relativePath; + int64_t size; + int ret = GetAgingFileInfo(results, path, cloudId, size); if (ret != E_OK) { - LOGE("get path error"); + LOGE("get aging file info failed"); continue; } - ret = DataConvertor::GetString(PhotoColumn::PHOTO_CLOUD_ID, cloudId, *results); + ret = UpdateAgingFile(cloudId); if (ret != E_OK) { - LOGE("get cloudId error"); + LOGE("update failed"); continue; } - ret = DataConvertor::GetLong(PhotoColumn::MEDIA_SIZE, size, *results); + ret = AgingDentryInsert(fileName, path, cloudId, relativePath, size); if (ret != E_OK) { - LOGE("get size error"); + ret = RollBackUpdateAgingFile(cloudId); + LOGE("aging file insert failed"); continue; } string filePath = GetLocalPath(userId_, path); ret = unlink(filePath.c_str()); if (ret != 0) { - LOGE("fail to delete"); - continue; - } - ret = UpdateAgingFile(cloudId); - if (ret != E_OK) { - LOGE("update failed"); + LOGE("fail to delete, errno:%{public}d", errno); + ret = RollBackUpdateAgingFile(cloudId); + ret = RollBackAgingDentryInsert(fileName, path, cloudId, relativePath, size); continue; } totalSize += size; @@ -4448,6 +4491,43 @@ int32_t FileDataHandler::FileAgingDelete(const int64_t agingTime, const int64_t break; } } + MetaFileMgr::GetInstance().ClearAll(); + return E_OK; +} + +int32_t FileDataHandler::RollBackUpdateAgingFile(const string cloudId) +{ + ValuesBucket values; + values.PutInt(PhotoColumn::PHOTO_POSITION, POSITION_BOTH); + int32_t changedRows; + string whereClause = PhotoColumn::PHOTO_CLOUD_ID + " = ?"; + int ret = Update(changedRows, values, whereClause, {cloudId}); + if (ret != E_OK) { + LOGE("rdb update failed, err=%{public}d", ret); + return E_RDB; + } + return E_OK; +} + +int32_t FileDataHandler::RollBackAgingDentryInsert(string fileName, string path, string cloudId, + string relativePath, int64_t size) +{ + DKRecord record; + if (GetDentryPathName(path, relativePath, fileName) != E_OK) { + LOGE("split to dentry path failed, path:%s", path.c_str()); + return E_INVAL_ARG; + } + int64_t mtime = static_cast(record.GetEditedTime()) / MILLISECOND_TO_SECOND; + string recordId = MetaFileMgr::RecordIdToCloudId(cloudId); + auto mFile = MetaFileMgr::GetInstance().GetMetaFile(userId_, relativePath); + MetaBase mBaseLookup(fileName); + MetaBase mBase(fileName, recordId); + mBase.size = static_cast(size); + mBase.mtime = static_cast(mtime); + if (mFile->DoLookup(mBaseLookup) == E_OK) { + LOGE("dentry exist when insert, do remove instead"); + return mFile->DoRemove(mBase); + } return E_OK; }