Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 6 files changed
  • 0 comments
  • 1 contributor
4  Makefile
@@ -2,9 +2,9 @@ MISPDIR=.
2 2
 include $(MISPDIR)/common.mak
3 3
 
4 4
 OBJECTS=crt0.o isr.o luainit.o main.o
5  
-OURLIBS=m mm yaffs2 glue lua
  5
+OURLIBS=m mm yaffs2 glue lua lfs
6 6
 
7  
-CFLAGS+=-I$(MISPDIR)/libm/include -I$(MISPDIR)/libmm/include -I$(MISPDIR)/libglue/include -I$(LUADIR)/src
  7
+CFLAGS+=-I$(MISPDIR)/libm/include -I$(MISPDIR)/libmm/include -I$(MISPDIR)/libglue/include -I$(LUADIR)/src -I$(MISPDIR)/liblfs/include
8 8
 
9 9
 all: misp.bin
10 10
 
22  liblfs/Makefile
... ...
@@ -0,0 +1,22 @@
  1
+MISPDIR=..
  2
+include $(MISPDIR)/common.mak
  3
+
  4
+CFLAGS+=-I$(MISPDIR)/liblfs/include -I$(YAFFSDIR)/direct -I$(YAFFSDIR) -DCONFIG_YAFFS_DIRECT -DCONFIG_YAFFS_DEFINES_TYPES -DCONFIG_YAFFS_PROVIDE_DEFS -DCONFIG_YAFFSFS_PROVIDE_VALUES -I$(LUADIR)/src
  5
+OBJECTS=lfs.o
  6
+
  7
+all: liblfs.a
  8
+
  9
+# pull in dependency info for *existing* .o files
  10
+-include $(OBJECTS:.o=.d)
  11
+
  12
+liblfs.a: $(OBJECTS)
  13
+	$(AR) clr liblfs.a $(OBJECTS)
  14
+	$(RANLIB) liblfs.a
  15
+
  16
+%.o: %.c
  17
+	$(compile-dep)
  18
+
  19
+.PHONY: clean
  20
+
  21
+clean:
  22
+	rm -f $(OBJECTS) $(OBJECTS:.o=.d) liblfs.a .*~ *~
14  liblfs/include/lfs.h
... ...
@@ -0,0 +1,14 @@
  1
+/*
  2
+** LuaFileSystem
  3
+** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem)
  4
+** Adapted for MISP - Sebastien Bourdeauducq, 2012
  5
+**
  6
+** $Id: lfs.h,v 1.5 2008/02/19 20:08:23 mascarenhas Exp $
  7
+*/
  8
+
  9
+#ifndef __LFS_H
  10
+#define __LFS_H
  11
+
  12
+int luaopen_lfs(lua_State *L);
  13
+
  14
+#endif /* __LFS_H */
424  liblfs/lfs.c
... ...
@@ -0,0 +1,424 @@
  1
+/*
  2
+** LuaFileSystem
  3
+** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem)
  4
+** Adapted for MISP - Sebastien Bourdeauducq, 2012
  5
+**
  6
+** File system manipulation library.
  7
+** This library offers these functions:
  8
+**   lfs.attributes (filepath [, attributename])
  9
+**   lfs.dir (path)
  10
+**   lfs.mkdir (path)
  11
+**   lfs.rmdir (path)
  12
+**   lfs.symlinkattributes (filepath [, attributename]) -- thanks to Sam Roberts
  13
+**
  14
+** $Id: lfs.c,v 1.61 2009/07/04 02:10:16 mascarenhas Exp $
  15
+*/
  16
+
  17
+#include <errno.h>
  18
+#include <stdio.h>
  19
+#include <string.h>
  20
+#include <stdlib.h>
  21
+#include <yaffsfs.h>
  22
+
  23
+#include <lua.h>
  24
+#include <lauxlib.h>
  25
+#include <lualib.h>
  26
+#include <lfs.h>
  27
+
  28
+#define S_IRWXU 00700
  29
+#define S_IRUSR 00400
  30
+#define S_IWUSR 00200
  31
+#define S_IXUSR 00100
  32
+
  33
+#define S_IRWXG 00070
  34
+#define S_IRGRP 00040
  35
+#define S_IWGRP 00020
  36
+#define S_IXGRP 00010
  37
+
  38
+#define S_IRWXO 00007
  39
+#define S_IROTH 00004
  40
+#define S_IWOTH 00002
  41
+#define S_IXOTH 00001
  42
+
  43
