wok view fusecloop/stuff/fusecloop.u @ rev 10936

fusecloop: fix index convertion
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Aug 27 17:16:27 2011 +0200 (2011-08-27)
parents 65b7fa14f14f
children f462b780444d
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,76 @@
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 data, offset = ((num_blocks + 1) * sizeof(offset)) + sizeof(head);
201 +
202 + strcpy(head.preamble, CLOOP_PREAMBLE);
203 + write(STDOUT_FILENO, &head, sizeof(head));
204 + for (i = 0; i < num_blocks; i++) {
205 + data = __be64_to_cpu(offset);
206 + write(STDOUT_FILENO, &data, sizeof(data));
207 + offset += offsets[i].size;
208 + }
209 + data = __be64_to_cpu(offset);
210 + write(STDOUT_FILENO, &data, sizeof(data));
211 + for (i = 0; i < num_blocks && lseek(handle, offsets[i].offset, SEEK_SET) >= 0; i++) {
212 + read(handle, buffer, offsets[i].size);
213 + write(STDOUT_FILENO, buffer, offsets[i].size);
214 + }
215 + return 0;
216 + }
217 +
218 + for (i = 0; i < num_blocks; i++) {
219 + unsigned long destlen = block_size;
220 + unsigned int size = offsets[i].size;
222 - read(handle, &offset, 2*sizeof(loff_t));
223 - lseek(handle, -sizeof(loff_t), SEEK_CUR);
224 -
225 - currpos = lseek(handle, 0, SEEK_CUR);
226 - if (lseek(handle, __be64_to_cpu(offset[0]), SEEK_SET) < 0) {
227 + if (lseek(handle, offsets[i].offset, SEEK_SET) < 0) {
228 fprintf(stderr, "lseek to %Lu: %s\n",
229 - __be64_to_cpu(offset[0]), strerror(errno));
230 + offsets[i].offset, strerror(errno));
231 exit(1);
232 }
234 - size=__be64_to_cpu(offset[1])-__be64_to_cpu(offset[0]);
235 - if (size > ntohl(head.block_size) + ntohl(head.block_size)/1000
236 - + 12 + 4) {
237 + if (size > zblock_maxsize) {
238 fprintf(stderr,
239 "Size %u for block %u (offset %Lu) too big\n",
240 - size, i, __be64_to_cpu(offset[0]));
241 + size, i, offsets[i].offset);
242 exit(1);
243 }
244 read(handle, buffer, size);
245 - if (lseek(handle, currpos, SEEK_SET) < 0) {
246 - perror("seeking");
247 - exit(1);
248 - }
250 - fprintf(stderr, "Block %u length %u => %lu\n",
251 - i, size, destlen);
252 + fprintf(stderr, "Block %u at %llu length %u => %lu\n",
253 + i, offsets[i].offset, size, destlen);
254 if (i == 3) {
255 fprintf(stderr,
256 "Block head:%02X%02X%02X%02X%02X%02X%02X%02X\n",
257 @@ -105,12 +140,12 @@
258 fprintf(stderr, "Uncomp: unknown error %u\n", i);
259 exit(1);
260 }
261 - if (destlen != ntohl(head.block_size)) {
262 - fprintf(stderr, "Uncomp: bad len %u (%lu not %u)\n", i,
263 - destlen, ntohl(head.block_size));
264 + if (destlen != block_size) {
265 + fprintf(stderr, "Uncomp: bad len %u (%lu not %lu)\n", i,
266 + destlen, block_size);
267 exit(1);
268 }
269 - write(STDOUT_FILENO, clear_buffer, ntohl(head.block_size));
270 + write(STDOUT_FILENO, clear_buffer, block_size);
271 }
272 return 0;
273 }
275 --- Makefile
276 +++ Makefile
277 @@ -1,16 +1,19 @@
278 PROGNAME=fusecloop
279 ARCFILES=*.c *.h *.pl Makefile configure README VERSION HELP INSTALL typescript *.cloop COPYING
280 -PROGS=fusecloop cloopreaderdemo extract_compressed_fs
281 +PROGS=fusecloop cloopreaderdemo extract_compressed_fs create_compressed_fs
282 FUSECFLAGS=`pkg-config fuse --cflags`
283 FUSELDFLAGS=`pkg-config fuse --libs`
285 CFLAGS= -Wall
287 -all: fusecloop extract_compressed_fs
288 +all: fusecloop extract_compressed_fs create_compressed_fs
290 extract_compressed_fs: extract_compressed_fs.c
291 ${CC} ${CFLAGS} ${LDFLAGS} -lz extract_compressed_fs.c -o extract_compressed_fs
293 +create_compressed_fs: create_compressed_fs.c
294 + ${CC} ${CFLAGS} ${LDFLAGS} -lz create_compressed_fs.c -o create_compressed_fs
295 +
296 fusecloop: fusecloop.c cloopreader.o strver debug.o
297 ${CC} ${CFLAGS} ${LDFLAGS} -lz cloopreader.o ${FUSECFLAGS} ${FUSELDFLAGS} fusecloop.c debug.o -o fusecloop
300 --- create_compressed_fs.c
301 +++ create_compressed_fs.c
302 @@ -0,0 +1,80 @@
303 +/* Creates a compressed file */
304 +#include "common_header.h"
305 +
306 +#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"
307 +#define CHUNK 65536
308 +#define DEFAULT_BLOCKSIZE 65536
309 +
310 +static void quit(char *s)
311 +{
312 + fprintf(stderr, "%s\n", s);
313 + exit(1);
314 +}
315 +
316 +static int readblock(unsigned char *buffer, int n)
317 +{
318 + int i;
319 +
320 + memset(buffer, 0, n);
321 + for (i = 0 ; i < n;) {
322 + int j = read(STDIN_FILENO, buffer + i, n - i);
323 + if (j < 0 && errno == EINTR) continue;
324 + if (j <= 0) break;
325 + i += j;
326 + }
327 + return i;
328 +}
329 +
330 +int main(int argc, char *argv[])
331 +{
332 + struct cloop_head head;
333 + struct cloop_tail tail;
334 + unsigned long block_size = 0;
335 + unsigned char *compressed, *uncompressed;
336 + unsigned long *index;
337 + int n, indexmax, zlenmax;
338 +
339 + if (argc > 1) {
340 + if (argv[1][0] < '0' || argv[1][0] > '9')
341 + quit("Usage : create_compressed_fs [block size] < input > output");
342 + block_size = atoi(argv[1]);
343 + }
344 + if (block_size < 4096)
345 + block_size = DEFAULT_BLOCKSIZE;
346 + fprintf(stderr, "Block size is %lu\n", block_size);
347 + zlenmax = block_size + block_size/1000 + 12;
348 +
349 + memset(&head, 0, sizeof(head));
350 + strcpy(head.preamble, CLOOP_PREAMBLE);
351 + head.num_blocks = -1;
352 + head.block_size = htonl(block_size);
353 + write(STDOUT_FILENO, &head, sizeof(head));
354 +
355 + compressed = malloc(zlenmax);
356 + uncompressed = malloc(block_size);
357 + index = malloc(indexmax = CHUNK);
358 + if (!compressed || !uncompressed || !index)
359 + quit("Malloc failed");
360 +
361 + for (n = 0; readblock(uncompressed, block_size); n++) {
362 + unsigned long len = zlenmax;
363 +
364 + if (compress2(compressed, &len, uncompressed, block_size,
365 + Z_BEST_COMPRESSION) != Z_OK)
366 + quit("Compression failed");
367 + fprintf(stderr, "Block %u length %lu => %lu\n",
368 + n, block_size, len);
369 + write(STDOUT_FILENO, compressed, len);
370 + if (n * sizeof(*index) >= indexmax) {
371 + index = realloc(index, indexmax += CHUNK);
372 + if (!index)
373 + quit("Realloc");
374 + }
375 + index[n] = ntohl(len);
376 + }
377 + write(STDOUT_FILENO, index, n * sizeof(*index));
378 + tail.index_size = ntohl(sizeof(*index));
379 + tail.num_blocks = ntohl(n);
380 + write(STDOUT_FILENO, &tail, sizeof(tail));
381 + return 0;
382 +}