From cd4a64bc79a90bb058f47272e207b0ae133d6d57 Mon Sep 17 00:00:00 2001 From: Dageking Date: Mon, 24 Jan 2022 11:13:54 +0800 Subject: [PATCH 1/2] add external storage code. Signed-off-by: Dageking --- services/include/file_manager_service_errno.h | 1 + .../src/fileoper/external_storage_oper.cpp | 70 ++++++++ services/src/fileoper/external_storage_oper.h | 33 ++++ .../src/fileoper/external_storage_utils.cpp | 155 ++++++++++++++++++ .../src/fileoper/external_storage_utils.h | 35 ++++ services/src/fileoper/oper_factory.cpp | 2 + services/src/fileoper/test/BUILD.gn | 64 ++++++++ services/src/fileoper/test/test.cpp | 36 ++++ 8 files changed, 396 insertions(+) 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/test/BUILD.gn create mode 100644 services/src/fileoper/test/test.cpp diff --git a/services/include/file_manager_service_errno.h b/services/include/file_manager_service_errno.h index 8ad3940e..86e56a70 100644 --- a/services/include/file_manager_service_errno.h +++ b/services/include/file_manager_service_errno.h @@ -22,6 +22,7 @@ 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 +constexpr int32_t E_EXIST = -6; // file exist } // namespace FileManagerService } // namespace OHOS #endif // STORAGE_SERVICES_INCLUDE_ERRNO_H diff --git a/services/src/fileoper/external_storage_oper.cpp b/services/src/fileoper/external_storage_oper.cpp new file mode 100644 index 00000000..2e7b5fa8 --- /dev/null +++ b/services/src/fileoper/external_storage_oper.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "external_storage_oper.h" + +#include + +#include "file_info.h" +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" +#include "log.h" +#include "external_storage_utils.h" + +namespace OHOS { +namespace FileManagerService { +int ExternalStorageOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) const +{ + DEBUG_LOG("ExternalStorageOper::OperProcess"); + int errCode = SUCCESS; + switch (code) { + case Operation::LIST_FILE: { + std::string path = data.ReadString(); + errCode = this->ListFile(path, reply); + break; + } + case Operation::CREATE_FILE: { + std::string path = data.ReadString(); + std::string name = data.ReadString(); + errCode = this->CreateFile(path, name, reply); + break; + } + default: { + DEBUG_LOG("not valid code %{public}d.", code); + break; + } + } + return errCode; +} + +int ExternalStorageOper::CreateFile(const std::string &path, const std::string &name, MessageParcel &reply) const +{ + DEBUG_LOG("ExternalStorageOper::CreateFile"); + return ExternalStorageUtils::DoCreateFile(path, name); +} + +int ExternalStorageOper::ListFile(const std::string &path, MessageParcel &reply) const +{ + DEBUG_LOG("ExternalStorageOper::ListFile"); + std::vector fileList; + auto ret = ExternalStorageUtils::DoListFile(path, fileList); + for (auto fileInfo : fileList) { + DEBUG_LOG("--->ExternalStorageOper::ListFile-->Type:%{public}s,Path:%{public}s,Name:%{public}s,Size:%{public}lld,AddedTime:%{public}lld,ModifiedTime:%{public}lld.", + fileInfo.GetType().c_str(), fileInfo.GetPath().c_str(), fileInfo.GetName().c_str(), fileInfo.GetSize(), fileInfo.GetAddedTime(), fileInfo.GetModifiedTime()); + } + return ret; +} +} // 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..f04ccc71 --- /dev/null +++ b/services/src/fileoper/external_storage_oper.h @@ -0,0 +1,33 @@ +/* + * 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 "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 &path, const std::string &name, MessageParcel &reply) const; + int ListFile(const std::string &path, 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..d61d10fb --- /dev/null +++ b/services/src/fileoper/external_storage_utils.cpp @@ -0,0 +1,155 @@ +/* + * 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 "file_manager_service_def.h" +#include "file_manager_service_errno.h" +#include "log.h" + +using namespace std; +namespace OHOS { +namespace FileManagerService { + +bool EndsWith(const std::string& str, const std::string& suffix) +{ + auto compareFun = [](unsigned char a, unsigned char b) { + return std::tolower(a) == std::tolower(b); + }; + if (str.size() < suffix.size()) { + return false; + } + std::string targetStr = str.substr(str.size() - suffix.size()); + if (targetStr.length() == suffix.length()) { + return std::equal(suffix.begin(), suffix.end(), targetStr.begin(), compareFun); + } else { + return false; + } +} + +static bool GetFileInfo(const unsigned char type, + const std::string &path, + const std::string &name, + FileInfo &fileInfo) +{ + struct stat st; + if (lstat(path.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 fType; + switch (type) + { + case DT_FIFO: + fType = "DT_FIFO"; + break; + case DT_CHR: + fType = "DT_CHR"; + break; + case DT_DIR: + fType = "DT_DIR"; + break; + case DT_BLK: + fType = "DT_BLK"; + break; + case DT_REG: + fType = "DT_REG"; + break; + case DT_LNK: + fType = "DT_LNK"; + break; + case DT_SOCK: + fType = "DT_SOCK"; + break; + case DT_WHT: + fType = "DT_WHT"; + break; + default: + fType = "DT_UNKNOWN"; + } + + fileInfo.SetType(fType); + return true; +} + +int ExternalStorageUtils::DoListFile(const std::string &path, std::vector &fileList) +{ + DEBUG_LOG("ExternalStorageUtils::DoListFile"); + DIR *dir = opendir(path.c_str()); + if (!dir) { + ERR_LOG("opendir path[%{public}s] fail.", path.c_str()); + return E_NOEXIST; + } + for (struct dirent *ent = readdir(dir); ent != nullptr; ent = readdir(dir)) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { + continue; + } + std::string fullPath(""); + fullPath.append(path); + if (!EndsWith(path, "/")) { + fullPath.append("/"); + } + FileInfo fileInfo; + if (!GetFileInfo(ent->d_type, fullPath, ent->d_name, fileInfo)) { + continue; + } + fileList.push_back(fileInfo); + } + closedir(dir); + return SUCCESS; +} + +int ExternalStorageUtils::DoCreateFile(const std::string &path, const std::string &name) +{ + DEBUG_LOG("ExternalStorageUtils::DoCreateFile"); + + std::string fullPath(""); + fullPath.append(path); + if (!EndsWith(path, "/")) { + fullPath.append("/"); + } + fullPath.append(name); + + if (access(fullPath.c_str(), F_OK) == 0) { + ERR_LOG("target file[%{public}s] exist.", fullPath.c_str()); + return E_EXIST; + } + + int fd = open(fullPath.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0771); + if (fd == -1) { + ERR_LOG("create file[%{public}s] fail.", fullPath.c_str()); + return E_CREATE_FAIL; + } + close(fd); + return SUCCESS; +} +} // 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..ef88f4aa --- /dev/null +++ b/services/src/fileoper/external_storage_utils.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef STORAGE_SERIVCES_EXTERNAL_STORAGE_UTILS_H +#define STORAGE_SERIVCES_EXTERNAL_STORAGE_UTILS_H + +#include +#include + +#include "file_info.h" +#include "file_oper.h" + +namespace OHOS { +namespace FileManagerService { +class ExternalStorageUtils { +public: + ExternalStorageUtils(); + ~ExternalStorageUtils(); + static int DoListFile(const std::string &path, std::vector &fileList); + static int DoCreateFile(const std::string &path, const std::string &name); +}; +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERIVCES_EXTERNAL_STORAGE_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 23f25ced..f7efda50 100644 --- a/services/src/fileoper/oper_factory.cpp +++ b/services/src/fileoper/oper_factory.cpp @@ -19,6 +19,7 @@ #include "file_oper.h" #include "log.h" #include "media_file_oper.h" +#include "external_storage_oper.h" using namespace std; namespace OHOS { @@ -35,6 +36,7 @@ unique_ptr OperFactory::GetFileOper(int equipmentId) 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/test/BUILD.gn b/services/src/fileoper/test/BUILD.gn new file mode 100644 index 00000000..55fae5fa --- /dev/null +++ b/services/src/fileoper/test/BUILD.gn @@ -0,0 +1,64 @@ +# 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. + +import("//build/ohos.gni") +FMS_BASE_DIR = "//foundation/filemanagement/user_file_service/services" + +ohos_executable("fileopertest") { + subsystem_name = "filemanagement" + part_name = "file_manager_service" + + include_dirs = [ + "$FMS_BASE_DIR/include", + "$FMS_BASE_DIR/src/fileoper", + "$FMS_BASE_DIR/include/fileoper", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include/hilog", + ] + + sources = [ + "$FMS_BASE_DIR/src/fileoper/test/test.cpp", + "$FMS_BASE_DIR/src/fileoper/media_file_oper.cpp", + "$FMS_BASE_DIR/src/fileoper/media_file_utils.cpp", + "$FMS_BASE_DIR/src/fileoper/oper_factory.cpp", + "$FMS_BASE_DIR/src/fileoper/external_storage_oper.cpp", + "$FMS_BASE_DIR/src/fileoper/external_storage_utils.cpp", + ] + + deps = [ + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//utils/native/base:utils", + "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/media_library:media_library", + "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/medialibrary_data_ability:medialibrary_data_ability", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/interfaces/innerkits/base:base", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager:dataobs_manager", + ] + + cflags = [] + + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + external_deps = [ + "ability_runtime:ability_manager", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "native_appdatamgr:native_appdatafwk", + "native_appdatamgr:native_dataability", + "native_appdatamgr:native_rdb", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] +} diff --git a/services/src/fileoper/test/test.cpp b/services/src/fileoper/test/test.cpp new file mode 100644 index 00000000..f2d7dbb5 --- /dev/null +++ b/services/src/fileoper/test/test.cpp @@ -0,0 +1,36 @@ + +#include + +#include "oper_factory.h" +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" +#include "log.h" + +using namespace OHOS::FileManagerService; +using namespace std; +int main(int argc, char** argv) +{ DEBUG_LOG("main -- start"); + OperFactory factory = OperFactory(); + int equipmentId = Equipment::EXTERNAL_STORAGE; + auto fp = factory.GetFileOper(equipmentId); + if (fp == nullptr) { + ERR_LOG("OnRemoteRequest inner error %{public}d", equipmentId); + return FAIL; + } + + uint32_t operCode = Operation::CREATE_FILE; + OHOS::MessageParcel data1; + OHOS::MessageParcel reply1; + data1.WriteString("/data/data"); + data1.WriteString("666.txt"); + int errCode = fp->OperProcess(operCode, data1, reply1); + DEBUG_LOG("Operation::CREATE_FILE -- > errCode:%{public}d", errCode); + + operCode = Operation::LIST_FILE; + OHOS::MessageParcel data2; + OHOS::MessageParcel reply2; + data2.WriteString("/data/data"); + errCode = fp->OperProcess(operCode, data2, reply2); + DEBUG_LOG("Operation::LIST_FILE -- > errCode:%{public}d", errCode); + return 0; +} \ No newline at end of file -- Gitee From 34222fbee28c2cb5379b288193f68eeb052adfa5 Mon Sep 17 00:00:00 2001 From: Dageking Date: Mon, 24 Jan 2022 16:45:54 +0800 Subject: [PATCH 2/2] add external storage code Signed-off-by: Dageking --- services/include/file_manager_service_errno.h | 1 - services/src/fileoper/external_storage_utils.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/services/include/file_manager_service_errno.h b/services/include/file_manager_service_errno.h index 86e56a70..8ad3940e 100644 --- a/services/include/file_manager_service_errno.h +++ b/services/include/file_manager_service_errno.h @@ -22,7 +22,6 @@ 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 -constexpr int32_t E_EXIST = -6; // file exist } // namespace FileManagerService } // namespace OHOS #endif // STORAGE_SERVICES_INCLUDE_ERRNO_H diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp index d61d10fb..9603f3c3 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -140,7 +140,7 @@ int ExternalStorageUtils::DoCreateFile(const std::string &path, const std::strin if (access(fullPath.c_str(), F_OK) == 0) { ERR_LOG("target file[%{public}s] exist.", fullPath.c_str()); - return E_EXIST; + return E_CREATE_FAIL; } int fd = open(fullPath.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0771); -- Gitee