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