~vcs-imports/openbox4/trunk

« back to all changes in this revision

Viewing changes to tools/nb4-mksquashfs/squashfs/mksquashfs.c

  • Committer: psolyca
  • Date: 2013-03-06 17:34:30 UTC
  • Revision ID: svn-v4:3124532d-43cb-4037-9335-164a1c69beb7:trunk:284
Reorganisation du depot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Create a squashfs filesystem.  This is a highly compressed read only filesystem.
3
 
 *
4
 
 * Copyright (c) 2002, 2003, 2004 Phillip Lougher <plougher@users.sourceforge.net>
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the GNU General Public License
8
 
 * as published by the Free Software Foundation; either version 2,
9
 
 * or (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 
 *
20
 
 * mksquashfs.c
21
 
 */
22
 
 
23
 
#define TRUE 1
24
 
#include <pwd.h>
25
 
#include <grp.h>
26
 
#include <time.h>
27
 
#include <unistd.h>
28
 
#include <stdio.h>
29
 
#include <sys/types.h>
30
 
#include <sys/stat.h>
31
 
#include <fcntl.h>
32
 
#include <errno.h>
33
 
#include <dirent.h>
34
 
#include <string.h>
35
 
#include <zlib.h>
36
 
#ifdef __MACOSX__
37
 
  #define __BYTE_ORDER BYTE_ORDER
38
 
  #define __BIG_ENDIAN BIG_ENDIAN
39
 
  #define __LITTLE_ENDIAN LITTLE_ENDIAN
40
 
#else
41
 
  #include <endian.h>
42
 
#endif
43
 
#include <stdlib.h>
44
 
#include <signal.h>
45
 
#include <setjmp.h>
46
 
#include <sys/mman.h>
47
 
 
48
 
#include "mksquashfs.h"
49
 
#include <squashfs_fs.h>
50
 
/* BRCM begin */
51
 
#include <stdarg.h>
52
 
#include "7zapi.h"
53
 
/* BRCM end */
54
 
 
55
 
/* Exit codes used by mkfs-type programs */
56
 
#define MKFS_OK          0      /* No errors */
57
 
#define MKFS_ERROR       8      /* Operational error */
58
 
#define MKFS_USAGE       16     /* Usage or syntax error */
59
 
 
60
 
 
61
 
#ifdef SQUASHFS_TRACE
62
 
#define TRACE(s, args...)               printf("mksquashfs: "s, ## args)
63
 
#else
64
 
#define TRACE(s, args...)
65
 
#endif
66
 
 
67
 
#define INFO(s, args...)                do { if(!silent) printf("mksquashfs: "s, ## args); } while(0)
68
 
#define ERROR(s, args...)               do { fprintf(stderr, s, ## args); } while(0)
69
 
#define EXIT_MKSQUASHFS()               do { if(restore)\
70
 
                                        restorefs();\
71
 
                                        exit(1); } while(0)
72
 
#define BAD_ERROR(s, args...)           do {\
73
 
                                        fprintf(stderr, "FATAL ERROR:" s, ##args);\
74
 
                                        EXIT_MKSQUASHFS();\
75
 
                                        } while(0)
76
 
 
77
 
/* the GNU C library has a wonderful scanf("%as", string) which will
78
 
 allocate the string with the right size, good to avoid buffer overruns. 
79
 
 the following macros use it if available or use a hacky workaround...
80
 
 */
81
 
 
82
 
#ifdef __GNUC__
83
 
#define SCANF_PREFIX "a"
84
 
#define SCANF_STRING(s) (&s)
85
 
#define GETCWD_SIZE 0
86
 
#else
87
 
#define SCANF_PREFIX "511"
88
 
#define SCANF_STRING(s) (s = xmalloc(512))
89
 
#define GETCWD_SIZE -1
90
 
inline int snprintf(char *str, size_t n, const char *fmt, ...)
91
 
{
92
 
        int ret;
93
 
        va_list ap;
94
 
 
95
 
        va_start(ap, fmt);
96
 
        ret = vsprintf(str, fmt, ap);
97
 
        va_end(ap);
98
 
        return ret;
99
 
}
100
 
#endif 
101
 
 
102
 
static FILE *devtable = NULL;
103
 
 
104
 
int duplicate_checking = 1, noF = 0, no_fragments = 0, always_use_fragments = 0;
105
 
int total_compressed = 0, total_uncompressed = 0;
106
 
 
107
 
int fd;
108
 
 
109
 
/* superblock attributes */
110
 
int noI = 0, noD = 0, check_data = 0, block_size = SQUASHFS_FILE_SIZE, block_log;
111
 
unsigned short uid_count = 0, guid_count = 0;
112
 
squashfs_uid uids[SQUASHFS_UIDS], guids[SQUASHFS_GUIDS];
113
 
int block_offset;
114
 
 
115
 
/* write position within data section */
116
 
unsigned int bytes = 0, total_bytes = 0;
117
 
 
118
 
/* in memory directory table - possibly compressed */
119
 
char *directory_table = NULL;
120
 
unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0;
121
 
 
122
 
/* cached directory table */
123
 
char *directory_data_cache = NULL;
124
 
unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
125
 
 
126
 
/* in memory inode table - possibly compressed */
127
 
char *inode_table = NULL;
128
 
unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0;
129
 
 
130
 
/* cached inode table */
131
 
char *data_cache = NULL;
132
 
unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
133
 
 
134
 
/* in memory directory header */
135
 
struct directory {
136
 
        unsigned int            start_block;
137
 
        unsigned int            size;
138
 
        unsigned char           *buff;
139
 
        unsigned char           *p;
140
 
        unsigned int            entry_count;
141
 
        squashfs_dir_header     *entry_count_p;
142
 
};
143
 
 
144
 
struct file_info *dupl[65536], *frag_dups[65536];
145
 
int dup_files = 0;
146
 
 
147
 
int swap, silent = TRUE;
148
 
int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0, sock_count = 0;
149
 
 
150
 
/* list of exclude dirs/files */
151
 
struct exclude_info {
152
 
        dev_t                   st_dev;
153
 
        ino_t                   st_ino;
154
 
};
155
 
 
156
 
#define EXCLUDE_SIZE 8192
157
 
int exclude = 0;
158
 
struct exclude_info *exclude_paths = NULL;
159
 
int excluded(char *filename, struct stat *buf);
160
 
 
161
 
/* fragment block data structures */
162
 
int fragments = 0;
163
 
static char *fragment_data;
164
 
static int fragment_size = 0;
165
 
struct fragment {
166
 
        unsigned int            index;
167
 
        int                     offset;
168
 
        int                     size;
169
 
};
170
 
 
171
 
 
172
 
#define FRAG_SIZE 32768
173
 
squashfs_fragment_entry *fragment_table = NULL;
174
 
 
175
 
/* list of source dirs/files */
176
 
int source = 0;
177
 
char **source_path;
178
 
char dev_path[1024];
179
 
 
180
 
/* list of root directory entries read from original filesystem */
181
 
int old_root_entries = 0;
182
 
struct old_root_entry_info {
183
 
        char                    name[SQUASHFS_NAME_LEN + 1];
184
 
        squashfs_inode          inode;
185
 
        int                     type;
186
 
};
187
 
 
188
 
/* in memory file info */
189
 
struct file_info {
190
 
        unsigned int            bytes;
191
 
        unsigned short          checksum;
192
 
        unsigned int            start;
193
 
        unsigned int            *block_list;
194
 
        struct file_info        *next;
195
 
        struct fragment         *fragment;
196
 
        unsigned short          fragment_checksum;
197
 
};
198
 
 
199
 
/* count of how many times SIGINT or SIGQUIT has been sent */
200
 
int interrupted = 0;
201
 
 
202
 
/* restore orignal filesystem state if appending to existing filesystem is cancelled */
203
 
jmp_buf env;
204
 
char *sdata_cache, *sdirectory_data_cache;
205
 
unsigned int sbytes, sinode_bytes, scache_bytes, sdirectory_bytes,
206
 
        sdirectory_cache_bytes, suid_count, sguid_count,
207
 
        stotal_bytes, stotal_inode_bytes, stotal_directory_bytes,
208
 
        sinode_count, sfile_count, ssym_count, sdev_count,
209
 
        sdir_count, sdup_files;
210
 
int sfragments;
211
 
int restore = 0;
212
 
 
213
 
/*flag whether destination file is a block device */
214
 
int block_device = 0;
215
 
 
216
 
/* flag indicating whether files are sorted using sort list(s) */
217
 
int sorted = 0;
218
 
 
219
 
long long global_uid = -1, global_gid = -1;
220
 
 
221
 
/* structure to used to pass in a pointer or an integer
222
 
 * to duplicate buffer read helper functions.
223
 
 */
224
 
struct duplicate_buffer_handle {
225
 
        unsigned char   *ptr;
226
 
        unsigned int    start;
227
 
};
228
 
 
229
 
struct old_root_entry_info *old_root_entry;
230
 
void add_old_root_entry(char *name, squashfs_inode inode, int type);
231
 
extern int read_super(int fd, squashfs_super_block *sBlk, int *be, char *source);
232
 
extern int read_filesystem(char *root_name, int fd, squashfs_super_block *sBlk, char **inode_table, int *inode_bytes,
233
 
                char **data_cache, int *cache_bytes, int *cache_size, char **directory_table, int *directory_bytes,
234
 
                char **directory_data_cache, int *directory_cache_bytes, int *directory_cache_size,
235
 
                int *file_count, int *sym_count, int *dev_count, int *dir_count, int *fifo_count, int *sock_count,
236
 
                squashfs_uid *uids, unsigned short *uid_count, squashfs_uid *guids, unsigned short *guid_count,
237
 
                unsigned int *uncompressed_file, unsigned int *uncompressed_inode, unsigned int *uncompressed_directory,
238
 
                void (push_directory_entry)(char *, squashfs_inode, int), squashfs_fragment_entry **fragment_table);
239
 
squashfs_inode get_sorted_inode(struct stat *buf);
240
 
int read_sort_file(char *filename, int source, char *source_path[]);
241
 
void sort_files_and_write(int source, char *source_path[]);
242
 
struct file_info *duplicate(unsigned char *(get_next_file_block)(struct duplicate_buffer_handle *, unsigned int), struct duplicate_buffer_handle *file_start, int bytes, unsigned int **block_list, int *start, int blocks, struct fragment **fragment, char *frag_data, int frag_bytes);
243
 
 
244
 
#define FALSE 0
245
 
 
246
 
#define MKINODE(A)      ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) + (((char *)A) - data_cache)))
247
 
 
248
 
/* BRCM begin */
249
 
#define LZMA    0
250
 
#define GZIP    1
251
 
int compress_algorithm = LZMA; 
252
 
/* BRCM end */
253
 
 
254
 
static void verror_msg(const char *s, va_list p)
255
 
{
256
 
        fflush(stdout);
257
 
        fprintf(stderr, "mkcramfs: ");
258
 
        vfprintf(stderr, s, p);                         
259
 
}
260
 
 
261
 
static void vperror_msg(const char *s, va_list p)
262
 
{
263
 
        int err = errno;
264
 
 
265
 
        if (s == 0)
266
 
                s = "";
267
 
        verror_msg(s, p);
268
 
        if (*s)
269
 
                s = ": ";
270
 
        fprintf(stderr, "%s%s\n", s, strerror(err));
271
 
}
272
 
 
273
 
static void perror_msg(const char *s, ...)
274
 
{
275
 
        va_list p;
276
 
 
277
 
        va_start(p, s);
278
 
        vperror_msg(s, p);
279
 
        va_end(p);
280
 
}
281
 
 
282
 
static void error_msg_and_die(const char *s, ...)
283
 
{
284
 
        va_list p;
285
 
 
286
 
        va_start(p, s);
287
 
        verror_msg(s, p);
288
 
        va_end(p);
289
 
        putc('\n', stderr);
290
 
        exit(MKFS_ERROR);
291
 
}
292
 
 
293
 
static void perror_msg_and_die(const char *s, ...)
294
 
{
295
 
        va_list p;
296
 
 
297
 
        va_start(p, s);
298
 
        vperror_msg(s, p);
299
 
        va_end(p);
300
 
        exit(MKFS_ERROR);
301
 
}
302
 
 
303
 
static FILE *xfopen(const char *path, const char *mode)
304
 
{
305
 
        FILE *fp;
306
 
 
307
 
        if ((fp = fopen(path, mode)) == NULL)
308
 
                perror_msg_and_die("%s", path);
309
 
        return fp;
310
 
}
311
 
 
312
 
void restorefs()
313
 
{
314
 
        ERROR("Exiting - restoring original filesystem!\n\n");
315
 
        bytes = sbytes;
316
 
        memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes);
317
 
        memcpy(directory_data_cache, sdirectory_data_cache, directory_cache_bytes = sdirectory_cache_bytes);
318
 
        inode_bytes = sinode_bytes;
319
 
        directory_bytes = sdirectory_bytes;
320
 
        uid_count = suid_count;
321
 
        guid_count = sguid_count;
322
 
        total_bytes = stotal_bytes;
323
 
        total_inode_bytes = stotal_inode_bytes;
324
 
        total_directory_bytes = stotal_directory_bytes;
325
 
        inode_count = sinode_count;
326
 
        file_count = sfile_count;
327
 
        sym_count = ssym_count;
328
 
        dev_count = sdev_count;
329
 
        dir_count = sdir_count;
330
 
        dup_files = sdup_files;
331
 
        fragments = sfragments;
332
 
        fragment_size = 0;
333
 
        longjmp(env, 1);
