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 #ifndef ECMASCRIPT_PLATFORM_FILE_H 17 #define ECMASCRIPT_PLATFORM_FILE_H 18 19 #ifdef PANDA_TARGET_WINDOWS 20 #include <windef.h> 21 #include <winbase.h> 22 #include <winnt.h> 23 #else 24 #include <fcntl.h> 25 #include <dlfcn.h> 26 #endif 27 28 #include <string> 29 30 #include "ecmascript/ecma_string.h" 31 #include "ecmascript/js_tagged_value.h" 32 33 namespace panda::ecmascript { 34 class SourceTextModule; 35 #ifdef PANDA_TARGET_WINDOWS 36 using fd_t = HANDLE; 37 #define INVALID_FD INVALID_HANDLE_VALUE 38 39 #define FILE_RDONLY GENERIC_READ 40 #define FILE_WRONLY GENERIC_WRITE 41 #define FILE_RDWR (GENERIC_READ | GENERIC_WRITE) 42 43 #ifdef ERROR 44 #undef ERROR 45 #endif 46 47 #ifdef VOID 48 #undef VOID 49 #endif 50 51 #ifdef CONST 52 #undef CONST 53 #endif 54 #else 55 using fd_t = int; 56 #define INVALID_FD (-1) 57 58 #define FILE_RDONLY O_RDONLY 59 #define FILE_WRONLY O_WRONLY 60 #define FILE_RDWR O_RDWR 61 #endif 62 63 #define FILE_SUCCESS 1 64 #define FILE_FAILED 0 65 66 std::string GetFileDelimiter(); 67 std::string GetPathSeparator(); 68 bool RealPath(const std::string &path, std::string &realPath, bool readOnly = true); 69 void DPrintf(fd_t fd, const std::string &buffer); 70 void Close(fd_t fd); 71 void FSync(fd_t fd); 72 MemMap FileMap(const char *fileName, int flag, int prot, int64_t offset = 0); 73 MemMap FileMapForAlignAddress(const char *fileName, int flag, int prot, 74 int64_t offset, uint32_t offStart); 75 int FileUnMap(MemMap addr); 76 JSHandle<EcmaString> ResolveFilenameFromNative(JSThread *thread, JSTaggedValue dirname, 77 JSTaggedValue request); 78 bool FileExist(const char *filename); 79 int Unlink(const char *filename); 80 bool TryToRemoveSO(JSThread *thread, JSHandle<SourceTextModule> module); 81 void *LoadLib(const std::string &libname); 82 void *FindSymbol(void *handle, const char *symbol); 83 int CloseLib(void *handle); 84 } // namespace panda::ecmascript 85 #endif // ECMASCRIPT_PLATFORM_FILE_H 86