-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkaptrepo.py
executable file
·143 lines (131 loc) · 3.83 KB
/
checkaptrepo.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
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
#!/usr/bin/python3
# Copyright 2018 Peter Green
# Released under the MIT/Expat license, see doc/COPYING
import os
import sys
import hashlib
import gzip
from sortedcontainers import SortedDict
def openg(filepath):
if os.path.exists(filepath):
f = open(filepath,'rb')
else:
f = gzip.open(filepath+b'.gz','rb')
return f
def addfiletoverify(filestoverify,filename,sha1,size):
size = int(size)
sha1andsize = (sha1,size)
if filename in filestoverify:
if (sha1andsize != filestoverify[filename]):
print('error: same file with different hash/size old:'+repr(filestoverify[filename])+' new:'+repr(sha1andsize))
sys.exit(1)
else:
filestoverify[filename] = sha1andsize
dists = os.listdir('dists/')
filestoverify = SortedDict() #sorted to hopefully get better locality on file accesses.
for dist in dists:
f = open('dists/'+dist+'/Release','rb')
insha1 = False;
for line in f:
#print(repr(line[0]))
if (line == b'SHA1:\n'):
insha1 = True
elif ((line[0] == 32) and insha1):
linesplit = line.split()
filename = b'dists/'+dist.encode('ascii')+b'/'+linesplit[2]
#if filename in filestoverify:
# if files
addfiletoverify(filestoverify,filename,linesplit[0],linesplit[1]);
if filename.endswith(b'Packages'):
print('found packages file: '+filename.decode('ascii'))
pf = openg(filename)
filename = None
size = None
sha1 = None
for line in pf:
linesplit = line.split()
if (len(linesplit) == 0):
if (filename != None):
addfiletoverify(filestoverify,filename,sha1,size);
filename = None
size = None
sha1 = None
elif (linesplit[0] == b'Filename:'):
filename = linesplit[1]
elif (linesplit[0] == b'Size:'):
size = linesplit[1]
elif (linesplit[0] == b'SHA1:'):
sha1 = linesplit[1]
pf.close()
elif filename.endswith(b'Sources'):
print('found sources file: '+filename.decode('ascii'))
pf = openg(filename)
filesfound = [];
directory = None
insha1p = False;
for line in pf:
linesplit = line.split()
if (len(linesplit) == 0):
for ls in filesfound:
#print(repr(ls))
addfiletoverify(filestoverify,directory+b'/'+ls[2],ls[0],ls[1]);
filesfound = [];
directory = None
insha1p = False
elif ((line[0] == 32) and insha1p):
filesfound.append(linesplit)
elif (linesplit[0] == b'Directory:'):
insha1p = False
directory = linesplit[1]
elif (linesplit[0] == b'Checksums-Sha1:'):
insha1p = True
else:
insha1p = False
pf.close()
else:
insha1 = False
f.close()
#print(repr(filestoverify))
#incomplete code to descend into dscs, seems this is
#not actually needed as files depended on by dscs
#are listed in the Sources file directly.
#filessofar = filestoverify.copy();
#for filename, sha1andsize in filessofar.items():
# if filename.endswith(b'dsc'):
# f = open(filename,'rb')
# insha1 = False
# for line in f:
# if (line == b'Checksums-Sha1::\n'):
# insha1 = True
# elif ((line[0] == 32) and insha1):
#
# else:
# insha1 = False
# f.close()
for filename, sha1andsize in filestoverify.items():
sha1,size = sha1andsize;
print('verifying '+filename.decode('ascii'))
if b'../' in filename:
print('fucked up filename')
sys.exit(1);
if not os.path.isfile(filename):
if not os.path.isfile(filename+b'.gz'):
print('missing file '+ filename.decode('ascii'))
sys.exit(1)
else:
#sometimes reprepro seems to create only a .gz file but includes the non-gzipped file in the index
f = gzip.open(filename+b'.gz','rb')
else:
f = open(filename,'rb')
data = f.read();
f.close()
sha1hash = hashlib.sha1(data)
sha1hashed = sha1hash.hexdigest().encode('ascii')
if (sha1 != sha1hashed):
print('hash mismatch');
sys.exit(1)
filesize = len(data)
if (size != filesize):
print('size mismatch');
sys.exit(1)
#print(repr(filestoverify))