diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index fb477512e2b2568c72a4bd508d95d176a4bafc91..ed6253eb5d44bd68cc63f7ce2b538c3d3a483f7b 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -43,7 +43,7 @@ ohos_shared_library("filemanager") { deps = [ "$FMS_BASE_DIR:fms_server", "//foundation/ace/napi:ace_napi", - "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js:build_kits_js", + "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js:fileio", "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/medialibrary_data_ability:medialibrary_data_ability", "//utils/native/base:utils", ] diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp index b3a6765e395aeb0dd15901c672a7736230295279..f919bfbdffa88b044934e637c2578b3f148bc430 100644 --- a/interfaces/kits/js/src/file_manager_napi.cpp +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -178,11 +178,15 @@ napi_value FileManagerNapi::CreateFile(napi_env env, napi_callback_info info) } } -void CreateFileArray(napi_env env, shared_ptr arg) +static bool CreateFileArray(napi_env env, shared_ptr arg) { for (unsigned int i = 0; i < arg->fileRes_.size(); i++) { NVal obj = NVal::CreateObject(env); - shared_ptr &res = arg->fileRes_[i]; + shared_ptr res = arg->fileRes_[i]; + if (res == nullptr) { + ERR_LOG("inner error, lack of memory, file count %{public}d", arg->fileRes_.size()); + return false; + } 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_); @@ -191,6 +195,7 @@ void CreateFileArray(napi_env env, shared_ptr arg) obj.AddProp("modified_time", NVal::CreateInt64(env, res->GetModifiedTime()).val_); napi_set_property(env, arg->ref_.Deref(env).val_, NVal::CreateInt32(env, i).val_, obj.val_); } + return true; } bool GetRootArgs(napi_env env, NFuncArg &funcArg, CmdOptions &option) @@ -246,7 +251,9 @@ napi_value FileManagerNapi::GetRoot(napi_env env, napi_callback_info info) return DealWithErrno(err); }; auto cbComplete = [arg](napi_env env, UniError err) -> NVal { - CreateFileArray(env, arg); + if (!err && !CreateFileArray(env, arg)) { + err = UniError(ENOMEM); + } if (err) { return { env, err.GetNapiErr(env) }; } else { @@ -359,10 +366,12 @@ napi_value FileManagerNapi::ListFile(napi_env env, napi_callback_info info) }; auto cbComplete = [arg](napi_env env, UniError err) -> NVal { + if (!err && !CreateFileArray(env, arg)) { + err = UniError(ENOMEM); + } if (err) { return { env, err.GetNapiErr(env) }; } else { - CreateFileArray(env, arg); return NVal(env, arg->ref_.Deref(env).val_); } }; diff --git a/services/src/client/file_manager_proxy.cpp b/services/src/client/file_manager_proxy.cpp index 9a27522193c6371b92b1fe50a8e828e10711da0f..663efaa91bb5f57bc73165c2509afe40893bc78e 100644 --- a/services/src/client/file_manager_proxy.cpp +++ b/services/src/client/file_manager_proxy.cpp @@ -41,35 +41,26 @@ FileManagerProxy::FileManagerProxy(const sptr &impl) int FileManagerProxy::GetRoot(const CmdOptions &option, vector> &fileRes) { CmdOptions op(option); + int code = Operation::GET_ROOT; if (op.GetDevInfo().GetName() == "external_storage") { - MessageParcel data; - data.WriteString(op.GetDevInfo().GetName()); - MessageParcel reply; - MessageOption messageOption; - int code = (Equipment::EXTERNAL_STORAGE << EQUIPMENT_SHIFT) | Operation::GET_ROOT; - int err = Remote()->SendRequest(code, data, reply, messageOption); - if (err != ERR_NONE) { - ERR_LOG("GetRoot inner error send request fail %{public}d", err); - return FAIL; - } - sptr cmdResponse; - err = GetCmdResponse(reply, cmdResponse); - if (err != ERR_NONE) { - return err; - } - fileRes = cmdResponse->GetFileInfoList(); + code = (Equipment::EXTERNAL_STORAGE << EQUIPMENT_SHIFT) | Operation::GET_ROOT; + } + MessageParcel data; + data.WriteString(op.GetDevInfo().GetName()); + MessageParcel reply; + MessageOption messageOption; + int err = Remote()->SendRequest(code, data, reply, messageOption); + if (err != ERR_NONE) { + ERR_LOG("GetRoot inner error send request fail %{public}d", err); + return FAIL; + } + sptr cmdResponse; + err = GetCmdResponse(reply, cmdResponse); + if (err != ERR_NONE) { return err; - } else { - shared_ptr image = make_shared(IMAGE_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); - fileRes.emplace_back(image); - shared_ptr video = make_shared(VIDEO_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); - fileRes.emplace_back(video); - shared_ptr audio = make_shared(AUDIO_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); - fileRes.emplace_back(audio); - shared_ptr file = make_shared(FILE_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); - fileRes.emplace_back(file); - return SUCCESS; } + fileRes = cmdResponse->GetFileInfoList(); + return err; } int FileManagerProxy::CreateFile(const std::string &path, const std::string &fileName, diff --git a/services/src/fileoper/cmd_response.h b/services/src/fileoper/cmd_response.h index 09d1a1f25a6d2b1f240413f5327c7ea1898eab6b..ba561b016364319879eae4d14e9e662af00bd414 100644 --- a/services/src/fileoper/cmd_response.h +++ b/services/src/fileoper/cmd_response.h @@ -20,6 +20,7 @@ #include #include "file_info.h" +#include "log.h" #include "parcel.h" namespace OHOS { namespace FileManagerService { @@ -68,7 +69,10 @@ public: int fileCount = vecFileInfo_.size(); parcel.WriteInt32(fileCount); for (int i = 0; i < fileCount; i++) { - parcel.WriteParcelable(vecFileInfo_[i].get()); + if (parcel.WriteParcelable(vecFileInfo_[i].get()) != true) { + ERR_LOG("Marshalling fail total:%{public}d i:%{public}d", fileCount, i); + return false; + } } return true; } diff --git a/services/src/fileoper/external_storage_oper.cpp b/services/src/fileoper/external_storage_oper.cpp index fd36754f756c8d24eee661e837b4b516aa138915..d19997353d4e5a09e05c69d819bb4d1fabcaa0b1 100644 --- a/services/src/fileoper/external_storage_oper.cpp +++ b/services/src/fileoper/external_storage_oper.cpp @@ -72,7 +72,9 @@ int ExternalStorageOper::GetRoot(const std::string &name, const std::string &pat CmdResponse cmdResponse; cmdResponse.SetErr(ret); cmdResponse.SetFileInfoList(fileList); - reply.WriteParcelable(&cmdResponse); + if (!reply.WriteParcelable(&cmdResponse)) { + ERR_LOG("reply write err parcel capacity%{public}d", reply.GetDataCapacity()); + } return ret; } @@ -83,7 +85,9 @@ int ExternalStorageOper::CreateFile(const std::string &uri, const std::string &n CmdResponse cmdResponse; cmdResponse.SetErr(ret); cmdResponse.SetUri(resultUir); - reply.WriteParcelable(&cmdResponse); + if (!reply.WriteParcelable(&cmdResponse)) { + ERR_LOG("reply write err parcel capacity:%{public}d", reply.GetDataCapacity()); + } return ret; } @@ -95,7 +99,9 @@ int ExternalStorageOper::ListFile(const std::string &type, const std::string &ur CmdResponse cmdResponse; cmdResponse.SetErr(ret); cmdResponse.SetFileInfoList(fileList); - reply.WriteParcelable(&cmdResponse); + if (!reply.WriteParcelable(&cmdResponse)) { + ERR_LOG("reply write err parcel capacity:%{public}d", reply.GetDataCapacity()); + } return ret; } } // namespace FileManagerService diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp index 715bcfc7c5dff2a22db53fd7a05d23a789a1b84b..9e267f2df646a2d291acbe64838711db5945af58 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -113,6 +113,7 @@ int ExternalStorageUtils::DoListFile(const std::string &type, const std::string ERR_LOG("invalid file count or offset."); return E_INVALID_FILE_NUMBER; } + DEBUG_LOG("limit %{public}lld, offset %{public}lld", count, offset); std::string path; if (!ConvertUriToAbsolutePath(uri, path)) { ERR_LOG("invalid uri[%{public}s].", uri.c_str()); @@ -192,7 +193,7 @@ int ExternalStorageUtils::DoGetRoot(const std::string &name, const std::string & { vector vecRootPath; if (!StorageManagerInf::GetMountedVolumes(vecRootPath)) { - ERR_LOG("there is valid extorage storage"); + ERR_LOG("none valid extorage storage"); return FAIL; } for (auto rootPath : vecRootPath) { diff --git a/services/src/fileoper/media_file_oper.cpp b/services/src/fileoper/media_file_oper.cpp index 3e9fd3fe2c0d3af3a313a8e2ccc0b9a8afa554f8..4fc457912622b02a1005b401c6b5e6c2077fce49 100644 --- a/services/src/fileoper/media_file_oper.cpp +++ b/services/src/fileoper/media_file_oper.cpp @@ -45,13 +45,20 @@ int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel errCode = Mkdir(name, path); break; } + case Operation::GET_ROOT: { + string path = data.ReadString(); + // name for extension + string name = "name"; + errCode = GetRoot(name, path, reply); + break; + } case Operation::LIST_FILE: { string devName = data.ReadString(); string devPath = data.ReadString(); string type = data.ReadString(); string path = data.ReadString(); - int off = data.ReadInt32(); - int count = data.ReadInt32(); + int off = data.ReadInt64(); + int count = data.ReadInt64(); // put fileInfo into reply errCode = ListFile(type, path, off, count, reply); break; @@ -78,7 +85,22 @@ int MediaFileOper::CreateFile(const std::string &name, const std::string &path, CmdResponse cmdResponse; cmdResponse.SetErr(ret); cmdResponse.SetUri(uri); - reply.WriteParcelable(&cmdResponse); + if (!reply.WriteParcelable(&cmdResponse)) { + ERR_LOG("reply write err parcel capacity:%{public}d", reply.GetDataCapacity()); + } + return ret; +} + +int MediaFileOper::GetRoot(const std::string &name, const std::string &path, MessageParcel &reply) const +{ + std::vector> fileList; + int ret = MediaFileUtils::DoGetRoot(name, path, fileList); + CmdResponse cmdResponse; + cmdResponse.SetErr(ret); + cmdResponse.SetFileInfoList(fileList); + if (!reply.WriteParcelable(&cmdResponse)) { + ERR_LOG("reply write err parcel capacity%{public}d", reply.GetDataCapacity()); + } return ret; } @@ -95,7 +117,9 @@ int MediaFileOper::ListFile(const string &type, const string &path, int offset, CmdResponse cmdResponse; cmdResponse.SetErr(res); cmdResponse.SetFileInfoList(fileList); - reply.WriteParcelable(&cmdResponse); + if (!reply.WriteParcelable(&cmdResponse)) { + ERR_LOG("reply write err parcel capacity:%{public}d", reply.GetDataCapacity()); + } return res; } diff --git a/services/src/fileoper/media_file_oper.h b/services/src/fileoper/media_file_oper.h index 0652065da9e711487ba1fcc0a243dbfbbc217227..bc4374b212fd9a7909a7bd1d2b5ab2e2c660b98d 100644 --- a/services/src/fileoper/media_file_oper.h +++ b/services/src/fileoper/media_file_oper.h @@ -27,6 +27,7 @@ public: private: 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 GetRoot(const std::string &name, const std::string &path, MessageParcel &reply) const; int Mkdir(const std::string &name, const std::string &path) const; }; } // namespace FileManagerService diff --git a/services/src/fileoper/media_file_utils.cpp b/services/src/fileoper/media_file_utils.cpp index 9c4889f5dbead1fd3ef6f6462f5bd76801446581..4169a7488cfffd377cd7548d8d3b4575f6898a58 100644 --- a/services/src/fileoper/media_file_utils.cpp +++ b/services/src/fileoper/media_file_utils.cpp @@ -287,7 +287,7 @@ int MediaFileUtils::DoListFile(const string &type, const string &path, int offse return err; } } - result = DoQuery(selection, selectionArgs); + result = DoQuery(selection, selectionArgs, offset, count); if (result == nullptr) { ERR_LOG("ListFile folder is empty"); return E_EMPTYFOLDER; @@ -297,11 +297,20 @@ int MediaFileUtils::DoListFile(const string &type, const string &path, int offse shared_ptr MediaFileUtils::DoQuery(const string &selection, const vector &selectionArgs) +{ + return DoQuery(selection, selectionArgs, 0, MAX_NUM); +} + +shared_ptr MediaFileUtils::DoQuery(const string &selection, + const vector &selectionArgs, int offset, int count) { ShowSelecArgs(selection, selectionArgs); NativeRdb::DataAbilityPredicates predicates; predicates.SetWhereClause(selection); predicates.SetWhereArgs(selectionArgs); + predicates.Limit(count); + predicates.Offset(offset); + DEBUG_LOG("limit %{public}d, offset %{public}d", count, offset); Uri uri = Uri(Media::MEDIALIBRARY_DATA_URI); vector columns; return abilityHelper->Query(uri, columns, predicates); @@ -418,5 +427,19 @@ bool MediaFileUtils::InitHelper(sptr obj) } return true; } + +int MediaFileUtils::DoGetRoot(const std::string &name, const std::string &path, + std::vector> &fileList) +{ + shared_ptr image = make_shared(IMAGE_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); + fileList.emplace_back(image); + shared_ptr video = make_shared(VIDEO_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); + fileList.emplace_back(video); + shared_ptr audio = make_shared(AUDIO_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); + fileList.emplace_back(audio); + shared_ptr file = make_shared(FILE_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); + fileList.emplace_back(file); + return SUCCESS; +} } // namespace FileManagerService } // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/media_file_utils.h b/services/src/fileoper/media_file_utils.h index 2bdfdfa167f9adf33479203c87e5cc68bed63740..0a3b923f6893cae853c1434db00c6e863b27d508 100644 --- a/services/src/fileoper/media_file_utils.h +++ b/services/src/fileoper/media_file_utils.h @@ -35,10 +35,14 @@ class MediaFileUtils { public: MediaFileUtils(); ~MediaFileUtils(); + static int DoGetRoot(const std::string &name, const std::string &path, + std::vector> &fileList); static int DoListFile(const std::string &type, const std::string &path, int offset, int count, std::shared_ptr &result); static std::shared_ptr DoQuery(const std::string &selection, const std::vector &selectionArgs); + static std::shared_ptr DoQuery(const std::string &selection, + const std::vector &selectionArgs, int offset, int count); static int DoInsert(const std::string &name, const std::string &path, const std::string &type, std::string &uri); static bool GetFileInfo(std::shared_ptr result, std::shared_ptr &fileInfo); static int GetFileInfoFromResult(std::shared_ptr result,