diff options
-rw-r--r-- | src/lfs.c | 65 | ||||
-rw-r--r-- | tests/test.lua | 7 |
2 files changed, 44 insertions, 28 deletions
@@ -16,9 +16,11 @@ | |||
16 | ** lfs.touch (filepath [, atime [, mtime]]) | 16 | ** lfs.touch (filepath [, atime [, mtime]]) |
17 | ** lfs.unlock (fh) | 17 | ** lfs.unlock (fh) |
18 | ** | 18 | ** |
19 | ** $Id: lfs.c,v 1.49 2008/02/19 20:08:23 mascarenhas Exp $ | 19 | ** $Id: lfs.c,v 1.50 2008/03/25 16:58:29 mascarenhas Exp $ |
20 | */ | 20 | */ |
21 | 21 | ||
22 | #define _LARGEFILE64_SOURCE | ||
23 | |||
22 | #include <errno.h> | 24 | #include <errno.h> |
23 | #include <stdio.h> | 25 | #include <stdio.h> |
24 | #include <string.h> | 26 | #include <string.h> |
@@ -73,11 +75,15 @@ typedef struct dir_data { | |||
73 | 75 | ||
74 | #ifdef _WIN32 | 76 | #ifdef _WIN32 |
75 | #define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m)) | 77 | #define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m)) |
78 | #define STAT_STRUCT struct _stat64 | ||
79 | #define STAT_FUNC _stat64 | ||
76 | #else | 80 | #else |
77 | #define _O_TEXT 0 | 81 | #define _O_TEXT 0 |
78 | #define _O_BINARY 0 | 82 | #define _O_BINARY 0 |
79 | #define lfs_setmode(L,file,m) ((void)((void)file,m), \ | 83 | #define lfs_setmode(L,file,m) ((void)((void)file,m), \ |
80 | luaL_error(L, LUA_QL("setmode") " not supported on non Windows platforms"), -1) | 84 | luaL_error(L, LUA_QL("setmode") " not supported on this platform"), -1) |
85 | #define STAT_STRUCT struct stat64 | ||
86 | #define STAT_FUNC stat64 | ||
81 | #endif | 87 | #endif |
82 | 88 | ||
83 | /* | 89 | /* |
@@ -203,11 +209,17 @@ static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { | |||
203 | return 3; | 209 | return 3; |
204 | } | 210 | } |
205 | } | 211 | } |
212 | #else | ||
213 | static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { | ||
214 | lua_pushboolean(L, 0); | ||
215 | lua_pushliteral(L, "setmode not supported on this platform"); | ||
216 | return 2; | ||
217 | } | ||
218 | #endif | ||
206 | 219 | ||
207 | static int lfs_f_setmode(lua_State *L) { | 220 | static int lfs_f_setmode(lua_State *L) { |
208 | return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); | 221 | return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); |
209 | } | 222 | } |
210 | #endif | ||
211 | 223 | ||
212 | /* | 224 | /* |
213 | ** Locks a file. | 225 | ** Locks a file. |
@@ -475,67 +487,67 @@ static int file_utime (lua_State *L) { | |||
475 | 487 | ||
476 | 488 | ||
477 | /* inode protection mode */ | 489 | /* inode protection mode */ |
478 | static void push_st_mode (lua_State *L, struct stat *info) { | 490 | static void push_st_mode (lua_State *L, STAT_STRUCT *info) { |
479 | lua_pushstring (L, mode2string (info->st_mode)); | 491 | lua_pushstring (L, mode2string (info->st_mode)); |
480 | } | 492 | } |
481 | /* device inode resides on */ | 493 | /* device inode resides on */ |
482 | static void push_st_dev (lua_State *L, struct stat *info) { | 494 | static void push_st_dev (lua_State *L, STAT_STRUCT *info) { |
483 | lua_pushnumber (L, (lua_Number)info->st_dev); | 495 | lua_pushnumber (L, (lua_Number)info->st_dev); |
484 | } | 496 | } |
485 | /* inode's number */ | 497 | /* inode's number */ |
486 | static void push_st_ino (lua_State *L, struct stat *info) { | 498 | static void push_st_ino (lua_State *L, STAT_STRUCT *info) { |
487 | lua_pushnumber (L, (lua_Number)info->st_ino); | 499 | lua_pushnumber (L, (lua_Number)info->st_ino); |
488 | } | 500 | } |
489 | /* number of hard links to the file */ | 501 | /* number of hard links to the file */ |
490 | static void push_st_nlink (lua_State *L, struct stat *info) { | 502 | static void push_st_nlink (lua_State *L, STAT_STRUCT *info) { |
491 | lua_pushnumber (L, (lua_Number)info->st_nlink); | 503 | lua_pushnumber (L, (lua_Number)info->st_nlink); |
492 | } | 504 | } |
493 | /* user-id of owner */ | 505 | /* user-id of owner */ |
494 | static void push_st_uid (lua_State *L, struct stat *info) { | 506 | static void push_st_uid (lua_State *L, STAT_STRUCT *info) { |
495 | lua_pushnumber (L, (lua_Number)info->st_uid); | 507 | lua_pushnumber (L, (lua_Number)info->st_uid); |
496 | } | 508 | } |
497 | /* group-id of owner */ | 509 | /* group-id of owner */ |
498 | static void push_st_gid (lua_State *L, struct stat *info) { | 510 | static void push_st_gid (lua_State *L, STAT_STRUCT *info) { |
499 | lua_pushnumber (L, (lua_Number)info->st_gid); | 511 | lua_pushnumber (L, (lua_Number)info->st_gid); |
500 | } | 512 | } |
501 | /* device type, for special file inode */ | 513 | /* device type, for special file inode */ |
502 | static void push_st_rdev (lua_State *L, struct stat *info) { | 514 | static void push_st_rdev (lua_State *L, STAT_STRUCT *info) { |
503 | lua_pushnumber (L, (lua_Number)info->st_rdev); | 515 | lua_pushnumber (L, (lua_Number)info->st_rdev); |
504 | } | 516 | } |
505 | /* time of last access */ | 517 | /* time of last access */ |
506 | static void push_st_atime (lua_State *L, struct stat *info) { | 518 | static void push_st_atime (lua_State *L, STAT_STRUCT *info) { |
507 | lua_pushnumber (L, info->st_atime); | 519 | lua_pushnumber (L, info->st_atime); |
508 | } | 520 | } |
509 | /* time of last data modification */ | 521 | /* time of last data modification */ |
510 | static void push_st_mtime (lua_State *L, struct stat *info) { | 522 | static void push_st_mtime (lua_State *L, STAT_STRUCT *info) { |
511 | lua_pushnumber (L, info->st_mtime); | 523 | lua_pushnumber (L, info->st_mtime); |
512 | } | 524 | } |
513 | /* time of last file status change */ | 525 | /* time of last file status change */ |
514 | static void push_st_ctime (lua_State *L, struct stat *info) { | 526 | static void push_st_ctime (lua_State *L, STAT_STRUCT *info) { |
515 | lua_pushnumber (L, info->st_ctime); | 527 | lua_pushnumber (L, info->st_ctime); |
516 | } | 528 | } |
517 | /* file size, in bytes */ | 529 | /* file size, in bytes */ |
518 | static void push_st_size (lua_State *L, struct stat *info) { | 530 | static void push_st_size (lua_State *L, STAT_STRUCT *info) { |
519 | lua_pushnumber (L, (lua_Number)info->st_size); | 531 | lua_pushnumber (L, (lua_Number)info->st_size); |
520 | } | 532 | } |
521 | #ifndef _WIN32 | 533 | #ifndef _WIN32 |
522 | /* blocks allocated for file */ | 534 | /* blocks allocated for file */ |
523 | static void push_st_blocks (lua_State *L, struct stat *info) { | 535 | static void push_st_blocks (lua_State *L, STAT_STRUCT *info) { |
524 | lua_pushnumber (L, (lua_Number)info->st_blocks); | 536 | lua_pushnumber (L, (lua_Number)info->st_blocks); |
525 | } | 537 | } |
526 | /* optimal file system I/O blocksize */ | 538 | /* optimal file system I/O blocksize */ |
527 | static void push_st_blksize (lua_State *L, struct stat *info) { | 539 | static void push_st_blksize (lua_State *L, STAT_STRUCT *info) { |
528 | lua_pushnumber (L, (lua_Number)info->st_blksize); | 540 | lua_pushnumber (L, (lua_Number)info->st_blksize); |
529 | } | 541 | } |
530 | #endif | 542 | #endif |
531 | static void push_invalid (lua_State *L, struct stat *info) { | 543 | static void push_invalid (lua_State *L, STAT_STRUCT *info) { |
532 | luaL_error(L, "invalid attribute name"); | 544 | luaL_error(L, "invalid attribute name"); |
533 | #ifndef _WIN32 | 545 | #ifndef _WIN32 |
534 | info->st_blksize = 0; /* never reached */ | 546 | info->st_blksize = 0; /* never reached */ |
535 | #endif | 547 | #endif |
536 | } | 548 | } |
537 | 549 | ||
538 | typedef void (*_push_function) (lua_State *L, struct stat *info); | 550 | typedef void (*_push_function) (lua_State *L, STAT_STRUCT *info); |
539 | 551 | ||
540 | struct _stat_members { | 552 | struct _stat_members { |
541 | const char *name; | 553 | const char *name; |
@@ -564,9 +576,9 @@ struct _stat_members members[] = { | |||
564 | /* | 576 | /* |
565 | ** Get file or symbolic link information | 577 | ** Get file or symbolic link information |
566 | */ | 578 | */ |
567 | static int _file_info_ (lua_State *L, int (*st)(const char*, struct stat*)) { | 579 | static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { |
568 | int i; | 580 | int i; |
569 | struct stat info; | 581 | STAT_STRUCT info; |
570 | const char *file = luaL_checkstring (L, 1); | 582 | const char *file = luaL_checkstring (L, 1); |
571 | 583 | ||
572 | if (st(file, &info)) { | 584 | if (st(file, &info)) { |
@@ -606,7 +618,7 @@ static int _file_info_ (lua_State *L, int (*st)(const char*, struct stat*)) { | |||
606 | ** Get file information using stat. | 618 | ** Get file information using stat. |
607 | */ | 619 | */ |
608 | static int file_info (lua_State *L) { | 620 | static int file_info (lua_State *L) { |
609 | return _file_info_ (L, stat); | 621 | return _file_info_ (L, STAT_FUNC); |
610 | } | 622 | } |
611 | 623 | ||
612 | 624 | ||
@@ -615,7 +627,13 @@ static int file_info (lua_State *L) { | |||
615 | */ | 627 | */ |
616 | #ifndef _WIN32 | 628 | #ifndef _WIN32 |
617 | static int link_info (lua_State *L) { | 629 | static int link_info (lua_State *L) { |
618 | return _file_info_ (L, lstat); | 630 | return _file_info_ (L, lstat64); |
631 | } | ||
632 | #else | ||
633 | static int link_info (lua_State *L) { | ||
634 | lua_pushboolean(L, 0); | ||
635 | lua_pushliteral(L, "symlinkattributes not supported on this platform"); | ||
636 | return 2; | ||
619 | } | 637 | } |
620 | #endif | 638 | #endif |
621 | 639 | ||
@@ -644,11 +662,8 @@ static const struct luaL_reg fslib[] = { | |||
644 | {"lock", file_lock}, | 662 | {"lock", file_lock}, |
645 | {"mkdir", make_dir}, | 663 | {"mkdir", make_dir}, |
646 | {"rmdir", remove_dir}, | 664 | {"rmdir", remove_dir}, |
647 | #ifndef _WIN32 | ||
648 | {"symlinkattributes", link_info}, | 665 | {"symlinkattributes", link_info}, |
649 | #else | ||
650 | {"setmode", lfs_f_setmode}, | 666 | {"setmode", lfs_f_setmode}, |
651 | #endif | ||
652 | {"touch", file_utime}, | 667 | {"touch", file_utime}, |
653 | {"unlock", file_unlock}, | 668 | {"unlock", file_unlock}, |
654 | {NULL, NULL}, | 669 | {NULL, NULL}, |
diff --git a/tests/test.lua b/tests/test.lua index 882838b..1fd6157 100644 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -69,7 +69,8 @@ local new_att = assert (lfs.attributes (tmpfile)) | |||
69 | assert (new_att.access == testdate2, "could not set access time") | 69 | assert (new_att.access == testdate2, "could not set access time") |
70 | assert (new_att.modification == testdate1, "could not set modification time") | 70 | assert (new_att.modification == testdate1, "could not set modification time") |
71 | 71 | ||
72 | if lfs.symlinkattributes then | 72 | local res, err = lfs.symlinkattributes(tmpfile) |
73 | if err ~= "symlinkattributes not supported on this platform" then | ||
73 | -- Checking symbolic link information (does not work in Windows) | 74 | -- Checking symbolic link information (does not work in Windows) |
74 | assert (os.execute ("ln -s "..tmpfile.." _a_link_for_test_")) | 75 | assert (os.execute ("ln -s "..tmpfile.." _a_link_for_test_")) |
75 | assert (lfs.attributes"_a_link_for_test_".mode == "file") | 76 | assert (lfs.attributes"_a_link_for_test_".mode == "file") |
@@ -81,9 +82,9 @@ if lfs.setmode then | |||
81 | -- Checking text/binary modes (works only in Windows) | 82 | -- Checking text/binary modes (works only in Windows) |
82 | local f = io.open(tmpfile, "w") | 83 | local f = io.open(tmpfile, "w") |
83 | local result, mode = lfs.setmode(f, "binary") | 84 | local result, mode = lfs.setmode(f, "binary") |
84 | assert(result and mode == "text") | 85 | assert((result and mode == "text") or (not result and mode == "setmode not supported on this platform")) |
85 | result, mode = lfs.setmode(f, "text") | 86 | result, mode = lfs.setmode(f, "text") |
86 | assert(result and mode == "binary") | 87 | assert((result and mode == "binary") or (not result and mode == "setmode not supported on this platform")) |
87 | f:close() | 88 | f:close() |
88 | end | 89 | end |
89 | 90 | ||