diff --git a/services/BUILD.gn b/services/BUILD.gn index f265421e45e261fc6c3f826a8bd99528d3ef9b76..06d18b816e0131b5bc4a7b146b4df0933e44b38b 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -43,9 +43,12 @@ 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", + "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 8ad3940e16d05c5def3190c1d34f7fd52d1f9e8f..41f90c0c098a0ce0d03784c070bee3e39ac6ff0a 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 new file mode 100644 index 0000000000000000000000000000000000000000..eb1a2171b44f215c632b044722875d7ac8310207 --- /dev/null +++ b/services/src/fileoper/external_storage_oper.cpp @@ -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. + */ + +#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 uri = data.ReadString(); + errCode = this->ListFile(uri, reply); + break; + } + case Operation::CREATE_FILE: { + std::string uri = data.ReadString(); + std::string name = data.ReadString(); + errCode = this->CreateFile(uri, name, reply); + break; + } + default: { + DEBUG_LOG("not valid code %{public}d.", code); + break; + } + } + return errCode; +} + +int ExternalStorageOper::CreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) const +{ + DEBUG_LOG("ExternalStorageOper::CreateFile"); + return ExternalStorageUtils::DoCreateFile(uri, name, reply); +} + +int ExternalStorageOper::ListFile(const std::string &uri, MessageParcel &reply) const +{ + DEBUG_LOG("ExternalStorageOper::ListFile"); + 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 new file mode 100644 index 0000000000000000000000000000000000000000..8e68577542c9db82a29daf2026a579b071ec8bbd --- /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 &uri, const std::string &name, MessageParcel &reply) const; + int ListFile(const std::string &uri, 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 0000000000000000000000000000000000000000..baeb0206811aa184e18d351a4891cacd8bae04de --- /dev/null +++ b/services/src/fileoper/external_storage_utils.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "external_storage_utils.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include "file_manager_service_def.h" +#include "file_manager_service_errno.h" +#include "log.h" +#include "utils.h" + +namespace OHOS { +namespace FileManagerService { +static bool GetFileInfo(const std::string &path, const std::string &name, FileInfo &fileInfo) +{ + std::string fullPath(""); + fullPath.append(path).append("/").append(name); + struct stat st; + if (lstat(fullPath.c_str(), &st) != 0) { + ERR_LOG("check file info fail."); + return false; + } + std::string fPath(path.c_str()); + std::string fName(name.c_str()); + + fileInfo.SetPath(fPath); + fileInfo.SetName(fName); + fileInfo.SetSize(st.st_size); + fileInfo.SetAddedTime(static_cast(st.st_ctim.tv_sec)); + fileInfo.SetModifiedTime(static_cast(st.st_mtim.tv_sec)); + std::string type = GetMimeType(fName); + fileInfo.SetType(type); + return true; +} + +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 &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; + } + FileInfo 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 &uri, const std::string &name, MessageParcel &reply) +{ + DEBUG_LOG("ExternalStorageUtils::DoCreateFile"); + + std::string path; + if (!ConvertUriToAbsolutePath(uri, path)) { + ERR_LOG("invalid uri[%{public}s].", uri.c_str()); + return E_INVALID_URI; + } + + path.append("/").append(name); + if (access(path.c_str(), F_OK) == 0) { + ERR_LOG("target file[%{public}s] exist.", path.c_str()); + return E_CREATE_FAIL; + } + + int fd = open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0771); + if (fd == -1) { + ERR_LOG("create file[%{public}s] fail.", path.c_str()); + return E_CREATE_FAIL; + } + close(fd); + + reply.WriteString(uri); + return SUCCESS; +} + +bool ExternalStorageUtils::PopFileInfo(FileInfo &fileInfo, MessageParcel &reply) +{ + std::string path; + std::string name; + std::string type; + int64_t size = 0; + int64_t at = 0; + int64_t mt = 0; + + reply.ReadString(path); + reply.ReadString(name); + reply.ReadString(type); + reply.ReadInt64(size); + reply.ReadInt64(at); + reply.ReadInt64(mt); + fileInfo = FileInfo(name, path, type); + fileInfo.SetSize(size); + fileInfo.SetAddedTime(at); + fileInfo.SetModifiedTime(mt); + return true; +} +} // namespace FileManagerService +} // namespace OHOS \ No newline at end of file diff --git a/services/src/fileoper/external_storage_utils.h b/services/src/fileoper/external_storage_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..448cef50f85f4c49c357302c08fb083d647b32a4 --- /dev/null +++ b/services/src/fileoper/external_storage_utils.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef STORAGE_SERIVCES_EXTERNAL_STORAGE_UTILS_H +#define STORAGE_SERIVCES_EXTERNAL_STORAGE_UTILS_H + +#include +#include + +#include "file_info.h" +#include "file_oper.h" + +namespace OHOS { +namespace FileManagerService { +class ExternalStorageUtils { +public: + ExternalStorageUtils(); + ~ExternalStorageUtils(); + 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 +#endif // STORAGE_SERIVCES_EXTERNAL_STORAGE_UTILS_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 index 600ee20cdd8b2223ba13fce365d0972e675e1e7e..28f93efa25a8736f691e021d7aabdac69d7665f8 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/oper_factory.cpp b/services/src/fileoper/oper_factory.cpp index 23f25ceda7a6ca854d6f3f7232f111297fcaf897..fa45591b043539b654c28b2e4e5d0fb9798ceb02 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: { diff --git a/services/src/fileoper/utils.cpp b/services/src/fileoper/utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dfe0a9e010f8992ae760a35f4d17f1544bfb73f --- /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 0000000000000000000000000000000000000000..8ca0b13b7a7b438c63d4009a69d696c872152c5e --- /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