A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 152 lines 4.3 kB view raw
1/* 2 * Based on LuaFileSystem : http://www.keplerproject.org/luafilesystem 3 * 4 * Copyright © 2003 Kepler Project. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 */ 25 26#include "plugin.h" 27#include "rocklibc.h" 28 29#include "lauxlib.h" 30#include "luadir.h" 31 32#define DIR_METATABLE "directory metatable" 33typedef struct dir_data { 34 int closed; 35 DIR *dir; 36} dir_data; 37 38static int make_dir (lua_State *L) { 39 const char *path = luaL_checkstring (L, 1); 40 lua_pushboolean (L, !rb->mkdir(path)); 41 return 1; 42} 43 44static int remove_dir (lua_State *L) { 45 const char *path = luaL_checkstring (L, 1); 46 lua_pushboolean (L, !rb->rmdir (path)); 47 return 1; 48} 49 50 51/* 52** Directory iterator 53*/ 54static int dir_iter (lua_State *L) { 55 struct dirent *entry; 56 dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); 57 58 luaL_argcheck (L, !d->closed, 1, "closed directory"); 59 60 if ((entry = rb->readdir (d->dir)) != NULL) { 61 struct dirinfo info = rb->dir_get_info(d->dir, entry); 62 lua_pushstring (L, entry->d_name); 63 lua_pushboolean (L, info.attribute & ATTR_DIRECTORY); 64 if (lua_toboolean (L, lua_upvalueindex(1))) { 65 lua_createtable(L, 0, 3); 66 lua_pushnumber (L, info.attribute); 67 lua_setfield (L, -2, "attribute"); 68 lua_pushnumber (L, info.size); 69 lua_setfield (L, -2, "size"); 70 lua_pushnumber (L, info.mtime); 71 lua_setfield (L, -2, "time"); 72 } 73 else 74 { 75 lua_pushnil(L); 76 } 77 return 3; 78 } else { 79 /* no more entries => close directory */ 80 rb->closedir (d->dir); 81 d->closed = 1; 82 return 0; 83 } 84} 85 86 87/* 88** Closes directory iterators 89*/ 90static int dir_close (lua_State *L) { 91 dir_data *d = (dir_data *)lua_touserdata (L, 1); 92 93 if (!d->closed && d->dir) { 94 rb->closedir (d->dir); 95 d->closed = 1; 96 } 97 return 0; 98} 99 100 101/* 102** Factory of directory iterators 103*/ 104static int dir_iter_factory (lua_State *L) { 105 const char *path = luaL_checkstring (L, 1); 106 dir_data *d; 107 lua_settop(L, 2); /* index 2 (bool) return attribute table */ 108 lua_pushcclosure(L, &dir_iter, 1); 109 d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); 110 d->closed = 0; 111 112 luaL_getmetatable (L, DIR_METATABLE); 113 lua_setmetatable (L, -2); 114 d->dir = rb->opendir (path); 115 if (d->dir == NULL) 116 { 117 luaL_error (L, "cannot open %s: %d", path, errno); 118 } 119 return 2; 120} 121 122 123/* 124** Creates directory metatable. 125*/ 126static int dir_create_meta (lua_State *L) { 127 luaL_newmetatable (L, DIR_METATABLE); 128 lua_createtable(L, 0, 2); 129 lua_pushcfunction (L, dir_iter); 130 lua_setfield (L, -2, "next"); 131 lua_pushcfunction (L, dir_close); 132 lua_setfield (L, -2, "close"); 133 /* set its __index field */ 134 lua_setfield (L, -2, "__index"); 135 /* set its __gc field */ 136 lua_pushcfunction (L, dir_close); 137 lua_setfield (L, -2, "__gc"); 138 return 1; 139} 140 141static const struct luaL_reg fslib[] = { 142 {"dir", dir_iter_factory}, 143 {"mkdir", make_dir}, 144 {"rmdir", remove_dir}, 145 {NULL, NULL}, 146}; 147 148int luaopen_luadir (lua_State *L) { 149 dir_create_meta (L); 150 luaL_register (L, LUA_DIRLIBNAME, fslib); 151 return 1; 152}