• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "fs_utils.h"
17 #include "filemgmt_libhilog.h"
18 
19 namespace OHOS::FileManagement::ModuleFileIO {
20 using namespace std;
21 
22 namespace {
23 const vector<string> PUBLIC_DIR_PATHS = { "/Documents" };
24 }
25 
GetActualLen(size_t bufLen,size_t bufOff,const optional<int64_t> & length)26 tuple<bool, size_t> FsUtils::GetActualLen(size_t bufLen, size_t bufOff, const optional<int64_t> &length)
27 {
28     if (bufLen < bufOff) {
29         HILOGE(
30             "Illegal parameter value: bufLen (%{public}zu) cannot be less than bufOff (%{public}zu)", bufLen, bufOff);
31         return { false, 0 };
32     }
33 
34     size_t retLen = bufLen - bufOff;
35 
36     if (length.has_value()) {
37         int64_t opLength = length.value();
38         if (opLength < 0 || static_cast<size_t>(opLength) > retLen) {
39             HILOGE("Invalid option.length: option.length=%{public}" PRId64 ", retLen=%{public}zu", opLength, retLen);
40             return { false, 0 };
41         }
42         retLen = static_cast<size_t>(opLength);
43     }
44     return { true, retLen };
45 }
46 
ConvertFlags(const uint32_t & flags)47 uint32_t FsUtils::ConvertFlags(const uint32_t &flags)
48 {
49     // default value is usrReadOnly 00
50     uint32_t flagsABI = 0;
51     flagsABI |= ((flags & USR_WRITE_ONLY) == USR_WRITE_ONLY) ? WRONLY : 0;
52     flagsABI |= ((flags & USR_RDWR) == USR_RDWR) ? RDWR : 0;
53     flagsABI |= ((flags & USR_CREATE) == USR_CREATE) ? CREATE : 0;
54     flagsABI |= ((flags & USR_TRUNC) == USR_TRUNC) ? TRUNC : 0;
55     flagsABI |= ((flags & USR_APPEND) == USR_APPEND) ? APPEND : 0;
56     flagsABI |= ((flags & USR_NONBLOCK) == USR_NONBLOCK) ? NONBLOCK : 0;
57     flagsABI |= ((flags & USR_DIRECTORY) == USR_DIRECTORY) ? DIRECTORY : 0;
58     flagsABI |= ((flags & USR_NOFOLLOW) == USR_NOFOLLOW) ? NOFOLLOW : 0;
59     flagsABI |= ((flags & USR_SYNC) == USR_SYNC) ? SYNC : 0;
60     return flagsABI;
61 }
62 
FsReqCleanup(uv_fs_t * req)63 void FsUtils::FsReqCleanup(uv_fs_t *req)
64 {
65     uv_fs_req_cleanup(req);
66     if (req) {
67         delete req;
68         req = nullptr;
69     }
70 }
71 
GetModeFromFlags(const uint32_t & flags)72 string FsUtils::GetModeFromFlags(const uint32_t &flags)
73 {
74     const string readMode = "r";
75     const string writeMode = "w";
76     const string appendMode = "a";
77     const string truncMode = "t";
78     string mode = readMode;
79     mode += (((flags & O_RDWR) == O_RDWR) ? writeMode : "");
80     mode = (((flags & O_WRONLY) == O_WRONLY) ? writeMode : mode);
81     if (mode != readMode) {
82         mode += ((flags & O_TRUNC) ? truncMode : "");
83         mode += ((flags & O_APPEND) ? appendMode : "");
84     }
85     return mode;
86 }
87 
CheckPublicDirPath(const string & sandboxPath)88 bool FsUtils::CheckPublicDirPath(const string &sandboxPath)
89 {
90     for (const string &path : PUBLIC_DIR_PATHS) {
91         if (sandboxPath.find(path) == 0) {
92             return true;
93         }
94     }
95     return false;
96 }
97 
Decode(const string & uri)98 string FsUtils::Decode(const string &uri)
99 {
100     ostringstream outPutStream;
101     const int32_t encodeLen = 2;
102     size_t index = 0;
103     while (index < uri.length()) {
104         if (uri[index] == '%') {
105             int hex = 0;
106             istringstream inputStream(uri.substr(index + 1, encodeLen));
107             inputStream >> hex >> hex;
108             outPutStream << static_cast<char>(hex);
109             index += encodeLen + 1;
110         } else {
111             outPutStream << uri[index];
112             index++;
113         }
114     }
115 
116     return outPutStream.str();
117 }
118 
119 } // namespace OHOS::FileManagement::ModuleFileIO
120