334
 
}
335
 
 
336
 
 
337
 
void sighandler()
338
 
{
339
 
        if(interrupted == 1)
340
 
                restorefs();
341
 
        else {
342
 
                ERROR("Interrupting will restore original filesystem!\n");
343
 
                ERROR("Interrupt again to quit\n");
344
 
                interrupted ++;
345
 
        }
346
 
}
347
 
 
348
 
 
349
 
unsigned int mangle(char *d, char *s, int size, int block_size, int uncompressed, int data_block)
350
 
{
351
 
        unsigned long c_byte;
352
 
        unsigned int res;
353
 
 
354
 
        /* BRCM begin */
355
 
        if (compress_algorithm == GZIP) {
356
 
                c_byte = block_size << 1;
357
 
                if(!uncompressed && (res = compress2(d, &c_byte, s, size, 9)) != Z_OK) {
358
 
                        if(res == Z_MEM_ERROR)
359
 
                                BAD_ERROR("zlib::compress failed, not enough memory\n");
360
 
                        else if(res == Z_BUF_ERROR)
361
 
                                BAD_ERROR("zlib::compress failed, not enough room in output buffer\n");
362
 
                        else
363
 
                                BAD_ERROR("zlib::compress failed, unknown error %d\n", res);
364
 
                        return 0;
365
 
                }                       
366
 
        }
367
 
 
368
 
        if (compress_algorithm == LZMA) {
369
 
                unsigned lzma_algo;
370
 
                unsigned lzma_dictsize;
371
 
                unsigned lzma_fastbytes;
372
 
                int opt_compression_level = 1;
373
 
 
374
 
                c_byte = block_size << 3;
375
 
                switch (opt_compression_level) {
376
 
                case 1 :
377
 
                        lzma_algo = 1;
378
 
                        lzma_dictsize = 1 << 20;
379
 
                        lzma_fastbytes = 64;
380
 
                        break;
381
 
                case 2 :
382
 
                        lzma_algo = 2;
383
 
                        lzma_dictsize = 1 << 22;
384
 
                        lzma_fastbytes = 128;
385
 
                        break;
386
 
                case 3 :
387
 
                        lzma_algo = 2;
388
 
                        lzma_dictsize = 1 << 24;
389
 
                        lzma_fastbytes = 255;
390
 
                        break;
391
 
                default :
392
 
                        BAD_ERROR("Invalid LZMA compression level. Must be 1,2,3.");
393
 
                        return 0;
394
 
                }
395
 
                if(!uncompressed && !(res = compress_lzma_7zapi((const unsigned char*)s, 
396
 
                                                                (unsigned)size, 
397
 
                                                                (unsigned char*)d,
398
 
                                                                (unsigned *)&c_byte,
399
 
                                                                lzma_algo, lzma_dictsize, lzma_fastbytes))) {
400
 
                        /* this should NEVER happen */
401
 
                        BAD_ERROR("Internal error - LZMA compression failed.\n");
402
 
                        return 0;
403
 
                }
404
 
                //printf("LZMA: block_size=%d, in_size=%d, out_size=%d\n", block_size, size, c_byte);
405
 
        }
406
 
        /* BRCM end */
407
 
        
408
 
        if(uncompressed || c_byte >= size) {
409
 
                memcpy(d, s, size);
410
 
                return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : SQUASHFS_COMPRESSED_BIT);
411
 
        }
412
 
 
413
 
        return (unsigned int) c_byte;
414
 
}
415
 
 
416
 
 
417
 
squashfs_base_inode_header *get_inode(int req_size)
418
 
