From 14ab26132162b248509b9c97457d9f3144960cc0 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Fri, 15 Nov 2013 16:48:52 +0200 Subject: augeas backend: remove some race conditions in set method --- acf2/persistence/backends/augeas.lua | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'acf2/persistence') diff --git a/acf2/persistence/backends/augeas.lua b/acf2/persistence/backends/augeas.lua index f883409..fbe993e 100644 --- a/acf2/persistence/backends/augeas.lua +++ b/acf2/persistence/backends/augeas.lua @@ -46,6 +46,11 @@ local function key(mode) return mode == '@' and '.' or mode:sub(2, -1) end +local function append_key_pred(path, mode, value) + return append_pred(path, key(mode).." = '"..value.."'") +end + + local function conv_path(path) local res = '/files' if #path == 0 then return res, nil, {} end @@ -69,7 +74,7 @@ local function conv_path(path) res = append_pred(res, comp) else assert(type(comp) == 'string' and comp:match('^[%w _-]+$')) - res = append_pred(res, key(mode).." = '"..comp.."'") + res = append_key_pred(res, mode, comp) table.insert(keys, key(mode)) end mode = nil @@ -186,10 +191,18 @@ function backend:set(mods) if count > 0 and not new then return apath, keys end if key_mode(pmode) then - ppath = insert(parent, true) + local cpath = pmode == '@' and append_key_pred( + ppath, '@', '' + ) or append_pred(ppath, 'count('..key(pmode)..') = 0') + + count = #self.aug:match(cpath) + assert(count < 2) + if count == 0 then cpath = insert(parent, true) end + local key = key(pmode) - self.aug:set(ppath..'/'..key, name) - return append_pred(ppath, key), keys + self.aug:set(cpath..'/'..key, name) + + return append_key_pred(ppath, pmode, name), keys end if #matches == 0 then -- cgit v1.2.3