From 45662bc26c4877652f82f82fa4a3ab40648ebe7d Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Sat, 10 Jun 2023 16:09:16 +0800 Subject: [PATCH 1/2] fs_adapt_windows Signed-off-by: 18721213663 --- file_api.gni | 4 ++ interfaces/kits/js/BUILD.gn | 68 ++++++++++-------- .../js/src/mod_fs/class_file/file_entity.h | 3 +- .../src/mod_fs/class_file/file_n_exporter.cpp | 12 ++++ .../src/mod_fs/class_file/file_n_exporter.h | 10 ++- .../src/mod_fs/class_stat/stat_n_exporter.cpp | 8 +++ .../src/mod_fs/class_stat/stat_n_exporter.h | 5 ++ interfaces/kits/js/src/mod_fs/common_func.cpp | 72 +++++++++++-------- interfaces/kits/js/src/mod_fs/common_func.h | 33 ++++++--- interfaces/kits/js/src/mod_fs/module.cpp | 14 +++- .../kits/js/src/mod_fs/properties/open.cpp | 58 ++++++++------- .../kits/js/src/mod_fs/properties/open.h | 4 ++ .../src/mod_fs/properties/prop_n_exporter.cpp | 63 +++++++++------- .../src/mod_fs/properties/prop_n_exporter.h | 4 ++ utils/filemgmt_libhilog/BUILD.gn | 12 +++- utils/filemgmt_libn/BUILD.gn | 6 ++ utils/filemgmt_libn/include/n_error.h | 5 +- utils/filemgmt_libn/include/n_exporter.h | 4 ++ utils/filemgmt_libn/include/n_val.h | 4 ++ utils/filemgmt_libn/src/n_val.cpp | 4 ++ 20 files changed, 261 insertions(+), 132 deletions(-) diff --git a/file_api.gni b/file_api.gni index df0cc8dbb..d843a2cc7 100644 --- a/file_api.gni +++ b/file_api.gni @@ -18,3 +18,7 @@ file_api_path = "//foundation/filemanagement/file_api" hiviewdfx_hilog_path = "//base/hiviewdfx/hilog" src_path = "${file_api_path}/interfaces/kits/js/src" utils_path = "${file_api_path}/utils" + +use_mac = "${current_os}_${current_cpu}" == "mac_x64" || + "${current_os}_${current_cpu}" == "mac_arm64" +use_mingw_win = "${current_os}_${current_cpu}" == "mingw_x86_64" diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index d3138ae37..4562091c5 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -118,54 +118,62 @@ ohos_shared_library("fs") { "src/common/file_helper/fd_guard.cpp", "src/mod_fs/class_file/file_n_exporter.cpp", "src/mod_fs/class_stat/stat_n_exporter.cpp", - "src/mod_fs/class_stream/flush.cpp", - "src/mod_fs/class_stream/stream_n_exporter.cpp", - "src/mod_fs/class_watcher/watcher_entity.cpp", - "src/mod_fs/class_watcher/watcher_n_exporter.cpp", "src/mod_fs/common_func.cpp", "src/mod_fs/module.cpp", "src/mod_fs/properties/close.cpp", - "src/mod_fs/properties/copy_file.cpp", - "src/mod_fs/properties/copydir.cpp", - "src/mod_fs/properties/create_stream.cpp", "src/mod_fs/properties/fdatasync.cpp", - "src/mod_fs/properties/fdopen_stream.cpp", "src/mod_fs/properties/fsync.cpp", - "src/mod_fs/properties/listfile.cpp", "src/mod_fs/properties/lstat.cpp", "src/mod_fs/properties/mkdtemp.cpp", - "src/mod_fs/properties/move.cpp", - "src/mod_fs/properties/movedir.cpp", "src/mod_fs/properties/open.cpp", "src/mod_fs/properties/prop_n_exporter.cpp", - "src/mod_fs/properties/read_text.cpp", "src/mod_fs/properties/rename.cpp", "src/mod_fs/properties/rmdirent.cpp", "src/mod_fs/properties/stat.cpp", - "src/mod_fs/properties/symlink.cpp", "src/mod_fs/properties/truncate.cpp", - "src/mod_fs/properties/watcher.cpp", ] - deps = [ - "${file_api_path}/interfaces/kits/native:remote_uri_native", - "${utils_path}/filemgmt_libn:filemgmt_libn", - ] + cflags_cc = [ "-std=c++17" ] - external_deps = [ - "ability_base:zuri", - "ability_runtime:abilitykit_native", - "access_token:libtokenid_sdk", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "data_share:datashare_common", - "data_share:datashare_consumer", - "ipc:ipc_core", - "samgr:samgr_proxy", - ] + deps = [ "${utils_path}/filemgmt_libn:filemgmt_libn" ] use_exceptions = true + + if (use_mingw_win) { + defines = [ "WIN_PLATFORM" ] + } + + if (!use_mingw_win && !use_mac) { + sources += [ + "src/mod_fs/class_stream/flush.cpp", + "src/mod_fs/class_stream/stream_n_exporter.cpp", + "src/mod_fs/class_watcher/watcher_entity.cpp", + "src/mod_fs/class_watcher/watcher_n_exporter.cpp", + "src/mod_fs/properties/copy_file.cpp", + "src/mod_fs/properties/copydir.cpp", + "src/mod_fs/properties/create_stream.cpp", + "src/mod_fs/properties/fdopen_stream.cpp", + "src/mod_fs/properties/listfile.cpp", + "src/mod_fs/properties/move.cpp", + "src/mod_fs/properties/movedir.cpp", + "src/mod_fs/properties/read_text.cpp", + "src/mod_fs/properties/symlink.cpp", + "src/mod_fs/properties/watcher.cpp", + ] + external_deps = [ + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + deps += [ "${file_api_path}/interfaces/kits/native:remote_uri_native" ] + } } ohos_shared_library("hash") { diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_entity.h b/interfaces/kits/js/src/mod_fs/class_file/file_entity.h index 7af17996a..e4691774e 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_file/file_entity.h @@ -32,7 +32,7 @@ struct FileEntity { std::unique_ptr fd_ = { nullptr }; std::string path_; std::string uri_; - +#ifndef WIN_PLATFORM virtual ~FileEntity() { if (!fd_.get()) { @@ -49,6 +49,7 @@ struct FileEntity { } } } +#endif }; } // namespace ModuleFileIO } // namespace FileManagement 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 7623d84ad..6ae49a9e1 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 @@ -65,6 +65,7 @@ napi_value FileNExporter::GetFD(napi_env env, napi_callback_info info) return NVal::CreateInt32(env, rafEntity->fd_.get()->GetFD()).val_; } +#ifndef WIN_PLATFORM static bool GetExclusive(napi_env env, NFuncArg &funcArg, bool &exclusive) { if (funcArg.GetArgc() >= NARG_CNT::ONE) { @@ -186,6 +187,7 @@ napi_value FileNExporter::UnLock(napi_env env, napi_callback_info info) } return NVal::CreateUndefined(env).val_; } +#endif napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) { @@ -214,12 +216,18 @@ bool FileNExporter::Export() { vector props = { NVal::DeclareNapiGetter("fd", GetFD), + #ifndef WIN_PLATFORM NVal::DeclareNapiFunction("lock", Lock), NVal::DeclareNapiFunction("tryLock", TryLock), NVal::DeclareNapiFunction("unlock", UnLock), + #endif }; +#ifdef WIN_PLATFORM + string className = GetNExporterName(); +#else string className = GetClassName(); +#endif bool succ = false; napi_value classValue = nullptr; tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, @@ -239,7 +247,11 @@ bool FileNExporter::Export() return exports_.AddProp(className, classValue); } +#ifdef WIN_PLATFORM +string FileNExporter::GetNExporterName() +#else string FileNExporter::GetClassName() +#endif { return FileNExporter::className_; } 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 38c6c1d00..5ccc6b08b 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 @@ -27,13 +27,17 @@ public: inline static const std::string className_ = "File"; bool Export() override; +#ifdef WIN_PLATFORM + std::string GetNExporterName() override; +#else std::string GetClassName() override; - - static napi_value Constructor(napi_env env, napi_callback_info info); - static napi_value GetFD(napi_env env, napi_callback_info info); 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); +#endif + static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value GetFD(napi_env env, napi_callback_info info); + FileNExporter(napi_env env, napi_value exports); ~FileNExporter() override; }; 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 30ae48f94..9fb3335a2 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 @@ -270,7 +270,11 @@ bool StatNExporter::Export() NVal::DeclareNapiGetter("ctime", GetCtime), }; +#ifdef WIN_PLATFORM + string className = GetNExporterName(); +#else string className = GetClassName(); +#endif bool succ = false; napi_value classValue = nullptr; tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, StatNExporter::Constructor, @@ -290,7 +294,11 @@ bool StatNExporter::Export() return exports_.AddProp(className, classValue); } +#ifdef WIN_PLATFORM +string StatNExporter::GetNExporterName() +#else string StatNExporter::GetClassName() +#endif { return StatNExporter::className_; } 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 1e196c5bc..1cd9fecf2 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 @@ -27,7 +27,12 @@ public: inline static const std::string className_ = "Stat"; bool Export() override; +#ifdef WIN_PLATFORM + inline static const int S_IFSOCK = 0140000; + std::string GetNExporterName() override; +#else std::string GetClassName() override; +#endif static napi_value 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 b3c289196..08be08c57 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -23,8 +23,10 @@ #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" +#ifndef WIN_PLATFORM #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" +#endif #include "filemgmt_libhilog.h" #include "filemgmt_libn.h" @@ -38,16 +40,16 @@ void InitOpenMode(napi_env env, napi_value exports) { char propertyName[] = "OpenMode"; napi_property_descriptor desc[] = { - DECLARE_NAPI_STATIC_PROPERTY("READ_ONLY", NVal::CreateInt32(env, RDONLY).val_), - DECLARE_NAPI_STATIC_PROPERTY("WRITE_ONLY", NVal::CreateInt32(env, WRONLY).val_), - DECLARE_NAPI_STATIC_PROPERTY("READ_WRITE", NVal::CreateInt32(env, RDWR).val_), - DECLARE_NAPI_STATIC_PROPERTY("CREATE", NVal::CreateInt32(env, CREATE).val_), - DECLARE_NAPI_STATIC_PROPERTY("TRUNC", NVal::CreateInt32(env, TRUNC).val_), - DECLARE_NAPI_STATIC_PROPERTY("APPEND", NVal::CreateInt32(env, APPEND).val_), - DECLARE_NAPI_STATIC_PROPERTY("NONBLOCK", NVal::CreateInt32(env, NONBLOCK).val_), - DECLARE_NAPI_STATIC_PROPERTY("DIR", NVal::CreateInt32(env, DIRECTORY).val_), - DECLARE_NAPI_STATIC_PROPERTY("NOFOLLOW", NVal::CreateInt32(env, NOFOLLOW).val_), - DECLARE_NAPI_STATIC_PROPERTY("SYNC", NVal::CreateInt32(env, SYNC).val_), + DECLARE_NAPI_STATIC_PROPERTY("READ_ONLY", NVal::CreateInt32(env, USR_READ_ONLY).val_), + DECLARE_NAPI_STATIC_PROPERTY("WRITE_ONLY", NVal::CreateInt32(env, USR_WRITE_ONLY).val_), + DECLARE_NAPI_STATIC_PROPERTY("READ_WRITE", NVal::CreateInt32(env, USR_RDWR).val_), + DECLARE_NAPI_STATIC_PROPERTY("CREATE", NVal::CreateInt32(env, USR_CREATE).val_), + DECLARE_NAPI_STATIC_PROPERTY("TRUNC", NVal::CreateInt32(env, USR_TRUNC).val_), + DECLARE_NAPI_STATIC_PROPERTY("APPEND", NVal::CreateInt32(env, USR_APPEND).val_), + DECLARE_NAPI_STATIC_PROPERTY("NONBLOCK", NVal::CreateInt32(env, USR_NONBLOCK).val_), + DECLARE_NAPI_STATIC_PROPERTY("DIR", NVal::CreateInt32(env, USR_DIRECTORY).val_), + DECLARE_NAPI_STATIC_PROPERTY("NOFOLLOW", NVal::CreateInt32(env, USR_NOFOLLOW).val_), + DECLARE_NAPI_STATIC_PROPERTY("SYNC", NVal::CreateInt32(env, USR_SYNC).val_), }; napi_value obj = nullptr; napi_status status = napi_create_object(env, &obj); @@ -74,7 +76,11 @@ static tuple GetActualLen(napi_env env, size_t bufLen, size_t bufO if (op.HasProp("length")) { int64_t opLength = 0; + #ifdef WIN_PLATFORM + tie(succ, opLength) = op.GetPropValue("length").ToInt64(static_cast(retLen)); + #else tie(succ, opLength) = op.GetProp("length").ToInt64(static_cast(retLen)); + #endif if (!succ || opLength < 0 || static_cast(opLength) > retLen) { HILOGE("Invalid option.length"); NError(EINVAL).ThrowErr(env); @@ -87,29 +93,17 @@ static tuple GetActualLen(napi_env env, size_t bufLen, size_t bufO unsigned int CommonFunc::ConvertJsFlags(unsigned int &flags) { - static constexpr unsigned int usrWriteOnly = 01; - static constexpr unsigned int usrReadWrite = 02; - static constexpr unsigned int usrCreate = 0100; - static constexpr unsigned int usrExecuteLock = 0200; - static constexpr unsigned int usrTruncate = 01000; - static constexpr unsigned int usrAppend = 02000; - static constexpr unsigned int usrNoneBlock = 04000; - static constexpr unsigned int usrDirectory = 0200000; - static constexpr unsigned int usrNoFollowed = 0400000; - static constexpr unsigned int usrSynchronous = 04010000; - // default value is usrReadOnly 00 unsigned int flagsABI = 0; - flagsABI |= ((flags & usrWriteOnly) == usrWriteOnly) ? O_WRONLY : 0; - flagsABI |= ((flags & usrReadWrite) == usrReadWrite) ? O_RDWR : 0; - flagsABI |= ((flags & usrCreate) == usrCreate) ? O_CREAT : 0; - flagsABI |= ((flags & usrExecuteLock) == usrExecuteLock) ? O_EXCL : 0; - flagsABI |= ((flags & usrTruncate) == usrTruncate) ? O_TRUNC : 0; - flagsABI |= ((flags & usrAppend) == usrAppend) ? O_APPEND : 0; - flagsABI |= ((flags & usrNoneBlock) == usrNoneBlock) ? O_NONBLOCK : 0; - flagsABI |= ((flags & usrDirectory) == usrDirectory) ? O_DIRECTORY : 0; - flagsABI |= ((flags & usrNoFollowed) == usrNoFollowed) ? O_NOFOLLOW : 0; - flagsABI |= ((flags & usrSynchronous) == usrSynchronous) ? O_SYNC : 0; + flagsABI |= ((flags & USR_WRITE_ONLY) == USR_WRITE_ONLY) ? WRONLY : 0; + flagsABI |= ((flags & USR_RDWR) == USR_RDWR) ? RDWR : 0; + flagsABI |= ((flags & USR_CREATE) == USR_CREATE) ? CREATE : 0; + flagsABI |= ((flags & USR_TRUNC) == USR_TRUNC) ? TRUNC : 0; + flagsABI |= ((flags & USR_APPEND) == USR_APPEND) ? APPEND : 0; + flagsABI |= ((flags & USR_NONBLOCK) == USR_NONBLOCK) ? NONBLOCK : 0; + flagsABI |= ((flags & USR_DIRECTORY) == USR_DIRECTORY) ? DIRECTORY : 0; + flagsABI |= ((flags & USR_NOFOLLOW) == USR_NOFOLLOW) ? NOFOLLOW : 0; + flagsABI |= ((flags & USR_SYNC) == USR_SYNC) ? SYNC : 0; flags = flagsABI; return flagsABI; } @@ -134,6 +128,7 @@ NVal CommonFunc::InstantiateStat(napi_env env, uv_stat_t &buf) return { env, objStat }; } +#ifndef WIN_PLATFORM NVal CommonFunc::InstantiateStream(napi_env env, unique_ptr fp) { napi_value objStream = NClass::InstantiateClass(env, StreamNExporter::className_, {}); @@ -153,6 +148,7 @@ NVal CommonFunc::InstantiateStream(napi_env env, unique_ptrfp.swap(fp); return { env, objStream }; } +#endif void CommonFunc::fs_req_cleanup(uv_fs_t* req) { @@ -250,8 +246,13 @@ tuple CommonFunc::GetReadArg(napi_env env, HILOGE("Failed to get actual length"); return { false, nullptr, retLen, offset }; } + #ifdef WIN_PLATFORM + if (op.HasProp("offset") && !op.GetPropValue("offset").TypeIs(napi_undefined)) { + tie(succ, offset) = op.GetPropValue("offset").ToInt64(); + #else if (op.HasProp("offset") && !op.GetProp("offset").TypeIs(napi_undefined)) { tie(succ, offset) = op.GetProp("offset").ToInt64(); + #endif if (!succ || offset < 0) { HILOGE("option.offset shall be positive number"); NError(EINVAL).ThrowErr(env); @@ -271,7 +272,11 @@ tuple, void *, size_t, int64_t> CommonFunc::GetWriteArg NVal op(env, argOption); NVal jsBuffer(env, argWBuf); unique_ptr bufferGuard = nullptr; +#ifdef WIN_PLATFORM + tie(succ, bufferGuard, bufLen) = DecodeString(env, jsBuffer, op.GetPropValue("encoding")); +#else tie(succ, bufferGuard, bufLen) = DecodeString(env, jsBuffer, op.GetProp("encoding")); +#endif if (!succ) { tie(succ, buf, bufLen) = NVal(env, argWBuf).ToArraybuffer(); if (!succ) { @@ -294,8 +299,13 @@ tuple, void *, size_t, int64_t> CommonFunc::GetWriteArg return { false, nullptr, nullptr, 0, offset }; } +#ifdef WIN_PLATFORM + if (op.HasProp("offset") && !op.GetPropValue("offset").TypeIs(napi_undefined)) { + tie(succ, offset) = op.GetPropValue("offset").ToInt64(); +#else if (op.HasProp("offset") && !op.GetProp("offset").TypeIs(napi_undefined)) { tie(succ, offset) = op.GetProp("offset").ToInt64(); +#endif if (!succ || offset < 0) { HILOGE("option.offset shall be positive number"); NError(EINVAL).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 7a169bf9d..69e83e0d3 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -24,16 +24,27 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { -constexpr int RDONLY = 00; -constexpr int WRONLY = 01; -constexpr int RDWR = 02; -constexpr int CREATE = 0100; -constexpr int TRUNC = 01000; -constexpr int APPEND = 02000; -constexpr int NONBLOCK = 04000; -constexpr int DIRECTORY = 0200000; -constexpr int NOFOLLOW = 0400000; -constexpr int SYNC = 04010000; +constexpr int RDONLY = UV_FS_O_RDONLY; +constexpr int WRONLY = UV_FS_O_WRONLY; +constexpr int RDWR = UV_FS_O_RDWR; +constexpr int CREATE = UV_FS_O_CREAT; +constexpr int TRUNC = UV_FS_O_TRUNC; +constexpr int APPEND = UV_FS_O_APPEND; +constexpr int NONBLOCK = UV_FS_O_NONBLOCK; +constexpr int DIRECTORY = UV_FS_O_DIRECTORY; +constexpr int NOFOLLOW = UV_FS_O_NOFOLLOW; +constexpr int SYNC = UV_FS_O_SYNC; + +constexpr unsigned int USR_READ_ONLY = 00; +constexpr unsigned int USR_WRITE_ONLY = 01; +constexpr unsigned int USR_RDWR = 02; +constexpr unsigned int USR_CREATE = 0100; +constexpr unsigned int USR_TRUNC = 01000; +constexpr unsigned int USR_APPEND = 02000; +constexpr unsigned int USR_NONBLOCK = 04000; +constexpr unsigned int USR_DIRECTORY = 0200000; +constexpr unsigned int USR_NOFOLLOW = 0400000; +constexpr unsigned int USR_SYNC = 04010000; struct FileInfo { bool isPath = false; @@ -46,7 +57,9 @@ void InitOpenMode(napi_env env, napi_value exports); struct CommonFunc { static unsigned int ConvertJsFlags(unsigned int &flags); static LibN::NVal InstantiateStat(napi_env env, uv_stat_t &buf); +#ifndef WIN_PLATFORM static LibN::NVal InstantiateStream(napi_env env, std::unique_ptr fp); +#endif static std::tuple GetReadArg(napi_env env, napi_value readBuf, napi_value option); diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 8f925287d..3d9d08c36 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -20,8 +20,10 @@ #include "class_file/file_n_exporter.h" #include "class_stat/stat_n_exporter.h" +#ifndef WIN_PLATFORM #include "class_stream/stream_n_exporter.h" #include "class_watcher/watcher_n_exporter.h" +#endif #include "filemgmt_libhilog.h" #include "properties/prop_n_exporter.h" @@ -37,15 +39,21 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); +#ifndef WIN_PLATFORM products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); - +#endif for (auto &&product : products) { + #ifdef WIN_PLATFORM + string nExporterName = product->GetNExporterName(); + #else + string nExporterName = product->GetClassName(); + #endif if (!product->Export()) { - HILOGE("INNER BUG. Failed to export class %{public}s for module fileio", product->GetClassName().c_str()); + HILOGE("INNER BUG. Failed to export class %{public}s for module fileio", nExporterName.c_str()); return nullptr; } else { - HILOGI("Class %{public}s for module fileio has been exported", product->GetClassName().c_str()); + HILOGI("Class %{public}s for module fileio has been exported", nExporterName.c_str()); } } return exports; diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 0e2b7413f..9daf5febc 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -19,28 +19,32 @@ #include #include -#include "ability.h" -#include "bundle_info.h" -#include "bundle_mgr_proxy.h" #include "class_file/file_entity.h" #include "class_file/file_n_exporter.h" #include "common_func.h" -#include "datashare_helper.h" -#include "file_utils.h" #include "filemgmt_libhilog.h" +#include "file_utils.h" +#ifndef WIN_PLATFORM +#include "ability.h" +#include "bundle_info.h" +#include "bundle_mgr_proxy.h" +#include "datashare_helper.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "remote_uri.h" #include "status_receiver_host.h" #include "system_ability_definition.h" +#endif namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +#ifndef WIN_PLATFORM using namespace OHOS::DistributedFS::ModuleRemoteUri; using namespace OHOS::AppExecFwk; +#endif static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg) { @@ -59,25 +63,6 @@ static tuple GetJsFlags(napi_env env, const NFuncArg &funcAr return { true, flags }; } -static string DealWithUriWithName(string str) -{ - static uint32_t MEET_COUNT = 6; - uint32_t count = 0; - uint32_t index; - for (index = 0; index < str.length(); index++) { - if (str[index] == '/') { - count++; - } - if (count == MEET_COUNT) { - break; - } - } - if (count == MEET_COUNT) { - str = str.substr(0, index); - } - return str; -} - static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) { napi_value objFile = NClass::InstantiateClass(env, FileNExporter::className_, {}); @@ -118,6 +103,26 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) return { env, objFile }; } +#ifndef WIN_PLATFORM +static string DealWithUriWithName(string str) +{ + static uint32_t MEET_COUNT = 6; + uint32_t count = 0; + uint32_t index; + for (index = 0; index < str.length(); index++) { + if (str[index] == '/') { + count++; + } + if (count == MEET_COUNT) { + break; + } + } + if (count == MEET_COUNT) { + str = str.substr(0, index); + } + return str; +} + static int OpenFileByDatashare(string path, unsigned int flags) { std::shared_ptr dataShareHelper = nullptr; @@ -186,6 +191,7 @@ static string GetPathFromFileUri(string path, string bundleName, unsigned int mo } return path; } +#endif napi_value Open::Sync(napi_env env, napi_callback_info info) { @@ -207,6 +213,7 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) } int fd = -1; string pathStr = string(path.get()); +#ifndef WIN_PLATFORM if (RemoteUri::IsMediaUri(pathStr)) { int ret = OpenFileByDatashare(pathStr, mode); if (ret >= 0) { @@ -228,6 +235,7 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(E_INVAL).ThrowErr(env); return nullptr; } +#endif std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { @@ -279,6 +287,7 @@ napi_value Open::Async(napi_env env, napi_callback_info info) auto argv = funcArg[NARG_POS::FIRST]; auto cbExec = [arg, argv, path = string(path.get()), mode = mode, env = env]() -> NError { string pathStr = path; + #ifndef WIN_PLATFORM int fd = -1; if (RemoteUri::IsMediaUri(path)) { int ret = OpenFileByDatashare(path, mode); @@ -303,6 +312,7 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Failed to open file by RemoteUri"); return NError(E_INVAL); } + #endif std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { diff --git a/interfaces/kits/js/src/mod_fs/properties/open.h b/interfaces/kits/js/src/mod_fs/properties/open.h index f758f77e0..042e00ec0 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.h +++ b/interfaces/kits/js/src/mod_fs/properties/open.h @@ -17,7 +17,9 @@ #define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_OPEN_H #include "filemgmt_libn.h" +#ifndef WIN_PLATFORM #include "iremote_broker.h" +#endif namespace OHOS { namespace FileManagement { @@ -28,6 +30,7 @@ public: static napi_value Sync(napi_env env, napi_callback_info info); }; +#ifndef WIN_PLATFORM class FileIoToken : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileio.open"); @@ -35,6 +38,7 @@ public: FileIoToken() = default; virtual ~FileIoToken() noexcept = default; }; +#endif const std::string PROCEDURE_OPEN_NAME = "FileIOOpen"; const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 0ba33786f..ab8004f39 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -26,29 +26,32 @@ #include "class_file/file_entity.h" #include "class_file/file_n_exporter.h" #include "close.h" -#include "copy_file.h" -#include "copydir.h" -#include "create_stream.h" #include "fdatasync.h" -#include "fdopen_stream.h" #include "file_utils.h" #include "filemgmt_libn.h" #include "fsync.h" #include "js_native_api.h" #include "js_native_api_types.h" -#include "listfile.h" #include "lstat.h" #include "mkdtemp.h" -#include "move.h" -#include "movedir.h" #include "open.h" -#include "read_text.h" #include "rename.h" #include "rmdirent.h" #include "stat.h" -#include "symlink.h" #include "truncate.h" + +#ifndef WIN_PLATFORM +#include "copy_file.h" +#include "copydir.h" +#include "create_stream.h" +#include "fdopen_stream.h" +#include "listfile.h" +#include "move.h" +#include "movedir.h" +#include "read_text.h" +#include "symlink.h" #include "watcher.h" +#endif namespace OHOS { namespace FileManagement { @@ -558,55 +561,61 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("accessSync", AccessSync), NVal::DeclareNapiFunction("close", Close::Async), NVal::DeclareNapiFunction("closeSync", Close::Sync), - NVal::DeclareNapiFunction("copyDir", CopyDir::Async), - NVal::DeclareNapiFunction("copyDirSync", CopyDir::Sync), - NVal::DeclareNapiFunction("copyFile", CopyFile::Async), - NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync), - NVal::DeclareNapiFunction("createStream", CreateStream::Async), - NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), NVal::DeclareNapiFunction("fdatasync", Fdatasync::Async), NVal::DeclareNapiFunction("fdatasyncSync", Fdatasync::Sync), - NVal::DeclareNapiFunction("fdopenStream", FdopenStream::Async), - NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync), NVal::DeclareNapiFunction("fsync", Fsync::Async), NVal::DeclareNapiFunction("fsyncSync", Fsync::Sync), - NVal::DeclareNapiFunction("listFile", ListFile::Async), - NVal::DeclareNapiFunction("listFileSync", ListFile::Sync), NVal::DeclareNapiFunction("lstat", Lstat::Async), NVal::DeclareNapiFunction("lstatSync", Lstat::Sync), NVal::DeclareNapiFunction("mkdir", Mkdir), NVal::DeclareNapiFunction("mkdirSync", MkdirSync), NVal::DeclareNapiFunction("mkdtemp", Mkdtemp::Async), NVal::DeclareNapiFunction("mkdtempSync", Mkdtemp::Sync), - NVal::DeclareNapiFunction("moveDir", MoveDir::Async), - NVal::DeclareNapiFunction("moveDirSync", MoveDir::Sync), - NVal::DeclareNapiFunction("moveFile", Move::Async), - NVal::DeclareNapiFunction("moveFileSync", Move::Sync), NVal::DeclareNapiFunction("open", Open::Async), NVal::DeclareNapiFunction("openSync", Open::Sync), NVal::DeclareNapiFunction("read", Read), NVal::DeclareNapiFunction("readSync", ReadSync), - NVal::DeclareNapiFunction("readText", ReadText::Async), - NVal::DeclareNapiFunction("readTextSync", ReadText::Sync), NVal::DeclareNapiFunction("rename", Rename::Async), NVal::DeclareNapiFunction("renameSync", Rename::Sync), NVal::DeclareNapiFunction("rmdir", Rmdirent::Async), NVal::DeclareNapiFunction("rmdirSync", Rmdirent::Sync), NVal::DeclareNapiFunction("stat", Stat::Async), NVal::DeclareNapiFunction("statSync", Stat::Sync), - NVal::DeclareNapiFunction("symlink", Symlink::Async), - NVal::DeclareNapiFunction("symlinkSync", Symlink::Sync), NVal::DeclareNapiFunction("truncate", Truncate::Async), NVal::DeclareNapiFunction("truncateSync", Truncate::Sync), NVal::DeclareNapiFunction("unlink", Unlink), NVal::DeclareNapiFunction("unlinkSync", UnlinkSync), NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("writeSync", WriteSync), + #ifndef WIN_PLATFORM + NVal::DeclareNapiFunction("copyDir", CopyDir::Async), + NVal::DeclareNapiFunction("copyDirSync", CopyDir::Sync), + NVal::DeclareNapiFunction("copyFile", CopyFile::Async), + NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync), + NVal::DeclareNapiFunction("createStream", CreateStream::Async), + NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), + NVal::DeclareNapiFunction("fdopenStream", FdopenStream::Async), + NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync), + NVal::DeclareNapiFunction("listFile", ListFile::Async), + NVal::DeclareNapiFunction("listFileSync", ListFile::Sync), + NVal::DeclareNapiFunction("moveDir", MoveDir::Async), + NVal::DeclareNapiFunction("moveDirSync", MoveDir::Sync), + NVal::DeclareNapiFunction("moveFile", Move::Async), + NVal::DeclareNapiFunction("moveFileSync", Move::Sync), + NVal::DeclareNapiFunction("readText", ReadText::Async), + NVal::DeclareNapiFunction("readTextSync", ReadText::Sync), + NVal::DeclareNapiFunction("symlink", Symlink::Async), + NVal::DeclareNapiFunction("symlinkSync", Symlink::Sync), NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), + #endif }); } +#ifdef WIN_PLATFORM +string PropNExporter::GetNExporterName() +#else string PropNExporter::GetClassName() +#endif { return PropNExporter::className_; } diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h index 50817ae74..7333185a4 100755 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h @@ -60,7 +60,11 @@ public: static napi_value Read(napi_env env, napi_callback_info info); static napi_value Write(napi_env env, napi_callback_info info); bool Export() override; +#ifdef WIN_PLATFORM + std::string GetNExporterName() override; +#else std::string GetClassName() override; +#endif PropNExporter(napi_env env, napi_value exports); ~PropNExporter() override; diff --git a/utils/filemgmt_libhilog/BUILD.gn b/utils/filemgmt_libhilog/BUILD.gn index abcf65651..342c7b438 100644 --- a/utils/filemgmt_libhilog/BUILD.gn +++ b/utils/filemgmt_libhilog/BUILD.gn @@ -14,6 +14,15 @@ import("//build/ohos.gni") import("//foundation/filemanagement/file_api/file_api.gni") +label_hilog = "${hiviewdfx_hilog_path}/interfaces/native/innerkits:libhilog" +if (use_mac) { + label_hilog = + "${hiviewdfx_hilog_path}/interfaces/native/innerkits:libhilog_mac" +} else if (use_mingw_win) { + label_hilog = + "${hiviewdfx_hilog_path}/interfaces/native/innerkits:libhilog_windows" +} + config("log_public_config") { visibility = [ ":*" ] @@ -22,8 +31,7 @@ config("log_public_config") { ohos_shared_library("filemgmt_libhilog") { public_configs = [ ":log_public_config" ] - public_deps = - [ "${hiviewdfx_hilog_path}/interfaces/native/innerkits:libhilog" ] + public_deps = [ label_hilog ] subsystem_name = "filemanagement" part_name = "file_api" diff --git a/utils/filemgmt_libn/BUILD.gn b/utils/filemgmt_libn/BUILD.gn index 09cff7b55..9032dd811 100644 --- a/utils/filemgmt_libn/BUILD.gn +++ b/utils/filemgmt_libn/BUILD.gn @@ -35,6 +35,12 @@ ohos_shared_library("filemgmt_libn") { "src/n_val.cpp", ] + cflags_cc = [ "-std=c++17" ] + + if (use_mingw_win) { + defines = [ "WIN_PLATFORM" ] + } + public_configs = [ ":libn_public_config" ] public_deps = [ "${arkui_napi_path}:ace_napi", diff --git a/utils/filemgmt_libn/include/n_error.h b/utils/filemgmt_libn/include/n_error.h index 1f5debc38..f56f661d2 100644 --- a/utils/filemgmt_libn/include/n_error.h +++ b/utils/filemgmt_libn/include/n_error.h @@ -27,11 +27,14 @@ namespace OHOS { namespace FileManagement { namespace LibN { -#ifdef IOS_PLATFORM +#if (defined IOS_PLATFORM) || (defined WIN_PLATFORM) constexpr int EBADR = 53; constexpr int EBADFD = 77; constexpr int ERESTART = 85; #endif +#ifdef WIN_PLATFORM +constexpr int EDQUOT = 122; +#endif constexpr int UNKROWN_ERR = -1; constexpr int ERRNO_NOERR = 0; constexpr int STORAGE_SERVICE_SYS_CAP_TAG = 13600000; diff --git a/utils/filemgmt_libn/include/n_exporter.h b/utils/filemgmt_libn/include/n_exporter.h index 2734dc0c2..4f4e6f826 100644 --- a/utils/filemgmt_libn/include/n_exporter.h +++ b/utils/filemgmt_libn/include/n_exporter.h @@ -31,7 +31,11 @@ public: NExporter(napi_env env, napi_value exports) : exports_(env, exports) {}; virtual ~NExporter() = default; virtual bool Export() = 0; +#ifdef WIN_PLATFORM + virtual std::string GetNExporterName() = 0; +#else virtual std::string GetClassName() = 0; +#endif protected: NVal exports_; diff --git a/utils/filemgmt_libn/include/n_val.h b/utils/filemgmt_libn/include/n_val.h index 02e98b18e..f721a8912 100644 --- a/utils/filemgmt_libn/include/n_val.h +++ b/utils/filemgmt_libn/include/n_val.h @@ -76,7 +76,11 @@ public: /* SHOULD ONLY BE USED FOR OBJECT */ bool HasProp(std::string propName) const; +#ifdef WIN_PLATFORM + NVal GetPropValue(std::string propName) const; +#else NVal GetProp(std::string propName) const; +#endif bool AddProp(std::vector &&propVec) const; bool AddProp(std::string propName, napi_value nVal) const; diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 6cc0ffb0b..8e2745122 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -238,7 +238,11 @@ bool NVal::HasProp(string propName) const return (status == napi_ok) && res; } +#ifdef WIN_PLATFORM +NVal NVal::GetPropValue(string propName) const +#else NVal NVal::GetProp(string propName) const +#endif { if (!HasProp(propName)) { return {env_, nullptr}; -- Gitee From cf1be0d25fc231c3db7afe578ec5a25e0bd6efaa Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Mon, 26 Jun 2023 10:16:36 +0800 Subject: [PATCH 2/2] fs_adapt_windows Signed-off-by: 18721213663 --- .../js/src/mod_fs/class_file/file_n_exporter.cpp | 4 ++-- interfaces/kits/js/src/mod_fs/common_func.cpp | 12 ++++++------ interfaces/kits/js/src/mod_fs/module.cpp | 6 +++--- interfaces/kits/js/src/mod_fs/properties/open.cpp | 4 ++-- .../js/src/mod_fs/properties/prop_n_exporter.cpp | 4 ++-- 5 files changed, 15 insertions(+), 15 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 6ae49a9e1..28fb937cf 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 @@ -216,11 +216,11 @@ bool FileNExporter::Export() { vector props = { NVal::DeclareNapiGetter("fd", GetFD), - #ifndef WIN_PLATFORM +#ifndef WIN_PLATFORM NVal::DeclareNapiFunction("lock", Lock), NVal::DeclareNapiFunction("tryLock", TryLock), NVal::DeclareNapiFunction("unlock", UnLock), - #endif +#endif }; #ifdef WIN_PLATFORM diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 08be08c57..bbb5a8556 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -76,11 +76,11 @@ static tuple GetActualLen(napi_env env, size_t bufLen, size_t bufO if (op.HasProp("length")) { int64_t opLength = 0; - #ifdef WIN_PLATFORM +#ifdef WIN_PLATFORM tie(succ, opLength) = op.GetPropValue("length").ToInt64(static_cast(retLen)); - #else +#else tie(succ, opLength) = op.GetProp("length").ToInt64(static_cast(retLen)); - #endif +#endif if (!succ || opLength < 0 || static_cast(opLength) > retLen) { HILOGE("Invalid option.length"); NError(EINVAL).ThrowErr(env); @@ -246,13 +246,13 @@ tuple CommonFunc::GetReadArg(napi_env env, HILOGE("Failed to get actual length"); return { false, nullptr, retLen, offset }; } - #ifdef WIN_PLATFORM +#ifdef WIN_PLATFORM if (op.HasProp("offset") && !op.GetPropValue("offset").TypeIs(napi_undefined)) { tie(succ, offset) = op.GetPropValue("offset").ToInt64(); - #else +#else if (op.HasProp("offset") && !op.GetProp("offset").TypeIs(napi_undefined)) { tie(succ, offset) = op.GetProp("offset").ToInt64(); - #endif +#endif if (!succ || offset < 0) { HILOGE("option.offset shall be positive number"); NError(EINVAL).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 3d9d08c36..0f32d92c3 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -44,11 +44,11 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); #endif for (auto &&product : products) { - #ifdef WIN_PLATFORM +#ifdef WIN_PLATFORM string nExporterName = product->GetNExporterName(); - #else +#else string nExporterName = product->GetClassName(); - #endif +#endif if (!product->Export()) { HILOGE("INNER BUG. Failed to export class %{public}s for module fileio", nExporterName.c_str()); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 9daf5febc..9d1c5a929 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -287,7 +287,7 @@ napi_value Open::Async(napi_env env, napi_callback_info info) auto argv = funcArg[NARG_POS::FIRST]; auto cbExec = [arg, argv, path = string(path.get()), mode = mode, env = env]() -> NError { string pathStr = path; - #ifndef WIN_PLATFORM +#ifndef WIN_PLATFORM int fd = -1; if (RemoteUri::IsMediaUri(path)) { int ret = OpenFileByDatashare(path, mode); @@ -312,7 +312,7 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Failed to open file by RemoteUri"); return NError(E_INVAL); } - #endif +#endif std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index ab8004f39..bdf5fe8e2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -587,7 +587,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("unlinkSync", UnlinkSync), NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("writeSync", WriteSync), - #ifndef WIN_PLATFORM +#ifndef WIN_PLATFORM NVal::DeclareNapiFunction("copyDir", CopyDir::Async), NVal::DeclareNapiFunction("copyDirSync", CopyDir::Sync), NVal::DeclareNapiFunction("copyFile", CopyFile::Async), @@ -607,7 +607,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("symlink", Symlink::Async), NVal::DeclareNapiFunction("symlinkSync", Symlink::Sync), NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), - #endif +#endif }); } -- Gitee