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 16 #ifndef OHOS_CLOUD_DISK_SERVICE_RDBSTORE_H 17 #define OHOS_CLOUD_DISK_SERVICE_RDBSTORE_H 18 19 #include "rdb_errno.h" 20 #include "rdb_helper.h" 21 #include "rdb_open_callback.h" 22 #include "rdb_store.h" 23 #include "rdb_store_config.h" 24 25 #include "cloud_file_utils.h" 26 #include "clouddisk_db_const.h" 27 #include "file_column.h" 28 #include "meta_file.h" 29 #include "transaction.h" 30 31 namespace OHOS { 32 namespace FileManagement { 33 namespace CloudDisk { 34 35 class CloudDiskRdbStore final { 36 public: 37 CloudDiskRdbStore(const std::string &bundleName, const int32_t &userId); 38 ~CloudDiskRdbStore(); 39 40 int32_t RdbInit(); 41 std::shared_ptr<NativeRdb::RdbStore> GetRaw(); 42 43 int32_t LookUp(const std::string &parentCloudId, const std::string &fileName, CloudDiskFileInfo &info); 44 int32_t GetAttr(const std::string &cloudId, CloudDiskFileInfo &info); 45 int32_t SetAttr(const std::string &fileName, const std::string &parentCloudId, const std::string &cloudId, 46 const unsigned long long &size); 47 int32_t ReadDir(const std::string &cloudId, std::vector<CloudDiskFileInfo> &infos); 48 int32_t MkDir(const std::string &cloudId, const std::string &parentCloudId, 49 const std::string &directoryName, bool noNeedUpload); 50 int32_t Create(const std::string &cloudId, const std::string &parentCloudId, 51 const std::string &fileName, bool noNeedUpload); 52 int32_t Write(const std::string &fileName, const std::string &parentCloudId, const std::string &cloudId); 53 int32_t GetXAttr(const std::string &cloudId, const std::string &key, std::string &value, 54 const CacheNode &node = {}, const std::string &extAttrKey = ""); 55 int32_t SetXAttr(const std::string &cloudId, const std::string &key, const std::string &value, 56 const std::string &name = "", const std::string &parentCloudId = ""); 57 int32_t Rename(const std::string &oldParentCloudId, const std::string &oldFileName, 58 const std::string &newParentCloudId, const std::string &newFileName); 59 int32_t Unlink(const std::string &cloudId, const int32_t &noUpload); 60 int32_t RecycleSetXattr(const std::string &name, const std::string &parentCloudId, 61 const std::string &cloudId, const std::string &value); 62 int32_t LocationSetXattr(const std::string &name, const std::string &parentCloudId, 63 const std::string &cloudId, const std::string &value); 64 int32_t HasTHMSetXattr(const std::string &name, const std::string &key, 65 const std::string &cloudId, const std::string &value); 66 int32_t FavoriteSetXattr(const std::string &cloudId, const std::string &value); 67 int32_t LocationGetXattr(const std::string &name, const std::string &key, std::string &value, 68 const std::string &parentCloudId); 69 int32_t FavoriteGetXattr(const std::string &cloudId, const std::string &key, std::string &value); 70 int32_t FileStatusGetXattr(const std::string &cloudId, const std::string &key, std::string &value); 71 int32_t TimeRecycledGetXattr(const std::string &cloudId, const std::string &key, std::string &value); 72 int32_t GetHasChild(const std::string &cloudId, bool &hasChild); 73 int32_t GetRowId(const std::string &cloudId, int64_t &rowId); 74 int32_t GetParentCloudId(const std::string &cloudId, std::string &parentCloudId); 75 int32_t ExtAttributeSetXattr(const std::string &cloudId, const std::string &value, const std::string &key); 76 int32_t GetExtAttr(const std::string &cloudId, std::string &value, int32_t &position); 77 int32_t GetExtAttrValue(const std::string &cloudId, const std::string &key, std::string &value); 78 int32_t GetRecycleInfo(std::shared_ptr<NativeRdb::Transaction> transaction, 79 const std::string &cloudId, int64_t &rowId, int32_t &position, std::string &attr); 80 int32_t GetSourcePath(const std::string &attr, const std::string &parentCloudId, std::string &sourcePath); 81 int32_t SourcePathSetValue(const std::string &cloudId, const std::string &attr, NativeRdb::ValuesBucket &setXattr); 82 int32_t HandleRecycleXattr(const std::string &name, const std::string &parentCloudId, const std::string &cloudId); 83 int32_t HandleRestoreXattr(const std::string &name, const std::string &parentCloudId, const std::string &cloudId); 84 85 /* clouddisk syncer */ 86 int32_t GetDirtyType(const std::string &cloudId, int32_t &fileStatus); 87 int32_t GetCurNode(const std::string &cloudId, CacheNode &curNode); 88 int32_t GetParentNode(const std::string parentCloudId, std::string &nextCloudId, std::string &fileName); 89 int32_t GetUriFromDB(const std::string &parentCloudId, std::string &uri); 90 int32_t GetNotifyUri(const CacheNode &cacheNode, std::string &uri); 91 int32_t GetNotifyData(const CacheNode &cacheNode, NotifyData ¬ifyData); 92 int32_t CheckRootIdValid(); 93 94 void DatabaseRestore(); 95 96 static const int32_t BATCH_LIMIT_SIZE = 500; 97 98 private: 99 void Stop(); 100 int32_t UnlinkSynced(const std::string &cloudId); 101 int32_t UnlinkLocal(const std::string &cloudId); 102 int32_t ReBuildDatabase(const std::string &databasePath); 103 104 std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 105 NativeRdb::RdbStoreConfig config_{""}; 106 const int32_t CONNECT_SIZE = 8; 107 std::string bundleName_; 108 int32_t userId_{0}; 109 std::string tableName_ = FileColumn::FILES_TABLE; 110 std::mutex rdbMutex_; 111 std::mutex backupMutex_; 112 std::string rootId_; 113 static std::map<char, bool> illegalCharacter; 114 }; 115 116 class CloudDiskDataCallBack : public NativeRdb::RdbOpenCallback { 117 public: 118 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 119 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 120 }; 121 122 #define RDBPTR_IS_NULLPTR(rdbStore_) \ 123 do { \ 124 if ((rdbStore_) == nullptr) { \ 125 LOGE("rdbStore_ is nullptr"); \ 126 return E_RDB; \ 127 } \ 128 } while (0) 129 130 #define CLOUDID_IS_NULL(cloudId) \ 131 do { \ 132 if ((cloudId).empty()) { \ 133 LOGE("cloudId is null"); \ 134 return E_INVAL_ARG; \ 135 } \ 136 } while (0) 137 } // namespace CloudDisk 138 } // namespace FileManagement 139 } // namespace OHOS 140 141 #endif // OHOS_CLOUD_DISK_SERVICE_RDBSTORE_H