Software /
code /
prosody
Comparison
util-src/pposix.c @ 5044:4ef0dbfead53
util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 28 Jul 2012 22:21:10 +0200 |
parent | 4950:02e5e9fa37b8 |
child | 5052:f1157cce5d7a |
comparison
equal
deleted
inserted
replaced
5043:2856e1cfbe95 | 5044:4ef0dbfead53 |
---|---|
30 #include <grp.h> | 30 #include <grp.h> |
31 | 31 |
32 #include <string.h> | 32 #include <string.h> |
33 #include <errno.h> | 33 #include <errno.h> |
34 #include "lua.h" | 34 #include "lua.h" |
35 #include "lualib.h" | |
35 #include "lauxlib.h" | 36 #include "lauxlib.h" |
37 | |
38 #include <fcntl.h> | |
39 #if defined(_GNU_SOURCE) | |
40 #include <linux/falloc.h> | |
41 #endif | |
36 | 42 |
37 #if (defined(_SVID_SOURCE) && !defined(WITHOUT_MALLINFO)) | 43 #if (defined(_SVID_SOURCE) && !defined(WITHOUT_MALLINFO)) |
38 #include <malloc.h> | 44 #include <malloc.h> |
39 #define WITH_MALLINFO | 45 #define WITH_MALLINFO |
40 #endif | 46 #endif |
640 lua_setfield(L, -2, "returnable"); | 646 lua_setfield(L, -2, "returnable"); |
641 return 1; | 647 return 1; |
642 } | 648 } |
643 #endif | 649 #endif |
644 | 650 |
651 /* File handle extraction blatantly stolen from | |
652 * https://github.com/rrthomas/luaposix/blob/master/lposix.c#L631 | |
653 * */ | |
654 | |
655 #if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE) | |
656 int lc_fallocate(lua_State* L) | |
657 { | |
658 off_t offset, len; | |
659 FILE *f = *(FILE**) luaL_checkudata(L, 1, LUA_FILEHANDLE); | |
660 | |
661 offset = luaL_checkinteger(L, 2); | |
662 len = luaL_checkinteger(L, 3); | |
663 | |
664 #if defined(_GNU_SOURCE) | |
665 if(fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len) != 0) | |
666 #elif _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L | |
667 if(posix_fallocate(fileno(f), offset, len) != 0) | |
668 #endif | |
669 { | |
670 #if ! defined(_GNU_SOURCE) | |
671 /* posix_fallocate() can leave a bunch of NULs at the end, so we cut that | |
672 * this assumes that offset == length of the file */ | |
673 ftruncate(fileno(f), offset); | |
674 #endif | |
675 lua_pushnil(L); | |
676 lua_pushstring(L, strerror(errno)); | |
677 return 2; | |
678 } | |
679 lua_pushboolean(L, 1); | |
680 return 1; | |
681 } | |
682 #endif | |
683 | |
645 /* Register functions */ | 684 /* Register functions */ |
646 | 685 |
647 int luaopen_util_pposix(lua_State *L) | 686 int luaopen_util_pposix(lua_State *L) |
648 { | 687 { |
649 luaL_Reg exports[] = { | 688 luaL_Reg exports[] = { |
677 | 716 |
678 #ifdef WITH_MALLINFO | 717 #ifdef WITH_MALLINFO |
679 { "meminfo", lc_meminfo }, | 718 { "meminfo", lc_meminfo }, |
680 #endif | 719 #endif |
681 | 720 |
721 #if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE) | |
722 { "fallocate", lc_fallocate }, | |
723 #endif | |
724 | |
682 { NULL, NULL } | 725 { NULL, NULL } |
683 }; | 726 }; |
684 | 727 |
685 luaL_register(L, "pposix", exports); | 728 luaL_register(L, "pposix", exports); |
686 | 729 |