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 "external_storage_utils.h"
17
18 #include <cerrno>
19 #include <cstring>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <securec.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <unordered_map>
26
27 #include "file_manager_service_def.h"
28 #include "file_manager_service_errno.h"
29 #include "log.h"
30 #include "storage_manager_inf.h"
31 using namespace std;
32 using namespace OHOS::StorageManager;
33 namespace OHOS {
34 namespace FileManagerService {
GetPathFromUri(const string & uri,string & path)35 static bool GetPathFromUri(const string &uri, string &path)
36 {
37 uint len = EXTERNAL_STORAGE_URI.size();
38 string head = uri.substr(0, len);
39 if (head != EXTERNAL_STORAGE_URI) {
40 ERR_LOG("invalid format uri %{private}s, head check fail", uri.c_str());
41 return false;
42 }
43 if (uri.size() == len) {
44 ERR_LOG("uri content is invalid %{private}s", uri.c_str());
45 return false;
46 }
47 path = uri.substr(len);
48 return true;
49 }
50
GetRealPath(string & path)51 static bool GetRealPath(string &path)
52 {
53 char filePath[PATH_MAX + 1] = { 0 };
54 if (realpath(path.c_str(), filePath) == nullptr) {
55 ERR_LOG("untrustPath invalid %{public}d\n", errno);
56 return false;
57 }
58 path = string(filePath);
59 return true;
60 }
61
GetFileInfo(const std::string & path,const std::string & name,shared_ptr<FileInfo> & fileInfo)62 static bool GetFileInfo(const std::string &path, const std::string &name, shared_ptr<FileInfo> &fileInfo)
63 {
64 std::string fullPath(path);
65 size_t len = fullPath.size();
66 if (fullPath.at(len - 1) != '/') {
67 fullPath.append("/").append(name);
68 } else {
69 fullPath.append(name);
70 }
71 struct stat st;
72 if (lstat(fullPath.c_str(), &st) != 0) {
73 ERR_LOG("check file info fail.");
74 return false;
75 }
76 std::string uri(EXTERNAL_STORAGE_URI);
77 std::string fName(name.c_str());
78
79 uri.append(fullPath);
80 fileInfo->SetPath(uri);
81 std::string type = S_ISDIR(st.st_mode) ? "album" : "file";
82 fileInfo->SetType(type);
83 fileInfo->SetName(fName);
84 fileInfo->SetSize(st.st_size);
85 fileInfo->SetAddedTime(static_cast<long>(st.st_ctim.tv_sec));
86 fileInfo->SetModifiedTime(static_cast<long>(st.st_mtim.tv_sec));
87 return true;
88 }
89
ConvertUriToAbsolutePath(const std::string & uri,std::string & path)90 static bool ConvertUriToAbsolutePath(const std::string &uri, std::string &path)
91 {
92 if (!GetPathFromUri(uri, path)) {
93 ERR_LOG("GetPathFromUri fail");
94 return false;
95 }
96 if (!GetRealPath(path)) {
97 ERR_LOG("get real path fail");
98 return false;
99 }
100 if (!StorageManagerInf::StoragePathValidCheck(path)) {
101 ERR_LOG("external uri path was ejected");
102 return false;
103 }
104 return true;
105 }
106
DoListFile(const std::string & type,const std::string & uri,const CmdOptions & option,std::vector<shared_ptr<FileInfo>> & fileList)107 int ExternalStorageUtils::DoListFile(const std::string &type, const std::string &uri, const CmdOptions &option,
108 std::vector<shared_ptr<FileInfo>> &fileList)
109 {
110 int64_t count = option.GetCount();
111 int64_t offset = option.GetOffset();
112 if (count < 0 || count > MAX_NUM || offset < 0) {
113 ERR_LOG("invalid file count or offset.");
114 return E_INVALID_FILE_NUMBER;
115 }
116 DEBUG_LOG("limit %{public}lld, offset %{public}lld", count, offset);
117 std::string path;
118 if (!ConvertUriToAbsolutePath(uri, path)) {
119 ERR_LOG("invalid uri[%{private}s].", uri.c_str());
120 return E_NOEXIST;
121 }
122
123 DIR *dir = opendir(path.c_str());
124 if (!dir) {
125 ERR_LOG("opendir path[%{private}s] fail.", path.c_str());
126 return E_NOEXIST;
127 }
128 if (offset != 0) {
129 int64_t index = 0;
130 for (dirent *ent = readdir(dir); ent != nullptr; ent = readdir(dir)) {
131 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
132 continue;
133 }
134 if (index == offset - 1) {
135 break;
136 }
137 index++;
138 }
139 }
140 for (dirent *ent = readdir(dir); ent != nullptr; ent = readdir(dir)) {
141 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
142 continue;
143 }
144 if (count > 0) {
145 shared_ptr<FileInfo> fileInfo = make_shared<FileInfo>();
146 if (!GetFileInfo(path, ent->d_name, fileInfo)) {
147 continue;
148 }
149 fileList.push_back(fileInfo);
150 count--;
151 if (count == 0) {
152 break;
153 }
154 }
155 }
156 closedir(dir);
157 if (option.GetCount() == MAX_NUM && count == 0) {
158 DEBUG_LOG("get files with MAX_NUM:[%{public}lld].", MAX_NUM);
159 }
160 return SUCCESS;
161 }
162
DoCreateFile(const std::string & uri,const std::string & name,std::string & resultUri)163 int ExternalStorageUtils::DoCreateFile(const std::string &uri, const std::string &name, std::string &resultUri)
164 {
165 std::string path;
166 if (!ConvertUriToAbsolutePath(uri, path)) {
167 ERR_LOG("invalid uri[%{public}s].", uri.c_str());
168 return E_NOEXIST;
169 }
170 size_t len = path.size();
171 if (path.at(len - 1) != '/') {
172 path.append("/").append(name);
173 } else {
174 path.append(name);
175 }
176 if (access(path.c_str(), F_OK) == 0) {
177 ERR_LOG("target file[%{public}s] exist.", path.c_str());
178 return E_CREATE_FAIL;
179 }
180
181 int fd = open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0771);
182 if (fd == -1) {
183 ERR_LOG("create file[%{public}s] fail.", path.c_str());
184 return E_CREATE_FAIL;
185 }
186 close(fd);
187 resultUri = EXTERNAL_STORAGE_URI + path;
188 return SUCCESS;
189 }
190
DoGetRoot(const std::string & name,const std::string & path,std::vector<shared_ptr<FileInfo>> & fileList)191 int ExternalStorageUtils::DoGetRoot(const std::string &name, const std::string &path,
192 std::vector<shared_ptr<FileInfo>> &fileList)
193 {
194 vector<string> vecRootPath;
195 if (!StorageManagerInf::GetMountedVolumes(vecRootPath)) {
196 ERR_LOG("none valid extorage storage");
197 return FAIL;
198 }
199 for (auto rootPath : vecRootPath) {
200 shared_ptr<FileInfo> fileInfo = make_shared<FileInfo>(FILE_ROOT_NAME, rootPath, ALBUM_TYPE);
201 fileList.push_back(fileInfo);
202 }
203 return SUCCESS;
204 }
205 } // namespace FileManagerService
206 } // namespace OHOS