Comparison

util-src/pposix.c @ 6156:6b1aee6536e8

Merge 0.9->0.10
author Kim Alvefur <zash@zash.se>
date Sun, 27 Apr 2014 01:02:20 +0200
parent 5928:1f6923702fc3
parent 6155:dc3497041aca
child 6411:6c8f6364bc48
comparison
equal deleted inserted replaced
6153:8fb54ec34741 6156:6b1aee6536e8
672 * */ 672 * */
673 673
674 #if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE) 674 #if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE)
675 int lc_fallocate(lua_State* L) 675 int lc_fallocate(lua_State* L)
676 { 676 {
677 int ret;
677 off_t offset, len; 678 off_t offset, len;
678 FILE *f = *(FILE**) luaL_checkudata(L, 1, LUA_FILEHANDLE); 679 FILE *f = *(FILE**) luaL_checkudata(L, 1, LUA_FILEHANDLE);
679 if (f == NULL) 680 if (f == NULL)
680 luaL_error(L, "attempt to use a closed file"); 681 luaL_error(L, "attempt to use a closed file");
681 682
682 offset = luaL_checkinteger(L, 2); 683 offset = luaL_checkinteger(L, 2);
683 len = luaL_checkinteger(L, 3); 684 len = luaL_checkinteger(L, 3);
684 685
685 #if defined(__linux__) && defined(_GNU_SOURCE) 686 #if defined(__linux__) && defined(_GNU_SOURCE)
686 if(fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len) == 0) 687 errno = 0;
688 ret = fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len);
689 if(ret == 0)
687 { 690 {
688 lua_pushboolean(L, 1); 691 lua_pushboolean(L, 1);
689 return 1; 692 return 1;
690 } 693 }
694 /* Some old versions of Linux apparently use the return value instead of errno */
695 if(errno == 0) errno = ret;
691 696
692 if(errno != ENOSYS && errno != EOPNOTSUPP) 697 if(errno != ENOSYS && errno != EOPNOTSUPP)
693 { 698 {
694 lua_pushnil(L); 699 lua_pushnil(L);
695 lua_pushstring(L, strerror(errno)); 700 lua_pushstring(L, strerror(errno));
699 #warning Only using posix_fallocate() fallback. 704 #warning Only using posix_fallocate() fallback.
700 #warning Linux fallocate() is strongly recommended if available: recompile with -D_GNU_SOURCE 705 #warning Linux fallocate() is strongly recommended if available: recompile with -D_GNU_SOURCE
701 #warning Note that posix_fallocate() will still be used on filesystems that dont support fallocate() 706 #warning Note that posix_fallocate() will still be used on filesystems that dont support fallocate()
702 #endif 707 #endif
703 708
704 if(posix_fallocate(fileno(f), offset, len) == 0) 709 ret = posix_fallocate(fileno(f), offset, len);
710 if(ret == 0)
705 { 711 {
706 lua_pushboolean(L, 1); 712 lua_pushboolean(L, 1);
707 return 1; 713 return 1;
708 } 714 }
709 else 715 else
710 { 716 {
711 lua_pushnil(L); 717 lua_pushnil(L);
712 lua_pushstring(L, strerror(errno)); 718 lua_pushstring(L, strerror(ret));
713 /* posix_fallocate() can leave a bunch of NULs at the end, so we cut that 719 /* posix_fallocate() can leave a bunch of NULs at the end, so we cut that
714 * this assumes that offset == length of the file */ 720 * this assumes that offset == length of the file */
715 ftruncate(fileno(f), offset); 721 ftruncate(fileno(f), offset);
716 return 2; 722 return 2;
717 } 723 }