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 int32_t InsertCopyData(std::string srcCloudId, std::string destCloudId, std::string destParentCloudId, 94 std::shared_ptr<NativeRdb::ResultSet> resultSet); 95 int32_t CopyFile(std::string srcCloudId, std::string destCloudId, std::string bundleName, 96 const int userId, std::string destPath); 97 int32_t CreateDentryFile(MetaBase metaBase, std::string destParentCloudId); 98 99 void DatabaseRestore(); 100 101 static const int32_t BATCH_LIMIT_SIZE = 500; 102 103 private: 104 void Stop(); 105 int32_t UnlinkSynced(const std::string &cloudId); 106 int32_t UnlinkLocal(const std::string &cloudId); 107 int32_t ReBuildDatabase(const std::string &databasePath); 108 int32_t CheckIsConflict(const std::string &name, const std::string &parentCloudId, std::string &newName); 109 int32_t RestoreUpdateRdb(const std::string &cloudId, const struct RestoreInfo &restoreInfo, 110 const NativeRdb::ValuesBucket &setXattr); 111 112 std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 113 NativeRdb::RdbStoreConfig config_{""}; 114 const int32_t CONNECT_SIZE = 8; 115 std::string bundleName_; 116 int32_t userId_{0}; 117 std::string tableName_ = FileColumn::FILES_TABLE; 118 std::mutex rdbMutex_; 119 std::mutex backupMutex_; 120 std::string rootId_; 121 static std::map<char, bool> illegalCharacter; 122 }; 123 124 class CloudDiskDataCallBack : public NativeRdb::RdbOpenCallback { 125 public: 126 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 127 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 128 }; 129 130 #define RDBPTR_IS_NULLPTR(rdbStore_) \ 131 do { \ 132 if ((rdbStore_) == nullptr) { \ 133 LOGE("rdbStore_ is nullptr"); \ 134 return E_RDB; \ 135 } \ 136 } while (0) 137 138 #define CLOUDID_IS_NULL(cloudId) \ 139 do { \ 140 if ((cloudId).empty()) { \ 141 LOGE("cloudId is null"); \ 142 return E_INVAL_ARG; \ 143 } \ 144 } while (0) 145 } // namespace CloudDisk 146 } // namespace FileManagement 147 } // namespace OHOS 148 149 #endif // OHOS_CLOUD_DISK_SERVICE_RDBSTORE_H