PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/migration/migrator/prosody_files.lua

http://github.com/bjc/prosody
Lua | 142 lines | 127 code | 10 blank | 5 comment | 17 complexity | fc000dca402d47c898e4e382ccd63ac9 MD5 | raw file
  1. local print = print;
  2. local assert = assert;
  3. local setmetatable = setmetatable;
  4. local tonumber = tonumber;
  5. local char = string.char;
  6. local coroutine = coroutine;
  7. local lfs = require "lfs";
  8. local loadfile = loadfile;
  9. local pcall = pcall;
  10. local mtools = require "migrator.mtools";
  11. local next = next;
  12. local pairs = pairs;
  13. local json = require "util.json";
  14. local os_getenv = os.getenv;
  15. local error = error;
  16. prosody = {};
  17. local dm = require "util.datamanager"
  18. module "prosody_files"
  19. local function is_dir(path) return lfs.attributes(path, "mode") == "directory"; end
  20. local function is_file(path) return lfs.attributes(path, "mode") == "file"; end
  21. local function clean_path(path)
  22. return path:gsub("\\", "/"):gsub("//+", "/"):gsub("^~", os_getenv("HOME") or "~");
  23. end
  24. local encode, decode; do
  25. local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
  26. decode = function (s) return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); end
  27. encode = function (s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end
  28. end
  29. local function decode_dir(x)
  30. if x:gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then
  31. return decode(x);
  32. end
  33. end
  34. local function decode_file(x)
  35. if x:match(".%.dat$") and x:gsub("%.dat$", ""):gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then
  36. return decode(x:gsub("%.dat$", ""));
  37. end
  38. end
  39. local function prosody_dir(path, ondir, onfile, ...)
  40. for x in lfs.dir(path) do
  41. local xpath = path.."/"..x;
  42. if decode_dir(x) and is_dir(xpath) then
  43. ondir(xpath, x, ...);
  44. elseif decode_file(x) and is_file(xpath) then
  45. onfile(xpath, x, ...);
  46. end
  47. end
  48. end
  49. local function handle_root_file(path, name)
  50. --print("root file: ", decode_file(name))
  51. coroutine.yield { user = nil, host = nil, store = decode_file(name) };
  52. end
  53. local function handle_host_file(path, name, host)
  54. --print("host file: ", decode_dir(host).."/"..decode_file(name))
  55. coroutine.yield { user = nil, host = decode_dir(host), store = decode_file(name) };
  56. end
  57. local function handle_store_file(path, name, store, host)
  58. --print("store file: ", decode_file(name).."@"..decode_dir(host).."/"..decode_dir(store))
  59. coroutine.yield { user = decode_file(name), host = decode_dir(host), store = decode_dir(store) };
  60. end
  61. local function handle_host_store(path, name, host)
  62. prosody_dir(path, function() end, handle_store_file, name, host);
  63. end
  64. local function handle_host_dir(path, name)
  65. prosody_dir(path, handle_host_store, handle_host_file, name);
  66. end
  67. local function handle_root_dir(path)
  68. prosody_dir(path, handle_host_dir, handle_root_file);
  69. end
  70. local function decode_user(item)
  71. local userdata = {
  72. user = item[1].user;
  73. host = item[1].host;
  74. stores = {};
  75. };
  76. for i=1,#item do -- loop over stores
  77. local result = {};
  78. local store = item[i];
  79. userdata.stores[store.store] = store.data;
  80. store.user = nil; store.host = nil; store.store = nil;
  81. end
  82. return userdata;
  83. end
  84. function reader(input)
  85. local path = clean_path(assert(input.path, "no input.path specified"));
  86. assert(is_dir(path), "input.path is not a directory");
  87. local iter = coroutine.wrap(function()handle_root_dir(path);end);
  88. -- get per-user stores, sorted
  89. local iter = mtools.sorted {
  90. reader = function()
  91. local x = iter();
  92. while x do
  93. dm.set_data_path(path);
  94. local err;
  95. x.data, err = dm.load(x.user, x.host, x.store);
  96. if x.data == nil and err then
  97. local p = dm.getpath(x.user, x.host, x.store);
  98. print(("Error loading data at path %s for %s@%s (%s store): %s")
  99. :format(p, x.user or "<nil>", x.host or "<nil>", x.store or "<nil>", err or "<nil>"));
  100. else
  101. return x;
  102. end
  103. x = iter();
  104. end
  105. end;
  106. sorter = function(a, b)
  107. local a_host, a_user, a_store = a.host or "", a.user or "", a.store or "";
  108. local b_host, b_user, b_store = b.host or "", b.user or "", b.store or "";
  109. 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);
  110. end;
  111. };
  112. -- merge stores to get users
  113. iter = mtools.merged(iter, function(a, b)
  114. return (a.host == b.host and a.user == b.user);
  115. end);
  116. return function()
  117. local x = iter();
  118. return x and decode_user(x);
  119. end
  120. end
  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. return _M;