1 /*
2 * Copyright (c) 2023 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 "FileSystem.h"
17
18 #include <sys/stat.h>
19 #include <unistd.h>
20
21 #include "PreviewerEngineLog.h"
22
23 using namespace std;
24
25 vector<string> FileSystem::pathList = {"file_system", "app", "ace", "data"};
26 string FileSystem::bundleName = "";
27 string FileSystem::fileSystemPath = "";
28
29 #ifdef _WIN32
30 std::string FileSystem::separator = "\\";
31 #else
32 std::string FileSystem::separator = "/";
33 #endif
34
IsFileExists(string path)35 bool FileSystem::IsFileExists(string path)
36 {
37 return S_ISREG(GetFileMode(path));
38 }
39
IsDirectoryExists(string path)40 bool FileSystem::IsDirectoryExists(string path)
41 {
42 return S_ISDIR(GetFileMode(path));
43 }
44
GetApplicationPath()45 string FileSystem::GetApplicationPath()
46 {
47 char appPath[MAX_PATH_LEN];
48 if (getcwd(appPath, MAX_PATH_LEN) == nullptr) {
49 ELOG("Get current path failed.");
50 return string();
51 }
52 string path(appPath);
53 return path;
54 }
55
GetVirtualFileSystemPath()56 const string& FileSystem::GetVirtualFileSystemPath()
57 {
58 return fileSystemPath;
59 }
60
MakeVirtualFileSystemPath()61 void FileSystem::MakeVirtualFileSystemPath()
62 {
63 string dirToMake = GetApplicationPath();
64 if (!IsDirectoryExists(dirToMake)) {
65 ELOG("Application path is not exists.");
66 return;
67 }
68 for (string path : pathList) {
69 dirToMake += separator;
70 dirToMake += path;
71 MakeDir(dirToMake.data());
72 }
73 dirToMake += separator;
74 dirToMake += bundleName;
75 MakeDir(dirToMake);
76 fileSystemPath = dirToMake;
77 }
78
MakeDir(string path)79 int FileSystem::MakeDir(string path)
80 {
81 int result = 0;
82 #ifdef _WIN32
83 result = mkdir(path.data());
84 #else
85 result = mkdir(path.data(), S_IRUSR | S_IWUSR | S_IXUSR);
86 #endif
87 return result;
88 }
89
SetBundleName(string name)90 void FileSystem::SetBundleName(string name)
91 {
92 bundleName = name;
93 }
94
GetFileMode(string path)95 unsigned short FileSystem::GetFileMode(string path)
96 {
97 struct stat info {};
98 if (stat(path.data(), &info) != 0) {
99 return 0;
100 }
101 return info.st_mode;
102 }
103
GetSeparator()104 string FileSystem::GetSeparator()
105 {
106 return separator;
107 }
108