BCM WL 6.30.102.9 (r366174)
[tomato.git] / release / src-rt / linux / linux-2.6 / scripts / squashfs / mksquashfs.c
blobe773f53cb9a3abc5365294b776da7c037ea35ade
1 /*
2 * Create a squashfs filesystem. This is a highly compressed read only filesystem.
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006
5 * Phillip Lougher <phillip@lougher.org.uk>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * mksquashfs.c
24 #define FALSE 0
25 #define TRUE 1
27 #include <pwd.h>
28 #include <grp.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <dirent.h>
37 #include <string.h>
38 #include <zlib.h>
39 #include <stdlib.h>
40 #include <signal.h>
41 #include <setjmp.h>
42 #include <sys/mman.h>
44 #ifndef linux
45 #define __BYTE_ORDER BYTE_ORDER
46 #define __BIG_ENDIAN BIG_ENDIAN
47 #define __LITTLE_ENDIAN LITTLE_ENDIAN
48 #else
49 #include <endian.h>
50 #endif
52 #include <squashfs_fs.h>
53 #include "mksquashfs.h"
54 #include "global.h"
55 #include "sort.h"
57 #ifdef SQUASHFS_TRACE
58 #define TRACE(s, args...) do { \
59 printf("mksquashfs: "s, ## args); \
60 } while(0)
61 #else
62 #define TRACE(s, args...)
63 #endif
65 #define INFO(s, args...) do {\
66 if(!silent)\
67 printf("mksquashfs: "s, ## args);\
68 } while(0)
69 #define ERROR(s, args...) do {\
70 fprintf(stderr, s, ## args);\
71 } while(0)
72 #define EXIT_MKSQUASHFS() do {\
73 if(restore)\
74 restorefs();\
75 if(delete && destination_file && !block_device)\
76 unlink(destination_file);\
77 exit(1);\
78 } while(0)
79 #define BAD_ERROR(s, args...) do {\
80 fprintf(stderr, "FATAL ERROR:" s, ##args);\
81 EXIT_MKSQUASHFS();\
82 } while(0)
84 int delete = FALSE;
85 long long total_compressed = 0, total_uncompressed = 0;
86 int fd;
88 /* filesystem flags for building */
89 int duplicate_checking = 1, noF = 0, no_fragments = 0, always_use_fragments = 0;
90 int noI = 0, noD = 0, check_data = 0;
91 int swap, silent = TRUE;
92 long long global_uid = -1, global_gid = -1;
94 /* superblock attributes */
95 int block_size = SQUASHFS_FILE_SIZE, block_log;
96 unsigned short uid_count = 0, guid_count = 0;
97 squashfs_uid uids[SQUASHFS_UIDS], guids[SQUASHFS_GUIDS];
98 int block_offset;
99 int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0, sock_count = 0;
101 /* write position within data section */
102 long long bytes = 0, total_bytes = 0;
104 /* in memory directory table - possibly compressed */
105 char *directory_table = NULL;
106 unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0;
108 /* cached directory table */
109 char *directory_data_cache = NULL;
110 unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
112 /* in memory inode table - possibly compressed */
113 char *inode_table = NULL;
114 unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0;
116 /* cached inode table */
117 char *data_cache = NULL;
118 unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
120 /* in memory directory data */
121 #define I_COUNT_SIZE 128
122 #define DIR_ENTRIES 32
123 #define INODE_HASH_SIZE 65536
124 #define INODE_HASH_MASK (INODE_HASH_SIZE - 1)
125 #define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK)
127 struct cached_dir_index {
128 squashfs_dir_index index;
129 char *name;
132 struct directory {
133 unsigned int start_block;
134 unsigned int size;
135 unsigned char *buff;
136 unsigned char *p;
137 unsigned int entry_count;
138 unsigned char *entry_count_p;
139 unsigned int i_count;
140 unsigned int i_size;
141 struct cached_dir_index *index;
142 unsigned char *index_count_p;
143 unsigned int inode_number;
146 struct inode_info *inode_info[INODE_HASH_SIZE];
148 /* hash tables used to do fast duplicate searches in duplicate check */
149 struct file_info *dupl[65536], *frag_dups[65536];
150 int dup_files = 0;
152 /* list of exclude dirs/files */
153 struct exclude_info {
154 dev_t st_dev;
155 ino_t st_ino;
158 #define EXCLUDE_SIZE 8192
159 int exclude = 0;
160 struct exclude_info *exclude_paths = NULL;
161 int excluded(char *filename, struct stat *buf);
163 /* fragment block data structures */
164 int fragments = 0;
165 char fragment_data[SQUASHFS_FILE_SIZE];
166 int fragment_size = 0;
167 struct fragment {
168 unsigned int index;
169 int offset;
170 int size;
172 #define FRAG_SIZE 32768
173 squashfs_fragment_entry *fragment_table = NULL;
176 /* current inode number for directories and non directories */
177 unsigned int dir_inode_no = 1;
178 unsigned int inode_no = 0;
179 unsigned int root_inode_number = 0;
181 /* list of source dirs/files */
182 int source = 0;
183 char **source_path;
185 /* list of root directory entries read from original filesystem */
186 int old_root_entries = 0;
187 struct old_root_entry_info {
188 char name[SQUASHFS_NAME_LEN + 1];
189 squashfs_inode inode;
190 int type;
191 int inode_number;
193 struct old_root_entry_info *old_root_entry;
195 /* in memory file info */
196 struct file_info {
197 long long bytes;
198 unsigned short checksum;
199 long long start;
200 unsigned int *block_list;
201 struct file_info *next;
202 struct fragment *fragment;
203 unsigned short fragment_checksum;
206 /* count of how many times SIGINT or SIGQUIT has been sent */
207 int interrupted = 0;
209 /* restore orignal filesystem state if appending to existing filesystem is cancelled */
210 jmp_buf env;
211 char *sdata_cache, *sdirectory_data_cache;
213 long long sbytes, stotal_bytes;
215 unsigned int sinode_bytes, scache_bytes, sdirectory_bytes,
216 sdirectory_cache_bytes, suid_count, sguid_count,
217 stotal_inode_bytes, stotal_directory_bytes,
218 sinode_count, sfile_count, ssym_count, sdev_count,
219 sdir_count, sfifo_count, ssock_count, sdup_files;
220 int sfragments;
221 int restore = 0;
223 /* flag whether destination file is a block device */
224 int block_device = 0;
226 /* flag indicating whether files are sorted using sort list(s) */
227 int sorted = 0;
229 /* save destination file name for deleting on error */
230 char *destination_file = NULL;
232 /* structure to used to pass in a pointer or an integer
233 * to duplicate buffer read helper functions.
235 struct duplicate_buffer_handle {
236 char *ptr;
237 long long start;
240 void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, int type);
241 extern int read_super(int fd, squashfs_super_block *sBlk, int *be, char *source);
242 extern long long read_filesystem(char *root_name, int fd, squashfs_super_block *sBlk, char **cinode_table,
243 char **data_cache, char **cdirectory_table, char **directory_data_cache,
244 unsigned int *last_directory_block, unsigned int *inode_dir_offset, unsigned int *inode_dir_file_size,
245 unsigned int *root_inode_size, unsigned int *inode_dir_start_block, int *file_count, int *sym_count,
246 int *dev_count, int *dir_count, int *fifo_count, int *sock_count, squashfs_uid *uids,
247 unsigned short *uid_count, squashfs_uid *guids, unsigned short *guid_count,
248 long long *uncompressed_file, unsigned int *uncompressed_inode, unsigned int *uncompressed_directory,
249 unsigned int *inode_dir_inode_number, unsigned int *inode_dir_parent_inode,
250 void (push_directory_entry)(char *, squashfs_inode, int, int),
251 squashfs_fragment_entry **fragment_table);
252 int get_sorted_inode(squashfs_inode *inode, struct stat *buf);
253 int read_sort_file(char *filename, int source, char *source_path[]);
254 void sort_files_and_write(struct dir_info *dir);
255 struct file_info *duplicate(char *(get_next_file_block)(struct duplicate_buffer_handle *, unsigned int), struct duplicate_buffer_handle *file_start, long long bytes, unsigned int **block_list, long long *start, int blocks, struct fragment **fragment, char *frag_data, int frag_bytes);
256 struct dir_info *dir_scan1(char *, int (_readdir)(char *, char *, struct dir_info *));
258 #define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) + (((char *)A) - data_cache)))
261 void restorefs()
263 ERROR("Exiting - restoring original filesystem!\n\n");
264 bytes = sbytes;
265 memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes);
266 memcpy(directory_data_cache, sdirectory_data_cache, directory_cache_bytes = sdirectory_cache_bytes);
267 inode_bytes = sinode_bytes;
268 directory_bytes = sdirectory_bytes;
269 uid_count = suid_count;
270 guid_count = sguid_count;
271 total_bytes = stotal_bytes;
272 total_inode_bytes = stotal_inode_bytes;
273 total_directory_bytes = stotal_directory_bytes;
274 inode_count = sinode_count;
275 file_count = sfile_count;
276 sym_count = ssym_count;
277 dev_count = sdev_count;
278 dir_count = sdir_count;
279 fifo_count = sfifo_count;
280 sock_count = ssock_count;
281 dup_files = sdup_files;
282 fragments = sfragments;
283 fragment_size = 0;
284 longjmp(env, 1);
288 void sighandler()
290 if(interrupted == 1)
291 restorefs();
292 else {
293 ERROR("Interrupting will restore original filesystem!\n");
294 ERROR("Interrupt again to quit\n");
295 interrupted ++;
300 void sighandler2()
302 EXIT_MKSQUASHFS();
306 unsigned int mangle(char *d, char *s, int size, int block_size, int uncompressed, int data_block)
308 unsigned long c_byte = block_size << 1;
309 unsigned int res;
311 if(!uncompressed && (res = compress2((unsigned char *) d, &c_byte, (unsigned char *) s, size, 9)) != Z_OK) {
312 if(res == Z_MEM_ERROR)
313 BAD_ERROR("zlib::compress failed, not enough memory\n");
314 else if(res == Z_BUF_ERROR)
315 BAD_ERROR("zlib::compress failed, not enough room in output buffer\n");
316 else
317 BAD_ERROR("zlib::compress failed, unknown error %d\n", res);
318 return 0;
321 if(uncompressed || c_byte >= size) {
322 memcpy(d, s, size);
323 return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : SQUASHFS_COMPRESSED_BIT);
326 return (unsigned int) c_byte;
330 squashfs_base_inode_header *get_inode(int req_size)
332 int data_space;
333 unsigned short c_byte;
335 while(cache_bytes >= SQUASHFS_METADATA_SIZE) {
336 if((inode_size - inode_bytes) < ((SQUASHFS_METADATA_SIZE << 1)) + 2) {
337 if((inode_table = (char *) realloc(inode_table, inode_size + (SQUASHFS_METADATA_SIZE << 1) + 2))
338 == NULL) {
339 goto failed;
341 inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
344 c_byte = mangle(inode_table + inode_bytes + block_offset, data_cache,
345 SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, noI, 0);
346 TRACE("Inode block @ %x, size %d\n", inode_bytes, c_byte);
347 if(!swap)
348 memcpy(inode_table + inode_bytes, &c_byte, sizeof(unsigned short));
349 else
350 SQUASHFS_SWAP_SHORTS((&c_byte), (inode_table + inode_bytes), 1);
351 if(check_data)
352 *((unsigned char *)(inode_table + inode_bytes + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
353 inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
354 total_inode_bytes += SQUASHFS_METADATA_SIZE + block_offset;
355 memcpy(data_cache, data_cache + SQUASHFS_METADATA_SIZE, cache_bytes - SQUASHFS_METADATA_SIZE);
356 cache_bytes -= SQUASHFS_METADATA_SIZE;
359 data_space = (cache_size - cache_bytes);
360 if(data_space < req_size) {
361 int realloc_size = cache_size == 0 ? ((req_size + SQUASHFS_METADATA_SIZE) & ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - data_space;
363 if((data_cache = (char *) realloc(data_cache, cache_size + realloc_size)) == NULL) {
364 goto failed;
366 cache_size += realloc_size;
369 cache_bytes += req_size;
371 return (squashfs_base_inode_header *)(data_cache + (cache_bytes - req_size));
373 failed:
374 BAD_ERROR("Out of memory in inode table reallocation!\n");
378 void read_bytes(int fd, long long byte, int bytes, char *buff)
380 off_t off = byte;
382 if(lseek(fd, off, SEEK_SET) == -1) {
383 perror("Lseek on destination failed");
384 EXIT_MKSQUASHFS();
387 if(read(fd, buff, bytes) == -1) {
388 perror("Read on destination failed");
389 EXIT_MKSQUASHFS();
394 void write_bytes(int fd, long long byte, int bytes, char *buff)
396 off_t off = byte;
398 if(lseek(fd, off, SEEK_SET) == -1) {
399 perror("Lseek on destination failed");
400 EXIT_MKSQUASHFS();
403 if(write(fd, buff, bytes) == -1) {
404 perror("Write on destination failed");
405 EXIT_MKSQUASHFS();
410 long long write_inodes()
412 unsigned short c_byte;
413 int avail_bytes;
414 char *datap = data_cache;
415 long long start_bytes = bytes;
417 while(cache_bytes) {
418 if(inode_size - inode_bytes < ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
419 if((inode_table = (char *) realloc(inode_table, inode_size + ((SQUASHFS_METADATA_SIZE << 1) + 2))) == NULL) {
420 BAD_ERROR("Out of memory in inode table reallocation!\n");
422 inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
424 avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? SQUASHFS_METADATA_SIZE : cache_bytes;
425 c_byte = mangle(inode_table + inode_bytes + block_offset, datap, avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0);
426 TRACE("Inode block @ %x, size %d\n", inode_bytes, c_byte);
427 if(!swap)
428 memcpy(inode_table + inode_bytes, &c_byte, sizeof(unsigned short));
429 else
430 SQUASHFS_SWAP_SHORTS((&c_byte), (inode_table + inode_bytes), 1);
431 if(check_data)
432 *((unsigned char *)(inode_table + inode_bytes + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
433 inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
434 total_inode_bytes += avail_bytes + block_offset;
435 datap += avail_bytes;
436 cache_bytes -= avail_bytes;
439 write_bytes(fd, bytes, inode_bytes, (char *) inode_table);
440 bytes += inode_bytes;
442 return start_bytes;
446 long long write_directories()
448 unsigned short c_byte;
449 int avail_bytes;
450 char *directoryp = directory_data_cache;
451 long long start_bytes = bytes;
453 while(directory_cache_bytes) {
454 if(directory_size - directory_bytes < ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
455 if((directory_table = (char *) realloc(directory_table, directory_size +
456 ((SQUASHFS_METADATA_SIZE << 1) + 2))) == NULL) {
457 BAD_ERROR("Out of memory in directory table reallocation!\n");
459 directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
461 avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? SQUASHFS_METADATA_SIZE : directory_cache_bytes;
462 c_byte = mangle(directory_table + directory_bytes + block_offset, directoryp, avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0);
463 TRACE("Directory block @ %x, size %d\n", directory_bytes, c_byte);
464 if(!swap)
465 memcpy(directory_table + directory_bytes, &c_byte, sizeof(unsigned short));
466 else
467 SQUASHFS_SWAP_SHORTS((&c_byte), (directory_table + directory_bytes), 1);
468 if(check_data)
469 *((unsigned char *)(directory_table + directory_bytes + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
470 directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
471 total_directory_bytes += avail_bytes + block_offset;
472 directoryp += avail_bytes;
473 directory_cache_bytes -= avail_bytes;
475 write_bytes(fd, bytes, directory_bytes, (char *) directory_table);
476 bytes += directory_bytes;
478 return start_bytes;
482 unsigned int get_uid(squashfs_uid uid)
484 int i;
486 for(i = 0; (i < uid_count) && uids[i] != uid; i++);
487 if(i == uid_count) {
488 if(uid_count == SQUASHFS_UIDS) {
489 ERROR("Out of uids! - using uid 0 - probably not what's wanted!\n");
490 i = 0;
491 } else
492 uids[uid_count++] = uid;
495 return i;
499 unsigned int get_guid(squashfs_uid uid, squashfs_uid guid)
501 int i;
503 if(uid == guid)
504 return SQUASHFS_GUIDS;
506 for(i = 0; (i < guid_count) && guids[i] != guid; i++);
507 if(i == guid_count) {
508 if(guid_count == SQUASHFS_GUIDS) {
509 ERROR("Out of gids! - using gid 0 - probably not what's wanted!\n");
510 return SQUASHFS_GUIDS;
511 } else
512 guids[guid_count++] = guid;
515 return i;
519 int create_inode(squashfs_inode *i_no, struct dir_ent *dir_ent, int type, long long byte_size, long long start_block, unsigned int offset, unsigned int *block_list, struct fragment *fragment, struct directory *dir_in)
521 struct stat *buf = &dir_ent->inode->buf;
522 squashfs_inode_header inode_header;
523 squashfs_base_inode_header *inode, *base = &inode_header.base;
524 char *filename = dir_ent->pathname;
525 int nlink = dir_ent->inode->nlink;
526 int inode_number = (type == SQUASHFS_LDIR_TYPE || type == SQUASHFS_DIR_TYPE) ? dir_ent->inode->inode_number : dir_ent->inode->inode_number + dir_inode_no;
528 base->mode = SQUASHFS_MODE(buf->st_mode);
529 base->uid = get_uid((squashfs_uid) global_uid == -1 ? buf->st_uid : global_uid);
530 base->inode_type = type;
531 base->guid = get_guid((squashfs_uid) global_uid == -1 ? buf->st_uid : global_uid, (squashfs_uid) global_gid == -1 ? buf->st_gid : global_gid);
532 base->mtime = buf->st_mtime;
533 base->inode_number = inode_number;
535 if(type == SQUASHFS_FILE_TYPE) {
536 int i;
537 squashfs_reg_inode_header *reg = &inode_header.reg, *inodep;
539 inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
540 inodep = (squashfs_reg_inode_header *) inode;
541 reg->file_size = byte_size;
542 reg->start_block = start_block;
543 reg->fragment = fragment->index;
544 reg->offset = fragment->offset;
545 if(!swap) {
546 memcpy(inodep, reg, sizeof(*reg));
547 memcpy(inodep->block_list, block_list, offset * sizeof(unsigned int));
548 } else {
549 SQUASHFS_SWAP_REG_INODE_HEADER(reg, inodep);
550 SQUASHFS_SWAP_INTS(block_list, inodep->block_list, offset);
552 TRACE("File inode, file_size %d, start_block %llx, blocks %d, fragment %d, offset %d, size %d\n", (int) byte_size,
553 start_block, offset, fragment->index, fragment->offset, fragment->size);
554 for(i = 0; i < offset; i++)
555 TRACE("Block %d, size %d\n", i, block_list[i]);
557 else if(type == SQUASHFS_LREG_TYPE) {
558 int i;
559 squashfs_lreg_inode_header *reg = &inode_header.lreg, *inodep;
561 inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
562 inodep = (squashfs_lreg_inode_header *) inode;
563 reg->nlink = nlink;
564 reg->file_size = byte_size;
565 reg->start_block = start_block;
566 reg->fragment = fragment->index;
567 reg->offset = fragment->offset;
568 if(!swap) {
569 memcpy(inodep, reg, sizeof(*reg));
570 memcpy(inodep->block_list, block_list, offset * sizeof(unsigned int));
571 } else {
572 SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inodep);
573 SQUASHFS_SWAP_INTS(block_list, inodep->block_list, offset);
575 TRACE("Long file inode, file_size %lld, start_block %llx, blocks %d, fragment %d, offset %d, size %d, nlink %d\n", byte_size,
576 start_block, offset, fragment->index, fragment->offset, fragment->size, nlink);
577 for(i = 0; i < offset; i++)
578 TRACE("Block %d, size %d\n", i, block_list[i]);
580 else if(type == SQUASHFS_LDIR_TYPE) {
581 int i;
582 unsigned char *p;
583 squashfs_ldir_inode_header *dir = &inode_header.ldir, *inodep;
584 struct cached_dir_index *index = dir_in->index;
585 unsigned int i_count = dir_in->i_count;
586 unsigned int i_size = dir_in->i_size;
588 if(byte_size >= 1 << 27)
589 BAD_ERROR("directory greater than 2^27-1 bytes!\n");
591 inode = get_inode(sizeof(*dir) + i_size);
592 inodep = (squashfs_ldir_inode_header *) inode;
593 dir->inode_type = SQUASHFS_LDIR_TYPE;
594 dir->nlink = dir_ent->dir->directory_count + 2;
595 dir->file_size = byte_size;
596 dir->offset = offset;
597 dir->start_block = start_block;
598 dir->i_count = i_count;
599 dir->parent_inode = dir_ent->our_dir ? dir_ent->our_dir->dir_ent->inode->inode_number : dir_inode_no + inode_no;
601 if(!swap)
602 memcpy(inode, dir, sizeof(*dir));
603 else
604 SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode);
605 p = (unsigned char *) inodep->index;
606 for(i = 0; i < i_count; i++) {
607 if(!swap)
608 memcpy(p, &index[i].index, sizeof(squashfs_dir_index));
609 else
610 SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p);
611 memcpy(((squashfs_dir_index *)p)->name, index[i].name, index[i].index.size + 1);
612 p += sizeof(squashfs_dir_index) + index[i].index.size + 1;
614 TRACE("Long directory inode, file_size %d, start_block %llx, offset %x, nlink %d\n", (int) byte_size,
615 start_block, offset, dir_ent->dir->directory_count + 2);
617 else if(type == SQUASHFS_DIR_TYPE) {
618 squashfs_dir_inode_header *dir = &inode_header.dir;
620 inode = get_inode(sizeof(*dir));
621 dir->nlink = dir_ent->dir->directory_count + 2;
622 dir->file_size = byte_size;
623 dir->offset = offset;
624 dir->start_block = start_block;
625 dir->parent_inode = dir_ent->our_dir ? dir_ent->our_dir->dir_ent->inode->inode_number : dir_inode_no + inode_no;
626 if(!swap)
627 memcpy(inode, dir, sizeof(*dir));
628 else
629 SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode);
630 TRACE("Directory inode, file_size %d, start_block %llx, offset %x, nlink %d\n", (int) byte_size,
631 start_block, offset, dir_ent->dir->directory_count + 2);
633 else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) {
634 squashfs_dev_inode_header *dev = &inode_header.dev;
636 inode = get_inode(sizeof(*dev));
637 dev->nlink = nlink;
638 dev->rdev = (unsigned short) ((major(buf->st_rdev) << 8) |
639 (minor(buf->st_rdev) & 0xff));
640 if(!swap)
641 memcpy(inode, dev, sizeof(*dev));
642 else
643 SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode);
644 TRACE("Device inode, rdev %x, nlink %d\n", dev->rdev, nlink);
646 else if(type == SQUASHFS_SYMLINK_TYPE) {
647 squashfs_symlink_inode_header *symlink = &inode_header.symlink, *inodep;
648 int byte;
649 char buff[65536];
651 if((byte = readlink(filename, buff, 65536)) == -1) {
652 perror("Error in reading symbolic link, skipping...");
653 return FALSE;
656 if(byte == 65536) {
657 ERROR("Symlink is greater than 65536 bytes! skipping...");
658 return FALSE;
661 inode = get_inode(sizeof(*symlink) + byte);
662 symlink->nlink = nlink;
663 inodep = (squashfs_symlink_inode_header *) inode;
664 symlink->symlink_size = byte;
665 if(!swap)
666 memcpy(inode, symlink, sizeof(*symlink));
667 else
668 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
669 strncpy(inodep->symlink, buff, byte);
670 TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, nlink);
672 else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) {
673 squashfs_ipc_inode_header *ipc = &inode_header.ipc;
675 inode = get_inode(sizeof(*ipc));
676 ipc->nlink = nlink;
677 if(!swap)
678 memcpy(inode, ipc, sizeof(*ipc));
679 else
680 SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode);
681 TRACE("ipc inode, type %s, nlink %d\n", type == SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink);
682 } else
683 return FALSE;
685 *i_no = MKINODE(inode);
686 inode_count ++;
688 TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type, base->uid, base->guid);
690 return TRUE;
694 void scan2_init_dir(struct directory *dir)
696 if((dir->buff = malloc(SQUASHFS_METADATA_SIZE)) == NULL) {
697 BAD_ERROR("Out of memory allocating directory buffer\n");
700 dir->size = SQUASHFS_METADATA_SIZE;
701 dir->p = dir->index_count_p = dir->buff;
702 dir->entry_count = 256;
703 dir->entry_count_p = NULL;
704 dir->index = NULL;
705 dir->i_count = dir->i_size = 0;
709 void add_dir(squashfs_inode inode, unsigned int inode_number, char *name, int type, struct directory *dir)
711 unsigned char *buff;
712 squashfs_dir_entry idir, *idirp;
713 unsigned int start_block = inode >> 16;
714 unsigned int offset = inode & 0xffff;
715 unsigned int size;
717 if((size = strlen(name)) > SQUASHFS_NAME_LEN) {
718 size = SQUASHFS_NAME_LEN;
719 ERROR("Filename is greater than %d characters, truncating! ...\n", SQUASHFS_NAME_LEN);
722 if(dir->p + sizeof(squashfs_dir_entry) + size + sizeof(squashfs_dir_header) >= dir->buff + dir->size) {
723 if((buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE)) == NULL) {
724 BAD_ERROR("Out of memory reallocating directory buffer\n");
727 dir->p = (dir->p - dir->buff) + buff;
728 if(dir->entry_count_p)
729 dir->entry_count_p = (dir->entry_count_p - dir->buff + buff);
730 dir->index_count_p = dir->index_count_p - dir->buff + buff;
731 dir->buff = buff;
734 if(dir->entry_count == 256 || start_block != dir->start_block || ((dir->entry_count_p != NULL) && ((dir->p + sizeof(squashfs_dir_entry) + size - dir->index_count_p) > SQUASHFS_METADATA_SIZE)) || ((long long) inode_number - dir->inode_number) > 32767 || ((long long) inode_number - dir->inode_number) < - 32768) {
735 if(dir->entry_count_p) {
736 squashfs_dir_header dir_header;
738 if((dir->p + sizeof(squashfs_dir_entry) + size - dir->index_count_p) > SQUASHFS_METADATA_SIZE) {
739 if(dir->i_count % I_COUNT_SIZE == 0)
740 if((dir->index = realloc(dir->index, (dir->i_count + I_COUNT_SIZE) * sizeof(struct cached_dir_index))) == NULL)
741 BAD_ERROR("Out of memory in directory index table reallocation!\n");
742 dir->index[dir->i_count].index.index = dir->p - dir->buff;
743 dir->index[dir->i_count].index.size = size - 1;
744 dir->index[dir->i_count++].name = name;
745 dir->i_size += sizeof(squashfs_dir_index) + size;
746 dir->index_count_p = dir->p;
749 dir_header.count = dir->entry_count - 1;
750 dir_header.start_block = dir->start_block;
751 dir_header.inode_number = dir->inode_number;
752 if(!swap)
753 memcpy(dir->entry_count_p, &dir_header, sizeof(dir_header));
754 else
755 SQUASHFS_SWAP_DIR_HEADER((&dir_header), (squashfs_dir_header *) dir->entry_count_p);
760 dir->entry_count_p = dir->p;
761 dir->start_block = start_block;
762 dir->entry_count = 0;
763 dir->inode_number = inode_number;
764 dir->p += sizeof(squashfs_dir_header);
767 idirp = (squashfs_dir_entry *) dir->p;
768 idir.offset = offset;
769 idir.type = type;
770 idir.size = size - 1;
771 idir.inode_number = ((long long) inode_number - dir->inode_number);
772 if(!swap)
773 memcpy(idirp, &idir, sizeof(idir));
774 else
775 SQUASHFS_SWAP_DIR_ENTRY((&idir), idirp);
776 strncpy(idirp->name, name, size);
777 dir->p += sizeof(squashfs_dir_entry) + size;
778 dir->entry_count ++;
782 int write_dir(squashfs_inode *inode, struct dir_info *dir_info, struct directory *dir)
784 unsigned int dir_size = dir->p - dir->buff;
785 int data_space = (directory_cache_size - directory_cache_bytes);
786 unsigned int directory_block, directory_offset, i_count, index;
787 unsigned short c_byte;
789 if(data_space < dir_size) {
790 int realloc_size = directory_cache_size == 0 ? ((dir_size + SQUASHFS_METADATA_SIZE) & ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space;
792 if((directory_data_cache = (char *) realloc(directory_data_cache, directory_cache_size + realloc_size)) == NULL) {
793 goto failed;
795 directory_cache_size += realloc_size;
798 if(dir_size) {
799 squashfs_dir_header dir_header;
801 dir_header.count = dir->entry_count - 1;
802 dir_header.start_block = dir->start_block;
803 dir_header.inode_number = dir->inode_number;
804 if(!swap)
805 memcpy(dir->entry_count_p, &dir_header, sizeof(dir_header));
806 else
807 SQUASHFS_SWAP_DIR_HEADER((&dir_header), (squashfs_dir_header *) dir->entry_count_p);
808 memcpy(directory_data_cache + directory_cache_bytes, dir->buff, dir_size);
810 directory_offset = directory_cache_bytes;
811 directory_block = directory_bytes;
812 directory_cache_bytes += dir_size;
813 i_count = 0;
814 index = SQUASHFS_METADATA_SIZE - directory_offset;
816 while(1) {
817 while(i_count < dir->i_count && dir->index[i_count].index.index < index)
818 dir->index[i_count++].index.start_block = directory_bytes;
819 index += SQUASHFS_METADATA_SIZE;
821 if(directory_cache_bytes < SQUASHFS_METADATA_SIZE)
822 break;
824 if((directory_size - directory_bytes) < ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
825 if((directory_table = (char *) realloc(directory_table,
826 directory_size + (SQUASHFS_METADATA_SIZE << 1) + 2)) == NULL) {
827 goto failed;
829 directory_size += SQUASHFS_METADATA_SIZE << 1;
832 c_byte = mangle(directory_table + directory_bytes + block_offset, directory_data_cache,
833 SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, noI, 0);
834 TRACE("Directory block @ %x, size %d\n", directory_bytes, c_byte);
835 if(!swap)
836 memcpy(directory_table + directory_bytes, &c_byte, sizeof(unsigned short));
837 else
838 SQUASHFS_SWAP_SHORTS((&c_byte), (directory_table + directory_bytes), 1);
839 if(check_data)
840 *((unsigned char *)(directory_table + directory_bytes + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
841 directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
842 total_directory_bytes += SQUASHFS_METADATA_SIZE + block_offset;
843 memcpy(directory_data_cache, directory_data_cache + SQUASHFS_METADATA_SIZE, directory_cache_bytes - SQUASHFS_METADATA_SIZE);
844 directory_cache_bytes -= SQUASHFS_METADATA_SIZE;
847 if(dir_info->dir_is_ldir) {
848 if(create_inode(inode, dir_info->dir_ent, SQUASHFS_LDIR_TYPE, dir_size + 3, directory_block, directory_offset, NULL, NULL, dir) == FALSE)
849 return FALSE;
850 } else {
851 if(create_inode(inode, dir_info->dir_ent, SQUASHFS_DIR_TYPE, dir_size + 3, directory_block, directory_offset, NULL, NULL, NULL) == FALSE)
852 return FALSE;
855 #ifdef SQUASHFS_TRACE
856 if(!swap) {
857 unsigned char *dirp;
858 int count;
860 TRACE("Directory contents of inode 0x%llx\n", *inode);
861 dirp = dir->buff;
862 while(dirp < dir->p) {
863 char buffer[SQUASHFS_NAME_LEN + 1];
864 squashfs_dir_entry idir, *idirp;
865 squashfs_dir_header *dirh = (squashfs_dir_header *) dirp;
866 count = dirh->count + 1;
867 dirp += sizeof(squashfs_dir_header);
869 TRACE("\tStart block 0x%x, count %d\n", dirh->start_block, count);
871 while(count--) {
872 idirp = (squashfs_dir_entry *) dirp;
873 memcpy((char *) &idir, (char *) idirp, sizeof(idir));
874 strncpy(buffer, idirp->name, idir.size + 1);
875 buffer[idir.size + 1] = '\0';
876 TRACE("\t\tname %s, inode offset 0x%x, type %d\n", buffer,
877 idir.offset, idir.type);
878 dirp += sizeof(squashfs_dir_entry) + idir.size + 1;
882 #endif
883 dir_count ++;
885 return TRUE;
887 failed:
888 BAD_ERROR("Out of memory in directory table reallocation!\n");
892 char *get_fragment(char *buffer, struct fragment *fragment)
894 squashfs_fragment_entry *disk_fragment = &fragment_table[fragment->index];
895 int size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size);
897 if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) {
898 int res;
899 unsigned long bytes = block_size;
900 char cbuffer[block_size];
902 read_bytes(fd, disk_fragment->start_block, size, cbuffer);
904 if((res = uncompress((unsigned char *) buffer, &bytes, (const unsigned char *) cbuffer, size)) != Z_OK) {
905 if(res == Z_MEM_ERROR)
906 BAD_ERROR("zlib::uncompress failed, not enough memory\n");
907 else if(res == Z_BUF_ERROR)
908 BAD_ERROR("zlib::uncompress failed, not enough room in output buffer\n");
909 else
910 BAD_ERROR("zlib::uncompress failed, unknown error %d\n", res);
912 } else
913 read_bytes(fd, disk_fragment->start_block, size, buffer);
915 return buffer + fragment->offset;
919 void write_fragment()
921 int compressed_size;
922 char buffer[block_size << 1];
924 if(fragment_size == 0)
925 return;
927 if(fragments % FRAG_SIZE == 0)
928 if((fragment_table = (squashfs_fragment_entry *) realloc(fragment_table, (fragments + FRAG_SIZE) * sizeof(squashfs_fragment_entry))) == NULL)
929 BAD_ERROR("Out of memory in fragment table\n");
930 fragment_table[fragments].size = mangle(buffer, fragment_data, fragment_size, block_size, noF, 1);
931 fragment_table[fragments].start_block = bytes;
932 compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[fragments].size);
933 write_bytes(fd, bytes, compressed_size, buffer);
934 bytes += compressed_size;
935 total_uncompressed += fragment_size;
936 total_compressed += compressed_size;
937 TRACE("Writing fragment %d, uncompressed size %d, compressed size %d\n",fragments, fragment_size, compressed_size);
938 fragments ++;
939 fragment_size = 0;
943 static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0};
944 struct fragment *get_and_fill_fragment(char *buff, int size)
946 struct fragment *ffrg;
948 if(size == 0)
949 return &empty_fragment;
951 if(fragment_size + size > block_size)
952 write_fragment();
954 if((ffrg = (struct fragment *) malloc(sizeof(struct fragment))) == NULL)
955 BAD_ERROR("Out of memory in fragment block allocation!\n");
957 ffrg->index = fragments;
958 ffrg->offset = fragment_size;
959 ffrg->size = size;
960 memcpy(fragment_data + fragment_size, buff, size);
961 fragment_size += size;
963 return ffrg;
967 long long write_fragment_table()
969 long long start_bytes;
970 unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments),
971 meta_blocks = SQUASHFS_FRAGMENT_INDEXES(fragments);
972 char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2], buffer[frag_bytes];
973 squashfs_fragment_entry *p = (squashfs_fragment_entry *) buffer;
974 unsigned short c_byte;
975 int i, compressed_size;
976 squashfs_fragment_index list[meta_blocks];
978 TRACE("write_fragment_table: fragments %d, frag_bytes %d, meta_blocks %d\n", fragments, frag_bytes, meta_blocks);
979 for(i = 0; i < fragments; i++, p++) {
980 TRACE("write_fragment_table: fragment %d, start_block %llx, size %d\n", i, fragment_table[i].start_block, fragment_table[i].size);
981 if(!swap)
982 memcpy(p, &fragment_table[i], sizeof(squashfs_fragment_entry));
983 else
984 SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_table[i], p);
987 for(i = 0; i < meta_blocks; i++) {
988 int avail_bytes = i == meta_blocks - 1 ? frag_bytes % SQUASHFS_METADATA_SIZE : SQUASHFS_METADATA_SIZE;
989 c_byte = mangle(cbuffer + block_offset, buffer + i * SQUASHFS_METADATA_SIZE , avail_bytes, SQUASHFS_METADATA_SIZE, noF, 0);
990 if(!swap)
991 memcpy(cbuffer, &c_byte, sizeof(unsigned short));
992 else
993 SQUASHFS_SWAP_SHORTS((&c_byte), cbuffer, 1);
994 if(check_data)
995 *((unsigned char *)(cbuffer + block_offset - 1)) = SQUASHFS_MARKER_BYTE;
996 list[i] = bytes;
997 compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + block_offset;
998 write_bytes(fd, bytes, compressed_size, cbuffer);
999 bytes += compressed_size;
1002 if(!swap)
1003 write_bytes(fd, bytes, sizeof(list), (char *) list);
1004 else {
1005 squashfs_fragment_index slist[meta_blocks];
1006 SQUASHFS_SWAP_FRAGMENT_INDEXES(list, slist, meta_blocks);
1007 write_bytes(fd, bytes, sizeof(list), (char *) slist);
1010 start_bytes = bytes;
1011 bytes += sizeof(list);
1013 return start_bytes;
1017 char *read_from_buffer(struct duplicate_buffer_handle *handle, unsigned int avail_bytes)
1019 char *v = handle->ptr;
1020 handle->ptr += avail_bytes;
1021 return v;
1025 char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE];
1026 char *read_from_file(struct duplicate_buffer_handle *handle, unsigned int avail_bytes)
1028 read_bytes(fd, handle->start, avail_bytes, read_from_file_buffer);
1029 handle->start += avail_bytes;
1030 return read_from_file_buffer;
1035 * Compute 16 bit BSD checksum over the data
1037 unsigned short get_checksum(char *(get_next_file_block)(struct duplicate_buffer_handle *, unsigned int), struct duplicate_buffer_handle *handle, long long l)
1039 unsigned short chksum = 0;
1040 unsigned int bytes = 0;
1041 unsigned char *b;
1042 struct duplicate_buffer_handle position = *handle;
1044 while(l) {
1045 bytes = l > SQUASHFS_FILE_MAX_SIZE ? SQUASHFS_FILE_MAX_SIZE : l;
1046 l -= bytes;
1047 b = (unsigned char *) get_next_file_block(&position, bytes);
1048 while(bytes--) {
1049 chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1;
1050 chksum += *b++;
1054 return chksum;
1058 int cached_frag = -1;
1059 void add_file(long long start, long long file_bytes, unsigned int *block_listp, int blocks, unsigned int fragment, int offset, int bytes)
1061 struct fragment *frg;
1062 struct file_info *dupl_ptr;
1063 char *datap;
1064 struct duplicate_buffer_handle handle;
1065 unsigned int *block_list = block_listp;
1067 if(!duplicate_checking)
1068 return;
1070 if((frg = (struct fragment *) malloc(sizeof(struct fragment))) == NULL)
1071 BAD_ERROR("Out of memory in fragment block allocation!\n");
1073 frg->index = fragment;
1074 frg->offset = offset;
1075 frg->size = bytes;
1076 if(fragment == cached_frag || fragment == SQUASHFS_INVALID_FRAG)
1077 datap = fragment_data + offset;
1078 else
1079 datap = get_fragment(fragment_data, frg);
1080 handle.start = start;
1081 if((dupl_ptr = duplicate(read_from_file, &handle, file_bytes, &block_listp, &start, blocks, &frg, datap, bytes)) != NULL)
1082 dupl_ptr->fragment = frg;
1083 else
1084 free(block_list);
1085 cached_frag = fragment;
1089 char cached_fragment[SQUASHFS_FILE_SIZE];
1090 int cached_frag1 = -1;
1092 struct file_info *duplicate(char *(get_next_file_block)(struct duplicate_buffer_handle *, unsigned int), struct duplicate_buffer_handle *file_start, long long bytes, unsigned int **block_list, long long *start, int blocks, struct fragment **fragment, char *frag_data, int frag_bytes)
1094 unsigned short checksum = get_checksum(get_next_file_block, file_start, bytes);
1095 struct duplicate_buffer_handle handle = { frag_data, 0 };
1096 unsigned short fragment_checksum = get_checksum(read_from_buffer, &handle, frag_bytes);
1097 struct file_info *dupl_ptr = bytes ? dupl[checksum] : frag_dups[fragment_checksum];
1100 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
1101 if(bytes == dupl_ptr->bytes && frag_bytes == dupl_ptr->fragment->size && fragment_checksum == dupl_ptr->fragment_checksum) {
1102 char buffer1[SQUASHFS_FILE_MAX_SIZE];
1103 long long dup_bytes = dupl_ptr->bytes;
1104 long long dup_start = dupl_ptr->start;
1105 struct duplicate_buffer_handle position = *file_start;
1106 char *buffer;
1107 while(dup_bytes) {
1108 int avail_bytes = dup_bytes > SQUASHFS_FILE_MAX_SIZE ? SQUASHFS_FILE_MAX_SIZE : dup_bytes;
1110 buffer = get_next_file_block(&position, avail_bytes);
1111 read_bytes(fd, dup_start, avail_bytes, buffer1);
1112 if(memcmp(buffer, buffer1, avail_bytes) != 0)
1113 break;
1114 dup_bytes -= avail_bytes;
1115 dup_start += avail_bytes;
1117 if(dup_bytes == 0) {
1118 char *fragment_buffer1;
1120 if(dupl_ptr->fragment->index == fragments || dupl_ptr->fragment->index == SQUASHFS_INVALID_FRAG)
1121 fragment_buffer1 = fragment_data + dupl_ptr->fragment->offset;
1122 else if(dupl_ptr->fragment->index == cached_frag1)
1123 fragment_buffer1 = cached_fragment + dupl_ptr->fragment->offset;
1124 else {
1125 fragment_buffer1 = get_fragment(cached_fragment, dupl_ptr->fragment);
1126 cached_frag1 = dupl_ptr->fragment->index;
1129 if(frag_bytes == 0 || memcmp(frag_data, fragment_buffer1, frag_bytes) == 0) {
1130 TRACE("Found duplicate file, start 0x%llx, size %lld, checksum 0x%x, fragment %d, size %d, offset %d, checksum 0x%x\n", dupl_ptr->start,
1131 dupl_ptr->bytes, dupl_ptr->checksum, dupl_ptr->fragment->index, frag_bytes, dupl_ptr->fragment->offset, fragment_checksum);
1132 *block_list = dupl_ptr->block_list;
1133 *start = dupl_ptr->start;
1134 *fragment = dupl_ptr->fragment;
1135 return 0;
1141 if((dupl_ptr = (struct file_info *) malloc(sizeof(struct file_info))) == NULL) {
1142 BAD_ERROR("Out of memory in dup_files allocation!\n");
1145 dupl_ptr->bytes = bytes;
1146 dupl_ptr->checksum = checksum;
1147 dupl_ptr->start = *start;
1148 dupl_ptr->fragment_checksum = fragment_checksum;
1149 dupl_ptr->block_list = *block_list;
1151 dup_files ++;
1152 if(bytes) {
1153 dupl_ptr->next = dupl[checksum];
1154 dupl[checksum] = dupl_ptr;
1155 } else {
1156 dupl_ptr->next = frag_dups[fragment_checksum];
1157 frag_dups[fragment_checksum] = dupl_ptr;
1160 return dupl_ptr;
1164 #define MINALLOCBYTES (1024 * 1024)
1165 int write_file(squashfs_inode *inode, struct dir_ent *dir_ent, long long size, int *duplicate_file)
1167 int block = 0, i, file, whole_file = 1, status;
1168 unsigned int c_byte, frag_bytes;
1169 long long bbytes, file_bytes = 0, start;
1170 char buff[block_size], *c_buffer = NULL, *filename = dir_ent->pathname;
1171 struct fragment *fragment;
1172 struct file_info *dupl_ptr = NULL;
1173 struct duplicate_buffer_handle handle;
1174 long long read_size = (size > SQUASHFS_MAX_FILE_SIZE) ? SQUASHFS_MAX_FILE_SIZE : size;
1175 int blocks = (read_size + block_size - 1) >> block_log, allocated_blocks = blocks;
1176 unsigned int *block_list, *block_listp;
1178 if((block_list = malloc(blocks * sizeof(unsigned int))) == NULL)
1179 BAD_ERROR("Out of memory allocating block_list\n");
1180 block_listp = block_list;
1182 if(!no_fragments && (read_size < block_size || always_use_fragments)) {
1183 allocated_blocks = blocks = read_size >> block_log;
1184 frag_bytes = read_size % block_size;
1185 } else
1186 frag_bytes = 0;
1188 if(size > read_size)
1189 ERROR("file %s truncated to %lld bytes\n", filename, SQUASHFS_MAX_FILE_SIZE);
1191 total_bytes += read_size;
1192 if((file = open(filename, O_RDONLY)) == -1)
1193 goto read_err;
1195 do {
1196 long long bytes = (((long long) allocated_blocks) + 1) << block_log;
1197 if(bytes != ((size_t) bytes) || (c_buffer = (char *) malloc(bytes)) == NULL) {
1198 TRACE("Out of memory allocating write_file buffer, allocated_blocks %ld, blocks %d\n", allocated_blocks, blocks);
1199 whole_file = 0;
1200 if(bytes < MINALLOCBYTES)
1201 BAD_ERROR("Out of memory allocating write_file buffer, could not allocate %ld blocks (%d Kbytes)\n", allocated_blocks, allocated_blocks << (block_log - 10));
1202 allocated_blocks >>= 1;
1204 } while(!c_buffer);
1206 for(start = bytes; block < blocks; file_bytes += bbytes) {
1207 for(i = 0, bbytes = 0; (i < allocated_blocks) && (block < blocks); i++) {
1208 int available_bytes = read_size - (block * block_size) > block_size ? block_size : read_size - (block * block_size);
1209 if(read(file, buff, available_bytes) == -1)
1210 goto read_err;
1211 c_byte = mangle(c_buffer + bbytes, buff, available_bytes, block_size, noD, 1);
1212 block_list[block ++] = c_byte;
1213 bbytes += SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
1215 if(!whole_file) {
1216 write_bytes(fd, bytes, bbytes, c_buffer);
1217 bytes += bbytes;
1221 if(frag_bytes != 0)
1222 if(read(file, buff, frag_bytes) == -1)
1223 goto read_err;
1225 close(file);
1226 if(whole_file) {
1227 handle.ptr = c_buffer;
1228 if(duplicate_checking && (dupl_ptr = duplicate(read_from_buffer, &handle, file_bytes, &block_listp, &start, blocks, &fragment, buff, frag_bytes)) == NULL) {
1229 *duplicate_file = TRUE;
1230 goto wr_inode;
1232 write_bytes(fd, bytes, file_bytes, c_buffer);
1233 bytes += file_bytes;
1234 } else {
1235 handle.start = start;
1236 if(duplicate_checking && (dupl_ptr = duplicate(read_from_file, &handle, file_bytes, &block_listp, &start, blocks, &fragment, buff, frag_bytes)) == NULL) {
1237 bytes = start;
1238 if(!block_device)
1239 ftruncate(fd, bytes);
1240 *duplicate_file = TRUE;
1241 goto wr_inode;
1245 fragment = get_and_fill_fragment(buff, frag_bytes);
1246 if(duplicate_checking)
1247 dupl_ptr->fragment = fragment;
1249 *duplicate_file = FALSE;
1251 wr_inode:
1252 free(c_buffer);
1253 file_count ++;
1254 if(dir_ent->inode->nlink == 1 && read_size < ((long long) (1<<30) - 1))
1255 status = create_inode(inode, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, blocks, block_listp, fragment, NULL);
1256 else
1257 status = create_inode(inode, dir_ent, SQUASHFS_LREG_TYPE, read_size, start, blocks, block_listp, fragment, NULL);
1258 if(duplicate_checking == FALSE || *duplicate_file == TRUE)
1259 free(block_list);
1260 return status;
1262 read_err:
1263 perror("Error in reading file, skipping...");
1264 free(c_buffer);
1265 free(block_list);
1266 return FALSE;
1270 char b_buffer[8192];
1271 char *name;
1272 char *basename_r();
1274 char *getbase(char *pathname)
1276 char *result;
1278 if(*pathname != '/') {
1279 result = getenv("PWD");
1280 strcat(strcat(strcpy(b_buffer, result), "/"), pathname);
1281 } else
1282 strcpy(b_buffer, pathname);
1283 name = b_buffer;
1284 if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0))
1285 return NULL;
1286 else
1287 return result;
1291 char *basename_r()
1293 char *s;
1294 char *p;
1295 int n = 1;
1297 for(;;) {
1298 s = name;
1299 if(*name == '\0')
1300 return NULL;
1301 if(*name != '/') {
1302 while(*name != '\0' && *name != '/') name++;
1303 n = name - s;
1305 while(*name == '/') name++;
1306 if(strncmp(s, ".", n) == 0)
1307 continue;
1308 if((*name == '\0') || (strncmp(s, "..", n) == 0) || ((p = basename_r()) == NULL)) {
1309 s[n] = '\0';
1310 return s;
1312 if(strcmp(p, "..") == 0)
1313 continue;
1314 return p;
1319 struct inode_info *lookup_inode(struct stat *buf)
1321 int inode_hash = INODE_HASH(buf->st_dev, buf->st_ino);
1322 struct inode_info *inode = inode_info[inode_hash];
1324 while(inode != NULL) {
1325 if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) {
1326 inode->nlink ++;
1327 return inode;
1329 inode = inode->next;
1332 if((inode = malloc(sizeof(struct inode_info))) == NULL)
1333 BAD_ERROR("Out of memory in inode hash table entry allocation\n");
1335 memcpy(&inode->buf, buf, sizeof(struct stat));
1336 inode->inode = SQUASHFS_INVALID_BLK;
1337 inode->nlink = 1;
1338 if((buf->st_mode & S_IFMT) == S_IFDIR)
1339 inode->inode_number = dir_inode_no ++;
1340 else
1341 inode->inode_number = inode_no ++;
1343 inode->next = inode_info[inode_hash];
1344 inode_info[inode_hash] = inode;
1346 return inode;
1350 inline void add_dir_entry(char *name, char *pathname, struct dir_info *sub_dir, struct inode_info *inode_info, void *data, struct dir_info *dir)
1352 if((dir->count % DIR_ENTRIES) == 0)
1353 if((dir->list = realloc(dir->list, (dir->count + DIR_ENTRIES) * sizeof(struct dir_ent *))) == NULL)
1354 BAD_ERROR("Out of memory in add_dir_entry\n");
1356 if((dir->list[dir->count] = malloc(sizeof(struct dir_ent))) == NULL)
1357 BAD_ERROR("Out of memory in linux_opendir\n");
1359 if(sub_dir)
1360 sub_dir->dir_ent = dir->list[dir->count];
1361 dir->list[dir->count]->name = strdup(name);
1362 dir->list[dir->count]->pathname = pathname != NULL ? strdup(pathname) : NULL;
1363 dir->list[dir->count]->inode = inode_info;
1364 dir->list[dir->count]->dir = sub_dir;
1365 dir->list[dir->count]->our_dir = dir;
1366 dir->list[dir->count++]->data = data;
1367 dir->byte_count += strlen(name) + sizeof(squashfs_dir_entry);
1371 int compare_name(const void *ent1_ptr, const void *ent2_ptr)
1373 struct dir_ent *ent1 = *((struct dir_ent **) ent1_ptr);
1374 struct dir_ent *ent2 = *((struct dir_ent **) ent2_ptr);
1376 return strcmp(ent1->name, ent2->name);
1380 void sort_directory(struct dir_info *dir)
1382 qsort(dir->list, dir->count, sizeof(struct dir_ent *), compare_name);
1384 if((dir->count < 257 && dir->byte_count < SQUASHFS_METADATA_SIZE))
1385 dir->dir_is_ldir = FALSE;
1389 struct dir_info *scan1_opendir(char *pathname)
1391 DIR *linuxdir;
1392 struct dirent *d_name;
1393 struct dir_info *dir;
1395 if((dir = malloc(sizeof(struct dir_info))) == NULL)
1396 return NULL;
1398 if(pathname[0] != '\0' && (dir->linuxdir = opendir(pathname)) == NULL) {
1399 free(dir);
1400 return NULL;
1402 dir->pathname = strdup(pathname);
1403 dir->count = dir->directory_count = dir->current_count = dir->byte_count = 0;
1404 dir->dir_is_ldir = TRUE;
1405 dir->list = NULL;
1407 return dir;
1411 int scan1_encomp_readdir(char *pathname, char *dir_name, struct dir_info *dir)
1413 int i, n, pass;
1414 char *basename;
1415 static int index = 0;
1417 if(dir->count < old_root_entries)
1418 for(i = 0; i < old_root_entries; i++) {
1419 if(old_root_entry[i].type == SQUASHFS_DIR_TYPE)
1420 dir->directory_count ++;
1421 add_dir_entry(old_root_entry[i].name, "", NULL, NULL, &old_root_entry[i], dir);
1424 while(index < source) {
1425 if((basename = getbase(source_path[index])) == NULL) {
1426 ERROR("Bad source directory %s - skipping ...\n", source_path[index]);
1427 index ++;
1428 continue;
1430 strcpy(dir_name, basename);
1431 pass = 1;
1432 for(;;) {
1433 for(n = 0; n < dir->count && strcmp(dir->list[n]->name, dir_name) != 0; n++);
1434 if(n == dir->count)
1435 break;
1436 ERROR("Source directory entry %s already used! - trying ", dir_name);
1437 sprintf(dir_name, "%s_%d", basename, pass++);
1438 ERROR("%s\n", dir_name);
1440 strcpy(pathname, source_path[index ++]);
1441 return 1;
1443 return 0;
1447 int scan1_single_readdir(char *pathname, char *dir_name, struct dir_info *dir)
1449 struct dirent *d_name;
1450 int i, pass;
1452 if(dir->count < old_root_entries)
1453 for(i = 0; i < old_root_entries; i++) {
1454 if(old_root_entry[i].type == SQUASHFS_DIR_TYPE)
1455 dir->directory_count ++;
1456 add_dir_entry(old_root_entry[i].name, "", NULL, NULL, &old_root_entry[i], dir);
1459 if((d_name = readdir(dir->linuxdir)) != NULL) {
1460 strcpy(dir_name, d_name->d_name);
1461 pass = 1;
1462 for(;;) {
1463 for(i = 0; i < dir->count && strcmp(dir->list[i]->name, dir_name) != 0; i++);
1464 if(i == dir->count)
1465 break;
1466 ERROR("Source directory entry %s already used! - trying ", dir_name);
1467 sprintf(dir_name, "%s_%d", d_name->d_name, pass++);
1468 ERROR("%s\n", dir_name);
1470 strcat(strcat(strcpy(pathname, dir->pathname), "/"), d_name->d_name);
1471 return 1;
1474 return 0;
1478 int scan1_readdir(char *pathname, char *dir_name, struct dir_info *dir)
1480 struct dirent *d_name;
1482 if((d_name = readdir(dir->linuxdir)) != NULL) {
1483 strcpy(dir_name, d_name->d_name);
1484 strcat(strcat(strcpy(pathname, dir->pathname), "/"), d_name->d_name);
1485 return 1;
1488 return 0;
1492 struct dir_ent *scan2_readdir(struct directory *dir, struct dir_info *dir_info)
1494 int current_count;
1496 while((current_count = dir_info->current_count++) < dir_info->count)
1497 if(dir_info->list[current_count]->data)
1498 add_dir(dir_info->list[current_count]->data->inode, dir_info->list[current_count]->data->inode_number,
1499 dir_info->list[current_count]->name, dir_info->list[current_count]->data->type, dir);
1500 else
1501 return dir_info->list[current_count];
1502 return FALSE;
1506 void scan1_freedir(struct dir_info *dir)
1508 if(dir->pathname[0] != '\0')
1509 closedir(dir->linuxdir);
1513 void scan2_freedir(struct directory *dir)
1515 if(dir->index)
1516 free(dir->index);
1517 free(dir->buff);
1521 void dir_scan(squashfs_inode *inode, char *pathname, int (_readdir)(char *, char *, struct dir_info *))
1523 struct dir_info *dir_info = dir_scan1(pathname, _readdir);
1524 struct dir_ent *dir_ent;
1525 struct inode_info *inode_info;
1527 if(dir_info == NULL)
1528 return;
1530 if((dir_ent = malloc(sizeof(struct dir_ent))) == NULL)
1531 BAD_ERROR("Out of memory in dir_scan\n");
1533 if((inode_info = malloc(sizeof(struct inode_info))) == NULL)
1534 BAD_ERROR("Out of memory in dir_scan\n");
1536 dir_ent->name = dir_ent->pathname = strdup(pathname);
1537 dir_ent->dir = dir_info;
1538 dir_ent->inode = inode_info;
1539 dir_ent->our_dir = NULL;
1540 dir_ent->data = NULL;
1541 inode_info->nlink = 1;
1542 inode_info->inode_number = root_inode_number ? root_inode_number : dir_inode_no++;
1543 dir_info->dir_ent = dir_ent;
1545 if(pathname[0] == '\0') {
1546 /* dummy top level directory, if multiple sources specified on command line */
1547 inode_info->buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO;
1548 inode_info->buf.st_uid = getuid();
1549 inode_info->buf.st_gid = getgid();
1550 inode_info->buf.st_mtime = time(NULL);
1551 } else if(lstat(pathname, &inode_info->buf) == -1) {
1552 char buffer[8192];
1553 sprintf(buffer, "Cannot stat dir/file %s, ignoring", pathname);
1554 perror(buffer);
1555 return;
1557 if(sorted)
1558 sort_files_and_write(dir_info);
1559 dir_scan2(inode, dir_info);
1563 struct dir_info *dir_scan1(char *pathname, int (_readdir)(char *, char *, struct dir_info *))
1565 struct dir_info *dir, *sub_dir;
1566 struct stat buf;
1567 char filename[8192], dir_name[8192];
1569 if((dir = scan1_opendir(pathname)) == NULL) {
1570 ERROR("Could not open %s, skipping...\n", pathname);
1571 goto error;
1574 while(_readdir(filename, dir_name, dir) != FALSE) {
1576 if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0)
1577 continue;
1579 if(lstat(filename, &buf) == -1) {
1580 char buffer[8192];
1581 sprintf(buffer, "Cannot stat dir/file %s, ignoring", filename);
1582 perror(buffer);
1583 continue;
1585 if(excluded(filename, &buf))
1586 continue;
1588 if((buf.st_mode & S_IFMT) == S_IFDIR) {
1589 if((sub_dir = dir_scan1(filename, scan1_readdir)) == NULL)
1590 continue;
1591 dir->directory_count ++;
1592 } else
1593 sub_dir = NULL;
1595 add_dir_entry(dir_name, filename, sub_dir, lookup_inode(&buf), NULL, dir);
1598 scan1_freedir(dir);
1599 sort_directory(dir);
1601 error:
1602 return dir;
1606 int dir_scan2(squashfs_inode *inode, struct dir_info *dir_info)
1608 int squashfs_type;
1609 int result = FALSE;
1610 int duplicate_file;
1611 char *pathname = dir_info->pathname;
1612 struct directory dir;
1613 struct dir_ent *dir_ent;
1615 scan2_init_dir(&dir);
1617 while((dir_ent = scan2_readdir(&dir, dir_info)) != NULL) {
1618 struct inode_info *inode_info = dir_ent->inode;
1619 struct stat *buf = &inode_info->buf;
1620 char *filename = dir_ent->pathname;
1621 char *dir_name = dir_ent->name;
1622 unsigned int inode_number = ((buf->st_mode & S_IFMT) == S_IFDIR) ? dir_ent->inode->inode_number : dir_ent->inode->inode_number + dir_inode_no;
1624 if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) {
1625 switch(buf->st_mode & S_IFMT) {
1626 case S_IFREG:
1627 squashfs_type = SQUASHFS_FILE_TYPE;
1628 result = write_file(inode, dir_ent, buf->st_size, &duplicate_file);
1629 INFO("file %s, uncompressed size %lld bytes %s\n", filename, buf->st_size, duplicate_file ? "DUPLICATE" : "");
1630 break;
1632 case S_IFDIR:
1633 squashfs_type = SQUASHFS_DIR_TYPE;
1634 result = dir_scan2(inode, dir_ent->dir);
1635 break;
1637 case S_IFLNK:
1638 squashfs_type = SQUASHFS_SYMLINK_TYPE;
1639 result = create_inode(inode, dir_ent, squashfs_type, 0, 0, 0, NULL, NULL, NULL);
1640 INFO("symbolic link %s inode 0x%llx\n", dir_name, *inode);
1641 sym_count ++;
1642 break;
1644 case S_IFCHR:
1645 squashfs_type = SQUASHFS_CHRDEV_TYPE;
1646 result = create_inode(inode, dir_ent, squashfs_type, 0, 0, 0, NULL, NULL, NULL);
1647 INFO("character device %s inode 0x%llx\n", dir_name, *inode);
1648 dev_count ++;
1649 break;
1651 case S_IFBLK:
1652 squashfs_type = SQUASHFS_BLKDEV_TYPE;
1653 result = create_inode(inode, dir_ent, squashfs_type, 0, 0, 0, NULL, NULL, NULL);
1654 INFO("block device %s inode 0x%llx\n", dir_name, *inode);
1655 dev_count ++;
1656 break;
1658 case S_IFIFO:
1659 squashfs_type = SQUASHFS_FIFO_TYPE;
1660 result = create_inode(inode, dir_ent, squashfs_type, 0, 0, 0, NULL, NULL, NULL);
1661 INFO("fifo %s inode 0x%llx\n", dir_name, *inode);
1662 fifo_count ++;
1663 break;
1665 case S_IFSOCK:
1666 squashfs_type = SQUASHFS_SOCKET_TYPE;
1667 result = create_inode(inode, dir_ent, squashfs_type, 0, 0, 0, NULL, NULL, NULL);
1668 INFO("unix domain socket %s inode 0x%llx\n", dir_name, *inode);
1669 sock_count ++;
1670 break;
1672 default:
1673 ERROR("%s unrecognised file type, mode is %x\n", filename, buf->st_mode);
1674 result = FALSE;
1676 if(result)
1677 dir_ent->inode->inode = *inode;
1678 dir_ent->inode->type = squashfs_type;
1679 } else {
1680 *inode = dir_ent->inode->inode;
1681 squashfs_type = dir_ent->inode->type;
1682 switch(squashfs_type) {
1683 case SQUASHFS_FILE_TYPE:
1684 if(!sorted)
1685 INFO("file %s, uncompressed size %lld bytes LINK\n", filename, buf->st_size);
1686 break;
1687 case SQUASHFS_SYMLINK_TYPE:
1688 INFO("symbolic link %s inode 0x%llx LINK\n", dir_name, *inode);
1689 break;
1690 case SQUASHFS_CHRDEV_TYPE:
1691 INFO("character device %s inode 0x%llx LINK\n", dir_name, *inode);
1692 break;
1693 caseSQUASHFS_BLKDEV_TYPE:
1694 INFO("block device %s inode 0x%llx LINK\n", dir_name, *inode);
1695 break;
1696 case SQUASHFS_FIFO_TYPE:
1697 INFO("fifo %s inode 0x%llx LINK\n", dir_name, *inode);
1698 break;
1699 case SQUASHFS_SOCKET_TYPE:
1700 INFO("unix domain socket %s inode 0x%llx LINK\n", dir_name, *inode);
1701 break;
1703 result = TRUE;
1707 if(result)
1708 add_dir(*inode, inode_number, dir_name, squashfs_type, &dir);
1711 result = write_dir(inode, dir_info, &dir);
1712 INFO("directory %s inode 0x%llx\n", pathname, *inode);
1714 scan2_freedir(&dir);
1716 return result;
1720 unsigned int slog(unsigned int block)
1722 int i;
1724 for(i = 12; i <= 16; i++)
1725 if(block == (1 << i))
1726 return i;
1727 return 0;
1731 int excluded(char *filename, struct stat *buf)
1733 int i;
1735 for(i = 0; i < exclude; i++)
1736 if((exclude_paths[i].st_dev == buf->st_dev) && (exclude_paths[i].st_ino == buf->st_ino))
1737 return TRUE;
1738 return FALSE;
1742 #define ADD_ENTRY(buf) \
1743 if(exclude % EXCLUDE_SIZE == 0) {\
1744 if((exclude_paths = (struct exclude_info *) realloc(exclude_paths, (exclude + EXCLUDE_SIZE) * sizeof(struct exclude_info))) == NULL)\
1745 BAD_ERROR("Out of memory in exclude dir/file table\n");\
1747 exclude_paths[exclude].st_dev = buf.st_dev;\
1748 exclude_paths[exclude++].st_ino = buf.st_ino;
1749 int add_exclude(char *path)
1751 int i;
1752 char buffer[4096], filename[4096];
1753 struct stat buf;
1755 if(path[0] == '/' || strncmp(path, "./", 2) == 0 || strncmp(path, "../", 3) == 0) {
1756 if(lstat(path, &buf) == -1) {
1757 sprintf(buffer, "Cannot stat exclude dir/file %s, ignoring", path);
1758 perror(buffer);
1759 return TRUE;
1761 ADD_ENTRY(buf);
1762 return TRUE;
1765 for(i = 0; i < source; i++) {
1766 strcat(strcat(strcpy(filename, source_path[i]), "/"), path);
1767 if(lstat(filename, &buf) == -1) {
1768 if(!(errno == ENOENT || errno == ENOTDIR)) {
1769 sprintf(buffer, "Cannot stat exclude dir/file %s, ignoring", filename);
1770 perror(buffer);
1772 continue;
1774 ADD_ENTRY(buf);
1776 return TRUE;
1780 void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, int type)
1782 if((old_root_entry = (struct old_root_entry_info *) realloc(old_root_entry, sizeof(struct old_root_entry_info)
1783 * (old_root_entries + 1))) == NULL)
1784 BAD_ERROR("Out of memory in old root directory entries reallocation\n");
1786 strcpy(old_root_entry[old_root_entries].name, name);
1787 old_root_entry[old_root_entries].inode = inode;
1788 old_root_entry[old_root_entries].inode_number = inode_number;
1789 old_root_entry[old_root_entries++].type = type;
1793 #define VERSION() \
1794 printf("mksquashfs version 3.0 (2006/03/15)\n");\
1795 printf("copyright (C) 2006 Phillip Lougher <phillip@lougher.org.uk>\n\n"); \
1796 printf("This program is free software; you can redistribute it and/or\n");\
1797 printf("modify it under the terms of the GNU General Public License\n");\
1798 printf("as published by the Free Software Foundation; either version 2,\n");\
1799 printf("or (at your option) any later version.\n\n");\
1800 printf("This program is distributed in the hope that it will be useful,\n");\
1801 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");\
1802 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");\
1803 printf("GNU General Public License for more details.\n");
1804 int main(int argc, char *argv[])
1806 struct stat buf, source_buf;
1807 int i;
1808 squashfs_super_block sBlk;
1809 char *b, *root_name = NULL;
1810 int be, nopad = FALSE, keep_as_directory = FALSE, orig_be;
1811 squashfs_inode inode;
1813 #if __BYTE_ORDER == __BIG_ENDIAN
1814 be = TRUE;
1815 #else
1816 be = FALSE;
1817 #endif
1819 block_log = slog(block_size);
1820 if(argc > 1 && strcmp(argv[1], "-version") == 0) {
1821 VERSION();
1822 exit(0);
1824 for(i = 1; i < argc && argv[i][0] != '-'; i++);
1825 if(i < 3)
1826 goto printOptions;
1827 source_path = argv + 1;
1828 source = i - 2;
1829 for(; i < argc; i++) {
1830 if(strcmp(argv[i], "-b") == 0) {
1831 if((++i == argc) || (block_size = strtol(argv[i], &b, 10), *b !='\0')) {
1832 ERROR("%s: -b missing or invalid block size\n", argv[0]);
1833 exit(1);
1836 if((block_log = slog(block_size)) == 0) {
1837 ERROR("%s: -b block size not power of two or not between 4096 and 64K\n", argv[0]);
1838 exit(1);
1840 } else if(strcmp(argv[i], "-ef") == 0) {
1841 if(++i == argc) {
1842 ERROR("%s: -ef missing filename\n", argv[0]);
1843 exit(1);
1845 } else if(strcmp(argv[i], "-no-duplicates") == 0)
1846 duplicate_checking = FALSE;
1848 else if(strcmp(argv[i], "-no-fragments") == 0)
1849 no_fragments = TRUE;
1851 else if(strcmp(argv[i], "-always-use-fragments") == 0)
1852 always_use_fragments = TRUE;
1854 else if(strcmp(argv[i], "-sort") == 0) {
1855 if(++i == argc) {
1856 ERROR("%s: -sort missing filename\n", argv[0]);
1857 exit(1);
1859 } else if(strcmp(argv[i], "-all-root") == 0 ||
1860 strcmp(argv[i], "-root-owned") == 0)
1861 global_uid = global_gid = 0;
1863 else if(strcmp(argv[i], "-force-uid") == 0) {
1864 if(++i == argc) {
1865 ERROR("%s: -force-uid missing uid or user\n", argv[0]);
1866 exit(1);
1868 if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') {
1869 if(global_uid < 0 || global_uid > (((long long) 1 << 32) - 1)) {
1870 ERROR("%s: -force-uid uid out of range\n", argv[0]);
1871 exit(1);
1873 } else {
1874 struct passwd *uid = getpwnam(argv[i]);
1875 if(uid)
1876 global_uid = uid->pw_uid;
1877 else {
1878 ERROR("%s: -force-uid invalid uid or unknown user\n", argv[0]);
1879 exit(1);
1882 } else if(strcmp(argv[i], "-force-gid") == 0) {
1883 if(++i == argc) {
1884 ERROR("%s: -force-gid missing gid or group\n", argv[0]);
1885 exit(1);
1887 if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') {
1888 if(global_gid < 0 || global_gid > (((long long) 1 << 32) - 1)) {
1889 ERROR("%s: -force-gid gid out of range\n", argv[0]);
1890 exit(1);
1892 } else {
1893 struct group *gid = getgrnam(argv[i]);
1894 if(gid)
1895 global_gid = gid->gr_gid;
1896 else {
1897 ERROR("%s: -force-gid invalid gid or unknown group\n", argv[0]);
1898 exit(1);
1901 } else if(strcmp(argv[i], "-noI") == 0 ||
1902 strcmp(argv[i], "-noInodeCompression") == 0)
1903 noI = TRUE;
1905 else if(strcmp(argv[i], "-noD") == 0 ||
1906 strcmp(argv[i], "-noDataCompression") == 0)
1907 noD = TRUE;
1909 else if(strcmp(argv[i], "-noF") == 0 ||
1910 strcmp(argv[i], "-noFragmentCompression") == 0)
1911 noF = TRUE;
1913 else if(strcmp(argv[i], "-nopad") == 0)
1914 nopad = TRUE;
1916 else if(strcmp(argv[i], "-check_data") == 0)
1917 check_data = TRUE;
1919 else if(strcmp(argv[i], "-info") == 0)
1920 silent = 0;
1922 else if(strcmp(argv[i], "-be") == 0)
1923 be = TRUE;
1925 else if(strcmp(argv[i], "-le") == 0)
1926 be = FALSE;
1928 else if(strcmp(argv[i], "-e") == 0)
1929 break;
1931 else if(strcmp(argv[i], "-noappend") == 0)
1932 delete = TRUE;
1934 else if(strcmp(argv[i], "-keep-as-directory") == 0)
1935 keep_as_directory = TRUE;
1937 else if(strcmp(argv[i], "-root-becomes") == 0) {
1938 if(++i == argc) {
1939 ERROR("%s: -root-becomes: missing name\n", argv[0]);
1940 exit(1);
1942 root_name = argv[i];
1943 } else if(strcmp(argv[i], "-version") == 0) {
1944 VERSION();
1945 } else {
1946 ERROR("%s: invalid option\n\n", argv[0]);
1947 printOptions:
1948 ERROR("SYNTAX:%s source1 source2 ... dest [options] [-e list of exclude\ndirs/files]\n", argv[0]);
1949 ERROR("\nOptions are\n");
1950 ERROR("-version\t\tprint version, licence and copyright message\n");
1951 ERROR("-info\t\t\tprint files written to filesystem\n");
1952 ERROR("-b <block_size>\t\tset data block to <block_size>. Default %d bytes\n", SQUASHFS_FILE_SIZE);
1953 ERROR("-noI\t\t\tdo not compress inode table\n");
1954 ERROR("-noD\t\t\tdo not compress data blocks\n");
1955 ERROR("-noF\t\t\tdo not compress fragment blocks\n");
1956 ERROR("-no-fragments\t\tdo not use fragments\n");
1957 ERROR("-always-use-fragments\tuse fragment blocks for files larger than block size\n");
1958 ERROR("-no-duplicates\t\tdo not perform duplicate checking\n");
1959 ERROR("-noappend\t\tdo not append to existing filesystem\n");
1960 ERROR("-keep-as-directory\tif one source directory is specified, create a root\n");
1961 ERROR("\t\t\tdirectory containing that directory, rather than the\n");
1962 ERROR("\t\t\tcontents of the directory\n");
1963 ERROR("-root-becomes <name>\twhen appending source files/directories, make the\n");
1964 ERROR("\t\t\toriginal root become a subdirectory in the new root\n");
1965 ERROR("\t\t\tcalled <name>, rather than adding the new source items\n");
1966 ERROR("\t\t\tto the original root\n");
1967 ERROR("-all-root\t\tmake all files owned by root\n");
1968 ERROR("-force-uid uid\t\tset all file uids to uid\n");
1969 ERROR("-force-gid gid\t\tset all file gids to gid\n");
1970 ERROR("-le\t\t\tcreate a little endian filesystem\n");
1971 ERROR("-be\t\t\tcreate a big endian filesystem\n");
1972 ERROR("-nopad\t\t\tdo not pad filesystem to a multiple of 4K\n");
1973 ERROR("-check_data\t\tadd checkdata for greater filesystem checks\n");
1974 ERROR("-root-owned\t\talternative name for -all-root\n");
1975 ERROR("-noInodeCompression\talternative name for -noI\n");
1976 ERROR("-noDataCompression\talternative name for -noD\n");
1977 ERROR("-noFragmentCompression\talternative name for -noF\n");
1978 ERROR("-sort <sort_file>\tsort files according to priorities in <sort_file>. One\n");
1979 ERROR("\t\t\tfile or dir with priority per line. Priority -32768 to\n");
1980 ERROR("\t\t\t32767, default priority 0\n");
1981 ERROR("-ef <exclude_file>\tlist of exclude dirs/files. One per line\n");
1982 exit(1);
1986 for(i = 0; i < source; i++)
1987 if(stat(source_path[i], &source_buf) == -1) {
1988 fprintf(stderr, "Cannot stat source directory \"%s\" because %s\n", source_path[i], strerror(errno));
1989 EXIT_MKSQUASHFS();
1992 destination_file = argv[source + 1];
1993 if(stat(argv[source + 1], &buf) == -1) {
1994 if(errno == ENOENT) { /* Does not exist */
1995 if((fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) == -1) {
1996 perror("Could not create destination file");
1997 exit(1);
1999 delete = TRUE;
2000 } else {
2001 perror("Could not stat destination file");
2002 exit(1);
2005 } else {
2006 if(S_ISBLK(buf.st_mode)) {
2007 if((fd = open(argv[source + 1], O_RDWR)) == -1) {
2008 perror("Could not open block device as destination");
2009 exit(1);
2011 block_device = 1;
2013 } else if(S_ISREG(buf.st_mode)) {
2014 if((fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | O_RDWR)) == -1) {
2015 perror("Could not open regular file for writing as destination");
2016 exit(1);
2019 else {
2020 ERROR("Destination not block device or regular file\n");
2021 exit(1);
2026 if(!delete) {
2027 if(read_super(fd, &sBlk, &orig_be, argv[source + 1]) == 0) {
2028 ERROR("Failed to read existing filesystem - will not overwrite - ABORTING!\n");
2029 EXIT_MKSQUASHFS();
2031 } else {
2032 signal(SIGTERM, sighandler2);
2033 signal(SIGINT, sighandler2);
2036 /* process the exclude files - must be done afer destination file has been possibly created */
2037 for(i = source + 2; i < argc; i++)
2038 if(strcmp(argv[i], "-ef") == 0) {
2039 FILE *fd;
2040 char filename[16385];
2041 if((fd = fopen(argv[++i], "r")) == NULL) {
2042 perror("Could not open exclude file...");
2043 EXIT_MKSQUASHFS();
2045 while(fscanf(fd, "%16384[^\n]\n", filename) != EOF)
2046 add_exclude(filename);
2047 fclose(fd);
2048 } else if(strcmp(argv[i], "-e") == 0)
2049 break;
2050 else if(strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "-root-becomes") == 0 || strcmp(argv[i], "-sort") == 0)
2051 i++;
2053 if(i != argc) {
2054 if(++i == argc) {
2055 ERROR("%s: -e missing arguments\n", argv[0]);
2056 EXIT_MKSQUASHFS();
2058 while(i < argc && add_exclude(argv[i++]));
2061 /* process the sort files - must be done afer the exclude files */
2062 for(i = source + 2; i < argc; i++)
2063 if(strcmp(argv[i], "-sort") == 0) {
2064 read_sort_file(argv[++i], source, source_path);
2065 sorted ++;
2066 } else if(strcmp(argv[i], "-e") == 0)
2067 break;
2068 else if(strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "-root-becomes") == 0 || strcmp(argv[i], "-ef") == 0)
2069 i++;
2071 if(delete) {
2072 printf("Creating %s %d.%d filesystem on %s, block size %d.\n",
2073 be ? "big endian" : "little endian", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size);
2074 bytes = sizeof(squashfs_super_block);
2075 } else {
2076 unsigned int last_directory_block, inode_dir_offset, inode_dir_file_size, root_inode_size,
2077 inode_dir_start_block, uncompressed_data, compressed_data, inode_dir_inode_number,
2078 inode_dir_parent_inode;
2079 unsigned int root_inode_start = SQUASHFS_INODE_BLK(sBlk.root_inode), root_inode_offset =
2080 SQUASHFS_INODE_OFFSET(sBlk.root_inode);
2082 be = orig_be;
2083 block_log = slog(block_size = sBlk.block_size);
2084 noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags);
2085 noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags);
2086 noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags);
2087 check_data = SQUASHFS_CHECK_DATA(sBlk.flags);
2088 no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags);
2089 always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags);
2090 duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags);
2092 if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, &data_cache,
2093 &directory_table, &directory_data_cache, &last_directory_block, &inode_dir_offset,
2094 &inode_dir_file_size, &root_inode_size, &inode_dir_start_block,
2095 &file_count, &sym_count, &dev_count, &dir_count, &fifo_count, &sock_count,
2096 (squashfs_uid *) uids, &uid_count, (squashfs_uid *) guids, &guid_count,
2097 &total_bytes, &total_inode_bytes, &total_directory_bytes, &inode_dir_inode_number,
2098 &inode_dir_parent_inode, add_old_root_entry, &fragment_table)) == 0) {
2099 ERROR("Failed to read existing filesystem - will not overwrite - ABORTING!\n");
2100 EXIT_MKSQUASHFS();
2102 if((fragments = sBlk.fragments))
2103 fragment_table = (squashfs_fragment_entry *) realloc((char *) fragment_table, ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) * sizeof(squashfs_fragment_entry));
2105 printf("Appending to existing %s %d.%d filesystem on %s, block size %d\n", be ? "big endian" :
2106 "little endian", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size);
2107 printf("All -be, -le, -b, -noI, -noD, -noF, -check_data, no-duplicates, no-fragments, -always-use-fragments and -2.0 options ignored\n");
2108 printf("\nIf appending is not wanted, please re-run with -noappend specified!\n\n");
2110 compressed_data = inode_dir_offset + inode_dir_file_size & ~(SQUASHFS_METADATA_SIZE - 1);
2111 uncompressed_data = inode_dir_offset + inode_dir_file_size & (SQUASHFS_METADATA_SIZE - 1);
2113 /* save original filesystem state for restoring ... */
2114 sfragments = fragments;
2115 sbytes = bytes;
2116 sinode_count = sBlk.inodes;
2117 sdata_cache = (char *)malloc(scache_bytes = root_inode_offset + root_inode_size);
2118 sdirectory_data_cache = (char *)malloc(sdirectory_cache_bytes = uncompressed_data);
2119 memcpy(sdata_cache, data_cache, scache_bytes);
2120 memcpy(sdirectory_data_cache, directory_data_cache + compressed_data, sdirectory_cache_bytes);
2121 sinode_bytes = root_inode_start;
2122 sdirectory_bytes = last_directory_block;
2123 suid_count = uid_count;
2124 sguid_count = guid_count;
2125 stotal_bytes = total_bytes;
2126 stotal_inode_bytes = total_inode_bytes;
2127 stotal_directory_bytes = total_directory_bytes + compressed_data;
2128 sfile_count = file_count;
2129 ssym_count = sym_count;
2130 sdev_count = dev_count;
2131 sdir_count = dir_count + 1;
2132 sfifo_count = fifo_count;
2133 ssock_count = sock_count;
2134 sdup_files = dup_files;
2135 restore = TRUE;
2136 if(setjmp(env))
2137 goto restore_filesystem;
2138 signal(SIGTERM, sighandler);
2139 signal(SIGINT, sighandler);
2140 write_bytes(fd, SQUASHFS_START, 4, "\0\0\0\0");
2142 /* set the filesystem state up to be able to append to the original filesystem. The filesystem state
2143 * differs depending on whether we're appending to the original root directory, or if the original
2144 * root directory becomes a sub-directory (root-becomes specified on command line, here root_name != NULL)
2146 inode_bytes = inode_size = root_inode_start;
2147 directory_size = last_directory_block;
2148 cache_size = root_inode_offset + root_inode_size;
2149 directory_cache_size = inode_dir_offset + inode_dir_file_size;
2150 if(root_name) {
2151 root_inode_number = inode_dir_parent_inode;
2152 dir_inode_no = sBlk.inodes + 2;
2153 directory_bytes = last_directory_block;
2154 directory_cache_bytes = uncompressed_data;
2155 memmove(directory_data_cache, directory_data_cache + compressed_data, uncompressed_data);
2156 cache_bytes = root_inode_offset + root_inode_size;
2157 add_old_root_entry(root_name, sBlk.root_inode, inode_dir_inode_number, SQUASHFS_DIR_TYPE);
2158 total_directory_bytes += compressed_data;
2159 dir_count ++;
2160 } else {
2161 root_inode_number = inode_dir_inode_number;
2162 dir_inode_no = sBlk.inodes + 1;
2163 directory_bytes = inode_dir_start_block;
2164 directory_cache_bytes = inode_dir_offset;
2165 cache_bytes = root_inode_offset;
2168 inode_count = file_count + dir_count + sym_count + dev_count + fifo_count + sock_count;
2171 #if __BYTE_ORDER == __BIG_ENDIAN
2172 swap = !be;
2173 #else
2174 swap = be;
2175 #endif
2177 block_offset = check_data ? 3 : 2;
2179 if(delete && !keep_as_directory && source == 1 && S_ISDIR(source_buf.st_mode))
2180 dir_scan(&inode, source_path[0], scan1_readdir);
2181 else if(!keep_as_directory && source == 1 && S_ISDIR(source_buf.st_mode))
2182 dir_scan(&inode, source_path[0], scan1_single_readdir);
2183 else
2184 dir_scan(&inode, "", scan1_encomp_readdir);
2185 sBlk.root_inode = inode;
2186 sBlk.inodes = inode_count;
2187 sBlk.s_magic = SQUASHFS_MAGIC;
2188 sBlk.s_major = SQUASHFS_MAJOR;
2189 sBlk.s_minor = SQUASHFS_MINOR;
2190 sBlk.block_size = block_size;
2191 sBlk.block_log = block_log;
2192 sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, check_data, noF, no_fragments, always_use_fragments, duplicate_checking);
2193 sBlk.mkfs_time = time(NULL);
2195 restore_filesystem:
2196 write_fragment();
2197 sBlk.fragments = fragments;
2198 sBlk.inode_table_start = write_inodes();
2199 sBlk.directory_table_start = write_directories();
2200 sBlk.fragment_table_start = write_fragment_table();
2202 TRACE("sBlk->inode_table_start 0x%x\n", sBlk.inode_table_start);
2203 TRACE("sBlk->directory_table_start 0x%x\n", sBlk.directory_table_start);
2204 TRACE("sBlk->fragment_table_start 0x%x\n", sBlk.fragment_table_start);
2206 if(sBlk.no_uids = uid_count) {
2207 if(!swap)
2208 write_bytes(fd, bytes, uid_count * sizeof(squashfs_uid), (char *) uids);
2209 else {
2210 squashfs_uid uids_copy[uid_count];
2212 SQUASHFS_SWAP_DATA(uids, uids_copy, uid_count, sizeof(squashfs_uid) * 8);
2213 write_bytes(fd, bytes, uid_count * sizeof(squashfs_uid), (char *) uids_copy);
2215 sBlk.uid_start = bytes;
2216 bytes += uid_count * sizeof(squashfs_uid);
2217 } else
2218 sBlk.uid_start = 0;
2220 if(sBlk.no_guids = guid_count) {
2221 if(!swap)
2222 write_bytes(fd, bytes, guid_count * sizeof(squashfs_uid), (char *) guids);
2223 else {
2224 squashfs_uid guids_copy[guid_count];
2226 SQUASHFS_SWAP_DATA(guids, guids_copy, guid_count, sizeof(squashfs_uid) * 8);
2227 write_bytes(fd, bytes, guid_count * sizeof(squashfs_uid), (char *) guids_copy);
2229 sBlk.guid_start = bytes;
2230 bytes += guid_count * sizeof(squashfs_uid);
2231 } else
2232 sBlk.guid_start = 0;
2234 sBlk.bytes_used = bytes;
2236 sBlk.unused = SQUASHFS_INVALID_BLK;
2238 if(!swap)
2239 write_bytes(fd, SQUASHFS_START, sizeof(squashfs_super_block), (char *) &sBlk);
2240 else {
2241 squashfs_super_block sBlk_copy;
2243 SQUASHFS_SWAP_SUPER_BLOCK((&sBlk), &sBlk_copy);
2244 write_bytes(fd, SQUASHFS_START, sizeof(squashfs_super_block), (char *) &sBlk_copy);
2247 if(!nopad && (i = bytes & (4096 - 1))) {
2248 char temp[4096] = {0};
2249 write_bytes(fd, bytes, 4096 - i, temp);
2252 total_bytes += total_inode_bytes + total_directory_bytes + uid_count
2253 * sizeof(unsigned short) + guid_count * sizeof(unsigned short) +
2254 sizeof(squashfs_super_block);
2256 printf("\n%s filesystem, data block size %d, %s data, %s metadata, %s fragments\n", be ?
2257 "Big endian" : "Little endian", block_size, noD ? "uncompressed" : "compressed", noI ?
2258 "uncompressed" : "compressed", no_fragments ? "no" : noF ? "uncompressed" : "compressed");
2259 printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, bytes / (1024.0 * 1024.0));
2260 printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n",
2261 ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0);
2262 printf("Inode table size %d bytes (%.2f Kbytes)\n",
2263 inode_bytes, inode_bytes / 1024.0);
2264 printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n",
2265 ((float) inode_bytes / total_inode_bytes) * 100.0, total_inode_bytes);
2266 printf("Directory table size %d bytes (%.2f Kbytes)\n",
2267 directory_bytes, directory_bytes / 1024.0);
2268 printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n",
2269 ((float) directory_bytes / total_directory_bytes) * 100.0, total_directory_bytes);
2270 if(duplicate_checking)
2271 printf("Number of duplicate files found %d\n", file_count - dup_files);
2272 else
2273 printf("No duplicate files removed\n");
2274 printf("Number of inodes %d\n", inode_count);
2275 printf("Number of files %d\n", file_count);
2276 if(!no_fragments)
2277 printf("Number of fragments %d\n", fragments);
2278 printf("Number of symbolic links %d\n", sym_count);
2279 printf("Number of device nodes %d\n", dev_count);
2280 printf("Number of fifo nodes %d\n", fifo_count);
2281 printf("Number of socket nodes %d\n", sock_count);
2282 printf("Number of directories %d\n", dir_count);
2283 printf("Number of uids %d\n", uid_count);
2285 for(i = 0; i < uid_count; i++) {
2286 struct passwd *user = getpwuid(uids[i]);
2287 printf("\t%s (%d)\n", user == NULL ? "unknown" : user->pw_name, uids[i]);
2290 printf("Number of gids %d\n", guid_count);
2292 for(i = 0; i < guid_count; i++) {
2293 struct group *group = getgrgid(guids[i]);
2294 printf("\t%s (%d)\n", group == NULL ? "unknown" : group->gr_name, guids[i]);
2296 close(fd);
2297 return 0;