Software /
code /
prosody
Comparison
util-src/pposix.c @ 10921:6eb5d2bb11af
util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Actually just an alias of pushnil, but it does make it more obvious
where the failure conditions are, which is good for readability.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 07 Jun 2020 02:25:56 +0200 |
parent | 10799:763bb2ce3f60 |
child | 11656:c368b4f6ee04 |
comparison
equal
deleted
inserted
replaced
10920:c171b4c59bd1 | 10921:6eb5d2bb11af |
---|---|
61 #if (LUA_VERSION_NUM == 501) | 61 #if (LUA_VERSION_NUM == 501) |
62 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) | 62 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) |
63 #endif | 63 #endif |
64 #if (LUA_VERSION_NUM < 503) | 64 #if (LUA_VERSION_NUM < 503) |
65 #define lua_isinteger(L, n) lua_isnumber(L, n) | 65 #define lua_isinteger(L, n) lua_isnumber(L, n) |
66 #endif | |
67 #if (LUA_VERSION_NUM < 504) | |
68 #define luaL_pushfail lua_pushnil | |
66 #endif | 69 #endif |
67 | 70 |
68 #include <fcntl.h> | 71 #include <fcntl.h> |
69 #if defined(__linux__) | 72 #if defined(__linux__) |
70 #include <linux/falloc.h> | 73 #include <linux/falloc.h> |
411 int ret; | 414 int ret; |
412 gid_t gid; | 415 gid_t gid; |
413 struct passwd *p; | 416 struct passwd *p; |
414 | 417 |
415 if(!lua_isstring(L, 1)) { | 418 if(!lua_isstring(L, 1)) { |
416 lua_pushnil(L); | 419 luaL_pushfail(L); |
417 lua_pushstring(L, "invalid-username"); | 420 lua_pushstring(L, "invalid-username"); |
418 return 2; | 421 return 2; |
419 } | 422 } |
420 | 423 |
421 p = getpwnam(lua_tostring(L, 1)); | 424 p = getpwnam(lua_tostring(L, 1)); |
422 | 425 |
423 if(!p) { | 426 if(!p) { |
424 lua_pushnil(L); | 427 luaL_pushfail(L); |
425 lua_pushstring(L, "no-such-user"); | 428 lua_pushstring(L, "no-such-user"); |
426 return 2; | 429 return 2; |
427 } | 430 } |
428 | 431 |
429 if(lua_gettop(L) < 2) { | 432 if(lua_gettop(L) < 2) { |
438 case LUA_TNUMBER: | 441 case LUA_TNUMBER: |
439 gid = lua_tointeger(L, 2); | 442 gid = lua_tointeger(L, 2); |
440 break; | 443 break; |
441 | 444 |
442 default: | 445 default: |
443 lua_pushnil(L); | 446 luaL_pushfail(L); |
444 lua_pushstring(L, "invalid-gid"); | 447 lua_pushstring(L, "invalid-gid"); |
445 return 2; | 448 return 2; |
446 } | 449 } |
447 | 450 |
448 ret = initgroups(lua_tostring(L, 1), gid); | 451 ret = initgroups(lua_tostring(L, 1), gid); |
449 | 452 |
450 if(ret) { | 453 if(ret) { |
451 switch(errno) { | 454 switch(errno) { |
452 case ENOMEM: | 455 case ENOMEM: |
453 lua_pushnil(L); | 456 luaL_pushfail(L); |
454 lua_pushstring(L, "no-memory"); | 457 lua_pushstring(L, "no-memory"); |
455 break; | 458 break; |
456 | 459 |
457 case EPERM: | 460 case EPERM: |
458 lua_pushnil(L); | 461 luaL_pushfail(L); |
459 lua_pushstring(L, "permission-denied"); | 462 lua_pushstring(L, "permission-denied"); |
460 break; | 463 break; |
461 | 464 |
462 default: | 465 default: |
463 lua_pushnil(L); | 466 luaL_pushfail(L); |
464 lua_pushstring(L, "unknown-error"); | 467 lua_pushstring(L, "unknown-error"); |
465 } | 468 } |
466 } else { | 469 } else { |
467 lua_pushboolean(L, 1); | 470 lua_pushboolean(L, 1); |
468 lua_pushnil(L); | 471 lua_pushnil(L); |
670 | 673 |
671 static int lc_uname(lua_State *L) { | 674 static int lc_uname(lua_State *L) { |
672 struct utsname uname_info; | 675 struct utsname uname_info; |
673 | 676 |
674 if(uname(&uname_info) != 0) { | 677 if(uname(&uname_info) != 0) { |
675 lua_pushnil(L); | 678 luaL_pushfail(L); |
676 lua_pushstring(L, strerror(errno)); | 679 lua_pushstring(L, strerror(errno)); |
677 return 2; | 680 return 2; |
678 } | 681 } |
679 | 682 |
680 lua_createtable(L, 0, 6); | 683 lua_createtable(L, 0, 6); |
700 const char *value; | 703 const char *value; |
701 | 704 |
702 /* If the second argument is nil or nothing, unset the var */ | 705 /* If the second argument is nil or nothing, unset the var */ |
703 if(lua_isnoneornil(L, 2)) { | 706 if(lua_isnoneornil(L, 2)) { |
704 if(unsetenv(var) != 0) { | 707 if(unsetenv(var) != 0) { |
705 lua_pushnil(L); | 708 luaL_pushfail(L); |
706 lua_pushstring(L, strerror(errno)); | 709 lua_pushstring(L, strerror(errno)); |
707 return 2; | 710 return 2; |
708 } | 711 } |
709 | 712 |
710 lua_pushboolean(L, 1); | 713 lua_pushboolean(L, 1); |
712 } | 715 } |
713 | 716 |
714 value = luaL_checkstring(L, 2); | 717 value = luaL_checkstring(L, 2); |
715 | 718 |
716 if(setenv(var, value, 1) != 0) { | 719 if(setenv(var, value, 1) != 0) { |
717 lua_pushnil(L); | 720 luaL_pushfail(L); |
718 lua_pushstring(L, strerror(errno)); | 721 lua_pushstring(L, strerror(errno)); |
719 return 2; | 722 return 2; |
720 } | 723 } |
721 | 724 |
722 lua_pushboolean(L, 1); | 725 lua_pushboolean(L, 1); |
774 /* Ignore and proceed to try to write */ | 777 /* Ignore and proceed to try to write */ |
775 break; | 778 break; |
776 | 779 |
777 case ENOSPC: /* No space left */ | 780 case ENOSPC: /* No space left */ |
778 default: /* Other issues */ | 781 default: /* Other issues */ |
779 lua_pushnil(L); | 782 luaL_pushfail(L); |
780 lua_pushstring(L, strerror(err)); | 783 lua_pushstring(L, strerror(err)); |
781 lua_pushinteger(L, err); | 784 lua_pushinteger(L, err); |
782 return 3; | 785 return 3; |
783 } | 786 } |
784 } | 787 } |
801 if(ftruncate(fileno(f), offset)) { | 804 if(ftruncate(fileno(f), offset)) { |
802 /* The file is now most likely corrupted, throw hard error */ | 805 /* The file is now most likely corrupted, throw hard error */ |
803 return luaL_error(L, "atomic_append() failed in ftruncate(): %s", strerror(errno)); | 806 return luaL_error(L, "atomic_append() failed in ftruncate(): %s", strerror(errno)); |
804 } | 807 } |
805 | 808 |
806 lua_pushnil(L); | 809 luaL_pushfail(L); |
807 lua_pushstring(L, strerror(err)); | 810 lua_pushstring(L, strerror(err)); |
808 lua_pushinteger(L, err); | 811 lua_pushinteger(L, err); |
809 return 3; | 812 return 3; |
810 } | 813 } |
811 | 814 |