• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef OHOS_FILEMGMT_DENTRY_META_FILE_H
17 #define OHOS_FILEMGMT_DENTRY_META_FILE_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <mutex>
22 #include <map>
23 #include <string>
24 #include <vector>
25 #include <sys/stat.h>
26 
27 #include "unique_fd.h"
28 
29 namespace OHOS {
30 namespace FileManagement {
31 
32 struct MetaBase;
33 class MetaFile {
34 public:
35     MetaFile() = delete;
36     ~MetaFile();
37     explicit MetaFile(uint32_t userId, const std::string &path);
38 
39     int32_t DoCreate(const MetaBase &base);
40     int32_t DoRemove(const MetaBase &base);
41     int32_t DoUpdate(const MetaBase &base);
42     int32_t DoRename(const MetaBase &oldBase, const std::string &newName);
43     int32_t DoLookup(MetaBase &base);
44     int32_t LoadChildren(std::vector<MetaBase> &bases);
45 
46     static std::string GetParentDir(const std::string &path);
47     static std::string GetFileName(const std::string &path);
48 
49 private:
50     std::mutex mtx_{};
51     std::string path_{};
52     std::string cacheFile_{};
53     UniqueFd fd_{};
54     uint64_t dentryCount_{0};
55     std::shared_ptr<MetaFile> parentMetaFile_{nullptr};
56 };
57 
58 enum {
59     FILE_TYPE_CONTENT = 0,
60     FILE_TYPE_THUMBNAIL,
61     FILE_TYPE_LCD,
62 };
63 
64 class MetaFileMgr {
65 public:
66     static MetaFileMgr& GetInstance();
67     /* recordId is hex string of 256 bits, convert to u8 cloudId[32] to kernel */
68     static std::string RecordIdToCloudId(const std::string hexStr);
69     static std::string CloudIdToRecordId(const std::string cloudId);
70     std::shared_ptr<MetaFile> GetMetaFile(uint32_t userId, const std::string &path);
71     void ClearAll();
72 private:
73     MetaFileMgr() = default;
74     ~MetaFileMgr() = default;
75     MetaFileMgr(const MetaFileMgr &m) = delete;
76     const MetaFileMgr &operator=(const MetaFileMgr &m) = delete;
77 
78     std::recursive_mutex mtx_{};
79     std::map<std::pair<uint32_t, std::string>, std::shared_ptr<MetaFile>> metaFiles_;
80 };
81 
82 struct MetaBase {
MetaBaseMetaBase83     MetaBase(const std::string &name) : name(name) {}
MetaBaseMetaBase84     MetaBase(const std::string &name, const std::string &cloudId) : name(name), cloudId(cloudId) {}
85     MetaBase() = default;
86     uint64_t mtime{0};
87     uint64_t size{0};
88     uint32_t mode{S_IFREG};
89     uint32_t fileType{FILE_TYPE_CONTENT};
90     std::string name{};
91     std::string cloudId{};
92 };
93 
94 struct BitOps {
95     static const uint8_t BIT_PER_BYTE = 8;
TestBitBitOps96     static int TestBit(uint32_t nr, const uint8_t addr[])
97     {
98         return 1 & (addr[nr / BIT_PER_BYTE] >> (nr & (BIT_PER_BYTE - 1)));
99     }
100 
ClearBitBitOps101     static void ClearBit(uint32_t nr, uint8_t addr[])
102     {
103         addr[nr / BIT_PER_BYTE] &= ~(1UL << ((nr) % BIT_PER_BYTE));
104     }
105 
SetBitBitOps106     static void SetBit(uint32_t nr, uint8_t addr[])
107     {
108         addr[nr / BIT_PER_BYTE] |= (1UL << ((nr) % BIT_PER_BYTE));
109     }
110 
FindNextBitBitOps111     static uint32_t FindNextBit(const uint8_t addr[], uint32_t maxSlots, uint32_t start)
112     {
113         while (start < maxSlots) {
114             if (BitOps::TestBit(start, addr)) {
115                 return start;
116             }
117             start++;
118         }
119         return maxSlots;
120     }
121 
FindNextZeroBitBitOps122     static uint32_t FindNextZeroBit(const uint8_t addr[], uint32_t maxSlots, uint32_t start)
123     {
124         while (start < maxSlots) {
125             if (!BitOps::TestBit(start, addr)) {
126                 return start;
127             }
128             start++;
129         }
130         return maxSlots;
131     }
132 };
133 
134 } // namespace FileManagement
135 } // namespace OHOS
136 
137 #endif // META_FILE_H
138