• 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 #include "meta_file.h"
17 
18 #include <fcntl.h>
19 #include <iomanip>
20 #include <sstream>
21 #include <sys/stat.h>
22 
23 #include "dfs_error.h"
24 #include "directory_ex.h"
25 #include "file_utils.h"
26 #include "securec.h"
27 #include "string_ex.h"
28 #include "sys/xattr.h"
29 #include "utils_log.h"
30 
31 namespace OHOS {
32 namespace FileManagement {
33 constexpr uint32_t DENTRYGROUP_SIZE = 4096;
34 constexpr uint32_t DENTRY_NAME_LEN = 8;
35 constexpr uint32_t DENTRY_RESERVED_LENGTH = 3;
36 constexpr uint32_t DENTRY_PER_GROUP = 60;
37 constexpr uint32_t DENTRY_BITMAP_LENGTH = 8;
38 constexpr uint32_t DENTRY_GROUP_RESERVED = 7;
39 constexpr uint32_t CLOUD_RECORD_ID_LEN = 33;
40 constexpr uint32_t DENTRYGROUP_HEADER = 4096;
41 constexpr uint32_t MAX_BUCKET_LEVEL = 63;
42 constexpr uint32_t BUCKET_BLOCKS = 2;
43 constexpr uint32_t BITS_PER_BYTE = 8;
44 constexpr uint32_t HMDFS_SLOT_LEN_BITS = 3;
45 constexpr uint64_t DELTA = 0x9E3779B9; /* Hashing code copied from f2fs */
46 constexpr uint64_t HMDFS_HASH_COL_BIT = (0x1ULL) << 63;
47 constexpr uint32_t DIR_SIZE = 4096;
48 
49 #pragma pack(push, 1)
50 struct HmdfsDentry {
51     uint32_t hash{0};
52     uint16_t mode{0};
53     uint16_t namelen{0};
54     uint64_t size{0};
55     uint64_t mtime{0};
56     uint8_t recordId[CLOUD_RECORD_ID_LEN]{0};
57     /* reserved bytes for long term extend, total 60 bytes */
58     union {
59         struct {
60             uint8_t fileType : 2;
61         };
62         uint8_t reserved[DENTRY_RESERVED_LENGTH];
63     };
64 };
65 
66 struct HmdfsDentryGroup {
67     uint8_t dentryVersion;
68     uint8_t bitmap[DENTRY_BITMAP_LENGTH];
69     HmdfsDentry nsl[DENTRY_PER_GROUP];
70     uint8_t fileName[DENTRY_PER_GROUP][DENTRY_NAME_LEN];
71     uint8_t reserved[DENTRY_GROUP_RESERVED];
72 };
73 static_assert(sizeof(HmdfsDentryGroup) == DENTRYGROUP_SIZE);
74 
75 struct HmdfsDcacheHeader {
76     uint64_t dcacheCrtime{0};
77     uint64_t dcacheCrtimeNsec{0};
78 
79     uint64_t dentryCtime{0};
80     uint64_t dentryCtimeNsec{0};
81 
82     uint64_t dentryCount{0};
83 };
84 #pragma pack(pop)
85 
PathHash(const std::string & path,bool caseSense)86 static uint64_t PathHash(const std::string &path, bool caseSense)
87 {
88     uint64_t res = 0;
89     const char *kp = path.c_str();
90 
91     while (*kp) {
92         char c = *kp;
93         if (!caseSense) {
94             c = tolower(c);
95         }
96         res = (res << 5) - res + static_cast<uint64_t>(c); /* hash shift width 5 */
97         kp++;
98     }
99     return res;
100 }
101 
GetDentryfileName(const std::string & path,bool caseSense)102 static std::string GetDentryfileName(const std::string &path, bool caseSense)
103 {
104     // path should be like "/", "/dir/", "/dir/dir/" ...
105     constexpr uint32_t fileNameLen = 32;
106     char buf[fileNameLen + 1] = {0};
107     uint64_t fileHash = PathHash(path, caseSense);
108     int ret = snprintf_s(buf, fileNameLen + 1, fileNameLen, "cloud_%016llx", fileHash);
109     if (ret < 0) {
110         LOGE("filename failer fileHash ret :%{public}d", ret);
111     }
112     return buf;
113 }
114 
GetDentryfileByPath(uint32_t userId,const std::string & path,bool caseSense=false)115 static std::string GetDentryfileByPath(uint32_t userId, const std::string &path, bool caseSense = false)
116 {
117     std::string cacheDir =
118         "/data/service/el2/" + std::to_string(userId) + "/hmdfs/cache/account_cache/dentry_cache/cloud/";
119     ForceCreateDirectory(cacheDir);
120     std::string dentryFileName = GetDentryfileName(path, caseSense);
121 
122     return cacheDir + dentryFileName;
123 }
124 
GetParentDir(const std::string & path)125 std::string MetaFile::GetParentDir(const std::string &path)
126 {
127     if ((path == "/") || (path == "")) {
128         return "";
129     }
130 
131     auto pos = path.find_last_of('/');
132     if ((pos == std::string::npos) || (pos == 0)) {
133         return "/";
134     }
135 
136     return path.substr(0, pos);
137 }
138 
GetFileName(const std::string & path)139 std::string MetaFile::GetFileName(const std::string &path)
140 {
141     if ((path == "/") || (path == "")) {
142         return "";
143     }
144 
145     auto pos = path.find_last_of('/');
146     if (pos == std::string::npos) {
147         return "";
148     }
149 
150     return path.substr(pos + 1);
151 }
152 
GetParentMetaFile(uint32_t userId,const std::string & path)153 static std::shared_ptr<MetaFile> GetParentMetaFile(uint32_t userId, const std::string &path)
154 {
155     std::string parentPath = MetaFile::GetParentDir(path);
156     if (parentPath == "") {
157         return nullptr;
158     }
159 
160     return MetaFileMgr::GetInstance().GetMetaFile(userId, parentPath);
161 }
162 
MetaFile(uint32_t userId,const std::string & path)163 MetaFile::MetaFile(uint32_t userId, const std::string &path)
164 {
165     path_ = path;
166     cacheFile_ = GetDentryfileByPath(userId, path);
167     fd_ = UniqueFd{open(cacheFile_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)};
168     LOGD("fd=%{public}d, errno :%{public}d", fd_.Get(), errno);
169 
170     int ret = fsetxattr(fd_, "user.hmdfs_cache", path.c_str(), path.size(), 0);
171     if (ret != 0) {
172         LOGE("setxattr failed, errno %{public}d, cacheFile_ %s", errno, cacheFile_.c_str());
173     }
174 
175     HmdfsDcacheHeader header{};
176     (void)FileUtils::ReadFile(fd_, 0, sizeof(header), &header);
177     dentryCount_ = header.dentryCount;
178 
179     /* lookup and create in parent */
180     parentMetaFile_ = GetParentMetaFile(userId, path);
181     std::string dirName = GetFileName(path);
182     if ((parentMetaFile_ == nullptr) || (dirName == "")) {
183         return;
184     }
185     MetaBase m(dirName, std::to_string(PathHash(path, false)));
186     ret = parentMetaFile_->DoLookup(m);
187     if (ret != E_OK) {
188         m.mode = S_IFDIR;
189         m.size = DIR_SIZE;
190         ret = parentMetaFile_->DoCreate(m);
191         if (ret != E_OK) {
192             LOGE("create parent failed, ret %{public}d", ret);
193         }
194     }
195 }
196 
~MetaFile()197 MetaFile::~MetaFile()
198 {
199     if ((dentryCount_ == 0) && (path_ != "/")) {
200         unlink(cacheFile_.c_str());
201         if (parentMetaFile_) {
202             std::string dirName = GetFileName(path_);
203             if (dirName == "") {
204                 return;
205             }
206             MetaBase m(dirName, std::to_string(PathHash(path_, false)));
207             parentMetaFile_->DoRemove(m);
208         }
209         return;
210     }
211 
212     HmdfsDcacheHeader header{.dentryCount = dentryCount_};
213     (void)FileUtils::WriteFile(fd_, &header, 0, sizeof(header));
214 }
215 
IsDotDotdot(const std::string & name)216 static bool IsDotDotdot(const std::string &name)
217 {
218     return name == "." || name == "..";
219 }
220 
Str2HashBuf(const char * msg,size_t len,uint32_t * buf,int num)221 static void Str2HashBuf(const char *msg, size_t len, uint32_t *buf, int num)
222 {
223     uint32_t pad = static_cast<uint32_t>(len) | (static_cast<uint32_t>(len) << 8);
224     pad |= pad << 16; /* hash pad length 16 */
225 
226     uint32_t val = pad;
227     len = std::min(len, static_cast<size_t>(num * sizeof(int)));
228     for (uint32_t i = 0; i < len; i++) {
229         if ((i % sizeof(int)) == 0) {
230             val = pad;
231         }
232         uint8_t c = static_cast<uint8_t>(tolower(msg[i]));
233         val = c + (val << 8); /* hash shift size 8 */
234         if ((i % 4) == 3) {   /* msg size 4, shift when 3 */
235             *buf++ = val;
236             val = pad;
237             num--;
238         }
239     }
240     if (--num >= 0) {
241         *buf++ = val;
242     }
243     while (--num >= 0) {
244         *buf++ = pad;
245     }
246 }
247 
TeaTransform(uint32_t buf[4],uint32_t const in[])248 static void TeaTransform(uint32_t buf[4], uint32_t const in[])
249 {
250     int n = 16;           /* transform total rounds 16 */
251     uint32_t a = in[0];   /* transform input pos 0 */
252     uint32_t b = in[1];   /* transform input pos 1 */
253     uint32_t c = in[2];   /* transform input pos 2 */
254     uint32_t d = in[3];   /* transform input pos 3 */
255     uint32_t b0 = buf[0]; /* buf pos 0 */
256     uint32_t b1 = buf[1]; /* buf pos 1 */
257     uint32_t sum = 0;
258 
259     do {
260         sum += DELTA;
261         b0 += ((b1 << 4) + a) ^ (b1 + sum) ^ ((b1 >> 5) + b); /* tea transform width 4 and 5 */
262         b1 += ((b0 << 4) + c) ^ (b0 + sum) ^ ((b0 >> 5) + d); /* tea transform width 4 and 5 */
263     } while (--n);
264 
265     buf[0] += b0;
266     buf[1] += b1;
267 }
268 
DentryHash(const std::string & name)269 static uint32_t DentryHash(const std::string &name)
270 {
271     if (IsDotDotdot(name)) {
272         return 0;
273     }
274 
275     constexpr int inLen = 8;      /* hash input buf size 8 */
276     constexpr int bufLen = 4;     /* hash output buf size 4 */
277     uint32_t in[inLen], buf[bufLen];
278     auto len = name.length();
279     constexpr decltype(len) hashWidth = 16; /* hash operation width 4 */
280     const char *p = name.c_str();
281 
282     buf[0] = 0x67452301; /* hash magic 1 */
283     buf[1] = 0xefcdab89; /* hash magic 2 */
284     buf[2] = 0x98badcfe; /* hash magic 3 */
285     buf[3] = 0x10325476; /* hash magic 4 */
286 
287     while (true) {
288         Str2HashBuf(p, len, in, bufLen);
289         TeaTransform(buf, in);
290 
291         if (len <= hashWidth) {
292             break;
293         }
294 
295         p += hashWidth;
296         len -= hashWidth;
297     };
298     uint32_t hash = buf[0];
299     uint32_t hmdfsHash = hash & ~HMDFS_HASH_COL_BIT;
300 
301     return hmdfsHash;
302 }
303 
GetDentrySlots(size_t nameLen)304 static inline uint32_t GetDentrySlots(size_t nameLen)
305 {
306     return static_cast<uint32_t>((nameLen + BITS_PER_BYTE - 1) >> HMDFS_SLOT_LEN_BITS);
307 }
308 
GetDentryGroupPos(size_t bidx)309 static inline off_t GetDentryGroupPos(size_t bidx)
310 {
311     return bidx * DENTRYGROUP_SIZE + DENTRYGROUP_HEADER;
312 }
313 
GetDentryGroupCnt(uint64_t size)314 static inline uint64_t GetDentryGroupCnt(uint64_t size)
315 {
316     return (size >= DENTRYGROUP_HEADER) ? ((size - DENTRYGROUP_HEADER) / DENTRYGROUP_SIZE) : 0;
317 }
318 
GetOverallBucket(uint32_t level)319 static uint32_t GetOverallBucket(uint32_t level)
320 {
321     if (level >= MAX_BUCKET_LEVEL) {
322         LOGI("level = %{public}d overflow", level);
323         return 0;
324     }
325     uint64_t buckets = (1ULL << (level + 1)) - 1;
326     return static_cast<uint32_t>(buckets);
327 }
328 
GetDcacheFileSize(uint32_t level)329 static size_t GetDcacheFileSize(uint32_t level)
330 {
331     size_t buckets = GetOverallBucket(level);
332     return buckets * DENTRYGROUP_SIZE * BUCKET_BLOCKS + DENTRYGROUP_HEADER;
333 }
334 
GetBucketaddr(uint32_t level,uint32_t buckoffset)335 static uint32_t GetBucketaddr(uint32_t level, uint32_t buckoffset)
336 {
337     if (level >= MAX_BUCKET_LEVEL) {
338         return 0;
339     }
340 
341     uint64_t curLevelMaxBucks = (1ULL << level);
342     if (buckoffset >= curLevelMaxBucks) {
343         return 0;
344     }
345 
346     return static_cast<uint32_t>(curLevelMaxBucks) + buckoffset - 1;
347 }
348 
GetBucketByLevel(uint32_t level)349 static uint32_t GetBucketByLevel(uint32_t level)
350 {
351     if (level >= MAX_BUCKET_LEVEL) {
352         LOGI("level = %{public}d overflow", level);
353         return 0;
354     }
355 
356     uint64_t buckets = (1ULL << level);
357     return static_cast<uint32_t>(buckets);
358 }
359 
RoomForFilename(const uint8_t bitmap[],size_t slots,uint32_t maxSlots)360 static uint32_t RoomForFilename(const uint8_t bitmap[], size_t slots, uint32_t maxSlots)
361 {
362     uint32_t bitStart = 0;
363 
364     while (1) {
365         uint32_t zeroStart = BitOps::FindNextZeroBit(bitmap, maxSlots, bitStart);
366         if (zeroStart >= maxSlots) {
367             return maxSlots;
368         }
369 
370         uint32_t zeroEnd = BitOps::FindNextBit(bitmap, maxSlots, zeroStart);
371         if (zeroEnd - zeroStart >= slots) {
372             return zeroStart;
373         }
374 
375         bitStart = zeroEnd + 1;
376         if (zeroEnd + 1 >= maxSlots) {
377             return maxSlots;
378         }
379     }
380     return 0;
381 }
382 
UpdateDentry(HmdfsDentryGroup & d,const MetaBase & base,uint32_t nameHash,uint32_t bitPos)383 static void UpdateDentry(HmdfsDentryGroup &d, const MetaBase &base, uint32_t nameHash, uint32_t bitPos)
384 {
385     HmdfsDentry *de;
386     const std::string name = base.name;
387     uint32_t slots = GetDentrySlots(name.length());
388 
389     de = &d.nsl[bitPos];
390     de->hash = nameHash;
391     de->namelen = name.length();
392     if (memcpy_s(d.fileName[bitPos], slots * DENTRY_NAME_LEN, name.c_str(), name.length())) {
393         LOGE("memcpy_s failed, dstLen = %{public}d, srcLen = %{public}zu", slots * DENTRY_NAME_LEN, name.length());
394     }
395     de->mtime = base.mtime;
396     de->fileType = base.fileType;
397     de->size = base.size;
398     de->mode = base.mode;
399     if (memcpy_s(de->recordId, CLOUD_RECORD_ID_LEN, base.cloudId.c_str(), base.cloudId.length())) {
400         LOGE("memcpy_s failed, dstLen = %{public}d, srcLen = %{public}zu", CLOUD_RECORD_ID_LEN, base.cloudId.length());
401     }
402 
403     for (uint32_t i = 0; i < slots; i++) {
404         BitOps::SetBit(bitPos + i, d.bitmap);
405         if (i) {
406             (de + i)->namelen = 0;
407         }
408     }
409 }
410 
DoCreate(const MetaBase & base)411 int32_t MetaFile::DoCreate(const MetaBase &base)
412 {
413     if (fd_ < 0) {
414         LOGE("bad metafile fd");
415         return EINVAL;
416     }
417 
418     off_t pos = 0;
419     uint32_t level = 0;
420     uint32_t bitPos, namehash;
421     unsigned long bidx;
422     HmdfsDentryGroup dentryBlk = {0};
423 
424     std::unique_lock<std::mutex> lock(mtx_);
425     namehash = DentryHash(base.name);
426 
427     bool found = false;
428     while (!found) {
429         if (level == MAX_BUCKET_LEVEL) {
430             return ENOSPC;
431         }
432         bidx = BUCKET_BLOCKS * GetBucketaddr(level, namehash % GetBucketByLevel(level));
433         unsigned long endBlock = bidx + BUCKET_BLOCKS;
434 
435         struct stat fileStat;
436         int err = fstat(fd_, &fileStat);
437         if (err < 0) {
438             return EINVAL;
439         }
440         if ((endBlock > GetDentryGroupCnt(fileStat.st_size)) &&
441             ftruncate(fd_, GetDcacheFileSize(level))) {
442             return ENOENT;
443         }
444 
445         for (; bidx < endBlock; bidx++) {
446             pos = GetDentryGroupPos(bidx);
447             int size = FileUtils::ReadFile(fd_, pos, DENTRYGROUP_SIZE, &dentryBlk);
448             if (size != DENTRYGROUP_SIZE) {
449                 return ENOENT;
450             }
451             bitPos = RoomForFilename(dentryBlk.bitmap, GetDentrySlots(base.name.length()), DENTRY_PER_GROUP);
452             if (bitPos < DENTRY_PER_GROUP) {
453                 found = true;
454                 break;
455             }
456         }
457         ++level;
458     }
459 
460     pos = GetDentryGroupPos(bidx);
461     UpdateDentry(dentryBlk, base, namehash, bitPos);
462     int size = FileUtils::WriteFile(fd_, &dentryBlk, pos, DENTRYGROUP_SIZE);
463     if (size != DENTRYGROUP_SIZE) {
464         LOGI("WriteFile failed, size %{public}d != %{public}d", size, DENTRYGROUP_SIZE);
465         return EINVAL;
466     }
467 
468     ++dentryCount_;
469     return E_OK;
470 }
471 
472 struct DcacheLookupCtx {
473     int fd{-1};
474     std::string name{};
475     uint32_t hash{0};
476     uint32_t bidx{0};
477     std::unique_ptr<HmdfsDentryGroup> page{nullptr};
478 };
479 
InitDcacheLookupCtx(DcacheLookupCtx * ctx,const MetaBase & base,int fd)480 static void InitDcacheLookupCtx(DcacheLookupCtx *ctx, const MetaBase &base, int fd)
481 {
482     ctx->fd = fd;
483     ctx->name = base.name;
484     ctx->bidx = 0;
485     ctx->page = nullptr;
486     ctx->hash = DentryHash(ctx->name);
487 }
488 
FindDentryPage(uint64_t index,DcacheLookupCtx * ctx)489 static std::unique_ptr<HmdfsDentryGroup> FindDentryPage(uint64_t index, DcacheLookupCtx *ctx)
490 {
491     auto dentryBlk = std::make_unique<HmdfsDentryGroup>();
492 
493     off_t pos = GetDentryGroupPos(index);
494     ssize_t size = FileUtils::ReadFile(ctx->fd, pos, DENTRYGROUP_SIZE, dentryBlk.get());
495     if (size != DENTRYGROUP_SIZE) {
496         return nullptr;
497     }
498     return dentryBlk;
499 }
500 
FindInBlock(HmdfsDentryGroup & dentryBlk,uint32_t namehash,const std::string & name)501 static HmdfsDentry *FindInBlock(HmdfsDentryGroup &dentryBlk, uint32_t namehash, const std::string &name)
502 {
503     int maxLen = 0;
504     uint32_t bitPos = 0;
505     HmdfsDentry *de = nullptr;
506 
507     while (bitPos < DENTRY_PER_GROUP) {
508         if (!BitOps::TestBit(bitPos, dentryBlk.bitmap)) {
509             bitPos++;
510             maxLen++;
511             continue;
512         }
513         de = &dentryBlk.nsl[bitPos];
514         if (!de->namelen) {
515             bitPos++;
516             continue;
517         }
518 
519         if (de->hash == namehash && de->namelen == name.length() &&
520             !memcmp(name.c_str(), dentryBlk.fileName[bitPos], de->namelen)) {
521             return de;
522         }
523         maxLen = 0;
524         bitPos += GetDentrySlots(de->namelen);
525     }
526 
527     return nullptr;
528 }
529 
InLevel(uint32_t level,DcacheLookupCtx * ctx)530 static HmdfsDentry *InLevel(uint32_t level, DcacheLookupCtx *ctx)
531 {
532     HmdfsDentry *de = nullptr;
533 
534     uint32_t nbucket = GetBucketByLevel(level);
535     if (!nbucket) {
536         return de;
537     }
538 
539     uint32_t bidx = GetBucketaddr(level, ctx->hash % nbucket) * BUCKET_BLOCKS;
540     uint32_t endBlock = bidx + BUCKET_BLOCKS;
541 
542     for (; bidx < endBlock; bidx++) {
543         auto dentryBlk = FindDentryPage(bidx, ctx);
544         if (dentryBlk == nullptr) {
545             break;
546         }
547 
548         de = FindInBlock(*dentryBlk, ctx->hash, ctx->name);
549         if (de != nullptr) {
550             ctx->page = std::move(dentryBlk);
551             break;
552         }
553     }
554     ctx->bidx = bidx;
555     return de;
556 }
557 
FindDentry(DcacheLookupCtx * ctx)558 static HmdfsDentry *FindDentry(DcacheLookupCtx *ctx)
559 {
560     for (uint32_t level = 0; level < MAX_BUCKET_LEVEL; level++) {
561         HmdfsDentry *de = InLevel(level, ctx);
562         if (de != nullptr) {
563             return de;
564         }
565     }
566     return nullptr;
567 }
568 
DoRemove(const MetaBase & base)569 int32_t MetaFile::DoRemove(const MetaBase &base)
570 {
571     if (fd_ < 0) {
572         LOGE("bad metafile fd");
573         return EINVAL;
574     }
575 
576     std::unique_lock<std::mutex> lock(mtx_);
577     DcacheLookupCtx ctx;
578     InitDcacheLookupCtx(&ctx, base, fd_);
579     HmdfsDentry *de = FindDentry(&ctx);
580     if (de == nullptr) {
581         LOGE("find dentry failed");
582         return ENOENT;
583     }
584 
585     uint32_t bitPos = (de - ctx.page->nsl);
586     uint32_t slots = GetDentrySlots(de->namelen);
587     for (uint32_t i = 0; i < slots; i++) {
588         BitOps::ClearBit(bitPos + i, ctx.page->bitmap);
589     }
590 
591     off_t ipos = GetDentryGroupPos(ctx.bidx);
592     ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), ipos, sizeof(HmdfsDentryGroup));
593     if (size != sizeof(HmdfsDentryGroup)) {
594         LOGE("WriteFile failed!, ret = %{public}zd", size);
595         return EIO;
596     }
597 
598     --dentryCount_;
599     return E_OK;
600 }
601 
DoLookup(MetaBase & base)602 int32_t MetaFile::DoLookup(MetaBase &base)
603 {
604     if (fd_ < 0) {
605         LOGE("bad metafile fd");
606         return EINVAL;
607     }
608 
609     std::unique_lock<std::mutex> lock(mtx_);
610     struct DcacheLookupCtx ctx;
611     InitDcacheLookupCtx(&ctx, base, fd_);
612     struct HmdfsDentry *de = FindDentry(&ctx);
613     if (de == nullptr) {
614         LOGI("find dentry failed");
615         return ENOENT;
616     }
617 
618     base.size = de->size;
619     base.mtime = de->mtime;
620     base.mode = de->mode;
621     base.fileType = de->fileType;
622     base.cloudId = std::string(reinterpret_cast<const char *>(de->recordId), CLOUD_RECORD_ID_LEN - 1);
623 
624     return E_OK;
625 }
626 
DoUpdate(const MetaBase & base)627 int32_t MetaFile::DoUpdate(const MetaBase &base)
628 {
629     if (fd_ < 0) {
630         LOGE("bad metafile fd");
631         return EINVAL;
632     }
633 
634     std::unique_lock<std::mutex> lock(mtx_);
635     struct DcacheLookupCtx ctx;
636     InitDcacheLookupCtx(&ctx, base, fd_);
637     struct HmdfsDentry *de = FindDentry(&ctx);
638     if (de == nullptr) {
639         LOGI("find dentry failed");
640         return ENOENT;
641     }
642 
643     de->mtime = base.mtime;
644     de->size = base.size;
645     de->mode = base.mode;
646     de->fileType = base.fileType;
647     if (memcpy_s(de->recordId, CLOUD_RECORD_ID_LEN, base.cloudId.c_str(), base.cloudId.length())) {
648         LOGE("memcpy_s failed, dstLen = %{public}d, srcLen = %{public}zu", CLOUD_RECORD_ID_LEN, base.cloudId.length());
649     }
650 
651     off_t ipos = GetDentryGroupPos(ctx.bidx);
652     ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), sizeof(struct HmdfsDentryGroup), ipos);
653     if (size != sizeof(struct HmdfsDentryGroup)) {
654         LOGI("write failed, ret = %zd", size);
655         return EIO;
656     }
657     return E_OK;
658 }
659 
DoRename(const MetaBase & oldBase,const std::string & newName)660 int32_t MetaFile::DoRename(const MetaBase &oldBase, const std::string &newName)
661 {
662     MetaBase base{oldBase.name};
663     int32_t ret = DoLookup(base);
664     if (ret) {
665         LOGE("ret = %{public}d, lookup %s failed", ret, base.name.c_str());
666         return ret;
667     }
668 
669     base.name = newName;
670     ret = DoCreate(base);
671     if (ret) {
672         LOGE("ret = %{public}d, create %s failed", ret, base.name.c_str());
673         return ret;
674     }
675 
676     base.name = oldBase.name;
677     ret = DoRemove(oldBase);
678     if (ret) {
679         LOGE("ret = %{public}d, remove %s failed", ret, oldBase.name.c_str());
680         base.name = newName;
681         (void)DoRemove(base);
682         return ret;
683     }
684 
685     return E_OK;
686 }
687 
DecodeDentrys(const HmdfsDentryGroup & dentryGroup,std::vector<MetaBase> & bases)688 static int32_t DecodeDentrys(const HmdfsDentryGroup &dentryGroup, std::vector<MetaBase> &bases)
689 {
690     for (uint32_t i = 0; i < DENTRY_PER_GROUP; i++) {
691         int len = dentryGroup.nsl[i].namelen;
692         if (!BitOps::TestBit(i, dentryGroup.bitmap) || len == 0 || len >= PATH_MAX) {
693             continue;
694         }
695 
696         std::string name(reinterpret_cast<const char *>(dentryGroup.fileName[i]), len);
697 
698         MetaBase base(name);
699         base.mode = dentryGroup.nsl[i].mode;
700         base.mtime = dentryGroup.nsl[i].mtime;
701         base.size = dentryGroup.nsl[i].size;
702         base.fileType = dentryGroup.nsl[i].fileType;
703         base.cloudId = std::string(reinterpret_cast<const char *>(dentryGroup.nsl[i].recordId), CLOUD_RECORD_ID_LEN);
704         bases.emplace_back(base);
705     }
706     return 0;
707 }
708 
LoadChildren(std::vector<MetaBase> & bases)709 int32_t MetaFile::LoadChildren(std::vector<MetaBase> &bases)
710 {
711     if (fd_ < 0) {
712         LOGE("bad metafile fd");
713         return EINVAL;
714     }
715 
716     std::lock_guard<std::mutex> lock(mtx_);
717     struct stat fileStat;
718     int ret = fstat(fd_, &fileStat);
719     if (ret != E_OK) {
720         return EINVAL;
721     }
722 
723     uint64_t fileSize = static_cast<uint64_t>(fileStat.st_size);
724     uint64_t groupCnt = GetDentryGroupCnt(fileSize);
725     HmdfsDentryGroup dentryGroup;
726 
727     for (uint64_t i = 1; i < groupCnt + 1; i++) {
728         uint64_t off = i * sizeof(HmdfsDentryGroup);
729         FileUtils::ReadFile(fd_, off, sizeof(HmdfsDentryGroup), &dentryGroup);
730         DecodeDentrys(dentryGroup, bases);
731     }
732     return E_OK;
733 }
734 
GetInstance()735 MetaFileMgr& MetaFileMgr::GetInstance()
736 {
737     static MetaFileMgr instance_;
738     return instance_;
739 }
740 
GetMetaFile(uint32_t userId,const std::string & path)741 std::shared_ptr<MetaFile> MetaFileMgr::GetMetaFile(uint32_t userId, const std::string &path)
742 {
743     std::shared_ptr<MetaFile> mFile = nullptr;
744     std::lock_guard<std::recursive_mutex> lock(mtx_);
745 
746     if (metaFiles_.find({userId, path}) != metaFiles_.end()) {
747         mFile = metaFiles_[{userId, path}];
748     } else {
749         mFile = std::make_shared<MetaFile>(userId, path);
750         metaFiles_[{userId, path}] = mFile;
751     }
752     return mFile;
753 }
754 
ClearAll()755 void MetaFileMgr::ClearAll()
756 {
757     std::lock_guard<std::recursive_mutex> lock(mtx_);
758     metaFiles_.clear();
759 }
760 
RecordIdToCloudId(const std::string hexStr)761 std::string MetaFileMgr::RecordIdToCloudId(const std::string hexStr)
762 {
763     std::string result;
764     constexpr std::size_t offset = 2;
765     constexpr int changeBase = 16;
766     for (std::size_t i = 0; i < hexStr.length(); i += offset) {
767         std::string hexByte = hexStr.substr(i, offset);
768         char *endPtr;
769         unsigned long hexValue = std::strtoul(hexByte.c_str(), &endPtr, changeBase);
770 
771         if (endPtr != hexByte.c_str() + hexByte.length()) {
772             LOGE("Invalid hexadecimal string: %{public}s", hexStr.c_str());
773             return "";
774         }
775         result += static_cast<char>(hexValue);
776     }
777     if (result.size() > CLOUD_RECORD_ID_LEN) {
778         LOGE("Invalid result length %{public}zu", result.size());
779         return "";
780     }
781 
782     return result;
783 }
784 
CloudIdToRecordId(const std::string cloudId)785 std::string MetaFileMgr::CloudIdToRecordId(const std::string cloudId)
786 {
787     std::stringstream result;
788     constexpr int width = 2;
789     for (std::size_t i = 0; i < cloudId.length(); i++) {
790         uint8_t u8Byte = cloudId[i];
791         result << std::setw(width) << std::setfill('0') << std::hex << static_cast<int>(u8Byte);
792     }
793     return result.str();
794 }
795 
796 } // namespace FileManagement
797 } // namespace OHOS
798