From 085d531219294cf1dd5b078ec66bff66af67efbe Mon Sep 17 00:00:00 2001 From: Dageking Date: Mon, 24 Jan 2022 17:22:46 +0800 Subject: [PATCH 1/3] add external storage code Signed-off-by: Dageking --- services/BUILD.gn | 2 + .../src/fileoper/external_storage_oper.cpp | 72 +++++++++ services/src/fileoper/external_storage_oper.h | 33 ++++ .../src/fileoper/external_storage_utils.cpp | 153 ++++++++++++++++++ .../src/fileoper/external_storage_utils.h | 35 ++++ services/src/fileoper/oper_factory.cpp | 3 +- 6 files changed, 297 insertions(+), 1 deletion(-) create mode 100644 services/src/fileoper/external_storage_oper.cpp create mode 100644 services/src/fileoper/external_storage_oper.h create mode 100644 services/src/fileoper/external_storage_utils.cpp create mode 100644 services/src/fileoper/external_storage_utils.h diff --git a/services/BUILD.gn b/services/BUILD.gn index f265421e..329c443b 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -43,6 +43,8 @@ ohos_shared_library("fms_server") { sources = [ "src/client/file_manager_proxy.cpp", + "src/fileoper/external_storage_oper.cpp", + "src/fileoper/external_storage_utils.cpp", "src/fileoper/media_file_oper.cpp", "src/fileoper/media_file_utils.cpp", "src/fileoper/oper_factory.cpp", diff --git a/services/src/fileoper/external_storage_oper.cpp b/services/src/fileoper/external_storage_oper.cpp new file mode 100644 index 00000000..77dd3e1e --- /dev/null +++ b/services/src/fileoper/external_storage_oper.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "external_storage_oper.h" + +#include + +#include "external_storage_utils.h" +#include "file_info.h" +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" +#include "log.h" + +namespace OHOS { +namespace FileManagerService { +int ExternalStorageOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) const +{ + DEBUG_LOG("ExternalStorageOper::OperProcess"); + int errCode = SUCCESS; + switch (code) { + case Operation::LIST_FILE: { + std::string 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..5d0a6cd4 --- /dev/null +++ b/services/src/fileoper/external_storage_utils.cpp @@ -0,0 +1,153 @@ +/* + * 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_CREATE_FAIL; + } + + 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..fa45591b 100644 --- a/services/src/fileoper/oper_factory.cpp +++ b/services/src/fileoper/oper_factory.cpp @@ -15,6 +15,7 @@ #include "oper_factory.h" +#include "external_storage_oper.h" #include "file_manager_service_def.h" #include "file_oper.h" #include "log.h" @@ -34,7 +35,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: { -- Gitee From 8a677b3e35c52b013d47a614dd292f952341f6bd Mon Sep 17 00:00:00 2001 From: Dageking Date: Mon, 24 Jan 2022 18:45:19 +0800 Subject: [PATCH 2/3] add external storage code Signed-off-by: Dageking --- services/src/fileoper/external_storage_oper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/src/fileoper/external_storage_oper.cpp b/services/src/fileoper/external_storage_oper.cpp index 77dd3e1e..85ca21cc 100644 --- a/services/src/fileoper/external_storage_oper.cpp +++ b/services/src/fileoper/external_storage_oper.cpp @@ -61,7 +61,7 @@ int ExternalStorageOper::ListFile(const std::string &path, MessageParcel &reply) 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, + 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()); -- Gitee From 24a2f11b30b2bea694c6aa78fea2487407bf7083 Mon Sep 17 00:00:00 2001 From: Dageking Date: Tue, 25 Jan 2022 11:19:04 +0800 Subject: [PATCH 3/3] add external storage code Signed-off-by: Dageking --- services/BUILD.gn | 1 + services/include/file_manager_service_errno.h | 1 + .../src/fileoper/external_storage_oper.cpp | 24 +-- services/src/fileoper/external_storage_oper.h | 4 +- .../src/fileoper/external_storage_utils.cpp | 143 +++++++++--------- .../src/fileoper/external_storage_utils.h | 6 +- services/src/fileoper/media_file_utils.cpp | 21 +-- services/src/fileoper/utils.cpp | 46 ++++++ services/src/fileoper/utils.h | 26 ++++ 9 files changed, 162 insertions(+), 110 deletions(-) create mode 100644 services/src/fileoper/utils.cpp create mode 100644 services/src/fileoper/utils.h diff --git a/services/BUILD.gn b/services/BUILD.gn index 329c443b..06d18b81 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -48,6 +48,7 @@ ohos_shared_library("fms_server") { "src/fileoper/media_file_oper.cpp", "src/fileoper/media_file_utils.cpp", "src/fileoper/oper_factory.cpp", + "src/fileoper/utils.cpp", "src/server/file_manager_service.cpp", "src/server/file_manager_service_stub.cpp", ] diff --git a/services/include/file_manager_service_errno.h b/services/include/file_manager_service_errno.h index 8ad3940e..41f90c0c 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_INVALID_URI = -6; // invalid uri } // 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 index 85ca21cc..eb1a2171 100644 --- a/services/src/fileoper/external_storage_oper.cpp +++ b/services/src/fileoper/external_storage_oper.cpp @@ -31,14 +31,14 @@ int ExternalStorageOper::OperProcess(uint32_t code, MessageParcel &data, Message int errCode = SUCCESS; switch (code) { case Operation::LIST_FILE: { - std::string path = data.ReadString(); - errCode = this->ListFile(path, reply); + std::string uri = data.ReadString(); + errCode = this->ListFile(uri, reply); break; } case Operation::CREATE_FILE: { - std::string path = data.ReadString(); + std::string uri = data.ReadString(); std::string name = data.ReadString(); - errCode = this->CreateFile(path, name, reply); + errCode = this->CreateFile(uri, name, reply); break; } default: { @@ -49,24 +49,16 @@ int ExternalStorageOper::OperProcess(uint32_t code, MessageParcel &data, Message return errCode; } -int ExternalStorageOper::CreateFile(const std::string &path, const std::string &name, MessageParcel &reply) const +int ExternalStorageOper::CreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) const { DEBUG_LOG("ExternalStorageOper::CreateFile"); - return ExternalStorageUtils::DoCreateFile(path, name); + return ExternalStorageUtils::DoCreateFile(uri, name, reply); } -int ExternalStorageOper::ListFile(const std::string &path, MessageParcel &reply) const +int ExternalStorageOper::ListFile(const std::string &uri, 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; + return ExternalStorageUtils::DoListFile(uri, reply); } } // namespace FileManagerService } // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/external_storage_oper.h b/services/src/fileoper/external_storage_oper.h index f04ccc71..8e685775 100644 --- a/services/src/fileoper/external_storage_oper.h +++ b/services/src/fileoper/external_storage_oper.h @@ -25,8 +25,8 @@ public: 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; + int CreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) const; + int ListFile(const std::string &uri, MessageParcel &reply) const; }; } // namespace FileManagerService } // namespace OHOS diff --git a/services/src/fileoper/external_storage_utils.cpp b/services/src/fileoper/external_storage_utils.cpp index 5d0a6cd4..baeb0206 100644 --- a/services/src/fileoper/external_storage_utils.cpp +++ b/services/src/fileoper/external_storage_utils.cpp @@ -16,6 +16,8 @@ #include "external_storage_utils.h" #include +#include + #include #include #include @@ -25,33 +27,16 @@ #include "file_manager_service_def.h" #include "file_manager_service_errno.h" #include "log.h" +#include "utils.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) +static bool GetFileInfo(const std::string &path, const std::string &name, FileInfo &fileInfo) { + std::string fullPath(""); + fullPath.append(path).append("/").append(name); struct stat st; - if (lstat(path.c_str(), &st) != 0) { + if (lstat(fullPath.c_str(), &st) != 0) { ERR_LOG("check file info fail."); return false; } @@ -63,91 +48,109 @@ static bool GetFileInfo(const unsigned char type, fileInfo.SetSize(st.st_size); fileInfo.SetAddedTime(static_cast(st.st_ctim.tv_sec)); fileInfo.SetModifiedTime(static_cast(st.st_mtim.tv_sec)); + std::string type = GetMimeType(fName); + fileInfo.SetType(type); + return true; +} - 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); +bool ConvertUriToAbsolutePath(const std::string &uri, std::string &path) +{ + // TODO convert uri to absolute path + path = ""; + path.append(uri); return true; } -int ExternalStorageUtils::DoListFile(const std::string &path, std::vector &fileList) +int ExternalStorageUtils::DoListFile(const std::string &uri, MessageParcel &reply) { DEBUG_LOG("ExternalStorageUtils::DoListFile"); + + std::string path; + if (!ConvertUriToAbsolutePath(uri, path)) { + ERR_LOG("invalid uri[%{public}s].", uri.c_str()); + return E_INVALID_URI; + } + DIR *dir = opendir(path.c_str()); if (!dir) { ERR_LOG("opendir path[%{public}s] fail.", path.c_str()); return E_NOEXIST; } + std::vector fileList; for (struct dirent *ent = readdir(dir); ent != nullptr; ent = readdir(dir)) { if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { continue; } - std::string fullPath(""); - fullPath.append(path); - if (!EndsWith(path, "/")) { - fullPath.append("/"); - } FileInfo fileInfo; - if (!GetFileInfo(ent->d_type, fullPath, ent->d_name, fileInfo)) { + if (!GetFileInfo(path, ent->d_name, fileInfo)) { continue; } fileList.push_back(fileInfo); } closedir(dir); + int64_t count = fileList.size(); + reply.WriteInt64(count); + if (count == 0) { + return E_EMPTYFOLDER; + } + for (auto file : fileList) { + reply.WriteString(file.GetPath()); + reply.WriteString(file.GetName()); + reply.WriteString(file.GetType()); + reply.WriteInt64(file.GetSize()); + reply.WriteInt64(file.GetAddedTime()); + reply.WriteInt64(file.GetModifiedTime()); + } return SUCCESS; } -int ExternalStorageUtils::DoCreateFile(const std::string &path, const std::string &name) +int ExternalStorageUtils::DoCreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) { DEBUG_LOG("ExternalStorageUtils::DoCreateFile"); - std::string fullPath(""); - fullPath.append(path); - if (!EndsWith(path, "/")) { - fullPath.append("/"); + std::string path; + if (!ConvertUriToAbsolutePath(uri, path)) { + ERR_LOG("invalid uri[%{public}s].", uri.c_str()); + return E_INVALID_URI; } - fullPath.append(name); - if (access(fullPath.c_str(), F_OK) == 0) { - ERR_LOG("target file[%{public}s] exist.", fullPath.c_str()); + path.append("/").append(name); + if (access(path.c_str(), F_OK) == 0) { + ERR_LOG("target file[%{public}s] exist.", path.c_str()); return E_CREATE_FAIL; } - int fd = open(fullPath.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0771); + int fd = open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0771); if (fd == -1) { - ERR_LOG("create file[%{public}s] fail.", fullPath.c_str()); + ERR_LOG("create file[%{public}s] fail.", path.c_str()); return E_CREATE_FAIL; } close(fd); + + reply.WriteString(uri); return SUCCESS; } + +bool ExternalStorageUtils::PopFileInfo(FileInfo &fileInfo, MessageParcel &reply) +{ + std::string path; + std::string name; + std::string type; + int64_t size = 0; + int64_t at = 0; + int64_t mt = 0; + + reply.ReadString(path); + reply.ReadString(name); + reply.ReadString(type); + reply.ReadInt64(size); + reply.ReadInt64(at); + reply.ReadInt64(mt); + fileInfo = FileInfo(name, path, type); + fileInfo.SetSize(size); + fileInfo.SetAddedTime(at); + fileInfo.SetModifiedTime(mt); + return true; +} } // namespace FileManagerService } // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/external_storage_utils.h b/services/src/fileoper/external_storage_utils.h index ef88f4aa..448cef50 100644 --- a/services/src/fileoper/external_storage_utils.h +++ b/services/src/fileoper/external_storage_utils.h @@ -27,8 +27,10 @@ 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); + static int DoListFile(const std::string &uri, MessageParcel &reply); + static int DoCreateFile(const std::string &uri, const std::string &name, MessageParcel &reply); + static bool PushFileInfo(std::vector &fileList, MessageParcel &reply); + static bool PopFileInfo(FileInfo &fileInfo, MessageParcel &reply); }; } // namespace FileManagerService } // namespace OHOS diff --git a/services/src/fileoper/media_file_utils.cpp b/services/src/fileoper/media_file_utils.cpp index 600ee20c..28f93efa 100644 --- a/services/src/fileoper/media_file_utils.cpp +++ b/services/src/fileoper/media_file_utils.cpp @@ -24,6 +24,7 @@ #include "media_data_ability_const.h" #include "medialibrary_data_ability.h" #include "log.h" +#include "utils.h" using namespace std; namespace OHOS { @@ -83,26 +84,6 @@ bool GetPathID(const string &uriPath, string &index) return true; } -int GetMediaType(const string &name) -{ - int mediaType = Media::MediaAsset::GetMediaType(name); - if (FILE_MIME_TYPE_MAPS.count(mediaType) == 0) { - ERR_LOG("invalid mediaType %{public}d", mediaType); - return FILE_MEDIA_TYPE; - } - return mediaType; -} - -string GetMimeType(const string &name) -{ - int mediaType = GetMediaType(name); - if (FILE_MIME_TYPE_MAPS.count(mediaType) == 0) { - ERR_LOG("invalid mediaType %{public}d", mediaType); - return FILE_MIME_TYPE; - } - return FILE_MIME_TYPE_MAPS.at(mediaType); -} - bool GetPathFromAlbumPath(const string &albumUri, string &path) { string id; diff --git a/services/src/fileoper/utils.cpp b/services/src/fileoper/utils.cpp new file mode 100644 index 00000000..8dfe0a9e --- /dev/null +++ b/services/src/fileoper/utils.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "utils.h" + +#include + +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" +#include "log.h" +#include "media_asset.h" + +namespace OHOS { +namespace FileManagerService { +int GetMediaType(const std::string &name) +{ + int mediaType = Media::MediaAsset::GetMediaType(name); + if (FILE_MIME_TYPE_MAPS.count(mediaType) == 0) { + ERR_LOG("invalid mediaType %{public}d", mediaType); + return FILE_MEDIA_TYPE; + } + return mediaType; +} + +std::string GetMimeType(const std::string &name) +{ + int mediaType = GetMediaType(name); + if (FILE_MIME_TYPE_MAPS.count(mediaType) == 0) { + ERR_LOG("invalid mediaType %{public}d", mediaType); + return FILE_MIME_TYPE; + } + return FILE_MIME_TYPE_MAPS.at(mediaType); +} +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/utils.h b/services/src/fileoper/utils.h new file mode 100644 index 00000000..8ca0b13b --- /dev/null +++ b/services/src/fileoper/utils.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef STORAGE_SERIVCES_FILE_OPER_UTILS_H +#define STORAGE_SERIVCES_FILE_OPER_UTILS_H + +#include + +namespace OHOS { +namespace FileManagerService { +int GetMediaType(const std::string &name); +std::string GetMimeType(const std::string &name); +} // namespace FileManagerService +} // namespace OHOS +#endif // STORAGE_SERIVCES_FILE_OPER_UTILS_H \ No newline at end of file -- Gitee