• 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 "utils_directory.h"
17 
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <system_error>
21 
22 #include "directory_ex.h"
23 
24 namespace OHOS {
25 namespace Storage {
26 namespace DistributedFile {
27 namespace Utils {
28 using namespace std;
29 
ForceCreateDirectory(const string & path,function<void (const string &)> onSubDirCreated)30 void ForceCreateDirectory(const string &path, function<void(const string &)> onSubDirCreated)
31 {
32     string::size_type index = 0;
33     do {
34         string subPath;
35         index = path.find('/', index + 1);
36         if (index == string::npos) {
37             subPath = path;
38         } else {
39             subPath = path.substr(0, index);
40         }
41 
42         if (access(subPath.c_str(), F_OK) != 0) {
43             if (mkdir(subPath.c_str(), (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != 0 && errno != EEXIST) {
44                 throw system_error(errno, system_category());
45             }
46             onSubDirCreated(subPath);
47         }
48     } while (index != string::npos);
49 }
50 
ForceCreateDirectory(const string & path)51 void ForceCreateDirectory(const string &path)
52 {
53     ForceCreateDirectory(path, nullptr);
54 }
55 
ForceCreateDirectory(const string & path,mode_t mode)56 void ForceCreateDirectory(const string &path, mode_t mode)
57 {
58     ForceCreateDirectory(path, [mode](const string &subPath) {
59         if (chmod(subPath.c_str(), mode) == -1) {
60             throw system_error(errno, system_category());
61         }
62     });
63 }
64 
ForceCreateDirectory(const string & path,mode_t mode,uid_t uid,gid_t gid)65 void ForceCreateDirectory(const string &path, mode_t mode, uid_t uid, gid_t gid)
66 {
67     ForceCreateDirectory(path, [mode, uid, gid](const string &subPath) {
68         if (chmod(subPath.c_str(), mode) == -1 || chown(subPath.c_str(), uid, gid) == -1) {
69             throw system_error(errno, system_category());
70         }
71     });
72 }
73 
ForceRemoveDirectory(const string & path)74 void ForceRemoveDirectory(const string &path)
75 {
76     if (!OHOS::ForceRemoveDirectory(path)) {
77         throw system_error(errno, system_category());
78     }
79 }
80 } // namespace Utils
81 } // namespace DistributedFile
82 } // namespace Storage
83 } // namespace OHOS
84