Comparison

util-src/pposix.c @ 5067:7db1056c63a9

util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
author Kim Alvefur <zash@zash.se>
date Tue, 31 Jul 2012 23:38:02 +0200
parent 5052:f1157cce5d7a
child 5068:14d4fc5859b9
comparison
equal deleted inserted replaced
5066:4be7093edde9 5067:7db1056c63a9
660 660
661 offset = luaL_checkinteger(L, 2); 661 offset = luaL_checkinteger(L, 2);
662 len = luaL_checkinteger(L, 3); 662 len = luaL_checkinteger(L, 3);
663 663
664 #if defined(_GNU_SOURCE) 664 #if defined(_GNU_SOURCE)
665 if(fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len) != 0) 665 if(fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len) == 0)
666 #elif _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L 666 {
667 #warning Using posix_fallocate() fallback. Linux fallocate() is strongly recommended if available: recompile with -D_GNU_SOURCE 667 lua_pushboolean(L, 1);
668 if(posix_fallocate(fileno(f), offset, len) != 0) 668 return 1;
669 #endif 669 }
670 { 670
671 #if ! defined(_GNU_SOURCE) 671 if(errno != ENOSYS && errno != EOPNOTSUPP)
672 {
673 lua_pushnil(L);
674 lua_pushstring(L, strerror(errno));
675 return 2;
676 }
677 #endif
678
679 if(posix_fallocate(fileno(f), offset, len) == 0)
680 {
681 lua_pushboolean(L, 1);
682 return 1;
683 }
684 else
685 {
686 lua_pushnil(L);
687 lua_pushstring(L, strerror(errno));
672 /* posix_fallocate() can leave a bunch of NULs at the end, so we cut that 688 /* posix_fallocate() can leave a bunch of NULs at the end, so we cut that
673 * this assumes that offset == length of the file */ 689 * this assumes that offset == length of the file */
674 ftruncate(fileno(f), offset); 690 ftruncate(fileno(f), offset);
675 #endif 691 return 2;
676 lua_pushnil(L); 692 }
677 lua_pushstring(L, strerror(errno));
678 return 2;
679 }
680 lua_pushboolean(L, 1);
681 return 1;
682 } 693 }
683 #endif 694 #endif
684 695
685 /* Register functions */ 696 /* Register functions */
686 697