tildefriends/src/file.c

213 lines
7.7 KiB
C
Raw Normal View History

#include "file.h"
#include "task.h"
#include <malloc.h>
#include <stdbool.h>
#include <uv.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dirent.h>
#include <unistd.h>
#endif
static JSValue _file_make_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _file_read_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _file_rename_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _file_stat(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _file_unlink_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static double _time_spec_to_double(const uv_timespec_t* time_spec);
static void _file_on_stat_complete(uv_fs_t* request);
typedef struct file_stat_t {
void* _task;
JSContext* _context;
promiseid_t _promise;
uv_fs_t _request;
} file_stat_t;
void tf_file_init(JSContext* context) {
JSValue global = JS_GetGlobalObject(context);
JSValue file = JS_NewObject(context);
JS_SetPropertyStr(context, global, "File", file);
JS_SetPropertyStr(context, file, "makeDirectory", JS_NewCFunction(context, _file_make_directory, "makeDirectory", 1));
JS_SetPropertyStr(context, file, "readDirectory", JS_NewCFunction(context, _file_read_directory, "readDirectory", 1));
JS_SetPropertyStr(context, file, "readFile", JS_NewCFunction(context, _file_read_file, "readFile", 1));
JS_SetPropertyStr(context, file, "renameFile", JS_NewCFunction(context, _file_rename_file, "renameFile", 2));
JS_SetPropertyStr(context, file, "stat", JS_NewCFunction(context, _file_stat, "stat", 1));
JS_SetPropertyStr(context, file, "unlinkFile", JS_NewCFunction(context, _file_unlink_file, "unlinkFile", 1));
JS_SetPropertyStr(context, file, "writeFile", JS_NewCFunction(context, _file_write_file, "writeFile", 2));
JS_FreeValue(context, global);
}
static void _free_array_buffer_data(JSRuntime* runtime, void* opaque, void* ptr) {
free(ptr);
}
static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
const char* file_name = JS_ToCString(context, argv[0]);
FILE* file = fopen(file_name, "rb");
JS_FreeCString(context, file_name);
if (!file) {
return JS_NULL;
}
long size = 0;
if (fseek(file, 0, SEEK_END) == 0) {
size = ftell(file);
}
if (size >= 0 &&
size < 4 * 1024 * 1024 &&
fseek(file, 0, SEEK_SET) == 0) {
uint8_t* data = malloc(size);
if (data &&
fread(data, 1, size, file) == (size_t)size) {
JSValue arrayBuffer = JS_NewArrayBuffer(context, data, size, _free_array_buffer_data, NULL, false);
JSValue global = JS_GetGlobalObject(context);
JSValue constructor = JS_GetPropertyStr(context, global, "Uint8Array");
JSValue typedArray = JS_CallConstructor(context, constructor, 1, &arrayBuffer);
JS_FreeValue(context, constructor);
JS_FreeValue(context, global);
JS_FreeValue(context, arrayBuffer);
return typedArray;
} else if (data) {
free(data);
}
}
return JS_NULL;
}
static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
JSValue result = JS_NULL;
const char* fileName = JS_ToCString(context, argv[0]);
FILE* file = fopen(fileName, "wb");
JS_FreeCString(context, fileName);
if (file) {
size_t size;
uint8_t* buffer = tf_try_get_array_buffer(context, &size, argv[1]);
if (buffer) {
int written = fwrite((const char*)buffer, 1, size, file);
result = JS_NewInt32(context, (size_t)written == size ? 0 : written);
} else {
const char* data = JS_ToCStringLen(context, &size, argv[1]);
int written = fwrite((const char*)data, 1, size, file);
result = JS_NewInt32(context, (size_t)written == size ? 0 : written);
JS_FreeCString(context, data);
}
fclose(file);
}
return result;
}
static JSValue _file_rename_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
void* task = JS_GetContextOpaque(context);
const char* oldName = JS_ToCString(context, argv[0]);
const char* newName = JS_ToCString(context, argv[1]);
uv_fs_t req;
int result = uv_fs_rename(tf_task_get_loop(task), &req, oldName, newName, 0);
JS_FreeCString(context, oldName);
JS_FreeCString(context, newName);
return JS_NewInt32(context, result);
}
static JSValue _file_unlink_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
void* task = JS_GetContextOpaque(context);
const char* fileName = JS_ToCString(context, argv[0]);
uv_fs_t req;
int result = uv_fs_unlink(tf_task_get_loop(task), &req, fileName, 0);
JS_FreeCString(context, fileName);
return JS_NewInt32(context, result);
}
static JSValue _file_read_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
const char* directory = JS_ToCString(context, argv[0]);
JSValue array = JS_NewArray(context);
#ifdef _WIN32
WIN32_FIND_DATA find;
std::string pattern = directory;
pattern += "\\*";
HANDLE handle = FindFirstFile(pattern.c_str(), &find);
if (handle != INVALID_HANDLE_VALUE) {
int index = 0;
do {
JS_SetPropertyUint32(context, array, index++, JS_NewString(context, find.cFileName));
} while (FindNextFile(handle, &find) != 0);
FindClose(handle);
}
#else
DIR* dir = opendir(directory);
if (dir) {
uint32_t index = 0;
struct dirent* entry = readdir(dir);
while (entry) {
JS_SetPropertyUint32(context, array, index++, JS_NewString(context, entry->d_name));
entry = readdir(dir);
}
closedir(dir);
}
#endif
JS_FreeCString(context, directory);
return array;
}
JSValue _file_make_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
void* task = JS_GetContextOpaque(context);
const char* directory = JS_ToCString(context, argv[0]);
uv_fs_t req;
int result = uv_fs_mkdir(tf_task_get_loop(task), &req, directory, 0755, 0);
JS_FreeCString(context, directory);
return JS_NewInt32(context, result);
}
JSValue _file_stat(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
void* task = JS_GetContextOpaque(context);
const char* path = JS_ToCString(context, argv[0]);
promiseid_t promise = tf_task_allocate_promise(task);
file_stat_t* data = malloc(sizeof(file_stat_t));
data->_task = task;
data->_promise = promise;
data->_request.data = data;
data->_context = context;
int result = uv_fs_stat(tf_task_get_loop(task), &data->_request, path, _file_on_stat_complete);
if (result) {
tf_task_reject_promise(task, promise, JS_NewInt32(context, result));
free(data);
}
JS_FreeCString(context, path);
return tf_task_get_promise(task, promise);
}
static double _time_spec_to_double(const uv_timespec_t* time_spec) {
return time_spec->tv_sec + (double)(time_spec->tv_nsec) / 1e9;
}
static void _file_on_stat_complete(uv_fs_t* request) {
file_stat_t* data = (file_stat_t*)(request->data);
JSContext* context = data->_context;
if (request->result) {
tf_task_reject_promise(data->_task, data->_promise, JS_NewInt32(context, request->result));
} else {
JSValue result = JS_NewObject(context);
JS_SetPropertyStr(context, result, "mtime", JS_NewFloat64(context, _time_spec_to_double(&request->statbuf.st_mtim)));
JS_SetPropertyStr(context, result, "ctime", JS_NewFloat64(context, _time_spec_to_double(&request->statbuf.st_ctim)));
JS_SetPropertyStr(context, result, "atime", JS_NewFloat64(context, _time_spec_to_double(&request->statbuf.st_atim)));
JS_SetPropertyStr(context, result, "size", JS_NewFloat64(context, request->statbuf.st_size));
tf_task_resolve_promise(data->_task, data->_promise, result);
JS_FreeValue(context, result);
}
uv_fs_req_cleanup(request);
free(data);
}