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