• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "storage_manager_inf.h"
17 
18 #include "file_manager_service_def.h"
19 #include "file_manager_service_errno.h"
20 #include "log.h"
21 
22 using namespace std;
23 namespace OHOS {
24 namespace FileManagerService {
GetMountPointFromPath(const string & path,string & mountPoint)25 static bool GetMountPointFromPath(const string &path, string &mountPoint)
26 {
27     size_t len = MOUNT_POINT_ROOT.size();
28     std::string head = path.substr(0, len);
29     std::string body = path.substr(len);
30     if (head != MOUNT_POINT_ROOT || body.size() == 0) {
31         ERR_LOG("invalid mountPoint %{public}s, head check fail", path.c_str());
32         return false;
33     }
34 
35     size_t index = body.find("/");
36     if (index != std::string::npos) {
37         mountPoint = MOUNT_POINT_ROOT + body.substr(0, index);
38     } else {
39         mountPoint = path;
40     }
41     return true;
42 }
43 
Connect()44 int StorageManagerInf::Connect()
45 {
46     DEBUG_LOG("StorageManagerConnect::Connect start");
47     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48     if (sam == nullptr) {
49         ERR_LOG("StorageManagerConnect::Connect samgr == nullptr");
50         return FAIL;
51     }
52     auto object = sam->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
53     if (object == nullptr) {
54         ERR_LOG("StorageManagerConnect::Connect object == nullptr");
55         return FAIL;
56     }
57     storageManager_ = iface_cast<StorageManager::IStorageManager>(object);
58     if (storageManager_ == nullptr) {
59         ERR_LOG("StorageManagerConnect::Connect service == nullptr");
60         return FAIL;
61     }
62     DEBUG_LOG("StorageManagerConnect::Connect end");
63     return SUCCESS;
64 }
65 
GetAllVolumes()66 std::vector<StorageManager::VolumeExternal> StorageManagerInf::GetAllVolumes()
67 {
68     vector<StorageManager::VolumeExternal> result = {};
69     if (Connect() != SUCCESS) {
70         ERR_LOG("GetTotalSizeOfVolume:Connect error");
71         return result;
72     }
73     return storageManager_->GetAllVolumes();
74 }
75 
GetMountedVolumes(vector<string> & vecRootPath)76 bool StorageManagerInf::GetMountedVolumes(vector<string> &vecRootPath)
77 {
78     bool succ = false;
79     vector<StorageManager::VolumeExternal> result = GetAllVolumes();
80     if (result.size() == 0) {
81         ERR_LOG("empty volume result");
82         return succ;
83     }
84     for (auto vol : result) {
85         DEBUG_LOG("find vol.GetPath() %{public}d %{public}d", vol.GetState(), VolumeState::MOUNTED);
86         if (vol.GetState() == VolumeState::MOUNTED) {
87             DEBUG_LOG("mounted find vol.GetPath() %{public}d %{public}d", vol.GetState(), VolumeState::MOUNTED);
88             vecRootPath.emplace_back(EXTERNAL_STORAGE_URI + vol.GetPath());
89             succ = true;
90         }
91     }
92     return succ;
93 }
94 
StoragePathValidCheck(const string & path)95 bool StorageManagerInf::StoragePathValidCheck(const string &path)
96 {
97     string mountPoint;
98     if (!GetMountPointFromPath(path, mountPoint)) {
99         ERR_LOG("uri path is invalid");
100         return false;
101     }
102     vector<StorageManager::VolumeExternal> result = GetAllVolumes();
103     if (result.size() == 0) {
104         ERR_LOG("empty volume result");
105         return false;
106     }
107     bool succ = false;
108     for (auto vol : result) {
109         if (vol.GetPath() ==  mountPoint && vol.GetState() == VolumeState::MOUNTED) {
110             succ = true;
111             break;
112         }
113     }
114     return succ;
115 }
116 } // FileManagerService
117 } // OHOS
118