From 26c46d912d3671b8acfd4e5d590e08d743ce81d8 Mon Sep 17 00:00:00 2001 From: Cuero Bugot Date: Sat, 4 Feb 2017 20:20:11 +0100 Subject: [PATCH 1/5] Remove dependence on cdb_make_free function (which was not public) as cdb_make_finish calls it anyway --- lcdb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lcdb.c b/lcdb.c index 0e68dce..9545815 100644 --- a/lcdb.c +++ b/lcdb.c @@ -198,11 +198,12 @@ static int lcdb_make(lua_State *L) { static int lcdbmakem_gc(lua_State *L) { struct cdb_make *cdbmp = luaL_checkudata(L, 1, LCDB_MAKE); + cdb_make_finish(cdbmp); if (cdbmp->cdb_fd >= 0) { close(cdbmp->cdb_fd); - cdb_make_free(cdbmp); cdbmp->cdb_fd = -1; } + return 0; } @@ -246,7 +247,6 @@ static int lcdbmakem_finish(lua_State *L) { if (cdb_make_finish(cdbmp) < 0 || fsync(cdb_fileno(cdbmp)) < 0 || close(cdb_fileno(cdbmp)) < 0 || rename(tmpname, dest) < 0) { - cdb_make_free(cdbmp); /* in case cdb_make_finish failed before freeing */ cdbmp->cdb_fd = -1; return luaL_error(L, strerror(errno)); } From bf848dc3fea8da1dc310f99ea3dbde9e1b0a6557 Mon Sep 17 00:00:00 2001 From: Cuero Bugot Date: Sat, 4 Feb 2017 23:48:27 +0100 Subject: [PATCH 2/5] fix a bug where the environment was not actually set for the cdb userdata object --- lcdb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lcdb.c b/lcdb.c index 9545815..3545e0c 100644 --- a/lcdb.c +++ b/lcdb.c @@ -157,7 +157,7 @@ static struct cdb_make *new_cdb_make(lua_State *L) { luaL_getmetatable(L, LCDB_MAKE); lua_setmetatable(L, -2); lua_newtable(L); - lua_setfenv(L, 1); + lua_setfenv(L, -2); return cdbmp; } From d90db901f23356c0374ebc53bf6b5aa3446cbd08 Mon Sep 17 00:00:00 2001 From: Cuero Bugot Date: Wed, 8 Feb 2017 10:30:55 +0100 Subject: [PATCH 3/5] add overwrite flag to cdb.make() api --- DOCUMENTATION.mkd | 7 +++++-- lcdb.c | 9 +++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/DOCUMENTATION.mkd b/DOCUMENTATION.mkd index 46ecd92..796a021 100644 --- a/DOCUMENTATION.mkd +++ b/DOCUMENTATION.mkd @@ -28,16 +28,19 @@ tinycdb library reports an error. Returns an iterator function. -## `cdb.make(destination, temporary)` +## `cdb.make(destination, temporary, [overwrite])` Create a cdb maker. Upon calling `maker:finish()`, the temporary file will be renamed to the destination, replacing it atomically. This function fails if the -temporary file already exists. If you allow maker to be garbage collected +temporary file already exists unless overwrite is set to true in wich case the +temporary file is overwriten. If you allow maker to be garbage collected without calling finish(), the temporary file will be left behind. Takes the parameters: * `destination` the destination filename. * `temporary` the name of the file to be used while the cdb is being constructed +* `overwrite` optional, boolean indicating if the temporary file should be overwritten + if it exists. Returns an instance of `cdb.make` or `nil` plus an error message. diff --git a/lcdb.c b/lcdb.c index 3545e0c..d9d2017 100644 --- a/lcdb.c +++ b/lcdb.c @@ -167,15 +167,20 @@ static struct cdb_make *check_cdb_make(lua_State *L, int n) { return cdbmp; } -/* cdb.make(destination, temporary) */ +/* cdb.make(destination, temporary, [overwrite]) */ static int lcdb_make(lua_State *L) { int fd; int ret; struct cdb_make *cdbmp; const char *dest = luaL_checkstring(L, 1); const char *tmpname = luaL_checkstring(L, 2); + int overwrite = lua_toboolean(L, 3); + int flags = O_RDWR|O_CREAT|O_EXCL|O_BINARY; - fd = open(tmpname, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0666); + if (overwrite) + flags &= ~O_EXCL; + + fd = open(tmpname, flags, 0666); if (fd < 0) return push_errno(L, errno); From efe6840dd1eaf025a57b55ab585b96658e838ab5 Mon Sep 17 00:00:00 2001 From: folays Date: Wed, 27 Jan 2016 15:33:01 +0100 Subject: [PATCH 4/5] upgrade to Lua 5.2 --- Makefile | 2 +- lcdb.c | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index cb992d1..3ca055b 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ LUABIN= $(LUA)/bin # probably no need to change anything below here CC= gcc -CFLAGS= $(INCS) $(WARN) -O2 +CFLAGS= $(INCS) $(WARN) -O2 -fPIC WARN= -Wall INCS= -I$(LUAINC) diff --git a/lcdb.c b/lcdb.c index d9d2017..eafc2e4 100644 --- a/lcdb.c +++ b/lcdb.c @@ -143,7 +143,7 @@ static int lcdbm_iternext(lua_State *L) { /* for k, v in db:pairs() do ... end */ static int lcdbm_pairs(lua_State *L) { - struct cdb *cdbp = check_cdb(L, 1); + struct cdb *__attribute__ ((unused))cdbp = check_cdb(L, 1); unsigned pos; cdb_seqinit(&pos, cdbp); @@ -157,7 +157,7 @@ static struct cdb_make *new_cdb_make(lua_State *L) { luaL_getmetatable(L, LCDB_MAKE); lua_setmetatable(L, -2); lua_newtable(L); - lua_setfenv(L, -2); + lua_setuservalue(L, -2); return cdbmp; } @@ -188,7 +188,7 @@ static int lcdb_make(lua_State *L) { ret = cdb_make_start(cdbmp, fd); /* store destination and tmpname in userdata environment */ - lua_getfenv(L, -1); + lua_getuservalue(L, -1); lua_pushstring(L, dest); lua_setfield(L, -2, "dest"); lua_pushstring(L, tmpname); @@ -243,7 +243,7 @@ static int lcdbmakem_add(lua_State *L) { static int lcdbmakem_finish(lua_State *L) { struct cdb_make *cdbmp = check_cdb_make(L, 1); /* retrieve destination, current filename */ - lua_getfenv(L, -1); + lua_getuservalue(L, -1); lua_getfield(L, -1, "dest"); const char *dest = lua_tostring(L, -1); lua_getfield(L, -2, "tmpname"); @@ -290,15 +290,13 @@ int luaopen_cdb(lua_State *L) { luaL_newmetatable(L, LCDB_DB); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - luaL_register(L, NULL, lcdb_m); + luaL_setfuncs(L, lcdb_m, 0); luaL_newmetatable(L, LCDB_MAKE); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - luaL_register(L, NULL, lcdbmake_m); - - lua_newtable(L); - luaL_register(L, NULL, lcdb_f); + luaL_setfuncs(L, lcdbmake_m, 0); + luaL_newlib(L, lcdb_f); return 1; } From 9fcf7a0b0092e4e675f2559dc72febb1b733abef Mon Sep 17 00:00:00 2001 From: ludrao Date: Mon, 27 Mar 2017 21:30:22 +0200 Subject: [PATCH 5/5] fixed double free prb --- lcdb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lcdb.c b/lcdb.c index eafc2e4..3fcdbe5 100644 --- a/lcdb.c +++ b/lcdb.c @@ -203,8 +203,8 @@ static int lcdb_make(lua_State *L) { static int lcdbmakem_gc(lua_State *L) { struct cdb_make *cdbmp = luaL_checkudata(L, 1, LCDB_MAKE); - cdb_make_finish(cdbmp); if (cdbmp->cdb_fd >= 0) { + cdb_make_finish(cdbmp); close(cdbmp->cdb_fd); cdbmp->cdb_fd = -1; }