summaryrefslogtreecommitdiffstats
path: root/aconf/persistence/backends/augeas.lua
blob: b3bcd2c9f8047ed23ec74406d3aeeffdb7125fbe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
--[[
Copyright (c) 2012-2019 Kaarle Ritvanen
See LICENSE file for license details
--]]

local topology = require('aconf.model.root').topology
local class = require('aconf.object').class
local pth = require('aconf.path')
local tostr = require('aconf.persistence.util').tostring

local util = require('aconf.util')
local contains = util.contains
local copy = util.copy


local stringy = require('stringy')


local function array_join(tbl, value)
   local res = copy(tbl)
   table.insert(res, value)
   return res
end

local function array_without_last(tbl)
   local res = copy(tbl)
   res[#res] = nil
   return res
end


local function basename(path)
   local res, pred = path:match('^.*/([@#$%w._-]+)([^/]*)$')
   assert(res)
   assert(res ~= '#')
   assert(pred == '' or pred:match('^%[.+%]$'))
   return res
end

local function append_pred(path, pred) return path..'['..pred..']' end

local function append_key_pred(path, key, value)
   return append_pred(path, key.." = '"..value.."'")
end

local function key(mode)
   if mode == 'value' then return '.' end
   if mode and stringy.startswith(mode, 'child-value:') then
      return mode:sub(13, -1)
   end
end

local function is_selector(mode) return mode and mode ~= 'parent-value' end



local function aug_top(path)
   path = copy(path)
   table.insert(path, 1, 'augeas')
   return topology(path)
end


local function conv_path(path)
   local res = '/files'
   if #path == 0 then return res, nil, {} end

   path = copy(path)
   local top = aug_top{}
   local mode, parent
   local keys = {}

   repeat
      local comp = path[1]
      assert(tostring(comp):match('^[%w %_%-%.%@%#%$]+$'))

      if mode then
	 if mode == 'enumerate' then
	    assert(type(comp) == 'number')
	    res = append_pred(res, comp)
	 else
	    local k = key(mode)
	    assert(k and type(comp) == 'string')
	    res = append_key_pred(res, k, comp)
	    table.insert(keys, k)
	 end

	 parent = nil

      else
	 parent = res
	 res = res..'/'..comp
	 keys = {}
      end

      top = top and top(pth.escape(comp))
      mode = top and top.mode

      table.remove(path, 1)
   until #path == 0

   if mode == 'parent-value' then
      assert(parent)
      res = parent
   end

   return res, mode, keys
end


local backend = class()

function backend:init() self.aug = require('augeas').init() end

function backend:get(path, top)
   local apath, mode, keys = conv_path(path)
   local existence = top == true
   if existence or not top then top = aug_top(path) end

   local tpe = top and top.type
   local tbl = tpe == 'table'
   if tbl and top.subtype == 'model' then mode = nil end

   local leaf = not tbl and not mode
   if mode == 'parent-value' then
      assert(not tbl)
      leaf = true
   end

   local matches = self.aug:match(apath..(not leaf and not mode and '/*' or ''))

   if #matches == 0 and not is_selector(mode) then return end

   if is_selector(mode) and #path > 1 and not self:get(
      array_without_last(path), true
   ) then
      return
   end

   if not tpe and not mode then
      if #matches > 1 then
	 leaf = false
	 mode = 'enumerate'
      else
	 local children = self.aug:match(apath..'/*')
	 if #children > 0 then
	    leaf = false
	    matches = children
	 end
      end
   end
   
   if leaf then
      assert(#matches == 1)
      return self.aug:get(apath)
   end

   if existence then return true end

   local names = {}

   for i, child in ipairs(matches) do
      local name
      if not mode then
	 name = basename(child)
	 name = tonumber(name) or name
	 if contains(keys, name) then name = nil end
      elseif mode == 'enumerate' then name = i
      else
	 local k = key(mode)
	 assert(k)
	 name = self.aug:get(child..(k == '.' and '' or '/'..k))
      end

      if name and self:get(array_join(path, name), true) then
	 names[name] = true
      end
   end

   return util.keys(names)
end

function backend:set(mods)
   local gc = {}

   for _, mod in ipairs(mods) do
      local path, value = table.unpack(mod)

      local function insert(path, new)
	 local apath, mode, keys = conv_path(path)
	 if #path == 0 then return apath, keys end
	 local name = path[#path]

	 local parent = array_without_last(path)
	 local ppath, pmode = conv_path(parent)

	 if is_selector(pmode) then
	    gc[pth.join('/', table.unpack(parent))] = false

	    if pmode == 'enumerate' then
	       local count = #self.aug:match(ppath)
	       while count < name do
		  insert(parent, true)
		  count = count + 1
	       end
	       return apath, keys
	    end
	 end

	 local matches = self.aug:match(apath)
	 local count = #matches

	 if count > 0 and not new then return apath, keys end

	 if key(pmode) then
	    apath = key(pmode) and append_pred(
	       ppath, 'count('..key(pmode)..') = 0'
	    ) or append_key_pred(ppath, '.', '')

	    matches = self.aug:match(apath)
	    assert(#matches < 2)
	    apath = matches[1] or insert(parent, true)

	    self.aug:set(apath..'/'..key(pmode), name)

	    return apath, keys
	 end

	 if #matches == 0 then
	    matches = self.aug:match(ppath..'/*')

	    local function order(path)
	       local top = aug_top(path)
	       return top and top.order
	    end
	    local ord = order(path)

	    for _, sibling in ipairs(matches) do
	       local sname = basename(sibling)
	       local sord = order(array_join(parent, sname))
	       if sord then
		  local snum = tonumber(sname)
		  if sord > ord or (
		     sord == ord and snum and type(name) == 'number' and snum > name
		  ) then
		     self.aug:insert(sibling, name, true)
		     return apath, keys
		  end
	       end
	    end
	 end

	 if #matches == 0 then
	    if new then self.aug:set(apath, nil) end
	    return apath, keys
	 end

	 self.aug:insert(matches[#matches], name)
	 return append_pred(apath, count + 1), keys
      end

      local apath, keys = insert(path)
      local is_table = type(value) == 'table'

      if not (is_table or contains(keys, '.')) then
	 self.aug:set(apath, value ~= nil and tostr(value) or nil)
      end

      util.setdefault(gc, pth.join('/', table.unpack(path)), true)
   end

   for path, _ in pairs(gc) do
      local p = pth.split(path)
      while #p > 0 do
	 local value = self:get(p)

	 if (
	    type(value) == 'string' and value ~= ''
	 ) or (
	    type(value) == 'table' and #value > 0
	 ) then
	    break
	 end

	 if gc[pth.join('/', table.unpack(p))] ~= false then
	    self.aug:rm(conv_path(p))
	 end
	 p[#p] = nil
      end
   end

   if self.aug:save() ~= 0 then
      print('Augeas save failed')
      for _, ep in ipairs(self.aug:match('/augeas//error')) do
	 print(ep, self.aug:get(ep))
      end
      assert(false)
   end
end


return backend