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