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