• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 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 #define HST_LOG_TAG "FileSystem"
16 
17 #include "osal/filesystem/file_system.h"
18 #ifdef _WIN32
19 #include <direct.h>
20 #include <fcntl.h>
21 #include <windows.h>
22 #else
23 #include <unistd.h>
24 #endif
25 
26 #include <cerrno>
27 #include <cstdio>
28 #include <cstdlib>
29 #include <cstring>
30 #include <dirent.h>
31 #include "common/log.h"
32 
33 #ifndef _WIN32
34 mode_t umask(mode_t mask);
35 #endif
36 
37 namespace OHOS {
38 namespace Media {
IsRegularFile(const std::string & path)39 bool FileSystem::IsRegularFile(const std::string& path)
40 {
41     struct stat s {};
42     return (stat(path.c_str(), &s) == 0) && S_ISREG(s.st_mode);
43 }
44 
IsFdValid(int32_t fd,struct stat & s)45 bool FileSystem::IsFdValid(int32_t fd, struct stat& s)
46 {
47     return fstat(fd, &s) == 0;
48 }
49 
IsRegularFile(int32_t fd)50 bool FileSystem::IsRegularFile(int32_t fd)
51 {
52     struct stat s {};
53     return IsFdValid(fd, s) && S_ISREG(s.st_mode);
54 }
55 
IsSocketFile(int32_t fd)56 bool FileSystem::IsSocketFile(int32_t fd)
57 {
58     struct stat s {};
59     return IsFdValid(fd, s) && S_ISSOCK(s.st_mode);
60 }
61 
IsSeekable(int32_t fd)62 bool FileSystem::IsSeekable(int32_t fd)
63 {
64     return IsRegularFile(fd) && lseek(fd, 0, SEEK_CUR) != -1;
65 }
66 
IsDirectory(const std::string & path)67 bool FileSystem::IsDirectory(const std::string& path)
68 {
69     struct stat s {};
70     return (stat(path.c_str(), &s) == 0) && S_ISDIR(s.st_mode);
71 }
72 
73 // judge regular file, directory, symbolic link file path exists
IsExists(const std::string & path)74 bool FileSystem::IsExists(const std::string& path)
75 {
76     return access(path.c_str(), 0) != -1;
77 }
78 
MakeDir(const std::string & path)79 bool FileSystem::MakeDir(const std::string& path)
80 {
81 #ifdef _WIN32
82     if (mkdir(path.c_str()) == -1) {
83         MEDIA_LOG_E("Fail to create dir " PUBLIC_LOG_S " due to " PUBLIC_LOG_S, path.c_str(), std::strerror(errno));
84         return false;
85     }
86 #else
87 #ifndef OHOS_LITE
88     auto oldMask = umask(0);
89 #endif
90     if (mkdir(path.c_str(), 755) == -1) { // 755 directory access permissions
91         MEDIA_LOG_E("Fail to create dir " PUBLIC_LOG_S " due to " PUBLIC_LOG_S, path.c_str(), std::strerror(errno));
92 #ifndef OHOS_LITE
93         umask(oldMask);
94 #endif
95         return false;
96     }
97 #ifndef OHOS_LITE
98     umask(oldMask);
99 #endif
100 #endif
101     return true;
102 }
103 
MakeMultipleDir(const std::string & path)104 bool FileSystem::MakeMultipleDir(const std::string& path)
105 {
106     FALSE_RETURN_V(!IsExists(path), true);
107 
108     // pos is 1, not 0  example: D:/a/b, /local/tmp/
109     // Avoid Linux root path before is empty string, which makes it impossible to judge whether the path exists
110     auto index = path.find('/', 1);
111     while (index != std::string::npos) {
112         std::string tPath = path.substr(0, index);
113         FALSE_RETURN_V(IsExists(tPath) || MakeDir(tPath), false);
114         index = path.find('/', index + 1);
115     }
116     return path[path.size() - 1] == '/' || MakeDir(path);
117 }
118 
ClearFileContent(const std::string & fullPath)119 void FileSystem::ClearFileContent(const std::string& fullPath)
120 {
121     MEDIA_LOG_I("Clear file content path : " PUBLIC_LOG_S, fullPath.c_str());
122     auto filePtr = fopen(fullPath.c_str(), "w+");
123     if (!filePtr) {
124         MEDIA_LOG_E("Clear file content fail.");
125         return;
126     }
127     fclose(filePtr);
128 }
129 
RemoveFilesInDir(const std::string & path)130 void FileSystem::RemoveFilesInDir(const std::string& path)
131 {
132     DIR *directory;
133     if ((directory = opendir(path.c_str())) != nullptr) {
134         struct dirent *info;
135         while ((info = readdir(directory)) != nullptr) {
136             if (strcmp(info->d_name, ".") == 0 || strcmp(info->d_name, "..") == 0) {
137                 continue;
138             }
139             std::string fullPath = path + "/" + info->d_name;
140             MEDIA_LOG_D("Remove file : " PUBLIC_LOG_S, fullPath.c_str());
141             NZERO_LOG(remove(fullPath.c_str()));
142         }
143         closedir(directory);
144     }
145 }
146 } // namespace Media
147 } // namespace OHOS