2
* Create a squashfs filesystem. This is a highly compressed read only filesystem.
4
* Copyright (c) 2002, 2003, 2004 Phillip Lougher <plougher@users.sourceforge.net>
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.
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.
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.
29
#include <sys/types.h>
37
#define __BYTE_ORDER BYTE_ORDER
38
#define __BIG_ENDIAN BIG_ENDIAN
39
#define __LITTLE_ENDIAN LITTLE_ENDIAN
48
#include "mksquashfs.h"
49
#include <squashfs_fs.h>
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 */
62
#define TRACE(s, args...) printf("mksquashfs: "s, ## args)
64
#define TRACE(s, args...)
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)\
72
#define BAD_ERROR(s, args...) do {\
73
fprintf(stderr, "FATAL ERROR:" s, ##args);\
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...
83
#define SCANF_PREFIX "a"
84
#define SCANF_STRING(s) (&s)
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, ...)
96
ret = vsprintf(str, fmt, ap);
102
static FILE *devtable = NULL;
104
int duplicate_checking = 1, noF = 0, no_fragments = 0, always_use_fragments = 0;
105
int total_compressed = 0, total_uncompressed = 0;
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];
115
/* write position within data section */
116
unsigned int bytes = 0, total_bytes = 0;
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;
122
/* cached directory table */
123
char *directory_data_cache = NULL;
124
unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
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;
130
/* cached inode table */
131
char *data_cache = NULL;
132
unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
134
/* in memory directory header */
136
unsigned int start_block;
140
unsigned int entry_count;
141
squashfs_dir_header *entry_count_p;
144
struct file_info *dupl[65536], *frag_dups[65536];
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;
150
/* list of exclude dirs/files */
151
struct exclude_info {
156
#define EXCLUDE_SIZE 8192
158
struct exclude_info *exclude_paths = NULL;
159
int excluded(char *filename, struct stat *buf);
161
/* fragment block data structures */
163
static char *fragment_data;
164
static int fragment_size = 0;
172
#define FRAG_SIZE 32768
173
squashfs_fragment_entry *fragment_table = NULL;
175
/* list of source dirs/files */
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;
188
/* in memory file info */
191
unsigned short checksum;
193
unsigned int *block_list;
194
struct file_info *next;
195
struct fragment *fragment;
196
unsigned short fragment_checksum;
199
/* count of how many times SIGINT or SIGQUIT has been sent */
202
/* restore orignal filesystem state if appending to existing filesystem is cancelled */
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;
213
/*flag whether destination file is a block device */
214
int block_device = 0;
216
/* flag indicating whether files are sorted using sort list(s) */
219
long long global_uid = -1, global_gid = -1;
221
/* structure to used to pass in a pointer or an integer
222
* to duplicate buffer read helper functions.
224
struct duplicate_buffer_handle {
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);
246
#define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) + (((char *)A) - data_cache)))
251
int compress_algorithm = LZMA;
254
static void verror_msg(const char *s, va_list p)
257
fprintf(stderr, "mkcramfs: ");
258
vfprintf(stderr, s, p);
261
static void vperror_msg(const char *s, va_list p)
270
fprintf(stderr, "%s%s\n", s, strerror(err));
273
static void perror_msg(const char *s, ...)
282
static void error_msg_and_die(const char *s, ...)
293
static void perror_msg_and_die(const char *s, ...)
303
static FILE *xfopen(const char *path, const char *mode)
307
if ((fp = fopen(path, mode)) == NULL)
308
perror_msg_and_die("%s", path);
314
ERROR("Exiting - restoring original filesystem!\n\n");
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;
342
ERROR("Interrupting will restore original filesystem!\n");
343
ERROR("Interrupt again to quit\n");
349
unsigned int mangle(char *d, char *s, int size, int block_size, int uncompressed, int data_block)
351
unsigned long c_byte;
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");
363
BAD_ERROR("zlib::compress failed, unknown error %d\n", res);
368
if (compress_algorithm == LZMA) {
370
unsigned lzma_dictsize;
371
unsigned lzma_fastbytes;
372
int opt_compression_level = 1;
374
c_byte = block_size << 3;
375
switch (opt_compression_level) {
378
lzma_dictsize = 1 << 20;
383
lzma_dictsize = 1 << 22;
384
lzma_fastbytes = 128;
388
lzma_dictsize = 1 << 24;
389
lzma_fastbytes = 255;
392
BAD_ERROR("Invalid LZMA compression level. Must be 1,2,3.");
395
if(!uncompressed && !(res = compress_lzma_7zapi((const unsigned char*)s,
399
lzma_algo, lzma_dictsize, lzma_fastbytes))) {
400
/* this should NEVER happen */
401
BAD_ERROR("Internal error - LZMA compression failed.\n");
404
//printf("LZMA: block_size=%d, in_size=%d, out_size=%d\n", block_size, size, c_byte);
408
if(uncompressed || c_byte >= size) {
410
return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : SQUASHFS_COMPRESSED_BIT);
413
return (unsigned int) c_byte;
417
squashfs_base_inode_header *get_inode(int req_size)
420
unsigned short c_byte;
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))
428
inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
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);
435
memcpy((void *) (inode_table + inode_bytes), (void *) &c_byte, sizeof(unsigned short));
437
SQUASHFS_SWAP_SHORTS((&c_byte), (inode_table + inode_bytes), 1);
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;
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;
450
if((data_cache = (char *) realloc(data_cache, cache_size + realloc_size)) == NULL) {
453
cache_size += realloc_size;
456
cache_bytes += req_size;
458
return (squashfs_base_inode_header *)(data_cache + (cache_bytes - req_size));
461
BAD_ERROR("Out of memory in inode table reallocation!\n");
465
void read_bytes(int fd, unsigned int byte, int bytes, char *buff)
469
if(lseek(fd, off, SEEK_SET) == -1) {
470
perror("Lseek on destination failed");
474
if(read(fd, buff, bytes) == -1) {
475
perror("Read on destination failed");
481
void write_bytes(int fd, unsigned int byte, int bytes, char *buff)
485
if(off + bytes > ((long long)1<<32) - 1 )
486
BAD_ERROR("Filesystem greater than maximum size 2^32 - 1\n");
488
if(lseek(fd, off, SEEK_SET) == -1) {
489
perror("Lseek on destination failed");
493
if(write(fd, buff, bytes) == -1) {
494
perror("Write on destination failed");
500
unsigned int write_inodes()
502
unsigned short c_byte;
504
char *datap = data_cache;
505
unsigned int start_bytes = 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");
512
inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
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);
518
memcpy((void *) (inode_table + inode_bytes), (void *) &c_byte, sizeof(unsigned short));
520
SQUASHFS_SWAP_SHORTS((&c_byte), (inode_table + inode_bytes), 1);
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;
529
write_bytes(fd, bytes, inode_bytes, (char *) inode_table);
530
bytes += inode_bytes;
536
unsigned int write_directories()
538
unsigned short c_byte;
540
char *directoryp = directory_data_cache;
541
unsigned int start_bytes = bytes;
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");
549
directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
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);
555
memcpy((void *) (directory_table + directory_bytes), (void *) &c_byte, sizeof(unsigned short));
557
SQUASHFS_SWAP_SHORTS((&c_byte), (directory_table + directory_bytes), 1);
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;
565
write_bytes(fd, bytes, directory_bytes, (char *) directory_table);
566
bytes += directory_bytes;
572
unsigned int get_uid(squashfs_uid uid)
576
for(i = 0; (i < uid_count) && uids[i] != uid; i++);
578
if(uid_count == SQUASHFS_UIDS) {
579
ERROR("Out of uids! - using uid 0 - probably not what's wanted!\n");
582
uids[uid_count++] = uid;
589
unsigned int get_guid(squashfs_uid uid, squashfs_uid guid)
594
return SQUASHFS_GUIDS;
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;
602
guids[guid_count++] = guid;
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)
614
squashfs_inode_header inode_header;
615
squashfs_base_inode_header *inode, *base = &inode_header.base;
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);
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) {
636
sprintf(buffer, "Cannot stat dir/file %s, ignoring", filename);
638
return SQUASHFS_INVALID;
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);
646
if(type == SQUASHFS_FILE_TYPE) {
648
squashfs_reg_inode_header *reg = &inode_header.reg, *inodep;
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. */
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;
662
memcpy((void *) inodep, (void *) reg, sizeof(*reg));
663
memcpy((void *) inodep->block_list, block_list, offset * sizeof(unsigned int));
665
SQUASHFS_SWAP_REG_INODE_HEADER(reg, inodep);
666
SQUASHFS_SWAP_INTS(block_list, inodep->block_list, offset);
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]);
673
else if(type == SQUASHFS_DIR_TYPE) {
674
squashfs_dir_inode_header *dir = &inode_header.dir;
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;
682
memcpy((void *) inode, (void *) dir, sizeof(*dir));
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);
688
else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) {
689
squashfs_dev_inode_header *dev = &inode_header.dev;
691
inode = get_inode(sizeof(*dev));
692
dev->rdev = (unsigned short) ((major(buf.st_rdev) << 8) |
693
(minor(buf.st_rdev) & 0xff));
695
memcpy((void *) inode, (void *) dev, sizeof(*dev));
697
SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode);
698
TRACE("Device inode, rdev %x\n", dev->rdev);
700
else if(type == SQUASHFS_SYMLINK_TYPE) {
701
squashfs_symlink_inode_header *symlink = &inode_header.symlink, *inodep;
705
if((byte = readlink(filename, buff, 65536)) == -1) {
706
perror("Error in reading symbolic link, skipping...");
707
return SQUASHFS_INVALID;
711
ERROR("Symlink is greater than 65536 bytes! skipping...");
712
return SQUASHFS_INVALID;
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. */
721
symlink->symlink_size = byte;
723
memcpy((void *) inode, symlink, sizeof(*symlink));
725
SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
726
strncpy(inodep->symlink, buff, byte);
727
TRACE("Symbolic link inode, symlink_size %d\n", byte);
729
else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) {
730
squashfs_ipc_inode_header *ipc = &inode_header.ipc;
732
inode = get_inode(sizeof(*ipc));
734
memcpy((void *) inode, (void *) ipc, sizeof(*ipc));
736
SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode);
737
TRACE("ipc inode, type %s %d\n", type == SQUASHFS_FIFO_TYPE ? "fifo" : "socket");
739
return SQUASHFS_INVALID;
741
i_no = MKINODE(inode);
744
TRACE("Created inode 0x%Lx, type %d, uid %d, guid %d\n", i_no, type, base->uid, base->guid);
750
void init_dir(struct directory *dir)
752
if((dir->buff = (char *)malloc(SQUASHFS_METADATA_SIZE)) == NULL) {
753
BAD_ERROR("Out of memory allocating directory buffer\n");
756
dir->size = SQUASHFS_METADATA_SIZE;
758
dir->entry_count = 256;
759
dir->entry_count_p = NULL;
763
void add_dir(squashfs_inode inode, char *name, int type, struct directory *dir)
766
squashfs_dir_entry idir, *idirp;
767
unsigned int start_block = inode >> 16;
768
unsigned int offset = inode & 0xffff;
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);
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");
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) -
788
if(dir->entry_count == 256 || start_block != dir->start_block) {
789
if(dir->entry_count_p) {
790
squashfs_dir_header dir_header;
792
dir_header.count = dir->entry_count - 1;
793
dir_header.start_block = dir->start_block;
795
memcpy((void *) dir->entry_count_p, (void *) &dir_header, sizeof(dir_header));
797
SQUASHFS_SWAP_DIR_HEADER((&dir_header), dir->entry_count_p);
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);
806
idirp = (squashfs_dir_entry *) dir->p;
807
idir.offset = offset;
809
idir.size = size - 1;
811
memcpy((void *) idirp, (void *) &idir, sizeof(idir));
813
SQUASHFS_SWAP_DIR_ENTRY((&idir), idirp);
814
strncpy(idirp->name, name, size);
815
dir->p += sizeof(squashfs_dir_entry) + size;
820
squashfs_inode write_dir(char *filename, struct directory *dir)
822
squashfs_inode inode;
823
unsigned int dir_size;
825
unsigned short c_byte;
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) {
833
directory_size += SQUASHFS_METADATA_SIZE << 1;
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);
840
memcpy((void *) directory_table + directory_bytes, (void *) &c_byte, sizeof(unsigned short));
842
SQUASHFS_SWAP_SHORTS((&c_byte), (directory_table + directory_bytes), 1);
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;
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;
856
if((directory_data_cache = (char *) realloc(directory_data_cache, directory_cache_size + realloc_size)) == NULL) {
859
directory_cache_size += realloc_size;
863
squashfs_dir_header dir_header;
865
dir_header.count = dir->entry_count - 1;
866
dir_header.start_block = dir->start_block;
868
memcpy((void *) dir->entry_count_p, (void *) &dir_header, sizeof(dir_header));
870
SQUASHFS_SWAP_DIR_HEADER((&dir_header), dir->entry_count_p);
871
memcpy(directory_data_cache + directory_cache_bytes, dir->buff, dir_size);
874
inode = create_inode(filename, SQUASHFS_DIR_TYPE, dir_size, directory_bytes, directory_cache_bytes, NULL, NULL, NULL);
876
directory_cache_bytes += dir_size;
878
#ifdef SQUASHFS_TRACE
883
TRACE("Directory contents of inode 0x%Lx\n", inode);
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);
892
TRACE("\tStart block 0x%x, count %d\n", dirh->start_block, 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;
911
BAD_ERROR("Out of memory in directory table reallocation!\n");
915
char *get_fragment(char *buffer, struct fragment *fragment)
917
if(fragment->index == fragments || fragment->index == SQUASHFS_INVALID_BLK)
918
return fragment_data + fragment->offset;
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;
924
if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) {
925
char cbuffer[block_size];
927
read_bytes(fd, disk_fragment->start_block, size, cbuffer);
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");
935
BAD_ERROR("zlib::uncompress failed, unknown error %d\n", res);
938
read_bytes(fd, disk_fragment->start_block, size, buffer);
939
return buffer + fragment->offset;
944
void write_fragment()
947
unsigned char buffer[block_size << 1];
949
if(fragment_size == 0)
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);
968
static struct fragment empty_fragment = {SQUASHFS_INVALID_BLK, 0, 0};
969
struct fragment *get_and_fill_fragment(char *buff, int size)
971
struct fragment *ffrg;
974
return &empty_fragment;
976
if(fragment_size + size > block_size)
979
if((ffrg = (struct fragment *) malloc(sizeof(struct fragment))) == NULL)
980
BAD_ERROR("Out of memory in fragment block allocation!\n");
982
ffrg->index = fragments;
983
ffrg->offset = fragment_size;
985
memcpy(fragment_data + fragment_size, buff, size);
986
fragment_size += size;
992
unsigned int write_fragment_table()
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];
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);
1006
memcpy((void *) p, &fragment_table[i], sizeof(squashfs_fragment_entry));
1008
SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_table[i], p);
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);
1015
memcpy((void *) cbuffer, (void *) &c_byte, sizeof(unsigned short));
1017
SQUASHFS_SWAP_SHORTS((&c_byte), cbuffer, 1);
1019
*((unsigned char *)(cbuffer + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
1021
compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
1022
write_bytes(fd, bytes, compressed_size, cbuffer);
1023
bytes += compressed_size;
1027
write_bytes(fd, bytes, sizeof(list), (char *) list);
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);
1034
start_bytes = bytes;
1035
bytes += sizeof(list);
1041
unsigned char *read_from_buffer(struct duplicate_buffer_handle *handle, unsigned int avail_bytes)
1043
unsigned char *v = handle->ptr;
1044
handle->ptr += avail_bytes;
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)
1052
read_bytes(fd, handle->start, avail_bytes, read_from_file_buffer);
1053
handle->start += avail_bytes;
1054
return read_from_file_buffer;
1059
* Compute 16 bit BSD checksum over the data
1061
unsigned short get_checksum(unsigned char *(get_next_file_block)(struct duplicate_buffer_handle *, unsigned int), struct duplicate_buffer_handle *handle, int l)
1063
unsigned short chksum = 0;
1064
unsigned int bytes = 0;
1066
struct duplicate_buffer_handle position = *handle;
1069
bytes = l > SQUASHFS_FILE_MAX_SIZE ? SQUASHFS_FILE_MAX_SIZE : l;
1071
b = get_next_file_block(&position, bytes);
1073
chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1;
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)
1085
struct fragment *frg;
1086
struct file_info *dupl_ptr;
1088
struct duplicate_buffer_handle handle;
1090
if(!duplicate_checking)
1093
if((frg = (struct fragment *) malloc(sizeof(struct fragment))) == NULL)
1094
BAD_ERROR("Out of memory in fragment block allocation!\n");
1096
frg->index = fragment;
1097
frg->offset = offset;
1099
if(cached_frag == fragment)
1100
datap = fragment_data + offset;
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;
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)
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];
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;
1125
int avail_bytes = dup_bytes > SQUASHFS_FILE_MAX_SIZE ? SQUASHFS_FILE_MAX_SIZE : dup_bytes;
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)
1131
dup_bytes -= avail_bytes;
1132
dup_start += avail_bytes;
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;
1149
if((dupl_ptr = (struct file_info *) malloc(sizeof(struct file_info))) == NULL) {
1150
BAD_ERROR("Out of memory in dup_files allocation!\n");
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");
1161
memcpy(dupl_ptr->block_list, *block_list, blocks * sizeof(unsigned int));
1164
dupl_ptr->next = dupl[checksum];
1165
dupl[checksum] = dupl_ptr;
1167
dupl_ptr->next = frag_dups[fragment_checksum];
1168
frag_dups[fragment_checksum] = dupl_ptr;
1175
#define MINALLOCBYTES (1024 * 1024)
1176
squashfs_inode write_file(char *filename, long long size, int *duplicate_file)
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;
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;
1195
if(size > read_size)
1196
ERROR("file %s truncated to %Ld bytes\n", filename, SQUASHFS_MAX_FILE_SIZE);
1198
total_bytes += read_size;
1199
if((file = open(filename, O_RDONLY)) == -1) {
1200
perror("Error in opening file, skipping...");
1201
return SQUASHFS_INVALID;
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);
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;
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)
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);
1224
write_bytes(fd, bytes, bbytes, c_buffer);
1230
if(read(file, buff, frag_bytes) == -1)
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;
1240
write_bytes(fd, bytes, file_bytes, c_buffer);
1241
bytes += file_bytes;
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) {
1247
ftruncate(fd, bytes);
1248
*duplicate_file = TRUE;
1253
fragment = get_and_fill_fragment(buff, frag_bytes);
1254
if(duplicate_checking)
1255
dupl_ptr->fragment = fragment;
1257
*duplicate_file = FALSE;
1262
return create_inode(filename, SQUASHFS_FILE_TYPE, read_size, start, blocks, block_listp, fragment, NULL);
1265
perror("Error in reading file, skipping...");
1267
return SQUASHFS_INVALID;
1273
char pathname[8192];
1277
void *linux_opendir(char *pathname, struct directory *dir)
1279
struct linuxdir *linuxdir;
1280
if((linuxdir = malloc(sizeof(struct linuxdir))) == NULL)
1282
if((linuxdir->linuxdir = opendir(pathname)) == NULL) {
1286
strcpy(linuxdir->pathname, pathname);
1287
return (void *) linuxdir;
1291
int linux_readdir(void *l, char *filename, char *dir_name, struct stat* devtable_inode_info)
1293
struct dirent *d_name;
1294
struct linuxdir *linuxdir = (struct linuxdir *)l;
1296
devtable_inode_info->st_mode = 0;
1299
if((d_name = readdir(linuxdir->linuxdir)) == NULL)
1301
if ( strncmp(linuxdir->pathname, dev_path, sizeof(linuxdir->pathname)) == 0 )
1303
if ( devtable_readdir(l, filename, dir_name, devtable_inode_info ) )
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);
1317
void linux_closedir(void *linuxdir)
1319
closedir(((struct linuxdir *)linuxdir)->linuxdir);
1324
static int interpret_table_entry(char *line, char* filename, char* dirname, struct stat* devtable_inode_info)
1326
char type, *name = NULL;
1328
unsigned long mode = 0755, uid = 0, gid = 0, major = 0, minor = 0;
1329
unsigned long start = 0, increment = 1, count = 0;
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)
1338
if (!strcmp(name, "/")) {
1339
error_msg_and_die("Device table entries require absolute paths");
1342
strcpy( filename, source_path[0] );
1343
strcat( filename, name );
1345
full = strdup( name );
1346
strcpy( dirname, (char*)basename(full) );
1361
mode |= (type == 'c') ? S_IFCHR : S_IFBLK;
1367
for (i = start; i < count; i++) {
1368
asprintf(&buf, "%s%lu", name, i);
1369
rdev = makedev(major, minor + (i * increment - start));
1371
devtable_inode_info->st_rdev = rdev;
1374
dev_t rdev = makedev(major, minor);
1375
devtable_inode_info->st_rdev = rdev;
1379
error_msg_and_die("Unsupported file type");
1383
devtable_inode_info->st_mode = mode;
1388
int devtable_readdir(void *l, char *filename, char *dir_name, struct stat* devtable_inode_info)
1392
int commentLine = FALSE;
1395
strcpy(filename, "");
1396
strcpy(dir_name, "");
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... */
1406
if (getline(&line, &length, devtable) != -1) {
1408
/* First trim off any whitespace */
1409
int len = strlen(line);
1411
/* trim trailing whitespace */
1412
while (len > 0 && isspace(line[len - 1]))
1414
/* trim leading whitespace */
1415
memmove(line, &line[strspn(line, " \n\r\t\v")], len);
1417
/* How long are we after trimming? */
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))
1439
} while (commentLine == TRUE);
1446
char b_buffer[8192];
1450
char *getbase(char *pathname)
1454
if(*pathname != '/') {
1455
result = getenv("PWD");
1456
strcat(strcat(strcpy(b_buffer, result), "/"), pathname);
1458
strcpy(b_buffer, pathname);
1460
if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0))
1478
while(*name != '\0' && *name != '/') name++;
1481
while(*name == '/') name++;
1482
if(strncmp(s, ".", n) == 0)
1484
if((*name == '\0') || (strncmp(s, "..", n) == 0) || ((p = basename_r()) == NULL)) {
1488
if(strcmp(p, "..") == 0)
1495
char encomp_pathname[8192];
1496
int encomp_entry = 0;
1498
void *encomp_opendir(char *pathname, struct directory *dir)
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);
1505
return (void *)source_path;
1509
int encomp_readdir(void *l, char *filename, char *dir_name, struct stat* devtable_inode_info)
1514
devtable_inode_info->st_mode = 0;
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]);
1522
strcpy(filename, source_path[encomp_entry]);
1523
strcpy(dir_name, basename);
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);
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);
1540
void encomp_closedir(void *linuxdir)
1545
void *single_opendir(char *pathname, struct directory *dir)
1547
encomp_opendir(pathname, dir);
1548
return linux_opendir(pathname, dir);
1552
int single_readdir(void *l, char *filename, char *dir_name, struct stat* devtable_inode_info)
1555
char name[SQUASHFS_NAME_LEN + 1];
1557
devtable_inode_info->st_mode = 0;
1559
if(linux_readdir(l, filename, dir_name, devtable_inode_info) == FALSE)
1562
strcpy(name, dir_name);
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);
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);
1576
squashfs_inode dir_scan(char *pathname, void* (_opendir)(char *, struct directory *), int (_readdir)(void *, char *, char *, struct stat*),
1577
void (_closedir)(void *))
1581
char filename[8192], dir_name[8192];
1583
squashfs_inode inode = SQUASHFS_INVALID;
1584
struct directory dir;
1586
struct stat devtable_inode_info;
1588
devtable_inode_info.st_mode = 0;
1591
if((linuxdir = _opendir(pathname, &dir)) == NULL) {
1592
ERROR("Could not open %s, skipping...\n", pathname);
1596
while(_readdir(linuxdir, filename, dir_name, &devtable_inode_info) != FALSE) {
1598
mode = devtable_inode_info.st_mode;
1600
if ( devtable_inode_info.st_mode == 0 ) {
1601
if(lstat(filename, &buf) == -1) {
1603
sprintf(buffer, "Cannot stat dir/file %s, ignoring", filename);
1610
if(excluded(filename, &buf))
1613
switch(mode & S_IFMT) {
1617
squashfs_type = SQUASHFS_FILE_TYPE;
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" : "");
1622
inode = get_sorted_inode(&buf);
1626
squashfs_type = SQUASHFS_DIR_TYPE;
1627
inode = dir_scan(filename, linux_opendir, linux_readdir, linux_closedir);
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);
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);
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);
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);
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);
1666
ERROR("%s unrecognised file type, mode is %x\n", filename, mode);
1670
if(inode != SQUASHFS_INVALID)
1671
add_dir(inode, dir_name, squashfs_type, &dir);
1674
_closedir(linuxdir);
1675
inode = write_dir(pathname, &dir);
1676
INFO("directory %s inode 0x%Lx\n", pathname, inode);
1685
unsigned int slog(unsigned int block)
1689
for(i = 9; i <= 16; i++)
1690
if(block == (1 << i))
1696
int excluded(char *filename, struct stat *buf)
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))
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");\
1712
exclude_paths[exclude].st_dev = buf.st_dev;\
1713
exclude_paths[exclude++].st_ino = buf.st_ino;
1714
int add_exclude(char *path)
1717
char buffer[4096], filename[4096];
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);
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);
1745
void add_old_root_entry(char *name, squashfs_inode inode, int type)
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");
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;
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[])
1772
squashfs_super_block sBlk;
1773
char *b, *root_name = NULL;
1774
int be, nopad = FALSE, delete = FALSE, keep_as_directory = FALSE, orig_be;
1776
#if __BYTE_ORDER == __BIG_ENDIAN
1782
block_log = slog(block_size);
1783
if(argc > 1 && strcmp(argv[1], "-version") == 0) {
1787
for(i = 1; i < argc && argv[i][0] != '-'; i++);
1790
source_path = argv + 1;
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]);
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]);
1803
} else if(strcmp(argv[i], "-ef") == 0) {
1805
ERROR("%s: -ef missing filename\n", argv[0]);
1808
} else if(strcmp(argv[i], "-D") == 0) {
1809
TRACE("-D passed as parameter\n");
1811
ERROR("%s: -D missing filename\n", argv[0]);
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;
1823
else if(strcmp(argv[i], "-no-fragments") == 0)
1824
no_fragments = TRUE;
1826
else if(strcmp(argv[i], "-always-use-fragments") == 0)
1827
always_use_fragments = TRUE;
1829
else if(strcmp(argv[i], "-sort") == 0) {
1831
ERROR("%s: -sort missing filename\n", argv[0]);
1834
} else if(strcmp(argv[i], "-all-root") == 0 ||
1835
strcmp(argv[i], "-root-owned") == 0)
1836
global_uid = global_gid = 0;
1838
else if(strcmp(argv[i], "-force-uid") == 0) {
1840
ERROR("%s: -force-uid missing uid or user\n", argv[0]);
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]);
1849
struct passwd *uid = getpwnam(argv[i]);
1851
global_uid = uid->pw_uid;
1853
ERROR("%s: -force-uid invalid uid or unknown user\n", argv[0]);
1857
} else if(strcmp(argv[i], "-force-gid") == 0) {
1859
ERROR("%s: -force-gid missing gid or group\n", argv[0]);
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]);
1868
struct group *gid = getgrnam(argv[i]);
1870
global_gid = gid->gr_gid;
1872
ERROR("%s: -force-gid invalid gid or unknown group\n", argv[0]);
1876
} else if(strcmp(argv[i], "-noI") == 0 ||
1877
strcmp(argv[i], "-noInodeCompression") == 0)
1880
else if(strcmp(argv[i], "-noD") == 0 ||
1881
strcmp(argv[i], "-noDataCompression") == 0)
1884
else if(strcmp(argv[i], "-noF") == 0 ||
1885
strcmp(argv[i], "-noFragmentCompression") == 0)
1888
else if(strcmp(argv[i], "-nopad") == 0)
1891
else if(strcmp(argv[i], "-check_data") == 0)
1894
else if(strcmp(argv[i], "-info") == 0)
1897
else if(strcmp(argv[i], "-be") == 0)
1900
else if(strcmp(argv[i], "-le") == 0)
1903
else if(strcmp(argv[i], "-gzip") == 0)
1904
compress_algorithm = GZIP;
1906
else if(strcmp(argv[i], "-lzma") == 0)
1907
compress_algorithm = LZMA;
1910
else if(strcmp(argv[i], "-e") == 0)
1913
else if(strcmp(argv[i], "-noappend") == 0)
1916
else if(strcmp(argv[i], "-keep-as-directory") == 0)
1917
keep_as_directory = TRUE;
1919
else if(strcmp(argv[i], "-root-becomes") == 0) {
1921
ERROR("%s: -root-becomes: missing name\n", argv[0]);
1924
root_name = argv[i];
1925
} else if(strcmp(argv[i], "-version") == 0) {
1928
ERROR("%s: invalid option\n\n", argv[0]);
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");
1955
ERROR("\t-gzip\t\t\t\tuse gzip compression\n");
1956
ERROR("\t-lzma\t\t\t\tuse lzma compression(default)\n");
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");
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");
1976
perror("Could not stat destination file");
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");
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");
1995
ERROR("Destination not block device or regular file\n");
2000
if(read_super(fd, &sBlk, &orig_be, argv[source + 1]) == 0) {
2001
if(S_ISREG(buf.st_mode)) { /* reopen truncating file */
2003
if((fd = open(argv[source + 1], O_TRUNC | O_RDWR)) == -1) {
2004
perror("Could not open regular file for writing as destination");
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) {
2018
char filename[16385];
2019
if((fd = fopen(argv[++i], "r")) == NULL) {
2020
perror("Could not open exclude file...");
2023
while(fscanf(fd, "%16384[^\n]\n", filename) != EOF)
2024
add_exclude(filename);
2026
} else if(strcmp(argv[i], "-e") == 0)
2028
else if(strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "-root-becomes") == 0 || strcmp(argv[i], "-sort") == 0)
2033
ERROR("%s: -e missing arguments\n", argv[0]);
2036
while(i < argc && add_exclude(argv[i++]));
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);
2044
} else if(strcmp(argv[i], "-e") == 0)
2046
else if(strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "-root-becomes") == 0 || strcmp(argv[i], "-ef") == 0)
2049
if((fragment_data = (char *) malloc(block_size)) == NULL)
2050
BAD_ERROR("Out of memory allocating fragment_data");
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);
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);
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");
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));
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");
2085
inode_size = inode_bytes;
2086
directory_size = directory_bytes;
2088
/* save original filesystem state for restoring ... */
2089
sfragments = fragments;
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;
2111
goto restore_filesystem;
2112
signal(SIGTERM, sighandler);
2113
signal(SIGINT, sighandler);
2114
write_bytes(fd, SQUASHFS_START, 4, "\0\0\0\0");
2117
#if __BYTE_ORDER == __BIG_ENDIAN
2123
block_offset = check_data ? 3 : 2;
2125
if(stat(source_path[0], &buf) == -1) {
2126
perror("Cannot stat source directory");
2130
snprintf(dev_path, sizeof(dev_path), "%s/dev", source_path[0]);
2133
sort_files_and_write(source, source_path);
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);
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);
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();
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);
2161
if(sBlk.no_uids = uid_count) {
2163
write_bytes(fd, bytes, uid_count * sizeof(squashfs_uid), (char *) uids);
2165
squashfs_uid uids_copy[uid_count];
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);
2170
sBlk.uid_start = bytes;
2171
bytes += uid_count * sizeof(squashfs_uid);
2175
if(sBlk.no_guids = guid_count) {
2177
write_bytes(fd, bytes, guid_count * sizeof(squashfs_uid), (char *) guids);
2179
squashfs_uid guids_copy[guid_count];
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);
2184
sBlk.guid_start = bytes;
2185
bytes += guid_count * sizeof(squashfs_uid);
2187
sBlk.guid_start = 0;
2189
sBlk.bytes_used = bytes;
2192
write_bytes(fd, SQUASHFS_START, sizeof(squashfs_super_block), (char *) &sBlk);
2194
squashfs_super_block sBlk_copy;
2196
SQUASHFS_SWAP_SUPER_BLOCK((&sBlk), &sBlk_copy);
2197
write_bytes(fd, SQUASHFS_START, sizeof(squashfs_super_block), (char *) &sBlk_copy);
2200
if(!nopad && (i = bytes & (4096 - 1))) {
2201
unsigned char temp[4096] = {0};
2202
write_bytes(fd, bytes, 4096 - i, temp);
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);
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);
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);
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);
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]);
2243
printf("Number of gids %d\n", guid_count);
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]);