From 8a6991ec256ed6d5b481eb9f0291653ce6f5d76b Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Tue, 30 Aug 2022 15:38:57 +0800 Subject: [PATCH] add iterator code Signed-off-by: linjun9528 --- .../include/file_access_ext_ability.h | 7 +- .../include/file_access_ext_proxy.h | 5 +- .../include/file_access_ext_stub_impl.h | 5 +- .../include/file_access_extension_info.h | 13 ++ .../file_access/include/file_access_helper.h | 8 +- .../include/ifile_access_ext_base.h | 5 +- .../include/js_file_access_ext_ability.h | 5 +- .../src/file_access_ext_ability.cpp | 11 +- .../file_access/src/file_access_ext_proxy.cpp | 47 ++-- .../file_access/src/file_access_ext_stub.cpp | 43 +++- .../src/file_access_ext_stub_impl.cpp | 19 +- .../file_access/src/file_access_helper.cpp | 44 ++-- .../src/js_file_access_ext_ability.cpp | 121 +++++----- .../kits/napi/file_access_module/BUILD.gn | 8 +- .../file_info/file_info_entity.h | 30 +++ .../file_info/file_iterator_entity.h | 38 ++++ .../file_info/napi_file_info_exporter.cpp | 209 ++++++++++++++++++ .../file_info/napi_file_info_exporter.h | 50 +++++ .../file_info/napi_file_iterator_exporter.cpp | 151 +++++++++++++ .../file_info/napi_file_iterator_exporter.h | 44 ++++ .../napi_fileaccess_helper.cpp | 101 +++------ .../napi_fileaccess_helper.h | 3 +- .../file_access_module/napi_notify_callback.h | 4 +- .../native_fileaccess_module.cpp | 27 ++- .../root_info/napi_root_info_exporter.cpp | 203 +++++++++++++++++ .../root_info/napi_root_info_exporter.h | 48 ++++ .../root_info/napi_root_iterator_exporter.cpp | 120 ++++++++++ .../root_info/napi_root_iterator_exporter.h | 41 ++++ .../root_info/root_info_entity.h | 30 +++ .../root_info/root_iterator_entity.h | 35 +++ .../FileExtensionAbility.ts | 15 +- utils/file_access_framework_errno.h | 3 +- 32 files changed, 1284 insertions(+), 209 deletions(-) create mode 100644 interfaces/kits/napi/file_access_module/file_info/file_info_entity.h create mode 100644 interfaces/kits/napi/file_access_module/file_info/file_iterator_entity.h create mode 100644 interfaces/kits/napi/file_access_module/file_info/napi_file_info_exporter.cpp create mode 100644 interfaces/kits/napi/file_access_module/file_info/napi_file_info_exporter.h create mode 100644 interfaces/kits/napi/file_access_module/file_info/napi_file_iterator_exporter.cpp create mode 100644 interfaces/kits/napi/file_access_module/file_info/napi_file_iterator_exporter.h create mode 100644 interfaces/kits/napi/file_access_module/root_info/napi_root_info_exporter.cpp create mode 100644 interfaces/kits/napi/file_access_module/root_info/napi_root_info_exporter.h create mode 100644 interfaces/kits/napi/file_access_module/root_info/napi_root_iterator_exporter.cpp create mode 100644 interfaces/kits/napi/file_access_module/root_info/napi_root_iterator_exporter.h create mode 100644 interfaces/kits/napi/file_access_module/root_info/root_info_entity.h create mode 100644 interfaces/kits/napi/file_access_module/root_info/root_iterator_entity.h diff --git a/frameworks/innerkits/file_access/include/file_access_ext_ability.h b/frameworks/innerkits/file_access/include/file_access_ext_ability.h index 09e72499..f9964058 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_ability.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_ability.h @@ -46,9 +46,10 @@ public: virtual int Delete(const Uri &sourceFile); virtual int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile); virtual int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile); - virtual std::vector ListFile(const Uri &sourceFile); - virtual std::vector GetRoots(); - virtual int Access(const Uri &uri, bool &isExist); + virtual int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec); + virtual int GetRoots(std::vector &rootInfoVec); + virtual int Access(const Uri &uri, bool &isExist); virtual int RegisterNotify(sptr ¬ify); virtual int UnregisterNotify(sptr ¬ify); virtual int Notify(const NotifyMessage &message); diff --git a/frameworks/innerkits/file_access/include/file_access_ext_proxy.h b/frameworks/innerkits/file_access/include/file_access_ext_proxy.h index 9a9a82c3..c320d1f2 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_proxy.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_proxy.h @@ -42,8 +42,9 @@ public: virtual int Delete(const Uri &sourceFile) override; virtual int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override; virtual int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override; - virtual std::vector ListFile(const Uri &sourceFile) override; - virtual std::vector GetRoots() override; + virtual int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec) override; + virtual int GetRoots(std::vector &rootInfoVec) override; virtual int Access(const Uri &uri, bool &isExist) override; virtual int RegisterNotify(sptr ¬ify) override; virtual int UnregisterNotify(sptr ¬ify) override; diff --git a/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h index 225990db..049f1f60 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h @@ -40,8 +40,9 @@ public: int Delete(const Uri &sourceFile) override; int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override; int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override; - std::vector ListFile(const Uri &sourceFileUri) override; - std::vector GetRoots() override; + int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec) override; + int GetRoots(std::vector &rootInfoVec) override; int Access(const Uri &uri, bool &isExist) override; int RegisterNotify(sptr ¬ify) override; int UnregisterNotify(sptr ¬ify) override; diff --git a/frameworks/innerkits/file_access/include/file_access_extension_info.h b/frameworks/innerkits/file_access/include/file_access_extension_info.h index 7254fd48..289d277a 100644 --- a/frameworks/innerkits/file_access/include/file_access_extension_info.h +++ b/frameworks/innerkits/file_access/include/file_access_extension_info.h @@ -17,8 +17,10 @@ #define FILE_ACCESS_EXTENSION_INFO_H #include +#include #include +#include "hilog_wrapper.h" #include "parcel.h" #include "uri.h" @@ -58,6 +60,17 @@ public: int64_t mtime {0}; std::string mimeType; + FileInfo &operator=(const FileInfo &other) + { + uri = other.uri; + fileName = other.fileName; + mode = other.mode; + size = other.size; + mtime = other.mtime; + mimeType = other.mimeType; + return *this; + } + bool ReadFromParcel(Parcel &parcel) { uri = parcel.ReadString(); diff --git a/frameworks/innerkits/file_access/include/file_access_helper.h b/frameworks/innerkits/file_access/include/file_access_helper.h index ef04a98d..208437b8 100644 --- a/frameworks/innerkits/file_access/include/file_access_helper.h +++ b/frameworks/innerkits/file_access/include/file_access_helper.h @@ -47,8 +47,7 @@ struct ConnectInfo { class FileAccessHelper final : public std::enable_shared_from_this { public: ~FileAccessHelper() = default; - // get all ability want info - static std::vector GetRegisterFileAccessExtAbilityInfo(); + static int GetRegisterFileAccessExtAbilityInfo(std::vector &wantVec); // create and connect all ability static std::shared_ptr Creator(const std::shared_ptr &context); // create and connect with want, if created, only connect with want @@ -65,8 +64,9 @@ public: int Delete(Uri &selectFile); int Move(Uri &sourceFile, Uri &targetParent, Uri &newFile); int Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile); - std::vector ListFile(Uri &sourceFile); - std::vector GetRoots(); + int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec); + int GetRoots(std::vector &rootInfoVec); int On(std::shared_ptr &callback); int Off(); private: diff --git a/frameworks/innerkits/file_access/include/ifile_access_ext_base.h b/frameworks/innerkits/file_access/include/ifile_access_ext_base.h index 4823a7dc..4fd85d23 100644 --- a/frameworks/innerkits/file_access/include/ifile_access_ext_base.h +++ b/frameworks/innerkits/file_access/include/ifile_access_ext_base.h @@ -52,8 +52,9 @@ public: virtual int Delete(const Uri &sourceFile) = 0; virtual int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) = 0; virtual int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) = 0; - virtual std::vector ListFile(const Uri &sourceFile) = 0; - virtual std::vector GetRoots() = 0; + virtual int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec) = 0; + virtual int GetRoots(std::vector &rootInfoVec) = 0; virtual int Access(const Uri &uri, bool &isExist) = 0; virtual int RegisterNotify(sptr ¬ify) = 0; virtual int UnregisterNotify(sptr ¬ify) = 0; diff --git a/frameworks/innerkits/file_access/include/js_file_access_ext_ability.h b/frameworks/innerkits/file_access/include/js_file_access_ext_ability.h index cb86a899..38f95f0a 100644 --- a/frameworks/innerkits/file_access/include/js_file_access_ext_ability.h +++ b/frameworks/innerkits/file_access/include/js_file_access_ext_ability.h @@ -66,8 +66,9 @@ public: int Delete(const Uri &sourceFile) override; int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override; int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override; - std::vector ListFile(const Uri &sourceFile) override; - std::vector GetRoots() override; + int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec) override; + int GetRoots(std::vector &rootInfoVec) override; int Access(const Uri &uri, bool &isExist) override; private: diff --git a/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp b/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp index 6c7c3373..c526b293 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp @@ -97,18 +97,17 @@ int FileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &displ return ERR_OPERATION_NOT_SUPPORT; } -std::vector FileAccessExtAbility::ListFile(const Uri &sourceFile) +int FileAccessExtAbility::ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec) { - std::vector vec; HILOG_ERROR("FileAccessExtAbility::ListFile Undefined operation"); - return vec; + return ERR_OPERATION_NOT_SUPPORT; } -std::vector FileAccessExtAbility::GetRoots() +int FileAccessExtAbility::GetRoots(std::vector &rootInfoVec) { - std::vector vec; HILOG_ERROR("FileAccessExtAbility::GetRoots Undefined operation"); - return vec; + return ERR_OPERATION_NOT_SUPPORT; } int FileAccessExtAbility::Access(const Uri &uri, bool &isExist) diff --git a/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp b/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp index 013236fc..c2f9fcd5 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp @@ -327,21 +327,33 @@ int FileAccessExtProxy::Rename(const Uri &sourceFile, const std::string &display return ret; } -std::vector FileAccessExtProxy::ListFile(const Uri &sourceFile) +int FileAccessExtProxy::ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "ListFile"); - std::vector vec; MessageParcel data; if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { HILOG_ERROR("WriteInterfaceToken failed"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ERR_IPC_ERROR; } - if (!data.WriteParcelable(&sourceFile)) { - HILOG_ERROR("fail to WriteParcelable sourceFileUri"); + if (!data.WriteParcelable(&fileInfo)) { + HILOG_ERROR("fail to WriteParcelable fileInfo"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_IPC_ERROR; + } + + if (!data.WriteInt64(offset)) { + HILOG_ERROR("fail to WriteInt64 offset"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_IPC_ERROR; + } + + if (!data.WriteInt64(maxCount)) { + HILOG_ERROR("fail to WriteInt64 maxCount"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ERR_IPC_ERROR; } MessageParcel reply; @@ -350,31 +362,30 @@ std::vector FileAccessExtProxy::ListFile(const Uri &sourceFile) if (err != NO_ERROR) { HILOG_ERROR("fail to SendRequest. err: %{public}d", err); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return err; } - vec.clear(); + fileInfoVec.clear(); int64_t count = reply.ReadInt64(); - for (int32_t i = 0; i < count; i++) { + for (int64_t i = 0; i < count; i++) { std::unique_ptr fileInfo(reply.ReadParcelable()); if (fileInfo != nullptr) { - vec.push_back(*fileInfo); + fileInfoVec.push_back(*fileInfo); } } FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ERR_OK; } -std::vector FileAccessExtProxy::GetRoots() +int FileAccessExtProxy::GetRoots(std::vector &rootInfoVec) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetRoots"); - std::vector vec; MessageParcel data; if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { HILOG_ERROR("WriteInterfaceToken failed"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ERR_IPC_ERROR; } MessageParcel reply; @@ -383,20 +394,20 @@ std::vector FileAccessExtProxy::GetRoots() if (err != NO_ERROR) { HILOG_ERROR("fail to SendRequest. err: %{public}d", err); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ERR_IPC_ERROR; } - vec.clear(); + rootInfoVec.clear(); uint64_t count = reply.ReadUint64(); for (uint64_t i = 0; i < count; i++) { std::unique_ptr rootInfo(reply.ReadParcelable()); if (rootInfo != nullptr) { - vec.push_back(*rootInfo); + rootInfoVec.push_back(*rootInfo); } } FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ERR_OK; } int FileAccessExtProxy::Access(const Uri &uri, bool &isExist) diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp index 725711cf..1ba9d74f 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp @@ -337,22 +337,42 @@ ErrCode FileAccessExtStub::CmdRename(MessageParcel &data, MessageParcel &reply) ErrCode FileAccessExtStub::CmdListFile(MessageParcel &data, MessageParcel &reply) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CmdListFile"); - std::shared_ptr uri(data.ReadParcelable()); - if (uri == nullptr) { + std::shared_ptr fileInfo(data.ReadParcelable()); + if (fileInfo == nullptr) { HILOG_ERROR("parameter ListFile uri is nullptr"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return ERR_INVALID_URI; + return ERR_IPC_ERROR; } - std::vector vec = ListFile(*uri); - uint64_t count {vec.size()}; - if (!reply.WriteUint64(count)) { + int offset = data.ReadInt64(); + if (offset < 0) { + HILOG_ERROR("parameter ListFile offset is invalid"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_IPC_ERROR; + } + + int maxCount = data.ReadInt64(); + if (maxCount < 0) { + HILOG_ERROR("parameter ListFile maxCount is invalid"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_IPC_ERROR; + } + + std::vector vec ;//= ListFile(*uri); + int ret = ListFile(*fileInfo, offset, maxCount, vec); + if (ret != ERR_OK) { + HILOG_ERROR("parameter ListFile fail"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + int64_t count {vec.size()}; + if (!reply.WriteInt64(count)) { HILOG_ERROR("parameter ListFile fail to WriteInt32 count"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return ERR_IPC_ERROR; } - for (uint64_t i = 0; i < count; i++) { + for (int64_t i = 0; i < count; i++) { if (!reply.WriteParcelable(&vec[i])) { HILOG_ERROR("parameter ListFile fail to WriteParcelable vec"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); @@ -367,7 +387,14 @@ ErrCode FileAccessExtStub::CmdListFile(MessageParcel &data, MessageParcel &reply ErrCode FileAccessExtStub::CmdGetRoots(MessageParcel &data, MessageParcel &reply) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CmdGetRoots"); - std::vector vec = GetRoots(); + std::vector vec; + int ret = GetRoots(vec); + if (ret != ERR_OK) { + HILOG_ERROR("parameter GetRoots fail"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + uint64_t count {vec.size()}; if (!reply.WriteUint64(count)) { HILOG_ERROR("parameter GetRoots fail to WriteInt32 count"); diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp index 927301cd..5475391b 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp @@ -110,34 +110,33 @@ int FileAccessExtStubImpl::Rename(const Uri &sourceFile, const std::string &disp return ret; } -std::vector FileAccessExtStubImpl::ListFile(const Uri &sourceFile) +int FileAccessExtStubImpl::ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "ListFile"); - std::vector vec; if (extension_ == nullptr) { HILOG_ERROR("ListFile get extension failed."); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ERR_IPC_ERROR; } - vec = extension_->ListFile(sourceFile); + int ret = extension_->ListFile(fileInfo, offset, maxCount, fileInfoVec); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ret; } -std::vector FileAccessExtStubImpl::GetRoots() +int FileAccessExtStubImpl::GetRoots(std::vector &rootInfoVec) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetRoots"); - std::vector vec; if (extension_ == nullptr) { HILOG_ERROR("GetRoots get extension failed."); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ERR_IPC_ERROR; } - vec = extension_->GetRoots(); + int ret = extension_->GetRoots(rootInfoVec); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return vec; + return ret; } int FileAccessExtStubImpl::Access(const Uri &uri, bool &isExist) diff --git a/frameworks/innerkits/file_access/src/file_access_helper.cpp b/frameworks/innerkits/file_access/src/file_access_helper.cpp index d3a0bb4d..f581f1f9 100644 --- a/frameworks/innerkits/file_access/src/file_access_helper.cpp +++ b/frameworks/innerkits/file_access/src/file_access_helper.cpp @@ -538,38 +538,45 @@ int FileAccessHelper::Rename(Uri &sourceFile, const std::string &displayName, Ur return index; } -std::vector FileAccessHelper::ListFile(Uri &sourceFile) +int FileAccessHelper::ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "ListFile"); - std::vector results; + Uri sourceFile(fileInfo.uri); if (!CheckUri(sourceFile)) { HILOG_ERROR("sourceFile format check error."); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return results; + return ERR_INVALID_URI; } sptr fileExtProxy = GetProxyByUri(sourceFile); if (fileExtProxy == nullptr) { HILOG_ERROR("failed with invalid fileAccessExtProxy"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return results; + return ERR_IPC_ERROR; } - results = fileExtProxy->ListFile(sourceFile); + HILOG_INFO("fileInfo.uri:%{public}s", fileInfo.uri.c_str()); + int ret = fileExtProxy->ListFile(fileInfo, offset, maxCount, fileInfoVec); + if (ret != ERR_OK) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + HILOG_INFO("fileInfoVec.size():%{public}zu,offset:%{public}" PRId64 ",maxCount:%{public}" PRId64, fileInfoVec.size(), offset, maxCount); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return results; + return ERR_OK; } -std::vector FileAccessHelper::GetRoots() +int FileAccessHelper::GetRoots(std::vector &rootInfoVec) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetRoots"); - std::vector rootsInfo; if (!GetProxy()) { HILOG_ERROR("failed with invalid fileAccessExtProxy"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return rootsInfo; + return ERR_IPC_ERROR; } + rootInfoVec.clear(); for (auto iter = cMap_.begin(); iter != cMap_.end(); ++iter) { auto connectInfo = iter->second; auto fileAccessExtProxy = connectInfo->fileAccessExtConnection->GetFileExtProxy(); @@ -577,34 +584,37 @@ std::vector FileAccessHelper::GetRoots() if (fileAccessExtProxy) { AddFileAccessDeathRecipient(fileAccessExtProxy->AsObject()); } - results = fileAccessExtProxy->GetRoots(); - rootsInfo.insert(rootsInfo.end(), results.begin(), results.end()); + int ret = fileAccessExtProxy->GetRoots(results); + if (ret != ERR_OK) { + return ret; + } + rootInfoVec.insert(rootInfoVec.end(), results.begin(), results.end()); } - return rootsInfo; + return ERR_OK; } -std::vector FileAccessHelper::GetRegisterFileAccessExtAbilityInfo() +int FileAccessHelper::GetRegisterFileAccessExtAbilityInfo(std::vector &wantVec) { - std::vector wants; std::vector extensionInfos; sptr bm = FileAccessHelper::GetBundleMgrProxy(); bool ret = bm->QueryExtensionAbilityInfos( AppExecFwk::ExtensionAbilityType::FILEACCESS_EXTENSION, DEFAULT_USERID, extensionInfos); if (!ret) { HILOG_ERROR("FileAccessHelper::GetRegisterFileAccessExtAbilityInfo QueryExtensionAbilityInfos error"); - return wants; + return ERR_INVALID_PARAM; } + wantVec.clear(); FileAccessHelper::wantsMap_.clear(); for (size_t i = 0; i < extensionInfos.size(); i++) { AAFwk::Want want; want.SetElementName(extensionInfos[i].bundleName, extensionInfos[i].name); FileAccessHelper::wantsMap_.insert(std::pair(extensionInfos[i].bundleName, want)); - wants.push_back(want); + wantVec.push_back(want); } - return wants; + return ERR_OK; } int FileAccessHelper::Access(Uri &uri, bool &isExist) diff --git a/frameworks/innerkits/file_access/src/js_file_access_ext_ability.cpp b/frameworks/innerkits/file_access/src/js_file_access_ext_ability.cpp index e3dcdcc3..8c650efe 100644 --- a/frameworks/innerkits/file_access/src/js_file_access_ext_ability.cpp +++ b/frameworks/innerkits/file_access/src/js_file_access_ext_ability.cpp @@ -659,98 +659,96 @@ int JsFileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &dis return errCode; } -static bool ParserListFileJsResult(NativeEngine &engine, NativeValue *nativeValue, - std::shared_ptr>> &result) +int JsFileAccessExtAbility::ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, + std::vector &fileInfoVec) { - if (result == nullptr) { - HILOG_ERROR("result is nullptr."); - return false; - } - - NativeObject *obj = ConvertNativeValueTo(nativeValue); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); - return false; - } - - bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), result->code); - NativeArray *nativeArray = ConvertNativeValueTo(obj->GetProperty("infos")); - for (uint32_t i = 0; i < nativeArray->GetLength(); i++) { - NativeValue *nativeFileInfo = nativeArray->GetElement(i); - if (nativeFileInfo == nullptr) { - HILOG_ERROR("get native FileInfo fail."); + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "ListFile"); + std::string sourceFile = fileInfo.uri; + auto value = std::make_shared>>(); + auto argParser = [sourceFile, offset, maxCount](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { + NativeValue *uri = engine.CreateString(sourceFile.c_str(), sourceFile.length()); + if (uri == nullptr) { + HILOG_ERROR("create sourceFile uri native js value fail."); return false; } - obj = ConvertNativeValueTo(nativeFileInfo); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); + NativeValue *nativeOffset = engine.CreateNumber(offset); + if (nativeOffset == nullptr) { + HILOG_ERROR("create nativeMaxNum native js value fail."); return false; } - FileInfo fileInfo; - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("uri"), fileInfo.uri); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("fileName"), fileInfo.fileName); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mode"), fileInfo.mode); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("size"), fileInfo.size); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mtime"), fileInfo.mtime); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mimeType"), fileInfo.mimeType); - if (!ret) { - HILOG_ERROR("Convert js value fail."); - return ret; - } - (result->data).emplace_back(std::move(fileInfo)); - } - return true; -} - -std::vector JsFileAccessExtAbility::ListFile(const Uri &sourceFile) -{ - StartTrace(HITRACE_TAG_FILEMANAGEMENT, "ListFile"); - auto value = std::make_shared>>(); - auto argParser = [sourceFile](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *uri = engine.CreateString(sourceFile.ToString().c_str(), sourceFile.ToString().length()); - if (uri == nullptr) { - HILOG_ERROR("create sourceFile uri native js value fail."); + NativeValue *nativeMaxCount = engine.CreateNumber(maxCount); + if (nativeMaxCount == nullptr) { + HILOG_ERROR("create nativeMaxNum native js value fail."); return false; } argv[ARGC_ZERO] = uri; - argc = ARGC_ONE; + argv[ARGC_ONE] = nativeOffset; + argv[ARGC_TWO] = nativeMaxCount; + argc = ARGC_THREE; return true; }; auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { - std::shared_ptr>> fileInfo = std::make_shared>>(); - if (fileInfo == nullptr) { - HILOG_ERROR("new Value fail."); + NativeObject *obj = ConvertNativeValueTo(result); + if (obj == nullptr) { + HILOG_ERROR("Convert js object fail."); return false; } - - bool ret = ParserListFileJsResult(engine, result, fileInfo); - if (!ret) { - HILOG_ERROR("Parser js value fail."); - return ret; + bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), value->code); + NativeArray *nativeArray = ConvertNativeValueTo(obj->GetProperty("infos")); + if (nativeArray == nullptr) { + HILOG_ERROR("Convert js array object fail."); + return false; } - value->code = fileInfo->code; - value->data = std::move(fileInfo->data); + for (uint32_t i = 0; i < nativeArray->GetLength(); i++) { + NativeValue *nativeFileInfo = nativeArray->GetElement(i); + if (nativeFileInfo == nullptr) { + HILOG_ERROR("get native FileInfo fail."); + return false; + } + + obj = ConvertNativeValueTo(nativeFileInfo); + if (obj == nullptr) { + HILOG_ERROR("Convert js object fail."); + return false; + } + FileInfo fileInfo; + ret = ret && ConvertFromJsValue(engine, obj->GetProperty("uri"), fileInfo.uri); + ret = ret && ConvertFromJsValue(engine, obj->GetProperty("fileName"), fileInfo.fileName); + ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mode"), fileInfo.mode); + ret = ret && ConvertFromJsValue(engine, obj->GetProperty("size"), fileInfo.size); + ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mtime"), fileInfo.mtime); + ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mimeType"), fileInfo.mimeType); + if (!ret) { + HILOG_ERROR("Convert js value fail."); + return ret; + } + + (value->data).emplace_back(std::move(fileInfo)); + } return true; }; auto errCode = CallJsMethod("listFile", jsRuntime_, jsObj_.get(), argParser, retParser); if (errCode != ERR_OK) { HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode); + return errCode; } if (value->code != ERR_OK) { HILOG_ERROR("fileio fail."); + return ERR_FILEIO_FAIL; } + fileInfoVec = std::move(value->data); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return value->data; + return ERR_OK; } -std::vector JsFileAccessExtAbility::GetRoots() +int JsFileAccessExtAbility::GetRoots(std::vector &RootInfoVec) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetRoots"); auto value = std::make_shared>>(); @@ -797,14 +795,17 @@ std::vector JsFileAccessExtAbility::GetRoots() auto errCode = CallJsMethod("getRoots", jsRuntime_, jsObj_.get(), argParser, retParser); if (errCode != ERR_OK) { HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode); + return errCode; } if (value->code != ERR_OK) { HILOG_ERROR("fileio fail."); + return ERR_FILEIO_FAIL; } + RootInfoVec = value->data; FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return value->data; + return ERR_OK; } int JsFileAccessExtAbility::Access(const Uri &uri, bool &isExist) diff --git a/interfaces/kits/napi/file_access_module/BUILD.gn b/interfaces/kits/napi/file_access_module/BUILD.gn index 3e556e12..b0b76b08 100644 --- a/interfaces/kits/napi/file_access_module/BUILD.gn +++ b/interfaces/kits/napi/file_access_module/BUILD.gn @@ -22,16 +22,22 @@ ohos_shared_library("fileaccess") { include_dirs = [ "./", - "${BASE_DIR}/utils", "${BASE_DIR}/interfaces/kits/napi/common", + "${BASE_DIR}/interfaces/kits/napi/file_access_module/file_info", + "${BASE_DIR}/interfaces/kits/napi/file_access_module/root_info", + "${BASE_DIR}/utils", "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn/include", ] sources = [ "${BASE_DIR}/interfaces/kits/napi/common/file_extension_info_napi.cpp", + "file_info/napi_file_info_exporter.cpp", + "file_info/napi_file_iterator_exporter.cpp", "napi_fileaccess_helper.cpp", "napi_notify_callback.cpp", "native_fileaccess_module.cpp", + "root_info/napi_root_info_exporter.cpp", + "root_info/napi_root_iterator_exporter.cpp", ] deps = [ diff --git a/interfaces/kits/napi/file_access_module/file_info/file_info_entity.h b/interfaces/kits/napi/file_access_module/file_info/file_info_entity.h new file mode 100644 index 00000000..e668b1f6 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/file_info/file_info_entity.h @@ -0,0 +1,30 @@ +/* + * 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 FILE_INFO_ENTITY_H +#define FILE_INFO_ENTITY_H + +#include "file_access_extension_info.h" +#include "file_access_helper.h" + +namespace OHOS { +namespace FileAccessFwk { +struct FileInfoEntity { + FileAccessHelper *fileAccessHelper { nullptr }; + FileInfo fileInfo {}; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_INFO_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/file_info/file_iterator_entity.h b/interfaces/kits/napi/file_access_module/file_info/file_iterator_entity.h new file mode 100644 index 00000000..c1719199 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/file_info/file_iterator_entity.h @@ -0,0 +1,38 @@ +/* + * 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 FILE_ITERATOR_ENTITY_H +#define FILE_ITERATOR_ENTITY_H + +#include + +#include "file_access_extension_info.h" +#include "file_access_helper.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +struct FileIteratorEntity { + FileAccessHelper *fileAccessHelper { nullptr }; + std::mutex entityOperateMutex; + FileInfo fileInfo {}; + std::vector fileInfoVec {}; + int64_t maxCount { 100 }; + int64_t offset { 0 }; + int64_t pos { 0 }; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_ITERATOR_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/file_info/napi_file_info_exporter.cpp b/interfaces/kits/napi/file_access_module/file_info/napi_file_info_exporter.cpp new file mode 100644 index 00000000..6bab3b78 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/file_info/napi_file_info_exporter.cpp @@ -0,0 +1,209 @@ +/* + * 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 "napi_file_info_exporter.h" + +#include "file_access_framework_errno.h" +#include "file_info_entity.h" +#include "file_iterator_entity.h" +#include "hilog_wrapper.h" +#include "napi_file_iterator_exporter.h" + +namespace OHOS { +namespace FileAccessFwk { +bool NapiFileInfoExporter::Export() +{ + std::vector props = { + NVal::DeclareNapiFunction("listFile", ListFile), + NVal::DeclareNapiGetter("uri", GetUri), + NVal::DeclareNapiGetter("fileName", GetFileName), + NVal::DeclareNapiGetter("mode", GetMode), + NVal::DeclareNapiGetter("size", GetSize), + NVal::DeclareNapiGetter("mtime", GetMtime), + NVal::DeclareNapiGetter("mimeType", GetMimeType), + }; + + std::string className = GetClassName(); + bool succ = false; + napi_value classValue = nullptr; + std::tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, + NapiFileInfoExporter::Constructor, std::move(props)); + if (!succ) { + HILOG_ERROR("Failed to define class NapiFileInfoExporter"); + NError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class NapiFileInfoExporter"); + return false; + } + + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + HILOG_ERROR("Failed to save class NapiFileInfoExporter"); + NError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class NapiFileInfoExporter"); + return false; + } + + return exports_.AddProp(className, classValue); +} + +napi_value NapiFileInfoExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto fileInfoEntity = std::make_unique(); + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), std::move(fileInfoEntity))) { + NError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj FileInfoEntity"); + return nullptr; + } + return funcArg.GetThisVar(); +} + +napi_value NapiFileInfoExporter::ListFile(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto fileInfoEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (fileInfoEntity == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot get entity of FileInfoEntity"); + return nullptr; + } + + if ((fileInfoEntity->fileInfo.mode & DOCUMENT_FLAG_REPRESENTS_DIR) != DOCUMENT_FLAG_REPRESENTS_DIR) { + HILOG_ERROR("current FileInfo is not dir."); + return NVal::CreateUndefined(env).val_; + } + + if (fileInfoEntity->fileAccessHelper == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "fileAccessHelper is null."); + return nullptr; + } + + auto objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {}); + if (objFileIteratorExporter == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot instantiate class NapiFileIteratorExporter"); + return nullptr; + } + + auto fileIteratorEntity = NClass::GetEntityOf(env, objFileIteratorExporter); + if (fileIteratorEntity == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot get the entity of FileIteratorEntity"); + return nullptr; + } + + { + std::lock_guard lock(fileIteratorEntity->entityOperateMutex); + fileIteratorEntity->fileAccessHelper = fileInfoEntity->fileAccessHelper; + fileIteratorEntity->fileInfo = fileInfoEntity->fileInfo; + fileIteratorEntity->offset = 0; + fileIteratorEntity->pos = 0; + auto ret = fileInfoEntity->fileAccessHelper->ListFile(fileInfoEntity->fileInfo, fileIteratorEntity->offset, + fileIteratorEntity->maxCount, fileIteratorEntity->fileInfoVec); + if (ret != ERR_OK) { + NError(ret).ThrowErr(env, "exec ListFile fail"); + return nullptr; + } + } + + return NVal(env, objFileIteratorExporter).val_; +} + +static FileInfoEntity *GetFileInfoEntity(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto fileInfoEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (fileInfoEntity == nullptr) { + NError(EIO).ThrowErr(env, "Cannot get entity of FileInfoEntity"); + return nullptr; + } + return fileInfoEntity; +} + +napi_value NapiFileInfoExporter::GetUri(napi_env env, napi_callback_info info) +{ + auto fileInfoEntity = GetFileInfoEntity(env, info); + if (fileInfoEntity == nullptr) { + HILOG_ERROR("Failed to get entity of FileInfoEntity"); + return NVal::CreateUndefined(env).val_; + } + return NVal::CreateUTF8String(env, fileInfoEntity->fileInfo.uri).val_; +} + +napi_value NapiFileInfoExporter::GetFileName(napi_env env, napi_callback_info info) +{ + auto fileInfoEntity = GetFileInfoEntity(env, info); + if (fileInfoEntity == nullptr) { + HILOG_ERROR("Failed to get entity of FileInfoEntity"); + return NVal::CreateUndefined(env).val_; + } + return NVal::CreateUTF8String(env, fileInfoEntity->fileInfo.fileName).val_; +} + +napi_value NapiFileInfoExporter::GetMode(napi_env env, napi_callback_info info) +{ + auto fileInfoEntity = GetFileInfoEntity(env, info); + if (fileInfoEntity == nullptr) { + HILOG_ERROR("Failed to get entity of FileInfoEntity"); + return NVal::CreateUndefined(env).val_; + } + return NVal::CreateInt32(env, fileInfoEntity->fileInfo.mode).val_; +} + +napi_value NapiFileInfoExporter::GetSize(napi_env env, napi_callback_info info) +{ + auto fileInfoEntity = GetFileInfoEntity(env, info); + if (fileInfoEntity == nullptr) { + HILOG_ERROR("Failed to get entity of FileInfoEntity"); + return NVal::CreateUndefined(env).val_; + } + return NVal::CreateInt64(env, fileInfoEntity->fileInfo.size).val_; +} + +napi_value NapiFileInfoExporter::GetMtime(napi_env env, napi_callback_info info) +{ + auto fileInfoEntity = GetFileInfoEntity(env, info); + if (fileInfoEntity == nullptr) { + HILOG_ERROR("Failed to get entity of FileInfoEntity"); + return NVal::CreateUndefined(env).val_; + } + return NVal::CreateInt64(env, fileInfoEntity->fileInfo.mtime).val_; +} + +napi_value NapiFileInfoExporter::GetMimeType(napi_env env, napi_callback_info info) +{ + auto fileInfoEntity = GetFileInfoEntity(env, info); + if (fileInfoEntity == nullptr) { + HILOG_ERROR("Failed to get entity of FileInfoEntity"); + return NVal::CreateUndefined(env).val_; + } + return NVal::CreateUTF8String(env, fileInfoEntity->fileInfo.mimeType).val_; +} + +std::string NapiFileInfoExporter::GetClassName() +{ + return NapiFileInfoExporter::className_; +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/file_info/napi_file_info_exporter.h b/interfaces/kits/napi/file_access_module/file_info/napi_file_info_exporter.h new file mode 100644 index 00000000..57264e33 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/file_info/napi_file_info_exporter.h @@ -0,0 +1,50 @@ +/* + * 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 NAPI_FILE_INFO_EXPORTER_H +#define NAPI_FILE_INFO_EXPORTER_H + +#include + +#include "file_access_extension_info.h" +#include "file_access_helper.h" +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileAccessFwk { +using namespace FileManagement::LibN; + +class NapiFileInfoExporter final : public NExporter { +public: + inline static const std::string className_ = "NapiFileInfoExporter"; + + NapiFileInfoExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}; + ~NapiFileInfoExporter() = default; + + static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value ListFile(napi_env env, napi_callback_info info); + static napi_value GetUri(napi_env env, napi_callback_info info); + static napi_value GetFileName(napi_env env, napi_callback_info info); + static napi_value GetMode(napi_env env, napi_callback_info info); + static napi_value GetSize(napi_env env, napi_callback_info info); + static napi_value GetMtime(napi_env env, napi_callback_info info); + static napi_value GetMimeType(napi_env env, napi_callback_info info); + + bool Export() override; + std::string GetClassName() override; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // NAPI_FILE_INFO_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/file_info/napi_file_iterator_exporter.cpp b/interfaces/kits/napi/file_access_module/file_info/napi_file_iterator_exporter.cpp new file mode 100644 index 00000000..d0759fc1 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/file_info/napi_file_iterator_exporter.cpp @@ -0,0 +1,151 @@ +/* + * 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 "napi_file_iterator_exporter.h" + +#include + +#include "file_info_entity.h" +#include "file_iterator_entity.h" +#include "hilog_wrapper.h" +#include "napi_file_info_exporter.h" + +namespace OHOS { +namespace FileAccessFwk { +bool NapiFileIteratorExporter::Export() +{ + std::vector props = { + NVal::DeclareNapiFunction("next", Next), + }; + + std::string className = GetClassName(); + bool succ = false; + napi_value classValue = nullptr; + std::tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, + NapiFileIteratorExporter::Constructor, std::move(props)); + if (!succ) { + NError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class NapiFileIteratorExporter"); + return false; + } + + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + NError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class NapiFileIteratorExporter"); + return false; + } + + return exports_.AddProp(className, classValue); +} + +napi_value NapiFileIteratorExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto fileIteratorEntity = std::make_unique(); + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), std::move(fileIteratorEntity))) { + NError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj FileIteratorEntity"); + return nullptr; + } + return funcArg.GetThisVar(); +} + +napi_value NapiFileIteratorExporter::Next(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto fileIteratorEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (fileIteratorEntity == nullptr) { + NError(EINVAL).ThrowErr(env, "Cannot get entity of FileIteratorEntity"); + return nullptr; + } + + HILOG_INFO("current fileinfo uri:%{public}s, mode:%{public}d", fileIteratorEntity->fileInfo.uri.c_str(), + fileIteratorEntity->fileInfo.mode); + if ((fileIteratorEntity->fileInfo.mode & DOCUMENT_FLAG_REPRESENTS_DIR) != DOCUMENT_FLAG_REPRESENTS_DIR) { + HILOG_ERROR("current FileInfo is not dir."); + return NVal::CreateUndefined(env).val_; + } + + auto objFileInfoExporter = NClass::InstantiateClass(env, NapiFileInfoExporter::className_, {}); + if (objFileInfoExporter == nullptr) { + NError(EINVAL).ThrowErr(env, "Cannot instantiate class NapiFileInfoExporter"); + return nullptr; + } + + auto fileInfoEntity = NClass::GetEntityOf(env, objFileInfoExporter); + if (fileInfoEntity == nullptr) { + NError(EINVAL).ThrowErr(env, "Cannot get the entity of FileInfoEntity"); + return nullptr; + } + + if (fileIteratorEntity->fileAccessHelper == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "fileAccessHelper is null."); + return nullptr; + } + + bool done = true; + { + std::lock_guard lock(fileIteratorEntity->entityOperateMutex); + if (fileIteratorEntity->fileInfoVec.size() == 0) { + auto retNVal = NVal::CreateObject(env); + retNVal.AddProp("value", NVal::CreateUndefined(env).val_); + retNVal.AddProp("done", NVal::CreateBool(env, done).val_); + return retNVal.val_; + } + + if (fileIteratorEntity->pos >= fileIteratorEntity->fileInfoVec.size()) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "out of index."); + return nullptr; + } + + fileInfoEntity->fileAccessHelper = fileIteratorEntity->fileAccessHelper; + fileInfoEntity->fileInfo = fileIteratorEntity->fileInfoVec[fileIteratorEntity->pos]; + fileIteratorEntity->pos++; + if (fileIteratorEntity->pos == fileIteratorEntity->fileInfoVec.size()) { + fileIteratorEntity->fileInfoVec.clear(); + fileIteratorEntity->offset += fileIteratorEntity->maxCount; + fileIteratorEntity->pos = 0; + int ret = fileIteratorEntity->fileAccessHelper->ListFile(fileIteratorEntity->fileInfo, + fileIteratorEntity->offset, fileIteratorEntity->maxCount, fileIteratorEntity->fileInfoVec); + if (ret != ERR_OK) { + NError(ret).ThrowErr(env, "exec ListFile fail."); + return nullptr; + } + } + done = (fileIteratorEntity->pos == fileIteratorEntity->fileInfoVec.size()); + } + + HILOG_INFO("FileInfoExporter --> fileInfo uri:%{public}s, mode:%{public}d", fileInfoEntity->fileInfo.uri.c_str(), + fileInfoEntity->fileInfo.mode); + auto retNVal = NVal::CreateObject(env); + retNVal.AddProp("value", objFileInfoExporter); + retNVal.AddProp("done", NVal::CreateBool(env, done).val_); + return retNVal.val_; +} + +std::string NapiFileIteratorExporter::GetClassName() +{ + return NapiFileIteratorExporter::className_; +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/file_info/napi_file_iterator_exporter.h b/interfaces/kits/napi/file_access_module/file_info/napi_file_iterator_exporter.h new file mode 100644 index 00000000..e1b51314 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/file_info/napi_file_iterator_exporter.h @@ -0,0 +1,44 @@ +/* + * 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 NAPI_FILE_ITERATOR_EXPORTER_H +#define NAPI_FILE_ITERATOR_EXPORTER_H + +#include + +#include "file_access_extension_info.h" +#include "file_access_helper.h" +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileAccessFwk { +using namespace FileManagement::LibN; + +class NapiFileIteratorExporter final : public NExporter { +public: + inline static const std::string className_ = "NapiFileIteratorExporter"; + + NapiFileIteratorExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}; + ~NapiFileIteratorExporter() = default; + + static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value Next(napi_env env, napi_callback_info info); + + bool Export() override; + std::string GetClassName() override; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // NAPI_FILE_ITERATOR_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index b88e2c49..92e5d655 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -20,14 +20,17 @@ #include "file_access_framework_errno.h" #include "file_access_helper.h" +#include "file_info_entity.h" #include "filemgmt_libn.h" #include "hilog_wrapper.h" #include "ifile_access_notify.h" #include "napi_base_context.h" #include "napi_common_fileaccess.h" #include "napi_error.h" +#include "napi_file_info_exporter.h" #include "napi_notify_callback.h" -#include "n_val.h" +#include "napi_root_iterator_exporter.h" +#include "root_iterator_entity.h" #include "securec.h" #include "uri.h" @@ -174,8 +177,8 @@ napi_value NAPI_GetRegisterFileAccessExtAbilityInfo(napi_env env, napi_callback_ auto result = std::make_shared>(); auto cbExec = [result]() -> NError { - *result = FileAccessHelper::GetRegisterFileAccessExtAbilityInfo(); - return NError(ERRNO_NOERR); + int ret = FileAccessHelper::GetRegisterFileAccessExtAbilityInfo(*result); + return NError(ret); }; auto cbComplete = [result](napi_env env, NError err) -> NVal { if (err) { @@ -206,7 +209,6 @@ napi_value FileAccessHelperInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("delete", NAPI_Delete), DECLARE_NAPI_FUNCTION("move", NAPI_Move), DECLARE_NAPI_FUNCTION("rename", NAPI_Rename), - DECLARE_NAPI_FUNCTION("listFile", NAPI_ListFile), DECLARE_NAPI_FUNCTION("getRoots", NAPI_GetRoots), DECLARE_NAPI_FUNCTION("access", NAPI_Access), DECLARE_NAPI_FUNCTION("on", NAPI_On), @@ -587,56 +589,6 @@ napi_value NAPI_Rename(napi_env env, napi_callback_info info) return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } -napi_value NAPI_ListFile(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - NapiError(ERR_PARAM_NUMBER).ThrowErr(env); - return nullptr; - } - - bool succ = false; - std::unique_ptr uri; - std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succ) { - NapiError(ERR_INVALID_PARAM).ThrowErr(env); - return nullptr; - } - - FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); - if (fileAccessHelper == nullptr) { - return nullptr; - } - - auto result = std::make_shared>(); - string uriString(uri.get()); - auto cbExec = [uriString, result, fileAccessHelper]() -> NError { - OHOS::Uri uri(uriString); - *result = fileAccessHelper->ListFile(uri); - return NError(ERRNO_NOERR); - }; - auto cbComplete = [result](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; - } - napi_value jsArray = WrapArrayFileInfoToJS(env, *result); - return {env, jsArray}; - }; - - const std::string procedureName = "listFile"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } - - NVal cb(env, funcArg[NARG_POS::SECOND]); - if (!cb.TypeIs(napi_function)) { - NapiError(ERR_INVALID_PARAM).ThrowErr(env); - return nullptr; - } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; -} - napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -652,30 +604,47 @@ napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) auto result = std::make_shared>(); auto cbExec = [result, fileAccessHelper]() -> NError { - *result = fileAccessHelper->GetRoots(); - return NError(ERRNO_NOERR); + int ret = fileAccessHelper->GetRoots(*result); + return NError(ret); }; - auto cbComplete = [result](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; + auto cbComplete = [fileAccessHelper, result](napi_env env, NError err) -> NVal { + auto objRootIterator = NClass::InstantiateClass(env, NapiRootIteratorExporter::className_, {}); + if (!objRootIterator) { + auto errGen = []() -> std::tuple { + return { ERR_INVALID_PARAM, "Cannot instantiate class NapiRootIteratorExporter" }; + }; + return { env, NError(errGen).GetNapiErr(env) }; } - napi_value jsArray = WrapArrayRootInfoToJS(env, *result); - return {env, jsArray}; + + auto rootIteratorEntity = NClass::GetEntityOf(env, objRootIterator); + if (!rootIteratorEntity) { + auto errGen = []() -> std::tuple { + return { ERR_INVALID_PARAM, "Cannot get the entity of RootIteratorEntity" }; + }; + return { env, NError(errGen).GetNapiErr(env) }; + } + + { + std::lock_guard lock(rootIteratorEntity->entityOperateMutex); + rootIteratorEntity->fileAccessHelper = fileAccessHelper; + rootIteratorEntity->devVec = std::move(*result); + rootIteratorEntity->pos = 0; + } + return { env, objRootIterator }; }; const std::string procedureName = "getRoots"; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } NVal cb(env, funcArg[NARG_POS::FIRST]); - if (!cb.TypeIs(napi_function)) { - NapiError(ERR_INVALID_PARAM).ThrowErr(env); - return nullptr; + if (cb.TypeIs(napi_function)) { + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + NapiError(ERR_INVALID_PARAM).ThrowErr(env); + return nullptr; } napi_value NAPI_Access(napi_env env, napi_callback_info info) diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h index d0a4dbff..8c4f0fb6 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h @@ -30,11 +30,10 @@ namespace FileAccessFwk { napi_value NAPI_Delete(napi_env env, napi_callback_info info); napi_value NAPI_Move(napi_env env, napi_callback_info info); napi_value NAPI_Rename(napi_env env, napi_callback_info info); - napi_value NAPI_ListFile(napi_env env, napi_callback_info info); napi_value NAPI_GetRoots(napi_env env, napi_callback_info info); napi_value NAPI_Access(napi_env env, napi_callback_info info); napi_value NAPI_On(napi_env env, napi_callback_info info); napi_value NAPI_Off(napi_env env, napi_callback_info info); -} } // namespace FileAccessFwk +} // namespace OHOS #endif // NAPI_FILEACCESS_HELPER_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/napi_notify_callback.h b/interfaces/kits/napi/file_access_module/napi_notify_callback.h index 5dec5b4f..2f89b006 100644 --- a/interfaces/kits/napi/file_access_module/napi_notify_callback.h +++ b/interfaces/kits/napi/file_access_module/napi_notify_callback.h @@ -44,6 +44,6 @@ private: napi_env env_; uv_loop_s *loop_ = nullptr; }; -} // FileAccessFwk -} // OHOS +} // namespace FileAccessFwk +} // namespace OHOS #endif // NAPI_NOTIFY_CALLBACK_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/native_fileaccess_module.cpp b/interfaces/kits/napi/file_access_module/native_fileaccess_module.cpp index e094d85c..8ccb330d 100644 --- a/interfaces/kits/napi/file_access_module/native_fileaccess_module.cpp +++ b/interfaces/kits/napi/file_access_module/native_fileaccess_module.cpp @@ -13,14 +13,22 @@ * limitations under the License. */ +#include +#include + +#include "filemgmt_libn.h" #include "file_extension_info_napi.h" #include "hilog_wrapper.h" #include "napi_fileaccess_helper.h" -#include "napi/native_api.h" -#include "napi/native_node_api.h" +#include "napi_file_info_exporter.h" +#include "napi_file_iterator_exporter.h" +#include "napi_root_info_exporter.h" +#include "napi_root_iterator_exporter.h" namespace OHOS { namespace FileAccessFwk { +using namespace FileManagement::LibN; + EXTERN_C_START /* * The module initialization. @@ -34,6 +42,21 @@ static napi_value Init(napi_env env, napi_value exports) InitDeviceType(env, exports); InitFileInfo(env, exports); InitRootInfo(env, exports); + + std::vector> products; + products.emplace_back(std::make_unique(env, exports)); + products.emplace_back(std::make_unique(env, exports)); + products.emplace_back(std::make_unique(env, exports)); + products.emplace_back(std::make_unique(env, exports)); + for (auto &&product : products) { + if (!product->Export()) { + HILOG_ERROR("INNER BUG. Failed to export class %{public}s", product->GetClassName().c_str()); + return nullptr; + } else { + HILOG_ERROR("Class %{public}s has been exported", product->GetClassName().c_str()); + } + } + return exports; } EXTERN_C_END diff --git a/interfaces/kits/napi/file_access_module/root_info/napi_root_info_exporter.cpp b/interfaces/kits/napi/file_access_module/root_info/napi_root_info_exporter.cpp new file mode 100644 index 00000000..5f4ca8a3 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/root_info/napi_root_info_exporter.cpp @@ -0,0 +1,203 @@ +/* + * 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 NAPI_FILEACCESS_HELPER_H +#define NAPI_FILEACCESS_HELPER_H + +#include "napi_root_info_exporter.h" + +#include + +#include "file_access_framework_errno.h" +#include "file_iterator_entity.h" +#include "hilog_wrapper.h" +#include "napi_file_iterator_exporter.h" +#include "root_info_entity.h" + +namespace OHOS { +namespace FileAccessFwk { +bool NapiRootInfoExporter::Export() +{ + std::vector props = { + NVal::DeclareNapiFunction("listFile", ListFile), + NVal::DeclareNapiGetter("deviceType", GetDeviceType), + NVal::DeclareNapiGetter("uri", GetUri), + NVal::DeclareNapiGetter("displayName", GetDisplayName), + NVal::DeclareNapiGetter("deviceFlags", GetDeviceFlags) + }; + + std::string className = GetClassName(); + bool succ = false; + napi_value classValue = nullptr; + std::tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, + NapiRootInfoExporter::Constructor, std::move(props)); + if (!succ) { + HILOG_ERROR("Failed to define class NapiRootInfoExporter"); + NError(ERR_INVALID_PARAM).ThrowErr(exports_.env_, "INNER BUG. Failed to define class NapiRootInfoExporter"); + return false; + } + + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + HILOG_ERROR("Failed to save class NapiRootInfoExporter"); + NError(ERR_INVALID_PARAM).ThrowErr(exports_.env_, "INNER BUG. Failed to save class NapiRootInfoExporter"); + return false; + } + + return exports_.AddProp(className, classValue); +} + +napi_value NapiRootInfoExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto rootInfoEntity = std::make_unique(); + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), std::move(rootInfoEntity))) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj RootInfoEntity"); + return nullptr; + } + return funcArg.GetThisVar(); +} + +napi_value NapiRootInfoExporter::ListFile(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto rootEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (rootEntity == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot get entity of FileInfoEntity"); + return nullptr; + } + + if (rootEntity->fileAccessHelper == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "fileAccessHelper is null."); + return nullptr; + } + + napi_value objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {}); + if (objFileIteratorExporter == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot instantiate class NapiFileIteratorExporter"); + return nullptr; + } + + auto fileIteratorEntity = NClass::GetEntityOf(env, objFileIteratorExporter); + if (fileIteratorEntity == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot get the entity of FileIteratorEntity"); + return nullptr; + } + + FileInfo fileInfo; + fileInfo.uri = rootEntity->rootInfo.uri; + fileInfo.mode = DOCUMENT_FLAG_REPRESENTS_DIR | DOCUMENT_FLAG_SUPPORTS_READ | DOCUMENT_FLAG_SUPPORTS_WRITE; + { + std::lock_guard lock(fileIteratorEntity->entityOperateMutex); + fileIteratorEntity->fileAccessHelper = rootEntity->fileAccessHelper; + fileIteratorEntity->fileInfo = fileInfo; + fileIteratorEntity->offset = 0; + fileIteratorEntity->pos = 0; + auto ret = rootEntity->fileAccessHelper->ListFile(fileInfo, fileIteratorEntity->offset, + fileIteratorEntity->maxCount, fileIteratorEntity->fileInfoVec); + if (ret != ERR_OK) { + NError(ret).ThrowErr(env, "exec ListFile fail."); + return nullptr; + } + } + + return NVal(env, objFileIteratorExporter).val_; +} + +napi_value NapiRootInfoExporter::GetDeviceType(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto rootInfoEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (rootInfoEntity == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot get entity of RootInfoEntity"); + return nullptr; + } + + return NVal::CreateInt64(env, (int64_t)(rootInfoEntity->rootInfo.deviceType)).val_; +} + +napi_value NapiRootInfoExporter::GetUri(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto rootInfoEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (rootInfoEntity == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot get entity of RootInfoEntity"); + return nullptr; + } + + return NVal::CreateUTF8String(env, rootInfoEntity->rootInfo.uri).val_; +} + +napi_value NapiRootInfoExporter::GetDisplayName(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto rootInfoEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (rootInfoEntity == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot get entity of RootInfoEntity"); + return nullptr; + } + + return NVal::CreateUTF8String(env, rootInfoEntity->rootInfo.displayName).val_; +} + +napi_value NapiRootInfoExporter::GetDeviceFlags(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto rootInfoEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (rootInfoEntity == nullptr) { + NError(ERR_INVALID_PARAM).ThrowErr(env, "Cannot get entity of RootInfoEntity"); + return nullptr; + } + + return NVal::CreateInt64(env, rootInfoEntity->rootInfo.deviceFlags).val_; +} + +std::string NapiRootInfoExporter::GetClassName() +{ + return NapiRootInfoExporter::className_; +} +} // namespace FileAccessFwk +} // namespace OHOS +#endif // NAPI_FILEACCESS_HELPER_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/root_info/napi_root_info_exporter.h b/interfaces/kits/napi/file_access_module/root_info/napi_root_info_exporter.h new file mode 100644 index 00000000..720a7c40 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/root_info/napi_root_info_exporter.h @@ -0,0 +1,48 @@ +/* + * 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 NAPI_ROOT_INFO_EXPORTER_H +#define NAPI_ROOT_INFO_EXPORTER_H + +#include + +#include "file_access_extension_info.h" +#include "file_access_helper.h" +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileAccessFwk { +using namespace FileManagement::LibN; + +class NapiRootInfoExporter final : public NExporter { +public: + inline static const std::string className_ = "NapiRootInfoExporter"; + + NapiRootInfoExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}; + ~NapiRootInfoExporter() = default; + + static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value ListFile(napi_env env, napi_callback_info info); + static napi_value GetDeviceType(napi_env env, napi_callback_info info); + static napi_value GetUri(napi_env env, napi_callback_info info); + static napi_value GetDisplayName(napi_env env, napi_callback_info info); + static napi_value GetDeviceFlags(napi_env env, napi_callback_info info); + + bool Export() override; + std::string GetClassName() override; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // NAPI_ROOT_INFO_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/root_info/napi_root_iterator_exporter.cpp b/interfaces/kits/napi/file_access_module/root_info/napi_root_iterator_exporter.cpp new file mode 100644 index 00000000..ed6f2a20 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/root_info/napi_root_iterator_exporter.cpp @@ -0,0 +1,120 @@ +/* + * 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 "napi_root_iterator_exporter.h" + +#include "hilog_wrapper.h" +#include "napi_root_info_exporter.h" +#include "root_info_entity.h" +#include "root_iterator_entity.h" + +namespace OHOS { +namespace FileAccessFwk { +bool NapiRootIteratorExporter::Export() +{ + std::vector props = { + NVal::DeclareNapiFunction("next", Next), + }; + + std::string className = GetClassName(); + bool succ = false; + napi_value classValue = nullptr; + std::tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, + NapiRootIteratorExporter::Constructor, std::move(props)); + if (!succ) { + NError(EIO).ThrowErr(exports_.env_, + "INNER BUG. Failed to define class NapNapiRootIteratorExporteriRootIterator"); + return false; + } + + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + NError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class NapiRootIteratorExporter"); + return false; + } + + return exports_.AddProp(className, classValue); +} + +napi_value NapiRootIteratorExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto rootIteratorEntity = std::make_unique(); + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), std::move(rootIteratorEntity))) { + NError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj FileIteratorEntity"); + return nullptr; + } + return funcArg.GetThisVar(); +} + +napi_value NapiRootIteratorExporter::Next(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(ERR_PARAM_NUMBER).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + napi_value thisVar = funcArg.GetThisVar(); + auto iterEntity = NClass::GetEntityOf(env, thisVar); + if (iterEntity == nullptr) { + NError(EIO).ThrowErr(env, "Cannot get entity of RootIteratorEntity"); + return nullptr; + } + + napi_value objRootExporter = NClass::InstantiateClass(env, NapiRootInfoExporter::className_, {}); + if (objRootExporter == nullptr) { + NError(EINVAL).ThrowErr(env, "Cannot instantiate class NapiRootInfoExporter"); + return nullptr; + } + + auto rootEntity = NClass::GetEntityOf(env, objRootExporter); + if (rootEntity == nullptr) { + NError(EINVAL).ThrowErr(env, "Cannot get the entity of RootInfoEntity"); + return nullptr; + } + + bool done = true; + { + std::lock_guard lock(iterEntity->entityOperateMutex); + auto len = (int64_t)iterEntity->devVec.size(); + rootEntity->fileAccessHelper = iterEntity->fileAccessHelper; + if (iterEntity->pos < len) { + rootEntity->rootInfo = iterEntity->devVec[iterEntity->pos]; + iterEntity->pos++; + done = (iterEntity->pos == len); + } else { + rootEntity = nullptr; + objRootExporter = nullptr; + } + } + + auto retNVal = NVal::CreateObject(env); + retNVal.AddProp("value", objRootExporter); + retNVal.AddProp("done", NVal::CreateBool(env, done).val_); + return retNVal.val_; +} + +std::string NapiRootIteratorExporter::GetClassName() +{ + return NapiRootIteratorExporter::className_; +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/root_info/napi_root_iterator_exporter.h b/interfaces/kits/napi/file_access_module/root_info/napi_root_iterator_exporter.h new file mode 100644 index 00000000..9902d7d1 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/root_info/napi_root_iterator_exporter.h @@ -0,0 +1,41 @@ +/* + * 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 NAPI_ROOT_ITERATOR_H +#define NAPI_ROOT_ITERATOR_H + +#include + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileAccessFwk { +using namespace FileManagement::LibN; +class NapiRootIteratorExporter final : public NExporter { +public: + inline static const std::string className_ = "NapiRootIteratorExporter"; + + NapiRootIteratorExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}; + ~NapiRootIteratorExporter() = default; + + static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value Next(napi_env env, napi_callback_info info); + + bool Export() override; + std::string GetClassName() override; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // NAPI_ROOT_ITERATOR_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/root_info/root_info_entity.h b/interfaces/kits/napi/file_access_module/root_info/root_info_entity.h new file mode 100644 index 00000000..bfeef115 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/root_info/root_info_entity.h @@ -0,0 +1,30 @@ +/* + * 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 ROOT_INFO_ENTITY_H +#define ROOT_INFO_ENTITY_H + +#include "file_access_extension_info.h" +#include "file_access_helper.h" + +namespace OHOS { +namespace FileAccessFwk { +struct RootInfoEntity { + FileAccessHelper *fileAccessHelper { nullptr }; + RootInfo rootInfo {}; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // ROOT_INFO_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/root_info/root_iterator_entity.h b/interfaces/kits/napi/file_access_module/root_info/root_iterator_entity.h new file mode 100644 index 00000000..8e746cf9 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/root_info/root_iterator_entity.h @@ -0,0 +1,35 @@ +/* + * 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 ROOT_ITERATOR_ENTITY_H +#define ROOT_ITERATOR_ENTITY_H + +#include +#include + +#include "file_access_extension_info.h" +#include "file_access_helper.h" + +namespace OHOS { +namespace FileAccessFwk { +struct RootIteratorEntity { + FileAccessHelper *fileAccessHelper { nullptr }; + std::vector devVec {}; + std::mutex entityOperateMutex; + int64_t pos { 0 }; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // ROOT_ITERATOR_ENTITY_H \ No newline at end of file diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts index c4ee729b..5e5f732d 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts @@ -446,7 +446,7 @@ export default class FileExtAbility extends Extension { }; } - listFile(sourceFileUri) { + listFile(sourceFileUri, offset, count) { if (!this.checkUri(sourceFileUri)) { return { infos: [], @@ -458,6 +458,7 @@ export default class FileExtAbility extends Extension { let path = this.getPath(sourceFileUri); let dir = fileio.opendirSync(path); let hasNextFile = true; + let i = 0; while (hasNextFile) { try { let dirent = dir.readSync(); @@ -468,6 +469,12 @@ export default class FileExtAbility extends Extension { } else { mode |= DocumentFlag.REPRESENTS_FILE; } + + if (offset > i) { + i ++; + continue; + } + infos.push({ uri: this.genNewFileUri(sourceFileUri, dirent.name), fileName: dirent.name, @@ -476,6 +483,12 @@ export default class FileExtAbility extends Extension { mtime: stat.mtime, mimeType: '', }); + + i ++; + if (i == (offset + count)) { + hasNextFile = false; + break; + } } catch (e) { hasNextFile = false; } diff --git a/utils/file_access_framework_errno.h b/utils/file_access_framework_errno.h index d45271e8..ee4e4063 100644 --- a/utils/file_access_framework_errno.h +++ b/utils/file_access_framework_errno.h @@ -39,7 +39,8 @@ enum { ERR_INVALID_NOTIFY, // invalid notify ERR_REMOVE_NOTIFY_FAIL, // remove notify fail ERR_NOTIFY_NOT_EXIST, // the notify is not exist - ERR_NOTIFY_FAIL // notify client fail + ERR_NOTIFY_FAIL, // notify client fail + ERR_SHM_FAIL // shm fail }; } // namespace FileAccessFwk } // namespace OHOS -- Gitee