-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarve-tar.py
More file actions
executable file
·173 lines (145 loc) · 4.32 KB
/
carve-tar.py
File metadata and controls
executable file
·173 lines (145 loc) · 4.32 KB
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
#!/usr/bin/python3
import argparse
import datetime
import os
import sys
import tarfile
def get_modes(info):
s = ""
if info.type == tarfile.REGTYPE or info.type == tarfile.AREGTYPE:
s += "-"
elif info.type == tarfile.LNKTYPE:
s += "l"
elif info.type == tarfile.SYMTYPE:
s += "s"
elif info.type == tarfile.CHRTYPE:
s += "c"
elif info.type == tarfile.BLKTYPE:
s += "b"
elif info.type == tarfile.DIRTYPE:
s += "d"
elif info.type == tarfile.FIFOTYPE:
s += "p"
else:
s += "?"
# Owner
if info.mode & 0o400:
s += "r"
else:
s += "-"
if info.mode & 0o200:
s += "w"
else:
s += "-"
if info.mode & 0o100:
s += "x"
else:
s += "-"
# Group
if info.mode & 0o040:
s += "r"
else:
s += "-"
if info.mode & 0o020:
s += "w"
else:
s += "-"
if info.mode & 0o010:
s += "x"
else:
s += "-"
# Other
if info.mode & 0o004:
s += "r"
else:
s += "-"
if info.mode & 0o002:
s += "w"
else:
s += "-"
if info.mode & 0o001:
s += "x"
else:
s += "-"
return s
parser = argparse.ArgumentParser(
description="carve files from tar archive",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("--verbose", action="store_true", help="enable verbose output")
parser.add_argument("--errors", action="store_true", help="output tar header errors")
parser.add_argument("--offsets", action="store_true", help="output tar header offsets")
parser.add_argument("--path", default=os.getcwd(), help="output directory")
parser.add_argument(
"--attrs",
action=argparse.BooleanOptionalAction,
default=True,
help="set output mtime and mode",
)
parser.add_argument(
"-t", "--list", action="store_true", help="list contents of archive"
)
parser.add_argument(
"-x", "--extract", action="store_true", help="extract files from archive"
)
parser.add_argument("file", nargs="?", help="path to file")
args = parser.parse_args()
if not args.list and not args.extract:
print("Option '--list' or '--extract' required.", file=sys.stderr)
print(f"Try '{sys.argv[0]} --help' for more information.", file=sys.stderr)
sys.exit(2)
if args.list and args.extract:
print(
"You may not specify more than one '--list' or '--extract' option.",
file=sys.stderr,
)
print(f"Try '{sys.argv[0]} --help' for more information.", file=sys.stderr)
sys.exit(2)
if args.file and args.file != "-":
f = open(args.file, "rb")
else:
f = sys.stdin.buffer
while True:
data = f.read(tarfile.BLOCKSIZE)
if len(data) != tarfile.BLOCKSIZE:
break
try:
info = tarfile.TarInfo.frombuf(data, "utf-8", "strict")
except (tarfile.HeaderError, UnicodeDecodeError) as e:
if args.errors:
print(f"Invalid tar header at {f.tell() - tarfile.BLOCKSIZE}: {e}")
continue
except Exception as e:
print(f"Unexpected error at {f.tell() - tarfile.BLOCKSIZE}", file=sys.stderr)
raise e
if args.offsets:
print(f"Valid tar header at {f.tell() - tarfile.BLOCKSIZE}")
data_len = (info.size + (tarfile.BLOCKSIZE - 1)) & ~(tarfile.BLOCKSIZE - 1)
name = info.name.lstrip("/")
output = os.path.join(args.path, name)
if args.list:
if args.verbose:
modes = get_modes(info)
size = str(info.size)
uid = str(info.uid)
gid = str(info.gid)
width = 19
pad = len(uid) + 1 + len(gid) + 1 + len(size)
if pad > width:
width = pad
mtime = datetime.datetime.fromtimestamp(info.mtime, datetime.UTC)
print(
f"{modes} {uid}/{gid} {info.size: >{width - pad + len(size)}} {mtime} {name}"
)
else:
print(name)
else:
if args.verbose:
print(name)
os.makedirs(os.path.dirname(output), mode=0o755, exist_ok=True)
with open(output, "wb") as o:
data = f.read(data_len)
o.write(data[: info.size])
if args.attrs:
os.chmod(output, info.mode, follow_symlinks=False)
os.utime(output, (info.mtime, info.mtime), follow_symlinks=False)