file.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // Module for interfacing with file system
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "lmem.h"
  5. #include "platform.h"
  6. #include <stdint.h>
  7. #include "vfs.h"
  8. #include <string.h>
  9. #include <alloca.h>
  10. #define FILE_READ_CHUNK 1024
  11. // use this time/date in absence of a timestamp
  12. #define FILE_TIMEDEF_YEAR 1970
  13. #define FILE_TIMEDEF_MON 01
  14. #define FILE_TIMEDEF_DAY 01
  15. #define FILE_TIMEDEF_HOUR 00
  16. #define FILE_TIMEDEF_MIN 00
  17. #define FILE_TIMEDEF_SEC 00
  18. static int file_fd = 0;
  19. static int file_fd_ref = LUA_NOREF;
  20. static int rtc_cb_ref = LUA_NOREF;
  21. typedef struct _file_fd_ud {
  22. int fd;
  23. } file_fd_ud;
  24. static void table2tm( lua_State *L, vfs_time *tm )
  25. {
  26. int idx = lua_gettop( L );
  27. // extract items from table
  28. lua_getfield( L, idx, "year" );
  29. lua_getfield( L, idx, "mon" );
  30. lua_getfield( L, idx, "day" );
  31. lua_getfield( L, idx, "hour" );
  32. lua_getfield( L, idx, "min" );
  33. lua_getfield( L, idx, "sec" );
  34. tm->year = luaL_optint( L, ++idx, FILE_TIMEDEF_YEAR );
  35. tm->mon = luaL_optint( L, ++idx, FILE_TIMEDEF_MON );
  36. tm->day = luaL_optint( L, ++idx, FILE_TIMEDEF_DAY );
  37. tm->hour = luaL_optint( L, ++idx, FILE_TIMEDEF_HOUR );
  38. tm->min = luaL_optint( L, ++idx, FILE_TIMEDEF_MIN );
  39. tm->sec = luaL_optint( L, ++idx, FILE_TIMEDEF_SEC );
  40. // remove items from stack
  41. lua_pop( L, 6 );
  42. }
  43. static sint32_t file_rtc_cb( vfs_time *tm )
  44. {
  45. sint32_t res = VFS_RES_ERR;
  46. if (rtc_cb_ref != LUA_NOREF) {
  47. lua_State *L = lua_getstate();
  48. lua_rawgeti( L, LUA_REGISTRYINDEX, rtc_cb_ref );
  49. if (luaL_pcallx( L, 0, 1 ) != LUA_OK)
  50. return res;
  51. if (lua_type( L, lua_gettop( L ) ) == LUA_TTABLE) {
  52. table2tm( L, tm );
  53. res = VFS_RES_OK;
  54. }
  55. // pop item returned by callback
  56. lua_pop( L, 1 );
  57. }
  58. return res;
  59. }
  60. // Lua: on()
  61. static int file_on(lua_State *L)
  62. {
  63. enum events{
  64. ON_RTC = 0
  65. };
  66. const char *const eventnames[] = {"rtc", NULL};
  67. int event = luaL_checkoption(L, 1, "rtc", eventnames);
  68. switch (event) {
  69. case ON_RTC:
  70. luaL_unref(L, LUA_REGISTRYINDEX, rtc_cb_ref);
  71. switch(lua_type(L, 2)) {
  72. case LUA_TFUNCTION:
  73. lua_pushvalue(L, 2); // copy argument (func) to the top of stack
  74. rtc_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  75. vfs_register_rtc_cb(file_rtc_cb);
  76. break;
  77. case LUA_TNIL:
  78. rtc_cb_ref = LUA_NOREF;
  79. vfs_register_rtc_cb(NULL);
  80. break;
  81. default:
  82. luaL_error(L, "Callback should be function or nil");
  83. }
  84. break;
  85. default:
  86. break;
  87. }
  88. return 0;
  89. }
  90. // Lua: close()
  91. static int file_close( lua_State* L )
  92. {
  93. int need_pop = FALSE;
  94. file_fd_ud *ud;
  95. if (lua_type( L, 1 ) != LUA_TUSERDATA) {
  96. // fall back to last opened file
  97. if (file_fd_ref != LUA_NOREF) {
  98. lua_rawgeti( L, LUA_REGISTRYINDEX, file_fd_ref );
  99. // top of stack is now default file descriptor
  100. ud = (file_fd_ud *)luaL_checkudata(L, -1, "file.obj");
  101. lua_pop( L, 1 );
  102. } else {
  103. // no default file currently opened
  104. return 0;
  105. }
  106. } else {
  107. ud = (file_fd_ud *)luaL_checkudata(L, 1, "file.obj");
  108. }
  109. if(ud->fd){
  110. vfs_close(ud->fd);
  111. // mark as closed
  112. ud->fd = 0;
  113. }
  114. // unref default file descriptor
  115. luaL_unref( L, LUA_REGISTRYINDEX, file_fd_ref );
  116. file_fd_ref = LUA_NOREF;
  117. return 0;
  118. }
  119. static int file_obj_free( lua_State *L )
  120. {
  121. file_fd_ud *ud = (file_fd_ud *)luaL_checkudata(L, 1, "file.obj");
  122. if (ud->fd) {
  123. // close file if it's still open
  124. vfs_close(ud->fd);
  125. ud->fd = 0;
  126. }
  127. return 0;
  128. }
  129. // Lua: format()
  130. static int file_format( lua_State* L )
  131. {
  132. size_t len;
  133. file_close(L);
  134. if( !vfs_format() )
  135. {
  136. NODE_ERR( "\n*** ERROR ***: unable to format. FS might be compromised.\n" );
  137. NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
  138. luaL_error(L, "Failed to format file system");
  139. }
  140. else{
  141. NODE_ERR( "format done.\n" );
  142. }
  143. return 0;
  144. }
  145. static int file_fscfg (lua_State *L)
  146. {
  147. uint32_t phys_addr, phys_size;
  148. vfs_fscfg("/FLASH", &phys_addr, &phys_size);
  149. lua_pushinteger (L, phys_addr);
  150. lua_pushinteger (L, phys_size);
  151. return 2;
  152. }
  153. // Lua: open(filename, mode)
  154. static int file_open( lua_State* L )
  155. {
  156. size_t len;
  157. // unref last file descriptor to allow gc'ing if not kept by user script
  158. luaL_unref( L, LUA_REGISTRYINDEX, file_fd_ref );
  159. file_fd_ref = LUA_NOREF;
  160. const char *fname = luaL_checklstring( L, 1, &len );
  161. const char *basename = vfs_basename( fname );
  162. luaL_argcheck(L, strlen(basename) <= FS_OBJ_NAME_LEN && strlen(fname) == len, 1, "filename invalid");
  163. const char *mode = luaL_optstring(L, 2, "r");
  164. file_fd = vfs_open(fname, mode);
  165. if(!file_fd){
  166. lua_pushnil(L);
  167. } else {
  168. file_fd_ud *ud = (file_fd_ud *) lua_newuserdata( L, sizeof( file_fd_ud ) );
  169. ud->fd = file_fd;
  170. luaL_getmetatable( L, "file.obj" );
  171. lua_setmetatable( L, -2 );
  172. // store reference to opened file
  173. lua_pushvalue( L, -1 );
  174. file_fd_ref = luaL_ref( L, LUA_REGISTRYINDEX );
  175. }
  176. return 1;
  177. }
  178. // Lua: list()
  179. static int file_list( lua_State* L )
  180. {
  181. vfs_dir *dir;
  182. const char *pattern;
  183. struct vfs_stat stat;
  184. int pcres;
  185. lua_settop(L, 1);
  186. pattern = luaL_optstring(L, 1, NULL); /* Pattern (arg) or nil (not) at 1 */
  187. dir = vfs_opendir("");
  188. if (dir == NULL) {
  189. return 0;
  190. }
  191. lua_newtable( L ); /* Table at 2 */
  192. if (pattern) {
  193. /*
  194. * We know that pattern is a string, and so the "match" method will always
  195. * exist. No need to check return value here
  196. */
  197. luaL_getmetafield( L, 1, "match" ); /* Function at 3 */
  198. }
  199. while (vfs_readdir(dir, &stat) == VFS_RES_OK) {
  200. if (pattern) {
  201. lua_settop( L, 3 ); /* Ensure nothing else on stack */
  202. /* Construct and pcall(string.match,name,pattern) */
  203. lua_pushvalue( L, 3 );
  204. lua_pushstring( L, stat.name );
  205. lua_pushvalue( L, 1 );
  206. pcres = lua_pcall( L, 2, 1, 0 );
  207. if (pcres != 0) {
  208. vfs_closedir(dir);
  209. lua_error( L );
  210. }
  211. if (lua_isnil( L, -1 )) {
  212. continue;
  213. }
  214. }
  215. lua_pushinteger( L, stat.size );
  216. lua_setfield( L, 2, stat.name );
  217. }
  218. /* Shed everything back to Table */
  219. lua_settop( L, 2 );
  220. vfs_closedir(dir);
  221. return 1;
  222. }
  223. static int get_file_obj( lua_State *L, int *argpos )
  224. {
  225. if (lua_type( L, 1 ) == LUA_TUSERDATA) {
  226. file_fd_ud *ud = (file_fd_ud *)luaL_checkudata(L, 1, "file.obj");
  227. *argpos = 2;
  228. return ud->fd;
  229. } else {
  230. *argpos = 1;
  231. return file_fd;
  232. }
  233. }
  234. #define GET_FILE_OBJ int argpos; \
  235. int fd = get_file_obj( L, &argpos );
  236. static int file_seek (lua_State *L)
  237. {
  238. GET_FILE_OBJ;
  239. static const int mode[] = {VFS_SEEK_SET, VFS_SEEK_CUR, VFS_SEEK_END};
  240. static const char *const modenames[] = {"set", "cur", "end", NULL};
  241. if(!fd)
  242. return luaL_error(L, "open a file first");
  243. int op = luaL_checkoption(L, argpos, "cur", modenames);
  244. long offset = luaL_optlong(L, ++argpos, 0);
  245. op = vfs_lseek(fd, offset, mode[op]);
  246. if (op < 0)
  247. lua_pushnil(L); /* error */
  248. else
  249. lua_pushinteger(L, vfs_tell(fd));
  250. return 1;
  251. }
  252. // Lua: exists(filename)
  253. static int file_exists( lua_State* L )
  254. {
  255. size_t len;
  256. const char *fname = luaL_checklstring( L, 1, &len );
  257. const char *basename = vfs_basename( fname );
  258. luaL_argcheck(L, strlen(basename) <= FS_OBJ_NAME_LEN && strlen(fname) == len, 1, "filename invalid");
  259. struct vfs_stat stat;
  260. lua_pushboolean(L, vfs_stat((char *)fname, &stat) == VFS_RES_OK ? 1 : 0);
  261. return 1;
  262. }
  263. // Lua: remove(filename)
  264. static int file_remove( lua_State* L )
  265. {
  266. size_t len;
  267. const char *fname = luaL_checklstring( L, 1, &len );
  268. const char *basename = vfs_basename( fname );
  269. luaL_argcheck(L, strlen(basename) <= FS_OBJ_NAME_LEN && strlen(fname) == len, 1, "filename invalid");
  270. vfs_remove((char *)fname);
  271. return 0;
  272. }
  273. // Lua: flush()
  274. static int file_flush( lua_State* L )
  275. {
  276. GET_FILE_OBJ;
  277. if(!fd)
  278. return luaL_error(L, "open a file first");
  279. if(vfs_flush(fd) == 0)
  280. lua_pushboolean(L, 1);
  281. else
  282. lua_pushnil(L);
  283. return 1;
  284. }
  285. // Lua: rename("oldname", "newname")
  286. static int file_rename( lua_State* L )
  287. {
  288. size_t len;
  289. const char *oldname = luaL_checklstring( L, 1, &len );
  290. const char *basename = vfs_basename( oldname );
  291. luaL_argcheck(L, strlen(basename) <= FS_OBJ_NAME_LEN && strlen(oldname) == len, 1, "filename invalid");
  292. const char *newname = luaL_checklstring( L, 2, &len );
  293. basename = vfs_basename( newname );
  294. luaL_argcheck(L, strlen(basename) <= FS_OBJ_NAME_LEN && strlen(newname) == len, 2, "filename invalid");
  295. if(0 <= vfs_rename( oldname, newname )){
  296. lua_pushboolean(L, 1);
  297. } else {
  298. lua_pushboolean(L, 0);
  299. }
  300. return 1;
  301. }
  302. // Lua: stat(filename)
  303. static int file_stat( lua_State* L )
  304. {
  305. size_t len;
  306. const char *fname = luaL_checklstring( L, 1, &len );
  307. luaL_argcheck( L, strlen(fname) <= FS_OBJ_NAME_LEN && strlen(fname) == len, 1, "filename invalid" );
  308. struct vfs_stat stat;
  309. if (vfs_stat( (char *)fname, &stat ) != VFS_RES_OK) {
  310. lua_pushnil( L );
  311. return 1;
  312. }
  313. lua_createtable( L, 0, 7 );
  314. lua_pushinteger( L, stat.size );
  315. lua_setfield( L, -2, "size" );
  316. lua_pushstring( L, stat.name );
  317. lua_setfield( L, -2, "name" );
  318. lua_pushboolean( L, stat.is_dir );
  319. lua_setfield( L, -2, "is_dir" );
  320. lua_pushboolean( L, stat.is_rdonly );
  321. lua_setfield( L, -2, "is_rdonly" );
  322. lua_pushboolean( L, stat.is_hidden );
  323. lua_setfield( L, -2, "is_hidden" );
  324. lua_pushboolean( L, stat.is_sys );
  325. lua_setfield( L, -2, "is_sys" );
  326. lua_pushboolean( L, stat.is_arch );
  327. lua_setfield( L, -2, "is_arch" );
  328. // time stamp as sub-table
  329. lua_createtable( L, 0, 6 );
  330. lua_pushinteger( L, stat.tm_valid ? stat.tm.year : FILE_TIMEDEF_YEAR );
  331. lua_setfield( L, -2, "year" );
  332. lua_pushinteger( L, stat.tm_valid ? stat.tm.mon : FILE_TIMEDEF_MON );
  333. lua_setfield( L, -2, "mon" );
  334. lua_pushinteger( L, stat.tm_valid ? stat.tm.day : FILE_TIMEDEF_DAY );
  335. lua_setfield( L, -2, "day" );
  336. lua_pushinteger( L, stat.tm_valid ? stat.tm.hour : FILE_TIMEDEF_HOUR );
  337. lua_setfield( L, -2, "hour" );
  338. lua_pushinteger( L, stat.tm_valid ? stat.tm.min : FILE_TIMEDEF_MIN );
  339. lua_setfield( L, -2, "min" );
  340. lua_pushinteger( L, stat.tm_valid ? stat.tm.sec : FILE_TIMEDEF_SEC );
  341. lua_setfield( L, -2, "sec" );
  342. lua_setfield( L, -2, "time" );
  343. return 1;
  344. }
  345. // g_read()
  346. static int file_g_read( lua_State* L, int n, int16_t end_char, int fd )
  347. {
  348. int i, j;
  349. luaL_Buffer b;
  350. char p[LUAL_BUFFERSIZE/2];
  351. if(!fd)
  352. return luaL_error(L, "open a file first");
  353. luaL_buffinit(L, &b);
  354. for (j = 0; j < n; j += sizeof(p)) {
  355. int nwanted = (n - j >= sizeof(p)) ? sizeof(p) : n - j;
  356. int nread = vfs_read(fd, p, nwanted);
  357. if (nread == VFS_RES_ERR || nread == 0) {
  358. lua_pushnil(L);
  359. return 1;
  360. }
  361. for (i = 0; i < nread; ++i) {
  362. luaL_addchar(&b, p[i]);
  363. if (p[i] == end_char) {
  364. vfs_lseek(fd, -nread + j + i + 1, VFS_SEEK_CUR); //reposition after end char found
  365. nread = 0; // force break on outer loop
  366. break;
  367. }
  368. }
  369. if (nread < nwanted)
  370. break;
  371. }
  372. luaL_pushresult(&b);
  373. return 1;
  374. }
  375. // Lua: read()
  376. // file.read() will read FILE _CHUNK bytes, or EOF is reached.
  377. // file.read(10) will read 10 byte from file, or EOF is reached.
  378. // file.read('q') will read until 'q' or EOF is reached.
  379. static int file_read( lua_State* L )
  380. {
  381. unsigned need_len = FILE_READ_CHUNK;
  382. int16_t end_char = EOF;
  383. size_t el;
  384. GET_FILE_OBJ;
  385. if( lua_type( L, argpos ) == LUA_TNUMBER )
  386. {
  387. need_len = ( unsigned )luaL_checkinteger( L, argpos );
  388. }
  389. else if(lua_isstring(L, argpos))
  390. {
  391. const char *end = luaL_checklstring( L, argpos, &el );
  392. if(el!=1){
  393. return luaL_error( L, "wrong arg range" );
  394. }
  395. end_char = (int16_t)end[0];
  396. }
  397. return file_g_read(L, need_len, end_char, fd);
  398. }
  399. // Lua: readline()
  400. static int file_readline( lua_State* L )
  401. {
  402. GET_FILE_OBJ;
  403. return file_g_read(L, FILE_READ_CHUNK, '\n', fd);
  404. }
  405. // Lua: getfile(filename)
  406. static int file_getfile( lua_State* L )
  407. {
  408. // Warning this code C calls other file_* routines to avoid duplication code. These
  409. // use Lua stack addressing of arguments, so this does Lua stack maniplation to
  410. // align these
  411. int ret_cnt = 0;
  412. lua_settop(L ,1);
  413. // Stack [1] = FD
  414. file_open(L);
  415. // Stack [1] = filename; [2] = FD or nil
  416. if (!lua_isnil(L, -1)) {
  417. lua_remove(L, 1); // dump filename, so [1] = FD
  418. file_fd_ud *ud = (file_fd_ud *)luaL_checkudata(L, 1, "file.obj");
  419. ret_cnt = file_g_read(L, LUAI_MAXINT32, EOF, ud->fd);
  420. // Stack [1] = FD; [2] = contents if ret_cnt = 1;
  421. file_close(L); // leaves Stack unchanged if [1] = FD
  422. lua_remove(L, 1); // Dump FD leaving contents as [1] / ToS
  423. }
  424. return ret_cnt;
  425. }
  426. // Lua: write("string")
  427. static int file_write( lua_State* L )
  428. {
  429. GET_FILE_OBJ;
  430. if(!fd)
  431. return luaL_error(L, "open a file first");
  432. size_t l, rl;
  433. const char *s = luaL_checklstring(L, argpos, &l);
  434. rl = vfs_write(fd, s, l);
  435. if(rl==l)
  436. lua_pushboolean(L, 1);
  437. else
  438. lua_pushnil(L);
  439. return 1;
  440. }
  441. // Lua: writeline("string")
  442. static int file_writeline( lua_State* L )
  443. {
  444. GET_FILE_OBJ;
  445. if(!fd)
  446. return luaL_error(L, "open a file first");
  447. size_t l, rl;
  448. const char *s = luaL_checklstring(L, argpos, &l);
  449. rl = vfs_write(fd, s, l);
  450. if(rl==l){
  451. rl = vfs_write(fd, "\n", 1);
  452. if(rl==1)
  453. lua_pushboolean(L, 1);
  454. else
  455. lua_pushnil(L);
  456. }
  457. else{
  458. lua_pushnil(L);
  459. }
  460. return 1;
  461. }
  462. // Lua: getfile(filename)
  463. static int file_putfile( lua_State* L )
  464. {
  465. // Warning this code C calls other file_* routines to avoid duplication code. These
  466. // use Lua stack addressing of arguments, so this does Lua stack maniplation to
  467. // align these
  468. int ret_cnt = 0;
  469. lua_settop(L, 2);
  470. lua_pushvalue(L, 2); //dup contents onto the ToS [3]
  471. lua_pushliteral(L, "w+");
  472. lua_replace(L, 2);
  473. // Stack [1] = filename; [2] "w+" [3] contents;
  474. file_open(L);
  475. // Stack [1] = filename; [2] "w+" [3] contents; [4] FD or nil
  476. if (!lua_isnil(L, -1)) {
  477. lua_remove(L, 2); //dump "w+" attribute literal
  478. lua_replace(L, 1);
  479. // Stack [1] = FD; [2] contents
  480. file_write(L);
  481. // Stack [1] = FD; [2] contents; [3] result status
  482. lua_remove(L, 2); //dump contents
  483. file_close(L);
  484. lua_remove(L, 1); // Dump FD leaving status as ToS
  485. }
  486. return 1;
  487. }
  488. // Lua: fsinfo()
  489. static int file_fsinfo( lua_State* L )
  490. {
  491. uint32_t total, used;
  492. if (vfs_fsinfo("", &total, &used)) {
  493. return luaL_error(L, "file system failed");
  494. }
  495. NODE_DBG("total: %d, used:%d\n", total, used);
  496. if(total>0x7FFFFFFF || used>0x7FFFFFFF || used > total)
  497. {
  498. return luaL_error(L, "file system error");
  499. }
  500. lua_pushinteger(L, total-used);
  501. lua_pushinteger(L, used);
  502. lua_pushinteger(L, total);
  503. return 3;
  504. }
  505. typedef struct {
  506. vfs_vol *vol;
  507. } volume_type;
  508. // Lua: vol = file.mount("/SD0")
  509. static int file_mount( lua_State *L )
  510. {
  511. const char *ldrv = luaL_checkstring( L, 1 );
  512. int num = luaL_optint( L, 2, -1 );
  513. volume_type *vol = (volume_type *)lua_newuserdata( L, sizeof( volume_type ) );
  514. if (vol->vol = vfs_mount( ldrv, num )) {
  515. /* set its metatable */
  516. luaL_getmetatable(L, "file.vol");
  517. lua_setmetatable(L, -2);
  518. return 1;
  519. } else {
  520. // remove created userdata
  521. lua_pop( L, 1 );
  522. return 0;
  523. }
  524. }
  525. // Lua: success = file.chdir("/SD0/")
  526. static int file_chdir( lua_State *L )
  527. {
  528. const char *path = luaL_checkstring( L, 1 );
  529. lua_pushboolean( L, 0 <= vfs_chdir( path ) );
  530. return 1;
  531. }
  532. static int file_vol_umount( lua_State *L )
  533. {
  534. volume_type *vol = luaL_checkudata( L, 1, "file.vol" );
  535. luaL_argcheck( L, vol, 1, "volume expected" );
  536. lua_pushboolean( L, 0 <= vfs_umount( vol->vol ) );
  537. // invalidate vfs descriptor, it has been free'd anyway
  538. vol->vol = NULL;
  539. return 1;
  540. }
  541. LROT_BEGIN(file_obj, NULL, LROT_MASK_GC_INDEX)
  542. LROT_FUNCENTRY( __gc, file_obj_free )
  543. LROT_TABENTRY( __index, file_obj )
  544. LROT_FUNCENTRY( close, file_close )
  545. LROT_FUNCENTRY( read, file_read )
  546. LROT_FUNCENTRY( readline, file_readline )
  547. LROT_FUNCENTRY( write, file_write )
  548. LROT_FUNCENTRY( writeline, file_writeline )
  549. LROT_FUNCENTRY( seek, file_seek )
  550. LROT_FUNCENTRY( flush, file_flush )
  551. LROT_END(file_obj, NULL, LROT_MASK_GC_INDEX)
  552. LROT_BEGIN(file_vol, NULL, LROT_MASK_INDEX)
  553. LROT_TABENTRY( __index, file_vol )
  554. LROT_FUNCENTRY( umount, file_vol_umount )
  555. LROT_END(file_vol, NULL, LROT_MASK_INDEX)
  556. // Module function map
  557. LROT_BEGIN(file, NULL, 0)
  558. LROT_FUNCENTRY( list, file_list )
  559. LROT_FUNCENTRY( open, file_open )
  560. LROT_FUNCENTRY( close, file_close )
  561. LROT_FUNCENTRY( write, file_write )
  562. LROT_FUNCENTRY( writeline, file_writeline )
  563. LROT_FUNCENTRY( read, file_read )
  564. LROT_FUNCENTRY( readline, file_readline )
  565. #ifdef BUILD_SPIFFS
  566. LROT_FUNCENTRY( format, file_format )
  567. LROT_FUNCENTRY( fscfg, file_fscfg )
  568. #endif
  569. LROT_FUNCENTRY( remove, file_remove )
  570. LROT_FUNCENTRY( seek, file_seek )
  571. LROT_FUNCENTRY( flush, file_flush )
  572. LROT_FUNCENTRY( rename, file_rename )
  573. LROT_FUNCENTRY( exists, file_exists )
  574. LROT_FUNCENTRY( getcontents, file_getfile )
  575. LROT_FUNCENTRY( putcontents, file_putfile )
  576. LROT_FUNCENTRY( fsinfo, file_fsinfo )
  577. LROT_FUNCENTRY( on, file_on )
  578. LROT_FUNCENTRY( stat, file_stat )
  579. #ifdef BUILD_FATFS
  580. LROT_FUNCENTRY( mount, file_mount )
  581. LROT_FUNCENTRY( chdir, file_chdir )
  582. #endif
  583. LROT_END(file, NULL, 0)
  584. int luaopen_file( lua_State *L ) {
  585. if (!vfs_mount("/FLASH", 0)) {
  586. // Failed to mount -- try reformat
  587. dbg_printf("Formatting file system. Please wait...\n");
  588. if (!vfs_format()) {
  589. NODE_ERR( "\n*** ERROR ***: unable to format. FS might be compromised.\n" );
  590. NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
  591. }
  592. }
  593. luaL_rometatable( L, "file.vol", LROT_TABLEREF(file_vol));
  594. luaL_rometatable( L, "file.obj", LROT_TABLEREF(file_obj));
  595. return 0;
  596. }
  597. NODEMCU_MODULE(FILE, "file", file, luaopen_file);