• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "open.h"
17 
18 #include <cstring>
19 #include <fcntl.h>
20 #include <tuple>
21 #include <unistd.h>
22 
23 #include "common_func.h"
24 #include "n_async/n_async_work_callback.h"
25 #include "n_async/n_async_work_promise.h"
26 #include "n_func_arg.h"
27 #include "remote_uri.h"
28 
29 namespace OHOS {
30 namespace DistributedFS {
31 namespace ModuleFileIO {
32 using namespace std;
33 
Sync(napi_env env,napi_callback_info info)34 napi_value Open::Sync(napi_env env, napi_callback_info info)
35 {
36     NFuncArg funcArg(env, info);
37     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) {
38         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
39         return nullptr;
40     }
41 
42     bool succ = false;
43     unique_ptr<char[]> path = nullptr;
44     tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
45     if (!succ) {
46         UniError(EINVAL).ThrowErr(env, "Invalid path");
47         return nullptr;
48     }
49 
50     unsigned int flags = O_RDONLY;
51     if (funcArg.GetArgc() >= NARG_CNT::TWO) {
52         auto [succGetFlags, authFlags] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(O_RDONLY);
53         if (!succGetFlags || authFlags < 0) {
54             UniError(EINVAL).ThrowErr(env, "Invalid flags");
55             return nullptr;
56         }
57         flags = static_cast<unsigned int>(authFlags);
58         (void)CommonFunc::ConvertJsFlags(flags);
59     }
60 
61     int fd = -1;
62     if (ModuleRemoteUri::RemoteUri::IsRemoteUri(path.get(), fd, flags)) {
63         return NVal::CreateInt64(env, fd).val_;
64     }
65 
66     int32_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
67     if (funcArg.GetArgc() != NARG_CNT::THREE) {
68         size_t flagsFirst { flags };
69         if ((flagsFirst & O_CREAT) || (flagsFirst & O_TMPFILE)) {
70             UniError(EINVAL).ThrowErr(env, "called with O_CREAT/O_TMPFILE but no mode");
71             return nullptr;
72         }
73     } else {
74         tie(succ, mode) = NVal(env, funcArg.GetArg(NARG_POS::THIRD)).ToInt32(mode);
75         if (!succ) {
76             UniError(EINVAL).ThrowErr(env, "Invalid mode");
77             return nullptr;
78         }
79     }
80     fd = open(path.get(), flags, mode);
81     if (fd == -1) {
82         if (errno == ENAMETOOLONG) {
83             UniError(errno).ThrowErr(env, "Filename too long");
84             return nullptr;
85         }
86         UniError(errno).ThrowErr(env);
87         return nullptr;
88     }
89 
90     return NVal::CreateInt64(env, fd).val_;
91 }
92 
DoOpenExec(const std::string & path,const unsigned int flags,const int32_t mode,shared_ptr<int32_t> arg)93 static UniError DoOpenExec(const std::string& path, const unsigned int flags, const int32_t mode,
94     shared_ptr<int32_t> arg)
95 {
96     int fd = -1;
97     if (!ModuleRemoteUri::RemoteUri::IsRemoteUri(path, fd, flags)) {
98         fd = open(path.c_str(), flags, mode);
99     }
100     *arg = fd;
101     if (fd == -1) {
102         return UniError(errno);
103     } else {
104         return UniError(ERRNO_NOERR);
105     }
106 }
107 
ParseFlags(napi_env env,const NFuncArg & funcArg,unsigned int & flags)108 static bool ParseFlags(napi_env env, const NFuncArg& funcArg, unsigned int& flags)
109 {
110     auto [succ, authFlags] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(O_RDONLY);
111     if (!succ || authFlags < 0) {
112         UniError(EINVAL).ThrowErr(env, "Invalid flags");
113         return false;
114     }
115     flags = static_cast<unsigned int>(authFlags);
116     (void)CommonFunc::ConvertJsFlags(flags);
117     return true;
118 }
119 
Async(napi_env env,napi_callback_info info)120 napi_value Open::Async(napi_env env, napi_callback_info info)
121 {
122     NFuncArg funcArg(env, info);
123     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::FOUR)) {
124         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
125         return nullptr;
126     }
127     auto [succ, path, unuse] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
128     if (!succ) {
129         UniError(EINVAL).ThrowErr(env, "Invalid path");
130         return nullptr;
131     }
132     size_t argc = funcArg.GetArgc();
133     unsigned int flags = O_RDONLY;
134     if (argc >= NARG_CNT::TWO) {
135         if (!ParseFlags(env, funcArg, flags)) {
136             return nullptr;
137         }
138     }
139     int32_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
140     if (argc >= NARG_CNT::THREE) {
141         tie(succ, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(mode);
142         if (!succ) {
143             UniError(EINVAL).ThrowErr(env, "Invalid mode");
144             return nullptr;
145         }
146     }
147     auto arg = make_shared<int32_t>();
148     auto cbExec = [path = string(path.get()), flags, mode, arg](napi_env env) -> UniError {
149         return DoOpenExec(path, flags, mode, arg);
150     };
151     auto cbComplCallback = [arg](napi_env env, UniError err) -> NVal {
152         if (err) {
153             if (err.GetErrno(ERR_CODE_SYSTEM_POSIX) == ENAMETOOLONG) {
154                 return {env, err.GetNapiErr(env, "Filename too long")};
155             }
156             return { env, err.GetNapiErr(env) };
157         }
158         return { NVal::CreateInt64(env, *arg) };
159     };
160     NVal thisVar(env, funcArg.GetThisVar());
161     if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO &&
162         !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) || (argc == NARG_CNT::THREE &&
163         !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function))) {
164         return NAsyncWorkPromise(env, thisVar).Schedule("FileIOOpen", cbExec, cbComplCallback).val_;
165     } else {
166         NVal cb(env, funcArg[argc - 1]);
167         return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOOpen", cbExec, cbComplCallback).val_;
168     }
169 }
170 } // namespace ModuleFileIO
171 } // namespace DistributedFS
172 } // namespace OHOS