cgi-bin/tek/class/loona/util.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Fri, 24 Oct 2008 01:35:27 +0200
changeset 251 2de5931b723d
parent 212 a1aad6415384
permissions -rw-r--r--
tek.os.posix has been moved to tek.lib.posix, module initialization procedure
simplified, added statvfs() under Linux
     1 
     2 --
     3 --	tek.class.loona.util - LOona utilities
     4 --	Written by Timm S. Mueller <tmueller at neoscientists.org>
     5 --	See copyright notice in COPYRIGHT
     6 --
     7 
     8 local utf8values = require "tek.lib".utf8values
     9 local posix = require "tek.lib.posix"
    10 local assert, os_time, os_remove = assert, os.time, os.remove
    11 local insert, concat, char = table.insert, table.concat, string.char
    12 
    13 module "tek.class.loona.util"
    14 _VERSION = "LOona utilities 4.2"
    15 
    16 -------------------------------------------------------------------------------
    17 --	readdir: iterate directory contents
    18 -------------------------------------------------------------------------------
    19 
    20 function readdir(path)
    21 	assert(path)
    22 	local d, msg = posix.opendir(path)
    23 	assert(d, msg and "Cannot open directory '" .. path .. "' : " .. msg)
    24 	return function()
    25 		local e
    26 		repeat
    27 			e = d:read()
    28 		until e ~= "." and e ~= ".."
    29 		return e
    30 	end
    31 end
    32 
    33 -------------------------------------------------------------------------------
    34 --	expire: remove directory entries matching a pattern whose atime is
    35 --	older than maxtime seconds
    36 -------------------------------------------------------------------------------
    37 
    38 function expire(dir, pat, maxtime)
    39 	assert(dir, "No directory")
    40 	assert(pat, "No pattern")
    41 	assert(maxtime, "No maxtime")
    42 	if posix.stat(dir, "mode") == "directory" then
    43 		local nowtime = os_time()
    44 		for e in readdir(dir) do
    45 			if not pat or e:match(pat) then
    46 				local fname = dir .. "/" .. e
    47 				-- assuming here that if an entry is a dangling softlink
    48 				-- (in which case stat() returns nil), it can be deleted anyway
    49 				local atime = posix.stat(fname, "access")
    50 				if not atime or (nowtime - atime > maxtime) then
    51  					local res, msg = os_remove(fname)
    52  					assert(res, msg and "Failed to delete item : " .. msg)
    53 				end
    54  			end
    55  		end
    56  		return true
    57 	end
    58 end
    59 
    60 -------------------------------------------------------------------------------
    61 --	encodeform: encode for forms (display '<', '>', '&', '"' literally)
    62 -------------------------------------------------------------------------------
    63 
    64 function encodeform(s)
    65 	local tab = { }
    66 	if s then
    67 		for c in utf8values(s) do
    68 			if c == 34 then
    69 				insert(tab, "&quot;")
    70 			elseif c == 38 then
    71 				insert(tab, "&amp;")
    72 			elseif c == 60 then
    73 				insert(tab, "&lt;")
    74 			elseif c == 62 then
    75 				insert(tab, "&gt;")
    76 			elseif c == 91 or c == 93 or c > 126 then
    77 				insert(tab, ("&#%03d;"):format(c))
    78 			else
    79 				insert(tab, char(c))
    80 			end
    81 		end
    82 	end
    83 	return concat(tab)
    84 end