+/*
  44
+ * ** compatibility with Lua 5.2
  45
+ * */
  46
+#if (LUA_VERSION_NUM == 502)
  47
+#undef luaL_register
  48
+#define luaL_register(L,n,f) \
  49
+	        { if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); }
  50
+
  51
+#endif
  52
+
  53
+#define DIR_METATABLE "directory metatable"
  54
+typedef struct dir_data {
  55
+	int  closed;
  56
+	yaffs_DIR *dir;
  57
+} dir_data;
  58
+
  59
+/*
  60
+** Utility functions
  61
+*/
  62
+static int pusherror(lua_State *L, const char *info)
  63
+{
  64
+	lua_pushnil(L);
  65
+	if (info==NULL)
  66
+		lua_pushstring(L, strerror(errno));
  67
+	else
  68
+		lua_pushfstring(L, "%s: %s", info, strerror(errno));
  69
+	lua_pushinteger(L, errno);
  70
+	return 3;
  71
+}
  72
+
  73
+static int pushresult(lua_State *L, int i, const char *info)
  74
+{
  75
+	if (i==-1)
  76
+		return pusherror(L, info);
  77
+	lua_pushinteger(L, i);
  78
+	return 1;
  79
+}
  80
+
  81
+/*
  82
+** Creates a link.
  83
+** @param #1 Object to link to.
  84
+** @param #2 Name of link.
  85
+** @param #3 True if link is symbolic (optional).
  86
+*/
  87
+static int make_link(lua_State *L)
  88
+{
  89
+	const char *oldpath = luaL_checkstring(L, 1);
  90
+	const char *newpath = luaL_checkstring(L, 2);
  91
+	return pushresult(L,
  92
+		(lua_toboolean(L,3) ? yaffs_symlink : yaffs_link)(oldpath, newpath), NULL);
  93
+}
  94
+
  95
+
  96
+/*
  97
+** Creates a directory.
  98
+** @param #1 Directory path.
  99
+*/
  100
+static int make_dir (lua_State *L) {
  101
+	const char *path = luaL_checkstring (L, 1);
  102
+	int fail;
  103
+	fail =  yaffs_mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
  104
+	                     S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  105
+	if (fail) {
  106
+		lua_pushnil (L);
  107
+        lua_pushfstring (L, "%s", strerror(errno));
  108
+		return 2;
  109
+	}
  110
+	lua_pushboolean (L, 1);
  111
+	return 1;
  112
+}
  113
+
  114
+/*
  115
+** Removes a directory.
  116
+** @param #1 Directory path.
  117
+*/
  118
+static int remove_dir (lua_State *L) {
  119
+	const char *path = luaL_checkstring (L, 1);
  120
+	int fail;
  121
+
  122
+	fail = yaffs_rmdir (path);
  123
+
  124
+	if (fail) {
  125
+		lua_pushnil (L);
  126
+		lua_pushfstring (L, "%s", strerror(errno));
  127
+		return 2;
  128
+	}
  129
+	lua_pushboolean (L, 1);
  130
+	return 1;
  131
+}
  132
+
  133
+/*
  134
+** Directory iterator
  135
+*/
  136
+static int dir_iter (lua_State *L) {
  137
+	struct yaffs_dirent *entry;
  138
+	
  139
+	dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE);
  140
+	luaL_argcheck (L, d->closed == 0, 1, "closed directory");
  141
+
  142
+	if ((entry = yaffs_readdir (d->dir)) != NULL) {
  143
+		lua_pushstring (L, entry->d_name);
  144
+		return 1;
  145
+	} else {
  146
+		/* no more entries => close directory */
  147
+		yaffs_closedir (d->dir);
  148
+		d->closed = 1;
  149
+		return 0;
  150
+	}
  151
+}
  152
+
  153
+
  154
+/*
  155
+** Closes directory iterators
  156
+*/
  157
+static int dir_close (lua_State *L) {
  158
+	dir_data *d = (dir_data *)lua_touserdata (L, 1);
  159
+	
  160
+	if (!d->closed && d->dir) {
  161
+		yaffs_closedir (d->dir);
  162
+	}
  163
+
  164
+	d->closed = 1;
  165
+	return 0;
  166
+}
  167
+
  168
+
  169
+/*
  170
+** Factory of directory iterators
  171
+*/
  172
