|
11 | 11 | # http://www.gnu.org/software/tar/manual/html_node/Standard.html
|
12 | 12 | _TAR_HEADER = {
|
13 | 13 | "name": (uctypes.ARRAY | 0, uctypes.UINT8 | 100),
|
14 |
| - "mode": (uctypes.ARRAY | 100, uctypes.UINT8 | 7), |
15 |
| - "uid": (uctypes.ARRAY | 108, uctypes.UINT8 | 7), |
16 |
| - "gid": (uctypes.ARRAY | 116, uctypes.UINT8 | 7), |
| 14 | + "mode": (uctypes.ARRAY | 100, uctypes.UINT8 | 8), |
| 15 | + "uid": (uctypes.ARRAY | 108, uctypes.UINT8 | 8), |
| 16 | + "gid": (uctypes.ARRAY | 116, uctypes.UINT8 | 8), |
17 | 17 | "size": (uctypes.ARRAY | 124, uctypes.UINT8 | 12),
|
18 | 18 | "mtime": (uctypes.ARRAY | 136, uctypes.UINT8 | 12),
|
19 | 19 | "chksum": (uctypes.ARRAY | 148, uctypes.UINT8 | 8),
|
|
26 | 26 | _RECORDSIZE = const(_BLOCKSIZE * 20) # length of records
|
27 | 27 |
|
28 | 28 |
|
29 |
| -# Write a string into a bytearray by copying each byte. |
30 |
| -def _setstring(b, s, maxlen): |
31 |
| - for i, c in enumerate(s.encode("utf-8")[:maxlen]): |
32 |
| - b[i] = c |
33 |
| - |
34 |
| - |
35 | 29 | def _open_write(self, name, mode, fileobj):
|
36 | 30 | if mode == "w":
|
37 | 31 | if not fileobj:
|
@@ -72,18 +66,18 @@ def addfile(self, tarinfo, fileobj=None):
|
72 | 66 | if not name.endswith("/"):
|
73 | 67 | name += "/"
|
74 | 68 | hdr = uctypes.struct(uctypes.addressof(buf), _TAR_HEADER, uctypes.LITTLE_ENDIAN)
|
75 |
| - _setstring(hdr.name, name, 100) |
76 |
| - _setstring(hdr.mode, "%06o " % (tarinfo.mode & 0o7777), 7) |
77 |
| - _setstring(hdr.uid, "%06o " % tarinfo.uid, 7) |
78 |
| - _setstring(hdr.gid, "%06o " % tarinfo.gid, 7) |
79 |
| - _setstring(hdr.size, "%011o " % size, 12) |
80 |
| - _setstring(hdr.mtime, "%011o " % tarinfo.mtime, 12) |
81 |
| - _setstring(hdr.typeflag, "5" if tarinfo.isdir() else "0", 1) |
| 69 | + hdr.name[:] = name.encode("utf-8")[:100] |
| 70 | + hdr.mode[:] = b"%07o\0" % (tarinfo.mode & 0o7777) |
| 71 | + hdr.uid[:] = b"%07o\0" % tarinfo.uid |
| 72 | + hdr.gid[:] = b"%07o\0" % tarinfo.gid |
| 73 | + hdr.size[:] = b"%011o\0" % size |
| 74 | + hdr.mtime[:] = b"%011o\0" % tarinfo.mtime |
| 75 | + hdr.typeflag[:] = b"5" if tarinfo.isdir() else b"0" |
82 | 76 | # Checksum is calculated with checksum field all blanks.
|
83 |
| - _setstring(hdr.chksum, " " * 8, 8) |
| 77 | + hdr.chksum[:] = b" " |
84 | 78 | # Calculate and insert the actual checksum.
|
85 | 79 | chksum = sum(buf)
|
86 |
| - _setstring(hdr.chksum, "%06o\0" % chksum, 7) |
| 80 | + hdr.chksum[:] = b"%06o\0 " % chksum |
87 | 81 | # Emit the header.
|
88 | 82 | self.f.write(buf)
|
89 | 83 | self.offset += len(buf)
|
|
0 commit comments