2 ** Code implementing read only functionality copied from
3 ** src/lfs.c at commit 2fd989cd6c777583be1c93616018c55b2cbb1bcf:
6 ** Copyright 2003-2014 Kepler Project
7 ** http://www.keplerproject.org/luafilesystem
9 ** File system manipulation library.
10 ** This library offers these functions:
11 ** lfs.attributes (filepath [, attributename])
16 ** $Id: lfs.c,v 1.61 2009/07/04 02:10:16 mascarenhas Exp $
24 #include <sys/types.h>
31 #define chdir_error strerror(errno)
33 /* Size of path buffer string, stolen from pwd.c */
36 # define PATH_MAX NAME_MAX
38 # define PATH_MAX FILENAME_MAX
41 # endif /* NAME_MAX */
45 #define DIR_METATABLE "directory metatable"
46 typedef struct dir_data
{
52 #define STAT_STRUCT struct stat
53 #define STAT_FUNC stat_via_fstat
55 /* Emulate stat via fstat */
56 int stat_via_fstat (const char *path
, struct stat
*buf
)
58 int fd
= open (path
, O_RDONLY
);
60 DIR *dir
= opendir (path
);
67 if (fstat (fd
, buf
) == -1) {
78 ** This function changes the working (current) directory
80 static int change_dir (lua_State
*L
) {
81 const char *path
= luaL_checkstring(L
, 1);
84 lua_pushfstring (L
,"Unable to change working directory to '%s'\n%s\n",
88 lua_pushboolean (L
, 1);
95 ** This function returns the current directory
96 ** If unable to get the current directory, it returns nil
97 ** and a string describing the error
99 static int get_dir (lua_State
*L
) {
101 /* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */
103 if ((path
= getcwd(buf
, PATH_MAX
)) == NULL
) {
105 lua_pushstring(L
, strerror(errno
));
109 lua_pushstring(L
, path
);
116 ** Directory iterator
118 static int dir_iter (lua_State
*L
) {
119 struct dirent
*entry
;
120 dir_data
*d
= (dir_data
*)luaL_checkudata (L
, 1, DIR_METATABLE
);
121 luaL_argcheck (L
, d
->closed
== 0, 1, "closed directory");
122 if ((entry
= readdir (d
->dir
)) != NULL
) {
123 lua_pushstring (L
, entry
->d_name
);
126 /* no more entries => close directory */
135 ** Closes directory iterators
137 static int dir_close (lua_State
*L
) {
138 dir_data
*d
= (dir_data
*)lua_touserdata (L
, 1);
139 if (!d
->closed
&& d
->dir
) {
148 ** Factory of directory iterators
150 static int dir_iter_factory (lua_State
*L
) {
151 const char *path
= luaL_checkstring (L
, 1);
153 lua_pushcfunction (L
, dir_iter
);
154 d
= (dir_data
*) lua_newuserdata (L
, sizeof(dir_data
));
155 luaL_getmetatable (L
, DIR_METATABLE
);
156 lua_setmetatable (L
, -2);
158 d
->dir
= opendir (path
);
160 luaL_error (L
, "cannot open %s: %s", path
, strerror (errno
));
166 ** Creates directory metatable.
168 static int dir_create_meta (lua_State
*L
) {
169 luaL_newmetatable (L
, DIR_METATABLE
);
173 lua_pushcfunction (L
, dir_iter
);
174 lua_setfield(L
, -2, "next");
175 lua_pushcfunction (L
, dir_close
);
176 lua_setfield(L
, -2, "close");
179 lua_setfield(L
, -2, "__index");
180 lua_pushcfunction (L
, dir_close
);
181 lua_setfield (L
, -2, "__gc");
187 ** Convert the inode protection mode to a string.
189 static const char *mode2string (mode_t mode
) {
192 else if ( S_ISDIR(mode
) )
194 else if ( S_ISLNK(mode
) )
196 else if ( S_ISSOCK(mode
) )
198 else if ( S_ISFIFO(mode
) )
200 else if ( S_ISCHR(mode
) )
201 return "char device";
202 else if ( S_ISBLK(mode
) )
203 return "block device";
209 /* inode protection mode */
210 static void push_st_mode (lua_State
*L
, STAT_STRUCT
*info
) {
211 lua_pushstring (L
, mode2string (info
->st_mode
));
213 /* file size, in bytes */
214 static void push_st_size (lua_State
*L
, STAT_STRUCT
*info
) {
215 lua_pushnumber (L
, (lua_Number
)info
->st_size
);
217 static void push_invalid (lua_State
*L
, STAT_STRUCT
*info
) {
218 luaL_error(L
, "invalid attribute name");
219 info
->st_size
= 0; /* never reached */
222 typedef void (*_push_function
) (lua_State
*L
, STAT_STRUCT
*info
);
224 struct _stat_members
{
229 struct _stat_members members
[] = {
230 { "mode", push_st_mode
},
231 { "size", push_st_size
},
232 { NULL
, push_invalid
}
236 ** Get file or symbolic link information
238 static int _file_info_ (lua_State
*L
, int (*st
)(const char*, STAT_STRUCT
*)) {
241 const char *file
= luaL_checkstring (L
, 1);
243 if (st(file
, &info
)) {
245 lua_pushfstring (L
, "cannot obtain information from file `%s'", file
);
248 if (lua_isstring (L
, 2)) {
250 const char *member
= lua_tostring (L
, 2);
251 if (strcmp (member
, "mode") == 0) v
= 0;
253 else if (strcmp (member
, "blocks") == 0) v
= 11;
254 else if (strcmp (member
, "blksize") == 0) v
= 12;
256 else /* look for member */
257 for (v
= 1; members
[v
].name
; v
++)
258 if (*members
[v
].name
== *member
)
260 /* push member value and return */
261 members
[v
].push (L
, &info
);
263 } else if (!lua_istable (L
, 2))
264 /* creates a table if none is given */
266 /* stores all members in table on top of the stack */
267 for (i
= 0; members
[i
].name
; i
++) {
268 lua_pushstring (L
, members
[i
].name
);
269 members
[i
].push (L
, &info
);
277 ** Get file information using stat.
279 static int file_info (lua_State
*L
) {
280 return _file_info_ (L
, STAT_FUNC
);
284 static const struct luaL_Reg fslib
[] = {
285 {"attributes", file_info
},
286 {"chdir", change_dir
},
287 {"currentdir", get_dir
},
288 {"dir", dir_iter_factory
},
292 LUALIB_API
int luaopen_lfs (lua_State
*L
) {
294 luaL_newlib (L
, fslib
);