diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lfs.c | 123 |
1 files changed, 121 insertions, 2 deletions
| @@ -9,13 +9,14 @@ | |||
| 9 | ** lfs.lock (fh, mode) | 9 | ** lfs.lock (fh, mode) |
| 10 | ** lfs.unlock (fh) | 10 | ** lfs.unlock (fh) |
| 11 | ** | 11 | ** |
| 12 | ** $Id: lfs.c,v 1.3 2004/10/15 10:04:15 tomas Exp $ | 12 | ** $Id: lfs.c,v 1.4 2004/10/23 22:33:11 tomas Exp $ |
| 13 | */ | 13 | */ |
| 14 | 14 | ||
| 15 | #include <errno.h> | 15 | #include <errno.h> |
| 16 | #include <stdio.h> | 16 | #include <stdio.h> |
| 17 | #include <string.h> | 17 | #include <string.h> |
| 18 | #include <time.h> | 18 | #include <time.h> |
| 19 | #include <sys/stat.h> | ||
| 19 | 20 | ||
| 20 | #ifdef WIN32 | 21 | #ifdef WIN32 |
| 21 | #include <direct.h> | 22 | #include <direct.h> |
| @@ -26,7 +27,6 @@ | |||
| 26 | #include <dirent.h> | 27 | #include <dirent.h> |
| 27 | #include <fcntl.h> | 28 | #include <fcntl.h> |
| 28 | #include <sys/types.h> | 29 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> | ||
| 30 | #endif | 30 | #endif |
| 31 | 31 | ||
| 32 | #include <lua.h> | 32 | #include <lua.h> |
| @@ -335,7 +335,126 @@ static int dir_create_meta (lua_State *L) { | |||
| 335 | } | 335 | } |
| 336 | 336 | ||
| 337 | 337 | ||
| 338 | #ifdef _WIN32 | ||
| 339 | #define S_ISDIR(mode) (mode&_S_IFDIR) | ||
| 340 | #define S_ISREG(mode) (mode&_S_IFREG) | ||
| 341 | #define S_ISLNK(mode) (0) | ||
| 342 | #define S_ISSOCK(mode) (0) | ||
| 343 | #define S_ISFIFO(mode) (0) | ||
| 344 | #define S_ISCHR(mode) (0) | ||
| 345 | #define S_ISBLK(mode) (0) | ||
| 346 | #endif | ||
| 347 | /* | ||
| 348 | ** Convert the inode protection mode to a string. | ||
| 349 | */ | ||
| 350 | static const char *mode2string (mode_t mode) { | ||
| 351 | if ( S_ISREG(mode) ) | ||
| 352 | return "file"; | ||
| 353 | else if ( S_ISDIR(mode) ) | ||
| 354 | return "directory"; | ||
| 355 | else if ( S_ISLNK(mode) ) | ||
| 356 | return "link"; | ||
| 357 | else if ( S_ISSOCK(mode) ) | ||
| 358 | return "socket"; | ||
| 359 | else if ( S_ISFIFO(mode) ) | ||
| 360 | return "named pipe"; | ||
| 361 | else if ( S_ISCHR(mode) ) | ||
| 362 | return "char device"; | ||
| 363 | else if ( S_ISBLK(mode) ) | ||
| 364 | return "block device"; | ||
| 365 | else | ||
| 366 | return "other"; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | /* | ||
| 371 | ** Convert a struct timespec to a Lua table. | ||
| 372 | */ | ||
| 373 | static lua_Number time2number (struct timespec t) { | ||
| 374 | return (lua_Number)t.tv_sec + (lua_Number)t.tv_nsec / 1000000000.0; | ||
| 375 | } | ||
| 376 | |||
| 377 | |||
| 378 | /* | ||
| 379 | ** Get file information | ||
| 380 | */ | ||
| 381 | static int file_info (lua_State *L) { | ||
| 382 | struct stat info; | ||
| 383 | const char *file = luaL_checkstring (L, 1); | ||
| 384 | |||
| 385 | if (stat(file, &info)) { | ||
| 386 | lua_pushnil (L); | ||
| 387 | lua_pushfstring (L, "cannot obtain information from file `%s'", file); | ||
| 388 | return 2; | ||
| 389 | } | ||
| 390 | lua_newtable (L); | ||
| 391 | /* device inode resides on */ | ||
| 392 | lua_pushliteral (L, "dev"); | ||
| 393 | lua_pushnumber (L, (lua_Number)info.st_dev); | ||
| 394 | lua_rawset (L, -3); | ||
| 395 | /* inode's number */ | ||
| 396 | lua_pushliteral (L, "ino"); | ||
| 397 | lua_pushnumber (L, (lua_Number)info.st_ino); | ||
| 398 | lua_rawset (L, -3); | ||
| 399 | /* inode protection mode */ | ||
| 400 | lua_pushliteral (L, "mode"); | ||
| 401 | lua_pushstring (L, mode2string (info.st_mode)); | ||
| 402 | lua_rawset (L, -3); | ||
| 403 | /* number or hard links to the file */ | ||
| 404 | lua_pushliteral (L, "nlink"); | ||
| 405 | lua_pushnumber (L, (lua_Number)info.st_nlink); | ||
| 406 | lua_rawset (L, -3); | ||
| 407 | /* user-id of owner */ | ||
| 408 | lua_pushliteral (L, "uid"); | ||
| 409 | lua_pushnumber (L, (lua_Number)info.st_uid); | ||
| 410 | lua_rawset (L, -3); | ||
| 411 | /* group-id of owner */ | ||
| 412 | lua_pushliteral (L, "gid"); | ||
| 413 | lua_pushnumber (L, (lua_Number)info.st_gid); | ||
| 414 | lua_rawset (L, -3); | ||
| 415 | /* device type, for special file inode */ | ||
| 416 | lua_pushliteral (L, "rdev"); | ||
| 417 | lua_pushnumber (L, (lua_Number)info.st_rdev); | ||
| 418 | lua_rawset (L, -3); | ||
| 419 | /* time of last access */ | ||
| 420 | lua_pushliteral (L, "access"); | ||
| 421 | lua_pushnumber (L, time2number (info.st_atimespec)); | ||
| 422 | lua_rawset (L, -3); | ||
| 423 | /* time of last data modification */ | ||
| 424 | lua_pushliteral (L, "modification"); | ||
| 425 | lua_pushnumber (L, time2number (info.st_mtimespec)); | ||
| 426 | lua_rawset (L, -3); | ||
| 427 | /* time of last file status change */ | ||
| 428 | lua_pushliteral (L, "change"); | ||
| 429 | lua_pushnumber (L, time2number (info.st_ctimespec)); | ||
| 430 | lua_rawset (L, -3); | ||
| 431 | /* file size, in bytes */ | ||
| 432 | lua_pushliteral (L, "size"); | ||
| 433 | lua_pushnumber (L, (lua_Number)info.st_size); | ||
| 434 | lua_rawset (L, -3); | ||
| 435 | /* blocks allocated for file */ | ||
| 436 | lua_pushliteral (L, "blocks"); | ||
| 437 | lua_pushnumber (L, (lua_Number)info.st_blocks); | ||
| 438 | lua_rawset (L, -3); | ||
| 439 | /* optimal file system I/O blocksize */ | ||
| 440 | lua_pushliteral (L, "blksize"); | ||
| 441 | lua_pushnumber (L, (lua_Number)info.st_blksize); | ||
| 442 | lua_rawset (L, -3); | ||
| 443 | /* user defined flags for file */ | ||
| 444 | lua_pushliteral (L, "flags"); | ||
| 445 | lua_pushnumber (L, (lua_Number)info.st_flags); | ||
| 446 | lua_rawset (L, -3); | ||
| 447 | /* file generation number */ | ||
| 448 | lua_pushliteral (L, "gen"); | ||
| 449 | lua_pushnumber (L, (lua_Number)info.st_gen); | ||
| 450 | lua_rawset (L, -3); | ||
| 451 | |||
| 452 | return 1; | ||
| 453 | } | ||
| 454 | |||
| 455 | |||
| 338 | static const struct luaL_reg fslib[] = { | 456 | static const struct luaL_reg fslib[] = { |
| 457 | {"attributes", file_info}, | ||
| 339 | {"chdir", change_dir}, | 458 | {"chdir", change_dir}, |
| 340 | {"currentdir", get_dir}, | 459 | {"currentdir", get_dir}, |
| 341 | {"dir", dir_iter_factory}, | 460 | {"dir", dir_iter_factory}, |
