-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcloud-vault
executable file
·410 lines (371 loc) · 15.1 KB
/
cloud-vault
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
#!/usr/bin/env python3
import argparse, base64, contextlib, datetime, hashlib, json, os, \
re, shlex, sqlite3, stat, subprocess, sys, urllib.parse
assert sys.hexversion >= 0x03050000 # Python 3.5 is required
sys.path.insert(0, os.path.expanduser("~/stuff/_urandom"))
import utils
EMPTY_HASH = "sha512:z4PhNX7vuL3xVChQ1m2AB9Yg5AUL"
DIGEST_LEN = 21 # in bytes
MIN_HASH_PREFIX = 8 # in bytes
class UserError(Exception):
pass
def bihash_file(hasher1, hasher2, file):
if hasher1 == hasher2:
h = utils.hash_file(hasher1, file)
return h, h
h1 = hasher1()
h2 = hasher2()
for block in iter(lambda: file.read(1 << 20), b""):
h1.update(block)
h2.update(block)
return h1, h2
def get_file_hash(file, known=None):
hasher = hashlib.sha512
if known is not None:
known_hasher, known_hash = known
assert len(known_hash) >= MIN_HASH_PREFIX * 2
else:
known_hasher = hasher
with open(file, "rb") as f:
hashed, known_hashed = bihash_file(hashlib.sha512, known_hasher, f)
if known is not None:
if not known_hashed.hexdigest().startswith(known_hash):
raise Exception("hash mismatch:\n"
"found: {!r}\n"
"provided: {!r}"
.format(known_hashed.hexdigest(),
known_hash))
return "sha512:" + display_digest(hashed)
def get_hasher(hash_algorithm):
if hash_algorithm not in hashlib.algorithms_available:
return None
return getattr(hashlib, hash_algorithm)
def convert_hash_to_hex(hash):
algorithm, digest = hash.split(":", 1)
hex_digest = base64.b16encode(base64.urlsafe_b64decode(digest))
return algorithm + ":" + hex_digest.decode("ascii").lower()
def display_digest(hashed):
digest = hashed.digest()[:DIGEST_LEN]
return base64.urlsafe_b64encode(digest).decode("ascii")
def display_mode(mode):
return "755" if mode & stat.S_IXUSR else "644"
def parse_mode(mode):
if mode not in ["644","755"]:
raise ValueError("unsupported mode: {}".format(mode))
return int(mode, base=8) & 0o777
def display_time(timestamp_ns):
return (
re.sub("[:-]", "", datetime.datetime.utcfromtimestamp(
timestamp_ns // 1000000000
).isoformat()) +
".{:09}Z".format(timestamp_ns % 1000000000))
def parse_time(isotime):
t, ns = isotime.split(".")
t = (datetime.datetime.strptime(t, "%Y%m%dT%H%M%S") -
datetime.datetime(1970, 1, 1)) // datetime.timedelta(seconds=1)
ns = int(ns.rstrip("Z"))
return t * 1000000000 + ns
def generate_remote_id(seed, hash):
if seed is None:
return None
return display_digest(hashlib.sha512((seed + hash).encode("utf-8")))
def warn(s):
sys.stderr.write(s)
sys.stderr.flush()
def db_hash_exists(db, hash):
return bool(tuple(db.execute("SELECT 1 FROM hashes WHERE hash = ?",
[hash])))
def db_insert_hash(db, hash, size):
with db:
db.execute("BEGIN")
db.execute("INSERT OR REPLACE INTO hashes (hash, size) VALUES (?, ?)",
[hash, size])
def db_remove_hash(db, hash):
with db:
db.execute("BEGIN")
db.execute("DELETE FROM hashes WHERE hash = ?", [hash])
def db_all_hashes(db):
return tuple(x for x, in db.execute("SELECT hash FROM hashes"))
def db_get_size_by_hash(db, hash):
for size, in db.execute("SELECT size FROM hashes WHERE hash = ?", [hash]):
return size
return None
def transfer_rm(transfer, hash, remote):
# handle empty files specially because cloud-transfer doesn't remember
# empty files at all
if hash == EMPTY_HASH:
pass
else:
subprocess.check_call(transfer + ["rm", "--", remote])
def transfer_download(transfer, hash, remote, local):
# handle empty files specially because cloud-transfer doesn't remember
# empty files at all
if hash == EMPTY_HASH:
with open(local, "w"):
pass
else:
try:
subprocess.check_call(transfer + ["download", "--", remote, local])
except:
try:
if os.lstat(local).st_size == 0:
os.remove(local)
except OSError:
pass
raise
def transfer_upload(transfer, hash, local, remote):
hex_hash = convert_hash_to_hex(hash)
subprocess.check_call(transfer + ["upload", "--hash", hex_hash, "--",
local, remote])
def is_vault_link(file):
return (os.path.islink(file) and
os.readlink(file).startswith("vault://"))
def get_info_from_vault_link(db, seed, file):
target = os.readlink(file)
hash, qs = re.match(r"vault://([^?]+)\?(.*)", target).groups()
attrs = dict(urllib.parse.parse_qsl(qs))
return {
"hash": hash,
"hex_hash": convert_hash_to_hex(hash),
"remote_id": generate_remote_id(seed, hash),
"mode": attrs["mode"],
"mtime": attrs["mtime"],
"size": db_get_size_by_hash(db, hash) if db is not None else None,
}
def get_info(seed, file, ignore_mode, known=None):
st = os.lstat(file)
if not stat.S_ISREG(st.st_mode):
raise UserError("not a file: {!r}".format(file))
hash = get_file_hash(file, known=known)
return {
"hash": hash,
"hex_hash": convert_hash_to_hex(hash),
"remote_id": generate_remote_id(seed, hash),
"mode": display_mode(0o644 if ignore_mode else st.st_mode),
"mtime": display_time(st.st_mtime_ns),
"size": st.st_size,
}
def info(files, ignore_mode, seed, transfer, db, **kwargs):
for file in files:
if not os.path.lexists(file):
raise UserError("does not exist: {}".format(file))
if os.path.islink(file):
info = get_info_from_vault_link(db, seed, file)
else:
info = get_info(seed, file, ignore_mode)
sys.stdout.write(json.dumps(info, **utils.JSON_PRETTY) + "\n")
def get(files, new, seed, transfer, db, **kwargs):
for file in files:
if not os.path.lexists(file):
raise UserError("does not exist: {}".format(file))
if os.path.islink(file):
if not is_vault_link(file):
raise UserError("cannot get a non-vault link: {}".format(file))
else:
warn("skipping (already got): {}\n".format(file))
continue
info = get_info_from_vault_link(db, seed, file)
mode = parse_mode(info["mode"])
mtime = parse_time(info["mtime"])
if not new and not db_hash_exists(db, info["hash"]):
raise UserError("hash not in index: {} (use --new to "
"record a new hash)".format(info["hash"]))
tmp = file + ".download.tmp" # keep name consistent to allow resumption
transfer_download(transfer, info["hash"], info["remote_id"], tmp)
tmp_hash = get_file_hash(tmp)
if tmp_hash != info["hash"]:
raise UserError("hash of downloaded file does not match:\n"
"downloaded: {}\n"
"link: {}"
.format(tmp_hash, info["hash"]))
db_insert_hash(db, info["hash"], os.path.getsize(tmp))
os.utime(tmp, ns=(os.lstat(tmp).st_atime_ns, mtime))
os.chmod(tmp, mode)
os.rename(file, file + ".link")
os.rename(tmp, file)
def put(files, delete, check, force, ignore_mode, seed, transfer, db, **kwargs):
if check is not None:
# determine checksum algorithm
m = re.search("(\w*)sums?$", check.lower())
if not m:
raise UserError("could not determine checksum algorithm from "
"file extension")
ck_algorithm, = m.groups()
ck_hasher = get_hasher(ck_algorithm)
if ck_hasher is None:
raise UserError("unsupported checksum algorithm: {}"
.format(ck_algorithm))
# populate checksums dictionary
checksums = {}
with open(check) as f:
for line in f:
if not line.strip():
continue
m = re.match("([0-9a-fA-F]+) (.*)", line)
if not m:
raise UserError("could not parse line in checksum file: {}"
.format(line))
ck_hash, ck_path = m.groups()
ck_path = os.path.realpath(
os.path.join(os.path.dirname(check), ck_path))
if ck_path in checksums:
raise UserError("checksum file contains conflicting hashes "
"for: {}".format(ck_path))
checksums[ck_path] = ck_hash
# bail early if one of the checksums is missing
for file in files:
if os.path.realpath(file) not in checksums:
raise UserError("no entry in checksum file for: {}"
.format(file))
for file in files:
if is_vault_link(file):
warn("skipping (already put): {}\n".format(file))
continue
if os.path.islink(file):
raise UserError("cannot put a non-vault link: {}".format(file))
bak_file = file + ".bak"
if not force and os.path.exists(bak_file):
raise UserError("backup file already exists: {}".format(bak_file))
if check is None:
known = None
else:
known = ck_hasher, checksums[os.path.realpath(file)]
info = get_info(seed, file, ignore_mode, known=known)
if db_hash_exists(db, info["hash"]):
warn("skipping upload (already indexed): {}\n"
.format(file))
else:
remote_id = generate_remote_id(seed, info["hash"])
transfer_upload(transfer, info["hash"], file, info["remote_id"])
db_insert_hash(db, info["hash"], info["size"])
target = "vault://{}?{}".format(info["hash"], urllib.parse.urlencode([
("mode", info["mode"]),
("mtime", info["mtime"]),
]))
if not force:
with open(bak_file, "x"):
pass
os.rename(file, bak_file)
os.symlink(target, file)
if delete:
os.remove(bak_file)
def gc_mark(hashes, file):
if not is_vault_link(file):
return
hash = get_info_from_vault_link(None, None, file)["hash"]
try:
hashes.remove(hash)
except KeyError:
pass
def gc(paths, purge, seed, transfer, db, purge_index, **kwargs):
hashes = set(db_all_hashes(db))
for path in paths:
if os.path.isdir(path):
for (dirpath, _, filenames) in os.walk(path):
for filename in filenames:
gc_mark(hashes, os.path.join(dirpath, filename))
else:
gc_mark(hashes, path)
for hash in sorted(hashes):
remote_id = generate_remote_id(seed, hash)
sys.stdout.write('{{"hash": "{}", "remote_id": "{}"}}\n'
.format(hash, remote_id))
if purge:
try:
transfer_rm(transfer, hash, remote_id)
db_remove_hash(db, hash)
except subprocess.CalledProcessError as e:
warn(str(e) + "\n")
elif purge_index:
db_remove_hash(db, hash)
def main():
p = argparse.ArgumentParser()
p.add_argument("--index", required=True,
help="SQLite3 database to store the known hashes")
p.add_argument("--seed", required=True,
help="seed for generating remote IDs")
p.add_argument("--transfer", required=True, type=shlex.split,
help="cloud-transfer command")
p.add_argument("--ignore-mode", action="store_true",
help="Treat the file mode as 644")
sp = p.add_subparsers()
spp = sp.add_parser("gc")
spp.set_defaults(func=gc)
spp.add_argument("paths", nargs="+")
spp.add_argument("--purge", action="store_true",
help="Delete the files at remote (implies --purge-index)")
spp.add_argument("--purge-index", action="store_true",
help="Delete the files in index only")
spp = sp.add_parser("info", help="Retrieve information about file/link")
spp.set_defaults(func=info)
spp.add_argument("files", nargs="+", type=os.path.abspath,
help="filename of the local file")
spp = sp.add_parser("get",
help=("Replace a vault link with the actual file. "
"The link is backed up to *.link"))
spp.set_defaults(func=get)
spp.add_argument("--new", action="store_true",
help=("Add a new file that has already been uploaded "
"but is not in index. The file will be stored "
"in index if download and verification succeed."))
spp.add_argument("files", nargs="+", type=os.path.abspath,
help="path to the local metadata files")
spp = sp.add_parser("put",
help=("Upload and replace the file with a vault link. "
"The original file is backed up to *.bak"))
spp.set_defaults(func=put)
spp.add_argument("--delete", action="store_true",
help="Don't make a backup of the file.")
spp.add_argument("-c", "--check",
help="Verify files using the provided checksum file")
spp.add_argument("-f", "--force", action="store_true",
help="Overwrite the backup file if it already exists.")
spp.add_argument("files", nargs="+", type=os.path.abspath,
help="filename of the local file")
kwargs = vars(p.parse_args())
func = kwargs.pop("func", None)
if not func:
p.print_usage()
return 2
os.makedirs(os.path.realpath(os.path.dirname(kwargs["index"])),
exist_ok=True)
with contextlib.closing(sqlite3.connect(kwargs["index"])) as db:
migrations = [
[
"""
CREATE TABLE hashes
( hash TEXT UNIQUE
)
""",
"""
CREATE TABLE aliases
( alias TEXT UNIQUE
, hash TEXT
, FOREIGN KEY(hash) REFERENCES hashes(hash)
)
""",
],
[
"""
ALTER TABLE "hashes" ADD COLUMN "size" INTEGER;
""",
],
]
db.execute("PRAGMA auto_vacuum = FULL")
with db:
db.execute("BEGIN")
version = max(0, tuple(db.execute("PRAGMA user_version"))[0][0])
if version > len(migrations):
raise Exception("database is newer than current version")
for migration in migrations[version:]:
for stmt in migration:
db.execute(stmt)
db.execute("PRAGMA user_version = {}".format(len(migrations)))
try:
return func(db=db, **kwargs) or 0
except UserError as e:
sys.stderr.write("cloud-vault: {}\n".format(e))
sys.stderr.flush()
sys.exit(1)
if __name__ == "__main__":
sys.exit(main())