• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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     bool succ = false;
33     int fd;
34     tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
35     if (!succ) {
36         UniError(EINVAL).ThrowErr(env, "Invalid fd");
37         return { false, -1, -1, -1 };
38     }
39     int offset;
40     tie(succ, offset) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
41     if (!succ) {
42         UniError(EINVAL).ThrowErr(env, "Invalid offset");
43         return { false, -1, -1, -1 };
44     }
45     int len;
46     tie(succ, len) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
47     if (!succ) {
48         UniError(EINVAL).ThrowErr(env, "Invalid len");
49         return { false, -1, -1, -1 };
50     }
51     return { succ, 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     bool succ = false;
62     int fd;
63     int offset;
64     int len;
65     tie(succ, fd, offset, len) = GetPosixFallocateArg(env, funcArg);
66     if (!succ) {
67         return nullptr;
68     }
69     int ret = posix_fallocate(fd, offset, len);
70     if (ret == -1) {
71         UniError(errno).ThrowErr(env);
72         return nullptr;
73     }
74     return NVal::CreateUndefined(env).val_;
75 }
76 
Async(napi_env env,napi_callback_info info)77 napi_value PosixFallocate::Async(napi_env env, napi_callback_info info)
78 {
79     NFuncArg funcArg(env, info);
80     if (!funcArg.InitArgs(NARG_CNT::THREE, NARG_CNT::FOUR)) {
81         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
82         return nullptr;
83     }
84     bool succ = false;
85     int fd;
86     int offset;
87     int len;
88     tie(succ, fd, offset, len) = GetPosixFallocateArg(env, funcArg);
89     if (!succ) {
90         return nullptr;
91     }
92     auto cbExec = [fd, offset, len](napi_env env) -> UniError {
93         if (posix_fallocate(fd, offset, len) == -1) {
94             return UniError(errno);
95         } else {
96             return UniError(ERRNO_NOERR);
97         }
98     };
99     auto cbCompl = [](napi_env env, UniError err) -> NVal {
100         if (err) {
101             return { env, err.GetNapiErr(env) };
102         }
103         return { NVal::CreateUndefined(env) };
104     };
105     NVal thisVar(env, funcArg.GetThisVar());
106     string procedureName = "fileioPosixFallocate";
107     size_t argc = funcArg.GetArgc();
108     if (argc == NARG_CNT::THREE) {
109         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
110     } else {
111         NVal cb(env, funcArg[NARG_POS::FOURTH]);
112         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
113     }
114     return NVal::CreateUndefined(env).val_;
115 }
116 } // namespace ModuleFileIO
117 } // namespace DistributedFS
118 } // namespace OHOS