1 /*
2  * Copyright (c) 2023 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 #ifndef CLOUD_FILE_DAEMON_CLOUD_DISK_INODE_H
16 #define CLOUD_FILE_DAEMON_CLOUD_DISK_INODE_H
17 
18 #include <atomic>
19 #include <memory>
20 #include <mutex>
21 #include <shared_mutex>
22 #include <string>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <unordered_map>
27 
28 #include "cloud_asset_read_session.h"
29 #include "file_operations_base.h"
30 
31 namespace OHOS {
32 namespace FileManagement {
33 namespace CloudDisk {
34 
35 enum CLOUD_DISK_INODE_LAYER {
36     CLOUD_DISK_INODE_ZERO_LAYER = 0, // data
37     CLOUD_DISK_INODE_FIRST_LAYER,    // bundleName
38     CLOUD_DISK_INODE_OTHER_LAYER     // others
39 };
40 
41 enum CLOUD_DISK_INODE_LAYER_LOCALID {
42     CLOUD_DISK_INODE_LAYER_LOCALID_UNKNOWN = 0, // placeholder
43     CLOUD_DISK_INODE_ROOT_LAYER_LOCALID,        // /
44     CLOUD_DISK_INODE_ZERO_LAYER_LOCALID,        // data
45     CLOUD_DISK_INODE_FIRST_LAYER_LOCALID        // bundleName
46 };
47 
48 enum CLOUD_DISK_FILE_DIRTY {
49     CLOUD_DISK_FILE_UNKNOWN = 0,
50     CLOUD_DISK_FILE_CREATE,
51     CLOUD_DISK_FILE_WRITE
52 };
53 
54 enum CLOUD_DISK_FILE_TYPE {
55     CLOUD_DISK_FILE_TYPE_UNKNOWN = 0,
56     CLOUD_DISK_FILE_TYPE_LOCAL,
57     CLOUD_DISK_FILE_TYPE_CLOUD
58 };
59 
60 struct CloudDiskInode {
61     int layer{CLOUD_DISK_INODE_ZERO_LAYER};
62     struct stat stat;
63     std::string cloudId{"rootId"};
64     std::string bundleName;
65     std::string fileName;
66     fuse_ino_t parent{0};
67     std::atomic<int> refCount{0};
68     std::string path; // just used in local file operation
69 
70     /* ops means file operation that uses local or database */
71     std::shared_ptr<FileOperationsBase> ops{nullptr};
72 };
73 
74 struct CloudDiskFile {
75     int type{CLOUD_DISK_FILE_TYPE_UNKNOWN};
76     int fileDirty{CLOUD_DISK_FILE_UNKNOWN};
77     int32_t fd{-1};
78     std::atomic<int> refCount{0};
79     std::shared_ptr<CloudFile::CloudAssetReadSession> readSession{nullptr};
80     bool isWriteOpen{false};
81 };
82 
83 struct CloudDiskFuseData {
84     int userId;
85     int64_t bundleNameId{1};
86     int64_t fileId{0};
87     std::shared_ptr<CloudDiskInode> rootNode{nullptr};
88     std::unordered_map<int64_t, std::shared_ptr<CloudDiskInode>> inodeCache;
89     std::unordered_map<int64_t, std::shared_ptr<CloudDiskFile>> fileCache;
90     std::unordered_map<std::string, int64_t> localIdCache;
91     std::shared_mutex bundleNameIdLock;
92     std::shared_mutex cacheLock;
93     std::shared_mutex fileLock;
94     std::shared_mutex fileIdLock;
95     std::shared_mutex localIdLock;
96     struct fuse_session *se;
97 };
98 } // namespace CloudDisk
99 } // namespace FileManagement
100 } // namespace OHOS
101 #endif // CLOUD_FILE_DAEMON_CLOUD_DISK_INODE_H
102