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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
#include "stdinc.h"
#include <fcall.h> /* dirmodefmt */
#include "vac.h"
#ifndef PLAN9PORT
#pragma varargck type "t" ulong
#endif
VacFs *fs;
int tostdout;
int diff;
int nwant;
char **want;
int *found;
int chatty;
VtConn *conn;
int errors;
int settimes;
int table;
int mtimefmt(Fmt*);
void unvac(VacFile*, char*, VacDir*);
void
usage(void)
{
fprint(2, "usage: unvac [-TVcdtv] [-h host] file.vac [file ...]\n");
threadexitsall("usage");
}
struct
{
vlong data;
vlong skipdata;
} stats;
void
threadmain(int argc, char *argv[])
{
int i, printstats;
char *host;
VacFile *f;
fmtinstall('H', encodefmt);
fmtinstall('V', vtscorefmt);
fmtinstall('F', vtfcallfmt);
fmtinstall('t', mtimefmt);
fmtinstall('M', dirmodefmt);
host = nil;
printstats = 0;
ARGBEGIN{
case 'T':
settimes = 1;
break;
case 'V':
chattyventi = 1;
break;
case 'c':
tostdout++;
break;
case 'd':
diff++;
break;
case 'h':
host = EARGF(usage());
break;
case 's':
printstats++;
break;
case 't':
table++;
break;
case 'v':
chatty++;
break;
default:
usage();
}ARGEND
if(argc < 1)
usage();
if(tostdout && diff){
fprint(2, "cannot use -c with -d\n");
usage();
}
conn = vtdial(host);
if(conn == nil)
sysfatal("could not connect to server: %r");
if(vtconnect(conn) < 0)
sysfatal("vtconnect: %r");
fs = vacfsopen(conn, argv[0], VtOREAD, 4<<20);
if(fs == nil)
sysfatal("vacfsopen: %r");
nwant = argc-1;
want = argv+1;
found = vtmallocz(nwant*sizeof found[0]);
if((f = vacfsgetroot(fs)) == nil)
sysfatal("vacfsgetroot: %r");
unvac(f, nil, nil);
for(i=0; i<nwant; i++){
if(want[i] && !found[i]){
fprint(2, "warning: didn't find %s\n", want[i]);
errors++;
}
}
if(errors)
threadexitsall("errors");
if(printstats)
fprint(2, "%lld bytes read, %lld bytes skipped\n",
stats.data, stats.skipdata);
threadexitsall(0);
}
int
writen(int fd, char *buf, int n)
{
int m;
int oldn;
oldn = n;
while(n > 0){
m = write(fd, buf, n);
if(m <= 0)
return -1;
buf += m;
n -= m;
}
return oldn;
}
int
wantfile(char *name)
{
int i, namelen, n;
if(nwant == 0)
return 1;
namelen = strlen(name);
for(i=0; i<nwant; i++){
if(want[i] == nil)
continue;
n = strlen(want[i]);
if(n < namelen && name[n] == '/' && memcmp(name, want[i], n) == 0)
return 1;
if(namelen < n && want[i][namelen] == '/' && memcmp(want[i], name, namelen) == 0)
return 1;
if(n == namelen && memcmp(name, want[i], n) == 0){
found[i] = 1;
return 1;
}
}
return 0;
}
void
unvac(VacFile *f, char *name, VacDir *vdir)
{
static char buf[65536];
int fd, n, m, bsize;
ulong mode, mode9;
char *newname;
char *what;
vlong off;
Dir d, *dp;
VacDirEnum *vde;
VacDir newvdir;
VacFile *newf;
if(vdir)
mode = vdir->mode;
else
mode = vacfilegetmode(f);
if(vdir){
if(table){
if(chatty){
mode9 = vdir->mode&0777;
if(mode&ModeDir)
mode9 |= DMDIR;
if(mode&ModeAppend)
mode9 |= DMAPPEND;
if(mode&ModeExclusive)
mode9 |= DMEXCL;
#ifdef PLAN9PORT
if(mode&ModeLink)
mode9 |= DMSYMLINK;
if(mode&ModeNamedPipe)
mode9 |= DMNAMEDPIPE;
if(mode&ModeSetUid)
mode9 |= DMSETUID;
if(mode&ModeSetGid)
mode9 |= DMSETGID;
if(mode&ModeDevice)
mode9 |= DMDEVICE;
#endif
print("%M %-10s %-10s %11lld %t %s\n",
mode9, vdir->uid, vdir->gid, vdir->size,
vdir->mtime, name);
}else
print("%s%s\n", name, (mode&ModeDir) ? "/" : "");
}
else if(chatty)
fprint(2, "%s%s\n", name, (mode&ModeDir) ? "/" : "");
}
if(mode&(ModeDevice|ModeLink|ModeNamedPipe|ModeExclusive)){
if(table)
return;
if(mode&ModeDevice)
what = "device";
else if(mode&ModeLink)
what = "link";
else if(mode&ModeNamedPipe)
what = "named pipe";
else if(mode&ModeExclusive)
what = "lock";
else
what = "unknown type of file";
fprint(2, "warning: ignoring %s %s\n", what, name);
return;
}
if(mode&ModeDir){
if((vde = vdeopen(f)) == nil){
fprint(2, "vdeopen %s: %r", name);
errors++;
return;
}
if(!table && !tostdout && vdir){
// create directory
if((dp = dirstat(name)) == nil){
if((fd = create(name, OREAD, DMDIR|(mode&0777))) < 0){
fprint(2, "mkdir %s: %r\n", name);
vdeclose(vde);
}
close(fd);
}else{
if(!(dp->mode&DMDIR)){
fprint(2, "%s already exists and is not a directory\n", name);
errors++;
free(dp);
vdeclose(vde);
return;
}
free(dp);
}
}
while(vderead(vde, &newvdir) > 0){
if(name == nil)
newname = newvdir.elem;
else
newname = smprint("%s/%s", name, newvdir.elem);
if(wantfile(newname)){
if((newf = vacfilewalk(f, newvdir.elem)) == nil){
fprint(2, "walk %s: %r\n", name);
errors++;
}else if(newf == f){
fprint(2, "walk loop: %s\n", newname);
vacfiledecref(newf);
}else{
unvac(newf, newname, &newvdir);
vacfiledecref(newf);
}
}
if(newname != newvdir.elem)
free(newname);
vdcleanup(&newvdir);
}
vdeclose(vde);
}else{
if(!table){
off = 0;
if(tostdout)
fd = dup(1, -1);
else if(diff && (fd = open(name, ORDWR)) >= 0){
bsize = vacfiledsize(f);
while((n = readn(fd, buf, bsize)) > 0){
if(sha1matches(f, off/bsize, (uchar*)buf, n)){
off += n;
stats.skipdata += n;
continue;
}
seek(fd, off, 0);
if((m = vacfileread(f, buf, n, off)) < 0)
break;
if(writen(fd, buf, m) != m){
fprint(2, "write %s: %r\n", name);
goto Err;
}
off += m;
stats.data += m;
if(m < n){
nulldir(&d);
d.length = off;
if(dirfwstat(fd, &d) < 0){
fprint(2, "dirfwstat %s: %r\n", name);
goto Err;
}
break;
}
}
}
else if((fd = create(name, OWRITE, mode&0777)) < 0){
fprint(2, "create %s: %r\n", name);
errors++;
return;
}
while((n = vacfileread(f, buf, sizeof buf, off)) > 0){
if(writen(fd, buf, n) != n){
fprint(2, "write %s: %r\n", name);
Err:
errors++;
close(fd);
remove(name);
return;
}
off += n;
stats.data += n;
}
close(fd);
}
}
if(vdir && settimes && !tostdout){
nulldir(&d);
d.mtime = vdir->mtime;
if(dirwstat(name, &d) < 0)
fprint(2, "warning: setting mtime on %s: %r", name);
}
}
int
mtimefmt(Fmt *f)
{
Tm *tm;
tm = localtime(va_arg(f->args, ulong));
fmtprint(f, "%04d-%02d-%02d %02d:%02d",
tm->year+1900, tm->mon+1, tm->mday,
tm->hour, tm->min);
return 0;
}
|