From e73483b02534b840848c09cdc124f28201fb3e95 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Wed, 27 Sep 2023 23:11:50 +0800 Subject: [PATCH 1/3] add getparent interface --- .../src/mod_fs/class_file/file_n_exporter.cpp | 34 +++++++++++++++++++ .../src/mod_fs/class_file/file_n_exporter.h | 1 + 2 files changed, 35 insertions(+) diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 0fe49afad..f1c476217 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -27,6 +27,7 @@ #include "filemgmt_libhilog.h" #include "filemgmt_libn.h" #include "../common_func.h" +#include "rust_file.h" namespace OHOS { namespace FileManagement { @@ -282,6 +283,38 @@ napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) return funcArg.GetThisVar(); } +napi_value FileNExporter::GetParent(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileEntity = GetFileEntity(env, funcArg.GetThisVar()); + if (!fileEntity) { + HILOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if (!fileEntity->fd_.get()) { + HILOGE("File has been closed"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto parentPath = ::GetParent(fileEntity->fd_.get()->GetFD()); + if (parentPath == nullptr) { + HILOGE("Invalid parent uri"); + if (errno != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + } + std::string result = std::string(parentPath->str); + StrFree(parentPath); + return NVal::CreateUTF8String(env, result).val_; +} + bool FileNExporter::Export() { vector props = { @@ -292,6 +325,7 @@ bool FileNExporter::Export() NVal::DeclareNapiFunction("lock", Lock), NVal::DeclareNapiFunction("tryLock", TryLock), NVal::DeclareNapiFunction("unlock", UnLock), + NVal::DeclareNapiFunction("getParent", GetParent), #endif }; diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h index 43e5278a0..6b2ce05cb 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h @@ -36,6 +36,7 @@ public: static napi_value Lock(napi_env env, napi_callback_info info); static napi_value TryLock(napi_env env, napi_callback_info info); static napi_value UnLock(napi_env env, napi_callback_info info); + static napi_value GetParent(napi_env env, napi_callback_info info); #endif static napi_value Constructor(napi_env env, napi_callback_info info); static napi_value GetFD(napi_env env, napi_callback_info info); -- Gitee From cc86a72ef3153c25c015f23e11a85c960b53af5e Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Wed, 27 Sep 2023 23:11:50 +0800 Subject: [PATCH 2/3] add getparent interface Signed-off-by: zhuhongtao666 --- .../src/mod_fs/class_file/file_n_exporter.cpp | 34 +++++++++++++++++++ .../src/mod_fs/class_file/file_n_exporter.h | 1 + 2 files changed, 35 insertions(+) diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 0fe49afad..f1c476217 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -27,6 +27,7 @@ #include "filemgmt_libhilog.h" #include "filemgmt_libn.h" #include "../common_func.h" +#include "rust_file.h" namespace OHOS { namespace FileManagement { @@ -282,6 +283,38 @@ napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) return funcArg.GetThisVar(); } +napi_value FileNExporter::GetParent(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileEntity = GetFileEntity(env, funcArg.GetThisVar()); + if (!fileEntity) { + HILOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if (!fileEntity->fd_.get()) { + HILOGE("File has been closed"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto parentPath = ::GetParent(fileEntity->fd_.get()->GetFD()); + if (parentPath == nullptr) { + HILOGE("Invalid parent uri"); + if (errno != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + } + std::string result = std::string(parentPath->str); + StrFree(parentPath); + return NVal::CreateUTF8String(env, result).val_; +} + bool FileNExporter::Export() { vector props = { @@ -292,6 +325,7 @@ bool FileNExporter::Export() NVal::DeclareNapiFunction("lock", Lock), NVal::DeclareNapiFunction("tryLock", TryLock), NVal::DeclareNapiFunction("unlock", UnLock), + NVal::DeclareNapiFunction("getParent", GetParent), #endif }; diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h index 43e5278a0..6b2ce05cb 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h @@ -36,6 +36,7 @@ public: static napi_value Lock(napi_env env, napi_callback_info info); static napi_value TryLock(napi_env env, napi_callback_info info); static napi_value UnLock(napi_env env, napi_callback_info info); + static napi_value GetParent(napi_env env, napi_callback_info info); #endif static napi_value Constructor(napi_env env, napi_callback_info info); static napi_value GetFD(napi_env env, napi_callback_info info); -- Gitee From 734db5577a1be4b3b0bd44f8660e0c8cf92ab125 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Thu, 28 Sep 2023 18:08:29 +0800 Subject: [PATCH 3/3] getParent update Signed-off-by: zhuhongtao666 --- .../src/mod_fs/class_file/file_n_exporter.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index f1c476217..4af0416bd 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -292,23 +292,16 @@ napi_value FileNExporter::GetParent(napi_env env, napi_callback_info info) return nullptr; } auto fileEntity = GetFileEntity(env, funcArg.GetThisVar()); - if (!fileEntity) { + if (!fileEntity || !fileEntity->fd_.get()) { HILOGE("Failed to get file entity"); NError(EINVAL).ThrowErr(env); return nullptr; } - if (!fileEntity->fd_.get()) { - HILOGE("File has been closed"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } auto parentPath = ::GetParent(fileEntity->fd_.get()->GetFD()); - if (parentPath == nullptr) { - HILOGE("Invalid parent uri"); - if (errno != 0) { - NError(errno).ThrowErr(env); - return nullptr; - } + if (!parentPath) { + HILOGE("Failed to get parent path by fileEntity"); + NError(errno).ThrowErr(env); + return nullptr; } std::string result = std::string(parentPath->str); StrFree(parentPath); -- Gitee