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