From 46b6194e0d1912d71b65ca63d92dfea4692911ea Mon Sep 17 00:00:00 2001 From: bubble_mao Date: Tue, 21 Nov 2023 19:27:42 +0800 Subject: [PATCH] stat Signed-off-by: bubble_mao --- .../js/src/mod_fs/class_stat/stat_entity.h | 5 ++ .../src/mod_fs/class_stat/stat_n_exporter.cpp | 19 ++++++ .../src/mod_fs/class_stat/stat_n_exporter.h | 2 +- interfaces/kits/js/src/mod_fs/common_func.cpp | 3 +- interfaces/kits/js/src/mod_fs/common_func.h | 3 +- .../kits/js/src/mod_fs/properties/stat.cpp | 66 +++++++++++++++---- 6 files changed, 81 insertions(+), 17 deletions(-) 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 63277a1ec..02125712b 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 @@ -18,9 +18,14 @@ #include "uv.h" +enum Location { + LOCAL = 1 << 1, + CLOUD = 1 << 2 +}; namespace OHOS::FileManagement::ModuleFileIO { struct StatEntity { uv_stat_t stat_; + Location locationType_; }; } // 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 9fb3335a2..2641c948a 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 @@ -226,6 +226,24 @@ napi_value StatNExporter::GetCtime(napi_env env, napi_callback_info info) return NVal::CreateInt64(env, static_cast(statEntity->stat_.st_ctim.tv_sec)).val_; } +napi_value StatNExporter::GetLocation(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 statEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!statEntity) { + HILOGE("Failed to get stat entity"); + return nullptr; + } + + return NVal::CreateInt32(env, static_cast(statEntity->locationType_)).val_; +} + napi_value StatNExporter::Constructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -268,6 +286,7 @@ bool StatNExporter::Export() NVal::DeclareNapiGetter("atime", GetAtime), NVal::DeclareNapiGetter("mtime", GetMtime), NVal::DeclareNapiGetter("ctime", GetCtime), + NVal::DeclareNapiGetter("location", GetLocation), }; #ifdef WIN_PLATFORM diff --git a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.h index 1cd9fecf2..a5435a3f0 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.h @@ -25,7 +25,6 @@ constexpr int S_PERMISSION = 00000777; class StatNExporter final : public NExporter { public: inline static const std::string className_ = "Stat"; - bool Export() override; #ifdef WIN_PLATFORM inline static const int S_IFSOCK = 0140000; @@ -52,6 +51,7 @@ public: static napi_value GetAtime(napi_env env, napi_callback_info info); static napi_value GetMtime(napi_env env, napi_callback_info info); static napi_value GetCtime(napi_env env, napi_callback_info info); + static napi_value GetLocation(napi_env env, napi_callback_info info); StatNExporter(napi_env env, napi_value exports); ~StatNExporter() override; diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 32f3fa64b..c7dec059e 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -145,7 +145,7 @@ unsigned int CommonFunc::ConvertJsFlags(unsigned int &flags) return flagsABI; } -NVal CommonFunc::InstantiateStat(napi_env env, const uv_stat_t &buf) +NVal CommonFunc::InstantiateStat(napi_env env, const uv_stat_t &buf, Location location) { napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {}); if (!objStat) { @@ -162,6 +162,7 @@ NVal CommonFunc::InstantiateStat(napi_env env, const uv_stat_t &buf) } statEntity->stat_ = buf; + statEntity->locationType_ = location; return { env, objStat }; } diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index cc7293ddf..6574a8744 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -19,6 +19,7 @@ #include "fd_guard.h" #include "n_val.h" #include "uv.h" +#include "class_stat/stat_entity.h" namespace OHOS { namespace FileManagement { @@ -60,7 +61,7 @@ void InitWhenceType(napi_env env, napi_value exports); struct CommonFunc { static unsigned int ConvertJsFlags(unsigned int &flags); - static LibN::NVal InstantiateStat(napi_env env, const uv_stat_t &buf); + static LibN::NVal InstantiateStat(napi_env env, const uv_stat_t &buf, Location locationType); #ifndef WIN_PLATFORM static LibN::NVal InstantiateFile(napi_env env, int fd, const std::string &pathOrUri, bool isUri); static LibN::NVal InstantiateStream(napi_env env, std::unique_ptr fp); diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index 490d79d36..bc8d69c72 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -22,38 +22,75 @@ #include "common_func.h" #include "file_utils.h" #include "filemgmt_libhilog.h" +#include namespace OHOS::FileManagement::ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +const char XATTR_LOACATION[] = {"user.cloud.location"}; +const Location DEFAULT_LOCATION = LOCAL; + +static Location GetLocationAttr(const FileInfo &fileInfo) +{ + if (!fileInfo.isPath) { + return DEFAULT_LOCATION; + } +#ifdef IOS_PLATFORM + auto xattrValueSize = getxattr(fileInfo.path.get(), XATTR_LOACATION, nullptr, 0, 0, 0); +#else + auto xattrValueSize = getxattr(fileInfo.path.get(), XATTR_LOACATION, nullptr, 0); +#endif + if (xattrValueSize == -1 || errno == ENOTSUP) { + return DEFAULT_LOCATION; + } + if (xattrValueSize <= 0) { + return DEFAULT_LOCATION; + } + std::unique_ptr xattrValue = std::make_unique(xattrValueSize); + if (xattrValue == nullptr) { + return DEFAULT_LOCATION; + } +#ifdef IOS_PLATFORM + xattrValueSize = getxattr(fileInfo.path.get(), XATTR_LOACATION, xattrValue.get(), xattrValueSize, 0, 0); +#else + xattrValueSize = getxattr(fileInfo.path.get(), XATTR_LOACATION, xattrValue.get(), xattrValueSize); +#endif + if (xattrValueSize == -1 || errno == ENOTSUP) { + return DEFAULT_LOCATION; + } + if (xattrValueSize <= 0) { + return DEFAULT_LOCATION; + } + return static_cast(atoi(xattrValue.get())); +} 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), {} } }; + 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 {false, FileInfo {false, {}, {}}}; } auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); - return { false, FileInfo { false, {}, {} } }; + return {false, FileInfo {false, {}, {}}}; } - return { true, FileInfo { false, {}, move(fdg) } }; + return {true, FileInfo {false, {}, move(fdg)}}; } HILOGE("Invalid parameter"); NError(EINVAL).ThrowErr(env); - return { false, FileInfo { false, {}, {} } }; + return {false, FileInfo {false, {}, {}}}; }; -static NError CheckFsStat(const FileInfo &fileInfo, uv_fs_t* req) +static NError CheckFsStat(const FileInfo &fileInfo, uv_fs_t *req) { if (fileInfo.isPath) { int ret = uv_fs_stat(nullptr, req, fileInfo.path.get(), nullptr); @@ -84,8 +121,8 @@ napi_value Stat::Sync(napi_env env, napi_callback_info info) return nullptr; } - std::unique_ptr stat_req = { - new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + 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); @@ -96,8 +133,7 @@ napi_value Stat::Sync(napi_env env, napi_callback_info info) err.ThrowErr(env); return nullptr; } - - auto stat = CommonFunc::InstantiateStat(env, stat_req->statbuf).val_; + auto stat = CommonFunc::InstantiateStat(env, stat_req->statbuf, GetLocationAttr(fileInfo)).val_; return stat; } @@ -120,8 +156,8 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) return nullptr; } auto cbExec = [arg, fileInfo = make_shared(move(fileInfo))]() -> NError { - std::unique_ptr stat_req = { - new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + 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); @@ -131,15 +167,17 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) return err; } arg->stat_ = stat_req->statbuf; + arg->locationType_ = GetLocationAttr(*fileInfo); return NError(ERRNO_NOERR); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { - return { env, err.GetNapiErr(env) }; + return {env, err.GetNapiErr(env)}; } - auto stat = CommonFunc::InstantiateStat(env, arg->stat_); + auto stat = CommonFunc::InstantiateStat(env, arg->stat_, arg->locationType_); return stat; }; + NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE) { return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STAT_NAME, cbExec, cbCompl).val_; -- Gitee