diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c0e9db88b19213b96c1ec9e78643484cd60fe3a --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 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 + +#include "../common_func.h" +#include "filemgmt_libhilog.h" +#include "uv.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJsArg) +{ + auto [isPath, path, ignore] = NVal(env, pathOrFdFromJsArg).ToUTF8String(); + if (isPath) { + return { true, FileInfo { true, move(path), {} } }; + } + auto [isFd, fd] = NVal(env, pathOrFdFromJsArg).ToInt32(); + if (!isFd || fd < 0) { + HILOGE("Invalid fd"); + NError(EINVAL).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } + return { true, FileInfo { false, {}, { fd, false } } }; +}; + +napi_value Truncate::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); + if (!succ) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + int truncateLen = 0; + if (funcArg.GetArgc() == NARG_CNT::TWO) { + tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(); + if (!succ) { + HILOGE("Invalid truncate length"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + } + if (fileInfo.isPath) { + std::unique_ptr open_req = { new uv_fs_t, uv_fs_req_cleanup }; + int ret = uv_fs_open(nullptr, open_req.get(), fileInfo.path.get(), O_RDWR, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + if (ret < 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + std::unique_ptr ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; + ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), open_req.get()->result, truncateLen, nullptr); + if (ret < 0) { + HILOGE("Failed to truncate file by path"); + NError(errno).ThrowErr(env); + return nullptr; + } + } else { + std::unique_ptr ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; + int ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), fileInfo.fdg.GetFD(), truncateLen, nullptr); + if (ret < 0) { + HILOGE("Failed to truncate file by fd"); + NError(EINVAL).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)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); + if (!succ) { + return nullptr; + } + int truncateLen = 0; + if (funcArg.GetArgc() > NARG_CNT::ONE && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) { + tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(); + if (!succ) { + HILOGE("Invalid truncate length"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + } + auto cbExec = [fileInfo = make_shared(move(fileInfo)), truncateLen, env = env]() -> NError { + using uv_fs_unique_ptr_type = std::unique_ptr; + if (fileInfo->isPath) { + uv_fs_unique_ptr_type open_req = { new uv_fs_t, uv_fs_req_cleanup }; + int ret = uv_fs_open(uv_default_loop(), open_req.get(), fileInfo->path.get(), O_RDWR, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + if (ret < 0) { + return NError(errno); + } + uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; + ret = uv_fs_ftruncate(uv_default_loop(), ftruncate_req.get(), open_req.get()->result, truncateLen, nullptr); + if (ret < 0) { + HILOGE("Failed to truncate file by path"); + return NError(errno); + } + } else { + uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, uv_fs_req_cleanup }; + int ret = uv_fs_ftruncate(uv_default_loop(), ftruncate_req.get(), fileInfo->fdg.GetFD(), truncateLen, nullptr); + if (ret < 0) { + HILOGE("Failed to truncate file by fd"); + return NError(errno); + } + } + return NError(ERRNO_NOERR); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return NVal::CreateUndefined(env); + } + }; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE || + (funcArg.GetArgc() == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; + } else { + if (funcArg.GetArgc() == NARG_CNT::ONE) { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; + } + } +} +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.h b/interfaces/kits/js/src/mod_fs/properties/truncate.h new file mode 100644 index 0000000000000000000000000000000000000000..8b0930870748954a1994665277e4a115ec9e4006 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 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. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_TRUNCATE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_TRUNCATE_H + +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::ModuleFileIO { +class Truncate final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); + static napi_value Async(napi_env env, napi_callback_info info); +}; +const std::string PROCEDURE_TRUNCATE_NAME = "FileIOTruncate"; +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_TRUNCATE_H \ No newline at end of file