+static int dir_iter_factory (lua_State *L) {
  173
+	const char *path = luaL_checkstring (L, 1);
  174
+	dir_data *d;
  175
+	lua_pushcfunction (L, dir_iter);
  176
+	d = (dir_data *) lua_newuserdata (L, sizeof(dir_data));
  177
+	luaL_getmetatable (L, DIR_METATABLE);
  178
+	lua_setmetatable (L, -2);
  179
+	d->closed = 0;
  180
+	d->dir = yaffs_opendir (path);
  181
+	if (d->dir == NULL)
  182
+          luaL_error (L, "cannot open %s: %s", path, strerror (errno));
  183
+	return 2;
  184
+}
  185
+
  186
+
  187
+/*
  188
+** Creates directory metatable.
  189
+*/
  190
+static int dir_create_meta (lua_State *L) {
  191
+	luaL_newmetatable (L, DIR_METATABLE);
  192
+
  193
+        /* Method table */
  194
+	lua_newtable(L);
  195
+	lua_pushcfunction (L, dir_iter);
  196
+	lua_setfield(L, -2, "next");
  197
+	lua_pushcfunction (L, dir_close);
  198
+	lua_setfield(L, -2, "close");
  199
+
  200
+        /* Metamethods */
  201
+	lua_setfield(L, -2, "__index");
  202
+	lua_pushcfunction (L, dir_close);
  203
+	lua_setfield (L, -2, "__gc");
  204
+	return 1;
  205
+}
  206
+
  207
+
  208
+/*
  209
+** Convert the inode protection mode to a string.
  210
+*/
  211
+static const char *mode2string (mode_t mode) {
  212
+  if ( S_ISREG(mode) )
  213
+    return "file";
  214
+  else if ( S_ISDIR(mode) )
  215
+    return "directory";
  216
+  else if ( S_ISLNK(mode) )
  217
+	return "link";
  218
+  else if ( S_ISSOCK(mode) )
  219
+    return "socket";
  220
+  else if ( S_ISFIFO(mode) )
  221
+	return "named pipe";
  222
+  else if ( S_ISCHR(mode) )
  223
+	return "char device";
  224
+  else if ( S_ISBLK(mode) )
  225
+	return "block device";
  226
+  else
  227
+	return "other";
  228
+}
  229
+
  230
+
  231
+/* inode protection mode */
  232
+static void push_st_mode (lua_State *L, struct yaffs_stat *info) {
  233
+	lua_pushstring (L, mode2string (info->st_mode));
  234
+}
  235
+/* device inode resides on */
  236
+static void push_st_dev (lua_State *L, struct yaffs_stat *info) {
  237
+	lua_pushnumber (L, (lua_Number)info->st_dev);
  238
+}
  239
+/* inode's number */
  240
+static void push_st_ino (lua_State *L, struct yaffs_stat *info) {
  241
+	lua_pushnumber (L, (lua_Number)info->st_ino);
  242
+}
  243
+/* number of hard links to the file */
  244
+static void push_st_nlink (lua_State *L, struct yaffs_stat *info) {
  245
+	lua_pushnumber (L, (lua_Number)info->st_nlink);
  246
+}
  247
+/* user-id of owner */
  248
+static void push_st_uid (lua_State *L, struct yaffs_stat *info) {
  249
+	lua_pushnumber (L, (lua_Number)info->st_uid);
  250
+}
  251
+/* group-id of owner */
  252
+static void push_st_gid (lua_State *L, struct yaffs_stat *info) {
  253
+	lua_pushnumber (L, (lua_Number)info->st_gid);
  254
+}
  255
+/* device type, for special file inode */
  256
+static void push_st_rdev (lua_State *L, struct yaffs_stat *info) {
  257
+	lua_pushnumber (L, (lua_Number)info->st_rdev);
  258
+}
  259
+/* time of last access */
  260
+static void push_st_atime (lua_State *L, struct yaffs_stat *info) {
  261
+	lua_pushnumber (L, info->yst_atime);
  262
+}
  263
+/* time of last data modification */
  264
+static void push_st_mtime (lua_State *L, struct yaffs_stat *info) {
  265
+	lua_pushnumber (L, info->yst_mtime);
  266
+}
  267
+/* time of last file status change */
  268
+static void push_st_ctime (lua_State *L, struct yaffs_stat *info) {
  269
+	lua_pushnumber (L, info->yst_ctime);
  270
+}
  271
+/* file size, in bytes */
  272
+static void push_st_size (lua_State *L, struct yaffs_stat *info) {
  273
+	lua_pushnumber (L, (lua_Number)info->st_size);
  274
+}
  275
+
  276
+/* blocks allocated for file */
  277
