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 ba00374c98a298620dbdb158b984022ff4e3cbf1..46ddeea5f905e6f22d468fc1dc530fa0630f5d5e 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; diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 4118a6f1f230215193607b8cdf5fd10c448df88e..99430b4de94c3b744eb26f3a81c55b6c85265533 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -185,6 +185,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/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index 0fd4c935314c7c71df28e0ca13f18a8d57f7d0e4..1446a2491acc178f7d005d394d7b31bb22331e40 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -26,38 +26,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); @@ -129,8 +166,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); @@ -169,8 +206,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); @@ -187,7 +224,7 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { - return { env, err.GetNapiErr(env) }; + return {env, err.GetNapiErr(env)}; } #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) auto stat = CommonFunc::InstantiateStat(env, arg->stat_, arg->location);