• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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]).ToUTF8String();
36     if (!resGetFirstArg) {
37         UniError(EINVAL).ThrowErr(env, "Invalid path");
38         return nullptr;
39     }
40 
41     int ret = -1;
42     if (funcArg.GetArgc() == NARG_CNT::ONE) {
43         ret = truncate(path.get(), 0);
44     } else {
45         auto [resGetSecondArg, len] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
46         if (!resGetSecondArg) {
47             UniError(EINVAL).ThrowErr(env, "Invalid len");
48         }
49         ret = truncate(path.get(), len);
50     }
51 
52     if (ret == -1) {
53         UniError(errno).ThrowErr(env);
54         return nullptr;
55     }
56 
57     return NVal::CreateUndefined(env).val_;
58 }
59 
Async(napi_env env,napi_callback_info info)60 napi_value Truncate::Async(napi_env env, napi_callback_info info)
61 {
62     NFuncArg funcArg(env, info);
63     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) {
64         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
65         return nullptr;
66     }
67 
68     auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
69     if (!resGetFirstArg) {
70         UniError(EINVAL).ThrowErr(env, "Invalid path");
71         return nullptr;
72     }
73 
74     int len = 0;
75     size_t argc = funcArg.GetArgc();
76     if (argc > NARG_CNT::ONE) {
77         auto [resGetSecondArg, length] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
78         if (!resGetSecondArg) {
79             UniError(EINVAL).ThrowErr(env, "Invalid len");
80             return nullptr;
81         }
82         len = length;
83     }
84 
85     auto cbExec = [path = string(path.get()), len](napi_env env) -> UniError {
86         int ret = truncate(path.c_str(), len);
87         if (ret == -1) {
88             return UniError(errno);
89         } else {
90             return UniError(ERRNO_NOERR);
91         }
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     const 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
114