libromfs_wiiu/source/romfs_dev.cpp

968 lines
27 KiB
C++
Raw Permalink Normal View History

2022-02-13 12:12:26 +01:00
#include <coreinit/cache.h>
2022-08-22 13:54:22 +02:00
#include <coreinit/filesystem_fsa.h>
#include <coreinit/mutex.h>
2021-01-01 01:43:50 +01:00
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
2022-02-13 12:12:26 +01:00
#include <malloc.h>
2021-01-01 01:43:50 +01:00
#include <string.h>
#include <sys/iosupport.h>
#include <sys/param.h>
2021-01-01 01:43:50 +01:00
#include <unistd.h>
#include "romfs_dev.h"
2022-09-04 21:32:52 +02:00
#include <coreinit/debug.h>
2022-04-14 22:37:00 +02:00
#include <mutex>
2021-01-01 01:43:50 +01:00
typedef struct romfs_mount {
devoptab_t device;
bool setup;
RomfsSource fd_type;
int32_t id;
int32_t fd;
time_t mtime;
uint64_t offset;
romfs_header header;
romfs_dir *cwd;
uint32_t *dirHashTable, *fileHashTable;
void *dirTable, *fileTable;
char name[32];
2022-08-22 13:54:22 +02:00
FSAFileHandle cafe_fd;
FSAClientHandle cafe_client;
OSMutex cafe_mutex;
2021-01-01 01:43:50 +01:00
} romfs_mount;
extern int __system_argc;
extern char **__system_argv;
//static char __thread __component[PATH_MAX+1];
static char __component[PATH_MAX + 1];
2022-02-13 12:12:26 +01:00
#define romFS_root(m) ((romfs_dir *) (m)->dirTable)
#define romFS_none ((uint32_t) ~0)
2021-01-01 01:43:50 +01:00
#define romFS_dir_mode (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH)
#define romFS_file_mode (S_IFREG | S_IRUSR | S_IRGRP | S_IROTH)
static romfs_dir *romFS_dir(romfs_mount *mount, uint32_t off) {
if (off + sizeof(romfs_dir) > mount->header.dirTableSize) { return NULL; }
2021-09-24 17:10:03 +02:00
romfs_dir *curDir = ((romfs_dir *) ((uint8_t *) mount->dirTable + off));
2021-01-01 01:43:50 +01:00
if (off + sizeof(romfs_dir) + curDir->nameLen > mount->header.dirTableSize) { return NULL; }
return curDir;
}
static romfs_file *romFS_file(romfs_mount *mount, uint32_t off) {
if (off + sizeof(romfs_file) > mount->header.fileTableSize) { return NULL; }
2021-09-24 17:10:03 +02:00
romfs_file *curFile = ((romfs_file *) ((uint8_t *) mount->fileTable + off));
2021-01-01 01:43:50 +01:00
if (off + sizeof(romfs_file) + curFile->nameLen > mount->header.fileTableSize) { return NULL; }
return curFile;
}
static ssize_t _romfs_read(romfs_mount *mount, uint64_t readOffset, void *buffer, uint64_t readSize) {
if (readSize == 0) {
return 0;
}
2022-04-14 22:37:00 +02:00
uint64_t pos = mount->offset + readOffset;
2021-01-01 01:43:50 +01:00
if (mount->fd_type == RomfsSource_FileDescriptor) {
off_t seek_offset = lseek(mount->fd, pos, SEEK_SET);
2022-04-14 22:37:00 +02:00
if (seek_offset < 0 || (off_t) pos != seek_offset) {
2021-01-01 01:43:50 +01:00
return -1;
}
return read(mount->fd, buffer, readSize);
2021-01-01 01:43:50 +01:00
} else if (mount->fd_type == RomfsSource_FileDescriptor_CafeOS) {
2022-08-22 13:54:22 +02:00
FSError status;
uint32_t bytesRead = 0;
__attribute__((aligned(0x40))) uint8_t alignedBuffer[0x40];
uint64_t len = readSize;
void *ptr = buffer;
while (bytesRead < len) {
// only use input buffer if cache-aligned and read size is a multiple of cache line size
// otherwise read into alignedBuffer
uint8_t *tmp = (uint8_t *) ptr;
size_t size = len - bytesRead;
if (size < 0x40) {
// read partial cache-line back-end
tmp = alignedBuffer;
} else if ((uintptr_t) ptr & 0x3F) {
// read partial cache-line front-end
tmp = alignedBuffer;
size = MIN(size, 0x40 - ((uintptr_t) ptr & 0x3F));
} else {
// read whole cache lines
size &= ~0x3F;
}
// Limit each request to 1 MiB
if (size > 0x100000) {
size = 0x100000;
}
status = FSAReadFileWithPos(mount->cafe_client, tmp, 1, size, pos, mount->cafe_fd, 0);
if (status < 0) {
if (bytesRead != 0) {
return bytesRead; // error after partial read
}
2021-01-01 01:43:50 +01:00
return -1;
}
if (tmp == alignedBuffer) {
memcpy(ptr, alignedBuffer, status);
2021-01-01 01:43:50 +01:00
}
pos += (uint32_t) status;
bytesRead += (uint32_t) status;
ptr = (void *) (((uint32_t) ptr) + status);
if ((size_t) status != size) {
return bytesRead; // partial read
}
2021-01-01 01:43:50 +01:00
}
return bytesRead;
2021-01-01 01:43:50 +01:00
}
return -1;
2021-01-01 01:43:50 +01:00
}
static bool _romfs_read_chk(romfs_mount *mount, uint64_t offset, void *buffer, uint64_t size) {
2022-04-14 22:37:00 +02:00
return _romfs_read(mount, offset, buffer, size) == (int64_t) size;
2021-01-01 01:43:50 +01:00
}
//-----------------------------------------------------------------------------
static int romfs_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode);
static int romfs_close(struct _reent *r, void *fd);
static ssize_t romfs_read(struct _reent *r, void *fd, char *ptr, size_t len);
static off_t romfs_seek(struct _reent *r, void *fd, off_t pos, int dir);
static int romfs_fstat(struct _reent *r, void *fd, struct stat *st);
static int romfs_stat(struct _reent *r, const char *path, struct stat *st);
static int romfs_chdir(struct _reent *r, const char *path);
static DIR_ITER *romfs_diropen(struct _reent *r, DIR_ITER *dirState, const char *path);
static int romfs_dirreset(struct _reent *r, DIR_ITER *dirState);
static int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat);
static int romfs_dirclose(struct _reent *r, DIR_ITER *dirState);
typedef struct {
romfs_mount *mount;
romfs_file *file;
uint64_t offset, pos;
} romfs_fileobj;
typedef struct {
romfs_mount *mount;
romfs_dir *dir;
uint32_t state;
uint32_t childDir;
uint32_t childFile;
} romfs_diriter;
static const devoptab_t romFS_devoptab =
{
.structSize = sizeof(romfs_fileobj),
.open_r = romfs_open,
.close_r = romfs_close,
.read_r = romfs_read,
.seek_r = romfs_seek,
.fstat_r = romfs_fstat,
.stat_r = romfs_stat,
.chdir_r = romfs_chdir,
.dirStateSize = sizeof(romfs_diriter),
.diropen_r = romfs_diropen,
.dirreset_r = romfs_dirreset,
.dirnext_r = romfs_dirnext,
.dirclose_r = romfs_dirclose,
// symlinks aren't supported so alias lstat to stat
2022-02-13 12:12:26 +01:00
.lstat_r = romfs_stat,
};
2021-01-01 01:43:50 +01:00
static bool romfs_initialised = false;
static romfs_mount romfs_mounts[32];
//-----------------------------------------------------------------------------
static int32_t romfsMountCommon(const char *name, romfs_mount *mount);
static void romfsInitMtime(romfs_mount *mount);
static void _romfsResetMount(romfs_mount *mount, int32_t id) {
memset(mount, 0, sizeof(*mount));
memcpy(&mount->device, &romFS_devoptab, sizeof(romFS_devoptab));
2022-02-13 12:12:26 +01:00
mount->device.name = mount->name;
2021-01-01 01:43:50 +01:00
mount->device.deviceData = mount;
2022-02-13 12:12:26 +01:00
mount->id = id;
2022-08-22 13:54:22 +02:00
mount->cafe_client = 0;
2021-01-01 01:43:50 +01:00
DCFlushRange(mount, sizeof(*mount));
}
static void _romfsInit(void) {
uint32_t i;
uint32_t total = sizeof(romfs_mounts) / sizeof(romfs_mount);
if (!romfs_initialised) {
for (i = 0; i < total; i++) {
_romfsResetMount(&romfs_mounts[i], i);
}
romfs_initialised = true;
}
}
static romfs_mount *romfsFindMount(const char *name) {
uint32_t i;
2022-02-13 12:12:26 +01:00
uint32_t total = sizeof(romfs_mounts) / sizeof(romfs_mount);
2021-01-01 01:43:50 +01:00
romfs_mount *mount = NULL;
_romfsInit();
for (i = 0; i < total; i++) {
mount = &romfs_mounts[i];
if (name == NULL) { //Find an unused mount entry.
if (!mount->setup) {
return mount;
}
} else if (mount->setup) { //Find the mount with the input name.
if (strncmp(mount->name, name, sizeof(mount->name)) == 0) {
return mount;
}
}
}
return NULL;
}
static romfs_mount *romfs_alloc(void) {
return romfsFindMount(NULL);
}
static void romfs_free(romfs_mount *mount) {
if (mount->fileTable) {
free(mount->fileTable);
}
if (mount->fileHashTable) {
free(mount->fileHashTable);
}
if (mount->dirTable) {
free(mount->dirTable);
}
if (mount->dirHashTable) {
free(mount->dirHashTable);
}
_romfsResetMount(mount, mount->id);
}
static void romfs_mountclose(romfs_mount *mount) {
if (mount->fd_type == RomfsSource_FileDescriptor) {
close(mount->fd);
}
2021-01-01 19:58:29 +01:00
if (mount->fd_type == RomfsSource_FileDescriptor_CafeOS) {
2022-08-22 13:54:22 +02:00
FSACloseFile(mount->cafe_client, mount->cafe_fd);
2021-01-01 19:58:29 +01:00
mount->cafe_fd = 0;
2022-08-22 13:54:22 +02:00
FSADelClient(mount->cafe_client);
mount->cafe_client = 0;
2021-01-01 01:43:50 +01:00
}
romfs_free(mount);
}
2022-04-14 22:37:00 +02:00
std::mutex romfsMutex;
2021-01-01 01:43:50 +01:00
int32_t romfsMount(const char *name, const char *filepath, RomfsSource source) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2022-09-04 21:32:52 +02:00
FSAInit();
2021-01-01 01:43:50 +01:00
romfs_mount *mount = romfs_alloc();
2022-04-14 22:37:00 +02:00
if (mount == nullptr) {
OSMemoryBarrier();
return -99;
2021-01-01 01:43:50 +01:00
}
// Regular RomFS
mount->fd_type = source;
if (mount->fd_type == RomfsSource_FileDescriptor) {
mount->fd = open(filepath, 0);
if (mount->fd == -1) {
romfs_free(mount);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
} else if (mount->fd_type == RomfsSource_FileDescriptor_CafeOS) {
memset(&mount->cafe_mutex, 0, sizeof(OSMutex));
OSInitMutex(&mount->cafe_mutex);
2022-08-22 13:54:22 +02:00
mount->cafe_client = FSAAddClient(nullptr);
if (mount->cafe_client == 0) {
2022-09-04 21:32:52 +02:00
OSReport("libromfs: FSAAddClient failed\n");
2021-01-01 01:43:50 +01:00
romfs_free(mount);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
2022-08-22 13:54:22 +02:00
FSError result = FSAOpenFileEx(mount->cafe_client, filepath, "r", static_cast<FSMode>(0x666), FS_OPEN_FLAG_NONE, 0, &mount->cafe_fd);
if (result != FS_ERROR_OK) {
2022-09-04 21:32:52 +02:00
OSReport("libromfs: FSAOpenFileEx failed for %s. %s\n", filepath, FSAGetStatusStr(result));
2022-08-22 13:54:22 +02:00
FSADelClient(mount->cafe_client);
2021-01-01 01:43:50 +01:00
romfs_free(mount);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
}
2022-04-14 22:37:00 +02:00
auto res = romfsMountCommon(name, mount);
OSMemoryBarrier();
return res;
2021-01-01 01:43:50 +01:00
}
int32_t romfsMountCommon(const char *name, romfs_mount *mount) {
memset(mount->name, 0, sizeof(mount->name));
strncpy(mount->name, name, sizeof(mount->name) - 1);
romfsInitMtime(mount);
if (_romfs_read(mount, 0, &mount->header, sizeof(mount->header)) != sizeof(mount->header)) {
goto fail_io;
}
2021-01-09 18:50:57 +01:00
if (memcmp(&mount->header.headerMagic, "WUHB", sizeof(mount->header.headerMagic)) != 0) {
goto fail_io;
}
2021-01-01 01:43:50 +01:00
if (mount->header.headerSize != 0x50) {
goto fail_io;
}
2022-02-13 12:12:26 +01:00
mount->dirHashTable = NULL;
mount->dirTable = NULL;
2021-01-01 01:43:50 +01:00
mount->fileHashTable = NULL;
2022-02-13 12:12:26 +01:00
mount->fileTable = NULL;
2021-01-01 01:43:50 +01:00
mount->dirHashTable = (uint32_t *) memalign(0x40, mount->header.dirHashTableSize);
if (!mount->dirHashTable) {
goto fail_oom;
}
if (!_romfs_read_chk(mount, mount->header.dirHashTableOff, mount->dirHashTable, mount->header.dirHashTableSize)) {
goto fail_io;
}
mount->dirTable = memalign(0x40, mount->header.dirTableSize);
if (!mount->dirTable) {
goto fail_oom;
}
if (!_romfs_read_chk(mount, mount->header.dirTableOff, mount->dirTable, mount->header.dirTableSize)) {
goto fail_io;
}
mount->fileHashTable = (uint32_t *) memalign(0x40, mount->header.fileHashTableSize);
if (!mount->fileHashTable) {
goto fail_oom;
}
if (!_romfs_read_chk(mount, mount->header.fileHashTableOff, mount->fileHashTable, mount->header.fileHashTableSize)) {
goto fail_io;
}
mount->fileTable = memalign(0x40, mount->header.fileTableSize);
if (!mount->fileTable) {
goto fail_oom;
}
if (!_romfs_read_chk(mount, mount->header.fileTableOff, mount->fileTable, mount->header.fileTableSize)) {
goto fail_io;
}
mount->cwd = romFS_root(mount);
if (AddDevice(&mount->device) < 0) {
goto fail_oom;
}
mount->setup = true;
DCFlushRange(mount, sizeof(*mount));
return 0;
2022-02-13 12:12:26 +01:00
fail_oom:
2021-01-01 01:43:50 +01:00
romfs_mountclose(mount);
return -9;
2021-01-01 01:43:50 +01:00
2022-02-13 12:12:26 +01:00
fail_io:
2021-01-01 01:43:50 +01:00
romfs_mountclose(mount);
return -10;
2021-01-01 01:43:50 +01:00
}
static void romfsInitMtime(romfs_mount *mount) {
mount->mtime = time(NULL);
}
int32_t romfsUnmount(const char *name) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_mount *mount;
char tmpname[34];
mount = romfsFindMount(name);
if (mount == NULL) {
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
// Remove device
memset(tmpname, 0, sizeof(tmpname));
strncpy(tmpname, mount->name, sizeof(tmpname) - 2);
strncat(tmpname, ":", sizeof(tmpname) - strlen(tmpname) - 1);
RemoveDevice(tmpname);
romfs_mountclose(mount);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
//-----------------------------------------------------------------------------
static inline uint8_t normalizePathChar(uint8_t c) {
if (c >= 'a' && c <= 'z') {
return c + 'A' - 'a';
} else {
return c;
}
}
2021-01-01 01:43:50 +01:00
static uint32_t calcHash(uint32_t parent, const uint8_t *name, uint32_t namelen, uint32_t total) {
2021-09-24 17:10:03 +02:00
uint32_t hash = parent ^ 123456789;
2021-01-01 01:43:50 +01:00
uint32_t i;
for (i = 0; i < namelen; i++) {
hash = (hash >> 5) | (hash << 27);
hash ^= normalizePathChar(name[i]);
2021-01-01 01:43:50 +01:00
}
return hash % total;
}
static bool comparePaths(const uint8_t *name1, const uint8_t *name2, uint32_t namelen) {
for (uint32_t i = 0; i < namelen; i++) {
uint8_t c1 = normalizePathChar(name1[i]);
uint8_t c2 = normalizePathChar(name2[i]);
if (c1 != c2) {
return false;
}
}
return true;
}
2021-01-01 01:43:50 +01:00
static int searchForDir(romfs_mount *mount, romfs_dir *parent, const uint8_t *name, uint32_t namelen, romfs_dir **out) {
uint64_t parentOff = (uintptr_t) parent - (uintptr_t) mount->dirTable;
2022-02-13 12:12:26 +01:00
uint32_t hash = calcHash(parentOff, name, namelen, mount->header.dirHashTableSize / 4);
romfs_dir *curDir = NULL;
2021-01-01 01:43:50 +01:00
uint32_t curOff;
*out = NULL;
for (curOff = mount->dirHashTable[hash]; curOff != romFS_none; curOff = curDir->nextHash) {
curDir = romFS_dir(mount, curOff);
if (curDir == NULL) { return EFAULT; }
if (curDir->parent != parentOff) {
continue;
}
if (curDir->nameLen != namelen) {
continue;
}
if (!comparePaths(curDir->name, name, namelen)) {
2021-01-01 01:43:50 +01:00
continue;
}
*out = curDir;
return 0;
}
return ENOENT;
}
static int searchForFile(romfs_mount *mount, romfs_dir *parent, const uint8_t *name, uint32_t namelen, romfs_file **out) {
2022-02-13 12:12:26 +01:00
uint64_t parentOff = (uintptr_t) parent - (uintptr_t) mount->dirTable;
uint32_t hash = calcHash(parentOff, name, namelen, mount->header.fileHashTableSize / 4);
2021-01-01 01:43:50 +01:00
romfs_file *curFile = NULL;
uint32_t curOff;
*out = NULL;
for (curOff = mount->fileHashTable[hash]; curOff != romFS_none;
2021-09-24 17:10:03 +02:00
curOff = curFile->nextHash) {
2021-01-01 01:43:50 +01:00
curFile = romFS_file(mount, curOff);
if (curFile == NULL) {
return EFAULT;
}
if (curFile->parent != parentOff) {
continue;
}
if (curFile->nameLen != namelen) {
continue;
}
if (!comparePaths(curFile->name, name, namelen)) {
2021-01-01 01:43:50 +01:00
continue;
}
*out = curFile;
return 0;
}
return ENOENT;
}
static int navigateToDir(romfs_mount *mount, romfs_dir **ppDir, const char **pPath, bool isDir) {
char *colonPos = strchr(*pPath, ':');
if (colonPos) { *pPath = colonPos + 1; }
if (!**pPath) {
return EILSEQ;
}
*ppDir = mount->cwd;
if (**pPath == '/') {
*ppDir = romFS_root(mount);
(*pPath)++;
}
OSLockMutex(&mount->cafe_mutex);
2021-01-01 01:43:50 +01:00
while (**pPath) {
2022-02-13 12:12:26 +01:00
char *slashPos = strchr(*pPath, '/');
2021-01-01 01:43:50 +01:00
char *component = __component;
if (slashPos) {
uint32_t len = slashPos - *pPath;
if (!len) {
OSUnlockMutex(&mount->cafe_mutex);
2021-01-01 01:43:50 +01:00
return EILSEQ;
}
if (len > PATH_MAX) {
OSUnlockMutex(&mount->cafe_mutex);
2021-01-01 01:43:50 +01:00
return ENAMETOOLONG;
}
memcpy(component, *pPath, len);
component[len] = 0;
2022-02-13 12:12:26 +01:00
*pPath = slashPos + 1;
2021-01-01 01:43:50 +01:00
} else if (isDir) {
component = (char *) *pPath;
*pPath += strlen(component);
} else {
OSUnlockMutex(&mount->cafe_mutex);
2021-01-01 01:43:50 +01:00
return 0;
}
if (component[0] == '.') {
if (!component[1]) { continue; }
if (component[1] == '.' && !component[2]) {
*ppDir = romFS_dir(mount, (*ppDir)->parent);
if (!*ppDir) {
OSUnlockMutex(&mount->cafe_mutex);
2021-01-01 01:43:50 +01:00
return EFAULT;
}
continue;
}
}
int ret = searchForDir(mount, *ppDir, (uint8_t *) component, strlen(component), ppDir);
if (ret != 0) {
OSUnlockMutex(&mount->cafe_mutex);
2021-01-01 01:43:50 +01:00
return ret;
}
}
OSUnlockMutex(&mount->cafe_mutex);
2021-01-01 01:43:50 +01:00
return 0;
}
static ino_t dir_inode(romfs_mount *mount, romfs_dir *dir) {
return (uint32_t *) dir - (uint32_t *) mount->dirTable;
}
static off_t dir_size(romfs_dir *dir) {
return sizeof(romfs_dir) + (dir->nameLen + 3) / 4;
}
static nlink_t dir_nlink(romfs_mount *mount, romfs_dir *dir) {
2022-02-13 12:12:26 +01:00
nlink_t count = 2; // one for self, one for parent
2021-01-01 01:43:50 +01:00
uint32_t offset = dir->childDir;
while (offset != romFS_none) {
romfs_dir *tmp = romFS_dir(mount, offset);
if (!tmp) { break; }
++count;
offset = tmp->sibling;
}
offset = dir->childFile;
while (offset != romFS_none) {
romfs_file *tmp = romFS_file(mount, offset);
if (!tmp) { break; }
++count;
offset = tmp->sibling;
}
return count;
}
static ino_t file_inode(romfs_mount *mount, romfs_file *file) {
return ((uint32_t *) file - (uint32_t *) mount->fileTable) + mount->header.dirTableSize / 4;
}
int romfsGetFileInfoPerPath(const char *romfs, const char *path, romfs_fileInfo *out) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
if (out == nullptr) {
2021-01-01 01:43:50 +01:00
return -1;
}
2022-04-14 22:37:00 +02:00
auto *mount = (romfs_mount *) romfsFindMount(romfs);
if (mount == nullptr) {
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -2;
}
2022-04-14 22:37:00 +02:00
romfs_dir *curDir = nullptr;
2022-02-13 12:12:26 +01:00
int errno2 = navigateToDir(mount, &curDir, &path, false);
2021-01-01 01:43:50 +01:00
if (errno2 != 0) {
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -3;
}
2022-04-14 22:37:00 +02:00
romfs_file *file = nullptr;
2022-02-13 12:12:26 +01:00
int err = searchForFile(mount, curDir, (uint8_t *) path, strlen(path), &file);
2021-01-01 01:43:50 +01:00
if (err != 0) {
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -4;
}
out->length = file->dataSize;
out->offset = mount->header.fileDataOff + file->dataOff;
return 0;
}
//-----------------------------------------------------------------------------
int romfs_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_fileobj *fileobj = (romfs_fileobj *) fileStruct;
fileobj->mount = (romfs_mount *) r->deviceData;
if ((flags & O_ACCMODE) != O_RDONLY) {
r->_errno = EROFS;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
romfs_dir *curDir = NULL;
2022-02-13 12:12:26 +01:00
r->_errno = navigateToDir(fileobj->mount, &curDir, &path, false);
2021-01-01 01:43:50 +01:00
if (r->_errno != 0) {
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
romfs_file *file = NULL;
2022-02-13 12:12:26 +01:00
int ret = searchForFile(fileobj->mount, curDir, (uint8_t *) path, strlen(path), &file);
2021-01-01 01:43:50 +01:00
if (ret != 0) {
if (ret == ENOENT && (flags & O_CREAT)) {
r->_errno = EROFS;
} else {
r->_errno = ret;
}
return -1;
} else if ((flags & O_CREAT) && (flags & O_EXCL)) {
r->_errno = EEXIST;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
2022-02-13 12:12:26 +01:00
fileobj->file = file;
2021-01-01 01:43:50 +01:00
fileobj->offset = fileobj->mount->header.fileDataOff + file->dataOff;
2022-02-13 12:12:26 +01:00
fileobj->pos = 0;
2021-01-01 01:43:50 +01:00
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
int romfs_close(struct _reent *r, void *fd) {
return 0;
}
ssize_t romfs_read(struct _reent *r, void *fd, char *ptr, size_t len) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_fileobj *file = (romfs_fileobj *) fd;
2022-02-13 12:12:26 +01:00
uint64_t endPos = file->pos + len;
2021-01-01 01:43:50 +01:00
/* check if past end-of-file */
if (file->pos >= file->file->dataSize) {
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
/* truncate the read to end-of-file */
if (endPos > file->file->dataSize) {
endPos = file->file->dataSize;
}
len = endPos - file->pos;
ssize_t adv = _romfs_read(file->mount, file->offset + file->pos, ptr, len);
if (adv >= 0) {
file->pos += adv;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return adv;
}
r->_errno = EIO;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
off_t romfs_seek(struct _reent *r, void *fd, off_t pos, int dir) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_fileobj *file = (romfs_fileobj *) fd;
off_t start;
switch (dir) {
case SEEK_SET:
start = 0;
break;
case SEEK_CUR:
start = file->pos;
break;
case SEEK_END:
start = file->file->dataSize;
break;
default:
r->_errno = EINVAL;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
/* don't allow negative position */
if (pos < 0) {
if (start + pos < 0) {
r->_errno = EINVAL;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
}
2022-02-13 12:12:26 +01:00
/* check for overflow */
2021-01-01 01:43:50 +01:00
else if (INT64_MAX - pos < start) {
r->_errno = EOVERFLOW;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
file->pos = start + pos;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return file->pos;
}
static void fillDir(struct stat *st, romfs_mount *mount, romfs_dir *dir) {
memset(st, 0, sizeof(*st));
2022-02-13 12:12:26 +01:00
st->st_ino = dir_inode(mount, dir);
st->st_mode = romFS_dir_mode;
st->st_nlink = dir_nlink(mount, dir);
st->st_size = dir_size(dir);
2021-01-01 01:43:50 +01:00
st->st_blksize = 512;
2022-02-13 12:12:26 +01:00
st->st_blocks = (st->st_blksize + 511) / 512;
2021-01-01 01:43:50 +01:00
st->st_atime = st->st_mtime = st->st_ctime = mount->mtime;
}
static void fillFile(struct stat *st, romfs_mount *mount, romfs_file *file) {
memset(st, 0, sizeof(struct stat));
2022-02-13 12:12:26 +01:00
st->st_ino = file_inode(mount, file);
st->st_mode = romFS_file_mode;
st->st_nlink = 1;
st->st_size = (off_t) file->dataSize;
2021-01-01 01:43:50 +01:00
st->st_blksize = 512;
2022-02-13 12:12:26 +01:00
st->st_blocks = (st->st_blksize + 511) / 512;
2021-01-01 01:43:50 +01:00
st->st_atime = st->st_mtime = st->st_ctime = mount->mtime;
}
int romfs_fstat(struct _reent *r, void *fd, struct stat *st) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_fileobj *fileobj = (romfs_fileobj *) fd;
fillFile(st, fileobj->mount, fileobj->file);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
int romfs_stat(struct _reent *r, const char *path, struct stat *st) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_mount *mount = (romfs_mount *) r->deviceData;
2022-02-13 12:12:26 +01:00
romfs_dir *curDir = NULL;
r->_errno = navigateToDir(mount, &curDir, &path, false);
2021-01-01 01:43:50 +01:00
if (r->_errno != 0) {
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
if (!*path) {
fillDir(st, mount, curDir);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
romfs_dir *dir = NULL;
2022-02-13 12:12:26 +01:00
int ret = 0;
ret = searchForDir(mount, curDir, (uint8_t *) path, strlen(path), &dir);
2021-01-01 01:43:50 +01:00
if (ret != 0 && ret != ENOENT) {
r->_errno = ret;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
if (ret == 0) {
fillDir(st, mount, dir);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
romfs_file *file = NULL;
2022-02-13 12:12:26 +01:00
ret = searchForFile(mount, curDir, (uint8_t *) path, strlen(path), &file);
2021-01-01 01:43:50 +01:00
if (ret != 0 && ret != ENOENT) {
r->_errno = ret;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
if (ret == 0) {
fillFile(st, mount, file);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
r->_errno = ENOENT;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
int romfs_chdir(struct _reent *r, const char *path) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_mount *mount = (romfs_mount *) r->deviceData;
2022-02-13 12:12:26 +01:00
romfs_dir *curDir = NULL;
r->_errno = navigateToDir(mount, &curDir, &path, true);
2021-01-01 01:43:50 +01:00
if (r->_errno != 0) {
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
mount->cwd = curDir;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
DIR_ITER *romfs_diropen(struct _reent *r, DIR_ITER *dirState, const char *path) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_diriter *iter = (romfs_diriter *) (dirState->dirStruct);
2022-02-13 12:12:26 +01:00
romfs_dir *curDir = NULL;
iter->mount = (romfs_mount *) r->deviceData;
2021-01-01 01:43:50 +01:00
r->_errno = navigateToDir(iter->mount, &curDir, &path, true);
if (r->_errno != 0) {
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return NULL;
}
2022-02-13 12:12:26 +01:00
iter->dir = curDir;
iter->state = 0;
iter->childDir = curDir->childDir;
2021-01-01 01:43:50 +01:00
iter->childFile = curDir->childFile;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return dirState;
}
int romfs_dirreset(struct _reent *r, DIR_ITER *dirState) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_diriter *iter = (romfs_diriter *) (dirState->dirStruct);
2022-02-13 12:12:26 +01:00
iter->state = 0;
iter->childDir = iter->dir->childDir;
2021-01-01 01:43:50 +01:00
iter->childFile = iter->dir->childFile;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat) {
2022-04-14 22:37:00 +02:00
std::lock_guard<std::mutex> lock(romfsMutex);
2021-01-01 01:43:50 +01:00
romfs_diriter *iter = (romfs_diriter *) (dirState->dirStruct);
if (iter->state == 0) {
/* '.' entry */
fillDir(filestat, iter->mount, iter->dir);
2021-01-01 01:43:50 +01:00
strcpy(filename, ".");
iter->state = 1;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
} else if (iter->state == 1) {
/* '..' entry */
romfs_dir *dir = romFS_dir(iter->mount, iter->dir->parent);
if (!dir) {
r->_errno = EFAULT;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
fillDir(filestat, iter->mount, dir);
2021-01-01 01:43:50 +01:00
strcpy(filename, "..");
iter->state = 2;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
if (iter->childDir != romFS_none) {
romfs_dir *dir = romFS_dir(iter->mount, iter->childDir);
if (!dir) {
r->_errno = EFAULT;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
iter->childDir = dir->sibling;
fillDir(filestat, iter->mount, dir);
2021-01-01 01:43:50 +01:00
memset(filename, 0, NAME_MAX);
if (dir->nameLen >= NAME_MAX) {
r->_errno = ENAMETOOLONG;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
strncpy(filename, (char *) dir->name, dir->nameLen);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
} else if (iter->childFile != romFS_none) {
romfs_file *file = romFS_file(iter->mount, iter->childFile);
if (!file) {
r->_errno = EFAULT;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
iter->childFile = file->sibling;
fillFile(filestat, iter->mount, file);
2021-01-01 01:43:50 +01:00
memset(filename, 0, NAME_MAX);
if (file->nameLen >= NAME_MAX) {
r->_errno = ENAMETOOLONG;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
strncpy(filename, (char *) file->name, file->nameLen);
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return 0;
}
r->_errno = ENOENT;
2022-04-14 22:37:00 +02:00
OSMemoryBarrier();
2021-01-01 01:43:50 +01:00
return -1;
}
int romfs_dirclose(struct _reent *r, DIR_ITER *dirState) {
return 0;
}