{
419
 
        int data_space;
420
 
        unsigned short c_byte;
421
 
 
422
 
        while(cache_bytes >= SQUASHFS_METADATA_SIZE) {
423
 
                if((inode_size - inode_bytes) < ((SQUASHFS_METADATA_SIZE << 1)) + 2) {
424
 
                        if((inode_table = (char *) realloc(inode_table, inode_size + (SQUASHFS_METADATA_SIZE << 1) + 2))
425
 
                                        == NULL) {
426
 
                                goto failed;
427
 
                        }
428
 
                        inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
429
 
                }
430
 
 
431
 
                c_byte = mangle(inode_table + inode_bytes + block_offset, data_cache,
432
 
                                                                SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, noI, 0);
433
 
                TRACE("Inode block @ %x, size %d\n", inode_bytes, c_byte);
434
 
                if(!swap)
435
 
                        memcpy((void *) (inode_table + inode_bytes), (void *) &c_byte, sizeof(unsigned short));
436
 
                else
437
 
                        SQUASHFS_SWAP_SHORTS((&c_byte), (inode_table + inode_bytes), 1);
438
 
                if(check_data)
439
 
                        *((unsigned char *)(inode_table + inode_bytes + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
440
 
                inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
441
 
                total_inode_bytes += SQUASHFS_METADATA_SIZE + block_offset;
442
 
                memcpy(data_cache, data_cache + SQUASHFS_METADATA_SIZE, cache_bytes - SQUASHFS_METADATA_SIZE);
443
 
                cache_bytes -= SQUASHFS_METADATA_SIZE;
444
 
        }
445
 
 
446
 
        data_space = (cache_size - cache_bytes);
447
 
        if(data_space < req_size) {
448
 
                        int realloc_size = cache_size == 0 ? ((req_size + SQUASHFS_METADATA_SIZE) & ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - data_space;
449
 
 
450
 
                        if((data_cache = (char *) realloc(data_cache, cache_size + realloc_size)) == NULL) {
451
 
                                goto failed;
452
 
                        }
453
 
                        cache_size += realloc_size;
454
 
        }
455
 
 
456
 
        cache_bytes += req_size;
457
 
 
458
 
        return (squashfs_base_inode_header *)(data_cache + (cache_bytes - req_size));
459
 
 
460
 
failed:
461
 
        BAD_ERROR("Out of memory in inode table reallocation!\n");
462
 
}
463
 
 
464
 
 
465
 
void read_bytes(int fd, unsigned int byte, int bytes, char *buff)
466
 
{
467
 
        off_t off = byte;
468
 
 
469
 
        if(lseek(fd, off, SEEK_SET) == -1) {
470
 
                perror("Lseek on destination failed");
471
 
                EXIT_MKSQUASHFS();
472
 
        }
473
 
 
474
 
        if(read(fd, buff, bytes) == -1) {
475
 
                perror("Read on destination failed");
476
 
                EXIT_MKSQUASHFS();
477
 
        }
478
 
}
479
 
 
480
 
 
481
 
void write_bytes(int fd, unsigned int byte, int bytes, char *buff)
482
 
{
483
 
        off_t off = byte;
484
 
 
485
 
        if(off + bytes > ((long long)1<<32) - 1 )
486
 
                BAD_ERROR("Filesystem greater than maximum size 2^32 - 1\n");
487
 
 
488
 
        if(lseek(fd, off, SEEK_SET) == -1) {
489
 
                perror("Lseek on destination failed");
490
 
                EXIT_MKSQUASHFS();
491
 
        }
492
 
 
493
 
        if(write(fd, buff, bytes) == -1) {
494
 
                perror("Write on destination failed");
495
 
                EXIT_MKSQUASHFS();
496
 
        }
497
 
}
498
 
 
499
 
 
500
 
unsigned int write_inodes()
501
 
{
502
 
        unsigned short c_byte;
503
 
        int avail_bytes;
504
 
        char *datap = data_cache;
505
 
        unsigned int start_bytes = bytes;
506
 
 
507
 
        while(cache_bytes) {
508
 
                if(inode_size - inode_bytes < ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
509
 
                        if((inode_table = (char *) realloc(inode_table, inode_size + ((SQUASHFS_METADATA_SIZE << 1) + 2))) == NULL) {
510
 
                                BAD_ERROR("Out of memory in inode table reallocation!\n");
511
 
                        }
512
 
                        inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
513
 
                }
514
 
                avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? SQUASHFS_METADATA_SIZE : cache_bytes;
515
 
                c_byte = mangle(inode_table + inode_bytes + block_offset, datap, avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0);
516
 
                TRACE("Inode block @ %x, size %d\n", inode_bytes, c_byte);
517
 
                if(!swap)
518
 
                        memcpy((void *) (inode_table + inode_bytes), (void *) &c_byte, sizeof(unsigned short));
519
 
                else
520
 
                        SQUASHFS_SWAP_SHORTS((&c_byte), (inode_table + inode_bytes), 1); 
521
 
                if(check_data)
522
 
                        *((unsigned char *)(inode_table + inode_bytes + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
523
 
                inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
524
 
                total_inode_bytes += avail_bytes + block_offset;
525
 
                datap += avail_bytes;
526
 
                cache_bytes -= avail_bytes;
527
 
        }
528
 
 
529
 
        write_bytes(fd, bytes, inode_bytes, (char *) inode_table);
530
 
        bytes += inode_bytes;
531
 
 
532
 
        return start_bytes;
533
 
}
534
 
 
535
 
 
536
 
unsigned int write_directories()
537
 
{
538
 
        unsigned short c_byte;
539
 
        int avail_bytes;
540
 
        char *directoryp = directory_data_cache;
541
 
        unsigned int start_bytes = bytes;
542
 
 
543
 
        while(directory_cache_bytes) {
544
 
                if(directory_size - directory_bytes < ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
545
 
                        if((directory_table = (char *) realloc(directory_table, directory_size +
546
 
                                        ((SQUASHFS_METADATA_SIZE << 1) + 2))) == NULL) {
547
 
                                BAD_ERROR("Out of memory in directory table reallocation!\n");
548
 
                        }
549
 
                        directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
550
 
                }
551
 
                avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? SQUASHFS_METADATA_SIZE : directory_cache_bytes;
552
 
                c_byte = mangle(directory_table + directory_bytes + block_offset, directoryp, avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0);
553
 
                TRACE("Directory block @ %x, size %d\n", directory_bytes, c_byte);
554
 
                if(!swap)
555
 
                        memcpy((void *) (directory_table + directory_bytes), (void *) &c_byte, sizeof(unsigned short));
556
 
                else
557
 
                        SQUASHFS_SWAP_SHORTS((&c_byte), (directory_table + directory_bytes), 1);
558
 
                if(check_data)
559
 
                        *((unsigned char *)(directory_table + directory_bytes + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
560
 
                directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
561
 
                total_directory_bytes += avail_bytes + block_offset;
562
 
                directoryp += avail_bytes;
563
 
                directory_cache_bytes -= avail_bytes;
564
 
        }
565
 
        write_bytes(fd, bytes, directory_bytes, (char *) directory_table);
566
 
        bytes += directory_bytes;
567
 
 
568
 
        return start_bytes;
569
 
}
570
 
 
571
 
 
572
 
unsigned int get_uid(squashfs_uid uid)
573
 
{
574
 
        int i;
575
 
 
576
 
        for(i = 0; (i < uid_count) && uids[i] != uid; i++);
577
 
        if(i == uid_count) {
578
 
                if(uid_count == SQUASHFS_UIDS) {
579
 
                        ERROR("Out of uids! - using uid 0 - probably not what's wanted!\n");
580
 
                        i = 0;
581
 
                } else
582
 
                        uids[uid_count++] = uid;
583
 
        }
584
 
 
585
 
        return i;
586
 
}
587
 
 
588
 
 
589
 
unsigned int get_guid(squashfs_uid uid, squashfs_uid guid)
590
 
{
591
 
        int i;
592
 
 
593
 
        if(uid == guid)
594
 
                return SQUASHFS_GUIDS;
595
 
 
596
 
        for(i = 0; (i < guid_count) && guids[i] != guid; i++);
597
 
        if(i == guid_count) {
598
 
                if(guid_count == SQUASHFS_GUIDS) {
599
 
                        ERROR("Out of gids! - using gid 0 - probably not what's wanted!\n");
600
 
                        return SQUASHFS_GUIDS;
601
 
                } else
602
 
                        guids[guid_count++] = guid;
603
 
        }
604
 
 
605
 
        return i;
606
 
}
607
 
 
608
 
 
609
 
squashfs_inode create_inode(char *filename, int type, int byte_size,
610
 
squashfs_block start_block, unsigned int offset, unsigned int *block_list, struct fragment *fragment, struct stat* devtable_inode_info)
611
 
{
612
 
        squashfs_inode i_no;
613
 
        struct stat buf;
614
 
        squashfs_inode_header inode_header;
615
 
        squashfs_base_inode_header *inode, *base = &inode_header.base;
616
 
 
617
 
        if(filename[0] == '\0') {
618
 
                /* dummy top level directory, if multiple sources specified on command line */
619
 
                buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO;
620
 
                buf.st_uid = getuid();
621
 
                buf.st_gid = getgid();
622
 
                buf.st_mtime = time(NULL);
623
 
        } else 
624
 
   /* We set st_mode different than 0 for all files in the device table list.
625
 
   ** That is how we "recognize" that the file is from the device table list.
626
 
   ** For files not in the table list st_mode is initially set to 0 and 
627
 
   ** then reinitialized using lstat */
628
 
   if ( devtable_inode_info != NULL && devtable_inode_info->st_mode != 0 ) {
629
 
      buf.st_mode = devtable_inode_info->st_mode;
630
 
      buf.st_rdev = devtable_inode_info->st_rdev;
631
 
      buf.st_uid = devtable_inode_info->st_uid;
632
 
      buf.st_gid = devtable_inode_info->st_gid;
633
 
      buf.st_mtime = time(NULL);;
634
 
        } else if(lstat(filename, &buf) == -1) {
635
 
                char buffer[8192];
636
 
                sprintf(buffer, "Cannot stat dir/file %s, ignoring", filename);
637
 
                perror(buffer);
638
 
                return SQUASHFS_INVALID;
639
 
        }
640
 
 
641
 
        base->mode = SQUASHFS_MODE(buf.st_mode);
642
 
        base->uid = get_uid((squashfs_uid) global_uid == -1 ? buf.st_uid : global_uid);
643
 
        base->inode_type = type;
644
 
        base->guid = get_guid((squashfs_uid) global_uid == -1 ? buf.st_uid : global_uid, (squashfs_uid) global_gid == -1 ? buf.st_gid : global_gid);
645
 
 
646
 
        if(type == SQUASHFS_FILE_TYPE) {
647
 
                int i;
648
 
                squashfs_reg_inode_header *reg = &inode_header.reg, *inodep;
649
 
 
650
 
/* CONFIG_MIPS_BRCM Begin Broadcom changed code. */
651
 
/* GCC4.0 does not like two assigns and a cast in one line.  Split them up. */
652
 
                inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
653
 
                inodep = (squashfs_reg_inode_header *) inode;
654
 
/* CONFIG_MIPS_BRCM end Broadcom changed code. */
655
 
 
656
 
                reg->mtime = buf.st_mtime;
657
 
                reg->file_size = byte_size;
658
 
                reg->start_block = start_block;
659
 
                reg->fragment = fragment->index;
660
 
                reg->offset = fragment->offset;
661
 
                if(!swap) {
662
 
                        memcpy((void *) inodep, (void *) reg, sizeof(*reg));
663
 
                        memcpy((void *) inodep->block_list, block_list, offset * sizeof(unsigned int));
664
 
                } else {
665
 
                        SQUASHFS_SWAP_REG_INODE_HEADER(reg, inodep);
666
 
                        SQUASHFS_SWAP_INTS(block_list, inodep->block_list, offset);
667
 
                }
668
 
                TRACE("File inode, file_size %d, start_block %x, blocks %d, fragment %d, offset %d, size %d\n", byte_size,
669
 
                        start_block, offset, fragment->index, fragment->offset, fragment->size);
670
 
                for(i = 0; i < offset; i++)
671
 
                        TRACE("Block %d, size %d\n", i, block_list[i]);
672
 
        }
673
 
        else if(type == SQUASHFS_DIR_TYPE) {
674
 
                squashfs_dir_inode_header *dir = &inode_header.dir;
675
 
 
676
 
                inode = get_inode(sizeof(*dir));
677
 
                dir->mtime = buf.st_mtime;
678
 
                dir->file_size = byte_size;
679
 
                dir->offset = offset;
680
 
                dir->start_block = start_block;
681
 
                if(!swap)
682
 
                        memcpy((void *) inode, (void *) dir, sizeof(*dir));
683
 
                else
684
 
                        SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode);
685
 
                TRACE("Directory inode, file_size %d, start_block %x, offset %x\n", byte_size,
686
 
                        start_block, offset);
687
 
        }
688
 
        else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) {
689
 
                squashfs_dev_inode_header *dev = &inode_header.dev;
690
 
 
691
 
                inode = get_inode(sizeof(*dev));
692
 
                dev->rdev = (unsigned short) ((major(buf.st_rdev) << 8) |
693
 
                        (minor(buf.st_rdev) & 0xff));
694
 
                if(!swap)
695
 
                        memcpy((void *) inode, (void *) dev, sizeof(*dev));
696
 
                else
697
 
                        SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode);
698
 
                TRACE("Device inode, rdev %x\n", dev->rdev);
699
 
        }
700
 
        else if(type == SQUASHFS_SYMLINK_TYPE) {
701
 
                squashfs_symlink_inode_header *symlink = &inode_header.symlink, *inodep;
702
 
                int byte;
703
 
                char buff[65536];
704
 
 
705
 
                if((byte = readlink(filename, buff, 65536)) == -1) {
706
 
                        perror("Error in reading symbolic link, skipping...");
707
 
                        return SQUASHFS_INVALID;
708
 
                }
709
 
 
710
 
                if(byte == 65536) {
711
 
                        ERROR("Symlink is greater than 65536 bytes! skipping...");
712
 
                        return SQUASHFS_INVALID;
713
 
                }
714
 
 
715
 
/* CONFIG_MIPS_BRCM Begin Broadcom changed code. */
716
 
/* GCC4.0 does not like two assigns and a cast in one line.  Split them up. */
717
 
                inode = get_inode(sizeof(*symlink) + byte);
718
 
                inodep = (squashfs_symlink_inode_header *) inode;
719
 
/* CONFIG_MIPS_BRCM end Broadcom changed code. */
720
 
 
721
 
                symlink->symlink_size = byte;
722
 
                if(!swap)
723
 
                        memcpy((void *) inode, symlink, sizeof(*symlink));
724
 
                else
725
 
                        SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
726
 
                strncpy(inodep->symlink, buff, byte);
727
 
                TRACE("Symbolic link inode, symlink_size %d\n",  byte);
728
 
        }
729
 
        else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) {
730
 
                squashfs_ipc_inode_header *ipc = &inode_header.ipc;
731
 
 
732
 
                inode = get_inode(sizeof(*ipc));
733
 
                if(!swap)
734
 
                        memcpy((void *) inode, (void *) ipc, sizeof(*ipc));
735
 
                else
736
 
                        SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode);
737
 
                TRACE("ipc inode, type %s %d\n", type == SQUASHFS_FIFO_TYPE ? "fifo" : "socket");
738
 
        } else
739
 
                return SQUASHFS_INVALID;
740
 
 
741
 
        i_no = MKINODE(inode);
742
 
        inode_count ++;
743
 
 
744
 
        TRACE("Created inode 0x%Lx, type %d, uid %d, guid %d\n", i_no, type, base->uid, base->guid);
745
 
 
746
 
        return i_no;
747
 
}
748
 
 
749
 
 
750
 
void init_dir(struct directory *dir)
751
 
{
752
 
        if((dir->buff = (char *)malloc(SQUASHFS_METADATA_SIZE)) == NULL) {
753
 
                BAD_ERROR("Out of memory allocating directory buffer\n");
754
 
        }
755
 
 
756
 
        dir->size = SQUASHFS_METADATA_SIZE;
757
 
        dir->p = dir->buff;
758
 
        dir->entry_count = 256;
759
 
        dir->entry_count_p = NULL;
760
 
}
761
 
 
762
 
 
763
 
void add_dir(squashfs_inode inode, char *name, int type, struct directory *dir)
764
 
{
765
 
        char *buff;
766
 
        squashfs_dir_entry idir, *idirp;
767
 
        unsigned int start_block = inode >> 16;
768
 
        unsigned int offset = inode & 0xffff;
769
 
        unsigned int size;
770
 
 
771
 
        if((size = strlen(name)) > SQUASHFS_NAME_LEN) {
772
 
                size = SQUASHFS_NAME_LEN;
773
 
                ERROR("Filename is greater than %d characters, truncating! ...\n", SQUASHFS_NAME_LEN);
774
 
        }
775
 
 
776
 
        if(dir->p + sizeof(squashfs_dir_entry) + size + 6 >= dir->buff + dir->size) {
777
 
                if((buff = (char *) realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE)) == NULL)  {
778
 
                        BAD_ERROR("Out of memory reallocating directory buffer\n");
779
 
                }
780
 
 
781
 
                dir->p = (dir->p - dir->buff) + buff;
782
 
                if(dir->entry_count_p) 
783
 
                        dir->entry_count_p = (squashfs_dir_header *) (((unsigned char *) dir->entry_count_p) -
784
 
                                dir->buff + buff);
785
 
                dir->buff = buff;
786
 
        }
787
 
 
788
 
        if(dir->entry_count == 256 || start_block != dir->start_block) {
789
 
                if(dir->entry_count_p) {
790
 
                        squashfs_dir_header dir_header;
791
 
 
792
 
                        dir_header.count = dir->entry_count - 1;
793
 
                        dir_header.start_block = dir->start_block;
794
 
                        if(!swap)
795
 
                                memcpy((void *) dir->entry_count_p, (void *) &dir_header, sizeof(dir_header));
796
 
                        else
797
 
                                SQUASHFS_SWAP_DIR_HEADER((&dir_header), dir->entry_count_p);
798
 
                }
799
 
 
800
 
                dir->entry_count_p = (squashfs_dir_header *) dir->p;
801
 
                dir->start_block = start_block;
802
 
                dir->entry_count = 0;
803
 
                dir->p += sizeof(squashfs_dir_header);
804
 
        }
805
 
 
806
 
        idirp = (squashfs_dir_entry *) dir->p;
807
 
        idir.offset = offset;
808
 
        idir.type = type;
809
 
        idir.size = size - 1;
810
 
        if(!swap)
811
 
                memcpy((void *) idirp, (void *) &idir, sizeof(idir));
812
 
        else
813
 
                SQUASHFS_SWAP_DIR_ENTRY((&idir), idirp);
814
 
        strncpy(idirp->name, name, size);
815
 
        dir->p += sizeof(squashfs_dir_entry) + size;
816
 
        dir->entry_count ++;
817
 
}
818
 
 
819
 
 
820
 
squashfs_inode write_dir(char *filename, struct directory *dir)
821
 
{
822
 
        squashfs_inode inode;
823
 
        unsigned int dir_size;
824
 
        int data_space;
825
 
        unsigned short c_byte;
826
 
 
827
 
        while(directory_cache_bytes >= SQUASHFS_METADATA_SIZE) {
828
 
                if((directory_size - directory_bytes) < ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
829
 
                        if((directory_table = (char *) realloc(directory_table,
830
 
                                                        directory_size + (SQUASHFS_METADATA_SIZE << 1) + 2)) == NULL) {
831
 
                                goto failed;
832
 
                        }
833
 
                        directory_size += SQUASHFS_METADATA_SIZE << 1;
834
 
                }
835
 
 
836
 
                c_byte = mangle(directory_table + directory_bytes + block_offset, directory_data_cache,
837
 
                                SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, noI, 0);
838
 
                TRACE("Directory block @ %x, size %d\n", directory_bytes, c_byte);
839
 
                if(!swap)
840
 
                        memcpy((void *) directory_table + directory_bytes, (void *) &c_byte, sizeof(unsigned short));
841
 
                else
842
 
                        SQUASHFS_SWAP_SHORTS((&c_byte), (directory_table + directory_bytes), 1);
843
 
                if(check_data)
844
 
                        *((unsigned char *)(directory_table + directory_bytes + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
845
 
                directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
846
 
                total_directory_bytes += SQUASHFS_METADATA_SIZE + block_offset;
847
 
                memcpy(directory_data_cache, directory_data_cache + SQUASHFS_METADATA_SIZE, directory_cache_bytes - SQUASHFS_METADATA_SIZE);
848
 
                directory_cache_bytes -= SQUASHFS_METADATA_SIZE;
849
 
        }
850
 
 
851
 
        dir_size = dir->p - dir->buff;
852
 
        data_space = (directory_cache_size - directory_cache_bytes);
853
 
        if(data_space < dir_size) {
854
 
                int realloc_size = directory_cache_size == 0 ? ((dir_size + SQUASHFS_METADATA_SIZE) & ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space;
855
 
 
856
 
                if((directory_data_cache = (char *) realloc(directory_data_cache, directory_cache_size + realloc_size)) == NULL) {
857
 
                        goto failed;
858
 
                }
859
 
                directory_cache_size += realloc_size;
860
 
        }
861
 
 
862
 
        if(dir_size) {
863
 
                squashfs_dir_header dir_header;
864
 
 
865
 
                dir_header.count = dir->entry_count - 1;
866
 
                dir_header.start_block = dir->start_block;
867
 
                if(!swap)
868
 
                        memcpy((void *) dir->entry_count_p, (void *) &dir_header, sizeof(dir_header));
869
 
                else
870
 
                        SQUASHFS_SWAP_DIR_HEADER((&dir_header), dir->entry_count_p);
871
 
                memcpy(directory_data_cache + directory_cache_bytes, dir->buff, dir_size);
872
 
        }
873
 
 
874
 
        inode = create_inode(filename, SQUASHFS_DIR_TYPE, dir_size, directory_bytes, directory_cache_bytes, NULL, NULL, NULL);
875
 
 
876
 
        directory_cache_bytes += dir_size;
877
 
 
878
 
#ifdef SQUASHFS_TRACE
879
 
        if(!swap) {
880
 
                unsigned char *dirp;
881
 
                int count;
882
 
 
883
 
                TRACE("Directory contents of inode 0x%Lx\n", inode);
884
 
                dirp = dir->buff;
885
 
                while(dirp < dir->p) {
886
 
                        char buffer[SQUASHFS_NAME_LEN + 1];
887
 
                        squashfs_dir_entry idir, *idirp;
888
 
                        squashfs_dir_header *dirh = (squashfs_dir_header *) dirp;
889
 
                        count = dirh->count + 1;
890
 
                        dirp += sizeof(squashfs_dir_header);
891
 
 
892
 
                        TRACE("\tStart block 0x%x, count %d\n", dirh->start_block, count);
893
 
 
894
 
                        while(count--) {
895
 
                                idirp = (squashfs_dir_entry *) dirp;
896
 
                                memcpy((char *) &idir, (char *) idirp, sizeof(idir));
897
 
                                strncpy(buffer, idirp->name, idir.size + 1);
898
 
                                buffer[idir.size + 1] = '\0';
899
 
                                TRACE("\t\tname %s, inode offset 0x%x, type %d\n", buffer,
900
 
                                                  idir.offset, idir.type);
901
 
                                dirp += sizeof(squashfs_dir_entry) + idir.size + 1;
902
 
                        }
903
 
                }
904
 
        }
905
 
#endif
906
 
        dir_count ++;
907
 
 
908
 
        return inode;
909
 
 
910
 
failed:
911
 
        BAD_ERROR("Out of memory in directory table reallocation!\n");
912
 
}
913
 
 
914
 
 
915
 
char *get_fragment(char *buffer, struct fragment *fragment)
916
 
{
917
 
        if(fragment->index == fragments || fragment->index == SQUASHFS_INVALID_BLK)
918
 
                return fragment_data + fragment->offset;
919
 
        else {
920
 
                squashfs_fragment_entry *disk_fragment = &fragment_table[fragment->index];
921
 
                int size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size), res;
922
 
                long bytes = block_size;
923
 
 
924
 
                if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) {
925
 
                        char cbuffer[block_size];
926
 
 
927
 
                        read_bytes(fd, disk_fragment->start_block, size, cbuffer);
928
 
 
929
 
                        if((res = uncompress(buffer, &bytes, (const char *) cbuffer, size)) != Z_OK) {
930
 
                                if(res == Z_MEM_ERROR)
931
 
                                        BAD_ERROR("zlib::uncompress failed, not enough memory\n");
932
 
                                else if(res == Z_BUF_ERROR)
933
 
                                        BAD_ERROR("zlib::uncompress failed, not enough room in output buffer\n");
934
 
                                else
935
 
                                        BAD_ERROR("zlib::uncompress failed, unknown error %d\n", res);
936
 
                        }
937
 
                } else
938
 
                        read_bytes(fd, disk_fragment->start_block, size, buffer);
939
 
                return buffer + fragment->offset;
940
 
        }
941
 
}
942
 
 
943
 
        
944
 
void write_fragment()
945
 
{
946
 
        int compressed_size;
947
 
        unsigned char buffer[block_size << 1];
948
 
 
949
 
        if(fragment_size == 0)
950
 
                return;
951
 
 
952
 
        if(fragments % FRAG_SIZE == 0)
953
 
                if((fragment_table = (squashfs_fragment_entry *) realloc(fragment_table, (fragments + FRAG_SIZE) * sizeof(squashfs_fragment_entry))) == NULL)
954
 
                        BAD_ERROR("Out of memory in fragment table\n");
955
 
        fragment_table[fragments].size = mangle(buffer, fragment_data, fragment_size, block_size, noF, 1);
956
 
        fragment_table[fragments].start_block = bytes;
957
 
        compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[fragments].size);
958
 
        write_bytes(fd, bytes, compressed_size, buffer);
959
 
        bytes += compressed_size;
960
 
        total_uncompressed += fragment_size;
961
 
        total_compressed += compressed_size;
962
 
        TRACE("Writing fragment %d, uncompressed size %d, compressed size %d\n",fragments, fragment_size, compressed_size);
963
 
        fragments ++;
964
 
        fragment_size = 0;
965
 
}
966
 
 
967
 
 
968
 
static struct fragment empty_fragment = {SQUASHFS_INVALID_BLK, 0, 0};
969
 
struct fragment *get_and_fill_fragment(char *buff, int size)
970
 
{
971
 
        struct fragment *ffrg;
972
 
 
973
 
        if(size == 0)
974
 
                return &empty_fragment;
975
 
 
976
 
        if(fragment_size + size > block_size)
977
 
                write_fragment();
978
 
 
979
 
        if((ffrg = (struct fragment *) malloc(sizeof(struct fragment))) == NULL)
980
 
                BAD_ERROR("Out of memory in fragment block allocation!\n");
981
 
 
982
 
        ffrg->index = fragments;
983
 
        ffrg->offset = fragment_size;
984
 
        ffrg->size = size;
985
 
        memcpy(fragment_data + fragment_size, buff, size);
986
 
        fragment_size += size;
987
 
 
988
 
        return ffrg;
989
 
}
990
 
 
991
 
 
992
 
unsigned int write_fragment_table()
993
 
{
994
 
        unsigned int start_bytes, frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments),
995
 
                meta_blocks = SQUASHFS_FRAGMENT_INDEXES(fragments);
996
 
        char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2], buffer[frag_bytes];
