diff options
author | TSaaristo <tero.saaristo@gmail.com> | 2017-12-11 23:04:51 +0200 |
---|---|---|
committer | Jan Ekström <jeebjp@gmail.com> | 2017-12-13 21:55:28 +0200 |
commit | 522bfe5be1212f356acbbd7d566092f8bd8d0748 (patch) | |
tree | 4e0992a9f092e9794c00290bd85f0aa8af30e8c0 /player/javascript.c | |
parent | 47131365a323914d667ba23d50474717f88cd81c (diff) | |
download | mpv-522bfe5be1212f356acbbd7d566092f8bd8d0748.tar.bz2 mpv-522bfe5be1212f356acbbd7d566092f8bd8d0748.tar.xz |
lua+js: implement utils.file_info()
This commit introduces mp.utils.file_info() for querying information
on file paths, implemented for both Lua and Javascript.
The function takes a file path as an argument and returns a Lua table /
JS object upon success. The table/object will contain the values:
mode, size, atime, mtime, ctime and the convenience booleans is_file, is_dir.
On error, the Lua side will return `nil, error` and the Javascript side
will return `undefined` (and mark the last error).
This feature utilizes the already existing cross-platform `mp_stat()`
function.
Diffstat (limited to 'player/javascript.c')
-rw-r--r-- | player/javascript.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/player/javascript.c b/player/javascript.c index 14c1872d67..3de900bdb1 100644 --- a/player/javascript.c +++ b/player/javascript.c @@ -836,6 +836,41 @@ static void script_readdir(js_State *J, void *af) } } +static void script_file_info(js_State *J) +{ + const char *path = js_tostring(J, 1); + + struct stat statbuf; + if (stat(path, &statbuf) != 0) { + push_failure(J, "Cannot stat path"); + return; + } + // Clear last error + set_last_error(jctx(J), 0, NULL); + + const char * stat_names[] = { + "mode", "size", + "atime", "mtime", "ctime", NULL + }; + const double stat_values[] = { + statbuf.st_mode, + statbuf.st_size, + statbuf.st_atime, + statbuf.st_mtime, + statbuf.st_ctime + }; + // Create an object and add all fields + push_nums_obj(J, stat_names, stat_values); + + // Convenience booleans + js_pushboolean(J, S_ISREG(statbuf.st_mode)); + js_setproperty(J, -2, "is_file"); + + js_pushboolean(J, S_ISDIR(statbuf.st_mode)); + js_setproperty(J, -2, "is_dir"); +} + + static void script_split_path(js_State *J) { const char *p = js_tostring(J, 1); @@ -1255,6 +1290,7 @@ static const struct fn_entry main_fns[] = { static const struct fn_entry utils_fns[] = { AF_ENTRY(readdir, 2), + FN_ENTRY(file_info, 1), FN_ENTRY(split_path, 1), AF_ENTRY(join_path, 2), AF_ENTRY(get_user_path, 1), |