• 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 "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 "foundation/log.h"
32 
33 #ifndef _WIN32
34 mode_t umask(mode_t __mask);
35 #endif
36 
37 namespace OHOS {
38 namespace Media {
39 namespace OSAL {
40 constexpr uint32_t MAX_FILE_PATH = 50;
IsRegularFile(const std::string & path)41 bool FileSystem::IsRegularFile(const std::string& path)
42 {
43     struct stat s {};
44     return (stat(path.c_str(), &s) == 0) && S_ISREG(s.st_mode);
45 }
46 
IsFdValid(int32_t fd,struct stat & s)47 bool FileSystem::IsFdValid(int32_t fd, struct stat& s)
48 {
49     return fstat(fd, &s) == 0;
50 }
51 
IsRegularFile(int32_t fd)52 bool FileSystem::IsRegularFile(int32_t fd)
53 {
54     struct stat s {};
55     return IsFdValid(fd, s) && S_ISREG(s.st_mode);
56 }
57 
IsSocketFile(int32_t fd)58 bool FileSystem::IsSocketFile(int32_t fd)
59 {
60     struct stat s {};
61     return IsFdValid(fd, s) && S_ISSOCK(s.st_mode);
62 }
63 
IsSeekable(int32_t fd)64 bool FileSystem::IsSeekable(int32_t fd)
65 {
66     return IsRegularFile(fd) && lseek(fd, 0, SEEK_CUR) != -1;
67 }
68 
IsDirectory(const std::string & path)69 bool FileSystem::IsDirectory(const std::string& path)
70 {
71     struct stat s {};
72     return (stat(path.c_str(), &s) == 0) && S_ISDIR(s.st_mode);
73 }
74 
75 // judge regular file, directory, symbolic link file path exists
IsExists(const std::string & path)76 bool FileSystem::IsExists(const std::string& path)
77 {
78     return access(path.c_str(), 0) != -1;
79 }
80 
MakeDir(const std::string & path)81 bool FileSystem::MakeDir(const std::string& path)
82 {
83 #ifdef _WIN32
84     if (mkdir(path.c_str()) == -1) {
85         MEDIA_LOG_E("Fail to create dir " PUBLIC_LOG_S " due to " PUBLIC_LOG_S, path.c_str(), std::strerror(errno));
86         return false;
87     }
88 #else
89 #ifndef OHOS_LITE
90     auto oldMask = umask(0);
91 #endif
92     if (mkdir(path.c_str(), 755) == -1) { // 755 directory access permissions
93         MEDIA_LOG_E("Fail to create dir " PUBLIC_LOG_S " due to " PUBLIC_LOG_S, path.c_str(), std::strerror(errno));
94 #ifndef OHOS_LITE
95         umask(oldMask);
96 #endif
97         return false;
98     }
99 #ifndef OHOS_LITE
100     umask(oldMask);
101 #endif
102 #endif
103     return true;
104 }
105 
MakeMultipleDir(const std::string & path)106 bool FileSystem::MakeMultipleDir(const std::string& path)
107 {
108     FALSE_RETURN_V(!IsExists(path), true);
109 
110     // pos is 1, not 0  example: D:/a/b, /local/tmp/
111     // Avoid Linux root path before is empty string, which makes it impossible to judge whether the path exists
112     auto index = path.find('/', 1);
113     while (index != std::string::npos) {
114         std::string tPath = path.substr(0, index);
115         FALSE_RETURN_V(IsExists(tPath) || MakeDir(tPath), false);
116         index = path.find('/', index + 1);
117     }
118     return path[path.size() - 1] == '/' || MakeDir(path);
119 }
120 
RemoveFilesInDir(const std::string & path)121 void FileSystem::RemoveFilesInDir(const std::string& path)
122 {
123     DIR *directory;
124     struct dirent *info;
125     if ((directory = opendir(path.c_str())) != nullptr) {
126         while ((info = readdir(directory)) != nullptr) {
127             if (strcmp(info->d_name, ".") == 0 || strcmp(info->d_name, "..") == 0) {
128                 continue;
129             }
130             std::string fullPath = path + "/" + info->d_name;
131             MEDIA_LOG_D("Remove file : " PUBLIC_LOG_S, fullPath.c_str());
132             NZERO_LOG(remove(fullPath.c_str()));
133         }
134         closedir(directory);
135     }
136 }
137 
GetTmpFileName()138 std::string FileSystem::GetTmpFileName()
139 {
140     char tempFileName[MAX_FILE_PATH] = "/tmp/hstTmp.XXXXXX";
141 #ifdef _WIN32
142     char tempPath[MAX_FILE_PATH];
143     auto pathLength = GetTempPath(MAX_FILE_PATH, tempPath);
144     FALSE_RETURN_V_MSG_E(pathLength < MAX_FILE_PATH && pathLength > 0, tempFileName, "get temp path failed");
145     auto ret = GetTempFileName(tempPath, "hstTmp", 0, tempFileName);
146     FALSE_RETURN_V_MSG_E(ret != 0, tempFileName, "get temp file name failed");
147 #else
148     mktemp(tempFileName);
149 #endif
150     return tempFileName;
151 }
152 } // namespace OSAL
153 } // namespace Media
154 } // namespace OHOS