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
33namespace panda::ecmascript {
34class SourceTextModule;
35#ifdef PANDA_TARGET_WINDOWS
36using 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
55using 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
66std::string PUBLIC_API GetFileDelimiter();
67std::string PUBLIC_API GetPathSeparator();
68bool PUBLIC_API RealPath(const std::string &path, std::string &realPath, bool readOnly = true);
69bool PUBLIC_API RealPathByChar(const char *path, char *realPath, int rowLength, bool readOnly);
70void DPrintf(fd_t fd, const std::string &buffer);
71void Close(fd_t fd);
72void FSync(fd_t fd);
73MemMap PUBLIC_API FileMap(const char *fileName, int flag, int prot, int64_t offset = 0);
74MemMap PUBLIC_API FileMapForAlignAddress(const char *fileName, int flag, int prot,
75                                         int64_t offset, uint32_t offStart);
76int PUBLIC_API FileUnMap(MemMap addr);
77CString ResolveFilenameFromNative(JSThread *thread, const CString &dirname, CString request);
78bool PUBLIC_API FileExist(const char *filename);
79int PUBLIC_API Unlink(const char *filename);
80bool TryToRemoveSO(JSThread *thread, JSHandle<SourceTextModule> module);
81void *LoadLib(const std::string &libname);
82void *FindSymbol(void *handle, const char *symbol);
83int CloseLib(void *handle);
84char *LoadLibError();
85}  // namespace panda::ecmascript
86#endif  // ECMASCRIPT_PLATFORM_FILE_H
87