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
|
implement Gettar;
include "sys.m";
sys: Sys;
print, sprint, fprint: import sys;
stdin, stderr: ref sys->FD;
include "draw.m";
include "arg.m";
TBLOCK: con 512; # tar logical blocksize
Header: adt{
name: string;
size: int;
mode: int;
mtime: int;
skip: int;
};
Gettar: module
{
init: fn(nil: ref Draw->Context, nil: list of string);
};
error(mess: string)
{
fprint(stderr,"gettar: %s\n",mess);
raise "fail:error";
}
verbose := 0;
NBLOCK: con 20; # traditional blocking factor for efficient read
tarbuf := array[NBLOCK*TBLOCK] of byte; # static buffer
nblock := NBLOCK; # how many blocks of data are in tarbuf
recno := NBLOCK; # how many blocks in tarbuf have been consumed
getblock(): array of byte
{
if(recno>=nblock){
i := sys->read(stdin,tarbuf,TBLOCK*NBLOCK);
if(i==0)
return nil;
if(i<0)
error(sys->sprint("read error: %r"));
if(i%TBLOCK!=0)
error("blocksize error");
nblock = i/TBLOCK;
recno = 0;
}
recno++;
return tarbuf[(recno-1)*TBLOCK:recno*TBLOCK];
}
octal(b:array of byte): int
{
sum := 0;
for(i:=0; i<len b; i++){
bi := int b[i];
if(bi==' ') continue;
if(bi==0) break;
sum = 8*sum + bi-'0';
}
return sum;
}
nullterm(b:array of byte): string
{
for(i:=0; i<len b; i++)
if(b[i]==byte 0) break;
return string b[0:i];
}
getdir(): ref Header
{
dblock := getblock();
if(len dblock==0)
return nil;
if(dblock[0]==byte 0)
return nil;
name := nullterm(dblock[0:100]);
if(int dblock[345]!=0)
name = nullterm(dblock[345:500])+"/"+name;
if(!absolute){
if(name[0] == '#')
name = "./"+name;
else if(name[0] == '/')
name = "."+name;
}
magic := string(dblock[257:262]);
if(magic[0]!=0 && magic!="ustar")
error("bad magic "+name);
chksum := octal(dblock[148:156]);
for(ci:=148; ci<156; ci++)
dblock[ci] = byte ' ';
for(i:=0; i<TBLOCK; i++)
chksum -= int dblock[i];
if(chksum!=0)
error("directory checksum error "+name);
skip := 1;
size := 0;
mode := 0;
mtime := 0;
case int dblock[156]{
'0' or '7' or 0 =>
skip = 0;
size = octal(dblock[124:136]);
mode = 8r777 & octal(dblock[100: 108]);
mtime = octal(dblock[136:148]);
'1' =>
fprint(stderr,"gettar: skipping link %s -> %s\n",name,string(dblock[157:257]));
'2' or 's' =>
fprint(stderr,"gettar: skipping symlink %s\n",name);
'3' or '4' or '6' =>
fprint(stderr,"gettar: skipping special file %s\n",name);
'5' =>
if(name[(len name)-1]=='/')
checkdir(name+".");
else
checkdir(name+"/.");
* =>
error(sprint("unrecognized typeflag %d for %s",int dblock[156],name));
}
return ref Header(name, size, mode, mtime, skip);
}
keep := 0;
absolute := 0;
init(nil: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
stdin = sys->fildes(0);
stderr = sys->fildes(2);
ofile: ref sys->FD;
arg := load Arg Arg->PATH;
arg->init(args);
arg->setusage("gettar [-kTRv] [file ...]");
while((o := arg->opt()) != 0)
case o {
'k' => keep = 1;
'v' => verbose = 1;
'R' => absolute = 1;
* => arg->usage();
}
args = arg->argv();
arg = nil;
while((file := getdir())!=nil){
if(!file.skip){
if((args == nil || matched(file.name, args)) && !(keep && exists(file.name))){
if(verbose)
sys->fprint(stderr, "%s\n", file.name);
checkdir(file.name);
ofile = sys->create(file.name, Sys->OWRITE, 8r666);
if(ofile==nil){
fprint(stderr, "gettar: cannot create %s: %r\n",file.name);
file.skip = 1;
}
}else
file.skip = 1;
}
bytes := file.size;
blocks := (bytes+TBLOCK-1)/TBLOCK;
if(file.skip){
for(; blocks>0; blocks--)
getblock();
continue;
}
for(; blocks>0; blocks--){
buf := getblock();
nwrite := bytes;
if(nwrite>TBLOCK)
nwrite = TBLOCK;
if(sys->write(ofile,buf,nwrite)!=nwrite)
error(sprint("write error for %s: %r",file.name));
bytes -= nwrite;
}
ofile = nil;
stat := sys->nulldir;
stat.mode = file.mode;
stat.mtime = file.mtime;
rc := sys->wstat(file.name,stat);
if(rc<0){
# try just the mode
stat.mtime = ~0;
rc = sys->wstat(file.name, stat);
if(rc < 0)
fprint(stderr,"gettar: cannot set mode/mtime %s %#o %ud: %r\n",file.name, file.mode, file.mtime);
}
}
}
checkdir(name: string)
{
(nc,compl) := sys->tokenize(name,"/");
path := "";
while(compl!=nil){
comp := hd compl;
if(comp=="..")
error(".. pathnames forbidden");
if(nc>1){
if(path=="")
path = comp;
else
path += "/"+comp;
(rc,stat) := sys->stat(path);
if(rc<0){
fd := sys->create(path,Sys->OREAD,Sys->DMDIR+8r777);
if(fd==nil)
error(sprint("cannot mkdir %s: %r",path));
fd = nil;
}else if(stat.mode&Sys->DMDIR==0)
error(sprint("found non-directory at %s",path));
}
nc--; compl = tl compl;
}
}
exists(path: string): int
{
return sys->stat(path).t0 >= 0;
}
matched(n: string, names: list of string): int
{
for(; names != nil; names = tl names){
p := hd names;
if(prefix(p, n))
return 1;
}
return 0;
}
prefix(p: string, s: string): int
{
l := len p;
if(l > len s)
return 0;
return p == s[0:l] && (l == len s || s[l] == '/');
}
|