wok-6.x annotate fusecloop/stuff/fusecloop.u @ rev 12392

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