Bump UCI version
[project/luci.git] / libs / uci / luasrc / model / uci.lua
index 91608e3..ecb8f47 100644 (file)
@@ -31,44 +31,68 @@ local table = require "table"
 
 local setmetatable, rawget, rawset = setmetatable, rawget, rawset
 local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring
-local require, getmetatable = require, getmetatable
+local require, getmetatable, type = require, getmetatable, type
 
 --- LuCI UCI model library.
-module("luci.model.uci")
+-- @cstyle     instance
+module "luci.model.uci"
 
+--- Create a new UCI-Cursor.
+-- @class function
+-- @name cursor
+-- @return     UCI-Cursor
 cursor = uci.cursor
+
 APIVERSION = uci.APIVERSION
 
---- Creates a new statevalue cursor
+--- Create a new Cursor initialized to the state directory.
 -- @return UCI cursor
 function cursor_state()
        return cursor(nil, "/var/state")
 end
 
---- UCI-Cursor
+
 local Cursor = getmetatable(cursor())
 
---- Applies the new config
--- @param config               UCI config
-function Cursor.apply(self, config)
-       local conf = require "luci.config"
-       return conf.uci_oncommit[config] and os.execute(conf.uci_oncommit[config])
+--- Applies UCI configuration changes
+-- @param configlist           List of UCI configurations
+-- @param command                      Don't apply only return the command
+function Cursor.apply(self, configlist, command)
+       configlist = self:_affected(configlist)
+       local reloadcmd = "/sbin/luci-reload " .. table.concat(configlist, " ")
+       
+       return command and reloadcmd or os.execute(reloadcmd .. " >/dev/null 2>&1")
 end
 
+
 --- Delete all sections of a given type that match certain criteria.
 -- @param config               UCI config
 -- @param type                 UCI section type
 -- @param comparator   Function that will be called for each section and
 -- returns a boolean whether to delete the current section (optional)
-function Cursor.delete_all(self, config, type, comparator)
+function Cursor.delete_all(self, config, stype, comparator)
        local del = {}
+       
+       if type(comparator) == "table" then
+               local tbl = comparator
+               comparator = function(section)
+                       for k, v in pairs(tbl) do
+                               if section[k] ~= v then
+                                       return false
+                               end
+                       end 
+                       return true
+               end
+       end
+       
        local function helper (section)
+
                if not comparator or comparator(section) then
-                       table.insert(del, section[".name"])
+                       del[#del+1] = section[".name"]
                end
        end
 
-       self:foreach(config, type, helper)
+       self:foreach(config, stype, helper)
 
        for i, j in ipairs(del) do
                self:delete(config, j)
@@ -140,6 +164,50 @@ function Cursor.set_list(self, config, section, option, value)
        return false
 end
 
+-- Return a list of initscripts affected by configuration changes.
+function Cursor._affected(self, configlist)
+       configlist = type(configlist) == "table" and configlist or {configlist}
+
+       local c = cursor()
+       c:load("ucitrack")
+
+       -- Resolve dependencies
+       local reloadlist = {}
+
+       local function _resolve_deps(name)
+               local reload = {name}
+               local deps = {}
+       
+               c:foreach("ucitrack", name,
+                       function(section)
+                               if section.affects then
+                                       for i, aff in ipairs(section.affects) do
+                                               deps[#deps+1] = aff
+                                       end
+                               end
+                       end)
+               
+               for i, dep in ipairs(deps) do
+                       for j, add in ipairs(_resolve_deps(dep)) do
+                               reload[#reload+1] = add
+                       end
+               end
+               
+               return reload
+       end
+       
+       -- Collect initscripts
+       for j, config in ipairs(configlist) do
+               for i, e in ipairs(_resolve_deps(config)) do
+                       if not util.contains(reloadlist, e) then
+                               reloadlist[#reloadlist+1] = e
+                       end
+               end
+       end
+       
+       return reloadlist
+end
+
 
 --- Add an anonymous section.
 -- @class function
@@ -159,7 +227,7 @@ end
 -- @name Cursor.commit
 -- @param config       UCI config
 -- @return                     Boolean whether operation succeeded
--- @see revert
+-- @see Cursor.revert
 
 --- Deletes a section or an option.
 -- @class function
@@ -193,30 +261,27 @@ end
 -- @return                     Table of UCI sections or table of UCI values
 
 --- Manually load a config.
--- Warning: This function is unsave! You should use load_config or load_state if possible.
 -- @class function
 -- @name Cursor.load
 -- @param config       UCI config
 -- @return                     Boolean whether operation succeeded
--- @see load_config
--- @see load_state
--- @see save
--- @see unload
+-- @see Cursor.save
+-- @see Cursor.unload
 
 --- Revert unsaved changes.
 -- @class function
 -- @name Cursor.revert
 -- @param config       UCI config
 -- @return                     Boolean whether operation succeeded
--- @see commit
+-- @see Cursor.commit
 
 --- Saves changes made to a config to make them committable.
 -- @class function
 -- @name Cursor.save
 -- @param config       UCI config
 -- @return                     Boolean whether operation succeeded
--- @see load
--- @see unload
+-- @see Cursor.load
+-- @see Cursor.unload
 
 --- Set a value or create a named section.
 -- @class function
@@ -254,5 +319,5 @@ end
 -- @name Cursor.unload
 -- @param config       UCI config
 -- @return                     Boolean whether operation succeeded
--- @see load
--- @see save
+-- @see Cursor.load
+-- @see Cursor.save