-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.lua
More file actions
426 lines (278 loc) · 8.67 KB
/
Copy pathsync.lua
File metadata and controls
426 lines (278 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
function SyncInit()
local sync={}
sync.import_hashes={}
sync.hashes_loaded=false
sync.hashes_changed=false
-- load a list of hashes of files we imported so we don't try to import the same file again
sync.load_hashes=function(self)
local str, toks, key, item
local S
if self.hashes_loaded == false
then
S=stream.STREAM(config:get("sync_in") .. "/imported.hashes", "r")
if S ~= nil
then
str=S:readln()
while str ~= nil
do
str=strutil.trim(str)
toks=strutil.TOKENIZER(str, " ", "q")
key=toks:next()
item={}
item.when=toks:next()
item.hash=toks:remaining()
self.import_hashes[key]=item
str=S:readln()
end
S:close()
self.hashes_loaded=true
end
end
return(import_hashes)
end
-- save a list of hashes of files we imported so we don't try to import the same file again
sync.save_hashes=function(self)
local S
local key, item
if self.hashes_changed == true
then
S=stream.STREAM(config:get("sync_in") .. "/imported.hashes", "w")
if S ~= nil
then
for key, item in pairs(self.import_hashes)
do
S:writeln(key .. " " .. item.when .. " " .. item.hash .. "\n")
end
S:close()
end
end
end
sync.add_hash=function(self, name, hash, lockbox)
local item={}
item.when=time.format("%Y-%m-%dT%H:%M:%S")
item.hash=hash
self.import_hashes[name]=item
self.hashes_changed=true
end
sync.del_hash=function(self, path)
fname=filesys.basename(path)
self.import_hashes[fname]=nil
end
sync.check_hash=function(self, path)
local fname, fhash, item
fname=filesys.basename(path)
fhash=hash.hashfile(path, "sha1", "p64")
item=self.import_hashes[fname]
if item ~= nil and item.hash == fhash then return true end
self:add_hash(fname, fhash)
return false
end
sync.display_last_syncs=function(self)
local when, diff, str
local lines=0
sync:load_hashes()
for key, item in pairs(self.import_hashes)
do
when=time.tosecs("%Y-%m-%dT%H:%M:%S", item.when)
str=""
diff=(time.secs() - when)
if diff > (3600 * 24 * 30) then str=str.."~er"
elseif diff > (3600 * 24 * 7) then str=str.."~ey"
elseif diff < (3600 * 24) then str=str.."~e"
end
str=str..item.when.."~0 "
str=str..item.hash.." "
str=str..key.." "
str=str.."\n"
lines=lines+1
Term:puts(str)
end
if lines == 0 then print("No sync file imports have been recorded.") end
end
sync.examine_import_file=function(self, path, password)
local info
-- this will only happen once, because it internally checks a flag
-- to see if already loaded
self:load_hashes()
info=LockboxFromFile(path, password)
if info ~= nil then info.already_loaded=self:check_hash(path)
else ui:error("cant open sync file: "..tostring(path))
end
return info
end
sync.import_item=function(self, existing, new)
local exist_time, new_time
local changed=false
exist_time=time.tosecs("%Y/%m/%dT%H:%M:%S", existing.updated)
new_time=time.tosecs("%Y/%m/%dT%H:%M:%S", new.updated)
if new_time > exist_time
then
existing.updated=new.updated
existing.value=new.value
changed=true
end
return changed
end
sync.import=function(self, box, other)
local key, item
local changed=false
for key,item in pairs(other.items)
do
existing=box.items[key]
if existing ~= nil then changed=self:import_item(existing, item)
else
box.items[key]=item
changed=true
end
end
return changed
end
-- import items to an EXISTING lockbox
sync.update_box=function(self, box, path)
local changed=false
local tmp
if GlobalDebug == true then io.stderr:write("sync:update_box '".. tostring(box.name) .. " from '" .. tostring(path) .. "' with password: '" .. tostring(box.password) .. "'\n") end
tmp=self:examine_import_file(path)
if tmp ~= nil
then
--if box already has a password, then use it
tmp.password=box.password
if tmp.already_loaded == true then print("ignoring previously imported file '"..path.."'")
elseif tmp:load(false) == false then ui:error("Failed to decrypt import file. Wrong password?")
elseif hosts:check_version(tmp.machine_id, tmp.name, tmp.version) == false then print("import file has older version than lockbox '" .. box.name .. "' ".. tostring(tmp.version) .." < "..tostring(box.version))
else
print("sync importing..." ..path)
if strutil.strlen(box.password) == 0 then box.password=tmp.password end
if strutil.strlen(box.passhint) == 0 then box.passhint=tmp.passhint end
self:import(box, tmp)
changed=true
end
tmp:destroy()
end
return(changed)
end
-- for each EXISTING lockbox, try to find an import
-- file that can be used to update it
sync.update=function(self, box)
local path, files, i, tmp
local changed=false
path=config:get("sync_in") .. "*-".. box.name .. ".sync"
files=get_filelist(path)
if GlobalDebug == true then io.stderr:write("update lockbox: '" .. box.name .. "' from '" .. tostring(path) .."' ".. tostring(#files) .. " files found.\n") end
for i,path in ipairs(files)
do
changed=self:update_box(box, path)
end
if changed == true then box:save()
else
if GlobalDebug == true then io.stderr:write("update lockbox: '" .. box.name .. "' lockbox unchanged. not saving.\n") end
end
return changed
end
-- for an EXISTING lockbox, with supplied name, do update
sync.update_by_name=function(self, name)
local box
box=lockboxes:find(name)
self:update(box)
end
sync.import_open_or_create=function(self, name, password, passhint)
local box
box=lockboxes:find(name)
if box == nil
then
box=LockboxCreate(name, nil, password, passhint)
if box == nil then return nil,"unable to create lockbox" end
else
if box:load(false) == false
then
ui:error("incorrect password for destination lockbox")
box.password=ui:ask_password("Enter password for destination lockbox '"..box.name.."': ", box.passhint)
if box:load(false) == false then return nil, "incorrect password for destination lockbox, abandoning sync." end
end
end
return box
end
-- import a single file, use name of the imported file to
-- update or create a lockbox for it, so here the destination box might not exist
sync.import_file=function(self, path)
local tmp, box
-- we must open tmp file in order to get a 'trust worthy' name for it
tmp=self:examine_import_file(path)
if tmp == nil then return end
-- always display this so the user knows what is happening and which lockbox is being dealt with
print("sync from file '" .. tostring(path) .. "' to lockbox '" .. tmp.name .. "'")
if tmp.already_loaded == true then print("ignoring previously imported file '"..path.."'")
elseif tmp:load(false) == false then ui:error("Failed to decrypt import file. Wrong password?")
else
box,error=self:import_open_or_create(tmp.name, tmp.password, tmp.passhint)
if box == nil
then
self:del_hash(path)
ui:error(error)
return false
end
--[[
if strutil.strlen(box.password) > 0 then tmp.password=box.password end
if strutil.strlen(box.passhint) > 0 then tmp.password=box.passhint end
]]--
if self:import(box, tmp) == true then box:save()
end
end
tmp:destroy()
return true
end
-- import a bunch of files, use the name of the imported file to
-- update or create a lockbox for it, so here the destination box might not exist
sync.import_files=function(self, item_list)
local i, result, error, toks, fname, fhash
local glob, path
self:load_hashes()
if strutil.strlen(item_list) == 0 then item_list=config:get("sync_in") .. "/*.sync" end
files=get_filelist(item_list)
if GlobalDebug == true then io.stderr:write("sync from '" .. item_list .. "' " .. tostring(#files) .. " files found.") end
print(tostring(#files) .. " sync files for import")
for i, path in ipairs(files)
do
self:import_file(path)
end
self:save_hashes(import_hashes)
end
-- Functions below this point relate to exporting or 'sending' files
sync.send_path=function(self, path, dir)
local dst_path, final_path, name
if strutil.strlen(dir) > 0
then
name=filesys.filename(path)
dst_path=dir .. sys.hostname() .. "-" .. name ..".tmp"
if string.sub(dir, 1, 4) == "ssh:" then SSHRunCommand("mkdir -p", dir)
else filesys.mkdirPath(dst_path)
end
filesys.copy(path, dst_path)
final_path=dir.. sys.hostname() .. "-" .. name ..".sync"
if string.sub(dir, 1, 4) == "ssh:" then SSHRunCommand("mv -f ", dst_path, final_path)
else filesys.rename(dst_path, final_path)
end
print("send lockbox '" .. name .."' at '"..path.."' to '"..final_path.."'")
end
end
sync.send=function(self, box, dir)
self:send_path(box.path, dir)
end
sync.export_items=function(self, cmd)
local list, dir
dir=cmd.dir
if strutil.strlen(dir)==0
then
dir=cmd.box
if strutil.strlen(dir)==0 then dir=config:get("sync_out") end
list=lockboxes:paths()
for i,item in pairs(list)
do
self:send_path(item, dir)
end
else
self:send_path(cmd.box, cmd.dir)
end
end
return sync
end