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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#define mkdir plan9mkdir
enum{
LEN = 8*1024,
NFLDS = 6, /* filename, modes, uid, gid, mtime, bytes */
};
int selected(char*, int, char*[]);
void mkdirs(char*, char*);
void mkdir(char*, ulong, ulong, char*, char*);
void extract(char*, ulong, ulong, char*, char*, uvlong);
void seekpast(uvlong);
void error(char*, ...);
void warn(char*, ...);
void usage(void);
#pragma varargck argpos warn 1
#pragma varargck argpos error 1
Biobuf bin;
uchar binbuf[2*LEN];
char linebuf[LEN];
int uflag;
int hflag;
int vflag;
int Tflag;
void
main(int argc, char **argv)
{
Biobuf bout;
char *fields[NFLDS], name[2*LEN], *p, *namep;
char *uid, *gid;
ulong mode, mtime;
uvlong bytes;
quotefmtinstall();
namep = name;
ARGBEGIN{
case 'd':
p = ARGF();
if(strlen(p) >= LEN)
error("destination fs name too long\n");
strcpy(name, p);
namep = name + strlen(name);
break;
case 'h':
hflag = 1;
Binit(&bout, 1, OWRITE);
break;
case 'u':
uflag = 1;
Tflag = 1;
break;
case 'T':
Tflag = 1;
break;
case 'v':
vflag = 1;
break;
default:
usage();
}ARGEND
Binits(&bin, 0, OREAD, binbuf, sizeof binbuf);
while(p = Brdline(&bin, '\n')){
p[Blinelen(&bin)-1] = '\0';
strcpy(linebuf, p);
p = linebuf;
if(strcmp(p, "end of archive") == 0){
Bterm(&bout);
fprint(2, "done\n");
exits(0);
}
if (gettokens(p, fields, NFLDS, " \t") != NFLDS){
warn("too few fields in file header");
continue;
}
p = unquotestrdup(fields[0]);
strcpy(namep, p);
free(p);
mode = strtoul(fields[1], 0, 8);
uid = fields[2];
gid = fields[3];
mtime = strtoul(fields[4], 0, 10);
bytes = strtoull(fields[5], 0, 10);
if(argc){
if(!selected(namep, argc, argv)){
if(bytes)
seekpast(bytes);
continue;
}
mkdirs(name, namep);
}
if(hflag){
Bprint(&bout, "%q %luo %q %q %lud %llud\n",
name, mode, uid, gid, mtime, bytes);
if(bytes)
seekpast(bytes);
continue;
}
if(mode & DMDIR)
mkdir(name, mode, mtime, uid, gid);
else
extract(name, mode, mtime, uid, gid, bytes);
}
fprint(2, "premature end of archive\n");
exits("premature end of archive");
}
int
fileprefix(char *prefix, char *s)
{
while(*prefix)
if(*prefix++ != *s++)
return 0;
if(*s && *s != '/')
return 0;
return 1;
}
int
selected(char *s, int argc, char *argv[])
{
int i;
for(i=0; i<argc; i++)
if(fileprefix(argv[i], s))
return 1;
return 0;
}
void
mkdirs(char *name, char *namep)
{
char buf[2*LEN], *p;
int fd;
strcpy(buf, name);
for(p = &buf[namep - name]; p = utfrune(p, '/'); p++){
if(p[1] == '\0')
return;
*p = 0;
fd = create(buf, OREAD, 0775|DMDIR);
close(fd);
*p = '/';
}
}
void
mkdir(char *name, ulong mode, ulong mtime, char *uid, char *gid)
{
Dir *d, xd;
int fd;
char *p;
char olderr[256];
fd = create(name, OREAD, mode);
if(fd < 0){
rerrstr(olderr, sizeof(olderr));
if((d = dirstat(name)) == nil || !(d->mode & DMDIR)){
free(d);
warn("can't make directory %q, mode %luo: %s", name, mode, olderr);
return;
}
free(d);
}
close(fd);
d = &xd;
nulldir(d);
p = utfrrune(name, L'/');
if(p)
p++;
else
p = name;
d->name = p;
if(uflag){
d->uid = uid;
d->gid = gid;
}
if(Tflag)
d->mtime = mtime;
d->mode = mode;
if(dirwstat(name, d) < 0)
warn("can't set modes for %q: %r", name);
if(uflag||Tflag){
if((d = dirstat(name)) == nil){
warn("can't reread modes for %q: %r", name);
return;
}
if(Tflag && d->mtime != mtime)
warn("%q: time mismatch %lud %lud\n", name, mtime, d->mtime);
if(uflag && strcmp(uid, d->uid))
warn("%q: uid mismatch %q %q", name, uid, d->uid);
if(uflag && strcmp(gid, d->gid))
warn("%q: gid mismatch %q %q", name, gid, d->gid);
}
}
void
extract(char *name, ulong mode, ulong mtime, char *uid, char *gid, uvlong bytes)
{
Dir d, *nd;
Biobuf *b;
char buf[LEN];
char *p;
ulong n;
uvlong tot;
if(vflag)
print("x %q %llud bytes\n", name, bytes);
b = Bopen(name, OWRITE);
if(!b){
warn("can't make file %q: %r", name);
seekpast(bytes);
return;
}
for(tot = 0; tot < bytes; tot += n){
n = sizeof buf;
if(tot + n > bytes)
n = bytes - tot;
n = Bread(&bin, buf, n);
if(n <= 0)
error("premature eof reading %q", name);
if(Bwrite(b, buf, n) != n)
warn("error writing %q: %r", name);
}
nulldir(&d);
p = utfrrune(name, '/');
if(p)
p++;
else
p = name;
d.name = p;
if(uflag){
d.uid = uid;
d.gid = gid;
}
if(Tflag)
d.mtime = mtime;
d.mode = mode;
Bflush(b);
if(dirfwstat(Bfildes(b), &d) < 0)
warn("can't set modes for %q: %r", name);
if(uflag||Tflag){
if((nd = dirfstat(Bfildes(b))) == nil)
warn("can't reread modes for %q: %r", name);
else{
if(Tflag && nd->mtime != mtime)
warn("%q: time mismatch %lud %lud\n",
name, mtime, nd->mtime);
if(uflag && strcmp(uid, nd->uid))
warn("%q: uid mismatch %q %q",
name, uid, nd->uid);
if(uflag && strcmp(gid, nd->gid))
warn("%q: gid mismatch %q %q",
name, gid, nd->gid);
free(nd);
}
}
Bterm(b);
}
void
seekpast(uvlong bytes)
{
char buf[LEN];
long n;
uvlong tot;
for(tot = 0; tot < bytes; tot += n){
n = sizeof buf;
if(tot + n > bytes)
n = bytes - tot;
n = Bread(&bin, buf, n);
if(n < 0)
error("premature eof");
}
}
void
error(char *fmt, ...)
{
char buf[1024];
va_list arg;
sprint(buf, "%q: ", argv0);
va_start(arg, fmt);
vseprint(buf+strlen(buf), buf+sizeof(buf), fmt, arg);
va_end(arg);
fprint(2, "%s\n", buf);
exits(0);
}
void
warn(char *fmt, ...)
{
char buf[1024];
va_list arg;
sprint(buf, "%q: ", argv0);
va_start(arg, fmt);
vseprint(buf+strlen(buf), buf+sizeof(buf), fmt, arg);
va_end(arg);
fprint(2, "%s\n", buf);
}
void
usage(void)
{
fprint(2, "usage: mkext [-h] [-u] [-v] [-d dest-fs] [file ...]\n");
exits("usage");
}
|