Mercurial > prosody-hg
comparison util/datamanager.lua @ 643:8ff454831f7d
Moved directory auto-creation to datamanager
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Thu, 25 Dec 2008 06:35:05 +0500 |
| parents | 3712d36b6d25 |
| children | ce772e283992 |
comparison
equal
deleted
inserted
replaced
| 642:0ae8584ba3e7 | 643:8ff454831f7d |
|---|---|
| 24 local char = string.char; | 24 local char = string.char; |
| 25 local loadfile, setfenv, pcall = loadfile, setfenv, pcall; | 25 local loadfile, setfenv, pcall = loadfile, setfenv, pcall; |
| 26 local log = require "util.logger".init("datamanager"); | 26 local log = require "util.logger".init("datamanager"); |
| 27 local io_open = io.open; | 27 local io_open = io.open; |
| 28 local os_remove = os.remove; | 28 local os_remove = os.remove; |
| 29 local io_popen = io.popen; | |
| 29 local tostring, tonumber = tostring, tonumber; | 30 local tostring, tonumber = tostring, tonumber; |
| 30 local error = error; | 31 local error = error; |
| 31 local next = next; | 32 local next = next; |
| 32 local t_insert = table.insert; | 33 local t_insert = table.insert; |
| 33 local append = require "util.serialization".append; | 34 local append = require "util.serialization".append; |
| 35 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end | |
| 34 | 36 |
| 35 module "datamanager" | 37 module "datamanager" |
| 36 | 38 |
| 37 ---- utils ----- | 39 ---- utils ----- |
| 38 local encode, decode; | 40 local encode, decode; |
| 46 encode = function (s) | 48 encode = function (s) |
| 47 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); | 49 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); |
| 48 end | 50 end |
| 49 end | 51 end |
| 50 | 52 |
| 53 local _mkdir = {}; | |
| 54 local function mkdir(path) | |
| 55 path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here | |
| 56 if not _mkdir[path] then | |
| 57 local x = io_popen("mkdir \""..path.."\" 2>&1"):read("*a"); | |
| 58 _mkdir[path] = true; | |
| 59 end | |
| 60 return path; | |
| 61 end | |
| 62 | |
| 63 local data_path = "data"; | |
| 64 | |
| 51 ------- API ------------- | 65 ------- API ------------- |
| 52 | 66 |
| 53 local data_path = "data"; | |
| 54 function set_data_path(path) | 67 function set_data_path(path) |
| 55 log("info", "Setting data path to %s", path); | 68 log("info", "Setting data path to %s", path); |
| 56 data_path = path; | 69 data_path = path; |
| 57 end | 70 end |
| 58 | 71 |
| 59 function getpath(username, host, datastore, ext) | 72 function getpath(username, host, datastore, ext, create) |
| 60 ext = ext or "dat"; | 73 ext = ext or "dat"; |
| 74 host = host and encode(host); | |
| 75 username = username and encode(username); | |
| 61 if username then | 76 if username then |
| 62 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext); | 77 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end |
| 78 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext); | |
| 63 elseif host then | 79 elseif host then |
| 64 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext); | 80 if create then mkdir(mkdir(data_path).."/"..host); end |
| 81 return format("%s/%s/%s.%s", data_path, host, datastore, ext); | |
| 65 else | 82 else |
| 83 if create then mkdir(data_path); end | |
| 66 return format("%s/%s.%s", data_path, datastore, ext); | 84 return format("%s/%s.%s", data_path, datastore, ext); |
| 67 end | 85 end |
| 68 end | 86 end |
| 69 | 87 |
| 70 function load(username, host, datastore) | 88 function load(username, host, datastore) |
| 85 function store(username, host, datastore, data) | 103 function store(username, host, datastore, data) |
| 86 if not data then | 104 if not data then |
| 87 data = {}; | 105 data = {}; |
| 88 end | 106 end |
| 89 -- save the datastore | 107 -- save the datastore |
| 90 local f, msg = io_open(getpath(username, host, datastore), "w+"); | 108 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+"); |
| 91 if not f then | 109 if not f then |
| 92 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); | 110 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); |
| 93 return; | 111 return; |
| 94 end | 112 end |
| 95 f:write("return "); | 113 f:write("return "); |
| 104 end | 122 end |
| 105 | 123 |
| 106 function list_append(username, host, datastore, data) | 124 function list_append(username, host, datastore, data) |
| 107 if not data then return; end | 125 if not data then return; end |
| 108 -- save the datastore | 126 -- save the datastore |
| 109 local f, msg = io_open(getpath(username, host, datastore, "list"), "a+"); | 127 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); |
| 110 if not f then | 128 if not f then |
| 111 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); | 129 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); |
| 112 return; | 130 return; |
| 113 end | 131 end |
| 114 f:write("item("); | 132 f:write("item("); |
| 121 function list_store(username, host, datastore, data) | 139 function list_store(username, host, datastore, data) |
| 122 if not data then | 140 if not data then |
| 123 data = {}; | 141 data = {}; |
| 124 end | 142 end |
| 125 -- save the datastore | 143 -- save the datastore |
| 126 local f, msg = io_open(getpath(username, host, datastore, "list"), "w+"); | 144 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+"); |
| 127 if not f then | 145 if not f then |
| 128 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); | 146 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); |
| 129 return; | 147 return; |
| 130 end | 148 end |
| 131 for _, d in ipairs(data) do | 149 for _, d in ipairs(data) do |
