libs/sys: protect iwinfo loading and return stub if module is not present
[project/luci.git] / libs / sys / luasrc / sys.lua
index 6d000ae..61da6f1 100644 (file)
@@ -30,15 +30,14 @@ local os     = require "os"
 local table  = require "table"
 local nixio  = require "nixio"
 local fs     = require "nixio.fs"
-local iwinfo = require "iwinfo"
 local uci    = require "luci.model.uci"
 
 local luci  = {}
 luci.util   = require "luci.util"
 luci.ip     = require "luci.ip"
 
-local tonumber, ipairs, pairs, pcall, type, next, setmetatable =
-       tonumber, ipairs, pairs, pcall, type, next, setmetatable
+local tonumber, ipairs, pairs, pcall, type, next, setmetatable, require =
+       tonumber, ipairs, pairs, pcall, type, next, setmetatable, require
 
 
 --- LuCI Linux and POSIX system utilities.
@@ -60,20 +59,6 @@ end
 -- @return                     String containg the return the output of the command
 exec = luci.util.exec
 
---- Invoke the luci-flash executable to write an image to the flash memory.
--- @param image                Local path or URL to image file
--- @param kpattern     Pattern of files to keep over flash process
--- @return                     Return value of os.execute()
-function flash(image, kpattern)
-       local cmd = "luci-flash "
-       if kpattern then
-               cmd = cmd .. "-k '" .. kpattern:gsub("'", "") .. "' "
-       end
-       cmd = cmd .. "'" .. image:gsub("'", "") .. "' >/dev/null 2>&1"
-
-       return os.execute(cmd)
-end
-
 --- Retrieve information about currently mounted file systems.
 -- @return     Table containing mount information
 function mounts()
@@ -604,7 +589,7 @@ end
 -- @return                     Boolean indicating wheather the passwords are equal
 function user.checkpasswd(username, pass)
        local pwh = user.getpasswd(username)
-       if not pwh or nixio.crypt(pass, pwh) ~= pwh then
+       if pwh and nixio.crypt(pass, pwh) ~= pwh then
                return false
        else
                return true
@@ -639,6 +624,8 @@ wifi = {}
 -- @param ifname        String containing the interface name
 -- @return              A wrapped iwinfo object instance
 function wifi.getiwinfo(ifname)
+       local stat, iwinfo = pcall(require, "iwinfo")
+
        if ifname then
                local c = 0
                local u = uci.cursor_state()
@@ -665,19 +652,17 @@ function wifi.getiwinfo(ifname)
                                end)
                end
 
-               local t = iwinfo.type(ifname)
-               if t then
-                       local x = iwinfo[t]
-                       return setmetatable({}, {
-                               __index = function(t, k)
-                                       if k == "ifname" then
-                                               return ifname
-                                       elseif x[k] then
-                                               return x[k](ifname)
-                                       end
+               local t = stat and iwinfo.type(ifname)
+               local x = t and iwinfo[t] or { }
+               return setmetatable({}, {
+                       __index = function(t, k)
+                               if k == "ifname" then
+                                       return ifname
+                               elseif x[k] then
+                                       return x[k](ifname)
                                end
-                       })
-               end
+                       end
+               })
        end
 end