• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "NativeFileSystem.h"
23 
24 std::vector<std::string> FileSystem::pathList = {"file_system", "app", "ace", "data"};
25 std::string FileSystem::bundleName = "";
26 std::string FileSystem::fileSystemPath = "";
27 
28 #ifdef _WIN32
29 std::string FileSystem::separator = "\\";
30 #else
31 std::string FileSystem::separator = "/";
32 #endif
33 
IsFileExists(std::string path)34 bool FileSystem::IsFileExists(std::string path)
35 {
36     return S_ISREG(GetFileMode(path));
37 }
38 
IsDirectoryExists(std::string path)39 bool FileSystem::IsDirectoryExists(std::string path)
40 {
41     return S_ISDIR(GetFileMode(path));
42 }
43 
GetApplicationPath()44 std::string FileSystem::GetApplicationPath()
45 {
46     char appPath[MAX_PATH_LEN];
47     if (getcwd(appPath, MAX_PATH_LEN) == nullptr) {
48         ELOG("Get current path failed.");
49         return std::string();
50     }
51     std::string path(appPath);
52     return path;
53 }
54 
GetVirtualFileSystemPath()55 const std::string& FileSystem::GetVirtualFileSystemPath()
56 {
57     return fileSystemPath;
58 }
59 
MakeVirtualFileSystemPath()60 void FileSystem::MakeVirtualFileSystemPath()
61 {
62     std::string dirToMake = GetApplicationPath();
63     if (!IsDirectoryExists(dirToMake)) {
64         ELOG("Application path is not exists.");
65         return;
66     }
67     for (std::string path : pathList) {
68         dirToMake += separator;
69         dirToMake += path;
70         MakeDir(dirToMake.data());
71     }
72     dirToMake += separator;
73     dirToMake += bundleName;
74     MakeDir(dirToMake);
75     fileSystemPath = dirToMake;
76 }
77 
MakeDir(std::string path)78 int FileSystem::MakeDir(std::string path)
79 {
80     int result = 0;
81 #ifdef _WIN32
82     result = mkdir(path.data());
83 #else
84     result = mkdir(path.data(), S_IRUSR | S_IWUSR | S_IXUSR);
85 #endif
86     return result;
87 }
88 
SetBundleName(std::string name)89 void FileSystem::SetBundleName(std::string name)
90 {
91     bundleName = name;
92 }
93 
GetFileMode(std::string path)94 unsigned short FileSystem::GetFileMode(std::string path)
95 {
96     struct stat info {};
97     if (stat(path.data(), &info) != 0) {
98         return 0;
99     }
100     return info.st_mode;
101 }
102 
GetSeparator()103 std::string FileSystem::GetSeparator()
104 {
105     return separator;
106 }
107 
FindSubfolderByName(const std::string & parentFolderPath,const std::string & subfolderName)108 std::string FileSystem::FindSubfolderByName(const std::string& parentFolderPath, const std::string& subfolderName)
109 {
110     return OHOS::Ide::NativeFileSystem::FindSubfolderByName(parentFolderPath, subfolderName);
111 }
112 
NormalizePath(const std::string & path)113 std::string FileSystem::NormalizePath(const std::string& path)
114 {
115     std::string normalizedPath = path;
116     char separatorChar = FileSystem::separator[0]; // 0 is get fist char of string
117     for (char& c : normalizedPath) {
118         if (c == '/' || c == '\\') {
119             c = separatorChar;
120         }
121     }
122     return normalizedPath;
123 }