-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmetafs
93 lines (84 loc) · 2.37 KB
/
metafs
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
local unpack = unpack or table.unpack
local oldfs = {}
for k, v in pairs(fs) do
if k == "copy" or k == "move" or k == "delete" or k == "open" then
oldfs[k] = v
end
end
local function isPathMeta(path)
local metapath = string.match((metadata.getMetaFolderForPath(fs.getDir(path))), "/?(.*)")
local path = string.sub(string.match(path, "/?(.*)"), 1, #metapath)
return path == metapath
end
local function getTimeStamp()
return os.day() * 24 + os.time()
end
local function deleteMetadata(path)
if fs.exists(fs.combine(metadata.getMetaFolderForPath(path))) then oldfs.delete(fs.combine(metadata.getMetaFolderForPath(path))) end
end
fs.copy = function(from, to)
if isPathMeta(from) and isPathMeta(to) then
return oldfs.copy(from, to)
elseif isPathMeta(from) then
--to is not in meta
oldfs.copy(from, to)
local timestamp = getTimeStamp()
metadata.save(to, {timestamp = {created = timestamp, modified = timestamp}})
elseif isPathMeta(to) then
return oldfs.copy(from, to)
else
oldfs.copy(from, to)
metadata.save(to, metadata.getForFile(from))
end
end
fs.move = function(from, to)
if isPathMeta(from) and isPathMeta(to) then
return oldfs.move(from, to)
elseif isPathMeta(from) then
oldfs.move(from, to)
local timestamp = getTimeStamp()
metadata.save(to, {timestamp = {created = timestamp, modified = timestamp}})
elseif isPathMeta(to) then
deleteMetadata(from)
return oldfs.move(from, to)
else
oldfs.move(from, to)
metadata.save(to, metadata.getForFile(from))
deleteMetadata(from)
end
end
fs.delete = function(path)
if isPathMeta(path) then
oldfs.delete(path)
else
oldfs.delete(path)
deleteMetadata(path)
end
end
fs.open = function(path, mode)
if isPathMeta(path) then
return oldfs.open(path, mode)
else
local filePath = path
local created = false
if not fs.exists(filePath) then created = true end
local handle = oldfs.open(path, mode)
if handle then
local oldClose = handle.close
handle.close = function()
oldClose()
local data = metadata.getForFile(filePath)
if not data then data = {} end
if not data.timestamp then data.timestamp = {} end
if created then data.timestamp.created = getTimeStamp() end
if mode == "r" or mode == "rb" then
data.timestamp.accessed = getTimeStamp()
else
data.timestamp.modified = getTimeStamp()
end
metadata.save(filePath, data)
end
return handle
end
end
end