Bump UCI version
[project/luci.git] / libs / uci / luasrc / model / uci.lua
index 659cd62..ecb8f47 100644 (file)
@@ -54,14 +54,17 @@ end
 
 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] .. " >/dev/null 2>&1")
+--- 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
@@ -74,7 +77,7 @@ function Cursor.delete_all(self, config, stype, comparator)
                local tbl = comparator
                comparator = function(section)
                        for k, v in pairs(tbl) do
-                               if not section[k] == v then
+                               if section[k] ~= v then
                                        return false
                                end
                        end 
@@ -85,7 +88,7 @@ function Cursor.delete_all(self, config, stype, comparator)
        local function helper (section)
 
                if not comparator or comparator(section) then
-                       table.insert(del, section[".name"])
+                       del[#del+1] = section[".name"]
                end
        end
 
@@ -161,22 +164,48 @@ 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}
 
-Cursor._changes = Cursor.changes
-function Cursor.changes(self, config)
-       if config then
-               return Cursor._changes(self, config)
-       else
-               local changes = Cursor._changes(self)
-               util.copcall(function()
-                       for k,v in pairs(require "luci.fs".dir(self:get_savedir())) do
-                               if v ~= "." and v ~= ".." then
-                                       util.update(changes, Cursor._changes(self, v))
+       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 changes
+               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