From ea6b9b49d6f27096830cc0dde0c2af1d78392673 Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Mon, 14 Feb 2022 11:01:34 +0800 Subject: [PATCH 1/2] alter listfile interface's return Signed-off-by: linjun9528 --- interfaces/kits/js/src/file_manager_napi.cpp | 4 +- services/BUILD.gn | 1 + services/src/client/file_manager_proxy.cpp | 17 ++-- services/src/client/file_manager_proxy.h | 4 +- services/src/client/ifms_client.h | 4 +- services/src/fileoper/cmd_response.h | 98 +++++++++++++++++++ .../src/fileoper/external_storage_oper.cpp | 10 +- .../src/fileoper/external_storage_utils.cpp | 19 +--- .../src/fileoper/external_storage_utils.h | 4 +- services/src/fileoper/file_info.cpp | 2 + 10 files changed, 131 insertions(+), 32 deletions(-) create mode 100644 services/src/fileoper/cmd_response.h diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp index 8d9d0add..bf6732d6 100644 --- a/interfaces/kits/js/src/file_manager_napi.cpp +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -31,7 +31,7 @@ using namespace std; using namespace DistributedFS; struct AsyncFileInfoArg { NRef ref_; - vector> fileRes_; + vector> fileRes_; explicit AsyncFileInfoArg(NVal ref) : ref_(ref), fileRes_() {}; ~AsyncFileInfoArg() = default; }; @@ -181,7 +181,7 @@ void CreateFileArray(napi_env env, shared_ptr arg) { for (unsigned int i = 0; i < arg->fileRes_.size(); i++) { NVal obj = NVal::CreateObject(env); - unique_ptr &res = arg->fileRes_[i]; + shared_ptr &res = arg->fileRes_[i]; obj.AddProp("name", NVal::CreateUTF8String(env, res->GetName()).val_); obj.AddProp("path", NVal::CreateUTF8String(env, res->GetPath()).val_); obj.AddProp("type", NVal::CreateUTF8String(env, res->GetType()).val_); diff --git a/services/BUILD.gn b/services/BUILD.gn index 989b6c65..49b30b68 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -45,6 +45,7 @@ ohos_shared_library("fms_server") { sources = [ "src/client/file_manager_proxy.cpp", + "src/fileoper/cmd_response.cpp", "src/fileoper/ext_storage/ext_storage_subscriber.cpp", "src/fileoper/ext_storage/storage_manager_inf.cpp", "src/fileoper/external_storage_oper.cpp", diff --git a/services/src/client/file_manager_proxy.cpp b/services/src/client/file_manager_proxy.cpp index b3a120ba..605a4c84 100644 --- a/services/src/client/file_manager_proxy.cpp +++ b/services/src/client/file_manager_proxy.cpp @@ -14,6 +14,7 @@ */ #include "file_manager_proxy.h" +#include "cmd_response.h" #include "file_info.h" #include "file_manager_service_def.h" #include "file_manager_service_errno.h" @@ -27,7 +28,7 @@ namespace OHOS { namespace FileManagerService { FileManagerProxy::FileManagerProxy(const sptr &impl) : IRemoteProxy(impl) {} -int FileManagerProxy::GetRoot(const CmdOptions &option, vector> &fileRes) +int FileManagerProxy::GetRoot(const CmdOptions &option, vector> &fileRes) { CmdOptions op(option); if (op.GetDevInfo().GetName() == "external_storage") { @@ -104,7 +105,7 @@ IFmsClient *IFmsClient::GetFmsInstance() } int FileManagerProxy::ListFile(const std::string &type, const std::string &path, const CmdOptions &option, - std::vector> &fileRes) + std::vector> &fileRes) { MessageParcel data; CmdOptions op(option); @@ -130,14 +131,12 @@ int FileManagerProxy::ListFile(const std::string &type, const std::string &path, ERR_LOG("inner error send request fail %{public}d", err); return FAIL; } - int fileInfoNum = 0; - reply.ReadInt32(fileInfoNum); - while (fileInfoNum) { - unique_ptr file(reply.ReadParcelable()); - fileRes.emplace_back(move(file)); - fileInfoNum--; + std::unique_ptr cmdResponse {CmdResponse::Unmarshalling(reply)}; + if (cmdResponse == nullptr) { + ERR_LOG("Unmarshalling cmdResponse fail %{public}d", err); + return FAIL; } - reply.ReadInt32(err); + fileRes = cmdResponse->GetFileInfoList(); return err; } diff --git a/services/src/client/file_manager_proxy.h b/services/src/client/file_manager_proxy.h index 085d3236..494648ed 100644 --- a/services/src/client/file_manager_proxy.h +++ b/services/src/client/file_manager_proxy.h @@ -29,10 +29,10 @@ public: virtual ~FileManagerProxy() = default; int Mkdir(const std::string &name, const std::string &path) override; int ListFile(const std::string &type, const std::string &path, const CmdOptions &option, - std::vector> &fileRes) override; + std::vector> &fileRes) override; int CreateFile(const std::string &path, const std::string &fileName, const CmdOptions &option, std::string &uri) override; - int GetRoot(const CmdOptions &option, std::vector> &fileRes) override; + int GetRoot(const CmdOptions &option, std::vector> &fileRes) override; private: static inline BrokerDelegator delegator_; }; diff --git a/services/src/client/ifms_client.h b/services/src/client/ifms_client.h index 386c4feb..ba0ce14a 100644 --- a/services/src/client/ifms_client.h +++ b/services/src/client/ifms_client.h @@ -24,8 +24,8 @@ public: static IFmsClient *GetFmsInstance(); virtual int Mkdir(const std::string &name, const std::string &path) = 0; virtual int ListFile(const std::string &type, const std::string &path, const CmdOptions &option, - std::vector> &fileRes) = 0; - virtual int GetRoot(const CmdOptions &option, std::vector> &fileRes) = 0; + std::vector> &fileRes) = 0; + virtual int GetRoot(const CmdOptions &option, std::vector> &fileRes) = 0; virtual int CreateFile(const std::string &path, const std::string &fileName, const CmdOptions &option, std::string &uri) = 0; }; diff --git a/services/src/fileoper/cmd_response.h b/services/src/fileoper/cmd_response.h new file mode 100644 index 00000000..eb65bac4 --- /dev/null +++ b/services/src/fileoper/cmd_response.h @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2021 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 STORAGE_SERVICES_CMD_RESPONSE_H +#define STORAGE_SERVICES_CMD_RESPONSE_H + +#include +#include +#include + +#include "file_info.h" +#include "parcel.h" +namespace OHOS { +namespace FileManagerService { +class CmdResponse : public Parcelable { +public: + CmdResponse() = default; + CmdResponse(int &err, std::string &uri, std::vector> &fileInfoList) + : err_(err), uri_(uri), vecFileInfo_(fileInfoList) + {} + ~CmdResponse() = default; + + void SetErr(const int err) + { + err_ = err; + } + + int GetErr() const + { + return err_; + } + + void SetUri(const std::string &uri) + { + uri_ = uri; + } + + std::string GetUri() const + { + return uri_; + } + + void SetFileInfoList(std::vector> &fileInfoList) + { + vecFileInfo_ = fileInfoList; + } + + std::vector> GetFileInfoList() + { + return vecFileInfo_; + } + + virtual bool Marshalling(Parcel &parcel) const override + { + parcel.WriteInt32(err_); + parcel.WriteString(uri_); + int fileCount = vecFileInfo_.size(); + parcel.WriteInt32(fileCount); + for (int i = 0; i < fileCount; i++) { + parcel.WriteParcelable(vecFileInfo_[i].get()); + } + return true; + } + + static CmdResponse* Unmarshalling(Parcel &parcel) + { + auto *obj = new (std::nothrow) CmdResponse(); + if (obj == nullptr) { + return nullptr; + } + obj->err_ = parcel.ReadInt32(); + obj->uri_ = parcel.ReadString(); + int fileCount = parcel.ReadInt32(); + for (int i = 0; i < fileCount; i++) { + std::shared_ptr file(parcel.ReadParcelable()); + obj->vecFileInfo_.emplace_back(move(file)); + } + return obj; + } +private: + int err_; + std::string uri_; + std::vector> vecFileInfo_; +}; +} // FileManagerService +} // namespace OHOS +#endif // STORAGE_SERVICES_CMD_RESPONSE_H \ No newline at end of file diff --git a/services/src/fileoper/external_storage_oper.cpp b/services/src/fileoper/external_storage_oper.cpp index a23e5b92..d4ec73dd 100644 --- a/services/src/fileoper/external_storage_oper.cpp +++ b/services/src/fileoper/external_storage_oper.cpp @@ -17,6 +17,7 @@ #include +#include "cmd_response.h" #include "external_storage_utils.h" #include "file_info.h" #include "file_manager_service_def.h" @@ -77,7 +78,14 @@ int ExternalStorageOper::CreateFile(const std::string &uri, const std::string &n int ExternalStorageOper::ListFile(const std::string &type, const std::string &uri, const CmdOptions &option, MessageParcel &reply) const { - return ExternalStorageUtils::DoListFile(type, uri, reply); + std::vector> fileList; + int ret = ExternalStorageUtils::DoListFile(type, uri, fileList); + CmdResponse cmdResponse; + cmdResponse.SetErr(ret); + cmdResponse.SetUri(""); + cmdResponse.SetFileInfoList(fileList); + cmdResponse.Marshalling(reply); + return ret; } } // namespace FileManagerService } // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp index 27db06ac..432a53ea 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -59,7 +59,7 @@ static bool GetRealPath(string &path) return true; } -static bool GetFileInfo(const std::string &path, const std::string &name, unique_ptr &fileInfo) +static bool GetFileInfo(const std::string &path, const std::string &name, shared_ptr &fileInfo) { std::string fullPath(path); size_t len = fullPath.size(); @@ -104,42 +104,31 @@ static bool ConvertUriToAbsolutePath(const std::string &uri, std::string &path) return true; } -int ExternalStorageUtils::DoListFile(const std::string &type, const std::string &uri, MessageParcel &reply) +int ExternalStorageUtils::DoListFile(const std::string &type, const std::string &uri, + std::vector> &fileList) { std::string path; - int fileCount = 0; if (!ConvertUriToAbsolutePath(uri, path)) { ERR_LOG("invalid uri[%{public}s].", uri.c_str()); - reply.WriteInt32(fileCount); return E_NOEXIST; } DIR *dir = opendir(path.c_str()); if (!dir) { ERR_LOG("opendir path[%{public}s] fail.", path.c_str()); - reply.WriteInt32(fileCount); return E_NOEXIST; } - std::vector> fileList; for (struct dirent *ent = readdir(dir); ent != nullptr; ent = readdir(dir)) { if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { continue; } - unique_ptr fileInfo = make_unique(); + shared_ptr fileInfo = make_shared(); if (!GetFileInfo(path, ent->d_name, fileInfo)) { continue; } fileList.push_back(move(fileInfo)); } closedir(dir); - fileCount = static_cast(fileList.size()); - reply.WriteInt32(fileCount); - if (fileCount == 0) { - return E_EMPTYFOLDER; - } - for (int i = 0; i < fileCount; i++) { - reply.WriteParcelable(fileList[i].get()); - } return SUCCESS; } diff --git a/services/src/fileoper/external_storage_utils.h b/services/src/fileoper/external_storage_utils.h index 61a139da..c11bcbef 100644 --- a/services/src/fileoper/external_storage_utils.h +++ b/services/src/fileoper/external_storage_utils.h @@ -15,6 +15,7 @@ #ifndef STORAGE_SERIVCES_EXTERNAL_STORAGE_UTILS_H #define STORAGE_SERIVCES_EXTERNAL_STORAGE_UTILS_H +#include #include #include @@ -28,7 +29,8 @@ class ExternalStorageUtils { public: ExternalStorageUtils(); ~ExternalStorageUtils(); - static int DoListFile(const std::string &type, const std::string &uri, MessageParcel &reply); + static int DoListFile(const std::string &type, const std::string &uri, + std::vector> &fileList); static int DoCreateFile(const std::string &uri, const std::string &name, MessageParcel &reply); static bool PopFileInfo(FileInfo &fileInfo, MessageParcel &reply); static int DoGetRoot(const std::string &name, const std::string &path, MessageParcel &reply); diff --git a/services/src/fileoper/file_info.cpp b/services/src/fileoper/file_info.cpp index 46168b76..3eaf9006 100644 --- a/services/src/fileoper/file_info.cpp +++ b/services/src/fileoper/file_info.cpp @@ -28,11 +28,13 @@ bool FileInfo::Marshalling(Parcel &parcel) const parcel.WriteInt64(modifiedTime_); return true; } + FileInfo* FileInfo::Unmarshalling(Parcel &parcel) { auto *obj = new (std::nothrow) FileInfo(); if (obj == nullptr) { ERR_LOG("Unmarshalling fail"); + return nullptr; } obj->path_ = parcel.ReadString(); obj->name_ = parcel.ReadString(); -- Gitee From 9850e6d998f5aa004b8d324b19d726ffcab7f358 Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Mon, 14 Feb 2022 11:15:54 +0800 Subject: [PATCH 2/2] alter listfile interface's return Signed-off-by: linjun9528 --- services/BUILD.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index 49b30b68..989b6c65 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -45,7 +45,6 @@ ohos_shared_library("fms_server") { sources = [ "src/client/file_manager_proxy.cpp", - "src/fileoper/cmd_response.cpp", "src/fileoper/ext_storage/ext_storage_subscriber.cpp", "src/fileoper/ext_storage/storage_manager_inf.cpp", "src/fileoper/external_storage_oper.cpp", -- Gitee