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 #include "cloud_file_utils.h"
16 #include <ctime>
17 #include <fcntl.h>
18 #include <filesystem>
19 #include <sys/xattr.h>
20 #include <sys/ioctl.h>
21 #include <unistd.h>
22 #include "utils_log.h"
23 namespace OHOS {
24 namespace FileManagement {
25 namespace CloudDisk {
26 using namespace std;
27 namespace {
28     static const string LOCAL_PATH_DATA_SERVICE_EL2 = "/data/service/el2/";
29     static const string LOCAL_PATH_HMDFS_CLOUD_DATA = "/hmdfs/cloud/data/";
30     static const string CLOUD_FILE_CLOUD_ID_XATTR = "user.cloud.cloudid";
31     static const uint32_t CLOUD_ID_MIN_SIZE = 3;
32     static const uint32_t CLOUD_ID_BUCKET_MID_TIMES = 2;
33     static const uint32_t CLOUD_ID_BUCKET_MAX_SIZE = 32;
34     static const int64_t SECOND_TO_MILLISECOND = 1e3;
35     static const int64_t MILLISECOND_TO_NANOSECOND = 1e6;
36     static const uint64_t DELTA_DISK = 0x9E3779B9;
37     static const uint64_t HMDFS_HASH_COL_BIT_DISK = (0x1ULL) << 63;
38 }
39 
40 constexpr unsigned HMDFS_IOC = 0xf2;
41 constexpr unsigned WRITEOPEN_CMD = 0x02;
42 #define HMDFS_IOC_GET_WRITEOPEN_CNT _IOR(HMDFS_IOC, WRITEOPEN_CMD, uint32_t)
43 const string CloudFileUtils::TMP_SUFFIX = ".temp.download";
44 
IsDotDotdot(const std::string & name)45 bool CloudFileUtils::IsDotDotdot(const std::string &name)
46 {
47     return name == "." || name == "..";
48 }
49 
Str2HashBuf(const char * msg,size_t len,uint32_t * buf,int num)50 void CloudFileUtils::Str2HashBuf(const char *msg, size_t len, uint32_t *buf, int num)
51 {
52 }
53 
TeaTransform(uint32_t buf[4],uint32_t const in[])54 void CloudFileUtils::TeaTransform(uint32_t buf[4], uint32_t const in[])
55 {
56 }
57 
DentryHash(const std::string & inputStr)58 uint32_t CloudFileUtils::DentryHash(const std::string &inputStr)
59 {
60     return -1;
61 }
62 
GetBucketId(string cloudId)63 uint32_t CloudFileUtils::GetBucketId(string cloudId)
64 {
65     return 0;
66 }
67 
Timespec2Milliseconds(const struct timespec & time)68 int64_t CloudFileUtils::Timespec2Milliseconds(const struct timespec &time)
69 {
70     return 0;
71 }
72 
GetLocalBucketPath(string cloudId,string bundleName,int32_t userId)73 string CloudFileUtils::GetLocalBucketPath(string cloudId, string bundleName, int32_t userId)
74 {
75     return "";
76 }
77 
GetLocalFilePath(string cloudId,string bundleName,int32_t userId)78 string CloudFileUtils::GetLocalFilePath(string cloudId, string bundleName, int32_t userId)
79 {
80     if (userId == 0) {
81         return "mock";
82     }
83     return "";
84 }
85 
GetPathWithoutTmp(const string & path)86 string CloudFileUtils::GetPathWithoutTmp(const string &path)
87 {
88     return "";
89 }
90 
EndsWith(const string & fullString,const string & ending)91 bool CloudFileUtils::EndsWith(const string &fullString, const string &ending)
92 {
93     return false;
94 }
95 
GetCloudId(const string & path)96 string CloudFileUtils::GetCloudId(const string &path)
97 {
98     return "";
99 }
100 
CheckIsCloud(const string & key)101 bool CloudFileUtils::CheckIsCloud(const string &key)
102 {
103     return key == CLOUD_CLOUD_ID_XATTR;
104 }
105 
CheckIsCloudLocation(const string & key)106 bool CloudFileUtils::CheckIsCloudLocation(const string &key)
107 {
108     return key == CLOUD_FILE_LOCATION;
109 }
110 
CheckIsHmdfsPermission(const string & key)111 bool CloudFileUtils::CheckIsHmdfsPermission(const string &key)
112 {
113     return key == HMDFS_PERMISSION_XATTR;
114 }
115 
CheckIsCloudRecycle(const string & key)116 bool CloudFileUtils::CheckIsCloudRecycle(const string &key)
117 {
118     return key == CLOUD_CLOUD_RECYCLE_XATTR;
119 }
120 
CheckIsFavorite(const string & key)121 bool CloudFileUtils::CheckIsFavorite(const string &key)
122 {
123     return key == IS_FAVORITE_XATTR;
124 }
125 
CheckFileStatus(const string & key)126 bool CloudFileUtils::CheckFileStatus(const string &key)
127 {
128     return key == IS_FILE_STATUS_XATTR;
129 }
130 
LocalWriteOpen(const string & dfsPath)131 bool CloudFileUtils::LocalWriteOpen(const string &dfsPath)
132 {
133     return false;
134 }
135 } // namespace CloudDisk
136 } // namespace FileManagement
137 } // namespace OHOS