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