997
 
        squashfs_fragment_entry *p = (squashfs_fragment_entry *) buffer;
998
 
        unsigned short c_byte;
999
 
        int i, compressed_size;
1000
 
        squashfs_fragment_index list[meta_blocks];
1001
 
 
1002
 
        TRACE("write_fragment_table: fragments %d, frag_bytes %d, meta_blocks %d\n", fragments, frag_bytes, meta_blocks);
1003
 
        for(i = 0; i < fragments; i++, p++) {
1004
 
                TRACE("write_fragment_table: fragment %d, start_block %x, size %d\n", i, fragment_table[i].start_block, fragment_table[i].size);
1005
 
                if(!swap)
1006
 
                        memcpy((void *) p, &fragment_table[i], sizeof(squashfs_fragment_entry));
1007
 
                else
1008
 
                        SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_table[i], p);
1009
 
        }
1010
 
 
1011
 
        for(i = 0; i < meta_blocks; i++) {
1012
 
                int avail_bytes = i == meta_blocks - 1 ? frag_bytes % SQUASHFS_METADATA_SIZE : SQUASHFS_METADATA_SIZE;
1013
 
                c_byte = mangle(cbuffer + block_offset, buffer + i * SQUASHFS_METADATA_SIZE , avail_bytes, SQUASHFS_METADATA_SIZE, noF, 0);
1014
 
                if(!swap)
1015
 
                        memcpy((void *) cbuffer, (void *) &c_byte, sizeof(unsigned short));
1016
 
                else
1017
 
                        SQUASHFS_SWAP_SHORTS((&c_byte), cbuffer, 1);
1018
 
                if(check_data)
1019
 
                        *((unsigned char *)(cbuffer + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
1020
 
                list[i] = bytes;
1021
 
                compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
1022
 
                write_bytes(fd, bytes, compressed_size, cbuffer);
1023
 
                bytes += compressed_size;
1024
 
        }
1025
 
 
1026
 
        if(!swap)
1027
 
                write_bytes(fd, bytes, sizeof(list), (char *) list);
1028
 
        else {
1029
 
                squashfs_fragment_index slist[meta_blocks];
1030
 
                SQUASHFS_SWAP_FRAGMENT_INDEXES(list, slist, meta_blocks);
1031
 
                write_bytes(fd, bytes, sizeof(list), (char *) slist);
1032
 
        }
1033
 
 
1034
 
        start_bytes = bytes;
1035
 
        bytes += sizeof(list);
1036
 
 
1037
 
        return start_bytes;
1038
 
}
1039
 
 
1040
 
 
1041
 
unsigned char *read_from_buffer(struct duplicate_buffer_handle *handle, unsigned int avail_bytes)
1042
 
{
1043
 
        unsigned char *v = handle->ptr;
1044
 
        handle->ptr += avail_bytes;     
1045
 
        return v;
1046
 
}
1047
 
 
1048
 
 
1049
 
char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE];
1050
 
unsigned char *read_from_file(struct duplicate_buffer_handle *handle, unsigned int avail_bytes)
1051
 
{
1052
 
        read_bytes(fd, handle->start, avail_bytes, read_from_file_buffer);
1053
 
        handle->start += avail_bytes;
1054
 
        return read_from_file_buffer;
1055
 
}
1056
 
 
1057
 
 
1058
 
/*
1059
 
 * Compute 16 bit BSD checksum over the data
1060
 
 */
1061
 
unsigned short get_checksum(unsigned char *(get_next_file_block)(struct duplicate_buffer_handle *, unsigned int), struct duplicate_buffer_handle *handle, int l)
1062
 
{
1063
 
        unsigned short chksum = 0;
1064
 
        unsigned int bytes = 0;
1065
 
        unsigned char *b;
1066
 
        struct duplicate_buffer_handle position = *handle;
1067
 
 
1068
 
        while(l) {
1069
 
                bytes = l > SQUASHFS_FILE_MAX_SIZE ? SQUASHFS_FILE_MAX_SIZE : l;
1070
 
                l -= bytes;
1071
 
                b = get_next_file_block(&position, bytes);
1072
 
                while(bytes--) {
1073
 
                        chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1;
1074
 
                        chksum += *b++;
1075
 
                }
1076
 
        }
1077
 
 
1078
 
        return chksum;
1079
 
}
1080
 
 
1081
 
 
1082
 
static unsigned int cached_frag = -1;
1083
 
void add_file(int start, int file_bytes, unsigned int *block_listp, int blocks, unsigned int fragment, int offset, int bytes)
1084
 
{
1085
 
        struct fragment *frg;
1086
 
        struct file_info *dupl_ptr;
1087
 
        char *datap;
1088
 
        struct duplicate_buffer_handle handle;
1089
 
        
1090
 
        if(!duplicate_checking)
1091
 
                return;
1092
 
 
1093
 
        if((frg = (struct fragment *) malloc(sizeof(struct fragment))) == NULL)
1094
 
                BAD_ERROR("Out of memory in fragment block allocation!\n");
1095
 
 
1096
 
        frg->index = fragment;
1097
 
        frg->offset = offset;
1098
 
        frg->size = bytes;
1099
 
        if(cached_frag == fragment)
1100
 
                datap = fragment_data + offset;
1101
 
        else
1102
 
                datap = get_fragment(fragment_data, frg);
1103
 
        handle.start = start;
1104
 
        if((dupl_ptr = duplicate(read_from_file, &handle, file_bytes, &block_listp, &start, blocks, &frg, datap, bytes)) != NULL)
1105
 
                dupl_ptr->fragment = frg;
1106
 
        cached_frag = fragment;
1107
 
}
1108
 
 
1109
 
 
1110
 
struct file_info *duplicate(unsigned char *(get_next_file_block)(struct duplicate_buffer_handle *, unsigned int), struct duplicate_buffer_handle *file_start, int bytes, unsigned int **block_list, int *start, int blocks, struct fragment **fragment, char *frag_data, int frag_bytes)
1111
 
{
1112
 
        unsigned short checksum = get_checksum(get_next_file_block, file_start, bytes);
1113
 
        struct duplicate_buffer_handle handle = { frag_data, 0 };
1114
 
        unsigned short fragment_checksum = get_checksum(read_from_buffer, &handle, frag_bytes);
1115
 
        struct file_info *dupl_ptr = bytes ? dupl[checksum] : frag_dups[fragment_checksum];
1116
 
 
1117
 
 
1118
 
        for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
1119
 
                if(bytes == dupl_ptr->bytes && frag_bytes == dupl_ptr->fragment->size && fragment_checksum == dupl_ptr->fragment_checksum) {
1120
 
                        unsigned char buffer1[SQUASHFS_FILE_MAX_SIZE];
1121
 
                        unsigned int dup_bytes = dupl_ptr->bytes, dup_start = dupl_ptr->start;
1122
 
                        struct duplicate_buffer_handle position = *file_start;
1123
 
                        unsigned char *buffer;
1124
 
                        while(dup_bytes) {
1125
 
                                int avail_bytes = dup_bytes > SQUASHFS_FILE_MAX_SIZE ? SQUASHFS_FILE_MAX_SIZE : dup_bytes;
1126
 
 
1127
 
                                buffer = get_next_file_block(&position, avail_bytes);
1128
 
                                read_bytes(fd, dup_start, avail_bytes, buffer1);
1129
 
                                if(memcmp(buffer, buffer1, avail_bytes) != 0)
1130
 
                                        break;
1131
 
                                dup_bytes -= avail_bytes;
1132
 
                                dup_start += avail_bytes;
1133
 
                        }
1134
 
                        if(dup_bytes == 0) {
1135
 
                                char frag_buffer1[block_size];
1136
 
                                char *fragment_buffer1 = get_fragment(frag_buffer1, dupl_ptr->fragment);
1137
 
                                if(frag_bytes == 0 || memcmp(frag_data, fragment_buffer1, frag_bytes) == 0) {
1138
 
                                        TRACE("Found duplicate file, start 0x%x, size %d, checksum 0x%x, fragment %d, size %d, offset %d, checksum 0x%x\n", dupl_ptr->start,
1139
 
                                                dupl_ptr->bytes, dupl_ptr->checksum, dupl_ptr->fragment->index, frag_bytes, dupl_ptr->fragment->offset, fragment_checksum);
1140
 
                                        *block_list = dupl_ptr->block_list;
1141
 
                                        *start = dupl_ptr->start;
1142
 
                                        *fragment = dupl_ptr->fragment;
1143
 
                                        return 0;
1144
 
                                }
1145
 
                        }
1146
 
                }
1147
 
 
1148
 
 
1149
 
        if((dupl_ptr = (struct file_info *) malloc(sizeof(struct file_info))) == NULL) {
1150
 
                BAD_ERROR("Out of memory in dup_files allocation!\n");
1151
 
        }
1152
 
 
1153
 
        dupl_ptr->bytes = bytes;
1154
 
        dupl_ptr->checksum = checksum;
1155
 
        dupl_ptr->start = *start;
1156
 
        dupl_ptr->fragment_checksum = fragment_checksum;
1157
 
        if((dupl_ptr->block_list = (unsigned int *) malloc(blocks * sizeof(unsigned int))) == NULL) {
1158
 
                BAD_ERROR("Out of memory allocating block_list\n");
1159
 
        }
1160
 
        
1161
 
        memcpy(dupl_ptr->block_list, *block_list, blocks * sizeof(unsigned int));
1162
 
        dup_files ++;
1163
 
        if(bytes) {
1164
 
                dupl_ptr->next = dupl[checksum];
1165
 
                dupl[checksum] = dupl_ptr;
1166
 
        } else {
1167
 
                dupl_ptr->next = frag_dups[fragment_checksum];
1168
 
                frag_dups[fragment_checksum] = dupl_ptr;
1169
 
        }
1170
 
 
1171
 
        return dupl_ptr;
1172
 
}
1173
 
 
1174
 
 
1175
 
