wok-next view fusecloop/stuff/fusecloop.u @ rev 10935
fusecloop: add create_compressed_fs
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Aug 27 13:32:15 2011 +0200 (2011-08-27) |
parents | 9de4fcf2f70c |
children | e5f7f1b3d407 |
line source
1 --- compressed_loop.h
2 +++ compressed_loop.h
3 @@ -41,6 +41,73 @@
4 /* data_index (num_blocks 64bit pointers, network order)... */
5 /* compressed data (gzip block compressed format)... */
7 +struct cloop_tail
8 +{
9 + u_int32_t index_size;
10 + u_int32_t num_blocks;
11 +};
12 +
13 +struct block_info
14 +{
15 + loff_t offset; /* 64-bit offsets of compressed block */
16 + u_int32_t size; /* 32-bit compressed block size */
17 + u_int32_t optidx; /* 32-bit index number */
18 +};
19 +
20 +static inline char *build_index(struct block_info *offsets, unsigned long n)
21 +{
22 + u_int32_t *ofs32 = (u_int32_t *) offsets;
23 + loff_t *ofs64 = (loff_t *) offsets;
24 +
25 + if (ofs32[0] == 0) {
26 + if (ofs32[2]) { /* ACCELERATED KNOPPIX V1.0 */
27 + while (n--) {
28 + offsets[n].offset = __be64_to_cpu(offsets[n].offset);
29 + offsets[n].size = ntohl(offsets[n].size);
30 + }
31 + return "128BE accelerated knoppix 1.0";
32 + }
33 + else { /* V2.0 */
34 + loff_t last = __be64_to_cpu(ofs64[n]);
35 + while (n--) {
36 + offsets[n].size = last -
37 + (offsets[n].offset = __be64_to_cpu(ofs64[n]));
38 + last = offsets[n].offset;
39 + }
40 + return "64BE v2.0";
41 + }
42 + }
43 + else if (ofs32[1] == 0) { /* V1.0 */
44 + loff_t last = __be64_to_cpu(ofs64[n]);
45 + while (n--) {
46 + offsets[n].size = last -
47 + (offsets[n].offset = __le64_to_cpu(ofs64[n]));
48 + last = offsets[n].offset;
49 + }
50 + return "64LE v1.0";
51 + }
52 + else if (ntohl(ofs32[0]) == (4*n) + 0x8C) { /* V0.68 */
53 + loff_t last = ntohl(ofs32[n]);
54 + while (n--) {
55 + offsets[n].size = last -
56 + (offsets[n].offset = ntohl(ofs32[n]));
57 + last = offsets[n].offset;
58 + }
59 + return "32BE v0.68";
60 + }
61 + else { /* V3.0 */
62 + int i, j;
63 +
64 + for (i = n; i-- > 0; )
65 + offsets[i].size = ntohl(ofs32[i]);
66 + for (i = 0, j = sizeof(struct cloop_head); i < n; i++) {
67 + offsets[i].offset = j;
68 + j += offsets[i].size;
69 + }
70 + return "32BE v3.0";
71 + }
72 +}
73 +
74 /* Cloop suspend IOCTL */
75 #define CLOOP_SUSPEND 0x4C07
78 --- cloopreader.h
79 +++ cloopreader.h
80 @@ -33,7 +33,7 @@
81 int numblocks;
82 ulong blocksize;
84 - loff_t* toc; /* Data index */
85 + struct block_info *toc; /* Data index */
86 size_t tocsize;
88 unsigned char* cblock; /* Compressed block */
90 --- cloopreader.c
91 +++ cloopreader.c
92 @@ -59,10 +59,21 @@
94 ALLOC(c->pblock,c->blocksize);
96 - c->tocsize=sizeof *c->toc * (c->numblocks+1); /* One extra address is position of EOF */
97 + c->tocsize=sizeof(*c->toc) * c->numblocks;
98 + if (c->numblocks == -1) {
99 + struct cloop_tail tail;
100 + loff_t end = lseek(c->fh,0,SEEK_END); /* lseek(,-n,SEEK_END) buggy ? */
101 +
102 + OP(lseek(c->fh, end - sizeof(tail), SEEK_SET));
103 + OP(read_all(c->fh, &tail, sizeof(tail)));
104 + c->numblocks = ntohl(tail.num_blocks);
105 + c->tocsize = ntohl(tail.index_size) * c->numblocks;
106 + OP(lseek(c->fh, end - sizeof(tail) - c->tocsize, SEEK_SET));
107 + }
108 ALLOC(c->toc,c->tocsize);
110 OP(read_all(c->fh,c->toc,c->tocsize)); /* read Data Index */
111 + build_index(c->toc, c->numblocks);
112 c->cblocksizecur=0;
113 c->curblock=-1;
114 return 0;
115 @@ -79,10 +90,10 @@
116 if(page>=c->numblocks){errno=EFAULT;return -1;}
117 c->curblock=page;
119 - bprintf("Seeking to 0x%Lx\n",btc(c->toc[page]));
120 - OP(lseek(c->fh,btc(c->toc[page]), SEEK_SET));
121 + bprintf("Seeking to 0x%Lx\n",c->toc[page].offset);
122 + OP(lseek(c->fh,c->toc[page].offset, SEEK_SET));
124 - c->cblocksize=btc(c->toc[page+1]) - btc(c->toc[page]);
125 + c->cblocksize=c->toc[page].size;
126 bprintf("Compressed size=%lu\n",c->cblocksize);
127 if(c->cblocksize > c->cblocksizecur){
128 if(c->cblocksizecur)free(c->cblock);
130 --- extract_compressed_fs.c
131 +++ extract_compressed_fs.c
132 @@ -1,15 +1,18 @@
133 /* Extracts a filesystem back from a compressed fs file */
134 #include "common_header.h"
135 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V2.0 Format\n" "insmod cloop.o file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
137 int main(int argc, char *argv[])
138 {
139 int handle;
140 struct cloop_head head;
141 unsigned int i;
142 + unsigned long num_blocks, block_size, zblock_maxsize;
143 unsigned char *buffer, *clear_buffer;
144 + struct block_info *offsets;
146 - if (argc != 2) {
147 - fprintf(stderr, "Need filename\n");
148 + if (argc < 2 || argv[1][0] == '-') {
149 + fprintf(stderr, "Usage: extract_compressed_fs file [--convert-to-v2] > output\n");
150 exit(1);
151 }
153 @@ -24,44 +27,77 @@
154 exit(1);
155 }
157 - buffer = malloc(ntohl(head.block_size) + ntohl(head.block_size)/1000
158 - + 12 + 4);
159 - clear_buffer = malloc(ntohl(head.block_size));
160 - fprintf(stderr, "%u blocks of size %u. Preamble:\n%s\n",
161 - ntohl(head.num_blocks), ntohl(head.block_size), head.preamble);
162 -
163 - for (i = 0; i < ntohl(head.num_blocks); i++) {
164 - int currpos;
165 - unsigned long destlen = ntohl(head.block_size);
166 - loff_t offset[2];
167 - unsigned int size;
168 + num_blocks = ntohl(head.num_blocks);
169 + block_size = ntohl(head.block_size);
170 + zblock_maxsize = block_size + block_size/1000 + 12 + 4;
171 + buffer = malloc(zblock_maxsize);
172 + clear_buffer = malloc(block_size);
173 + fprintf(stderr, "%lu blocks of size %lu. Preamble:\n%s\n",
174 + num_blocks, block_size, head.preamble);
175 +
176 + if (num_blocks == -1) {
177 + struct cloop_tail tail;
178 + if (lseek(handle, - sizeof(tail), SEEK_END) < 0 ||
179 + read(handle, &tail, sizeof(tail)) != sizeof(tail) ||
180 + lseek(handle, - sizeof(tail) -
181 + (ntohl(tail.num_blocks) * ntohl(tail.index_size)),
182 + SEEK_END) < 0) {
183 + perror("Reading tail\n");
184 + exit(1);
185 + }
186 + head.num_blocks = tail.num_blocks;
187 + num_blocks = ntohl(head.num_blocks);
188 + i = num_blocks * ntohl(tail.index_size);
189 + }
190 + else i = num_blocks * sizeof(*offsets);
191 + offsets = malloc(i);
192 + if (!offsets || read(handle, offsets, i) != i) {
193 + perror("Reading index\n");
194 + exit(1);
195 + }
196 +
197 + fprintf(stderr, "Index %s.\n", build_index(offsets, num_blocks));
198 +
199 + if (argc > 2) {
200 + loff_t offset;
201 + int delta = ((num_blocks + 1) * sizeof(offset)) +
202 + sizeof(head) - offsets[0].offset;
203 +
204 + strcpy(head.preamble, CLOOP_PREAMBLE);
205 + write(STDOUT_FILENO, &head, sizeof(head));
206 + for (i = 0; i < num_blocks; i++) {
207 + offset = __be64_to_cpu(delta + offsets[i].offset);
208 + write(STDOUT_FILENO, &offset, sizeof(offset));
209 + }
210 + offset = __be64_to_cpu(delta + offsets[num_blocks-1].offset + offsets[num_blocks-1].size);
211 + write(STDOUT_FILENO, &offset, sizeof(offset));
212 + for (i = 0; i < num_blocks && lseek(handle, offsets[i].offset, SEEK_SET) >= 0; i++) {
213 + read(handle, buffer, offsets[i].size);
214 + write(STDOUT_FILENO, buffer, offsets[i].size);
215 + }
216 + return 0;
217 + }
218 +
219 + for (i = 0; i < num_blocks; i++) {
220 + unsigned long destlen = block_size;
221 + unsigned int size = offsets[i].size;
223 - read(handle, &offset, 2*sizeof(loff_t));
224 - lseek(handle, -sizeof(loff_t), SEEK_CUR);
225 -
226 - currpos = lseek(handle, 0, SEEK_CUR);
227 - if (lseek(handle, __be64_to_cpu(offset[0]), SEEK_SET) < 0) {
228 + if (lseek(handle, offsets[i].offset, SEEK_SET) < 0) {
229 fprintf(stderr, "lseek to %Lu: %s\n",
230 - __be64_to_cpu(offset[0]), strerror(errno));
231 + offsets[i].offset, strerror(errno));
232 exit(1);
233 }
235 - size=__be64_to_cpu(offset[1])-__be64_to_cpu(offset[0]);
236 - if (size > ntohl(head.block_size) + ntohl(head.block_size)/1000
237 - + 12 + 4) {
238 + if (size > zblock_maxsize) {
239 fprintf(stderr,
240 "Size %u for block %u (offset %Lu) too big\n",
241 - size, i, __be64_to_cpu(offset[0]));
242 + size, i, offsets[i].offset);
243 exit(1);
244 }
245 read(handle, buffer, size);
246 - if (lseek(handle, currpos, SEEK_SET) < 0) {
247 - perror("seeking");
248 - exit(1);
249 - }
251 - fprintf(stderr, "Block %u length %u => %lu\n",
252 - i, size, destlen);
253 + fprintf(stderr, "Block %u at %llu length %u => %lu\n",
254 + i, offsets[i].offset, size, destlen);
255 if (i == 3) {
256 fprintf(stderr,
257 "Block head:%02X%02X%02X%02X%02X%02X%02X%02X\n",
258 @@ -105,12 +141,12 @@
259 fprintf(stderr, "Uncomp: unknown error %u\n", i);
260 exit(1);
261 }
262 - if (destlen != ntohl(head.block_size)) {
263 - fprintf(stderr, "Uncomp: bad len %u (%lu not %u)\n", i,
264 - destlen, ntohl(head.block_size));
265 + if (destlen != block_size) {
266 + fprintf(stderr, "Uncomp: bad len %u (%lu not %lu)\n", i,
267 + destlen, block_size);
268 exit(1);
269 }
270 - write(STDOUT_FILENO, clear_buffer, ntohl(head.block_size));
271 + write(STDOUT_FILENO, clear_buffer, block_size);
272 }
273 return 0;
274 }
276 --- Makefile
277 +++ Makefile
278 @@ -1,16 +1,19 @@
279 PROGNAME=fusecloop
280 ARCFILES=*.c *.h *.pl Makefile configure README VERSION HELP INSTALL typescript *.cloop COPYING
281 -PROGS=fusecloop cloopreaderdemo extract_compressed_fs
282 +PROGS=fusecloop cloopreaderdemo extract_compressed_fs create_compressed_fs
283 FUSECFLAGS=`pkg-config fuse --cflags`
284 FUSELDFLAGS=`pkg-config fuse --libs`
286 CFLAGS= -Wall
288 -all: fusecloop extract_compressed_fs
289 +all: fusecloop extract_compressed_fs create_compressed_fs
291 extract_compressed_fs: extract_compressed_fs.c
292 ${CC} ${CFLAGS} ${LDFLAGS} -lz extract_compressed_fs.c -o extract_compressed_fs
294 +create_compressed_fs: create_compressed_fs.c
295 + ${CC} ${CFLAGS} ${LDFLAGS} -lz create_compressed_fs.c -o create_compressed_fs
296 +
297 fusecloop: fusecloop.c cloopreader.o strver debug.o
298 ${CC} ${CFLAGS} ${LDFLAGS} -lz cloopreader.o ${FUSECFLAGS} ${FUSELDFLAGS} fusecloop.c debug.o -o fusecloop
301 --- create_compressed_fs.c
302 +++ create_compressed_fs.c
303 @@ -0,0 +1,80 @@
304 +/* Creates a compressed file */
305 +#include "common_header.h"
306 +
307 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V3.0 Format\n" "insmod cloop.o file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
308 +#define CHUNK 65536
309 +#define DEFAULT_BLOCKSIZE 65536
310 +
311 +static void quit(char *s)
312 +{
313 + fprintf(stderr, "%s\n", s);
314 + exit(1);
315 +}
316 +
317 +static int readblock(unsigned char *buffer, int n)
318 +{
319 + int i;
320 +
321 + memset(buffer, 0, n);
322 + for (i = 0 ; i < n;) {
323 + int j = read(STDIN_FILENO, buffer + i, n - i);
324 + if (j < 0 && errno == EINTR) continue;
325 + if (j <= 0) break;
326 + i += j;
327 + }
328 + return i;
329 +}
330 +
331 +int main(int argc, char *argv[])
332 +{
333 + struct cloop_head head;
334 + struct cloop_tail tail;
335 + unsigned long block_size = 0;
336 + unsigned char *compressed, *uncompressed;
337 + unsigned long *index;
338 + int n, indexmax, zlenmax;
339 +
340 + if (argc > 1) {
341 + if (argv[1][0] < '0' || argv[1][0] > '9')
342 + quit("Usage : create_compressed_fs [block size] < input > output");
343 + block_size = atoi(argv[1]);
344 + }
345 + if (block_size < 4096)
346 + block_size = DEFAULT_BLOCKSIZE;
347 + fprintf(stderr, "Block size is %lu\n", block_size);
348 + zlenmax = block_size + block_size/1000 + 12;
349 +
350 + memset(&head, 0, sizeof(head));
351 + strcpy(head.preamble, CLOOP_PREAMBLE);
352 + head.num_blocks = -1;
353 + head.block_size = htonl(block_size);
354 + write(STDOUT_FILENO, &head, sizeof(head));
355 +
356 + compressed = malloc(zlenmax);
357 + uncompressed = malloc(block_size);
358 + index = malloc(indexmax = CHUNK);
359 + if (!compressed || !uncompressed || !index)
360 + quit("Malloc failed");
361 +
362 + for (n = 0; readblock(uncompressed, block_size); n++) {
363 + unsigned long len = zlenmax;
364 +
365 + if (compress2(compressed, &len, uncompressed, block_size,
366 + Z_BEST_COMPRESSION) != Z_OK)
367 + quit("Compression failed");
368 + fprintf(stderr, "Block %u length %lu => %lu\n",
369 + n, block_size, len);
370 + write(STDOUT_FILENO, compressed, len);
371 + if (n * sizeof(*index) >= indexmax) {
372 + index = realloc(index, indexmax += CHUNK);
373 + if (!index)
374 + quit("Realloc");
375 + }
376 + index[n] = ntohl(len);
377 + }
378 + write(STDOUT_FILENO, index, n * sizeof(*index));
379 + tail.index_size = ntohl(sizeof(*index));
380 + tail.num_blocks = ntohl(n);
381 + write(STDOUT_FILENO, &tail, sizeof(tail));
382 + return 0;
383 +}