lovr/src/filesystem/filesystem.c

322 lines
7.8 KiB
C
Raw Normal View History

2016-11-19 09:28:01 +00:00
#include "filesystem/filesystem.h"
2017-10-22 00:35:50 +00:00
#include "filesystem/file.h"
2016-11-19 09:28:01 +00:00
#include "util.h"
2016-11-01 00:14:31 +00:00
#include <physfs.h>
2016-11-07 22:30:32 +00:00
#include <stdio.h>
2017-01-22 02:18:12 +00:00
#include <stdlib.h>
2018-07-04 20:51:35 +00:00
#include <string.h>
2016-11-07 02:39:16 +00:00
#ifdef __APPLE__
2016-11-01 00:14:31 +00:00
#include <mach-o/dyld.h>
2017-03-11 09:37:00 +00:00
#endif
#if _WIN32
2016-11-12 09:19:47 +00:00
#include <windows.h>
#include <initguid.h>
#include <KnownFolders.h>
#include <ShlObj.h>
#include <wchar.h>
2017-03-11 09:37:00 +00:00
#else
#include <unistd.h>
#include <pwd.h>
2016-11-01 00:14:31 +00:00
#endif
2016-11-07 22:30:32 +00:00
static FilesystemState state;
2017-03-11 09:37:00 +00:00
void lovrFilesystemInit(const char* arg0, const char* arg1) {
if (state.initialized) return;
state.initialized = true;
2016-11-01 00:14:31 +00:00
if (!PHYSFS_init(arg0)) {
lovrThrow("Could not initialize filesystem: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
2016-11-01 00:14:31 +00:00
}
2016-11-07 22:30:32 +00:00
2017-03-11 09:37:00 +00:00
state.source = malloc(LOVR_PATH_MAX * sizeof(char));
2016-11-07 22:30:32 +00:00
state.identity = NULL;
2017-10-31 08:14:09 +00:00
state.isFused = true;
2018-03-22 17:39:31 +00:00
vec_init(&state.requirePatterns);
lovrFilesystemSetRequirePath("?.lua;?/init.lua");
2017-03-11 09:37:00 +00:00
// Try to mount either an archive fused to the executable or an archive from the command line
lovrFilesystemGetExecutablePath(state.source, LOVR_PATH_MAX);
2017-04-02 12:55:21 +00:00
if (lovrFilesystemMount(state.source, NULL, 1)) {
2017-10-31 08:14:09 +00:00
state.isFused = false;
2017-04-02 12:55:21 +00:00
if (arg1) {
strncpy(state.source, arg1, LOVR_PATH_MAX);
if (!lovrFilesystemMount(state.source, NULL, 1)) {
return;
2017-04-02 12:55:21 +00:00
}
2017-03-11 09:37:00 +00:00
}
2017-04-02 12:55:21 +00:00
free(state.source);
state.source = NULL;
2017-03-11 09:37:00 +00:00
}
2016-11-01 00:14:31 +00:00
}
void lovrFilesystemDestroy() {
if (!state.initialized) return;
2017-03-11 09:37:00 +00:00
free(state.source);
2016-11-08 06:23:13 +00:00
free(state.savePathFull);
free(state.savePathRelative);
2018-03-22 17:39:31 +00:00
free(state.requirePath);
vec_deinit(&state.requirePatterns);
2016-11-01 00:14:31 +00:00
PHYSFS_deinit();
memset(&state, 0, sizeof(FilesystemState));
2016-11-01 00:14:31 +00:00
}
2017-03-11 09:37:00 +00:00
int lovrFilesystemCreateDirectory(const char* path) {
return !PHYSFS_mkdir(path);
}
2016-11-07 22:31:11 +00:00
2017-03-11 09:37:00 +00:00
int lovrFilesystemGetAppdataDirectory(char* dest, unsigned int size) {
#ifdef __APPLE__
const char* home;
if ((home = getenv("HOME")) == NULL) {
home = getpwuid(getuid())->pw_dir;
2016-11-07 22:31:11 +00:00
}
2017-03-11 09:37:00 +00:00
snprintf(dest, size, "%s/Library/Application Support", home);
return 0;
#elif _WIN32
PWSTR appData = NULL;
SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &appData);
PHYSFS_utf8FromUtf16(appData, dest, size);
2017-03-11 09:37:00 +00:00
CoTaskMemFree(appData);
return 0;
#elif EMSCRIPTEN
2017-04-23 00:02:41 +00:00
strncpy(dest, "/home/web_user", size);
2017-04-16 23:56:49 +00:00
return 0;
2017-08-01 19:23:33 +00:00
#elif __linux__
const char* home;
if ((home = getenv("HOME")) == NULL) {
home = getpwuid(getuid())->pw_dir;
}
snprintf(dest, size, "%s/.config", home);
2017-03-11 09:37:00 +00:00
#else
#error "This platform is missing an implementation for lovrFilesystemGetAppdataDirectory"
#endif
return 1;
2016-11-07 22:31:11 +00:00
}
2017-03-11 09:37:00 +00:00
void lovrFilesystemGetDirectoryItems(const char* path, getDirectoryItemsCallback callback, void* userdata) {
PHYSFS_enumerate(path, callback, userdata);
2016-11-01 00:14:31 +00:00
}
2016-11-07 22:30:32 +00:00
int lovrFilesystemGetExecutablePath(char* dest, unsigned int size) {
2016-11-07 02:39:16 +00:00
#ifdef __APPLE__
2016-11-07 22:30:32 +00:00
if (_NSGetExecutablePath(dest, &size) == 0) {
return 0;
2016-11-01 00:14:31 +00:00
}
2016-11-14 22:16:16 +00:00
#elif _WIN32
2016-11-12 09:19:47 +00:00
return !GetModuleFileName(NULL, dest, size);
#elif EMSCRIPTEN
2017-04-16 23:56:49 +00:00
return 1;
2017-08-01 19:23:33 +00:00
#elif __linux__
memset(dest, 0, size);
if (readlink("/proc/self/exe", dest, size) != -1) {
return 0;
2017-08-01 19:23:33 +00:00
}
2016-11-14 22:16:16 +00:00
#else
#error "This platform is missing an implementation for lovrFilesystemGetExecutablePath"
2016-11-01 00:14:31 +00:00
#endif
2016-11-07 22:30:32 +00:00
return 1;
}
const char* lovrFilesystemGetIdentity() {
return state.identity;
}
2017-03-11 09:37:00 +00:00
long lovrFilesystemGetLastModified(const char* path) {
PHYSFS_Stat stat;
return PHYSFS_stat(path, &stat) ? stat.modtime : -1;
2017-03-11 09:37:00 +00:00
}
2016-11-07 22:30:32 +00:00
const char* lovrFilesystemGetRealDirectory(const char* path) {
2017-03-11 09:37:00 +00:00
return PHYSFS_getRealDir(path);
}
vec_str_t* lovrFilesystemGetRequirePath() {
2018-03-22 17:39:31 +00:00
return &state.requirePatterns;
}
2017-03-11 09:37:00 +00:00
const char* lovrFilesystemGetSaveDirectory() {
return state.savePathFull;
}
2017-10-22 00:35:50 +00:00
size_t lovrFilesystemGetSize(const char* path) {
PHYSFS_Stat stat;
return PHYSFS_stat(path, &stat) ? stat.filesize : -1;
2016-11-07 22:30:32 +00:00
}
const char* lovrFilesystemGetSource() {
2017-03-11 09:37:00 +00:00
return state.source;
2016-11-07 22:30:32 +00:00
}
const char* lovrFilesystemGetUserDirectory() {
#if defined(__APPLE__) || defined(__linux__)
const char* home;
if ((home = getenv("HOME")) == NULL) {
home = getpwuid(getuid())->pw_dir;
}
return home;
#elif _WIN32
return getenv("USERPROFILE");
#elif EMSCRIPTEN
return "/home/web_user";
#else
#error "This platform is missing an implementation for lovrFilesystemGetUserDirectory"
#endif
2016-11-01 00:14:31 +00:00
}
int lovrFilesystemGetWorkingDirectory(char* dest, unsigned int size) {
#ifdef _WIN32
WCHAR w_cwd[LOVR_PATH_MAX];
_wgetcwd(w_cwd, LOVR_PATH_MAX);
PHYSFS_utf8FromUtf16(w_cwd, dest, size);
return 0;
#else
if (getcwd(dest, size)) {
return 0;
}
#endif
return 1;
}
2017-10-31 08:14:09 +00:00
bool lovrFilesystemIsDirectory(const char* path) {
PHYSFS_Stat stat;
2017-10-31 08:14:09 +00:00
return PHYSFS_stat(path, &stat) ? stat.filetype == PHYSFS_FILETYPE_DIRECTORY : false;
2016-11-01 01:35:00 +00:00
}
2017-10-31 08:14:09 +00:00
bool lovrFilesystemIsFile(const char* path) {
PHYSFS_Stat stat;
2017-10-31 08:14:09 +00:00
return PHYSFS_stat(path, &stat) ? stat.filetype == PHYSFS_FILETYPE_REGULAR : false;
2016-11-01 01:35:00 +00:00
}
2017-10-31 08:14:09 +00:00
bool lovrFilesystemIsFused() {
2017-03-11 09:37:00 +00:00
return state.isFused;
}
2017-10-31 08:14:09 +00:00
int lovrFilesystemMount(const char* path, const char* mountpoint, bool append) {
2017-03-11 09:37:00 +00:00
return !PHYSFS_mount(path, mountpoint, append);
}
void* lovrFilesystemRead(const char* path, size_t* bytesRead) {
2016-11-02 03:27:15 +00:00
2017-10-22 00:35:50 +00:00
// Create file
File* file = lovrFileCreate(path);
2017-10-21 07:19:05 +00:00
if (!file) {
lovrRelease(file);
2016-11-02 03:27:15 +00:00
return NULL;
}
2017-10-22 00:35:50 +00:00
// Open it
2017-10-22 03:23:29 +00:00
if (lovrFileOpen(file, OPEN_READ)) {
lovrRelease(file);
2017-10-22 03:23:29 +00:00
return NULL;
}
2017-10-22 00:35:50 +00:00
2016-11-05 22:55:01 +00:00
// Get file size
2017-10-22 00:35:50 +00:00
size_t size = lovrFileGetSize(file);
if (size == (unsigned int) -1) {
lovrRelease(file);
2016-11-05 22:55:01 +00:00
return NULL;
}
// Allocate buffer
void* data = malloc(size);
if (!data) {
lovrRelease(file);
2016-11-05 22:55:01 +00:00
return NULL;
}
// Perform read
*bytesRead = lovrFileRead(file, data, size);
2017-10-22 00:35:50 +00:00
lovrFileClose(file);
lovrRelease(file);
2016-11-02 03:27:15 +00:00
2016-11-05 22:55:01 +00:00
// Make sure we got everything
2017-03-11 09:37:00 +00:00
if (*bytesRead != (size_t) size) {
2016-11-05 22:55:01 +00:00
free(data);
2016-11-02 03:27:15 +00:00
return NULL;
}
return data;
}
2017-03-11 09:37:00 +00:00
int lovrFilesystemRemove(const char* path) {
return !PHYSFS_delete(path);
}
2016-11-07 22:30:32 +00:00
int lovrFilesystemSetIdentity(const char* identity) {
state.identity = identity;
// Unmount old write directory
if (state.savePathFull && state.savePathRelative) {
PHYSFS_unmount(state.savePathRelative);
2016-11-07 22:30:32 +00:00
} else {
state.savePathRelative = malloc(LOVR_PATH_MAX);
state.savePathFull = malloc(LOVR_PATH_MAX);
2017-03-11 09:37:00 +00:00
if (!state.savePathRelative || !state.savePathFull) {
return 1;
}
2016-11-07 22:30:32 +00:00
}
2017-03-11 09:37:00 +00:00
lovrFilesystemGetAppdataDirectory(state.savePathFull, LOVR_PATH_MAX);
if (!PHYSFS_setWriteDir(state.savePathFull)) {
const char* error = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
lovrThrow("Could not set write directory: %s (%s)", error, state.savePathFull);
}
snprintf(state.savePathRelative, LOVR_PATH_MAX, "LOVR/%s", identity ? identity : "default");
2017-08-01 19:23:33 +00:00
char fullPathBuffer[LOVR_PATH_MAX];
snprintf(fullPathBuffer, LOVR_PATH_MAX, "%s/%s", state.savePathFull, state.savePathRelative);
strncpy(state.savePathFull, fullPathBuffer, LOVR_PATH_MAX);
2017-03-11 09:37:00 +00:00
PHYSFS_mkdir(state.savePathRelative);
if (!PHYSFS_setWriteDir(state.savePathFull)) {
const char* error = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
lovrThrow("Could not set write directory: %s (%s)", error, state.savePathRelative);
}
2017-03-11 09:37:00 +00:00
PHYSFS_mount(state.savePathFull, NULL, 0);
2016-11-07 22:30:32 +00:00
2017-03-11 09:37:00 +00:00
return 0;
2016-11-07 22:30:32 +00:00
}
2018-03-22 17:39:31 +00:00
void lovrFilesystemSetRequirePath(const char* requirePath) {
if (state.requirePath) {
free(state.requirePath);
vec_clear(&state.requirePatterns);
}
state.requirePath = strdup(requirePath);
char* p = state.requirePath;
while (1) {
vec_push(&state.requirePatterns, p);
if ((p = strchr(p, ';')) != NULL) {
*p++ = '\0';
} else {
break;
}
}
}
2017-03-11 09:37:00 +00:00
int lovrFilesystemUnmount(const char* path) {
return !PHYSFS_unmount(path);
2016-11-01 00:14:31 +00:00
}
2016-11-07 22:31:02 +00:00
2017-10-31 08:14:09 +00:00
size_t lovrFilesystemWrite(const char* path, const char* content, size_t size, bool append) {
2017-10-22 00:35:50 +00:00
File* file = lovrFileCreate(path);
2017-10-21 07:19:05 +00:00
if (!file) {
2016-11-07 22:31:02 +00:00
return 0;
}
2017-10-22 00:35:50 +00:00
lovrFileOpen(file, append ? OPEN_APPEND : OPEN_WRITE);
size_t bytesWritten = lovrFileWrite(file, (void*) content, size);
2017-10-22 00:35:50 +00:00
lovrFileClose(file);
2018-02-26 08:59:03 +00:00
lovrRelease(file);
2016-11-07 22:31:02 +00:00
return bytesWritten;
}