Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: fix not exposing tjs.(l)stat #236

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions docs/txikijs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,26 +341,26 @@ declare namespace tjs {
}

interface StatResult {
st_dev: bigint;
st_mode: bigint;
st_nlink: bigint;
st_uid: bigint;
st_gid: bigint;
st_rdev: bigint;
st_ino: bigint;
st_size: bigint;
st_blksize: bigint;
st_blocks: bigint;
st_flags: bigint;
st_gen: bigint;
st_dev: number;
st_mode: number;
st_nlink: number;
st_uid: number;
st_gid: number;
st_rdev: number;
st_ino: number;
st_size: number;
st_blksize: number;
st_blocks: number;
st_flags: number;
st_gen: number;
st_atim: number;
st_mtim: number;
st_ctim: number;
st_birthtim: number;
}

/**
* Flag used to check in {@link StatResult}'s `st_flags` field.
* Flag used to check in {@link StatResult}'s `st_mode` field.
* See [stat(2)](https://man7.org/linux/man-pages/man2/lstat.2.html)
* Available values:
*
Expand All @@ -377,6 +377,23 @@ declare namespace tjs {
*/
const S_XXX: number;

/**
* Gets file status information.
* See [stat(2)](https://man7.org/linux/man-pages/man2/stat.2.html)
*
* @param path Path to the file.
*/
function stat(path: string): Promise<StatResult>;

/**
* Gets file status information. If the path is a link it returns information
* about the link itself.
* See [stat(2)](https://man7.org/linux/man-pages/man2/stat.2.html)
*
* @param path Path to the file.
*/
function lstat(path: string): Promise<StatResult>;

/**
* Opens the file at the given path. Opening modes:
*
Expand Down
6 changes: 3 additions & 3 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ typedef struct {
static JSValue js__stat2obj(JSContext *ctx, uv_stat_t *st) {
JSValue obj = JS_NewObjectProto(ctx, JS_NULL);
#define SET_UINT64_FIELD(x) \
JS_DefinePropertyValueStr(ctx, obj, STRINGIFY(x), JS_NewBigUint64(ctx, st->x), JS_PROP_C_W_E)
JS_DefinePropertyValueStr(ctx, obj, STRINGIFY(x), JS_NewUint32(ctx, st->x), JS_PROP_C_W_E)
SET_UINT64_FIELD(st_dev);
SET_UINT64_FIELD(st_mode);
SET_UINT64_FIELD(st_nlink);
Expand Down Expand Up @@ -837,8 +837,8 @@ static const JSCFunctionListEntry tjs_fs_funcs[] = {
TJS_CONST(S_ISUID),
#endif
TJS_CFUNC_DEF("open", 3, tjs_fs_open),
JS_CFUNC_MAGIC_DEF("stat", 1, tjs_fs_stat, 0),
JS_CFUNC_MAGIC_DEF("lstat", 1, tjs_fs_stat, 1),
TJS_CFUNC_MAGIC_DEF("stat", 1, tjs_fs_stat, 0),
TJS_CFUNC_MAGIC_DEF("lstat", 1, tjs_fs_stat, 1),
TJS_CFUNC_DEF("realpath", 1, tjs_fs_realpath),
TJS_CFUNC_DEF("unlink", 1, tjs_fs_unlink),
TJS_CFUNC_DEF("rename", 2, tjs_fs_rename),
Expand Down
1 change: 1 addition & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void tjs_assert(const struct AssertionInfo info);
#define TJS_CONST(x) JS_PROP_INT32_DEF(#x, x, JS_PROP_ENUMERABLE)
#define TJS_CONST2(name, val) JS_PROP_INT32_DEF(name, val, JS_PROP_ENUMERABLE)
#define TJS_CFUNC_DEF(name, length, func1) { name, JS_PROP_C_W_E, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define TJS_CFUNC_MAGIC_DEF(name, length, func1, magic) { name, JS_PROP_C_W_E, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_generic_magic, { .generic_magic = func1 } } } }

uv_loop_t *tjs_get_loop(JSContext *ctx);
int tjs_obj2addr(JSContext *ctx, JSValueConst obj, struct sockaddr_storage *ss);
Expand Down
10 changes: 10 additions & 0 deletions tests/test-fs-stat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import assert from './assert.js';
import { path } from '@tjs/std';


(async () => {
const st = await tjs.stat(path.join(import.meta.dirname, import.meta.basename));

assert.ok(st);
assert.eq(st.st_mode & tjs.S_IFREG, tjs.S_IFREG, 'is a regular file');
})();