Software /
code /
prosody
Comparison
util/datamanager.lua @ 8299:756a2a00e7e7
util.datamanager: Encode the 'store' path component, preserving underscores
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 08 Oct 2017 18:34:57 +0200 |
parent | 8092:0a1c0f1107d2 |
child | 8555:4f0f5b49bb03 |
comparison
equal
deleted
inserted
replaced
8298:dd9fa511494a | 8299:756a2a00e7e7 |
---|---|
40 end); | 40 end); |
41 | 41 |
42 local _ENV = nil; | 42 local _ENV = nil; |
43 | 43 |
44 ---- utils ----- | 44 ---- utils ----- |
45 local encode, decode; | 45 local encode, decode, store_encode; |
46 do | 46 do |
47 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber(k, 16)); return t[k]; end }); | 47 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber(k, 16)); return t[k]; end }); |
48 | 48 |
49 decode = function (s) | 49 decode = function (s) |
50 return s and (s:gsub("%%(%x%x)", urlcodes)); | 50 return s and (s:gsub("%%(%x%x)", urlcodes)); |
51 end | 51 end |
52 | 52 |
53 encode = function (s) | 53 encode = function (s) |
54 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); | 54 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); |
55 end | |
56 | |
57 -- Special encode function for store names, which historically were unencoded. | |
58 -- All currently known stores use a-z and underscore, so this one preserves underscores. | |
59 store_encode = function (s) | |
60 return s and (s:gsub("[^_%w]", function (c) return format("%%%02x", c:byte()); end)); | |
55 end | 61 end |
56 end | 62 end |
57 | 63 |
58 if not atomic_append then | 64 if not atomic_append then |
59 function atomic_append(f, data) | 65 function atomic_append(f, data) |
117 | 123 |
118 local function getpath(username, host, datastore, ext, create) | 124 local function getpath(username, host, datastore, ext, create) |
119 ext = ext or "dat"; | 125 ext = ext or "dat"; |
120 host = (host and encode(host)) or "_global"; | 126 host = (host and encode(host)) or "_global"; |
121 username = username and encode(username); | 127 username = username and encode(username); |
128 datastore = store_encode(datastore); | |
122 if username then | 129 if username then |
123 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end | 130 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end |
124 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext); | 131 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext); |
125 else | 132 else |
126 if create then mkdir(mkdir(data_path).."/"..host); end | 133 if create then mkdir(mkdir(data_path).."/"..host); end |