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 
18 namespace OHOS {
19 namespace CJSystemapi {
20 
ConvertCjFlags(unsigned int & flags)21 unsigned int CommonFunc::ConvertCjFlags(unsigned int &flags)
22 {
23     // default value is usrReadOnly 00
24     unsigned int flagsABI = 0;
25     flagsABI |= ((flags & USR_WRITE_ONLY) == USR_WRITE_ONLY) ? WRONLY : 0;
26     flagsABI |= ((flags & USR_RDWR) == USR_RDWR) ? RDWR : 0;
27     flagsABI |= ((flags & USR_CREATE) == USR_CREATE) ? CREATE : 0;
28     flagsABI |= ((flags & USR_TRUNC) == USR_TRUNC) ? TRUNC : 0;
29     flagsABI |= ((flags & USR_APPEND) == USR_APPEND) ? APPEND : 0;
30     flagsABI |= ((flags & USR_NONBLOCK) == USR_NONBLOCK) ? NONBLOCK : 0;
31     flagsABI |= ((flags & USR_DIRECTORY) == USR_DIRECTORY) ? DIRECTORY : 0;
32     flagsABI |= ((flags & USR_NOFOLLOW) == USR_NOFOLLOW) ? NOFOLLOW : 0;
33     flagsABI |= ((flags & USR_SYNC) == USR_SYNC) ? SYNC : 0;
34     flags = flagsABI;
35     return flagsABI;
36 }
37 using namespace std;
38 
FsReqCleanup(uv_fs_t * req)39 void CommonFunc::FsReqCleanup(uv_fs_t* req)
40 {
41     if (req) {
42         uv_fs_req_cleanup(req);
43         delete req;
44         req = nullptr;
45     }
46 }
47 
GetModeFromFlags(unsigned int flags)48 string CommonFunc::GetModeFromFlags(unsigned int flags)
49 {
50     const string readMode = "r";
51     const string writeMode = "w";
52     const string appendMode = "a";
53     const string truncMode = "t";
54     string mode = readMode;
55     mode += (((flags & O_RDWR) == O_RDWR) ? writeMode : "");
56     mode = (((flags & O_WRONLY) == O_WRONLY) ? writeMode : mode);
57     if (mode != readMode) {
58         mode += ((flags & O_TRUNC) ? truncMode : "");
59         mode += ((flags & O_APPEND) ? appendMode : "");
60     }
61     return mode;
62 }
63 
64 }
65 }