Comparison

util-src/pposix.c @ 13184:d16845afb3e2

util.pposix: Add remove_blocks() for deleting parts of files Allows implementing e.g. a FIFO Will probably only work on some Linux file systems like ext4.
author Kim Alvefur <zash@zash.se>
date Wed, 07 Jun 2023 05:07:03 +0200
parent 13152:792f360a582b
comparison
equal deleted inserted replaced
13183:33b114fbb5de 13184:d16845afb3e2
800 lua_pushstring(L, strerror(err)); 800 lua_pushstring(L, strerror(err));
801 lua_pushinteger(L, err); 801 lua_pushinteger(L, err);
802 return 3; 802 return 3;
803 } 803 }
804 804
805 static int lc_remove_blocks(lua_State *L) {
806 #if defined(__linux__)
807 int err;
808
809 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
810 off_t offset = (off_t)luaL_checkinteger(L, 2);
811 off_t length = (off_t)luaL_checkinteger(L, 3);
812
813 errno = 0;
814
815 if((err = fallocate(fileno(f), FALLOC_FL_COLLAPSE_RANGE, offset, length))) {
816 if(errno != 0) {
817 /* Some old versions of Linux apparently use the return value instead of errno */
818 err = errno;
819 }
820
821 switch(err) {
822 default: /* Other issues */
823 luaL_pushfail(L);
824 lua_pushstring(L, strerror(err));
825 lua_pushinteger(L, err);
826 return 3;
827 }
828 }
829
830 lua_pushboolean(L, err == 0);
831 return 1;
832 #else
833 luaL_pushfail(L);
834 lua_pushstring(L, strerror(EOPNOTSUPP));
835 lua_pushinteger(L, EOPNOTSUPP);
836 return 3;
837 #endif
838 }
839
805 static int lc_isatty(lua_State *L) { 840 static int lc_isatty(lua_State *L) {
806 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE); 841 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
807 const int fd = fileno(f); 842 const int fd = fileno(f);
808 lua_pushboolean(L, isatty(fd)); 843 lua_pushboolean(L, isatty(fd));
809 return 1; 844 return 1;
845 #ifdef WITH_MALLINFO 880 #ifdef WITH_MALLINFO
846 { "meminfo", lc_meminfo }, 881 { "meminfo", lc_meminfo },
847 #endif 882 #endif
848 883
849 { "atomic_append", lc_atomic_append }, 884 { "atomic_append", lc_atomic_append },
885 { "remove_blocks", lc_remove_blocks },
850 886
851 { "isatty", lc_isatty }, 887 { "isatty", lc_isatty },
852 888
853 { NULL, NULL } 889 { NULL, NULL }
854 }; 890 };