#define MINALLOCBYTES (1024 * 1024)
1176
 
squashfs_inode write_file(char *filename, long long size, int *duplicate_file)
1177
 
{
1178
 
        unsigned int frag_bytes, file, start, file_bytes = 0, block = 0;
1179
 
        unsigned int c_byte;
1180
 
        long long read_size = (size > SQUASHFS_MAX_FILE_SIZE) ? SQUASHFS_MAX_FILE_SIZE : size;
1181
 
        unsigned int blocks = (read_size + block_size - 1) >> block_log;
1182
 
        unsigned int block_list[blocks], *block_listp = block_list;
1183
 
        char buff[block_size], *c_buffer;
1184
 
        int allocated_blocks = blocks, i, bbytes, whole_file = 1;
1185
 
        struct fragment *fragment;
1186
 
        struct file_info *dupl_ptr;
1187
 
        struct duplicate_buffer_handle handle;
1188
 
 
1189
 
        if(!no_fragments && (read_size < block_size || always_use_fragments)) {
1190
 
                allocated_blocks = blocks = read_size >> block_log;
1191
 
                frag_bytes = read_size % block_size;
1192
 
        } else
1193
 
                frag_bytes = 0;
1194
 
 
1195
 
        if(size > read_size)
1196
 
                ERROR("file %s truncated to %Ld bytes\n", filename, SQUASHFS_MAX_FILE_SIZE);
1197
 
 
1198
 
        total_bytes += read_size;
1199
 
        if((file = open(filename, O_RDONLY)) == -1) {
1200
 
                perror("Error in opening file, skipping...");
1201
 
                return SQUASHFS_INVALID;
1202
 
        }
1203
 
 
1204
 
        do {
1205
 
                if((c_buffer = (char *) malloc((allocated_blocks + 1) << block_log)) == NULL) {
1206
 
                        TRACE("Out of memory allocating write_file buffer, allocated_blocks %d, blocks %d\n", allocated_blocks, blocks);
1207
 
                        whole_file = 0;
1208
 
                        if((allocated_blocks << (block_log - 1)) < MINALLOCBYTES)
1209
 
                                BAD_ERROR("Out of memory allocating write_file buffer, could not allocate %d blocks (%d Kbytes)\n", allocated_blocks, allocated_blocks << (block_log - 10));
1210
 
                        allocated_blocks >>= 1;
1211
 
                }
1212
 
        } while(!c_buffer);
1213
 
 
1214
 
        for(start = bytes; block < blocks; file_bytes += bbytes) {
1215
 
                for(i = 0, bbytes = 0; (i < allocated_blocks) && (block < blocks); i++) {
1216
 
                        int available_bytes = read_size - (block * block_size) > block_size ? block_size : read_size - (block * block_size);
1217
 
                        if(read(file, buff, available_bytes) == -1)
1218
 
                                goto read_err;
1219
 
                        c_byte = mangle(c_buffer + bbytes, buff, available_bytes, block_size, noD, 1);
1220
 
                        block_list[block ++] = c_byte;
1221
 
                        bbytes += SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
1222
 
                }
1223
 
                if(!whole_file) {
1224
 
                        write_bytes(fd, bytes, bbytes, c_buffer);
1225
 
                        bytes += bbytes;
1226
 
                }
1227
 
        }
1228
 
 
1229
 
        if(frag_bytes != 0)
1230
 
                if(read(file, buff, frag_bytes) == -1)
1231
 
                        goto read_err;
1232
 
 
1233
 
        close(file);
1234
 
        if(whole_file) {
1235
 
                handle.ptr = c_buffer;
1236
 
                if(duplicate_checking && (dupl_ptr = duplicate(read_from_buffer, &handle, file_bytes, &block_listp, &start, blocks, &fragment, buff, frag_bytes)) == NULL) {
1237
 
                        *duplicate_file = TRUE;
1238
 
                        goto wr_inode;
1239
 
                }
1240
 
                write_bytes(fd, bytes, file_bytes, c_buffer);
1241
 
                bytes += file_bytes;
1242
 
        } else {
1243
 
                handle.start = start;
1244
 
                if(duplicate_checking && (dupl_ptr = duplicate(read_from_file, &handle, file_bytes, &block_listp, &start, blocks, &fragment, buff, frag_bytes)) == NULL) {
1245
 
                        bytes = start;
1246
 
                        if(!block_device)
1247
 
                                ftruncate(fd, bytes);
1248
 
                        *duplicate_file = TRUE;
1249
 
                        goto wr_inode;
1250
 
                }
1251
 
        }
1252
 
 
1253
 
        fragment = get_and_fill_fragment(buff, frag_bytes);
1254
 
        if(duplicate_checking)
1255
 
                dupl_ptr->fragment = fragment;
1256
 
 
1257
 
        *duplicate_file = FALSE;
1258
 
 
1259
 
wr_inode:
1260
 
        free(c_buffer);
1261
 
        file_count ++;
1262
 
        return create_inode(filename, SQUASHFS_FILE_TYPE, read_size, start, blocks, block_listp, fragment, NULL);
1263
 
 
1264
 
read_err:
1265
 
        perror("Error in reading file, skipping...");
1266
 
        free(c_buffer);
1267
 
        return SQUASHFS_INVALID;
1268
 
}
1269
 
 
1270
 
 
1271
 
struct linuxdir {
1272
 
        DIR     *linuxdir;
1273
 
        char    pathname[8192];
1274
 
};
1275
 
 
1276
 
 
1277
 
void *linux_opendir(char *pathname, struct directory *dir)
1278
 
{
1279
 
        struct linuxdir *linuxdir;
1280
 
        if((linuxdir = malloc(sizeof(struct linuxdir))) == NULL)
1281
 
                return NULL;
1282
 
        if((linuxdir->linuxdir = opendir(pathname)) == NULL) {
1283
 
                free(linuxdir);
1284
 
                return NULL;
1285
 
        }
1286
 
        strcpy(linuxdir->pathname, pathname);
1287
 
        return (void *) linuxdir;
1288
 
}
1289
 
 
1290
 
 
1291
 
int linux_readdir(void *l, char *filename, char *dir_name, struct stat* devtable_inode_info)
1292
 
{
1293
 
        struct dirent *d_name;
1294
 
        struct linuxdir *linuxdir = (struct linuxdir *)l;
1295
 
 
1296
 
        devtable_inode_info->st_mode = 0;
1297
 
 
1298
 
        do {
1299
 
                if((d_name = readdir(linuxdir->linuxdir)) == NULL)
1300
 
                {
1301
 
                        if ( strncmp(linuxdir->pathname, dev_path, sizeof(linuxdir->pathname)) == 0 )
1302
 
                        {
1303
 
                           if ( devtable_readdir(l, filename, dir_name, devtable_inode_info ) )
1304
 
                           {
1305
 
                              return( TRUE );
1306
 
                           }
1307
 
                        }
1308
 
                        return FALSE;
1309
 
                }
1310
 
        } while(strcmp(d_name->d_name, ".") == 0 || strcmp(d_name->d_name, "..") == 0);
1311
 
        strcat(strcat(strcpy(filename, linuxdir->pathname), "/"), d_name->d_name);
1312
 
        strcpy(dir_name, d_name->d_name);
1313
 
        return TRUE;
1314
 
}
1315
 
 
1316
 
 
1317
 
void linux_closedir(void *linuxdir)
1318
 
{
1319
 
        closedir(((struct linuxdir *)linuxdir)->linuxdir);
1320
 
        free(linuxdir);
1321
 
}
1322
 
 
1323
 
 
1324
 
static int interpret_table_entry(char *line, char* filename, char* dirname, struct stat* devtable_inode_info)
1325
 
{
1326
 
        char type, *name = NULL;
1327
 
        char* full;
1328
 
        unsigned long mode = 0755, uid = 0, gid = 0, major = 0, minor = 0;
1329
 
        unsigned long start = 0, increment = 1, count = 0;
1330
 
 
1331
 
        if (sscanf (line, "%" SCANF_PREFIX "s %c %lo %lu %lu %lu %lu %lu %lu %lu",
1332
 
                 SCANF_STRING(name), &type, &mode, &uid, &gid, &major, &minor,
1333
 
                 &start, &increment, &count) < 0) 
1334
 
        {
1335
 
                return 1;
1336
 
        }
1337
 
 
1338
 
        if (!strcmp(name, "/")) {
1339
 
                error_msg_and_die("Device table entries require absolute paths");
1340
 
        }
1341
 
 
1342
 
        strcpy( filename, source_path[0] );
1343
 
        strcat( filename, name );
1344
 
 
1345
 
   full = strdup( name );
1346
 
        strcpy( dirname, (char*)basename(full) );
1347
 
   free(full);
1348
 
 
1349
 
        switch (type) {
1350
 
        case 'd':
1351
 
                mode |= S_IFDIR;
1352
 
                break;
1353
 
        case 'f':
1354
 
                mode |= S_IFREG;
1355
 
                break;
1356
 
        case 'p':
1357
 
                mode |= S_IFIFO;
1358
 
                break;
1359
 
        case 'c':
1360
 
        case 'b':
1361
 
                mode |= (type == 'c') ? S_IFCHR : S_IFBLK;
1362
 
                if (count > 0) {
1363
 
                        char *buf;
1364
 
                        unsigned long i;
1365
 
                        dev_t rdev;
1366
 
 
1367
 
                        for (i = start; i < count; i++) {
1368
 
                                asprintf(&buf, "%s%lu", name, i);
1369
 
                                rdev = makedev(major, minor + (i * increment - start));
1370
 
                                free(buf);
1371
 
                                devtable_inode_info->st_rdev = rdev;
1372
 
                        }
1373
 
                } else {
1374
 
                        dev_t rdev = makedev(major, minor);
1375
 
                        devtable_inode_info->st_rdev = rdev;
1376
 
                }
1377
 
                break;
1378
 
        default:
1379
 
                error_msg_and_die("Unsupported file type");
1380
 
        }
1381
 
        free(name);
1382
 
 
1383
 
        devtable_inode_info->st_mode = mode;
1384
 
 
1385
 
        return 0;
1386
 
}
1387
 
 
1388
 
int devtable_readdir(void *l, char *filename, char *dir_name, struct stat* devtable_inode_info)
1389
 
{
1390
 
        char *line;
1391
 
        int status = FALSE;
1392
 
        int commentLine = FALSE;
1393
 
        size_t length = 0;
1394
 
 
1395
 
        strcpy(filename, "");
1396
 
        strcpy(dir_name, "");
1397
 
 
1398
 
        /* Looks ok so far.  The general plan now is to read in one
1399
 
         * line at a time, check for leading comment delimiters ('#'),
1400
 
         * then try and parse the line as a device table.  If we fail
1401
 
         * to parse things, try and help the poor fool to fix their
1402
 
         * device table with a useful error msg... */
1403
 
        line = NULL;
1404
 
        do
1405
 
        {
1406
 
                if (getline(&line, &length, devtable) != -1) {
1407
 
                        status = TRUE;
1408
 
                        /* First trim off any whitespace */
1409
 
                        int len = strlen(line);
1410
 
 
1411
 
                        /* trim trailing whitespace */
1412
 
                        while (len > 0 && isspace(line[len - 1]))
1413
 
                                line[--len] = '\0';
1414
 
                        /* trim leading whitespace */
1415
 
                        memmove(line, &line[strspn(line, " \n\r\t\v")], len);
1416
 
 
1417
 
                        /* How long are we after trimming? */
1418
 
                        len = strlen(line);
1419
 
 
1420
 
                        /* If this is NOT a comment line, try to interpret it */
1421
 
                        if (len && *line != '#') {
1422
 
                                commentLine = FALSE;
1423
 
                        if (interpret_table_entry(line, filename, dir_name, devtable_inode_info))
1424
 
                                        status = FALSE;
1425
 
                        }
1426
 
                        else
1427
 
                        {
1428
 
                                commentLine = TRUE;
1429
 
                                continue;
1430
 
                        }
1431
 
 
1432
 
                        free(line);
1433
 
                        line = NULL;
1434
 
                }
1435
 
                else
1436
 
                {
1437
 
                        free(line);
1438
 
                }
1439
 
        } while (commentLine == TRUE);
1440
 
 
1441
 
 
1442
 
        return status;
1443
 
}
1444
 
 
1445
 
 
1446
 
char b_buffer[8192];
1447
 
char *name;
1448
 
char *basename_r();
1449
 
 
1450
 
char *getbase(char *pathname)
1451
 
{
1452
 
        char *result;
1453
 
 
1454
 
        if(*pathname != '/') {
1455
 
                result = getenv("PWD");
1456
 
                strcat(strcat(strcpy(b_buffer, result), "/"), pathname);
1457
 
        } else
1458
 
                strcpy(b_buffer, pathname);
1459
 
        name = b_buffer;
1460
 
        if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0))
1461
 
                return NULL;
1462
 
        else
1463
 
                return result;
1464
 
}
1465
 
 
1466
 
 
1467
 
char *basename_r()
1468
 
