From f15f720bd2c5809b3ba06111aa55fb9443751bbf Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Fri, 18 Feb 2022 17:02:22 +0800 Subject: [PATCH 1/5] add file count filter & alter ListFile,GetRoot,CreateFile interface's return type Signed-off-by: linjun9528 --- services/include/file_manager_service_def.h | 2 +- services/include/file_manager_service_errno.h | 1 + services/src/client/file_manager_proxy.cpp | 54 +++++----- services/src/fileoper/cmd_response.h | 98 +++++++++++++++++++ .../src/fileoper/external_storage_oper.cpp | 25 ++++- .../src/fileoper/external_storage_utils.cpp | 78 ++++++--------- .../src/fileoper/external_storage_utils.h | 10 +- services/src/fileoper/file_info.cpp | 2 + services/src/fileoper/media_file_oper.cpp | 27 +++-- services/src/fileoper/media_file_oper.h | 2 +- services/src/fileoper/media_file_utils.cpp | 31 +++--- services/src/fileoper/media_file_utils.h | 5 +- 12 files changed, 230 insertions(+), 105 deletions(-) create mode 100644 services/src/fileoper/cmd_response.h diff --git a/services/include/file_manager_service_def.h b/services/include/file_manager_service_def.h index 6992a138..14a3f1a3 100644 --- a/services/include/file_manager_service_def.h +++ b/services/include/file_manager_service_def.h @@ -41,7 +41,7 @@ enum VolumeState { MOUNTED, EJECTING }; - +constexpr int32_t MAX_NUM = 200; constexpr int32_t CODE_MASK = 0xff; constexpr int32_t EQUIPMENT_SHIFT = 16; diff --git a/services/include/file_manager_service_errno.h b/services/include/file_manager_service_errno.h index 8ad3940e..dcc22b5f 100644 --- a/services/include/file_manager_service_errno.h +++ b/services/include/file_manager_service_errno.h @@ -22,6 +22,7 @@ constexpr int32_t E_NOEXIST = -2; // file not exist constexpr int32_t E_EMPTYFOLDER = -3; // folder empty constexpr int32_t E_INVALID_OPERCODE = -4; // not valid oper code constexpr int32_t E_CREATE_FAIL = -5; // create file fail +constexpr int32_t E_INVALID_FILE_NUMBER = -6; // file count or offset invalid } // namespace FileManagerService } // namespace OHOS #endif // STORAGE_SERVICES_INCLUDE_ERRNO_H diff --git a/services/src/client/file_manager_proxy.cpp b/services/src/client/file_manager_proxy.cpp index b3a120ba..21ce6e18 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" @@ -25,6 +26,16 @@ using namespace std; namespace OHOS { namespace FileManagerService { +static int GetCmdResponse(MessageParcel &reply, sptr &cmdResponse) +{ + cmdResponse = reply.ReadParcelable(); + if (cmdResponse == nullptr) { + ERR_LOG("Unmarshalling cmdResponse fail"); + return FAIL; + } + return cmdResponse->GetErr(); +} + FileManagerProxy::FileManagerProxy(const sptr &impl) : IRemoteProxy(impl) {} int FileManagerProxy::GetRoot(const CmdOptions &option, vector> &fileRes) @@ -41,15 +52,12 @@ int FileManagerProxy::GetRoot(const CmdOptions &option, vector file = make_unique(FILE_ROOT_NAME, rootPath, ALBUM_TYPE); - fileRes.emplace_back(move(file)); + sptr cmdResponse; + err = GetCmdResponse(reply, cmdResponse); + if (err != ERR_NONE) { + return err; } - reply.ReadInt32(err); + fileRes = cmdResponse->GetFileInfoList(); return err; } else { unique_ptr image = make_unique(IMAGE_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); @@ -82,8 +90,12 @@ int FileManagerProxy::CreateFile(const std::string &path, const std::string &fil ERR_LOG("inner error send request fail %{public}d", err); return FAIL; } - reply.ReadString(uri); - reply.ReadInt32(err); + sptr cmdResponse; + err = GetCmdResponse(reply, cmdResponse); + if (err != ERR_NONE) { + return err; + } + uri = cmdResponse->GetUri(); return err; } @@ -104,21 +116,21 @@ 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); std::string devName(op.GetDevInfo().GetName()); std::string devPath(op.GetDevInfo().GetPath()); - int32_t offset = op.GetOffset(); - int32_t count = op.GetCount(); + int64_t offset = op.GetOffset(); + int64_t count = op.GetCount(); data.WriteString(devName); data.WriteString(devPath); data.WriteString(type); data.WriteString(path); - data.WriteInt32(offset); - data.WriteInt32(count); + data.WriteInt64(offset); + data.WriteInt64(count); MessageParcel reply; MessageOption messageOption; uint32_t code = Operation::LIST_FILE; @@ -130,14 +142,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--; + sptr cmdResponse; + err = GetCmdResponse(reply, cmdResponse); + if (err != ERR_NONE) { + return err; } - reply.ReadInt32(err); + fileRes = cmdResponse->GetFileInfoList(); return err; } diff --git a/services/src/fileoper/cmd_response.h b/services/src/fileoper/cmd_response.h new file mode 100644 index 00000000..be55b7f8 --- /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_(move(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_ = move(fileInfoList); + } + + std::vector> GetFileInfoList() + { + return move(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::unique_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..780ab8ea 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" @@ -66,18 +67,36 @@ int ExternalStorageOper::OperProcess(uint32_t code, MessageParcel &data, Message int ExternalStorageOper::GetRoot(const std::string &name, const std::string &path, MessageParcel &reply) const { - return ExternalStorageUtils::DoGetRoot(name, path, reply); + std::vector> fileList; + int ret = ExternalStorageUtils::DoGetRoot(name, path, fileList); + CmdResponse cmdResponse; + cmdResponse.SetErr(ret); + cmdResponse.SetFileInfoList(fileList); + reply.WriteParcelable(&cmdResponse); + return ret; } int ExternalStorageOper::CreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) const { - return ExternalStorageUtils::DoCreateFile(uri, name, reply); + std::string resultUir; + int ret = ExternalStorageUtils::DoCreateFile(uri, name, resultUir); + CmdResponse cmdResponse; + cmdResponse.SetErr(ret); + cmdResponse.SetUri(resultUir); + reply.WriteParcelable(&cmdResponse); + return ret; } 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, option, fileList); + CmdResponse cmdResponse; + cmdResponse.SetErr(ret); + cmdResponse.SetFileInfoList(fileList); + reply.WriteParcelable(&cmdResponse); + 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..92c997d7 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -104,46 +104,55 @@ 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, const CmdOptions &option, + std::vector> &fileList) { + int64_t count = option.GetCount(); + int64_t offset = option.GetOffset(); + if (count < 0 || count > MAX_NUM || offset < 0 || offset > MAX_NUM) { + ERR_LOG("invalid file count or offset."); + return E_INVALID_FILE_NUMBER; + } + count = (count == 0) ? MAX_NUM : count; + 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; + int64_t index = 0; 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(); - if (!GetFileInfo(path, ent->d_name, fileInfo)) { + if (index < offset) { + index++; continue; } - fileList.push_back(move(fileInfo)); + if (count > 0) { + unique_ptr fileInfo = make_unique(); + if (!GetFileInfo(path, ent->d_name, fileInfo)) { + continue; + } + fileList.push_back(move(fileInfo)); + count--; + if (count == 0) { + break; + } + } + index++; } 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; } -int ExternalStorageUtils::DoCreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) +int ExternalStorageUtils::DoCreateFile(const std::string &uri, const std::string &name, std::string &resultUri) { std::string path; if (!ConvertUriToAbsolutePath(uri, path)) { @@ -167,48 +176,23 @@ int ExternalStorageUtils::DoCreateFile(const std::string &uri, const std::string return E_CREATE_FAIL; } close(fd); - - std::string fullUri(EXTERNAL_STORAGE_URI); - fullUri.append(path); - reply.WriteString(fullUri); + resultUri = EXTERNAL_STORAGE_URI + path; return SUCCESS; } -int ExternalStorageUtils::DoGetRoot(const std::string &name, const std::string &path, MessageParcel &reply) +int ExternalStorageUtils::DoGetRoot(const std::string &name, const std::string &path, + std::vector> &fileList) { vector vecRootPath; if (!StorageManagerInf::GetMountedVolumes(vecRootPath)) { ERR_LOG("there is valid extorage storage"); - reply.WriteInt32(0); return FAIL; } - reply.WriteInt32(vecRootPath.size()); for (auto rootPath : vecRootPath) { - reply.WriteString(rootPath); + unique_ptr fileInfo = make_unique(FILE_ROOT_NAME, rootPath, ALBUM_TYPE); + fileList.push_back(move(fileInfo)); } return SUCCESS; } - -bool ExternalStorageUtils::PopFileInfo(FileInfo &fileInfo, MessageParcel &reply) -{ - std::string path; - std::string type; - std::string name; - int64_t size = 0; - int64_t at = 0; - int64_t mt = 0; - - reply.ReadString(path); - reply.ReadString(type); - reply.ReadString(name); - reply.ReadInt64(size); - reply.ReadInt64(at); - reply.ReadInt64(mt); - fileInfo = FileInfo(name, path, type); - fileInfo.SetSize(size); - fileInfo.SetAddedTime(at); - fileInfo.SetModifiedTime(mt); - return true; -} } // namespace FileManagerService } // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/external_storage_utils.h b/services/src/fileoper/external_storage_utils.h index 61a139da..56d17871 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,10 +29,11 @@ class ExternalStorageUtils { public: ExternalStorageUtils(); ~ExternalStorageUtils(); - static int DoListFile(const std::string &type, const std::string &uri, MessageParcel &reply); - 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); + static int DoListFile(const std::string &type, const std::string &uri, const CmdOptions &option, + std::vector> &fileList); + static int DoCreateFile(const std::string &uri, const std::string &name, std::string &resultUri); + static int DoGetRoot(const std::string &name, const std::string &path, + std::vector> &fileList); }; } // namespace FileManagerService } // namespace OHOS 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(); diff --git a/services/src/fileoper/media_file_oper.cpp b/services/src/fileoper/media_file_oper.cpp index dbc2520c..f9d1b730 100644 --- a/services/src/fileoper/media_file_oper.cpp +++ b/services/src/fileoper/media_file_oper.cpp @@ -18,6 +18,7 @@ #include #include "cmd_options.h" +#include "cmd_response.h" #include "file_info.h" #include "file_manager_service_def.h" #include "file_manager_service_errno.h" @@ -58,9 +59,7 @@ int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel case Operation::CREATE_FILE: { string name = data.ReadString(); string path = data.ReadString(); - string uri; - errCode = CreateFile(name, path, uri); - reply.WriteString(uri); + errCode = CreateFile(name, path, reply); break; } default: { @@ -71,17 +70,33 @@ int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel return errCode; } -int MediaFileOper::CreateFile(const std::string &name, const std::string &path, std::string &uri) const +int MediaFileOper::CreateFile(const std::string &name, const std::string &path, MessageParcel &reply) const { string type = "file"; - return MediaFileUtils::DoInsert(name, path, type, uri); + std::string uri; + int ret = MediaFileUtils::DoInsert(name, path, type, uri); + CmdResponse cmdResponse; + cmdResponse.SetErr(ret); + cmdResponse.SetUri(uri); + reply.WriteParcelable(&cmdResponse); + return ret; } int MediaFileOper::ListFile(const string &type, const string &path, int offset, int count, MessageParcel &reply) const { shared_ptr result; int res = MediaFileUtils::DoListFile(type, path, offset, count, result); - return MediaFileUtils::GetFileInfoFromResult(result, reply, res); + if (res != SUCCESS) { + return res; + } + + std::vector> fileList; + res = MediaFileUtils::GetFileInfoFromResult(result, fileList); + CmdResponse cmdResponse; + cmdResponse.SetErr(res); + cmdResponse.SetFileInfoList(fileList); + reply.WriteParcelable(&cmdResponse); + return res; } int MediaFileOper::Mkdir(const string &name, const string &path) const diff --git a/services/src/fileoper/media_file_oper.h b/services/src/fileoper/media_file_oper.h index 8220a0ca..0652065d 100644 --- a/services/src/fileoper/media_file_oper.h +++ b/services/src/fileoper/media_file_oper.h @@ -25,7 +25,7 @@ public: virtual ~MediaFileOper() = default; int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) const override; private: - int CreateFile(const std::string &name, const std::string &path, std::string &uri) const; + int CreateFile(const std::string &name, const std::string &path, MessageParcel &reply) const; int ListFile(const std::string &type, const std::string &path, int offset, int count, MessageParcel &data) const; int Mkdir(const std::string &name, const std::string &path) const; }; diff --git a/services/src/fileoper/media_file_utils.cpp b/services/src/fileoper/media_file_utils.cpp index 5fbcba74..6ccc7f1b 100644 --- a/services/src/fileoper/media_file_utils.cpp +++ b/services/src/fileoper/media_file_utils.cpp @@ -357,7 +357,8 @@ bool MediaFileUtils::InitMediaTableColIndexMap(shared_ptr result, MessageParcel &reply) +bool MediaFileUtils::GetFileInfo(shared_ptr result, + unique_ptr &fileInfo) { if (!InitMediaTableColIndexMap(result)) { ERR_LOG("InitMediaTableColIndexMap returns fail"); @@ -368,45 +369,39 @@ bool MediaFileUtils::PushFileInfo(shared_ptr resu result->GetString(mediaTableMap[index++].first, id); string uri; result->GetString(mediaTableMap[index++].first, uri); - unique_ptr file = make_unique(); + string path = uri + "/" + id; - file->SetPath(path); + fileInfo->SetPath(path); string type; result->GetString(mediaTableMap[index++].first, type); - file->SetType(type); + fileInfo->SetType(type); string name; result->GetString(mediaTableMap[index++].first, name); - file->SetName(name); + fileInfo->SetName(name); int64_t value; result->GetLong(mediaTableMap[index++].first, value); - file->SetSize(value); + fileInfo->SetSize(value); result->GetLong(mediaTableMap[index++].first, value); - file->SetAddedTime(value); + fileInfo->SetAddedTime(value); result->GetLong(mediaTableMap[index++].first, value); - file->SetModifiedTime(value); - reply.WriteParcelable(file.get()); + fileInfo->SetModifiedTime(value); return true; } int MediaFileUtils::GetFileInfoFromResult(shared_ptr result, - MessageParcel &reply, int res) + std::vector> &fileList) { int count = 0; - if (res) { - // deal with status isn't succ; - ERR_LOG("AbsSharedResultSet status isn't succ"); - reply.WriteInt32(count); - return res; - } result->GetRowCount(count); - reply.WriteInt32(count); if (count == 0) { ERR_LOG("AbsSharedResultSet null"); return E_EMPTYFOLDER; } result->GoToFirstRow(); for (int i = 0; i < count; i++) { - PushFileInfo(result, reply); + unique_ptr fileInfo = make_unique(); + GetFileInfo(result, fileInfo); + fileList.push_back(move(fileInfo)); result->GoToNextRow(); } return SUCCESS; diff --git a/services/src/fileoper/media_file_utils.h b/services/src/fileoper/media_file_utils.h index 56ceff40..18b8bca5 100644 --- a/services/src/fileoper/media_file_utils.h +++ b/services/src/fileoper/media_file_utils.h @@ -40,10 +40,9 @@ public: static std::shared_ptr DoQuery(const std::string &selection, const std::vector &selectionArgs); static int DoInsert(const std::string &name, const std::string &path, const std::string &type, std::string &uri); - static bool PushFileInfo(std::shared_ptr result, MessageParcel &reply); - static bool PopFileInfo(FileInfo &file, MessageParcel &reply); + static bool GetFileInfo(std::shared_ptr result, std::unique_ptr &fileInfo); static int GetFileInfoFromResult(std::shared_ptr result, - MessageParcel &reply, int res); + std::vector> &fileList); static bool InitMediaTableColIndexMap(std::shared_ptr result); static bool InitHelper(sptr obj); private: -- Gitee From 535aa5030bcd79663cd831ad7da96342e0ec4eb4 Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Mon, 21 Feb 2022 11:40:39 +0800 Subject: [PATCH 2/5] add file count filter & alter ListFile,GetRoot,CreateFile interface's return type Signed-off-by: linjun9528 --- interfaces/kits/js/src/file_manager_napi.cpp | 6 ++--- services/include/file_manager_service_def.h | 2 +- services/src/fileoper/cmd_options.h | 2 ++ .../src/fileoper/external_storage_utils.cpp | 23 +++++++++++++------ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp index 8d9d0add..deef4f08 100644 --- a/interfaces/kits/js/src/file_manager_napi.cpp +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -92,7 +92,7 @@ tuple, unique_ptr, CmdOptions> GetCreateFileArg bool succ = false; unique_ptr path; unique_ptr fileName; - CmdOptions option("local", "", 0, 0, false); + CmdOptions option("local", "", 0, MAX_NUM, false); tie(succ, path, ignore) = NVal(env, funcArg[CreateFileArgs::CF_PATH]).ToUTF8String(); if (!succ) { return {false, nullptr, nullptr, option}; @@ -223,7 +223,7 @@ napi_value FileManagerNapi::GetRoot(napi_env env, napi_callback_info info) return nullptr; } - CmdOptions option("local", "", 0, 0, false); + CmdOptions option("local", "", 0, MAX_NUM, false); if (funcArg.GetArgc() != 0) { if (!GetRootArgs(env, funcArg, option)) { UniError(EINVAL).ThrowErr(env, "GetRoot func get dev para fails"); @@ -303,7 +303,7 @@ tuple, unique_ptr, CmdOptions> GetListFileArg( bool succ = false; unique_ptr path; unique_ptr type; - CmdOptions option("local", "", 0, 0, false); + CmdOptions option("local", "", 0, MAX_NUM, false); tie(succ, path, ignore) = NVal(env, funcArg[ListFileArgs::LF_PATH]).ToUTF8String(); if (!succ) { ERR_LOG("ListFileArgs LF_PATH para fails"); diff --git a/services/include/file_manager_service_def.h b/services/include/file_manager_service_def.h index 14a3f1a3..6992a138 100644 --- a/services/include/file_manager_service_def.h +++ b/services/include/file_manager_service_def.h @@ -41,7 +41,7 @@ enum VolumeState { MOUNTED, EJECTING }; -constexpr int32_t MAX_NUM = 200; + constexpr int32_t CODE_MASK = 0xff; constexpr int32_t EQUIPMENT_SHIFT = 16; diff --git a/services/src/fileoper/cmd_options.h b/services/src/fileoper/cmd_options.h index e3619d29..4dc8472c 100644 --- a/services/src/fileoper/cmd_options.h +++ b/services/src/fileoper/cmd_options.h @@ -19,6 +19,8 @@ namespace OHOS { namespace FileManagerService { +constexpr int64_t MAX_NUM = 200; + class DevInfo { public: DevInfo() = default; diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp index 92c997d7..2197c529 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -126,13 +126,20 @@ int ExternalStorageUtils::DoListFile(const std::string &type, const std::string ERR_LOG("opendir path[%{public}s] fail.", path.c_str()); return E_NOEXIST; } - int64_t index = 0; - for (struct dirent *ent = readdir(dir); ent != nullptr; ent = readdir(dir)) { - if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { - continue; - } - if (index < offset) { + if (offset != 0) { + int64_t index = 0; + for (dirent *ent = readdir(dir); ent != nullptr; ent = readdir(dir)) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { + continue; + } + if (index == offset - 1) { + break; + } index++; + } + } + for (dirent *ent = readdir(dir); ent != nullptr; ent = readdir(dir)) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { continue; } if (count > 0) { @@ -146,9 +153,11 @@ int ExternalStorageUtils::DoListFile(const std::string &type, const std::string break; } } - index++; } closedir(dir); + if (option.GetCount() == MAX_NUM && count == 0) { + DEBUG_LOG("get files with MAX_NUM:[%{public}lld].", MAX_NUM); + } return SUCCESS; } -- Gitee From 0b8b0f31fb9ffda599db6666d66d5269b8f815f6 Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Mon, 21 Feb 2022 11:47:06 +0800 Subject: [PATCH 3/5] add file count filter & alter ListFile,GetRoot,CreateFile interface's return type Signed-off-by: linjun9528 --- services/src/fileoper/external_storage_utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp index 2197c529..09844c2d 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -109,7 +109,7 @@ int ExternalStorageUtils::DoListFile(const std::string &type, const std::string { int64_t count = option.GetCount(); int64_t offset = option.GetOffset(); - if (count < 0 || count > MAX_NUM || offset < 0 || offset > MAX_NUM) { + if (count < 0 || count > MAX_NUM || offset < 0) { ERR_LOG("invalid file count or offset."); return E_INVALID_FILE_NUMBER; } -- Gitee From b6d37fbae25b287f2f85cfbe10eb9e2fc9693984 Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Mon, 21 Feb 2022 11:51:11 +0800 Subject: [PATCH 4/5] add file count filter & alter ListFile,GetRoot,CreateFile interface's return type Signed-off-by: linjun9528 --- services/src/fileoper/external_storage_utils.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp index 09844c2d..ed99964b 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -113,8 +113,6 @@ int ExternalStorageUtils::DoListFile(const std::string &type, const std::string ERR_LOG("invalid file count or offset."); return E_INVALID_FILE_NUMBER; } - count = (count == 0) ? MAX_NUM : count; - std::string path; if (!ConvertUriToAbsolutePath(uri, path)) { ERR_LOG("invalid uri[%{public}s].", uri.c_str()); -- Gitee From 7c339b11f1eaca782d5b43692e67e1ac0941b41c Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Mon, 21 Feb 2022 12:55:12 +0800 Subject: [PATCH 5/5] add file count filter & alter ListFile,GetRoot,CreateFile interface's return type Signed-off-by: linjun9528 --- interfaces/kits/js/BUILD.gn | 2 ++ interfaces/kits/js/src/file_manager_napi.cpp | 1 + services/include/file_manager_service_def.h | 2 +- services/src/fileoper/cmd_options.h | 6 ++---- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 097fba03..fb477512 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -32,6 +32,7 @@ ohos_shared_library("filemanager") { "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi/n_async", "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi", "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common", + "//foundation/multimedia/medialibrary_standard/interfaces/innerkits/native/include", ] sources = [ @@ -43,6 +44,7 @@ ohos_shared_library("filemanager") { "$FMS_BASE_DIR:fms_server", "//foundation/ace/napi:ace_napi", "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js:build_kits_js", + "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/medialibrary_data_ability:medialibrary_data_ability", "//utils/native/base:utils", ] cflags = [] diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp index deef4f08..a211b0ba 100644 --- a/interfaces/kits/js/src/file_manager_napi.cpp +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -22,6 +22,7 @@ #include "file_manager_napi_def.h" #include "file_manager_proxy.h" +#include "file_manager_service_def.h" #include "file_manager_service_errno.h" #include "ifms_client.h" #include "log.h" diff --git a/services/include/file_manager_service_def.h b/services/include/file_manager_service_def.h index 6992a138..1cd2f168 100644 --- a/services/include/file_manager_service_def.h +++ b/services/include/file_manager_service_def.h @@ -41,7 +41,7 @@ enum VolumeState { MOUNTED, EJECTING }; - +constexpr int64_t MAX_NUM = 200; constexpr int32_t CODE_MASK = 0xff; constexpr int32_t EQUIPMENT_SHIFT = 16; diff --git a/services/src/fileoper/cmd_options.h b/services/src/fileoper/cmd_options.h index 4dc8472c..b119ead8 100644 --- a/services/src/fileoper/cmd_options.h +++ b/services/src/fileoper/cmd_options.h @@ -16,11 +16,9 @@ #define STORAGE_SERVICES_DEV_INFO_H #include - +#include "file_manager_service_def.h" namespace OHOS { namespace FileManagerService { -constexpr int64_t MAX_NUM = 200; - class DevInfo { public: DevInfo() = default; @@ -125,7 +123,7 @@ public: private: DevInfo dev_; int64_t offset_ {0}; - int64_t count_ {0}; + int64_t count_ {MAX_NUM}; bool hasOpt_ {false}; }; } // namespace FileManagerService -- Gitee