123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- #include "il2cpp-config.h"
- #include "pal_platform.h"
- #if IL2CPP_USES_POSIX_CLASS_LIBRARY_PAL
- #include "os/ClassLibraryPAL/pal_mirror_structs.h"
- #include "os/File.h"
- #include "os/Posix/FileHandle.h"
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #if IL2CPP_HAVE_FCOPYFILE
- #include <copyfile.h>
- #endif
- struct DirectoryEntry;
- extern "C"
- {
- struct DIRWrapper
- {
- DIR* dir;
- DirectoryEntry* result;
- size_t curIndex;
- size_t numEntries;
- #if !IL2CPP_HAVE_REWINDDIR
- char* dirPath;
- #endif
- };
-
- IL2CPP_EXPORT int32_t SystemNative_Stat2(const char* path, struct FileStatus* output);
- IL2CPP_EXPORT int32_t SystemNative_LStat2(const char* path, struct FileStatus* output);
- IL2CPP_EXPORT int32_t SystemNative_Unlink(const char* path);
- IL2CPP_EXPORT int32_t SystemNative_GetReadDirRBufferSize(void);
- IL2CPP_EXPORT int32_t SystemNative_ReadDirR(struct DIRWrapper* dirWrapper, uint8_t* buffer, int32_t bufferSize, struct DirectoryEntry* outputEntry);
- IL2CPP_EXPORT struct DIRWrapper* SystemNative_OpenDir(const char* path);
- IL2CPP_EXPORT int32_t SystemNative_CloseDir(struct DIRWrapper* dirWrapper);
- IL2CPP_EXPORT int32_t SystemNative_MkDir(const char* path, int32_t mode);
- IL2CPP_EXPORT int32_t SystemNative_ChMod(const char* path, int32_t mode);
- IL2CPP_EXPORT int32_t SystemNative_Link(const char* source, const char* linkTarget);
- IL2CPP_EXPORT int32_t SystemNative_Symlink(const char* target, const char* linkPath);
- IL2CPP_EXPORT int32_t SystemNative_ReadLink(const char* path, char* buffer, int32_t bufferSize);
- IL2CPP_EXPORT int32_t SystemNative_Rename(const char* oldPath, const char* newPath);
- IL2CPP_EXPORT int32_t SystemNative_RmDir(const char* path);
- IL2CPP_EXPORT int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd);
- IL2CPP_EXPORT int32_t SystemNative_LChflags(const char* path, uint32_t flags);
- IL2CPP_EXPORT int32_t SystemNative_LChflagsCanSetHiddenFlag();
- }
- #if IL2CPP_HAVE_STAT_TIMESPEC
- #define ST_ATIME_NSEC(statstruct) ((statstruct)->st_atimespec.tv_nsec)
- #define ST_MTIME_NSEC(statstruct) ((statstruct)->st_mtimespec.tv_nsec)
- #define ST_CTIME_NSEC(statstruct) ((statstruct)->st_ctimespec.tv_nsec)
- #else
- #if IL2CPP_HAVE_STAT_TIM
- #define ST_ATIME_NSEC(statstruct) ((statstruct)->st_atim.tv_nsec)
- #define ST_MTIME_NSEC(statstruct) ((statstruct)->st_mtim.tv_nsec)
- #define ST_CTIME_NSEC(statstruct) ((statstruct)->st_ctim.tv_nsec)
- #else
- #if IL2CPP_HAVE_STAT_NSEC
- #define ST_ATIME_NSEC(statstruct) ((statstruct)->st_atimensec)
- #define ST_MTIME_NSEC(statstruct) ((statstruct)->st_mtimensec)
- #define ST_CTIME_NSEC(statstruct) ((statstruct)->st_ctimensec)
- #else
- #define ST_ATIME_NSEC(statstruct) 0
- #define ST_MTIME_NSEC(statstruct) 0
- #define ST_CTIME_NSEC(statstruct) 0
- #endif
- #endif
- #endif
- enum
- {
- FILESTATUS_FLAGS_NONE = 0,
- FILESTATUS_FLAGS_HAS_BIRTHTIME = 1,
- };
- enum
- {
- PAL_UF_HIDDEN = 0x8000
- };
- enum NodeType
- {
- PAL_DT_UNKNOWN = 0,
- PAL_DT_FIFO = 1,
- PAL_DT_CHR = 2,
- PAL_DT_DIR = 4,
- PAL_DT_BLK = 6,
- PAL_DT_REG = 8,
- PAL_DT_LNK = 10,
- PAL_DT_SOCK = 12,
- PAL_DT_WHT = 14
- };
- struct DirectoryEntry
- {
- char* Name;
- int32_t NameLength;
- int32_t InodeType;
- };
- static void ConvertFileStatus(const struct stat_* src, struct FileStatus* dst)
- {
- dst->Dev = (int64_t)src->st_dev;
- dst->Ino = (int64_t)src->st_ino;
- dst->Flags = FILESTATUS_FLAGS_NONE;
- dst->Mode = (int32_t)src->st_mode;
- dst->Uid = src->st_uid;
- dst->Gid = src->st_gid;
- dst->Size = src->st_size;
- dst->ATime = src->st_atime;
- dst->MTime = src->st_mtime;
- dst->CTime = src->st_ctime;
- dst->ATimeNsec = ST_ATIME_NSEC(src);
- dst->MTimeNsec = ST_MTIME_NSEC(src);
- dst->CTimeNsec = ST_CTIME_NSEC(src);
- #if IL2CPP_HAVE_STAT_BIRTHTIME
- dst->BirthTime = src->st_birthtimespec.tv_sec;
- dst->BirthTimeNsec = src->st_birthtimespec.tv_nsec;
- dst->Flags |= FILESTATUS_FLAGS_HAS_BIRTHTIME;
- #else
-
- dst->BirthTime = 0;
- dst->BirthTimeNsec = 0;
- #endif
- #if defined(IL2CPP_HAVE_STAT_FLAGS) && defined(UF_HIDDEN)
- dst->UserFlags = ((src->st_flags & UF_HIDDEN) == UF_HIDDEN) ? PAL_UF_HIDDEN : 0;
- #else
- dst->UserFlags = 0;
- #endif
- }
- #if IL2CPP_HAVE_REMAP_PATH
- #define REMAP_PATH(path) pal_remap_path(path).c_str()
- #else
- #define REMAP_PATH(path) path
- #endif
- int32_t SystemNative_Stat2(const char* path, struct FileStatus* output)
- {
- struct stat_ result = {};
- int ret;
- while ((ret = stat_(REMAP_PATH(path), &result)) < 0 && errno == EINTR)
- ;
- if (ret == 0)
- {
- ConvertFileStatus(&result, output);
- }
- return ret;
- }
- int32_t SystemNative_LStat2(const char* path, struct FileStatus* output)
- {
- struct stat_ result = {};
- int ret = lstat_(REMAP_PATH(path), &result);
- if (ret == 0)
- {
- ConvertFileStatus(&result, output);
- }
- return ret;
- }
- int32_t SystemNative_Unlink(const char* path)
- {
- int32_t result = 0;
- while ((result = unlink(REMAP_PATH(path))) < 0 && errno == EINTR)
- ;
- return result;
- }
- static void ConvertDirent(const struct dirent* entry, struct DirectoryEntry* outputEntry)
- {
-
-
-
- outputEntry->Name = strdup(entry->d_name);
- IL2CPP_ASSERT(outputEntry->Name != NULL);
- #if !defined(DT_UNKNOWN)
-
-
-
- outputEntry->InodeType = PAL_DT_UNKNOWN;
- #else
- outputEntry->InodeType = (int32_t)entry->d_type;
- #endif
- #if IL2CPP_HAVE_DIRENT_NAME_LEN
- outputEntry->NameLength = entry->d_namlen;
- #else
- outputEntry->NameLength = -1;
- #endif
- }
- #if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
- static const size_t dirent_alignment = 8;
- #endif
- int32_t SystemNative_GetReadDirRBufferSize(void)
- {
- #if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
-
- IL2CPP_ASSERT(sizeof(struct dirent) < 2048);
-
- return sizeof(struct dirent) + dirent_alignment - 1;
- #else
- return 0;
- #endif
- }
- static int CompareByName(const void *p1, const void *p2)
- {
- auto directoryEntry1 = ((struct DirectoryEntry*)p1);
- auto directoryEntry2 = ((struct DirectoryEntry*)p2);
-
-
- if (directoryEntry1->Name == directoryEntry2->Name)
- return 0;
- if (directoryEntry1->Name == NULL)
- return 1;
- if (directoryEntry2->Name == NULL)
- return -1;
- return strcmp(directoryEntry1->Name, directoryEntry2->Name);
- }
- int32_t SystemNative_ReadDirR(struct DIRWrapper* dirWrapper, uint8_t* buffer, int32_t bufferSize, struct DirectoryEntry* outputEntry)
- {
- IL2CPP_ASSERT(dirWrapper != NULL);
- IL2CPP_ASSERT(dirWrapper->dir != NULL);
- IL2CPP_ASSERT(outputEntry != NULL);
- #if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
- IL2CPP_ASSERT(buffer != NULL);
-
- struct dirent* entry = (struct dirent*)((size_t)(buffer + dirent_alignment - 1) & ~(dirent_alignment - 1));
-
- if ((buffer + bufferSize) < ((uint8_t*)entry + sizeof(struct dirent)))
- {
- IL2CPP_ASSERT(false && "Buffer size too small; use GetReadDirRBufferSize to get required buffer size");
- return ERANGE;
- }
- struct dirent* result = NULL;
- #ifdef _AIX
-
-
-
-
-
-
- errno = 0;
- int error = readdir_r(dir, entry, &result);
- if (error == 9)
- {
- memset(outputEntry, 0, sizeof(*outputEntry));
- return errno == 0 ? -1 : errno;
- }
- #else
- int error = readdir_r(dir, entry, &result);
-
- if (error != 0)
- {
- IL2CPP_ASSERT(error > 0);
- memset(outputEntry, 0, sizeof(*outputEntry));
- return error;
- }
-
- if (result == NULL)
- {
- memset(outputEntry, 0, sizeof(*outputEntry));
- return -1;
- }
- #endif
-
- IL2CPP_ASSERT(result == entry);
- #else
- (void)buffer;
- (void)bufferSize;
- errno = 0;
- bool endOfEntries = false;
- if (!dirWrapper->result)
- {
- struct dirent* entry;
- size_t numEntries = 0;
- while ((entry = readdir(dirWrapper->dir)))
- numEntries++;
- if (numEntries)
- {
-
- dirWrapper->result = (DirectoryEntry*)calloc(numEntries, sizeof(struct DirectoryEntry));
- dirWrapper->curIndex = 0;
- #if IL2CPP_HAVE_REWINDDIR
- rewinddir(dirWrapper->dir);
- #else
- closedir(dirWrapper->dir);
- dirWrapper->dir = opendir(dirWrapper->dirPath);
- #endif
-
-
-
-
- size_t index = 0;
- while ((entry = readdir(dirWrapper->dir)) && index < numEntries)
- {
- ConvertDirent(entry, &dirWrapper->result[index]);
- index++;
- }
- qsort(dirWrapper->result, numEntries, sizeof(struct DirectoryEntry), CompareByName);
- dirWrapper->numEntries = index;
- }
- }
- if (dirWrapper->curIndex < dirWrapper->numEntries)
- {
- *outputEntry = dirWrapper->result[dirWrapper->curIndex];
- dirWrapper->curIndex++;
- }
- else
- {
- endOfEntries = true;
- }
-
- if (endOfEntries)
- {
- memset(outputEntry, 0, sizeof(*outputEntry));
-
- if (errno != 0)
- {
- IL2CPP_ASSERT(errno == EBADF && "Invalid directory stream descriptor dir" && errno);
- return errno;
- }
- return -1;
- }
- #endif
- return 0;
- }
- struct DIRWrapper* SystemNative_OpenDir(const char* path)
- {
- const char* remapped_path = NULL;
- #if IL2CPP_HAVE_REMAP_PATH
- auto remapped_path_string = pal_remap_path(path);
- remapped_path = remapped_path_string.c_str();
- #else
- remapped_path = path;
- #endif
- DIR* dir = opendir(remapped_path);
- if (dir == NULL)
- return NULL;
- struct DIRWrapper* ret = (struct DIRWrapper*)malloc(sizeof(struct DIRWrapper));
- ret->dir = dir;
- ret->result = NULL;
- ret->curIndex = 0;
- ret->numEntries = 0;
- #if !IL2CPP_HAVE_REWINDDIR
- ret->dirPath = strdup(remapped_path);
- #endif
- return ret;
- }
- int32_t SystemNative_CloseDir(struct DIRWrapper* dirWrapper)
- {
- IL2CPP_ASSERT(dirWrapper != NULL);
- int32_t ret = closedir(dirWrapper->dir);
- if (dirWrapper->result)
- {
- for (int i = 0; i < dirWrapper->numEntries; i++)
- free(dirWrapper->result[i].Name);
- free(dirWrapper->result);
- }
- dirWrapper->result = NULL;
- #if !IL2CPP_HAVE_REWINDDIR
- if (dirWrapper->dirPath)
- free(dirWrapper->dirPath);
- #endif
- free(dirWrapper);
- return ret;
- }
- int32_t SystemNative_MkDir(const char* path, int32_t mode)
- {
- int32_t result = 0;
- while ((result = mkdir(REMAP_PATH(path), (mode_t)mode)) < 0 && errno == EINTR)
- ;
- return result;
- }
- int32_t SystemNative_ChMod(const char* path, int32_t mode)
- {
- int32_t result = 0;
- while ((result = chmod_(REMAP_PATH(path), (mode_t)mode)) < 0 && errno == EINTR)
- ;
- return result;
- }
- int32_t SystemNative_Link(const char* source, const char* linkTarget)
- {
- int32_t result = 0;
- while ((result = link_(REMAP_PATH(source), REMAP_PATH(linkTarget))) < 0 && errno == EINTR)
- ;
- return result;
- }
- int32_t SystemNative_Symlink(const char* target, const char* linkPath)
- {
- return symlink_(REMAP_PATH(target), REMAP_PATH(linkPath));
- }
- int32_t SystemNative_ReadLink(const char* path, char* buffer, int32_t bufferSize)
- {
- IL2CPP_ASSERT(buffer != NULL || bufferSize == 0);
- IL2CPP_ASSERT(bufferSize >= 0);
- if (bufferSize <= 0)
- {
- errno = EINVAL;
- return -1;
- }
- ssize_t count = readlink_(REMAP_PATH(path), buffer, (size_t)bufferSize);
- IL2CPP_ASSERT(count >= -1 && count <= bufferSize);
- return (int32_t)count;
- }
- int32_t SystemNative_Rename(const char* oldPath, const char* newPath)
- {
- int32_t result;
- while ((result = rename(REMAP_PATH(oldPath), REMAP_PATH(newPath))) < 0 && errno == EINTR)
- ;
- return result;
- }
- int32_t SystemNative_RmDir(const char* path)
- {
- int32_t result = 0;
- while ((result = rmdir(REMAP_PATH(path))) < 0 && errno == EINTR)
- ;
- return result;
- }
- #if !IL2CPP_HAVE_FCOPYFILE
- static int32_t CopyFile_ReadWrite(int inFd, int outFd)
- {
-
- const int BufferLength = 80 * 1024 * sizeof(char);
- char* buffer = (char*)malloc(BufferLength);
- if (buffer == NULL)
- {
- return -1;
- }
-
- while (true)
- {
-
- ssize_t bytesRead;
- while ((bytesRead = read(inFd, buffer, BufferLength)) < 0 && errno == EINTR)
- ;
- if (bytesRead == -1)
- {
- int tmp = errno;
- free(buffer);
- errno = tmp;
- return -1;
- }
- if (bytesRead == 0)
- {
- break;
- }
- IL2CPP_ASSERT(bytesRead > 0);
-
- ssize_t offset = 0;
- while (bytesRead > 0)
- {
- ssize_t bytesWritten;
- while ((bytesWritten = write(outFd, buffer + offset, (size_t)bytesRead)) < 0 && errno == EINTR)
- ;
- if (bytesWritten == -1)
- {
- int tmp = errno;
- free(buffer);
- errno = tmp;
- return -1;
- }
- IL2CPP_ASSERT(bytesWritten >= 0);
- bytesRead -= bytesWritten;
- offset += bytesWritten;
- }
- }
- free(buffer);
- return 0;
- }
- #endif
- int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd)
- {
- #if IL2CPP_HAVE_CUSTOM_COPYFILE
- return pal_custom_copy_file(sourceFd, destinationFd);
- #else
- int inFd, outFd;
- inFd = il2cpp::os::File::IsHandleOpenFileHandle(sourceFd) ? reinterpret_cast<il2cpp::os::FileHandle*>(sourceFd)->fd : static_cast<int>(sourceFd);
- outFd = il2cpp::os::File::IsHandleOpenFileHandle(destinationFd) ? reinterpret_cast<il2cpp::os::FileHandle*>(destinationFd)->fd : static_cast<int>(destinationFd);
- #if IL2CPP_HAVE_FCOPYFILE
-
-
-
- return fcopyfile(inFd, outFd, NULL, COPYFILE_ALL) == 0 ? 0 : -1;
- #else
-
- int ret;
- struct stat_ sourceStat;
- bool copied = false;
-
- while ((ret = fstat_(inFd, &sourceStat)) < 0 && errno == EINTR)
- ;
- if (ret != 0)
- {
-
- return -1;
- }
-
-
- while ((ret = fchmod(outFd, sourceStat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) < 0 && errno == EINTR)
- ;
- #if !IL2CPP_CANNOT_MODIFY_FILE_PERMISSIONS
- if (ret != 0)
- {
- return -1;
- }
- #endif
- #if IL2CPP_HAVE_SENDFILE_4
-
-
-
-
-
- uint64_t size = (uint64_t)sourceStat.st_size;
-
-
- while (size > 0)
- {
- ssize_t sent = sendfile(outFd, inFd, NULL, (size >= SSIZE_MAX ? SSIZE_MAX : (size_t)size));
- if (sent < 0)
- {
- if (errno != EINVAL && errno != ENOSYS)
- {
- return -1;
- }
- else
- {
- break;
- }
- }
- else
- {
- IL2CPP_ASSERT((size_t)sent <= size);
- size -= (size_t)sent;
- }
- }
- if (size == 0)
- {
- copied = true;
- }
-
-
-
- #endif
-
- if (!copied && CopyFile_ReadWrite(inFd, outFd) != 0)
- {
- return -1;
- }
-
-
-
-
- #if IL2CPP_HAVE_FUTIMENS
-
- struct timespec origTimes[2];
- origTimes[0].tv_sec = (time_t)sourceStat.st_atime;
- origTimes[0].tv_nsec = ST_ATIME_NSEC(&sourceStat);
- origTimes[1].tv_sec = (time_t)sourceStat.st_mtime;
- origTimes[1].tv_nsec = ST_MTIME_NSEC(&sourceStat);
- while ((ret = futimens(outFd, origTimes)) < 0 && errno == EINTR)
- ;
- #elif IL2CPP_HAVE_FUTIMES
- struct timeval origTimes[2];
- origTimes[0].tv_sec = sourceStat.st_atime;
- origTimes[0].tv_usec = ST_ATIME_NSEC(&sourceStat) / 1000;
- origTimes[1].tv_sec = sourceStat.st_mtime;
- origTimes[1].tv_usec = ST_MTIME_NSEC(&sourceStat) / 1000;
- while ((ret = futimes(outFd, origTimes)) < 0 && errno == EINTR)
- ;
- #endif
- #if !IL2CPP_CANNOT_MODIFY_FILE_PERMISSIONS
- if (ret != 0)
- {
- return -1;
- }
- #endif
- return 0;
- #endif
- #endif
- }
- int32_t SystemNative_LChflags(const char* path, uint32_t flags)
- {
- #if defined(UF_HIDDEN) && defined(IL2CPP_HAVE_STAT_FLAGS) && defined(IL2CPP_HAVE_LCHFLAGS)
- int32_t result;
- while ((result = lchflags(path, flags)) < 0 && errno == EINTR)
- ;
- return result;
- #else
- return -1;
- #endif
- }
- int32_t SystemNative_LChflagsCanSetHiddenFlag(void)
- {
- #if defined(UF_HIDDEN) && defined(IL2CPP_HAVE_STAT_FLAGS) && defined(IL2CPP_HAVE_LCHFLAGS)
- return true;
- #else
- return false;
- #endif
- }
- #endif
|