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