diff options
| author | tomas <tomas> | 2005-08-18 01:00:44 +0000 |
|---|---|---|
| committer | tomas <tomas> | 2005-08-18 01:00:44 +0000 |
| commit | e206fd0e63bc98c2804d4e85c4d5376f9d2e82d3 (patch) | |
| tree | 21d95b6bcb73bd4977746da2d71a25080ad3ee80 /src | |
| parent | 87b373b3d80e234c482e6276a20be2d329d4b795 (diff) | |
| download | luafilesystem-e206fd0e63bc98c2804d4e85c4d5376f9d2e82d3.tar.gz luafilesystem-e206fd0e63bc98c2804d4e85c4d5376f9d2e82d3.tar.bz2 luafilesystem-e206fd0e63bc98c2804d4e85c4d5376f9d2e82d3.zip | |
Upgrading lfs.attributes so that it could receive an extra parameter.
If it is a table, then it is used instead of creating a new one.
If it is a string, then it is used to indicate the attribute which will
be returned (only one parameter is accepted).
Diffstat (limited to 'src')
| -rw-r--r-- | src/lfs.c | 162 |
1 files changed, 105 insertions, 57 deletions
| @@ -11,7 +11,7 @@ | |||
| 11 | ** lfs.touch (filepath [, atime [, mtime]]) | 11 | ** lfs.touch (filepath [, atime [, mtime]]) |
| 12 | ** lfs.unlock (fh) | 12 | ** lfs.unlock (fh) |
| 13 | ** | 13 | ** |
| 14 | ** $Id: lfs.c,v 1.26 2005/08/16 13:47:58 uid20006 Exp $ | 14 | ** $Id: lfs.c,v 1.27 2005/08/18 01:00:44 tomas Exp $ |
| 15 | */ | 15 | */ |
| 16 | 16 | ||
| 17 | #include <errno.h> | 17 | #include <errno.h> |
| @@ -277,9 +277,9 @@ static int remove_dir (lua_State *L) { | |||
| 277 | */ | 277 | */ |
| 278 | static int dir_iter (lua_State *L) { | 278 | static int dir_iter (lua_State *L) { |
| 279 | dir_data *d = (dir_data *)lua_touserdata (L, lua_upvalueindex (1)); | 279 | dir_data *d = (dir_data *)lua_touserdata (L, lua_upvalueindex (1)); |
| 280 | luaL_argcheck (L, !d->closed, 1, "closed directory"); | ||
| 280 | #ifdef _WIN32 | 281 | #ifdef _WIN32 |
| 281 | struct _finddata_t c_file; | 282 | struct _finddata_t c_file; |
| 282 | luaL_argcheck (L, !d->closed, 1, "closed directory"); | ||
| 283 | if (d->hFile == 0L) { /* first entry */ | 283 | if (d->hFile == 0L) { /* first entry */ |
| 284 | if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) { | 284 | if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) { |
| 285 | lua_pushnil (L); | 285 | lua_pushnil (L); |
| @@ -302,7 +302,6 @@ static int dir_iter (lua_State *L) { | |||
| 302 | } | 302 | } |
| 303 | #else | 303 | #else |
| 304 | struct dirent *entry; | 304 | struct dirent *entry; |
| 305 | luaL_argcheck (L, !d->closed, 1, "closed directory"); | ||
| 306 | if ((entry = readdir (d->dir)) != NULL) { | 305 | if ((entry = readdir (d->dir)) != NULL) { |
| 307 | lua_pushstring (L, entry->d_name); | 306 | lua_pushstring (L, entry->d_name); |
| 308 | return 1; | 307 | return 1; |
| @@ -451,10 +450,92 @@ static int file_utime (lua_State *L) { | |||
| 451 | } | 450 | } |
| 452 | 451 | ||
| 453 | 452 | ||
| 453 | /* inode protection mode */ | ||
| 454 | static void push_st_mode (lua_State *L, struct stat *info) { | ||
| 455 | lua_pushstring (L, mode2string (info->st_mode)); | ||
| 456 | } | ||
| 457 | /* device inode resides on */ | ||
| 458 | static void push_st_dev (lua_State *L, struct stat *info) { | ||
| 459 | lua_pushnumber (L, (lua_Number)info->st_dev); | ||
| 460 | } | ||
| 461 | /* inode's number */ | ||
| 462 | static void push_st_ino (lua_State *L, struct stat *info) { | ||
| 463 | lua_pushnumber (L, (lua_Number)info->st_ino); | ||
| 464 | } | ||
| 465 | /* number of hard links to the file */ | ||
| 466 | static void push_st_nlink (lua_State *L, struct stat *info) { | ||
| 467 | lua_pushnumber (L, (lua_Number)info->st_nlink); | ||
| 468 | } | ||
| 469 | /* user-id of owner */ | ||
| 470 | static void push_st_uid (lua_State *L, struct stat *info) { | ||
| 471 | lua_pushnumber (L, (lua_Number)info->st_uid); | ||
| 472 | } | ||
| 473 | /* group-id of owner */ | ||
| 474 | static void push_st_gid (lua_State *L, struct stat *info) { | ||
| 475 | lua_pushnumber (L, (lua_Number)info->st_gid); | ||
| 476 | } | ||
| 477 | /* device type, for special file inode */ | ||
| 478 | static void push_st_rdev (lua_State *L, struct stat *info) { | ||
| 479 | lua_pushnumber (L, (lua_Number)info->st_rdev); | ||
| 480 | } | ||
| 481 | /* time of last access */ | ||
| 482 | static void push_st_atime (lua_State *L, struct stat *info) { | ||
| 483 | lua_pushnumber (L, info->st_atime); | ||
| 484 | } | ||
| 485 | /* time of last data modification */ | ||
| 486 | static void push_st_mtime (lua_State *L, struct stat *info) { | ||
| 487 | lua_pushnumber (L, info->st_mtime); | ||
| 488 | } | ||
| 489 | /* time of last file status change */ | ||
| 490 | static void push_st_ctime (lua_State *L, struct stat *info) { | ||
| 491 | lua_pushnumber (L, info->st_ctime); | ||
| 492 | } | ||
| 493 | /* file size, in bytes */ | ||
| 494 | static void push_st_size (lua_State *L, struct stat *info) { | ||
| 495 | lua_pushnumber (L, (lua_Number)info->st_size); | ||
| 496 | } | ||
| 497 | #ifndef _WIN32 | ||
| 498 | /* blocks allocated for file */ | ||
| 499 | static void push_st_blocks (lua_State *L, struct stat *info) { | ||
| 500 | lua_pushnumber (L, (lua_Number)info->st_blocks); | ||
| 501 | } | ||
| 502 | /* optimal file system I/O blocksize */ | ||
| 503 | static void push_st_blksize (lua_State *L, struct stat *info) { | ||
| 504 | lua_pushnumber (L, (lua_Number)info->st_blksize); | ||
| 505 | } | ||
| 506 | #endif | ||
| 507 | |||
| 508 | typedef void (*_push_function) (lua_State *L, struct stat *info); | ||
| 509 | |||
| 510 | struct _stat_members { | ||
| 511 | const char *name; | ||
| 512 | _push_function push; | ||
| 513 | }; | ||
| 514 | |||
| 515 | struct _stat_members members[] = { | ||
| 516 | { "mode", push_st_mode }, | ||
| 517 | { "dev", push_st_dev }, | ||
| 518 | { "ino", push_st_ino }, | ||
| 519 | { "nlink", push_st_nlink }, | ||
| 520 | { "uid", push_st_uid }, | ||
| 521 | { "gid", push_st_gid }, | ||
| 522 | { "rdev", push_st_rdev }, | ||
| 523 | { "access", push_st_atime }, | ||
| 524 | { "modification", push_st_mtime }, | ||
| 525 | { "change", push_st_ctime }, | ||
| 526 | { "size", push_st_size }, | ||
| 527 | #ifndef _WIN32 | ||
| 528 | { "blocks", push_st_blocks }, | ||
| 529 | { "blksize", push_st_blksize }, | ||
| 530 | #endif | ||
| 531 | { NULL, NULL } | ||
| 532 | }; | ||
| 533 | |||
| 454 | /* | 534 | /* |
| 455 | ** Get file information | 535 | ** Get file information |
| 456 | */ | 536 | */ |
| 457 | static int file_info (lua_State *L) { | 537 | static int file_info (lua_State *L) { |
| 538 | int i; | ||
| 458 | struct stat info; | 539 | struct stat info; |
| 459 | const char *file = luaL_checkstring (L, 1); | 540 | const char *file = luaL_checkstring (L, 1); |
| 460 | 541 | ||
| @@ -463,62 +544,29 @@ static int file_info (lua_State *L) { | |||
| 463 | lua_pushfstring (L, "cannot obtain information from file `%s'", file); | 544 | lua_pushfstring (L, "cannot obtain information from file `%s'", file); |
| 464 | return 2; | 545 | return 2; |
| 465 | } | 546 | } |
| 466 | lua_newtable (L); | 547 | if (lua_isstring (L, 2)) { |
| 467 | /* device inode resides on */ | 548 | int v; |
| 468 | lua_pushliteral (L, "dev"); | 549 | const char *member = lua_tostring (L, 2); |
| 469 | lua_pushnumber (L, (lua_Number)info.st_dev); | 550 | if (strcmp (member, "mode") == 0) v = 0; |
| 470 | lua_rawset (L, -3); | ||
| 471 | /* inode's number */ | ||
| 472 | lua_pushliteral (L, "ino"); | ||
| 473 | lua_pushnumber (L, (lua_Number)info.st_ino); | ||
| 474 | lua_rawset (L, -3); | ||
| 475 | /* inode protection mode */ | ||
| 476 | lua_pushliteral (L, "mode"); | ||
| 477 | lua_pushstring (L, mode2string (info.st_mode)); | ||
| 478 | lua_rawset (L, -3); | ||
| 479 | /* number of hard links to the file */ | ||
| 480 | lua_pushliteral (L, "nlink"); | ||
| 481 | lua_pushnumber (L, (lua_Number)info.st_nlink); | ||
| 482 | lua_rawset (L, -3); | ||
| 483 | /* user-id of owner */ | ||
| 484 | lua_pushliteral (L, "uid"); | ||
| 485 | lua_pushnumber (L, (lua_Number)info.st_uid); | ||
| 486 | lua_rawset (L, -3); | ||
| 487 | /* group-id of owner */ | ||
| 488 | lua_pushliteral (L, "gid"); | ||
| 489 | lua_pushnumber (L, (lua_Number)info.st_gid); | ||
| 490 | lua_rawset (L, -3); | ||
| 491 | /* device type, for special file inode */ | ||
| 492 | lua_pushliteral (L, "rdev"); | ||
| 493 | lua_pushnumber (L, (lua_Number)info.st_rdev); | ||
| 494 | lua_rawset (L, -3); | ||
| 495 | /* time of last access */ | ||
| 496 | lua_pushliteral (L, "access"); | ||
| 497 | lua_pushnumber (L, info.st_atime); | ||
| 498 | lua_rawset (L, -3); | ||
| 499 | /* time of last data modification */ | ||
| 500 | lua_pushliteral (L, "modification"); | ||
| 501 | lua_pushnumber (L, info.st_mtime); | ||
| 502 | lua_rawset (L, -3); | ||
| 503 | /* time of last file status change */ | ||
| 504 | lua_pushliteral (L, "change"); | ||
| 505 | lua_pushnumber (L, info.st_ctime); | ||
| 506 | lua_rawset (L, -3); | ||
| 507 | /* file size, in bytes */ | ||
| 508 | lua_pushliteral (L, "size"); | ||
| 509 | lua_pushnumber (L, (lua_Number)info.st_size); | ||
| 510 | lua_rawset (L, -3); | ||
| 511 | #ifndef _WIN32 | 551 | #ifndef _WIN32 |
| 512 | /* blocks allocated for file */ | 552 | else if (strcmp (member, "blksize") == 0) v = 12; |
| 513 | lua_pushliteral (L, "blocks"); | ||
| 514 | lua_pushnumber (L, (lua_Number)info.st_blocks); | ||
| 515 | lua_rawset (L, -3); | ||
| 516 | /* optimal file system I/O blocksize */ | ||
| 517 | lua_pushliteral (L, "blksize"); | ||
| 518 | lua_pushnumber (L, (lua_Number)info.st_blksize); | ||
| 519 | lua_rawset (L, -3); | ||
| 520 | #endif | 553 | #endif |
| 521 | 554 | else /* look for member */ | |
| 555 | for (v = 1; members[v].name; v++) | ||
| 556 | if (*members[v].name == *member) | ||
| 557 | break; | ||
| 558 | /* push member value and return */ | ||
| 559 | members[v].push (L, &info); | ||
| 560 | return 1; | ||
| 561 | } else if (!lua_istable (L, 2)) | ||
| 562 | /* creates a table if none is given */ | ||
| 563 | lua_newtable (L); | ||
| 564 | /* stores all members in table on top of the stack */ | ||
| 565 | for (i = 0; members[i].name; i++) { | ||
| 566 | lua_pushstring (L, members[i].name); | ||
| 567 | members[i].push (L, &info); | ||
| 568 | lua_rawset (L, -3); | ||
| 569 | } | ||
| 522 | return 1; | 570 | return 1; |
| 523 | } | 571 | } |
| 524 | 572 | ||
