1 /*
2 * Copyright (c) 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
16 #include "ecmascript/platform/file.h"
17
18 #include <dirent.h>
19 #include "common_components/log/log.h"
20 #include "ecmascript/module/js_module_source_text.h"
21
22 namespace panda::ecmascript {
GetFileDelimiter()23 std::string GetFileDelimiter()
24 {
25 return ":";
26 }
27
GetPathSeparator()28 std::string GetPathSeparator()
29 {
30 return "/";
31 }
32
RealPath(const std::string & path,std::string & realPath,bool readOnly)33 bool RealPath(const std::string &path, std::string &realPath, bool readOnly)
34 {
35 if (path.empty() || path.size() > PATH_MAX) {
36 LOG_ECMA(WARN) << "File path is illegal";
37 return false;
38 }
39 char buffer[PATH_MAX] = { '\0' };
40 if (!realpath(path.c_str(), buffer)) {
41 // Maybe file does not exist.
42 if (!readOnly && errno == ENOENT) {
43 realPath = path;
44 return true;
45 }
46 LOG_ECMA(ERROR) << "File path: " << path << " realpath failure. errno: " << errno;
47 return false;
48 }
49 realPath = std::string(buffer);
50 return true;
51 }
52
RealPathByChar(const char * path,char * realPath,int rowLength,bool readOnly)53 bool RealPathByChar(const char *path, char *realPath, int rowLength, bool readOnly)
54 {
55 if (strlen(path) == 0 || strlen(path) > PATH_MAX) {
56 return false;
57 }
58 char buffer[PATH_MAX] = { '\0' };
59 if (!realpath(path, buffer)) {
60 // Maybe file does not exist.
61 if (!readOnly && errno == ENOENT) {
62 if (strcpy_s(realPath, rowLength, path) != 0) {
63 return false;
64 }
65 return true;
66 }
67 return false;
68 }
69 if (strcpy_s(realPath, rowLength, buffer) != 0) {
70 return false;
71 }
72 return true;
73 }
74
DPrintf(fd_t fd,const std::string & buffer)75 void DPrintf(fd_t fd, const std::string &buffer)
76 {
77 int ret = dprintf(fd, "%s", buffer.c_str());
78 if (ret < 0) {
79 LOG_ECMA(DEBUG) << "dprintf fd(" << fd << ") failed";
80 }
81 }
82
FSync(fd_t fd)83 void FSync(fd_t fd)
84 {
85 int ret = fsync(fd);
86 if (ret < 0) {
87 LOG_ECMA(DEBUG) << "fsync fd(" << fd << ") failed";
88 }
89 }
90
FdsanExchangeOwnerTag(fd_t fd)91 void FdsanExchangeOwnerTag(fd_t fd)
92 {
93 #if defined(PANDA_TARGET_OHOS)
94 fdsan_exchange_owner_tag(fd, 0, LOG_DOMAIN);
95 #else
96 LOG_ECMA(DEBUG) << "Unsupport FdsanExchangeOwnerTag fd(" << fd << ")";
97 #endif
98 }
99
Close(fd_t fd)100 void Close(fd_t fd)
101 {
102 #if defined(PANDA_TARGET_OHOS)
103 fdsan_close_with_tag(fd, LOG_DOMAIN);
104 #else
105 close(fd);
106 #endif
107 }
108
FileMap(const char * fileName,int flag,int prot,int64_t offset)109 MemMap FileMap(const char *fileName, int flag, int prot, int64_t offset)
110 {
111 fd_t fd = open(fileName, flag);
112 if (fd == INVALID_FD) {
113 LOG_ECMA(ERROR) << fileName << " file open failed";
114 return MemMap();
115 }
116 FdsanExchangeOwnerTag(fd);
117
118 off_t size = lseek(fd, 0, SEEK_END);
119 if (size <= 0) {
120 Close(fd);
121 LOG_ECMA(ERROR) << fileName << " file is empty";
122 return MemMap();
123 }
124
125 void *addr = mmap(nullptr, size, prot, MAP_PRIVATE, fd, offset);
126 Close(fd);
127 return MemMap(addr, size);
128 }
129
CreateFileMap(const char * fileName,int fileSize,int flag,int prot)130 MemMap CreateFileMap([[maybe_unused]] const char *fileName, [[maybe_unused]] int fileSize,
131 [[maybe_unused]] int flag, [[maybe_unused]] int prot)
132 {
133 fd_t fd = open(fileName, flag, S_IRWXU | S_IRGRP | S_IROTH); // rw-r-r-
134 if (fd == INVALID_FD) {
135 LOG_ECMA(ERROR) << fileName << " file open failed";
136 return MemMap();
137 }
138 FdsanExchangeOwnerTag(fd);
139
140 if (ftruncate(fd, fileSize) == -1) {
141 LOG_ECMA(ERROR) << fileName << " file ftruncate failed";
142 close(fd);
143 return MemMap();
144 }
145 struct stat st;
146 if (fstat(fd, &st) == -1 || st.st_size == 0) {
147 LOG_ECMA(ERROR) << fileName << " file fstat failed";
148 close(fd);
149 return MemMap();
150 }
151
152 void *addr = mmap(nullptr, fileSize, prot, MAP_SHARED, fd, 0);
153 Close(fd);
154 return MemMap(addr, fileSize);
155 }
156
FileMapForAlignAddressByFd(const fd_t fd,int prot,int64_t offset,uint32_t offStart)157 MemMap FileMapForAlignAddressByFd(const fd_t fd, int prot, int64_t offset, uint32_t offStart)
158 {
159 if (fd == INVALID_FD) {
160 LOG_ECMA(ERROR) << fd << " fd open failed";
161 return MemMap();
162 }
163
164 off_t size = lseek(fd, 0, SEEK_END);
165 if (size <= 0) {
166 LOG_ECMA(ERROR) << fd << " fd is empty";
167 return MemMap();
168 }
169 void *addr = mmap(nullptr, size + offset - offStart, prot, MAP_PRIVATE, fd, offStart);
170 return MemMap(addr, size);
171 }
172
FileUnMap(MemMap addr)173 int FileUnMap(MemMap addr)
174 {
175 return munmap(addr.GetOriginAddr(), addr.GetSize());
176 }
177
FileSync(MemMap addr,int flag)178 int FileSync(MemMap addr, int flag)
179 {
180 return msync(addr.GetOriginAddr(), addr.GetSize(), flag);
181 }
182
ResolveFilenameFromNative(JSThread * thread,const CString & dirname,CString request)183 CString ResolveFilenameFromNative(JSThread *thread, const CString &dirname,
184 CString request)
185 {
186 std::string relativePath;
187 if (request.find("./") == 0) {
188 request = request.substr(2); // 2 : delete './'
189 }
190 int suffixEnd = static_cast<int>(request.find_last_of('.'));
191 if (request[0] == '/') { // absolute FilePath
192 relativePath = request.substr(0, suffixEnd) + ".abc";
193 } else {
194 int pos = static_cast<int>(dirname.find_last_of('/'));
195 relativePath = dirname.substr(0, pos + 1) + request.substr(0, suffixEnd) + ".abc";
196 }
197
198 std::string absPath = "";
199 if (RealPath(relativePath.c_str(), absPath)) {
200 return absPath.c_str();
201 }
202 THROW_REFERENCE_ERROR_AND_RETURN(thread, "resolve absolute path fail", CString());
203 }
204
FileExist(const char * filename)205 bool FileExist(const char *filename)
206 {
207 return (access(filename, F_OK) != -1);
208 }
209
Unlink(const char * filename)210 int Unlink(const char *filename)
211 {
212 return unlink(filename);
213 }
214
LoadLib(const std::string & libname)215 void *LoadLib(const std::string &libname)
216 {
217 return dlopen(libname.c_str(), RTLD_NOW);
218 }
219
FindSymbol(void * handle,const char * symbol)220 void *FindSymbol(void *handle, const char *symbol)
221 {
222 return dlsym(handle, symbol);
223 }
224
CloseLib(void * handle)225 int CloseLib(void *handle)
226 {
227 return dlclose(handle);
228 }
229
LoadLibError()230 char *LoadLibError()
231 {
232 return dlerror();
233 }
234
DeleteFilesWithSuffix(const std::string & dirPath,const std::string & suffix)235 void DeleteFilesWithSuffix(const std::string &dirPath, const std::string &suffix)
236 {
237 DIR* dir = opendir(dirPath.data());
238 if (!dir) {
239 LOG_FULL(ERROR) << "DeleteFilesWithSuffix open dir Failed";
240 return;
241 }
242 struct dirent* entry;
243 while ((entry = readdir(dir)) != nullptr) {
244 if (entry->d_type == DT_REG) {
245 const std::string &fileName = entry->d_name;
246 if (fileName.find(suffix) == std::string::npos) {
247 continue;
248 }
249 std::string fullPath = std::string(dirPath) + fileName;
250 if (remove(fullPath.c_str()) == 0) {
251 LOG_ECMA(INFO) << "DeleteFilesWithSuffix remove path success: " << fullPath;
252 } else {
253 LOG_ECMA(ERROR) << "DeleteFilesWithSuffix remove path failed: " << fullPath;
254 }
255 }
256 }
257 closedir(dir);
258 }
259 } // namespace panda::ecmascript
260