diff --git a/interfaces/kits/js/src/mod_fs/class_stat/stat_entity.h b/interfaces/kits/js/src/mod_fs/class_stat/stat_entity.h index 88f6c67f0c3c66d5895c388486f55f21310b437e..63277a1ec4e4e07fcd8dc775f88ae175c4a007c4 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/stat_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_stat/stat_entity.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -16,11 +16,11 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STAT_STAT_ENTITY_H #define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STAT_STAT_ENTITY_H -#include +#include "uv.h" namespace OHOS::FileManagement::ModuleFileIO { struct StatEntity { - struct stat stat_; + uv_stat_t stat_; }; } // namespace OHOS::FileManagement::ModuleFileIO #endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STAT_STAT_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp index a88d6fc504574c6163fbe98bbfd560282e1615ae..60769b339d8aa968d9973f5543cf9a2095832f5e 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -187,7 +187,7 @@ napi_value StatNExporter::GetAtime(napi_env env, napi_callback_info info) return nullptr; } - return NVal::CreateInt64(env, statEntity->stat_.st_atim.tv_sec).val_; + return NVal::CreateInt64(env, static_cast(statEntity->stat_.st_atim.tv_sec)).val_; } napi_value StatNExporter::GetMtime(napi_env env, napi_callback_info info) @@ -205,7 +205,7 @@ napi_value StatNExporter::GetMtime(napi_env env, napi_callback_info info) return nullptr; } - return NVal::CreateInt64(env, statEntity->stat_.st_mtim.tv_sec).val_; + return NVal::CreateInt64(env, static_cast(statEntity->stat_.st_mtim.tv_sec)).val_; } napi_value StatNExporter::GetCtime(napi_env env, napi_callback_info info) @@ -223,7 +223,7 @@ napi_value StatNExporter::GetCtime(napi_env env, napi_callback_info info) return nullptr; } - return NVal::CreateInt64(env, statEntity->stat_.st_ctim.tv_sec).val_; + return NVal::CreateInt64(env, static_cast(statEntity->stat_.st_ctim.tv_sec)).val_; } napi_value StatNExporter::Constructor(napi_env env, napi_callback_info info) diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 3c4d55e9b21bbe4668c693f392d4fd0a44ef2f01..f5f999312217c2fd17b2b8ab99b94a128a91c2ff 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -115,7 +115,7 @@ unsigned int CommonFunc::ConvertJsFlags(unsigned int &flags) return flagsABI; } -NVal CommonFunc::InstantiateStat(napi_env env, struct stat &buf) +NVal CommonFunc::InstantiateStat(napi_env env, uv_stat_t &buf) { napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {}); if (!objStat) { @@ -217,7 +217,7 @@ static tuple, size_t> DecodeString(napi_env env, NVal j HILOGE("Failed to convert encoding to UTF8"); return { false, nullptr, 0 }; } - + string_view encodingStr(encodingBuf.release()); if (encodingStr == "utf-8") { return jsStr.ToUTF8String(); diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 13ef8e1db60d8c37cb310722dde1df191abe88e3..7a169bf9d9cdfcaa703cf3936a927393fe292fce 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -45,7 +45,7 @@ void InitOpenMode(napi_env env, napi_value exports); struct CommonFunc { static unsigned int ConvertJsFlags(unsigned int &flags); - static LibN::NVal InstantiateStat(napi_env env, struct stat &buf); + static LibN::NVal InstantiateStat(napi_env env, uv_stat_t &buf); static LibN::NVal InstantiateStream(napi_env env, std::unique_ptr fp); static std::tuple GetReadArg(napi_env env, napi_value readBuf, diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 315564b84ca75798c9be02ed600e590de60fe808..2ab79dfa324c3120e02b0f7df26e0135a1299f21 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -42,22 +42,24 @@ napi_value Lstat::Sync(napi_env env, napi_callback_info info) return nullptr; } - struct stat buf; - int ret = lstat(pathPtr.get(), &buf); + std::unique_ptr lstat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!lstat_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + int ret = uv_fs_lstat(nullptr, lstat_req.get(), pathPtr.get(), nullptr); if (ret < 0) { HILOGE("Failed to get stat of file by path %{public}s", pathPtr.get()); NError(errno).ThrowErr(env); return nullptr; } - auto stat = CommonFunc::InstantiateStat(env, buf).val_; + auto stat = CommonFunc::InstantiateStat(env, lstat_req->statbuf).val_; return stat; } -struct AsyncStatArg { - struct stat stat_; -}; - napi_value Lstat::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -75,15 +77,21 @@ napi_value Lstat::Async(napi_env env, napi_callback_info info) } string path = tmp.get(); - auto arg = make_shared(); + auto arg = make_shared(); auto cbExec = [arg, path]() -> NError { - int ret = lstat(path.c_str(), &arg->stat_); + std::unique_ptr lstat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!lstat_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_lstat(nullptr, lstat_req.get(), path.c_str(), nullptr); if (ret < 0) { HILOGE("Failed to get stat of file by path: %{public}s, ret: %{public}d", path.c_str(), ret); return NError(errno); - } else { - return NError(ERRNO_NOERR); } + arg->stat_ = lstat_req->statbuf; + return NError(ERRNO_NOERR); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index 29773d02896797b7123aa850f178c090609c2caa..8d4a72f53cdbd1e51e8002ee8ea17bcf0e133907 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -65,9 +65,16 @@ napi_value Stat::Sync(napi_env env, napi_callback_info info) return nullptr; } - struct stat buf; + std::unique_ptr stat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!stat_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (fileInfo.isPath) { - if (stat(fileInfo.path.get(), &buf) < 0) { + int ret = uv_fs_stat(nullptr, stat_req.get(), fileInfo.path.get(), nullptr); + if (ret < 0) { HILOGE("Failed to stat file with path"); NError(errno).ThrowErr(env); return nullptr; @@ -80,14 +87,10 @@ napi_value Stat::Sync(napi_env env, napi_callback_info info) } } - auto stat = CommonFunc::InstantiateStat(env, buf).val_; + auto stat = CommonFunc::InstantiateStat(env, stat_req->statbuf).val_; return stat; } -struct AsyncStatArg { - struct stat stat_; -}; - napi_value Stat::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -101,19 +104,28 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + auto arg = make_shared(); auto cbExec = [arg, fileInfo = make_shared(move(fileInfo))]() -> NError { + std::unique_ptr stat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!stat_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } if (fileInfo->isPath) { - if (stat(fileInfo->path.get(), &arg->stat_) < 0) { + int ret = uv_fs_stat(nullptr, stat_req.get(), fileInfo->path.get(), nullptr); + if (ret < 0) { HILOGE("Failed to stat file with path"); return NError(errno); } } else { - if (fstat(fileInfo->fdg->GetFD(), &arg->stat_) < 0) { + int ret = uv_fs_fstat(nullptr, stat_req.get(), fileInfo->fdg->GetFD(), nullptr); + if (ret < 0) { HILOGE("Failed to stat file with fd"); return NError(errno); } } + arg->stat_ = stat_req->statbuf; return NError(ERRNO_NOERR); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal {