• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "utils.h"
17 #include "cj_common_ffi.h"
18 #include "macro.h"
19 #include "uni_error.h"
20 
21 namespace OHOS {
22 namespace CJSystemapi {
23 
ConvertCjFlags(unsigned int & flags)24 unsigned int CommonFunc::ConvertCjFlags(unsigned int &flags)
25 {
26     // default value is usrReadOnly 00
27     unsigned int flagsABI = 0;
28     flagsABI |= ((flags & USR_WRITE_ONLY) == USR_WRITE_ONLY) ? WRONLY : 0;
29     flagsABI |= ((flags & USR_RDWR) == USR_RDWR) ? RDWR : 0;
30     flagsABI |= ((flags & USR_CREATE) == USR_CREATE) ? CREATE : 0;
31     flagsABI |= ((flags & USR_TRUNC) == USR_TRUNC) ? TRUNC : 0;
32     flagsABI |= ((flags & USR_APPEND) == USR_APPEND) ? APPEND : 0;
33     flagsABI |= ((flags & USR_NONBLOCK) == USR_NONBLOCK) ? NONBLOCK : 0;
34     flagsABI |= ((flags & USR_DIRECTORY) == USR_DIRECTORY) ? DIRECTORY : 0;
35     flagsABI |= ((flags & USR_NOFOLLOW) == USR_NOFOLLOW) ? NOFOLLOW : 0;
36     flagsABI |= ((flags & USR_SYNC) == USR_SYNC) ? SYNC : 0;
37     flags = flagsABI;
38     return flagsABI;
39 }
40 using namespace std;
41 
FsReqCleanup(uv_fs_t * req)42 void CommonFunc::FsReqCleanup(uv_fs_t* req)
43 {
44     if (req) {
45         uv_fs_req_cleanup(req);
46         delete req;
47         req = nullptr;
48     }
49 }
50 
GetModeFromFlags(unsigned int flags)51 string CommonFunc::GetModeFromFlags(unsigned int flags)
52 {
53     const string readMode = "r";
54     const string writeMode = "w";
55     const string appendMode = "a";
56     const string truncMode = "t";
57     string mode = readMode;
58     mode += (((flags & O_RDWR) == O_RDWR) ? writeMode : "");
59     mode = (((flags & O_WRONLY) == O_WRONLY) ? writeMode : mode);
60     if (mode != readMode) {
61         mode += ((flags & O_TRUNC) ? truncMode : "");
62         mode += ((flags & O_APPEND) ? appendMode : "");
63     }
64     return mode;
65 }
66 
GetWriteArg(void * buffer,int64_t length,int64_t offset,const std::string & encode)67 std::tuple<int, void *, size_t, int64_t> CommonFunc::GetWriteArg(
68     void *buffer, int64_t length, int64_t offset, const std::string& encode)
69 {
70     if (buffer == nullptr) {
71         return { EINVAL, nullptr, 0, offset };
72     }
73     if (length > UINT_MAX) {
74         LOGE("The Size of buffer is too large");
75         return { EINVAL, nullptr, 0, offset };
76     }
77     if (offset < 0) {
78         LOGE("option.offset shall be positive number");
79         return { EINVAL, nullptr, 0, offset };
80     }
81     return { SUCCESS_CODE, buffer, length, offset };
82 }
83 
84 }
85 }