summaryrefslogtreecommitdiffstats
path: root/player/lua.c
diff options
context:
space:
mode:
authorTSaaristo <tero.saaristo@gmail.com>2017-12-11 23:04:51 +0200
committerJan Ekström <jeebjp@gmail.com>2017-12-13 21:55:28 +0200
commit522bfe5be1212f356acbbd7d566092f8bd8d0748 (patch)
tree4e0992a9f092e9794c00290bd85f0aa8af30e8c0 /player/lua.c
parent47131365a323914d667ba23d50474717f88cd81c (diff)
downloadmpv-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/lua.c')
-rw-r--r--player/lua.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/player/lua.c b/player/lua.c
index cf754b661b..e084624ccf 100644
--- a/player/lua.c
+++ b/player/lua.c
@@ -1085,6 +1085,48 @@ static int script_readdir(lua_State *L)
return 1;
}
+static int script_file_info(lua_State *L)
+{
+ const char *path = luaL_checkstring(L, 1);
+
+ struct stat statbuf;
+ if (stat(path, &statbuf) != 0) {
+ lua_pushnil(L);
+ lua_pushstring(L, "error");
+ return 2;
+ }
+
+ lua_newtable(L); // Result stat table
+
+ const char * stat_names[] = {
+ "mode", "size",
+ "atime", "mtime", "ctime", NULL
+ };
+ const unsigned int stat_values[] = {
+ statbuf.st_mode,
+ statbuf.st_size,
+ statbuf.st_atime,
+ statbuf.st_mtime,
+ statbuf.st_ctime
+ };
+
+ // Add all fields
+ for (int i = 0; stat_names[i]; i++) {
+ lua_pushinteger(L, stat_values[i]);
+ lua_setfield(L, -2, stat_names[i]);
+ }
+
+ // Convenience booleans
+ lua_pushboolean(L, S_ISREG(statbuf.st_mode));
+ lua_setfield(L, -2, "is_file");
+
+ lua_pushboolean(L, S_ISDIR(statbuf.st_mode));
+ lua_setfield(L, -2, "is_dir");
+
+ // Return table
+ return 1;
+}
+
static int script_split_path(lua_State *L)
{
const char *p = luaL_checkstring(L, 1);
@@ -1291,6 +1333,7 @@ static const struct fn_entry main_fns[] = {
static const struct fn_entry utils_fns[] = {
FN_ENTRY(readdir),
+ FN_ENTRY(file_info),
FN_ENTRY(split_path),
FN_ENTRY(join_path),
FN_ENTRY(subprocess),