From dc3d27d2b93f2b1c49dce87b4193fa6c8184b754 Mon Sep 17 00:00:00 2001 From: Dageking Date: Fri, 28 Jan 2022 18:10:10 +0800 Subject: [PATCH 1/3] add external storage code Signed-off-by: Dageking --- interfaces/kits/js/src/file_manager_napi.cpp | 101 +++++++----- .../kits/js/src/file_manager_napi_def.h | 13 +- services/BUILD.gn | 2 + services/src/client/file_manager_proxy.cpp | 21 ++- services/src/client/file_manager_proxy.h | 4 +- services/src/client/ifms_client.h | 6 +- services/src/fileoper/cmd_options.h | 131 +++++++++++++++ .../src/fileoper/external_storage_oper.cpp | 70 ++++++++ services/src/fileoper/external_storage_oper.h | 35 ++++ .../src/fileoper/external_storage_utils.cpp | 150 ++++++++++++++++++ .../src/fileoper/external_storage_utils.h | 37 +++++ services/src/fileoper/media_file_oper.cpp | 3 + services/src/fileoper/oper_factory.cpp | 4 +- 13 files changed, 520 insertions(+), 57 deletions(-) create mode 100644 services/src/fileoper/cmd_options.h create mode 100644 services/src/fileoper/external_storage_oper.cpp create mode 100644 services/src/fileoper/external_storage_oper.h create mode 100644 services/src/fileoper/external_storage_utils.cpp create mode 100644 services/src/fileoper/external_storage_utils.h diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp index d1e506ac..52b13922 100644 --- a/interfaces/kits/js/src/file_manager_napi.cpp +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -43,12 +43,6 @@ struct AsyncUriArg { ~AsyncUriArg() = default; }; -struct ListFileOptionArgs { - int64_t offset = 0; - int64_t count = 0; - bool hasOp = false; -}; - tuple FileManagerNapi::GetFmsClient() { if (fmsClient_ == nullptr) { @@ -185,42 +179,69 @@ napi_value FileManagerNapi::GetRoot(napi_env env, napi_callback_info info) } } -tuple, unique_ptr, unique_ptr, ListFileOptionArgs> GetListFileArg( +bool GetLstFileOption(const NVal &argv, CmdOptions &option) +{ + bool ret = false; + if (argv.HasProp("dev")) { + unique_ptr devName; + NVal dev(argv.GetProp("dev")); + if (dev.HasProp("name")) { + tie(ret, devName, ignore) = dev.GetProp("name").ToUTF8String(); + if (!ret) { + ERR_LOG("ListFileArgs LF_OPTION dev para fails"); + return false; + } + option.SetDevInfo(DevInfo(devName.get(), "")); + } + } + if (argv.HasProp("offset")) { + int64_t offset; + tie(ret, offset) = argv.GetProp("offset").ToInt64(); + if (!ret) { + ERR_LOG("ListFileArgs LF_OPTION offset para fails"); + return false; + } + option.SetOffset(offset); + } + if (argv.HasProp("count")) { + int64_t count; + tie(ret, count) = argv.GetProp("count").ToInt64(); + if (!ret) { + ERR_LOG("ListFileArgs LF_OPTION count para fails"); + return false; + } + option.setCount(count); + } + return true; +} + +tuple, unique_ptr, CmdOptions> GetListFileArg( napi_env env, NFuncArg &funcArg) { bool succ = false; - unique_ptr type; - ListFileOptionArgs option; - tie(succ, type, ignore) = NVal(env, funcArg[ListFileArgs::LF_TYPE]).ToUTF8String(); - if (!succ) { - ERR_LOG("ListFileArgs LF_TYPE para fails"); - return {false, nullptr, nullptr, nullptr, option}; - } unique_ptr path; + unique_ptr type; + CmdOptions option; tie(succ, path, ignore) = NVal(env, funcArg[ListFileArgs::LF_PATH]).ToUTF8String(); if (!succ) { ERR_LOG("ListFileArgs LF_PATH para fails"); - return {false, nullptr, nullptr, nullptr, option}; + return {false, nullptr, nullptr, option}; } + tie(succ, type, ignore) = NVal(env, funcArg[ListFileArgs::LF_TYPE]).ToUTF8String(); + if (!succ) { + ERR_LOG("ListFileArgs LF_TYPE para fails"); + return {false, nullptr, nullptr, option}; + } + NVal op(env, NVal(env, funcArg[ListFileArgs::LF_OPTION]).val_); - if (op.HasProp("offset")) { - tie(succ, option.offset) = op.GetProp("offset").ToInt64(); - if (!succ) { - ERR_LOG("ListFileArgs LF_OPTION offset para fails"); - return {false, nullptr, nullptr, nullptr, option}; - } - option.hasOp = true; + if (op.TypeIs(napi_function)) { + return {true, move(type), move(path), option}; } - if (op.HasProp("count")) { - tie(succ, option.count) = op.GetProp("count").ToInt64(); - if (!succ) { - ERR_LOG("ListFileArgs LF_OPTION count para fails"); - return {false, nullptr, nullptr, nullptr, option}; - } - option.hasOp = true; + option.SetHasOpt(true); + if (!GetLstFileOption(op, option)) { + return {false, nullptr, nullptr, option}; } - // currently devName is ignore - return {true, nullptr, move(type), move(path), option}; + return {true, move(type), move(path), option}; } napi_value FileManagerNapi::ListFile(napi_env env, napi_callback_info info) @@ -231,13 +252,14 @@ napi_value FileManagerNapi::ListFile(napi_env env, napi_callback_info info) return nullptr; } bool succ = false; - // first is devName for extention - // second is type unique_ptr type; unique_ptr path; - ListFileOptionArgs option; - // currently devName is ignore - tie(succ, ignore, type, path, option) = GetListFileArg(env, funcArg); + CmdOptions option; + tie(succ, type, path, option) = GetListFileArg(env, funcArg); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Get argments fails"); + return nullptr; + } napi_value fileArr; napi_create_array(env, &fileArr); auto arg = make_shared(NVal(env, fileArr)); @@ -250,7 +272,8 @@ napi_value FileManagerNapi::ListFile(napi_env env, napi_callback_info info) return UniError(ESRCH); } vector fileRes; - int err = client->ListFile(type, path, option.offset, option.count, fileRes); + int err = client->ListFile(type, path, option, fileRes); + arg->fileRes_ = fileRes; return DealWithErrno(err); }; @@ -266,10 +289,10 @@ napi_value FileManagerNapi::ListFile(napi_env env, napi_callback_info info) string procedureName = "ListFile"; int argc = funcArg.GetArgc(); NVal thisVar(env, funcArg.GetThisVar()); - if (argc == LIST_FILE_PARA_MIN || (argc != LIST_FILE_PARA_MAX && option.hasOp)) { + if (argc == LIST_FILE_PARA_MIN || (argc != LIST_FILE_PARA_MAX && option.GetHasOpt())) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } else { - int cbIdx = ((!option.hasOp) ? ListFileArgs::LF_CALLBACK_WITHOUT_OP : ListFileArgs::LF_CALLBACK_WITH_OP); + int cbIdx = ((!option.GetHasOpt()) ? ListFileArgs::LF_CALLBACK_WITHOUT_OP : ListFileArgs::LF_CALLBACK_WITH_OP); NVal cb(env, funcArg[cbIdx]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } diff --git a/interfaces/kits/js/src/file_manager_napi_def.h b/interfaces/kits/js/src/file_manager_napi_def.h index c1549c67..1d008cd9 100644 --- a/interfaces/kits/js/src/file_manager_napi_def.h +++ b/interfaces/kits/js/src/file_manager_napi_def.h @@ -34,20 +34,19 @@ enum GetRootArgs { }; enum ListFileArgs { - LF_DEV = 0, + LF_PATH = 0, LF_TYPE = 1, - LF_PATH = 2, - LF_OPTION = 3, - LF_CALLBACK_WITHOUT_OP = 3, - LF_CALLBACK_WITH_OP = 4, + LF_OPTION = 2, + LF_CALLBACK_WITHOUT_OP = 2, + LF_CALLBACK_WITH_OP = 3, }; constexpr int CREATE_FILE_PARA_MAX = 4; constexpr int CREATE_FILE_PARA_MIN = 3; constexpr int GET_ROOT_PARA_MAX = 2; constexpr int GET_ROOT_PARA_MIN = 1; -constexpr int LIST_FILE_PARA_MAX = 5; -constexpr int LIST_FILE_PARA_MIN = 3; +constexpr int LIST_FILE_PARA_MAX = 4; +constexpr int LIST_FILE_PARA_MIN = 2; } // namespace FileManagerService } // namespace OHOS #endif // STORAGE_FILE_MANAGER_NAPI_DEF_H diff --git a/services/BUILD.gn b/services/BUILD.gn index f265421e..329c443b 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -43,6 +43,8 @@ ohos_shared_library("fms_server") { sources = [ "src/client/file_manager_proxy.cpp", + "src/fileoper/external_storage_oper.cpp", + "src/fileoper/external_storage_utils.cpp", "src/fileoper/media_file_oper.cpp", "src/fileoper/media_file_utils.cpp", "src/fileoper/oper_factory.cpp", diff --git a/services/src/client/file_manager_proxy.cpp b/services/src/client/file_manager_proxy.cpp index 78621283..e56b4421 100644 --- a/services/src/client/file_manager_proxy.cpp +++ b/services/src/client/file_manager_proxy.cpp @@ -71,16 +71,29 @@ IFmsClient *IFmsClient::GetFmsInstance() return &proxy; } -int FileManagerProxy::ListFile(const string &type, const string &path, int off, int count, vector &fileRes) +int FileManagerProxy::ListFile(const std::string &type, const std::string &path, const CmdOptions &option, + 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(); + + data.WriteString(devName); + data.WriteString(devPath); data.WriteString(type); data.WriteString(path); - data.WriteInt32(off); + data.WriteInt32(offset); data.WriteInt32(count); MessageParcel reply; - MessageOption option; - int err = Remote()->SendRequest(Operation::LIST_FILE, data, reply, option); + MessageOption messageOption; + uint32_t code = Equipment::INTERNAL_STORAGE; + if (op.GetDevInfo().GetName() == "external_storage") { + code = (Equipment::EXTERNAL_STORAGE << EQUIPMENT_SHIFT) | Operation::LIST_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 2d93d10d..080fedbb 100644 --- a/services/src/client/file_manager_proxy.h +++ b/services/src/client/file_manager_proxy.h @@ -28,8 +28,8 @@ public: explicit FileManagerProxy(const sptr &impl); virtual ~FileManagerProxy() = default; int Mkdir(const std::string &name, const std::string &path) override; - int ListFile(const std::string &type, const std::string &path, - int off, int count, std::vector &fileRes) 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) const override; private: diff --git a/services/src/client/ifms_client.h b/services/src/client/ifms_client.h index 99b656e2..26088466 100644 --- a/services/src/client/ifms_client.h +++ b/services/src/client/ifms_client.h @@ -14,8 +14,8 @@ */ #ifndef STORAGE_IFILE_MANAGER_CLIENT_H #define STORAGE_IFILE_MANAGER_CLIENT_H +#include "cmd_options.h" #include "file_info.h" - namespace OHOS { namespace FileManagerService { class IFmsClient { @@ -23,8 +23,8 @@ public: virtual ~IFmsClient() {} 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, - int off, int count, std::vector &fileRes) = 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) const = 0; virtual int CreateFile(const std::string &name, const std::string &path, std::string &uri) = 0; }; diff --git a/services/src/fileoper/cmd_options.h b/services/src/fileoper/cmd_options.h new file mode 100644 index 00000000..4eae65c1 --- /dev/null +++ b/services/src/fileoper/cmd_options.h @@ -0,0 +1,131 @@ +/* + * 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_DEV_INFO_H +#define STORAGE_SERVICES_DEV_INFO_H + +#include + +namespace OHOS { +namespace FileManagerService { +class DevInfo { +public: + DevInfo() = default; + ~DevInfo() = default; + DevInfo(const std::string &nameIn, const std::string &pathIn) : name_(nameIn), path_(pathIn) + {} + + DevInfo(const DevInfo &dev) + { + this->name_ = dev.name_; + this->path_ = dev.path_; + } + + DevInfo& operator=(const DevInfo& dev) + { + this->name_ = dev.name_; + this->path_ = dev.path_; + return *this; + } + + std::string GetName() + { + return name_; + } + + void SetName(const std::string& name) + { + name_ = name; + } + + std::string GetPath() + { + return path_; + } + + void SetPath(const std::string& path) + { + path_ = path; + } + +private: + std::string name_ {""}; + std::string path_ {""}; +}; + +class CmdOptions { +public: + CmdOptions() = default; + ~CmdOptions() = default; + + CmdOptions(DevInfo devIn, int64_t offsetIn, int64_t countIn, bool hasOptIn) + : dev_(devIn), offset_(offsetIn), count_(countIn), hasOpt_(hasOptIn) + {} + CmdOptions(const std::string &nameIn, const std::string &pathIn, + int64_t offsetIn, int64_t countIn, bool hasOptIn) + : dev_(nameIn, pathIn), offset_(offsetIn), count_(countIn), hasOpt_(hasOptIn) + {} + + CmdOptions(const CmdOptions &option) = default; + CmdOptions& operator=(const CmdOptions& option) = default; + + DevInfo GetDevInfo() + { + return dev_; + } + + void SetDevInfo(const DevInfo& dev) + { + dev_ = dev; + } + + int64_t GetOffset() + { + return offset_; + } + + void SetOffset(int64_t offset) + { + offset_ = offset; + } + + int64_t GetCount() + { + return count_; + } + + void setCount(int64_t count) + { + count_ = count; + } + + bool GetHasOpt() + { + return hasOpt_; + } + + void SetHasOpt(bool hasOpt) + { + hasOpt_ = hasOpt; + } + +private: + DevInfo dev_; + int64_t offset_ {0}; + int64_t count_ {0}; + bool hasOpt_ {false}; +}; +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERVICES_DEV_INFO_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 new file mode 100644 index 00000000..4d970f67 --- /dev/null +++ b/services/src/fileoper/external_storage_oper.cpp @@ -0,0 +1,70 @@ +/* + * 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. + */ + +#include "external_storage_oper.h" + +#include + +#include "external_storage_utils.h" +#include "file_info.h" +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" +#include "log.h" + +namespace OHOS { +namespace FileManagerService { +int ExternalStorageOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) const +{ + DEBUG_LOG("ExternalStorageOper::OperProcess"); + int errCode = SUCCESS; + switch (code) { + case Operation::LIST_FILE: { + std::string devName = data.ReadString(); + std::string devPath = data.ReadString(); + std::string type = data.ReadString(); + std::string path = data.ReadString(); + int64_t offset = data.ReadInt64(); + int64_t count = data.ReadInt64(); + + CmdOptions option(devName, devPath, offset, count, true); + errCode = this->ListFile(type, path, option, reply); + break; + } + case Operation::CREATE_FILE: { + std::string name = data.ReadString(); + std::string uri = data.ReadString(); + errCode = this->CreateFile(uri, name, reply); + break; + } + default: { + DEBUG_LOG("not valid code %{public}d.", code); + break; + } + } + return errCode; +} + +int ExternalStorageOper::CreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) const +{ + return ExternalStorageUtils::DoCreateFile(uri, name, reply); +} + +int ExternalStorageOper::ListFile(const std::string &type, const std::string &uri, const CmdOptions &option, + MessageParcel &reply) const +{ + return ExternalStorageUtils::DoListFile(type, uri, reply); +} +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/external_storage_oper.h b/services/src/fileoper/external_storage_oper.h new file mode 100644 index 00000000..09502070 --- /dev/null +++ b/services/src/fileoper/external_storage_oper.h @@ -0,0 +1,35 @@ +/* + * 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_SERIVCES_EXTERNAL_STORAGE_OPER_H +#define STORAGE_SERIVCES_EXTERNAL_STORAGE_OPER_H + +#include +#include "cmd_options.h" +#include "file_oper.h" +namespace OHOS { +namespace FileManagerService { +class ExternalStorageOper : public FileOper { +public: + ExternalStorageOper() = default; + virtual ~ExternalStorageOper() = default; + int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) const override; +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; +}; +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERIVCES_EXTERNAL_STORAGE_OPER_H \ No newline at end of file diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp new file mode 100644 index 00000000..73e9a5ab --- /dev/null +++ b/services/src/fileoper/external_storage_utils.cpp @@ -0,0 +1,150 @@ +/* + * 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. + */ + +#include "external_storage_utils.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" +#include "log.h" + +namespace OHOS { +namespace FileManagerService { +static bool GetFileInfo(const std::string &path, const std::string &name, FileInfo &fileInfo) +{ + std::string fullPath(""); + fullPath.append(path).append("/").append(name); + struct stat st; + if (lstat(fullPath.c_str(), &st) != 0) { + ERR_LOG("check file info fail."); + return false; + } + std::string fPath(path.c_str()); + std::string fName(name.c_str()); + + fileInfo.SetPath(fPath); + std::string type = "file"; + fileInfo.SetType(type); + fileInfo.SetName(fName); + fileInfo.SetSize(st.st_size); + fileInfo.SetAddedTime(static_cast(st.st_ctim.tv_sec)); + fileInfo.SetModifiedTime(static_cast(st.st_mtim.tv_sec)); + return true; +} + +static bool ConvertUriToAbsolutePath(const std::string &uri, std::string &path) +{ + // TODO convert uri to absolute path + path = "/data/media"; + return true; +} + +int ExternalStorageUtils::DoListFile(const std::string &type, const std::string &uri, MessageParcel &reply) +{ + std::string path; + if (!ConvertUriToAbsolutePath(uri, path)) { + ERR_LOG("invalid uri[%{public}s].", uri.c_str()); + return E_NOEXIST; + } + + DIR *dir = opendir(path.c_str()); + if (!dir) { + ERR_LOG("opendir path[%{public}s] fail.", path.c_str()); + 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; + } + FileInfo fileInfo; + if (!GetFileInfo(path, ent->d_name, fileInfo)) { + continue; + } + fileList.push_back(fileInfo); + } + closedir(dir); + int fileCount = static_cast(fileList.size()); + reply.WriteInt32(fileCount); + if (fileCount == 0) { + return E_EMPTYFOLDER; + } + for (auto file : fileList) { + reply.WriteString(file.GetPath()); + reply.WriteString(file.GetType()); + reply.WriteString(file.GetName()); + reply.WriteInt64(file.GetSize()); + reply.WriteInt64(file.GetAddedTime()); + reply.WriteInt64(file.GetModifiedTime()); + } + return SUCCESS; +} + +int ExternalStorageUtils::DoCreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) +{ + std::string path; + if (!ConvertUriToAbsolutePath(uri, path)) { + ERR_LOG("invalid uri[%{public}s].", uri.c_str()); + return E_NOEXIST; + } + + path.append("/").append(name); + if (access(path.c_str(), F_OK) == 0) { + ERR_LOG("target file[%{public}s] exist.", path.c_str()); + return E_CREATE_FAIL; + } + + int fd = open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0771); + if (fd == -1) { + ERR_LOG("create file[%{public}s] fail.", path.c_str()); + return E_CREATE_FAIL; + } + close(fd); + + reply.WriteString(uri); + 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 new file mode 100644 index 00000000..7f4e7f35 --- /dev/null +++ b/services/src/fileoper/external_storage_utils.h @@ -0,0 +1,37 @@ +/* + * 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_SERIVCES_EXTERNAL_STORAGE_UTILS_H +#define STORAGE_SERIVCES_EXTERNAL_STORAGE_UTILS_H + +#include +#include + +#include "cmd_options.h" +#include "file_info.h" +#include "file_oper.h" + +namespace OHOS { +namespace FileManagerService { +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); +}; +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERIVCES_EXTERNAL_STORAGE_UTILS_H \ No newline at end of file diff --git a/services/src/fileoper/media_file_oper.cpp b/services/src/fileoper/media_file_oper.cpp index b858055b..17460246 100644 --- a/services/src/fileoper/media_file_oper.cpp +++ b/services/src/fileoper/media_file_oper.cpp @@ -17,6 +17,7 @@ #include +#include "cmd_options.h" #include "file_info.h" #include "file_manager_service_def.h" #include "file_manager_service_errno.h" @@ -44,6 +45,8 @@ int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel 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(); diff --git a/services/src/fileoper/oper_factory.cpp b/services/src/fileoper/oper_factory.cpp index 23f25ced..e0b8ea24 100644 --- a/services/src/fileoper/oper_factory.cpp +++ b/services/src/fileoper/oper_factory.cpp @@ -15,6 +15,7 @@ #include "oper_factory.h" +#include "external_storage_oper.h" #include "file_manager_service_def.h" #include "file_oper.h" #include "log.h" @@ -33,8 +34,7 @@ unique_ptr OperFactory::GetFileOper(int equipmentId) break; } case Equipment::EXTERNAL_STORAGE: { - DEBUG_LOG("FileOper exter %{public}d %{public}d.", Equipment::EXTERNAL_STORAGE, equipmentId); - // do Exteranl storage process; + fp = make_unique(); break; } default: { -- Gitee From 7b5669661c977f9f2a72846e21d73ab521fbb92f Mon Sep 17 00:00:00 2001 From: Dageking Date: Fri, 28 Jan 2022 19:22:22 +0800 Subject: [PATCH 2/3] add external storage code Signed-off-by: Dageking --- interfaces/kits/js/src/file_manager_napi.cpp | 4 ++-- services/src/client/file_manager_proxy.cpp | 2 +- services/src/fileoper/external_storage_oper.cpp | 2 +- services/src/fileoper/external_storage_utils.cpp | 1 - 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp index 52b13922..bde1d1ae 100644 --- a/interfaces/kits/js/src/file_manager_napi.cpp +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -305,11 +305,11 @@ napi_value FileManagerNapi::Mkdir(napi_env env, napi_callback_info info) bool FileManagerNapi::Export() { - return exports_.AddProp({ + return exports_.AddProp( { NVal::DeclareNapiFunction("listFile", ListFile), NVal::DeclareNapiFunction("createFile", CreateFile), NVal::DeclareNapiFunction("getRoot", GetRoot), - }); + } ); } string FileManagerNapi::GetClassName() diff --git a/services/src/client/file_manager_proxy.cpp b/services/src/client/file_manager_proxy.cpp index e56b4421..cd7a1145 100644 --- a/services/src/client/file_manager_proxy.cpp +++ b/services/src/client/file_manager_proxy.cpp @@ -72,7 +72,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); diff --git a/services/src/fileoper/external_storage_oper.cpp b/services/src/fileoper/external_storage_oper.cpp index 4d970f67..7d821893 100644 --- a/services/src/fileoper/external_storage_oper.cpp +++ b/services/src/fileoper/external_storage_oper.cpp @@ -62,7 +62,7 @@ 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 + MessageParcel &reply) const { return ExternalStorageUtils::DoListFile(type, uri, reply); } diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp index 73e9a5ab..8ce1b5b9 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -54,7 +54,6 @@ static bool GetFileInfo(const std::string &path, const std::string &name, FileIn static bool ConvertUriToAbsolutePath(const std::string &uri, std::string &path) { - // TODO convert uri to absolute path path = "/data/media"; return true; } -- Gitee From bfc483f0c4f1844190c8f0dbb2209f08bf7a6f2d Mon Sep 17 00:00:00 2001 From: Dageking Date: Fri, 28 Jan 2022 19:28:54 +0800 Subject: [PATCH 3/3] add external storage code Signed-off-by: Dageking --- interfaces/kits/js/src/file_manager_napi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp index bde1d1ae..5968fb1f 100644 --- a/interfaces/kits/js/src/file_manager_napi.cpp +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -309,7 +309,7 @@ bool FileManagerNapi::Export() NVal::DeclareNapiFunction("listFile", ListFile), NVal::DeclareNapiFunction("createFile", CreateFile), NVal::DeclareNapiFunction("getRoot", GetRoot), - } ); + }); } string FileManagerNapi::GetClassName() -- Gitee