{
1469
 
        char *s;
1470
 
        char *p;
1471
 
        int n = 1;
1472
 
 
1473
 
        for(;;) {
1474
 
                s = name;
1475
 
                if(*name == '\0')
1476
 
                        return NULL;
1477
 
                if(*name != '/') {
1478
 
                        while(*name != '\0' && *name != '/') name++;
1479
 
                        n = name - s;
1480
 
                }
1481
 
                while(*name == '/') name++;
1482
 
                if(strncmp(s, ".", n) == 0)
1483
 
                        continue;
1484
 
                if((*name == '\0') || (strncmp(s, "..", n) == 0) || ((p = basename_r()) == NULL)) {
1485
 
                        s[n] = '\0';
1486
 
                        return s;
1487
 
                }
1488
 
                if(strcmp(p, "..") == 0)
1489
 
                        continue;
1490
 
                return p;
1491
 
        }
1492
 
}
1493
 
 
1494
 
 
1495
 
char encomp_pathname[8192];
1496
 
int encomp_entry = 0;
1497
 
 
1498
 
void *encomp_opendir(char *pathname, struct directory *dir)
1499
 
{
1500
 
        int i;
1501
 
 
1502
 
        for(i = 0; i < old_root_entries; i++)
1503
 
                add_dir(old_root_entry[i].inode, old_root_entry[i].name, old_root_entry[i].type, dir);
1504
 
 
1505
 
        return (void *)source_path;
1506
 
}
1507
 
 
1508
 
 
1509
 
int encomp_readdir(void *l, char *filename, char *dir_name, struct stat* devtable_inode_info)
1510
 
{
1511
 
        char *basename;
1512
 
        int n, pass = 1;
1513
 
 
1514
 
        devtable_inode_info->st_mode = 0;
1515
 
 
1516
 
        while(encomp_entry < source) {
1517
 
                if((basename = getbase(source_path[encomp_entry])) == NULL) {
1518
 
                        ERROR("Bad source directory %s - skipping ...\n", source_path[encomp_entry]);
1519
 
                        encomp_entry++;
1520
 
                        continue;
1521
 
                }
1522
 
                strcpy(filename, source_path[encomp_entry]);
1523
 
                strcpy(dir_name, basename);
1524
 
                for(;;) {
1525
 
                        for(n = 0; n < old_root_entries && strcmp(old_root_entry[n].name, dir_name) != 0; n++);
1526
 
                        if(n == old_root_entries) {
1527
 
                                add_old_root_entry(dir_name, 0, 0);
1528
 
                                encomp_entry++;
1529
 
                                return TRUE;
1530
 
                        }
1531
 
                        ERROR("Source directory entry %s already used! - trying ", dir_name);
1532
 
                        sprintf(dir_name, "%s_%d", basename, pass++);
1533
 
                        ERROR("%s\n", dir_name);
1534
 
                }
1535
 
        }
1536
 
        return FALSE;
1537
 
}
1538
 
 
1539
 
 
1540
 
void encomp_closedir(void *linuxdir)
1541
 
{
1542
 
}
1543
 
 
1544
 
 
1545
 
void *single_opendir(char *pathname, struct directory *dir)
1546
 
{
1547
 
        encomp_opendir(pathname, dir);
1548
 
        return linux_opendir(pathname, dir);
1549
 
}
1550
 
 
1551
 
 
1552
 
int single_readdir(void *l, char *filename, char *dir_name, struct stat* devtable_inode_info)
1553
 
{
1554
 
        int i, pass = 1;
1555
 
        char name[SQUASHFS_NAME_LEN + 1];
1556
 
 
1557
 
        devtable_inode_info->st_mode = 0;
1558
 
 
1559
 
        if(linux_readdir(l, filename, dir_name, devtable_inode_info) == FALSE)
1560
 
                return FALSE;
1561
 
 
1562
 
        strcpy(name, dir_name);
1563
 
        for(;;) {
1564
 
                for(i = 0; i < old_root_entries && strcmp(old_root_entry[i].name, dir_name) != 0; i++);
1565
 
                if(i == old_root_entries) {
1566
 
                        add_old_root_entry(dir_name, 0, 0);
1567
 
                        return TRUE;
1568
 
                }
1569
 
                ERROR("Source directory entry %s already used! - trying ", dir_name);
1570
 
                sprintf(dir_name, "%s_%d", name, pass++);
1571
 
                ERROR("%s\n", dir_name);
1572
 
        }
1573
 
}
1574
 
 
1575
 
 
1576
 
squashfs_inode dir_scan(char *pathname, void* (_opendir)(char *, struct directory *), int (_readdir)(void *, char *, char *, struct stat*),
1577
 
                void (_closedir)(void *))
1578
 
{
1579
 
        void *linuxdir;
1580
 
        struct stat buf;
1581
 
        char filename[8192], dir_name[8192];
1582
 
        int squashfs_type;
1583
 
        squashfs_inode inode = SQUASHFS_INVALID;
1584
 
        struct directory dir;
1585
 
 
1586
 
        struct stat devtable_inode_info;
1587
 
        unsigned long mode;
1588
 
        devtable_inode_info.st_mode = 0;
1589
 
        
1590
 
        init_dir(&dir);
1591
 
        if((linuxdir = _opendir(pathname, &dir)) == NULL) {
1592
 
                ERROR("Could not open %s, skipping...\n", pathname);
1593
 
                goto error;
1594
 
        }
1595
 
        
1596
 
        while(_readdir(linuxdir, filename, dir_name, &devtable_inode_info) != FALSE) {
1597
 
 
1598
 
                mode = devtable_inode_info.st_mode;
1599
 
 
1600
 
                if ( devtable_inode_info.st_mode == 0 ) {
1601
 
                        if(lstat(filename, &buf) == -1) {
1602
 
                                char buffer[8192];
1603
 
                                sprintf(buffer, "Cannot stat dir/file %s, ignoring", filename);
1604
 
                                perror(buffer);
1605
 
                                continue;
1606
 
                        }
1607
 
                        mode = buf.st_mode;
1608
 
                }
1609
 
 
1610
 
                if(excluded(filename, &buf))
1611
 
                        continue;
1612
 
 
1613
 
                switch(mode & S_IFMT) {
1614
 
                        case S_IFREG: {
1615
 
                                int duplicate_file;
1616
 
 
1617
 
                                squashfs_type = SQUASHFS_FILE_TYPE;
1618
 
                                if(!sorted) {
1619
 
                                        inode = write_file(filename, buf.st_size, &duplicate_file);
1620
 
                                        INFO("file %s, uncompressed size %Ld bytes, %s\n", filename, buf.st_size, duplicate_file ? "DUPLICATE" : "");
1621
 
                                } else
1622
 
                                        inode = get_sorted_inode(&buf);
1623
 
                                break;
1624
 
                        }
1625
 
                        case S_IFDIR:
1626
 
                                squashfs_type = SQUASHFS_DIR_TYPE;
1627
 
                                inode = dir_scan(filename, linux_opendir, linux_readdir, linux_closedir);
1628
 
                                break;
1629
 
 
1630
 
                        case S_IFLNK:
1631
 
                                squashfs_type = SQUASHFS_SYMLINK_TYPE;
1632
 
                                inode = create_inode(filename, squashfs_type, 0, 0, 0, NULL, NULL, &devtable_inode_info);
1633
 
                                INFO("symbolic link %s inode 0x%Lx\n", dir_name, inode);
1634
 
                                sym_count ++;
1635
 
                                break;
1636
 
 
1637
 
                        case S_IFCHR:
1638
 
                                squashfs_type = SQUASHFS_CHRDEV_TYPE;
1639
 
                                inode = create_inode(filename, squashfs_type, 0, 0, 0, NULL, NULL, &devtable_inode_info);
1640
 
                                INFO("character device %s inode 0x%Lx\n", dir_name, inode);
1641
 
                                dev_count ++;
1642
 
                                break;
1643
 
 
1644
 
                        case S_IFBLK:
1645
 
                                squashfs_type = SQUASHFS_BLKDEV_TYPE;
1646
 
                                inode = create_inode(filename, squashfs_type, 0, 0, 0, NULL, NULL, &devtable_inode_info);
1647
 
                                INFO("block device %s inode 0x%Lx\n", dir_name, inode);
1648
 
                                dev_count ++;
1649
 
                                break;
1650
 
 
1651
 
                        case S_IFIFO:
1652
 
                                squashfs_type = SQUASHFS_FIFO_TYPE;
1653
 
                                inode = create_inode(filename, squashfs_type, 0, 0, 0, NULL, NULL, &devtable_inode_info);
1654
 
                                INFO("fifo %s inode 0x%Lx\n", dir_name, inode);
1655
 
                                fifo_count ++;
1656
 
                                break;
1657
 
 
1658
 
                        case S_IFSOCK:
1659
 
                                squashfs_type = SQUASHFS_SOCKET_TYPE;
1660
 
                                inode = create_inode(filename, squashfs_type, 0, 0, 0, NULL, NULL, &devtable_inode_info);
1661
 
                                INFO("unix domain socket %s inode 0x%Lx\n", dir_name, inode);
1662
 
                                sock_count ++;
1663
 
                                break;
1664
 
 
1665
 
                         default:
1666
 
                                ERROR("%s unrecognised file type, mode is %x\n", filename, mode);
1667
 
                                continue;
1668
 
                        }
1669
 
 
1670
 
                if(inode != SQUASHFS_INVALID)
1671
 
                        add_dir(inode, dir_name, squashfs_type, &dir);
1672
 
        }
1673
 
 
1674
 
        _closedir(linuxdir);
1675
 
        inode = write_dir(pathname, &dir);
1676
 
        INFO("directory %s inode 0x%Lx\n", pathname, inode);
1677
 
 
1678
 
error:
1679
 
        free(dir.buff);
1680
 
 
1681
 
        return inode;
1682
 
}
1683
 
 
1684
 
 
1685
 
unsigned int slog(unsigned int block)
1686
 
{
1687
 
        int i;
1688
 
 
1689
 
        for(i = 9; i <= 16; i++)
1690
 
                if(block == (1 << i))
1691
 
                        return i;
1692
 
        return 0;
1693
 
}
1694
 
 
1695
 
 
1696
 
int excluded(char *filename, struct stat *buf)
1697
 
{
1698
 
        int i;
1699
 
 
1700
 
        for(i = 0; i < exclude; i++)
1701
 
                if((exclude_paths[i].st_dev == buf->st_dev) && (exclude_paths[i].st_ino == buf->st_ino))
1702
 
                        return TRUE;
1703
 
        return FALSE;
1704
 
}
1705
 
 
1706
 
 
1707
 
#define ADD_ENTRY(buf) \
1708
 
        if(exclude % EXCLUDE_SIZE == 0) {\
1709
 
                if((exclude_paths = (struct exclude_info *) realloc(exclude_paths, (exclude + EXCLUDE_SIZE) * sizeof(struct exclude_info))) == NULL)\
1710
 
                        BAD_ERROR("Out of memory in exclude dir/file table\n");\
1711
 
        }\
1712
 
        exclude_paths[exclude].st_dev = buf.st_dev;\
1713
 
        exclude_paths[exclude++].st_ino = buf.st_ino;
1714
 
int add_exclude(char *path)
1715
 
{
1716
 
        int i;
1717
 
        char buffer[4096], filename[4096];
1718
 
        struct stat buf;
1719
 
 
1720
 
        if(path[0] == '/' || strncmp(path, "./", 2) == 0 || strncmp(path, "../", 3) == 0) {
1721
 
                if(lstat(path, &buf) == -1) {
1722
 
                        sprintf(buffer, "Cannot stat exclude dir/file %s, ignoring", path);
1723
 
                        perror(buffer);
1724
 
                        return TRUE;
1725
 
                }
1726
 
                ADD_ENTRY(buf);
1727
 
                return TRUE;
1728
 
        }
1729
 
 
1730
 
        for(i = 0; i < source; i++) {
1731
 
                strcat(strcat(strcpy(filename, source_path[i]), "/"), path);
1732
 
                if(lstat(filename, &buf) == -1) {
1733
 
                        if(!(errno == ENOENT || errno == ENOTDIR)) {
1734
 
                                sprintf(buffer, "Cannot stat exclude dir/file %s, ignoring", filename);
1735
 
                                perror(buffer);
1736
 
                        }
1737
 
                        continue;
1738
 
                }
1739
 
                ADD_ENTRY(buf);
1740
 
        }
1741
 
        return TRUE;
1742
 
}
1743
 
 
1744
 
 
1745
 
void add_old_root_entry(char *name, squashfs_inode inode, int type)
1746
 
{
1747
 
        if((old_root_entry = (struct old_root_entry_info *) realloc(old_root_entry, sizeof(struct old_root_entry_info)
1748
 
                                * (old_root_entries + 1))) == NULL)
1749
 
                BAD_ERROR("Out of memory in old root directory entries reallocation\n");
1750
 
 
1751
 
        strcpy(old_root_entry[old_root_entries].name, name);
1752
 
        old_root_entry[old_root_entries].inode = inode;
1753
 
        old_root_entry[old_root_entries++].type = type;
1754
 
}
1755
 
 
1756
 
 
1757
 
#define VERSION() \
1758
 
        printf("mksquashfs version 2.0\n");\
1759
 
        printf("copyright (C) 2004 Phillip Lougher (plougher@users.sourceforge.net)\n\n"); \
1760
 
        printf("This program is free software; you can redistribute it and/or\n");\
1761
 
        printf("modify it under the terms of the GNU General Public License\n");\
