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