Comparison

util/datamanager.lua @ 206:e30d0e30a0ff

Datamanager now deletes files with no data
author Waqas Hussain <waqas20@gmail.com>
date Mon, 03 Nov 2008 07:50:09 +0500
parent 182:f5cb6b5a6eb7
child 247:681b29aa134f
comparison
equal deleted inserted replaced
205:4ca91e0551b2 206:e30d0e30a0ff
3 local pairs = pairs; 3 local pairs = pairs;
4 local char = string.char; 4 local char = string.char;
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall; 5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
6 local log = log; 6 local log = log;
7 local io_open = io.open; 7 local io_open = io.open;
8 local os_remove = os.remove;
8 local tostring = tostring; 9 local tostring = tostring;
9 local error = error; 10 local error = error;
11 local next = next;
10 12
11 local indent = function(f, i) 13 local indent = function(f, i)
12 for n = 1, i do 14 for n = 1, i do
13 f:write("\t"); 15 f:write("\t");
14 end 16 end
91 end 93 end
92 return ret; 94 return ret;
93 end 95 end
94 96
95 function store(username, host, datastore, data) 97 function store(username, host, datastore, data)
98 if not data then
99 data = {};
100 end
101 -- save the datastore
96 local f, msg = io_open(getpath(username, host, datastore), "w+"); 102 local f, msg = io_open(getpath(username, host, datastore), "w+");
97 if not f then 103 if not f then
98 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); 104 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
99 return nil; 105 return;
100 end 106 end
101 f:write("return "); 107 f:write("return ");
102 simplesave(f, data, 1); 108 simplesave(f, data, 1);
103 f:close(); 109 f:close();
110 if not next(data) then -- try to delete empty datastore
111 os_remove(getpath(username, host, datastore));
112 end
113 -- we write data even when we are deleting because lua doesn't have a
114 -- platform independent way of checking for non-exisitng files
104 return true; 115 return true;
105 end 116 end
106 117
107 return _M; 118 return _M;