-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpio.py
309 lines (258 loc) · 9.79 KB
/
cpio.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
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
#coding: utf-8
from __future__ import absolute_import
import logging
import sys
import os
import stat
import itertools
from collections import defaultdict, namedtuple
log = logging.getLogger('CPIO')
MyStat = namedtuple('MyStat', [
'st_uid',
'st_gid',
'st_ino',
'st_mode',
'st_mtime',
'st_nlink',
'st_dev',
'st_rdev',
'st_size',
'symlink_path', # bytes (!)
'host_filename', # unicode or bytes
])
class CPIOException(Exception):
pass
class CPIO(object):
def __init__(self, outfile, save_mtime=True, save_uid_gid=True):
"""
:type outfile: FileIO of bytes
"""
self.outfile = outfile
self.ino_real2fake = defaultdict(itertools.count(1).next)
self.ino2htuple = dict()
self.input_encoding = sys.getfilesystemencoding()
self.output_encoding = 'utf-8'
self.save_mtime = save_mtime
self.save_uid_gid = save_uid_gid
self.position = 0
log.debug('Output encoding is %s, input one is %s', self.output_encoding, self.input_encoding)
def _outwrite(self, data):
"""
:type data: bytes
"""
self.outfile.write(data)
self.position += len(data)
def _align(self):
offset = -(self.position % -4)
if offset:
self._outwrite(b'\x00' * offset)
def _write_file_contents(self, statres, cpio_filename):
"""
:type statres: MyStat
:type cpio_filename: bytes
"""
if stat.S_ISLNK(statres.st_mode):
size = len(statres.symlink_path)
elif stat.S_ISREG(statres.st_mode):
size = statres.st_size
else:
size = 0
# very common error, so check sooner
if size > 0xffffffff:
raise CPIOException('Too big file size for this CPIO format', statres.st_size)
if size != statres.st_size:
log.warning('Replacing size of stat struct %d => %d', statres.st_size, size)
statres._replace(st_size=size)
if cpio_filename == b'TRAILER!!!':
raise CPIOException('Attempt to pass reserved filename', cpio_filename)
self._write_header(statres, cpio_filename)
# hardlinks, empty files and directories fall here
if not size:
return
if stat.S_ISLNK(statres.st_mode):
self._align()
self._outwrite(statres.symlink_path)
return
#TODO: splice
if stat.S_ISREG(statres.st_mode):
self._align()
with open(statres.host_filename, 'rbe') as xxx:
while size:
chunk = xxx.read(65536)
if not chunk:
break
self._outwrite(chunk)
size -= len(chunk)
if size:
raise CPIOException('File was changed while reading', statres.host_filename)
return
raise CPIOException('Unknown file type', statres.st_mode)
def _write_header(self, statres, cpio_filename):
cpio_filename += b'\x00'
fields = [
statres.st_ino,
statres.st_mode,
statres.st_uid,
statres.st_gid,
statres.st_nlink,
statres.st_mtime,
statres.st_size,
os.major(statres.st_dev),
os.minor(statres.st_dev),
os.major(statres.st_rdev),
os.minor(statres.st_rdev),
# Yes, length including last zero byte
len(cpio_filename),
# New CRC Format
# The CRC format is identical to the new ASCII format described in the pre-
# vious section except that the magic field is set to ``070702'' and the
# check field is set to the sum of all bytes in the file data. This sum is
# computed treating all bytes as unsigned values and using unsigned arith-
# metic. Only the least-significant 32 bits of the sum are stored.
# BUGS
# The ``CRC'' format is mis-named, as it uses a simple checksum and not a
# cyclic redundancy check.
0,
]
self._align()
self._outwrite(b'070701')
# writelines will not add newlines. stupid function name
for (n, i) in enumerate(fields):
# UNIX epoch overflow, negative timestamps and so on...
if (i > 0xffffffff) or (i < 0):
raise CPIOException('You are looser', n, i)
self._outwrite(b'{0:08x}'.format(i))
self._outwrite(cpio_filename)
def inject_path(self, path, root='/'):
"""
:type path: basestring
"""
# TODO: check that:
# new file will not be written by existing symlink
# archive contains all needed dirs
# is not duplicate of another file in archive
statres = os.lstat(path)
ino = self.ino_real2fake[(statres.st_dev, statres.st_ino)]
if self.save_mtime:
# statres.st_mtime may be floating point
mtime = int(statres.st_mtime)
else:
# mtime = 0 may make some programs crazy...
mtime = 1
if self.save_uid_gid:
uid = statres.st_uid
gid = statres.st_gid
else:
uid = 0
gid = 0
if stat.S_ISDIR(statres.st_mode):
nlink = 2
else:
nlink = statres.st_nlink
if stat.S_ISREG(statres.st_mode) or stat.S_ISLNK(statres.st_mode):
size = statres.st_size
else:
size = 0
if stat.S_ISLNK(statres.st_mode):
symlink_path = os.readlink(path)
if not isinstance(symlink_path, unicode):
symlink_path = symlink_path.decode(self.input_encoding)
symlink_path = symlink_path.encode(self.output_encoding)
else:
symlink_path = None
nnn = os.path.normpath(root)
if nnn != root:
raise ValueError('Please normalize path before passing it to CPIO %r vs %r', nnn, root)
if not path.startswith(root):
raise ValueError('Invalid root or path value', path, root)
cpio_filename = path[len(root):]
if not cpio_filename:
cpio_filename = u'.'
if not isinstance(cpio_filename, unicode):
cpio_filename = cpio_filename.decode(self.input_encoding)
if cpio_filename.startswith(u'/'):
cpio_filename = cpio_filename[1:]
cpio_filename = cpio_filename.encode(self.output_encoding)
newstatres = MyStat(
st_uid=uid,
st_gid=gid,
st_ino=ino,
st_mtime=mtime,
st_mode=statres.st_mode,
st_nlink=nlink,
st_dev=0,
st_rdev=statres.st_rdev,
st_size=size,
symlink_path=symlink_path,
host_filename=path,
)
# TODO: check if OS lie about hardlink count
if (nlink == 1) or stat.S_ISDIR(statres.st_mode):
self._write_file_contents(newstatres, cpio_filename)
else:
# hardlinks will be handled later, as we can not know if it is intermediate item or last
log.debug('Detected potential hardlink %r (%r)', path, cpio_filename)
htuple = self.ino2htuple.setdefault(ino, (newstatres, []))
htuple[1].append(cpio_filename)
def _hardlinks_handle(self):
# handle hardlinks
if self.ino2htuple:
log.debug('Writing hardlinks')
for (statres, cpio_filenames) in self.ino2htuple.itervalues():
lnk_count = len(cpio_filenames)
if lnk_count > statres.st_nlink:
raise CPIOException('Found more hardlinks (%r) than host FS reports (%r). See %r', cpio_filenames,
statres.st_nlink, statres.host_filename)
if lnk_count < statres.st_nlink:
log.info('Found fewer hardlinks: %r than host FS reports (%r). See %r', cpio_filenames,
statres.st_nlink, statres.host_filename)
statres._replace(st_nlink=lnk_count)
fake_statres = MyStat(*statres)
fake_statres._replace(st_size=0)
last_cpio_filename = cpio_filenames.pop()
for cpio_filename in cpio_filenames:
self._write_file_contents(fake_statres, cpio_filename)
self._write_file_contents(statres, last_cpio_filename)
def _write_trailer(self):
statres = MyStat(
st_uid=0,
st_gid=0,
st_ino=0,
st_mtime=0,
st_mode=0,
st_nlink=0,
st_dev=0,
st_rdev=0,
st_size=0,
symlink_path=None,
host_filename=None,
)
self._write_header(statres, b'TRAILER!!!')
# for really buggy cpio unpacker implementations...
self._align()
def __enter__(self):
return self
def __exit__(self, *exc_info):
self.finalize()
def finalize(self):
self._hardlinks_handle()
self._write_trailer()
def superinject(self, src):
def _walkhandler(error):
raise error
if isinstance(src, basestring):
src = [src]
for root in src:
if os.path.islink(root):
raise ValueError('Attempt to super-inject by symlink', root)
if not os.path.isdir(root):
raise ValueError('Attempt to super-inject by non-dir', root)
self.inject_path(root, root)
for (prefix, dirs, files) in os.walk(root, onerror=_walkhandler):
for item in itertools.chain(files, dirs):
self.inject_path(os.path.join(prefix, item), root)
#from gzip import GzipFile
#
#with GzipFile('test.gz', 'wbe', 9, dst) as dst:
# with CPIO(dst) as cpio:
# cpio.superibject(src, True)