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 <cerrno>
19 #include <climits>
20 #include <sys/mman.h>
21 #include <unistd.h>
22
23 #include "ecmascript/base/path_helper.h"
24 #include "ecmascript/ecma_macros.h"
25 #include "ecmascript/js_tagged_value-inl.h"
26 #include "ecmascript/log_wrapper.h"
27 #include "ecmascript/module/js_module_source_text.h"
28 #include "ecmascript/platform/map.h"
29
30 namespace panda::ecmascript {
GetFileDelimiter()31 std::string GetFileDelimiter()
32 {
33 return ":";
34 }
35
GetPathSeparator()36 std::string GetPathSeparator()
37 {
38 return "/";
39 }
40
RealPath(const std::string & path,std::string & realPath,bool readOnly)41 bool RealPath(const std::string &path, std::string &realPath, bool readOnly)
42 {
43 if (path.empty() || path.size() > PATH_MAX) {
44 LOG_ECMA(WARN) << "File path is illeage";
45 return false;
46 }
47 char buffer[PATH_MAX] = { '\0' };
48 if (!realpath(path.c_str(), buffer)) {
49 // Maybe file does not exist.
50 if (!readOnly && errno == ENOENT) {
51 realPath = path;
52 return true;
53 }
54 LOG_ECMA(ERROR) << "File path:" << path << " realpath failure. errno: " << errno;
55 return false;
56 }
57 realPath = std::string(buffer);
58 return true;
59 }
60
DPrintf(fd_t fd,const std::string & buffer)61 void DPrintf(fd_t fd, const std::string &buffer)
62 {
63 int ret = dprintf(fd, "%s", buffer.c_str());
64 if (ret < 0) {
65 LOG_ECMA(DEBUG) << "dprintf fd(" << fd << ") failed";
66 }
67 }
68
FSync(fd_t fd)69 void FSync(fd_t fd)
70 {
71 int ret = fsync(fd);
72 if (ret < 0) {
73 LOG_ECMA(DEBUG) << "fsync fd(" << fd << ") failed";
74 }
75 }
76
Close(fd_t fd)77 void Close(fd_t fd)
78 {
79 close(fd);
80 }
81
FileMap(const char * fileName,int flag,int prot,int64_t offset)82 MemMap FileMap(const char *fileName, int flag, int prot, int64_t offset)
83 {
84 fd_t fd = open(fileName, flag);
85 if (fd == INVALID_FD) {
86 LOG_ECMA(ERROR) << fileName << " file open failed";
87 return MemMap();
88 }
89
90 off_t size = lseek(fd, 0, SEEK_END);
91 if (size <= 0) {
92 close(fd);
93 LOG_ECMA(ERROR) << fileName << " file is empty";
94 return MemMap();
95 }
96
97 void *addr = mmap(nullptr, size, prot, MAP_PRIVATE, fd, offset);
98 close(fd);
99 return MemMap(addr, size);
100 }
101
FileMapForAlignAddress(const char * fileName,int flag,int prot,int64_t offset,uint32_t offStart)102 MemMap FileMapForAlignAddress(const char *fileName, int flag, int prot,
103 int64_t offset, uint32_t offStart)
104 {
105 fd_t fd = open(fileName, flag);
106 if (fd == INVALID_FD) {
107 LOG_ECMA(ERROR) << fileName << " file open failed";
108 return MemMap();
109 }
110
111 off_t size = lseek(fd, 0, SEEK_END);
112 if (size <= 0) {
113 close(fd);
114 LOG_ECMA(ERROR) << fileName << " file is empty";
115 return MemMap();
116 }
117 void *addr = mmap(nullptr, size + offset - offStart, prot, MAP_PRIVATE, fd, offStart);
118 close(fd);
119 return MemMap(addr, size);
120 }
121
FileUnMap(MemMap addr)122 int FileUnMap(MemMap addr)
123 {
124 return munmap(addr.GetOriginAddr(), addr.GetSize());
125 }
126
ResolveFilenameFromNative(JSThread * thread,JSTaggedValue dirname,JSTaggedValue request)127 JSHandle<EcmaString> ResolveFilenameFromNative(JSThread *thread, JSTaggedValue dirname,
128 JSTaggedValue request)
129 {
130 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
131 CString fullname;
132 CString resolvedFilename;
133 CString dirnameStr = ConvertToString(EcmaString::Cast(dirname.GetTaggedObject()));
134 CString requestStr = ConvertToString(EcmaString::Cast(request.GetTaggedObject()));
135
136 if (requestStr.find("./") == 0) {
137 requestStr = requestStr.substr(2); // 2 : delete './'
138 }
139 int suffixEnd = static_cast<int>(requestStr.find_last_of('.'));
140 if (suffixEnd == -1) {
141 RETURN_HANDLE_IF_ABRUPT_COMPLETION(EcmaString, thread);
142 }
143 if (requestStr[0] == '/') { // absolute FilePath
144 fullname = requestStr.substr(0, suffixEnd) + ".abc";
145 } else {
146 int pos = static_cast<int>(dirnameStr.find_last_of('/'));
147 if (pos == -1) {
148 RETURN_HANDLE_IF_ABRUPT_COMPLETION(EcmaString, thread);
149 }
150 fullname = dirnameStr.substr(0, pos + 1) + requestStr.substr(0, suffixEnd) + ".abc";
151 }
152
153 std::string relativePath = ConvertToStdString(fullname);
154 std::string absPath = "";
155 if (RealPath(relativePath, absPath)) {
156 resolvedFilename = ConvertToString(absPath);
157 return factory->NewFromUtf8(resolvedFilename);
158 }
159 CString msg = "resolve absolute path fail";
160 THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, EcmaString, msg.c_str());
161 }
162
FileExist(const char * filename)163 bool FileExist(const char *filename)
164 {
165 return (access(filename, 0) != -1);
166 }
167
Unlink(const char * filename)168 int Unlink(const char *filename)
169 {
170 return unlink(filename);
171 }
172
TryToRemoveSO(JSThread * thread,JSHandle<SourceTextModule> module)173 bool TryToRemoveSO(JSThread *thread, JSHandle<SourceTextModule> module)
174 {
175 UnloadNativeModuleCallback unloadNativeModuleCallback = thread->GetEcmaVM()->GetUnloadNativeModuleCallback();
176 if (unloadNativeModuleCallback == nullptr) {
177 LOG_ECMA(ERROR) << "unloadNativeModuleCallback is nullptr";
178 return false;
179 }
180
181 CString soName = base::PathHelper::GetStrippedModuleName(ConvertToString(module->GetEcmaModuleRecordName()));
182 return unloadNativeModuleCallback(soName.c_str());
183 }
184
LoadLib(const std::string & libname)185 void *LoadLib(const std::string &libname)
186 {
187 return dlopen(libname.c_str(), RTLD_NOW);
188 }
189
FindSymbol(void * handle,const char * symbol)190 void *FindSymbol(void *handle, const char *symbol)
191 {
192 return dlsym(handle, symbol);
193 }
194
CloseLib(void * handle)195 int CloseLib(void *handle)
196 {
197 return dlclose(handle);
198 }
199
200 } // namespace panda::ecmascript
201