diff --git a/services/test/BUILD.gn b/interfaces/kits/js/BUILD.gn similarity index 47% rename from services/test/BUILD.gn rename to interfaces/kits/js/BUILD.gn index ae241dfe151f551fa3532632509fce7fe895f176..ef2e1f9e8f2ff0ef2493ec878028419255e9e3ea 100644 --- a/services/test/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -11,32 +11,44 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("//build/ohos.gni") +FMS_BASE_DIR = "//foundation/filemanagement/user_file_service/services" -import("//build/test.gni") -FMS_BASE_DIR = "//foundation/storage/services" -ohos_unittest("fms_test") { - module_out_path = "storage/fms" +ohos_shared_library("filemanager") { + subsystem_name = "filemanagement" + part_name = "file_manager_service" - sources = [ "fms_test.cpp" ] + relative_install_dir = "module" include_dirs = [ - "//third_party/json/include", - "//base/security/huks/interfaces/innerkits/huks_standard/main/include", + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "//third_party/libuv/include", "$FMS_BASE_DIR/include", "$FMS_BASE_DIR/src/client", "$FMS_BASE_DIR/src/server", + "$FMS_BASE_DIR/src/fileoper", + "//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", ] - configs = [ - "//build/config/compiler:exceptions", + sources = [ + "src/file_manager_napi.cpp", + "src/module.cpp", ] deps = [ - "//foundation/storage/services:fms_server", - "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "$FMS_BASE_DIR:fms_server", + "//foundation/ace/napi:ace_napi", + "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js:build_kits_js", "//utils/native/base:utils", ] - + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } external_deps = [ "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", @@ -44,9 +56,3 @@ ohos_unittest("fms_test") { "samgr_standard:samgr_proxy", ] } - -group("test") { - testonly = true - - deps = [ ":fms_test" ] -} diff --git a/interfaces/kits/js/src/file_manager_napi.cpp b/interfaces/kits/js/src/file_manager_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1e506ac89c48b31145c5aa7d71d9d22b2d6c458 --- /dev/null +++ b/interfaces/kits/js/src/file_manager_napi.cpp @@ -0,0 +1,301 @@ +/* + * 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 "file_manager_napi.h" + +#include +#include +#include +#include + +#include "file_manager_napi_def.h" +#include "file_manager_proxy.h" +#include "file_manager_service_errno.h" +#include "ifms_client.h" +#include "log.h" +namespace OHOS { +namespace FileManagerService { +using namespace std; +using namespace DistributedFS; +struct AsyncFileInfoArg { + NRef ref_; + vector fileRes_; + explicit AsyncFileInfoArg(NVal ref) : ref_(ref), fileRes_() {}; + ~AsyncFileInfoArg() = default; +}; + +struct AsyncUriArg { + NRef ref_; + string uri_; + explicit AsyncUriArg(NVal ref) : ref_(ref), uri_() {}; + ~AsyncUriArg() = default; +}; + +struct ListFileOptionArgs { + int64_t offset = 0; + int64_t count = 0; + bool hasOp = false; +}; + +tuple FileManagerNapi::GetFmsClient() +{ + if (fmsClient_ == nullptr) { + fmsClient_ = IFmsClient::GetFmsInstance(); + } + if (fmsClient_ == nullptr) { + ERR_LOG("fms get instance fails"); + return make_tuple(false, nullptr); + } else { + return make_tuple(true, fmsClient_); + } +} + +UniError DealWithErrno(int err) +{ + unordered_map errMap = { + {FAIL, ESRCH}, + {E_CREATE_FAIL, EPERM}, + {E_NOEXIST, ENOENT}, + {E_EMPTYFOLDER, ENOTDIR}, + {SUCCESS, ERRNO_NOERR}, + }; + if (errMap.count(err) == 0) { + ERR_LOG("unhandler err number %{public}d", err); + return UniError(FAIL); + } else { + return UniError(errMap[err]); + } +} + +napi_value FileManagerNapi::CreateFile(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(CREATE_FILE_PARA_MIN, CREATE_FILE_PARA_MAX)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + 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(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + 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 { + IFmsClient* client = nullptr; + bool succ = false; + tie(succ, client) = GetFmsClient(); + if (!succ) { + return UniError(ESRCH); + } + string uri = ""; + int err = client->CreateFile(name, path, arg->uri_); + return DealWithErrno(err); + }; + auto cbComplete = [arg](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return NVal::CreateUTF8String(env, arg->uri_); + } + }; + string procedureName = "CreateFile"; + int argc = funcArg.GetArgc(); + NVal thisVar(env, funcArg.GetThisVar()); + if (argc == CREATE_FILE_PARA_MIN) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[CreateFileArgs::CF_CALLBACK]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } +} + +void CreateFileArray(napi_env env, shared_ptr arg) +{ + for (unsigned int i = 0; i < arg->fileRes_.size(); i++) { + NVal obj = NVal::CreateObject(env); + FileInfo res = arg->fileRes_[i]; + 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_); + obj.AddProp("size", NVal::CreateInt64(env, res.GetSize()).val_); + obj.AddProp("added_time", NVal::CreateInt64(env, res.GetAddedTime()).val_); + 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_); + } +} + +napi_value FileManagerNapi::GetRoot(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(GET_ROOT_PARA_MIN, GET_ROOT_PARA_MAX)) { + UniError(EINVAL).ThrowErr(env, "Number of argments unmatched"); + return nullptr; + } + napi_value fileArr; + napi_create_array(env, &fileArr); + auto arg = make_shared(NVal(env, fileArr)); + auto cbExec = [arg] (napi_env env) -> UniError { + IFmsClient* client = nullptr; + bool succ = false; + tie(succ, client) = GetFmsClient(); + if (!succ) { + return UniError(ESRCH); + } + vector fileRes; + int err = client->GetRoot("local", fileRes); + arg->fileRes_ = fileRes; + return DealWithErrno(err); + }; + auto cbComplete = [arg](napi_env env, UniError err) -> NVal { + CreateFileArray(env, arg); + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return NVal(env, arg->ref_.Deref(env).val_); + } + }; + string procedureName = "GetRoot"; + int argc = funcArg.GetArgc(); + NVal thisVar(env, funcArg.GetThisVar()); + if (argc == GET_ROOT_PARA_MIN) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[GetRootArgs::GR_CALLBACK]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } +} + +tuple, unique_ptr, unique_ptr, ListFileOptionArgs> 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; + 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}; + } + 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.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; + } + // currently devName is ignore + return {true, nullptr, move(type), move(path), option}; +} + +napi_value FileManagerNapi::ListFile(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(LIST_FILE_PARA_MIN, LIST_FILE_PARA_MAX)) { + UniError(EINVAL).ThrowErr(env, "Number of argments unmatched"); + 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); + 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] + (napi_env env) -> UniError { + IFmsClient* client = nullptr; + bool succ = false; + tie(succ, client) = GetFmsClient(); + if (!succ) { + return UniError(ESRCH); + } + vector fileRes; + int err = client->ListFile(type, path, option.offset, option.count, fileRes); + arg->fileRes_ = fileRes; + return DealWithErrno(err); + }; + + auto cbComplete = [arg](napi_env env, UniError err) -> NVal { + CreateFileArray(env, arg); + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return NVal(env, arg->ref_.Deref(env).val_); + } + }; + 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)) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + int cbIdx = ((!option.hasOp) ? 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_; + } +} + +napi_value FileManagerNapi::Mkdir(napi_env env, napi_callback_info info) +{ + return NVal::CreateUndefined(env).val_; +} + +bool FileManagerNapi::Export() +{ + return exports_.AddProp({ + NVal::DeclareNapiFunction("listFile", ListFile), + NVal::DeclareNapiFunction("createFile", CreateFile), + NVal::DeclareNapiFunction("getRoot", GetRoot), + }); +} + +string FileManagerNapi::GetClassName() +{ + return FileManagerNapi::className_; +} + +FileManagerNapi::FileManagerNapi(napi_env env, napi_value exports) : NExporter(env, exports) {} + +FileManagerNapi::~FileManagerNapi() {} +} // namespace FileManagerService +} // namespace OHOS diff --git a/interfaces/kits/js/src/file_manager_napi.h b/interfaces/kits/js/src/file_manager_napi.h new file mode 100644 index 0000000000000000000000000000000000000000..8b06b57af5786ed8d4cf9c04eab819839b870ec9 --- /dev/null +++ b/interfaces/kits/js/src/file_manager_napi.h @@ -0,0 +1,40 @@ +/* + * 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_FILE_MANAGER_NAPI_H +#define STORAGE_FILE_MANAGER_NAPI_H + +#include "ifms_client.h" +#include "n_exporter.h" + +namespace OHOS { +namespace FileManagerService { +class FileManagerNapi final : public DistributedFS::NExporter { +public: + static napi_value CreateFile(napi_env env, napi_callback_info info); + static napi_value ListFile(napi_env env, napi_callback_info info); + static napi_value Mkdir(napi_env env, napi_callback_info info); + static napi_value GetRoot(napi_env env, napi_callback_info info); + bool Export() override; + std::string GetClassName() override; + static std::tuple GetFmsClient(); + FileManagerNapi(napi_env env, napi_value exports); + ~FileManagerNapi() override; +private: + inline static const std::string className_ = "__properities__"; + inline static IFmsClient* fmsClient_ = nullptr; +}; +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_FILE_MANAGER_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/file_manager_napi_def.h b/interfaces/kits/js/src/file_manager_napi_def.h new file mode 100644 index 0000000000000000000000000000000000000000..c1549c67910c9e340e4aa3de3720e0a99ae233c1 --- /dev/null +++ b/interfaces/kits/js/src/file_manager_napi_def.h @@ -0,0 +1,53 @@ +/* + * 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_FILE_MANAGER_NAPI_DEF_H +#define STORAGE_FILE_MANAGER_NAPI_DEF_H +#include "n_async_work_callback.h" +#include "n_async_work_promise.h" +#include "n_func_arg.h" +#include "uni_error.h" + +namespace OHOS { +namespace FileManagerService { +enum CreateFileArgs { + CF_DEV = 0, + CF_FILENAME = 1, + CF_PATH = 2, + CF_CALLBACK = 3, +}; + +enum GetRootArgs { + GR_DEV = 0, + GR_CALLBACK = 1, +}; + +enum ListFileArgs { + LF_DEV = 0, + LF_TYPE = 1, + LF_PATH = 2, + LF_OPTION = 3, + LF_CALLBACK_WITHOUT_OP = 3, + LF_CALLBACK_WITH_OP = 4, +}; + +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; +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_FILE_MANAGER_NAPI_DEF_H diff --git a/interfaces/kits/js/src/module.cpp b/interfaces/kits/js/src/module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c5b7a2c310e6e20d3bf722a7f493d398845c982 --- /dev/null +++ b/interfaces/kits/js/src/module.cpp @@ -0,0 +1,44 @@ +/* + * 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 +#include + +#include "file_manager_napi.h" +#include "log.h" + +using namespace std; + +namespace OHOS { +namespace FileManagerService { +using namespace DistributedFS; +static napi_value Export(napi_env env, napi_value exports) +{ + std::vector> products; + products.emplace_back(make_unique(env, exports)); + + for (auto &&product : products) { + if (!product->Export()) { + ERR_LOG("INNER BUG. Failed to export class %{public}s for module filemanager", + product->GetClassName().c_str()); + return nullptr; + } + } + return exports; +} + +NAPI_MODULE(filemanager, Export) +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/ohos.build b/ohos.build index f25cf252c1078f1b37ac528b2053b9019cdd044a..0fb5d5ffbeba8d3de6e58151efc88015d1ab68aa 100644 --- a/ohos.build +++ b/ohos.build @@ -1,11 +1,11 @@ { "subsystem": "filemanagement", "parts": { - "filemanager": { + "file_manager_service": { "module_list": [ - "//foundation/storage/services:fms", - "//foundation/storage/sa_profile:filemanager_service_sa_profile", - "//foundation/storage/interfaces/kits/js:filemanager" + "//foundation/filemanagement/user_file_service/services:fms", + "//foundation/filemanagement/user_file_service/sa_profile:filemanager_service_sa_profile", + "//foundation/filemanagement/user_file_service/interfaces/kits/js:filemanager" ] } } diff --git a/sa_profile/4933.xml b/sa_profile/5010.xml similarity index 97% rename from sa_profile/4933.xml rename to sa_profile/5010.xml index 216eb6a8d7487075d7f01989d6c4040e3b6366f5..96b4b077a81c497777fd43b45b984474d5598b0b 100644 --- a/sa_profile/4933.xml +++ b/sa_profile/5010.xml @@ -19,7 +19,7 @@ /system/lib/libfms_server.z.so - 4933 + 5010 /system/lib/libfms_server.z.so true false diff --git a/sa_profile/BUILD.gn b/sa_profile/BUILD.gn index 11b34990c58d10de761f9aeb2d62dd7bbae464f8..f9e13690a77b425040c5c146f02dabdbf2e4fad0 100644 --- a/sa_profile/BUILD.gn +++ b/sa_profile/BUILD.gn @@ -14,7 +14,7 @@ import("//build/ohos/sa_profile/sa_profile.gni") ohos_sa_profile("filemanager_service_sa_profile") { - sources = [ "4933.xml" ] + sources = [ "5010.xml" ] - part_name = "filemanager" + part_name = "file_manager_service" } diff --git a/services/BUILD.gn b/services/BUILD.gn index 746be0d69ee00fdd80dc8ea62d91f2d78840692e..5703a5c5fd40dbdf7b9514ccb3b3770d736f1163 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -12,30 +12,25 @@ # limitations under the License. import("//build/ohos.gni") -FMS_BASE_DIR = "//foundation/storage/services" +FMS_BASE_DIR = "//foundation/filemanagement/user_file_service/services" group("fms") { deps = [ - ":fms_exe", ":fms_server", - ":fms_service.rc", + ":fms_service.cfg", ] } -ohos_prebuilt_etc("fms_service.rc") { - if (use_musl) { - source = "etc/fms_service.cfg" - } else { - source = "etc/fms_service.rc" - } +ohos_prebuilt_etc("fms_service.cfg") { + source = "etc/fms_service.cfg" relative_install_dir = "init" - subsystem_name = "storage" - part_name = "filemanager" + subsystem_name = "filemanagement" + part_name = "file_manager_service" } ohos_shared_library("fms_server") { - subsystem_name = "storage" - part_name = "filemanager" + subsystem_name = "filemanagement" + part_name = "file_manager_service" include_dirs = [ "$FMS_BASE_DIR/include", @@ -49,6 +44,7 @@ ohos_shared_library("fms_server") { sources = [ "src/client/file_manager_proxy.cpp", "src/fileoper/media_file_oper.cpp", + "src/fileoper/media_file_utils.cpp", "src/fileoper/oper_factory.cpp", "src/server/file_manager_service.cpp", "src/server/file_manager_service_stub.cpp", @@ -60,6 +56,7 @@ ohos_shared_library("fms_server") { "//foundation/aafwk/standard/interfaces/innerkits/base:base", "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager:dataobs_manager", "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/media_library:media_library", "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/medialibrary_data_ability:medialibrary_data_ability", "//utils/native/base:utils", ] @@ -78,39 +75,3 @@ ohos_shared_library("fms_server") { "samgr_standard:samgr_proxy", ] } - -ohos_executable("fms_exe") { - install_enable = true - - sources = [ "exe/main.cpp" ] - - include_dirs = [ - "$FMS_BASE_DIR/include", - "$FMS_BASE_DIR/src/client", - "$FMS_BASE_DIR/src/server", - "$FMS_BASE_DIR/src/fileoper", - ] - - deps = [ - ":fms_server", - "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", - "//utils/native/base:utils", - ] - - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] - - subsystem_name = "storage" - part_name = "filemanager" -} - -# Unittest -group("test") { - testonly = true - - deps = [ "test:test" ] -} diff --git a/services/include/file_manager_service_def.h b/services/include/file_manager_service_def.h new file mode 100644 index 0000000000000000000000000000000000000000..6efd8096aa36d8edc97b1661cec2427cd42e9074 --- /dev/null +++ b/services/include/file_manager_service_def.h @@ -0,0 +1,77 @@ +/* + * 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_FILE_MANAGER_SERVICE_DEF_H +#define STORAGE_FILE_MANAGER_SERVICE_DEF_H + +#include +#include +#include +#include "media_data_ability_const.h" +#include "media_lib_service_const.h" + +namespace OHOS { +namespace FileManagerService { +enum Operation { + GET_ROOT, + MAKE_DIR, + LIST_FILE, + CREATE_FILE +}; + +enum Equipment { + INTERNAL_STORAGE, + EXTERNAL_STORAGE +}; + +constexpr int32_t CODE_MASK = 0xff; +constexpr int32_t EQUIPMENT_SHIFT = 16; + +const std::string FISRT_LEVEL_ALBUM = "dataability:///album"; +const std::string ROOT_PATH = "/data/media"; +const std::string IMAGE_ROOT_NAME = "image_album"; +const std::string VIDEO_ROOT_NAME = "video_album"; +const std::string AUDIO_ROOT_NAME = "audio_album"; +const std::string FILE_ROOT_NAME = "file_folder"; + +const std::string ALBUM_TYPE = "album"; +const std::string FILE_MIME_TYPE = "file/*"; +constexpr int FILE_MEDIA_TYPE = Media::MediaType::MEDIA_TYPE_FILE; +constexpr int RESULTSET_EMPTY = 0; +constexpr int RESULTSET_ONE = 1; + +const std::unordered_map FILE_MIME_TYPE_MAPS = { + {Media::MediaType::MEDIA_TYPE_IMAGE, "image/*"}, + {Media::MediaType::MEDIA_TYPE_AUDIO, "audio/*"}, + {Media::MediaType::MEDIA_TYPE_VIDEO, "video/*"}, + {Media::MediaType::MEDIA_TYPE_FILE, "file/*"}, + {Media::MediaType::MEDIA_TYPE_ALBUM, "file/*"}, +}; + +const std::unordered_map MEDIA_TYPE_FOLDER_MAPS = { + {Media::MediaType::MEDIA_TYPE_IMAGE, ROOT_PATH + "/image"}, + {Media::MediaType::MEDIA_TYPE_AUDIO, ROOT_PATH + "/audio"}, + {Media::MediaType::MEDIA_TYPE_VIDEO, ROOT_PATH + "/video"}, + {Media::MediaType::MEDIA_TYPE_FILE, ROOT_PATH + "/document"}, +}; + +const std::unordered_map MEDIA_TYPE_URI_MAPS = { + {Media::MediaType::MEDIA_TYPE_IMAGE, Media::MEDIALIBRARY_IMAGE_URI}, + {Media::MediaType::MEDIA_TYPE_AUDIO, Media::MEDIALIBRARY_AUDIO_URI}, + {Media::MediaType::MEDIA_TYPE_VIDEO, Media::MEDIALIBRARY_VIDEO_URI}, + {Media::MediaType::MEDIA_TYPE_FILE, Media::MEDIALIBRARY_FILE_URI}, +}; +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_FILE_MANAGER_SERVICE_DEF_H diff --git a/services/include/file_manager_service_const.h b/services/include/file_manager_service_errno.h similarity index 59% rename from services/include/file_manager_service_const.h rename to services/include/file_manager_service_errno.h index 5eb1218db734a976b5c98b9a5a475c001193f857..8ad3940e16d05c5def3190c1d34f7fd52d1f9e8f 100644 --- a/services/include/file_manager_service_const.h +++ b/services/include/file_manager_service_errno.h @@ -12,29 +12,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#pragma once - -#include +#ifndef STORAGE_SERVICES_INCLUDE_ERRNO_H +#define STORAGE_SERVICES_INCLUDE_ERRNO_H namespace OHOS { namespace FileManagerService { -enum FILE_OPER { - GET_ROOT, - MKDIR, - LIST_FILE, - CREATE_FILE -}; - -enum EQUIPMENT { - INTERNAL_CARD, - EXTERNAL_CARD -}; - -const int32_t CODE_MASK = 0xff; -const int32_t EQUIPMENT_SHIFT = 16; - -const int32_t SUCCESS = 0; -const int32_t FAIL = -1; -const int32_t E_NOEXIST = -2; // file not exist -const int32_t E_EMPTYFOLDER = -3; // folder empty +constexpr int32_t SUCCESS = 0; +constexpr int32_t FAIL = -1; // internal Error +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 } // namespace FileManagerService } // namespace OHOS +#endif // STORAGE_SERVICES_INCLUDE_ERRNO_H diff --git a/services/include/log.h b/services/include/log.h index 87bd8a21117e25658b5049bdbc176556b3747b3b..a771dfc5041d6f2d7bbdfbe91b99e9c999ef8c65 100644 --- a/services/include/log.h +++ b/services/include/log.h @@ -12,67 +12,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#pragma once +#ifndef STORAGE_SERIVCE_INCLUDE_LOG_H +#define STORAGE_SERIVCE_INCLUDE_LOG_H #include -#ifndef FILE_SUBSYSTEM_DEV_ON_PC #include "hilog/log.h" -#endif #undef LOG_DOMAIN #undef LOG_TAG -#define LOG_DOMAIN 0xD002B00 -#define LOG_TAG "Storage:FMS" +#define LOG_DOMAIN 0xD00430A +#define LOG_TAG "FileManagerment:FMS" #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) -#ifndef FILE_SUBSYSTEM_DEV_ON_PC -#ifndef OHOS_DEBUG -#define DECORATOR_HILOG(op, fmt, args...) \ - do { \ - op(LOG_CORE, fmt, ##args); \ - } while (0) -#else -#define DECORATOR_HILOG(op, fmt, args...) \ - do { \ - op(LOG_CORE, "{%s()-%s:%d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); \ - } while (0) -#endif - -#define DEBUG_LOG(fmt, args...) HILOG_DEBUG(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); -#define ERR_LOG(fmt, args...) HILOG_ERROR(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); -#define WARNING_LOG(fmt, args...) HILOG_WARN(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); -#define INFO_LOG(fmt, args...) HILOG_INFO(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); -#define FATAL_LOG(fmt, args...) HILOG_FATAL(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); - -#else -#define PCLOG(fmt, ...) \ - do { \ - const std::vector filter = { \ - "{public}", \ - "{private}", \ - }; \ - std::string str____(fmt); \ - for (auto &&pattern : filter) { \ - size_t pos = 0; \ - while (std::string::npos != (pos = str____.find(pattern))) { \ - str____.erase(pos, pattern.length()); \ - } \ - } \ - str____ += "\n"; \ - printf(str____.c_str(), ##__VA_ARGS__); \ - } while (0); - -#define DEBUG_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) -#define INFO_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) -#define WARNING_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) -#define ERR_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) -#define FATAL_LOG(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__) -#endif - -#define OK 0 -#define INVALID_PARAM (-1) -#define INIT_FAIL (-2) -#define ERR (-3) -#define PERMISSION_DENIED (-4) +#define DEBUG_LOG(fmt, args...) \ + HILOG_DEBUG(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args) +#define ERR_LOG(fmt, args...) \ + HILOG_ERROR(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args) +#define WARNING_LOG(fmt, args...) \ + HILOG_WARN(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args) +#define INFO_LOG(fmt, args...) \ + HILOG_INFO(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args) +#define FATAL_LOG(fmt, args...) \ + HILOG_FATAL(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args) +#endif // STORAGE_SERIVCE_INCLUDE_LOG_H \ No newline at end of file diff --git a/services/src/client/file_manager_proxy.cpp b/services/src/client/file_manager_proxy.cpp index 80a65d19ad378b3014dc787e8edb335e3f27bb03..78621283a98aea141078a66bcda99f4347dd73f3 100644 --- a/services/src/client/file_manager_proxy.cpp +++ b/services/src/client/file_manager_proxy.cpp @@ -14,50 +14,41 @@ */ #include "file_manager_proxy.h" -#include "file_manager_service_const.h" #include "file_info.h" +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" #include "file_manager_service_stub.h" #include "log.h" +#include "media_file_utils.h" using namespace std; namespace OHOS { namespace FileManagerService { -int GetFileInfo(FileInfo &file, MessageParcel &reply) -{ - string path; - string name; - string type; - int64_t size = 0; - int64_t at = 0; - int64_t mt = 0; +FileManagerProxy::FileManagerProxy(const sptr &impl) + : IRemoteProxy(impl) {} - reply.ReadString(path); - reply.ReadString(type); - reply.ReadString(name); - reply.ReadInt64(size); - reply.ReadInt64(at); - reply.ReadInt64(mt); - file = FileInfo(name, path, type, size, at, mt); +int FileManagerProxy::GetRoot(const std::string &devName, vector &fileRes) const +{ + // currently getRoot don't need to sendReq + fileRes.emplace_back(IMAGE_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); + fileRes.emplace_back(VIDEO_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); + fileRes.emplace_back(AUDIO_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); + fileRes.emplace_back(FILE_ROOT_NAME, FISRT_LEVEL_ALBUM, ALBUM_TYPE); return SUCCESS; } -FileManagerProxy::FileManagerProxy(const sptr &impl) - : IRemoteProxy(impl) { } - -int FileManagerProxy::CreateFile(string name, string path, string &uri) +int FileManagerProxy::CreateFile(const string &name, const string &path, string &uri) { MessageParcel data; - MessageParcel reply; - MessageOption option; - - sptr remote = Remote(); data.WriteString(name); data.WriteString(path); - int err = remote->SendRequest(FILE_OPER::CREATE_FILE, data, reply, option); + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(Operation::CREATE_FILE, data, reply, option); if (err != ERR_NONE) { - ERR_LOG("FileManagerProxy::CreateFile send request fail %{public}d", err); - return err; + ERR_LOG("inner error send request fail %{public}d", err); + return FAIL; } reply.ReadString(uri); reply.ReadInt32(err); @@ -76,31 +67,29 @@ IFmsClient *IFmsClient::GetFmsInstance() ERR_LOG("FileManager Service object is NULL."); return nullptr; } - static FileManagerProxy proxy(object); + static FileManagerProxy proxy = FileManagerProxy(object); return &proxy; } -int FileManagerProxy::ListFile(string path, int off, int count, vector &fileRes) +int FileManagerProxy::ListFile(const string &type, const string &path, int off, int count, vector &fileRes) { - int err; MessageParcel data; - MessageParcel reply; - MessageOption option; - - sptr remote = Remote(); + data.WriteString(type); data.WriteString(path); data.WriteInt32(off); data.WriteInt32(count); - err = remote->SendRequest(FILE_OPER::LIST_FILE, data, reply, option); + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(Operation::LIST_FILE, data, reply, option); if (err != ERR_NONE) { - ERR_LOG("FileManagerProxy::ListFile err %{public}d", err); - return err; + ERR_LOG("inner error send request fail %{public}d", err); + return FAIL; } int fileInfoNum = 0; reply.ReadInt32(fileInfoNum); while (fileInfoNum) { FileInfo file; - GetFileInfo(file, reply); + MediaFileUtils::PopFileInfo(file, reply); fileRes.emplace_back(file); fileInfoNum--; } @@ -108,20 +97,17 @@ int FileManagerProxy::ListFile(string path, int off, int count, vector return err; } -int FileManagerProxy::Mkdir(string name, string path) +int FileManagerProxy::Mkdir(const string &name, const string &path) { - int err; MessageParcel data; - MessageParcel reply; - MessageOption option; - - sptr remote = Remote(); data.WriteString(name); data.WriteString(path); - err = remote->SendRequest(FILE_OPER::MKDIR, data, reply, option); + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(Operation::MAKE_DIR, data, reply, option); if (err != ERR_NONE) { - ERR_LOG("FileManagerProxy::mkdir err %{public}d", err); - return err; + ERR_LOG("inner error send request fail %{public}d", err); + return FAIL; } reply.ReadInt32(err); return err; diff --git a/services/src/client/file_manager_proxy.h b/services/src/client/file_manager_proxy.h index 4df64c1aeb09b1b78632b1862727fb51d2e43b78..2d93d10ded0dec08f90996a050e43a9d0f0186a8 100644 --- a/services/src/client/file_manager_proxy.h +++ b/services/src/client/file_manager_proxy.h @@ -12,7 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#pragma once +#ifndef STORAGE_FILE_MANAGER_PROXY_H +#define STORAGE_FILE_MANAGER_PROXY_H #include "file_manager_service_stub.h" #include "ifms_client.h" @@ -26,13 +27,14 @@ class FileManagerProxy : public IRemoteProxy, public IFmsCl public: explicit FileManagerProxy(const sptr &impl); virtual ~FileManagerProxy() = default; - static IFmsClient* GetFmsInstance(); - int Mkdir(std::string name, std::string path) override; - int ListFile(std::string path, int off, int count, std::vector &fileRes) override; - int CreateFile(std::string name, std::string path, std::string &uri) override; - + 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 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: static inline BrokerDelegator delegator_; }; } // namespace FileManagerService -} // namespace OHOS \ No newline at end of file +} // namespace OHOS +#endif // STORAGE_FILE_MANAGER_PROXY_H \ No newline at end of file diff --git a/services/src/client/ifms_client.h b/services/src/client/ifms_client.h index 105411fb0c3ecf8e6884d59ebe1cc8e4dad15d6d..99b656e235b885488f8b774b1ee2af146af8f99c 100644 --- a/services/src/client/ifms_client.h +++ b/services/src/client/ifms_client.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#pragma once +#ifndef STORAGE_IFILE_MANAGER_CLIENT_H +#define STORAGE_IFILE_MANAGER_CLIENT_H #include "file_info.h" namespace OHOS { @@ -21,12 +21,13 @@ namespace FileManagerService { class IFmsClient { public: virtual ~IFmsClient() {} - - static IFmsClient *GetFmsInstance(); - - virtual int Mkdir(std::string name, std::string path) = 0; - virtual int ListFile(std::string path, int off, int count, std::vector &fileRes) = 0; - virtual int CreateFile(std::string name, std::string path, std::string &uri) = 0; + 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 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; }; } // namespace FileManagerService { -} // namespace OHOS \ No newline at end of file +} // namespace OHOS +#endif // STORAGE_IFILE_MANAGER_CLIENT_H \ No newline at end of file diff --git a/services/src/fileoper/file_info.h b/services/src/fileoper/file_info.h index 9262ccc36464045acb4ac9aab68cc5756c60d00a..a8ab503084dfce5f5219539059f0ea1161c83e2d 100644 --- a/services/src/fileoper/file_info.h +++ b/services/src/fileoper/file_info.h @@ -12,11 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef STORAGE_SERVICES_FILE_INFO_H +#define STORAGE_SERVICES_FILE_INFO_H -#pragma once - -#include #include +#include #include "ipc_types.h" #include "iremote_broker.h" #include "iremote_proxy.h" @@ -26,10 +26,35 @@ namespace OHOS { namespace FileManagerService { class FileInfo { public: - FileInfo() {}; - FileInfo(const std::string &name, const std::string &path, const std::string &type, int64_t size, int64_t added_time, - int64_t modified_time) : path_(path), name_(name), type_(type), size_(size), - added_time_(added_time), modified_time_(modified_time) {} + FileInfo(const std::string &name, const std::string &path, const std::string &type) : path_(path), + name_(name), type_(type) {} + FileInfo() = default; + ~FileInfo() = default; + + void SetName(std::string &name) + { + name_ = name; + } + void SetPath(std::string &path) + { + path_ = path; + } + void SetType(std::string &type) + { + type_ = type; + } + void SetSize(int64_t size) + { + size_ = size; + } + void SetAddedTime(int64_t time) + { + addedTime_ = time; + } + void SetModifiedTime(int64_t time) + { + modifiedTime_ = time; + } std::string GetName() const { return name_; @@ -46,21 +71,22 @@ public: { return size_; } - int64_t GetAdded_Time() const + int64_t GetAddedTime() const { - return added_time_; + return addedTime_; } - int64_t GetModified_time() const + int64_t GetModifiedTime() const { - return modified_time_; + return modifiedTime_; } private: std::string path_; std::string name_; std::string type_; - int64_t size_; - int64_t added_time_; - int64_t modified_time_; + int64_t size_ {0}; + int64_t addedTime_ {0}; + int64_t modifiedTime_ {0}; }; -} // OHOS -} // FileManager \ No newline at end of file +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERVICES_FILE_INFO_H \ No newline at end of file diff --git a/services/src/fileoper/file_oper.h b/services/src/fileoper/file_oper.h index f8027a3ec675aafba92d3857d5c59e9e0a1fadf6..57839af34e1f371b0e4909ddf0081c07775c6df0 100644 --- a/services/src/fileoper/file_oper.h +++ b/services/src/fileoper/file_oper.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#pragma once +#ifndef STORAGE_SERVICES_FILE_OPER_H +#define STORAGE_SERVICES_FILE_OPER_H #include #include "ipc_types.h" @@ -25,11 +25,10 @@ namespace OHOS { namespace FileManagerService { class FileOper { public: + FileOper() = default; virtual ~FileOper() = default; - virtual int Mkdir(const std::string &name, const std::string &path) = 0; - virtual int ListFile(const std::string &path, int offset, int count, MessageParcel &data) = 0; - virtual int CreateFile(const std::string &name, const std::string &path, std::string &uri) = 0; - virtual int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) = 0; + virtual int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) const = 0; }; -} // OHOS -} // FileManager \ No newline at end of file +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERVICES_FILE_OPER_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 2e9734bdf909f3339b75df97ae89b1dbb8c298c1..b858055b4fed13a84b0056a7ddc9fefbc51794f5 100644 --- a/services/src/fileoper/media_file_oper.cpp +++ b/services/src/fileoper/media_file_oper.cpp @@ -18,162 +18,41 @@ #include #include "file_info.h" -#include "file_manager_service_const.h" +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" #include "ipc_types.h" #include "iremote_broker.h" #include "iremote_proxy.h" #include "iremote_stub.h" #include "log.h" #include "media_data_ability_const.h" -#include "medialibrary_data_ability.h" +#include "media_file_utils.h" using namespace std; namespace OHOS { namespace FileManagerService { -const std::vector mediaData = { - Media::MEDIA_DATA_DB_ID, - Media::MEDIA_DATA_DB_URI, - Media::MEDIA_DATA_DB_NAME - }; -const std::vector> mediaMetaMap = { - {0, "string"}, // 0 id - {1, "string"}, // 1 fileuri - {4, "string"}, // 4 type - {6, "string"}, // 6 name - {7, "int"}, // 7 size - {8, "int"}, // 8 at - // 9 mt - {9, "int"} - }; -int Insert(const string &name, const string &path, const string &type) -{ - Media::ValuesBucket values; - Media::MediaLibraryDataAbility mediaLibDb; - string abilityUri = Media::MEDIALIBRARY_DATA_URI; - string oper; - if (type == "album") { - oper = Media::MEDIA_ALBUMOPRN + "/" + Media::MEDIA_ALBUMOPRN_CREATEALBUM; - } else if (type == "file") { - oper = Media::MEDIA_FILEOPRN + "/" + Media::MEDIA_FILEOPRN_CREATEASSET; - } - Uri createUri(abilityUri + "/" + oper); - struct stat statInfo {}; - // need getfileStat info - // need MEDIA_DATA_DB_RELATIVE_PATH - // need MEDIA_DATA_DB_NAME - values.PutLong(Media::MEDIA_DATA_DB_DATE_ADDED, statInfo.st_ctime); - values.PutLong(Media::MEDIA_DATA_DB_DATE_MODIFIED, statInfo.st_mtime); - mediaLibDb.InitMediaLibraryRdbStore(); - return mediaLibDb.Insert(createUri, values); -} - -bool PushFileInfo(shared_ptr result, MessageParcel &reply) -{ - string id; - string uri; - result->GetString(mediaMetaMap[0].first, id); - result->GetString(mediaMetaMap[1].first, uri); - reply.WriteString(uri + "/" + id); - for (int i = 2; i < mediaMetaMap.size(); i++) { - if (mediaMetaMap[i].second == "string") { - string value; - result->GetString(mediaMetaMap[i].first, value); - reply.WriteString(value); - } else { - int64_t value; - result->GetLong(mediaMetaMap[i].first, value); - reply.WriteInt64(value); - } - } - return true; -} - -int GetFileInfoFromResult(shared_ptr result, MessageParcel &reply) -{ - int count = 0; - result->GetRowCount(count); - if (count == 0) { - ERR_LOG("GetFileInfoFromResult::AbsSharedResultSet null"); - return FAIL; - } - result->GoToFirstRow(); - reply.WriteInt32(count); - for (int i = 0; i < count; i++) { - PushFileInfo(result, reply); - result->GoToNextRow(); - } - return SUCCESS; -} - -bool GetRelativePath(const string &path, string &relativePath) -{ - NativeRdb::DataAbilityPredicates predicates; - string selection = Media::MEDIA_DATA_DB_ID + " LIKE ? "; - vector selectionArgs = { path }; - predicates.SetWhereClause(selection); - predicates.SetWhereArgs(selectionArgs); - Media::MediaLibraryDataAbility mediaLibDb; - mediaLibDb.InitMediaLibraryRdbStore(); - Uri uri(Media::MEDIALIBRARY_DATA_URI); - vector columns; - shared_ptr result = mediaLibDb.Query(uri, columns, predicates); - if (result == nullptr) { - return false; - } - int count = 0; - result->GetRowCount(count); - - if (count != 1) { - ERR_LOG("GetRelativePath::AbsSharedResultSet more than one uri"); - } - int32_t columnIndex; - int ret = result->GetColumnIndex(Media::MEDIA_DATA_DB_FILE_PATH, columnIndex); - if (ret != NativeRdb::E_OK) { - return false; - } - result->GoToFirstRow(); - ret = result->GetString(columnIndex, relativePath); - if (ret != NativeRdb::E_OK) { - return false; - } - return true; -} - -int MediaFileOper::CreateFile(const std::string &name, const std::string &path, std::string &uri) -{ - string type = "file"; - int index = Insert(name, path, type); - // media type need to check the path - if (index < 0) { - ERR_LOG("MediaFileOper:: Fail to create fail %{public}s %{public}s", name.c_str(), path.c_str()); - return index; - } - uri = Media::MEDIALIBRARY_FILE_URI; - uri += "/" + to_string(index); - return SUCCESS; -} - -int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) +int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) const { int errCode = SUCCESS; // media process switch (code) { - case FILE_OPER::MKDIR: { + case Operation::MAKE_DIR: { string name = data.ReadString(); string path = data.ReadString(); errCode = Mkdir(name, path); break; } - case FILE_OPER::LIST_FILE: { + case Operation::LIST_FILE: { + string type = data.ReadString(); string path = data.ReadString(); int off = data.ReadInt32(); int count = data.ReadInt32(); - errCode = ListFile(path, off, count, reply); - // need reply fileInfo + // put fileInfo into reply + errCode = ListFile(type, path, off, count, reply); break; } - case FILE_OPER::CREATE_FILE: { + case Operation::CREATE_FILE: { string name = data.ReadString(); string path = data.ReadString(); string uri; @@ -181,42 +60,29 @@ int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel reply.WriteString(uri); break; } - default: + default: { + DEBUG_LOG("not valid code %{public}d.", code); break; + } } return errCode; } -int MediaFileOper::ListFile(const string &path, int offset, int count, MessageParcel &reply) +int MediaFileOper::CreateFile(const std::string &name, const std::string &path, std::string &uri) const { - // get the relative path from the path uri - string relativePath; - if (!GetRelativePath(path, relativePath)) { - ERR_LOG("MediaFileOper::path not exsit"); - return E_NOEXIST; - } - NativeRdb::DataAbilityPredicates predicates; - string selection = Media::MEDIA_DATA_DB_RELATIVE_PATH + " LIKE ? "; - vector selectionArgs = { relativePath }; - predicates.SetWhereClause(selection); - predicates.SetWhereArgs(selectionArgs); - Media::MediaLibraryDataAbility mediaLibDb; - mediaLibDb.InitMediaLibraryRdbStore(); - Uri uri(Media::MEDIALIBRARY_DATA_URI); - vector columns; - shared_ptr result = mediaLibDb.Query(uri, columns, predicates); - if (result == nullptr) { - ERR_LOG("MediaFileOper::ListFile folder is empty"); - return E_EMPTYFOLDER; - } - GetFileInfoFromResult(result, reply); - return SUCCESS; + string type = "file"; + return MediaFileUtils::DoInsert(name, path, type, uri); +} + +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); } -int MediaFileOper::Mkdir(const string &name, const string &path) +int MediaFileOper::Mkdir(const string &name, const string &path) const { - string type = "album"; - Insert(name, path, type); DEBUG_LOG("MediaFileOper::mkdir path %{public}s.", path.c_str()); return SUCCESS; } diff --git a/services/src/fileoper/media_file_oper.h b/services/src/fileoper/media_file_oper.h index 76d84858ce489857a30b74e133ec21d0899d068f..dfb5ecad4029cbc11786bf90dd217e4ebbf25c0b 100644 --- a/services/src/fileoper/media_file_oper.h +++ b/services/src/fileoper/media_file_oper.h @@ -12,21 +12,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#pragma once +#ifndef STORAGE_SERVICES_MEDIA_FILE_OPER_H +#define STORAGE_SERVICES_MEDIA_FILE_OPER_H #include #include "file_oper.h" - namespace OHOS { namespace FileManagerService { class MediaFileOper : public FileOper { public: + MediaFileOper() = default; virtual ~MediaFileOper() = default; - int Mkdir(const std::string &name, const std::string &path) override; - int ListFile(const std::string &path, int offset, int count, MessageParcel &data) override; - int CreateFile(const std::string &name, const std::string &path, std::string &uri) override; - int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) override; + 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 Mkdir(const std::string &name, const std::string &path) const; }; -} // OHOS -} // FileManager \ No newline at end of file +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERVICES_MEDIA_FILE_OPER_H \ No newline at end of file diff --git a/services/src/fileoper/media_file_utils.cpp b/services/src/fileoper/media_file_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..600ee20cdd8b2223ba13fce365d0972e675e1e7e --- /dev/null +++ b/services/src/fileoper/media_file_utils.cpp @@ -0,0 +1,424 @@ +/* + * 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 "media_file_utils.h" + +#include + +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" + +#include "media_asset.h" +#include "media_data_ability_const.h" +#include "medialibrary_data_ability.h" +#include "log.h" + +using namespace std; +namespace OHOS { +namespace FileManagerService { +bool GetPathFromResult(shared_ptr result, string &path) +{ + int count = 0; + result->GetRowCount(count); + if (count == RESULTSET_EMPTY) { + ERR_LOG("AbsSharedResultSet null"); + return false; + } + int32_t columnIndex; + int ret = result->GetColumnIndex(Media::MEDIA_DATA_DB_FILE_PATH, columnIndex); + if (ret != NativeRdb::E_OK) { + ERR_LOG("NativeRdb gets MEDIA_DATA_DB_FILE_PATH index fail"); + return false; + } + result->GoToFirstRow(); + ret = result->GetString(columnIndex, path); + if (ret != NativeRdb::E_OK) { + ERR_LOG("NativeRdb gets path index fail"); + return false; + } + return true; +} + +bool IsNumber(const string &str) +{ + if (str.length() == 0) { + ERR_LOG("IsNumber input is empty "); + return false; + } + + for (char const &c : str) { + if (isdigit(c) == 0) { + ERR_LOG("Index is not a number"); + return false; + } + } + + return true; +} + +bool GetPathID(const string &uriPath, string &index) +{ + string::size_type pos = uriPath.find_last_of('/'); + if (pos == string::npos) { + ERR_LOG("invalid uri %{public}s", uriPath.c_str()); + return false; + } + index = uriPath.substr(pos + 1); + if (!IsNumber(index)) { + ERR_LOG("invalid uri %{public}s invalid id %{public}s", uriPath.c_str(), index.c_str()); + return false; + } + 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; + if (!GetPathID(albumUri, id)) { + ERR_LOG("GetPathID fails"); + return false; + } + string selection = Media::MEDIA_DATA_DB_ID + " LIKE ? "; + vector selectionArgs = {id}; + shared_ptr result = MediaFileUtils::DoQuery(selection, selectionArgs); + if (result == nullptr) { + ERR_LOG("AbsSharedResultSet null"); + return false; + } + return GetPathFromResult(result, path); +} + +string GetType(string type) +{ + unordered_map typeMap = { + {"image", Media::MediaType::MEDIA_TYPE_IMAGE}, + {"video", Media::MediaType::MEDIA_TYPE_VIDEO}, + {"audio", Media::MediaType::MEDIA_TYPE_AUDIO} + }; + if (typeMap.count(type) == 0) { + // type is wrong + ERR_LOG("Type %{public}s", type.c_str()); + return ""; + } + return ToString(typeMap[type]); +} + +bool IsFirstLevelUriPath(const string &path) +{ + // check whether path is first level uri + return path == FISRT_LEVEL_ALBUM; +} + +bool GetAlbumFromResult(shared_ptr &result, vector &album) +{ + int count = 0; + result->GetRowCount(count); + result->GoToFirstRow(); + int32_t columnIndex; + int ret = result->GetColumnIndex(Media::MEDIA_DATA_DB_RELATIVE_PATH, columnIndex); + if (ret != NativeRdb::E_OK) { + ERR_LOG("NativeRdb gets MEDIA_DATA_DB_RELATIVE_PATH columnIndex fail"); + return false; + } + + for (int i = 0; i < count; i++) { + string path; + ret = result->GetString(columnIndex, path); + if (ret != NativeRdb::E_OK) { + ERR_LOG("NativeRdb gets path columnIndex fail"); + return false; + } + auto it = find(album.begin(), album.end(), path); + if (it == album.end()) { + album.emplace_back(path); + } + result->GoToNextRow(); + } + return true; +} + +vector FindAlbumByType(string type) +{ + // find out the first level Album + // first find out file by type + // then get the album + string selection = Media::MEDIA_DATA_DB_MEDIA_TYPE + " LIKE ?"; + vector selectionArgs = {type}; + shared_ptr result = MediaFileUtils::DoQuery(selection, selectionArgs); + vector album; + if (result == nullptr) { + ERR_LOG("query album type returns fail"); + return album; + } + GetAlbumFromResult(result, album); + return album; +} + +/* listfile + * ----find "file" type + * --------first level view---- + * --------selection MEDIA_DATA_DB_RELATIVE_PATH == root_path && + * (MEDIA_DATA_DB_MEDIA_TYPE == "file" || MEDIA_DATA_DB_MEDIA_TYPE == "album") + * --------other level view ---- + * --------selection MEDIA_DATA_DB_RELATIVE_PATH == uri.MEDIA_DATA_DB_FILE_PATH && + * (MEDIA_DATA_DB_MEDIA_TYPE == "file" || MEDIA_DATA_DB_MEDIA_TYPE == "album") + * + * ----find "image"/"audio"/"video" type + * --------first level view---- + * --------find out all album with type file + * --------selection MEDIA_DATA_DB_MEDIA_TYPE == type + * --------Get the relative path ----> Album path + * --------selection MEDIA_DATA_DB_FILE_PATH == Album path1 || selection MEDIA_DATA_DB_FILE_PATH == Album path2 || ... + * --------second level view ---- + * --------selection MEDIA_DATA_DB_RELATIVE_PATH == uri.MEDIA_DATA_DB_FILE_PATH && MEDIA_DATA_DB_MEDIA_TYPE == type + */ +int CreateSelectionAndArgsFirstLevel(const string &type, string &selection, vector &selectionArgs) +{ + if (type == "file") { + selection = Media::MEDIA_DATA_DB_RELATIVE_PATH + " LIKE ? AND (" + Media::MEDIA_DATA_DB_MEDIA_TYPE; + selection += " LIKE ? OR " + Media::MEDIA_DATA_DB_MEDIA_TYPE + " LIKE ? )"; + selectionArgs = { + ROOT_PATH, ToString(Media::MediaType::MEDIA_TYPE_FILE), + ToString(Media::MediaType::MEDIA_TYPE_ALBUM) + }; + } else { + selectionArgs = FindAlbumByType(GetType(type)); + selection = Media::MEDIA_DATA_DB_FILE_PATH + " LIKE ?"; + if (selectionArgs.size() > 1) { + for (uint32_t i = 1; i < selectionArgs.size(); i++) { + selection += " OR " + Media::MEDIA_DATA_DB_FILE_PATH + " LIKE ?"; + } + } + } + return SUCCESS; +} + +int CreateSelectionAndArgsOtherLevel(const string &type, const string &albumUri, string &selection, + vector &selectionArgs) +{ + // get the album path from the album uri + string albumPath; + if (!GetPathFromAlbumPath(albumUri, albumPath)) { + ERR_LOG("path not exsit"); + return E_NOEXIST; + } + if (type == "file") { + selection = Media::MEDIA_DATA_DB_RELATIVE_PATH + " LIKE ? AND (" + Media::MEDIA_DATA_DB_MEDIA_TYPE; + selection += " LIKE ? OR " + Media::MEDIA_DATA_DB_MEDIA_TYPE + " LIKE ? )"; + selectionArgs = { + albumPath, ToString(Media::MediaType::MEDIA_TYPE_FILE), + ToString(Media::MediaType::MEDIA_TYPE_ALBUM) + }; + } else { + selection = Media::MEDIA_DATA_DB_RELATIVE_PATH + " LIKE ? AND " + Media::MEDIA_DATA_DB_MEDIA_TYPE + " LIKE ?"; + selectionArgs = { albumPath, GetType(type) }; + } + return SUCCESS; +} + +bool GetAlbumPath(const string &name, const string &path, string &albumPath) +{ + if (IsFirstLevelUriPath(path)) { + int mediaType = GetMediaType(name); + albumPath = (MEDIA_TYPE_FOLDER_MAPS.count(mediaType) == 0) ? MEDIA_TYPE_FOLDER_MAPS.at(FILE_MEDIA_TYPE) : + MEDIA_TYPE_FOLDER_MAPS.at(mediaType); + return true; + } + return GetPathFromAlbumPath(path, albumPath); +} + +int MediaFileUtils::DoListFile(const string type, const string &path, int offset, int count, + shared_ptr &result) +{ + string selection; + vector selectionArgs; + if (IsFirstLevelUriPath(path)) { + DEBUG_LOG("IsFirstLevelUriPath "); + CreateSelectionAndArgsFirstLevel(type, selection, selectionArgs); + } else { + int err = CreateSelectionAndArgsOtherLevel(type, path, selection, selectionArgs); + if (err) { + ERR_LOG("CreateSelectionAndArgsOtherLevel returns fail"); + return err; + } + } + result = DoQuery(selection, selectionArgs); + if (result == nullptr) { + ERR_LOG("ListFile folder is empty"); + return E_EMPTYFOLDER; + } + return SUCCESS; +} + +shared_ptr MediaFileUtils::DoQuery(string selection, vector selectionArgs) +{ + NativeRdb::DataAbilityPredicates predicates; + predicates.SetWhereClause(selection); + predicates.SetWhereArgs(selectionArgs); + Media::MediaLibraryDataAbility mediaLibDb; + mediaLibDb.InitMediaLibraryRdbStore(); + Uri uri = Uri(Media::MEDIALIBRARY_DATA_URI); + vector columns; + return mediaLibDb.Query(uri, columns, predicates); +} + +int MediaFileUtils::DoInsert(const string &name, const string &path, const string &type, string &uri) +{ + Media::ValuesBucket values; + + string albumPath; + if (!GetAlbumPath(name, path, albumPath)) { + ERR_LOG("path not exsit"); + return E_NOEXIST; + } + // album path + name ---> MEDIA_DATA_DB_FILE_PATH + values.PutString(Media::MEDIA_DATA_DB_FILE_PATH, albumPath + "/" + name); + values.PutString(Media::MEDIA_DATA_DB_MIME_TYPE, GetMimeType(name)); + values.PutInt(Media::MEDIA_DATA_DB_MEDIA_TYPE, GetMediaType(name)); + + Uri createAsset(Media::MEDIALIBRARY_DATA_URI + "/" + Media::MEDIA_FILEOPRN + "/" + + Media::MEDIA_FILEOPRN_CREATEASSET); + Media::MediaLibraryDataAbility mediaLibDb; + mediaLibDb.InitMediaLibraryRdbStore(); + int index = mediaLibDb.Insert(createAsset, values); + if (index < 0) { + ERR_LOG("Fail to create fail %{public}s %{public}s", name.c_str(), path.c_str()); + return E_CREATE_FAIL; + } + uri = (MEDIA_TYPE_URI_MAPS.count(GetMediaType(name)) == 0) ? MEDIA_TYPE_URI_MAPS.at(FILE_MEDIA_TYPE) : + MEDIA_TYPE_URI_MAPS.at(GetMediaType(name)); + uri += "/" + to_string(index); + return SUCCESS; +} + +bool MediaFileUtils::InitMediaTableColIndexMap(shared_ptr result) +{ + if (mediaTableMap.size() == 0) { + DEBUG_LOG("init mediaTableMap"); + vector> mediaData = { + {Media::MEDIA_DATA_DB_ID, "string"}, + {Media::MEDIA_DATA_DB_URI, "string"}, + {Media::MEDIA_DATA_DB_MEDIA_TYPE, "string"}, + {Media::MEDIA_DATA_DB_NAME, "string"}, + {Media::MEDIA_DATA_DB_SIZE, "int"}, + {Media::MEDIA_DATA_DB_DATE_ADDED, "int"}, + {Media::MEDIA_DATA_DB_DATE_MODIFIED, "int"} + }; + for (auto i : mediaData) { + int columnIndex = 0; + int ret = result->GetColumnIndex(i.first, columnIndex); + if (ret != NativeRdb::E_OK) { + ERR_LOG("NativeRdb returns fail"); + return false; + } + mediaTableMap.emplace_back(columnIndex, i.second); + } + } + return true; +} + +bool MediaFileUtils::PushFileInfo(shared_ptr result, MessageParcel &reply) +{ + if (!InitMediaTableColIndexMap(result)) { + ERR_LOG("InitMediaTableColIndexMap returns fail"); + return false; + } + int index = 0; + string id; + result->GetString(mediaTableMap[index++].first, id); + string uri; + result->GetString(mediaTableMap[index++].first, uri); + reply.WriteString(uri + "/" + id); + for (int i = index; i < mediaTableMap.size(); i++) { + if (mediaTableMap[i].second == "string") { + string value; + result->GetString(mediaTableMap[i].first, value); + reply.WriteString(value); + } else { + int64_t value; + result->GetLong(mediaTableMap[i].first, value); + reply.WriteInt64(value); + } + } + return true; +} + +bool MediaFileUtils::PopFileInfo(FileInfo &file, MessageParcel &reply) +{ + string path; + string name; + string type; + 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); + file = FileInfo(name, path, type); + file.SetSize(size); + file.SetAddedTime(at); + file.SetModifiedTime(mt); + return true; +} + +int MediaFileUtils::GetFileInfoFromResult(shared_ptr result, + MessageParcel &reply, int res) +{ + 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); + result->GoToNextRow(); + } + 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 new file mode 100644 index 0000000000000000000000000000000000000000..265189b026874e9afe0754f14680660dd2f7eee2 --- /dev/null +++ b/services/src/fileoper/media_file_utils.h @@ -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. + */ +#ifndef STORAGE_SERIVCES_MEDIA_FILE_UTILS_H +#define STORAGE_SERIVCES_MEDIA_FILE_UTILS_H + +#include +#include + +#include "abs_shared_result_set.h" +#include "file_info.h" +#include "file_oper.h" + +namespace OHOS { +namespace FileManagerService { +class MediaFileUtils { +public: + MediaFileUtils(); + ~MediaFileUtils(); + static int DoListFile(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); + 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 int GetFileInfoFromResult(std::shared_ptr result, + MessageParcel &reply, int res); + static bool InitMediaTableColIndexMap(std::shared_ptr result); +private: + inline static std::vector> mediaTableMap = {}; +}; +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERIVCES_MEDIA_FILE_UTILS_H \ No newline at end of file diff --git a/services/src/fileoper/oper_factory.cpp b/services/src/fileoper/oper_factory.cpp index 542d88585ee4e798650209fbad94121c86e942bf..23f25ceda7a6ca854d6f3f7232f111297fcaf897 100644 --- a/services/src/fileoper/oper_factory.cpp +++ b/services/src/fileoper/oper_factory.cpp @@ -15,29 +15,30 @@ #include "oper_factory.h" +#include "file_manager_service_def.h" #include "file_oper.h" -#include "file_manager_service_const.h" #include "log.h" #include "media_file_oper.h" using namespace std; namespace OHOS { namespace FileManagerService { -FileOper* OperFactory::getFileOper(int equipmentId) +unique_ptr OperFactory::GetFileOper(int equipmentId) { - FileOper* fp = nullptr; - DEBUG_LOG("OperFactory::getFileOper %{public}d.", equipmentId); + unique_ptr fp; + DEBUG_LOG("FileOper %{public}d.", equipmentId); switch (equipmentId) { - case EQUIPMENT::INTERNAL_CARD: { - fp = new MediaFileOper(); + case Equipment::INTERNAL_STORAGE: { + fp = make_unique(); break; } - case EQUIPMENT::EXTERNAL_CARD: { + case Equipment::EXTERNAL_STORAGE: { + DEBUG_LOG("FileOper exter %{public}d %{public}d.", Equipment::EXTERNAL_STORAGE, equipmentId); // do Exteranl storage process; - // return ExternalOper() break; } default: { + DEBUG_LOG("default FileOper %{public}d.", equipmentId); break; } } diff --git a/services/src/fileoper/oper_factory.h b/services/src/fileoper/oper_factory.h index 992e2ccbe2cb2ba463c835851ee105d71bb1cf4f..90443b054695ec6866aa735a58b652920c992288 100644 --- a/services/src/fileoper/oper_factory.h +++ b/services/src/fileoper/oper_factory.h @@ -12,7 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#ifndef STORAGE_SERVICES_OPER_FACTORY_H +#define STORAGE_SERVICES_OPER_FACTORY_H #pragma once #include @@ -23,7 +24,8 @@ namespace OHOS { namespace FileManagerService { class OperFactory { public: - FileOper* getFileOper(int equipmentId); + std::unique_ptr GetFileOper(int equipmentId); }; -} // OHOS -} // FileManager \ No newline at end of file +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERVICES_OPER_FACTORY_H \ No newline at end of file diff --git a/services/src/server/file_manager_service.cpp b/services/src/server/file_manager_service.cpp index 2a924a621d41fa5e6ccd50039e073420e309400b..5eae3bcff17f7f2d0037ce832c371ba895d11d99 100644 --- a/services/src/server/file_manager_service.cpp +++ b/services/src/server/file_manager_service.cpp @@ -19,17 +19,17 @@ #include "log.h" #include "system_ability_definition.h" - namespace OHOS { namespace FileManagerService { REGISTER_SYSTEM_ABILITY_BY_ID(FileManagerService, FILE_MANAGER_SERVICE_ID, true); FileManagerService::FileManagerService(int32_t systemAbilityId, bool runOnCreate) - : SystemAbility(systemAbilityId, runOnCreate) { } -void FileManagerService::OnDump() { } + : SystemAbility(systemAbilityId, runOnCreate) {} +void FileManagerService::OnDump() {} void FileManagerService::OnStart() { + DEBUG_LOG("FileManagerService OnStart"); bool res = Publish(this); if (!res) { ERR_LOG("FileManagerService OnStart invalid"); diff --git a/services/src/server/file_manager_service.h b/services/src/server/file_manager_service.h index 181d29d4d64ad34acaf10f88f689bd987053e7c4..3410c40973e31563e488848469326085990e6c2e 100644 --- a/services/src/server/file_manager_service.h +++ b/services/src/server/file_manager_service.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#pragma once +#ifndef STORAGE_FILE_MANAGER_SERVICE_H +#define STORAGE_FILE_MANAGER_SERVICE_H #include "file_manager_service_stub.h" #include "iremote_stub.h" @@ -33,3 +33,4 @@ public: }; } // namespace FileManagerService } // namespace OHOS +#endif // STORAGE_FILE_MANAGER_SERVICE_H \ No newline at end of file diff --git a/services/src/server/file_manager_service_stub.cpp b/services/src/server/file_manager_service_stub.cpp index d286a6f011fbb06fde4eeda6109981b430a58a26..a11c8371d9f01e1a2abe9e7874258777bb61a4a8 100644 --- a/services/src/server/file_manager_service_stub.cpp +++ b/services/src/server/file_manager_service_stub.cpp @@ -15,8 +15,9 @@ #include "file_manager_service_stub.h" -#include "file_manager_service_const.h" #include "file_manager_service.h" +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" #include "log.h" #include "oper_factory.h" @@ -24,28 +25,28 @@ using namespace std; namespace OHOS { namespace FileManagerService { -int getEquipmentCode(uint32_t code) +int GetEquipmentCode(uint32_t code) { return (code >> EQUIPMENT_SHIFT) & CODE_MASK; } -int getOperCode(uint32_t code) + +int GetOperCode(uint32_t code) { return code & CODE_MASK; } + int FileManagerServiceStub::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) { - int equipmentId = getEquipmentCode(code); - int operCode = getOperCode(code); + int equipmentId = GetEquipmentCode(code); + int operCode = GetOperCode(code); OperFactory factory = OperFactory(); - auto *fp = factory.getFileOper(equipmentId); + auto fp = factory.GetFileOper(equipmentId); if (fp == nullptr) { ERR_LOG("OnRemoteRequest inner error %{public}d", code); return FAIL; } int errCode = fp->OperProcess(operCode, data, reply); - - delete fp; return errCode; } diff --git a/services/src/server/file_manager_service_stub.h b/services/src/server/file_manager_service_stub.h index d0fbc97e062aec7a59a3530fe9c44b379fd28d52..e00697a98cf0b7bdda1cfd05700fc87ed0ee3143 100644 --- a/services/src/server/file_manager_service_stub.h +++ b/services/src/server/file_manager_service_stub.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#pragma once +#ifndef STORAGE_FILE_MANAGER_SERVICE_STUB_H +#define STORAGE_FILE_MANAGER_SERVICE_STUB_H #include "ipc_types.h" #include "iremote_broker.h" @@ -30,9 +30,12 @@ public: class FileManagerServiceStub : public IRemoteStub { public: + FileManagerServiceStub() = default; + virtual ~FileManagerServiceStub() = default; int OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply); virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, - MessageParcel &reply, MessageOption &option) override; + MessageParcel &reply, MessageOption &option) override; }; } // namespace FileManagerService } // namespace OHOS +#endif // STORAGE_FILE_MANAGER_SERVICE_STUB_H \ No newline at end of file diff --git a/services/test/fms_test.cpp b/services/test/fms_test.cpp deleted file mode 100644 index a339a9c6d1eb3bcdf2eabc95f5ccacec21631330..0000000000000000000000000000000000000000 --- a/services/test/fms_test.cpp +++ /dev/null @@ -1,225 +0,0 @@ -/* - * 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include "file_manager_proxy.h" - -namespace { -using namespace std; -using namespace OHOS; -class FMSTest : public testing::Test { -public: - static void SetUpTestCase(void) - { - cout << "FMS code test" << endl; - } - static void TearDownTestCase() {} -}; - -/** - * @tc.number: SUB_STORAGE_FMS_Proxy_GetFmsInstance_0000 - * @tc.name: fms_Proxy_GetFmsInstance_0000 - * @tc.desc: Test function of GetFmsInstance interface. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: - */ -HWTEST_F(FMSTest, FMS_Proxy_GetFmsInstance_0000, testing::ext::TestSize.Level1) -{ - cout << "FMSTest-begin FMS_Proxy_GetFmsInstance_0000" << endl; - try { - FileManagerService::FileManagerProxy proxy; - IFmsClient result = proxy.GetFmsInstance(); - EXPECT_NE(result, nullptr); - } catch (...) { - cout << "FMSTest-an exception occurred." << endl; - } - cout << "FMSTest-end FMS_Proxy_GetFmsInstance_0000" << endl; -} - -/** - * @tc.number: SUB_STORAGE_FMS_Proxy_getFileInfo_0000 - * @tc.name: fms_Proxy_getFileInfo_0000 - * @tc.desc: Test function of getFileInfo interface. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: - */ -HWTEST_F(FMSTest, FMS_Proxy_getFileInfo_0000, testing::ext::TestSize.Level1) -{ - cout << "FMSTest-begin FMS_Proxy_getFileInfo_0000" << endl; - try { - FileManagerService::FileManagerProxy proxy; - FileInfo fileInfo; - MessageParcel messageParcel; - messageParcel.WriteString("FMS_Proxy_getFileInfo_0000"); - messageParcel.WriteString("./"); - messageParcel.WriteString("file"); - messageParcel.WriteString(0); - messageParcel.WriteString(0); - messageParcel.WriteString(0); - int result = proxy.getFileInfo(fileInfo, messageParcel); - EXPECT_EQ(result, 0); - EXPECT_NE(fileInfo, nullptr); - } catch (...) { - cout << "FMSTest-an exception occurred." << endl; - } - cout << "FMSTest-end FMS_Proxy_getFileInfo_0000" << endl; -} - -/** - * @tc.number: SUB_STORAGE_FMS_Proxy_ListFile_0000 - * @tc.name: fms_Proxy_ListFile_0000 - * @tc.desc: Test function of ListFile interface. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: - */ -HWTEST_F(FMSTest, FMS_Proxy_ListFile_0000, testing::ext::TestSize.Level1) -{ - cout << "FMSTest-begin FMS_Proxy_ListFile_0000" << endl; - try { - FileManagerService::FileManagerProxy proxy; - vector fileList; - int result = proxy.ListFile("./", 0, 1, fileList); - EXPECT_EQ(result, 0); - EXPECT_NE(fileList, nullptr); - } catch (...) { - cout << "FMSTest-an exception occurred." << endl; - } - cout << "FMSTest-end FMS_Proxy_ListFile_0000" << endl; -} - -/** - * @tc.number: SUB_STORAGE_FMS_Mediafile_Insert_0000 - * @tc.name: fms_Mediafile_Insert_0000 - * @tc.desc: Test function of Insert interface. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: - */ -HWTEST_F(FMSTest, FMS_Mediafile_Insert_0000, testing::ext::TestSize.Level1) -{ - cout << "FMSTest-begin FMS_Mediafile_Insert_0000" << endl; - try { - int result = MediaFileOper::Insert("FMS_Mediafile_Insert_0000", "./", "file"); - EXPECT_EQ(result, 0); - } catch (...) { - cout << "FMSTest-an exception occurred." << endl; - } - cout << "FMSTest-end FMS_Mediafile_Insert_0000" << endl; -} - -/** - * @tc.number: SUB_STORAGE_FMS_Mediafile_CreateFile_0000 - * @tc.name: fms_Mediafile_CreateFile_0000 - * @tc.desc: Test function of CreateFile interface. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: - */ -HWTEST_F(FMSTest, FMS_Mediafile_CreateFile_0000, testing::ext::TestSize.Level1) -{ - cout << "FMSTest-begin FMS_Mediafile_CreateFile_0000" << endl; - try { - string path = "./"; - string url; - int result = MediaFileOper::CreateFile("FMS_Mediafile_CreateFile_0000", path, url); - EXPECT_EQ(result, 0); - } catch (...) { - cout << "FMSTest-an exception occurred." << endl; - } - cout << "FMSTest-end FMS_Mediafile_CreateFile_0000" << endl; -} - -/** - * @tc.number: SUB_STORAGE_FMS_Mediafile_pushFileInfo_0000 - * @tc.name: fms_Mediafile_pushFileInfo_0000 - * @tc.desc: Test function of pushFileInfo interface. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: - */ -HWTEST_F(FMSTest, FMS_Mediafile_pushFileInfo_0000, testing::ext::TestSize.Level1) -{ - cout << "FMSTest-begin FMS_Mediafile_pushFileInfo_0000" << endl; - try { - FileInfo fileInfo = FileInfo("FMS_Mediafile_pushFileInfo_0000", "./", "file", 0, 0, 0); - MessageParcel messageParcel; - int result = MediaFileOper::pushFileInfo(fileInfo, messageParcel); - EXPECT_EQ(result, 0); - } catch (...) { - cout << "FMSTest-an exception occurred." << endl; - } - cout << "FMSTest-end FMS_Mediafile_pushFileInfo_0000" << endl; -} - -/** - * @tc.number: SUB_STORAGE_FMS_Mediafile_ListFile_0000 - * @tc.name: fms_Mediafile_ListFile_0000 - * @tc.desc: Test function of ListFile interface. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: - */ -HWTEST_F(FMSTest, FMS_Mediafile_ListFile_0000, testing::ext::TestSize.Level1) -{ - cout << "FMSTest-begin FMS_Mediafile_ListFile_0000" << endl; - try { - MessageParcel messageParcel; - int result = MediaFileOper::ListFile("./", 0, 0, messageParcel); - EXPECT_EQ(result, 0); - } catch (...) { - cout << "FMSTest-an exception occurred." << endl; - } - cout << "FMSTest-end FMS_Mediafile_ListFile_0000" << endl; -} - -/** - * @tc.number: SUB_STORAGE_FMS_Mediafile_mkdir_0000 - * @tc.name: fms_Mediafile_mkdir_0000 - * @tc.desc: Test function of mkdir interface. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: - */ -HWTEST_F(FMSTest, FMS_Mediafile_mkdir_0000, testing::ext::TestSize.Level1) -{ - cout << "FMSTest-begin FMS_Mediafile_mkdir_0000" << endl; - try { - int result = MediaFileOper::mkdir("FMS_Mediafile_mkdir_0000", "./"); - EXPECT_EQ(result, 1); - } catch (...) { - cout << "FMSTest-an exception occurred." << endl; - } - cout << "FMSTest-end FMS_Mediafile_mkdir_0000" << endl; -} -} // namespace \ No newline at end of file