+static void push_st_blocks (lua_State *L, struct yaffs_stat *info) {
  278
+	lua_pushnumber (L, (lua_Number)info->st_blocks);
  279
+}
  280
+/* optimal file system I/O blocksize */
  281
+static void push_st_blksize (lua_State *L, struct yaffs_stat *info) {
  282
+	lua_pushnumber (L, (lua_Number)info->st_blksize);
  283
+}
  284
+static void push_invalid (lua_State *L, struct yaffs_stat *info) {
  285
+  luaL_error(L, "invalid attribute name");
  286
+  info->st_blksize = 0; /* never reached */
  287
+}
  288
+
  289
+ /*
  290
+** Convert the inode protection mode to a permission list.
  291
+*/
  292
+static const char *perm2string (mode_t mode) {
  293
+  static char perms[10] = "---------\0";
  294
+  int i;
  295
+  for (i=0;i<9;i++) perms[i]='-';
  296
+  if (mode & S_IRUSR) perms[0] = 'r';
  297
+  if (mode & S_IWUSR) perms[1] = 'w';
  298
+  if (mode & S_IXUSR) perms[2] = 'x';
  299
+  if (mode & S_IRGRP) perms[3] = 'r';
  300
+  if (mode & S_IWGRP) perms[4] = 'w';
  301
+  if (mode & S_IXGRP) perms[5] = 'x';
  302
+  if (mode & S_IROTH) perms[6] = 'r';
  303
+  if (mode & S_IWOTH) perms[7] = 'w';
  304
+  if (mode & S_IXOTH) perms[8] = 'x';
  305
+  return perms;
  306
+}
  307
+
  308
+/* permssions string */
  309
+static void push_st_perm (lua_State *L, struct yaffs_stat *info) {
  310
+    lua_pushstring (L, perm2string (info->st_mode));
  311
+}
  312
+
  313
+typedef void (*_push_function) (lua_State *L, struct yaffs_stat *info);
  314
+
  315
+struct _stat_members {
  316
+	const char *name;
  317
+	_push_function push;
  318
+};
  319
+
  320
+struct _stat_members members[] = {
  321
+	{ "mode",         push_st_mode },
  322
+	{ "dev",          push_st_dev },
  323
+	{ "ino",          push_st_ino },
  324
+	{ "nlink",        push_st_nlink },
  325
+	{ "uid",          push_st_uid },
  326
+	{ "gid",          push_st_gid },
  327
+	{ "rdev",         push_st_rdev },
  328
+	{ "access",       push_st_atime },
  329
+	{ "modification", push_st_mtime },
  330
+	{ "change",       push_st_ctime },
  331
+	{ "size",         push_st_size },
  332
+ 	{ "permissions",  push_st_perm },
  333
+	{ "blocks",       push_st_blocks },
  334
+	{ "blksize",      push_st_blksize },
  335
+	{ NULL, push_invalid }
  336
+};
  337
+
  338
+/*
  339
+** Get file or symbolic link information
  340
+*/
  341
+static int _file_info_ (lua_State *L, int (*st)(const char*, struct yaffs_stat*)) {
  342
+	int i;
  343
+	struct yaffs_stat info;
  344
+	const char *file = luaL_checkstring (L, 1);
  345
+
  346
+	if (st(file, &info)) {
  347
+		lua_pushnil (L);
  348
+		lua_pushfstring (L, "cannot obtain information from file `%s'", file);
  349
+		return 2;
  350
+	}
  351
+	if (lua_isstring (L, 2)) {
  352
+		int v;
  353
+		const char *member = lua_tostring (L, 2);
  354
+		if (strcmp (member, "mode") == 0) v = 0;
  355
+		else if (strcmp (member, "blocks")  == 0) v = 11;
  356
+		else if (strcmp (member, "blksize") == 0) v = 12;
  357
+		else /* look for member */
  358
+			for (v = 1; members[v].name; v++)
  359
+				if (*members[v].name == *member)
  360
+					break;
  361
+		/* push member value and return */
  362
+		members[v].push (L, &info);
  363
+		return 1;
  364
+	} else if (!lua_istable (L, 2))
  365
+		/* creates a table if none is given */
  366
+		lua_newtable (L);
  367
+	/* stores all members in table on top of the stack */
  368
+	for (i = 0; members[i].name; i++) {
  369
+		lua_pushstring (L, members[i].name);
  370
+		members[i].push (L, &info);
  371
+		lua_rawset (L, -3);
  372
+	}
  373
+	return 1;
  374
+}
  375
+
  376
+
  377
