2021-10-24 11:46:30 -04:00
|
|
|
#include "file.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
#include "task.h"
|
2021-11-03 18:15:46 -04:00
|
|
|
#include "util.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdbool.h>
|
2021-10-27 19:27:21 -04:00
|
|
|
#include <string.h>
|
2021-01-02 13:10:00 -05:00
|
|
|
#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);
|
2021-10-27 20:53:16 -04:00
|
|
|
static JSValue _file_remove_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
2021-01-02 13:10:00 -05:00
|
|
|
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;
|
|
|
|
|
2022-05-16 18:30:14 -04:00
|
|
|
typedef struct fs_req_t {
|
|
|
|
uv_fs_t fs;
|
|
|
|
uv_file file;
|
|
|
|
size_t size;
|
|
|
|
char buffer[];
|
|
|
|
} fs_req_t;
|
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
void tf_file_register(JSContext* context)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JSValue file = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, global, "File", file);
|
2021-10-27 19:27:21 -04:00
|
|
|
JS_SetPropertyStr(context, file, "readFile", JS_NewCFunction(context, _file_read_file, "readFile", 1));
|
|
|
|
JS_SetPropertyStr(context, file, "writeFile", JS_NewCFunction(context, _file_write_file, "writeFile", 2));
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, file, "makeDirectory", JS_NewCFunction(context, _file_make_directory, "makeDirectory", 1));
|
2021-10-27 20:53:16 -04:00
|
|
|
JS_SetPropertyStr(context, file, "removeDirectory", JS_NewCFunction(context, _file_remove_directory, "removeDirectory", 1));
|
|
|
|
JS_SetPropertyStr(context, file, "unlinkFile", JS_NewCFunction(context, _file_unlink_file, "unlinkFile", 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
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_FreeValue(context, global);
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:10:45 -05:00
|
|
|
static const int k_file_read_max = 8 * 1024 * 1024;
|
2021-10-05 21:25:33 -04:00
|
|
|
|
2021-10-27 19:27:21 -04:00
|
|
|
static void _file_async_close_callback(uv_fs_t* req)
|
2021-10-05 21:25:33 -04:00
|
|
|
{
|
|
|
|
uv_fs_req_cleanup(req);
|
|
|
|
free(req);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-05 21:25:33 -04:00
|
|
|
static void _file_read_read_callback(uv_fs_t* req)
|
|
|
|
{
|
|
|
|
uv_fs_req_cleanup(req);
|
2022-05-16 18:30:14 -04:00
|
|
|
fs_req_t* fsreq = (fs_req_t*)req;
|
2021-10-05 21:25:33 -04:00
|
|
|
tf_task_t* task = req->loop->data;
|
|
|
|
JSContext* context = tf_task_get_context(task);
|
|
|
|
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
|
|
|
|
if (req->result >= 0)
|
|
|
|
{
|
2022-06-02 05:40:19 -04:00
|
|
|
JSValue arrayBuffer = JS_NewArrayBufferCopy(context, (const uint8_t*)fsreq->buffer, req->result);
|
2021-10-05 21:25:33 -04:00
|
|
|
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);
|
|
|
|
tf_task_resolve_promise(task, promise, typedArray);
|
|
|
|
JS_FreeValue(context, typedArray);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-10-05 21:25:33 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(req->result)));
|
|
|
|
}
|
2022-05-16 18:30:14 -04:00
|
|
|
int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
|
2021-10-05 21:25:33 -04:00
|
|
|
if (result < 0)
|
|
|
|
{
|
2022-06-03 23:01:12 -04:00
|
|
|
uv_fs_req_cleanup(req);
|
2022-06-02 05:40:19 -04:00
|
|
|
free(fsreq);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-10-05 21:25:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _file_read_open_callback(uv_fs_t* req)
|
|
|
|
{
|
|
|
|
uv_fs_req_cleanup(req);
|
2022-06-02 05:40:19 -04:00
|
|
|
fs_req_t* fsreq = (fs_req_t*)req;
|
2021-10-05 21:25:33 -04:00
|
|
|
tf_task_t* task = req->loop->data;
|
|
|
|
JSContext* context = tf_task_get_context(task);
|
|
|
|
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
|
|
|
|
if (req->result >= 0)
|
|
|
|
{
|
2022-06-02 05:40:19 -04:00
|
|
|
uv_buf_t buf = { .base = fsreq->buffer, .len = fsreq->size };
|
|
|
|
fsreq->file = req->result;
|
|
|
|
int result = uv_fs_read(req->loop, req, fsreq->file, &buf, 1, 0, _file_read_read_callback);
|
2021-10-05 21:25:33 -04:00
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(result)));
|
2022-06-02 05:40:19 -04:00
|
|
|
result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
2022-06-03 23:01:12 -04:00
|
|
|
uv_fs_req_cleanup(req);
|
2022-06-02 05:40:19 -04:00
|
|
|
free(fsreq);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
2021-10-05 21:25:33 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(req->result)));
|
2022-06-03 23:01:12 -04:00
|
|
|
uv_fs_req_cleanup(req);
|
2021-10-05 21:25:33 -04:00
|
|
|
free(req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-10-05 21:25:33 -04:00
|
|
|
void* task = JS_GetContextOpaque(context);
|
|
|
|
const char* file_name = JS_ToCString(context, argv[0]);
|
|
|
|
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue promise_value = tf_task_allocate_promise(task, &promise);
|
2022-05-16 18:30:14 -04:00
|
|
|
fs_req_t* req = malloc(sizeof(fs_req_t) + k_file_read_max);
|
|
|
|
*req = (fs_req_t)
|
2021-10-05 21:25:33 -04:00
|
|
|
{
|
2022-05-16 18:30:14 -04:00
|
|
|
.fs =
|
|
|
|
{
|
|
|
|
.data = (void*)(intptr_t)promise,
|
|
|
|
},
|
2022-06-02 05:40:19 -04:00
|
|
|
.size = k_file_read_max,
|
2021-10-05 21:25:33 -04:00
|
|
|
};
|
2022-05-16 18:30:14 -04:00
|
|
|
int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_RDONLY, 0, _file_read_open_callback);
|
2021-10-05 21:25:33 -04:00
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(result)));
|
2022-06-03 23:01:12 -04:00
|
|
|
uv_fs_req_cleanup(&req->fs);
|
2022-03-07 16:57:52 -05:00
|
|
|
free(req);
|
2021-10-05 21:25:33 -04:00
|
|
|
}
|
|
|
|
JS_FreeCString(context, file_name);
|
2022-01-13 22:05:37 -05:00
|
|
|
return promise_value;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-27 19:27:21 -04:00
|
|
|
static void _file_write_write_callback(uv_fs_t* req)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-10-27 19:27:21 -04:00
|
|
|
uv_fs_req_cleanup(req);
|
2022-05-16 18:30:14 -04:00
|
|
|
fs_req_t* fsreq = (fs_req_t*)req;
|
2021-10-27 19:27:21 -04:00
|
|
|
tf_task_t* task = req->loop->data;
|
|
|
|
JSContext* context = tf_task_get_context(task);
|
|
|
|
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
|
|
|
|
if (req->result >= 0)
|
|
|
|
{
|
|
|
|
tf_task_resolve_promise(task, promise, JS_NewInt64(context, req->result));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(req->result)));
|
|
|
|
}
|
2022-06-02 05:40:19 -04:00
|
|
|
int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
2022-06-03 23:01:12 -04:00
|
|
|
uv_fs_req_cleanup(req);
|
2022-06-02 05:40:19 -04:00
|
|
|
free(fsreq);
|
|
|
|
}
|
2021-10-27 19:27:21 -04:00
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-10-27 19:27:21 -04:00
|
|
|
static void _file_write_open_callback(uv_fs_t* req)
|
|
|
|
{
|
2022-05-20 20:06:01 -04:00
|
|
|
fs_req_t* fsreq = (fs_req_t*)req;
|
2021-10-27 19:27:21 -04:00
|
|
|
uv_fs_req_cleanup(req);
|
|
|
|
tf_task_t* task = req->loop->data;
|
|
|
|
JSContext* context = tf_task_get_context(task);
|
|
|
|
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
|
|
|
|
if (req->result >= 0)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-05-20 20:06:01 -04:00
|
|
|
uv_buf_t buf = { .base = fsreq->buffer, .len = fsreq->size };
|
|
|
|
fsreq->file = req->result;
|
|
|
|
int result = uv_fs_write(req->loop, req, fsreq->file, &buf, 1, 0, _file_write_write_callback);
|
2021-10-27 19:27:21 -04:00
|
|
|
if (result < 0)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-10-27 19:27:21 -04:00
|
|
|
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(result)));
|
2022-05-20 20:06:01 -04:00
|
|
|
result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
|
2022-06-02 05:40:19 -04:00
|
|
|
if (result < 0)
|
|
|
|
{
|
2022-06-03 23:01:12 -04:00
|
|
|
uv_fs_req_cleanup(req);
|
2022-06-02 05:40:19 -04:00
|
|
|
free(fsreq);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 19:27:21 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(req->result)));
|
2022-06-03 23:01:12 -04:00
|
|
|
uv_fs_req_cleanup(req);
|
2021-10-27 19:27:21 -04:00
|
|
|
free(req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
void* task = JS_GetContextOpaque(context);
|
|
|
|
const char* file_name = JS_ToCString(context, argv[0]);
|
|
|
|
|
|
|
|
size_t size;
|
2021-11-03 18:15:46 -04:00
|
|
|
uint8_t* buffer = tf_util_try_get_array_buffer(context, &size, argv[1]);
|
2021-10-27 19:27:21 -04:00
|
|
|
bool is_array_buffer = false;
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
is_array_buffer = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = (uint8_t*)JS_ToCStringLen(context, &size, argv[1]);
|
|
|
|
}
|
|
|
|
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue promise_value = tf_task_allocate_promise(task, &promise);
|
2022-05-16 18:30:14 -04:00
|
|
|
fs_req_t* req = malloc(sizeof(fs_req_t) + size);
|
|
|
|
*req = (fs_req_t)
|
2021-10-27 19:27:21 -04:00
|
|
|
{
|
2022-05-16 18:30:14 -04:00
|
|
|
.fs =
|
|
|
|
{
|
|
|
|
.data = (void*)(intptr_t)promise,
|
|
|
|
},
|
|
|
|
.size = size,
|
2021-10-27 19:27:21 -04:00
|
|
|
};
|
2022-05-16 18:30:14 -04:00
|
|
|
memcpy(req->buffer, buffer, size);
|
2021-10-27 19:27:21 -04:00
|
|
|
if (!is_array_buffer)
|
|
|
|
{
|
|
|
|
JS_FreeCString(context, (const char*)buffer);
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:30:14 -04:00
|
|
|
int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_CREAT | UV_FS_O_WRONLY, 0644, _file_write_open_callback);
|
2021-10-27 19:27:21 -04:00
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(result)));
|
|
|
|
}
|
|
|
|
JS_FreeCString(context, file_name);
|
|
|
|
return promise_value;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-27 20:53:16 -04:00
|
|
|
static void _file_async_callback(uv_fs_t* req)
|
|
|
|
{
|
|
|
|
uv_fs_req_cleanup(req);
|
|
|
|
tf_task_t* task = req->loop->data;
|
|
|
|
JSContext* context = tf_task_get_context(task);
|
|
|
|
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
|
|
|
|
if (req->result == 0)
|
|
|
|
{
|
|
|
|
tf_task_resolve_promise(task, promise, JS_NewInt32(context, req->result));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_NewInt32(context, req->result));
|
|
|
|
}
|
|
|
|
free(req);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _file_rename_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
void* task = JS_GetContextOpaque(context);
|
2021-10-27 20:53:16 -04:00
|
|
|
const char* old_name = JS_ToCString(context, argv[0]);
|
|
|
|
const char* new_name = JS_ToCString(context, argv[1]);
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue promise_value = tf_task_allocate_promise(task, &promise);
|
2021-10-27 20:53:16 -04:00
|
|
|
uv_fs_t* req = malloc(sizeof(uv_fs_t));
|
|
|
|
*req = (uv_fs_t)
|
|
|
|
{
|
|
|
|
.data = (void*)(intptr_t)promise,
|
|
|
|
};
|
|
|
|
int result = uv_fs_rename(tf_task_get_loop(task), req, old_name, new_name,_file_async_callback);
|
|
|
|
JS_FreeCString(context, old_name);
|
|
|
|
JS_FreeCString(context, new_name);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_NewInt32(context, result));
|
|
|
|
}
|
|
|
|
return promise_value;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _file_unlink_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
void* task = JS_GetContextOpaque(context);
|
2021-10-27 20:53:16 -04:00
|
|
|
const char* file_name = JS_ToCString(context, argv[0]);
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue promise_value = tf_task_allocate_promise(task, &promise);
|
2021-10-27 20:53:16 -04:00
|
|
|
uv_fs_t* req = malloc(sizeof(uv_fs_t));
|
|
|
|
*req = (uv_fs_t)
|
|
|
|
{
|
|
|
|
.data = (void*)(intptr_t)promise,
|
|
|
|
};
|
|
|
|
int result = uv_fs_unlink(tf_task_get_loop(task), req, file_name, _file_async_callback);
|
|
|
|
JS_FreeCString(context, file_name);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_NewInt32(context, result));
|
|
|
|
}
|
|
|
|
return promise_value;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-27 20:53:16 -04:00
|
|
|
JSValue _file_make_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-10-27 20:53:16 -04:00
|
|
|
void* task = JS_GetContextOpaque(context);
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* directory = JS_ToCString(context, argv[0]);
|
|
|
|
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue promise_value = tf_task_allocate_promise(task, &promise);
|
2021-10-27 20:53:16 -04:00
|
|
|
uv_fs_t* req = malloc(sizeof(uv_fs_t));
|
|
|
|
*req = (uv_fs_t)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-10-27 20:53:16 -04:00
|
|
|
.data = (void*)(intptr_t)promise,
|
|
|
|
};
|
|
|
|
int result = uv_fs_mkdir(tf_task_get_loop(task), req, directory, 0755, _file_async_callback);
|
|
|
|
JS_FreeCString(context, directory);
|
|
|
|
if (result < 0)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-10-27 20:53:16 -04:00
|
|
|
tf_task_reject_promise(task, promise, JS_NewInt32(context, result));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-10-27 20:53:16 -04:00
|
|
|
return promise_value;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-27 20:53:16 -04:00
|
|
|
JSValue _file_remove_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
void* task = JS_GetContextOpaque(context);
|
|
|
|
const char* directory = JS_ToCString(context, argv[0]);
|
|
|
|
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue promise_value = tf_task_allocate_promise(task, &promise);
|
2021-10-27 20:53:16 -04:00
|
|
|
uv_fs_t* req = malloc(sizeof(uv_fs_t));
|
|
|
|
*req = (uv_fs_t)
|
|
|
|
{
|
|
|
|
.data = (void*)(intptr_t)promise,
|
|
|
|
};
|
|
|
|
int result = uv_fs_rmdir(tf_task_get_loop(task), req, directory, _file_async_callback);
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeCString(context, directory);
|
2021-10-27 20:53:16 -04:00
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
tf_task_reject_promise(task, promise, JS_NewInt32(context, result));
|
|
|
|
}
|
|
|
|
return promise_value;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _file_stat(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
void* task = JS_GetContextOpaque(context);
|
|
|
|
const char* path = JS_ToCString(context, argv[0]);
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue promise_value = tf_task_allocate_promise(task, &promise);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_reject_promise(task, promise, JS_NewInt32(context, result));
|
2022-06-03 23:01:12 -04:00
|
|
|
uv_fs_req_cleanup(&data->_request);
|
2021-01-02 13:10:00 -05:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
JS_FreeCString(context, path);
|
2022-01-13 22:05:37 -05:00
|
|
|
return promise_value;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static double _time_spec_to_double(const uv_timespec_t* time_spec)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return time_spec->tv_sec + (double)(time_spec->tv_nsec) / 1e9;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _file_on_stat_complete(uv_fs_t* request)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
file_stat_t* data = (file_stat_t*)(request->data);
|
|
|
|
JSContext* context = data->_context;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (request->result)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_reject_promise(data->_task, data->_promise, JS_NewInt32(context, request->result));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
|
|
|
}
|