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 "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
31 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
32 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
33 return nullptr;
34 }
35
36 bool succ = false;
37 unique_ptr<char[]> path;
38 tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
39 if (!succ) {
40 UniError(EINVAL).ThrowErr(env, "Invalid path");
41 return nullptr;
42 }
43
44 int ret = -1;
45 if (funcArg.GetArgc() == NARG_CNT::ONE) {
46 ret = truncate(path.get(), 0);
47 } else {
48 int len;
49 tie(succ, len) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
50 if (!succ) {
51 UniError(EINVAL).ThrowErr(env, "Invalid len");
52 }
53 ret = truncate(path.get(), len);
54 }
55
56 if (ret == -1) {
57 UniError(errno).ThrowErr(env);
58 return nullptr;
59 }
60 return NVal::CreateUndefined(env).val_;
61 }
62
Async(napi_env env,napi_callback_info info)63 napi_value Truncate::Async(napi_env env, napi_callback_info info)
64 {
65 NFuncArg funcArg(env, info);
66 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) {
67 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
68 return nullptr;
69 }
70 bool succ = false;
71 unique_ptr<char[]> path;
72 int len = 0;
73 tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
74 if (!succ) {
75 UniError(EINVAL).ThrowErr(env, "Invalid path");
76 return nullptr;
77 }
78 size_t argc = funcArg.GetArgc();
79 if (argc > NARG_CNT::ONE) {
80 tie(succ, len) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
81 if (!succ) {
82 UniError(EINVAL).ThrowErr(env, "Invalid len");
83 return nullptr;
84 }
85 }
86 auto cbExec = [path = string(path.get()), len](napi_env env) -> UniError {
87 int ret = truncate(path.c_str(), len);
88 if (ret == -1) {
89 return UniError(errno);
90 } else {
91 return UniError(ERRNO_NOERR);
92 }
93 };
94 auto cbCompl = [](napi_env env, UniError err) -> NVal {
95 if (err) {
96 return { env, err.GetNapiErr(env) };
97 } else {
98 return NVal::CreateUndefined(env);
99 }
100 };
101
102 NVal thisVar(env, funcArg.GetThisVar());
103 string procedureName = "fileIOTruncate";
104 if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) {
105 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
106 } else {
107 NVal cb(env, funcArg[NARG_POS::THIRD]);
108 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
109 }
110 }
111 } // namespace ModuleFileIO
112 } // namespace DistributedFS
113 } // namespace OHOS