From f107f28ea45b15611b5699cb21662daf5ca0535a Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Mon, 31 Jul 2017 16:17:17 -0400 Subject: Add lock and unlock utility functions --- lib/gitano/util.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib') diff --git a/lib/gitano/util.lua b/lib/gitano/util.lua index dcb41f4..76183e3 100644 --- a/lib/gitano/util.lua +++ b/lib/gitano/util.lua @@ -569,6 +569,21 @@ local function tempfile(repo) return fd, temppattern end +local function lockfile(path) + local fh = assert(sio.open(path, "cw+")) + local ok, msg = fh:lock("w", "set", 0, 0, true, false) + if not ok then + fh:close() + error(msg) + end + return fh +end + +local function unlockfile(fh) + fh:lock("", "set", 0, 0, true, false) + fh:close() +end + return { parse_cmdline = _parse_cmdline, @@ -605,4 +620,7 @@ return { check_password = check_password, run_command = run_command, + + lockfile = lockfile, + unlockfile = unlockfile, } -- cgit v1.2.1