1 /*
2 * Copyright (c) 2024 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 "ecmascript/platform/filesystem.h"
17
18 #include <fstream>
19 #include <dirent.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include "ecmascript/platform/file.h"
25
26 #include "os/filesystem.h"
27
28 namespace panda::ecmascript::filesystem {
CreateDirectory(const std::string & path)29 bool CreateDirectory(const std::string& path)
30 {
31 if (path.empty() || Exists(path)) {
32 return true;
33 }
34 auto pos = path.find_last_of('/');
35 if (pos != std::string::npos) {
36 if (!CreateDirectory(path.substr(0, pos))) {
37 return false;
38 }
39 }
40 panda::os::CreateDirectories(path);
41 if (!Exists(path)) {
42 return false;
43 }
44 return true;
45 }
46
Exists(const std::string & path)47 bool Exists(const std::string& path)
48 {
49 return panda::ecmascript::FileExist(path.c_str());
50 }
51
TempDirectoryPath()52 std::string TempDirectoryPath()
53 {
54 const char* temp = getenv("TMPDIR");
55 if (temp) {
56 return std::string(temp);
57 }
58 return "/tmp";
59 }
60
FileSize(const std::string & path)61 std::size_t FileSize(const std::string& path)
62 {
63 struct stat info;
64 stat(path.c_str(), &info);
65 return info.st_size;
66 }
67
RemoveAll(const std::string & path)68 bool RemoveAll(const std::string& path)
69 {
70 struct stat info;
71 if (stat(path.c_str(), &info) != 0) {
72 return false;
73 }
74
75 if (S_ISDIR(info.st_mode)) {
76 DIR* dir = opendir(path.c_str());
77 if (!dir) {
78 return false;
79 }
80
81 struct dirent* entry;
82 while ((entry = readdir(dir)) != nullptr) {
83 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
84 continue;
85 }
86 std::string entryPath = path + "/" + entry->d_name;
87 if (!RemoveAll(entryPath)) {
88 closedir(dir);
89 return false;
90 }
91 }
92 closedir(dir);
93 return rmdir(path.c_str()) == 0;
94 } else {
95 return unlink(path.c_str()) == 0;
96 }
97 }
98
CreateEmptyFile(const std::string & fileName)99 void CreateEmptyFile(const std::string& fileName)
100 {
101 std::string realPath;
102 if (!panda::ecmascript::RealPath(fileName, realPath, false)) {
103 LOG_FULL(ERROR) << "failed to create empty file: " << fileName << ", error: " << std::strerror(errno);
104 return;
105 }
106 if (Exists(realPath)) {
107 LOG_FULL(INFO) << realPath << " exists, skip creation";
108 return;
109 }
110
111 auto index = realPath.find_last_of('/');
112 if (index != std::string::npos) {
113 std::string dir = realPath.substr(0, index);
114 if (Exists(dir)) {
115 LOG_FULL(INFO) << dir << " exists, skip creation";
116 } else {
117 LOG_FULL(INFO) << "creating folder " << dir;
118 if (!CreateDirectory(dir)) {
119 LOG_FULL(ERROR) << "failed to create folder " << dir << ", error: " << std::strerror(errno);
120 return;
121 }
122 }
123 }
124
125 std::ofstream file(realPath);
126 if (!file.good()) {
127 LOG_FULL(ERROR) << "failed to create empty file: " << realPath << ", error: " << std::strerror(errno);
128 } else {
129 LOG_FULL(INFO) << "created empty file: " << realPath;
130 }
131 file.close();
132 }
133 } // namespace panda::ecmascript::kungfu::filesystem