• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "rmdir_core.h"
17 
18 #include <cstring>
19 #include <dirent.h>
20 #include <filesystem>
21 #include <tuple>
22 #include <unistd.h>
23 
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 
27 #include "filemgmt_libhilog.h"
28 
29 namespace OHOS {
30 namespace FileManagement {
31 namespace ModuleFileIO {
32 using namespace std;
33 
34 #ifdef __MUSL__
RmDirent(const string & fpath)35 static int32_t RmDirent(const string &fpath)
36 {
37     std::filesystem::path strToPath(fpath);
38     std::error_code errCode;
39     std::uintmax_t num = std::filesystem::remove_all(strToPath, errCode);
40     if (errCode.value() != ERRNO_NOERR) {
41         HILOGD("Failed to remove directory, error code: %{public}d", errCode.value());
42         return errCode.value();
43     }
44     if (!num || std::filesystem::exists(strToPath, errCode)) {
45         HILOGE("Failed to remove directory, dirPath does not exist");
46         return ENOENT;
47     }
48     if (errCode.value() != ERRNO_NOERR) {
49         HILOGE("fs exists fail, error code: %{public}d", errCode.value());
50         return errCode.value();
51     }
52     return ERRNO_NOERR;
53 }
54 
55 #else
RmDirent(const string & fpath)56 static int32_t RmDirent(const string &fpath)
57 {
58     std::unique_ptr<uv_fs_t, decltype(FsUtils::FsReqCleanup)*> scandirReq = {
59         new (std::nothrow) uv_fs_t, FsUtils::FsReqCleanup };
60     if (!scandirReq) {
61         HILOGE("Failed to request heap memory.");
62         return ENOMEM;
63     }
64     int ret = 0;
65     ret = uv_fs_scandir(nullptr, scandirReq.get(), fpath.c_str(), 0, nullptr);
66     if (ret < 0) {
67         HILOGE("Failed to scandir, ret: %{public}d", ret);
68         return ret;
69     }
70     uv_dirent_t dent;
71     while (uv_fs_scandir_next(scandirReq.get(), &dent) != UV_EOF) {
72         string filePath = fpath + "/" + string(dent.name);
73         if (dent.type == UV_DIRENT_FILE) {
74             std::unique_ptr<uv_fs_t, decltype(FsUtils::FsReqCleanup)*> unlinkReq = {
75                 new (std::nothrow) uv_fs_t, FsUtils::FsReqCleanup };
76             if (!unlinkReq) {
77                 HILOGE("Failed to request heap memory.");
78                 return ENOMEM;
79             }
80             ret = uv_fs_unlink(nullptr, unlinkReq.get(), filePath.c_str(), nullptr);
81             if (ret < 0) {
82                 HILOGE("Failed to unlink file, ret: %{public}d", ret);
83                 return ret;
84             }
85         } else if (dent.type == UV_DIRENT_DIR) {
86             auto rmDirentRes = RmDirent(filePath);
87             if (rmDirentRes) {
88                 return rmDirentRes;
89             }
90         }
91     }
92     std::unique_ptr<uv_fs_t, decltype(FsUtils::FsReqCleanup)*> rmdirReq = {
93         new (std::nothrow) uv_fs_t, FsUtils::FsReqCleanup};
94     if (!rmdirReq) {
95         HILOGE("Failed to request heap memory.");
96         return ENOMEM;
97     }
98     ret = uv_fs_rmdir(nullptr, rmdirReq.get(), fpath.c_str(), nullptr);
99     if (ret < 0) {
100         HILOGE("Failed to rmdir empty dir, ret: %{public}d", ret);
101         return ret;
102     }
103     return ERRNO_NOERR;
104 }
105 #endif
106 
DoRmdirent(const string & fpath)107 FsResult<void> RmdirentCore::DoRmdirent(const string &fpath)
108 {
109     if (fpath.empty()) {
110         HILOGE("Invalid path");
111         return FsResult<void>::Error(EINVAL);
112     }
113 
114     auto err = RmDirent(fpath);
115     if (err) {
116         return FsResult<void>::Error(err);
117     }
118     return FsResult<void>::Success();
119 }
120 } // namespace ModuleFileIO
121 } // namespace FileManagement
122 } // namespace OHOS