-
Notifications
You must be signed in to change notification settings - Fork 1
/
Index.py
51 lines (37 loc) · 1.47 KB
/
Index.py
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
import os
from subprocess import Popen, PIPE, STDOUT
import re
import hashlib
import lz4
from Subvolume import Subvolume
class Index():
def __init__(self):
self.matchSubvolume = re.compile("^ID (\d+) gen (\d+) top level (\d+) path (.+)$")
self.matchAllocation = re.compile("^inode (\d+) file offset (\d+) len (\d+) disk start (\d+) offset (\d+) gen (\d+) flags (\w+) (.*)$")
self.folder = ".btrfs"
self.subvolume = Subvolume()
pass
def create(self):
if not os.path.exists(self.folder):
os.makedirs(self.folder)
def scan(self):
subvolumes = self.subvolume.listSubvolumes()
for subvolume in subvolumes:
self.scanSubvolume(subvolume)
def scanSubvolume(self, subvolume):
hex = hashlib.sha1(subvolume).hexdigest()
file = self.folder + "/" + hex + ".lz4"
completed = self.folder + "/" + hex + ".complete"
print "scaning: " + subvolume + " (" + hex + ")"
if os.path.exists(completed):
return
p = Popen('btrfs subvolume find-new ' + subvolume + ' 0', shell=True, stdout=PIPE, stderr=STDOUT)
list = []
for line in p.stdout.readlines():
match = self.matchAllocation.match(line)
if match is None:
continue
list.append(match.group(0))
with open(file, "w") as text_file:
text_file.write(lz4.compress("\n".join(list)))
open(completed, 'a').close()