]> git.e2factory.org Git - e2factory.git/commitdiff
implement locking module and initialize
authorGordon Hecker <gh@emlix.com>
Tue, 22 Sep 2009 12:11:34 +0000 (14:11 +0200)
committerGordon Hecker <gh@emlix.com>
Fri, 2 Oct 2009 10:40:08 +0000 (12:40 +0200)
Signed-off-by: Gordon Hecker <gh@emlix.com>
generic/Makefile
generic/e2lib.lua
generic/lock.lua [new file with mode: 0644]

index c91d4c1a272cbc18ef8ca87af7447758c43be8b4..4013e03e71f7ebd0ec515e61bd99238c2cc1e96b 100644 (file)
@@ -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 $@
 
index 7a61953735e35c7b0c0976245e53681331c55810..3e5660f8c6df4a670eca647c64e7e91bc43316f4 100644 (file)
@@ -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 (file)
index 0000000..428b306
--- /dev/null
@@ -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
+]]