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); 50 int32_t Create(const std::string &cloudId, const std::string &parentCloudId, 51 const std::string &fileName); 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 GetRowIdAndPosition(std::shared_ptr<NativeRdb::Transaction> transaction, 63 const std::string &cloudId, int64_t &rowId, int32_t &position); 64 int32_t LocationSetXattr(const std::string &name, const std::string &parentCloudId, 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 GetHasChild(const std::string &cloudId, bool &hasChild); 72 int32_t GetRowId(const std::string &cloudId, int64_t &rowId); 73 int32_t GetParentCloudId(const std::string &cloudId, std::string &parentCloudId); 74 int32_t ExtAttributeSetXattr(const std::string &cloudId, const std::string &value, const std::string &key); 75 int32_t GetExtAttr(const std::string &cloudId, std::string &value, int32_t &position); 76 int32_t GetExtAttrValue(const std::string &cloudId, const std::string &key, std::string &value); 77 78 /* clouddisk syncer */ 79 int32_t GetDirtyType(const std::string &cloudId, int32_t &fileStatus); 80 int32_t GetCurNode(const std::string &cloudId, CacheNode &curNode); 81 int32_t GetParentNode(const std::string parentCloudId, std::string &nextCloudId, std::string &fileName); 82 int32_t GetUriFromDB(const std::string &parentCloudId, std::string &uri); 83 int32_t GetNotifyUri(const CacheNode &cacheNode, std::string &uri); 84 int32_t GetNotifyData(const CacheNode &cacheNode, NotifyData ¬ifyData); 85 int32_t CheckRootIdValid(); 86 87 void DatabaseRestore(); 88 89 static const int32_t BATCH_LIMIT_SIZE = 500; 90 91 private: 92 void Stop(); 93 int32_t UnlinkSynced(const std::string &cloudId); 94 int32_t UnlinkLocal(const std::string &cloudId); 95 int32_t ReBuildDatabase(const std::string &databasePath); 96 97 std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 98 NativeRdb::RdbStoreConfig config_{""}; 99 const int32_t CONNECT_SIZE = 8; 100 std::string bundleName_; 101 int32_t userId_{0}; 102 std::string tableName_ = FileColumn::FILES_TABLE; 103 std::mutex rdbMutex_; 104 std::mutex backupMutex_; 105 std::string rootId_; 106 static std::map<char, bool> illegalCharacter; 107 }; 108 109 class CloudDiskDataCallBack : public NativeRdb::RdbOpenCallback { 110 public: 111 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 112 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 113 }; 114 115 #define RDBPTR_IS_NULLPTR(rdbStore_) \ 116 do { \ 117 if ((rdbStore_) == nullptr) { \ 118 LOGE("rdbStore_ is nullptr"); \ 119 return E_RDB; \ 120 } \ 121 } while (0) 122 123 #define CLOUDID_IS_NULL(cloudId) \ 124 do { \ 125 if ((cloudId).empty()) { \ 126 LOGE("cloudId is null"); \ 127 return E_INVAL_ARG; \ 128 } \ 129 } while (0) 130 } // namespace CloudDisk 131 } // namespace FileManagement 132 } // namespace OHOS 133 134 #endif // OHOS_CLOUD_DISK_SERVICE_RDBSTORE_H