/usr/share/lua/5.1/versium/util.lua is in sputnik 12.06.27-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | -----------------------------------------------------------------------------
-- Provides some utility functions for use by versium and its clients.
--
-- (c) 2007, 2008 Yuri Takhteyev (yuri@freewisdom.org)
-- (c) 2007 Hisham Muhammad (quick_LCS())
-- License: MIT/X, see http://sputnik.freewisdom.org/en/License
-----------------------------------------------------------------------------
module(..., package.seeall)
local errors = require"versium.errors"
-----------------------------------------------------------------------------
-- Escapes a node id to make it safe for use as a file name.
--
-- @param id a node id.
-- @return an escaped node id.
-----------------------------------------------------------------------------
function fs_escape_id(id)
assert(id and id:len() > 0)
return fs_escape_id_but_keep_slash(id):gsub("/", "%%2F")
end
-----------------------------------------------------------------------------
-- Escapes a node id to make it safe for use as a file name, but keeps
-- forward slash in it.
--
-- @param id a node id.
-- @return an escaped node id.
-----------------------------------------------------------------------------
function fs_escape_id_but_keep_slash(id, options)
return id:gsub("%%", "%%25"):gsub(":", "%%3A")
end
-----------------------------------------------------------------------------
-- Un-escapes a node id. (See escape_id().)
--
-- @param id an escaped node id.
-- @return the original node id.
-----------------------------------------------------------------------------
function fs_unescape_id(id)
assert(id and id:len() > 0)
return id:gsub("%%2F", "/"):gsub("%%3A", ":"):gsub("%%25", "%%")
end
-----------------------------------------------------------------------------
-- Writes data to a file (to be replaced with atomic write).
--
-- @param path the file path.
-- @param data data to be written to the file.
-- @return nothing
-----------------------------------------------------------------------------
function write_file(path, data, node)
assert(path)
assert(data)
local f, err = io.open(path, "w")
assert(f, errors.could_not_save(node, err))
f:write(data)
f:close()
end
-----------------------------------------------------------------------------
-- An auxiliary function for reading the content of a file. (Throws an a
-- Versium-specific error if something goes wrong.)
--
-- @param path the file path.
-- @return the data read from the file as a string.
-----------------------------------------------------------------------------
function read_file(path, node)
assert(path)
local f, err = io.open(path)
assert(f, errors.could_not_read(node, err))
local data = f:read("*all")
f:close()
return data
end
-----------------------------------------------------------------------------
-- Reads data from a file without complaining if the file doesn't exist.
--
-- @param path the file path.
-- @return the data read from the file or just "" if the file
-- doesn't exist.
-----------------------------------------------------------------------------
function read_file_if_exists(path)
assert(path)
local status, f = pcall(io.open, path)
if status and f then
local data = f:read("*all")
f:close()
return data
else
return ""
end
end
-----------------------------------------------------------------------------
-- Converts a versium time stamp into the requested format. Uses some code
-- from http://lua-users.org/wiki/TimeZone)
--
-- @param timestamp Versium timestamp (string)
-- @param format Lua time format (string)
-- @param tzoffset time zone offset as "+hh:mm" or "-hh:mm"
-- (ISO 8601) or "local" [string, optional, defaults
-- to "local"]
-- @param tzname name/description of the time zone [string, optional,
-- defaults to tzoffset, valid XHTML is ok]
-- @return formatted time (string)
-----------------------------------------------------------------------------
function format_time(timestamp, format, tzoffset, tzname)
if tzoffset == "local" then -- calculate local time zone (for the server)
local now = os.time()
local local_t = os.date("*t", now)
local utc_t = os.date("!*t", now)
local delta = (local_t.hour - utc_t.hour)*60 + (local_t.min - utc_t.min)
local h, m = math.modf( delta / 60)
tzoffset = string.format("%+.4d", 100 * h + 60 * m)
end
tzoffset = tzoffset or "GMT"
format = format:gsub("%%z", tzname or tzoffset)
if tzoffset == "GMT" then
tzoffset = "+0000"
end
tzoffset = tzoffset:gsub(":", "")
local sign = 1
if tzoffset:sub(1,1) == "-" then
sign = -1
tzoffset = tzoffset:sub(2)
elseif tzoffset:sub(1,1) == "+" then
tzoffset = tzoffset:sub(2)
end
tzoffset = sign * (tonumber(tzoffset:sub(1,2))*60 + tonumber(tzoffset:sub(3,4)))*60
return os.date(format, os.time{ year=timestamp:sub(1,4), month=timestamp:sub(6,7),
day=timestamp:sub(9,10), hour=timestamp:sub(12,13),
min=timestamp:sub(15,16), sec=timestamp:sub(18)} + tzoffset)
end
|