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
RealPath(const std::string & path,std::string & realPath,bool readOnly)36 bool RealPath(const std::string &path, std::string &realPath, bool readOnly)
37 {
38 if (path.empty() || path.size() > PATH_MAX) {
39 LOG_ECMA(WARN) << "File path is illeage";
40 return false;
41 }
42 char buffer[PATH_MAX] = { '\0' };
43 if (!realpath(path.c_str(), buffer)) {
44 // Maybe file does not exist.
45 if (!readOnly && errno == ENOENT) {
46 realPath = path;
47 return true;
48 }
49 LOG_ECMA(ERROR) << "File path:" << path << " realpath failure";
50 return false;
51 }
52 realPath = std::string(buffer);
53 return true;
54 }
55
DPrintf(fd_t fd,const std::string & buffer)56 void DPrintf(fd_t fd, const std::string &buffer)
57 {
58 int ret = dprintf(fd, "%s", buffer.c_str());
59 if (ret < 0) {
60 LOG_ECMA(DEBUG) << "dprintf fd(" << fd << ") failed";
61 }
62 }
63
FSync(fd_t fd)64 void FSync(fd_t fd)
65 {
66 int ret = fsync(fd);
67 if (ret < 0) {
68 LOG_ECMA(DEBUG) << "fsync fd(" << fd << ") failed";
69 }
70 }
71
Close(fd_t fd)72 void Close(fd_t fd)
73 {
74 close(fd);
75 }
76
FileMap(const char * fileName,int flag,int prot,int64_t offset)77 MemMap FileMap(const char *fileName, int flag, int prot, int64_t offset)
78 {
79 fd_t fd = open(fileName, flag);
80 if (fd == INVALID_FD) {
81 LOG_ECMA(ERROR) << fileName << " file open failed";
82 return MemMap();
83 }
84
85 off_t size = lseek(fd, 0, SEEK_END);
86 if (size <= 0) {
87 close(fd);
88 LOG_ECMA(ERROR) << fileName << " file is empty";
89 return MemMap();
90 }
91
92 void *addr = mmap(nullptr, size, prot, MAP_PRIVATE, fd, offset);
93 close(fd);
94 return MemMap(addr, size);
95 }
96
FileUnMap(MemMap addr)97 int FileUnMap(MemMap addr)
98 {
99 return munmap(addr.GetOriginAddr(), addr.GetSize());
100 }
101
ResolveFilenameFromNative(JSThread * thread,JSTaggedValue dirname,JSTaggedValue request)102 JSHandle<EcmaString> ResolveFilenameFromNative(JSThread *thread, JSTaggedValue dirname,
103 JSTaggedValue request)
104 {
105 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
106 CString fullname;
107 CString resolvedFilename;
108 CString dirnameStr = ConvertToString(EcmaString::Cast(dirname.GetTaggedObject()));
109 CString requestStr = ConvertToString(EcmaString::Cast(request.GetTaggedObject()));
110
111 if (requestStr.find("./") == 0) {
112 requestStr = requestStr.substr(2); // 2 : delete './'
113 }
114 int suffixEnd = static_cast<int>(requestStr.find_last_of('.'));
115 if (suffixEnd == -1) {
116 RETURN_HANDLE_IF_ABRUPT_COMPLETION(EcmaString, thread);
117 }
118 if (requestStr[0] == '/') { // absolute FilePath
119 fullname = requestStr.substr(0, suffixEnd) + ".abc";
120 } else {
121 int pos = static_cast<int>(dirnameStr.find_last_of('/'));
122 if (pos == -1) {
123 RETURN_HANDLE_IF_ABRUPT_COMPLETION(EcmaString, thread);
124 }
125 fullname = dirnameStr.substr(0, pos + 1) + requestStr.substr(0, suffixEnd) + ".abc";
126 }
127
128 std::string relativePath = ConvertToStdString(fullname);
129 std::string absPath = "";
130 if (RealPath(relativePath, absPath)) {
131 resolvedFilename = ConvertToString(absPath);
132 return factory->NewFromUtf8(resolvedFilename);
133 }
134 CString msg = "resolve absolute path fail";
135 THROW_NEW_ERROR_AND_RETURN_HANDLE(thread, ErrorType::REFERENCE_ERROR, EcmaString, msg.c_str());
136 }
137
FileExist(const char * filename)138 bool FileExist(const char *filename)
139 {
140 return (access(filename, 0) != -1);
141 }
142
Unlink(const char * filename)143 int Unlink(const char *filename)
144 {
145 return unlink(filename);
146 }
147
TryToRemoveSO(JSThread * thread,JSHandle<SourceTextModule> module)148 bool TryToRemoveSO(JSThread *thread, JSHandle<SourceTextModule> module)
149 {
150 UnloadNativeModuleCallback unloadNativeModuleCallback = thread->GetEcmaVM()->GetUnloadNativeModuleCallback();
151 if (unloadNativeModuleCallback == nullptr) {
152 LOG_ECMA(ERROR) << "unloadNativeModuleCallback is nullptr";
153 return false;
154 }
155
156 CString soName = base::PathHelper::GetStrippedModuleName(ConvertToString(module->GetEcmaModuleRecordName()));
157 return unloadNativeModuleCallback(soName.c_str());
158 }
159 } // namespace panda::ecmascript
160