diff --git a/services/src/fileoper/ext_storage/ext_storage_subscriber.cpp b/services/src/fileoper/ext_storage/ext_storage_subscriber.cpp index 410864dd94eaa48c62225d4be7215ee88193651c..db69769ba66d9232d1491d9196ff725401ef2d78 100644 --- a/services/src/fileoper/ext_storage/ext_storage_subscriber.cpp +++ b/services/src/fileoper/ext_storage/ext_storage_subscriber.cpp @@ -21,11 +21,13 @@ #include "bundle_info.h" #include "common_event_manager.h" #include "common_event_support.h" +#include "int_wrapper.h" #include "log.h" #include "string_wrapper.h" -#include "int_wrapper.h" +#include "storage_manager_inf.h" #include "want.h" + using namespace OHOS::AAFwk; namespace OHOS { namespace FileManagerService { @@ -42,12 +44,15 @@ bool ExtStorageSubscriber::Subscriber(void) { if (ExtStorageSubscriber_ == nullptr) { EventFwk::MatchingSkills matchingSkills; - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_DISK_UNMOUNTED); - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_DISK_MOUNTED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_VOLUME_MOUNTED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_VOLUME_UNMOUNTED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_VOLUME_EJECT); EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); ExtStorageSubscriber_ = std::make_shared(subscribeInfo); EventFwk::CommonEventManager::SubscribeCommonEvent(ExtStorageSubscriber_); + + ExtStorageSubscriber_->CheckAndUpdateData(); } return true; } @@ -68,29 +73,73 @@ void ExtStorageSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &event DEBUG_LOG("%{public}s, id:%{public}s.", __func__, id.c_str()); DEBUG_LOG("%{public}s, diskId:%{public}s.", __func__, diskId.c_str()); - if (action == EventFwk::CommonEventSupport::COMMON_EVENT_DISK_MOUNTED) { - int32_t volumeState = AAFwk::Integer::Unbox(AAFwk::IInteger::Query(wantParams.GetParam("volumeState"))); - std::string fsUuid = AAFwk::String::Unbox(AAFwk::IString::Query(wantParams.GetParam("fsUuid"))); - std::string path = AAFwk::String::Unbox(AAFwk::IString::Query(wantParams.GetParam("path"))); - DEBUG_LOG("%{public}s, volumeState:%{public}d.", __func__, volumeState); - DEBUG_LOG("%{public}s, id:%{public}s, fsUuid:%{public}s, path:%{public}s.", - __func__, id.c_str(), fsUuid.c_str(), path.c_str()); - - ExtStorageStatus extStatus(id, diskId, fsUuid, path, VolumeState(volumeState)); - mountStatus.insert(std::pair(path, extStatus)); - } + CheckAndUpdateData(); } bool ExtStorageSubscriber::CheckMountPoint(const std::string &path) { - auto extStorageStatus = mountStatus.find(path); - if (extStorageStatus == mountStatus.end()) { + if (ExtStorageSubscriber_ == nullptr) { + ExtStorageSubscriber::Subscriber(); + } + + auto extStorageStatus = ExtStorageSubscriber_->mountStatus_.find(path); + if (extStorageStatus == ExtStorageSubscriber_->mountStatus_.end()) { return false; } else { if (extStorageStatus->second.GetVolumeState() == VolumeState::MOUNTED) { return true; } - return false; + } + return false; +} + +void ExtStorageSubscriber::CheckAndUpdateData() +{ + std::vector volVec = StorageManagerInf::GetAllVolumes(); + // Clean up redundant data in map + auto iter = mountStatus_.begin(); + while (iter != mountStatus_.end()) { + bool isExist = false; + for (auto volStatus : volVec) { + std::string path = volStatus.GetPath(); + if (iter->first == path) { + isExist = true; + DEBUG_LOG("%{public}s, path in map, path:%{public}s.", __func__, path.c_str()); + break; + } + } + if (isExist == false) { + mountStatus_.erase(iter++); + DEBUG_LOG("%{public}s, erase map, path:%{public}s.", __func__, iter->first.c_str()); + } else { + iter++; + } + } + // update map data + for (auto volStatus : volVec) { + std::string path = volStatus.GetPath(); + if (!path.empty()) { + // Update the data if it already exists in the map; No new data to insert + auto iter = mountStatus_.find(path); + if (iter == mountStatus_.end()) { + ExtStorageStatus extStatus(volStatus.GetId(), volStatus.GetDiskId(), volStatus.GetUuid(), + path, VolumeState(volStatus.GetState())); + mountStatus_.insert(std::pair(path, extStatus)); + DEBUG_LOG("%{public}s, insert map, id:%{public}s.", __func__, volStatus.GetId().c_str()); + DEBUG_LOG("%{public}s, insert map, diskid:%{public}s.", __func__, volStatus.GetDiskId().c_str()); + DEBUG_LOG("%{public}s, insert map, uuid:%{public}s.", __func__, volStatus.GetUuid().c_str()); + DEBUG_LOG("%{public}s, insert map, path:%{public}s.", __func__, path.c_str()); + DEBUG_LOG("%{public}s, insert map, state:%{public}d.", __func__, volStatus.GetState()); + } else { + iter->second.SetId(volStatus.GetId()); + iter->second.SetDiskId(volStatus.GetDiskId()); + iter->second.SetFsUuid(volStatus.GetUuid()); + iter->second.SetPath(path); + iter->second.SetVolumeState(VolumeState(volStatus.GetState())); + DEBUG_LOG("%{public}s, update data, path:%{public}s, state:%{public}d.", __func__, + path.c_str(), volStatus.GetState()); + } + } } } } // namespace FileManagerService diff --git a/services/src/fileoper/ext_storage/ext_storage_subscriber.h b/services/src/fileoper/ext_storage/ext_storage_subscriber.h index 18a34b779970bb9b509c5aa06372bfea2e18ed16..833766898da0169d38f94b3a4991768cd679a3ce 100644 --- a/services/src/fileoper/ext_storage/ext_storage_subscriber.h +++ b/services/src/fileoper/ext_storage/ext_storage_subscriber.h @@ -43,9 +43,11 @@ public: */ virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; - bool CheckMountPoint(const std::string &path); + static bool CheckMountPoint(const std::string &path); +private: + void CheckAndUpdateData(); - std::unordered_map mountStatus; + std::unordered_map mountStatus_; }; } // namespace FileManagerService } // namespace OHOS diff --git a/services/src/fileoper/ext_storage/storage_manager_inf.cpp b/services/src/fileoper/ext_storage/storage_manager_inf.cpp index fc14f55032f4d634b89bf405ffcd60d53b863fda..2baaf3f85cca7196d54434cde437892c8fe80c72 100644 --- a/services/src/fileoper/ext_storage/storage_manager_inf.cpp +++ b/services/src/fileoper/ext_storage/storage_manager_inf.cpp @@ -15,6 +15,7 @@ #include "storage_manager_inf.h" +#include "ext_storage_subscriber.h" #include "file_manager_service_def.h" #include "file_manager_service_errno.h" #include "log.h" @@ -99,19 +100,7 @@ bool StorageManagerInf::StoragePathValidCheck(const string &path) ERR_LOG("uri path is invalid"); return false; } - vector result = GetAllVolumes(); - if (result.size() == 0) { - ERR_LOG("empty volume result"); - return false; - } - bool succ = false; - for (auto vol : result) { - if (vol.GetPath() == mountPoint && vol.GetState() == VolumeState::MOUNTED) { - succ = true; - break; - } - } - return succ; + return ExtStorageSubscriber::CheckMountPoint(mountPoint); } } // FileManagerService } // OHOS