diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp index 4b3a8f3af91845bd95af9a37b47abd6833373dc1..2d8432d86db7596eee735cef214f740e3366fa39 100644 --- a/interfaces/kits/js/src/file_manager_napi.cpp +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -73,6 +73,50 @@ UniError DealWithErrno(int err) } } +tuple, unique_ptr, CmdOptions> GetCreateFileArgs(napi_env env, NFuncArg &funcArg) +{ + bool succ = false; + unique_ptr path; + unique_ptr fileName; + CmdOptions option("local", "", 0, 0, false); + tie(succ, path, ignore) = NVal(env, funcArg[CreateFileArgs::CF_PATH]).ToUTF8String(); + if (!succ) { + return {false, nullptr, nullptr, option}; + } + tie(succ, fileName, ignore) = NVal(env, funcArg[CreateFileArgs::CF_FILENAME]).ToUTF8String(); + if (!succ) { + return {false, nullptr, nullptr, option}; + } + + if (funcArg.GetArgc() < CreateFileArgs::CF_OPTION) { + return {false, nullptr, nullptr, option}; + } + + NVal op(env, NVal(env, funcArg[CreateFileArgs::CF_OPTION]).val_); + if (op.TypeIs(napi_function)) { + return {true, move(path), move(fileName), option}; + } + + option.SetHasOpt(true); + if (!op.HasProp("dev")) { + return {true, move(path), move(fileName), option}; + } + + NVal dev(op.GetProp("dev")); + if (dev.HasProp("name")) { + bool ret = false; + unique_ptr strName; + tie(ret, strName, ignore) = dev.GetProp("name").ToUTF8String(); + if (!ret) { + ERR_LOG("CreateFile func get dev para fails"); + return {false, nullptr, nullptr, option}; + } + option.SetDevInfo(DevInfo(strName.get(), "")); + } + + return {true, move(path), move(fileName), option}; +} + napi_value FileManagerNapi::CreateFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -81,20 +125,17 @@ napi_value FileManagerNapi::CreateFile(napi_env env, napi_callback_info info) return nullptr; } bool succ = false; - unique_ptr name; unique_ptr path; - tie(succ, name, ignore) = NVal(env, funcArg[CreateFileArgs::CF_FILENAME]).ToUTF8String(); - if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid name"); - return nullptr; - } - tie(succ, path, ignore) = NVal(env, funcArg[CreateFileArgs::CF_PATH]).ToUTF8String(); + unique_ptr fileName; + CmdOptions option; + tie(succ, path, fileName, option) = GetCreateFileArgs(env, funcArg); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid path"); + UniError(EINVAL).ThrowErr(env, "CreateFile func get args fails"); return nullptr; } auto arg = make_shared(NVal(env, funcArg.GetThisVar())); - auto cbExec = [arg, name = string(name.get()), path = string(path.get())] (napi_env env) -> UniError { + auto cbExec = [arg, path = string(path.get()), fileName = string(fileName.get()), option = option] + (napi_env env) -> UniError { IFmsClient* client = nullptr; bool succ = false; tie(succ, client) = GetFmsClient(); @@ -102,7 +143,7 @@ napi_value FileManagerNapi::CreateFile(napi_env env, napi_callback_info info) return UniError(ESRCH); } string uri = ""; - int err = client->CreateFile(name, path, arg->uri_); + int err = client->CreateFile(path, fileName, option, arg->uri_); return DealWithErrno(err); }; auto cbComplete = [arg](napi_env env, UniError err) -> NVal { @@ -115,10 +156,12 @@ napi_value FileManagerNapi::CreateFile(napi_env env, napi_callback_info info) string procedureName = "CreateFile"; int argc = funcArg.GetArgc(); NVal thisVar(env, funcArg.GetThisVar()); - if (argc == CREATE_FILE_PARA_MIN) { + if (argc == CREATE_FILE_PARA_MIN || (argc != CREATE_FILE_PARA_MAX && option.GetHasOpt())) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } else { - NVal cb(env, funcArg[CreateFileArgs::CF_CALLBACK]); + int cbIdx = (!option.GetHasOpt() ? + CreateFileArgs::CF_CALLBACK_WITHOUT_OP : CreateFileArgs::CF_CALLBACK_WITH_OP); + NVal cb(env, funcArg[cbIdx]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } } @@ -138,6 +181,33 @@ void CreateFileArray(napi_env env, shared_ptr arg) } } +bool GetRootArgs(napi_env env, NFuncArg &funcArg, CmdOptions &option) +{ + NVal op(env, NVal(env, funcArg[GetRootArgs::GR_OPTION]).val_); + if (op.TypeIs(napi_function)) { + return true; + } + + option.SetHasOpt(true); + if (!op.HasProp("dev")) { + return true; + } + + NVal dev(op.GetProp("dev")); + if (dev.HasProp("name")) { + bool ret = false; + unique_ptr name; + tie(ret, name, ignore) = dev.GetProp("name").ToUTF8String(); + if (!ret) { + ERR_LOG("GetRoot func get dev para fails"); + return false; + } + option.SetDevInfo(DevInfo(name.get(), "")); + } + + return true; +} + napi_value FileManagerNapi::GetRoot(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -145,17 +215,26 @@ napi_value FileManagerNapi::GetRoot(napi_env env, napi_callback_info info) UniError(EINVAL).ThrowErr(env, "Number of argments unmatched"); return nullptr; } + + CmdOptions option("local", "", 0, 0, false); + if (funcArg.GetArgc() != 0) { + if (!GetRootArgs(env, funcArg, option)) { + UniError(EINVAL).ThrowErr(env, "GetRoot func get dev para fails"); + return nullptr; + } + } + napi_value fileArr; napi_create_array(env, &fileArr); auto arg = make_shared(NVal(env, fileArr)); - auto cbExec = [arg] (napi_env env) -> UniError { + auto cbExec = [option = option, arg] (napi_env env) -> UniError { IFmsClient* client = nullptr; bool succ = false; tie(succ, client) = GetFmsClient(); if (!succ) { return UniError(ESRCH); } - int err = client->GetRoot("local", arg->fileRes_); + int err = client->GetRoot(option, arg->fileRes_); return DealWithErrno(err); }; auto cbComplete = [arg](napi_env env, UniError err) -> NVal { @@ -169,15 +248,16 @@ napi_value FileManagerNapi::GetRoot(napi_env env, napi_callback_info info) string procedureName = "GetRoot"; int argc = funcArg.GetArgc(); NVal thisVar(env, funcArg.GetThisVar()); - if (argc == GET_ROOT_PARA_MIN) { + if (argc == GET_ROOT_PARA_MIN || (argc != GET_ROOT_PARA_MAX && option.GetHasOpt())) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } else { - NVal cb(env, funcArg[GetRootArgs::GR_CALLBACK]); + int cbIdx = (!option.GetHasOpt() ? GetRootArgs::GR_CALLBACK_WITHOUT_OP : GetRootArgs::GR_CALLBACK_WITH_OP); + NVal cb(env, funcArg[cbIdx]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } } -bool GetLstFileOption(const NVal &argv, CmdOptions &option) +bool GetListFileOption(const NVal &argv, CmdOptions &option) { bool ret = false; if (argv.HasProp("dev")) { @@ -236,7 +316,7 @@ tuple, unique_ptr, CmdOptions> GetListFileArg( return {true, move(type), move(path), option}; } option.SetHasOpt(true); - if (!GetLstFileOption(op, option)) { + if (!GetListFileOption(op, option)) { return {false, nullptr, nullptr, option}; } return {true, move(type), move(path), option}; diff --git a/interfaces/kits/js/src/file_manager_napi_def.h b/interfaces/kits/js/src/file_manager_napi_def.h index 1d008cd986bbe5e1f303b1f4e76bc0cf85e2cf4d..05d753484fda359baf38733764dac37cc666e9fc 100644 --- a/interfaces/kits/js/src/file_manager_napi_def.h +++ b/interfaces/kits/js/src/file_manager_napi_def.h @@ -22,15 +22,17 @@ namespace OHOS { namespace FileManagerService { enum CreateFileArgs { - CF_DEV = 0, + CF_PATH = 0, CF_FILENAME = 1, - CF_PATH = 2, - CF_CALLBACK = 3, + CF_OPTION = 2, + CF_CALLBACK_WITHOUT_OP = 2, + CF_CALLBACK_WITH_OP = 3, }; enum GetRootArgs { - GR_DEV = 0, - GR_CALLBACK = 1, + GR_OPTION = 0, + GR_CALLBACK_WITHOUT_OP = 0, + GR_CALLBACK_WITH_OP = 1, }; enum ListFileArgs { @@ -42,9 +44,9 @@ enum ListFileArgs { }; constexpr int CREATE_FILE_PARA_MAX = 4; -constexpr int CREATE_FILE_PARA_MIN = 3; +constexpr int CREATE_FILE_PARA_MIN = 2; constexpr int GET_ROOT_PARA_MAX = 2; -constexpr int GET_ROOT_PARA_MIN = 1; +constexpr int GET_ROOT_PARA_MIN = 0; constexpr int LIST_FILE_PARA_MAX = 4; constexpr int LIST_FILE_PARA_MIN = 2; } // namespace FileManagerService diff --git a/services/src/client/file_manager_proxy.cpp b/services/src/client/file_manager_proxy.cpp index f3ee9466037a1084adfc54b3a33eb723db4ac122..b3a120ba2ed6315afd6c1358f14f0d842f9e57d8 100644 --- a/services/src/client/file_manager_proxy.cpp +++ b/services/src/client/file_manager_proxy.cpp @@ -27,15 +27,16 @@ namespace OHOS { namespace FileManagerService { FileManagerProxy::FileManagerProxy(const sptr &impl) : IRemoteProxy(impl) {} -int FileManagerProxy::GetRoot(const std::string &devName, vector> &fileRes) +int FileManagerProxy::GetRoot(const CmdOptions &option, vector> &fileRes) { - if (devName == "external_storage") { + CmdOptions op(option); + if (op.GetDevInfo().GetName() == "external_storage") { MessageParcel data; - data.WriteString(devName); + data.WriteString(op.GetDevInfo().GetName()); MessageParcel reply; - MessageOption option; + MessageOption messageOption; int code = (Equipment::EXTERNAL_STORAGE << EQUIPMENT_SHIFT) | Operation::GET_ROOT; - int err = Remote()->SendRequest(code, data, reply, option); + 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; @@ -63,14 +64,20 @@ int FileManagerProxy::GetRoot(const std::string &devName, vectorSendRequest(Operation::CREATE_FILE, data, reply, option); + MessageOption messageOption; + int code = Operation::CREATE_FILE; + CmdOptions op(option); + if (op.GetDevInfo().GetName() == "external_storage") { + code = (Equipment::EXTERNAL_STORAGE << EQUIPMENT_SHIFT) | Operation::CREATE_FILE; + } + int err = Remote()->SendRequest(code, data, reply, messageOption); if (err != ERR_NONE) { ERR_LOG("inner error send request fail %{public}d", err); return FAIL; diff --git a/services/src/client/file_manager_proxy.h b/services/src/client/file_manager_proxy.h index 3f733b05a15e5630055108a2f07725388d3c34e8..085d32361989e70bc933156aab91ba787f78c6ec 100644 --- a/services/src/client/file_manager_proxy.h +++ b/services/src/client/file_manager_proxy.h @@ -30,8 +30,9 @@ public: 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; - int CreateFile(const std::string &name, const std::string &path, std::string &uri) override; - int GetRoot(const std::string &devName, 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; private: static inline BrokerDelegator delegator_; }; diff --git a/services/src/client/ifms_client.h b/services/src/client/ifms_client.h index c03898f6ec03a21cdb12fc6acc61fbe595c74949..386c4febe5c168cdf0a62da52b9acb289fcb65b5 100644 --- a/services/src/client/ifms_client.h +++ b/services/src/client/ifms_client.h @@ -25,8 +25,9 @@ public: 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 std::string &devName, std::vector> &fileRes) = 0; - virtual int CreateFile(const std::string &name, const std::string &path, std::string &uri) = 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; }; } // namespace FileManagerService { } // namespace OHOS diff --git a/services/src/fileoper/cmd_options.h b/services/src/fileoper/cmd_options.h index 4eae65c1e696463e561eba0f6bbdd844ce4c7e86..e3619d29a34e0e1f74d96dfd33074ce21b05f685 100644 --- a/services/src/fileoper/cmd_options.h +++ b/services/src/fileoper/cmd_options.h @@ -39,7 +39,7 @@ public: return *this; } - std::string GetName() + std::string GetName() const { return name_; } @@ -49,7 +49,7 @@ public: name_ = name; } - std::string GetPath() + std::string GetPath() const { return path_; } @@ -80,7 +80,7 @@ public: CmdOptions(const CmdOptions &option) = default; CmdOptions& operator=(const CmdOptions& option) = default; - DevInfo GetDevInfo() + DevInfo GetDevInfo() const { return dev_; } @@ -90,7 +90,7 @@ public: dev_ = dev; } - int64_t GetOffset() + int64_t GetOffset() const { return offset_; } @@ -100,7 +100,7 @@ public: offset_ = offset; } - int64_t GetCount() + int64_t GetCount() const { return count_; } @@ -110,7 +110,7 @@ public: count_ = count; } - bool GetHasOpt() + bool GetHasOpt() const { return hasOpt_; } diff --git a/services/src/fileoper/external_storage_oper.cpp b/services/src/fileoper/external_storage_oper.cpp index da0f6c52c43f2bd0ed3e56e844eadea957ccd0fe..a23e5b92ad1d1ba7d64335f06eb919302539c2db 100644 --- a/services/src/fileoper/external_storage_oper.cpp +++ b/services/src/fileoper/external_storage_oper.cpp @@ -53,7 +53,7 @@ int ExternalStorageOper::OperProcess(uint32_t code, MessageParcel &data, Message string path = data.ReadString(); // name for extension string name = "name"; - errCode = GetRoot(path, name, reply); + errCode = GetRoot(name, path, reply); break; } default: { @@ -64,9 +64,9 @@ int ExternalStorageOper::OperProcess(uint32_t code, MessageParcel &data, Message return errCode; } -int ExternalStorageOper::GetRoot(const std::string &path, const std::string &name, MessageParcel &reply) const +int ExternalStorageOper::GetRoot(const std::string &name, const std::string &path, MessageParcel &reply) const { - return ExternalStorageUtils::DoGetRoot(path, name, reply); + return ExternalStorageUtils::DoGetRoot(name, path, reply); } int ExternalStorageOper::CreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) const diff --git a/services/src/fileoper/external_storage_oper.h b/services/src/fileoper/external_storage_oper.h index 53ee4e2ebca5bd805b38dfec999be2a6ee1f7eff..af5ee57a4d0c23f826e175370fbc8fcd06918b99 100644 --- a/services/src/fileoper/external_storage_oper.h +++ b/services/src/fileoper/external_storage_oper.h @@ -29,7 +29,7 @@ private: int CreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) const; int ListFile(const std::string &type, const std::string &uri, const CmdOptions &option, MessageParcel &reply) const; - int GetRoot(const std::string &path, const std::string &name, MessageParcel &reply) const; + int GetRoot(const std::string &name, const std::string &path, MessageParcel &reply) const; }; } // namespace FileManagerService } // namespace OHOS diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp index 8edf3678392c4fca276462990be1e968a59104c8..63d654b094318b3d871b07a4eae918a6c6607c47 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -161,7 +161,7 @@ int ExternalStorageUtils::DoCreateFile(const std::string &uri, const std::string return SUCCESS; } -int ExternalStorageUtils::DoGetRoot(const std::string &path, const std::string &name, MessageParcel &reply) +int ExternalStorageUtils::DoGetRoot(const std::string &name, const std::string &path, MessageParcel &reply) { vector vecRootPath; #ifdef VOLUME_ENABLE @@ -172,8 +172,8 @@ int ExternalStorageUtils::DoGetRoot(const std::string &path, const std::string & } #endif reply.WriteInt32(vecRootPath.size()); - for (auto path : vecRootPath) { - reply.WriteString(path); + for (auto rootPath : vecRootPath) { + reply.WriteString(rootPath); } return SUCCESS; } diff --git a/services/src/fileoper/external_storage_utils.h b/services/src/fileoper/external_storage_utils.h index c09ba5622b7655703c26aa77528bbc4f0c4975ae..61a139da2f78a11cac8d74788324ef114380334a 100644 --- a/services/src/fileoper/external_storage_utils.h +++ b/services/src/fileoper/external_storage_utils.h @@ -31,7 +31,7 @@ public: 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 &path, const std::string &name, MessageParcel &reply); + static int DoGetRoot(const std::string &name, const std::string &path, MessageParcel &reply); }; } // namespace FileManagerService } // namespace OHOS