aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/mod_posix.lua2
-rwxr-xr-xprosodyctl2
-rw-r--r--util-src/pposix.c65
3 files changed, 66 insertions, 3 deletions
diff --git a/plugins/mod_posix.lua b/plugins/mod_posix.lua
index 2103071a..412a7779 100644
--- a/plugins/mod_posix.lua
+++ b/plugins/mod_posix.lua
@@ -7,7 +7,7 @@
--
-local want_pposix_version = "0.3.6";
+local want_pposix_version = "0.3.7";
local pposix = assert(require "util.pposix");
if pposix._VERSION ~= want_pposix_version then
diff --git a/prosodyctl b/prosodyctl
index cc440c59..4fb67a95 100755
--- a/prosodyctl
+++ b/prosodyctl
@@ -135,7 +135,7 @@ dependencies.log_warnings();
-- Switch away from root and into the prosody user --
local switched_user, current_uid;
-local want_pposix_version = "0.3.6";
+local want_pposix_version = "0.3.7";
local ok, pposix = pcall(require, "util.pposix");
if ok and pposix then
diff --git a/util-src/pposix.c b/util-src/pposix.c
index 4b4552f5..a68aa89d 100644
--- a/util-src/pposix.c
+++ b/util-src/pposix.c
@@ -13,7 +13,7 @@
* POSIX support functions for Lua
*/
-#define MODULE_VERSION "0.3.6"
+#define MODULE_VERSION "0.3.7"
#if defined(__linux__)
@@ -788,6 +788,68 @@ int lc_fallocate(lua_State *L) {
}
}
+/*
+ * Append some data to a file handle
+ * Attempt to allocate space first
+ * Truncate to original size on failure
+ */
+int lc_atomic_append(lua_State *L) {
+ int err;
+ size_t len;
+
+ FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
+ const char *data = luaL_checklstring(L, 2, &len);
+
+ off_t offset = ftell(f);
+
+#if defined(__linux__)
+ /* Try to allocate space without changing the file size. */
+ if((err = fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len))) {
+ if(errno != 0) {
+ /* Some old versions of Linux apparently use the return value instead of errno */
+ err = errno;
+ }
+ switch(err) {
+ case ENOSYS: /* Kernel doesn't implement fallocate */
+ case EOPNOTSUPP: /* Filesystem doesn't support it */
+ /* Ignore and proceed to try to write */
+ break;
+
+ case ENOSPC: /* No space left */
+ default: /* Other issues */
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(err));
+ lua_pushinteger(L, err);
+ return 3;
+ }
+ }
+#endif
+
+ if(fwrite(data, sizeof(char), len, f) == len) {
+ if(fflush(f) == 0) {
+ lua_pushboolean(L, 1); /* Great success! */
+ return 1;
+ } else {
+ err = errno;
+ }
+ } else {
+ err = ferror(f);
+ }
+
+ fseek(f, offset, SEEK_SET);
+
+ /* Cut partially written data */
+ if(ftruncate(fileno(f), offset)) {
+ /* The file is now most likely corrupted, throw hard error */
+ return luaL_error(L, "atomic_append() failed in ftruncate(): %s", strerror(errno));
+ }
+
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(err));
+ lua_pushinteger(L, err);
+ return 3;
+}
+
/* Register functions */
int luaopen_util_pposix(lua_State *L) {
@@ -828,6 +890,7 @@ int luaopen_util_pposix(lua_State *L) {
#endif
{ "fallocate", lc_fallocate },
+ { "atomic_append", lc_atomic_append },
{ NULL, NULL }
};