1 /*
2  * Copyright (c) 2021-2022 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 #include <cstring>
17 #include <fcntl.h>
18 #include <tuple>
19 #include <unistd.h>
20 
21 #include "../../common/napi/n_async/n_async_work_callback.h"
22 #include "../../common/napi/n_async/n_async_work_promise.h"
23 #include "../../common/napi/n_func_arg.h"
24 
25 namespace OHOS {
26 namespace DistributedFS {
27 namespace ModuleFileIO {
28 using namespace std;
29 
GetPosixFallocateArg(napi_env env,const NFuncArg & funcArg)30 static tuple<bool, int, int, int> GetPosixFallocateArg(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, len] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
45     if (!resGetThirdArg) {
46         UniError(EINVAL).ThrowErr(env, "Invalid len");
47         return { false, -1, -1, -1 };
48     }
49 
50     return { true, fd, offset, len };
51 }
52 
Sync(napi_env env,napi_callback_info info)53 napi_value PosixFallocate::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 [resGetPosixFallocateArg, fd, offset, len] = GetPosixFallocateArg(env, funcArg);
62     if (!resGetPosixFallocateArg) {
63         return nullptr;
64     }
65 
66     int ret = posix_fallocate(fd, offset, len);
67     if (ret == -1) {
68         UniError(errno).ThrowErr(env);
69         return nullptr;
70     }
71 
72     return NVal::CreateUndefined(env).val_;
73 }
74 
Async(napi_env env,napi_callback_info info)75 napi_value PosixFallocate::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 [resGetPosixFallocateArg, fd, offset, len] = GetPosixFallocateArg(env, funcArg);
84     if (!resGetPosixFallocateArg) {
85         return nullptr;
86     }
87 
88     auto cbExec = [fd = fd, offset = offset, len = len](napi_env env) -> UniError {
89         if (posix_fallocate(fd, offset, len) == -1) {
90             return UniError(errno);
91         } else {
92             return UniError(ERRNO_NOERR);
93         }
94     };
95 
96     auto cbCompl = [](napi_env env, UniError err) -> NVal {
97         if (err) {
98             return { env, err.GetNapiErr(env) };
99         }
100         return { NVal::CreateUndefined(env) };
101     };
102 
103     NVal thisVar(env, funcArg.GetThisVar());
104     const string procedureName = "fileioPosixFallocate";
105     size_t argc = funcArg.GetArgc();
106     if (argc == NARG_CNT::THREE) {
107         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
108     } else {
109         NVal cb(env, funcArg[NARG_POS::FOURTH]);
110         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
111     }
112 
113     return NVal::CreateUndefined(env).val_;
114 }
115 } // namespace ModuleFileIO
116 } // namespace DistributedFS
117 } // namespace OHOS