From b23a6bb4948b9ea6b280789a52ba78b332aae872 Mon Sep 17 00:00:00 2001 From: Gordon Hecker Date: Tue, 22 Sep 2009 14:11:34 +0200 Subject: [PATCH] implement locking module and initialize Signed-off-by: Gordon Hecker --- generic/Makefile | 11 ++++++-- generic/e2lib.lua | 7 +++++ generic/lock.lua | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 generic/lock.lua diff --git a/generic/Makefile b/generic/Makefile index c91d4c1..4013e03 100644 --- a/generic/Makefile +++ b/generic/Makefile @@ -37,10 +37,12 @@ CLEAN_FILES = *~ *.lc *.so lua-version-map.lua e2-su-2.2 .PHONY: all install uninstall local install-local clean -all: e2generic_global.lc luafile_ll_global.so e2util_global.so e2-su-2.2 +all: e2generic_global.lc luafile_ll_global.so e2util_global.so e2-su-2.2 \ + lock.lc install: all install -m 644 e2generic_global.lc $(DESTDIR)$(LIBDIR) + install -m 644 lock.lc $(DESTDIR)$(LIBDIR) install -m 755 luafile_ll_global.so $(DESTDIR)$(LIBDIR) install -m 755 e2util_global.so $(DESTDIR)$(LIBDIR) install -m 4754 -o root -g $(E2_GROUP) e2-su-2.2 $(DESTDIR)$(BINDIR)/ @@ -48,14 +50,16 @@ install: all uninstall: rm -f $(DESTDIR)$(BINDIR)/e2-su-2.2 rm -f $(DESTDIR)$(LIBDIR)/e2generic_global.lc + rm -f $(DESTDIR)$(LIBDIR)/lock.lc rm -f $(DESTDIR)$(LIBDIR)/luafile_ll_global.so rm -f $(DESTDIR)$(LIBDIR)/e2util_global.so -local: e2generic_local.lc luafile_ll_local.so e2util_local.so +local: e2generic_local.lc luafile_ll_local.so e2util_local.so lock.lc install-local: local mkdir -p $(LOCALLIBDIR) $(LOCALMAKDIR) install -m 644 e2generic_local.lc $(LOCALLIBDIR) + install -m 644 lock.lc $(LOCALLIBDIR) install -m 755 luafile_ll_local.so $(LOCALLIBDIR) install -m 755 e2util_local.so $(LOCALLIBDIR) install -m 644 e2-su-2.2.c $(LOCALMAKDIR) @@ -89,6 +93,9 @@ e2generic_local.lc: strict.lua collection.lua e2lib_local_prefix.lua \ %: %.in $(TOPLEVEL)/scripts/genscript.sh $< $@ +%.lc: %.lua + $(LUAC) -o $@ $< + e2-su-2.2: e2-su-2.2.c $(CC) $(CFLAGS) $(E2_SU_CFLAGS) $(LDFLAGS) $< -o $@ diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 7a61953..3e5660f 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -26,6 +26,8 @@ ]] require("buildconfig") +require("lock") + _version = "e2factory, the emlix embedded build system, version " .. buildconfig.VERSION @@ -84,6 +86,7 @@ e2lib = { buildnumber_server_url = nil, template_path = string.format("%s/templates", buildconfig.SYSCONFDIR), extension_config = ".e2/extensions", + lock = nil, } -- Interrupt handling @@ -171,6 +174,8 @@ function e2lib.init() if not e2lib.hostname then e2lib.abort("hostname ist not set") end + + e2lib.lock = lock.new() end function e2lib.init2() @@ -271,6 +276,7 @@ function e2lib.abort(...) end e2lib.rmtempdirs() e2lib.rmtempfiles() + e2lib.lock:cleanup() os.exit(1) end @@ -492,6 +498,7 @@ function e2lib.finish(rc) end e2lib.rmtempdirs() e2lib.rmtempfiles() + e2lib.lock:cleanup() os.exit(rc) end diff --git a/generic/lock.lua b/generic/lock.lua new file mode 100644 index 0000000..428b306 --- /dev/null +++ b/generic/lock.lua @@ -0,0 +1,67 @@ +module("lock",package.seeall) + +function new() + local lock = { + + locks = {}, + + lock = function(l, dir) + local e = new_error("locking failed") + rc, re = e2lib.mkdir(dir) + if not rc then + return false, e:cat(re) + end + table.insert(l.locks, dir) + return true + end, + + unlock = function(l, dir) + local e = new_error("unlocking failed") + for i,x in ipairs(l.locks) do + if dir == x then + table.remove(l.locks, i) + rc, re = e2lib.rmdir(dir) + if not rc then + return false, e:cat(re) + end + end + end + return true, nil + end, + + cleanup = function(l) + while #l.locks > 0 do + l:unlock(l.locks[1]) + end + end, + } + + return lock +end + +--[[ +local test=false +if test then + -- some dummy functions to test without context... + function new_error(x) + return true + end + e2lib = {} + e2lib.mkdir = function(x) + print("mkdir " .. x) + return true + end + e2lib.rmdir = function(x) + print("rmdir " .. x) + return true + end + + l = new() + + l:lock("/tmp/foo1") + l:lock("/tmp/foo2") + l:lock("/tmp/foo3") + l:unlock("/tmp/foo2") + l:cleanup() +end +]] -- 2.39.5