1762
 
        printf("as published by the Free Software Foundation; either version 2,\n");\
1763
 
        printf("or (at your option) any later version.\n\n");\
1764
 
        printf("This program is distributed in the hope that it will be useful,\n");\
1765
 
        printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");\
1766
 
        printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");\
1767
 
        printf("GNU General Public License for more details.\n");
1768
 
int main(int argc, char *argv[])
1769
 
{
1770
 
        struct stat buf;
1771
 
        int i;
1772
 
        squashfs_super_block sBlk;
1773
 
        char *b, *root_name = NULL;
1774
 
        int be, nopad = FALSE, delete = FALSE, keep_as_directory = FALSE, orig_be;
1775
 
 
1776
 
#if __BYTE_ORDER == __BIG_ENDIAN
1777
 
        be = TRUE;
1778
 
#else
1779
 
        be = FALSE;
1780
 
#endif
1781
 
 
1782
 
        block_log = slog(block_size);
1783
 
        if(argc > 1 && strcmp(argv[1], "-version") == 0) {
1784
 
                VERSION();
1785
 
                exit(0);
1786
 
        }
1787
 
        for(i = 1; i < argc && argv[i][0] != '-'; i++);
1788
 
        if(i < 3)
1789
 
                goto printOptions;
1790
 
        source_path = argv + 1;
1791
 
        source = i - 2;
1792
 
        for(; i < argc; i++) {
1793
 
                if(strcmp(argv[i], "-b") == 0) {
1794
 
                        if((++i == argc) || (block_size = strtol(argv[i], &b, 10), *b !='\0')) {
1795
 
                                ERROR("%s: -b missing or invalid block size\n", argv[0]);
1796
 
                                exit(1);
1797
 
                        }
1798
 
 
1799
 
                        if((block_log = slog(block_size)) == 0) {
1800
 
                                ERROR("%s: -b block size not power of two or not between 512 and 64K\n", argv[0]);
1801
 
                                exit(1);
1802
 
                        }
1803
 
                } else if(strcmp(argv[i], "-ef") == 0) {
1804
 
                        if(++i == argc) {
1805
 
                                ERROR("%s: -ef missing filename\n", argv[0]);
1806
 
                                exit(1);
1807
 
                        }
1808
 
                } else if(strcmp(argv[i], "-D") == 0) {
1809
 
                        TRACE("-D passed as parameter\n");
1810
 
                        if(++i == argc) {
1811
 
                                ERROR("%s: -D missing filename\n", argv[0]);
1812
 
                                exit(1);
1813
 
                        }
1814
 
                        TRACE("devTable filename = %s\n", argv[i]);
1815
 
                        devtable = xfopen(argv[i], "r");
1816
 
                        if (fstat(fileno(devtable), &buf) < 0)
1817
 
                                perror_msg_and_die(argv[i]);
1818
 
                        if (buf.st_size < 10)
1819
 
                                ERROR("%s: not a proper device table file\n", argv[0]);
1820
 
                } else if(strcmp(argv[i], "-no-duplicates") == 0)
1821
 
                        duplicate_checking = FALSE;
1822
 
 
1823
 
                else if(strcmp(argv[i], "-no-fragments") == 0)
1824
 
                        no_fragments = TRUE;
1825
 
 
1826
 
                 else if(strcmp(argv[i], "-always-use-fragments") == 0)
1827
 
                        always_use_fragments = TRUE;
1828
 
 
1829
 
                 else if(strcmp(argv[i], "-sort") == 0) {
1830
 
                        if(++i == argc) {
1831
 
                                ERROR("%s: -sort missing filename\n", argv[0]);
1832
 
                                exit(1);
1833
 
                        }
1834
 
                } else if(strcmp(argv[i], "-all-root") == 0 ||
1835
 
                                strcmp(argv[i], "-root-owned") == 0)
1836
 
                        global_uid = global_gid = 0;
1837
 
 
1838
 
                else if(strcmp(argv[i], "-force-uid") == 0) {
1839
 
                        if(++i == argc) {
1840
 
                                ERROR("%s: -force-uid missing uid or user\n", argv[0]);
1841
 
                                exit(1);
1842
 
                        }
1843
 
                        if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') {
1844
 
                                if(global_uid < 0 || global_uid > (((long long) 1 << 32) - 1)) {
1845
 
                                        ERROR("%s: -force-uid uid out of range\n", argv[0]);
1846
 
                                        exit(1);
1847
 
                                }
1848
 
                        } else {
1849
 
                                struct passwd *uid = getpwnam(argv[i]);
1850
 
                                if(uid)
1851
 
                                        global_uid = uid->pw_uid;
1852
 
                                else {
1853
 
                                        ERROR("%s: -force-uid invalid uid or unknown user\n", argv[0]);
1854
 
                                        exit(1);
1855
 
                                }
1856
 
                        }
1857
 
                } else if(strcmp(argv[i], "-force-gid") == 0) {
1858
 
                        if(++i == argc) {
1859
 
                                ERROR("%s: -force-gid missing gid or group\n", argv[0]);
1860
 
                                exit(1);
1861
 
                        }
1862
 
                        if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') {
1863
 
                                if(global_gid < 0 || global_gid > (((long long) 1 << 32) - 1)) {
1864
 
                                        ERROR("%s: -force-gid gid out of range\n", argv[0]);
1865
 
                                        exit(1);
1866
 
                                }
1867
 
                        } else {
1868
 
                                struct group *gid = getgrnam(argv[i]);
1869
 
                                if(gid)
1870
 
                                        global_gid = gid->gr_gid;
1871
 
                                else {
1872
 
                                        ERROR("%s: -force-gid invalid gid or unknown group\n", argv[0]);
1873
 
                                        exit(1);
1874
 
                                }
1875
 
                        }
1876
 
                } else if(strcmp(argv[i], "-noI") == 0 ||
1877
 
                                strcmp(argv[i], "-noInodeCompression") == 0)
1878
 
                        noI = TRUE;
1879
 
 
1880
 
                else if(strcmp(argv[i], "-noD") == 0 ||
1881
 
                                strcmp(argv[i], "-noDataCompression") == 0)
1882
 
                        noD = TRUE;
1883
 
 
1884
 
                else if(strcmp(argv[i], "-noF") == 0 ||
1885
 
                                strcmp(argv[i], "-noFragmentCompression") == 0)
1886
 
                        noF = TRUE;
1887
 
 
1888
 
                else if(strcmp(argv[i], "-nopad") == 0)
1889
 
                        nopad = TRUE;
1890
 
 
1891
 
                else if(strcmp(argv[i], "-check_data") == 0)
1892
 
                        check_data = TRUE;
1893
 
 
1894
 
                else if(strcmp(argv[i], "-info") == 0)
1895
 
                        silent = 0;
1896
 
 
1897
 
                else if(strcmp(argv[i], "-be") == 0)
1898
 
                        be = TRUE;
1899
 
 
1900
 
                else if(strcmp(argv[i], "-le") == 0)
1901
 
                        be = FALSE;
1902
 
                /* BRCM begin */        
1903
 
                else if(strcmp(argv[i], "-gzip") == 0)
1904
 
                        compress_algorithm = GZIP;
1905
 
                
1906
 
                else if(strcmp(argv[i], "-lzma") == 0)
1907
 
                        compress_algorithm = LZMA;
1908
 
                /* BRCM end */
1909
 
                
1910
 
                else if(strcmp(argv[i], "-e") == 0)
1911
 
                        break;
1912
 
 
1913
 
                else if(strcmp(argv[i], "-noappend") == 0)
1914
 
                        delete = TRUE;
1915
 
 
1916
 
                else if(strcmp(argv[i], "-keep-as-directory") == 0)
1917
 
                        keep_as_directory = TRUE;
1918
 
 
1919
 
                else if(strcmp(argv[i], "-root-becomes") == 0) {
1920
 
                        if(++i == argc) {
1921
 
                                ERROR("%s: -root-becomes: missing name\n", argv[0]);
1922
 
                                exit(1);
1923
 
                        }       
1924
 
                        root_name = argv[i];
1925
 
                } else if(strcmp(argv[i], "-version") == 0) {
1926
 
                        VERSION();
1927
 
                } else {
1928
 
                        ERROR("%s: invalid option\n\n", argv[0]);
1929
 
printOptions:
1930
 
                        ERROR("SYNTAX:%s source1 source2 ...  dest [options] [-e list of exclude dirs/files]\n", argv[0]);
1931
 
                        ERROR("\nOptions are\n");
1932
 
                        ERROR("\t-info\t\t\t\tprint files written to filesystem\n");
1933
 
                        ERROR("\t-sort sort file\t\t\tsort files according to priorities in sort file.  One file or dir\n");
1934
 
                        ERROR("\t\t\t\t\twith priority per line.  Priority -32768 to 32767, default priority 0\n");
1935
 
                        ERROR("\t-b block size\t\t\tsize of blocks in ");
1936
 
                        ERROR("filesystem, default %d\n", SQUASHFS_FILE_SIZE);
1937
 
                        ERROR("\t-noappend\t\t\tDo not append to existing filesystem on dest, write a new filesystem\n");
1938
 
                        ERROR("\t\t\t\t\tThis is the default action if dest does not exist, or if no filesystem is on it\n");
1939
 
                        ERROR("\t-keep-as-directory\t\tIf one source directory is specified, create a root directory\n");
1940
 
                        ERROR("\t\t\t\t\tcontaining that directory, rather than the contents of the directory\n");
1941
 
                        ERROR("\t-root-becomes name\t\tWhen appending source files/directories, make the original\n");
1942
 
                        ERROR("\t\t\t\t\troot become a subdirectory in the new root called name, rather\n");
1943
 
                        ERROR("\t\t\t\t\tthan adding the new source items to the original root\n");
1944
 
                        ERROR("\t-noI -noInodeCompression\tdo not compress inode table\n");
1945
 
                        ERROR("\t-noD -noDataCompression\t\tdo not compress data blocks\n");
1946
 
                        ERROR("\t-noF -noFragmentCompression\tdo not compress fragment blocks\n");
1947
 
                        ERROR("\t-no-duplicates\t\t\tdo not perform duplicate checking\n");
1948
 
                        ERROR("\t-no-fragments\t\t\tdo not use fragments\n");
1949
 
                        ERROR("\t-always-use-fragments\t\tuse fragment blocks for files larger than block size\n");
1950
 
                        ERROR("\t-nopad\t\t\t\tdo not pad filesystem to a multiple of 4K\n");
1951
 
                        ERROR("\t-check_data\t\t\tadd checkdata for greater filesystem checks\n");
1952
 
                        ERROR("\t-le\t\t\t\tcreate a little endian filesystem\n");
1953
 
                        ERROR("\t-be\t\t\t\tcreate a big endian filesystem\n");
1954
 
                        /* BRCM begin */
1955
 
                        ERROR("\t-gzip\t\t\t\tuse gzip compression\n");
1956
 
                        ERROR("\t-lzma\t\t\t\tuse lzma compression(default)\n");                        
1957
 
                        /* BRCM end */
1958
 
                        ERROR("\t-ef exclude file\t\tfile is a list of exclude dirs/files - one per line\n");
1959
 
                        ERROR("\t-all-root\t\t\toverride file uid/gid and make all file uid/gids owned by root\n");
1960
 
                        ERROR("\t-root-owned\t\t\talternative name for -all-root\n");
1961
 
                        ERROR("\t-force-uid uid\t\t\tset all file uids to uid\n");
1962
 
                        ERROR("\t-force-gid gid\t\t\tset all file gids to gid\n");
1963
 
                        ERROR("\t-version\t\t\tprint version, licence and copyright message\n");
1964
 
                        exit(1);
1965
 
                }
1966
 
        }
1967
 
 
1968
 
        if(stat(argv[source + 1], &buf) == -1) {
1969
 
                if(errno == ENOENT) { /* Does not exist */
1970
 
                        if((fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) == -1) {
1971
 
                                perror("Could not create destination file");
1972
 
                                exit(1);
1973
 
                        }
1974
 
                        delete = TRUE;
1975
 
                } else {
1976
 
                        perror("Could not stat destination file");
1977
 
                        exit(1);
1978
 
                }
1979
 
 
1980
 
        } else {
1981
 
                if(S_ISBLK(buf.st_mode)) {
1982
 
                        if((fd = open(argv[source + 1], O_RDWR)) == -1) {
1983
 
                                perror("Could not open block device as destination");
1984
 
                                exit(1);
1985
 
                        }
1986
 
                        block_device = 1;
1987
 
 
1988
 
                } else if(S_ISREG(buf.st_mode))  {
1989
 
                        if((fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | O_RDWR)) == -1) {
1990
 
                                perror("Could not open regular file for writing as destination");
1991
 
                                exit(1);
1992
 
                        }
1993
 
                }
1994
 
                else {
1995
 
                        ERROR("Destination not block device or regular file\n");
1996
 
                        exit(1);
1997
 
                }
1998
 
 
1999
 
                if(!delete) {
2000
 
                        if(read_super(fd, &sBlk, &orig_be, argv[source + 1]) == 0) {
2001
 
                                if(S_ISREG(buf.st_mode)) { /* reopen truncating file */
2002
 
                                        close(fd);
2003
 
                                        if((fd = open(argv[source + 1], O_TRUNC  | O_RDWR)) == -1) {
2004
 
                                                perror("Could not open regular file for writing as destination");
2005
 
                                                exit(1);
2006
 
                                        }
2007
 
                                }
2008
 
                                delete = TRUE;
2009
 
                        }
2010
 
 
2011
 
                }
2012
 
        }
