Software /
code /
prosody
Comparison
util-src/pposix.c @ 8012:e898c8fda986
util.pposix: Remove fallocate
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 28 Feb 2017 13:26:05 +0100 |
parent | 8010:49feb0da29e1 |
child | 9028:d4c2a3060e7e |
child | 9278:8f9e18d4fe50 |
comparison
equal
deleted
inserted
replaced
8011:f8ba814fe029 | 8012:e898c8fda986 |
---|---|
11 /* | 11 /* |
12 * pposix.c | 12 * pposix.c |
13 * POSIX support functions for Lua | 13 * POSIX support functions for Lua |
14 */ | 14 */ |
15 | 15 |
16 #define MODULE_VERSION "0.3.7" | 16 #define MODULE_VERSION "0.4.0" |
17 | 17 |
18 | 18 |
19 #if defined(__linux__) | 19 #if defined(__linux__) |
20 #define _GNU_SOURCE | 20 #define _GNU_SOURCE |
21 #else | 21 #else |
728 lua_setfield(L, -2, "returnable"); | 728 lua_setfield(L, -2, "returnable"); |
729 return 1; | 729 return 1; |
730 } | 730 } |
731 #endif | 731 #endif |
732 | 732 |
733 /* File handle extraction blatantly stolen from | |
734 * https://github.com/rrthomas/luaposix/blob/master/lposix.c#L631 | |
735 * */ | |
736 | |
737 int lc_fallocate(lua_State *L) { | |
738 int ret; | |
739 off_t offset, len; | |
740 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE); | |
741 | |
742 if(f == NULL) { | |
743 return luaL_error(L, "attempt to use a closed file"); | |
744 } | |
745 | |
746 offset = luaL_checkinteger(L, 2); | |
747 len = luaL_checkinteger(L, 3); | |
748 | |
749 #if defined(__linux__) | |
750 errno = 0; | |
751 ret = fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len); | |
752 | |
753 if(ret == 0) { | |
754 lua_pushboolean(L, 1); | |
755 return 1; | |
756 } | |
757 | |
758 /* Some old versions of Linux apparently use the return value instead of errno */ | |
759 if(errno == 0) { | |
760 errno = ret; | |
761 } | |
762 | |
763 if(errno != ENOSYS && errno != EOPNOTSUPP) { | |
764 lua_pushnil(L); | |
765 lua_pushstring(L, strerror(errno)); | |
766 return 2; | |
767 } | |
768 | |
769 #endif | |
770 | |
771 ret = posix_fallocate(fileno(f), offset, len); | |
772 | |
773 if(ret == 0) { | |
774 lua_pushboolean(L, 1); | |
775 return 1; | |
776 } else { | |
777 lua_pushnil(L); | |
778 lua_pushstring(L, strerror(ret)); | |
779 | |
780 /* posix_fallocate() can leave a bunch of NULs at the end, so we cut that | |
781 * this assumes that offset == length of the file */ | |
782 if(ftruncate(fileno(f), offset) != 0) { | |
783 lua_pushstring(L, strerror(errno)); | |
784 return 3; | |
785 } | |
786 | |
787 return 2; | |
788 } | |
789 } | |
790 | |
791 /* | 733 /* |
792 * Append some data to a file handle | 734 * Append some data to a file handle |
793 * Attempt to allocate space first | 735 * Attempt to allocate space first |
794 * Truncate to original size on failure | 736 * Truncate to original size on failure |
795 */ | 737 */ |
887 | 829 |
888 #ifdef WITH_MALLINFO | 830 #ifdef WITH_MALLINFO |
889 { "meminfo", lc_meminfo }, | 831 { "meminfo", lc_meminfo }, |
890 #endif | 832 #endif |
891 | 833 |
892 { "fallocate", lc_fallocate }, | |
893 { "atomic_append", lc_atomic_append }, | 834 { "atomic_append", lc_atomic_append }, |
894 | 835 |
895 { NULL, NULL } | 836 { NULL, NULL } |
896 }; | 837 }; |
897 | 838 |