2014-12-22 12:35:05 +01:00
|
|
|
// Module for interfacing with file system
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
|
|
|
|
|
|
|
#include "c_types.h"
|
2016-09-05 20:17:13 +02:00
|
|
|
#include "vfs.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
#include "c_string.h"
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
static int file_fd = 0;
|
|
|
|
static int rtc_cb_ref = LUA_NOREF;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
|
|
|
|
static void table2tm( lua_State *L, vfs_time *tm )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2016-09-05 20:17:13 +02:00
|
|
|
int idx = lua_gettop( L );
|
|
|
|
|
|
|
|
// extract items from table
|
|
|
|
lua_getfield( L, idx, "year" );
|
|
|
|
lua_getfield( L, idx, "mon" );
|
|
|
|
lua_getfield( L, idx, "day" );
|
|
|
|
lua_getfield( L, idx, "hour" );
|
|
|
|
lua_getfield( L, idx, "min" );
|
|
|
|
lua_getfield( L, idx, "sec" );
|
|
|
|
|
|
|
|
tm->year = luaL_optint( L, ++idx, 2016 );
|
|
|
|
tm->mon = luaL_optint( L, ++idx, 6 );
|
|
|
|
tm->day = luaL_optint( L, ++idx, 21 );
|
|
|
|
tm->hour = luaL_optint( L, ++idx, 0 );
|
|
|
|
tm->min = luaL_optint( L, ++idx, 0 );
|
|
|
|
tm->sec = luaL_optint( L, ++idx, 0 );
|
|
|
|
|
|
|
|
// remove items from stack
|
|
|
|
lua_pop( L, 6 );
|
|
|
|
}
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
static sint32_t file_rtc_cb( vfs_time *tm )
|
|
|
|
{
|
|
|
|
sint32_t res = VFS_RES_ERR;
|
2016-02-14 00:58:04 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
if (rtc_cb_ref != LUA_NOREF) {
|
|
|
|
lua_State *L = lua_getstate();
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
lua_rawgeti( L, LUA_REGISTRYINDEX, rtc_cb_ref );
|
|
|
|
lua_call( L, 0, 1 );
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
if (lua_type( L, lua_gettop( L ) ) == LUA_TTABLE) {
|
|
|
|
table2tm( L, tm );
|
|
|
|
res = VFS_RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pop item returned by callback
|
|
|
|
lua_pop( L, 1 );
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
2016-09-05 20:17:13 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: on()
|
|
|
|
static int file_on(lua_State *L)
|
|
|
|
{
|
|
|
|
enum events{
|
|
|
|
ON_RTC = 0
|
|
|
|
};
|
|
|
|
const char *const eventnames[] = {"rtc", NULL};
|
|
|
|
|
|
|
|
int event = luaL_checkoption(L, 1, "rtc", eventnames);
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case ON_RTC:
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, rtc_cb_ref);
|
|
|
|
|
|
|
|
if ((lua_type(L, 2) == LUA_TFUNCTION) ||
|
|
|
|
(lua_type(L, 2) == LUA_TLIGHTFUNCTION)) {
|
|
|
|
lua_pushvalue(L, 2); // copy argument (func) to the top of stack
|
|
|
|
rtc_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
vfs_register_rtc_cb(file_rtc_cb);
|
|
|
|
} else {
|
|
|
|
rtc_cb_ref = LUA_NOREF;
|
|
|
|
vfs_register_rtc_cb(NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: close()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_close( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2016-09-05 20:17:13 +02:00
|
|
|
if(file_fd){
|
|
|
|
vfs_close(file_fd);
|
|
|
|
file_fd = 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: format()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_format( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
file_close(L);
|
2016-09-05 20:17:13 +02:00
|
|
|
if( !vfs_format() )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2016-06-05 23:10:58 +02:00
|
|
|
NODE_ERR( "\n*** ERROR ***: unable to format. FS might be compromised.\n" );
|
2015-01-07 08:57:17 +01:00
|
|
|
NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
|
2016-06-05 23:10:58 +02:00
|
|
|
luaL_error(L, "Failed to format file system");
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
NODE_ERR( "format done.\n" );
|
|
|
|
}
|
2016-09-05 20:17:13 +02:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
static int file_fscfg (lua_State *L)
|
|
|
|
{
|
|
|
|
uint32_t phys_addr, phys_size;
|
|
|
|
|
|
|
|
vfs_fscfg("/FLASH", &phys_addr, &phys_size);
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
lua_pushinteger (L, phys_addr);
|
|
|
|
lua_pushinteger (L, phys_size);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: open(filename, mode)
|
|
|
|
static int file_open( lua_State* L )
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
if(file_fd){
|
|
|
|
vfs_close(file_fd);
|
|
|
|
file_fd = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *fname = luaL_checklstring( L, 1, &len );
|
|
|
|
const char *basename = vfs_basename( fname );
|
|
|
|
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(fname) == len, 1, "filename invalid");
|
|
|
|
|
|
|
|
const char *mode = luaL_optstring(L, 2, "r");
|
|
|
|
|
|
|
|
file_fd = vfs_open(fname, mode);
|
|
|
|
|
|
|
|
if(!file_fd){
|
|
|
|
lua_pushnil(L);
|
|
|
|
} else {
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
// Lua: list()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_list( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2016-09-05 20:17:13 +02:00
|
|
|
vfs_dir *dir;
|
|
|
|
vfs_item *item;
|
|
|
|
|
|
|
|
if (dir = vfs_opendir("")) {
|
|
|
|
lua_newtable( L );
|
|
|
|
while (item = vfs_readdir(dir)) {
|
|
|
|
lua_pushinteger(L, vfs_item_size(item));
|
|
|
|
lua_setfield(L, -2, vfs_item_name(item));
|
|
|
|
vfs_closeitem(item);
|
|
|
|
}
|
|
|
|
vfs_closedir(dir);
|
|
|
|
return 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
2016-09-05 20:17:13 +02:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_seek (lua_State *L)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2016-09-05 20:17:13 +02:00
|
|
|
static const int mode[] = {VFS_SEEK_SET, VFS_SEEK_CUR, VFS_SEEK_END};
|
2014-12-22 12:35:05 +01:00
|
|
|
static const char *const modenames[] = {"set", "cur", "end", NULL};
|
2016-09-05 20:17:13 +02:00
|
|
|
if(!file_fd)
|
2014-12-22 12:35:05 +01:00
|
|
|
return luaL_error(L, "open a file first");
|
|
|
|
int op = luaL_checkoption(L, 1, "cur", modenames);
|
|
|
|
long offset = luaL_optlong(L, 2, 0);
|
2016-09-05 20:17:13 +02:00
|
|
|
op = vfs_lseek(file_fd, offset, mode[op]);
|
2015-07-16 08:11:22 +02:00
|
|
|
if (op < 0)
|
2015-02-25 20:34:07 +01:00
|
|
|
lua_pushnil(L); /* error */
|
2014-12-22 12:35:05 +01:00
|
|
|
else
|
2016-09-05 20:17:13 +02:00
|
|
|
lua_pushinteger(L, vfs_tell(file_fd));
|
2014-12-22 12:35:05 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-13 23:37:04 +01:00
|
|
|
// Lua: exists(filename)
|
|
|
|
static int file_exists( lua_State* L )
|
|
|
|
{
|
|
|
|
size_t len;
|
2016-02-14 00:58:04 +01:00
|
|
|
const char *fname = luaL_checklstring( L, 1, &len );
|
2016-09-05 20:17:13 +02:00
|
|
|
const char *basename = vfs_basename( fname );
|
|
|
|
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(fname) == len, 1, "filename invalid");
|
2016-02-13 23:37:04 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
vfs_item *stat = vfs_stat((char *)fname);
|
2016-02-13 23:37:04 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
lua_pushboolean(L, stat ? 1 : 0);
|
|
|
|
|
|
|
|
if (stat) vfs_closeitem(stat);
|
2016-02-13 23:37:04 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Lua: remove(filename)
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_remove( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
size_t len;
|
2016-02-14 00:58:04 +01:00
|
|
|
const char *fname = luaL_checklstring( L, 1, &len );
|
2016-09-05 20:17:13 +02:00
|
|
|
const char *basename = vfs_basename( fname );
|
|
|
|
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(fname) == len, 1, "filename invalid");
|
2014-12-22 12:35:05 +01:00
|
|
|
file_close(L);
|
2016-09-05 20:17:13 +02:00
|
|
|
vfs_remove((char *)fname);
|
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: flush()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_flush( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2016-09-05 20:17:13 +02:00
|
|
|
if(!file_fd)
|
2014-12-22 12:35:05 +01:00
|
|
|
return luaL_error(L, "open a file first");
|
2016-09-05 20:17:13 +02:00
|
|
|
if(vfs_flush(file_fd) == 0)
|
2014-12-22 12:35:05 +01:00
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-11 14:20:54 +01:00
|
|
|
// Lua: rename("oldname", "newname")
|
|
|
|
static int file_rename( lua_State* L )
|
|
|
|
{
|
|
|
|
size_t len;
|
2016-09-05 20:17:13 +02:00
|
|
|
if(file_fd){
|
|
|
|
vfs_close(file_fd);
|
|
|
|
file_fd = 0;
|
2015-02-11 14:20:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *oldname = luaL_checklstring( L, 1, &len );
|
2016-09-05 20:17:13 +02:00
|
|
|
const char *basename = vfs_basename( oldname );
|
|
|
|
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(oldname) == len, 1, "filename invalid");
|
2016-02-14 00:58:04 +01:00
|
|
|
|
|
|
|
const char *newname = luaL_checklstring( L, 2, &len );
|
2016-09-05 20:17:13 +02:00
|
|
|
basename = vfs_basename( newname );
|
|
|
|
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(newname) == len, 2, "filename invalid");
|
2015-02-11 14:20:54 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
if(0 <= vfs_rename( oldname, newname )){
|
2015-02-11 14:20:54 +01:00
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
} else {
|
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-31 06:53:26 +01:00
|
|
|
// g_read()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_g_read( lua_State* L, int n, int16_t end_char )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2015-07-31 08:29:29 +02:00
|
|
|
if(n <= 0 || n > LUAL_BUFFERSIZE)
|
2014-12-31 06:53:26 +01:00
|
|
|
n = LUAL_BUFFERSIZE;
|
|
|
|
if(end_char < 0 || end_char >255)
|
|
|
|
end_char = EOF;
|
2015-01-08 09:03:21 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
luaL_Buffer b;
|
2016-09-05 20:17:13 +02:00
|
|
|
if(!file_fd)
|
2014-12-22 12:35:05 +01:00
|
|
|
return luaL_error(L, "open a file first");
|
|
|
|
|
|
|
|
luaL_buffinit(L, &b);
|
|
|
|
char *p = luaL_prepbuffer(&b);
|
2015-07-31 08:29:29 +02:00
|
|
|
int i;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
n = vfs_read(file_fd, p, n);
|
2015-07-31 08:29:29 +02:00
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
if (p[i] == end_char)
|
|
|
|
{
|
|
|
|
++i;
|
2014-12-22 12:35:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(i==0){
|
|
|
|
luaL_pushresult(&b); /* close buffer */
|
|
|
|
return (lua_objlen(L, -1) > 0); /* check whether read something */
|
|
|
|
}
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
vfs_lseek(file_fd, -(n - i), VFS_SEEK_CUR);
|
2014-12-22 12:35:05 +01:00
|
|
|
luaL_addsize(&b, i);
|
|
|
|
luaL_pushresult(&b); /* close buffer */
|
|
|
|
return 1; /* read at least an `eol' */
|
|
|
|
}
|
|
|
|
|
2014-12-31 06:53:26 +01:00
|
|
|
// Lua: read()
|
|
|
|
// file.read() will read all byte in file
|
|
|
|
// file.read(10) will read 10 byte from file, or EOF is reached.
|
|
|
|
// file.read('q') will read until 'q' or EOF is reached.
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_read( lua_State* L )
|
2014-12-31 06:53:26 +01:00
|
|
|
{
|
|
|
|
unsigned need_len = LUAL_BUFFERSIZE;
|
|
|
|
int16_t end_char = EOF;
|
|
|
|
size_t el;
|
|
|
|
if( lua_type( L, 1 ) == LUA_TNUMBER )
|
|
|
|
{
|
|
|
|
need_len = ( unsigned )luaL_checkinteger( L, 1 );
|
|
|
|
if( need_len > LUAL_BUFFERSIZE ){
|
|
|
|
need_len = LUAL_BUFFERSIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(lua_isstring(L, 1))
|
|
|
|
{
|
|
|
|
const char *end = luaL_checklstring( L, 1, &el );
|
|
|
|
if(el!=1){
|
|
|
|
return luaL_error( L, "wrong arg range" );
|
|
|
|
}
|
|
|
|
end_char = (int16_t)end[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_g_read(L, need_len, end_char);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: readline()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_readline( lua_State* L )
|
2014-12-31 06:53:26 +01:00
|
|
|
{
|
|
|
|
return file_g_read(L, LUAL_BUFFERSIZE, '\n');
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Lua: write("string")
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_write( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2016-09-05 20:17:13 +02:00
|
|
|
if(!file_fd)
|
2014-12-22 12:35:05 +01:00
|
|
|
return luaL_error(L, "open a file first");
|
|
|
|
size_t l, rl;
|
|
|
|
const char *s = luaL_checklstring(L, 1, &l);
|
2016-09-05 20:17:13 +02:00
|
|
|
rl = vfs_write(file_fd, s, l);
|
2014-12-22 12:35:05 +01:00
|
|
|
if(rl==l)
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: writeline("string")
|
2015-01-05 03:09:51 +01:00
|
|
|
static int file_writeline( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2016-09-05 20:17:13 +02:00
|
|
|
if(!file_fd)
|
2014-12-22 12:35:05 +01:00
|
|
|
return luaL_error(L, "open a file first");
|
|
|
|
size_t l, rl;
|
|
|
|
const char *s = luaL_checklstring(L, 1, &l);
|
2016-09-05 20:17:13 +02:00
|
|
|
rl = vfs_write(file_fd, s, l);
|
2014-12-22 12:35:05 +01:00
|
|
|
if(rl==l){
|
2016-09-05 20:17:13 +02:00
|
|
|
rl = vfs_write(file_fd, "\n", 1);
|
2014-12-22 12:35:05 +01:00
|
|
|
if(rl==1)
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
// Lua: fsinfo()
|
|
|
|
static int file_fsinfo( lua_State* L )
|
2016-01-12 07:53:45 +01:00
|
|
|
{
|
2016-09-05 20:17:13 +02:00
|
|
|
u32_t total, used;
|
|
|
|
if (vfs_fsinfo("", &total, &used)) {
|
|
|
|
return luaL_error(L, "file system failed");
|
|
|
|
}
|
|
|
|
NODE_DBG("total: %d, used:%d\n", total, used);
|
|
|
|
if(total>0x7FFFFFFF || used>0x7FFFFFFF || used > total)
|
|
|
|
{
|
|
|
|
return luaL_error(L, "file system error");
|
|
|
|
}
|
|
|
|
lua_pushinteger(L, total-used);
|
|
|
|
lua_pushinteger(L, used);
|
|
|
|
lua_pushinteger(L, total);
|
|
|
|
return 3;
|
2016-01-12 07:53:45 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
typedef struct {
|
|
|
|
vfs_vol *vol;
|
|
|
|
} volume_type;
|
|
|
|
|
|
|
|
// Lua: vol = file.mount("/SD0")
|
|
|
|
static int file_mount( lua_State *L )
|
|
|
|
{
|
|
|
|
const char *ldrv = luaL_checkstring( L, 1 );
|
|
|
|
int num = luaL_optint( L, 2, -1 );
|
|
|
|
volume_type *vol = (volume_type *)lua_newuserdata( L, sizeof( volume_type ) );
|
|
|
|
|
|
|
|
if (vol->vol = vfs_mount( ldrv, num )) {
|
|
|
|
/* set its metatable */
|
|
|
|
luaL_getmetatable(L, "vfs.vol");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
// remove created userdata
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: success = file.chdir("/SD0/")
|
|
|
|
static int file_chdir( lua_State *L )
|
|
|
|
{
|
|
|
|
const char *path = luaL_checkstring( L, 1 );
|
|
|
|
|
|
|
|
lua_pushboolean( L, 0 <= vfs_chdir( path ) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_vol_umount( lua_State *L )
|
|
|
|
{
|
|
|
|
volume_type *vol = luaL_checkudata( L, 1, "file.vol" );
|
|
|
|
luaL_argcheck( L, vol, 1, "volume expected" );
|
|
|
|
|
|
|
|
lua_pushboolean( L, 0 <= vfs_umount( vol->vol ) );
|
|
|
|
|
|
|
|
// invalidate vfs descriptor, it has been free'd anyway
|
|
|
|
vol->vol = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const LUA_REG_TYPE file_vol_map[] =
|
|
|
|
{
|
|
|
|
{ LSTRKEY( "umount" ), LFUNCVAL( file_vol_umount )},
|
|
|
|
//{ LSTRKEY( "getfree" ), LFUNCVAL( file_vol_getfree )},
|
|
|
|
//{ LSTRKEY( "getlabel" ), LFUNCVAL( file_vol_getlabel )},
|
|
|
|
//{ LSTRKEY( "__gc" ), LFUNCVAL( file_vol_free ) },
|
|
|
|
{ LSTRKEY( "__index" ), LROVAL( file_vol_map ) },
|
|
|
|
{ LNILKEY, LNILVAL }
|
|
|
|
};
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Module function map
|
2015-12-16 06:04:58 +01:00
|
|
|
static const LUA_REG_TYPE file_map[] = {
|
2015-12-13 03:29:37 +01:00
|
|
|
{ LSTRKEY( "list" ), LFUNCVAL( file_list ) },
|
|
|
|
{ LSTRKEY( "open" ), LFUNCVAL( file_open ) },
|
|
|
|
{ LSTRKEY( "close" ), LFUNCVAL( file_close ) },
|
|
|
|
{ LSTRKEY( "write" ), LFUNCVAL( file_write ) },
|
2014-12-22 12:35:05 +01:00
|
|
|
{ LSTRKEY( "writeline" ), LFUNCVAL( file_writeline ) },
|
2015-12-13 03:29:37 +01:00
|
|
|
{ LSTRKEY( "read" ), LFUNCVAL( file_read ) },
|
|
|
|
{ LSTRKEY( "readline" ), LFUNCVAL( file_readline ) },
|
2016-09-05 20:17:13 +02:00
|
|
|
#ifdef BUILD_SPIFFS
|
2015-12-13 03:29:37 +01:00
|
|
|
{ LSTRKEY( "format" ), LFUNCVAL( file_format ) },
|
2016-09-05 20:17:13 +02:00
|
|
|
{ LSTRKEY( "fscfg" ), LFUNCVAL( file_fscfg ) },
|
|
|
|
#endif
|
2015-12-13 03:29:37 +01:00
|
|
|
{ LSTRKEY( "remove" ), LFUNCVAL( file_remove ) },
|
|
|
|
{ LSTRKEY( "seek" ), LFUNCVAL( file_seek ) },
|
|
|
|
{ LSTRKEY( "flush" ), LFUNCVAL( file_flush ) },
|
|
|
|
{ LSTRKEY( "rename" ), LFUNCVAL( file_rename ) },
|
2016-02-13 23:37:04 +01:00
|
|
|
{ LSTRKEY( "exists" ), LFUNCVAL( file_exists ) },
|
2016-09-05 20:17:13 +02:00
|
|
|
{ LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) },
|
|
|
|
{ LSTRKEY( "on" ), LFUNCVAL( file_on ) },
|
|
|
|
#ifdef BUILD_FATFS
|
|
|
|
{ LSTRKEY( "mount" ), LFUNCVAL( file_mount ) },
|
|
|
|
{ LSTRKEY( "chdir" ), LFUNCVAL( file_chdir ) },
|
2014-12-22 12:35:05 +01:00
|
|
|
#endif
|
|
|
|
{ LNILKEY, LNILVAL }
|
|
|
|
};
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
int luaopen_file( lua_State *L ) {
|
|
|
|
luaL_rometatable( L, "file.vol", (void *)file_vol_map );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODEMCU_MODULE(FILE, "file", file_map, luaopen_file);
|