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