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.
32 #include <sys/types.h>
45 #define __BYTE_ORDER BYTE_ORDER
46 #define __BIG_ENDIAN BIG_ENDIAN
47 #define __LITTLE_ENDIAN LITTLE_ENDIAN
52 #include <squashfs_fs.h>
53 #include "mksquashfs.h"
58 #define TRACE(s, args...) do { \
59 printf("mksquashfs: "s, ## args); \
62 #define TRACE(s, args...)
65 #define INFO(s, args...) do {\
67 printf("mksquashfs: "s, ## args);\
69 #define ERROR(s, args...) do {\
70 fprintf(stderr, s, ## args);\
72 #define EXIT_MKSQUASHFS() do {\
75 if(delete && destination_file && !block_device)\
76 unlink(destination_file);\
79 #define BAD_ERROR(s, args...) do {\
80 fprintf(stderr, "FATAL ERROR:" s, ##args);\
85 long long total_compressed
= 0, total_uncompressed
= 0;
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
];
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
;
133 unsigned int start_block
;
137 unsigned int entry_count
;
138 unsigned char *entry_count_p
;
139 unsigned int i_count
;
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];
152 /* list of exclude dirs/files */
153 struct exclude_info
{
158 #define EXCLUDE_SIZE 8192
160 struct exclude_info
*exclude_paths
= NULL
;
161 int excluded(char *filename
, struct stat
*buf
);
163 /* fragment block data structures */
165 char fragment_data
[SQUASHFS_FILE_SIZE
];
166 int fragment_size
= 0;
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 */
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
;
193 struct old_root_entry_info
*old_root_entry
;
195 /* in memory file info */
198 unsigned short checksum
;
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 */
209 /* restore orignal filesystem state if appending to existing filesystem is cancelled */
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
;
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) */
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
{
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)))
263 ERROR("Exiting - restoring original filesystem!\n\n");
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
;
293 ERROR("Interrupting will restore original filesystem!\n");
294 ERROR("Interrupt again to quit\n");
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;
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");
317 BAD_ERROR("zlib::compress failed, unknown error %d\n", res
);
321 if(uncompressed
|| c_byte
>= 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
)
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))
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
);
348 memcpy(inode_table
+ inode_bytes
, &c_byte
, sizeof(unsigned short));
350 SQUASHFS_SWAP_SHORTS((&c_byte
), (inode_table
+ inode_bytes
), 1);
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
) {
366 cache_size
+= realloc_size
;
369 cache_bytes
+= req_size
;
371 return (squashfs_base_inode_header
*)(data_cache
+ (cache_bytes
- req_size
));
374 BAD_ERROR("Out of memory in inode table reallocation!\n");
378 void read_bytes(int fd
, long long byte
, int bytes
, char *buff
)
382 if(lseek(fd
, off
, SEEK_SET
) == -1) {
383 perror("Lseek on destination failed");
387 if(read(fd
, buff
, bytes
) == -1) {
388 perror("Read on destination failed");
394 void write_bytes(int fd
, long long byte
, int bytes
, char *buff
)
398 if(lseek(fd
, off
, SEEK_SET
) == -1) {
399 perror("Lseek on destination failed");
403 if(write(fd
, buff
, bytes
) == -1) {
404 perror("Write on destination failed");
410 long long write_inodes()
412 unsigned short c_byte
;
414 char *datap
= data_cache
;
415 long long start_bytes
= 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
);
428 memcpy(inode_table
+ inode_bytes
, &c_byte
, sizeof(unsigned short));
430 SQUASHFS_SWAP_SHORTS((&c_byte
), (inode_table
+ inode_bytes
), 1);
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
;
446 long long write_directories()
448 unsigned short c_byte
;
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
);
465 memcpy(directory_table
+ directory_bytes
, &c_byte
, sizeof(unsigned short));
467 SQUASHFS_SWAP_SHORTS((&c_byte
), (directory_table
+ directory_bytes
), 1);
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
;
482 unsigned int get_uid(squashfs_uid uid
)
486 for(i
= 0; (i
< uid_count
) && uids
[i
] != uid
; i
++);
488 if(uid_count
== SQUASHFS_UIDS
) {
489 ERROR("Out of uids! - using uid 0 - probably not what's wanted!\n");
492 uids
[uid_count
++] = uid
;
499 unsigned int get_guid(squashfs_uid uid
, squashfs_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
;
512 guids
[guid_count
++] = guid
;
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
) {
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
;
546 memcpy(inodep
, reg
, sizeof(*reg
));
547 memcpy(inodep
->block_list
, block_list
, offset
* sizeof(unsigned int));
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
) {
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
;
564 reg
->file_size
= byte_size
;
565 reg
->start_block
= start_block
;
566 reg
->fragment
= fragment
->index
;
567 reg
->offset
= fragment
->offset
;
569 memcpy(inodep
, reg
, sizeof(*reg
));
570 memcpy(inodep
->block_list
, block_list
, offset
* sizeof(unsigned int));
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
) {
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
;
602 memcpy(inode
, dir
, sizeof(*dir
));
604 SQUASHFS_SWAP_LDIR_INODE_HEADER(dir
, inode
);
605 p
= (unsigned char *) inodep
->index
;
606 for(i
= 0; i
< i_count
; i
++) {
608 memcpy(p
, &index
[i
].index
, sizeof(squashfs_dir_index
));
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
;
627 memcpy(inode
, dir
, sizeof(*dir
));
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
));
638 dev
->rdev
= (unsigned short) ((major(buf
->st_rdev
) << 8) |
639 (minor(buf
->st_rdev
) & 0xff));
641 memcpy(inode
, dev
, sizeof(*dev
));
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
;
651 if((byte
= readlink(filename
, buff
, 65536)) == -1) {
652 perror("Error in reading symbolic link, skipping...");
657 ERROR("Symlink is greater than 65536 bytes! skipping...");
661 inode
= get_inode(sizeof(*symlink
) + byte
);
662 symlink
->nlink
= nlink
;
663 inodep
= (squashfs_symlink_inode_header
*) inode
;
664 symlink
->symlink_size
= byte
;
666 memcpy(inode
, symlink
, sizeof(*symlink
));
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
));
678 memcpy(inode
, ipc
, sizeof(*ipc
));
680 SQUASHFS_SWAP_IPC_INODE_HEADER(ipc
, inode
);
681 TRACE("ipc inode, type %s, nlink %d\n", type
== SQUASHFS_FIFO_TYPE
? "fifo" : "socket", nlink
);
685 *i_no
= MKINODE(inode
);
688 TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no
, type
, base
->uid
, base
->guid
);
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
;
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
)
712 squashfs_dir_entry idir
, *idirp
;
713 unsigned int start_block
= inode
>> 16;
714 unsigned int offset
= inode
& 0xffff;
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
;
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
;
753 memcpy(dir
->entry_count_p
, &dir_header
, sizeof(dir_header
));
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
;
770 idir
.size
= size
- 1;
771 idir
.inode_number
= ((long long) inode_number
- dir
->inode_number
);
773 memcpy(idirp
, &idir
, sizeof(idir
));
775 SQUASHFS_SWAP_DIR_ENTRY((&idir
), idirp
);
776 strncpy(idirp
->name
, name
, size
);
777 dir
->p
+= sizeof(squashfs_dir_entry
) + size
;
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
) {
795 directory_cache_size
+= realloc_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
;
805 memcpy(dir
->entry_count_p
, &dir_header
, sizeof(dir_header
));
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
;
814 index
= SQUASHFS_METADATA_SIZE
- directory_offset
;
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
)
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
) {
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
);
836 memcpy(directory_table
+ directory_bytes
, &c_byte
, sizeof(unsigned short));
838 SQUASHFS_SWAP_SHORTS((&c_byte
), (directory_table
+ directory_bytes
), 1);
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
)
851 if(create_inode(inode
, dir_info
->dir_ent
, SQUASHFS_DIR_TYPE
, dir_size
+ 3, directory_block
, directory_offset
, NULL
, NULL
, NULL
) == FALSE
)
855 #ifdef SQUASHFS_TRACE
860 TRACE("Directory contents of inode 0x%llx\n", *inode
);
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
);
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;
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
)) {
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");
910 BAD_ERROR("zlib::uncompress failed, unknown error %d\n", res
);
913 read_bytes(fd
, disk_fragment
->start_block
, size
, buffer
);
915 return buffer
+ fragment
->offset
;
919 void write_fragment()
922 char buffer
[block_size
<< 1];
924 if(fragment_size
== 0)
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
);
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
;
949 return &empty_fragment
;
951 if(fragment_size
+ size
> block_size
)
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
;
960 memcpy(fragment_data
+ fragment_size
, buff
, size
);
961 fragment_size
+= size
;
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
);
982 memcpy(p
, &fragment_table
[i
], sizeof(squashfs_fragment_entry
));
984 SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_table
[i
], p
);
987 for(i
= 0; i
< meta_blocks
; i
++) {
988 int avail_bytes
= frag_bytes
> SQUASHFS_METADATA_SIZE
? SQUASHFS_METADATA_SIZE
: frag_bytes
;
989 c_byte
= mangle(cbuffer
+ block_offset
, buffer
+ i
* SQUASHFS_METADATA_SIZE
, avail_bytes
, SQUASHFS_METADATA_SIZE
, noF
, 0);
991 memcpy(cbuffer
, &c_byte
, sizeof(unsigned short));
993 SQUASHFS_SWAP_SHORTS((&c_byte
), cbuffer
, 1);
995 *((unsigned char *)(cbuffer
+ block_offset
- 1)) = SQUASHFS_MARKER_BYTE
;
997 compressed_size
= SQUASHFS_COMPRESSED_SIZE(c_byte
) + block_offset
;
998 write_bytes(fd
, bytes
, compressed_size
, cbuffer
);
999 bytes
+= compressed_size
;
1000 frag_bytes
-= avail_bytes
;
1004 write_bytes(fd
, bytes
, sizeof(list
), (char *) list
);
1006 squashfs_fragment_index slist
[meta_blocks
];
1007 SQUASHFS_SWAP_FRAGMENT_INDEXES(list
, slist
, meta_blocks
);
1008 write_bytes(fd
, bytes
, sizeof(list
), (char *) slist
);
1011 start_bytes
= bytes
;
1012 bytes
+= sizeof(list
);
1018 char *read_from_buffer(struct duplicate_buffer_handle
*handle
, unsigned int avail_bytes
)
1020 char *v
= handle
->ptr
;
1021 handle
->ptr
+= avail_bytes
;
1026 char read_from_file_buffer
[SQUASHFS_FILE_MAX_SIZE
];
1027 char *read_from_file(struct duplicate_buffer_handle
*handle
, unsigned int avail_bytes
)
1029 read_bytes(fd
, handle
->start
, avail_bytes
, read_from_file_buffer
);
1030 handle
->start
+= avail_bytes
;
1031 return read_from_file_buffer
;
1036 * Compute 16 bit BSD checksum over the data
1038 unsigned short get_checksum(char *(get_next_file_block
)(struct duplicate_buffer_handle
*, unsigned int), struct duplicate_buffer_handle
*handle
, long long l
)
1040 unsigned short chksum
= 0;
1041 unsigned int bytes
= 0;
1043 struct duplicate_buffer_handle position
= *handle
;
1046 bytes
= l
> SQUASHFS_FILE_MAX_SIZE
? SQUASHFS_FILE_MAX_SIZE
: l
;
1048 b
= (unsigned char *) get_next_file_block(&position
, bytes
);
1050 chksum
= (chksum
& 1) ? (chksum
>> 1) | 0x8000 : chksum
>> 1;
1059 int cached_frag
= -1;
1060 void add_file(long long start
, long long file_bytes
, unsigned int *block_listp
, int blocks
, unsigned int fragment
, int offset
, int bytes
)
1062 struct fragment
*frg
;
1063 struct file_info
*dupl_ptr
;
1065 struct duplicate_buffer_handle handle
;
1066 unsigned int *block_list
= block_listp
;
1068 if(!duplicate_checking
)
1071 if((frg
= (struct fragment
*) malloc(sizeof(struct fragment
))) == NULL
)
1072 BAD_ERROR("Out of memory in fragment block allocation!\n");
1074 frg
->index
= fragment
;
1075 frg
->offset
= offset
;
1077 if(fragment
== cached_frag
|| fragment
== SQUASHFS_INVALID_FRAG
)
1078 datap
= fragment_data
+ offset
;
1080 datap
= get_fragment(fragment_data
, frg
);
1081 handle
.start
= start
;
1082 if((dupl_ptr
= duplicate(read_from_file
, &handle
, file_bytes
, &block_listp
, &start
, blocks
, &frg
, datap
, bytes
)) != NULL
)
1083 dupl_ptr
->fragment
= frg
;
1086 cached_frag
= fragment
;
1090 char cached_fragment
[SQUASHFS_FILE_SIZE
];
1091 int cached_frag1
= -1;
1093 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
)
1095 unsigned short checksum
= get_checksum(get_next_file_block
, file_start
, bytes
);
1096 struct duplicate_buffer_handle handle
= { frag_data
, 0 };
1097 unsigned short fragment_checksum
= get_checksum(read_from_buffer
, &handle
, frag_bytes
);
1098 struct file_info
*dupl_ptr
= bytes
? dupl
[checksum
] : frag_dups
[fragment_checksum
];
1101 for(; dupl_ptr
; dupl_ptr
= dupl_ptr
->next
)
1102 if(bytes
== dupl_ptr
->bytes
&& frag_bytes
== dupl_ptr
->fragment
->size
&& fragment_checksum
== dupl_ptr
->fragment_checksum
) {
1103 char buffer1
[SQUASHFS_FILE_MAX_SIZE
];
1104 long long dup_bytes
= dupl_ptr
->bytes
;
1105 long long dup_start
= dupl_ptr
->start
;
1106 struct duplicate_buffer_handle position
= *file_start
;
1109 int avail_bytes
= dup_bytes
> SQUASHFS_FILE_MAX_SIZE
? SQUASHFS_FILE_MAX_SIZE
: dup_bytes
;
1111 buffer
= get_next_file_block(&position
, avail_bytes
);
1112 read_bytes(fd
, dup_start
, avail_bytes
, buffer1
);
1113 if(memcmp(buffer
, buffer1
, avail_bytes
) != 0)
1115 dup_bytes
-= avail_bytes
;
1116 dup_start
+= avail_bytes
;
1118 if(dup_bytes
== 0) {
1119 char *fragment_buffer1
;
1121 if(dupl_ptr
->fragment
->index
== fragments
|| dupl_ptr
->fragment
->index
== SQUASHFS_INVALID_FRAG
)
1122 fragment_buffer1
= fragment_data
+ dupl_ptr
->fragment
->offset
;
1123 else if(dupl_ptr
->fragment
->index
== cached_frag1
)
1124 fragment_buffer1
= cached_fragment
+ dupl_ptr
->fragment
->offset
;
1126 fragment_buffer1
= get_fragment(cached_fragment
, dupl_ptr
->fragment
);
1127 cached_frag1
= dupl_ptr
->fragment
->index
;
1130 if(frag_bytes
== 0 || memcmp(frag_data
, fragment_buffer1
, frag_bytes
) == 0) {
1131 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
,
1132 dupl_ptr
->bytes
, dupl_ptr
->checksum
, dupl_ptr
->fragment
->index
, frag_bytes
, dupl_ptr
->fragment
->offset
, fragment_checksum
);
1133 *block_list
= dupl_ptr
->block_list
;
1134 *start
= dupl_ptr
->start
;
1135 *fragment
= dupl_ptr
->fragment
;
1142 if((dupl_ptr
= (struct file_info
*) malloc(sizeof(struct file_info
))) == NULL
) {
1143 BAD_ERROR("Out of memory in dup_files allocation!\n");
1146 dupl_ptr
->bytes
= bytes
;
1147 dupl_ptr
->checksum
= checksum
;
1148 dupl_ptr
->start
= *start
;
1149 dupl_ptr
->fragment_checksum
= fragment_checksum
;
1150 dupl_ptr
->block_list
= *block_list
;
1154 dupl_ptr
->next
= dupl
[checksum
];
1155 dupl
[checksum
] = dupl_ptr
;
1157 dupl_ptr
->next
= frag_dups
[fragment_checksum
];
1158 frag_dups
[fragment_checksum
] = dupl_ptr
;
1165 #define MINALLOCBYTES (1024 * 1024)
1166 int write_file(squashfs_inode
*inode
, struct dir_ent
*dir_ent
, long long size
, int *duplicate_file
)
1168 int block
= 0, i
, file
, whole_file
= 1, status
;
1169 unsigned int c_byte
, frag_bytes
;
1170 long long bbytes
, file_bytes
= 0, start
;
1171 char buff
[block_size
], *c_buffer
= NULL
, *filename
= dir_ent
->pathname
;
1172 struct fragment
*fragment
;
1173 struct file_info
*dupl_ptr
= NULL
;
1174 struct duplicate_buffer_handle handle
;
1175 long long read_size
= (size
> SQUASHFS_MAX_FILE_SIZE
) ? SQUASHFS_MAX_FILE_SIZE
: size
;
1176 int blocks
= (read_size
+ block_size
- 1) >> block_log
, allocated_blocks
= blocks
;
1177 unsigned int *block_list
, *block_listp
;
1179 if((block_list
= malloc(blocks
* sizeof(unsigned int))) == NULL
)
1180 BAD_ERROR("Out of memory allocating block_list\n");
1181 block_listp
= block_list
;
1183 if(!no_fragments
&& (read_size
< block_size
|| always_use_fragments
)) {
1184 allocated_blocks
= blocks
= read_size
>> block_log
;
1185 frag_bytes
= read_size
% block_size
;
1189 if(size
> read_size
)
1190 ERROR("file %s truncated to %lld bytes\n", filename
, SQUASHFS_MAX_FILE_SIZE
);
1192 total_bytes
+= read_size
;
1193 if((file
= open(filename
, O_RDONLY
)) == -1)
1197 long long bytes
= (((long long) allocated_blocks
) + 1) << block_log
;
1198 if(bytes
!= ((size_t) bytes
) || (c_buffer
= (char *) malloc(bytes
)) == NULL
) {
1199 TRACE("Out of memory allocating write_file buffer, allocated_blocks %ld, blocks %d\n", allocated_blocks
, blocks
);
1201 if(bytes
< MINALLOCBYTES
)
1202 BAD_ERROR("Out of memory allocating write_file buffer, could not allocate %ld blocks (%d Kbytes)\n", allocated_blocks
, allocated_blocks
<< (block_log
- 10));
1203 allocated_blocks
>>= 1;
1207 for(start
= bytes
; block
< blocks
; file_bytes
+= bbytes
) {
1208 for(i
= 0, bbytes
= 0; (i
< allocated_blocks
) && (block
< blocks
); i
++) {
1209 int available_bytes
= read_size
- (block
* block_size
) > block_size
? block_size
: read_size
- (block
* block_size
);
1210 if(read(file
, buff
, available_bytes
) == -1)
1212 c_byte
= mangle(c_buffer
+ bbytes
, buff
, available_bytes
, block_size
, noD
, 1);
1213 block_list
[block
++] = c_byte
;
1214 bbytes
+= SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte
);
1217 write_bytes(fd
, bytes
, bbytes
, c_buffer
);
1223 if(read(file
, buff
, frag_bytes
) == -1)
1228 handle
.ptr
= c_buffer
;
1229 if(duplicate_checking
&& (dupl_ptr
= duplicate(read_from_buffer
, &handle
, file_bytes
, &block_listp
, &start
, blocks
, &fragment
, buff
, frag_bytes
)) == NULL
) {
1230 *duplicate_file
= TRUE
;
1233 write_bytes(fd
, bytes
, file_bytes
, c_buffer
);
1234 bytes
+= file_bytes
;
1236 handle
.start
= start
;
1237 if(duplicate_checking
&& (dupl_ptr
= duplicate(read_from_file
, &handle
, file_bytes
, &block_listp
, &start
, blocks
, &fragment
, buff
, frag_bytes
)) == NULL
) {
1240 ftruncate(fd
, bytes
);
1241 *duplicate_file
= TRUE
;
1246 fragment
= get_and_fill_fragment(buff
, frag_bytes
);
1247 if(duplicate_checking
)
1248 dupl_ptr
->fragment
= fragment
;
1250 *duplicate_file
= FALSE
;
1255 if(dir_ent
->inode
->nlink
== 1 && read_size
< ((long long) (1<<30) - 1))
1256 status
= create_inode(inode
, dir_ent
, SQUASHFS_FILE_TYPE
, read_size
, start
, blocks
, block_listp
, fragment
, NULL
);
1258 status
= create_inode(inode
, dir_ent
, SQUASHFS_LREG_TYPE
, read_size
, start
, blocks
, block_listp
, fragment
, NULL
);
1259 if(duplicate_checking
== FALSE
|| *duplicate_file
== TRUE
)
1264 perror("Error in reading file, skipping...");
1271 char b_buffer
[8192];
1275 char *getbase(char *pathname
)
1279 if(*pathname
!= '/') {
1280 result
= getenv("PWD");
1281 strcat(strcat(strcpy(b_buffer
, result
), "/"), pathname
);
1283 strcpy(b_buffer
, pathname
);
1285 if(((result
= basename_r()) == NULL
) || (strcmp(result
, "..") == 0))
1303 while(*name
!= '\0' && *name
!= '/') name
++;
1306 while(*name
== '/') name
++;
1307 if(strncmp(s
, ".", n
) == 0)
1309 if((*name
== '\0') || (strncmp(s
, "..", n
) == 0) || ((p
= basename_r()) == NULL
)) {
1313 if(strcmp(p
, "..") == 0)
1320 struct inode_info
*lookup_inode(struct stat
*buf
)
1322 int inode_hash
= INODE_HASH(buf
->st_dev
, buf
->st_ino
);
1323 struct inode_info
*inode
= inode_info
[inode_hash
];
1325 while(inode
!= NULL
) {
1326 if(memcmp(buf
, &inode
->buf
, sizeof(struct stat
)) == 0) {
1330 inode
= inode
->next
;
1333 if((inode
= malloc(sizeof(struct inode_info
))) == NULL
)
1334 BAD_ERROR("Out of memory in inode hash table entry allocation\n");
1336 memcpy(&inode
->buf
, buf
, sizeof(struct stat
));
1337 inode
->inode
= SQUASHFS_INVALID_BLK
;
1339 if((buf
->st_mode
& S_IFMT
) == S_IFDIR
)
1340 inode
->inode_number
= dir_inode_no
++;
1342 inode
->inode_number
= inode_no
++;
1344 inode
->next
= inode_info
[inode_hash
];
1345 inode_info
[inode_hash
] = inode
;
1351 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
)
1353 if((dir
->count
% DIR_ENTRIES
) == 0)
1354 if((dir
->list
= realloc(dir
->list
, (dir
->count
+ DIR_ENTRIES
) * sizeof(struct dir_ent
*))) == NULL
)
1355 BAD_ERROR("Out of memory in add_dir_entry\n");
1357 if((dir
->list
[dir
->count
] = malloc(sizeof(struct dir_ent
))) == NULL
)
1358 BAD_ERROR("Out of memory in linux_opendir\n");
1361 sub_dir
->dir_ent
= dir
->list
[dir
->count
];
1362 dir
->list
[dir
->count
]->name
= strdup(name
);
1363 dir
->list
[dir
->count
]->pathname
= pathname
!= NULL
? strdup(pathname
) : NULL
;
1364 dir
->list
[dir
->count
]->inode
= inode_info
;
1365 dir
->list
[dir
->count
]->dir
= sub_dir
;
1366 dir
->list
[dir
->count
]->our_dir
= dir
;
1367 dir
->list
[dir
->count
++]->data
= data
;
1368 dir
->byte_count
+= strlen(name
) + sizeof(squashfs_dir_entry
);
1372 int compare_name(const void *ent1_ptr
, const void *ent2_ptr
)
1374 struct dir_ent
*ent1
= *((struct dir_ent
**) ent1_ptr
);
1375 struct dir_ent
*ent2
= *((struct dir_ent
**) ent2_ptr
);
1377 return strcmp(ent1
->name
, ent2
->name
);
1381 void sort_directory(struct dir_info
*dir
)
1383 qsort(dir
->list
, dir
->count
, sizeof(struct dir_ent
*), compare_name
);
1385 if((dir
->count
< 257 && dir
->byte_count
< SQUASHFS_METADATA_SIZE
))
1386 dir
->dir_is_ldir
= FALSE
;
1390 struct dir_info
*scan1_opendir(char *pathname
)
1393 struct dirent
*d_name
;
1394 struct dir_info
*dir
;
1396 if((dir
= malloc(sizeof(struct dir_info
))) == NULL
)
1399 if(pathname
[0] != '\0' && (dir
->linuxdir
= opendir(pathname
)) == NULL
) {
1403 dir
->pathname
= strdup(pathname
);
1404 dir
->count
= dir
->directory_count
= dir
->current_count
= dir
->byte_count
= 0;
1405 dir
->dir_is_ldir
= TRUE
;
1412 int scan1_encomp_readdir(char *pathname
, char *dir_name
, struct dir_info
*dir
)
1416 static int index
= 0;
1418 if(dir
->count
< old_root_entries
)
1419 for(i
= 0; i
< old_root_entries
; i
++) {
1420 if(old_root_entry
[i
].type
== SQUASHFS_DIR_TYPE
)
1421 dir
->directory_count
++;
1422 add_dir_entry(old_root_entry
[i
].name
, "", NULL
, NULL
, &old_root_entry
[i
], dir
);
1425 while(index
< source
) {
1426 if((basename
= getbase(source_path
[index
])) == NULL
) {
1427 ERROR("Bad source directory %s - skipping ...\n", source_path
[index
]);
1431 strcpy(dir_name
, basename
);
1434 for(n
= 0; n
< dir
->count
&& strcmp(dir
->list
[n
]->name
, dir_name
) != 0; n
++);
1437 ERROR("Source directory entry %s already used! - trying ", dir_name
);
1438 sprintf(dir_name
, "%s_%d", basename
, pass
++);
1439 ERROR("%s\n", dir_name
);
1441 strcpy(pathname
, source_path
[index
++]);
1448 int scan1_single_readdir(char *pathname
, char *dir_name
, struct dir_info
*dir
)
1450 struct dirent
*d_name
;
1453 if(dir
->count
< old_root_entries
)
1454 for(i
= 0; i
< old_root_entries
; i
++) {
1455 if(old_root_entry
[i
].type
== SQUASHFS_DIR_TYPE
)
1456 dir
->directory_count
++;
1457 add_dir_entry(old_root_entry
[i
].name
, "", NULL
, NULL
, &old_root_entry
[i
], dir
);
1460 if((d_name
= readdir(dir
->linuxdir
)) != NULL
) {
1461 strcpy(dir_name
, d_name
->d_name
);
1464 for(i
= 0; i
< dir
->count
&& strcmp(dir
->list
[i
]->name
, dir_name
) != 0; i
++);
1467 ERROR("Source directory entry %s already used! - trying ", dir_name
);
1468 sprintf(dir_name
, "%s_%d", d_name
->d_name
, pass
++);
1469 ERROR("%s\n", dir_name
);
1471 strcat(strcat(strcpy(pathname
, dir
->pathname
), "/"), d_name
->d_name
);
1479 int scan1_readdir(char *pathname
, char *dir_name
, struct dir_info
*dir
)
1481 struct dirent
*d_name
;
1483 if((d_name
= readdir(dir
->linuxdir
)) != NULL
) {
1484 strcpy(dir_name
, d_name
->d_name
);
1485 strcat(strcat(strcpy(pathname
, dir
->pathname
), "/"), d_name
->d_name
);
1493 struct dir_ent
*scan2_readdir(struct directory
*dir
, struct dir_info
*dir_info
)
1497 while((current_count
= dir_info
->current_count
++) < dir_info
->count
)
1498 if(dir_info
->list
[current_count
]->data
)
1499 add_dir(dir_info
->list
[current_count
]->data
->inode
, dir_info
->list
[current_count
]->data
->inode_number
,
1500 dir_info
->list
[current_count
]->name
, dir_info
->list
[current_count
]->data
->type
, dir
);
1502 return dir_info
->list
[current_count
];
1507 void scan1_freedir(struct dir_info
*dir
)
1509 if(dir
->pathname
[0] != '\0')
1510 closedir(dir
->linuxdir
);
1514 void scan2_freedir(struct directory
*dir
)
1522 void dir_scan(squashfs_inode
*inode
, char *pathname
, int (_readdir
)(char *, char *, struct dir_info
*))
1524 struct dir_info
*dir_info
= dir_scan1(pathname
, _readdir
);
1525 struct dir_ent
*dir_ent
;
1526 struct inode_info
*inode_info
;
1528 if(dir_info
== NULL
)
1531 if((dir_ent
= malloc(sizeof(struct dir_ent
))) == NULL
)
1532 BAD_ERROR("Out of memory in dir_scan\n");
1534 if((inode_info
= malloc(sizeof(struct inode_info
))) == NULL
)
1535 BAD_ERROR("Out of memory in dir_scan\n");
1537 dir_ent
->name
= dir_ent
->pathname
= strdup(pathname
);
1538 dir_ent
->dir
= dir_info
;
1539 dir_ent
->inode
= inode_info
;
1540 dir_ent
->our_dir
= NULL
;
1541 dir_ent
->data
= NULL
;
1542 inode_info
->nlink
= 1;
1543 inode_info
->inode_number
= root_inode_number
? root_inode_number
: dir_inode_no
++;
1544 dir_info
->dir_ent
= dir_ent
;
1546 if(pathname
[0] == '\0') {
1547 /* dummy top level directory, if multiple sources specified on command line */
1548 inode_info
->buf
.st_mode
= S_IRWXU
| S_IRWXG
| S_IRWXO
;
1549 inode_info
->buf
.st_uid
= getuid();
1550 inode_info
->buf
.st_gid
= getgid();
1551 inode_info
->buf
.st_mtime
= time(NULL
);
1552 } else if(lstat(pathname
, &inode_info
->buf
) == -1) {
1554 sprintf(buffer
, "Cannot stat dir/file %s, ignoring", pathname
);
1559 sort_files_and_write(dir_info
);
1560 dir_scan2(inode
, dir_info
);
1564 struct dir_info
*dir_scan1(char *pathname
, int (_readdir
)(char *, char *, struct dir_info
*))
1566 struct dir_info
*dir
, *sub_dir
;
1568 char filename
[8192], dir_name
[8192];
1570 if((dir
= scan1_opendir(pathname
)) == NULL
) {
1571 ERROR("Could not open %s, skipping...\n", pathname
);
1575 while(_readdir(filename
, dir_name
, dir
) != FALSE
) {
1577 if(strcmp(dir_name
, ".") == 0 || strcmp(dir_name
, "..") == 0)
1580 if(lstat(filename
, &buf
) == -1) {
1582 sprintf(buffer
, "Cannot stat dir/file %s, ignoring", filename
);
1586 if(excluded(filename
, &buf
))
1589 if((buf
.st_mode
& S_IFMT
) == S_IFDIR
) {
1590 if((sub_dir
= dir_scan1(filename
, scan1_readdir
)) == NULL
)
1592 dir
->directory_count
++;
1596 add_dir_entry(dir_name
, filename
, sub_dir
, lookup_inode(&buf
), NULL
, dir
);
1600 sort_directory(dir
);
1607 int dir_scan2(squashfs_inode
*inode
, struct dir_info
*dir_info
)
1612 char *pathname
= dir_info
->pathname
;
1613 struct directory dir
;
1614 struct dir_ent
*dir_ent
;
1616 scan2_init_dir(&dir
);
1618 while((dir_ent
= scan2_readdir(&dir
, dir_info
)) != NULL
) {
1619 struct inode_info
*inode_info
= dir_ent
->inode
;
1620 struct stat
*buf
= &inode_info
->buf
;
1621 char *filename
= dir_ent
->pathname
;
1622 char *dir_name
= dir_ent
->name
;
1623 unsigned int inode_number
= ((buf
->st_mode
& S_IFMT
) == S_IFDIR
) ? dir_ent
->inode
->inode_number
: dir_ent
->inode
->inode_number
+ dir_inode_no
;
1625 if(dir_ent
->inode
->inode
== SQUASHFS_INVALID_BLK
) {
1626 switch(buf
->st_mode
& S_IFMT
) {
1628 squashfs_type
= SQUASHFS_FILE_TYPE
;
1629 result
= write_file(inode
, dir_ent
, buf
->st_size
, &duplicate_file
);
1630 INFO("file %s, uncompressed size %lld bytes %s\n", filename
, buf
->st_size
, duplicate_file
? "DUPLICATE" : "");
1634 squashfs_type
= SQUASHFS_DIR_TYPE
;
1635 result
= dir_scan2(inode
, dir_ent
->dir
);
1639 squashfs_type
= SQUASHFS_SYMLINK_TYPE
;
1640 result
= create_inode(inode
, dir_ent
, squashfs_type
, 0, 0, 0, NULL
, NULL
, NULL
);
1641 INFO("symbolic link %s inode 0x%llx\n", dir_name
, *inode
);
1646 squashfs_type
= SQUASHFS_CHRDEV_TYPE
;
1647 result
= create_inode(inode
, dir_ent
, squashfs_type
, 0, 0, 0, NULL
, NULL
, NULL
);
1648 INFO("character device %s inode 0x%llx\n", dir_name
, *inode
);
1653 squashfs_type
= SQUASHFS_BLKDEV_TYPE
;
1654 result
= create_inode(inode
, dir_ent
, squashfs_type
, 0, 0, 0, NULL
, NULL
, NULL
);
1655 INFO("block device %s inode 0x%llx\n", dir_name
, *inode
);
1660 squashfs_type
= SQUASHFS_FIFO_TYPE
;
1661 result
= create_inode(inode
, dir_ent
, squashfs_type
, 0, 0, 0, NULL
, NULL
, NULL
);
1662 INFO("fifo %s inode 0x%llx\n", dir_name
, *inode
);
1667 squashfs_type
= SQUASHFS_SOCKET_TYPE
;
1668 result
= create_inode(inode
, dir_ent
, squashfs_type
, 0, 0, 0, NULL
, NULL
, NULL
);
1669 INFO("unix domain socket %s inode 0x%llx\n", dir_name
, *inode
);
1674 ERROR("%s unrecognised file type, mode is %x\n", filename
, buf
->st_mode
);
1678 dir_ent
->inode
->inode
= *inode
;
1679 dir_ent
->inode
->type
= squashfs_type
;
1681 *inode
= dir_ent
->inode
->inode
;
1682 squashfs_type
= dir_ent
->inode
->type
;
1683 switch(squashfs_type
) {
1684 case SQUASHFS_FILE_TYPE
:
1686 INFO("file %s, uncompressed size %lld bytes LINK\n", filename
, buf
->st_size
);
1688 case SQUASHFS_SYMLINK_TYPE
:
1689 INFO("symbolic link %s inode 0x%llx LINK\n", dir_name
, *inode
);
1691 case SQUASHFS_CHRDEV_TYPE
:
1692 INFO("character device %s inode 0x%llx LINK\n", dir_name
, *inode
);
1694 caseSQUASHFS_BLKDEV_TYPE
:
1695 INFO("block device %s inode 0x%llx LINK\n", dir_name
, *inode
);
1697 case SQUASHFS_FIFO_TYPE
:
1698 INFO("fifo %s inode 0x%llx LINK\n", dir_name
, *inode
);
1700 case SQUASHFS_SOCKET_TYPE
:
1701 INFO("unix domain socket %s inode 0x%llx LINK\n", dir_name
, *inode
);
1709 add_dir(*inode
, inode_number
, dir_name
, squashfs_type
, &dir
);
1712 result
= write_dir(inode
, dir_info
, &dir
);
1713 INFO("directory %s inode 0x%llx\n", pathname
, *inode
);
1715 scan2_freedir(&dir
);
1721 unsigned int slog(unsigned int block
)
1725 for(i
= 12; i
<= 16; i
++)
1726 if(block
== (1 << i
))
1732 int excluded(char *filename
, struct stat
*buf
)
1736 for(i
= 0; i
< exclude
; i
++)
1737 if((exclude_paths
[i
].st_dev
== buf
->st_dev
) && (exclude_paths
[i
].st_ino
== buf
->st_ino
))
1743 #define ADD_ENTRY(buf) \
1744 if(exclude % EXCLUDE_SIZE == 0) {\
1745 if((exclude_paths = (struct exclude_info *) realloc(exclude_paths, (exclude + EXCLUDE_SIZE) * sizeof(struct exclude_info))) == NULL)\
1746 BAD_ERROR("Out of memory in exclude dir/file table\n");\
1748 exclude_paths[exclude].st_dev = buf.st_dev;\
1749 exclude_paths[exclude++].st_ino = buf.st_ino;
1750 int add_exclude(char *path
)
1753 char buffer
[4096], filename
[4096];
1756 if(path
[0] == '/' || strncmp(path
, "./", 2) == 0 || strncmp(path
, "../", 3) == 0) {
1757 if(lstat(path
, &buf
) == -1) {
1758 sprintf(buffer
, "Cannot stat exclude dir/file %s, ignoring", path
);
1766 for(i
= 0; i
< source
; i
++) {
1767 strcat(strcat(strcpy(filename
, source_path
[i
]), "/"), path
);
1768 if(lstat(filename
, &buf
) == -1) {
1769 if(!(errno
== ENOENT
|| errno
== ENOTDIR
)) {
1770 sprintf(buffer
, "Cannot stat exclude dir/file %s, ignoring", filename
);
1781 void add_old_root_entry(char *name
, squashfs_inode inode
, int inode_number
, int type
)
1783 if((old_root_entry
= (struct old_root_entry_info
*) realloc(old_root_entry
, sizeof(struct old_root_entry_info
)
1784 * (old_root_entries
+ 1))) == NULL
)
1785 BAD_ERROR("Out of memory in old root directory entries reallocation\n");
1787 strcpy(old_root_entry
[old_root_entries
].name
, name
);
1788 old_root_entry
[old_root_entries
].inode
= inode
;
1789 old_root_entry
[old_root_entries
].inode_number
= inode_number
;
1790 old_root_entry
[old_root_entries
++].type
= type
;
1795 printf("mksquashfs version 3.0 (2006/03/15)\n");\
1796 printf("copyright (C) 2006 Phillip Lougher <phillip@lougher.org.uk>\n\n"); \
1797 printf("This program is free software; you can redistribute it and/or\n");\
1798 printf("modify it under the terms of the GNU General Public License\n");\
1799 printf("as published by the Free Software Foundation; either version 2,\n");\
1800 printf("or (at your option) any later version.\n\n");\
1801 printf("This program is distributed in the hope that it will be useful,\n");\
1802 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");\
1803 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");\
1804 printf("GNU General Public License for more details.\n");
1805 int main(int argc
, char *argv
[])
1807 struct stat buf
, source_buf
;
1809 squashfs_super_block sBlk
;
1810 char *b
, *root_name
= NULL
;
1811 int be
, nopad
= FALSE
, keep_as_directory
= FALSE
, orig_be
;
1812 squashfs_inode inode
;
1814 #if __BYTE_ORDER == __BIG_ENDIAN
1820 block_log
= slog(block_size
);
1821 if(argc
> 1 && strcmp(argv
[1], "-version") == 0) {
1825 for(i
= 1; i
< argc
&& argv
[i
][0] != '-'; i
++);
1828 source_path
= argv
+ 1;
1830 for(; i
< argc
; i
++) {
1831 if(strcmp(argv
[i
], "-b") == 0) {
1832 if((++i
== argc
) || (block_size
= strtol(argv
[i
], &b
, 10), *b
!='\0')) {
1833 ERROR("%s: -b missing or invalid block size\n", argv
[0]);
1837 if((block_log
= slog(block_size
)) == 0) {
1838 ERROR("%s: -b block size not power of two or not between 4096 and 64K\n", argv
[0]);
1841 } else if(strcmp(argv
[i
], "-ef") == 0) {
1843 ERROR("%s: -ef missing filename\n", argv
[0]);
1846 } else if(strcmp(argv
[i
], "-no-duplicates") == 0)
1847 duplicate_checking
= FALSE
;
1849 else if(strcmp(argv
[i
], "-no-fragments") == 0)
1850 no_fragments
= TRUE
;
1852 else if(strcmp(argv
[i
], "-always-use-fragments") == 0)
1853 always_use_fragments
= TRUE
;
1855 else if(strcmp(argv
[i
], "-sort") == 0) {
1857 ERROR("%s: -sort missing filename\n", argv
[0]);
1860 } else if(strcmp(argv
[i
], "-all-root") == 0 ||
1861 strcmp(argv
[i
], "-root-owned") == 0)
1862 global_uid
= global_gid
= 0;
1864 else if(strcmp(argv
[i
], "-force-uid") == 0) {
1866 ERROR("%s: -force-uid missing uid or user\n", argv
[0]);
1869 if((global_uid
= strtoll(argv
[i
], &b
, 10)), *b
=='\0') {
1870 if(global_uid
< 0 || global_uid
> (((long long) 1 << 32) - 1)) {
1871 ERROR("%s: -force-uid uid out of range\n", argv
[0]);
1875 struct passwd
*uid
= getpwnam(argv
[i
]);
1877 global_uid
= uid
->pw_uid
;
1879 ERROR("%s: -force-uid invalid uid or unknown user\n", argv
[0]);
1883 } else if(strcmp(argv
[i
], "-force-gid") == 0) {
1885 ERROR("%s: -force-gid missing gid or group\n", argv
[0]);
1888 if((global_gid
= strtoll(argv
[i
], &b
, 10)), *b
=='\0') {
1889 if(global_gid
< 0 || global_gid
> (((long long) 1 << 32) - 1)) {
1890 ERROR("%s: -force-gid gid out of range\n", argv
[0]);
1894 struct group
*gid
= getgrnam(argv
[i
]);
1896 global_gid
= gid
->gr_gid
;
1898 ERROR("%s: -force-gid invalid gid or unknown group\n", argv
[0]);
1902 } else if(strcmp(argv
[i
], "-noI") == 0 ||
1903 strcmp(argv
[i
], "-noInodeCompression") == 0)
1906 else if(strcmp(argv
[i
], "-noD") == 0 ||
1907 strcmp(argv
[i
], "-noDataCompression") == 0)
1910 else if(strcmp(argv
[i
], "-noF") == 0 ||
1911 strcmp(argv
[i
], "-noFragmentCompression") == 0)
1914 else if(strcmp(argv
[i
], "-nopad") == 0)
1917 else if(strcmp(argv
[i
], "-check_data") == 0)
1920 else if(strcmp(argv
[i
], "-info") == 0)
1923 else if(strcmp(argv
[i
], "-be") == 0)
1926 else if(strcmp(argv
[i
], "-le") == 0)
1929 else if(strcmp(argv
[i
], "-e") == 0)
1932 else if(strcmp(argv
[i
], "-noappend") == 0)
1935 else if(strcmp(argv
[i
], "-keep-as-directory") == 0)
1936 keep_as_directory
= TRUE
;
1938 else if(strcmp(argv
[i
], "-root-becomes") == 0) {
1940 ERROR("%s: -root-becomes: missing name\n", argv
[0]);
1943 root_name
= argv
[i
];
1944 } else if(strcmp(argv
[i
], "-version") == 0) {
1947 ERROR("%s: invalid option\n\n", argv
[0]);
1949 ERROR("SYNTAX:%s source1 source2 ... dest [options] [-e list of exclude\ndirs/files]\n", argv
[0]);
1950 ERROR("\nOptions are\n");
1951 ERROR("-version\t\tprint version, licence and copyright message\n");
1952 ERROR("-info\t\t\tprint files written to filesystem\n");
1953 ERROR("-b <block_size>\t\tset data block to <block_size>. Default %d bytes\n", SQUASHFS_FILE_SIZE
);
1954 ERROR("-noI\t\t\tdo not compress inode table\n");
1955 ERROR("-noD\t\t\tdo not compress data blocks\n");
1956 ERROR("-noF\t\t\tdo not compress fragment blocks\n");
1957 ERROR("-no-fragments\t\tdo not use fragments\n");
1958 ERROR("-always-use-fragments\tuse fragment blocks for files larger than block size\n");
1959 ERROR("-no-duplicates\t\tdo not perform duplicate checking\n");
1960 ERROR("-noappend\t\tdo not append to existing filesystem\n");
1961 ERROR("-keep-as-directory\tif one source directory is specified, create a root\n");
1962 ERROR("\t\t\tdirectory containing that directory, rather than the\n");
1963 ERROR("\t\t\tcontents of the directory\n");
1964 ERROR("-root-becomes <name>\twhen appending source files/directories, make the\n");
1965 ERROR("\t\t\toriginal root become a subdirectory in the new root\n");
1966 ERROR("\t\t\tcalled <name>, rather than adding the new source items\n");
1967 ERROR("\t\t\tto the original root\n");
1968 ERROR("-all-root\t\tmake all files owned by root\n");
1969 ERROR("-force-uid uid\t\tset all file uids to uid\n");
1970 ERROR("-force-gid gid\t\tset all file gids to gid\n");
1971 ERROR("-le\t\t\tcreate a little endian filesystem\n");
1972 ERROR("-be\t\t\tcreate a big endian filesystem\n");
1973 ERROR("-nopad\t\t\tdo not pad filesystem to a multiple of 4K\n");
1974 ERROR("-check_data\t\tadd checkdata for greater filesystem checks\n");
1975 ERROR("-root-owned\t\talternative name for -all-root\n");
1976 ERROR("-noInodeCompression\talternative name for -noI\n");
1977 ERROR("-noDataCompression\talternative name for -noD\n");
1978 ERROR("-noFragmentCompression\talternative name for -noF\n");
1979 ERROR("-sort <sort_file>\tsort files according to priorities in <sort_file>. One\n");
1980 ERROR("\t\t\tfile or dir with priority per line. Priority -32768 to\n");
1981 ERROR("\t\t\t32767, default priority 0\n");
1982 ERROR("-ef <exclude_file>\tlist of exclude dirs/files. One per line\n");
1987 for(i
= 0; i
< source
; i
++)
1988 if(lstat(source_path
[i
], &source_buf
) == -1) {
1989 fprintf(stderr
, "Cannot stat source directory \"%s\" because %s\n", source_path
[i
], strerror(errno
));
1993 destination_file
= argv
[source
+ 1];
1994 if(stat(argv
[source
+ 1], &buf
) == -1) {
1995 if(errno
== ENOENT
) { /* Does not exist */
1996 if((fd
= open(argv
[source
+ 1], O_CREAT
| O_TRUNC
| O_RDWR
, S_IRWXU
)) == -1) {
1997 perror("Could not create destination file");
2002 perror("Could not stat destination file");
2007 if(S_ISBLK(buf
.st_mode
)) {
2008 if((fd
= open(argv
[source
+ 1], O_RDWR
)) == -1) {
2009 perror("Could not open block device as destination");
2014 } else if(S_ISREG(buf
.st_mode
)) {
2015 if((fd
= open(argv
[source
+ 1], (delete ? O_TRUNC
: 0) | O_RDWR
)) == -1) {
2016 perror("Could not open regular file for writing as destination");
2021 ERROR("Destination not block device or regular file\n");
2028 if(read_super(fd
, &sBlk
, &orig_be
, argv
[source
+ 1]) == 0) {
2029 ERROR("Failed to read existing filesystem - will not overwrite - ABORTING!\n");
2033 signal(SIGTERM
, sighandler2
);
2034 signal(SIGINT
, sighandler2
);
2037 /* process the exclude files - must be done afer destination file has been possibly created */
2038 for(i
= source
+ 2; i
< argc
; i
++)
2039 if(strcmp(argv
[i
], "-ef") == 0) {
2041 char filename
[16385];
2042 if((fd
= fopen(argv
[++i
], "r")) == NULL
) {
2043 perror("Could not open exclude file...");
2046 while(fscanf(fd
, "%16384[^\n]\n", filename
) != EOF
)
2047 add_exclude(filename
);
2049 } else if(strcmp(argv
[i
], "-e") == 0)
2051 else if(strcmp(argv
[i
], "-b") == 0 || strcmp(argv
[i
], "-root-becomes") == 0 || strcmp(argv
[i
], "-sort") == 0)
2056 ERROR("%s: -e missing arguments\n", argv
[0]);
2059 while(i
< argc
&& add_exclude(argv
[i
++]));
2062 /* process the sort files - must be done afer the exclude files */
2063 for(i
= source
+ 2; i
< argc
; i
++)
2064 if(strcmp(argv
[i
], "-sort") == 0) {
2065 read_sort_file(argv
[++i
], source
, source_path
);
2067 } else if(strcmp(argv
[i
], "-e") == 0)
2069 else if(strcmp(argv
[i
], "-b") == 0 || strcmp(argv
[i
], "-root-becomes") == 0 || strcmp(argv
[i
], "-ef") == 0)
2073 printf("Creating %s %d.%d filesystem on %s, block size %d.\n",
2074 be
? "big endian" : "little endian", SQUASHFS_MAJOR
, SQUASHFS_MINOR
, argv
[source
+ 1], block_size
);
2075 bytes
= sizeof(squashfs_super_block
);
2077 unsigned int last_directory_block
, inode_dir_offset
, inode_dir_file_size
, root_inode_size
,
2078 inode_dir_start_block
, uncompressed_data
, compressed_data
, inode_dir_inode_number
,
2079 inode_dir_parent_inode
;
2080 unsigned int root_inode_start
= SQUASHFS_INODE_BLK(sBlk
.root_inode
), root_inode_offset
=
2081 SQUASHFS_INODE_OFFSET(sBlk
.root_inode
);
2084 block_log
= slog(block_size
= sBlk
.block_size
);
2085 noI
= SQUASHFS_UNCOMPRESSED_INODES(sBlk
.flags
);
2086 noD
= SQUASHFS_UNCOMPRESSED_DATA(sBlk
.flags
);
2087 noF
= SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk
.flags
);
2088 check_data
= SQUASHFS_CHECK_DATA(sBlk
.flags
);
2089 no_fragments
= SQUASHFS_NO_FRAGMENTS(sBlk
.flags
);
2090 always_use_fragments
= SQUASHFS_ALWAYS_FRAGMENTS(sBlk
.flags
);
2091 duplicate_checking
= SQUASHFS_DUPLICATES(sBlk
.flags
);
2093 if((bytes
= read_filesystem(root_name
, fd
, &sBlk
, &inode_table
, &data_cache
,
2094 &directory_table
, &directory_data_cache
, &last_directory_block
, &inode_dir_offset
,
2095 &inode_dir_file_size
, &root_inode_size
, &inode_dir_start_block
,
2096 &file_count
, &sym_count
, &dev_count
, &dir_count
, &fifo_count
, &sock_count
,
2097 (squashfs_uid
*) uids
, &uid_count
, (squashfs_uid
*) guids
, &guid_count
,
2098 &total_bytes
, &total_inode_bytes
, &total_directory_bytes
, &inode_dir_inode_number
,
2099 &inode_dir_parent_inode
, add_old_root_entry
, &fragment_table
)) == 0) {
2100 ERROR("Failed to read existing filesystem - will not overwrite - ABORTING!\n");
2103 if((fragments
= sBlk
.fragments
))
2104 fragment_table
= (squashfs_fragment_entry
*) realloc((char *) fragment_table
, ((fragments
+ FRAG_SIZE
- 1) & ~(FRAG_SIZE
- 1)) * sizeof(squashfs_fragment_entry
));
2106 printf("Appending to existing %s %d.%d filesystem on %s, block size %d\n", be
? "big endian" :
2107 "little endian", SQUASHFS_MAJOR
, SQUASHFS_MINOR
, argv
[source
+ 1], block_size
);
2108 printf("All -be, -le, -b, -noI, -noD, -noF, -check_data, no-duplicates, no-fragments, -always-use-fragments and -2.0 options ignored\n");
2109 printf("\nIf appending is not wanted, please re-run with -noappend specified!\n\n");
2111 compressed_data
= inode_dir_offset
+ inode_dir_file_size
& ~(SQUASHFS_METADATA_SIZE
- 1);
2112 uncompressed_data
= inode_dir_offset
+ inode_dir_file_size
& (SQUASHFS_METADATA_SIZE
- 1);
2114 /* save original filesystem state for restoring ... */
2115 sfragments
= fragments
;
2117 sinode_count
= sBlk
.inodes
;
2118 sdata_cache
= (char *)malloc(scache_bytes
= root_inode_offset
+ root_inode_size
);
2119 sdirectory_data_cache
= (char *)malloc(sdirectory_cache_bytes
= uncompressed_data
);
2120 memcpy(sdata_cache
, data_cache
, scache_bytes
);
2121 memcpy(sdirectory_data_cache
, directory_data_cache
+ compressed_data
, sdirectory_cache_bytes
);
2122 sinode_bytes
= root_inode_start
;
2123 sdirectory_bytes
= last_directory_block
;
2124 suid_count
= uid_count
;
2125 sguid_count
= guid_count
;
2126 stotal_bytes
= total_bytes
;
2127 stotal_inode_bytes
= total_inode_bytes
;
2128 stotal_directory_bytes
= total_directory_bytes
+ compressed_data
;
2129 sfile_count
= file_count
;
2130 ssym_count
= sym_count
;
2131 sdev_count
= dev_count
;
2132 sdir_count
= dir_count
+ 1;
2133 sfifo_count
= fifo_count
;
2134 ssock_count
= sock_count
;
2135 sdup_files
= dup_files
;
2138 goto restore_filesystem
;
2139 signal(SIGTERM
, sighandler
);
2140 signal(SIGINT
, sighandler
);
2141 write_bytes(fd
, SQUASHFS_START
, 4, "\0\0\0\0");
2143 /* set the filesystem state up to be able to append to the original filesystem. The filesystem state
2144 * differs depending on whether we're appending to the original root directory, or if the original
2145 * root directory becomes a sub-directory (root-becomes specified on command line, here root_name != NULL)
2147 inode_bytes
= inode_size
= root_inode_start
;
2148 directory_size
= last_directory_block
;
2149 cache_size
= root_inode_offset
+ root_inode_size
;
2150 directory_cache_size
= inode_dir_offset
+ inode_dir_file_size
;
2152 root_inode_number
= inode_dir_parent_inode
;
2153 dir_inode_no
= sBlk
.inodes
+ 2;
2154 directory_bytes
= last_directory_block
;
2155 directory_cache_bytes
= uncompressed_data
;
2156 memmove(directory_data_cache
, directory_data_cache
+ compressed_data
, uncompressed_data
);
2157 cache_bytes
= root_inode_offset
+ root_inode_size
;
2158 add_old_root_entry(root_name
, sBlk
.root_inode
, inode_dir_inode_number
, SQUASHFS_DIR_TYPE
);
2159 total_directory_bytes
+= compressed_data
;
2162 root_inode_number
= inode_dir_inode_number
;
2163 dir_inode_no
= sBlk
.inodes
+ 1;
2164 directory_bytes
= inode_dir_start_block
;
2165 directory_cache_bytes
= inode_dir_offset
;
2166 cache_bytes
= root_inode_offset
;
2169 inode_count
= file_count
+ dir_count
+ sym_count
+ dev_count
+ fifo_count
+ sock_count
;
2172 #if __BYTE_ORDER == __BIG_ENDIAN
2178 block_offset
= check_data
? 3 : 2;
2180 if(delete && !keep_as_directory
&& source
== 1 && S_ISDIR(source_buf
.st_mode
))
2181 dir_scan(&inode
, source_path
[0], scan1_readdir
);
2182 else if(!keep_as_directory
&& source
== 1 && S_ISDIR(source_buf
.st_mode
))
2183 dir_scan(&inode
, source_path
[0], scan1_single_readdir
);
2185 dir_scan(&inode
, "", scan1_encomp_readdir
);
2186 sBlk
.root_inode
= inode
;
2187 sBlk
.inodes
= inode_count
;
2188 sBlk
.s_magic
= SQUASHFS_MAGIC
;
2189 sBlk
.s_major
= SQUASHFS_MAJOR
;
2190 sBlk
.s_minor
= SQUASHFS_MINOR
;
2191 sBlk
.block_size
= block_size
;
2192 sBlk
.block_log
= block_log
;
2193 sBlk
.flags
= SQUASHFS_MKFLAGS(noI
, noD
, check_data
, noF
, no_fragments
, always_use_fragments
, duplicate_checking
);
2194 sBlk
.mkfs_time
= time(NULL
);
2198 sBlk
.fragments
= fragments
;
2199 sBlk
.inode_table_start
= write_inodes();
2200 sBlk
.directory_table_start
= write_directories();
2201 sBlk
.fragment_table_start
= write_fragment_table();
2203 TRACE("sBlk->inode_table_start 0x%x\n", sBlk
.inode_table_start
);
2204 TRACE("sBlk->directory_table_start 0x%x\n", sBlk
.directory_table_start
);
2205 TRACE("sBlk->fragment_table_start 0x%x\n", sBlk
.fragment_table_start
);
2207 if(sBlk
.no_uids
= uid_count
) {
2209 write_bytes(fd
, bytes
, uid_count
* sizeof(squashfs_uid
), (char *) uids
);
2211 squashfs_uid uids_copy
[uid_count
];
2213 SQUASHFS_SWAP_DATA(uids
, uids_copy
, uid_count
, sizeof(squashfs_uid
) * 8);
2214 write_bytes(fd
, bytes
, uid_count
* sizeof(squashfs_uid
), (char *) uids_copy
);
2216 sBlk
.uid_start
= bytes
;
2217 bytes
+= uid_count
* sizeof(squashfs_uid
);
2221 if(sBlk
.no_guids
= guid_count
) {
2223 write_bytes(fd
, bytes
, guid_count
* sizeof(squashfs_uid
), (char *) guids
);
2225 squashfs_uid guids_copy
[guid_count
];
2227 SQUASHFS_SWAP_DATA(guids
, guids_copy
, guid_count
, sizeof(squashfs_uid
) * 8);
2228 write_bytes(fd
, bytes
, guid_count
* sizeof(squashfs_uid
), (char *) guids_copy
);
2230 sBlk
.guid_start
= bytes
;
2231 bytes
+= guid_count
* sizeof(squashfs_uid
);
2233 sBlk
.guid_start
= 0;
2235 sBlk
.bytes_used
= bytes
;
2237 sBlk
.unused
= SQUASHFS_INVALID_BLK
;
2240 write_bytes(fd
, SQUASHFS_START
, sizeof(squashfs_super_block
), (char *) &sBlk
);
2242 squashfs_super_block sBlk_copy
;
2244 SQUASHFS_SWAP_SUPER_BLOCK((&sBlk
), &sBlk_copy
);
2245 write_bytes(fd
, SQUASHFS_START
, sizeof(squashfs_super_block
), (char *) &sBlk_copy
);
2248 if(!nopad
&& (i
= bytes
& (4096 - 1))) {
2249 char temp
[4096] = {0};
2250 write_bytes(fd
, bytes
, 4096 - i
, temp
);
2253 total_bytes
+= total_inode_bytes
+ total_directory_bytes
+ uid_count
2254 * sizeof(unsigned short) + guid_count
* sizeof(unsigned short) +
2255 sizeof(squashfs_super_block
);
2257 printf("\n%s filesystem, data block size %d, %s data, %s metadata, %s fragments\n", be
?
2258 "Big endian" : "Little endian", block_size
, noD
? "uncompressed" : "compressed", noI
?
2259 "uncompressed" : "compressed", no_fragments
? "no" : noF
? "uncompressed" : "compressed");
2260 printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes
/ 1024.0, bytes
/ (1024.0 * 1024.0));
2261 printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n",
2262 ((float) bytes
/ total_bytes
) * 100.0, total_bytes
/ 1024.0);
2263 printf("Inode table size %d bytes (%.2f Kbytes)\n",
2264 inode_bytes
, inode_bytes
/ 1024.0);
2265 printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n",
2266 ((float) inode_bytes
/ total_inode_bytes
) * 100.0, total_inode_bytes
);
2267 printf("Directory table size %d bytes (%.2f Kbytes)\n",
2268 directory_bytes
, directory_bytes
/ 1024.0);
2269 printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n",
2270 ((float) directory_bytes
/ total_directory_bytes
) * 100.0, total_directory_bytes
);
2271 if(duplicate_checking
)
2272 printf("Number of duplicate files found %d\n", file_count
- dup_files
);
2274 printf("No duplicate files removed\n");
2275 printf("Number of inodes %d\n", inode_count
);
2276 printf("Number of files %d\n", file_count
);
2278 printf("Number of fragments %d\n", fragments
);
2279 printf("Number of symbolic links %d\n", sym_count
);
2280 printf("Number of device nodes %d\n", dev_count
);
2281 printf("Number of fifo nodes %d\n", fifo_count
);
2282 printf("Number of socket nodes %d\n", sock_count
);
2283 printf("Number of directories %d\n", dir_count
);
2284 printf("Number of uids %d\n", uid_count
);
2286 for(i
= 0; i
< uid_count
; i
++) {
2287 struct passwd
*user
= getpwuid(uids
[i
]);
2288 printf("\t%s (%d)\n", user
== NULL
? "unknown" : user
->pw_name
, uids
[i
]);
2291 printf("Number of gids %d\n", guid_count
);
2293 for(i
= 0; i
< guid_count
; i
++) {
2294 struct group
*group
= getgrgid(guids
[i
]);
2295 printf("\t%s (%d)\n", group
== NULL
? "unknown" : group
->gr_name
, guids
[i
]);