2013
 
 
2014
 
        /* process the exclude files - must be done afer destination file has been possibly created */
2015
 
        for(i = source + 2; i < argc; i++)
2016
 
                if(strcmp(argv[i], "-ef") == 0) {
2017
 
                        FILE *fd;
2018
 
                        char filename[16385];
2019
 
                        if((fd = fopen(argv[++i], "r")) == NULL) {
2020
 
                                perror("Could not open exclude file...");
2021
 
                                exit(1);
2022
 
                        }
2023
 
                        while(fscanf(fd, "%16384[^\n]\n", filename) != EOF)
2024
 
                                add_exclude(filename);
2025
 
                        fclose(fd);
2026
 
                } else if(strcmp(argv[i], "-e") == 0)
2027
 
                        break;
2028
 
                else if(strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "-root-becomes") == 0 || strcmp(argv[i], "-sort") == 0)
2029
 
                        i++;
2030
 
 
2031
 
        if(i != argc) {
2032
 
                if(++i == argc) {
2033
 
                        ERROR("%s: -e missing arguments\n", argv[0]);
2034
 
                        exit(1);
2035
 
                }
2036
 
                while(i < argc && add_exclude(argv[i++]));
2037
 
        }
2038
 
 
2039
 
        /* process the sort files - must be done afer the exclude files  */
2040
 
        for(i = source + 2; i < argc; i++)
2041
 
                if(strcmp(argv[i], "-sort") == 0) {
2042
 
                        read_sort_file(argv[++i], source, source_path);
2043
 
                        sorted ++;
2044
 
                } else if(strcmp(argv[i], "-e") == 0)
2045
 
                        break;
2046
 
                else if(strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "-root-becomes") == 0 || strcmp(argv[i], "-ef") == 0)
2047
 
                        i++;
2048
 
 
2049
 
        if((fragment_data = (char *) malloc(block_size)) == NULL)
2050
 
                BAD_ERROR("Out of memory allocating fragment_data");
2051
 
 
2052
 
        if(delete) {
2053
 
                printf("Creating %s filesystem on %s, block size %d.\n",
2054
 
                                be ? "big endian" : "little endian", argv[source + 1], block_size);
2055
 
                bytes = sizeof(squashfs_super_block);
2056
 
        } else {
2057
 
                be = orig_be;
2058
 
                block_log = slog(block_size = sBlk.block_size);
2059
 
                noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags);
2060
 
                noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags);
2061
 
                noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags);
2062
 
                check_data = SQUASHFS_CHECK_DATA(sBlk.flags);
2063
 
                no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags);
2064
 
                always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags);
2065
 
                duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags);
2066
 
                
2067
 
                fragments = SQUASHFS_INVALID_BLK;
2068
 
                if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, &inode_bytes, &data_cache,
2069
 
                                &cache_bytes, &cache_size, &directory_table, &directory_bytes,
2070
 
                                &directory_data_cache, &directory_cache_bytes, &directory_cache_size,
2071
 
                                &file_count, &sym_count, &dev_count, &dir_count, &fifo_count, &sock_count, (squashfs_uid *) uids, &uid_count,
2072
 
                                (squashfs_uid *) guids, &guid_count,
2073
 
                                &total_bytes, &total_inode_bytes, &total_directory_bytes, add_old_root_entry, &fragment_table)) == 0) {
2074
 
                        ERROR("Failed to read existing filesystem - will not overwrite - ABORTING!\n");
2075
 
                        exit(1);
2076
 
                }
2077
 
                if((fragments = sBlk.fragments))
2078
 
                        fragment_table = (squashfs_fragment_entry *) realloc((char *) fragment_table, ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) * sizeof(squashfs_fragment_entry)); 
2079
 
 
2080
 
                printf("Appending to existing %s squashfs filesystem on %s, block size %d\n", be ? "big endian" :
2081
 
                        "little endian", argv[source + 1], block_size);
2082
 
                printf("All -be, -le, -b, -noI, noD, noF, -check_data, no-duplicates, no-fragments and always-use-fragments options ignored\n");
2083
 
                printf("\nIf appending is not wanted, please re-run with -noappend specified!\n\n");
2084
 
 
2085
 
                inode_size = inode_bytes;
2086
 
                directory_size = directory_bytes;
2087
 
 
2088
 
                /* save original filesystem state for restoring ... */
2089
 
                sfragments = fragments;
2090
 
                sbytes = bytes;
2091
 
                sinode_count = sBlk.inodes;
2092
 
                inode_count = file_count + dir_count + sym_count + dev_count;
2093
 
                sdata_cache = (char *)malloc(scache_bytes = cache_size);
2094
 
                sdirectory_data_cache = (char *)malloc(sdirectory_cache_bytes = directory_cache_size);
2095
 
                memcpy(sdata_cache, data_cache, scache_bytes);
2096
 
                memcpy(sdirectory_data_cache, directory_data_cache, sdirectory_cache_bytes);
2097
 
                sinode_bytes = inode_bytes;
2098
 
                sdirectory_bytes = directory_bytes;
2099
 
                suid_count = uid_count;
2100
 
                sguid_count = guid_count;
2101
 
                stotal_bytes = total_bytes;
2102
 
                stotal_inode_bytes = total_inode_bytes;
2103
 
                stotal_directory_bytes = total_directory_bytes;
2104
 
                sfile_count = file_count;
2105
 
                ssym_count = sym_count;
2106
 
                sdev_count = dev_count;
2107
 
                sdir_count = dir_count;
2108
 
                sdup_files = dup_files;
2109
 
                restore = TRUE;
2110
 
                if(setjmp(env))
2111
 
                        goto restore_filesystem;
2112
 
                signal(SIGTERM, sighandler);
2113
 
                signal(SIGINT, sighandler);
2114
 
                write_bytes(fd, SQUASHFS_START, 4, "\0\0\0\0");
2115
 
        }
2116
 
 
2117
 
#if __BYTE_ORDER == __BIG_ENDIAN
2118
 
        swap = !be;
2119
 
#else
2120
 
        swap = be;
2121
 
#endif
2122
 
 
2123
 
        block_offset = check_data ? 3 : 2;
2124
 
 
2125
 
        if(stat(source_path[0], &buf) == -1) {
2126
 
                perror("Cannot stat source directory");
2127
 
                EXIT_MKSQUASHFS();
2128
 
        }
2129
 
 
2130
 
        snprintf(dev_path, sizeof(dev_path), "%s/dev", source_path[0]);
2131
 
 
2132
 
        if(sorted)
2133
 
                sort_files_and_write(source, source_path);
2134
 
 
2135
 
        if(delete && !keep_as_directory && source == 1 && S_ISDIR(buf.st_mode))
2136
 
                sBlk.root_inode = dir_scan(source_path[0], linux_opendir, linux_readdir, linux_closedir);
2137
 
        else if(!keep_as_directory && source == 1 && S_ISDIR(buf.st_mode))
2138
 
                sBlk.root_inode = dir_scan(source_path[0], single_opendir, single_readdir, linux_closedir);
2139
 
        else
2140
 
                sBlk.root_inode = dir_scan("", encomp_opendir, encomp_readdir, encomp_closedir);
2141
 
        sBlk.inodes = inode_count;
2142
 
        sBlk.s_magic = SQUASHFS_MAGIC;
2143
 
        sBlk.s_major = SQUASHFS_MAJOR;
2144
 
        sBlk.s_minor = SQUASHFS_MINOR;
2145
 
        sBlk.block_size = block_size;
2146
 
        sBlk.block_log = block_log;
2147
 
        sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, check_data, noF, no_fragments, always_use_fragments, duplicate_checking);
2148
 
        sBlk.mkfs_time = time(NULL);
2149
 
 
2150
 
restore_filesystem:
2151
 
        write_fragment();
2152
 
        sBlk.fragments = fragments;
2153
 
        sBlk.inode_table_start = write_inodes();
2154
 
        sBlk.directory_table_start = write_directories();
2155
 
        sBlk.fragment_table_start = write_fragment_table();
2156
 
 
2157
 
        TRACE("sBlk->inode_table_start 0x%x\n", sBlk.inode_table_start);
2158
 
        TRACE("sBlk->directory_table_start 0x%x\n", sBlk.directory_table_start);
2159
 
        TRACE("sBlk->fragment_table_start 0x%x\n", sBlk.fragment_table_start);
2160
 
 
2161
 
        if(sBlk.no_uids = uid_count) {
2162
 
                if(!swap)
2163
 
                        write_bytes(fd, bytes, uid_count * sizeof(squashfs_uid), (char *) uids);
2164
 
                else {
2165
 
                        squashfs_uid uids_copy[uid_count];
2166
 
 
2167
 
                        SQUASHFS_SWAP_DATA(uids, uids_copy, uid_count, sizeof(squashfs_uid) * 8);
2168
 
                        write_bytes(fd, bytes, uid_count * sizeof(squashfs_uid), (char *) uids_copy);
2169
 
                }
2170
 
                sBlk.uid_start = bytes;
2171
 
                bytes += uid_count * sizeof(squashfs_uid);
2172
 
        } else
2173
 
                sBlk.uid_start = 0;
2174
 
 
2175
 
        if(sBlk.no_guids = guid_count) {
2176
 
                if(!swap)
2177
 
                        write_bytes(fd, bytes, guid_count * sizeof(squashfs_uid), (char *) guids);
2178
 
                else {
2179
 
                        squashfs_uid guids_copy[guid_count];
2180
 
 
2181
 
                        SQUASHFS_SWAP_DATA(guids, guids_copy, guid_count, sizeof(squashfs_uid) * 8);
2182
 
                        write_bytes(fd, bytes, guid_count * sizeof(squashfs_uid), (char *) guids_copy);
2183
 
                }
2184
 
                sBlk.guid_start = bytes;
2185
 
                bytes += guid_count * sizeof(squashfs_uid);
2186
 
        } else
2187
 
                sBlk.guid_start = 0;
2188
 
 
2189
 
        sBlk.bytes_used = bytes;
2190
 
 
2191
 
        if(!swap)
2192
 
                write_bytes(fd, SQUASHFS_START, sizeof(squashfs_super_block), (char *) &sBlk);
2193
 
        else {
2194
 
                squashfs_super_block sBlk_copy;
2195
 
 
2196
 
                SQUASHFS_SWAP_SUPER_BLOCK((&sBlk), &sBlk_copy); 
2197
 
                write_bytes(fd, SQUASHFS_START, sizeof(squashfs_super_block), (char *) &sBlk_copy);
2198
 
        }
2199
 
 
2200
 
        if(!nopad && (i = bytes & (4096 - 1))) {
2201
 
                unsigned char temp[4096] = {0};
2202
 
                write_bytes(fd, bytes, 4096 - i, temp);
2203
 
        }
2204
 
 
2205
 
        total_bytes += total_inode_bytes + total_directory_bytes + uid_count
2206
 
                * sizeof(unsigned short) + guid_count * sizeof(unsigned short) +
2207
 
                sizeof(squashfs_super_block);
2208
 
 
2209
 
        printf("\n%s filesystem, data block size %d, %s data, %s metadata, %s fragments\n", be ?
2210
 
                "Big endian" : "Little endian", block_size, noI ? "uncompressed" : "compressed", noD ?
2211
 
        "uncompressed" : "compressed", no_fragments ? "no" : noF ? "uncompressed" : "compressed");
2212
 
        printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, bytes / (1024.0 * 1024.0));
2213
 
        printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n",
2214
 
                ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0);
2215
 
        printf("Inode table size %d bytes (%.2f Kbytes)\n",
2216
 
                inode_bytes, inode_bytes / 1024.0);
2217
 
        printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n",
2218
 
                ((float) inode_bytes / total_inode_bytes) * 100.0, total_inode_bytes);
2219
 
        printf("Directory table size %d bytes (%.2f Kbytes)\n",
2220
 
                directory_bytes, directory_bytes / 1024.0);
2221
 
        printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n",
2222
 
                ((float) directory_bytes / total_directory_bytes) * 100.0, total_directory_bytes);
2223
 
        if(duplicate_checking)
2224
 
                printf("Number of duplicate files found %d\n", file_count - dup_files);
2225
 
        else
2226
 
                printf("No duplicate files removed\n");
2227
 
        printf("Number of inodes %d\n", inode_count);
2228
 
        printf("Number of files %d\n", file_count);
2229
 
        if(!no_fragments)
2230
 
                printf("Number of fragments %d\n", fragments);
2231
 
        printf("Number of symbolic links  %d\n", sym_count);
2232
 
        printf("Number of device nodes %d\n", dev_count);
2233
 
        printf("Number of fifo nodes %d\n", fifo_count);
2234
 
        printf("Number of socket nodes %d\n", sock_count);
2235
 
        printf("Number of directories %d\n", dir_count);
2236
 
        printf("Number of uids %d\n", uid_count);
2237
 
 
2238
 
        for(i = 0; i < uid_count; i++) {
2239
 
                struct passwd *user = getpwuid(uids[i]);
2240
 
                printf("\t%s (%d)\n", user == NULL ? "unknown" : user->pw_name, uids[i]);
2241
 
        }
2242
 
 
2243
 
        printf("Number of gids %d\n", guid_count);
2244
 
 
2245
 
        for(i = 0; i < guid_count; i++) {
2246
 
                struct group *group = getgrgid(guids[i]);
2247
 
                printf("\t%s (%d)\n", group == NULL ? "unknown" : group->gr_name, guids[i]);
2248
 
        }
2249
 
        close(fd);
2250
 
        fclose(devtable);
2251
 
        return 0;
2252
 
}