diff --git a/interfaces/kits/js/src/mod_fs/properties/close.cpp b/interfaces/kits/js/src/mod_fs/properties/close.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b7525c66016cf3c8e16d69a33185f88973d17bdd --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/close.cpp @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2023 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 "close.h" + +#include +#include +#include +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +static FileEntity *GetFileEntity(napi_env env, napi_value objFile) +{ + auto fileEntity = NClass::GetEntityOf(env, objFile); + if (!fileEntity) { + HILOGE("Failed to get file entity"); + return nullptr; + } + if (!fileEntity->fd_) { + HILOGE("The fd of rafEntity is not exist"); + return nullptr; + } + return fileEntity; +} + +static tuple ParseJsOperand(napi_env env, napi_value fdOrFileFromJsArg) +{ + auto [isFd, fd] = NVal(env, fdOrFileFromJsArg).ToInt32(); + if (isFd) { + return { true, FileStruct { true, fd, nullptr } }; + } + auto file = GetFileEntity(env, fdOrFileFromJsArg); + if (file) { + return { true, FileStruct { false, -1, file } }; + } + + return { false, FileStruct { false, -1, nullptr } }; +} + +napi_value Close::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [resGetFirstArg, fileStruct] = ParseJsOperand(env, funcArg[NARG_POS::FIRST]); + if (!resGetFirstArg) { + HILOGI("Failed to parse fd or FileEntity from JS parameter"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + if (fileStruct.isFd) { + std::unique_ptr close_req = { new uv_fs_t, uv_fs_req_cleanup }; + int ret = uv_fs_close(nullptr, close_req.get(), fileStruct.fd, nullptr); + if (ret < 0) { + HILOGE("Failed to close file with fd: %{public}d, ret: %{public}d", fileStruct.fd, ret); + NError(errno).ThrowErr(env); + return nullptr; + } + } else { + fileStruct.fileEntity->fd_.reset(); + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value Close::Async(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 [resGetFirstArg, fileStruct] = ParseJsOperand(env, funcArg[NARG_POS::FIRST]); + if (!resGetFirstArg) { + HILOGI("Failed to parse JS operand"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [fileStruct = fileStruct]() -> NError { + if (fileStruct.isFd) { + std::unique_ptr close_req = { new uv_fs_t, uv_fs_req_cleanup }; + int ret = uv_fs_close(nullptr, close_req.get(), fileStruct.fd, nullptr); + if (ret < 0) { + HILOGE("Failed to close file with ret: %{public}d", ret); + return NError(errno); + } + } else { + fileStruct.fileEntity->fd_.reset(); + } + return NError(ERRNO_NOERR); + }; + + auto cbComplete = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return NVal::CreateUndefined(env); + } + }; + + size_t argc = funcArg.GetArgc(); + NVal thisVar(env, funcArg.GetThisVar()); + if (argc == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_CLOSE_NAME, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_CLOSE_NAME, cbExec, cbComplete).val_; + } +} +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/close.h b/interfaces/kits/js/src/mod_fs/properties/close.h new file mode 100755 index 0000000000000000000000000000000000000000..8fa3bab4eec2d6ddde166f96980f4eefc8cb980e --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/close.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 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_CLOSE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CLOSE_H + +#include "class_file/file_entity.h" +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +class Close final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); + static napi_value Async(napi_env env, napi_callback_info info); +}; + +struct FileStruct { + bool isFd = false; + int fd; + FileEntity *fileEntity; +}; + +const std::string PROCEDURE_CLOSE_NAME = "FileIOClose"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CLOSE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/symlink.cpp b/interfaces/kits/js/src/mod_fs/properties/symlink.cpp old mode 100644 new mode 100755 index 9699168a4a3157115ff915ad24708368e2d0ba59..8412893c39b29128613634cbf5ac708a7603e988 --- a/interfaces/kits/js/src/mod_fs/properties/symlink.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/symlink.cpp @@ -1,114 +1,118 @@ -/* - * 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 "symlink.h" - -#include -#include -#include -#include - -#include "filemgmt_libhilog.h" - -namespace OHOS::FileManagement::ModuleFileIO { -using namespace std; -using namespace OHOS::FileManagement::LibN; - -static tuple GetSymlinkArg(napi_env env, const NFuncArg &funcArg) -{ - auto [resGetFirstArg, src, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!resGetFirstArg) { - HILOGE("Invalid src"); - NError(EINVAL).ThrowErr(env); - return { false, "", "" }; - } - - auto [resGetSecondArg, dest, useless] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); - if (!resGetSecondArg) { - HILOGE("Invalid dest"); - NError(EINVAL).ThrowErr(env); - return { false, "", "" }; - } - - return { true, src.get(), dest.get() }; -} - -napi_value Symlink::Sync(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::TWO)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto [resGetSymlinkArg, oldPath, newPath] = GetSymlinkArg(env, funcArg); - if (!resGetSymlinkArg) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - if (symlink(oldPath.c_str(), newPath.c_str()) < 0) { - HILOGE("Failed to create a link for old path: %{public}s", oldPath.c_str()); - NError(errno).ThrowErr(env); - return nullptr; - } - - return NVal::CreateUndefined(env).val_; -} - -napi_value Symlink::Async(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto [resGetSymlinkArg, oldPath, newPath] = GetSymlinkArg(env, funcArg); - if (!resGetSymlinkArg) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto cbExec = [oldPath = move(oldPath), newPath = move(newPath)]() -> NError { - int ret = symlink(oldPath.c_str(), newPath.c_str()); - if (ret < 0) { - HILOGE("Failed to create a link for old path: %{public}s, ret: %{public}d", oldPath.c_str(), ret); - return NError(errno); - } else { - return NError(ERRNO_NOERR); - } - }; - - auto cbComplCallback = [](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; - } - return { NVal::CreateUndefined(env) }; - }; - - const string procedureName = "FileIOSymLink"; - NVal thisVar(env, funcArg.GetThisVar()); - size_t argc = funcArg.GetArgc(); - if (argc == NARG_CNT::TWO) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_; - } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_; - } -} -} // namespace OHOS::FileManagement::ModuleFileIO +/* + * Copyright (c) 2022-2023 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 "symlink.h" + +#include +#include +#include +#include +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +static tuple GetSymlinkArg(napi_env env, const NFuncArg &funcArg) +{ + auto [resGetFirstArg, src, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + return { false, "", "" }; + } + + auto [resGetSecondArg, dest, useless] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + if (!resGetSecondArg) { + return { false, "", "" }; + } + + return { true, src.get(), dest.get() }; +} + +napi_value Symlink::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [resGetSymlinkArg, oldPath, newPath] = GetSymlinkArg(env, funcArg); + if (!resGetSymlinkArg) { + HILOGE("Failed to get symlink arguments"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + std::unique_ptr symlink_req = {new uv_fs_t, uv_fs_req_cleanup}; + int ret = uv_fs_symlink(nullptr, symlink_req.get(), oldPath.c_str(), newPath.c_str(), 0, nullptr); + if (ret < 0) { + HILOGE("Failed to create a link for old path"); + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value Symlink::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [resGetSymlinkArg, oldPath, newPath] = GetSymlinkArg(env, funcArg); + if (!resGetSymlinkArg) { + HILOGE("Failed to get symlink arguments"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [oldPath = move(oldPath), newPath = move(newPath)]() -> NError { + std::unique_ptr symlink_req = {new uv_fs_t, uv_fs_req_cleanup}; + int ret = uv_fs_symlink(nullptr, symlink_req.get(), oldPath.c_str(), newPath.c_str(), 0, nullptr); + if (ret < 0) { + HILOGE("Failed to create a link for old path"); + return NError(errno); + } + return NError(ERRNO_NOERR); + }; + + auto cbComplCallback = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + size_t argc = funcArg.GetArgc(); + if (argc == NARG_CNT::TWO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_SYMLINK_NAME, cbExec, cbComplCallback).val_; + } else { + NVal cb(env, funcArg[NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_SYMLINK_NAME, cbExec, cbComplCallback).val_; + } +} +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/symlink.h b/interfaces/kits/js/src/mod_fs/properties/symlink.h old mode 100644 new mode 100755 index 0b14e8bec6259b8a39019db02047bce7a0c02c75..a25e1f4d7ff980b45eb8c6526180f23f43d51ab5 --- a/interfaces/kits/js/src/mod_fs/properties/symlink.h +++ b/interfaces/kits/js/src/mod_fs/properties/symlink.h @@ -1,28 +1,34 @@ -/* - * 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_SYMLINK_H -#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_SYMLINK_H - -#include "filemgmt_libn.h" - -namespace OHOS::FileManagement::ModuleFileIO { -class Symlink final { -public: - static napi_value Async(napi_env env, napi_callback_info info); - static napi_value Sync(napi_env env, napi_callback_info info); -}; -} // namespace OHOS::FileManagement::ModuleFileIO +/* + * Copyright (c) 2022-2023 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_SYMLINK_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_SYMLINK_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +class Symlink final { +public: + static napi_value Async(napi_env env, napi_callback_info info); + static napi_value Sync(napi_env env, napi_callback_info info); +}; + +const std::string PROCEDURE_SYMLINK_NAME = "FileIOSymLink"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS #endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_SYMLINK_H \ No newline at end of file