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