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 <windef.h>
19 #include <winbase.h>
20 #include <winnt.h>
21 #include <climits>
22 #include <fileapi.h>
23
24 #ifdef ERROR
25 #undef ERROR
26 #endif
27
28 #ifdef VOID
29 #undef VOID
30 #endif
31
32 #ifdef CONST
33 #undef CONST
34 #endif
35
36 #include "ecmascript/ecma_macros.h"
37 #include "ecmascript/ecma_string.h"
38 #include "ecmascript/js_tagged_value-inl.h"
39 #include "ecmascript/log_wrapper.h"
40
41 namespace panda::ecmascript {
GetFileDelimiter()42 std::string GetFileDelimiter()
43 {
44 return ";";
45 }
46
GetPathSeparator()47 std::string GetPathSeparator()
48 {
49 return "\\";
50 }
51
RealPath(const std::string & path,std::string & realPath,bool readOnly)52 bool RealPath(const std::string &path, std::string &realPath, [[maybe_unused]] bool readOnly)
53 {
54 realPath = "";
55 if (path.empty() || path.size() > PATH_MAX) {
56 LOG_ECMA(WARN) << "File path is illeage";
57 return false;
58 }
59 char buffer[PATH_MAX] = { '\0' };
60 if (!_fullpath(buffer, path.c_str(), sizeof(buffer) - 1)) {
61 LOG_ECMA(WARN) << "File path:" << path << " full path failure";
62 return false;
63 }
64 realPath = std::string(buffer);
65 return true;
66 }
67
RealPathByChar(const char * path,char * realPath,int rowLength,bool readOnly)68 bool RealPathByChar(const char *path, char *realPath, int rowLength, bool readOnly)
69 {
70 (void)path;
71 (void)realPath;
72 (void)rowLength;
73 (void)readOnly;
74 return false;
75 }
76
DPrintf(fd_t fd,const std::string & buffer)77 void DPrintf(fd_t fd, const std::string &buffer)
78 {
79 LOG_ECMA(DEBUG) << "Unsupport dprintf fd(" << fd << ") in windows, buffer:" << buffer;
80 }
81
FSync(fd_t fd)82 void FSync(fd_t fd)
83 {
84 LOG_ECMA(DEBUG) << "Unsupport fsync fd(" << fd << ") in windows";
85 }
86
FdsanExchangeOwnerTag(fd_t fd)87 void FdsanExchangeOwnerTag(fd_t fd)
88 {
89 LOG_ECMA(DEBUG) << "Unsupport FdsanExchangeOwnerTag fd(" << fd << ") in windows";
90 }
91
Close(fd_t fd)92 void Close(fd_t fd)
93 {
94 CloseHandle(fd);
95 }
96
FileMap(const char * fileName,int flag,int prot,int64_t offset)97 MemMap FileMap(const char *fileName, int flag, int prot, int64_t offset)
98 {
99 if (prot == PAGE_PROT_READWRITE) {
100 flag |= FILE_RDONLY | FILE_WRONLY;
101 }
102 fd_t fd = CreateFile(fileName, flag, 0, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
103 if (fd == INVALID_FD) {
104 LOG_ECMA(ERROR) << fileName << " file open failed";
105 return MemMap();
106 }
107
108 LARGE_INTEGER fileSize;
109 if (!GetFileSizeEx(fd, &fileSize)) {
110 CloseHandle(fd);
111 LOG_ECMA(ERROR) << "GetFileSize failed with error code:" << GetLastError();
112 return MemMap();
113 }
114 auto size = fileSize.QuadPart;
115 if (size <= 0) {
116 CloseHandle(fd);
117 LOG_HOST_TOOL_ERROR << fileName << " file is empty";
118 return MemMap();
119 }
120
121 // 32: high 32 bits
122 fd_t extra = CreateFileMapping(fd, NULL, prot, size >> 32, size & 0xffffffff, nullptr);
123 if (extra == nullptr) {
124 CloseHandle(fd);
125 LOG_ECMA(ERROR) << "CreateFileMapping failed with error code:" << GetLastError();
126 return MemMap();
127 }
128 int accessor = (prot == PAGE_PROT_READ) ? FILE_MAP_READ : FILE_MAP_WRITE;
129 void *addr = MapViewOfFile(extra, accessor, offset >> 32, offset & 0xffffffff, size);
130 CloseHandle(extra);
131 CloseHandle(fd);
132 if (addr == nullptr) {
133 LOG_ECMA(ERROR) << "MapViewOfFile failed with error code:" << GetLastError();
134 }
135 return MemMap(addr, size);
136 }
137
CreateFileMap(const char * fileName,int fileSize,int flag,int prot)138 MemMap CreateFileMap([[maybe_unused]] const char *fileName, [[maybe_unused]] int fileSize,
139 [[maybe_unused]] int flag, [[maybe_unused]] int prot)
140 {
141 LOG_ECMA(INFO) << "Unsupport CreateFileMap";
142 return MemMap(nullptr, 0);
143 }
144
FileMapForAlignAddressByFd(const fd_t fd,int prot,int64_t offset,uint32_t offStart)145 MemMap FileMapForAlignAddressByFd(const fd_t fd, int prot, int64_t offset, uint32_t offStart)
146 {
147 // AOT not used, previewer used
148 LOG_ECMA(INFO) << "Don't used fd:" << fd
149 << " prot:" << prot << " offset:" << offset << " offStart:" << offStart;
150 return MemMap();
151 }
152
FileUnMap(MemMap addr)153 int FileUnMap(MemMap addr)
154 {
155 if (UnmapViewOfFile(addr.GetOriginAddr()) == 0) {
156 return FILE_FAILED;
157 }
158 return FILE_SUCCESS;
159 }
160
FileSync(MemMap addr,int flag)161 int FileSync(MemMap addr, int flag)
162 {
163 LOG_ECMA(INFO) << "Don't used MemMap:" << addr.GetOriginAddr() << ", flag:" << flag;
164 return -1;
165 }
166
ResolveFilenameFromNative(JSThread * thread,const CString & dirname,CString request)167 CString ResolveFilenameFromNative(JSThread *thread, const CString &dirname,
168 CString request)
169 {
170 std::string relativePath;
171 int suffixEnd = static_cast<int>(request.find_last_of('.'));
172 if (request[1] == ':') { // absoluteFilePath
173 relativePath = request.substr(0, suffixEnd) + ".abc";
174 } else {
175 int pos = static_cast<int>(dirname.find_last_of('\\'));
176 relativePath = dirname.substr(0, pos + 1) + request.substr(0, suffixEnd) + ".abc";
177 }
178
179 std::string absPath;
180 if (RealPath(relativePath, absPath)) {
181 return absPath.c_str();
182 }
183 THROW_REFERENCE_ERROR_AND_RETURN(thread, "resolve absolute path fail", CString());
184 }
185
FileExist(const char * filename)186 bool FileExist(const char *filename)
187 {
188 return (_access(filename, 0) != -1);
189 }
190
Unlink(const char * filename)191 int Unlink(const char *filename)
192 {
193 return _unlink(filename);
194 }
195
LoadLib(const std::string & liname)196 void *LoadLib([[maybe_unused]] const std::string &liname)
197 {
198 LOG_ECMA(INFO) << "Unsupport LoadLib";
199 return nullptr;
200 }
201
FindSymbol(void * handle,const char * symbol)202 void *FindSymbol([[maybe_unused]] void *handle, [[maybe_unused]] const char *symbol)
203 {
204 LOG_ECMA(INFO) << "Unsupport FindSymbol";
205 return nullptr;
206 }
207
CloseLib(void * handle)208 int CloseLib([[maybe_unused]] void *handle)
209 {
210 LOG_ECMA(INFO) << "Unsupport CloseLib";
211 return 0;
212 }
213
LoadLibError()214 char *LoadLibError()
215 {
216 LOG_ECMA(INFO) << "Unsupport LoadLibError";
217 return nullptr;
218 }
219
DeleteFilesWithSuffix(const std::string & dirPath,const std::string & suffix)220 void DeleteFilesWithSuffix(const std::string &dirPath, const std::string &suffix)
221 {
222 LOG_ECMA(INFO) << "Unsupport dirPath: " << dirPath << ", suffix: " << suffix;
223 }
224 } // namespace panda::ecmascript
225