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 #include "lseek.h"
16
17 #include <cstring>
18 #include <tuple>
19 #include <unistd.h>
20
21 #include "n_async_work_callback.h"
22 #include "n_async_work_promise.h"
23 #include "n_func_arg.h"
24
25 namespace OHOS {
26 namespace DistributedFS {
27 namespace ModuleFileIO {
28 using namespace std;
29
GetLseekArg(napi_env env,const NFuncArg & funcArg)30 static tuple<bool, int, int, int> GetLseekArg(napi_env env, const NFuncArg &funcArg)
31 {
32 auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
33 if (!resGetFirstArg) {
34 UniError(EINVAL).ThrowErr(env, "Invalid fd");
35 return { false, -1, -1, -1 };
36 }
37
38 auto [resGetSecondArg, offset] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
39 if (!resGetSecondArg) {
40 UniError(EINVAL).ThrowErr(env, "Invalid offset");
41 return { false, -1, -1, -1 };
42 }
43
44 auto [resGetThirdArg, whence] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
45 if (!resGetThirdArg) {
46 UniError(EINVAL).ThrowErr(env, "Invalid whence");
47 return { false, -1, -1, -1 };
48 }
49
50 return { true, fd, offset, whence };
51 }
52
Sync(napi_env env,napi_callback_info info)53 napi_value Lseek::Sync(napi_env env, napi_callback_info info)
54 {
55 NFuncArg funcArg(env, info);
56 if (!funcArg.InitArgs(NARG_CNT::THREE)) {
57 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
58 return nullptr;
59 }
60
61 auto [resGetLseekArg, fd, offset, whence] = GetLseekArg(env, funcArg);
62 if (!resGetLseekArg) {
63 return nullptr;
64 }
65
66 int ret = lseek(fd, offset, whence);
67 if (ret == -1) {
68 UniError(errno).ThrowErr(env);
69 return nullptr;
70 }
71
72 return NVal::CreateInt64(env, ret).val_;
73 }
74
Async(napi_env env,napi_callback_info info)75 napi_value Lseek::Async(napi_env env, napi_callback_info info)
76 {
77 NFuncArg funcArg(env, info);
78 if (!funcArg.InitArgs(NARG_CNT::THREE, NARG_CNT::FOUR)) {
79 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
80 return nullptr;
81 }
82
83 auto [resGetLseekArg, fd, offset, whence] = GetLseekArg(env, funcArg);
84 if (!resGetLseekArg) {
85 return nullptr;
86 }
87
88 auto arg = make_shared<int32_t>();
89 auto cbExec = [fd = fd, offset = offset, whence = whence, arg](napi_env env) -> UniError {
90 int ret = lseek(fd, offset, whence);
91 *arg = ret;
92 if (ret == -1) {
93 return UniError(errno);
94 } else {
95 return UniError(ERRNO_NOERR);
96 }
97 };
98
99 auto cbComplCallback = [arg](napi_env env, UniError err) -> NVal {
100 if (err) {
101 return { env, err.GetNapiErr(env) };
102 }
103 return { NVal::CreateInt64(env, *arg) };
104 };
105
106 size_t argc = funcArg.GetArgc();
107 const string procedureName = "FileIOLseek";
108 NVal thisVar(env, funcArg.GetThisVar());
109 if (argc == NARG_CNT::THREE) {
110 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
111 } else {
112 NVal cb(env, funcArg[NARG_POS::FOURTH]);
113 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
114 }
115 }
116 } // namespace ModuleFileIO
117 } // namespace DistributedFS
118 } // namespace OHOS