From 958376569ec4b9906b302b052be02cfa85109ddd Mon Sep 17 00:00:00 2001 From: panqinxu Date: Fri, 16 Dec 2022 10:46:53 +0800 Subject: [PATCH] Add the reconstruction interfaces stat Signed-off-by: panqinxu --- interfaces/kits/js/src/mod_fs/common_func.h | 10 +- .../kits/js/src/mod_fs/properties/stat.cpp | 148 ++++++++++++++++++ .../kits/js/src/mod_fs/properties/stat.h | 29 ++++ 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 interfaces/kits/js/src/mod_fs/properties/stat.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/stat.h diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 305db2469..544c40047 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -16,11 +16,13 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H #define INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H -#include "../common/napi/uni_header.h" +#include "fd_guard.h" +#include "n_val.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { + constexpr int64_t INVALID_POSITION = std::numeric_limits::max(); constexpr int RDONLY = 00; constexpr int WRONLY = 01; @@ -33,6 +35,12 @@ constexpr int DIRECTORY = 0200000; constexpr int NOFOLLOW = 0400000; constexpr int SYNC = 04010000; +struct FileInfo { + bool isPath = false; + std::unique_ptr path; + FDGuard fdg; +}; + void InitOpenMode(napi_env env, napi_value exports); struct CommonFunc { diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp new file mode 100644 index 000000000..9028f9e25 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -0,0 +1,148 @@ +/* + * 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 "stat.h" + +#include +#include + +#include "../common_func.h" +#include "class_stat/stat_entity.h" +#include "class_stat/stat_n_exporter.h" +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +static NVal InstantiateStat(napi_env env, struct stat &buf) +{ + napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {}); + if (!objStat) { + HILOGE("Failed to instantiate stat class"); + NError(EIO).ThrowErr(env); + return NVal(); + } + + auto statEntity = NClass::GetEntityOf(env, objStat); + if (!statEntity) { + HILOGE("Failed to get stat entity"); + NError(EIO).ThrowErr(env); + return NVal(); + } + + statEntity->stat_ = buf; + return { env, objStat }; +} + +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) { + if (fd < 0) { + HILOGE("Invalid fd"); + NError(EINVAL).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } + return { true, FileInfo { false, {}, { fd, false } } }; + } + HILOGE("Invalid parameter"); + NError(EINVAL).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; +}; + +napi_value Stat::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 [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); + if (!succ) { + return nullptr; + } + + struct stat buf; + if (fileInfo.isPath) { + if (stat(fileInfo.path.get(), &buf) < 0) { + HILOGE("Failed to stat file with path"); + NError(errno).ThrowErr(env); + return nullptr; + } + } else { + if (fstat(fileInfo.fdg.GetFD(), &buf) < 0) { + HILOGE("Failed to stat file with fd"); + NError(errno).ThrowErr(env); + return nullptr; + } + } + + auto stat = InstantiateStat(env, buf).val_; + return stat; +} + +struct AsyncStatArg { + struct stat stat_; +}; + +napi_value Stat::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 [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); + if (!succ) { + return nullptr; + } + + auto arg = make_shared(); + auto cbExec = [arg, fileInfo = make_shared(move(fileInfo))]() -> NError { + if (fileInfo->isPath) { + if (stat(fileInfo->path.get(), &arg->stat_) < 0) { + HILOGE("Failed to stat file with path"); + return NError(errno); + } + } else { + if (fstat(fileInfo->fdg.GetFD(), &arg->stat_) < 0) { + HILOGE("Failed to stat file with fd"); + return NError(errno); + } + } + return NError(ERRNO_NOERR); + }; + auto cbCompl = [arg](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + auto stat = InstantiateStat(env, arg->stat_); + return stat; + }; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STAT_NAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STAT_NAME, cbExec, cbCompl).val_; + } +} +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.h b/interfaces/kits/js/src/mod_fs/properties/stat.h new file mode 100644 index 000000000..fafdaa54e --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/stat.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_STAT_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_STAT_H + +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::ModuleFileIO { +class Stat 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_STAT_NAME = "FileIOStat"; +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_STAT_H \ No newline at end of file -- Gitee