+/*
  378
+** Get file information using stat.
  379
+*/
  380
+static int file_info (lua_State *L) {
  381
+	return _file_info_ (L, yaffs_stat);
  382
+}
  383
+
  384
+
  385
+/*
  386
+** Get symbolic link information using lstat.
  387
+*/
  388
+static int link_info (lua_State *L) {
  389
+	return _file_info_ (L, yaffs_lstat);
  390
+}
  391
+
  392
+
  393
+/*
  394
+** Assumes the table is on top of the stack.
  395
+*/
  396
+static void set_info (lua_State *L) {
  397
+	lua_pushliteral (L, "_COPYRIGHT");
  398
+	lua_pushliteral (L, "Copyright (C) 2003-2009 Kepler Project");
  399
+	lua_settable (L, -3);
  400
+	lua_pushliteral (L, "_DESCRIPTION");
  401
+	lua_pushliteral (L, "LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution");
  402
+	lua_settable (L, -3);
  403
+	lua_pushliteral (L, "_VERSION");
  404
+	lua_pushliteral (L, "LuaFileSystem 1.5.0-misp");
  405
+	lua_settable (L, -3);
  406
+}
  407
+
  408
+
  409
+static const struct luaL_Reg fslib[] = {
  410
+	{"attributes", file_info},
  411
+	{"dir", dir_iter_factory},
  412
+        {"link", make_link},
  413
+	{"mkdir", make_dir},
  414
+	{"rmdir", remove_dir},
  415
+	{"symlinkattributes", link_info},
  416
+	{NULL, NULL},
  417
+};
  418
+
  419
+int luaopen_lfs (lua_State *L) {
  420
+	dir_create_meta (L);
  421
+	luaL_register (L, "lfs", fslib);
  422
+	set_info (L);
  423
+	return 1;
  424
+}
20  luainit.c
... ...
@@ -1,19 +1,14 @@
1 1
 /*
2  
-* Based on Lua 5.2.0 linit.c, see Copyright Notice in lua.h
3  
-*/
4  
-
5  
-
6  
-#define linit_c
7  
-#define LUA_LIB
8  
-
9  
-#include "lua.h"
  2
+ * Based on Lua 5.2.0 linit.c, see Copyright Notice in lua.h
  3
+ */
10 4
 
11  
-#include "lualib.h"
12  
-#include "lauxlib.h"
  5
+#include <lua.h>
  6
+#include <lualib.h>
  7
+#include <lauxlib.h>
  8
+#include <lfs.h>
13 9
 
14 10
 /*
15  
- * these libs are loaded by lua.c and are readily available to any Lua
16  
- * program
  11
+ * these libs are readily available to any Lua program
17 12
  */
18 13
 static const luaL_Reg loadedlibs[] = {
19 14
 	{"_G", luaopen_base},
@@ -25,6 +20,7 @@ static const luaL_Reg loadedlibs[] = {
25 20
 	{LUA_BITLIBNAME, luaopen_bit32},
26 21
 	{LUA_MATHLIBNAME, luaopen_math},
27 22
 	{LUA_DBLIBNAME, luaopen_debug},
  23
+	{"lfs", luaopen_lfs},
28 24
 	{NULL, NULL}
29 25
 };
30 26
 
19  main.c
@@ -18,6 +18,25 @@ static void test_lua(void)
18 18
 	L = luaL_newstate();
19 19
 	luaL_openlibs(L);
20 20
 	luaL_dostring(L, "for i=10,1,-1 do print(i) end\n");
  21
+	luaL_dostring(L, "function attrdir (path)\n"
  22
+"    for file in lfs.dir(path) do\n"
  23
+"        if file ~= \".\" and file ~= \"..\" then\n"
  24
+"            local f = path..'/'..file\n"
  25
+"            print (\"\t \"..f)\n"
  26
+"            local attr = lfs.attributes (f)\n"
  27
+"            assert (type(attr) == \"table\")\n"
  28
+"            if attr.mode == \"directory\" then\n"
  29
+"                attrdir (f)\n"
  30
+"            else\n"
  31
+"                for name, value in pairs(attr) do\n"
  32
+"                    print (name, value)\n"
  33
+"                end\n"
  34
+"            end\n"
  35
+"        end\n"
  36
+"    end\n"
  37
+"end\n"
  38
+"\n"
  39
+"attrdir (\"/\")\n");
21 40
 	lua_close(L);
22 41
 }
23 42
 

No commit comments for this range

Something went wrong with that request. Please try again.