Software / code / prosody
Comparison
tools/migration/migrator/prosody_files.lua @ 4216:ff80a8471e86
tools/migration/*: Numerous changes and restructuring, and the addition of a Makefile
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 26 Feb 2011 00:23:48 +0000 |
| parent | 4197:tools/migration/prosody_files.lua@bef732980436 |
| child | 4239:69fe5fd861e7 |
comparison
equal
deleted
inserted
replaced
| 4214:1674cd17557c | 4216:ff80a8471e86 |
|---|---|
| 1 | |
| 2 local print = print; | |
| 3 local assert = assert; | |
| 4 local setmetatable = setmetatable; | |
| 5 local tonumber = tonumber; | |
| 6 local char = string.char; | |
| 7 local coroutine = coroutine; | |
| 8 local lfs = require "lfs"; | |
| 9 local loadfile = loadfile; | |
| 10 local setfenv = setfenv; | |
| 11 local pcall = pcall; | |
| 12 local mtools = require "migrator.mtools"; | |
| 13 local next = next; | |
| 14 local pairs = pairs; | |
| 15 local json = require "util.json"; | |
| 16 | |
| 17 prosody = {}; | |
| 18 local dm = require "util.datamanager" | |
| 19 | |
| 20 module "prosody_files" | |
| 21 | |
| 22 local function is_dir(path) return lfs.attributes(path, "mode") == "directory"; end | |
| 23 local function is_file(path) return lfs.attributes(path, "mode") == "file"; end | |
| 24 local function clean_path(path) | |
| 25 return path:gsub("\\", "/"):gsub("//+", "/"); | |
| 26 end | |
| 27 local encode, decode; do | |
| 28 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); | |
| 29 decode = function (s) return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); end | |
| 30 encode = function (s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end | |
| 31 end | |
| 32 local function decode_dir(x) | |
| 33 if x:gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then | |
| 34 return decode(x); | |
| 35 end | |
| 36 end | |
| 37 local function decode_file(x) | |
| 38 if x:match(".%.dat$") and x:gsub("%.dat$", ""):gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then | |
| 39 return decode(x:gsub("%.dat$", "")); | |
| 40 end | |
| 41 end | |
| 42 local function prosody_dir(path, ondir, onfile, ...) | |
| 43 for x in lfs.dir(path) do | |
| 44 local xpath = path.."/"..x; | |
| 45 if decode_dir(x) and is_dir(xpath) then | |
| 46 ondir(xpath, x, ...); | |
| 47 elseif decode_file(x) and is_file(xpath) then | |
| 48 onfile(xpath, x, ...); | |
| 49 end | |
| 50 end | |
| 51 end | |
| 52 | |
| 53 local function handle_root_file(path, name) | |
| 54 --print("root file: ", decode_file(name)) | |
| 55 coroutine.yield { user = nil, host = nil, store = decode_file(name) }; | |
| 56 end | |
| 57 local function handle_host_file(path, name, host) | |
| 58 --print("host file: ", decode_dir(host).."/"..decode_file(name)) | |
| 59 coroutine.yield { user = nil, host = decode_dir(host), store = decode_file(name) }; | |
| 60 end | |
| 61 local function handle_store_file(path, name, store, host) | |
| 62 --print("store file: ", decode_file(name).."@"..decode_dir(host).."/"..decode_dir(store)) | |
| 63 coroutine.yield { user = decode_file(name), host = decode_dir(host), store = decode_dir(store) }; | |
| 64 end | |
| 65 local function handle_host_store(path, name, host) | |
| 66 prosody_dir(path, function() end, handle_store_file, name, host); | |
| 67 end | |
| 68 local function handle_host_dir(path, name) | |
| 69 prosody_dir(path, handle_host_store, handle_host_file, name); | |
| 70 end | |
| 71 local function handle_root_dir(path) | |
| 72 prosody_dir(path, handle_host_dir, handle_root_file); | |
| 73 end | |
| 74 | |
| 75 local function decode_user(item) | |
| 76 local userdata = { | |
| 77 user = item[1].user; | |
| 78 host = item[1].host; | |
| 79 stores = {}; | |
| 80 }; | |
| 81 for i=1,#item do -- loop over stores | |
| 82 local result = {}; | |
| 83 local store = item[i]; | |
| 84 userdata.stores[store.store] = store.data; | |
| 85 store.user = nil; store.host = nil; store.store = nil; | |
| 86 end | |
| 87 return userdata; | |
| 88 end | |
| 89 | |
| 90 function reader(input) | |
| 91 local path = clean_path(assert(input.path, "no input.path specified")); | |
| 92 assert(is_dir(path), "input.path is not a directory"); | |
| 93 local iter = coroutine.wrap(function()handle_root_dir(path);end); | |
| 94 -- get per-user stores, sorted | |
| 95 local iter = mtools.sorted { | |
| 96 reader = function() | |
| 97 local x = iter(); | |
| 98 if x then | |
| 99 dm.set_data_path(path); | |
| 100 x.data = assert(dm.load(x.user, x.host, x.store)); | |
| 101 return x; | |
| 102 end | |
| 103 end; | |
| 104 sorter = function(a, b) | |
| 105 local a_host, a_user, a_store = a.host or "", a.user or "", a.store or ""; | |
| 106 local b_host, b_user, b_store = b.host or "", b.user or "", b.store or ""; | |
| 107 return a_host > b_host or (a_host==b_host and a_user > b_user) or (a_host==b_host and a_user==b_user and a_store > b_store); | |
| 108 end; | |
| 109 }; | |
| 110 -- merge stores to get users | |
| 111 iter = mtools.merged(iter, function(a, b) | |
| 112 return (a.host == b.host and a.user == b.user); | |
| 113 end); | |
| 114 | |
| 115 return function() | |
| 116 local x = iter(); | |
| 117 return x and decode_user(x); | |
| 118 end | |
| 119 end | |
| 120 | |
| 121 function writer(output) | |
| 122 local path = clean_path(assert(output.path, "no output.path specified")); | |
| 123 assert(is_dir(path), "output.path is not a directory"); | |
| 124 return function(item) | |
| 125 if not item then return; end -- end of input | |
| 126 dm.set_data_path(path); | |
| 127 for store, data in pairs(item.stores) do | |
| 128 assert(dm.store(item.user, item.host, store, data)); | |
| 129 end | |
| 130 end | |
| 131 end | |
| 132 | |
| 133 return _M; |