aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2020-04-21 18:58:59 -0300
committerHisham Muhammad <hisham@gobolinux.org>2020-04-22 11:30:28 -0300
commit3e879ffba1b66ede11a038ba46d55475a51e538b (patch)
tree5865a94b42e5cdde249fd7242500453ef87e26c4
parent4f7e1b5a49575c77657b77bea383f79cddd768e3 (diff)
downloadluafilesystem-3e879ffba1b66ede11a038ba46d55475a51e538b.tar.gz
luafilesystem-3e879ffba1b66ede11a038ba46d55475a51e538b.tar.bz2
luafilesystem-3e879ffba1b66ede11a038ba46d55475a51e538b.zip
indent -kr -nut -i2 src/lfs.c src/lfs.h
-rw-r--r--src/lfs.c1218
-rw-r--r--src/lfs.h27
2 files changed, 690 insertions, 555 deletions
diff --git a/src/lfs.c b/src/lfs.c
index cc9d1cd..17700fa 100644
--- a/src/lfs.c
+++ b/src/lfs.c
@@ -1,6 +1,7 @@
1/* 1/*
2** LuaFileSystem 2** LuaFileSystem
3** Copyright Kepler Project 2003 - 2017 (http://keplerproject.github.io/luafilesystem) 3** Copyright Kepler Project 2003 - 2020
4** (http://keplerproject.github.io/luafilesystem)
4** 5**
5** File system manipulation library. 6** File system manipulation library.
6** This library offers these functions: 7** This library offers these functions:
@@ -22,15 +23,15 @@
22#ifndef LFS_DO_NOT_USE_LARGE_FILE 23#ifndef LFS_DO_NOT_USE_LARGE_FILE
23#ifndef _WIN32 24#ifndef _WIN32
24#ifndef _AIX 25#ifndef _AIX
25#define _FILE_OFFSET_BITS 64 /* Linux, Solaris and HP-UX */ 26#define _FILE_OFFSET_BITS 64 /* Linux, Solaris and HP-UX */
26#else 27#else
27#define _LARGE_FILES 1 /* AIX */ 28#define _LARGE_FILES 1 /* AIX */
28#endif 29#endif
29#endif 30#endif
30#endif 31#endif
31 32
32#ifdef _WIN32 33#ifdef _WIN32
33 #define _WIN32_WINNT 0x600 34#define _WIN32_WINNT 0x600
34#endif 35#endif
35 36
36#ifndef LFS_DO_NOT_USE_LARGE_FILE 37#ifndef LFS_DO_NOT_USE_LARGE_FILE
@@ -45,31 +46,39 @@
45#include <sys/stat.h> 46#include <sys/stat.h>
46 47
47#ifdef _WIN32 48#ifdef _WIN32
48 #include <direct.h> 49
49 #include <windows.h> 50#include <direct.h>
50 #include <io.h> 51#include <windows.h>
51 #include <sys/locking.h> 52#include <io.h>
52 #ifdef __BORLANDC__ 53#include <sys/locking.h>
53 #include <utime.h> 54
54 #else 55#ifdef __BORLANDC__
55 #include <sys/utime.h> 56#include <utime.h>
56 #endif 57#else
57 #include <fcntl.h> 58#include <sys/utime.h>
58 /* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */ 59#endif
59 #define LFS_MAXPATHLEN MAX_PATH 60
61#include <fcntl.h>
62
63/* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */
64#define LFS_MAXPATHLEN MAX_PATH
65
66#else
67
68#include <unistd.h>
69#include <dirent.h>
70#include <fcntl.h>
71#include <sys/types.h>
72#include <utime.h>
73#include <sys/param.h> /* for MAXPATHLEN */
74
75#ifdef MAXPATHLEN
76#define LFS_MAXPATHLEN MAXPATHLEN
60#else 77#else
61 #include <unistd.h> 78#include <limits.h> /* for _POSIX_PATH_MAX */
62 #include <dirent.h> 79#define LFS_MAXPATHLEN _POSIX_PATH_MAX
63 #include <fcntl.h> 80#endif
64 #include <sys/types.h> 81
65 #include <utime.h>
66 #include <sys/param.h> /* for MAXPATHLEN */
67 #ifdef MAXPATHLEN
68 #define LFS_MAXPATHLEN MAXPATHLEN
69 #else
70 #include <limits.h> /* for _POSIX_PATH_MAX */
71 #define LFS_MAXPATHLEN _POSIX_PATH_MAX
72 #endif
73#endif 82#endif
74 83
75#include <lua.h> 84#include <lua.h>
@@ -81,7 +90,7 @@
81#define LFS_VERSION "1.7.0" 90#define LFS_VERSION "1.7.0"
82#define LFS_LIBNAME "lfs" 91#define LFS_LIBNAME "lfs"
83 92
84#if LUA_VERSION_NUM >= 503 /* Lua 5.3+ */ 93#if LUA_VERSION_NUM >= 503 /* Lua 5.3+ */
85 94
86#ifndef luaL_optlong 95#ifndef luaL_optlong
87#define luaL_optlong luaL_optinteger 96#define luaL_optlong luaL_optinteger
@@ -90,9 +99,9 @@
90#endif 99#endif
91 100
92#if LUA_VERSION_NUM >= 502 101#if LUA_VERSION_NUM >= 502
93# define new_lib(L, l) (luaL_newlib(L, l)) 102#define new_lib(L, l) (luaL_newlib(L, l))
94#else 103#else
95# define new_lib(L, l) (lua_newtable(L), luaL_register(L, NULL, l)) 104#define new_lib(L, l) (lua_newtable(L), luaL_register(L, NULL, l))
96#endif 105#endif
97 106
98/* Define 'strerror' for systems that do not implement it */ 107/* Define 'strerror' for systems that do not implement it */
@@ -102,80 +111,85 @@
102 111
103#define DIR_METATABLE "directory metatable" 112#define DIR_METATABLE "directory metatable"
104typedef struct dir_data { 113typedef struct dir_data {
105 int closed; 114 int closed;
106#ifdef _WIN32 115#ifdef _WIN32
107 intptr_t hFile; 116 intptr_t hFile;
108 char pattern[MAX_PATH+1]; 117 char pattern[MAX_PATH + 1];
109#else 118#else
110 DIR *dir; 119 DIR *dir;
111#endif 120#endif
112} dir_data; 121} dir_data;
113 122
114#define LOCK_METATABLE "lock metatable" 123#define LOCK_METATABLE "lock metatable"
115 124
116#ifdef _WIN32 125#ifdef _WIN32
117 #ifdef __BORLANDC__ 126
118 #define lfs_setmode(file, m) (setmode(_fileno(file), m)) 127#ifdef __BORLANDC__
119 #define STAT_STRUCT struct stati64 128#define lfs_setmode(file, m) (setmode(_fileno(file), m))
120 #else 129#define STAT_STRUCT struct stati64
121 #define lfs_setmode(file, m) (_setmode(_fileno(file), m))
122 #define STAT_STRUCT struct _stati64
123 #endif
124 #ifndef _S_IFLNK
125 #define _S_IFLNK 0x400
126 #endif
127
128 #ifndef S_ISDIR
129 #define S_ISDIR(mode) (mode&_S_IFDIR)
130 #endif
131 #ifndef S_ISREG
132 #define S_ISREG(mode) (mode&_S_IFREG)
133 #endif
134 #ifndef S_ISLNK
135 #define S_ISLNK(mode) (mode&_S_IFLNK)
136 #endif
137 #ifndef S_ISSOCK
138 #define S_ISSOCK(mode) (0)
139 #endif
140 #ifndef S_ISFIFO
141 #define S_ISFIFO(mode) (0)
142 #endif
143 #ifndef S_ISCHR
144 #define S_ISCHR(mode) (mode&_S_IFCHR)
145 #endif
146 #ifndef S_ISBLK
147 #define S_ISBLK(mode) (0)
148 #endif
149
150 #define STAT_FUNC _stati64
151 #define LSTAT_FUNC lfs_win32_lstat
152#else 130#else
153 #define _O_TEXT 0 131#define lfs_setmode(file, m) (_setmode(_fileno(file), m))
154 #define _O_BINARY 0 132#define STAT_STRUCT struct _stati64
155 #define lfs_setmode(file, m) ((void)file, (void)m, 0) 133#endif
156 #define STAT_STRUCT struct stat 134
157 #define STAT_FUNC stat 135#ifndef _S_IFLNK
158 #define LSTAT_FUNC lstat 136#define _S_IFLNK 0x400
137#endif
138
139#ifndef S_ISDIR
140#define S_ISDIR(mode) (mode&_S_IFDIR)
141#endif
142#ifndef S_ISREG
143#define S_ISREG(mode) (mode&_S_IFREG)
144#endif
145#ifndef S_ISLNK
146#define S_ISLNK(mode) (mode&_S_IFLNK)
147#endif
148#ifndef S_ISSOCK
149#define S_ISSOCK(mode) (0)
150#endif
151#ifndef S_ISFIFO
152#define S_ISFIFO(mode) (0)
153#endif
154#ifndef S_ISCHR
155#define S_ISCHR(mode) (mode&_S_IFCHR)
156#endif
157#ifndef S_ISBLK
158#define S_ISBLK(mode) (0)
159#endif
160
161#define STAT_FUNC _stati64
162#define LSTAT_FUNC lfs_win32_lstat
163
164#else
165
166#define _O_TEXT 0
167#define _O_BINARY 0
168#define lfs_setmode(file, m) ((void)file, (void)m, 0)
169#define STAT_STRUCT struct stat
170#define STAT_FUNC stat
171#define LSTAT_FUNC lstat
172
159#endif 173#endif
160 174
161#ifdef _WIN32 175#ifdef _WIN32
162 #define lfs_mkdir _mkdir 176#define lfs_mkdir _mkdir
163#else 177#else
164 #define lfs_mkdir(path) (mkdir((path), \ 178#define lfs_mkdir(path) (mkdir((path), \
165 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH)) 179 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH))
166#endif 180#endif
167 181
168#ifdef _WIN32 182#ifdef _WIN32
169 183
170int lfs_win32_pusherror(lua_State *L) 184int lfs_win32_pusherror(lua_State * L)
171{ 185{
172 int en = GetLastError(); 186 int en = GetLastError();
173 lua_pushnil(L); 187 lua_pushnil(L);
174 if(en == ERROR_FILE_EXISTS || en == ERROR_SHARING_VIOLATION) 188 if (en == ERROR_FILE_EXISTS || en == ERROR_SHARING_VIOLATION)
175 lua_pushstring(L, "File exists"); 189 lua_pushstring(L, "File exists");
176 else 190 else
177 lua_pushstring(L, strerror(en)); 191 lua_pushstring(L, strerror(en));
178 return 2; 192 return 2;
179} 193}
180 194
181#define TICKS_PER_SECOND 10000000 195#define TICKS_PER_SECOND 10000000
@@ -185,10 +199,10 @@ time_t windowsToUnixTime(FILETIME ft)
185 ULARGE_INTEGER uli; 199 ULARGE_INTEGER uli;
186 uli.LowPart = ft.dwLowDateTime; 200 uli.LowPart = ft.dwLowDateTime;
187 uli.HighPart = ft.dwHighDateTime; 201 uli.HighPart = ft.dwHighDateTime;
188 return (time_t)(uli.QuadPart / TICKS_PER_SECOND - EPOCH_DIFFERENCE); 202 return (time_t) (uli.QuadPart / TICKS_PER_SECOND - EPOCH_DIFFERENCE);
189} 203}
190 204
191int lfs_win32_lstat(const char *path, STAT_STRUCT *buffer) 205int lfs_win32_lstat(const char *path, STAT_STRUCT * buffer)
192{ 206{
193 WIN32_FILE_ATTRIBUTE_DATA win32buffer; 207 WIN32_FILE_ATTRIBUTE_DATA win32buffer;
194 if (GetFileAttributesEx(path, GetFileExInfoStandard, &win32buffer)) { 208 if (GetFileAttributesEx(path, GetFileExInfoStandard, &win32buffer)) {
@@ -211,23 +225,25 @@ int lfs_win32_lstat(const char *path, STAT_STRUCT *buffer)
211 return 1; 225 return 1;
212 } 226 }
213} 227}
228
214#endif 229#endif
215 230
216/* 231/*
217** Utility functions 232** Utility functions
218*/ 233*/
219static int pusherror(lua_State *L, const char *info) 234static int pusherror(lua_State * L, const char *info)
220{ 235{
221 lua_pushnil(L); 236 lua_pushnil(L);
222 if (info==NULL) 237 if (info == NULL)
223 lua_pushstring(L, strerror(errno)); 238 lua_pushstring(L, strerror(errno));
224 else 239 else
225 lua_pushfstring(L, "%s: %s", info, strerror(errno)); 240 lua_pushfstring(L, "%s: %s", info, strerror(errno));
226 lua_pushinteger(L, errno); 241 lua_pushinteger(L, errno);
227 return 3; 242 return 3;
228} 243}
229 244
230static int pushresult(lua_State *L, int res, const char *info) { 245static int pushresult(lua_State * L, int res, const char *info)
246{
231 if (res == -1) { 247 if (res == -1) {
232 return pusherror(L, info); 248 return pusherror(L, info);
233 } else { 249 } else {
@@ -240,17 +256,18 @@ static int pushresult(lua_State *L, int res, const char *info) {
240/* 256/*
241** This function changes the working (current) directory 257** This function changes the working (current) directory
242*/ 258*/
243static int change_dir (lua_State *L) { 259static int change_dir(lua_State * L)
244 const char *path = luaL_checkstring(L, 1); 260{
245 if (chdir(path)) { 261 const char *path = luaL_checkstring(L, 1);
246 lua_pushnil (L); 262 if (chdir(path)) {
247 lua_pushfstring (L,"Unable to change working directory to '%s'\n%s\n", 263 lua_pushnil(L);
248 path, chdir_error); 264 lua_pushfstring(L, "Unable to change working directory to '%s'\n%s\n",
249 return 2; 265 path, chdir_error);
250 } else { 266 return 2;
251 lua_pushboolean (L, 1); 267 } else {
252 return 1; 268 lua_pushboolean(L, 1);
253 } 269 return 1;
270 }
254} 271}
255 272
256/* 273/*
@@ -258,59 +275,62 @@ static int change_dir (lua_State *L) {
258** If unable to get the current directory, it returns nil 275** If unable to get the current directory, it returns nil
259** and a string describing the error 276** and a string describing the error
260*/ 277*/
261static int get_dir (lua_State *L) { 278static int get_dir(lua_State * L)
279{
262#ifdef NO_GETCWD 280#ifdef NO_GETCWD
263 lua_pushnil(L); 281 lua_pushnil(L);
264 lua_pushstring(L, "Function 'getcwd' not provided by system"); 282 lua_pushstring(L, "Function 'getcwd' not provided by system");
265 return 2; 283 return 2;
266#else 284#else
267 char *path = NULL; 285 char *path = NULL;
268 /* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */ 286 /* Passing (NULL, 0) is not guaranteed to work.
269 size_t size = LFS_MAXPATHLEN; /* initial buffer size */ 287 Use a temp buffer and size instead. */
270 int result; 288 size_t size = LFS_MAXPATHLEN; /* initial buffer size */
271 while (1) { 289 int result;
272 char* path2 = realloc(path, size); 290 while (1) {
273 if (!path2) /* failed to allocate */ { 291 char *path2 = realloc(path, size);
274 result = pusherror(L, "get_dir realloc() failed"); 292 if (!path2) { /* failed to allocate */
275 break; 293 result = pusherror(L, "get_dir realloc() failed");
276 } 294 break;
277 path = path2; 295 }
278 if (getcwd(path, size) != NULL) { 296 path = path2;
279 /* success, push the path to the Lua stack */ 297 if (getcwd(path, size) != NULL) {
280 lua_pushstring(L, path); 298 /* success, push the path to the Lua stack */
281 result = 1; 299 lua_pushstring(L, path);
282 break; 300 result = 1;
283 } 301 break;
284 if (errno != ERANGE) { /* unexpected error */
285 result = pusherror(L, "get_dir getcwd() failed");
286 break;
287 }
288 /* ERANGE = insufficient buffer capacity, double size and retry */
289 size *= 2;
290 } 302 }
291 free(path); 303 if (errno != ERANGE) { /* unexpected error */
292 return result; 304 result = pusherror(L, "get_dir getcwd() failed");
305 break;
306 }
307 /* ERANGE = insufficient buffer capacity, double size and retry */
308 size *= 2;
309 }
310 free(path);
311 return result;
293#endif 312#endif
294} 313}
295 314
296/* 315/*
297** Check if the given element on the stack is a file and returns it. 316** Check if the given element on the stack is a file and returns it.
298*/ 317*/
299static FILE *check_file (lua_State *L, int idx, const char *funcname) { 318static FILE *check_file(lua_State * L, int idx, const char *funcname)
319{
300#if LUA_VERSION_NUM == 501 320#if LUA_VERSION_NUM == 501
301 FILE **fh = (FILE **)luaL_checkudata (L, idx, "FILE*"); 321 FILE **fh = (FILE **) luaL_checkudata(L, idx, "FILE*");
302 if (*fh == NULL) { 322 if (*fh == NULL) {
303 luaL_error (L, "%s: closed file", funcname); 323 luaL_error(L, "%s: closed file", funcname);
304 return 0; 324 return 0;
305 } else 325 } else
306 return *fh; 326 return *fh;
307#elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504 327#elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504
308 luaL_Stream *fh = (luaL_Stream *)luaL_checkudata (L, idx, "FILE*"); 328 luaL_Stream *fh = (luaL_Stream *) luaL_checkudata(L, idx, "FILE*");
309 if (fh->closef == 0 || fh->f == NULL) { 329 if (fh->closef == 0 || fh->f == NULL) {
310 luaL_error (L, "%s: closed file", funcname); 330 luaL_error(L, "%s: closed file", funcname);
311 return 0; 331 return 0;
312 } else 332 } else
313 return fh->f; 333 return fh->f;
314#else 334#else
315#error unsupported Lua version 335#error unsupported Lua version
316#endif 336#endif
@@ -320,85 +340,114 @@ static FILE *check_file (lua_State *L, int idx, const char *funcname) {
320/* 340/*
321** 341**
322*/ 342*/
323static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long start, long len, const char *funcname) { 343static int _file_lock(lua_State * L, FILE * fh, const char *mode,
324 int code; 344 const long start, long len, const char *funcname)
345{
346 int code;
325#ifdef _WIN32 347#ifdef _WIN32
326 /* lkmode valid values are: 348 /* lkmode valid values are:
327 LK_LOCK Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error. 349 LK_LOCK Locks the specified bytes. If the bytes cannot be locked,
328 LK_NBLCK Locks the specified bytes. If the bytes cannot be locked, the constant returns an error. 350 the program immediately tries again after 1 second.
329 LK_NBRLCK Same as _LK_NBLCK. 351 If, after 10 attempts, the bytes cannot be locked,
330 LK_RLCK Same as _LK_LOCK. 352 the constant returns an error.
331 LK_UNLCK Unlocks the specified bytes, which must have been previously locked. 353 LK_NBLCK Locks the specified bytes. If the bytes cannot be locked,
332 354 the constant returns an error.
333 Regions should be locked only briefly and should be unlocked before closing a file or exiting the program. 355 LK_NBRLCK Same as _LK_NBLCK.
334 356 LK_RLCK Same as _LK_LOCK.
335 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp 357 LK_UNLCK Unlocks the specified bytes, which must have been
336 */ 358 previously locked.
337 int lkmode; 359
338 switch (*mode) { 360 Regions should be locked only briefly and should be unlocked
339 case 'r': lkmode = LK_NBLCK; break; 361 before closing a file or exiting the program.
340 case 'w': lkmode = LK_NBLCK; break; 362
341 case 'u': lkmode = LK_UNLCK; break; 363 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp
342 default : return luaL_error (L, "%s: invalid mode", funcname); 364 */
343 } 365 int lkmode;
344 if (!len) { 366 switch (*mode) {
345 fseek (fh, 0L, SEEK_END); 367 case 'r':
346 len = ftell (fh); 368 lkmode = LK_NBLCK;
347 } 369 break;
348 fseek (fh, start, SEEK_SET); 370 case 'w':
371 lkmode = LK_NBLCK;
372 break;
373 case 'u':
374 lkmode = LK_UNLCK;
375 break;
376 default:
377 return luaL_error(L, "%s: invalid mode", funcname);
378 }
379 if (!len) {
380 fseek(fh, 0L, SEEK_END);
381 len = ftell(fh);
382 }
383 fseek(fh, start, SEEK_SET);
349#ifdef __BORLANDC__ 384#ifdef __BORLANDC__
350 code = locking (fileno(fh), lkmode, len); 385 code = locking(fileno(fh), lkmode, len);
351#else 386#else
352 code = _locking (fileno(fh), lkmode, len); 387 code = _locking(fileno(fh), lkmode, len);
353#endif 388#endif
354#else 389#else
355 struct flock f; 390 struct flock f;
356 switch (*mode) { 391 switch (*mode) {
357 case 'w': f.l_type = F_WRLCK; break; 392 case 'w':
358 case 'r': f.l_type = F_RDLCK; break; 393 f.l_type = F_WRLCK;
359 case 'u': f.l_type = F_UNLCK; break; 394 break;
360 default : return luaL_error (L, "%s: invalid mode", funcname); 395 case 'r':
361 } 396 f.l_type = F_RDLCK;
362 f.l_whence = SEEK_SET; 397 break;
363 f.l_start = (off_t)start; 398 case 'u':
364 f.l_len = (off_t)len; 399 f.l_type = F_UNLCK;
365 code = fcntl (fileno(fh), F_SETLK, &f); 400 break;
401 default:
402 return luaL_error(L, "%s: invalid mode", funcname);
403 }
404 f.l_whence = SEEK_SET;
405 f.l_start = (off_t) start;
406 f.l_len = (off_t) len;
407 code = fcntl(fileno(fh), F_SETLK, &f);
366#endif 408#endif
367 return (code != -1); 409 return (code != -1);
368} 410}
369 411
370#ifdef _WIN32 412#ifdef _WIN32
371typedef struct lfs_Lock { 413typedef struct lfs_Lock {
372 HANDLE fd; 414 HANDLE fd;
373} lfs_Lock; 415} lfs_Lock;
374static int lfs_lock_dir(lua_State *L) { 416static int lfs_lock_dir(lua_State * L)
375 size_t pathl; HANDLE fd; 417{
418 size_t pathl;
419 HANDLE fd;
376 lfs_Lock *lock; 420 lfs_Lock *lock;
377 char *ln; 421 char *ln;
378 const char *lockfile = "/lockfile.lfs"; 422 const char *lockfile = "/lockfile.lfs";
379 const char *path = luaL_checklstring(L, 1, &pathl); 423 const char *path = luaL_checklstring(L, 1, &pathl);
380 ln = (char*)malloc(pathl + strlen(lockfile) + 1); 424 ln = (char *) malloc(pathl + strlen(lockfile) + 1);
381 if(!ln) { 425 if (!ln) {
382 lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2; 426 lua_pushnil(L);
427 lua_pushstring(L, strerror(errno));
428 return 2;
383 } 429 }
384 strcpy(ln, path); strcat(ln, lockfile); 430 strcpy(ln, path);
431 strcat(ln, lockfile);
385 fd = CreateFile(ln, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 432 fd = CreateFile(ln, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
386 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL); 433 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);
387 free(ln); 434 free(ln);
388 if(fd == INVALID_HANDLE_VALUE) { 435 if (fd == INVALID_HANDLE_VALUE) {
389 return lfs_win32_pusherror(L); 436 return lfs_win32_pusherror(L);
390 } 437 }
391 lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock)); 438 lock = (lfs_Lock *) lua_newuserdata(L, sizeof(lfs_Lock));
392 lock->fd = fd; 439 lock->fd = fd;
393 luaL_getmetatable (L, LOCK_METATABLE); 440 luaL_getmetatable(L, LOCK_METATABLE);
394 lua_setmetatable (L, -2); 441 lua_setmetatable(L, -2);
395 return 1; 442 return 1;
396} 443}
397static int lfs_unlock_dir(lua_State *L) { 444
398 lfs_Lock *lock = (lfs_Lock *)luaL_checkudata(L, 1, LOCK_METATABLE); 445static int lfs_unlock_dir(lua_State * L)
399 if(lock->fd != INVALID_HANDLE_VALUE) { 446{
447 lfs_Lock *lock = (lfs_Lock *) luaL_checkudata(L, 1, LOCK_METATABLE);
448 if (lock->fd != INVALID_HANDLE_VALUE) {
400 CloseHandle(lock->fd); 449 CloseHandle(lock->fd);
401 lock->fd=INVALID_HANDLE_VALUE; 450 lock->fd = INVALID_HANDLE_VALUE;
402 } 451 }
403 return 0; 452 return 0;
404} 453}
@@ -406,30 +455,38 @@ static int lfs_unlock_dir(lua_State *L) {
406typedef struct lfs_Lock { 455typedef struct lfs_Lock {
407 char *ln; 456 char *ln;
408} lfs_Lock; 457} lfs_Lock;
409static int lfs_lock_dir(lua_State *L) { 458static int lfs_lock_dir(lua_State * L)
459{
410 lfs_Lock *lock; 460 lfs_Lock *lock;
411 size_t pathl; 461 size_t pathl;
412 char *ln; 462 char *ln;
413 const char *lockfile = "/lockfile.lfs"; 463 const char *lockfile = "/lockfile.lfs";
414 const char *path = luaL_checklstring(L, 1, &pathl); 464 const char *path = luaL_checklstring(L, 1, &pathl);
415 lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock)); 465 lock = (lfs_Lock *) lua_newuserdata(L, sizeof(lfs_Lock));
416 ln = (char*)malloc(pathl + strlen(lockfile) + 1); 466 ln = (char *) malloc(pathl + strlen(lockfile) + 1);
417 if(!ln) { 467 if (!ln) {
418 lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2; 468 lua_pushnil(L);
469 lua_pushstring(L, strerror(errno));
470 return 2;
419 } 471 }
420 strcpy(ln, path); strcat(ln, lockfile); 472 strcpy(ln, path);
421 if(symlink("lock", ln) == -1) { 473 strcat(ln, lockfile);
422 free(ln); lua_pushnil(L); 474 if (symlink("lock", ln) == -1) {
423 lua_pushstring(L, strerror(errno)); return 2; 475 free(ln);
476 lua_pushnil(L);
477 lua_pushstring(L, strerror(errno));
478 return 2;
424 } 479 }
425 lock->ln = ln; 480 lock->ln = ln;
426 luaL_getmetatable (L, LOCK_METATABLE); 481 luaL_getmetatable(L, LOCK_METATABLE);
427 lua_setmetatable (L, -2); 482 lua_setmetatable(L, -2);
428 return 1; 483 return 1;
429} 484}
430static int lfs_unlock_dir(lua_State *L) { 485
431 lfs_Lock *lock = (lfs_Lock *)luaL_checkudata(L, 1, LOCK_METATABLE); 486static int lfs_unlock_dir(lua_State * L)
432 if(lock->ln) { 487{
488 lfs_Lock *lock = (lfs_Lock *) luaL_checkudata(L, 1, LOCK_METATABLE);
489 if (lock->ln) {
433 unlink(lock->ln); 490 unlink(lock->ln);
434 free(lock->ln); 491 free(lock->ln);
435 lock->ln = NULL; 492 lock->ln = NULL;
@@ -438,9 +495,10 @@ static int lfs_unlock_dir(lua_State *L) {
438} 495}
439#endif 496#endif
440 497
441static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { 498static int lfs_g_setmode(lua_State * L, FILE * f, int arg)
442 static const int mode[] = {_O_BINARY, _O_TEXT}; 499{
443 static const char *const modenames[] = {"binary", "text", NULL}; 500 static const int mode[] = { _O_BINARY, _O_TEXT };
501 static const char *const modenames[] = { "binary", "text", NULL };
444 int op = luaL_checkoption(L, arg, NULL, modenames); 502 int op = luaL_checkoption(L, arg, NULL, modenames);
445 int res = lfs_setmode(f, mode[op]); 503 int res = lfs_setmode(f, mode[op]);
446 if (res != -1) { 504 if (res != -1) {
@@ -459,7 +517,8 @@ static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
459 } 517 }
460} 518}
461 519
462static int lfs_f_setmode(lua_State *L) { 520static int lfs_f_setmode(lua_State * L)
521{
463 return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); 522 return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2);
464} 523}
465 524
@@ -470,19 +529,20 @@ static int lfs_f_setmode(lua_State *L) {
470** @param #3 Number with start position (optional). 529** @param #3 Number with start position (optional).
471** @param #4 Number with length (optional). 530** @param #4 Number with length (optional).
472*/ 531*/
473static int file_lock (lua_State *L) { 532static int file_lock(lua_State * L)
474 FILE *fh = check_file (L, 1, "lock"); 533{
475 const char *mode = luaL_checkstring (L, 2); 534 FILE *fh = check_file(L, 1, "lock");
476 const long start = (long) luaL_optinteger (L, 3, 0); 535 const char *mode = luaL_checkstring(L, 2);
477 long len = (long) luaL_optinteger (L, 4, 0); 536 const long start = (long) luaL_optinteger(L, 3, 0);
478 if (_file_lock (L, fh, mode, start, len, "lock")) { 537 long len = (long) luaL_optinteger(L, 4, 0);
479 lua_pushboolean (L, 1); 538 if (_file_lock(L, fh, mode, start, len, "lock")) {
480 return 1; 539 lua_pushboolean(L, 1);
481 } else { 540 return 1;
482 lua_pushnil (L); 541 } else {
483 lua_pushfstring (L, "%s", strerror(errno)); 542 lua_pushnil(L);
484 return 2; 543 lua_pushfstring(L, "%s", strerror(errno));
485 } 544 return 2;
545 }
486} 546}
487 547
488 548
@@ -492,18 +552,19 @@ static int file_lock (lua_State *L) {
492** @param #2 Number with start position (optional). 552** @param #2 Number with start position (optional).
493** @param #3 Number with length (optional). 553** @param #3 Number with length (optional).
494*/ 554*/
495static int file_unlock (lua_State *L) { 555static int file_unlock(lua_State * L)
496 FILE *fh = check_file (L, 1, "unlock"); 556{
497 const long start = (long) luaL_optinteger (L, 2, 0); 557 FILE *fh = check_file(L, 1, "unlock");
498 long len = (long) luaL_optinteger (L, 3, 0); 558 const long start = (long) luaL_optinteger(L, 2, 0);
499 if (_file_lock (L, fh, "u", start, len, "unlock")) { 559 long len = (long) luaL_optinteger(L, 3, 0);
500 lua_pushboolean (L, 1); 560 if (_file_lock(L, fh, "u", start, len, "unlock")) {
501 return 1; 561 lua_pushboolean(L, 1);
502 } else { 562 return 1;
503 lua_pushnil (L); 563 } else {
504 lua_pushfstring (L, "%s", strerror(errno)); 564 lua_pushnil(L);
505 return 2; 565 lua_pushfstring(L, "%s", strerror(errno));
506 } 566 return 2;
567 }
507} 568}
508 569
509 570
@@ -513,13 +574,15 @@ static int file_unlock (lua_State *L) {
513** @param #2 Name of link. 574** @param #2 Name of link.
514** @param #3 True if link is symbolic (optional). 575** @param #3 True if link is symbolic (optional).
515*/ 576*/
516static int make_link(lua_State *L) 577static int make_link(lua_State * L)
517{ 578{
518 const char *oldpath = luaL_checkstring(L, 1); 579 const char *oldpath = luaL_checkstring(L, 1);
519 const char *newpath = luaL_checkstring(L, 2); 580 const char *newpath = luaL_checkstring(L, 2);
520#ifndef _WIN32 581#ifndef _WIN32
521 return pushresult(L, 582 return pushresult(L,
522 (lua_toboolean(L, 3) ? symlink : link)(oldpath, newpath), NULL); 583 (lua_toboolean(L, 3) ? symlink : link) (oldpath,
584 newpath),
585 NULL);
523#else 586#else
524 int symbolic = lua_toboolean(L, 3); 587 int symbolic = lua_toboolean(L, 3);
525 STAT_STRUCT oldpathinfo; 588 STAT_STRUCT oldpathinfo;
@@ -529,18 +592,20 @@ static int make_link(lua_State *L)
529 } 592 }
530 if (!symbolic && is_dir) { 593 if (!symbolic && is_dir) {
531 lua_pushnil(L); 594 lua_pushnil(L);
532 lua_pushstring(L, "hard links to directories are not supported on Windows"); 595 lua_pushstring(L,
596 "hard links to directories are not supported on Windows");
533 return 2; 597 return 2;
534 } 598 }
535 599
536 int result = symbolic ? CreateSymbolicLink(newpath, oldpath, is_dir) 600 int result = symbolic ? CreateSymbolicLink(newpath, oldpath, is_dir)
537 : CreateHardLink(newpath, oldpath, NULL); 601 : CreateHardLink(newpath, oldpath, NULL);
538 602
539 if (result) { 603 if (result) {
540 return pushresult(L, result, NULL); 604 return pushresult(L, result, NULL);
541 } else { 605 } else {
542 lua_pushnil(L); lua_pushstring(L, symbolic ? "make_link CreateSymbolicLink() failed" 606 lua_pushnil(L);
543 : "make_link CreateHardLink() failed"); 607 lua_pushstring(L, symbolic ? "make_link CreateSymbolicLink() failed"
608 : "make_link CreateHardLink() failed");
544 return 2; 609 return 2;
545 } 610 }
546#endif 611#endif
@@ -551,7 +616,8 @@ static int make_link(lua_State *L)
551** Creates a directory. 616** Creates a directory.
552** @param #1 Directory path. 617** @param #1 Directory path.
553*/ 618*/
554static int make_dir (lua_State *L) { 619static int make_dir(lua_State * L)
620{
555 const char *path = luaL_checkstring(L, 1); 621 const char *path = luaL_checkstring(L, 1);
556 return pushresult(L, lfs_mkdir(path), NULL); 622 return pushresult(L, lfs_mkdir(path), NULL);
557} 623}
@@ -561,7 +627,8 @@ static int make_dir (lua_State *L) {
561** Removes a directory. 627** Removes a directory.
562** @param #1 Directory path. 628** @param #1 Directory path.
563*/ 629*/
564static int remove_dir (lua_State *L) { 630static int remove_dir(lua_State * L)
631{
565 const char *path = luaL_checkstring(L, 1); 632 const char *path = luaL_checkstring(L, 1);
566 return pushresult(L, rmdir(path), NULL); 633 return pushresult(L, rmdir(path), NULL);
567} 634}
@@ -570,46 +637,47 @@ static int remove_dir (lua_State *L) {
570/* 637/*
571** Directory iterator 638** Directory iterator
572*/ 639*/
573static int dir_iter (lua_State *L) { 640static int dir_iter(lua_State * L)
641{
574#ifdef _WIN32 642#ifdef _WIN32
575 struct _finddata_t c_file; 643 struct _finddata_t c_file;
576#else 644#else
577 struct dirent *entry; 645 struct dirent *entry;
578#endif 646#endif
579 dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); 647 dir_data *d = (dir_data *) luaL_checkudata(L, 1, DIR_METATABLE);
580 luaL_argcheck (L, d->closed == 0, 1, "closed directory"); 648 luaL_argcheck(L, d->closed == 0, 1, "closed directory");
581#ifdef _WIN32 649#ifdef _WIN32
582 if (d->hFile == 0L) { /* first entry */ 650 if (d->hFile == 0L) { /* first entry */
583 if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) { 651 if ((d->hFile = _findfirst(d->pattern, &c_file)) == -1L) {
584 lua_pushnil (L); 652 lua_pushnil(L);
585 lua_pushstring (L, strerror (errno)); 653 lua_pushstring(L, strerror(errno));
586 d->closed = 1; 654 d->closed = 1;
587 return 2; 655 return 2;
588 } else { 656 } else {
589 lua_pushstring (L, c_file.name); 657 lua_pushstring(L, c_file.name);
590 return 1; 658 return 1;
591 } 659 }
592 } else { /* next entry */ 660 } else { /* next entry */
593 if (_findnext (d->hFile, &c_file) == -1L) { 661 if (_findnext(d->hFile, &c_file) == -1L) {
594 /* no more entries => close directory */ 662 /* no more entries => close directory */
595 _findclose (d->hFile); 663 _findclose(d->hFile);
596 d->closed = 1; 664 d->closed = 1;
597 return 0; 665 return 0;
598 } else { 666 } else {
599 lua_pushstring (L, c_file.name); 667 lua_pushstring(L, c_file.name);
600 return 1; 668 return 1;
601 } 669 }
602 } 670 }
603#else 671#else
604 if ((entry = readdir (d->dir)) != NULL) { 672 if ((entry = readdir(d->dir)) != NULL) {
605 lua_pushstring (L, entry->d_name); 673 lua_pushstring(L, entry->d_name);
606 return 1; 674 return 1;
607 } else { 675 } else {
608 /* no more entries => close directory */ 676 /* no more entries => close directory */
609 closedir (d->dir); 677 closedir(d->dir);
610 d->closed = 1; 678 d->closed = 1;
611 return 0; 679 return 0;
612 } 680 }
613#endif 681#endif
614} 682}
615 683
@@ -617,85 +685,89 @@ static int dir_iter (lua_State *L) {
617/* 685/*
618** Closes directory iterators 686** Closes directory iterators
619*/ 687*/
620static int dir_close (lua_State *L) { 688static int dir_close(lua_State * L)
621 dir_data *d = (dir_data *)lua_touserdata (L, 1); 689{
690 dir_data *d = (dir_data *) lua_touserdata(L, 1);
622#ifdef _WIN32 691#ifdef _WIN32
623 if (!d->closed && d->hFile) { 692 if (!d->closed && d->hFile) {
624 _findclose (d->hFile); 693 _findclose(d->hFile);
625 } 694 }
626#else 695#else
627 if (!d->closed && d->dir) { 696 if (!d->closed && d->dir) {
628 closedir (d->dir); 697 closedir(d->dir);
629 } 698 }
630#endif 699#endif
631 d->closed = 1; 700 d->closed = 1;
632 return 0; 701 return 0;
633} 702}
634 703
635 704
636/* 705/*
637** Factory of directory iterators 706** Factory of directory iterators
638*/ 707*/
639static int dir_iter_factory (lua_State *L) { 708static int dir_iter_factory(lua_State * L)
640 const char *path = luaL_checkstring (L, 1); 709{
641 dir_data *d; 710 const char *path = luaL_checkstring(L, 1);
642 lua_pushcfunction (L, dir_iter); 711 dir_data *d;
643 d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); 712 lua_pushcfunction(L, dir_iter);
644 luaL_getmetatable (L, DIR_METATABLE); 713 d = (dir_data *) lua_newuserdata(L, sizeof(dir_data));
645 lua_setmetatable (L, -2); 714 luaL_getmetatable(L, DIR_METATABLE);
646 d->closed = 0; 715 lua_setmetatable(L, -2);
716 d->closed = 0;
647#ifdef _WIN32 717#ifdef _WIN32
648 d->hFile = 0L; 718 d->hFile = 0L;
649 if (strlen(path) > MAX_PATH-2) 719 if (strlen(path) > MAX_PATH - 2)
650 luaL_error (L, "path too long: %s", path); 720 luaL_error(L, "path too long: %s", path);
651 else 721 else
652 sprintf (d->pattern, "%s/*", path); 722 sprintf(d->pattern, "%s/*", path);
653#else 723#else
654 d->dir = opendir (path); 724 d->dir = opendir(path);
655 if (d->dir == NULL) 725 if (d->dir == NULL)
656 luaL_error (L, "cannot open %s: %s", path, strerror (errno)); 726 luaL_error(L, "cannot open %s: %s", path, strerror(errno));
657#endif 727#endif
658 return 2; 728 return 2;
659} 729}
660 730
661 731
662/* 732/*
663** Creates directory metatable. 733** Creates directory metatable.
664*/ 734*/
665static int dir_create_meta (lua_State *L) { 735static int dir_create_meta(lua_State * L)
666 luaL_newmetatable (L, DIR_METATABLE); 736{
667 737 luaL_newmetatable(L, DIR_METATABLE);
668 /* Method table */ 738
669 lua_newtable(L); 739 /* Method table */
670 lua_pushcfunction (L, dir_iter); 740 lua_newtable(L);
671 lua_setfield(L, -2, "next"); 741 lua_pushcfunction(L, dir_iter);
672 lua_pushcfunction (L, dir_close); 742 lua_setfield(L, -2, "next");
673 lua_setfield(L, -2, "close"); 743 lua_pushcfunction(L, dir_close);
674 744 lua_setfield(L, -2, "close");
675 /* Metamethods */ 745
676 lua_setfield(L, -2, "__index"); 746 /* Metamethods */
677 lua_pushcfunction (L, dir_close); 747 lua_setfield(L, -2, "__index");
678 lua_setfield (L, -2, "__gc"); 748 lua_pushcfunction(L, dir_close);
679 return 1; 749 lua_setfield(L, -2, "__gc");
750 return 1;
680} 751}
681 752
682 753
683/* 754/*
684** Creates lock metatable. 755** Creates lock metatable.
685*/ 756*/
686static int lock_create_meta (lua_State *L) { 757static int lock_create_meta(lua_State * L)
687 luaL_newmetatable (L, LOCK_METATABLE); 758{
688 759 luaL_newmetatable(L, LOCK_METATABLE);
689 /* Method table */ 760
690 lua_newtable(L); 761 /* Method table */
691 lua_pushcfunction(L, lfs_unlock_dir); 762 lua_newtable(L);
692 lua_setfield(L, -2, "free"); 763 lua_pushcfunction(L, lfs_unlock_dir);
693 764 lua_setfield(L, -2, "free");
694 /* Metamethods */ 765
695 lua_setfield(L, -2, "__index"); 766 /* Metamethods */
696 lua_pushcfunction(L, lfs_unlock_dir); 767 lua_setfield(L, -2, "__index");
697 lua_setfield(L, -2, "__gc"); 768 lua_pushcfunction(L, lfs_unlock_dir);
698 return 1; 769 lua_setfield(L, -2, "__gc");
770 return 1;
699} 771}
700 772
701 773
@@ -703,26 +775,28 @@ static int lock_create_meta (lua_State *L) {
703** Convert the inode protection mode to a string. 775** Convert the inode protection mode to a string.
704*/ 776*/
705#ifdef _WIN32 777#ifdef _WIN32
706static const char *mode2string (unsigned short mode) { 778static const char *mode2string(unsigned short mode)
779{
707#else 780#else
708static const char *mode2string (mode_t mode) { 781static const char *mode2string(mode_t mode)
782{
709#endif 783#endif
710 if ( S_ISREG(mode) ) 784 if (S_ISREG(mode))
711 return "file"; 785 return "file";
712 else if ( S_ISDIR(mode) ) 786 else if (S_ISDIR(mode))
713 return "directory"; 787 return "directory";
714 else if ( S_ISLNK(mode) ) 788 else if (S_ISLNK(mode))
715 return "link"; 789 return "link";
716 else if ( S_ISSOCK(mode) ) 790 else if (S_ISSOCK(mode))
717 return "socket"; 791 return "socket";
718 else if ( S_ISFIFO(mode) ) 792 else if (S_ISFIFO(mode))
719 return "named pipe"; 793 return "named pipe";
720 else if ( S_ISCHR(mode) ) 794 else if (S_ISCHR(mode))
721 return "char device"; 795 return "char device";
722 else if ( S_ISBLK(mode) ) 796 else if (S_ISBLK(mode))
723 return "block device"; 797 return "block device";
724 else 798 else
725 return "other"; 799 return "other";
726} 800}
727 801
728 802
@@ -732,11 +806,12 @@ static const char *mode2string (mode_t mode) {
732** @param #2 Access time in seconds, current time is used if missing. 806** @param #2 Access time in seconds, current time is used if missing.
733** @param #3 Modification time in seconds, access time is used if missing. 807** @param #3 Modification time in seconds, access time is used if missing.
734*/ 808*/
735static int file_utime (lua_State *L) { 809static int file_utime(lua_State * L)
810{
736 const char *file = luaL_checkstring(L, 1); 811 const char *file = luaL_checkstring(L, 1);
737 struct utimbuf utb, *buf; 812 struct utimbuf utb, *buf;
738 813
739 if (lua_gettop (L) == 1) /* set to current date/time */ 814 if (lua_gettop(L) == 1) /* set to current date/time */
740 buf = NULL; 815 buf = NULL;
741 else { 816 else {
742 utb.actime = (time_t) luaL_optnumber(L, 2, 0); 817 utb.actime = (time_t) luaL_optnumber(L, 2, 0);
@@ -749,173 +824,225 @@ static int file_utime (lua_State *L) {
749 824
750 825
751/* inode protection mode */ 826/* inode protection mode */
752static void push_st_mode (lua_State *L, STAT_STRUCT *info) { 827static void push_st_mode(lua_State * L, STAT_STRUCT * info)
753 lua_pushstring (L, mode2string (info->st_mode)); 828{
829 lua_pushstring(L, mode2string(info->st_mode));
754} 830}
831
755/* device inode resides on */ 832/* device inode resides on */
756static void push_st_dev (lua_State *L, STAT_STRUCT *info) { 833static void push_st_dev(lua_State * L, STAT_STRUCT * info)
757 lua_pushinteger (L, (lua_Integer) info->st_dev); 834{
835 lua_pushinteger(L, (lua_Integer) info->st_dev);
758} 836}
837
759/* inode's number */ 838/* inode's number */
760static void push_st_ino (lua_State *L, STAT_STRUCT *info) { 839static void push_st_ino(lua_State * L, STAT_STRUCT * info)
761 lua_pushinteger (L, (lua_Integer) info->st_ino); 840{
841 lua_pushinteger(L, (lua_Integer) info->st_ino);
762} 842}
843
763/* number of hard links to the file */ 844/* number of hard links to the file */
764static void push_st_nlink (lua_State *L, STAT_STRUCT *info) { 845static void push_st_nlink(lua_State * L, STAT_STRUCT * info)
765 lua_pushinteger (L, (lua_Integer)info->st_nlink); 846{
847 lua_pushinteger(L, (lua_Integer) info->st_nlink);
766} 848}
849
767/* user-id of owner */ 850/* user-id of owner */
768static void push_st_uid (lua_State *L, STAT_STRUCT *info) { 851static void push_st_uid(lua_State * L, STAT_STRUCT * info)
769 lua_pushinteger (L, (lua_Integer)info->st_uid); 852{
853 lua_pushinteger(L, (lua_Integer) info->st_uid);
770} 854}
855
771/* group-id of owner */ 856/* group-id of owner */
772static void push_st_gid (lua_State *L, STAT_STRUCT *info) { 857static void push_st_gid(lua_State * L, STAT_STRUCT * info)
773 lua_pushinteger (L, (lua_Integer)info->st_gid); 858{
859 lua_pushinteger(L, (lua_Integer) info->st_gid);
774} 860}
861
775/* device type, for special file inode */ 862/* device type, for special file inode */
776static void push_st_rdev (lua_State *L, STAT_STRUCT *info) { 863static void push_st_rdev(lua_State * L, STAT_STRUCT * info)
777 lua_pushinteger (L, (lua_Integer) info->st_rdev); 864{
865 lua_pushinteger(L, (lua_Integer) info->st_rdev);
778} 866}
867
779/* time of last access */ 868/* time of last access */
780static void push_st_atime (lua_State *L, STAT_STRUCT *info) { 869static void push_st_atime(lua_State * L, STAT_STRUCT * info)
781 lua_pushinteger (L, (lua_Integer) info->st_atime); 870{
871 lua_pushinteger(L, (lua_Integer) info->st_atime);
782} 872}
873
783/* time of last data modification */ 874/* time of last data modification */
784static void push_st_mtime (lua_State *L, STAT_STRUCT *info) { 875static void push_st_mtime(lua_State * L, STAT_STRUCT * info)
785 lua_pushinteger (L, (lua_Integer) info->st_mtime); 876{
877 lua_pushinteger(L, (lua_Integer) info->st_mtime);
786} 878}
879
787/* time of last file status change */ 880/* time of last file status change */
788static void push_st_ctime (lua_State *L, STAT_STRUCT *info) { 881static void push_st_ctime(lua_State * L, STAT_STRUCT * info)
789 lua_pushinteger (L, (lua_Integer) info->st_ctime); 882{
883 lua_pushinteger(L, (lua_Integer) info->st_ctime);
790} 884}
885
791/* file size, in bytes */ 886/* file size, in bytes */
792static void push_st_size (lua_State *L, STAT_STRUCT *info) { 887static void push_st_size(lua_State * L, STAT_STRUCT * info)
793 lua_pushinteger (L, (lua_Integer)info->st_size); 888{
889 lua_pushinteger(L, (lua_Integer) info->st_size);
794} 890}
891
795#ifndef _WIN32 892#ifndef _WIN32
796/* blocks allocated for file */ 893/* blocks allocated for file */
797static void push_st_blocks (lua_State *L, STAT_STRUCT *info) { 894static void push_st_blocks(lua_State * L, STAT_STRUCT * info)
798 lua_pushinteger (L, (lua_Integer)info->st_blocks); 895{
896 lua_pushinteger(L, (lua_Integer) info->st_blocks);
799} 897}
898
800/* optimal file system I/O blocksize */ 899/* optimal file system I/O blocksize */
801static void push_st_blksize (lua_State *L, STAT_STRUCT *info) { 900static void push_st_blksize(lua_State * L, STAT_STRUCT * info)
802 lua_pushinteger (L, (lua_Integer)info->st_blksize); 901{
902 lua_pushinteger(L, (lua_Integer) info->st_blksize);
803} 903}
804#endif 904#endif
805 905
806 /* 906 /*
807** Convert the inode protection mode to a permission list. 907 ** Convert the inode protection mode to a permission list.
808*/ 908 */
809 909
810#ifdef _WIN32 910#ifdef _WIN32
811static const char *perm2string (unsigned short mode) { 911static const char *perm2string(unsigned short mode)
912{
812 static char perms[10] = "---------"; 913 static char perms[10] = "---------";
813 int i; 914 int i;
814 for (i=0;i<9;i++) perms[i]='-'; 915 for (i = 0; i < 9; i++)
815 if (mode & _S_IREAD) 916 perms[i] = '-';
816 { perms[0] = 'r'; perms[3] = 'r'; perms[6] = 'r'; } 917 if (mode & _S_IREAD) {
817 if (mode & _S_IWRITE) 918 perms[0] = 'r';
818 { perms[1] = 'w'; perms[4] = 'w'; perms[7] = 'w'; } 919 perms[3] = 'r';
819 if (mode & _S_IEXEC) 920 perms[6] = 'r';
820 { perms[2] = 'x'; perms[5] = 'x'; perms[8] = 'x'; } 921 }
922 if (mode & _S_IWRITE) {
923 perms[1] = 'w';
924 perms[4] = 'w';
925 perms[7] = 'w';
926 }
927 if (mode & _S_IEXEC) {
928 perms[2] = 'x';
929 perms[5] = 'x';
930 perms[8] = 'x';
931 }
821 return perms; 932 return perms;
822} 933}
823#else 934#else
824static const char *perm2string (mode_t mode) { 935static const char *perm2string(mode_t mode)
936{
825 static char perms[10] = "---------"; 937 static char perms[10] = "---------";
826 int i; 938 int i;
827 for (i=0;i<9;i++) perms[i]='-'; 939 for (i = 0; i < 9; i++)
828 if (mode & S_IRUSR) perms[0] = 'r'; 940 perms[i] = '-';
829 if (mode & S_IWUSR) perms[1] = 'w'; 941 if (mode & S_IRUSR)
830 if (mode & S_IXUSR) perms[2] = 'x'; 942 perms[0] = 'r';
831 if (mode & S_IRGRP) perms[3] = 'r'; 943 if (mode & S_IWUSR)
832 if (mode & S_IWGRP) perms[4] = 'w'; 944 perms[1] = 'w';
833 if (mode & S_IXGRP) perms[5] = 'x'; 945 if (mode & S_IXUSR)
834 if (mode & S_IROTH) perms[6] = 'r'; 946 perms[2] = 'x';
835 if (mode & S_IWOTH) perms[7] = 'w'; 947 if (mode & S_IRGRP)
836 if (mode & S_IXOTH) perms[8] = 'x'; 948 perms[3] = 'r';
949 if (mode & S_IWGRP)
950 perms[4] = 'w';
951 if (mode & S_IXGRP)
952 perms[5] = 'x';
953 if (mode & S_IROTH)
954 perms[6] = 'r';
955 if (mode & S_IWOTH)
956 perms[7] = 'w';
957 if (mode & S_IXOTH)
958 perms[8] = 'x';
837 return perms; 959 return perms;
838} 960}
839#endif 961#endif
840 962
841/* permssions string */ 963/* permssions string */
842static void push_st_perm (lua_State *L, STAT_STRUCT *info) { 964static void push_st_perm(lua_State * L, STAT_STRUCT * info)
843 lua_pushstring (L, perm2string (info->st_mode)); 965{
966 lua_pushstring(L, perm2string(info->st_mode));
844} 967}
845 968
846typedef void (*_push_function) (lua_State *L, STAT_STRUCT *info); 969typedef void (*_push_function)(lua_State * L, STAT_STRUCT * info);
847 970
848struct _stat_members { 971struct _stat_members {
849 const char *name; 972 const char *name;
850 _push_function push; 973 _push_function push;
851}; 974};
852 975
853struct _stat_members members[] = { 976struct _stat_members members[] = {
854 { "mode", push_st_mode }, 977 { "mode", push_st_mode },
855 { "dev", push_st_dev }, 978 { "dev", push_st_dev },
856 { "ino", push_st_ino }, 979 { "ino", push_st_ino },
857 { "nlink", push_st_nlink }, 980 { "nlink", push_st_nlink },
858 { "uid", push_st_uid }, 981 { "uid", push_st_uid },
859 { "gid", push_st_gid }, 982 { "gid", push_st_gid },
860 { "rdev", push_st_rdev }, 983 { "rdev", push_st_rdev },
861 { "access", push_st_atime }, 984 { "access", push_st_atime },
862 { "modification", push_st_mtime }, 985 { "modification", push_st_mtime },
863 { "change", push_st_ctime }, 986 { "change", push_st_ctime },
864 { "size", push_st_size }, 987 { "size", push_st_size },
865 { "permissions", push_st_perm }, 988 { "permissions", push_st_perm },
866#ifndef _WIN32 989#ifndef _WIN32
867 { "blocks", push_st_blocks }, 990 { "blocks", push_st_blocks },
868 { "blksize", push_st_blksize }, 991 { "blksize", push_st_blksize },
869#endif 992#endif
870 { NULL, NULL } 993 { NULL, NULL }
871}; 994};
872 995
873/* 996/*
874** Get file or symbolic link information 997** Get file or symbolic link information
875*/ 998*/
876static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { 999static int _file_info_(lua_State * L,
877 STAT_STRUCT info; 1000 int (*st)(const char *, STAT_STRUCT *))
878 const char *file = luaL_checkstring (L, 1); 1001{
879 int i; 1002 STAT_STRUCT info;
880 1003 const char *file = luaL_checkstring(L, 1);
881 if (st(file, &info)) { 1004 int i;
882 lua_pushnil(L); 1005
883 lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno)); 1006 if (st(file, &info)) {
884 lua_pushinteger(L, errno); 1007 lua_pushnil(L);
885 return 3; 1008 lua_pushfstring(L, "cannot obtain information from file '%s': %s",
886 } 1009 file, strerror(errno));
887 if (lua_isstring (L, 2)) { 1010 lua_pushinteger(L, errno);
888 const char *member = lua_tostring (L, 2); 1011 return 3;
889 for (i = 0; members[i].name; i++) { 1012 }
890 if (strcmp(members[i].name, member) == 0) { 1013 if (lua_isstring(L, 2)) {
891 /* push member value and return */ 1014 const char *member = lua_tostring(L, 2);
892 members[i].push (L, &info); 1015 for (i = 0; members[i].name; i++) {
893 return 1; 1016 if (strcmp(members[i].name, member) == 0) {
894 } 1017 /* push member value and return */
895 } 1018 members[i].push(L, &info);
896 /* member not found */
897 return luaL_error(L, "invalid attribute name '%s'", member);
898 }
899 /* creates a table if none is given, removes extra arguments */
900 lua_settop(L, 2);
901 if (!lua_istable (L, 2)) {
902 lua_newtable (L);
903 }
904 /* stores all members in table on top of the stack */
905 for (i = 0; members[i].name; i++) {
906 lua_pushstring (L, members[i].name);
907 members[i].push (L, &info);
908 lua_rawset (L, -3);
909 }
910 return 1; 1019 return 1;
1020 }
1021 }
1022 /* member not found */
1023 return luaL_error(L, "invalid attribute name '%s'", member);
1024 }
1025 /* creates a table if none is given, removes extra arguments */
1026 lua_settop(L, 2);
1027 if (!lua_istable(L, 2)) {
1028 lua_newtable(L);
1029 }
1030 /* stores all members in table on top of the stack */
1031 for (i = 0; members[i].name; i++) {
1032 lua_pushstring(L, members[i].name);
1033 members[i].push(L, &info);
1034 lua_rawset(L, -3);
1035 }
1036 return 1;
911} 1037}
912 1038
913 1039
914/* 1040/*
915** Get file information using stat. 1041** Get file information using stat.
916*/ 1042*/
917static int file_info (lua_State *L) { 1043static int file_info(lua_State * L)
918 return _file_info_ (L, STAT_FUNC); 1044{
1045 return _file_info_(L, STAT_FUNC);
919} 1046}
920 1047
921 1048
@@ -925,84 +1052,90 @@ static int file_info (lua_State *L) {
925** Returns 1 if successful (with the target on top of the stack), 1052** Returns 1 if successful (with the target on top of the stack),
926** 0 on failure (with stack unchanged, and errno set). 1053** 0 on failure (with stack unchanged, and errno set).
927*/ 1054*/
928static int push_link_target(lua_State *L) { 1055static int push_link_target(lua_State * L)
929 const char *file = luaL_checkstring(L, 1); 1056{
1057 const char *file = luaL_checkstring(L, 1);
930#ifdef _WIN32 1058#ifdef _WIN32
931 HANDLE h = CreateFile(file, GENERIC_READ, 1059 HANDLE h = CreateFile(file, GENERIC_READ,
932 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 1060 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
933 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 1061 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
934 if (h == INVALID_HANDLE_VALUE) { 1062 if (h == INVALID_HANDLE_VALUE) {
935 return lfs_win32_pusherror(L); 1063 return lfs_win32_pusherror(L);
936 } 1064 }
937#endif 1065#endif
938 char *target = NULL; 1066 char *target = NULL;
939 int tsize, size = 256; /* size = initial buffer capacity */ 1067 int tsize, size = 256; /* size = initial buffer capacity */
940 int ok = 0; 1068 int ok = 0;
941 while (!ok) { 1069 while (!ok) {
942 char* target2 = realloc(target, size); 1070 char *target2 = realloc(target, size);
943 if (!target2) { /* failed to allocate */ 1071 if (!target2) { /* failed to allocate */
944 break; 1072 break;
945 } 1073 }
946 target = target2; 1074 target = target2;
947#ifdef _WIN32 1075#ifdef _WIN32
948 tsize = GetFinalPathNameByHandle(h, target, size, FILE_NAME_OPENED); 1076 tsize = GetFinalPathNameByHandle(h, target, size, FILE_NAME_OPENED);
949#else 1077#else
950 tsize = readlink(file, target, size); 1078 tsize = readlink(file, target, size);
951#endif 1079#endif
952 if (tsize < 0) { /* a readlink() error occurred */ 1080 if (tsize < 0) { /* a readlink() error occurred */
953 break; 1081 break;
954 } 1082 }
955 if (tsize < size) { 1083 if (tsize < size) {
956#ifdef _WIN32 1084#ifdef _WIN32
957 if (tsize > 4 && strncmp(target, "\\\\?\\", 4) == 0) { 1085 if (tsize > 4 && strncmp(target, "\\\\?\\", 4) == 0) {
958 memmove_s(target, tsize - 3, target + 4, tsize - 3); 1086 memmove_s(target, tsize - 3, target + 4, tsize - 3);
959 tsize -= 4; 1087 tsize -= 4;
960 } 1088 }
961#endif 1089#endif
962 ok = 1; 1090 ok = 1;
963 break; 1091 break;
964 } 1092 }
965 /* possibly truncated readlink() result, double size and retry */ 1093 /* possibly truncated readlink() result, double size and retry */
966 size *= 2; 1094 size *= 2;
967 } 1095 }
968 if (ok) { 1096 if (ok) {
969 target[tsize] = '\0'; 1097 target[tsize] = '\0';
970 lua_pushlstring(L, target, tsize); 1098 lua_pushlstring(L, target, tsize);
971 } 1099 }
972#ifdef _WIN32 1100#ifdef _WIN32
973 CloseHandle(h); 1101 CloseHandle(h);
974#endif 1102#endif
975 free(target); 1103 free(target);
976 return ok; 1104 return ok;
977} 1105}
978 1106
979/* 1107/*
980** Get symbolic link information using lstat. 1108** Get symbolic link information using lstat.
981*/ 1109*/
982static int link_info (lua_State *L) { 1110static int link_info(lua_State * L)
983 int ret; 1111{
984 if (lua_isstring (L, 2) && (strcmp(lua_tostring(L, 2), "target") == 0)) { 1112 int ret;
985 int ok = push_link_target(L); 1113 if (lua_isstring(L, 2) && (strcmp(lua_tostring(L, 2), "target") == 0)) {
986 return ok ? 1 : pusherror(L, "could not obtain link target"); 1114 int ok = push_link_target(L);
987 } 1115 return ok ? 1 : pusherror(L, "could not obtain link target");
988 ret = _file_info_ (L, LSTAT_FUNC); 1116 }
989 if (ret == 1 && lua_type(L, -1) == LUA_TTABLE) { 1117 ret = _file_info_(L, LSTAT_FUNC);
990 int ok = push_link_target(L); 1118 if (ret == 1 && lua_type(L, -1) == LUA_TTABLE) {
991 if (ok) { 1119 int ok = push_link_target(L);
992 lua_setfield(L, -2, "target"); 1120 if (ok) {
993 } 1121 lua_setfield(L, -2, "target");
994 } 1122 }
995 return ret; 1123 }
1124 return ret;
996} 1125}
997 1126
998 1127
999/* 1128/*
1000** Assumes the table is on top of the stack. 1129** Assumes the table is on top of the stack.
1001*/ 1130*/
1002static void set_info (lua_State *L) { 1131static void set_info(lua_State * L)
1132{
1003 lua_pushliteral(L, "Copyright (C) 2003-2017 Kepler Project"); 1133 lua_pushliteral(L, "Copyright (C) 2003-2017 Kepler Project");
1004 lua_setfield(L, -2, "_COPYRIGHT"); 1134 lua_setfield(L, -2, "_COPYRIGHT");
1005 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"); 1135 lua_pushliteral(L,
1136 "LuaFileSystem is a Lua library developed to complement "
1137 "the set of functions related to file systems offered by "
1138 "the standard Lua distribution");
1006 lua_setfield(L, -2, "_DESCRIPTION"); 1139 lua_setfield(L, -2, "_DESCRIPTION");
1007 lua_pushliteral(L, "LuaFileSystem " LFS_VERSION); 1140 lua_pushliteral(L, "LuaFileSystem " LFS_VERSION);
1008 lua_setfield(L, -2, "_VERSION"); 1141 lua_setfield(L, -2, "_VERSION");
@@ -1010,28 +1143,29 @@ static void set_info (lua_State *L) {
1010 1143
1011 1144
1012static const struct luaL_Reg fslib[] = { 1145static const struct luaL_Reg fslib[] = {
1013 {"attributes", file_info}, 1146 { "attributes", file_info },
1014 {"chdir", change_dir}, 1147 { "chdir", change_dir },
1015 {"currentdir", get_dir}, 1148 { "currentdir", get_dir },
1016 {"dir", dir_iter_factory}, 1149 { "dir", dir_iter_factory },
1017 {"link", make_link}, 1150 { "link", make_link },
1018 {"lock", file_lock}, 1151 { "lock", file_lock },
1019 {"mkdir", make_dir}, 1152 { "mkdir", make_dir },
1020 {"rmdir", remove_dir}, 1153 { "rmdir", remove_dir },
1021 {"symlinkattributes", link_info}, 1154 { "symlinkattributes", link_info },
1022 {"setmode", lfs_f_setmode}, 1155 { "setmode", lfs_f_setmode },
1023 {"touch", file_utime}, 1156 { "touch", file_utime },
1024 {"unlock", file_unlock}, 1157 { "unlock", file_unlock },
1025 {"lock_dir", lfs_lock_dir}, 1158 { "lock_dir", lfs_lock_dir },
1026 {NULL, NULL}, 1159 { NULL, NULL },
1027}; 1160};
1028 1161
1029LFS_EXPORT int luaopen_lfs (lua_State *L) { 1162LFS_EXPORT int luaopen_lfs(lua_State * L)
1030 dir_create_meta (L); 1163{
1031 lock_create_meta (L); 1164 dir_create_meta(L);
1032 new_lib (L, fslib); 1165 lock_create_meta(L);
1033 lua_pushvalue(L, -1); 1166 new_lib(L, fslib);
1034 lua_setglobal(L, LFS_LIBNAME); 1167 lua_pushvalue(L, -1);
1035 set_info (L); 1168 lua_setglobal(L, LFS_LIBNAME);
1036 return 1; 1169 set_info(L);
1170 return 1;
1037} 1171}
diff --git a/src/lfs.h b/src/lfs.h
index 4587564..13b60a9 100644
--- a/src/lfs.h
+++ b/src/lfs.h
@@ -1,33 +1,34 @@
1/* 1/*
2** LuaFileSystem 2** LuaFileSystem
3** Copyright Kepler Project 2003 - 2017 (http://keplerproject.github.io/luafilesystem) 3** Copyright Kepler Project 2003 - 2020
4** (http://keplerproject.github.io/luafilesystem)
4*/ 5*/
5 6
6/* Define 'chdir' for systems that do not implement it */ 7/* Define 'chdir' for systems that do not implement it */
7#ifdef NO_CHDIR 8#ifdef NO_CHDIR
8 #define chdir(p) (-1) 9#define chdir(p) (-1)
9 #define chdir_error "Function 'chdir' not provided by system" 10#define chdir_error "Function 'chdir' not provided by system"
10#else 11#else
11 #define chdir_error strerror(errno) 12#define chdir_error strerror(errno)
12#endif 13#endif
13 14
14#ifdef _WIN32 15#ifdef _WIN32
15 #define chdir(p) (_chdir(p)) 16#define chdir(p) (_chdir(p))
16 #define getcwd(d, s) (_getcwd(d, s)) 17#define getcwd(d, s) (_getcwd(d, s))
17 #define rmdir(p) (_rmdir(p)) 18#define rmdir(p) (_rmdir(p))
18 #define LFS_EXPORT __declspec (dllexport) 19#define LFS_EXPORT __declspec (dllexport)
19 #ifndef fileno 20#ifndef fileno
20 #define fileno(f) (_fileno(f)) 21#define fileno(f) (_fileno(f))
21 #endif 22#endif
22#else 23#else
23 #define LFS_EXPORT 24#define LFS_EXPORT
24#endif 25#endif
25 26
26#ifdef __cplusplus 27#ifdef __cplusplus
27extern "C" { 28extern "C" {
28#endif 29#endif
29 30
30LFS_EXPORT int luaopen_lfs (lua_State *L); 31 LFS_EXPORT int luaopen_lfs(lua_State * L);
31 32
32#ifdef __cplusplus 33#ifdef __cplusplus
33} 34}