• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "ecmascript/base/path_helper.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 
Close(fd_t fd)91 void Close(fd_t fd)
92 {
93     close(fd);
94 }
95 
FileMap(const char * fileName,int flag,int prot,int64_t offset)96 MemMap FileMap(const char *fileName, int flag, int prot, int64_t offset)
97 {
98     fd_t fd = open(fileName, flag);
99     if (fd == INVALID_FD) {
100         LOG_ECMA(ERROR) << fileName << " file open failed";
101         return MemMap();
102     }
103 
104     off_t size = lseek(fd, 0, SEEK_END);
105     if (size <= 0) {
106         close(fd);
107         LOG_ECMA(ERROR) << fileName << " file is empty";
108         return MemMap();
109     }
110 
111     void *addr = mmap(nullptr, size, prot, MAP_PRIVATE, fd, offset);
112     close(fd);
113     return MemMap(addr, size);
114 }
115 
FileMapForAlignAddressByFd(const fd_t fd,int prot,int64_t offset,uint32_t offStart)116 MemMap FileMapForAlignAddressByFd(const fd_t fd, int prot, int64_t offset, uint32_t offStart)
117 {
118     if (fd == INVALID_FD) {
119         LOG_ECMA(ERROR) << fd << " fd open failed";
120         return MemMap();
121     }
122 
123     off_t size = lseek(fd, 0, SEEK_END);
124     if (size <= 0) {
125         LOG_ECMA(ERROR) << fd << " fd is empty";
126         return MemMap();
127     }
128     void *addr = mmap(nullptr, size + offset - offStart, prot, MAP_PRIVATE, fd, offStart);
129     return MemMap(addr, size);
130 }
131 
FileUnMap(MemMap addr)132 int FileUnMap(MemMap addr)
133 {
134     return munmap(addr.GetOriginAddr(), addr.GetSize());
135 }
136 
ResolveFilenameFromNative(JSThread * thread,const CString & dirname,CString request)137 CString ResolveFilenameFromNative(JSThread *thread, const CString &dirname,
138                                   CString request)
139 {
140     std::string relativePath;
141     if (request.find("./") == 0) {
142         request = request.substr(2); // 2 : delete './'
143     }
144     int suffixEnd = static_cast<int>(request.find_last_of('.'));
145     if (request[0] == '/') { // absolute FilePath
146         relativePath = request.substr(0, suffixEnd) + ".abc";
147     } else {
148         int pos = static_cast<int>(dirname.find_last_of('/'));
149         relativePath = dirname.substr(0, pos + 1) + request.substr(0, suffixEnd) + ".abc";
150     }
151 
152     std::string absPath = "";
153     if (RealPath(relativePath.c_str(), absPath)) {
154         return absPath.c_str();
155     }
156     THROW_REFERENCE_ERROR_AND_RETURN(thread, "resolve absolute path fail", CString());
157 }
158 
FileExist(const char * filename)159 bool FileExist(const char *filename)
160 {
161     return (access(filename, F_OK) != -1);
162 }
163 
Unlink(const char * filename)164 int Unlink(const char *filename)
165 {
166     return unlink(filename);
167 }
168 
TryToRemoveSO(JSThread * thread,JSHandle<SourceTextModule> module)169 bool TryToRemoveSO(JSThread *thread, JSHandle<SourceTextModule> module)
170 {
171     UnloadNativeModuleCallback unloadNativeModuleCallback = thread->GetEcmaVM()->GetUnloadNativeModuleCallback();
172     if (unloadNativeModuleCallback == nullptr) {
173         LOG_ECMA(ERROR) << "unloadNativeModuleCallback is nullptr";
174         return false;
175     }
176 
177     CString soName = base::PathHelper::GetStrippedModuleName(module->GetEcmaModuleRecordNameString());
178     return unloadNativeModuleCallback(soName.c_str());
179 }
180 
LoadLib(const std::string & libname)181 void *LoadLib(const std::string &libname)
182 {
183     return dlopen(libname.c_str(), RTLD_NOW);
184 }
185 
FindSymbol(void * handle,const char * symbol)186 void *FindSymbol(void *handle, const char *symbol)
187 {
188     return dlsym(handle, symbol);
189 }
190 
CloseLib(void * handle)191 int CloseLib(void *handle)
192 {
193     return dlclose(handle);
194 }
195 
LoadLibError()196 char *LoadLibError()
197 {
198     return dlerror();
199 }
200 
201 }  // namespace panda::ecmascript
202