From f23530ce57cb91ed84b1a3974d180bb5dc66799b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E8=BF=AA?= Date: Mon, 30 May 2022 21:47:27 +0800 Subject: [PATCH] add getUserStorageStats interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文迪 --- bundle.json | 3 +- services/storage_manager/BUILD.gn | 17 +++++ .../src/storage_statistics_n_exporter.cpp | 38 +++++++--- .../storage/src/storage_status_service.cpp | 75 ++++++++++++++++++- services/storage_manager/storage_manager.cfg | 4 +- storage_service_aafwk.gni | 5 +- 6 files changed, 125 insertions(+), 17 deletions(-) diff --git a/bundle.json b/bundle.json index 577e7fdf..e1981810 100644 --- a/bundle.json +++ b/bundle.json @@ -14,7 +14,8 @@ "subsystem": "filemanagement", "syscap": [], "features": [ - "storage_service_fstools" + "storage_service_fstools", + "storage_service_graphic" ], "adapted_system_type": ["standard"], "rom": "", diff --git a/services/storage_manager/BUILD.gn b/services/storage_manager/BUILD.gn index 0c93066c..0d6cc27c 100644 --- a/services/storage_manager/BUILD.gn +++ b/services/storage_manager/BUILD.gn @@ -13,6 +13,7 @@ import("//build/ohos.gni") import("//build/ohos/sa_profile/sa_profile.gni") +import("//foundation/filemanagement/storage_service/storage_service_aafwk.gni") config("storage_manager_config") { include_dirs = [ @@ -23,6 +24,7 @@ config("storage_manager_config") { "../storage_daemon/include", "//foundation/filemanagement/storage_service/services/common/include", "//foundation/bundlemanager/bundle_framework/services/bundlemgr/include", + "//foundation/multimedia/medialibrary_standard/interfaces/inner_api/media_library_helper/include", ] } @@ -51,18 +53,33 @@ ohos_shared_library("storage_manager") { deps = [ "../../interfaces/innerkits/storage_manager/native:storage_manager_sa_proxy", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager:dataobs_manager", + "//foundation/bundlemanager/bundle_framework/common:libappexecfwk_common", "//foundation/bundlemanager/bundle_framework/services/bundlemgr:libbms", + "//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/data_share:datashare_abilitykit", "//utils/native/base:utils", ] + if (storage_service_graphic) { + defines += [ "STORAGE_SERVICE_GRAPHIC" ] + deps += [ + "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/media_library_helper:media_library", + "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/media_library_manager:media_library_manager", + ] + } + external_deps = [ "ability_base:base", "ability_base:want", "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "common_event_service:cesfwk_innerkits", + "dataability:native_dataability", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", ] diff --git a/services/storage_manager/kits_impl/src/storage_statistics_n_exporter.cpp b/services/storage_manager/kits_impl/src/storage_statistics_n_exporter.cpp index 8d5055bd..2d4b367c 100644 --- a/services/storage_manager/kits_impl/src/storage_statistics_n_exporter.cpp +++ b/services/storage_manager/kits_impl/src/storage_statistics_n_exporter.cpp @@ -227,22 +227,32 @@ napi_value GetSystemSize(napi_env env, napi_callback_info info) napi_value GetUserStorageStats(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs((int)NARG_CNT::ONE, (int)NARG_CNT::TWO)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched 1-2"); + if (!funcArg.InitArgs((int)NARG_CNT::ZERO, (int)NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched 0-2"); return nullptr; } - - bool succ = false; - int64_t userId; - std::tie(succ, userId) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToInt64(); - if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid userId"); - return nullptr; + bool fac = false; + int64_t userId = -1; + if (funcArg.GetArgc() >= 1) { + NVal ui(env, NVal(env, funcArg[(int)NARG_POS::FIRST]).val_); + if (ui.TypeIs(napi_number)) { + bool succ = false; + std::tie(succ, userId) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToInt64(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid userId"); + return nullptr; + } + fac = true; + } } auto storageStats = std::make_shared(); - auto cbExec = [userId, storageStats](napi_env env) -> UniError { - *storageStats = DelayedSingleton::GetInstance()->GetUserStorageStats(userId); + auto cbExec = [fac, userId, storageStats](napi_env env) -> UniError { + if (!fac) { + *storageStats = DelayedSingleton::GetInstance()->GetUserStorageStats(); + } else { + *storageStats = DelayedSingleton::GetInstance()->GetUserStorageStats(userId); + } return UniError(ERRNO_NOERR); }; auto cbComplete = [storageStats](napi_env env, UniError err) -> NVal { @@ -260,9 +270,13 @@ napi_value GetUserStorageStats(napi_env env, napi_callback_info info) }; std::string procedureName = "GetUserStorageStats"; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == (uint)NARG_CNT::ONE) { + if (funcArg.GetArgc() == (uint)NARG_CNT::ZERO || (funcArg.GetArgc() == (uint)NARG_CNT::ONE && fac)) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } else { + if (!fac) { + NVal cb(env, funcArg[(int)NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } NVal cb(env, funcArg[(int)NARG_POS::SECOND]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } diff --git a/services/storage_manager/storage/src/storage_status_service.cpp b/services/storage_manager/storage/src/storage_status_service.cpp index 7bc4ed47..6339d3c3 100644 --- a/services/storage_manager/storage/src/storage_status_service.cpp +++ b/services/storage_manager/storage/src/storage_status_service.cpp @@ -20,7 +20,16 @@ #include "storage_service_constant.h" #include "storage_service_errno.h" #include "storage_service_log.h" +#include "storage/storage_total_status_service.h" #include "installd_client.h" +#include "bundle_mgr_interface.h" +#include "application_info.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#ifdef STORAGE_SERVICE_GRAPHIC +#include "media_library_manager.h" +#include "media_volume.h" +#endif using namespace std; @@ -74,13 +83,75 @@ BundleStats StorageStatusService::GetBundleStats(std::string pkgName) StorageStats StorageStatusService::GetUserStorageStats() { - StorageStats result; - return result; + int userId = GetCurrentUserId(); + return GetUserStorageStats(userId); } StorageStats StorageStatusService::GetUserStorageStats(int32_t userId) { StorageStats result; + // totalSize + int64_t totalSize = 0; + totalSize = DelayedSingleton::GetInstance()->GetTotalSize(); + // appSize + LOGI("StorageStatusService::GetUserStorageStats userId is %{public}d", userId); + auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + LOGE("StorageStatusService::GetUserStorageStats samgr == nullptr"); + return result; + } + sptr remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + LOGE("StorageStatusService::GetUserStorageStats remoteObj == nullptr"); + return result; + } + + auto bundleMgr = iface_cast(remoteObject); + vector appInfos; + bool res = bundleMgr->GetApplicationInfos( + AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, appInfos); + if (!res) { + LOGE("StorageStatusService::GetUserStorageStats an error occured in querying appInfos"); + return result; + } + int64_t appSize = 0; + for (auto appInfo : appInfos) { + int64_t bundleSize = 0; + LOGI("StorageStatusService::GetCurUserStorageStats pkgname is %{public}s", appInfo.name.c_str()); + vector bundleStats; + int errorcode = AppExecFwk::InstalldClient::GetInstance()->GetBundleStats(appInfo.name, userId, bundleStats); + if (bundleStats.size() != dataDir.size() || errorcode != E_OK) { + LOGE("StorageStatusService::An error occurred in querying bundle stats."); + return result; + } + for (uint i = 0; i < bundleStats.size(); i++) { + bundleSize += bundleStats[i]; + } + appSize += bundleSize; + } + // mediaSize +#ifdef STORAGE_SERVICE_GRAPHIC + Media::MediaLibraryManager mgr; + Media::MediaVolume mediaVol; + auto remoteObj = sam->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID); + if (remoteObj == nullptr) { + LOGE("StorageStatusService::GetUserStorageStats remoteObj == nullptr"); + return result; + } + mgr.InitMediaLibraryManager(remoteObj); + if (mgr.QueryTotalSize(mediaVol)) { + LOGE("StorageStatusService::GetUserStorageStats an error occured in querying mediaSize"); + return result; + } +#endif + result.total_ = totalSize; + result.app_ = appSize; +#ifdef STORAGE_SERVICE_GRAPHIC + result.audio_ = mediaVol.GetAudiosSize(); + result.video_ = mediaVol.GetVideosSize(); + result.image_ = mediaVol.GetImagesSize(); + result.file_ = mediaVol.GetFilesSize(); +#endif return result; } diff --git a/services/storage_manager/storage_manager.cfg b/services/storage_manager/storage_manager.cfg index 530cb312..32578bfc 100644 --- a/services/storage_manager/storage_manager.cfg +++ b/services/storage_manager/storage_manager.cfg @@ -5,6 +5,8 @@ "uid": "storage_manager", "gid": ["storage_manager"], "start-mode" : "boot", - "secon" : "u:r:storage_manager:s0" + "secon" : "u:r:storage_manager:s0", + "apl": "system_basic", + "permission": ["ohos.permission.READ_MEDIA"] }] } diff --git a/storage_service_aafwk.gni b/storage_service_aafwk.gni index 0b6286b3..b0119744 100644 --- a/storage_service_aafwk.gni +++ b/storage_service_aafwk.gni @@ -9,6 +9,9 @@ # 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. +# limitations under the License. aafwk_kits_path = "//foundation/aafwk/standard/frameworks/kits" +declare_args() { + storage_service_graphic = true +}