wok-current view fusecloop/stuff/fusecloop.u @ rev 14712
Add libnl1
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Tue Jun 11 09:04:29 2013 +0200 (2013-06-11) |
parents | 297bc4cc8984 |
children | ade6b9da6804 |
line source
1 --- compressed_loop.h
2 +++ compressed_loop.h
3 @@ -41,6 +41,74 @@
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 table_size;
10 + u_int32_t index_size;
11 + u_int32_t num_blocks;
12 +};
13 +
14 +struct block_info
15 +{
16 + loff_t offset; /* 64-bit offsets of compressed block */
17 + u_int32_t size; /* 32-bit compressed block size */
18 + u_int32_t optidx; /* 32-bit index number */
19 +};
20 +
21 +static inline char *build_index(struct block_info *offsets, unsigned long n)
22 +{
23 + u_int32_t *ofs32 = (u_int32_t *) offsets;
24 + loff_t *ofs64 = (loff_t *) offsets;
25 +
26 + if (ofs32[0] == 0) {
27 + if (ofs32[2]) { /* ACCELERATED KNOPPIX V1.0 */
28 + while (n--) {
29 + offsets[n].offset = __be64_to_cpu(offsets[n].offset);
30 + offsets[n].size = ntohl(offsets[n].size);
31 + }
32 + return (char *) "128BE accelerated knoppix 1.0";
33 + }
34 + else { /* V2.0 */
35 + loff_t last = __be64_to_cpu(ofs64[n]);
36 + while (n--) {
37 + offsets[n].size = last -
38 + (offsets[n].offset = __be64_to_cpu(ofs64[n]));
39 + last = offsets[n].offset;
40 + }
41 + return (char *) "64BE v2.0";
42 + }
43 + }
44 + else if (ofs32[1] == 0) { /* V1.0 */
45 + loff_t last = __le64_to_cpu(ofs64[n]);
46 + while (n--) {
47 + offsets[n].size = last -
48 + (offsets[n].offset = __le64_to_cpu(ofs64[n]));
49 + last = offsets[n].offset;
50 + }
51 + return (char *) "64LE v1.0";
52 + }
53 + else if (ntohl(ofs32[0]) == (4*n) + 0x8C) { /* V0.68 */
54 + loff_t last = ntohl(ofs32[n]);
55 + while (n--) {
56 + offsets[n].size = last -
57 + (offsets[n].offset = ntohl(ofs32[n]));
58 + last = offsets[n].offset;
59 + }
60 + return (char *) "32BE v0.68";
61 + }
62 + else { /* V3.0 */
63 + int i, j;
64 +
65 + for (i = n; i-- > 0; )
66 + offsets[i].size = ntohl(ofs32[i]);
67 + for (i = 0, j = sizeof(struct cloop_head); i < n; i++) {
68 + offsets[i].offset = j;
69 + j += offsets[i].size;
70 + }
71 + return (char *) "32BE v3.0";
72 + }
73 +}
74 +
75 /* Cloop suspend IOCTL */
76 #define CLOOP_SUSPEND 0x4C07
79 --- cloopreader.h
80 +++ cloopreader.h
81 @@ -33,7 +33,7 @@
82 int numblocks;
83 ulong blocksize;
85 - loff_t* toc; /* Data index */
86 + struct block_info *toc; /* Data index */
87 size_t tocsize;
89 unsigned char* cblock; /* Compressed block */
91 --- cloopreader.c
92 +++ cloopreader.c
93 @@ -59,10 +59,32 @@
95 ALLOC(c->pblock,c->blocksize);
97 - c->tocsize=sizeof *c->toc * (c->numblocks+1); /* One extra address is position of EOF */
98 - ALLOC(c->toc,c->tocsize);
99 + if (c->numblocks + 1 == 0) {
100 + struct cloop_tail tail;
101 + loff_t end = lseek(c->fh,0,SEEK_END); /* lseek(,-n,SEEK_END) buggy ? */
102 + void *p;
103 + ulong toclen, len;
105 - OP(read_all(c->fh,c->toc,c->tocsize)); /* read Data Index */
106 + OP(lseek(c->fh, end - sizeof(tail), SEEK_SET));
107 + OP(read_all(c->fh, &tail, sizeof(tail)));
108 + c->numblocks = ntohl(tail.num_blocks);
109 + c->tocsize = sizeof(*c->toc) * c->numblocks;
110 + len = ntohl(tail.table_size);
111 + toclen = (ntohl(tail.index_size) & 255) * c->numblocks;
112 + OP(lseek(c->fh, end - sizeof(tail) - len, SEEK_SET));
113 + ALLOC(c->toc,c->tocsize);
114 + ALLOC(p,len);
115 + OP(read_all(c->fh,p,len)); /* read Data Index */
116 + if (uncompress((void *)c->toc,&toclen,p,len) != Z_OK)
117 + exit(1);
118 + free(p);
119 + }
120 + else {
121 + c->tocsize = sizeof(*c->toc) * c->numblocks;
122 + ALLOC(c->toc,c->tocsize);
123 + OP(read_all(c->fh,c->toc,c->tocsize)); /* read Data Index */
124 + }
125 + build_index(c->toc, c->numblocks);
126 c->cblocksizecur=0;
127 c->curblock=-1;
128 return 0;
129 @@ -79,10 +101,10 @@
130 if(page>=c->numblocks){errno=EFAULT;return -1;}
131 c->curblock=page;
133 - bprintf("Seeking to 0x%Lx\n",btc(c->toc[page]));
134 - OP(lseek(c->fh,btc(c->toc[page]), SEEK_SET));
135 + bprintf("Seeking to 0x%Lx\n",c->toc[page].offset);
136 + OP(lseek(c->fh,c->toc[page].offset, SEEK_SET));
138 - c->cblocksize=btc(c->toc[page+1]) - btc(c->toc[page]);
139 + c->cblocksize=c->toc[page].size;
140 bprintf("Compressed size=%lu\n",c->cblocksize);
141 if(c->cblocksize > c->cblocksizecur){
142 if(c->cblocksizecur)free(c->cblock);
144 --- extract_compressed_fs.c
145 +++ extract_compressed_fs.c
146 @@ -1,15 +1,19 @@
147 /* Extracts a filesystem back from a compressed fs file */
148 +#define _LARGEFILE64_SOURCE
149 #include "common_header.h"
150 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V2.0 Format\n" "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
152 int main(int argc, char *argv[])
153 {
154 int handle;
155 struct cloop_head head;
156 unsigned int i;
157 + unsigned long num_blocks, block_size, zblock_maxsize;
158 unsigned char *buffer, *clear_buffer;
159 + struct block_info *offsets;
161 - if (argc != 2) {
162 - fprintf(stderr, "Need filename\n");
163 + if (argc < 2 || argv[1][0] == '-') {
164 + fprintf(stderr, "Usage: extract_compressed_fs file [--convert-to-v2] > output\n");
165 exit(1);
166 }
168 @@ -24,44 +28,99 @@
169 exit(1);
170 }
172 - buffer = malloc(ntohl(head.block_size) + ntohl(head.block_size)/1000
173 - + 12 + 4);
174 - clear_buffer = malloc(ntohl(head.block_size));
175 - fprintf(stderr, "%u blocks of size %u. Preamble:\n%s\n",
176 - ntohl(head.num_blocks), ntohl(head.block_size), head.preamble);
177 + num_blocks = ntohl(head.num_blocks);
178 + block_size = ntohl(head.block_size);
179 + zblock_maxsize = block_size + block_size/1000 + 12 + 4;
180 + buffer = malloc(zblock_maxsize);
181 + clear_buffer = malloc(block_size);
183 - for (i = 0; i < ntohl(head.num_blocks); i++) {
184 - int currpos;
185 - unsigned long destlen = ntohl(head.block_size);
186 - loff_t offset[2];
187 - unsigned int size;
188 + if (num_blocks == (unsigned long) -1) {
189 + void *table;
190 + struct cloop_tail tail;
191 + unsigned long len, table_size;
192 + loff_t end = lseek64(handle, 0, SEEK_END);
193 +
194 + if (lseek64(handle, end - sizeof(tail), SEEK_SET) < 0 ||
195 + read(handle, &tail, sizeof(tail)) != sizeof(tail) ||
196 + lseek64(handle, end - sizeof(tail) -
197 + ntohl(tail.table_size), SEEK_SET) < 0) {
198 + perror("Reading tail\n");
199 + exit(1);
200 + }
201 + head.num_blocks = tail.num_blocks;
202 + num_blocks = ntohl(head.num_blocks);
203 + table_size = ntohl(tail.table_size);
204 + table = malloc(table_size);
205 + len = i = num_blocks * (ntohl(tail.index_size) & 255);
206 + offsets = malloc(num_blocks * sizeof(*offsets));
207 + if (!table || !offsets ||
208 + read(handle, table, table_size) != table_size ||
209 + uncompress((void *)offsets, &len, table, table_size) != Z_OK ||
210 + len != i) {
211 + perror("Reading index\n");
212 + exit(1);
213 + }
214 + free(table);
215 + }
216 + else {
217 + offsets = malloc(i = num_blocks * sizeof(*offsets));
218 + if (!offsets || read(handle, offsets, i) != i) {
219 + perror("Reading index\n");
220 + exit(1);
221 + }
222 + }
223 +
224 + fprintf(stderr, "%lu blocks of size %lu. Preamble:\n%s\n",
225 + num_blocks, block_size, head.preamble);
226 + fprintf(stderr, "Index %s.\n", build_index(offsets, num_blocks));
227 +
228 + if (argc > 2) {
229 + unsigned n;
230 + loff_t data, offset = ((num_blocks + 1) * sizeof(offset)) + sizeof(head);
231 +
232 + strcpy(head.preamble, CLOOP_PREAMBLE);
233 + write(STDOUT_FILENO, &head, n = sizeof(head));
234 + for (i = 0; i < num_blocks; i++) {
235 + data = __be64_to_cpu(offset);
236 + write(STDOUT_FILENO, &data, sizeof(data));
237 + n += sizeof(data);
238 + offset += offsets[i].size;
239 + }
240 + data = __be64_to_cpu(offset);
241 + write(STDOUT_FILENO, &data, sizeof(data));
242 + for (i = 0; i < num_blocks && lseek64(handle, offsets[i].offset, SEEK_SET) >= 0; i++) {
243 + read(handle, buffer, offsets[i].size);
244 + write(STDOUT_FILENO, buffer, offsets[i].size);
245 + n += offsets[i].size;
246 + }
247 + n &= 0x1FF;
248 + if (n) {
249 + memset(buffer, 0, 512);
250 + write(STDOUT_FILENO, buffer, 512 - n);
251 + }
252 + return 0;
253 + }
254 +
255 + for (i = 0; i < num_blocks; i++) {
256 + unsigned long destlen = block_size;
257 + unsigned int size = offsets[i].size;
259 - read(handle, &offset, 2*sizeof(loff_t));
260 - lseek(handle, -sizeof(loff_t), SEEK_CUR);
261 -
262 - currpos = lseek(handle, 0, SEEK_CUR);
263 - if (lseek(handle, __be64_to_cpu(offset[0]), SEEK_SET) < 0) {
264 + if (lseek64(handle, offsets[i].offset, SEEK_SET) < 0) {
265 fprintf(stderr, "lseek to %Lu: %s\n",
266 - __be64_to_cpu(offset[0]), strerror(errno));
267 + offsets[i].offset, strerror(errno));
268 exit(1);
269 }
271 - size=__be64_to_cpu(offset[1])-__be64_to_cpu(offset[0]);
272 - if (size > ntohl(head.block_size) + ntohl(head.block_size)/1000
273 - + 12 + 4) {
274 + if (size > zblock_maxsize) {
275 fprintf(stderr,
276 "Size %u for block %u (offset %Lu) too big\n",
277 - size, i, __be64_to_cpu(offset[0]));
278 + size, i, offsets[i].offset);
279 exit(1);
280 }
281 read(handle, buffer, size);
282 - if (lseek(handle, currpos, SEEK_SET) < 0) {
283 - perror("seeking");
284 - exit(1);
285 - }
287 - fprintf(stderr, "Block %u length %u => %lu\n",
288 - i, size, destlen);
289 + fprintf(stderr, "Block %u at %llu length %u => %lu\n",
290 + i, offsets[i].offset, size, destlen);
291 if (i == 3) {
292 fprintf(stderr,
293 "Block head:%02X%02X%02X%02X%02X%02X%02X%02X\n",
294 @@ -105,12 +164,12 @@
295 fprintf(stderr, "Uncomp: unknown error %u\n", i);
296 exit(1);
297 }
298 - if (destlen != ntohl(head.block_size)) {
299 - fprintf(stderr, "Uncomp: bad len %u (%lu not %u)\n", i,
300 - destlen, ntohl(head.block_size));
301 + if (destlen != block_size) {
302 + fprintf(stderr, "Uncomp: bad len %u (%lu not %lu)\n", i,
303 + destlen, block_size);
304 exit(1);
305 }
306 - write(STDOUT_FILENO, clear_buffer, ntohl(head.block_size));
307 + write(STDOUT_FILENO, clear_buffer, block_size);
308 }
309 return 0;
310 }
312 --- Makefile
313 +++ Makefile
314 @@ -1,16 +1,19 @@
315 PROGNAME=fusecloop
316 ARCFILES=*.c *.h *.pl Makefile configure README VERSION HELP INSTALL typescript *.cloop COPYING
317 -PROGS=fusecloop cloopreaderdemo extract_compressed_fs
318 +PROGS=fusecloop cloopreaderdemo extract_compressed_fs create_compressed_fs
319 FUSECFLAGS=`pkg-config fuse --cflags`
320 FUSELDFLAGS=`pkg-config fuse --libs`
322 CFLAGS= -Wall
324 -all: fusecloop extract_compressed_fs
325 +all: fusecloop extract_compressed_fs create_compressed_fs
327 extract_compressed_fs: extract_compressed_fs.c
328 ${CC} ${CFLAGS} ${LDFLAGS} -lz extract_compressed_fs.c -o extract_compressed_fs
330 +create_compressed_fs: create_compressed_fs.c
331 + ${CC} ${CFLAGS} ${LDFLAGS} -lz create_compressed_fs.c -o create_compressed_fs
332 +
333 fusecloop: fusecloop.c cloopreader.o strver debug.o
334 ${CC} ${CFLAGS} ${LDFLAGS} -lz cloopreader.o ${FUSECFLAGS} ${FUSELDFLAGS} fusecloop.c debug.o -o fusecloop
338 --- create_compressed_fs.c
339 +++ create_compressed_fs.c
340 @@ -0,0 +1,147 @@
341 +#ifdef FIND_BEST_COMPRESSION
342 +#include <compress.h>
343 +extern "C" {
344 +#include <stdlib.h>
345 +#include <string.h>
346 +
347 +static int best_compress(unsigned char *compressed,
348 + unsigned long *compressed_len,
349 + unsigned char *uncompressed,
350 + unsigned long uncompressed_len)
351 +{
352 + int i, j, err;
353 + unsigned char *buf[2];
354 + unsigned len;
355 + unsigned long llen, best = *compressed_len * 2;
356 + static unsigned char *buffer;
357 + static unsigned long buffersz;
358 +
359 + if (buffersz < *compressed_len) {
360 + if (buffer) free(buffer);
361 + buffer = (unsigned char *) malloc(buffersz = *compressed_len);
362 + if (!buffer) return Z_MEM_ERROR;
363 + }
364 + buf[0] = compressed;
365 + buf[1] = buffer;
366 + for (i = j = 0; i <= 10; i++) {
367 + llen = len = *compressed_len;
368 + if (i == 10)
369 + err = (compress_zlib(shrink_extreme, buf[j],
370 + len, uncompressed,
371 + uncompressed_len)) ? Z_OK : Z_DATA_ERROR;
372 + else {
373 + err = compress2(buf[j], &llen, uncompressed,
374 + uncompressed_len, i);
375 + len = llen;
376 + }
377 + if (err != Z_OK) return err;
378 + if (len < best) {
379 + best = len;
380 + j = 1 - j;
381 + }
382 + }
383 + *compressed_len = best;
384 + if (j == 0)
385 + memcpy(compressed, buffer, best);
386 + return err;
387 +}
388 +#define compress2(a,b,c,d,e) best_compress(a,b,c,d)
389 +#endif
390 +
391 +/* Creates a compressed file */
392 +#include "common_header.h"
393 +
394 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V3.0 Format\n" "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
395 +#define CHUNK 65536
396 +#define DEFAULT_BLOCKSIZE 65536
397 +
398 +static void quit(const char *s)
399 +{
400 + fprintf(stderr, "%s\n", s);
401 + exit(1);
402 +}
403 +
404 +static int readblock(unsigned char *buffer, int n)
405 +{
406 + int i;
407 +
408 + memset(buffer, 0, n);
409 + for (i = 0 ; i < n;) {
410 + int j = read(STDIN_FILENO, buffer + i, n - i);
411 + if (j < 0 && errno == EINTR) continue;
412 + if (j <= 0) break;
413 + i += j;
414 + }
415 + return i;
416 +}
417 +
418 +int main(int argc, char *argv[])
419 +{
420 + struct cloop_head head;
421 + struct cloop_tail tail;
422 + unsigned long block_size = 0;
423 + unsigned char *compressed, *uncompressed;
424 + unsigned long *index;
425 + int n, indexmax, zlenmax;
426 + unsigned long len, pos;
427 + static char padding[512];
428 +
429 + if (argc > 1) {
430 + if (argv[1][0] < '0' || argv[1][0] > '9')
431 + quit("Usage : create_compressed_fs [block size] < input > output");
432 + block_size = atoi(argv[1]);
433 + }
434 + if (block_size < 4096)
435 + block_size = DEFAULT_BLOCKSIZE;
436 + fprintf(stderr, "Block size is %lu\n", block_size);
437 + zlenmax = block_size + block_size/1000 + 12;
438 +
439 + memset(&head, 0, sizeof(head));
440 + strcpy(head.preamble, CLOOP_PREAMBLE);
441 + head.num_blocks = -1;
442 + head.block_size = htonl(block_size);
443 + write(STDOUT_FILENO, &head, sizeof(head));
444 + pos = sizeof(head);
445 +
446 + compressed = (unsigned char *) malloc(zlenmax);
447 + uncompressed = (unsigned char *) malloc(block_size);
448 + index = (unsigned long *) malloc(indexmax = CHUNK);
449 + if (!compressed || !uncompressed || !index)
450 + quit("Malloc failed");
451 +
452 + for (n = 0; readblock(uncompressed, block_size); n++) {
453 + len = zlenmax;
454 + if (compress2(compressed, &len, uncompressed, block_size,
455 + Z_BEST_COMPRESSION) != Z_OK)
456 + quit("Compression failed");
457 + fprintf(stderr, "Block %u length %lu => %lu\n",
458 + n, block_size, len);
459 + write(STDOUT_FILENO, compressed, len);
460 + pos += len;
461 + if (n * sizeof(*index) >= indexmax) {
462 + index = (unsigned long *) realloc(index,
463 + indexmax += CHUNK);
464 + if (!index)
465 + quit("Realloc");
466 + }
467 + index[n] = ntohl(len);
468 + }
469 + tail.index_size = ntohl(sizeof(*index));
470 + tail.num_blocks = ntohl(n);
471 + n *= sizeof(*index);
472 + len = n + n/1000 + 12;
473 + compressed = (unsigned char *) realloc(compressed, n);
474 + if (!compressed || compress2(compressed, &len, (unsigned char *) index,
475 + n, Z_BEST_COMPRESSION) != Z_OK)
476 + quit("Index compression failed");
477 + tail.table_size = ntohl(len);
478 + pos += len + sizeof(tail);
479 + n = pos & 511;
480 + if (n) write(STDOUT_FILENO, padding, 512 - n);
481 + write(STDOUT_FILENO, compressed, len);
482 + write(STDOUT_FILENO, &tail, sizeof(tail));
483 + return 0;
484 +}
485 +#ifdef FIND_BEST_COMPRESSION
486 +}
487 +#endif
489 --- fusecloop.c
490 +++ fusecloop.c
491 @@ -65,7 +65,7 @@
493 memcpy(stbuf,&stb,sizeof stb);
494 stbuf->st_mode&=~0222;
495 - stbuf->st_size = cd.blocksize * cd.numblocks;
496 + stbuf->st_size = (loff_t) cd.blocksize * cd.numblocks;
497 /*
498 stbuf->st_mode = S_IFREG | 0444;
499 stbuf->st_nlink = 1;