• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, bool newFileNoNeedUpload);
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, int32_t &dirtyType);
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, int32_t &dirtyType);
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 &notifyData);
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     int32_t GetSrcCloudId(const std::string &cloudId, std::string &srcCloudId);
99     void TriggerSyncForWrite(const std::string &fileName, const std::string &parentCloudId);
100     int32_t HandleRenameValue(NativeRdb::ValuesBucket &rename, int32_t position, uint8_t noNeedUpload,
101         const CacheNode &oldNode, const CacheNode &newNode);
102     int32_t UpdateTHMStatus(std::shared_ptr<CloudDiskMetaFile> metaFile, MetaBase &metabase, int32_t status);
103 
104     void DatabaseRestore();
105 
106     static const int32_t BATCH_LIMIT_SIZE = 500;
107 
108 private:
109     void Stop();
110     int32_t UnlinkSynced(const std::string &cloudId);
111     int32_t UnlinkLocal(const std::string &cloudId);
112     int32_t ReBuildDatabase(const std::string &databasePath);
113     int32_t CheckIsConflict(const std::string &name, const std::string &parentCloudId, std::string &newName);
114     int32_t RestoreUpdateRdb(const std::string &cloudId, const struct RestoreInfo &restoreInfo,
115         const NativeRdb::ValuesBucket &setXattr);
116 
117     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
118     NativeRdb::RdbStoreConfig config_{""};
119     const int32_t CONNECT_SIZE = 8;
120     std::string bundleName_;
121     int32_t userId_{0};
122     std::string tableName_ = FileColumn::FILES_TABLE;
123     std::mutex rdbMutex_;
124     std::mutex backupMutex_;
125     std::string rootId_;
126     static std::map<char, bool> illegalCharacter;
127 };
128 
129 class CloudDiskDataCallBack : public NativeRdb::RdbOpenCallback {
130 public:
131     int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
132     int32_t OnUpgradeExtend(NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion);
133     int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
134 };
135 
136 #define RDBPTR_IS_NULLPTR(rdbStore_)    \
137     do {    \
138         if ((rdbStore_) == nullptr) {    \
139             LOGE("rdbStore_ is nullptr");    \
140             return E_RDB;    \
141         }    \
142     } while (0)
143 
144 #define CLOUDID_IS_NULL(cloudId)    \
145     do {    \
146         if ((cloudId).empty()) {    \
147             LOGE("cloudId is null");    \
148             return E_INVAL_ARG;    \
149         }    \
150     } while (0)
151 } // namespace CloudDisk
152 } // namespace FileManagement
153 } // namespace OHOS
154 
155 #endif // OHOS_CLOUD_DISK_SERVICE_RDBSTORE_H