summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2009-01-24 16:52:21 +0000
committerTed Trask <ttrask01@yahoo.com>2009-01-24 16:52:21 +0000
commit80f200377d7ed85ae3f6a93348b386bb63b4aeed (patch)
treee93a6adee26e92e79299309b89d8a2be57b9fcf8 /lib
parenta96cb599a24a578a98af2e70da40037c00a7db6b (diff)
downloadacf-core-80f200377d7ed85ae3f6a93348b386bb63b4aeed.tar.bz2
acf-core-80f200377d7ed85ae3f6a93348b386bb63b4aeed.tar.xz
Started process of removing as many io.popen calls as possible. Not complete.
git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@1695 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'lib')
-rw-r--r--lib/fs.lua54
-rw-r--r--lib/menubuilder.lua8
-rw-r--r--lib/processinfo.lua51
-rw-r--r--lib/roles.lua16
4 files changed, 68 insertions, 61 deletions
diff --git a/lib/fs.lua b/lib/fs.lua
index f484bf1..0b4c196 100644
--- a/lib/fs.lua
+++ b/lib/fs.lua
@@ -53,6 +53,24 @@ function create_directory ( path )
return is_dir(path)
end
+-- Deletes a directory along with its contents
+function remove_directory ( path )
+ if fs.is_dir(path) then
+ for d in posix.files(path) do
+ if (d == ".") or (d == "..") then
+ -- ignore
+ elseif fs.is_dir(path .. "/" .. d) then
+ remove_directory(path .. "/" ..d)
+ else
+ os.remove(path .. "/" ..d)
+ end
+ end
+ os.remove(path)
+ return true
+ end
+ return false
+end
+
-- Creates a blank file (and the directory if necessary)
function create_file ( path )
path = path or ""
@@ -116,29 +134,27 @@ function write_line_file ( path, str )
end
end
--- iterator function for finding dir entries matching filespec (what)
--- starting at where, or currentdir if not specified.
--- Finds regexes, not fileglobs
-function find ( what, where )
- -- returns an array of files under "where" that match what "f"
- local function find_files_as_array ( f, where, t )
- where = where or posix.getcwd()
- f = f or ".*"
- t = t or {}
- if fs.is_dir(where) then
- for d in posix.files ( where ) do
- if fs.is_dir ( where .. "/" .. d ) and (d ~= ".") and ( d ~= "..") then
- find_files_as_array (f, where .. "/" .. d, t )
- end
- if (string.match (d, "^" .. f .. "$" )) then
- table.insert (t, ( string.gsub ( where .. "/" .. d, "/+", "/" ) ) )
- end
+-- returns an array of files under "where" that match "what" (a Lua pattern)
+function find_files_as_array ( what, where, t )
+ where = where or posix.getcwd()
+ what = what or ".*"
+ t = t or {}
+ if fs.is_dir(where) then
+ for d in posix.files ( where ) do
+ if fs.is_dir ( where .. "/" .. d ) and (d ~= ".") and ( d ~= "..") then
+ find_files_as_array (what, where .. "/" .. d, t )
+ end
+ if (string.match (d, "^" .. what .. "$" )) then
+ table.insert (t, ( string.gsub ( where .. "/" .. d, "/+", "/" ) ) )
end
end
- return (t)
end
+ return (t)
+end
- -- This is the iterator
+-- iterator function for finding dir entries matching (what) (a Lua pattern)
+-- starting at where, or currentdir if not specified.
+function find ( what, where )
local t = find_files_as_array ( what, where )
local idx = 0
return function ()
diff --git a/lib/menubuilder.lua b/lib/menubuilder.lua
index 4105db6..5b9509b 100644
--- a/lib/menubuilder.lua
+++ b/lib/menubuilder.lua
@@ -8,15 +8,9 @@ module(..., package.seeall)
require("format")
-- returns a table of the "*.menu" tables
--- uses the system "find" command
-- startdir should be the app dir.
local get_candidates = function (startdir)
- local t = {}
- local fh = io.popen('find ' .. format.escapespecialcharacters(startdir) .. ' -name "*.menu"')
- for x in fh:lines() do
- t[#t + 1] = x
- end
- return t
+ return fs.find_files_as_array(".*%.menu", startdir)
end
-- Split string into priority and name, convert '_' to space
diff --git a/lib/processinfo.lua b/lib/processinfo.lua
index bc8c5dd..f41e62f 100644
--- a/lib/processinfo.lua
+++ b/lib/processinfo.lua
@@ -8,34 +8,40 @@ require("format")
local path = "PATH=/usr/bin:/bin:/usr/sbin:/sbin "
function package_version(packagename)
- local cmderrors
- local f = io.popen( "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin apk_version -vs " .. format.escapespecialcharacters(packagename) .." | egrep -v 'acf' 2>/dev/null" )
- local cmdresult = f:read("*l")
- if (cmdresult) and (#cmdresult > 0) then
- cmdresult = (string.match(cmdresult,"^%S*") or "Unknown")
- else
- cmderrors = "Program not installed"
- end
+ local result
+ local errtxt = "Program not installed"
+ local f = io.popen( "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin apk_version -vs " .. format.escapespecialcharacters(packagename) )
+ local cmdresult = f:read("*a") or ""
f:close()
- return cmdresult,cmderrors
+ for line in string.gmatch(cmdresult, "[^\n]+") do
+ if string.match(line, "^"..format.escapemagiccharacters(packagename).."-") then
+ result = string.match(line, "%S+")
+ errtxt = nil
+ break
+ end
+ end
+ return result,errtxt
end
function process_startupsequence(servicename)
- local cmderrors
- local f = io.popen( "/sbin/rc_status | egrep '^S' | egrep \"" .. format.escapespecialcharacters(servicename) .."\" 2>/dev/null" )
- local cmdresult = f:read("*a")
- if (cmdresult) and (#cmdresult > 0) then
- cmdresult = "Service will autostart at next boot (at sequence '" .. string.match(cmdresult,"^%a+(%d%d)") .. "')"
- else
- cmderrors = "Not programmed to autostart"
- end
+ local result
+ local errtxt = "Not programmed to autostart"
+ local f = io.popen( "/sbin/rc_status" )
+ local cmdresult = f:read("*a") or ""
f:close()
- return cmdresult,cmderrors
+ for line in string.gmatch(cmdresult, "[^\n]+") do
+ if string.match(line, "^%a%d%d"..format.escapemagiccharacters(servicename).."$") then
+ result = "Service will autostart at next boot (at sequence '" .. string.match(line,"^%a(%d%d)") .. "')"
+ errtxt = nil
+ break
+ end
+ end
+ return result,errtxt
end
function read_startupsequence()
local config = {}
- local f = io.popen( "/sbin/rc_status 2>/dev/null" )
+ local f = io.popen( "/sbin/rc_status" )
local cmdresult = f:read("*a") or ""
f:close()
local section = 0
@@ -183,14 +189,11 @@ end
local function has_pidfile(name)
local pid
- local f = io.popen(path .. "find /var/run/ -name "..format.escapespecialcharacters(name)..".pid")
- local file = f:read("*a")
- f:close()
- if file and string.find(file, "%w") then
+ local file = "/var/run/"..name..".pid"
+ if fs.is_file(file) then
-- check to see if there's a matching proc directory and that it was created slightly after the pid file
-- this allows us to find init scripts with differing process names and avoids the problem with
-- proc numbers wrapping
- file = string.match(file, "^%s*(.*%S)")
local tmp = string.match(fs.read_file(file) or "", "%d+")
if tmp then
local dir = "/proc/" .. tmp
diff --git a/lib/roles.lua b/lib/roles.lua
index e57490e..1ca8ae2 100644
--- a/lib/roles.lua
+++ b/lib/roles.lua
@@ -11,24 +11,18 @@ guest_role = "GUEST"
-- returns a table of the *.roles files
-- startdir should be the app dir
local get_roles_candidates = function (startdir)
- local t = {}
- local fh = io.popen('find ' .. format.escapespecialcharacters(startdir) .. ' -name "*.roles"')
- for x in fh:lines() do
- t[#t + 1] = x
- end
- return t
+ return fs.find_files_as_array(".*%.roles", startdir) or {}
end
-- Return a list of *controller.lua files
list_controllers = function(self)
local list = {}
- local f = io.popen("/usr/bin/find /usr/share/acf/ |/bin/grep \"controller.lua$\" ")
- for a in f:lines() do
- if not string.find(a, "acf_") then
- list[#list + 1 ] = a
+ for file in fs.find(".*controller%.lua", "/usr/share/acf") do
+ if not string.find(file, "acf_") then
+ list[#list + 1] = file
end
end
- f:close()
+
return list
end