1 /*
2 * Copyright (c) 2023 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 <cstring>
17 #include <dirent.h>
18 #include <iostream>
19 #include <memory>
20 #include <sstream>
21 #include <string_view>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include "filemgmt_libn.h"
26 #include "hilog_wrapper.h"
27
28 namespace OHOS::FileManagement {
29
30 using namespace FileManagement::LibN;
31 using namespace std;
32
33 constexpr int DIR_DEFAULT_PERM = 0770;
34 constexpr int FILTER_MATCH = 1;
35 constexpr int FILTER_DISMATCH = 0;
36 constexpr int MODE_FORCE_MOVE = 0;
37 constexpr uint64_t TIME_CONVERT_BASE = 1000000000;
38
39 struct NameListArg {
40 struct dirent** namelist = { nullptr };
41 int direntNum = 0;
42 };
43
44 struct StatEntity {
45 uv_stat_t stat_;
46 };
47
StartsWith(const string & str,const string prefix)48 static bool StartsWith(const string& str, const string prefix)
49 {
50 return (str.rfind(prefix, 0) == 0);
51 }
52
Deleter(struct NameListArg * arg)53 static void Deleter(struct NameListArg *arg)
54 {
55 for (int i = 0; i < arg->direntNum; i++) {
56 free((arg->namelist)[i]);
57 (arg->namelist)[i] = nullptr;
58 }
59 free(arg->namelist);
60 }
61
FilterFunc(const struct dirent * filename)62 static int32_t FilterFunc(const struct dirent *filename)
63 {
64 if (string_view(filename->d_name) == "." || string_view(filename->d_name) == "..") {
65 return FILTER_DISMATCH;
66 }
67 return FILTER_MATCH;
68 }
69
fs_req_cleanup(uv_fs_t * req)70 static void fs_req_cleanup(uv_fs_t* req)
71 {
72 uv_fs_req_cleanup(req);
73 if (req) {
74 delete req;
75 req = nullptr;
76 }
77 }
78
CheckFsStatByPath(const string & path,uv_fs_t * req)79 static int CheckFsStatByPath(const string &path, uv_fs_t* req)
80 {
81 int ret = uv_fs_stat(nullptr, req, path.c_str(), nullptr);
82 if (ret < 0) {
83 HILOG_ERROR("Failed to stat file with path");
84 return ret;
85 }
86 return ERRNO_NOERR;
87 }
88
GetStat(const string & path,StatEntity & statEntity)89 static bool GetStat(const string &path, StatEntity &statEntity)
90 {
91 unique_ptr<uv_fs_t, decltype(fs_req_cleanup)*> stat_req = {
92 new (nothrow) uv_fs_t, fs_req_cleanup };
93 if (!stat_req) {
94 HILOG_ERROR("Failed to request heap memory.");
95 return false;
96 }
97 auto err = CheckFsStatByPath(path, stat_req.get());
98 if (!err) {
99 statEntity = StatEntity { stat_req->statbuf };
100 return true;
101 }
102 return false;
103 }
104
CheckDir(const string & path)105 static bool CheckDir(const string &path)
106 {
107 struct stat fileInformation;
108 if (stat(path.c_str(), &fileInformation) == 0 && (fileInformation.st_mode & S_IFDIR)) {
109 return true;
110 } else {
111 HILOG_ERROR("Failed to stat file");
112 }
113 return false;
114 }
115
Access(const string & path)116 static tuple<bool, int> Access(const string &path)
117 {
118 unique_ptr<uv_fs_t, decltype(fs_req_cleanup)*> access_req = { new uv_fs_t, fs_req_cleanup };
119 if (!access_req) {
120 HILOG_ERROR("Failed to request heap memory.");
121 return {false, ENOMEM};
122 }
123 int ret = uv_fs_access(nullptr, access_req.get(), path.c_str(), 0, nullptr);
124 if (ret < 0 && (string_view(uv_err_name(ret)) != "ENOENT")) {
125 HILOG_ERROR("Failed to access file by path");
126 return {false, ret};
127 }
128 bool isExist = (ret == 0);
129 return {isExist, ERRNO_NOERR};
130 }
131
Mkdir(const string & path)132 static bool Mkdir(const string &path)
133 {
134 unique_ptr<uv_fs_t, decltype(fs_req_cleanup)*> mkdir_req = { new uv_fs_t, fs_req_cleanup };
135 if (!mkdir_req) {
136 HILOG_ERROR("Failed to request heap memory.");
137 return false;
138 }
139 int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.c_str(), DIR_DEFAULT_PERM, nullptr);
140 if (ret < 0) {
141 HILOG_ERROR("Failed to create directory");
142 return false;
143 }
144 if (ret == 0) {
145 return true;
146 }
147 return false;
148 }
149
CopyAndDeleteFile(const string & src,const string & dest)150 static int CopyAndDeleteFile(const string &src, const string &dest)
151 {
152 // 获取源文件时间
153 StatEntity statEntity;
154 if (!GetStat(src, statEntity)) {
155 HILOG_ERROR("Failed to get file stat.");
156 return EINVAL;
157 }
158 // 拼接秒数和纳秒数
159 uint64_t acTimeLong = statEntity.stat_.st_atim.tv_sec * TIME_CONVERT_BASE + statEntity.stat_.st_atim.tv_nsec;
160 uint64_t modTimeLong = statEntity.stat_.st_mtim.tv_sec * TIME_CONVERT_BASE + statEntity.stat_.st_mtim.tv_nsec;
161 double acTime = static_cast<long double>(acTimeLong) / TIME_CONVERT_BASE;
162 double modTime = static_cast<long double>(modTimeLong) / TIME_CONVERT_BASE;
163
164 int ret = 0;
165 uv_fs_t copyfile_req;
166 ret = uv_fs_copyfile(nullptr, ©file_req, src.c_str(), dest.c_str(), MODE_FORCE_MOVE, nullptr);
167 uv_fs_req_cleanup(©file_req);
168
169 // 设置目标文件时间
170 uv_fs_t utime_req;
171 uv_fs_utime(nullptr, &utime_req, dest.c_str(), acTime, modTime, nullptr);
172 uv_fs_req_cleanup(&utime_req);
173
174 if (ret < 0) {
175 HILOG_ERROR("Failed to move file using copyfile interface.");
176 return ret;
177 }
178 uv_fs_t unlink_req;
179 ret = uv_fs_unlink(nullptr, &unlink_req, src.c_str(), nullptr);
180 if (ret < 0) {
181 HILOG_ERROR("Failed to unlink src file");
182 ret = uv_fs_unlink(nullptr, &unlink_req, dest.c_str(), nullptr);
183 if (ret < 0) {
184 HILOG_ERROR("Failed to unlink dest file");
185 }
186 uv_fs_req_cleanup(&unlink_req);
187 return ret;
188 }
189 uv_fs_req_cleanup(&unlink_req);
190 return ERRNO_NOERR;
191 }
192
RenameFile(const string & src,const string & dest)193 static int RenameFile(const string &src, const string &dest)
194 {
195 unique_ptr<uv_fs_t, decltype(fs_req_cleanup)*> rename_req = {
196 new uv_fs_t, fs_req_cleanup };
197 if (!rename_req) {
198 HILOG_ERROR("RenameFile: Failed to request heap memory.");
199 return false;
200 }
201 int ret = uv_fs_rename(nullptr, rename_req.get(), src.c_str(), dest.c_str(), nullptr);
202 if (ret < 0 && (string_view(uv_err_name(ret)) == "EXDEV")) {
203 HILOG_DEBUG("RenameFile: using CopyAndDeleteFile.");
204 return CopyAndDeleteFile(src, dest);
205 }
206 if (ret < 0) {
207 HILOG_ERROR("RenameFile: Failed to move file using rename syscall ret %{public}d ", ret);
208 return ret;
209 }
210 return ERRNO_NOERR;
211 }
212
RmDirent(const string & fpath)213 static NError RmDirent(const string &fpath)
214 {
215 unique_ptr<uv_fs_t, decltype(fs_req_cleanup)*> scandir_req = {
216 new (nothrow) uv_fs_t, fs_req_cleanup };
217 if (!scandir_req) {
218 HILOG_ERROR("Failed to request heap memory.");
219 return NError(ENOMEM);
220 }
221 int ret = 0;
222 ret = uv_fs_scandir(nullptr, scandir_req.get(), fpath.c_str(), 0, nullptr);
223 if (ret < 0) {
224 HILOG_ERROR("Failed to scandir, ret: %{public}d", ret);
225 return NError(ret);
226 }
227 uv_dirent_t dent;
228 while (uv_fs_scandir_next(scandir_req.get(), &dent) != UV_EOF) {
229 string filePath = fpath + "/" + string(dent.name);
230 if (dent.type == UV_DIRENT_FILE) {
231 unique_ptr<uv_fs_t, decltype(fs_req_cleanup)*> unlink_req = {
232 new (nothrow) uv_fs_t, fs_req_cleanup };
233 if (!unlink_req) {
234 HILOG_ERROR("Failed to request heap memory.");
235 return NError(ENOMEM);
236 }
237 ret = uv_fs_unlink(nullptr, unlink_req.get(), filePath.c_str(), nullptr);
238 if (ret < 0) {
239 HILOG_ERROR("Failed to unlink file, ret: %{public}d", ret);
240 return NError(ret);
241 }
242 } else if (dent.type == UV_DIRENT_DIR) {
243 auto rmDirentRes = RmDirent(filePath);
244 if (rmDirentRes) {
245 return rmDirentRes;
246 }
247 }
248 }
249 unique_ptr<uv_fs_t, decltype(fs_req_cleanup)*> rmdir_req = {
250 new (nothrow) uv_fs_t, fs_req_cleanup };
251 if (!rmdir_req) {
252 HILOG_ERROR("Failed to request heap memory.");
253 return NError(ENOMEM);
254 }
255 ret = uv_fs_rmdir(nullptr, rmdir_req.get(), fpath.c_str(), nullptr);
256 if (ret < 0) {
257 HILOG_ERROR("Failed to rmdir empty dir, ret: %{public}d", ret);
258 return NError(ret);
259 }
260 return NError(ERRNO_NOERR);
261 }
262
ScanDir(const string & path)263 static int ScanDir(const string &path)
264 {
265 unique_ptr<struct NameListArg, decltype(Deleter)*> pNameList = { new (nothrow) struct NameListArg, Deleter };
266 if (!pNameList) {
267 HILOG_ERROR("Failed to request heap memory.");
268 return ENOMEM;
269 }
270 HILOG_INFO("RecursiveFunc: scandir path = %{public}s", path.c_str());
271 return scandir(path.c_str(), &(pNameList->namelist), FilterFunc, alphasort);
272 }
273 } // OHOS::FileManagement