/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "truncate.h" #include #include #include namespace OHOS { namespace DistributedFS { namespace ModuleFileIO { using namespace std; napi_value Truncate::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; } auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!resGetFirstArg) { UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } int ret = -1; if (funcArg.GetArgc() == NARG_CNT::ONE) { ret = truncate(path.get(), 0); } else { auto [resGetSecondArg, len] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); if (!resGetSecondArg) { UniError(EINVAL).ThrowErr(env, "Invalid len"); } ret = truncate(path.get(), len); } if (ret == -1) { UniError(errno).ThrowErr(env); return nullptr; } return NVal::CreateUndefined(env).val_; } napi_value Truncate::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; } auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!resGetFirstArg) { UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } int len = 0; size_t argc = funcArg.GetArgc(); if (argc > NARG_CNT::ONE) { auto [resGetSecondArg, length] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); if (!resGetSecondArg) { UniError(EINVAL).ThrowErr(env, "Invalid len"); return nullptr; } len = length; } auto cbExec = [path = string(path.get()), len](napi_env env) -> UniError { int ret = truncate(path.c_str(), len); if (ret == -1) { return UniError(errno); } else { return UniError(ERRNO_NOERR); } }; auto cbCompl = [](napi_env env, UniError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } else { return NVal::CreateUndefined(env); } }; NVal thisVar(env, funcArg.GetThisVar()); const string procedureName = "FileIOTruncate"; if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; } else { NVal cb(env, funcArg[NARG_POS::THIRD]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; } } } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS