-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnestic.lua
executable file
·163 lines (140 loc) · 4.12 KB
/
mnestic.lua
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
#!/usr/bin/env lua
-- antimemetic folders, with periodic mnestics
-- require
-- luarocks: subproc, luaposix
-- commands: rsync, ln, date, pgrep
local subproc = require('subproc')
local posix = require('posix')
local home = os.getenv("HOME")
if home == nil then
print "Error: no home directory set."
os.exit(1)
end
-- ramfs
local ramfs_root = "/ram/mnestic"
-- persistent storage
local mnestic_root = home .. '/.mnestic'
local _, _, notify_send_exists = subproc('which', 'notify-send')
local function notify(msg)
if notify_send_exists == 0 then
-- comment out to disable notifications
subproc('notify-send', '[mnestic] ' .. msg)
end
end
local targets = {
{
process = "chromium",
directories = {
home .. "/.config/chromium",
home .. "/.cache/chromium"
}
},
{
process = "palemoon",
directories = {
home .. "/.moonchild productions/pale moon",
home .. "/.cache/moonchild productions/pale moon"
}
},
{
process = "weechat",
directories = {
home .. "/.weechat"
}
}
}
-- ensure no dir ends in slash
local dir_names = {ramfs_root, mnestic_root}
for _, target in ipairs(targets) do
for _, dir in ipairs(target.directories) do
table.insert(dir_names, dir)
end
end
for _, dir in ipairs(dir_names) do
local err = false
if dir:sub(-1) == '/' then
print("Error: directory '" .. dir .. "' ends with slash but should not")
err = true
end
if err then
os.exit(4)
end
end
-- rsync, but do it right
-- - ensure trailing slash on src and dst
-- - rsync -a
local function correct_rsync(src, dst)
if src:sub(-1) ~= '/' then
src = src .. '/'
end
if dst:sub(-1) ~= '/' then
dst = dst .. '/'
end
return subproc('rsync', '-a', '--delete-after', src, dst)
end
print "Launching mnestic daemon"
notify("launching")
print "Creating missing targets directories"
for _, target in ipairs(targets) do
for _, dir in ipairs(target.directories) do
subproc('mkdir', '-p', mnestic_root .. dir)
end
end
print "Creating missing symlinks. Does not check that existing symlinks point to the right place"
for _, target in ipairs(targets) do
for _, dir in ipairs(target.directories) do
local _, _, exists = subproc('[', '-e', dir, ']')
local _, _, is_symlink = subproc('[', '-L', dir, ']')
if exists == 0 and is_symlink ~= 0 then
print("Error: '" .. dir .. "' exists but is not a symlink")
os.exit(3)
end
if is_symlink ~= 0 then
local output, _, success = subproc('ln', '-s', ramfs_root .. dir, dir)
if success ~= 0 then
print("Error creating symlink for '" .. dir .. "': ")
print(output)
os.exit(2)
end
end
end
end
print "Copying persistent storage to ramfs"
correct_rsync(mnestic_root, ramfs_root)
-- when process is not running, sync to persist
local function checkpoint()
local date_printed = false
for _, target in ipairs(targets) do
local _, _, pgrep_code = subproc('pgrep', target.process)
local running = pgrep_code == 0
if running ~= target.was_running then
if not running then
if not date_printed then
local date = subproc('date')
print("")
print("------------------------")
print(date)
date_printed = true
end
notify("syncing " .. target.process)
for _, dir in ipairs(target.directories) do
print("Syncing " .. dir)
correct_rsync(ramfs_root .. dir, mnestic_root .. dir)
end
end
end
target.was_running = running
end
if date_printed then
print("Sync done")
notify("sync done")
end
end
print "Entering persistence loop"
local function main_loop()
while true do
checkpoint()
posix.sleep(10)
end
end
pcall(main_loop)