libs/sys: Added luci.sys.mounts
[project/luci.git] / libs / sys / luasrc / sys.lua
index 14a2f29..779e202 100644 (file)
@@ -30,6 +30,7 @@ require("posix")
 require("luci.bits")
 require("luci.util")
 require("luci.fs")
+require("luci.ip")
 
 --- Invoke the luci-flash executable to write an image to the flash memory.
 -- @param kpattern     Pattern of files to keep over flash process
@@ -44,6 +45,36 @@ function flash(image, kpattern)
        return os.execute(cmd)
 end
 
+--- Retrieve information about currently mounted file systems.
+-- @return     Table containing mount information
+function mounts()
+       local data = {}
+       local k = {"fs", "blocks", "used", "available", "percent", "mountpoint"}
+       local ps = luci.util.execi("df")
+       
+       if not ps then
+               return
+       else
+               ps()
+       end
+       
+       for line in ps do
+               local row = {}
+               
+               local j = 1
+               for value in line:gmatch("[^%s]+") do
+                       row[k[j]] = value
+                       j = j + 1
+               end
+               
+               if row[k[1]] then
+                       table.insert(data, row)
+               end
+       end
+       
+       return data
+end
+
 --- Retrieve environment variables. If no variable is given then a table
 -- containing the whole environment is returned otherwise this function returns
 -- the corresponding string value for the given name or nil if no such variable
@@ -183,15 +214,6 @@ function net.arptable()
        return _parse_delimited_table(io.lines("/proc/net/arp"), "%s%s+")
 end
 
---- Test whether an IP-Adress belongs to a certain net.
--- @param ip           IPv4 address to test
--- @param ipnet                IPv4 network address of the net range to compare against
--- @param prefix       Network prefix of the net range to compare against
--- @return                     Boolean indicating wheather the ip is within the range
-function net.belongs(ip, ipnet, prefix)
-       return (net.ip4bin(ip):sub(1, prefix) == net.ip4bin(ipnet):sub(1, prefix))
-end
-
 --- Determine the current default route.
 -- @return     Table with the properties of the current default route.
 --                     The following fields are defined:
@@ -220,6 +242,21 @@ function net.devices()
        return devices
 end
 
+
+--- Return information about available network interfaces.
+-- @return     Table containing all current interface names and their information
+function net.deviceinfo()
+       local devices = {}
+       for line in io.lines("/proc/net/dev") do
+               local name, data = line:match("^ *(.-): *(.*)$")
+               if name and data then
+                       devices[name] = luci.util.split(data, " +", nil, true)
+               end
+       end
+       return devices
+end
+
+
 -- Determine the MAC address belonging to the given IP address.
 -- @param ip   IPv4 address
 -- @return             String containing the MAC address or nil if it cannot be found
@@ -235,19 +272,6 @@ function net.ip4mac(ip)
        return mac
 end
 
---- Calculate the prefix from a given netmask.
--- @param mask IPv4 net mask
--- @return             Number containing the corresponding numerical prefix
-function net.mask4prefix(mask)
-       local bin = net.ip4bin(mask)
-
-       if not bin then
-               return nil
-       end
-
-       return #luci.util.split(bin, "1")-1
-end
-
 --- Returns the current kernel routing table entries.
 -- @return     Table of tables with properties of the corresponding routes.
 --                     The following fields are defined for route entry tables:
@@ -257,54 +281,6 @@ function net.routes()
        return _parse_delimited_table(io.lines("/proc/net/route"))
 end
 
---- Convert hexadecimal 32 bit value to IPv4 address.
--- @param hex  String containing the hexadecimal value
--- @param be   Boolean indicating wheather the given value is big endian
--- @return             String containing the corresponding IP4 address
-function net.hexip4(hex, be)
-       if #hex ~= 8 then
-               return nil
-       end
-
-       be = be or luci.util.bigendian()
-
-       local hexdec = luci.bits.Hex2Dec
-
-       local ip = ""
-       if be then
-               ip = ip .. tostring(hexdec(hex:sub(1,2))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(3,4))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(5,6))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(7,8)))
-       else
-               ip = ip .. tostring(hexdec(hex:sub(7,8))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(5,6))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(3,4))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(1,2)))
-       end
-
-       return ip
-end
-
---- Convert given IPv4 address to binary value.
--- @param ip   String containing a IPv4 address
--- @return             String containing corresponding binary value
-function net.ip4bin(ip)
-       local parts = luci.util.split(ip, '.')
-       if #parts ~= 4 then
-               return nil
-       end
-
-       local decbin = luci.bits.Dec2Bin
-
-       local bin = ""
-       bin = bin .. decbin(parts[1], 8)
-       bin = bin .. decbin(parts[2], 8)
-       bin = bin .. decbin(parts[3], 8)
-       bin = bin .. decbin(parts[4], 8)
-
-       return bin
-end
 
 --- Tests whether the given host responds to ping probes.
 -- @param host String containing a hostname or IPv4 address
@@ -364,10 +340,7 @@ user.getuser = posix.getpasswd
 function user.checkpasswd(username, password)
        local account = user.getuser(username)
 
-       -- FIXME: detect testing environment
-       if luci.fs.stat("/etc/shadow") and not luci.fs.access("/etc/shadow", "r") then
-               return true
-       elseif account then
+       if account then
                if account.passwd == "!" then
                        return true
                else