mod_storage_internal_ng/mod_storage_internal_ng.lua
author Kim Alvefur <zash@zash.se>
Sun, 25 Jan 2015 13:04:02 +0100
changeset 1593 3e4d15ae2133
permissions -rw-r--r--
mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1593
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
-- Copyright (C) 2014-2015 Kim Alvefur
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
--
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
-- This file is MIT/X11 licensed.
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
local tonumber = tonumber;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
local tostring = tostring;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
local type = type;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
local io_open = io.open;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
local os_remove = os.remove;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
local os_rename = os.rename;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
local noop = function () end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
local id = function (x) return x end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
local lfs = require"lfs";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
local st = require"util.stanza";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
local xml = require"util.xml";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
local sha256 = require"util.hashes".sha256;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
local datetime = require"util.datetime";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
local path_join = require"util.paths".join;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
local http = require"util.http";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
local encode, decode = import("util.serialization", "serialize", "deserialize");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
local decode_file = encoder.decode_file;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
local log = module._log;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
local base_path = prosody.paths.data;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
local suffix = ".dat";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
-- No path escaping, assume no forbidden chars in names
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
local function getpath(...)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
	return path_join(base_path, module.host, ...);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
local did_mkdir_already = {};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
local function mkdir(path)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
	if not did_mkdir_already[path] then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    39
		did_mkdir_already[path] = true;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    40
		return lfs.mkdir(path);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    43
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
function module.load()
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    45
	mkdir(getpath()); -- Create base dir for host
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    46
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    47
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
-- Cleanup
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
local function cleanup(scratch, f, msg)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
	if f then f:close(); end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
	os_remove(scratch);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    52
	return nil, msg;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    54
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    55
local function atomic_store(filename, data)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    56
	local scratch = filename.."~";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    57
	local f, ok, msg;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    58
	f, msg = io_open(scratch, "wb");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    59
	if not f then return cleanup(scratch, f, msg) end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    60
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    61
	ok, msg = f:write(data);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    62
	if not ok then return cleanup(scratch, f, msg) end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    63
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    64
	ok, msg = f:close();
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    65
	if not ok then return cleanup(scratch, f, msg) end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    66
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    67
	return os_rename(scratch, filename);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    68
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    69
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    70
local function read_fh(fh, max_length)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    71
	return decode(fh:read(max_length or "*a"));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    72
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    73
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    74
local function read_file(path, max_length)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    75
	local f, err = io_open(path, "rb");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    76
	if not f then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    77
		return f, err;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    78
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    79
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    80
	local data = read_fh(fh, max_length);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
	f:close();
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    82
	return data;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    83
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    84
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    85
local function log_error(path, store, err, user)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    86
	local mode = lfs.attributes(path, "mode");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    87
	if not mode then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    88
		log("debug", "Assuming empty %s storage ('%s') for user: %s", store, err, user or "nil");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    89
		return nil;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    90
	else -- file exists, but can't be read
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    91
		-- TODO more detailed error checking and logging?
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    92
		log("error", "Failed to load %s storage ('%s') for user: %s", store, err, user or "nil");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    93
		return nil, "Error reading storage";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    94
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    95
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    96
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    97
local function h(s)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    98
	return sha256(s, true):sub(1, 2);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    99
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   100
local function new_keyval_store(store)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   101
	local driver = { store = store, typ = "keyval" };
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   102
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   103
	function driver:get(user)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   104
		user = user or "@";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   105
		local path = getpath(user, store) .. suffix;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   106
		local data, err = read_file(path);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   107
		if not data then return log_error(path, store, err, user or "<nil>") end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   108
		return data;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   109
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   110
	function driver:set(user, data)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   111
		user = user or "@";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   112
		mkdir(getpath(user));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   113
		local path = getpath(user, store) .. suffix;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   114
		if data == nil then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   115
			if lfs.attributes(path, "mode") == "file" then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   116
				return os_remove(path);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   117
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   118
			return true;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   119
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   120
		data = encode(data);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   121
		return atomic_store(path, data);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   122
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   123
	function driver:users()
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   124
		local next, state = lfs.dir(getpath());
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   125
		return function (state)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   126
			for node in next, state do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   127
				if lfs.attributes(getpath(node, store) .. suffix, "mode") == "file" then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   128
					return node;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   129
				end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   130
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   131
		end, state;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   132
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   133
	return driver;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   134
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   135
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   136
local function new_map_store(store, as_keyval)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   137
	local driver = { store = store, typ = "map" };
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   138
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   139
	-- FIXME Keys may be encoded to values longer than the file system supports
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   140
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   141
	function driver:get(user, key)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   142
		assert(key ~= nil, "map key can't be nil");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   143
		user = user or "@";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   144
		key = http.urlencode(encode(key));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   145
		local path = getpath(user, store, key) .. suffix;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   146
		local data, err = read_file(path);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   147
		if not data then return log_error(path, store, err, user or "<nil>") end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   148
		return data;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   149
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   150
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   151
	function driver:set(user, key, data)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   152
		assert(key ~= nil, "map key can't be nil");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   153
		user = user or "@";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   154
		key = http.urlencode(encode(key));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   155
		mkdir(getpath(user));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   156
		mkdir(getpath(user, store));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   157
		local path = getpath(user, store, key) .. suffix;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   158
		if data == nil then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   159
			return os_remove(path);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   160
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   161
		data = encode(data);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   162
		return atomic_store(path, data);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   163
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   164
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   165
	function driver:keys(user)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   166
		user = user or "@";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   167
		local ok, iter, state, var = pcall(lfs.dir, getpath(user, store));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   168
		if not ok then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   169
			log("warn", iter);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   170
			return noop;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   171
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   172
		return function (state, var)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   173
			local item;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   174
			repeat
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   175
				item = iter(state, var);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   176
				if item == nil then return end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   177
			until lfs.attributes(getpath(user,store, item), "mode") == "file" and item:sub(-#suffix) == suffix;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   178
			return decode(http.urldecode(item:sub(1, -#suffix-1)));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   179
		end, state, var;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   180
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   181
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   182
	function driver:pairs(user)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   183
		local iter, state, var = self:keys(user);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   184
		return function (state, var)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   185
			local key = iter(state, var);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   186
			if key == nil then return end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   187
			return key, self:get(user, key);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   188
		end, state, var;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   189
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   190
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   191
	function driver:empty(user)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   192
		for key in self:keys(user) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   193
			self:set(user, key, nil);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   194
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   195
		return true;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   196
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   197
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   198
	function driver:collect(user)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   199
		local data = {};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   200
		for k,v in self:pairs(user) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   201
			data[k] = v;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   202
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   203
		return data;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   204
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   205
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   206
	if as_keyval then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   207
		local keyval_driver = new_keyval_store(store);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   208
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   209
		function keyval_driver:get(user)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   210
			return driver:collect(user);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   211
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   212
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   213
		function keyval_driver:set(user, data)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   214
			driver:empty(user);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   215
			if data ~= nil then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   216
				for k, v in pairs(data) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   217
					driver:set(user, k, v);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   218
				end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   219
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   220
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   221
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   222
		return keyval_driver;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   223
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   224
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   225
	return driver;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   226
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   227
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   228
-- date utilities
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   229
local function tomorrow(date)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   230
	return datetime.date(datetime.parse(date .. "T12:00:00Z") + 86400);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   231
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   232
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   233
local function yesterday(date)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   234
	return datetime.date(datetime.parse(date .. "T12:00:00Z") - 86400);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   235
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   236
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   237
local function new_archive_store(store)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   238
	local driver = { store = store, typ = "archive" };
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   239
	local fallocate = require"util.pposix".fallocate or function (f, o, l)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   240
		local ok, err = f:write(string.rep(" ", l));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   241
		if not ok then return ok, err end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   242
		return f:seek("set", o);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   243
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   244
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   245
	local index_storage = new_map_store(store);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   246
	local _get = index_storage.get;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   247
	function index_storage:get(user, key)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   248
		local ok, err = _get(self, user, key);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   249
		if not ok then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   250
			log("debug", tostring(err));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   251
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   252
		return ok, err;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   253
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   254
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   255
	local datelists = setmetatable({}, { __mode = 'v'; });
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   256
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   257
	local function get_datelist(user, with)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   258
		local key = with and (user..'\0'..with) or user;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   259
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   260
		local list = datelists[key];
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   261
		if list then return list end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   262
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   263
		if with then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   264
			local datemap = index_storage:get(user, with);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   265
			if datemap then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   266
				list = {};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   267
				local i = 1;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   268
				for date in pairs(datemap) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   269
					list[i], i = date, i+1;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   270
				end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   271
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   272
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   273
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   274
		if not list then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   275
			list = {};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   276
			local ok, iter, state, var = pcall(lfs.dir, getpath(user, store));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   277
			if not ok then return list; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   278
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   279
			local i = 1;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   280
			local filename, mode, date;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   281
			for item in iter, state, var do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   282
				filename = getpath(user, store, item);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   283
				mode = lfs.attributes(filename, "mode");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   284
				if mode == "file" and item:match("^%d%d%d%d%-%d%d%-%d%d") and item:sub(-#suffix) == suffix then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   285
					list[i], i = item:sub(1,10), i+1;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   286
				end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   287
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   288
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   289
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   290
		table.sort(list);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   291
		for i = 1, #list do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   292
			list[ list[i] ] = i;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   293
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   294
		datelists[key] = list;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   295
		return list;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   296
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   297
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   298
	function driver:append(user, key, when, with, value)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   299
		user = user or "@";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   300
		local ok;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   301
		local date = datetime.date(when);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   302
		mkdir(getpath(user));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   303
		mkdir(getpath(user, store));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   304
		local filename = getpath(user, store, date) .. suffix;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   305
		local f, msg = io_open(filename, "r+b");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   306
		if not f then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   307
			f, msg = io_open(filename, "wb");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   308
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   309
		if not f then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   310
			return f, msg;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   311
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   312
		local mt = getmetatable(value);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   313
		local typ = type(value);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   314
		--[[
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   315
		if mt.__freeze then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   316
			value, typ = mt.__freeze(value);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   317
		else--]]
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   318
		if mt == st.stanza_mt then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   319
			value, typ = st.preserialize(value), "stanza";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   320
			-- if false then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   321
				-- value, typ = tostring(value), "xml";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   322
			-- else
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   323
			-- end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   324
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   325
		local pos = f:seek("end");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   326
		key = ("%sp%x"):format(date, pos);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   327
		key = key .. "r".. sha256(user..key, true):sub(1, 8);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   328
		local metadata = encode({ key, datetime.datetime(when), with, typ });
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   329
		local data = encode(value);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   330
		local meta_len, data_len = #metadata, #data;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   331
		local meta_ls, data_ls = ("%d"):format(meta_len), ("%d"):format(data_len);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   332
		local total_ls = ("%d"):format(meta_len + #meta_ls + data_len + #data_ls + 4); -- two newlines?
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   333
		-- ok, msg = fallocate(f, pos, data_len + #len_str * 2 + 3);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   334
		-- if not ok then return ok, msg; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   335
		ok, msg = f:write(meta_ls, "\n", metadata, "\n", data_ls, "\n", data, "\n", total_ls, "\n");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   336
		if not ok then return ok, msg; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   337
		ok, msg = f:close();
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   338
		if not ok then return ok, msg; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   339
		datelists[user] = nil;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   340
		if with then datelists[user .. "\0" .. with] = nil end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   341
		local index = index_storage:get(user, with) or {};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   342
		if not index[date] then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   343
			index[date] = true;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   344
			index_storage:set(user, with, index);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   345
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   346
		return key;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   347
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   348
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   349
	local unfreeze = {
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   350
		xml = require"util.xml".parse;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   351
		stanza = st.deserialize;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   352
	}
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   353
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   354
	local function valid_key(user, key)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   355
		if not key then return nil, "no-key"; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   356
		if sha256(user..key:match("^(.-)r"), 1):sub(1, 8) ~= key:match("r(.*)$") then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   357
			log("warn", "invalid-key-signature");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   358
			-- return nil, "invalid-key-signature";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   359
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   360
		local when = datetime.parse(key:sub(1, 10).."T00:00:00Z");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   361
		if not when then return nil, "invalid-key-date"; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   362
		local pos = key:match("^p(%x+)r", 11);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   363
		pos = tonumber(pos, 16);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   364
		if not pos then return nil, "invalid-key-offset"; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   365
		return when, pos;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   366
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   367
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   368
	function driver:get(user, key)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   369
		local date, seek = valid_key(user, key);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   370
		if not date then return nil, seek; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   371
		date = datetime.date(date);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   372
		local filename = getpath(user, store, date) .. suffix;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   373
		local fh, err = io_open(filename, "rb");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   374
		if not fh then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   375
			return nil, err;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   376
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   377
		fh:seek("set", seek);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   378
		local len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   379
		local meta = fh:read(tonumber(len));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   380
		meta = decode(meta);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   381
		if not meta or meta[1] ~= key then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   382
			fh:close();
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   383
			return nil, "not found";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   384
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   385
		len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   386
		local data = fh:read(tonumber(len));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   387
		fh:close();
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   388
		data = decode(data);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   389
		local when = meta[2];
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   390
		if type(when) == "string" then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   391
			when = datetime.parse(when);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   392
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   393
		return (unfreeze[ meta[4] ] or id)(data), when, meta[3];
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   394
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   395
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   396
	local NULL = {};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   397
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   398
	-- The ones in math doesn't support strings
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   399
	local function max(a, b) return a > b and a or b; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   400
	local function min(a, b) return a < b and a or b; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   401
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   402
	function driver:_update_index(user, query)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   403
		index_storage:empty();
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   404
		local with_index = {};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   405
		local iter, err = self:find(user, query);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   406
		if not iter then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   407
			log("error", tostring(err));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   408
			return nil, err;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   409
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   410
		local date = datetime.date;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   411
		for key, message, when, with in iter do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   412
			log("debug", "Item %s with %s", key, with);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   413
			if with then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   414
				local with_dates = with_index[with];
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   415
				if not with_dates then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   416
					with_dates = {};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   417
					with_index[with] = with_dates;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   418
				end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   419
				when = date(when);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   420
				with_dates[when] = true;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   421
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   422
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   423
		for key, value in pairs(with_index) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   424
			log("debug", "Write index %s", key);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   425
			index_storage:set(user, key, value);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   426
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   427
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   428
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   429
	local function find_sol(fh) -- Find Start of Line
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   430
		local pos;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   431
		repeat
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   432
			pos = fh:seek("cur", -2);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   433
		until not pos or pos == 0 or fh:read(1) == "\n";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   434
		return pos;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   435
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   436
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   437
	function driver:find(user, query)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   438
		query = query or NULL;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   439
		user = user or "@";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   440
		local datelist = get_datelist(user, query.with);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   441
		if not datelist[1] then return noop, 0 end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   442
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   443
		local start = query.start or 0;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   444
		local ending = query["end"] or 0x7fffffff;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   445
		local start_date = max(datelist[1], datetime.date(start));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   446
		local end_date = min(datelist[#datelist], datetime.date(ending));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   447
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   448
		local seek_once = query.reverse and query.before or query.after;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   449
		local date = query.reverse and end_date or start_date;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   450
		if seek_once then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   451
			local t;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   452
			t, seek_once = valid_key(user, seek_once);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   453
			if not t then return nil, seek_once; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   454
			date = datetime.date(t);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   455
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   456
		local limit, results = query.limit, 0;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   457
		local log = function () end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   458
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   459
		if query.reverse then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   460
			-- log("debug", "find first date");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   461
			if seek_once == 0 then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   462
				date = yesterday(date);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   463
				seek_once = nil;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   464
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   465
			while not datelist[date] and date >= start_date do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   466
				date = yesterday(date);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   467
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   468
			-- log("debug", "datelist[%q] = %s", date, tostring(datelist[date]));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   469
			-- log("debug", "%s <= %s → %s", tostring(date), tostring(start_date), tostring(date <= start_date));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   470
			-- log("warn", "reverse");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   471
			return coroutine.wrap(function ()
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   472
				local filename, fh, err, len, pos, meta, data, when;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   473
				-- log("debug", "enter outer loop");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   474
				-- log("debug", "date = %s", tostring(date));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   475
				while date and datelist[date] and date >= start_date do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   476
					-- log("debug", "iterate outer loop");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   477
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   478
					filename = getpath(user, store, date) .. suffix;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   479
					fh, err = io_open(filename, "rb");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   480
					if not fh then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   481
						log("warn", err);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   482
					else
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   483
						if seek_once then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   484
							pos = fh:seek("set", seek_once);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   485
							seek_once = nil;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   486
						else
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   487
							fh:seek("end");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   488
						end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   489
						pos = find_sol(fh);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   490
						if pos and pos > 1 then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   491
							len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   492
							pos = fh:seek("cur", -tonumber(len)-1);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   493
							pos = find_sol(fh);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   494
						end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   495
						if pos < 2 then pos = fh:seek("set", 0); end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   496
						len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   497
						log("debug", "enter inner loop");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   498
						while len do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   499
							meta = fh:read(tonumber(len));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   500
							fh:seek("cur", 1);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   501
							meta = meta and decode(meta);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   502
							if not meta then break; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   503
							len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   504
							-- log("debug", "{ %q, %d, %q, %q, <%s> }", data[1], data[2], data[3], data[4], type(data[5]));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   505
							when = meta[2];
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   506
							if type(when) == "string" then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   507
								when = datetime.parse(when);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   508
							end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   509
							if when >= start and when <= ending and (not query.with or meta[3] == query.with) then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   510
								data = fh:read(tonumber(len));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   511
								-- log("debug", "data is %s:%s", type(data), tostring(data));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   512
								data = decode(data);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   513
								coroutine.yield(meta[1], (unfreeze[ meta[4] ] or id)(data), when, meta[3]);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   514
								results = results + 1;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   515
								if limit and results >= limit then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   516
									-- log("debug", "results => limit");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   517
									return;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   518
								end 
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   519
							end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   520
							if not pos or pos == 0 then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   521
								-- We just read the first entry, proceed to the last in the previous file
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   522
								break
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   523
							end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   524
							pos = fh:seek("set", pos); -- Skip to top of the entry we just parsed
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   525
							pos = find_sol(fh); -- Find previous newline
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   526
							len = fh:read("*l"); -- Read the trailing length (meta + data)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   527
							pos = len and fh:seek("cur", -tonumber(len) - #len);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   528
							pos = find_sol(fh);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   529
							if not pos or pos == 1 then fh:seek("set", 0); pos = nil; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   530
							len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   531
						end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   532
					end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   533
					fh = fh:close();
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   534
					-- log("debug", "out of inner loop");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   535
					date = datelist[ ( datelist[date] or 0 ) - 1];
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   536
					-- log("debug", "next date is %s", date or "EOF");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   537
				end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   538
				-- log("debug", "out of outer loop");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   539
			end);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   540
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   541
		else
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   542
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   543
			-- log("debug", "find start date");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   544
			while not datelist[date] and date <= end_date do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   545
				date = tomorrow(date);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   546
				-- log("debug", "datelist[%q]", date);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   547
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   548
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   549
			return coroutine.wrap(function ()
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   550
				local filename, fh, err, len, pos, meta, data, when;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   551
				-- log("debug", "enter outer loop");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   552
				-- log("debug", "date = %s", tostring(date));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   553
				while date and datelist[date] and date <= end_date do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   554
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   555
					filename = getpath(user, store, date) .. suffix;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   556
					-- log("debug", "filename = %q", filename);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   557
					fh, err = io_open(filename, "rb");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   558
					if not fh then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   559
						log("warn", err);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   560
					else
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   561
						if seek_once then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   562
							fh:seek("set", seek_once);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   563
							len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   564
							fh:seek("cur", tonumber(len) + 1);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   565
							len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   566
							fh:seek("cur", tonumber(len) + 1);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   567
							fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   568
							seek_once = nil;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   569
						end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   570
						-- pos = fh:seek();
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   571
						len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   572
						-- log("debug", "enter inner loop");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   573
						while tonumber(len) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   574
							-- log("debug", "len is %s:%s", type(len), tostring(len));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   575
							meta = fh:read(tonumber(len));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   576
							fh:seek("cur", 1);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   577
							-- log("debug", "meta is %s:%s", type(meta), tostring(meta));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   578
							meta = meta and decode(meta);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   579
							if not meta then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   580
								-- log("debug", "no metadata?");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   581
								break;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   582
							end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   583
							len = fh:read("*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   584
							-- log("debug", "{ %q, %s, %q, %q } + #%d", meta[1], meta[2], meta[3], meta[4], len);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   585
							when = meta[2];
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   586
							if type(when) == "string" then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   587
								when = datetime.parse(when);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   588
							end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   589
							-- log("debug", "when = %s", tostring(when))
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   590
							-- log("debug", "start = %s", tostring(start))
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   591
							-- log("debug", "ending = %s", tostring(ending))
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   592
							if when >= start and when <= ending and (not query.with or meta[3] == query.with) and not seek_once then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   593
								-- log("debug", "len is %s:%s", type(len), tostring(len));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   594
								data = fh:read(tonumber(len));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   595
								fh:seek("cur", 1); fh:read("*l"); -- Go past the trailing length line
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   596
								-- log("debug", "data is %s:%s", type(data), tostring(data));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   597
								data = decode(data);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   598
								coroutine.yield(meta[1], (unfreeze[ meta[4] ] or id)(data), when, meta[3]);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   599
								results = results + 1;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   600
								if limit and results >= limit then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   601
									-- log("debug", "results => limit");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   602
									return;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   603
								end 
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   604
							else
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   605
								fh:seek("cur", tonumber(len)+1);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   606
								-- log("debug", "%q", fh:read"*l");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   607
							end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   608
							if seek_once then seek_once = nil; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   609
							len = fh:read"*l";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   610
							-- log("debug", "len = %s", tostring(len));
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   611
						end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   612
					end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   613
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   614
					date = datelist[ ( datelist[date] or -1 ) + 1];
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   615
					-- log("debug", "next date is %s", date or "EOF");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   616
				end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   617
				-- log("debug", "out of outer loop");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   618
			end);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   619
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   620
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   621
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   622
	function driver:delete(user, query)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   623
		user = user or "@";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   624
		if query and query.with then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   625
			return nil, "not-implemented"; -- More complicated
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   626
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   627
		query = query or NULL;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   628
		local dstart, dend = "0000-00-00", "9999-99-99";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   629
		if query["start"] then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   630
			dstart = datetime.date(query["start"]);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   631
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   632
		if query["end"] then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   633
			dend = datetime.date(query["end"]);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   634
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   635
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   636
		for i, date in ipairs(get_datelist(user)) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   637
			if date > dstart and date < dend then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   638
				os_remove(getpath(user, store, date) .. suffix);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   639
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   640
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   641
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   642
		-- The expensive part
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   643
		local dates, changed;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   644
		for key in index_storage:keys(user) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   645
			changed = false;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   646
			dates = index_storage:get(user, key);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   647
			for date in pairs(dates) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   648
				if date > dstart and date < dend then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   649
					dates[date] = nil;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   650
					changed = true;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   651
				end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   652
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   653
			if changed then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   654
				if next(dates) == nil then dates = nil; end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   655
				index_storage:set(user, key, dates);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   656
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   657
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   658
		-- driver:_update_index(user)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   659
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   660
	return driver;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   661
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   662
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   663
local drivers = {
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   664
	keyval = new_keyval_store;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   665
	-- map = new_map_store;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   666
	archive = new_archive_store;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   667
};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   668
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   669
local pseudomaps = {
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   670
	private = module:get_host_type() == "local", -- Private XML storage
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   671
	persistent = module:get_host_type() == "component", -- Room persistence
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   672
};
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   673
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   674
function open(_, store, typ)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   675
	typ = typ or "keyval";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   676
	-- log("debug", "open(%q, %q, %q)", module.name, store, typ);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   677
	if pseudomaps[store] then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   678
		return new_map_store(store, typ == "keyval");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   679
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   680
	if not drivers[typ] then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   681
		return nil, "unsupported-store";
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   682
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   683
	return drivers[typ](store);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   684
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   685
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   686
local function rm(path)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   687
	did_mkdir_already[path] = nil;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   688
	os_remove(path);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   689
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   690
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   691
function purge(_, user)
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   692
	for item in lfs.dir(getpath(user)) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   693
		local filename = getpath(user, item);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   694
		local mode = lfs.attributes(filename, "mode");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   695
		if mode == "file" and item:sub(-#suffix) == suffix then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   696
			rm(filename);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   697
		elseif mode == "directory" and item ~= "." and item ~= ".." then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   698
			-- map or archive
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   699
			for mapitem in lfs.dir(getpath(user, item)) do
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   700
				local filename = getpath(user, item, mapitem);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   701
				local mode = lfs.attributes(filename, "mode");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   702
				if mode == "file" and mapitem:sub(-#suffix) == suffix then
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   703
					rm(filename);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   704
				end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   705
			end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   706
			rm(filename);
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   707
		end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   708
	end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   709
	return true;
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   710
end
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   711
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   712
module:provides("storage");
3e4d15ae2133 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   713