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
|
#include <u.h>
#include <libc.h>
#include "sac.h"
#include "sacfs.h"
enum {
NCACHE = 1024, /* must be power of two */
OffsetSize = 4, /* size of block offset */
};
int warn(char *);
int seen(Dir *);
void usage(void);
void outwrite(void *buf, int n, long off);
void putl(void *p, uint v);
void *emalloc(int size);
void sacfs(char *root);
void sacfile(char *name, Dir *dir, SacDir *sd);
void sacdir(char *name, Dir *dir, SacDir *sd);
int uflag=0;
int fflag=0;
long blocksize = 4*1024;
struct Out
{
int fd;
long size;
} out;
typedef struct Cache Cache;
struct Cache
{
Dir* cache;
int n;
int max;
} cache[NCACHE];
void
main(int argc, char *argv[])
{
char *s, *ss;
char *outfile = nil;
ARGBEGIN {
case 'u':
uflag=1;
break;
case 'o':
outfile = ARGF();
break;
case 'b':
s = ARGF();
if(s) {
blocksize = strtoul(s, &ss, 0);
if(s == ss)
usage();
if(*ss == 'k')
blocksize *= 1024;
}
if(blocksize < sizeof(SacDir))
sysfatal("blocksize too small");
break;
} ARGEND
if(outfile == nil) {
sysfatal("still to do: need to create temp file");
} else {
out.fd = create(outfile, OWRITE|OTRUNC, 0664);
if(out.fd < 0)
sysfatal("could not create file: %s: %r", outfile);
}
if(argc==0)
sacfs(".");
else
sacfs(argv[0]);
close(out.fd);
exits(0);
}
void
usage(void)
{
fprint(2, "usage: %s [-u] [-b blocksize] -o output [root]\n", argv0);
exits("usage");
}
void
sacfs(char *root)
{
Dir *dir;
long offset;
SacHeader hdr;
SacDir sd;
dir = dirstat(root);
if(dir == nil)
sysfatal("could not stat root: %s: %r", root);
offset = out.size;
out.size += sizeof(SacHeader) + sizeof(SacDir);
memset(&hdr, 0, sizeof(hdr));
putl(hdr.magic, Magic);
putl(hdr.blocksize, blocksize);
if(dir->mode & DMDIR)
sacdir(root, dir, &sd);
else
sacfile(root, dir, &sd);
putl(hdr.length, out.size);
outwrite(&hdr, sizeof(hdr), offset);
outwrite(&sd, sizeof(sd), offset+sizeof(SacHeader));
free(dir);
}
void
setsd(SacDir *sd, Dir *dir, long length, long blocks)
{
static qid = 1;
memset(sd, 0, sizeof(SacDir));
strncpy(sd->name, dir->name, NAMELEN);
strncpy(sd->uid, dir->uid, NAMELEN);
strncpy(sd->gid, dir->gid, NAMELEN);
putl(sd->qid, qid++|(dir->mode&DMDIR));
putl(sd->mode, dir->mode);
putl(sd->atime, dir->atime);
putl(sd->mtime, dir->mtime);
putl(sd->length, length);
putl(sd->blocks, blocks);
}
void
sacfile(char *name, Dir *dir, SacDir *sd)
{
int fd, i, n, nn;
long nblock;
uchar *blocks;
uchar *buf, *cbuf;
long offset;
long block;
fd = open(name, OREAD);
if(fd < 0)
sysfatal("could not open file: %s: %r", name);
nblock = (dir->length + blocksize-1)/blocksize;
blocks = emalloc((nblock+1)*OffsetSize);
buf = emalloc(blocksize);
cbuf = emalloc(blocksize);
offset = out.size;
out.size += (nblock+1)*OffsetSize;
for(i=0; i<nblock; i++) {
n = read(fd, buf, blocksize);
if(n < 0)
sysfatal("read failed: %s: %r", name);
if(n == 0)
sysfatal("unexpected eof: %s", name);
if(n < blocksize && i != nblock-1)
sysfatal("short read: %s: got %d", name, n);
block = out.size;
nn = sac(cbuf, buf, n);
if(nn < 0 || uflag) {
outwrite(buf, n, out.size);
out.size += n;
} else {
block = -block;
outwrite(cbuf, nn, out.size);
out.size += nn;
}
putl(blocks+i*OffsetSize, block);
}
putl(blocks+i*OffsetSize, out.size);
outwrite(blocks, (nblock+1)*OffsetSize, offset);
setsd(sd, dir, dir->length, offset);
close(fd);
free(buf);
free(cbuf);
free(blocks);
}
void
sacdir(char *name, Dir *dir, SacDir *sd)
{
Dir *dirs, *p;
int i, n, nn, per;
SacDir *sds;
int ndir, fd, nblock;
long offset, block;
uchar *blocks, *cbuf;
char file[512];
fd = open(name, OREAD);
if(fd < 0)
sysfatal("could not open directory: %s: %r", name);
ndir = dirreadall(fd, &dirs);
if(ndir < 0)
sysfatal("could not read directory: %s: %r", name);
close(fd);
per = blocksize/sizeof(SacDir);
nblock = (ndir+per-1)/per;
sds = emalloc(nblock*per*sizeof(SacDir));
p = dirs;
for(i=0; i<ndir; i++,p++) {
sprint(file, "%s/%s", name, p->name);
if(p->mode & DMDIR)
sacdir(file, p, sds+i);
else
sacfile(file, p, sds+i);
}
free(dirs);
blocks = emalloc((nblock+1)*OffsetSize);
offset = out.size;
out.size += (nblock+1)*OffsetSize;
n = per*sizeof(SacDir);
cbuf = emalloc(n);
for(i=0; i<nblock; i++) {
block = out.size;
if(n > (ndir-i*per)*sizeof(SacDir))
n = (ndir-i*per)*sizeof(SacDir);
nn = sac(cbuf, (uchar*)(sds+i*per), n);
if(nn < 0 || uflag) {
outwrite(sds+i*per, n, out.size);
out.size += n;
} else {
block = -block;
outwrite(cbuf, nn, out.size);
out.size += nn;
}
putl(blocks+i*OffsetSize, block);
}
free(cbuf);
putl(blocks+i*OffsetSize, out.size);
outwrite(blocks, (nblock+1)*OffsetSize, offset);
setsd(sd, dir, ndir, offset);
free(sds);
free(blocks);
}
int
seen(Dir *dir)
{
Dir *dp;
int i;
Cache *c;
c = &cache[dir->qid.path&(NCACHE-1)];
dp = c->cache;
for(i=0; i<c->n; i++, dp++)
if(dir->qid.path == dp->qid.path &&
dir->type == dp->type &&
dir->dev == dp->dev)
return 1;
if(c->n == c->max){
c->cache = realloc(c->cache, (c->max+=20)*sizeof(Dir));
if(cache == 0)
sysfatal("malloc failure");
}
c->cache[c->n++] = *dir;
return 0;
}
void
outwrite(void *buf, int n, long offset)
{
if(seek(out.fd, offset, 0) < 0)
sysfatal("seek failed: %r");
if(write(out.fd, buf, n) < n)
sysfatal("write failed: %r");
}
void
putl(void *p, uint v)
{
uchar *a;
a = p;
a[0] = v>>24;
a[1] = v>>16;
a[2] = v>>8;
a[3] = v;
}
void *
emalloc(int size)
{
void *p;
p = malloc(size);
if(p == nil)
sysfatal("malloc failed");
memset(p, 0, size);
return p;
}
|