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_FILE_UTILS_H
16 #define CLOUD_FILE_DAEMON_CLOUD_FILE_UTILS_H
17 
18 #include <string>
19 #include <sys/stat.h>
20 namespace OHOS {
21 namespace FileManagement {
22 namespace CloudDisk {
23 struct CloudDiskFileInfo {
24     std::string name;
25     std::string cloudId;
26     std::string parentCloudId;
27     uint32_t location{1};
28     uint32_t mode{S_IFREG};
29     int64_t localId{0};
30     unsigned long long rowId{0};
31     unsigned long long size{0};
32     unsigned long long atime{0};
33     unsigned long long ctime{0};
34     unsigned long long mtime{0};
35     bool IsDirectory{false};
36     off_t nextOff{0};
37 };
38 
39 namespace {
40     static const std::string CLOUD_CLOUD_ID_XATTR = "user.cloud.cloudid";
41     static const std::string CLOUD_FILE_LOCATION = "user.cloud.location";
42     static const std::string HMDFS_PERMISSION_XATTR = "user.hmdfs.perm";
43     static const std::string CLOUD_CLOUD_RECYCLE_XATTR = "user.cloud.recycle";
44     static const std::string IS_FAVORITE_XATTR = "user.cloud.favorite";
45     static const std::string IS_FILE_STATUS_XATTR = "user.cloud.filestatus";
46     static const std::string CLOUD_EXT_ATTR = "extended_attributes";
47 }
48 
49 class CloudFileUtils final {
50 public:
51     static bool CheckIsCloud(const std::string &key);
52     static bool CheckIsCloudLocation(const std::string &key);
53     static bool CheckIsHmdfsPermission(const std::string &key);
54     static bool CheckIsCloudRecycle(const std::string &key);
55     static bool CheckIsFavorite(const std::string &key);
56     static bool CheckFileStatus(const std::string &key);
57     static std::string GetLocalBucketPath(std::string cloudId, std::string bundleName,
58                                           int32_t userId);
59     static std::string GetLocalFilePath(std::string cloudId, std::string bundleName,
60                                         int32_t userId);
61     static std::string GetPathWithoutTmp(const std::string &path);
62     static std::string GetCloudId(const std::string &path);
63     static uint32_t GetBucketId(std::string cloudId);
64     static int64_t Timespec2Milliseconds(const struct timespec &time);
65     static bool LocalWriteOpen(const std::string &dfsPath);
66     static uint32_t DentryHash(const std::string &cloudId);
67     static void TeaTransform(uint32_t buf[4], uint32_t const in[]);
68     static void Str2HashBuf(const char *msg, size_t len, uint32_t *buf, int num);
69     static bool IsDotDotdot(const std::string &name);
70     static const std::string TMP_SUFFIX;
71 private:
72     static bool EndsWith(const std::string &fullString, const std::string &ending);
73 };
74 } // namespace CloudDisk
75 } // namespace FileManagement
76 } // namespace OHOS
77 #endif // CLOUD_FILE_DAEMON_CLOUD_FILE_UTILS_H
78