-
Notifications
You must be signed in to change notification settings - Fork 3
/
sparsebundle.py
executable file
·143 lines (116 loc) · 4 KB
/
sparsebundle.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/env python
debug = False
import plistlib
import os
import io
if debug:
import sys
def prdebug(text):
if debug:
print >>sys.stderr, text
class SparseBundle(io.RawIOBase):
def __init__(self, dirname):
if dirname is None:
raise IOError("dirname can't be None")
dirname = os.path.realpath(dirname)
if not os.path.exists(dirname):
raise IOError("%s does not exist." % dirname)
if not os.path.isdir(dirname):
raise IOError("%s is not a directory." % dirname)
if not os.path.exists("%s/Info.plist" % dirname):
raise IOError("%s/Info.plist does not exist." % dirname)
p = plistlib.readPlist("%s/Info.plist" % dirname)
if p['diskimage-bundle-type'] != 'com.apple.diskimage.sparsebundle':
raise IOError("Unsupported bundle type %s" % p['diskimage-bundle-type'])
self.source = dirname
self.offset = 0
self.band_size = int(p['band-size'])
self.size = int(p['size'])
self.band_files = {}
self.band_lru = []
self.max_file_cache = 64
def open_band(self, offset):
band = offset / self.band_size
if not band in self.band_files:
prdebug("Opening band %x" % band)
f = open("%s/bands/%x" % (self.source, band), "rb")
self.band_files[band] = f
self.band_lru.append(band)
if len(self.band_lru) > self.max_file_cache:
oldband = self.band_lru[0]
del self.band_files[oldband]
self.band_lru.remove(oldband)
else:
prdebug("Pinging band %x" % band)
self.band_lru.remove(band)
self.band_lru.append(band)
self.band_files[band].seek(offset % self.band_size, io.SEEK_SET)
return band
def readinto(self, buf):
if self.offset < 0 or self.offset >= self.size:
return 0
length = len(buf)
if debug:
for i in range(0,length):
buf[i] = 0
buf_offset = 0
while length > 0:
band_offset = self.offset % self.band_size
chunk = min(self.band_size - band_offset, length)
try:
band = self.open_band(self.offset)
sparse_read = False
except IOError, e:
sparse_read = True
band = None
except Exception, e:
raise IOError("Can't open band file %x: %s" % \
((self.offset / self.band_size), str(e)))
start = buf_offset
end = start + chunk
if band is not None:
v = memoryview(buf)
r = self.band_files[band].readinto(v[start:end])
del v
else:
prdebug("Zeroing %d:%d" % (start, end))
for i in range(start,end):
buf[i] = 0
buf_offset += chunk
length -= chunk
self.offset += chunk
return buf_offset
def seekable():
return True
def readable():
return True
def writable():
return False
def close(self):
for b in self.band_files:
self.band_files[b].close()
del self.band_files
self.band_files = {}
def seek(self, offset, whence=io.SEEK_SET):
if whence == io.SEEK_SET:
self.offset = offset
elif whence == io.SEEK_CUR:
self.offset += offset
elif whence == io.SEEK_END:
self.offset = self.size + offset
else:
raise IOError("Illegal whence %d" % whence)
return self.offset
def tell(self):
return self.offset
def write(self, buf):
raise IOError("SparseBundle implementation is read-only.")
if __name__ == '__main__':
path = "/home/jeffm/test image.sparsebundle"
f = SparseBundle(path)
f.seek(f.band_size * 2 - 512)
r = f.read(1024)
print r
f.close()
del f
# vim:set et sw=4 si: