diff --git a/services/BUILD.gn b/services/BUILD.gn index 78366209e0d768b06d58cd2cead640a6aff7df52..1a330bfb83122f4fba7b48b735a9f389a43f92f5 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -62,7 +62,6 @@ ohos_shared_library("fms_server") { "//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/aafwk/standard/interfaces/innerkits/base:base", - "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager:dataobs_manager", "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", "//foundation/filemanagement/storage_service/interfaces/innerkits/storage_manager/native:storage_manager_sa_proxy", "//foundation/filemanagement/storage_service/services/storage_manager:storage_manager", diff --git a/services/include/file_manager_service_def.h b/services/include/file_manager_service_def.h index 9b49f39dea543e96db1390f019e8b0ce493ce472..6992a138120d52d2566b6cc00d0ecee9f98c0beb 100644 --- a/services/include/file_manager_service_def.h +++ b/services/include/file_manager_service_def.h @@ -46,7 +46,10 @@ 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"; +// use to find out album in the root dir +const std::string RELATIVE_ROOT_PATH = ""; + +const std::string MEDIA_ROOT_PATH = "/storage/media/local/files"; const std::string IMAGE_ROOT_NAME = "image_album"; const std::string VIDEO_ROOT_NAME = "video_album"; const std::string AUDIO_ROOT_NAME = "audio_album"; @@ -71,10 +74,10 @@ const std::unordered_map FILE_MIME_TYPE_MAPS = { }; 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"}, + {Media::MediaType::MEDIA_TYPE_IMAGE, "image/"}, + {Media::MediaType::MEDIA_TYPE_AUDIO, "audio/"}, + {Media::MediaType::MEDIA_TYPE_VIDEO, "video/"}, + {Media::MediaType::MEDIA_TYPE_FILE, "document/"}, }; const std::unordered_map MEDIA_TYPE_URI_MAPS = { @@ -83,6 +86,14 @@ const std::unordered_map MEDIA_TYPE_URI_MAPS = { {Media::MediaType::MEDIA_TYPE_VIDEO, Media::MEDIALIBRARY_VIDEO_URI}, {Media::MediaType::MEDIA_TYPE_FILE, Media::MEDIALIBRARY_FILE_URI}, }; + +#define GET_COLUMN_INDEX_FROM_NAME(result, name, index) \ + do { \ + if (result->GetColumnIndex(name, index) != NativeRdb::E_OK) { \ + ERR_LOG("NativeRdb gets %{public}s index fail", name.c_str()); \ + return false; \ + } \ + } while (0) } // namespace FileManagerService } // namespace OHOS #endif // STORAGE_FILE_MANAGER_SERVICE_DEF_H diff --git a/services/src/fileoper/media_file_utils.cpp b/services/src/fileoper/media_file_utils.cpp index bf30808a928c336652603c5941b22db7b037aba7..5fbcba7406d3b3bdc156099484f2f9e0349f7671 100644 --- a/services/src/fileoper/media_file_utils.cpp +++ b/services/src/fileoper/media_file_utils.cpp @@ -17,13 +17,14 @@ #include +#include "data_ability_predicates.h" #include "file_manager_service_def.h" #include "file_manager_service_errno.h" - +#include "log.h" #include "media_asset.h" #include "media_data_ability_const.h" -#include "medialibrary_data_ability.h" -#include "log.h" +#include "rdb_errno.h" +#include "values_bucket.h" using namespace std; namespace OHOS { @@ -36,18 +37,26 @@ bool GetPathFromResult(shared_ptr result, string 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; - } + int32_t columnIndex = 0; + GET_COLUMN_INDEX_FROM_NAME(result, Media::MEDIA_DATA_DB_FILE_PATH, columnIndex); result->GoToFirstRow(); - ret = result->GetString(columnIndex, path); + int ret = result->GetString(columnIndex, path); if (ret != NativeRdb::E_OK) { ERR_LOG("NativeRdb gets path index fail"); return false; } + GET_COLUMN_INDEX_FROM_NAME(result, Media::MEDIA_DATA_DB_RELATIVE_PATH, columnIndex); + string relativePath; + ret = result->GetString(columnIndex, relativePath); + if (ret != NativeRdb::E_OK) { + relativePath = ""; + DEBUG_LOG("NativeRdb gets relative path is null %{public}d", columnIndex); + } + // get relative path from absolute path + string::size_type pos = path.find_last_of('/'); + if (pos != string::npos) { + path = relativePath + path.substr(pos + 1) + "/"; + } return true; } @@ -147,21 +156,17 @@ bool GetAlbumFromResult(shared_ptr &result, vecto 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; - } - + GET_COLUMN_INDEX_FROM_NAME(result, Media::MEDIA_DATA_DB_RELATIVE_PATH, columnIndex); for (int i = 0; i < count; i++) { string path; - ret = result->GetString(columnIndex, path); - if (ret != NativeRdb::E_OK) { + if (result->GetString(columnIndex, path) != NativeRdb::E_OK) { ERR_LOG("NativeRdb gets path columnIndex fail"); return false; } - auto it = find(album.begin(), album.end(), path); - if (it == album.end()) { + path = MEDIA_ROOT_PATH + "/" + path.substr(0, path.size() - 1); + // add into ablum if not in album + if (find(album.begin(), album.end(), path) == album.end()) { + DEBUG_LOG(" // add into ablum path %{public}s", path.c_str()); album.emplace_back(path); } result->GoToNextRow(); @@ -210,7 +215,7 @@ int CreateSelectionAndArgsFirstLevel(const string &type, string &selection, vect 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), + RELATIVE_ROOT_PATH, ToString(Media::MediaType::MEDIA_TYPE_FILE), ToString(Media::MediaType::MEDIA_TYPE_ALBUM) }; } else { @@ -259,13 +264,21 @@ bool GetAlbumPath(const string &name, const string &path, string &albumPath) return GetPathFromAlbumPath(path, albumPath); } +static void ShowSelecArgs(const string &selection, const vector &selectionArgs) +{ + DEBUG_LOG("selection %{public}s ", selection.c_str()); + for (auto s : selectionArgs) { + DEBUG_LOG("selectionArgs %{public}s", s.c_str()); + } +} + 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 "); + DEBUG_LOG("IsFirstLevelUriPath"); CreateSelectionAndArgsFirstLevel(type, selection, selectionArgs); } else { int err = CreateSelectionAndArgsOtherLevel(type, path, selection, selectionArgs); @@ -282,44 +295,43 @@ int MediaFileUtils::DoListFile(const string &type, const string &path, int offse return SUCCESS; } -shared_ptr MediaFileUtils::DoQuery(string selection, vector selectionArgs) +shared_ptr MediaFileUtils::DoQuery(const string &selection, + const vector &selectionArgs) { + ShowSelecArgs(selection, 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); + return abilityHelper->Query(uri, columns, predicates); } int MediaFileUtils::DoInsert(const string &name, const string &path, const string &type, string &uri) { - Media::ValuesBucket values; - + NativeRdb::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_RELATIVE_PATH, albumPath); + values.PutString(Media::MEDIA_DATA_DB_NAME, 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); + int index = abilityHelper->Insert(createAsset, values); if (index < 0) { - ERR_LOG("Fail to create fail %{public}s %{public}s", name.c_str(), path.c_str()); + ERR_LOG("Fail to create fail file %{public}s uri %{public}s album %{public}s", name.c_str(), + path.c_str(), albumPath.c_str()); return E_CREATE_FAIL; } + // use file id concatenate head as uri 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; } @@ -338,11 +350,7 @@ bool MediaFileUtils::InitMediaTableColIndexMap(shared_ptrGetColumnIndex(i.first, columnIndex); - if (ret != NativeRdb::E_OK) { - ERR_LOG("NativeRdb returns fail"); - return false; - } + GET_COLUMN_INDEX_FROM_NAME(result, i.first, columnIndex); mediaTableMap.emplace_back(columnIndex, i.second); } } @@ -380,28 +388,6 @@ bool MediaFileUtils::PushFileInfo(shared_ptr resu 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) { @@ -425,5 +411,17 @@ int MediaFileUtils::GetFileInfoFromResult(shared_ptr obj) +{ + if (abilityHelper == nullptr) { + abilityHelper = AppExecFwk::DataAbilityHelper::Creator(obj, make_shared(Media::MEDIALIBRARY_DATA_URI)); + if (abilityHelper == nullptr) { + DEBUG_LOG("get %{public}s helper fail", Media::MEDIALIBRARY_DATA_URI.c_str()); + return false; + } + } + return true; +} } // 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 index 371eb5356c69e5efcf8ec4d5113307367bc19cbb..56ceff40d38cf582596d676d3ed9f247332f49d1 100644 --- a/services/src/fileoper/media_file_utils.h +++ b/services/src/fileoper/media_file_utils.h @@ -22,6 +22,13 @@ #include "file_info.h" #include "file_oper.h" +#include "ipc_types.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" + +#include "data_ability_helper.h" + namespace OHOS { namespace FileManagerService { class MediaFileUtils { @@ -30,16 +37,18 @@ public: ~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 std::shared_ptr DoQuery(const std::string &selection, + const 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); + static bool InitHelper(sptr obj); private: inline static std::vector> mediaTableMap = {}; + inline static std::shared_ptr abilityHelper = nullptr; }; } // namespace FileManagerService } // namespace OHOS diff --git a/services/src/server/file_manager_service_stub.cpp b/services/src/server/file_manager_service_stub.cpp index a11c8371d9f01e1a2abe9e7874258777bb61a4a8..f3c5d21e4b99295d5a1dedeb86e740e60259cb1b 100644 --- a/services/src/server/file_manager_service_stub.cpp +++ b/services/src/server/file_manager_service_stub.cpp @@ -15,22 +15,23 @@ #include "file_manager_service_stub.h" -#include "file_manager_service.h" #include "file_manager_service_def.h" #include "file_manager_service_errno.h" +#include "file_manager_service.h" #include "log.h" +#include "media_file_utils.h" #include "oper_factory.h" using namespace std; namespace OHOS { namespace FileManagerService { -int GetEquipmentCode(uint32_t code) +static int GetEquipmentCode(uint32_t code) { return (code >> EQUIPMENT_SHIFT) & CODE_MASK; } -int GetOperCode(uint32_t code) +static int GetOperCode(uint32_t code) { return code & CODE_MASK; } @@ -54,6 +55,11 @@ int FileManagerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { // to do checkpermission() + if (!MediaFileUtils::InitHelper(AsObject())) { + ERR_LOG("InitHelper error %{public}d", FAIL); + reply.WriteInt32(FAIL); + return FAIL; + } // do file process int32_t errCode = OperProcess(code, data, reply); reply.WriteInt32(errCode);