From 47cebcd573bb8b31e25bab5e9c347ad07a8e3623 Mon Sep 17 00:00:00 2001 From: Dageking Date: Thu, 27 Jan 2022 21:05:54 +0800 Subject: [PATCH] add external storage code Signed-off-by: Dageking --- interfaces/kits/js/src/file_manager_napi.cpp | 53 ++++-- .../kits/js/src/file_manager_napi_def.h | 13 +- services/BUILD.gn | 3 + services/src/client/file_manager_proxy.cpp | 10 +- services/src/client/file_manager_proxy.h | 2 +- services/src/client/ifms_client.h | 3 +- services/src/fileoper/dev_info.h | 28 ++++ .../src/fileoper/external_storage_oper.cpp | 71 ++++++++ services/src/fileoper/external_storage_oper.h | 37 +++++ .../src/fileoper/external_storage_utils.cpp | 156 ++++++++++++++++++ .../src/fileoper/external_storage_utils.h | 39 +++++ services/src/fileoper/media_file_oper.cpp | 9 +- services/src/fileoper/media_file_oper.h | 3 +- services/src/fileoper/media_file_utils.cpp | 23 +-- services/src/fileoper/media_file_utils.h | 3 +- services/src/fileoper/oper_factory.cpp | 4 +- services/src/fileoper/utils.cpp | 46 ++++++ services/src/fileoper/utils.h | 26 +++ 18 files changed, 475 insertions(+), 54 deletions(-) create mode 100644 services/src/fileoper/dev_info.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 create mode 100644 services/src/fileoper/utils.cpp create mode 100644 services/src/fileoper/utils.h diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp index d1e506ac..d5029b4d 100644 --- a/interfaces/kits/js/src/file_manager_napi.cpp +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -189,27 +189,41 @@ tuple, unique_ptr, unique_ptr, ListFile napi_env env, NFuncArg &funcArg) { bool succ = false; + unique_ptr path; unique_ptr type; ListFileOptionArgs option; - tie(succ, type, ignore) = NVal(env, funcArg[ListFileArgs::LF_TYPE]).ToUTF8String(); + unique_ptr devName = nullptr; + tie(succ, path, ignore) = NVal(env, funcArg[ListFileArgs::LF_PATH]).ToUTF8String(); if (!succ) { - ERR_LOG("ListFileArgs LF_TYPE para fails"); + ERR_LOG("ListFileArgs LF_PATH para fails"); return {false, nullptr, nullptr, nullptr, option}; } - unique_ptr path; - tie(succ, path, ignore) = NVal(env, funcArg[ListFileArgs::LF_PATH]).ToUTF8String(); + tie(succ, type, ignore) = NVal(env, funcArg[ListFileArgs::LF_TYPE]).ToUTF8String(); if (!succ) { - ERR_LOG("ListFileArgs LF_PATH para fails"); + ERR_LOG("ListFileArgs LF_TYPE para fails"); return {false, nullptr, nullptr, nullptr, option}; } NVal op(env, NVal(env, funcArg[ListFileArgs::LF_OPTION]).val_); + if (op.TypeIs(napi_function)) { + return {true, nullptr, move(type), move(path), option}; + } + option.hasOp = true; + if (op.HasProp("dev")) { + NVal dev(op.GetProp("dev")); + if (dev.HasProp("name")) { + tie(succ, devName, ignore) = dev.GetProp("name").ToUTF8String(); + if (!succ) { + ERR_LOG("ListFileArgs LF_OPTION dev para fails"); + return {false, nullptr, nullptr, nullptr, option}; + } + } + } 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.HasProp("count")) { tie(succ, option.count) = op.GetProp("count").ToInt64(); @@ -217,10 +231,11 @@ tuple, unique_ptr, unique_ptr, ListFile ERR_LOG("ListFileArgs LF_OPTION count para fails"); return {false, nullptr, nullptr, nullptr, option}; } - option.hasOp = true; } - // currently devName is ignore - return {true, nullptr, move(type), move(path), option}; + if (devName == nullptr) { + return {true, nullptr, move(type), move(path), option}; + } + return {true, move(devName), move(type), move(path), option}; } napi_value FileManagerNapi::ListFile(napi_env env, napi_callback_info info) @@ -231,17 +246,23 @@ 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); + unique_ptr devName; + tie(succ, devName, type, path, option) = GetListFileArg(env, funcArg); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Get argments fails"); + return nullptr; + } + if (devName == nullptr) { + devName = make_unique(1); + devName[0] = '\0'; + } napi_value fileArr; napi_create_array(env, &fileArr); auto arg = make_shared(NVal(env, fileArr)); - auto cbExec = [type = string(type.get()), path = string(path.get()), option, arg] + auto cbExec = [devName = string(devName.get()), type = string(type.get()), path = string(path.get()), option, arg] (napi_env env) -> UniError { IFmsClient* client = nullptr; bool succ = false; @@ -249,8 +270,10 @@ napi_value FileManagerNapi::ListFile(napi_env env, napi_callback_info info) if (!succ) { return UniError(ESRCH); } + DevInfo dev; + dev.name = devName; vector fileRes; - int err = client->ListFile(type, path, option.offset, option.count, fileRes); + int err = client->ListFile(dev, type, path, option.offset, option.count, fileRes); arg->fileRes_ = fileRes; return DealWithErrno(err); }; 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..36649298 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -43,6 +43,9 @@ 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/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..4e82d2cb 100644 --- a/services/src/client/file_manager_proxy.cpp +++ b/services/src/client/file_manager_proxy.cpp @@ -71,16 +71,22 @@ IFmsClient *IFmsClient::GetFmsInstance() return &proxy; } -int FileManagerProxy::ListFile(const string &type, const string &path, int off, int count, vector &fileRes) +int FileManagerProxy::ListFile(const DevInfo &dev, const string &type, const string &path, int off, int count, vector &fileRes) { MessageParcel data; + data.WriteString(dev.name); + data.WriteString(dev.path); data.WriteString(type); data.WriteString(path); data.WriteInt32(off); data.WriteInt32(count); MessageParcel reply; MessageOption option; - int err = Remote()->SendRequest(Operation::LIST_FILE, data, reply, option); + uint32_t code = Equipment::INTERNAL_STORAGE; + if (dev.name == "external_storage") { + code = (Equipment::EXTERNAL_STORAGE << 16) | Operation::LIST_FILE; + } + int err = Remote()->SendRequest(code, data, reply, option); 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..186c8919 100644 --- a/services/src/client/file_manager_proxy.h +++ b/services/src/client/file_manager_proxy.h @@ -28,7 +28,7 @@ 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 ListFile(const DevInfo &dev, const std::string &type, const std::string &path, int off, int count, 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; diff --git a/services/src/client/ifms_client.h b/services/src/client/ifms_client.h index 99b656e2..440240c8 100644 --- a/services/src/client/ifms_client.h +++ b/services/src/client/ifms_client.h @@ -14,6 +14,7 @@ */ #ifndef STORAGE_IFILE_MANAGER_CLIENT_H #define STORAGE_IFILE_MANAGER_CLIENT_H +#include "dev_info.h" #include "file_info.h" namespace OHOS { @@ -23,7 +24,7 @@ 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, + virtual int ListFile(const DevInfo &dev, const std::string &type, const std::string &path, int off, int count, 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/dev_info.h b/services/src/fileoper/dev_info.h new file mode 100644 index 00000000..4983f289 --- /dev/null +++ b/services/src/fileoper/dev_info.h @@ -0,0 +1,28 @@ +/* + * 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 { +struct DevInfo { + std::string name; + std::string path; +}; +} // 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..1142e999 --- /dev/null +++ b/services/src/fileoper/external_storage_oper.cpp @@ -0,0 +1,71 @@ +/* + * 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: { + DevInfo dev; + dev.name = data.ReadString(); + dev.path = data.ReadString(); + std::string type = data.ReadString(); + std::string path = data.ReadString(); + int64_t offset = data.ReadInt64(); + int64_t count = data.ReadInt64(); + errCode = this->ListFile(dev, type, path, offset, count, 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 +{ + DEBUG_LOG("ExternalStorageOper::CreateFile"); + return ExternalStorageUtils::DoCreateFile(uri, name, reply); +} + +int ExternalStorageOper::ListFile(const DevInfo &dev, + const std::string &type, const std::string &uri, int64_t offset, int64_t count, MessageParcel &reply) const +{ + DEBUG_LOG("ExternalStorageOper::ListFile"); + return ExternalStorageUtils::DoListFile(dev, type, uri, offset, count, 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..97817c51 --- /dev/null +++ b/services/src/fileoper/external_storage_oper.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_OPER_H +#define STORAGE_SERIVCES_EXTERNAL_STORAGE_OPER_H + +#include + +#include "dev_info.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 DevInfo &dev, const std::string &type, + const std::string &uri, int64_t offset, int64_t count, 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..5d82ff10 --- /dev/null +++ b/services/src/fileoper/external_storage_utils.cpp @@ -0,0 +1,156 @@ +/* + * 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" +#include "utils.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); + 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)); + std::string type = GetMimeType(fName); + fileInfo.SetType(type); + 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 DevInfo &dev, const std::string &type, + const std::string &uri, int64_t offset, int64_t count, MessageParcel &reply) +{ + DEBUG_LOG("ExternalStorageUtils::DoListFile"); + + 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); + int64_t fileCount = fileList.size(); + reply.WriteInt64(fileCount); + if (count == 0) { + return E_EMPTYFOLDER; + } + for (auto file : fileList) { + reply.WriteString(file.GetPath()); + reply.WriteString(file.GetName()); + reply.WriteString(file.GetType()); + 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) +{ + DEBUG_LOG("ExternalStorageUtils::DoCreateFile"); + + 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 name; + std::string type; + int64_t size = 0; + int64_t at = 0; + int64_t mt = 0; + + reply.ReadString(path); + reply.ReadString(name); + reply.ReadString(type); + 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..de2e128e --- /dev/null +++ b/services/src/fileoper/external_storage_utils.h @@ -0,0 +1,39 @@ +/* + * 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 "dev_info.h" +#include "file_info.h" +#include "file_oper.h" + +namespace OHOS { +namespace FileManagerService { +class ExternalStorageUtils { +public: + ExternalStorageUtils(); + ~ExternalStorageUtils(); + static int DoListFile(const DevInfo &dev, const std::string &type, + const std::string &uri, int64_t off, int64_t count, MessageParcel &reply); + static int DoCreateFile(const std::string &uri, const std::string &name, MessageParcel &reply); + static bool PushFileInfo(std::vector &fileList, 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..b37b8f7d 100644 --- a/services/src/fileoper/media_file_oper.cpp +++ b/services/src/fileoper/media_file_oper.cpp @@ -44,12 +44,15 @@ int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel break; } case Operation::LIST_FILE: { + DevInfo dev; + dev.name = data.ReadString(); + dev.path = data.ReadString(); string type = data.ReadString(); string path = data.ReadString(); int off = data.ReadInt32(); int count = data.ReadInt32(); // put fileInfo into reply - errCode = ListFile(type, path, off, count, reply); + errCode = ListFile(dev, type, path, off, count, reply); break; } case Operation::CREATE_FILE: { @@ -74,10 +77,10 @@ int MediaFileOper::CreateFile(const std::string &name, const std::string &path, return MediaFileUtils::DoInsert(name, path, type, uri); } -int MediaFileOper::ListFile(const string type, const string &path, int offset, int count, MessageParcel &reply) const +int MediaFileOper::ListFile(const DevInfo &dev, 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); + int res = MediaFileUtils::DoListFile(dev, type, path, offset, count, result); return MediaFileUtils::GetFileInfoFromResult(result, reply, res); } diff --git a/services/src/fileoper/media_file_oper.h b/services/src/fileoper/media_file_oper.h index dfb5ecad..6bf52c34 100644 --- a/services/src/fileoper/media_file_oper.h +++ b/services/src/fileoper/media_file_oper.h @@ -16,6 +16,7 @@ #define STORAGE_SERVICES_MEDIA_FILE_OPER_H #include +#include "dev_info.h" #include "file_oper.h" namespace OHOS { namespace FileManagerService { @@ -26,7 +27,7 @@ public: 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 ListFile(const std::string type, const std::string &path, int offset, int count, MessageParcel &data) const; + int ListFile(const DevInfo &dev, 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; }; } // namespace FileManagerService diff --git a/services/src/fileoper/media_file_utils.cpp b/services/src/fileoper/media_file_utils.cpp index 600ee20c..7b2b345d 100644 --- a/services/src/fileoper/media_file_utils.cpp +++ b/services/src/fileoper/media_file_utils.cpp @@ -24,6 +24,7 @@ #include "media_data_ability_const.h" #include "medialibrary_data_ability.h" #include "log.h" +#include "utils.h" using namespace std; namespace OHOS { @@ -83,26 +84,6 @@ bool GetPathID(const string &uriPath, string &index) return true; } -int GetMediaType(const string &name) -{ - int mediaType = Media::MediaAsset::GetMediaType(name); - if (FILE_MIME_TYPE_MAPS.count(mediaType) == 0) { - ERR_LOG("invalid mediaType %{public}d", mediaType); - return FILE_MEDIA_TYPE; - } - return mediaType; -} - -string GetMimeType(const string &name) -{ - int mediaType = GetMediaType(name); - if (FILE_MIME_TYPE_MAPS.count(mediaType) == 0) { - ERR_LOG("invalid mediaType %{public}d", mediaType); - return FILE_MIME_TYPE; - } - return FILE_MIME_TYPE_MAPS.at(mediaType); -} - bool GetPathFromAlbumPath(const string &albumUri, string &path) { string id; @@ -259,7 +240,7 @@ bool GetAlbumPath(const string &name, const string &path, string &albumPath) return GetPathFromAlbumPath(path, albumPath); } -int MediaFileUtils::DoListFile(const string type, const string &path, int offset, int count, +int MediaFileUtils::DoListFile(const DevInfo &dev, const string type, const string &path, int offset, int count, shared_ptr &result) { string selection; diff --git a/services/src/fileoper/media_file_utils.h b/services/src/fileoper/media_file_utils.h index 265189b0..803588ed 100644 --- a/services/src/fileoper/media_file_utils.h +++ b/services/src/fileoper/media_file_utils.h @@ -19,6 +19,7 @@ #include #include "abs_shared_result_set.h" +#include "dev_info.h" #include "file_info.h" #include "file_oper.h" @@ -28,7 +29,7 @@ class MediaFileUtils { public: MediaFileUtils(); ~MediaFileUtils(); - static int DoListFile(const std::string type, const std::string &path, int offset, int count, + static int DoListFile(const DevInfo &dev, const std::string type, const std::string &path, int offset, int count, std::shared_ptr &result); static std::shared_ptr DoQuery(std::string selection, std::vector selectionArgs); 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: { diff --git a/services/src/fileoper/utils.cpp b/services/src/fileoper/utils.cpp new file mode 100644 index 00000000..8dfe0a9e --- /dev/null +++ b/services/src/fileoper/utils.cpp @@ -0,0 +1,46 @@ +/* + * 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 "utils.h" + +#include + +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" +#include "log.h" +#include "media_asset.h" + +namespace OHOS { +namespace FileManagerService { +int GetMediaType(const std::string &name) +{ + int mediaType = Media::MediaAsset::GetMediaType(name); + if (FILE_MIME_TYPE_MAPS.count(mediaType) == 0) { + ERR_LOG("invalid mediaType %{public}d", mediaType); + return FILE_MEDIA_TYPE; + } + return mediaType; +} + +std::string GetMimeType(const std::string &name) +{ + int mediaType = GetMediaType(name); + if (FILE_MIME_TYPE_MAPS.count(mediaType) == 0) { + ERR_LOG("invalid mediaType %{public}d", mediaType); + return FILE_MIME_TYPE; + } + return FILE_MIME_TYPE_MAPS.at(mediaType); +} +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/utils.h b/services/src/fileoper/utils.h new file mode 100644 index 00000000..8ca0b13b --- /dev/null +++ b/services/src/fileoper/utils.h @@ -0,0 +1,26 @@ +/* + * 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_FILE_OPER_UTILS_H +#define STORAGE_SERIVCES_FILE_OPER_UTILS_H + +#include + +namespace OHOS { +namespace FileManagerService { +int GetMediaType(const std::string &name); +std::string GetMimeType(const std::string &name); +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERIVCES_FILE_OPER_UTILS_H \ No newline at end of file -- Gitee