1 /*
2  * Create a squashfs filesystem.  This is a highly compressed read only
3  * filesystem.
4  *
5  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
6  * 2012, 2013, 2014
7  * Phillip Lougher <phillip@squashfs.org.uk>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2,
12  * or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * mksquashfs.c
24  */
25 
26 #define FALSE 0
27 #define TRUE 1
28 #define MAX_LINE 16384
29 
30 #include <pwd.h>
31 #include <grp.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <stddef.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <dirent.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <signal.h>
44 #include <setjmp.h>
45 #include <sys/types.h>
46 #include <sys/mman.h>
47 #include <pthread.h>
48 #include <regex.h>
49 #include <fnmatch.h>
50 #include <sys/wait.h>
51 #include <limits.h>
52 #include <ctype.h>
53 
54 #ifndef FNM_EXTMATCH /* glibc extension */
55     #define FNM_EXTMATCH 0
56 #endif
57 
58 #ifndef linux
59 #define __BYTE_ORDER BYTE_ORDER
60 #define __BIG_ENDIAN BIG_ENDIAN
61 #define __LITTLE_ENDIAN LITTLE_ENDIAN
62 #include <sys/sysctl.h>
63 #else
64 #include <endian.h>
65 #include <sys/sysinfo.h>
66 #endif
67 
68 #include "squashfs_fs.h"
69 #include "squashfs_swap.h"
70 #include "mksquashfs.h"
71 #include "sort.h"
72 #include "pseudo.h"
73 #include "compressor.h"
74 #include "xattr.h"
75 #include "action.h"
76 #include "error.h"
77 #include "progressbar.h"
78 #include "info.h"
79 #include "caches-queues-lists.h"
80 #include "read_fs.h"
81 #include "restore.h"
82 #include "process_fragments.h"
83 
84 /* ANDROID CHANGES START*/
85 #ifdef ANDROID
86 #include "android.h"
87 #include "private/android_filesystem_config.h"
88 #include "private/canned_fs_config.h"
89 #include "private/fs_config.h"
90 int android_config = FALSE;
91 char *context_file = NULL;
92 char *mount_point = NULL;
93 char *target_out_path = NULL;
94 fs_config_func_t fs_config_func = NULL;
95 int compress_thresh_per = 0;
96 int align_4k_blocks = TRUE;
97 FILE *block_map_file = NULL;
98 #endif
99 /* ANDROID CHANGES END */
100 
101 int delete = FALSE;
102 int fd;
103 struct squashfs_super_block sBlk;
104 
105 /* filesystem flags for building */
106 int comp_opts = FALSE;
107 int no_xattrs = XATTR_DEF;
108 int noX = FALSE;
109 int duplicate_checking = TRUE;
110 int noF = FALSE;
111 int no_fragments = FALSE;
112 int always_use_fragments = FALSE;
113 int noI = FALSE;
114 int noD = FALSE;
115 int silent = TRUE;
116 int exportable = TRUE;
117 int sparse_files = TRUE;
118 int old_exclude = TRUE;
119 int use_regex = FALSE;
120 int nopad = FALSE;
121 int exit_on_error = FALSE;
122 
123 long long global_uid = -1, global_gid = -1;
124 
125 /* superblock attributes */
126 int block_size = SQUASHFS_FILE_SIZE, block_log;
127 unsigned int id_count = 0;
128 int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0,
129 	sock_count = 0;
130 
131 /* ANDROID CHANGES START*/
132 #ifdef ANDROID
133 int whitelisted_count = 0;
134 #endif
135 /* ANDROID CHANGES END */
136 
137 /* write position within data section */
138 long long bytes = 0, total_bytes = 0;
139 
140 /* in memory directory table - possibly compressed */
141 char *directory_table = NULL;
142 unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0;
143 
144 /* cached directory table */
145 char *directory_data_cache = NULL;
146 unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
147 
148 /* in memory inode table - possibly compressed */
149 char *inode_table = NULL;
150 unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0;
151 
152 /* cached inode table */
153 char *data_cache = NULL;
154 unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
155 
156 /* inode lookup table */
157 squashfs_inode *inode_lookup_table = NULL;
158 
159 /* in memory directory data */
160 #define I_COUNT_SIZE		128
161 #define DIR_ENTRIES		32
162 #define INODE_HASH_SIZE		65536
163 #define INODE_HASH_MASK		(INODE_HASH_SIZE - 1)
164 #define INODE_HASH(dev, ino)	(ino & INODE_HASH_MASK)
165 
166 struct cached_dir_index {
167 	struct squashfs_dir_index	index;
168 	char				*name;
169 };
170 
171 struct directory {
172 	unsigned int		start_block;
173 	unsigned int		size;
174 	unsigned char		*buff;
175 	unsigned char		*p;
176 	unsigned int		entry_count;
177 	unsigned char		*entry_count_p;
178 	unsigned int		i_count;
179 	unsigned int		i_size;
180 	struct cached_dir_index	*index;
181 	unsigned char		*index_count_p;
182 	unsigned int		inode_number;
183 };
184 
185 struct inode_info *inode_info[INODE_HASH_SIZE];
186 
187 /* hash tables used to do fast duplicate searches in duplicate check */
188 struct file_info *dupl[65536];
189 int dup_files = 0;
190 
191 /* exclude file handling */
192 /* list of exclude dirs/files */
193 struct exclude_info {
194 	dev_t			st_dev;
195 	ino_t			st_ino;
196 };
197 
198 #define EXCLUDE_SIZE 8192
199 int exclude = 0;
200 struct exclude_info *exclude_paths = NULL;
201 int old_excluded(char *filename, struct stat *buf);
202 
203 struct path_entry {
204 	char *name;
205 	regex_t *preg;
206 	struct pathname *paths;
207 };
208 
209 struct pathname {
210 	int names;
211 	struct path_entry *name;
212 };
213 
214 struct pathnames {
215 	int count;
216 	struct pathname *path[0];
217 };
218 #define PATHS_ALLOC_SIZE 10
219 
220 struct pathnames *paths = NULL;
221 struct pathname *path = NULL;
222 struct pathname *stickypath = NULL;
223 int excluded(char *name, struct pathnames *paths, struct pathnames **new);
224 
225 int fragments = 0;
226 
227 #define FRAG_SIZE 32768
228 
229 struct squashfs_fragment_entry *fragment_table = NULL;
230 int fragments_outstanding = 0;
231 
232 int fragments_locked = FALSE;
233 
234 /* current inode number for directories and non directories */
235 unsigned int inode_no = 1;
236 unsigned int root_inode_number = 0;
237 
238 /* list of source dirs/files */
239 int source = 0;
240 char **source_path;
241 
242 /* list of root directory entries read from original filesystem */
243 int old_root_entries = 0;
244 struct old_root_entry_info {
245 	char			*name;
246 	struct inode_info	inode;
247 };
248 struct old_root_entry_info *old_root_entry;
249 
250 /* restore orignal filesystem state if appending to existing filesystem is
251  * cancelled */
252 int appending = FALSE;
253 char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed;
254 
255 long long sbytes, stotal_bytes;
256 
257 unsigned int sinode_bytes, scache_bytes, sdirectory_bytes,
258 	sdirectory_cache_bytes, sdirectory_compressed_bytes,
259 	stotal_inode_bytes, stotal_directory_bytes,
260 	sinode_count = 0, sfile_count, ssym_count, sdev_count,
261 	sdir_count, sfifo_count, ssock_count, sdup_files;
262 int sfragments;
263 int threads;
264 
265 /* flag whether destination file is a block device */
266 int block_device = FALSE;
267 
268 /* flag indicating whether files are sorted using sort list(s) */
269 int sorted = FALSE;
270 
271 /* save destination file name for deleting on error */
272 char *destination_file = NULL;
273 
274 /* recovery file for abnormal exit on appending */
275 char *recovery_file = NULL;
276 int recover = TRUE;
277 
278 /* uid/gid mapping tables */
279 #define UGID_ENTRIES 340
280 
281 struct ugid_map_entry {
282 	unsigned int	child_id;
283 	unsigned int	parent_id;
284 	unsigned int	length;
285 };
286 struct ugid_map_entry uid_mapping[UGID_ENTRIES], gid_mapping[UGID_ENTRIES];
287 unsigned int uid_map_count = 0, gid_map_count = 0;
288 
289 
290 struct id *id_hash_table[ID_ENTRIES];
291 struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS];
292 unsigned int uid_count = 0, guid_count = 0;
293 unsigned int sid_count = 0, suid_count = 0, sguid_count = 0;
294 
295 struct cache *reader_buffer, *fragment_buffer, *reserve_cache;
296 struct cache *bwriter_buffer, *fwriter_buffer;
297 struct queue *to_reader, *to_deflate, *to_writer, *from_writer,
298 	*to_frag, *locked_fragment, *to_process_frag;
299 struct seq_queue *to_main;
300 pthread_t reader_thread, writer_thread, main_thread;
301 pthread_t *deflator_thread, *frag_deflator_thread, *frag_thread;
302 pthread_t *restore_thread = NULL;
303 pthread_mutex_t	fragment_mutex = PTHREAD_MUTEX_INITIALIZER;
304 pthread_mutex_t	pos_mutex = PTHREAD_MUTEX_INITIALIZER;
305 pthread_mutex_t	dup_mutex = PTHREAD_MUTEX_INITIALIZER;
306 
307 /* user options that control parallelisation */
308 int processors = -1;
309 int bwriter_size;
310 
311 /* compression operations */
312 struct compressor *comp = NULL;
313 int compressor_opt_parsed = FALSE;
314 void *stream = NULL;
315 
316 /* xattr stats */
317 unsigned int xattr_bytes = 0, total_xattr_bytes = 0;
318 
319 /* fragment to file mapping used when appending */
320 int append_fragments = 0;
321 struct append_file **file_mapping;
322 
323 /* root of the in-core directory structure */
324 struct dir_info *root_dir;
325 
326 static char *read_from_disk(long long start, unsigned int avail_bytes);
327 void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
328 	int type);
329 struct file_info *duplicate(long long file_size, long long bytes,
330 	unsigned int **block_list, long long *start, struct fragment **fragment,
331 	struct file_buffer *file_buffer, int blocks, unsigned short checksum,
332 	int checksum_flag);
333 struct dir_info *dir_scan1(char *, char *, struct pathnames *,
334 	struct dir_ent *(_readdir)(struct dir_info *), int);
335 void dir_scan2(struct dir_info *dir, struct pseudo *pseudo);
336 void dir_scan3(struct dir_info *dir);
337 void dir_scan4(struct dir_info *dir);
338 void dir_scan5(struct dir_info *dir);
339 void dir_scan6(struct dir_info *dir);
340 void dir_scan7(squashfs_inode *inode, struct dir_info *dir_info);
341 struct file_info *add_non_dup(long long file_size, long long bytes,
342 	unsigned int *block_list, long long start, struct fragment *fragment,
343 	unsigned short checksum, unsigned short fragment_checksum,
344 	int checksum_flag, int checksum_frag_flag);
345 long long generic_write_table(int, void *, int, void *, int);
346 void restorefs();
347 struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth);
348 void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad);
349 unsigned short get_checksum_mem(char *buff, int bytes);
350 void check_usable_phys_mem(int total_mem);
351 
352 /* ANDROID CHANGES START*/
353 #ifdef ANDROID
354 static int whitelisted(struct stat *buf);
355 static void add_whitelist_entry(char *filename, struct stat *buf);
356 static int add_whitelist(char *path);
357 static void process_whitelist_file(char *argv);
358 
359 #define WHITELIST_SIZE 8192
360 int whitelist = 0;
361 
362 struct whitelist_info {
363 	dev_t			st_dev;
364 	ino_t			st_ino;
365 };
366 char *whitelist_filename = NULL;
367 struct whitelist_info *whitelist_paths = NULL;
368 #endif
369 /* ANDROID CHANGES END */
370 
prep_exit()371 void prep_exit()
372 {
373 	if(restore_thread) {
374 		if(pthread_self() == *restore_thread) {
375 			/*
376 			 * Recursive failure when trying to restore filesystem!
377 			 * Nothing to do except to exit, otherwise we'll just
378 			 * appear to hang.  The user should be able to restore
379 			 * from the recovery file (which is why it was added, in
380 			 * case of catastrophic failure in Mksquashfs)
381 			 */
382 			exit(1);
383 		} else {
384 			/* signal the restore thread to restore */
385 			pthread_kill(*restore_thread, SIGUSR1);
386 			pthread_exit(NULL);
387 		}
388 	} else if(delete) {
389 		if(destination_file && !block_device)
390 			unlink(destination_file);
391 	} else if(recovery_file)
392 		unlink(recovery_file);
393 }
394 
395 
add_overflow(int a,int b)396 int add_overflow(int a, int b)
397 {
398 	return (INT_MAX - a) < b;
399 }
400 
401 
shift_overflow(int a,int shift)402 int shift_overflow(int a, int shift)
403 {
404 	return (INT_MAX >> shift) < a;
405 }
406 
407 
multiply_overflow(int a,int multiplier)408 int multiply_overflow(int a, int multiplier)
409 {
410 	return (INT_MAX / multiplier) < a;
411 }
412 
413 
multiply_overflowll(long long a,int multiplier)414 int multiply_overflowll(long long a, int multiplier)
415 {
416 	return (LLONG_MAX / multiplier) < a;
417 }
418 
419 
420 #define MKINODE(A)	((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \
421 			+ (((char *)A) - data_cache)))
422 
423 
restorefs()424 void restorefs()
425 {
426 	ERROR("Exiting - restoring original filesystem!\n\n");
427 
428 	bytes = sbytes;
429 	memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes);
430 	memcpy(directory_data_cache, sdirectory_data_cache,
431 		sdirectory_cache_bytes);
432 	directory_cache_bytes = sdirectory_cache_bytes;
433 	inode_bytes = sinode_bytes;
434 	directory_bytes = sdirectory_bytes;
435  	memcpy(directory_table + directory_bytes, sdirectory_compressed,
436 		sdirectory_compressed_bytes);
437  	directory_bytes += sdirectory_compressed_bytes;
438 	total_bytes = stotal_bytes;
439 	total_inode_bytes = stotal_inode_bytes;
440 	total_directory_bytes = stotal_directory_bytes;
441 	inode_count = sinode_count;
442 	file_count = sfile_count;
443 	sym_count = ssym_count;
444 	dev_count = sdev_count;
445 	dir_count = sdir_count;
446 	fifo_count = sfifo_count;
447 	sock_count = ssock_count;
448 	dup_files = sdup_files;
449 	fragments = sfragments;
450 	id_count = sid_count;
451 	restore_xattrs();
452 	write_filesystem_tables(&sBlk, nopad);
453 	exit(1);
454 }
455 
456 
sighandler()457 void sighandler()
458 {
459 	EXIT_MKSQUASHFS();
460 }
461 
462 
mangle2(void * strm,char * d,char * s,int size,int block_size,int uncompressed,int data_block)463 int mangle2(void *strm, char *d, char *s, int size,
464 	int block_size, int uncompressed, int data_block)
465 {
466 	int error, c_byte = 0;
467 
468 	if(!uncompressed) {
469 		c_byte = compressor_compress(comp, strm, d, s, size, block_size,
470 			 &error);
471 		if(c_byte == -1)
472 			BAD_ERROR("mangle2:: %s compress failed with error "
473 				"code %d\n", comp->name, error);
474 	}
475 
476 	if(c_byte == 0 || c_byte >= size ||
477 			(c_byte > (size * ((100.0 - compress_thresh_per) / 100.0)))) {
478 		memcpy(d, s, size);
479 		return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK :
480 			SQUASHFS_COMPRESSED_BIT);
481 	}
482 
483 	return c_byte;
484 }
485 
486 
mangle(char * d,char * s,int size,int block_size,int uncompressed,int data_block)487 int mangle(char *d, char *s, int size, int block_size,
488 	int uncompressed, int data_block)
489 {
490 	return mangle2(stream, d, s, size, block_size, uncompressed,
491 		data_block);
492 }
493 
494 
get_inode(int req_size)495 void *get_inode(int req_size)
496 {
497 	int data_space;
498 	unsigned short c_byte;
499 
500 	while(cache_bytes >= SQUASHFS_METADATA_SIZE) {
501 		if((inode_size - inode_bytes) <
502 				((SQUASHFS_METADATA_SIZE << 1)) + 2) {
503 			void *it = realloc(inode_table, inode_size +
504 				(SQUASHFS_METADATA_SIZE << 1) + 2);
505 			if(it == NULL)
506 				MEM_ERROR();
507 			inode_table = it;
508 			inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
509 		}
510 
511 		c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET,
512 			data_cache, SQUASHFS_METADATA_SIZE,
513 			SQUASHFS_METADATA_SIZE, noI, 0);
514 		TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte);
515 		SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1);
516 		inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET;
517 		total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET;
518 		memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE,
519 			cache_bytes - SQUASHFS_METADATA_SIZE);
520 		cache_bytes -= SQUASHFS_METADATA_SIZE;
521 	}
522 
523 	data_space = (cache_size - cache_bytes);
524 	if(data_space < req_size) {
525 			int realloc_size = cache_size == 0 ?
526 				((req_size + SQUASHFS_METADATA_SIZE) &
527 				~(SQUASHFS_METADATA_SIZE - 1)) : req_size -
528 				data_space;
529 
530 			void *dc = realloc(data_cache, cache_size +
531 				realloc_size);
532 			if(dc == NULL)
533 				MEM_ERROR();
534 			cache_size += realloc_size;
535 			data_cache = dc;
536 	}
537 
538 	cache_bytes += req_size;
539 
540 	return data_cache + cache_bytes - req_size;
541 }
542 
543 
read_bytes(int fd,void * buff,int bytes)544 int read_bytes(int fd, void *buff, int bytes)
545 {
546 	int res, count;
547 
548 	for(count = 0; count < bytes; count += res) {
549 		res = read(fd, buff + count, bytes - count);
550 		if(res < 1) {
551 			if(res == 0)
552 				goto bytes_read;
553 			else if(errno != EINTR) {
554 				ERROR("Read failed because %s\n",
555 						strerror(errno));
556 				return -1;
557 			} else
558 				res = 0;
559 		}
560 	}
561 
562 bytes_read:
563 	return count;
564 }
565 
566 
read_fs_bytes(int fd,long long byte,int bytes,void * buff)567 int read_fs_bytes(int fd, long long byte, int bytes, void *buff)
568 {
569 	off_t off = byte;
570 	int res = 1;
571 
572 	TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n",
573 		byte, bytes);
574 
575 	pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
576 	pthread_mutex_lock(&pos_mutex);
577 	if(lseek(fd, off, SEEK_SET) == -1) {
578 		ERROR("read_fs_bytes: Lseek on destination failed because %s, "
579 			"offset=0x%llx\n", strerror(errno), off);
580 		res = 0;
581 	} else if(read_bytes(fd, buff, bytes) < bytes) {
582 		ERROR("Read on destination failed\n");
583 		res = 0;
584 	}
585 
586 	pthread_cleanup_pop(1);
587 	return res;
588 }
589 
590 
write_bytes(int fd,void * buff,int bytes)591 int write_bytes(int fd, void *buff, int bytes)
592 {
593 	int res, count;
594 
595 	for(count = 0; count < bytes; count += res) {
596 		res = write(fd, buff + count, bytes - count);
597 		if(res == -1) {
598 			if(errno != EINTR) {
599 				ERROR("Write failed because %s\n",
600 						strerror(errno));
601 				return -1;
602 			}
603 			res = 0;
604 		}
605 	}
606 
607 	return 0;
608 }
609 
610 
write_destination(int fd,long long byte,int bytes,void * buff)611 void write_destination(int fd, long long byte, int bytes, void *buff)
612 {
613 	off_t off = byte;
614 
615 	pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
616 	pthread_mutex_lock(&pos_mutex);
617 
618 	if(lseek(fd, off, SEEK_SET) == -1) {
619 		ERROR("write_destination: Lseek on destination "
620 			"failed because %s, offset=0x%llx\n", strerror(errno),
621 			off);
622 		BAD_ERROR("Probably out of space on output %s\n",
623 			block_device ? "block device" : "filesystem");
624 	}
625 
626 	if(write_bytes(fd, buff, bytes) == -1)
627 		BAD_ERROR("Failed to write to output %s\n",
628 			block_device ? "block device" : "filesystem");
629 
630 	pthread_cleanup_pop(1);
631 }
632 
633 
write_inodes()634 long long write_inodes()
635 {
636 	unsigned short c_byte;
637 	int avail_bytes;
638 	char *datap = data_cache;
639 	long long start_bytes = bytes;
640 
641 	while(cache_bytes) {
642 		if(inode_size - inode_bytes <
643 				((SQUASHFS_METADATA_SIZE << 1) + 2)) {
644 			void *it = realloc(inode_table, inode_size +
645 				((SQUASHFS_METADATA_SIZE << 1) + 2));
646 			if(it == NULL)
647 				MEM_ERROR();
648 			inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
649 			inode_table = it;
650 		}
651 		avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ?
652 			SQUASHFS_METADATA_SIZE : cache_bytes;
653 		c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap,
654 			avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0);
655 		TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte);
656 		SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1);
657 		inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET;
658 		total_inode_bytes += avail_bytes + BLOCK_OFFSET;
659 		datap += avail_bytes;
660 		cache_bytes -= avail_bytes;
661 	}
662 
663 	write_destination(fd, bytes, inode_bytes,  inode_table);
664 	bytes += inode_bytes;
665 
666 	return start_bytes;
667 }
668 
669 
write_directories()670 long long write_directories()
671 {
672 	unsigned short c_byte;
673 	int avail_bytes;
674 	char *directoryp = directory_data_cache;
675 	long long start_bytes = bytes;
676 
677 	while(directory_cache_bytes) {
678 		if(directory_size - directory_bytes <
679 				((SQUASHFS_METADATA_SIZE << 1) + 2)) {
680 			void *dt = realloc(directory_table,
681 				directory_size + ((SQUASHFS_METADATA_SIZE << 1)
682 				+ 2));
683 			if(dt == NULL)
684 				MEM_ERROR();
685 			directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
686 			directory_table = dt;
687 		}
688 		avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ?
689 			SQUASHFS_METADATA_SIZE : directory_cache_bytes;
690 		c_byte = mangle(directory_table + directory_bytes +
691 			BLOCK_OFFSET, directoryp, avail_bytes,
692 			SQUASHFS_METADATA_SIZE, noI, 0);
693 		TRACE("Directory block @ 0x%x, size %d\n", directory_bytes,
694 			c_byte);
695 		SQUASHFS_SWAP_SHORTS(&c_byte,
696 			directory_table + directory_bytes, 1);
697 		directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) +
698 			BLOCK_OFFSET;
699 		total_directory_bytes += avail_bytes + BLOCK_OFFSET;
700 		directoryp += avail_bytes;
701 		directory_cache_bytes -= avail_bytes;
702 	}
703 	write_destination(fd, bytes, directory_bytes, directory_table);
704 	bytes += directory_bytes;
705 
706 	return start_bytes;
707 }
708 
709 
write_id_table()710 long long write_id_table()
711 {
712 	unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count);
713 	unsigned int p[id_count];
714 	int i;
715 
716 	TRACE("write_id_table: ids %d, id_bytes %d\n", id_count, id_bytes);
717 	for(i = 0; i < id_count; i++) {
718 		TRACE("write_id_table: id index %d, id %d", i, id_table[i]->id);
719 		SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1);
720 	}
721 
722 	return generic_write_table(id_bytes, p, 0, NULL, noI);
723 }
724 
725 
get_id(unsigned int id)726 struct id *get_id(unsigned int id)
727 {
728 	int hash = ID_HASH(id);
729 	struct id *entry = id_hash_table[hash];
730 
731 	for(; entry; entry = entry->next)
732 		if(entry->id == id)
733 			break;
734 
735 	return entry;
736 }
737 
738 
create_id(unsigned int id)739 struct id *create_id(unsigned int id)
740 {
741 	int hash = ID_HASH(id);
742 	struct id *entry = malloc(sizeof(struct id));
743 	if(entry == NULL)
744 		MEM_ERROR();
745 	entry->id = id;
746 	entry->index = id_count ++;
747 	entry->flags = 0;
748 	entry->next = id_hash_table[hash];
749 	id_hash_table[hash] = entry;
750 	id_table[entry->index] = entry;
751 	return entry;
752 }
753 
754 
resolve_child_ugid(unsigned int * ugid,const struct ugid_map_entry * ugid_mapping,unsigned int ugid_map_count)755 int resolve_child_ugid(unsigned int *ugid,
756 	const struct ugid_map_entry *ugid_mapping,
757 	unsigned int ugid_map_count)
758 {
759 	unsigned int i;
760 
761 	for (i = 0; i < ugid_map_count; i++) {
762 		if (ugid_mapping[i].parent_id <= *ugid &&
763 			*ugid <
764 			ugid_mapping[i].parent_id + ugid_mapping[i].length) {
765 			*ugid = ugid_mapping[i].child_id + *ugid -
766 				ugid_mapping[i].parent_id;
767 			return 1;
768 		}
769 	}
770 
771 	return 0;
772 }
773 
774 
get_uid(unsigned int uid,int resolve)775 unsigned int get_uid(unsigned int uid, int resolve)
776 {
777 	struct id *entry;
778 
779 	if (resolve && !resolve_child_ugid(&uid, uid_mapping, uid_map_count))
780 		BAD_ERROR("uid not found in mapping: %d\n", uid);
781 	entry = get_id(uid);
782 
783 	if(entry == NULL) {
784 		if(id_count == SQUASHFS_IDS)
785 			BAD_ERROR("Out of uids!\n");
786 		entry = create_id(uid);
787 	}
788 
789 	if((entry->flags & ISA_UID) == 0) {
790 		entry->flags |= ISA_UID;
791 		uid_count ++;
792 	}
793 
794 	return entry->index;
795 }
796 
797 
get_guid(unsigned int guid,int resolve)798 unsigned int get_guid(unsigned int guid, int resolve)
799 {
800 	struct id *entry;
801 
802 	if (resolve && !resolve_child_ugid(&guid, gid_mapping, gid_map_count))
803 		BAD_ERROR("gid not found in mapping: %d\n", guid);
804 	entry = get_id(guid);
805 
806 	if(entry == NULL) {
807 		if(id_count == SQUASHFS_IDS)
808 			BAD_ERROR("Out of gids!\n");
809 		entry = create_id(guid);
810 	}
811 
812 	if((entry->flags & ISA_GID) == 0) {
813 		entry->flags |= ISA_GID;
814 		guid_count ++;
815 	}
816 
817 	return entry->index;
818 }
819 
820 
821 #define ALLOC_SIZE 128
822 
_pathname(struct dir_ent * dir_ent,char * pathname,int * size)823 char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size)
824 {
825 	if(pathname == NULL) {
826 		pathname = malloc(ALLOC_SIZE);
827 		if(pathname == NULL)
828 			MEM_ERROR();
829 	}
830 
831 	for(;;) {
832 		int res = snprintf(pathname, *size, "%s/%s",
833 			dir_ent->our_dir->pathname,
834 			dir_ent->source_name ? : dir_ent->name);
835 
836 		if(res < 0)
837 			BAD_ERROR("snprintf failed in pathname\n");
838 		else if(res >= *size) {
839 			/*
840 			 * pathname is too small to contain the result, so
841 			 * increase it and try again
842 			 */
843 			*size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1);
844 			pathname = realloc(pathname, *size);
845 			if(pathname == NULL)
846 				MEM_ERROR();
847 		} else
848 			break;
849 	}
850 
851 	return pathname;
852 }
853 
854 
pathname(struct dir_ent * dir_ent)855 char *pathname(struct dir_ent *dir_ent)
856 {
857 	static char *pathname = NULL;
858 	static int size = ALLOC_SIZE;
859 
860 	if (dir_ent->nonstandard_pathname)
861 		return dir_ent->nonstandard_pathname;
862 
863 	return pathname = _pathname(dir_ent, pathname, &size);
864 }
865 
866 
pathname_reader(struct dir_ent * dir_ent)867 char *pathname_reader(struct dir_ent *dir_ent)
868 {
869 	static char *pathname = NULL;
870 	static int size = ALLOC_SIZE;
871 
872 	if (dir_ent->nonstandard_pathname)
873 		return dir_ent->nonstandard_pathname;
874 
875 	return pathname = _pathname(dir_ent, pathname, &size);
876 }
877 
878 
subpathname(struct dir_ent * dir_ent)879 char *subpathname(struct dir_ent *dir_ent)
880 {
881 	static char *subpath = NULL;
882 	static int size = ALLOC_SIZE;
883 	int res;
884 
885 	if(subpath == NULL) {
886 		subpath = malloc(ALLOC_SIZE);
887 		if(subpath == NULL)
888 			MEM_ERROR();
889 	}
890 
891 	for(;;) {
892 		if(dir_ent->our_dir->subpath[0] != '\0')
893 			res = snprintf(subpath, size, "%s/%s",
894 				dir_ent->our_dir->subpath, dir_ent->name);
895 		else
896 			res = snprintf(subpath, size, "/%s", dir_ent->name);
897 
898 		if(res < 0)
899 			BAD_ERROR("snprintf failed in subpathname\n");
900 		else if(res >= size) {
901 			/*
902 			 * subpath is too small to contain the result, so
903 			 * increase it and try again
904 			 */
905 			size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1);
906 			subpath = realloc(subpath, size);
907 			if(subpath == NULL)
908 				MEM_ERROR();
909 		} else
910 			break;
911 	}
912 
913 	return subpath;
914 }
915 
916 
get_inode_no(struct inode_info * inode)917 static inline unsigned int get_inode_no(struct inode_info *inode)
918 {
919 	return inode->inode_number;
920 }
921 
922 
get_parent_no(struct dir_info * dir)923 static inline unsigned int get_parent_no(struct dir_info *dir)
924 {
925 	return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no;
926 }
927 
928 
929 /* ANDROID CHANGES START*/
930 #ifdef ANDROID
931 
932 /* Round up the passed |n| value to the smallest multiple of 4096 greater or
933  * equal than |n| and return the 4K-block number for that value. */
round_up_block(unsigned long long n)934 static unsigned long long round_up_block(unsigned long long n) {
935 	const unsigned long long kMapBlockSize = 4096;
936 	return (n + kMapBlockSize - 1) / kMapBlockSize;
937 }
938 
write_block_map_entry(char * sub_path,unsigned long long start_block,unsigned long long total_size,char * mount_point,FILE * block_map_file)939 static inline void write_block_map_entry(char *sub_path, unsigned long long start_block, unsigned long long total_size,
940 		char * mount_point, FILE *block_map_file) {
941 	if (block_map_file) {
942 		/* We assign each 4K block based on what file the first byte of the block
943 		 * belongs to. The current file consists of the chunk of bytes in the
944 		 * interval [start_block, start_block + total_size), (closed on the left end
945 		 * and open on the right end). We then compute the first block whose first
946 		 * byte is equal to or greater than start_block as |round_start| and then
947 		 * the first block whose first byte is *past* this interval, as
948 		 * |round_end + 1|. This means that the blocks that should be assigned to
949 		 * the current file are in the interval [round_start, round_end + 1), or
950 		 * simply [round_start, round_end].
951 		 */
952 		unsigned long long round_start = round_up_block(start_block);
953 		unsigned long long round_end = round_up_block(start_block + total_size) - 1;
954 		if (round_start && total_size && round_start <= round_end) {
955 			fprintf(block_map_file, "/%s", mount_point);
956 			if (sub_path[0] != '/') fprintf(block_map_file, "/");
957 			if (round_start == round_end)
958 				fprintf(block_map_file, "%s %lld\n", sub_path, round_start);
959 			else
960 				fprintf(block_map_file, "%s %lld-%lld\n", sub_path, round_start, round_end);
961 		}
962 	}
963 }
964 #endif
965 /* ANDROID CHANGES END */
966 
create_inode(squashfs_inode * i_no,struct dir_info * dir_info,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,long long sparse)967 int create_inode(squashfs_inode *i_no, struct dir_info *dir_info,
968 	struct dir_ent *dir_ent, int type, long long byte_size,
969 	long long start_block, unsigned int offset, unsigned int *block_list,
970 	struct fragment *fragment, struct directory *dir_in, long long sparse)
971 {
972 	struct stat *buf = &dir_ent->inode->buf;
973 	union squashfs_inode_header inode_header;
974 	struct squashfs_base_inode_header *base = &inode_header.base;
975 	void *inode;
976 	char *filename = pathname(dir_ent);
977 	int nlink = dir_ent->inode->nlink;
978 	int xattr = read_xattrs(dir_ent);
979 
980 	switch(type) {
981 	case SQUASHFS_FILE_TYPE:
982 		if(dir_ent->inode->nlink > 1 ||
983 				byte_size >= (1LL << 32) ||
984 				start_block >= (1LL << 32) ||
985 				sparse || IS_XATTR(xattr))
986 			type = SQUASHFS_LREG_TYPE;
987 		break;
988 	case SQUASHFS_DIR_TYPE:
989 		if(dir_info->dir_is_ldir || IS_XATTR(xattr))
990 			type = SQUASHFS_LDIR_TYPE;
991 		break;
992 	case SQUASHFS_SYMLINK_TYPE:
993 		if(IS_XATTR(xattr))
994 			type = SQUASHFS_LSYMLINK_TYPE;
995 		break;
996 	case SQUASHFS_BLKDEV_TYPE:
997 		if(IS_XATTR(xattr))
998 			type = SQUASHFS_LBLKDEV_TYPE;
999 		break;
1000 	case SQUASHFS_CHRDEV_TYPE:
1001 		if(IS_XATTR(xattr))
1002 			type = SQUASHFS_LCHRDEV_TYPE;
1003 		break;
1004 	case SQUASHFS_FIFO_TYPE:
1005 		if(IS_XATTR(xattr))
1006 			type = SQUASHFS_LFIFO_TYPE;
1007 		break;
1008 	case SQUASHFS_SOCKET_TYPE:
1009 		if(IS_XATTR(xattr))
1010 			type = SQUASHFS_LSOCKET_TYPE;
1011 		break;
1012 	}
1013 
1014 	base->mode = SQUASHFS_MODE(buf->st_mode);
1015 	base->uid = get_uid((unsigned int) global_uid == -1 ?
1016 		buf->st_uid : global_uid, 1);
1017 	base->inode_type = type;
1018 	base->guid = get_guid((unsigned int) global_gid == -1 ?
1019 		buf->st_gid : global_gid, 1);
1020 	base->mtime = buf->st_mtime;
1021 	base->inode_number = get_inode_no(dir_ent->inode);
1022 
1023 	if(type == SQUASHFS_FILE_TYPE) {
1024 		int i;
1025 		struct squashfs_reg_inode_header *reg = &inode_header.reg;
1026 		size_t off = offsetof(struct squashfs_reg_inode_header, block_list);
1027 /* ANDROID CHANGES START*/
1028 #ifdef ANDROID
1029 		unsigned long long total_size = 0;
1030 		char *sub_path;
1031 #endif
1032 /* ANDROID CHANGES END */
1033 
1034 		inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
1035 		reg->file_size = byte_size;
1036 		reg->start_block = start_block;
1037 		reg->fragment = fragment->index;
1038 		reg->offset = fragment->offset;
1039 		SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode);
1040 		SQUASHFS_SWAP_INTS(block_list, inode + off, offset);
1041 		TRACE("File inode, file_size %lld, start_block 0x%llx, blocks "
1042 			"%d, fragment %d, offset %d, size %d\n", byte_size,
1043 			start_block, offset, fragment->index, fragment->offset,
1044 			fragment->size);
1045 		for(i = 0; i < offset; i++) {
1046 			TRACE("Block %d, size %d\n", i, block_list[i]);
1047 			total_size += SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]);
1048 		}
1049 /* ANDROID CHANGES START*/
1050 #ifdef ANDROID
1051 		sub_path = subpathname(dir_ent);
1052 		if (block_map_file && fragment->index == -1) {
1053 			write_block_map_entry(sub_path, start_block, total_size, mount_point, block_map_file);
1054 		}
1055 #endif
1056 /* ANDROID CHANGES END */
1057 	}
1058 	else if(type == SQUASHFS_LREG_TYPE) {
1059 /* ANDROID CHANGES START*/
1060 #ifdef ANDROID
1061 		unsigned long long total_size = 0;
1062 		char *sub_path;
1063 #endif
1064 /* ANDROID CHANGES END */
1065 		int i;
1066 		struct squashfs_lreg_inode_header *reg = &inode_header.lreg;
1067 		size_t off = offsetof(struct squashfs_lreg_inode_header, block_list);
1068 
1069 		inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
1070 		reg->nlink = nlink;
1071 		reg->file_size = byte_size;
1072 		reg->start_block = start_block;
1073 		reg->fragment = fragment->index;
1074 		reg->offset = fragment->offset;
1075 		if(sparse && sparse >= byte_size)
1076 			sparse = byte_size - 1;
1077 		reg->sparse = sparse;
1078 		reg->xattr = xattr;
1079 		SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode);
1080 		SQUASHFS_SWAP_INTS(block_list, inode + off, offset);
1081 		TRACE("Long file inode, file_size %lld, start_block 0x%llx, "
1082 			"blocks %d, fragment %d, offset %d, size %d, nlink %d"
1083 			"\n", byte_size, start_block, offset, fragment->index,
1084 			fragment->offset, fragment->size, nlink);
1085 		for(i = 0; i < offset; i++) {
1086 			TRACE("Block %d, size %d\n", i, block_list[i]);
1087 			total_size += SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]);
1088 		}
1089 /* ANDROID CHANGES START*/
1090 #ifdef ANDROID
1091 		sub_path = subpathname(dir_ent);
1092 		if (block_map_file && fragment->index == -1) {
1093 			write_block_map_entry(sub_path, start_block, total_size, mount_point, block_map_file);
1094 		}
1095 #endif
1096 /* ANDROID CHANGES END */
1097 	}
1098 	else if(type == SQUASHFS_LDIR_TYPE) {
1099 		int i;
1100 		unsigned char *p;
1101 		struct squashfs_ldir_inode_header *dir = &inode_header.ldir;
1102 		struct cached_dir_index *index = dir_in->index;
1103 		unsigned int i_count = dir_in->i_count;
1104 		unsigned int i_size = dir_in->i_size;
1105 
1106 		if(byte_size >= 1 << 27)
1107 			BAD_ERROR("directory greater than 2^27-1 bytes!\n");
1108 
1109 		inode = get_inode(sizeof(*dir) + i_size);
1110 		dir->inode_type = SQUASHFS_LDIR_TYPE;
1111 		dir->nlink = dir_ent->dir->directory_count + 2;
1112 		dir->file_size = byte_size;
1113 		dir->offset = offset;
1114 		dir->start_block = start_block;
1115 		dir->i_count = i_count;
1116 		dir->parent_inode = get_parent_no(dir_ent->our_dir);
1117 		dir->xattr = xattr;
1118 
1119 		SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode);
1120 		p = inode + offsetof(struct squashfs_ldir_inode_header, index);
1121 		for(i = 0; i < i_count; i++) {
1122 			SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p);
1123 			p += offsetof(struct squashfs_dir_index, name);
1124 			memcpy(p, index[i].name, index[i].index.size + 1);
1125 			p += index[i].index.size + 1;
1126 		}
1127 		TRACE("Long directory inode, file_size %lld, start_block "
1128 			"0x%llx, offset 0x%x, nlink %d\n", byte_size,
1129 			start_block, offset, dir_ent->dir->directory_count + 2);
1130 	}
1131 	else if(type == SQUASHFS_DIR_TYPE) {
1132 		struct squashfs_dir_inode_header *dir = &inode_header.dir;
1133 
1134 		inode = get_inode(sizeof(*dir));
1135 		dir->nlink = dir_ent->dir->directory_count + 2;
1136 		dir->file_size = byte_size;
1137 		dir->offset = offset;
1138 		dir->start_block = start_block;
1139 		dir->parent_inode = get_parent_no(dir_ent->our_dir);
1140 		SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode);
1141 		TRACE("Directory inode, file_size %lld, start_block 0x%llx, "
1142 			"offset 0x%x, nlink %d\n", byte_size, start_block,
1143 			offset, dir_ent->dir->directory_count + 2);
1144 	}
1145 	else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) {
1146 		struct squashfs_dev_inode_header *dev = &inode_header.dev;
1147 		unsigned int major = major(buf->st_rdev);
1148 		unsigned int minor = minor(buf->st_rdev);
1149 
1150 		if(major > 0xfff) {
1151 			ERROR("Major %d out of range in device node %s, "
1152 				"truncating to %d\n", major, filename,
1153 				major & 0xfff);
1154 			major &= 0xfff;
1155 		}
1156 		if(minor > 0xfffff) {
1157 			ERROR("Minor %d out of range in device node %s, "
1158 				"truncating to %d\n", minor, filename,
1159 				minor & 0xfffff);
1160 			minor &= 0xfffff;
1161 		}
1162 		inode = get_inode(sizeof(*dev));
1163 		dev->nlink = nlink;
1164 		dev->rdev = (major << 8) | (minor & 0xff) |
1165 				((minor & ~0xff) << 12);
1166 		SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode);
1167 		TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink);
1168 	}
1169 	else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) {
1170 		struct squashfs_ldev_inode_header *dev = &inode_header.ldev;
1171 		unsigned int major = major(buf->st_rdev);
1172 		unsigned int minor = minor(buf->st_rdev);
1173 
1174 		if(major > 0xfff) {
1175 			ERROR("Major %d out of range in device node %s, "
1176 				"truncating to %d\n", major, filename,
1177 				major & 0xfff);
1178 			major &= 0xfff;
1179 		}
1180 		if(minor > 0xfffff) {
1181 			ERROR("Minor %d out of range in device node %s, "
1182 				"truncating to %d\n", minor, filename,
1183 				minor & 0xfffff);
1184 			minor &= 0xfffff;
1185 		}
1186 		inode = get_inode(sizeof(*dev));
1187 		dev->nlink = nlink;
1188 		dev->rdev = (major << 8) | (minor & 0xff) |
1189 				((minor & ~0xff) << 12);
1190 		dev->xattr = xattr;
1191 		SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode);
1192 		TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink);
1193 	}
1194 	else if(type == SQUASHFS_SYMLINK_TYPE) {
1195 		struct squashfs_symlink_inode_header *symlink = &inode_header.symlink;
1196 		int byte = strlen(dir_ent->inode->symlink);
1197 		size_t off = offsetof(struct squashfs_symlink_inode_header, symlink);
1198 
1199 		inode = get_inode(sizeof(*symlink) + byte);
1200 		symlink->nlink = nlink;
1201 		symlink->symlink_size = byte;
1202 		SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
1203 		strncpy(inode + off, dir_ent->inode->symlink, byte);
1204 		TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte,
1205 			nlink);
1206 	}
1207 	else if(type == SQUASHFS_LSYMLINK_TYPE) {
1208 		struct squashfs_symlink_inode_header *symlink = &inode_header.symlink;
1209 		int byte = strlen(dir_ent->inode->symlink);
1210 		size_t off = offsetof(struct squashfs_symlink_inode_header, symlink);
1211 
1212 		inode = get_inode(sizeof(*symlink) + byte +
1213 						sizeof(unsigned int));
1214 		symlink->nlink = nlink;
1215 		symlink->symlink_size = byte;
1216 		SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
1217 		strncpy(inode + off, dir_ent->inode->symlink, byte);
1218 		SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1);
1219 		TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte,
1220 			nlink);
1221 	}
1222 	else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) {
1223 		struct squashfs_ipc_inode_header *ipc = &inode_header.ipc;
1224 
1225 		inode = get_inode(sizeof(*ipc));
1226 		ipc->nlink = nlink;
1227 		SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode);
1228 		TRACE("ipc inode, type %s, nlink %d\n", type ==
1229 			SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink);
1230 	}
1231 	else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) {
1232 		struct squashfs_lipc_inode_header *ipc = &inode_header.lipc;
1233 
1234 		inode = get_inode(sizeof(*ipc));
1235 		ipc->nlink = nlink;
1236 		ipc->xattr = xattr;
1237 		SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode);
1238 		TRACE("ipc inode, type %s, nlink %d\n", type ==
1239 			SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink);
1240 	} else
1241 		BAD_ERROR("Unrecognised inode %d in create_inode\n", type);
1242 
1243 	*i_no = MKINODE(inode);
1244 	inode_count ++;
1245 
1246 	TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type,
1247 		base->uid, base->guid);
1248 
1249 	return TRUE;
1250 }
1251 
1252 
add_dir(squashfs_inode inode,unsigned int inode_number,char * name,int type,struct directory * dir)1253 void add_dir(squashfs_inode inode, unsigned int inode_number, char *name,
1254 	int type, struct directory *dir)
1255 {
1256 	unsigned char *buff;
1257 	struct squashfs_dir_entry idir;
1258 	unsigned int start_block = inode >> 16;
1259 	unsigned int offset = inode & 0xffff;
1260 	unsigned int size = strlen(name);
1261 	size_t name_off = offsetof(struct squashfs_dir_entry, name);
1262 
1263 	if(size > SQUASHFS_NAME_LEN) {
1264 		size = SQUASHFS_NAME_LEN;
1265 		ERROR("Filename is greater than %d characters, truncating! ..."
1266 			"\n", SQUASHFS_NAME_LEN);
1267 	}
1268 
1269 	if(dir->p + sizeof(struct squashfs_dir_entry) + size +
1270 			sizeof(struct squashfs_dir_header)
1271 			>= dir->buff + dir->size) {
1272 		buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE);
1273 		if(buff == NULL)
1274 			MEM_ERROR();
1275 
1276 		dir->p = (dir->p - dir->buff) + buff;
1277 		if(dir->entry_count_p)
1278 			dir->entry_count_p = (dir->entry_count_p - dir->buff +
1279 			buff);
1280 		dir->index_count_p = dir->index_count_p - dir->buff + buff;
1281 		dir->buff = buff;
1282 	}
1283 
1284 	if(dir->entry_count == 256 || start_block != dir->start_block ||
1285 			((dir->entry_count_p != NULL) &&
1286 			((dir->p + sizeof(struct squashfs_dir_entry) + size -
1287 			dir->index_count_p) > SQUASHFS_METADATA_SIZE)) ||
1288 			((long long) inode_number - dir->inode_number) > 32767
1289 			|| ((long long) inode_number - dir->inode_number)
1290 			< -32768) {
1291 		if(dir->entry_count_p) {
1292 			struct squashfs_dir_header dir_header;
1293 
1294 			if((dir->p + sizeof(struct squashfs_dir_entry) + size -
1295 					dir->index_count_p) >
1296 					SQUASHFS_METADATA_SIZE) {
1297 				if(dir->i_count % I_COUNT_SIZE == 0) {
1298 					dir->index = realloc(dir->index,
1299 						(dir->i_count + I_COUNT_SIZE) *
1300 						sizeof(struct cached_dir_index));
1301 					if(dir->index == NULL)
1302 						MEM_ERROR();
1303 				}
1304 				dir->index[dir->i_count].index.index =
1305 					dir->p - dir->buff;
1306 				dir->index[dir->i_count].index.size = size - 1;
1307 				dir->index[dir->i_count++].name = name;
1308 				dir->i_size += sizeof(struct squashfs_dir_index)
1309 					+ size;
1310 				dir->index_count_p = dir->p;
1311 			}
1312 
1313 			dir_header.count = dir->entry_count - 1;
1314 			dir_header.start_block = dir->start_block;
1315 			dir_header.inode_number = dir->inode_number;
1316 			SQUASHFS_SWAP_DIR_HEADER(&dir_header,
1317 				dir->entry_count_p);
1318 
1319 		}
1320 
1321 
1322 		dir->entry_count_p = dir->p;
1323 		dir->start_block = start_block;
1324 		dir->entry_count = 0;
1325 		dir->inode_number = inode_number;
1326 		dir->p += sizeof(struct squashfs_dir_header);
1327 	}
1328 
1329 	idir.offset = offset;
1330 	idir.type = type;
1331 	idir.size = size - 1;
1332 	idir.inode_number = ((long long) inode_number - dir->inode_number);
1333 	SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p);
1334 	strncpy((char *) dir->p + name_off, name, size);
1335 	dir->p += sizeof(struct squashfs_dir_entry) + size;
1336 	dir->entry_count ++;
1337 }
1338 
1339 
write_dir(squashfs_inode * inode,struct dir_info * dir_info,struct directory * dir)1340 void write_dir(squashfs_inode *inode, struct dir_info *dir_info,
1341 	struct directory *dir)
1342 {
1343 	unsigned int dir_size = dir->p - dir->buff;
1344 	int data_space = directory_cache_size - directory_cache_bytes;
1345 	unsigned int directory_block, directory_offset, i_count, index;
1346 	unsigned short c_byte;
1347 
1348 	if(data_space < dir_size) {
1349 		int realloc_size = directory_cache_size == 0 ?
1350 			((dir_size + SQUASHFS_METADATA_SIZE) &
1351 			~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space;
1352 
1353 		void *dc = realloc(directory_data_cache,
1354 			directory_cache_size + realloc_size);
1355 		if(dc == NULL)
1356 			MEM_ERROR();
1357 		directory_cache_size += realloc_size;
1358 		directory_data_cache = dc;
1359 	}
1360 
1361 	if(dir_size) {
1362 		struct squashfs_dir_header dir_header;
1363 
1364 		dir_header.count = dir->entry_count - 1;
1365 		dir_header.start_block = dir->start_block;
1366 		dir_header.inode_number = dir->inode_number;
1367 		SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p);
1368 		memcpy(directory_data_cache + directory_cache_bytes, dir->buff,
1369 			dir_size);
1370 	}
1371 	directory_offset = directory_cache_bytes;
1372 	directory_block = directory_bytes;
1373 	directory_cache_bytes += dir_size;
1374 	i_count = 0;
1375 	index = SQUASHFS_METADATA_SIZE - directory_offset;
1376 
1377 	while(1) {
1378 		while(i_count < dir->i_count &&
1379 				dir->index[i_count].index.index < index)
1380 			dir->index[i_count++].index.start_block =
1381 				directory_bytes;
1382 		index += SQUASHFS_METADATA_SIZE;
1383 
1384 		if(directory_cache_bytes < SQUASHFS_METADATA_SIZE)
1385 			break;
1386 
1387 		if((directory_size - directory_bytes) <
1388 					((SQUASHFS_METADATA_SIZE << 1) + 2)) {
1389 			void *dt = realloc(directory_table,
1390 				directory_size + (SQUASHFS_METADATA_SIZE << 1)
1391 				+ 2);
1392 			if(dt == NULL)
1393 				MEM_ERROR();
1394 			directory_size += SQUASHFS_METADATA_SIZE << 1;
1395 			directory_table = dt;
1396 		}
1397 
1398 		c_byte = mangle(directory_table + directory_bytes +
1399 				BLOCK_OFFSET, directory_data_cache,
1400 				SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE,
1401 				noI, 0);
1402 		TRACE("Directory block @ 0x%x, size %d\n", directory_bytes,
1403 			c_byte);
1404 		SQUASHFS_SWAP_SHORTS(&c_byte,
1405 			directory_table + directory_bytes, 1);
1406 		directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) +
1407 			BLOCK_OFFSET;
1408 		total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET;
1409 		memmove(directory_data_cache, directory_data_cache +
1410 			SQUASHFS_METADATA_SIZE, directory_cache_bytes -
1411 			SQUASHFS_METADATA_SIZE);
1412 		directory_cache_bytes -= SQUASHFS_METADATA_SIZE;
1413 	}
1414 
1415 	create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE,
1416 		dir_size + 3, directory_block, directory_offset, NULL, NULL,
1417 		dir, 0);
1418 
1419 #ifdef SQUASHFS_TRACE
1420 	{
1421 		unsigned char *dirp;
1422 		int count;
1423 
1424 		TRACE("Directory contents of inode 0x%llx\n", *inode);
1425 		dirp = dir->buff;
1426 		while(dirp < dir->p) {
1427 			char buffer[SQUASHFS_NAME_LEN + 1];
1428 			struct squashfs_dir_entry idir, *idirp;
1429 			struct squashfs_dir_header dirh;
1430 			SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp,
1431 				&dirh);
1432 			count = dirh.count + 1;
1433 			dirp += sizeof(struct squashfs_dir_header);
1434 
1435 			TRACE("\tStart block 0x%x, count %d\n",
1436 				dirh.start_block, count);
1437 
1438 			while(count--) {
1439 				idirp = (struct squashfs_dir_entry *) dirp;
1440 				SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir);
1441 				strncpy(buffer, idirp->name, idir.size + 1);
1442 				buffer[idir.size + 1] = '\0';
1443 				TRACE("\t\tname %s, inode offset 0x%x, type "
1444 					"%d\n", buffer, idir.offset, idir.type);
1445 				dirp += sizeof(struct squashfs_dir_entry) + idir.size +
1446 					1;
1447 			}
1448 		}
1449 	}
1450 #endif
1451 	dir_count ++;
1452 }
1453 
1454 
get_fragment(struct fragment * fragment)1455 static struct file_buffer *get_fragment(struct fragment *fragment)
1456 {
1457 	struct squashfs_fragment_entry *disk_fragment;
1458 	struct file_buffer *buffer, *compressed_buffer;
1459 	long long start_block;
1460 	int res, size, index = fragment->index;
1461 	char locked;
1462 
1463 	/*
1464 	 * Lookup fragment block in cache.
1465 	 * If the fragment block doesn't exist, then get the compressed version
1466 	 * from the writer cache or off disk, and decompress it.
1467 	 *
1468 	 * This routine has two things which complicate the code:
1469 	 *
1470 	 *	1. Multiple threads can simultaneously lookup/create the
1471 	 *	   same buffer.  This means a buffer needs to be "locked"
1472 	 *	   when it is being filled in, to prevent other threads from
1473 	 *	   using it when it is not ready.  This is because we now do
1474 	 *	   fragment duplicate checking in parallel.
1475 	 *	2. We have two caches which need to be checked for the
1476 	 *	   presence of fragment blocks: the normal fragment cache
1477 	 *	   and a "reserve" cache.  The reserve cache is used to
1478 	 *	   prevent an unnecessary pipeline stall when the fragment cache
1479 	 *	   is full of fragments waiting to be compressed.
1480 	 */
1481 
1482 	if(fragment->index == SQUASHFS_INVALID_FRAG)
1483 		return NULL;
1484 
1485 	pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1486 	pthread_mutex_lock(&dup_mutex);
1487 
1488 again:
1489 	buffer = cache_lookup_nowait(fragment_buffer, index, &locked);
1490 	if(buffer) {
1491 		pthread_mutex_unlock(&dup_mutex);
1492 		if(locked)
1493 			/* got a buffer being filled in.  Wait for it */
1494 			cache_wait_unlock(buffer);
1495 		goto finished;
1496 	}
1497 
1498 	/* not in fragment cache, is it in the reserve cache? */
1499 	buffer = cache_lookup_nowait(reserve_cache, index, &locked);
1500 	if(buffer) {
1501 		pthread_mutex_unlock(&dup_mutex);
1502 		if(locked)
1503 			/* got a buffer being filled in.  Wait for it */
1504 			cache_wait_unlock(buffer);
1505 		goto finished;
1506 	}
1507 
1508 	/* in neither cache, try to get it from the fragment cache */
1509 	buffer = cache_get_nowait(fragment_buffer, index);
1510 	if(!buffer) {
1511 		/*
1512 		 * no room, get it from the reserve cache, this is
1513 		 * dimensioned so it will always have space (no more than
1514 		 * processors + 1 can have an outstanding reserve buffer)
1515 		 */
1516 		buffer = cache_get_nowait(reserve_cache, index);
1517 		if(!buffer) {
1518 			/* failsafe */
1519 			ERROR("no space in reserve cache\n");
1520 			goto again;
1521 		}
1522 	}
1523 
1524 	pthread_mutex_unlock(&dup_mutex);
1525 
1526 	compressed_buffer = cache_lookup(fwriter_buffer, index);
1527 
1528 	pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
1529 	pthread_mutex_lock(&fragment_mutex);
1530 	disk_fragment = &fragment_table[index];
1531 	size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size);
1532 	start_block = disk_fragment->start_block;
1533 	pthread_cleanup_pop(1);
1534 
1535 	if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) {
1536 		int error;
1537 		char *data;
1538 
1539 		if(compressed_buffer)
1540 			data = compressed_buffer->data;
1541 		else {
1542 			data = read_from_disk(start_block, size);
1543 			if(data == NULL) {
1544 				ERROR("Failed to read fragment from output"
1545 					" filesystem\n");
1546 				BAD_ERROR("Output filesystem corrupted?\n");
1547 			}
1548 		}
1549 
1550 		res = compressor_uncompress(comp, buffer->data, data, size,
1551 			block_size, &error);
1552 		if(res == -1)
1553 			BAD_ERROR("%s uncompress failed with error code %d\n",
1554 				comp->name, error);
1555 	} else if(compressed_buffer)
1556 		memcpy(buffer->data, compressed_buffer->data, size);
1557 	else {
1558 		res = read_fs_bytes(fd, start_block, size, buffer->data);
1559 		if(res == 0) {
1560 			ERROR("Failed to read fragment from output "
1561 				"filesystem\n");
1562 			BAD_ERROR("Output filesystem corrupted?\n");
1563 		}
1564 	}
1565 
1566 	cache_unlock(buffer);
1567 	cache_block_put(compressed_buffer);
1568 
1569 finished:
1570 	pthread_cleanup_pop(0);
1571 
1572 	return buffer;
1573 }
1574 
1575 
get_fragment_checksum(struct file_info * file)1576 unsigned short get_fragment_checksum(struct file_info *file)
1577 {
1578 	struct file_buffer *frag_buffer;
1579 	struct append_file *append;
1580 	int res, index = file->fragment->index;
1581 	unsigned short checksum;
1582 
1583 	if(index == SQUASHFS_INVALID_FRAG)
1584 		return 0;
1585 
1586 	pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1587 	pthread_mutex_lock(&dup_mutex);
1588 	res = file->have_frag_checksum;
1589 	checksum = file->fragment_checksum;
1590 	pthread_cleanup_pop(1);
1591 
1592 	if(res)
1593 		return checksum;
1594 
1595 	frag_buffer = get_fragment(file->fragment);
1596 
1597 	pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1598 
1599 	for(append = file_mapping[index]; append; append = append->next) {
1600 		int offset = append->file->fragment->offset;
1601 		int size = append->file->fragment->size;
1602 		unsigned short cksum =
1603 			get_checksum_mem(frag_buffer->data + offset, size);
1604 
1605 		if(file == append->file)
1606 			checksum = cksum;
1607 
1608 		pthread_mutex_lock(&dup_mutex);
1609 		append->file->fragment_checksum = cksum;
1610 		append->file->have_frag_checksum = TRUE;
1611 		pthread_mutex_unlock(&dup_mutex);
1612 	}
1613 
1614 	cache_block_put(frag_buffer);
1615 	pthread_cleanup_pop(0);
1616 
1617 	return checksum;
1618 }
1619 
1620 
lock_fragments()1621 void lock_fragments()
1622 {
1623 	pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
1624 	pthread_mutex_lock(&fragment_mutex);
1625 	fragments_locked = TRUE;
1626 	pthread_cleanup_pop(1);
1627 }
1628 
1629 
unlock_fragments()1630 void unlock_fragments()
1631 {
1632 	int frg, size;
1633 	struct file_buffer *write_buffer;
1634 
1635 	pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
1636 	pthread_mutex_lock(&fragment_mutex);
1637 
1638 	/*
1639 	 * Note queue_empty() is inherently racy with respect to concurrent
1640 	 * queue get and pushes.  We avoid this because we're holding the
1641 	 * fragment_mutex which ensures no other threads can be using the
1642 	 * queue at this time.
1643 	 */
1644 	while(!queue_empty(locked_fragment)) {
1645 		write_buffer = queue_get(locked_fragment);
1646 		frg = write_buffer->block;
1647 		size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size);
1648 		fragment_table[frg].start_block = bytes;
1649 		write_buffer->block = bytes;
1650 		bytes += size;
1651 		fragments_outstanding --;
1652 		queue_put(to_writer, write_buffer);
1653 		TRACE("fragment_locked writing fragment %d, compressed size %d"
1654 			"\n", frg, size);
1655 	}
1656 	fragments_locked = FALSE;
1657 	pthread_cleanup_pop(1);
1658 }
1659 
1660 /* Called with the fragment_mutex locked */
add_pending_fragment(struct file_buffer * write_buffer,int c_byte,int fragment)1661 void add_pending_fragment(struct file_buffer *write_buffer, int c_byte,
1662 	int fragment)
1663 {
1664 	fragment_table[fragment].size = c_byte;
1665 	write_buffer->block = fragment;
1666 
1667 	queue_put(locked_fragment, write_buffer);
1668 }
1669 
1670 
write_fragment(struct file_buffer * fragment)1671 void write_fragment(struct file_buffer *fragment)
1672 {
1673 	if(fragment == NULL)
1674 		return;
1675 
1676 	pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
1677 	pthread_mutex_lock(&fragment_mutex);
1678 	fragment_table[fragment->block].unused = 0;
1679 	fragments_outstanding ++;
1680 	queue_put(to_frag, fragment);
1681 	pthread_cleanup_pop(1);
1682 }
1683 
1684 
allocate_fragment()1685 struct file_buffer *allocate_fragment()
1686 {
1687 	struct file_buffer *fragment = cache_get(fragment_buffer, fragments);
1688 
1689 	pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
1690 	pthread_mutex_lock(&fragment_mutex);
1691 
1692 	if(fragments % FRAG_SIZE == 0) {
1693 		void *ft = realloc(fragment_table, (fragments +
1694 			FRAG_SIZE) * sizeof(struct squashfs_fragment_entry));
1695 		if(ft == NULL)
1696 			MEM_ERROR();
1697 		fragment_table = ft;
1698 	}
1699 
1700 	fragment->size = 0;
1701 	fragment->block = fragments ++;
1702 
1703 	pthread_cleanup_pop(1);
1704 
1705 	return fragment;
1706 }
1707 
1708 
1709 static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0};
1710 
1711 
free_fragment(struct fragment * fragment)1712 void free_fragment(struct fragment *fragment)
1713 {
1714 	if(fragment != &empty_fragment)
1715 		free(fragment);
1716 }
1717 
1718 
get_and_fill_fragment(struct file_buffer * file_buffer,struct dir_ent * dir_ent)1719 struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer,
1720 	struct dir_ent *dir_ent)
1721 {
1722 	struct fragment *ffrg;
1723 	struct file_buffer **fragment;
1724 
1725 	if(file_buffer == NULL || file_buffer->size == 0)
1726 		return &empty_fragment;
1727 
1728 	fragment = eval_frag_actions(root_dir, dir_ent);
1729 
1730 	if((*fragment) && (*fragment)->size + file_buffer->size > block_size) {
1731 		write_fragment(*fragment);
1732 		*fragment = NULL;
1733 	}
1734 
1735 	ffrg = malloc(sizeof(struct fragment));
1736 	if(ffrg == NULL)
1737 		MEM_ERROR();
1738 
1739 	if(*fragment == NULL)
1740 		*fragment = allocate_fragment();
1741 
1742 	ffrg->index = (*fragment)->block;
1743 	ffrg->offset = (*fragment)->size;
1744 	ffrg->size = file_buffer->size;
1745 	memcpy((*fragment)->data + (*fragment)->size, file_buffer->data,
1746 		file_buffer->size);
1747 	(*fragment)->size += file_buffer->size;
1748 
1749 	return ffrg;
1750 }
1751 
1752 
generic_write_table(int length,void * buffer,int length2,void * buffer2,int uncompressed)1753 long long generic_write_table(int length, void *buffer, int length2,
1754 	void *buffer2, int uncompressed)
1755 {
1756 	int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) /
1757 		SQUASHFS_METADATA_SIZE;
1758 	long long *list, start_bytes;
1759 	int compressed_size, i, list_size = meta_blocks * sizeof(long long);
1760 	unsigned short c_byte;
1761 	char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2];
1762 
1763 #ifdef SQUASHFS_TRACE
1764 	long long obytes = bytes;
1765 	int olength = length;
1766 #endif
1767 
1768 	list = malloc(list_size);
1769 	if(list == NULL)
1770 		MEM_ERROR();
1771 
1772 	for(i = 0; i < meta_blocks; i++) {
1773 		int avail_bytes = length > SQUASHFS_METADATA_SIZE ?
1774 			SQUASHFS_METADATA_SIZE : length;
1775 		c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i *
1776 			SQUASHFS_METADATA_SIZE , avail_bytes,
1777 			SQUASHFS_METADATA_SIZE, uncompressed, 0);
1778 		SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1);
1779 		list[i] = bytes;
1780 		compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) +
1781 			BLOCK_OFFSET;
1782 		TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes,
1783 			compressed_size);
1784 		write_destination(fd, bytes, compressed_size, cbuffer);
1785 		bytes += compressed_size;
1786 		total_bytes += avail_bytes;
1787 		length -= avail_bytes;
1788 	}
1789 
1790 	start_bytes = bytes;
1791 	if(length2) {
1792 		write_destination(fd, bytes, length2, buffer2);
1793 		bytes += length2;
1794 		total_bytes += length2;
1795 	}
1796 
1797 	SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks);
1798 	write_destination(fd, bytes, list_size, list);
1799 	bytes += list_size;
1800 	total_bytes += list_size;
1801 
1802 	TRACE("generic_write_table: total uncompressed %d compressed %lld\n",
1803 		olength, bytes - obytes);
1804 
1805 	free(list);
1806 
1807 	return start_bytes;
1808 }
1809 
1810 
write_fragment_table()1811 long long write_fragment_table()
1812 {
1813 	unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments);
1814 	int i;
1815 
1816 	TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments,
1817 		frag_bytes);
1818 	for(i = 0; i < fragments; i++) {
1819 		TRACE("write_fragment_table: fragment %d, start_block 0x%llx, "
1820 			"size %d\n", i, fragment_table[i].start_block,
1821 			fragment_table[i].size);
1822 		SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]);
1823 	}
1824 
1825 	return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF);
1826 }
1827 
1828 
1829 char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE];
read_from_disk(long long start,unsigned int avail_bytes)1830 static char *read_from_disk(long long start, unsigned int avail_bytes)
1831 {
1832 	int res;
1833 
1834 	res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer);
1835 	if(res == 0)
1836 		return NULL;
1837 
1838 	return read_from_file_buffer;
1839 }
1840 
1841 
1842 char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE];
read_from_disk2(long long start,unsigned int avail_bytes)1843 char *read_from_disk2(long long start, unsigned int avail_bytes)
1844 {
1845 	int res;
1846 
1847 	res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2);
1848 	if(res == 0)
1849 		return NULL;
1850 
1851 	return read_from_file_buffer2;
1852 }
1853 
1854 
1855 /*
1856  * Compute 16 bit BSD checksum over the data
1857  */
get_checksum(char * buff,int bytes,unsigned short chksum)1858 unsigned short get_checksum(char *buff, int bytes, unsigned short chksum)
1859 {
1860 	unsigned char *b = (unsigned char *) buff;
1861 
1862 	while(bytes --) {
1863 		chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1;
1864 		chksum += *b++;
1865 	}
1866 
1867 	return chksum;
1868 }
1869 
1870 
get_checksum_disk(long long start,long long l,unsigned int * blocks)1871 unsigned short get_checksum_disk(long long start, long long l,
1872 	unsigned int *blocks)
1873 {
1874 	unsigned short chksum = 0;
1875 	unsigned int bytes;
1876 	struct file_buffer *write_buffer;
1877 	int i;
1878 
1879 	for(i = 0; l; i++)  {
1880 		bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]);
1881 		if(bytes == 0) /* sparse block */
1882 			continue;
1883 		write_buffer = cache_lookup(bwriter_buffer, start);
1884 		if(write_buffer) {
1885 			chksum = get_checksum(write_buffer->data, bytes,
1886 				chksum);
1887 			cache_block_put(write_buffer);
1888 		} else {
1889 			void *data = read_from_disk(start, bytes);
1890 			if(data == NULL) {
1891 				ERROR("Failed to checksum data from output"
1892 					" filesystem\n");
1893 				BAD_ERROR("Output filesystem corrupted?\n");
1894 			}
1895 
1896 			chksum = get_checksum(data, bytes, chksum);
1897 		}
1898 
1899 		l -= bytes;
1900 		start += bytes;
1901 	}
1902 
1903 	return chksum;
1904 }
1905 
1906 
get_checksum_mem(char * buff,int bytes)1907 unsigned short get_checksum_mem(char *buff, int bytes)
1908 {
1909 	return get_checksum(buff, bytes, 0);
1910 }
1911 
1912 
get_checksum_mem_buffer(struct file_buffer * file_buffer)1913 unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer)
1914 {
1915 	if(file_buffer == NULL)
1916 		return 0;
1917 	else
1918 		return get_checksum(file_buffer->data, file_buffer->size, 0);
1919 }
1920 
1921 
1922 #define DUP_HASH(a) (a & 0xffff)
add_file(long long start,long long file_size,long long file_bytes,unsigned int * block_listp,int blocks,unsigned int fragment,int offset,int bytes)1923 void add_file(long long start, long long file_size, long long file_bytes,
1924 	unsigned int *block_listp, int blocks, unsigned int fragment,
1925 	int offset, int bytes)
1926 {
1927 	struct fragment *frg;
1928 	unsigned int *block_list = block_listp;
1929 	struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
1930 	struct append_file *append_file;
1931 	struct file_info *file;
1932 
1933 	if(!duplicate_checking || file_size == 0)
1934 		return;
1935 
1936 	for(; dupl_ptr; dupl_ptr = dupl_ptr->next) {
1937 		if(file_size != dupl_ptr->file_size)
1938 			continue;
1939 		if(blocks != 0 && start != dupl_ptr->start)
1940 			continue;
1941 		if(fragment != dupl_ptr->fragment->index)
1942 			continue;
1943 		if(fragment != SQUASHFS_INVALID_FRAG && (offset !=
1944 				dupl_ptr->fragment->offset || bytes !=
1945 				dupl_ptr->fragment->size))
1946 			continue;
1947 		return;
1948 	}
1949 
1950 	frg = malloc(sizeof(struct fragment));
1951 	if(frg == NULL)
1952 		MEM_ERROR();
1953 
1954 	frg->index = fragment;
1955 	frg->offset = offset;
1956 	frg->size = bytes;
1957 
1958 	file = add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0,
1959 		FALSE, FALSE);
1960 
1961 	if(fragment == SQUASHFS_INVALID_FRAG)
1962 		return;
1963 
1964 	append_file = malloc(sizeof(struct append_file));
1965 	if(append_file == NULL)
1966 		MEM_ERROR();
1967 
1968 	append_file->file = file;
1969 	append_file->next = file_mapping[fragment];
1970 	file_mapping[fragment] = append_file;
1971 }
1972 
1973 
pre_duplicate(long long file_size)1974 int pre_duplicate(long long file_size)
1975 {
1976 	struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
1977 
1978 	for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
1979 		if(dupl_ptr->file_size == file_size)
1980 			return TRUE;
1981 
1982 	return FALSE;
1983 }
1984 
1985 
add_non_dup(long long file_size,long long bytes,unsigned int * block_list,long long start,struct fragment * fragment,unsigned short checksum,unsigned short fragment_checksum,int checksum_flag,int checksum_frag_flag)1986 struct file_info *add_non_dup(long long file_size, long long bytes,
1987 	unsigned int *block_list, long long start, struct fragment *fragment,
1988 	unsigned short checksum, unsigned short fragment_checksum,
1989 	int checksum_flag, int checksum_frag_flag)
1990 {
1991 	struct file_info *dupl_ptr = malloc(sizeof(struct file_info));
1992 
1993 	if(dupl_ptr == NULL)
1994 		MEM_ERROR();
1995 
1996 	dupl_ptr->file_size = file_size;
1997 	dupl_ptr->bytes = bytes;
1998 	dupl_ptr->block_list = block_list;
1999 	dupl_ptr->start = start;
2000 	dupl_ptr->fragment = fragment;
2001 	dupl_ptr->checksum = checksum;
2002 	dupl_ptr->fragment_checksum = fragment_checksum;
2003 	dupl_ptr->have_frag_checksum = checksum_frag_flag;
2004 	dupl_ptr->have_checksum = checksum_flag;
2005 
2006 	pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
2007         pthread_mutex_lock(&dup_mutex);
2008 	dupl_ptr->next = dupl[DUP_HASH(file_size)];
2009 	dupl[DUP_HASH(file_size)] = dupl_ptr;
2010 	dup_files ++;
2011 	pthread_cleanup_pop(1);
2012 
2013 	return dupl_ptr;
2014 }
2015 
2016 
frag_duplicate(struct file_buffer * file_buffer,char * dont_put)2017 struct fragment *frag_duplicate(struct file_buffer *file_buffer, char *dont_put)
2018 {
2019 	struct file_info *dupl_ptr;
2020 	struct file_buffer *buffer;
2021 	struct file_info *dupl_start = file_buffer->dupl_start;
2022 	long long file_size = file_buffer->file_size;
2023 	unsigned short checksum = file_buffer->checksum;
2024 	int res;
2025 
2026 	if(file_buffer->duplicate) {
2027 		TRACE("Found duplicate file, fragment %d, size %d, offset %d, "
2028 			"checksum 0x%x\n", dupl_start->fragment->index,
2029 			file_size, dupl_start->fragment->offset, checksum);
2030 		*dont_put = TRUE;
2031 		return dupl_start->fragment;
2032 	} else {
2033 		*dont_put = FALSE;
2034 		dupl_ptr = dupl[DUP_HASH(file_size)];
2035 	}
2036 
2037 	for(; dupl_ptr && dupl_ptr != dupl_start; dupl_ptr = dupl_ptr->next) {
2038 		if(file_size == dupl_ptr->file_size && file_size ==
2039 				dupl_ptr->fragment->size) {
2040 			if(get_fragment_checksum(dupl_ptr) == checksum) {
2041 				buffer = get_fragment(dupl_ptr->fragment);
2042 				res = memcmp(file_buffer->data, buffer->data +
2043 					dupl_ptr->fragment->offset, file_size);
2044 				cache_block_put(buffer);
2045 				if(res == 0)
2046 					break;
2047 			}
2048 		}
2049 	}
2050 
2051 	if(!dupl_ptr || dupl_ptr == dupl_start)
2052 		return NULL;
2053 
2054 	TRACE("Found duplicate file, fragment %d, size %d, offset %d, "
2055 		"checksum 0x%x\n", dupl_ptr->fragment->index, file_size,
2056 		dupl_ptr->fragment->offset, checksum);
2057 
2058 	return dupl_ptr->fragment;
2059 }
2060 
2061 
duplicate(long long file_size,long long bytes,unsigned int ** block_list,long long * start,struct fragment ** fragment,struct file_buffer * file_buffer,int blocks,unsigned short checksum,int checksum_flag)2062 struct file_info *duplicate(long long file_size, long long bytes,
2063 	unsigned int **block_list, long long *start, struct fragment **fragment,
2064 	struct file_buffer *file_buffer, int blocks, unsigned short checksum,
2065 	int checksum_flag)
2066 {
2067 	struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
2068 	int frag_bytes = file_buffer ? file_buffer->size : 0;
2069 	unsigned short fragment_checksum = file_buffer ?
2070 		file_buffer->checksum : 0;
2071 
2072 	for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
2073 		if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes
2074 				 && frag_bytes == dupl_ptr->fragment->size) {
2075 			long long target_start, dup_start = dupl_ptr->start;
2076 			int block;
2077 
2078 			if(memcmp(*block_list, dupl_ptr->block_list, blocks *
2079 					sizeof(unsigned int)) != 0)
2080 				continue;
2081 
2082 			if(checksum_flag == FALSE) {
2083 				checksum = get_checksum_disk(*start, bytes,
2084 					*block_list);
2085 				checksum_flag = TRUE;
2086 			}
2087 
2088 			if(!dupl_ptr->have_checksum) {
2089 				dupl_ptr->checksum =
2090 					get_checksum_disk(dupl_ptr->start,
2091 					dupl_ptr->bytes, dupl_ptr->block_list);
2092 				dupl_ptr->have_checksum = TRUE;
2093 			}
2094 
2095 			if(checksum != dupl_ptr->checksum ||
2096 					fragment_checksum !=
2097 					get_fragment_checksum(dupl_ptr))
2098 				continue;
2099 
2100 			target_start = *start;
2101 			for(block = 0; block < blocks; block ++) {
2102 				int size = SQUASHFS_COMPRESSED_SIZE_BLOCK
2103 					((*block_list)[block]);
2104 				struct file_buffer *target_buffer = NULL;
2105 				struct file_buffer *dup_buffer = NULL;
2106 				char *target_data, *dup_data;
2107 				int res;
2108 
2109 				if(size == 0)
2110 					continue;
2111 				target_buffer = cache_lookup(bwriter_buffer,
2112 					target_start);
2113 				if(target_buffer)
2114 					target_data = target_buffer->data;
2115 				else {
2116 					target_data =
2117 						read_from_disk(target_start,
2118 						size);
2119 					if(target_data == NULL) {
2120 						ERROR("Failed to read data from"
2121 							" output filesystem\n");
2122 						BAD_ERROR("Output filesystem"
2123 							" corrupted?\n");
2124 					}
2125 				}
2126 
2127 				dup_buffer = cache_lookup(bwriter_buffer,
2128 					dup_start);
2129 				if(dup_buffer)
2130 					dup_data = dup_buffer->data;
2131 				else {
2132 					dup_data = read_from_disk2(dup_start,
2133 						size);
2134 					if(dup_data == NULL) {
2135 						ERROR("Failed to read data from"
2136 							" output filesystem\n");
2137 						BAD_ERROR("Output filesystem"
2138 							" corrupted?\n");
2139 					}
2140 				}
2141 
2142 				res = memcmp(target_data, dup_data, size);
2143 				cache_block_put(target_buffer);
2144 				cache_block_put(dup_buffer);
2145 				if(res != 0)
2146 					break;
2147 				target_start += size;
2148 				dup_start += size;
2149 			}
2150 			if(block == blocks) {
2151 				struct file_buffer *frag_buffer =
2152 					get_fragment(dupl_ptr->fragment);
2153 
2154 				if(frag_bytes == 0 ||
2155 						memcmp(file_buffer->data,
2156 						frag_buffer->data +
2157 						dupl_ptr->fragment->offset,
2158 						frag_bytes) == 0) {
2159 					TRACE("Found duplicate file, start "
2160 						"0x%llx, size %lld, checksum "
2161 						"0x%x, fragment %d, size %d, "
2162 						"offset %d, checksum 0x%x\n",
2163 						dupl_ptr->start,
2164 						dupl_ptr->bytes,
2165 						dupl_ptr->checksum,
2166 						dupl_ptr->fragment->index,
2167 						frag_bytes,
2168 						dupl_ptr->fragment->offset,
2169 						fragment_checksum);
2170 					*block_list = dupl_ptr->block_list;
2171 					*start = dupl_ptr->start;
2172 					*fragment = dupl_ptr->fragment;
2173 					cache_block_put(frag_buffer);
2174 					return 0;
2175 				}
2176 				cache_block_put(frag_buffer);
2177 			}
2178 		}
2179 
2180 
2181 	return add_non_dup(file_size, bytes, *block_list, *start, *fragment,
2182 		checksum, fragment_checksum, checksum_flag, TRUE);
2183 }
2184 
2185 
is_fragment(struct inode_info * inode)2186 static inline int is_fragment(struct inode_info *inode)
2187 {
2188 	off_t file_size = inode->buf.st_size;
2189 
2190 	/*
2191 	 * If this block is to be compressed differently to the
2192 	 * fragment compression then it cannot be a fragment
2193 	 */
2194 	if(inode->noF != noF)
2195 		return FALSE;
2196 
2197 	return !inode->no_fragments && file_size && (file_size < block_size ||
2198 		(inode->always_use_fragments && file_size & (block_size - 1)));
2199 }
2200 
2201 
put_file_buffer(struct file_buffer * file_buffer)2202 void put_file_buffer(struct file_buffer *file_buffer)
2203 {
2204 	/*
2205 	 * Decide where to send the file buffer:
2206 	 * - compressible non-fragment blocks go to the deflate threads,
2207 	 * - fragments go to the process fragment threads,
2208 	 * - all others go directly to the main thread
2209 	 */
2210 	if(file_buffer->error) {
2211 		file_buffer->fragment = 0;
2212 		seq_queue_put(to_main, file_buffer);
2213 	} else if (file_buffer->file_size == 0)
2214 		seq_queue_put(to_main, file_buffer);
2215  	else if(file_buffer->fragment)
2216 		queue_put(to_process_frag, file_buffer);
2217 	else
2218 		queue_put(to_deflate, file_buffer);
2219 }
2220 
2221 
2222 static int seq = 0;
reader_read_process(struct dir_ent * dir_ent)2223 void reader_read_process(struct dir_ent *dir_ent)
2224 {
2225 	long long bytes = 0;
2226 	struct inode_info *inode = dir_ent->inode;
2227 	struct file_buffer *prev_buffer = NULL, *file_buffer;
2228 	int status, byte, res, child;
2229 	int file = pseudo_exec_file(get_pseudo_file(inode->pseudo_id), &child);
2230 
2231 	if(!file) {
2232 		file_buffer = cache_get_nohash(reader_buffer);
2233 		file_buffer->sequence = seq ++;
2234 		goto read_err;
2235 	}
2236 
2237 	while(1) {
2238 		file_buffer = cache_get_nohash(reader_buffer);
2239 		file_buffer->sequence = seq ++;
2240 		file_buffer->noD = inode->noD;
2241 
2242 		byte = read_bytes(file, file_buffer->data, block_size);
2243 		if(byte == -1)
2244 			goto read_err2;
2245 
2246 		file_buffer->size = byte;
2247 		file_buffer->file_size = -1;
2248 		file_buffer->error = FALSE;
2249 		file_buffer->fragment = FALSE;
2250 		bytes += byte;
2251 
2252 		if(byte == 0)
2253 			break;
2254 
2255 		/*
2256 		 * Update progress bar size.  This is done
2257 		 * on every block rather than waiting for all blocks to be
2258 		 * read incase write_file_process() is running in parallel
2259 		 * with this.  Otherwise the current progress bar position
2260 		 * may get ahead of the progress bar size.
2261 		 */
2262 		progress_bar_size(1);
2263 
2264 		if(prev_buffer)
2265 			put_file_buffer(prev_buffer);
2266 		prev_buffer = file_buffer;
2267 	}
2268 
2269 	/*
2270  	 * Update inode file size now that the size of the dynamic pseudo file
2271 	 * is known.  This is needed for the -info option.
2272 	 */
2273 	inode->buf.st_size = bytes;
2274 
2275 	res = waitpid(child, &status, 0);
2276 	close(file);
2277 
2278 	if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
2279 		goto read_err;
2280 
2281 	if(prev_buffer == NULL)
2282 		prev_buffer = file_buffer;
2283 	else {
2284 		cache_block_put(file_buffer);
2285 		seq --;
2286 	}
2287 	prev_buffer->file_size = bytes;
2288 	prev_buffer->fragment = is_fragment(inode);
2289 	put_file_buffer(prev_buffer);
2290 
2291 	return;
2292 
2293 read_err2:
2294 	close(file);
2295 read_err:
2296 	if(prev_buffer) {
2297 		cache_block_put(file_buffer);
2298 		seq --;
2299 		file_buffer = prev_buffer;
2300 	}
2301 	file_buffer->error = TRUE;
2302 	put_file_buffer(file_buffer);
2303 }
2304 
2305 
reader_read_file(struct dir_ent * dir_ent)2306 void reader_read_file(struct dir_ent *dir_ent)
2307 {
2308 	struct stat *buf = &dir_ent->inode->buf, buf2;
2309 	struct file_buffer *file_buffer;
2310 	int blocks, file, res;
2311 	long long bytes, read_size;
2312 	struct inode_info *inode = dir_ent->inode;
2313 
2314 	if(inode->read)
2315 		return;
2316 
2317 	inode->read = TRUE;
2318 again:
2319 	bytes = 0;
2320 	read_size = buf->st_size;
2321 	blocks = (read_size + block_size - 1) >> block_log;
2322 
2323 	file = open(pathname_reader(dir_ent), O_RDONLY);
2324 	if(file == -1) {
2325 		file_buffer = cache_get_nohash(reader_buffer);
2326 		file_buffer->sequence = seq ++;
2327 		goto read_err2;
2328 	}
2329 
2330 	do {
2331 		file_buffer = cache_get_nohash(reader_buffer);
2332 		file_buffer->file_size = read_size;
2333 		file_buffer->sequence = seq ++;
2334 		file_buffer->noD = inode->noD;
2335 		file_buffer->error = FALSE;
2336 
2337 		/*
2338 		 * Always try to read block_size bytes from the file rather
2339 		 * than expected bytes (which will be less than the block_size
2340 		 * at the file tail) to check that the file hasn't grown
2341 		 * since being stated.  If it is longer (or shorter) than
2342 		 * expected, then restat, and try again.  Note the special
2343 		 * case where the file is an exact multiple of the block_size
2344 		 * is dealt with later.
2345 		 */
2346 		file_buffer->size = read_bytes(file, file_buffer->data,
2347 			block_size);
2348 		if(file_buffer->size == -1)
2349 			goto read_err;
2350 
2351 		bytes += file_buffer->size;
2352 
2353 		if(blocks > 1) {
2354 			/* non-tail block should be exactly block_size */
2355 			if(file_buffer->size < block_size)
2356 				goto restat;
2357 
2358 			file_buffer->fragment = FALSE;
2359 			put_file_buffer(file_buffer);
2360 		}
2361 	} while(-- blocks > 0);
2362 
2363 	/* Overall size including tail should match */
2364 	if(read_size != bytes)
2365 		goto restat;
2366 
2367 	if(read_size && read_size % block_size == 0) {
2368 		/*
2369 		 * Special case where we've not tried to read past the end of
2370 		 * the file.  We expect to get EOF, i.e. the file isn't larger
2371 		 * than we expect.
2372 		 */
2373 		char buffer;
2374 		int res;
2375 
2376 		res = read_bytes(file, &buffer, 1);
2377 		if(res == -1)
2378 			goto read_err;
2379 
2380 		if(res != 0)
2381 			goto restat;
2382 	}
2383 
2384 	file_buffer->fragment = is_fragment(inode);
2385 	put_file_buffer(file_buffer);
2386 
2387 	close(file);
2388 
2389 	return;
2390 
2391 restat:
2392 	res = fstat(file, &buf2);
2393 	if(res == -1) {
2394 		ERROR("Cannot stat dir/file %s because %s\n",
2395 			pathname_reader(dir_ent), strerror(errno));
2396 		goto read_err;
2397 	}
2398 
2399 	if(read_size != buf2.st_size) {
2400 		close(file);
2401 		memcpy(buf, &buf2, sizeof(struct stat));
2402 		file_buffer->error = 2;
2403 		put_file_buffer(file_buffer);
2404 		goto again;
2405 	}
2406 read_err:
2407 	close(file);
2408 read_err2:
2409 	file_buffer->error = TRUE;
2410 	put_file_buffer(file_buffer);
2411 }
2412 
2413 
reader_scan(struct dir_info * dir)2414 void reader_scan(struct dir_info *dir) {
2415 	struct dir_ent *dir_ent = dir->list;
2416 
2417 	for(; dir_ent; dir_ent = dir_ent->next) {
2418 		struct stat *buf = &dir_ent->inode->buf;
2419 		if(dir_ent->inode->root_entry)
2420 			continue;
2421 
2422 		if(IS_PSEUDO_PROCESS(dir_ent->inode)) {
2423 			reader_read_process(dir_ent);
2424 			continue;
2425 		}
2426 
2427 		switch(buf->st_mode & S_IFMT) {
2428 			case S_IFREG:
2429 				reader_read_file(dir_ent);
2430 				break;
2431 			case S_IFDIR:
2432 				reader_scan(dir_ent->dir);
2433 				break;
2434 		}
2435 	}
2436 }
2437 
2438 
reader(void * arg)2439 void *reader(void *arg)
2440 {
2441 	if(!sorted)
2442 		reader_scan(queue_get(to_reader));
2443 	else {
2444 		int i;
2445 		struct priority_entry *entry;
2446 
2447 		queue_get(to_reader);
2448 		for(i = 65535; i >= 0; i--)
2449 			for(entry = priority_list[i]; entry;
2450 							entry = entry->next)
2451 				reader_read_file(entry->dir);
2452 	}
2453 
2454 	pthread_exit(NULL);
2455 }
2456 
2457 
writer(void * arg)2458 void *writer(void *arg)
2459 {
2460 	while(1) {
2461 		struct file_buffer *file_buffer = queue_get(to_writer);
2462 		off_t off;
2463 
2464 		if(file_buffer == NULL) {
2465 			queue_put(from_writer, NULL);
2466 			continue;
2467 		}
2468 
2469 		off = file_buffer->block;
2470 
2471 		pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
2472 		pthread_mutex_lock(&pos_mutex);
2473 
2474 		if(lseek(fd, off, SEEK_SET) == -1) {
2475 			ERROR("writer: Lseek on destination failed because "
2476 				"%s, offset=0x%llx\n", strerror(errno), off);
2477 			BAD_ERROR("Probably out of space on output "
2478 				"%s\n", block_device ? "block device" :
2479 				"filesystem");
2480 		}
2481 
2482 		if(write_bytes(fd, file_buffer->data,
2483 				file_buffer->size) == -1)
2484 			BAD_ERROR("Failed to write to output %s\n",
2485 				block_device ? "block device" : "filesystem");
2486 
2487 		pthread_cleanup_pop(1);
2488 
2489 		cache_block_put(file_buffer);
2490 	}
2491 }
2492 
2493 
all_zero(struct file_buffer * file_buffer)2494 int all_zero(struct file_buffer *file_buffer)
2495 {
2496 	int i;
2497 	long entries = file_buffer->size / sizeof(long);
2498 	long *p = (long *) file_buffer->data;
2499 
2500 	for(i = 0; i < entries && p[i] == 0; i++);
2501 
2502 	if(i == entries) {
2503 		for(i = file_buffer->size & ~(sizeof(long) - 1);
2504 			i < file_buffer->size && file_buffer->data[i] == 0;
2505 			i++);
2506 
2507 		return i == file_buffer->size;
2508 	}
2509 
2510 	return 0;
2511 }
2512 
2513 
deflator(void * arg)2514 void *deflator(void *arg)
2515 {
2516 	struct file_buffer *write_buffer = cache_get_nohash(bwriter_buffer);
2517 	void *stream = NULL;
2518 	int res;
2519 
2520 	res = compressor_init(comp, &stream, block_size, 1);
2521 	if(res)
2522 		BAD_ERROR("deflator:: compressor_init failed\n");
2523 
2524 	while(1) {
2525 		struct file_buffer *file_buffer = queue_get(to_deflate);
2526 
2527 		if(sparse_files && all_zero(file_buffer)) {
2528 			file_buffer->c_byte = 0;
2529 			seq_queue_put(to_main, file_buffer);
2530 		} else {
2531 			write_buffer->c_byte = mangle2(stream,
2532 				write_buffer->data, file_buffer->data,
2533 				file_buffer->size, block_size,
2534 				file_buffer->noD, 1);
2535 			write_buffer->sequence = file_buffer->sequence;
2536 			write_buffer->file_size = file_buffer->file_size;
2537 			write_buffer->block = file_buffer->block;
2538 			write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK
2539 				(write_buffer->c_byte);
2540 			write_buffer->fragment = FALSE;
2541 			write_buffer->error = FALSE;
2542 			cache_block_put(file_buffer);
2543 			seq_queue_put(to_main, write_buffer);
2544 			write_buffer = cache_get_nohash(bwriter_buffer);
2545 		}
2546 	}
2547 }
2548 
2549 
frag_deflator(void * arg)2550 void *frag_deflator(void *arg)
2551 {
2552 	void *stream = NULL;
2553 	int res;
2554 
2555 	res = compressor_init(comp, &stream, block_size, 1);
2556 	if(res)
2557 		BAD_ERROR("frag_deflator:: compressor_init failed\n");
2558 
2559 	pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
2560 
2561 	while(1) {
2562 		int c_byte, compressed_size;
2563 		struct file_buffer *file_buffer = queue_get(to_frag);
2564 		struct file_buffer *write_buffer =
2565 			cache_get(fwriter_buffer, file_buffer->block);
2566 
2567 		c_byte = mangle2(stream, write_buffer->data, file_buffer->data,
2568 			file_buffer->size, block_size, noF, 1);
2569 		compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
2570 		write_buffer->size = compressed_size;
2571 		pthread_mutex_lock(&fragment_mutex);
2572 		if(fragments_locked == FALSE) {
2573 			fragment_table[file_buffer->block].size = c_byte;
2574 			fragment_table[file_buffer->block].start_block = bytes;
2575 			write_buffer->block = bytes;
2576 			bytes += compressed_size;
2577 			fragments_outstanding --;
2578 			queue_put(to_writer, write_buffer);
2579 			pthread_mutex_unlock(&fragment_mutex);
2580 			TRACE("Writing fragment %lld, uncompressed size %d, "
2581 				"compressed size %d\n", file_buffer->block,
2582 				file_buffer->size, compressed_size);
2583 		} else {
2584 				add_pending_fragment(write_buffer, c_byte,
2585 					file_buffer->block);
2586 				pthread_mutex_unlock(&fragment_mutex);
2587 		}
2588 		cache_block_put(file_buffer);
2589 	}
2590 
2591 	pthread_cleanup_pop(0);
2592 }
2593 
2594 
get_file_buffer()2595 struct file_buffer *get_file_buffer()
2596 {
2597 	struct file_buffer *file_buffer = seq_queue_get(to_main);
2598 
2599 	return file_buffer;
2600 }
2601 
2602 
write_file_empty(squashfs_inode * inode,struct dir_ent * dir_ent,struct file_buffer * file_buffer,int * duplicate_file)2603 void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent,
2604 	struct file_buffer *file_buffer, int *duplicate_file)
2605 {
2606 	file_count ++;
2607 	*duplicate_file = FALSE;
2608 	cache_block_put(file_buffer);
2609 	create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0,
2610 		 NULL, &empty_fragment, NULL, 0);
2611 }
2612 
2613 
write_file_frag(squashfs_inode * inode,struct dir_ent * dir_ent,struct file_buffer * file_buffer,int * duplicate_file)2614 void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent,
2615 	struct file_buffer *file_buffer, int *duplicate_file)
2616 {
2617 	int size = file_buffer->file_size;
2618 	struct fragment *fragment;
2619 	unsigned short checksum = file_buffer->checksum;
2620 	char dont_put;
2621 
2622 	fragment = frag_duplicate(file_buffer, &dont_put);
2623 	*duplicate_file = !fragment;
2624 	if(!fragment) {
2625 		fragment = get_and_fill_fragment(file_buffer, dir_ent);
2626 		if(duplicate_checking)
2627 			add_non_dup(size, 0, NULL, 0, fragment, 0, checksum,
2628 				TRUE, TRUE);
2629 	}
2630 
2631 	if(dont_put)
2632 		free(file_buffer);
2633 	else
2634 		cache_block_put(file_buffer);
2635 
2636 	total_bytes += size;
2637 	file_count ++;
2638 
2639 	inc_progress_bar();
2640 
2641 	create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0,
2642 			0, NULL, fragment, NULL, 0);
2643 
2644 	if(!duplicate_checking)
2645 		free_fragment(fragment);
2646 }
2647 
2648 
write_file_process(squashfs_inode * inode,struct dir_ent * dir_ent,struct file_buffer * read_buffer,int * duplicate_file)2649 int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent,
2650 	struct file_buffer *read_buffer, int *duplicate_file)
2651 {
2652 	long long read_size, file_bytes, start;
2653 	struct fragment *fragment;
2654 	unsigned int *block_list = NULL;
2655 	int block = 0, status;
2656 	long long sparse = 0;
2657 	struct file_buffer *fragment_buffer = NULL;
2658 
2659 	*duplicate_file = FALSE;
2660 
2661 	lock_fragments();
2662 
2663 	file_bytes = 0;
2664 	start = bytes;
2665 	while (1) {
2666 		read_size = read_buffer->file_size;
2667 		if(read_buffer->fragment)
2668 			fragment_buffer = read_buffer;
2669 		else {
2670 			block_list = realloc(block_list, (block + 1) *
2671 				sizeof(unsigned int));
2672 			if(block_list == NULL)
2673 				MEM_ERROR();
2674 			block_list[block ++] = read_buffer->c_byte;
2675 			if(read_buffer->c_byte) {
2676 				read_buffer->block = bytes;
2677 				bytes += read_buffer->size;
2678 				cache_hash(read_buffer, read_buffer->block);
2679 				file_bytes += read_buffer->size;
2680 				queue_put(to_writer, read_buffer);
2681 			} else {
2682 				sparse += read_buffer->size;
2683 				cache_block_put(read_buffer);
2684 			}
2685 		}
2686 		inc_progress_bar();
2687 
2688 		if(read_size != -1)
2689 			break;
2690 
2691 		read_buffer = get_file_buffer();
2692 		if(read_buffer->error)
2693 			goto read_err;
2694 	}
2695 
2696 	unlock_fragments();
2697 	fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
2698 
2699 	if(duplicate_checking)
2700 		add_non_dup(read_size, file_bytes, block_list, start, fragment,
2701 			0, fragment_buffer ? fragment_buffer->checksum : 0,
2702 			FALSE, TRUE);
2703 	cache_block_put(fragment_buffer);
2704 	file_count ++;
2705 	total_bytes += read_size;
2706 
2707 	create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2708 		 block, block_list, fragment, NULL, sparse);
2709 
2710 	if(duplicate_checking == FALSE) {
2711 		free(block_list);
2712 		free_fragment(fragment);
2713 	}
2714 
2715 	return 0;
2716 
2717 read_err:
2718 	dec_progress_bar(block);
2719 	status = read_buffer->error;
2720 	bytes = start;
2721 	if(!block_device) {
2722 		int res;
2723 
2724 		queue_put(to_writer, NULL);
2725 		if(queue_get(from_writer) != 0)
2726 			EXIT_MKSQUASHFS();
2727 		res = ftruncate(fd, bytes);
2728 		if(res != 0)
2729 			BAD_ERROR("Failed to truncate dest file because %s\n",
2730 				strerror(errno));
2731 	}
2732 	unlock_fragments();
2733 	free(block_list);
2734 	cache_block_put(read_buffer);
2735 	return status;
2736 }
2737 
2738 
write_file_blocks_dup(squashfs_inode * inode,struct dir_ent * dir_ent,struct file_buffer * read_buffer,int * duplicate_file)2739 int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent,
2740 	struct file_buffer *read_buffer, int *duplicate_file)
2741 {
2742 	int block, thresh;
2743 	long long read_size = read_buffer->file_size;
2744 	long long file_bytes, dup_start, start;
2745 	struct fragment *fragment;
2746 	struct file_info *dupl_ptr;
2747 	int blocks = (read_size + block_size - 1) >> block_log;
2748 	unsigned int *block_list, *block_listp;
2749 	struct file_buffer **buffer_list;
2750 	int status;
2751 	long long sparse = 0;
2752 	struct file_buffer *fragment_buffer = NULL;
2753 
2754 	block_list = malloc(blocks * sizeof(unsigned int));
2755 	if(block_list == NULL)
2756 		MEM_ERROR();
2757 	block_listp = block_list;
2758 
2759 	buffer_list = malloc(blocks * sizeof(struct file_buffer *));
2760 	if(buffer_list == NULL)
2761 		MEM_ERROR();
2762 
2763 	lock_fragments();
2764 
2765 	file_bytes = 0;
2766 	start = dup_start = bytes;
2767 	thresh = blocks > bwriter_size ? blocks - bwriter_size : 0;
2768 
2769 	for(block = 0; block < blocks;) {
2770 		if(read_buffer->fragment) {
2771 			block_list[block] = 0;
2772 			buffer_list[block] = NULL;
2773 			fragment_buffer = read_buffer;
2774 			blocks = read_size >> block_log;
2775 		} else {
2776 			block_list[block] = read_buffer->c_byte;
2777 
2778 			if(read_buffer->c_byte) {
2779 				read_buffer->block = bytes;
2780 				bytes += read_buffer->size;
2781 				file_bytes += read_buffer->size;
2782 				cache_hash(read_buffer, read_buffer->block);
2783 				if(block < thresh) {
2784 					buffer_list[block] = NULL;
2785 					queue_put(to_writer, read_buffer);
2786 				} else
2787 					buffer_list[block] = read_buffer;
2788 			} else {
2789 				buffer_list[block] = NULL;
2790 				sparse += read_buffer->size;
2791 				cache_block_put(read_buffer);
2792 			}
2793 		}
2794 		inc_progress_bar();
2795 
2796 		if(++block < blocks) {
2797 			read_buffer = get_file_buffer();
2798 			if(read_buffer->error)
2799 				goto read_err;
2800 		}
2801 	}
2802 
2803 	dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start,
2804 		&fragment, fragment_buffer, blocks, 0, FALSE);
2805 
2806 	if(dupl_ptr) {
2807 		*duplicate_file = FALSE;
2808 		for(block = thresh; block < blocks; block ++)
2809 			if(buffer_list[block])
2810 				queue_put(to_writer, buffer_list[block]);
2811 		fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
2812 		dupl_ptr->fragment = fragment;
2813 	} else {
2814 		*duplicate_file = TRUE;
2815 		for(block = thresh; block < blocks; block ++)
2816 			cache_block_put(buffer_list[block]);
2817 		bytes = start;
2818 		if(thresh && !block_device) {
2819 			int res;
2820 
2821 			queue_put(to_writer, NULL);
2822 			if(queue_get(from_writer) != 0)
2823 				EXIT_MKSQUASHFS();
2824 			res = ftruncate(fd, bytes);
2825 			if(res != 0)
2826 				BAD_ERROR("Failed to truncate dest file because"
2827 					"  %s\n", strerror(errno));
2828 		}
2829 	}
2830 
2831 	unlock_fragments();
2832 	cache_block_put(fragment_buffer);
2833 	free(buffer_list);
2834 	file_count ++;
2835 	total_bytes += read_size;
2836 
2837 	/*
2838 	 * sparse count is needed to ensure squashfs correctly reports a
2839  	 * a smaller block count on stat calls to sparse files.  This is
2840  	 * to ensure intelligent applications like cp correctly handle the
2841  	 * file as a sparse file.  If the file in the original filesystem isn't
2842  	 * stored as a sparse file then still store it sparsely in squashfs, but
2843  	 * report it as non-sparse on stat calls to preserve semantics
2844  	 */
2845 	if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2846 		sparse = 0;
2847 
2848 	create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size,
2849 		dup_start, blocks, block_listp, fragment, NULL, sparse);
2850 
2851 	if(*duplicate_file == TRUE)
2852 		free(block_list);
2853 
2854 	return 0;
2855 
2856 read_err:
2857 	dec_progress_bar(block);
2858 	status = read_buffer->error;
2859 	bytes = start;
2860 	if(thresh && !block_device) {
2861 		int res;
2862 
2863 		queue_put(to_writer, NULL);
2864 		if(queue_get(from_writer) != 0)
2865 			EXIT_MKSQUASHFS();
2866 		res = ftruncate(fd, bytes);
2867 		if(res != 0)
2868 			BAD_ERROR("Failed to truncate dest file because %s\n",
2869 				strerror(errno));
2870 	}
2871 	unlock_fragments();
2872 	for(blocks = thresh; blocks < block; blocks ++)
2873 		cache_block_put(buffer_list[blocks]);
2874 	free(buffer_list);
2875 	free(block_list);
2876 	cache_block_put(read_buffer);
2877 	return status;
2878 }
2879 
2880 
write_file_blocks(squashfs_inode * inode,struct dir_ent * dir_ent,struct file_buffer * read_buffer,int * dup)2881 int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent,
2882 	struct file_buffer *read_buffer, int *dup)
2883 {
2884 	long long read_size = read_buffer->file_size;
2885 	long long file_bytes, start;
2886 	struct fragment *fragment;
2887 	unsigned int *block_list;
2888 	int block, status;
2889 	int blocks = (read_size + block_size - 1) >> block_log;
2890 	long long sparse = 0;
2891 	struct file_buffer *fragment_buffer = NULL;
2892 
2893 	if(pre_duplicate(read_size))
2894 		return write_file_blocks_dup(inode, dir_ent, read_buffer, dup);
2895 
2896 	*dup = FALSE;
2897 
2898 	block_list = malloc(blocks * sizeof(unsigned int));
2899 	if(block_list == NULL)
2900 		MEM_ERROR();
2901 
2902 	lock_fragments();
2903 
2904 	file_bytes = 0;
2905 /* ANDROID CHANGES START*/
2906 #ifdef ANDROID
2907 	if (align_4k_blocks && bytes % 4096) {
2908 		bytes += 4096 - (bytes % 4096);
2909 	}
2910 #endif
2911 /* ANDROID CHANGES END */
2912 	start = bytes;
2913 	for(block = 0; block < blocks;) {
2914 		if(read_buffer->fragment) {
2915 			block_list[block] = 0;
2916 			fragment_buffer = read_buffer;
2917 			blocks = read_size >> block_log;
2918 		} else {
2919 			block_list[block] = read_buffer->c_byte;
2920 			if(read_buffer->c_byte) {
2921 				read_buffer->block = bytes;
2922 				bytes += read_buffer->size;
2923 				cache_hash(read_buffer, read_buffer->block);
2924 				file_bytes += read_buffer->size;
2925 				queue_put(to_writer, read_buffer);
2926 			} else {
2927 				sparse += read_buffer->size;
2928 				cache_block_put(read_buffer);
2929 			}
2930 		}
2931 		inc_progress_bar();
2932 
2933 		if(++block < blocks) {
2934 			read_buffer = get_file_buffer();
2935 			if(read_buffer->error)
2936 				goto read_err;
2937 		}
2938 	}
2939 
2940 	unlock_fragments();
2941 	fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
2942 
2943 	if(duplicate_checking)
2944 		add_non_dup(read_size, file_bytes, block_list, start, fragment,
2945 			0, fragment_buffer ? fragment_buffer->checksum : 0,
2946 			FALSE, TRUE);
2947 	cache_block_put(fragment_buffer);
2948 	file_count ++;
2949 	total_bytes += read_size;
2950 
2951 	/*
2952 	 * sparse count is needed to ensure squashfs correctly reports a
2953  	 * a smaller block count on stat calls to sparse files.  This is
2954  	 * to ensure intelligent applications like cp correctly handle the
2955  	 * file as a sparse file.  If the file in the original filesystem isn't
2956  	 * stored as a sparse file then still store it sparsely in squashfs, but
2957  	 * report it as non-sparse on stat calls to preserve semantics
2958  	 */
2959 	if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2960 		sparse = 0;
2961 
2962 	create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2963 		 blocks, block_list, fragment, NULL, sparse);
2964 
2965 	if(duplicate_checking == FALSE) {
2966 		free(block_list);
2967 		free_fragment(fragment);
2968 	}
2969 
2970 	return 0;
2971 
2972 read_err:
2973 	dec_progress_bar(block);
2974 	status = read_buffer->error;
2975 	bytes = start;
2976 	if(!block_device) {
2977 		int res;
2978 
2979 		queue_put(to_writer, NULL);
2980 		if(queue_get(from_writer) != 0)
2981 			EXIT_MKSQUASHFS();
2982 		res = ftruncate(fd, bytes);
2983 		if(res != 0)
2984 			BAD_ERROR("Failed to truncate dest file because %s\n",
2985 				strerror(errno));
2986 	}
2987 	unlock_fragments();
2988 	free(block_list);
2989 	cache_block_put(read_buffer);
2990 	return status;
2991 }
2992 
2993 
write_file(squashfs_inode * inode,struct dir_ent * dir,int * dup)2994 void write_file(squashfs_inode *inode, struct dir_ent *dir, int *dup)
2995 {
2996 	int status;
2997 	struct file_buffer *read_buffer;
2998 
2999 again:
3000 	read_buffer = get_file_buffer();
3001 	status = read_buffer->error;
3002 
3003 	if(status)
3004 		cache_block_put(read_buffer);
3005 	else if(read_buffer->file_size == -1)
3006 		status = write_file_process(inode, dir, read_buffer, dup);
3007 	else if(read_buffer->file_size == 0)
3008 		write_file_empty(inode, dir, read_buffer, dup);
3009 	else if(read_buffer->fragment && read_buffer->c_byte)
3010 		write_file_frag(inode, dir, read_buffer, dup);
3011 	else
3012 		status = write_file_blocks(inode, dir, read_buffer, dup);
3013 
3014 	if(status == 2) {
3015 		ERROR("File %s changed size while reading filesystem, "
3016 			"attempting to re-read\n", pathname(dir));
3017 		goto again;
3018 	} else if(status == 1) {
3019 		ERROR_START("Failed to read file %s", pathname(dir));
3020 		ERROR_EXIT(", creating empty file\n");
3021 		write_file_empty(inode, dir, NULL, dup);
3022 	}
3023 }
3024 
3025 
3026 #define BUFF_SIZE 512
3027 char *name;
3028 char *basename_r();
3029 
getbase(char * pathname)3030 char *getbase(char *pathname)
3031 {
3032 	static char *b_buffer = NULL;
3033 	static int b_size = BUFF_SIZE;
3034 	char *result;
3035 
3036 	if(b_buffer == NULL) {
3037 		b_buffer = malloc(b_size);
3038 		if(b_buffer == NULL)
3039 			MEM_ERROR();
3040 	}
3041 
3042 	while(1) {
3043 		if(*pathname != '/') {
3044 			result = getcwd(b_buffer, b_size);
3045 			if(result == NULL && errno != ERANGE)
3046 				BAD_ERROR("Getcwd failed in getbase\n");
3047 
3048 			/* enough room for pathname + "/" + '\0' terminator? */
3049 			if(result && strlen(pathname) + 2 <=
3050 						b_size - strlen(b_buffer)) {
3051 				strcat(strcat(b_buffer, "/"), pathname);
3052 				break;
3053 			}
3054 		} else if(strlen(pathname) < b_size) {
3055 			strcpy(b_buffer, pathname);
3056 			break;
3057 		}
3058 
3059 		/* Buffer not large enough, realloc and try again */
3060 		b_buffer = realloc(b_buffer, b_size += BUFF_SIZE);
3061 		if(b_buffer == NULL)
3062 			MEM_ERROR();
3063 	}
3064 
3065 	name = b_buffer;
3066 	if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0))
3067 		return NULL;
3068 	else
3069 		return result;
3070 }
3071 
3072 
basename_r()3073 char *basename_r()
3074 {
3075 	char *s;
3076 	char *p;
3077 	int n = 1;
3078 
3079 	for(;;) {
3080 		s = name;
3081 		if(*name == '\0')
3082 			return NULL;
3083 		if(*name != '/') {
3084 			while(*name != '\0' && *name != '/') name++;
3085 			n = name - s;
3086 		}
3087 		while(*name == '/') name++;
3088 		if(strncmp(s, ".", n) == 0)
3089 			continue;
3090 		if((*name == '\0') || (strncmp(s, "..", n) == 0) ||
3091 				((p = basename_r()) == NULL)) {
3092 			s[n] = '\0';
3093 			return s;
3094 		}
3095 		if(strcmp(p, "..") == 0)
3096 			continue;
3097 		return p;
3098 	}
3099 }
3100 
3101 
lookup_inode3(struct stat * buf,int pseudo,int id,char * symlink,int bytes)3102 struct inode_info *lookup_inode3(struct stat *buf, int pseudo, int id,
3103 	char *symlink, int bytes)
3104 {
3105 	int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino);
3106 	struct inode_info *inode;
3107 
3108 	/*
3109 	 * Look-up inode in hash table, if it already exists we have a
3110 	 * hard-link, so increment the nlink count and return it.
3111 	 * Don't do the look-up for directories because we don't hard-link
3112 	 * directories.
3113 	 */
3114 	if ((buf->st_mode & S_IFMT) != S_IFDIR) {
3115 		for(inode = inode_info[ino_hash]; inode; inode = inode->next) {
3116 			if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) {
3117 				inode->nlink ++;
3118 				return inode;
3119 			}
3120 		}
3121 	}
3122 
3123 	inode = malloc(sizeof(struct inode_info) + bytes);
3124 	if(inode == NULL)
3125 		MEM_ERROR();
3126 
3127 	if(bytes)
3128 		memcpy(&inode->symlink, symlink, bytes);
3129 	memcpy(&inode->buf, buf, sizeof(struct stat));
3130 	inode->read = FALSE;
3131 	inode->root_entry = FALSE;
3132 	inode->pseudo_file = pseudo;
3133 	inode->pseudo_id = id;
3134 	inode->inode = SQUASHFS_INVALID_BLK;
3135 	inode->nlink = 1;
3136 	inode->inode_number = 0;
3137 
3138 	/*
3139 	 * Copy filesystem wide defaults into inode, these filesystem
3140 	 * wide defaults may be altered on an individual inode basis by
3141 	 * user specified actions
3142 	 *
3143 	*/
3144 	inode->no_fragments = no_fragments;
3145 	inode->always_use_fragments = always_use_fragments;
3146 
3147 /* ANDROID CHANGES START*/
3148 #ifdef ANDROID
3149 	/* Check the whitelist */
3150 	inode->noD = whitelisted(buf);
3151 #else
3152 	inode->noD = noD;
3153 #endif
3154 /* ANDROID CHANGES END */
3155 
3156 	inode->noF = noF;
3157 
3158 	inode->next = inode_info[ino_hash];
3159 	inode_info[ino_hash] = inode;
3160 
3161 	return inode;
3162 }
3163 
3164 
lookup_inode2(struct stat * buf,int pseudo,int id)3165 static inline struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id)
3166 {
3167 	return lookup_inode3(buf, pseudo, id, NULL, 0);
3168 }
3169 
3170 
lookup_inode(struct stat * buf)3171 static inline struct inode_info *lookup_inode(struct stat *buf)
3172 {
3173 	return lookup_inode2(buf, 0, 0);
3174 }
3175 
3176 
alloc_inode_no(struct inode_info * inode,unsigned int use_this)3177 static inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this)
3178 {
3179 	if (inode->inode_number == 0) {
3180 		inode->inode_number = use_this ? : inode_no ++;
3181 		if((inode->buf.st_mode & S_IFMT) == S_IFREG)
3182 			progress_bar_size((inode->buf.st_size + block_size - 1)
3183 								 >> block_log);
3184 	}
3185 }
3186 
3187 
create_dir_entry(char * name,char * source_name,char * nonstandard_pathname,struct dir_info * dir)3188 static inline struct dir_ent *create_dir_entry(char *name, char *source_name,
3189 	char *nonstandard_pathname, struct dir_info *dir)
3190 {
3191 	struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent));
3192 	if(dir_ent == NULL)
3193 		MEM_ERROR();
3194 
3195 	dir_ent->name = name;
3196 	dir_ent->source_name = source_name;
3197 	dir_ent->nonstandard_pathname = nonstandard_pathname;
3198 	dir_ent->our_dir = dir;
3199 	dir_ent->inode = NULL;
3200 	dir_ent->next = NULL;
3201 /* ANDROID CHANGES START*/
3202 #ifdef ANDROID
3203 	dir_ent->capabilities = 0;
3204 #endif
3205 /* ANDROID CHANGES END */
3206 
3207 	return dir_ent;
3208 }
3209 
3210 
add_dir_entry(struct dir_ent * dir_ent,struct dir_info * sub_dir,struct inode_info * inode_info)3211 static inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir,
3212 	struct inode_info *inode_info)
3213 {
3214 	struct dir_info *dir = dir_ent->our_dir;
3215 
3216 	if(sub_dir)
3217 		sub_dir->dir_ent = dir_ent;
3218 
3219 /* ANDROID CHANGES START*/
3220 #ifdef ANDROID
3221 	if (android_config) {
3222 		if (mount_point) {
3223 			char *mounted_path;
3224 			char *rel_path;
3225 
3226 			alloc_mounted_path(mount_point, subpathname(dir_ent), &mounted_path);
3227 			rel_path = mounted_path;
3228 			while (rel_path && *rel_path == '/')
3229 				rel_path++;
3230 			android_fs_config(fs_config_func, rel_path, &inode_info->buf, target_out_path, &dir_ent->capabilities);
3231 			free(mounted_path);
3232 		} else {
3233 			android_fs_config(fs_config_func, pathname(dir_ent), &inode_info->buf, target_out_path, &dir_ent->capabilities);
3234 		}
3235 	}
3236 #endif
3237 /* ANDROID CHANGES END */
3238 
3239 	dir_ent->inode = inode_info;
3240 	dir_ent->dir = sub_dir;
3241 
3242 	dir_ent->next = dir->list;
3243 	dir->list = dir_ent;
3244 	dir->count++;
3245 }
3246 
add_dir_entry2(char * name,char * source_name,char * nonstandard_pathname,struct dir_info * sub_dir,struct inode_info * inode_info,struct dir_info * dir)3247 static inline void add_dir_entry2(char *name, char *source_name,
3248 	char *nonstandard_pathname, struct dir_info *sub_dir,
3249 	struct inode_info *inode_info, struct dir_info *dir)
3250 {
3251 	struct dir_ent *dir_ent = create_dir_entry(name, source_name,
3252 		nonstandard_pathname, dir);
3253 
3254 
3255 	add_dir_entry(dir_ent, sub_dir, inode_info);
3256 }
3257 
3258 
free_dir_entry(struct dir_ent * dir_ent)3259 static inline void free_dir_entry(struct dir_ent *dir_ent)
3260 {
3261 	if(dir_ent->name)
3262 		free(dir_ent->name);
3263 
3264 	if(dir_ent->source_name)
3265 		free(dir_ent->source_name);
3266 
3267 	if(dir_ent->nonstandard_pathname)
3268 		free(dir_ent->nonstandard_pathname);
3269 
3270 	/* if this entry has been associated with an inode, then we need
3271 	 * to update the inode nlink count.  Orphaned inodes are harmless, and
3272 	 * is easier to leave them than go to the bother of deleting them */
3273 	if(dir_ent->inode && !dir_ent->inode->root_entry)
3274 		dir_ent->inode->nlink --;
3275 
3276 	free(dir_ent);
3277 }
3278 
3279 
add_excluded(struct dir_info * dir)3280 static inline void add_excluded(struct dir_info *dir)
3281 {
3282 	dir->excluded ++;
3283 }
3284 
3285 
dir_scan(squashfs_inode * inode,char * pathname,struct dir_ent * (_readdir)(struct dir_info *),int progress)3286 void dir_scan(squashfs_inode *inode, char *pathname,
3287 	struct dir_ent *(_readdir)(struct dir_info *), int progress)
3288 {
3289 	struct stat buf;
3290 	struct dir_ent *dir_ent;
3291 /* ANDROID CHANGES START*/
3292 #ifdef ANDROID
3293 	uint64_t caps = 0;
3294 #endif
3295 /* ANDROID CHANGES END */
3296 
3297 	root_dir = dir_scan1(pathname, "", paths, _readdir, 1);
3298 	if(root_dir == NULL)
3299 		return;
3300 
3301 	/* Create root directory dir_ent and associated inode, and connect
3302 	 * it to the root directory dir_info structure */
3303 	dir_ent = create_dir_entry("", NULL, pathname,
3304 						scan1_opendir("", "", 0));
3305 
3306 	if(pathname[0] == '\0') {
3307 		/*
3308  		 * dummy top level directory, if multiple sources specified on
3309 		 * command line
3310 		 */
3311 		memset(&buf, 0, sizeof(buf));
3312 		buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR;
3313 		buf.st_uid = getuid();
3314 		buf.st_gid = getgid();
3315 		buf.st_mtime = time(NULL);
3316 		buf.st_dev = 0;
3317 		buf.st_ino = 0;
3318 		dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0);
3319 	} else {
3320 		if(lstat(pathname, &buf) == -1)
3321 			/* source directory has disappeared? */
3322 			BAD_ERROR("Cannot stat source directory %s because %s\n",
3323 				pathname, strerror(errno));
3324 /* ANDROID CHANGES START*/
3325 #ifdef ANDROID
3326 		buf.st_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_IFDIR; // root mode
3327 		buf.st_uid = 0;
3328 		buf.st_gid = 0;
3329 		buf.st_mtime = time(NULL);
3330 		buf.st_dev = 0;
3331 		buf.st_ino = 0;
3332 #endif
3333 /* ANDROID CHANGES END */
3334 		dir_ent->inode = lookup_inode(&buf);
3335 	}
3336 
3337 /* ANDROID CHANGES START*/
3338 #ifdef ANDROID
3339 	dir_ent->capabilities = caps;
3340 	if (android_config) {
3341 		android_fs_config(fs_config_func, "", &dir_ent->inode->buf, target_out_path, &dir_ent->capabilities);
3342 	}
3343 #endif
3344 /* ANDROID CHANGES END */
3345 
3346 	dir_ent->dir = root_dir;
3347 	root_dir->dir_ent = dir_ent;
3348 
3349 	/*
3350 	 * Process most actions and any pseudo files
3351 	 */
3352 	if(actions() || get_pseudo())
3353 		dir_scan2(root_dir, get_pseudo());
3354 
3355 	/*
3356 	 * Process move actions
3357 	 */
3358 	if(move_actions()) {
3359 		dir_scan3(root_dir);
3360 		do_move_actions();
3361 	}
3362 
3363 	/*
3364 	 * Process prune actions
3365 	 */
3366 	if(prune_actions())
3367 		dir_scan4(root_dir);
3368 
3369 	/*
3370 	 * Process empty actions
3371 	 */
3372 	if(empty_actions())
3373 		dir_scan5(root_dir);
3374 
3375  	/*
3376 	 * Sort directories and compute the inode numbers
3377 	 */
3378 	dir_scan6(root_dir);
3379 
3380 	alloc_inode_no(dir_ent->inode, root_inode_number);
3381 
3382 	eval_actions(root_dir, dir_ent);
3383 
3384 	if(sorted)
3385 		generate_file_priorities(root_dir, 0,
3386 			&root_dir->dir_ent->inode->buf);
3387 
3388 	if(appending) {
3389 		sigset_t sigmask;
3390 
3391 		restore_thread = init_restore_thread();
3392 		sigemptyset(&sigmask);
3393 		sigaddset(&sigmask, SIGINT);
3394 		sigaddset(&sigmask, SIGTERM);
3395 		sigaddset(&sigmask, SIGUSR1);
3396 		if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
3397 			BAD_ERROR("Failed to set signal mask\n");
3398 		write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0");
3399 	}
3400 
3401 	queue_put(to_reader, root_dir);
3402 
3403 	set_progressbar_state(progress);
3404 
3405 	if(sorted)
3406 		sort_files_and_write(root_dir);
3407 
3408 	dir_scan7(inode, root_dir);
3409 	dir_ent->inode->inode = *inode;
3410 	dir_ent->inode->type = SQUASHFS_DIR_TYPE;
3411 }
3412 
3413 
3414 /*
3415  * dir_scan1 routines...
3416  * These scan the source directories into memory for processing.
3417  * Exclude actions are processed here (in contrast to the other actions)
3418  * because they affect what is scanned.
3419  */
scan1_opendir(char * pathname,char * subpath,int depth)3420 struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth)
3421 {
3422 	struct dir_info *dir;
3423 
3424 	dir = malloc(sizeof(struct dir_info));
3425 	if(dir == NULL)
3426 		MEM_ERROR();
3427 
3428 	if(pathname[0] != '\0') {
3429 		dir->linuxdir = opendir(pathname);
3430 		if(dir->linuxdir == NULL) {
3431 			free(dir);
3432 			return NULL;
3433 		}
3434 	}
3435 
3436 	dir->pathname = strdup(pathname);
3437 	dir->subpath = strdup(subpath);
3438 	dir->count = 0;
3439 	dir->directory_count = 0;
3440 	dir->dir_is_ldir = TRUE;
3441 	dir->list = NULL;
3442 	dir->depth = depth;
3443 	dir->excluded = 0;
3444 
3445 	return dir;
3446 }
3447 
3448 
scan1_encomp_readdir(struct dir_info * dir)3449 struct dir_ent *scan1_encomp_readdir(struct dir_info *dir)
3450 {
3451 	static int index = 0;
3452 
3453 	if(dir->count < old_root_entries) {
3454 		int i;
3455 
3456 		for(i = 0; i < old_root_entries; i++) {
3457 			if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
3458 				dir->directory_count ++;
3459 			add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
3460 				&old_root_entry[i].inode, dir);
3461 		}
3462 	}
3463 
3464 	while(index < source) {
3465 		char *basename = NULL;
3466 		char *dir_name = getbase(source_path[index]);
3467 		int pass = 1, res;
3468 
3469 		if(dir_name == NULL) {
3470 			ERROR_START("Bad source directory %s",
3471 				source_path[index]);
3472 			ERROR_EXIT(" - skipping ...\n");
3473 			index ++;
3474 			continue;
3475 		}
3476 		dir_name = strdup(dir_name);
3477 		for(;;) {
3478 			struct dir_ent *dir_ent = dir->list;
3479 
3480 			for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3481 				dir_ent = dir_ent->next);
3482 			if(dir_ent == NULL)
3483 				break;
3484 			ERROR("Source directory entry %s already used! - trying"
3485 				" ", dir_name);
3486 			if(pass == 1)
3487 				basename = dir_name;
3488 			else
3489 				free(dir_name);
3490 			res = asprintf(&dir_name, "%s_%d", basename, pass++);
3491 			if(res == -1)
3492 				BAD_ERROR("asprintf failed in "
3493 					"scan1_encomp_readdir\n");
3494 			ERROR("%s\n", dir_name);
3495 		}
3496 		return create_dir_entry(dir_name, basename,
3497 			strdup(source_path[index ++]), dir);
3498 	}
3499 	return NULL;
3500 }
3501 
3502 
scan1_single_readdir(struct dir_info * dir)3503 struct dir_ent *scan1_single_readdir(struct dir_info *dir)
3504 {
3505 	struct dirent *d_name;
3506 	int i;
3507 
3508 	if(dir->count < old_root_entries) {
3509 		for(i = 0; i < old_root_entries; i++) {
3510 			if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
3511 				dir->directory_count ++;
3512 			add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
3513 				&old_root_entry[i].inode, dir);
3514 		}
3515 	}
3516 
3517 	if((d_name = readdir(dir->linuxdir)) != NULL) {
3518 		char *basename = NULL;
3519 		char *dir_name = strdup(d_name->d_name);
3520 		int pass = 1, res;
3521 
3522 		for(;;) {
3523 			struct dir_ent *dir_ent = dir->list;
3524 
3525 			for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3526 				dir_ent = dir_ent->next);
3527 			if(dir_ent == NULL)
3528 				break;
3529 			ERROR("Source directory entry %s already used! - trying"
3530 				" ", dir_name);
3531 			if (pass == 1)
3532 				basename = dir_name;
3533 			else
3534 				free(dir_name);
3535 			res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++);
3536 			if(res == -1)
3537 				BAD_ERROR("asprintf failed in "
3538 					"scan1_single_readdir\n");
3539 			ERROR("%s\n", dir_name);
3540 		}
3541 		return create_dir_entry(dir_name, basename, NULL, dir);
3542 	}
3543 
3544 	return NULL;
3545 }
3546 
3547 
scan1_readdir(struct dir_info * dir)3548 struct dir_ent *scan1_readdir(struct dir_info *dir)
3549 {
3550 	struct dirent *d_name = readdir(dir->linuxdir);
3551 
3552 	return d_name ?
3553 		create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) :
3554 		NULL;
3555 }
3556 
3557 
scan1_freedir(struct dir_info * dir)3558 void scan1_freedir(struct dir_info *dir)
3559 {
3560 	if(dir->pathname[0] != '\0')
3561 		closedir(dir->linuxdir);
3562 }
3563 
3564 
dir_scan1(char * filename,char * subpath,struct pathnames * paths,struct dir_ent * (_readdir)(struct dir_info *),int depth)3565 struct dir_info *dir_scan1(char *filename, char *subpath,
3566 	struct pathnames *paths,
3567 	struct dir_ent *(_readdir)(struct dir_info *), int depth)
3568 {
3569 	struct dir_info *dir = scan1_opendir(filename, subpath, depth);
3570 	struct dir_ent *dir_ent;
3571 
3572 	if(dir == NULL) {
3573 		ERROR_START("Could not open %s", filename);
3574 		ERROR_EXIT(", skipping...\n");
3575 		return NULL;
3576 	}
3577 
3578 	while((dir_ent = _readdir(dir))) {
3579 		struct dir_info *sub_dir;
3580 		struct stat buf;
3581 		struct pathnames *new = NULL;
3582 		char *filename = pathname(dir_ent);
3583 		char *subpath = NULL;
3584 		char *dir_name = dir_ent->name;
3585 
3586 		if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) {
3587 			free_dir_entry(dir_ent);
3588 			continue;
3589 		}
3590 
3591 		if(lstat(filename, &buf) == -1) {
3592 			ERROR_START("Cannot stat dir/file %s because %s",
3593 				filename, strerror(errno));
3594 			ERROR_EXIT(", ignoring\n");
3595 			free_dir_entry(dir_ent);
3596 			continue;
3597 		}
3598 
3599 		if((buf.st_mode & S_IFMT) != S_IFREG &&
3600 					(buf.st_mode & S_IFMT) != S_IFDIR &&
3601 					(buf.st_mode & S_IFMT) != S_IFLNK &&
3602 					(buf.st_mode & S_IFMT) != S_IFCHR &&
3603 					(buf.st_mode & S_IFMT) != S_IFBLK &&
3604 					(buf.st_mode & S_IFMT) != S_IFIFO &&
3605 					(buf.st_mode & S_IFMT) != S_IFSOCK) {
3606 			ERROR_START("File %s has unrecognised filetype %d",
3607 				filename, buf.st_mode & S_IFMT);
3608 			ERROR_EXIT(", ignoring\n");
3609 			free_dir_entry(dir_ent);
3610 			continue;
3611 		}
3612 
3613 		if((old_exclude && old_excluded(filename, &buf)) ||
3614 			(!old_exclude && excluded(dir_name, paths, &new))) {
3615 			add_excluded(dir);
3616 			free_dir_entry(dir_ent);
3617 			continue;
3618 		}
3619 
3620 		if(exclude_actions()) {
3621 			subpath = subpathname(dir_ent);
3622 
3623 			if(eval_exclude_actions(dir_name, filename, subpath,
3624 							&buf, depth, dir_ent)) {
3625 				add_excluded(dir);
3626 				free_dir_entry(dir_ent);
3627 				continue;
3628 			}
3629 		}
3630 
3631 		switch(buf.st_mode & S_IFMT) {
3632 		case S_IFDIR:
3633 			if(subpath == NULL)
3634 				subpath = subpathname(dir_ent);
3635 
3636 			sub_dir = dir_scan1(filename, subpath, new,
3637 					scan1_readdir, depth + 1);
3638 			if(sub_dir) {
3639 				dir->directory_count ++;
3640 				add_dir_entry(dir_ent, sub_dir,
3641 							lookup_inode(&buf));
3642 			} else
3643 				free_dir_entry(dir_ent);
3644 			break;
3645 		case S_IFLNK: {
3646 			int byte;
3647 			static char buff[65536]; /* overflow safe */
3648 
3649 			byte = readlink(filename, buff, 65536);
3650 			if(byte == -1) {
3651 				ERROR_START("Failed to read symlink %s",
3652 								filename);
3653 				ERROR_EXIT(", ignoring\n");
3654 			} else if(byte == 65536) {
3655 				ERROR_START("Symlink %s is greater than 65536 "
3656 							"bytes!", filename);
3657 				ERROR_EXIT(", ignoring\n");
3658 			} else {
3659 				/* readlink doesn't 0 terminate the returned
3660 				 * path */
3661 				buff[byte] = '\0';
3662 				add_dir_entry(dir_ent, NULL, lookup_inode3(&buf,
3663 							 0, 0, buff, byte + 1));
3664 			}
3665 			break;
3666 		}
3667 		default:
3668 			add_dir_entry(dir_ent, NULL, lookup_inode(&buf));
3669 		}
3670 
3671 		free(new);
3672 	}
3673 
3674 	scan1_freedir(dir);
3675 
3676 	return dir;
3677 }
3678 
3679 
3680 /*
3681  * dir_scan2 routines...
3682  * This processes most actions and any pseudo files
3683  */
scan2_readdir(struct dir_info * dir,struct dir_ent * dir_ent)3684 struct dir_ent *scan2_readdir(struct dir_info *dir, struct dir_ent *dir_ent)
3685 {
3686 	if (dir_ent == NULL)
3687 		dir_ent = dir->list;
3688 	else
3689 		dir_ent = dir_ent->next;
3690 
3691 	for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next);
3692 
3693 	return dir_ent;
3694 }
3695 
3696 
scan2_lookup(struct dir_info * dir,char * name)3697 struct dir_ent *scan2_lookup(struct dir_info *dir, char *name)
3698 {
3699 	struct dir_ent *dir_ent = dir->list;
3700 
3701 	for(; dir_ent && strcmp(dir_ent->name, name) != 0;
3702 					dir_ent = dir_ent->next);
3703 
3704 	return dir_ent;
3705 }
3706 
3707 
dir_scan2(struct dir_info * dir,struct pseudo * pseudo)3708 void dir_scan2(struct dir_info *dir, struct pseudo *pseudo)
3709 {
3710 	struct dir_ent *dir_ent = NULL;
3711 	struct pseudo_entry *pseudo_ent;
3712 	struct stat buf;
3713 	static int pseudo_ino = 1;
3714 
3715 	while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
3716 		struct inode_info *inode_info = dir_ent->inode;
3717 		struct stat *buf = &inode_info->buf;
3718 		char *name = dir_ent->name;
3719 
3720 		eval_actions(root_dir, dir_ent);
3721 
3722 		if((buf->st_mode & S_IFMT) == S_IFDIR)
3723 			dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo));
3724 	}
3725 
3726 	while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) {
3727 		dir_ent = scan2_lookup(dir, pseudo_ent->name);
3728 		if(pseudo_ent->dev->type == 'm') {
3729 			struct stat *buf;
3730 			if(dir_ent == NULL) {
3731 				ERROR_START("Pseudo modify file \"%s\" does "
3732 					"not exist in source filesystem.",
3733 					pseudo_ent->pathname);
3734 				ERROR_EXIT("  Ignoring.\n");
3735 				continue;
3736 			}
3737 			if(dir_ent->inode->root_entry) {
3738 				ERROR_START("Pseudo modify file \"%s\" is a "
3739 					"pre-existing file in the filesystem "
3740 					"being appended to.  It cannot be "\
3741 					"modified.", pseudo_ent->pathname);
3742 				ERROR_EXIT("  Ignoring.\n");
3743 				continue;
3744 			}
3745 			buf = &dir_ent->inode->buf;
3746 			buf->st_mode = (buf->st_mode & S_IFMT) |
3747 				pseudo_ent->dev->mode;
3748 			buf->st_uid = pseudo_ent->dev->uid;
3749 			buf->st_gid = pseudo_ent->dev->gid;
3750 			continue;
3751 		}
3752 
3753 		if(dir_ent) {
3754 			if(dir_ent->inode->root_entry) {
3755 				ERROR_START("Pseudo file \"%s\" is a "
3756 					"pre-existing file in the filesystem "
3757 					"being appended to.",
3758 					pseudo_ent->pathname);
3759 				ERROR_EXIT("  Ignoring.\n");
3760 			} else {
3761 				ERROR_START("Pseudo file \"%s\" exists in "
3762 					"source filesystem \"%s\".",
3763 					pseudo_ent->pathname,
3764 					pathname(dir_ent));
3765 				ERROR_EXIT("\nIgnoring, exclude it (-e/-ef) to "
3766 					"override.\n");
3767 			}
3768 			continue;
3769 		}
3770 
3771 		memset(&buf, 0, sizeof(buf));
3772 		buf.st_mode = pseudo_ent->dev->mode;
3773 		buf.st_uid = pseudo_ent->dev->uid;
3774 		buf.st_gid = pseudo_ent->dev->gid;
3775 		buf.st_rdev = makedev(pseudo_ent->dev->major,
3776 			pseudo_ent->dev->minor);
3777 		buf.st_mtime = time(NULL);
3778 		buf.st_ino = pseudo_ino ++;
3779 
3780 		if(pseudo_ent->dev->type == 'd') {
3781 			struct dir_ent *dir_ent =
3782 				create_dir_entry(pseudo_ent->name, NULL,
3783 						pseudo_ent->pathname, dir);
3784 			char *subpath = strdup(subpathname(dir_ent));
3785 			struct dir_info *sub_dir = scan1_opendir("", subpath,
3786 						dir->depth + 1);
3787 			if(sub_dir == NULL) {
3788 				ERROR_START("Could not create pseudo directory "
3789 					"\"%s\"", pseudo_ent->pathname);
3790 				ERROR_EXIT(", skipping...\n");
3791 				free(subpath);
3792 				pseudo_ino --;
3793 				continue;
3794 			}
3795 			dir_scan2(sub_dir, pseudo_ent->pseudo);
3796 			dir->directory_count ++;
3797 			add_dir_entry(dir_ent, sub_dir,
3798 				lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0));
3799 		} else if(pseudo_ent->dev->type == 'f') {
3800 			add_dir_entry2(pseudo_ent->name, NULL,
3801 				pseudo_ent->pathname, NULL,
3802 				lookup_inode2(&buf, PSEUDO_FILE_PROCESS,
3803 				pseudo_ent->dev->pseudo_id), dir);
3804 		} else {
3805 			add_dir_entry2(pseudo_ent->name, NULL,
3806 				pseudo_ent->pathname, NULL,
3807 				lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir);
3808 		}
3809 	}
3810 }
3811 
3812 
3813 /*
3814  * dir_scan3 routines...
3815  * This processes the move action
3816  */
dir_scan3(struct dir_info * dir)3817 void dir_scan3(struct dir_info *dir)
3818 {
3819 	struct dir_ent *dir_ent = NULL;
3820 
3821 	while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
3822 
3823 		eval_move_actions(root_dir, dir_ent);
3824 
3825 		if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
3826 			dir_scan3(dir_ent->dir);
3827 	}
3828 }
3829 
3830 
3831 /*
3832  * dir_scan4 routines...
3833  * This processes the prune action.  This action is designed to do fine
3834  * grained tuning of the in-core directory structure after the exclude,
3835  * move and pseudo actions have been performed.  This allows complex
3836  * tests to be performed which are impossible at exclude time (i.e.
3837  * tests which rely on the in-core directory structure)
3838  */
free_dir(struct dir_info * dir)3839 void free_dir(struct dir_info *dir)
3840 {
3841 	struct dir_ent *dir_ent = dir->list;
3842 
3843 	while(dir_ent) {
3844 		struct dir_ent *tmp = dir_ent;
3845 
3846 		if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
3847 			free_dir(dir_ent->dir);
3848 
3849 		dir_ent = dir_ent->next;
3850 		free_dir_entry(tmp);
3851 	}
3852 
3853 	free(dir->pathname);
3854 	free(dir->subpath);
3855 	free(dir);
3856 }
3857 
3858 
dir_scan4(struct dir_info * dir)3859 void dir_scan4(struct dir_info *dir)
3860 {
3861 	struct dir_ent *dir_ent = dir->list, *prev = NULL;
3862 
3863 	while(dir_ent) {
3864 		if(dir_ent->inode->root_entry) {
3865 			prev = dir_ent;
3866 			dir_ent = dir_ent->next;
3867 			continue;
3868 		}
3869 
3870 		if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
3871 			dir_scan4(dir_ent->dir);
3872 
3873 		if(eval_prune_actions(root_dir, dir_ent)) {
3874 			struct dir_ent *tmp = dir_ent;
3875 
3876 			if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) {
3877 				free_dir(dir_ent->dir);
3878 				dir->directory_count --;
3879 			}
3880 
3881 			dir->count --;
3882 
3883 			/* remove dir_ent from list */
3884 			dir_ent = dir_ent->next;
3885 			if(prev)
3886 				prev->next = dir_ent;
3887 			else
3888 				dir->list = dir_ent;
3889 
3890 			/* free it */
3891 			free_dir_entry(tmp);
3892 
3893 			add_excluded(dir);
3894 			continue;
3895 		}
3896 
3897 		prev = dir_ent;
3898 		dir_ent = dir_ent->next;
3899 	}
3900 }
3901 
3902 
3903 /*
3904  * dir_scan5 routines...
3905  * This processes the empty action.  This action has to be processed after
3906  * all other actions because the previous exclude and move actions and the
3907  * pseudo actions affect whether a directory is empty
3908  */
dir_scan5(struct dir_info * dir)3909 void dir_scan5(struct dir_info *dir)
3910 {
3911 	struct dir_ent *dir_ent = dir->list, *prev = NULL;
3912 
3913 	while(dir_ent) {
3914 		if(dir_ent->inode->root_entry) {
3915 			prev = dir_ent;
3916 			dir_ent = dir_ent->next;
3917 			continue;
3918 		}
3919 
3920 		if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) {
3921 			dir_scan5(dir_ent->dir);
3922 
3923 			if(eval_empty_actions(root_dir, dir_ent)) {
3924 				struct dir_ent *tmp = dir_ent;
3925 
3926 				/*
3927 				 * delete sub-directory, this is by definition
3928 				 * empty
3929 				 */
3930 				free(dir_ent->dir->pathname);
3931 				free(dir_ent->dir->subpath);
3932 				free(dir_ent->dir);
3933 
3934 				/* remove dir_ent from list */
3935 				dir_ent = dir_ent->next;
3936 				if(prev)
3937 					prev->next = dir_ent;
3938 				else
3939 					dir->list = dir_ent;
3940 
3941 				/* free it */
3942 				free_dir_entry(tmp);
3943 
3944 				/* update counts */
3945 				dir->directory_count --;
3946 				dir->count --;
3947 				add_excluded(dir);
3948 				continue;
3949 			}
3950 		}
3951 
3952 		prev = dir_ent;
3953 		dir_ent = dir_ent->next;
3954 	}
3955 }
3956 
3957 
3958 /*
3959  * dir_scan6 routines...
3960  * This sorts every directory and computes the inode numbers
3961  */
3962 
3963 /*
3964  * Bottom up linked list merge sort.
3965  *
3966  * Qsort and other O(n log n) algorithms work well with arrays but not
3967  * linked lists.  Merge sort another O(n log n) sort algorithm on the other hand
3968  * is not ideal for arrays (as it needs an additonal n storage locations
3969  * as sorting is not done in place), but it is ideal for linked lists because
3970  * it doesn't require any extra storage,
3971  */
sort_directory(struct dir_info * dir)3972 void sort_directory(struct dir_info *dir)
3973 {
3974 	struct dir_ent *cur, *l1, *l2, *next;
3975 	int len1, len2, stride = 1;
3976 
3977 	if(dir->list == NULL || dir->count < 2)
3978 		return;
3979 
3980 	/*
3981 	 * We can consider our linked-list to be made up of stride length
3982 	 * sublists.  Eacn iteration around this loop merges adjacent
3983 	 * stride length sublists into larger 2*stride sublists.  We stop
3984 	 * when stride becomes equal to the entire list.
3985 	 *
3986 	 * Initially stride = 1 (by definition a sublist of 1 is sorted), and
3987 	 * these 1 element sublists are merged into 2 element sublists,  which
3988 	 * are then merged into 4 element sublists and so on.
3989 	 */
3990 	do {
3991 		l2 = dir->list; /* head of current linked list */
3992 		cur = NULL; /* empty output list */
3993 
3994 		/*
3995 		 * Iterate through the linked list, merging adjacent sublists.
3996 		 * On each interation l2 points to the next sublist pair to be
3997 		 * merged (if there's only one sublist left this is simply added
3998 		 * to the output list)
3999 		 */
4000 		while(l2) {
4001 			l1 = l2;
4002 			for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next);
4003 			len2 = stride;
4004 
4005 			/*
4006 			 * l1 points to first sublist.
4007 			 * l2 points to second sublist.
4008 			 * Merge them onto the output list
4009 			 */
4010 			while(len1 && l2 && len2) {
4011 				if(strcmp(l1->name, l2->name) <= 0) {
4012 					next = l1;
4013 					l1 = l1->next;
4014 					len1 --;
4015 				} else {
4016 					next = l2;
4017 					l2 = l2->next;
4018 					len2 --;
4019 				}
4020 
4021 				if(cur) {
4022 					cur->next = next;
4023 					cur = next;
4024 				} else
4025 					dir->list = cur = next;
4026 			}
4027 			/*
4028 			 * One sublist is now empty, copy the other one onto the
4029 			 * output list
4030 			 */
4031 			for(; len1; len1 --, l1 = l1->next) {
4032 				if(cur) {
4033 					cur->next = l1;
4034 					cur = l1;
4035 				} else
4036 					dir->list = cur = l1;
4037 			}
4038 			for(; l2 && len2; len2 --, l2 = l2->next) {
4039 				if(cur) {
4040 					cur->next = l2;
4041 					cur = l2;
4042 				} else
4043 					dir->list = cur = l2;
4044 			}
4045 		}
4046 		cur->next = NULL;
4047 		stride = stride << 1;
4048 	} while(stride < dir->count);
4049 }
4050 
4051 
dir_scan6(struct dir_info * dir)4052 void dir_scan6(struct dir_info *dir)
4053 {
4054 	struct dir_ent *dir_ent;
4055 	unsigned int byte_count = 0;
4056 
4057 	sort_directory(dir);
4058 
4059 	for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) {
4060 		byte_count += strlen(dir_ent->name) +
4061 			sizeof(struct squashfs_dir_entry);
4062 
4063 		if(dir_ent->inode->root_entry)
4064 			continue;
4065 
4066 		alloc_inode_no(dir_ent->inode, 0);
4067 
4068 		if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
4069 			dir_scan6(dir_ent->dir);
4070 	}
4071 
4072 	if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE))
4073 		dir->dir_is_ldir = FALSE;
4074 }
4075 
4076 
4077 /*
4078  * dir_scan6 routines...
4079  * This generates the filesystem metadata and writes it out to the destination
4080  */
scan7_init_dir(struct directory * dir)4081 void scan7_init_dir(struct directory *dir)
4082 {
4083 	dir->buff = malloc(SQUASHFS_METADATA_SIZE);
4084 	if(dir->buff == NULL)
4085 		MEM_ERROR();
4086 
4087 	dir->size = SQUASHFS_METADATA_SIZE;
4088 	dir->p = dir->index_count_p = dir->buff;
4089 	dir->entry_count = 256;
4090 	dir->entry_count_p = NULL;
4091 	dir->index = NULL;
4092 	dir->i_count = dir->i_size = 0;
4093 }
4094 
4095 
scan7_readdir(struct directory * dir,struct dir_info * dir_info,struct dir_ent * dir_ent)4096 struct dir_ent *scan7_readdir(struct directory *dir, struct dir_info *dir_info,
4097 	struct dir_ent *dir_ent)
4098 {
4099 	if (dir_ent == NULL)
4100 		dir_ent = dir_info->list;
4101 	else
4102 		dir_ent = dir_ent->next;
4103 
4104 	for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next)
4105 		add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number,
4106 			dir_ent->name, dir_ent->inode->type, dir);
4107 
4108 	return dir_ent;
4109 }
4110 
4111 
scan7_freedir(struct directory * dir)4112 void scan7_freedir(struct directory *dir)
4113 {
4114 	if(dir->index)
4115 		free(dir->index);
4116 	free(dir->buff);
4117 }
4118 
4119 
dir_scan7(squashfs_inode * inode,struct dir_info * dir_info)4120 void dir_scan7(squashfs_inode *inode, struct dir_info *dir_info)
4121 {
4122 	int squashfs_type;
4123 	int duplicate_file;
4124 	struct directory dir;
4125 	struct dir_ent *dir_ent = NULL;
4126 
4127 	scan7_init_dir(&dir);
4128 
4129 	while((dir_ent = scan7_readdir(&dir, dir_info, dir_ent)) != NULL) {
4130 		struct stat *buf = &dir_ent->inode->buf;
4131 
4132 		update_info(dir_ent);
4133 
4134 		if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) {
4135 			switch(buf->st_mode & S_IFMT) {
4136 				case S_IFREG:
4137 					squashfs_type = SQUASHFS_FILE_TYPE;
4138 					write_file(inode, dir_ent,
4139 						&duplicate_file);
4140 					INFO("file %s, uncompressed size %lld "
4141 						"bytes %s\n",
4142 						subpathname(dir_ent),
4143 						(long long) buf->st_size,
4144 						duplicate_file ?  "DUPLICATE" :
4145 						 "");
4146 					break;
4147 
4148 				case S_IFDIR:
4149 					squashfs_type = SQUASHFS_DIR_TYPE;
4150 					dir_scan7(inode, dir_ent->dir);
4151 					break;
4152 
4153 				case S_IFLNK:
4154 					squashfs_type = SQUASHFS_SYMLINK_TYPE;
4155 					create_inode(inode, NULL, dir_ent,
4156 						squashfs_type, 0, 0, 0, NULL,
4157 						NULL, NULL, 0);
4158 					INFO("symbolic link %s inode 0x%llx\n",
4159 						subpathname(dir_ent), *inode);
4160 					sym_count ++;
4161 					break;
4162 
4163 				case S_IFCHR:
4164 					squashfs_type = SQUASHFS_CHRDEV_TYPE;
4165 					create_inode(inode, NULL, dir_ent,
4166 						squashfs_type, 0, 0, 0, NULL,
4167 						NULL, NULL, 0);
4168 					INFO("character device %s inode 0x%llx"
4169 						"\n", subpathname(dir_ent),
4170 						*inode);
4171 					dev_count ++;
4172 					break;
4173 
4174 				case S_IFBLK:
4175 					squashfs_type = SQUASHFS_BLKDEV_TYPE;
4176 					create_inode(inode, NULL, dir_ent,
4177 						squashfs_type, 0, 0, 0, NULL,
4178 						NULL, NULL, 0);
4179 					INFO("block device %s inode 0x%llx\n",
4180 						subpathname(dir_ent), *inode);
4181 					dev_count ++;
4182 					break;
4183 
4184 				case S_IFIFO:
4185 					squashfs_type = SQUASHFS_FIFO_TYPE;
4186 					create_inode(inode, NULL, dir_ent,
4187 						squashfs_type, 0, 0, 0, NULL,
4188 						NULL, NULL, 0);
4189 					INFO("fifo %s inode 0x%llx\n",
4190 						subpathname(dir_ent), *inode);
4191 					fifo_count ++;
4192 					break;
4193 
4194 				case S_IFSOCK:
4195 					squashfs_type = SQUASHFS_SOCKET_TYPE;
4196 					create_inode(inode, NULL, dir_ent,
4197 						squashfs_type, 0, 0, 0, NULL,
4198 						NULL, NULL, 0);
4199 					INFO("unix domain socket %s inode "
4200 						"0x%llx\n",
4201 						subpathname(dir_ent), *inode);
4202 					sock_count ++;
4203 					break;
4204 
4205 				default:
4206 					BAD_ERROR("%s unrecognised file type, "
4207 						"mode is %x\n",
4208 						subpathname(dir_ent),
4209 						buf->st_mode);
4210 			}
4211 			dir_ent->inode->inode = *inode;
4212 			dir_ent->inode->type = squashfs_type;
4213 		 } else {
4214 			*inode = dir_ent->inode->inode;
4215 			squashfs_type = dir_ent->inode->type;
4216 			switch(squashfs_type) {
4217 				case SQUASHFS_FILE_TYPE:
4218 					if(!sorted)
4219 						INFO("file %s, uncompressed "
4220 							"size %lld bytes LINK"
4221 							"\n",
4222 							subpathname(dir_ent),
4223 							(long long)
4224 							buf->st_size);
4225 					break;
4226 				case SQUASHFS_SYMLINK_TYPE:
4227 					INFO("symbolic link %s inode 0x%llx "
4228 						"LINK\n", subpathname(dir_ent),
4229 						 *inode);
4230 					break;
4231 				case SQUASHFS_CHRDEV_TYPE:
4232 					INFO("character device %s inode 0x%llx "
4233 						"LINK\n", subpathname(dir_ent),
4234 						*inode);
4235 					break;
4236 				case SQUASHFS_BLKDEV_TYPE:
4237 					INFO("block device %s inode 0x%llx "
4238 						"LINK\n", subpathname(dir_ent),
4239 						*inode);
4240 					break;
4241 				case SQUASHFS_FIFO_TYPE:
4242 					INFO("fifo %s inode 0x%llx LINK\n",
4243 						subpathname(dir_ent), *inode);
4244 					break;
4245 				case SQUASHFS_SOCKET_TYPE:
4246 					INFO("unix domain socket %s inode "
4247 						"0x%llx LINK\n",
4248 						subpathname(dir_ent), *inode);
4249 					break;
4250 			}
4251 		}
4252 
4253 		add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name,
4254 			squashfs_type, &dir);
4255 	}
4256 
4257 	write_dir(inode, dir_info, &dir);
4258 	INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent),
4259 		*inode);
4260 
4261 	scan7_freedir(&dir);
4262 }
4263 
4264 
slog(unsigned int block)4265 unsigned int slog(unsigned int block)
4266 {
4267 	int i;
4268 
4269 	for(i = 12; i <= 20; i++)
4270 		if(block == (1 << i))
4271 			return i;
4272 	return 0;
4273 }
4274 
4275 
old_excluded(char * filename,struct stat * buf)4276 int old_excluded(char *filename, struct stat *buf)
4277 {
4278 	int i;
4279 
4280 	for(i = 0; i < exclude; i++)
4281 		if((exclude_paths[i].st_dev == buf->st_dev) &&
4282 				(exclude_paths[i].st_ino == buf->st_ino))
4283 			return TRUE;
4284 	return FALSE;
4285 }
4286 
4287 
4288 #define ADD_ENTRY(buf) \
4289 	if(exclude % EXCLUDE_SIZE == 0) { \
4290 		exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \
4291 			* sizeof(struct exclude_info)); \
4292 		if(exclude_paths == NULL) \
4293 			MEM_ERROR(); \
4294 	} \
4295 	exclude_paths[exclude].st_dev = buf.st_dev; \
4296 	exclude_paths[exclude++].st_ino = buf.st_ino;
old_add_exclude(char * path)4297 int old_add_exclude(char *path)
4298 {
4299 	int i;
4300 	char *filename;
4301 	struct stat buf;
4302 
4303 	if(path[0] == '/' || strncmp(path, "./", 2) == 0 ||
4304 			strncmp(path, "../", 3) == 0) {
4305 		if(lstat(path, &buf) == -1) {
4306 			ERROR_START("Cannot stat exclude dir/file %s because "
4307 				"%s", path, strerror(errno));
4308 			ERROR_EXIT(", ignoring\n");
4309 			return TRUE;
4310 		}
4311 		ADD_ENTRY(buf);
4312 		return TRUE;
4313 	}
4314 
4315 	for(i = 0; i < source; i++) {
4316 		int res = asprintf(&filename, "%s/%s", source_path[i], path);
4317 		if(res == -1)
4318 			BAD_ERROR("asprintf failed in old_add_exclude\n");
4319 		if(lstat(filename, &buf) == -1) {
4320 			if(!(errno == ENOENT || errno == ENOTDIR)) {
4321 				ERROR_START("Cannot stat exclude dir/file %s "
4322 					"because %s", filename, strerror(errno));
4323 				ERROR_EXIT(", ignoring\n");
4324 			}
4325 			free(filename);
4326 			continue;
4327 		}
4328 		free(filename);
4329 		ADD_ENTRY(buf);
4330 	}
4331 	return TRUE;
4332 }
4333 
4334 
add_old_root_entry(char * name,squashfs_inode inode,int inode_number,int type)4335 void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
4336 	int type)
4337 {
4338 	old_root_entry = realloc(old_root_entry,
4339 		sizeof(struct old_root_entry_info) * (old_root_entries + 1));
4340 	if(old_root_entry == NULL)
4341 		MEM_ERROR();
4342 
4343 	old_root_entry[old_root_entries].name = strdup(name);
4344 	old_root_entry[old_root_entries].inode.inode = inode;
4345 	old_root_entry[old_root_entries].inode.inode_number = inode_number;
4346 	old_root_entry[old_root_entries].inode.type = type;
4347 	old_root_entry[old_root_entries++].inode.root_entry = TRUE;
4348 }
4349 
4350 
initialise_threads(int readq,int fragq,int bwriteq,int fwriteq,int freelst,char * destination_file)4351 void initialise_threads(int readq, int fragq, int bwriteq, int fwriteq,
4352 	int freelst, char *destination_file)
4353 {
4354 	int i;
4355 	sigset_t sigmask, old_mask;
4356 	int total_mem = readq;
4357 	int reader_size;
4358 	int fragment_size;
4359 	int fwriter_size;
4360 	/*
4361 	 * bwriter_size is global because it is needed in
4362 	 * write_file_blocks_dup()
4363 	 */
4364 
4365 	/*
4366 	 * Never allow the total size of the queues to be larger than
4367 	 * physical memory
4368 	 *
4369 	 * When adding together the possibly user supplied values, make
4370 	 * sure they've not been deliberately contrived to overflow an int
4371 	 */
4372 	if(add_overflow(total_mem, fragq))
4373 		BAD_ERROR("Queue sizes rediculously too large\n");
4374 	total_mem += fragq;
4375 	if(add_overflow(total_mem, bwriteq))
4376 		BAD_ERROR("Queue sizes rediculously too large\n");
4377 	total_mem += bwriteq;
4378 	if(add_overflow(total_mem, fwriteq))
4379 		BAD_ERROR("Queue sizes rediculously too large\n");
4380 	total_mem += fwriteq;
4381 
4382 	check_usable_phys_mem(total_mem);
4383 
4384 	/*
4385 	 * convert from queue size in Mbytes to queue size in
4386 	 * blocks.
4387 	 *
4388 	 * This isn't going to overflow an int unless there exists
4389 	 * systems with more than 8 Petabytes of RAM!
4390 	 */
4391 	reader_size = readq << (20 - block_log);
4392 	fragment_size = fragq << (20 - block_log);
4393 	bwriter_size = bwriteq << (20 - block_log);
4394 	fwriter_size = fwriteq << (20 - block_log);
4395 
4396 	/*
4397 	 * setup signal handlers for the main thread, these cleanup
4398 	 * deleting the destination file, if appending the
4399 	 * handlers for SIGTERM and SIGINT will be replaced with handlers
4400 	 * allowing the user to press ^C twice to restore the existing
4401 	 * filesystem.
4402 	 *
4403 	 * SIGUSR1 is an internal signal, which is used by the sub-threads
4404 	 * to tell the main thread to terminate, deleting the destination file,
4405 	 * or if necessary restoring the filesystem on appending
4406 	 */
4407 	signal(SIGTERM, sighandler);
4408 	signal(SIGINT, sighandler);
4409 	signal(SIGUSR1, sighandler);
4410 
4411 	/* block SIGQUIT and SIGHUP, these are handled by the info thread */
4412 	sigemptyset(&sigmask);
4413 	sigaddset(&sigmask, SIGQUIT);
4414 	sigaddset(&sigmask, SIGHUP);
4415 	sigaddset(&sigmask, SIGALRM);
4416 	if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
4417 		BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4418 
4419 	/*
4420 	 * temporarily block these signals, so the created sub-threads
4421 	 * will ignore them, ensuring the main thread handles them
4422 	 */
4423 	sigemptyset(&sigmask);
4424 	sigaddset(&sigmask, SIGINT);
4425 	sigaddset(&sigmask, SIGTERM);
4426 	sigaddset(&sigmask, SIGUSR1);
4427 	if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1)
4428 		BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4429 
4430 	if(processors == -1) {
4431 #ifndef linux
4432 		int mib[2];
4433 		size_t len = sizeof(processors);
4434 
4435 		mib[0] = CTL_HW;
4436 #ifdef HW_AVAILCPU
4437 		mib[1] = HW_AVAILCPU;
4438 #else
4439 		mib[1] = HW_NCPU;
4440 #endif
4441 
4442 		if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) {
4443 			ERROR_START("Failed to get number of available "
4444 				"processors.");
4445 			ERROR_EXIT("  Defaulting to 1\n");
4446 			processors = 1;
4447 		}
4448 #else
4449 		processors = sysconf(_SC_NPROCESSORS_ONLN);
4450 #endif
4451 	}
4452 
4453 	if(multiply_overflow(processors, 3) ||
4454 			multiply_overflow(processors * 3, sizeof(pthread_t)))
4455 		BAD_ERROR("Processors too large\n");
4456 
4457 	deflator_thread = malloc(processors * 3 * sizeof(pthread_t));
4458 	if(deflator_thread == NULL)
4459 		MEM_ERROR();
4460 
4461 	frag_deflator_thread = &deflator_thread[processors];
4462 	frag_thread = &frag_deflator_thread[processors];
4463 
4464 	to_reader = queue_init(1);
4465 	to_deflate = queue_init(reader_size);
4466 	to_process_frag = queue_init(reader_size);
4467 	to_writer = queue_init(bwriter_size + fwriter_size);
4468 	from_writer = queue_init(1);
4469 	to_frag = queue_init(fragment_size);
4470 	locked_fragment = queue_init(fragment_size);
4471 	to_main = seq_queue_init();
4472 	reader_buffer = cache_init(block_size, reader_size, 0, 0);
4473 	bwriter_buffer = cache_init(block_size, bwriter_size, 1, freelst);
4474 	fwriter_buffer = cache_init(block_size, fwriter_size, 1, freelst);
4475 	fragment_buffer = cache_init(block_size, fragment_size, 1, 0);
4476 	reserve_cache = cache_init(block_size, processors + 1, 1, 0);
4477 	pthread_create(&reader_thread, NULL, reader, NULL);
4478 	pthread_create(&writer_thread, NULL, writer, NULL);
4479 	init_progress_bar();
4480 	init_info();
4481 
4482 	for(i = 0; i < processors; i++) {
4483 		if(pthread_create(&deflator_thread[i], NULL, deflator, NULL))
4484 			BAD_ERROR("Failed to create thread\n");
4485 		if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator,
4486 				NULL) != 0)
4487 			BAD_ERROR("Failed to create thread\n");
4488 		if(pthread_create(&frag_thread[i], NULL, frag_thrd,
4489 				(void *) destination_file) != 0)
4490 			BAD_ERROR("Failed to create thread\n");
4491 	}
4492 
4493 	main_thread = pthread_self();
4494 
4495 	printf("Parallel mksquashfs: Using %d processor%s\n", processors,
4496 			processors == 1 ? "" : "s");
4497 
4498 	/* Restore the signal mask for the main thread */
4499 	if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1)
4500 		BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4501 }
4502 
4503 
write_inode_lookup_table()4504 long long write_inode_lookup_table()
4505 {
4506 	int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count);
4507 	void *it;
4508 
4509 	if(inode_count == sinode_count)
4510 		goto skip_inode_hash_table;
4511 
4512 	it = realloc(inode_lookup_table, lookup_bytes);
4513 	if(it == NULL)
4514 		MEM_ERROR();
4515 	inode_lookup_table = it;
4516 
4517 	for(i = 0; i < INODE_HASH_SIZE; i ++) {
4518 		struct inode_info *inode;
4519 
4520 		for(inode = inode_info[i]; inode; inode = inode->next) {
4521 
4522 			inode_number = get_inode_no(inode);
4523 
4524 			/* The empty action will produce orphaned inode
4525 			 * entries in the inode_info[] table.  These
4526 			 * entries because they are orphaned will not be
4527 			 * allocated an inode number in dir_scan5(), so
4528 			 * skip any entries with the default dummy inode
4529 			 * number of 0 */
4530 			if(inode_number == 0)
4531 				continue;
4532 
4533 			SQUASHFS_SWAP_LONG_LONGS(&inode->inode,
4534 				&inode_lookup_table[inode_number - 1], 1);
4535 
4536 		}
4537 	}
4538 
4539 skip_inode_hash_table:
4540 	return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL,
4541 		noI);
4542 }
4543 
4544 
get_component(char * target,char ** targname)4545 char *get_component(char *target, char **targname)
4546 {
4547 	char *start;
4548 
4549 	while(*target == '/')
4550 		target ++;
4551 
4552 	start = target;
4553 	while(*target != '/' && *target != '\0')
4554 		target ++;
4555 
4556 	*targname = strndup(start, target - start);
4557 
4558 	while(*target == '/')
4559 		target ++;
4560 
4561 	return target;
4562 }
4563 
4564 
free_path(struct pathname * paths)4565 void free_path(struct pathname *paths)
4566 {
4567 	int i;
4568 
4569 	for(i = 0; i < paths->names; i++) {
4570 		if(paths->name[i].paths)
4571 			free_path(paths->name[i].paths);
4572 		free(paths->name[i].name);
4573 		if(paths->name[i].preg) {
4574 			regfree(paths->name[i].preg);
4575 			free(paths->name[i].preg);
4576 		}
4577 	}
4578 
4579 	free(paths);
4580 }
4581 
4582 
add_path(struct pathname * paths,char * target,char * alltarget)4583 struct pathname *add_path(struct pathname *paths, char *target, char *alltarget)
4584 {
4585 	char *targname;
4586 	int i, error;
4587 
4588 	target = get_component(target, &targname);
4589 
4590 	if(paths == NULL) {
4591 		paths = malloc(sizeof(struct pathname));
4592 		if(paths == NULL)
4593 			MEM_ERROR();
4594 
4595 		paths->names = 0;
4596 		paths->name = NULL;
4597 	}
4598 
4599 	for(i = 0; i < paths->names; i++)
4600 		if(strcmp(paths->name[i].name, targname) == 0)
4601 			break;
4602 
4603 	if(i == paths->names) {
4604 		/* allocate new name entry */
4605 		paths->names ++;
4606 		paths->name = realloc(paths->name, (i + 1) *
4607 			sizeof(struct path_entry));
4608 		if(paths->name == NULL)
4609 			MEM_ERROR();
4610 		paths->name[i].name = targname;
4611 		paths->name[i].paths = NULL;
4612 		if(use_regex) {
4613 			paths->name[i].preg = malloc(sizeof(regex_t));
4614 			if(paths->name[i].preg == NULL)
4615 				MEM_ERROR();
4616 			error = regcomp(paths->name[i].preg, targname,
4617 				REG_EXTENDED|REG_NOSUB);
4618 			if(error) {
4619 				char str[1024]; /* overflow safe */
4620 
4621 				regerror(error, paths->name[i].preg, str, 1024);
4622 				BAD_ERROR("invalid regex %s in export %s, "
4623 					"because %s\n", targname, alltarget,
4624 					str);
4625 			}
4626 		} else
4627 			paths->name[i].preg = NULL;
4628 
4629 		if(target[0] == '\0')
4630 			/* at leaf pathname component */
4631 			paths->name[i].paths = NULL;
4632 		else
4633 			/* recurse adding child components */
4634 			paths->name[i].paths = add_path(NULL, target,
4635 				alltarget);
4636 	} else {
4637 		/* existing matching entry */
4638 		free(targname);
4639 
4640 		if(paths->name[i].paths == NULL) {
4641 			/* No sub-directory which means this is the leaf
4642 			 * component of a pre-existing exclude which subsumes
4643 			 * the exclude currently being added, in which case stop
4644 			 * adding components */
4645 		} else if(target[0] == '\0') {
4646 			/* at leaf pathname component and child components exist
4647 			 * from more specific excludes, delete as they're
4648 			 * subsumed by this exclude */
4649 			free_path(paths->name[i].paths);
4650 			paths->name[i].paths = NULL;
4651 		} else
4652 			/* recurse adding child components */
4653 			add_path(paths->name[i].paths, target, alltarget);
4654 	}
4655 
4656 	return paths;
4657 }
4658 
4659 
add_exclude(char * target)4660 void add_exclude(char *target)
4661 {
4662 
4663 	if(target[0] == '/' || strncmp(target, "./", 2) == 0 ||
4664 			strncmp(target, "../", 3) == 0)
4665 		BAD_ERROR("/, ./ and ../ prefixed excludes not supported with "
4666 			"-wildcards or -regex options\n");
4667 	else if(strncmp(target, "... ", 4) == 0)
4668 		stickypath = add_path(stickypath, target + 4, target + 4);
4669 	else
4670 		path = add_path(path, target, target);
4671 }
4672 
4673 
display_path(int depth,struct pathname * paths)4674 void display_path(int depth, struct pathname *paths)
4675 {
4676 	int i, n;
4677 
4678 	if(paths == NULL)
4679 		return;
4680 
4681 	for(i = 0; i < paths->names; i++) {
4682 		for(n = 0; n < depth; n++)
4683 			printf("\t");
4684 		printf("%d: %s\n", depth, paths->name[i].name);
4685 		display_path(depth + 1, paths->name[i].paths);
4686 	}
4687 }
4688 
4689 
display_path2(struct pathname * paths,char * string)4690 void display_path2(struct pathname *paths, char *string)
4691 {
4692 	int i;
4693 	char *path;
4694 
4695 	if(paths == NULL) {
4696 		printf("%s\n", string);
4697 		return;
4698 	}
4699 
4700 	for(i = 0; i < paths->names; i++) {
4701 		int res = asprintf(&path, "%s/%s", string, paths->name[i].name);
4702 		if(res == -1)
4703 			BAD_ERROR("asprintf failed in display_path2\n");
4704 		display_path2(paths->name[i].paths, path);
4705 		free(path);
4706 	}
4707 }
4708 
4709 
add_subdir(struct pathnames * paths,struct pathname * path)4710 struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path)
4711 {
4712 	int count = paths == NULL ? 0 : paths->count;
4713 
4714 	if(count % PATHS_ALLOC_SIZE == 0) {
4715 		paths = realloc(paths, sizeof(struct pathnames) +
4716 			(count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *));
4717 		if(paths == NULL)
4718 			MEM_ERROR();
4719 	}
4720 
4721 	paths->path[count] = path;
4722 	paths->count = count  + 1;
4723 	return paths;
4724 }
4725 
4726 
excluded_match(char * name,struct pathname * path,struct pathnames ** new)4727 int excluded_match(char *name, struct pathname *path, struct pathnames **new)
4728 {
4729 	int i;
4730 
4731 	for(i = 0; i < path->names; i++) {
4732 		int match = use_regex ?
4733 			regexec(path->name[i].preg, name, (size_t) 0,
4734 					NULL, 0) == 0 :
4735 			fnmatch(path->name[i].name, name,
4736 				FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0;
4737 
4738 		if(match) {
4739 			 if(path->name[i].paths == NULL || new == NULL)
4740 				/* match on a leaf component, any subdirectories
4741 			 	* in the filesystem should be excluded */
4742 				return TRUE;
4743 			else
4744 				/* match on a non-leaf component, add any
4745 				 * subdirectories to the new set of
4746 				 * subdirectories to scan for this name */
4747 				*new = add_subdir(*new, path->name[i].paths);
4748 		}
4749 	}
4750 
4751 	return FALSE;
4752 }
4753 
4754 
excluded(char * name,struct pathnames * paths,struct pathnames ** new)4755 int excluded(char *name, struct pathnames *paths, struct pathnames **new)
4756 {
4757 	int n;
4758 
4759 	if(stickypath && excluded_match(name, stickypath, NULL))
4760 		return TRUE;
4761 
4762 	for(n = 0; paths && n < paths->count; n++) {
4763 		int res = excluded_match(name, paths->path[n], new);
4764 		if(res) {
4765 			free(*new);
4766 			*new = NULL;
4767 			return TRUE;
4768 		}
4769 	}
4770 
4771 	/*
4772 	 * Either:
4773 	 * -  no matching names found, return empty new search set, or
4774 	 * -  one or more matches with sub-directories found (no leaf matches),
4775 	 *    in which case return new search set.
4776 	 *
4777 	 * In either case return FALSE as we don't want to exclude this entry
4778 	 */
4779 	return FALSE;
4780 }
4781 
4782 
process_exclude_file(char * argv)4783 void process_exclude_file(char *argv)
4784 {
4785 	FILE *fd;
4786 	char buffer[MAX_LINE + 1]; /* overflow safe */
4787 	char *filename;
4788 
4789 	fd = fopen(argv, "r");
4790 	if(fd == NULL)
4791 		BAD_ERROR("Failed to open exclude file \"%s\" because %s\n",
4792 			argv, strerror(errno));
4793 
4794 	while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) {
4795 		int len = strlen(filename);
4796 
4797 		if(len == MAX_LINE && filename[len - 1] != '\n')
4798 			/* line too large */
4799 			BAD_ERROR("Line too long when reading "
4800 				"exclude file \"%s\", larger than %d "
4801 				"bytes\n", argv, MAX_LINE);
4802 
4803 		/*
4804 		 * Remove '\n' terminator if it exists (the last line
4805 		 * in the file may not be '\n' terminated)
4806 		 */
4807 		if(len && filename[len - 1] == '\n')
4808 			filename[len - 1] = '\0';
4809 
4810 		/* Skip any leading whitespace */
4811 		while(isspace(*filename))
4812 			filename ++;
4813 
4814 		/* if comment line, skip */
4815 		if(*filename == '#')
4816 			continue;
4817 
4818 		/*
4819 		 * check for initial backslash, to accommodate
4820 		 * filenames with leading space or leading # character
4821 		 */
4822 		if(*filename == '\\')
4823 			filename ++;
4824 
4825 		/* if line is now empty after skipping characters, skip it */
4826 		if(*filename == '\0')
4827 			continue;
4828 
4829 		if(old_exclude)
4830 			old_add_exclude(filename);
4831 		else
4832 			add_exclude(filename);
4833 	}
4834 
4835 	if(ferror(fd))
4836 		BAD_ERROR("Reading exclude file \"%s\" failed because %s\n",
4837 			argv, strerror(errno));
4838 
4839 	fclose(fd);
4840 }
4841 
4842 /* ANDROID CHANGES START*/
4843 #ifdef ANDROID
4844 /*
4845  * Return TRUE (don't compress) if the (regular) file is in the
4846  * whitelist. Else return the Global noD value.
4847  *
4848  * Note : These functions are lifted 100% from the existing exclude
4849  * file code. For maintainability, I've kept this code separate from
4850  * the exclude code instead of having common code for both paths.
4851  */
4852 static int
whitelisted(struct stat * buf)4853 whitelisted(struct stat *buf)
4854 {
4855 	int i;
4856 
4857 	/*
4858 	 * only regular files in the whitelist
4859 	 */
4860 	if (!S_ISREG(buf->st_mode))
4861 		return noD;
4862 	for (i = 0; i < whitelist; i++) {
4863 		if ((whitelist_paths[i].st_dev == buf->st_dev) &&
4864 		    (whitelist_paths[i].st_ino == buf->st_ino)) {
4865 			/* Don't compress */
4866 			whitelisted_count++;
4867 			return TRUE;
4868 		}
4869 	}
4870 	return noD;
4871 }
4872 
4873 static void
add_whitelist_entry(char * filename,struct stat * buf)4874 add_whitelist_entry(char *filename, struct stat *buf)
4875 {
4876 	if (!S_ISREG(buf->st_mode)) {
4877 		BAD_ERROR("Cannot whitelist %s only regular files can be whitelisted",
4878 			  filename);
4879 	}
4880 	if (whitelist % WHITELIST_SIZE == 0) {
4881 		whitelist_paths = realloc(whitelist_paths,
4882 					  (whitelist + WHITELIST_SIZE)
4883 					  * sizeof(struct whitelist_info));
4884 		if (whitelist_paths == NULL)
4885 			MEM_ERROR();
4886 	}
4887 	whitelist_paths[whitelist].st_dev = buf->st_dev;
4888 	whitelist_paths[whitelist++].st_ino = buf->st_ino;
4889 }
4890 
4891 static int
add_whitelist(char * path)4892 add_whitelist(char *path)
4893 {
4894 	int i;
4895 	char *filename;
4896 	struct stat buf;
4897 
4898 	/* Absolute of (filesystem) relative path */
4899 	if (path[0] == '/' || strncmp(path, "./", 2) == 0 ||
4900 	    strncmp(path, "../", 3) == 0) {
4901 		if(lstat(path, &buf) == -1) {
4902 			BAD_ERROR("Cannot stat whitelist dir/file %s because "
4903 				  "%s", path, strerror(errno));
4904 		}
4905 		add_whitelist_entry(path, &buf);
4906 		return TRUE;
4907 	}
4908 
4909 	/* pathname relative to mksquashfs source dirs */
4910 	for(i = 0; i < source; i++) {
4911 		int res = asprintf(&filename, "%s/%s", source_path[i], path);
4912 		if(res == -1)
4913 			BAD_ERROR("asprintf failed in add_whitelist\n");
4914 		if(lstat(filename, &buf) == -1) {
4915 			if(!(errno == ENOENT || errno == ENOTDIR)) {
4916 				BAD_ERROR("Cannot stat whitelist dir/file %s "
4917 					  "because %s", filename, strerror(errno));
4918 			}
4919 			free(filename);
4920 			continue;
4921 		}
4922 		add_whitelist_entry(filename, &buf);
4923 		free(filename);
4924 	}
4925 	return TRUE;
4926 }
4927 
4928 static void
process_whitelist_file(char * argv)4929 process_whitelist_file(char *argv)
4930 {
4931 	FILE *fd;
4932 	char buffer[MAX_LINE + 1]; /* overflow safe */
4933 	char *filename;
4934 
4935 	fd = fopen(argv, "r");
4936 	if(fd == NULL)
4937 		BAD_ERROR("Failed to open whitelist file \"%s\" because %s\n",
4938 			argv, strerror(errno));
4939 
4940 	while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) {
4941 		int len = strlen(filename);
4942 
4943 		if(len == MAX_LINE && filename[len - 1] != '\n')
4944 			/* line too large */
4945 			BAD_ERROR("Line too long when reading "
4946 				"whitelist file \"%s\", larger than %d "
4947 				"bytes\n", argv, MAX_LINE);
4948 
4949 		/*
4950 		 * Remove '\n' terminator if it exists (the last line
4951 		 * in the file may not be '\n' terminated)
4952 		 */
4953 		if(len && filename[len - 1] == '\n')
4954 			filename[len - 1] = '\0';
4955 
4956 		/* Skip any leading whitespace */
4957 		while(isspace(*filename))
4958 			filename ++;
4959 
4960 		/* if comment line, skip */
4961 		if(*filename == '#')
4962 			continue;
4963 
4964 		/*
4965 		 * check for initial backslash, to accommodate
4966 		 * filenames with leading space or leading # character
4967 		 */
4968 		if(*filename == '\\')
4969 			filename ++;
4970 
4971 		/* if line is now empty after skipping characters, skip it */
4972 		if(*filename == '\0')
4973 			continue;
4974 
4975 		add_whitelist(filename);
4976 	}
4977 
4978 	if(ferror(fd))
4979 		BAD_ERROR("Reading whitelist file \"%s\" failed because %s\n",
4980 			argv, strerror(errno));
4981 
4982 	fclose(fd);
4983 }
4984 #endif
4985 /* ANDROID CHANGES END */
4986 
4987 #define RECOVER_ID "Squashfs recovery file v1.0\n"
4988 #define RECOVER_ID_SIZE 28
4989 
write_recovery_data(struct squashfs_super_block * sBlk)4990 void write_recovery_data(struct squashfs_super_block *sBlk)
4991 {
4992 	int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start;
4993 	pid_t pid = getpid();
4994 	char *metadata;
4995 	char header[] = RECOVER_ID;
4996 
4997 	if(recover == FALSE) {
4998 		printf("No recovery data option specified.\n");
4999 		printf("Skipping saving recovery file.\n\n");
5000 		return;
5001 	}
5002 
5003 	metadata = malloc(bytes);
5004 	if(metadata == NULL)
5005 		MEM_ERROR();
5006 
5007 	res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata);
5008 	if(res == 0) {
5009 		ERROR("Failed to read append filesystem metadata\n");
5010 		BAD_ERROR("Filesystem corrupted?\n");
5011 	}
5012 
5013 	res = asprintf(&recovery_file, "squashfs_recovery_%s_%d",
5014 		getbase(destination_file), pid);
5015 	if(res == -1)
5016 		MEM_ERROR();
5017 
5018 	recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
5019 	if(recoverfd == -1)
5020 		BAD_ERROR("Failed to create recovery file, because %s.  "
5021 			"Aborting\n", strerror(errno));
5022 
5023 	if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1)
5024 		BAD_ERROR("Failed to write recovery file, because %s\n",
5025 			strerror(errno));
5026 
5027 	if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1)
5028 		BAD_ERROR("Failed to write recovery file, because %s\n",
5029 			strerror(errno));
5030 
5031 	if(write_bytes(recoverfd, metadata, bytes) == -1)
5032 		BAD_ERROR("Failed to write recovery file, because %s\n",
5033 			strerror(errno));
5034 
5035 	close(recoverfd);
5036 	free(metadata);
5037 
5038 	printf("Recovery file \"%s\" written\n", recovery_file);
5039 	printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n");
5040 	printf("mksquashfs dummy %s -recover %s\n", destination_file,
5041 		recovery_file);
5042 	printf("to restore filesystem\n\n");
5043 }
5044 
5045 
read_recovery_data(char * recovery_file,char * destination_file)5046 void read_recovery_data(char *recovery_file, char *destination_file)
5047 {
5048 	int fd, recoverfd, bytes;
5049 	struct squashfs_super_block orig_sBlk, sBlk;
5050 	char *metadata;
5051 	int res;
5052 	struct stat buf;
5053 	char header[] = RECOVER_ID;
5054 	char header2[RECOVER_ID_SIZE];
5055 
5056 	recoverfd = open(recovery_file, O_RDONLY);
5057 	if(recoverfd == -1)
5058 		BAD_ERROR("Failed to open recovery file because %s\n",
5059 			strerror(errno));
5060 
5061 	if(stat(destination_file, &buf) == -1)
5062 		BAD_ERROR("Failed to stat destination file, because %s\n",
5063 			strerror(errno));
5064 
5065 	fd = open(destination_file, O_RDWR);
5066 	if(fd == -1)
5067 		BAD_ERROR("Failed to open destination file because %s\n",
5068 			strerror(errno));
5069 
5070 	res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE);
5071 	if(res == -1)
5072 		BAD_ERROR("Failed to read recovery file, because %s\n",
5073 			strerror(errno));
5074 	if(res < RECOVER_ID_SIZE)
5075 		BAD_ERROR("Recovery file appears to be truncated\n");
5076 	if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 )
5077 		BAD_ERROR("Not a recovery file\n");
5078 
5079 	res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block));
5080 	if(res == -1)
5081 		BAD_ERROR("Failed to read recovery file, because %s\n",
5082 			strerror(errno));
5083 	if(res < sizeof(struct squashfs_super_block))
5084 		BAD_ERROR("Recovery file appears to be truncated\n");
5085 
5086 	res = read_fs_bytes(fd, 0, sizeof(struct squashfs_super_block), &orig_sBlk);
5087 	if(res == 0) {
5088 		ERROR("Failed to read superblock from output filesystem\n");
5089 		BAD_ERROR("Output filesystem is empty!\n");
5090 	}
5091 
5092 	if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4,
5093 			sizeof(struct squashfs_super_block) - 4) != 0)
5094 		BAD_ERROR("Recovery file and destination file do not seem to "
5095 			"match\n");
5096 
5097 	bytes = sBlk.bytes_used - sBlk.inode_table_start;
5098 
5099 	metadata = malloc(bytes);
5100 	if(metadata == NULL)
5101 		MEM_ERROR();
5102 
5103 	res = read_bytes(recoverfd, metadata, bytes);
5104 	if(res == -1)
5105 		BAD_ERROR("Failed to read recovery file, because %s\n",
5106 			strerror(errno));
5107 	if(res < bytes)
5108 		BAD_ERROR("Recovery file appears to be truncated\n");
5109 
5110 	write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk);
5111 
5112 	write_destination(fd, sBlk.inode_table_start, bytes, metadata);
5113 
5114 	close(recoverfd);
5115 	close(fd);
5116 
5117 	printf("Successfully wrote recovery file \"%s\".  Exiting\n",
5118 		recovery_file);
5119 
5120 	exit(0);
5121 }
5122 
5123 
write_filesystem_tables(struct squashfs_super_block * sBlk,int nopad)5124 void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad)
5125 {
5126 	int i;
5127 
5128 	sBlk->fragments = fragments;
5129 	sBlk->no_ids = id_count;
5130 	sBlk->inode_table_start = write_inodes();
5131 	sBlk->directory_table_start = write_directories();
5132 	sBlk->fragment_table_start = write_fragment_table();
5133 	sBlk->lookup_table_start = exportable ? write_inode_lookup_table() :
5134 		SQUASHFS_INVALID_BLK;
5135 	sBlk->id_table_start = write_id_table();
5136 	sBlk->xattr_id_table_start = write_xattrs();
5137 
5138 	TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start);
5139 	TRACE("sBlk->directory_table_start 0x%llx\n",
5140 		sBlk->directory_table_start);
5141 	TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start);
5142 	if(exportable)
5143 		TRACE("sBlk->lookup_table_start 0x%llx\n",
5144 			sBlk->lookup_table_start);
5145 
5146 	sBlk->bytes_used = bytes;
5147 
5148 	sBlk->compression = comp->id;
5149 
5150 	SQUASHFS_INSWAP_SUPER_BLOCK(sBlk);
5151 	write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk);
5152 
5153 	if(!nopad && (i = bytes & (4096 - 1))) {
5154 		char temp[4096] = {0};
5155 		write_destination(fd, bytes, 4096 - i, temp);
5156 	}
5157 
5158 	close(fd);
5159 
5160 	if(recovery_file)
5161 		unlink(recovery_file);
5162 
5163 	total_bytes += total_inode_bytes + total_directory_bytes +
5164 		sizeof(struct squashfs_super_block) + total_xattr_bytes;
5165 
5166 	printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size"
5167 		" %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR,
5168 		SQUASHFS_MINOR, comp->name, block_size);
5169 	printf("\t%s data, %s metadata, %s fragments, %s xattrs\n",
5170 		noD ? "uncompressed" : "compressed", noI ?  "uncompressed" :
5171 		"compressed", no_fragments ? "no" : noF ? "uncompressed" :
5172 		"compressed", no_xattrs ? "no" : noX ? "uncompressed" :
5173 		"compressed");
5174 	printf("\tduplicates are %sremoved\n", duplicate_checking ? "" :
5175 		"not ");
5176 	printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0,
5177 		bytes / (1024.0 * 1024.0));
5178 	printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n",
5179 		((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0);
5180 	printf("Inode table size %d bytes (%.2f Kbytes)\n",
5181 		inode_bytes, inode_bytes / 1024.0);
5182 	printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n",
5183 		((float) inode_bytes / total_inode_bytes) * 100.0,
5184 		total_inode_bytes);
5185 	printf("Directory table size %d bytes (%.2f Kbytes)\n",
5186 		directory_bytes, directory_bytes / 1024.0);
5187 	printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n",
5188 		((float) directory_bytes / total_directory_bytes) * 100.0,
5189 		total_directory_bytes);
5190 	if(total_xattr_bytes) {
5191 		printf("Xattr table size %d bytes (%.2f Kbytes)\n",
5192 			xattr_bytes, xattr_bytes / 1024.0);
5193 		printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n",
5194 			((float) xattr_bytes / total_xattr_bytes) * 100.0,
5195 			total_xattr_bytes);
5196 	}
5197 	if(duplicate_checking)
5198 		printf("Number of duplicate files found %d\n", file_count -
5199 			dup_files);
5200 	else
5201 		printf("No duplicate files removed\n");
5202 	printf("Number of inodes %d\n", inode_count);
5203 	printf("Number of files %d\n", file_count);
5204 	if(!no_fragments)
5205 		printf("Number of fragments %d\n", fragments);
5206 	printf("Number of symbolic links  %d\n", sym_count);
5207 	printf("Number of device nodes %d\n", dev_count);
5208 	printf("Number of fifo nodes %d\n", fifo_count);
5209 	printf("Number of socket nodes %d\n", sock_count);
5210 	printf("Number of directories %d\n", dir_count);
5211 	printf("Number of ids (unique uids + gids) %d\n", id_count);
5212 	printf("Number of uids %d\n", uid_count);
5213 
5214 	for(i = 0; i < id_count; i++) {
5215 		if(id_table[i]->flags & ISA_UID) {
5216 			struct passwd *user = getpwuid(id_table[i]->id);
5217 			printf("\t%s (%d)\n", user == NULL ? "unknown" :
5218 				user->pw_name, id_table[i]->id);
5219 		}
5220 	}
5221 
5222 	printf("Number of gids %d\n", guid_count);
5223 
5224 	for(i = 0; i < id_count; i++) {
5225 		if(id_table[i]->flags & ISA_GID) {
5226 			struct group *group = getgrgid(id_table[i]->id);
5227 			printf("\t%s (%d)\n", group == NULL ? "unknown" :
5228 				group->gr_name, id_table[i]->id);
5229 		}
5230 	}
5231 
5232 	printf("Number of whitelisted (uncompressed) files %d\n",
5233 	       whitelisted_count);
5234 }
5235 
5236 
parse_numberll(char * start,long long * res,int size)5237 int parse_numberll(char *start, long long *res, int size)
5238 {
5239 	char *end;
5240 	long long number;
5241 
5242 	errno = 0; /* To distinguish success/failure after call */
5243 
5244 	number = strtoll(start, &end, 10);
5245 
5246 	/*
5247 	 * check for strtoll underflow or overflow in conversion, and other
5248 	 * errors.
5249 	 */
5250 	if((errno == ERANGE && (number == LLONG_MIN || number == LLONG_MAX)) ||
5251 			(errno != 0 && number == 0))
5252 		return 0;
5253 
5254 	/* reject negative numbers as invalid */
5255 	if(number < 0)
5256 		return 0;
5257 
5258 	if(size) {
5259 		/*
5260 		 * Check for multiplier and trailing junk.
5261 		 * But first check that a number exists before the
5262 		 * multiplier
5263 		 */
5264 		if(end == start)
5265 			return 0;
5266 
5267 		switch(end[0]) {
5268 		case 'g':
5269 		case 'G':
5270 			if(multiply_overflowll(number, 1073741824))
5271 				return 0;
5272 			number *= 1073741824;
5273 
5274 			if(end[1] != '\0')
5275 				/* trailing junk after multiplier, but
5276 				 * allow it to be "bytes" */
5277 				if(strcmp(end + 1, "bytes"))
5278 					return 0;
5279 
5280 			break;
5281 		case 'm':
5282 		case 'M':
5283 			if(multiply_overflowll(number, 1048576))
5284 				return 0;
5285 			number *= 1048576;
5286 
5287 			if(end[1] != '\0')
5288 				/* trailing junk after multiplier, but
5289 				 * allow it to be "bytes" */
5290 				if(strcmp(end + 1, "bytes"))
5291 					return 0;
5292 
5293 			break;
5294 		case 'k':
5295 		case 'K':
5296 			if(multiply_overflowll(number, 1024))
5297 				return 0;
5298 			number *= 1024;
5299 
5300 			if(end[1] != '\0')
5301 				/* trailing junk after multiplier, but
5302 				 * allow it to be "bytes" */
5303 				if(strcmp(end + 1, "bytes"))
5304 					return 0;
5305 
5306 			break;
5307 		case '\0':
5308 			break;
5309 		default:
5310 			/* trailing junk after number */
5311 			return 0;
5312 		}
5313 	} else if(end[0] != '\0')
5314 		/* trailing junk after number */
5315 		return 0;
5316 
5317 	*res = number;
5318 	return 1;
5319 }
5320 
5321 
parse_number(char * start,int * res,int size)5322 int parse_number(char *start, int *res, int size)
5323 {
5324 	long long number;
5325 
5326 	if(!parse_numberll(start, &number, size))
5327 		return 0;
5328 
5329 	/* check if long result will overflow signed int */
5330 	if(number > INT_MAX)
5331 		return 0;
5332 
5333 	*res = (int) number;
5334 	return 1;
5335 }
5336 
5337 
parse_num(char * arg,int * res)5338 int parse_num(char *arg, int *res)
5339 {
5340 	return parse_number(arg, res, 0);
5341 }
5342 
5343 
parse_ugid_map(char * map_str,struct ugid_map_entry ugid_mapping[UGID_ENTRIES],unsigned int * ugid_map_count)5344 int parse_ugid_map(char *map_str,
5345 	struct ugid_map_entry ugid_mapping[UGID_ENTRIES],
5346 	unsigned int *ugid_map_count)
5347 {
5348 	char *line_state, *token_state;
5349 	char *line, *line_str, *token, *token_str;
5350 	long long numbers[3];
5351 	int i;
5352 
5353 	for (*ugid_map_count = 0, line_str = map_str;;
5354 		++*ugid_map_count, line_str = NULL) {
5355 		line = strtok_r(line_str, "\n", &line_state);
5356 		if (line == NULL)
5357 			break;
5358                 ERROR("line: %s\n", line);
5359 		if (*ugid_map_count >= UGID_ENTRIES) {
5360 			ERROR("Too many entries for u/gid mapping\n");
5361 			return -1;
5362 		}
5363 
5364 		for (i = 0, token_str = line; i < 3; i++, token_str = NULL) {
5365 			token = strtok_r(token_str, " ", &token_state);
5366                         ERROR("token: %d, %s\n", i, token);
5367 			if (token == NULL ||
5368 				!parse_numberll(token, &numbers[i], 0) ||
5369 				numbers[i] < 0 || numbers[i] > ULONG_MAX) {
5370 				ERROR("Malformed u/gid mapping line1\n");
5371 				return -1;
5372 			}
5373 		}
5374 
5375 		if (numbers[0] + numbers[2] > ULONG_MAX) {
5376 			ERROR("u/gid mapping overflow\n");
5377 			return -1;
5378 		}
5379 
5380 		if (numbers[1] + numbers[2] > ULONG_MAX) {
5381 			ERROR("u/gid mapping overflow\n");
5382 			return -1;
5383 		}
5384 
5385 		if (strtok_r(NULL, " ", &token_state) != NULL) {
5386 			ERROR("Malformed u/gid mapping line2\n");
5387 			return -1;
5388 		}
5389 
5390 		ugid_mapping[*ugid_map_count].child_id =
5391 			(unsigned int)numbers[0];
5392 		ugid_mapping[*ugid_map_count].parent_id =
5393 			(unsigned int)numbers[1];
5394 		ugid_mapping[*ugid_map_count].length = (unsigned int)numbers[2];
5395 	}
5396 
5397 	return 0;
5398 }
5399 
5400 
get_physical_memory()5401 int get_physical_memory()
5402 {
5403 	int phys_mem;
5404 #ifndef linux
5405 	#ifdef HW_MEMSIZE
5406 		#define SYSCTL_PHYSMEM HW_MEMSIZE
5407 	#elif defined(HW_PHYSMEM64)
5408 		#define SYSCTL_PHYSMEM HW_PHYSMEM64
5409 	#else
5410 		#define SYSCTL_PHYSMEM HW_PHYSMEM
5411 	#endif
5412 
5413 	int mib[2];
5414 	uint64_t sysctl_physmem = 0;
5415 	size_t sysctl_len = sizeof(sysctl_physmem);
5416 
5417 	mib[0] = CTL_HW;
5418 	mib[1] = SYSCTL_PHYSMEM;
5419 
5420 	if(sysctl(mib, 2, &sysctl_physmem, &sysctl_len, NULL, 0) == 0) {
5421 		/* some systems use 32-bit values, work with what we're given */
5422 		if (sysctl_len == 4)
5423 			sysctl_physmem = *(uint32_t*)&sysctl_physmem;
5424 		phys_mem = sysctl_physmem >> 20;
5425 	} else {
5426 		ERROR_START("Failed to get amount of available "
5427 			"memory.");
5428 		ERROR_EXIT("  Defaulting to least viable amount\n");
5429 		phys_mem = SQUASHFS_LOWMEM;
5430 	}
5431   #undef SYSCTL_PHYSMEM
5432 #else
5433 	/* Long longs are used here because with PAE, a 32-bit
5434 	  machine can have more than 4GB of physical memory */
5435 
5436 	long long num_pages = sysconf(_SC_PHYS_PAGES);
5437 	long long page_size = sysconf(_SC_PAGESIZE);
5438 	phys_mem = num_pages * page_size >> 20;
5439 	if(num_pages == -1 || page_size == -1)
5440 		return 0;
5441 
5442 #endif
5443 
5444 	if(phys_mem < SQUASHFS_LOWMEM)
5445 		BAD_ERROR("Mksquashfs requires more physical memory than is "
5446 			"available!\n");
5447 
5448 	return phys_mem;
5449 }
5450 
5451 
check_usable_phys_mem(int total_mem)5452 void check_usable_phys_mem(int total_mem)
5453 {
5454 	/*
5455 	 * We want to allow users to use as much of their physical
5456 	 * memory as they wish.  However, for practical reasons there are
5457 	 * limits which need to be imposed, to protect users from themselves
5458 	 * and to prevent people from using Mksquashfs as a DOS attack by using
5459 	 * all physical memory.   Mksquashfs uses memory to cache data from disk
5460 	 * to optimise performance.  It is pointless to ask it to use more
5461 	 * than 75% of physical memory, as this causes thrashing and it is thus
5462 	 * self-defeating.
5463 	 */
5464 	int mem = get_physical_memory();
5465 
5466 	mem = (mem >> 1) + (mem >> 2); /* 75% */
5467 
5468 	if(total_mem > mem && mem) {
5469 		ERROR("Total memory requested is more than 75%% of physical "
5470 						"memory.\n");
5471 		ERROR("Mksquashfs uses memory to cache data from disk to "
5472 						"optimise performance.\n");
5473 		ERROR("It is pointless to ask it to use more than this amount "
5474 						"of memory, as this\n");
5475 		ERROR("causes thrashing and it is thus self-defeating.\n");
5476 		BAD_ERROR("Requested memory size too large\n");
5477 	}
5478 
5479 	if(sizeof(void *) == 4 && total_mem > 2048) {
5480 		/*
5481 		 * If we're running on a kernel with PAE or on a 64-bit kernel,
5482 		 * then the 75% physical memory limit can still easily exceed
5483 		 * the addressable memory by this process.
5484 		 *
5485 		 * Due to the typical kernel/user-space split (1GB/3GB, or
5486 		 * 2GB/2GB), we have to conservatively assume the 32-bit
5487 		 * processes can only address 2-3GB.  So refuse if the user
5488 		 * tries to allocate more than 2GB.
5489 		 */
5490 		ERROR("Total memory requested may exceed maximum "
5491 				"addressable memory by this process\n");
5492 		BAD_ERROR("Requested memory size too large\n");
5493 	}
5494 }
5495 
5496 
get_default_phys_mem()5497 int get_default_phys_mem()
5498 {
5499 	/*
5500 	 * get_physical_memory() relies on /proc being mounted.
5501 	 * If it fails, issue a warning, and use
5502 	 * SQUASHFS_LOWMEM / SQUASHFS_TAKE as default,
5503 	 * and allow a larger value to be set with -mem.
5504 	 */
5505 	int mem = get_physical_memory();
5506 
5507 	if(mem == 0) {
5508 		mem = SQUASHFS_LOWMEM / SQUASHFS_TAKE;
5509 
5510 		ERROR("Warning: Cannot get size of physical memory, probably "
5511 				"because /proc is missing.\n");
5512 		ERROR("Warning: Defaulting to minimal use of %d Mbytes, use "
5513 				"-mem to set a better value,\n", mem);
5514 		ERROR("Warning: or fix /proc.\n");
5515 	} else
5516 		mem /= SQUASHFS_TAKE;
5517 
5518 	if(sizeof(void *) == 4 && mem > 640) {
5519 		/*
5520 		 * If we're running on a kernel with PAE or on a 64-bit kernel,
5521 		 * the default memory usage can exceed the addressable
5522 		 * memory by this process.
5523 		 * Due to the typical kernel/user-space split (1GB/3GB, or
5524 		 * 2GB/2GB), we have to conservatively assume the 32-bit
5525 		 * processes can only address 2-3GB.  So limit the  default
5526 		 * usage to 640M, which gives room for other data.
5527 		 */
5528 		mem = 640;
5529 	}
5530 
5531 	return mem;
5532 }
5533 
5534 
calculate_queue_sizes(int mem,int * readq,int * fragq,int * bwriteq,int * fwriteq)5535 void calculate_queue_sizes(int mem, int *readq, int *fragq, int *bwriteq,
5536 							int *fwriteq)
5537 {
5538 	*readq = mem / SQUASHFS_READQ_MEM;
5539 	*bwriteq = mem / SQUASHFS_BWRITEQ_MEM;
5540 	*fwriteq = mem / SQUASHFS_FWRITEQ_MEM;
5541 	*fragq = mem - *readq - *bwriteq - *fwriteq;
5542 }
5543 
5544 
5545 #define VERSION() \
5546 	printf("mksquashfs version 4.3-git (2014/09/12)\n");\
5547 	printf("copyright (C) 2014 Phillip Lougher "\
5548 		"<phillip@squashfs.org.uk>\n\n"); \
5549 	printf("This program is free software; you can redistribute it and/or"\
5550 		"\n");\
5551 	printf("modify it under the terms of the GNU General Public License"\
5552 		"\n");\
5553 	printf("as published by the Free Software Foundation; either version "\
5554 		"2,\n");\
5555 	printf("or (at your option) any later version.\n\n");\
5556 	printf("This program is distributed in the hope that it will be "\
5557 		"useful,\n");\
5558 	printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\
5559 		"of\n");\
5560 	printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"\
5561 		"\n");\
5562 	printf("GNU General Public License for more details.\n");
main(int argc,char * argv[])5563 int main(int argc, char *argv[])
5564 {
5565 	struct stat buf, source_buf;
5566 	int res, i;
5567 	char *b, *root_name = NULL;
5568 	int keep_as_directory = FALSE;
5569 	squashfs_inode inode;
5570 	int readq;
5571 	int fragq;
5572 	int bwriteq;
5573 	int fwriteq;
5574 	int total_mem = get_default_phys_mem();
5575 	int progress = TRUE;
5576 	int force_progress = FALSE;
5577 	struct file_buffer **fragment = NULL;
5578 /* ANDROID CHANGES START*/
5579 #ifdef ANDROID
5580 	const char *fs_config_file = NULL;
5581 #endif
5582 /* ANDROID CHANGES END */
5583 
5584 	if(argc > 1 && strcmp(argv[1], "-version") == 0) {
5585 		VERSION();
5586 		exit(0);
5587 	}
5588 
5589 	block_log = slog(block_size);
5590 	calculate_queue_sizes(total_mem, &readq, &fragq, &bwriteq, &fwriteq);
5591 
5592         for(i = 1; i < argc && argv[i][0] != '-'; i++);
5593 	if(i < 3)
5594 		goto printOptions;
5595 	source_path = argv + 1;
5596 	source = i - 2;
5597 
5598 	/*
5599 	 * Scan the command line for -comp xxx option, this is to ensure
5600 	 * any -X compressor specific options are passed to the
5601 	 * correct compressor
5602 	 */
5603 	for(; i < argc; i++) {
5604 		struct compressor *prev_comp = comp;
5605 
5606 		if(strcmp(argv[i], "-comp") == 0) {
5607 			if(++i == argc) {
5608 				ERROR("%s: -comp missing compression type\n",
5609 					argv[0]);
5610 				exit(1);
5611 			}
5612 			comp = lookup_compressor(argv[i]);
5613 			if(!comp->supported) {
5614 				ERROR("%s: Compressor \"%s\" is not supported!"
5615 					"\n", argv[0], argv[i]);
5616 				ERROR("%s: Compressors available:\n", argv[0]);
5617 				display_compressors("", COMP_DEFAULT);
5618 				exit(1);
5619 			}
5620 			if(prev_comp != NULL && prev_comp != comp) {
5621 				ERROR("%s: -comp multiple conflicting -comp"
5622 					" options specified on command line"
5623 					", previously %s, now %s\n", argv[0],
5624 					prev_comp->name, comp->name);
5625 				exit(1);
5626 			}
5627 			compressor_opt_parsed = 1;
5628 
5629 		} else if(strcmp(argv[i], "-e") == 0)
5630 			break;
5631 		else if(strcmp(argv[i], "-root-becomes") == 0 ||
5632 				strcmp(argv[i], "-ef") == 0 ||
5633 				strcmp(argv[i], "-pf") == 0 ||
5634 				strcmp(argv[i], "-vaf") == 0 ||
5635 				strcmp(argv[i], "-comp") == 0)
5636 			i++;
5637 	}
5638 
5639 	/*
5640 	 * if no -comp option specified lookup default compressor.  Note the
5641 	 * Makefile ensures the default compressor has been built, and so we
5642 	 * don't need to to check for failure here
5643 	 */
5644 	if(comp == NULL)
5645 		comp = lookup_compressor(COMP_DEFAULT);
5646 
5647 	for(i = source + 2; i < argc; i++) {
5648 		if(strcmp(argv[i], "-action") == 0 ||
5649 				strcmp(argv[i], "-a") ==0) {
5650 			if(++i == argc) {
5651 				ERROR("%s: %s missing action\n",
5652 					argv[0], argv[i - 1]);
5653 				exit(1);
5654 			}
5655 			res = parse_action(argv[i], ACTION_LOG_NONE);
5656 			if(res == 0)
5657 				exit(1);
5658 
5659 		} else if(strcmp(argv[i], "-verbose-action") == 0 ||
5660 				strcmp(argv[i], "-va") ==0) {
5661 			if(++i == argc) {
5662 				ERROR("%s: %s missing action\n",
5663 					argv[0], argv[i - 1]);
5664 				exit(1);
5665 			}
5666 			res = parse_action(argv[i], ACTION_LOG_VERBOSE);
5667 			if(res == 0)
5668 				exit(1);
5669 
5670 		} else if(strcmp(argv[i], "-true-action") == 0 ||
5671 				strcmp(argv[i], "-ta") ==0) {
5672 			if(++i == argc) {
5673 				ERROR("%s: %s missing action\n",
5674 					argv[0], argv[i - 1]);
5675 				exit(1);
5676 			}
5677 			res = parse_action(argv[i], ACTION_LOG_TRUE);
5678 			if(res == 0)
5679 				exit(1);
5680 
5681 		} else if(strcmp(argv[i], "-false-action") == 0 ||
5682 				strcmp(argv[i], "-fa") ==0) {
5683 			if(++i == argc) {
5684 				ERROR("%s: %s missing action\n",
5685 					argv[0], argv[i - 1]);
5686 				exit(1);
5687 			}
5688 			res = parse_action(argv[i], ACTION_LOG_FALSE);
5689 			if(res == 0)
5690 				exit(1);
5691 
5692 		} else if(strcmp(argv[i], "-action-file") == 0 ||
5693 				strcmp(argv[i], "-af") ==0) {
5694 			if(++i == argc) {
5695 				ERROR("%s: %s missing filename\n", argv[0],
5696 							argv[i - 1]);
5697 				exit(1);
5698 			}
5699 			if(read_action_file(argv[i], ACTION_LOG_NONE) == FALSE)
5700 				exit(1);
5701 
5702 		} else if(strcmp(argv[i], "-verbose-action-file") == 0 ||
5703 				strcmp(argv[i], "-vaf") ==0) {
5704 			if(++i == argc) {
5705 				ERROR("%s: %s missing filename\n", argv[0],
5706 							argv[i - 1]);
5707 				exit(1);
5708 			}
5709 			if(read_action_file(argv[i], ACTION_LOG_VERBOSE) == FALSE)
5710 				exit(1);
5711 
5712 		} else if(strcmp(argv[i], "-true-action-file") == 0 ||
5713 				strcmp(argv[i], "-taf") ==0) {
5714 			if(++i == argc) {
5715 				ERROR("%s: %s missing filename\n", argv[0],
5716 							argv[i - 1]);
5717 				exit(1);
5718 			}
5719 			if(read_action_file(argv[i], ACTION_LOG_TRUE) == FALSE)
5720 				exit(1);
5721 
5722 		} else if(strcmp(argv[i], "-false-action-file") == 0 ||
5723 				strcmp(argv[i], "-faf") ==0) {
5724 			if(++i == argc) {
5725 				ERROR("%s: %s missing filename\n", argv[0],
5726 							argv[i - 1]);
5727 				exit(1);
5728 			}
5729 			if(read_action_file(argv[i], ACTION_LOG_FALSE) == FALSE)
5730 				exit(1);
5731 
5732 		} else if(strcmp(argv[i], "-comp") == 0)
5733 			/* parsed previously */
5734 			i++;
5735 
5736 		else if(strncmp(argv[i], "-X", 2) == 0) {
5737 			int args;
5738 
5739 			if(strcmp(argv[i] + 2, "help") == 0)
5740 				goto print_compressor_options;
5741 
5742 			args = compressor_options(comp, argv + i, argc - i);
5743 			if(args < 0) {
5744 				if(args == -1) {
5745 					ERROR("%s: Unrecognised compressor"
5746 						" option %s\n", argv[0],
5747 						argv[i]);
5748 					if(!compressor_opt_parsed)
5749 						ERROR("%s: Did you forget to"
5750 							" specify -comp?\n",
5751 							argv[0]);
5752 print_compressor_options:
5753 					ERROR("%s: selected compressor \"%s\""
5754 						".  Options supported: %s\n",
5755 						argv[0], comp->name,
5756 						comp->usage ? "" : "none");
5757 					if(comp->usage)
5758 						comp->usage();
5759 				}
5760 				exit(1);
5761 			}
5762 			i += args;
5763 
5764 		} else if(strcmp(argv[i], "-pf") == 0) {
5765 			if(++i == argc) {
5766 				ERROR("%s: -pf missing filename\n", argv[0]);
5767 				exit(1);
5768 			}
5769 			if(read_pseudo_file(argv[i]) == FALSE)
5770 				exit(1);
5771 		} else if(strcmp(argv[i], "-p") == 0) {
5772 			if(++i == argc) {
5773 				ERROR("%s: -p missing pseudo file definition\n",
5774 					argv[0]);
5775 				exit(1);
5776 			}
5777 			if(read_pseudo_def(argv[i]) == FALSE)
5778 				exit(1);
5779 		} else if(strcmp(argv[i], "-recover") == 0) {
5780 			if(++i == argc) {
5781 				ERROR("%s: -recover missing recovery file\n",
5782 					argv[0]);
5783 				exit(1);
5784 			}
5785 			read_recovery_data(argv[i], argv[source + 1]);
5786 		} else if(strcmp(argv[i], "-no-recovery") == 0)
5787 			recover = FALSE;
5788 		else if(strcmp(argv[i], "-wildcards") == 0) {
5789 			old_exclude = FALSE;
5790 			use_regex = FALSE;
5791 		} else if(strcmp(argv[i], "-regex") == 0) {
5792 			old_exclude = FALSE;
5793 			use_regex = TRUE;
5794 		} else if(strcmp(argv[i], "-no-sparse") == 0)
5795 			sparse_files = FALSE;
5796 		else if(strcmp(argv[i], "-no-progress") == 0)
5797 			progress = FALSE;
5798 		else if(strcmp(argv[i], "-progress") == 0)
5799 			force_progress = TRUE;
5800 		else if(strcmp(argv[i], "-no-exports") == 0)
5801 			exportable = FALSE;
5802 		else if(strcmp(argv[i], "-processors") == 0) {
5803 			if((++i == argc) || !parse_num(argv[i], &processors)) {
5804 				ERROR("%s: -processors missing or invalid "
5805 					"processor number\n", argv[0]);
5806 				exit(1);
5807 			}
5808 			if(processors < 1) {
5809 				ERROR("%s: -processors should be 1 or larger\n",
5810 					argv[0]);
5811 				exit(1);
5812 			}
5813 		} else if(strcmp(argv[i], "-read-queue") == 0) {
5814 			if((++i == argc) || !parse_num(argv[i], &readq)) {
5815 				ERROR("%s: -read-queue missing or invalid "
5816 					"queue size\n", argv[0]);
5817 				exit(1);
5818 			}
5819 			if(readq < 1) {
5820 				ERROR("%s: -read-queue should be 1 megabyte or "
5821 					"larger\n", argv[0]);
5822 				exit(1);
5823 			}
5824 		} else if(strcmp(argv[i], "-write-queue") == 0) {
5825 			if((++i == argc) || !parse_num(argv[i], &bwriteq)) {
5826 				ERROR("%s: -write-queue missing or invalid "
5827 					"queue size\n", argv[0]);
5828 				exit(1);
5829 			}
5830 			if(bwriteq < 2) {
5831 				ERROR("%s: -write-queue should be 2 megabytes "
5832 					"or larger\n", argv[0]);
5833 				exit(1);
5834 			}
5835 			fwriteq = bwriteq >> 1;
5836 			bwriteq -= fwriteq;
5837 		} else if(strcmp(argv[i], "-fragment-queue") == 0) {
5838 			if((++i == argc) || !parse_num(argv[i], &fragq)) {
5839 				ERROR("%s: -fragment-queue missing or invalid "
5840 					"queue size\n", argv[0]);
5841 				exit(1);
5842 			}
5843 			if(fragq < 1) {
5844 				ERROR("%s: -fragment-queue should be 1 "
5845 					"megabyte or larger\n", argv[0]);
5846 				exit(1);
5847 			}
5848 		} else if(strcmp(argv[i], "-mem") == 0) {
5849 			long long number;
5850 
5851 			if((++i == argc) ||
5852 					!parse_numberll(argv[i], &number, 1)) {
5853 				ERROR("%s: -mem missing or invalid mem size\n",
5854 					 argv[0]);
5855 				exit(1);
5856 			}
5857 
5858 			/*
5859 			 * convert from bytes to Mbytes, ensuring the value
5860 			 * does not overflow a signed int
5861 			 */
5862 			if(number >= (1LL << 51)) {
5863 				ERROR("%s: -mem invalid mem size\n", argv[0]);
5864 				exit(1);
5865 			}
5866 
5867 			total_mem = number / 1048576;
5868 			if(total_mem < (SQUASHFS_LOWMEM / SQUASHFS_TAKE)) {
5869 				ERROR("%s: -mem should be %d Mbytes or "
5870 					"larger\n", argv[0],
5871 					SQUASHFS_LOWMEM / SQUASHFS_TAKE);
5872 				exit(1);
5873 			}
5874 			calculate_queue_sizes(total_mem, &readq, &fragq,
5875 				&bwriteq, &fwriteq);
5876 		} else if(strcmp(argv[i], "-b") == 0) {
5877 			if(++i == argc) {
5878 				ERROR("%s: -b missing block size\n", argv[0]);
5879 				exit(1);
5880 			}
5881 			if(!parse_number(argv[i], &block_size, 1)) {
5882 				ERROR("%s: -b invalid block size\n", argv[0]);
5883 				exit(1);
5884 			}
5885 			if((block_log = slog(block_size)) == 0) {
5886 				ERROR("%s: -b block size not power of two or "
5887 					"not between 4096 and 1Mbyte\n",
5888 					argv[0]);
5889 				exit(1);
5890 			}
5891 		} else if(strcmp(argv[i], "-ef") == 0) {
5892 			if(++i == argc) {
5893 				ERROR("%s: -ef missing filename\n", argv[0]);
5894 				exit(1);
5895 			}
5896 		} else if(strcmp(argv[i], "-no-duplicates") == 0)
5897 			duplicate_checking = FALSE;
5898 
5899 		else if(strcmp(argv[i], "-no-fragments") == 0)
5900 			no_fragments = TRUE;
5901 
5902 		 else if(strcmp(argv[i], "-always-use-fragments") == 0)
5903 			always_use_fragments = TRUE;
5904 
5905 		 else if(strcmp(argv[i], "-sort") == 0) {
5906 			if(++i == argc) {
5907 				ERROR("%s: -sort missing filename\n", argv[0]);
5908 				exit(1);
5909 			}
5910 		} else if(strcmp(argv[i], "-all-root") == 0 ||
5911 				strcmp(argv[i], "-root-owned") == 0)
5912 			global_uid = global_gid = 0;
5913 
5914 		else if(strcmp(argv[i], "-force-uid") == 0) {
5915 			if(++i == argc) {
5916 				ERROR("%s: -force-uid missing uid or user\n",
5917 					argv[0]);
5918 				exit(1);
5919 			}
5920 			if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') {
5921 				if(global_uid < 0 || global_uid >
5922 						(((long long) 1 << 32) - 1)) {
5923 					ERROR("%s: -force-uid uid out of range"
5924 						"\n", argv[0]);
5925 					exit(1);
5926 				}
5927 			} else {
5928 				struct passwd *uid = getpwnam(argv[i]);
5929 				if(uid)
5930 					global_uid = uid->pw_uid;
5931 				else {
5932 					ERROR("%s: -force-uid invalid uid or "
5933 						"unknown user\n", argv[0]);
5934 					exit(1);
5935 				}
5936 			}
5937 		} else if(strcmp(argv[i], "-force-gid") == 0) {
5938 			if(++i == argc) {
5939 				ERROR("%s: -force-gid missing gid or group\n",
5940 					argv[0]);
5941 				exit(1);
5942 			}
5943 			if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') {
5944 				if(global_gid < 0 || global_gid >
5945 						(((long long) 1 << 32) - 1)) {
5946 					ERROR("%s: -force-gid gid out of range"
5947 						"\n", argv[0]);
5948 					exit(1);
5949 				}
5950 			} else {
5951 				struct group *gid = getgrnam(argv[i]);
5952 				if(gid)
5953 					global_gid = gid->gr_gid;
5954 				else {
5955 					ERROR("%s: -force-gid invalid gid or "
5956 						"unknown group\n", argv[0]);
5957 					exit(1);
5958 				}
5959 			}
5960 		} else if(strcmp(argv[i], "-noI") == 0 ||
5961 				strcmp(argv[i], "-noInodeCompression") == 0)
5962 			noI = TRUE;
5963 
5964 		else if(strcmp(argv[i], "-noD") == 0 ||
5965 				strcmp(argv[i], "-noDataCompression") == 0)
5966 			noD = TRUE;
5967 
5968 		else if(strcmp(argv[i], "-noF") == 0 ||
5969 				strcmp(argv[i], "-noFragmentCompression") == 0)
5970 			noF = TRUE;
5971 
5972 		else if(strcmp(argv[i], "-noX") == 0 ||
5973 				strcmp(argv[i], "-noXattrCompression") == 0)
5974 			noX = TRUE;
5975 
5976 		else if(strcmp(argv[i], "-no-xattrs") == 0)
5977 			no_xattrs = TRUE;
5978 
5979 		else if(strcmp(argv[i], "-xattrs") == 0)
5980 			no_xattrs = FALSE;
5981 
5982 /* ANDROID CHANGES START*/
5983 #ifdef ANDROID
5984 		else if(strcmp(argv[i], "-context-file") == 0) {
5985 			if(++i == argc) {
5986 				ERROR("%s: -context-file: missing file name\n",
5987 					argv[0]);
5988 				exit(1);
5989 			}
5990 			context_file = argv[i];
5991 		}
5992 		else if(strcmp(argv[i], "-fs-config-file") == 0) {
5993 			if(++i == argc) {
5994 				ERROR("%s: -fs-config-file: missing file name\n",
5995 					argv[0]);
5996 				exit(1);
5997 			}
5998 			fs_config_file = argv[i];
5999 		} else if(strcmp(argv[i], "-whitelist") == 0) {
6000 			if(++i == argc) {
6001 				ERROR("%s: -whitelist missing filename\n", argv[0]);
6002 				exit(1);
6003 			}
6004 			whitelist_filename = argv[i];
6005 		}
6006 		else if(strcmp(argv[i], "-t") == 0) {
6007 			if(++i == argc) {
6008 				ERROR("%s: -t missing compression threshold percentage\n", argv[0]);
6009 				exit(1);
6010 			}
6011 			if(!parse_number(argv[i], &compress_thresh_per, 1)) {
6012 				ERROR("%s: -t invalid compression threshold percentage\n", argv[0]);
6013 				exit(1);
6014 			}
6015 			if(compress_thresh_per > 100 || compress_thresh_per < 0) {
6016 				ERROR("%s: -t compression threshold percentage not between 0 and 100\n",
6017 					argv[0]);
6018 				exit(1);
6019 			}
6020 		}
6021 #endif
6022 /* ANDROID CHANGES END */
6023 		else if(strcmp(argv[i], "-nopad") == 0)
6024 			nopad = TRUE;
6025 
6026 		else if(strcmp(argv[i], "-info") == 0)
6027 			silent = FALSE;
6028 
6029 		else if(strcmp(argv[i], "-e") == 0)
6030 			break;
6031 
6032 		else if(strcmp(argv[i], "-noappend") == 0)
6033 			delete = TRUE;
6034 
6035 		else if(strcmp(argv[i], "-keep-as-directory") == 0)
6036 			keep_as_directory = TRUE;
6037 /* ANDROID CHANGES START*/
6038 #ifdef ANDROID
6039 		else if(strcmp(argv[i], "-android-fs-config") == 0)
6040 			android_config = TRUE;
6041 		else if(strcmp(argv[i], "-mount-point") == 0) {
6042 			if(++i == argc) {
6043 				ERROR("%s: -mount-point: missing mount point name\n",
6044 					argv[0]);
6045 				exit(1);
6046 			}
6047 			mount_point = argv[i];
6048 		}
6049 		else if(strcmp(argv[i], "-product-out") == 0) {
6050 			if(++i == argc) {
6051 				ERROR("%s: -product-out: missing path name\n",
6052 					argv[0]);
6053 				exit(1);
6054 			}
6055 			target_out_path = argv[i];
6056 		}
6057 		else if(strcmp(argv[i], "-disable-4k-align") == 0)
6058 			align_4k_blocks = FALSE;
6059 		else if(strcmp(argv[i], "-block-map") == 0) {
6060 			if(++i == argc) {
6061 				ERROR("%s: -block-map: missing path name\n",
6062 					argv[0]);
6063 				exit(1);
6064 			}
6065 			block_map_file = fopen(argv[i], "w");
6066 			if (block_map_file == NULL) {
6067 				ERROR("%s: -block-map: failed to open %s\n",
6068 					argv[0], argv[i]);
6069 				exit(1);
6070 			}
6071 			if (!align_4k_blocks) {
6072 				ERROR("WARNING: Using block maps with unaligned 4k blocks "
6073 					  "is not ideal as block map offsets are multiples of 4k, "
6074 					  "consider not passing -disable-4k-align\n");
6075 			}
6076 		}
6077 #endif
6078 /* ANDROID CHANGES END */
6079 
6080 		else if(strcmp(argv[i], "-exit-on-error") == 0)
6081 			exit_on_error = TRUE;
6082 
6083 		else if(strcmp(argv[i], "-root-becomes") == 0) {
6084 			if(++i == argc) {
6085 				ERROR("%s: -root-becomes: missing name\n",
6086 					argv[0]);
6087 				exit(1);
6088 			}
6089 			root_name = argv[i];
6090 		} else if (strcmp(argv[i], "-uid-map") == 0) {
6091 			if (++i == argc) {
6092 				ERROR("%s: -uid-map: missing mapping\n",
6093 					argv[0]);
6094 				exit(1);
6095 			}
6096 			if (parse_ugid_map(argv[i], uid_mapping,
6097 				&uid_map_count) != 0) {
6098 				ERROR("%s: -uid-map: invalid mapping\n",
6099 					argv[0]);
6100 				exit(1);
6101 			}
6102 		} else if (strcmp(argv[i], "-gid-map") == 0) {
6103 			if (++i == argc) {
6104 				ERROR("%s: -gid-map: missing mapping\n",
6105 					argv[0]);
6106 				exit(1);
6107 			}
6108 			if (parse_ugid_map(argv[i], gid_mapping,
6109 				&gid_map_count) != 0) {
6110 				ERROR("%s: -gid-map: invalid mapping\n",
6111 					argv[0]);
6112 				exit(1);
6113 			}
6114 		} else if(strcmp(argv[i], "-version") == 0) {
6115 			VERSION();
6116 		} else {
6117 			ERROR("%s: invalid option\n\n", argv[0]);
6118 printOptions:
6119 			ERROR("SYNTAX:%s source1 source2 ...  dest [options] "
6120 				"[-e list of exclude\ndirs/files]\n", argv[0]);
6121 			ERROR("\nFilesystem build options:\n");
6122 			ERROR("-comp <comp>\t\tselect <comp> compression\n");
6123 			ERROR("\t\t\tCompressors available:\n");
6124 			display_compressors("\t\t\t", COMP_DEFAULT);
6125 			ERROR("-b <block_size>\t\tset data block to "
6126 				"<block_size>.  Default 128 Kbytes\n");
6127 			ERROR("\t\t\tOptionally a suffix of K or M can be"
6128 				" given to specify\n\t\t\tKbytes or Mbytes"
6129 				" respectively\n");
6130 			ERROR("-no-exports\t\tdon't make the filesystem "
6131 				"exportable via NFS\n");
6132 			ERROR("-no-sparse\t\tdon't detect sparse files\n");
6133 			ERROR("-no-xattrs\t\tdon't store extended attributes"
6134 				NOXOPT_STR "\n");
6135 			ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR
6136 				"\n");
6137 /* ANDROID CHANGES START*/
6138 #ifdef ANDROID
6139 			ERROR("-context-file <file>\tApply selinux security "
6140 				"xattrs from context-file instead\n\t\t\t"
6141 				"of reading xattrs from file system\n");
6142 			ERROR("-fs-config-file <file>\tAndroid specific "
6143 				"filesystem config file\n");
6144 			ERROR("-t <compress_thresh>\tset minimum "
6145 				"acceptable compression ratio of a block to\n\t\t\t"
6146 				"<compress_thresh_per> otherwise don't compress. "
6147 				"Default 0%\n");
6148 			ERROR("-whitelist <file>\tAndroid specific whitelist "
6149 			      "one entry per line (no wildcards)\n");
6150 #endif
6151 /* ANDROID CHANGES END */
6152 			ERROR("-noI\t\t\tdo not compress inode table\n");
6153 			ERROR("-noD\t\t\tdo not compress data blocks\n");
6154 			ERROR("-noF\t\t\tdo not compress fragment blocks\n");
6155 			ERROR("-noX\t\t\tdo not compress extended "
6156 				"attributes\n");
6157 			ERROR("-no-fragments\t\tdo not use fragments\n");
6158 			ERROR("-always-use-fragments\tuse fragment blocks for "
6159 				"files larger than block size\n");
6160 			ERROR("-no-duplicates\t\tdo not perform duplicate "
6161 				"checking\n");
6162 			ERROR("-all-root\t\tmake all files owned by root\n");
6163 			ERROR("-force-uid uid\t\tset all file uids to uid\n");
6164 			ERROR("-force-gid gid\t\tset all file gids to gid\n");
6165 			ERROR("-nopad\t\t\tdo not pad filesystem to a multiple "
6166 				"of 4K\n");
6167 			ERROR("-keep-as-directory\tif one source directory is "
6168 				"specified, create a root\n");
6169 			ERROR("\t\t\tdirectory containing that directory, "
6170 				"rather than the\n");
6171 			ERROR("\t\t\tcontents of the directory\n");
6172 /* ANDROID CHANGES START*/
6173 #ifdef ANDROID
6174 			ERROR("-android-fs-config\tuse android fs config "
6175 				"for mode, uid, and gids of inodes\n");
6176 			ERROR("-mount-point <name>\tNeed to be provided when "
6177 				"android-fs-config or context-file\n\t\t\tare "
6178 				"enabled and source directory is not mount point\n");
6179 			ERROR("-product-out <path>\tPRODUCT_OUT directory to "
6180 				"read device specific FS rules files from\n");
6181 			ERROR("-disable-4k-align \tDon't 4k align data blocks. Default is false\n");
6182 			ERROR("-block-map <path>\tGenerate a block map for non-fragment files\n");
6183 #endif
6184 /* ANDROID CHANGES END */
6185 			ERROR("\nFilesystem filter options:\n");
6186 			ERROR("-p <pseudo-definition>\tAdd pseudo file "
6187 				"definition\n");
6188 			ERROR("-pf <pseudo-file>\tAdd list of pseudo file "
6189 				"definitions\n");
6190 			ERROR("-sort <sort_file>\tsort files according to "
6191 				"priorities in <sort_file>.  One\n");
6192 			ERROR("\t\t\tfile or dir with priority per line.  "
6193 				"Priority -32768 to\n");
6194 			ERROR("\t\t\t32767, default priority 0\n");
6195 			ERROR("-ef <exclude_file>\tlist of exclude dirs/files."
6196 				"  One per line\n");
6197 			ERROR("-wildcards\t\tAllow extended shell wildcards "
6198 				"(globbing) to be used in\n\t\t\texclude "
6199 				"dirs/files\n");
6200 			ERROR("-regex\t\t\tAllow POSIX regular expressions to "
6201 				"be used in exclude\n\t\t\tdirs/files\n");
6202 			ERROR("-uid-map <mapping>\tUser ID mapping.\n");
6203 			ERROR("\t\t\tFollows the format described in "
6204 				"user_namespaces(7).\n");
6205 			ERROR("-gid-map <mapping>\tGroup ID mapping.\n");
6206 			ERROR("\t\t\tFollows the format described in "
6207 				"user_namespaces(7).\n");
6208 			ERROR("\nFilesystem append options:\n");
6209 			ERROR("-noappend\t\tdo not append to existing "
6210 				"filesystem\n");
6211 			ERROR("-root-becomes <name>\twhen appending source "
6212 				"files/directories, make the\n");
6213 			ERROR("\t\t\toriginal root become a subdirectory in "
6214 				"the new root\n");
6215 			ERROR("\t\t\tcalled <name>, rather than adding the new "
6216 				"source items\n");
6217 			ERROR("\t\t\tto the original root\n");
6218 			ERROR("\nMksquashfs runtime options:\n");
6219 			ERROR("-version\t\tprint version, licence and "
6220 				"copyright message\n");
6221 			ERROR("-exit-on-error\t\ttreat normally ignored errors "
6222 				"as fatal\n");
6223 			ERROR("-recover <name>\t\trecover filesystem data "
6224 				"using recovery file <name>\n");
6225 			ERROR("-no-recovery\t\tdon't generate a recovery "
6226 				"file\n");
6227 			ERROR("-info\t\t\tprint files written to filesystem\n");
6228 			ERROR("-no-progress\t\tdon't display the progress "
6229 				"bar\n");
6230 			ERROR("-progress\t\tdisplay progress bar when using "
6231 				"the -info option\n");
6232 			ERROR("-processors <number>\tUse <number> processors."
6233 				"  By default will use number of\n");
6234 			ERROR("\t\t\tprocessors available\n");
6235 			ERROR("-mem <size>\t\tUse <size> physical memory.  "
6236 				"Currently set to %dM\n", total_mem);
6237 			ERROR("\t\t\tOptionally a suffix of K, M or G can be"
6238 				" given to specify\n\t\t\tKbytes, Mbytes or"
6239 				" Gbytes respectively\n");
6240 			ERROR("\nMiscellaneous options:\n");
6241 			ERROR("-root-owned\t\talternative name for -all-root"
6242 				"\n");
6243 			ERROR("-noInodeCompression\talternative name for -noI"
6244 				"\n");
6245 			ERROR("-noDataCompression\talternative name for -noD"
6246 				"\n");
6247 			ERROR("-noFragmentCompression\talternative name for "
6248 				"-noF\n");
6249 			ERROR("-noXattrCompression\talternative name for "
6250 				"-noX\n");
6251 			ERROR("\n-Xhelp\t\t\tprint compressor options for"
6252 				" selected compressor\n");
6253 			ERROR("\nCompressors available and compressor specific "
6254 				"options:\n");
6255 			display_compressor_usage(COMP_DEFAULT);
6256 			exit(1);
6257 		}
6258 	}
6259 
6260 	if (!uid_map_count) {
6261 		uid_mapping[0].child_id = 0;
6262 		uid_mapping[0].parent_id = 0;
6263 		uid_mapping[0].length = 4294967295u;
6264 		uid_map_count = 1;
6265 	}
6266 	if (!gid_map_count) {
6267 		gid_mapping[0].child_id = 0;
6268 		gid_mapping[0].parent_id = 0;
6269 		gid_mapping[0].length = 4294967295u;
6270 		gid_map_count = 1;
6271 	}
6272 
6273 /* ANDROID CHANGES START*/
6274 #ifdef ANDROID
6275 	if (fs_config_file) {
6276 		if (load_canned_fs_config(fs_config_file) < 0) {
6277 			fprintf(stderr, "failed to load %s\n", fs_config_file);
6278 			exit(1);
6279 		}
6280 		fs_config_func = canned_fs_config;
6281 	} else if (mount_point) {
6282 		fs_config_func = fs_config;
6283 	}
6284 	if (whitelist_filename)
6285 		process_whitelist_file(whitelist_filename);
6286 #endif
6287 /* ANDROID CHANGES END */
6288 
6289 	/*
6290 	 * Some compressors may need the options to be checked for validity
6291 	 * once all the options have been processed
6292 	 */
6293 	res = compressor_options_post(comp, block_size);
6294 	if(res)
6295 		EXIT_MKSQUASHFS();
6296 
6297 	/*
6298 	 * If the -info option has been selected then disable the
6299 	 * progress bar unless it has been explicitly enabled with
6300 	 * the -progress option
6301 	 */
6302 	if(!silent)
6303 		progress = force_progress;
6304 
6305 #ifdef SQUASHFS_TRACE
6306 	/*
6307 	 * Disable progress bar if full debug tracing is enabled.
6308 	 * The progress bar in this case just gets in the way of the
6309 	 * debug trace output
6310 	 */
6311 	progress = FALSE;
6312 #endif
6313 
6314 	for(i = 0; i < source; i++)
6315 		if(lstat(source_path[i], &source_buf) == -1) {
6316 			fprintf(stderr, "Cannot stat source directory \"%s\" "
6317 				"because %s\n", source_path[i],
6318 				strerror(errno));
6319 			EXIT_MKSQUASHFS();
6320 		}
6321 
6322 	destination_file = argv[source + 1];
6323 	if(stat(argv[source + 1], &buf) == -1) {
6324 		if(errno == ENOENT) { /* Does not exist */
6325 			fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR,
6326 				S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
6327 			if(fd == -1) {
6328 				perror("Could not create destination file");
6329 				exit(1);
6330 			}
6331 			delete = TRUE;
6332 		} else {
6333 			perror("Could not stat destination file");
6334 			exit(1);
6335 		}
6336 
6337 	} else {
6338 		if(S_ISBLK(buf.st_mode)) {
6339 			if((fd = open(argv[source + 1], O_RDWR)) == -1) {
6340 				perror("Could not open block device as "
6341 					"destination");
6342 				exit(1);
6343 			}
6344 			block_device = 1;
6345 
6346 		} else if(S_ISREG(buf.st_mode))	 {
6347 			fd = open(argv[source + 1], (delete ? O_TRUNC : 0) |
6348 				O_RDWR);
6349 			if(fd == -1) {
6350 				perror("Could not open regular file for "
6351 					"writing as destination");
6352 				exit(1);
6353 			}
6354 		}
6355 		else {
6356 			ERROR("Destination not block device or regular file\n");
6357 			exit(1);
6358 		}
6359 
6360 	}
6361 
6362 	/*
6363 	 * process the exclude files - must be done afer destination file has
6364 	 * been possibly created
6365 	 */
6366 	for(i = source + 2; i < argc; i++)
6367 		if(strcmp(argv[i], "-ef") == 0)
6368 			/*
6369 			 * Note presence of filename arg has already
6370 			 * been checked
6371 			 */
6372 			process_exclude_file(argv[++i]);
6373 		else if(strcmp(argv[i], "-e") == 0)
6374 			break;
6375 		else if(strcmp(argv[i], "-root-becomes") == 0 ||
6376 				strcmp(argv[i], "-sort") == 0 ||
6377 				strcmp(argv[i], "-pf") == 0 ||
6378 				strcmp(argv[i], "-af") == 0 ||
6379 				strcmp(argv[i], "-vaf") == 0 ||
6380 				strcmp(argv[i], "-comp") == 0)
6381 			i++;
6382 
6383 	if(i != argc) {
6384 		if(++i == argc) {
6385 			ERROR("%s: -e missing arguments\n", argv[0]);
6386 			EXIT_MKSQUASHFS();
6387 		}
6388 		while(i < argc)
6389 			if(old_exclude)
6390 				old_add_exclude(argv[i++]);
6391 			else
6392 				add_exclude(argv[i++]);
6393 	}
6394 
6395 	/* process the sort files - must be done afer the exclude files  */
6396 	for(i = source + 2; i < argc; i++)
6397 		if(strcmp(argv[i], "-sort") == 0) {
6398 			int res = read_sort_file(argv[++i], source,
6399 								source_path);
6400 			if(res == FALSE)
6401 				BAD_ERROR("Failed to read sort file\n");
6402 			sorted ++;
6403 		} else if(strcmp(argv[i], "-e") == 0)
6404 			break;
6405 		else if(strcmp(argv[i], "-root-becomes") == 0 ||
6406 				strcmp(argv[i], "-ef") == 0 ||
6407 				strcmp(argv[i], "-pf") == 0 ||
6408 				strcmp(argv[i], "-af") == 0 ||
6409 				strcmp(argv[i], "-vaf") == 0 ||
6410 				strcmp(argv[i], "-comp") == 0)
6411 			i++;
6412 
6413 	if(!delete) {
6414 	        comp = read_super(fd, &sBlk, argv[source + 1]);
6415 	        if(comp == NULL) {
6416 			ERROR("Failed to read existing filesystem - will not "
6417 				"overwrite - ABORTING!\n");
6418 			ERROR("To force Mksquashfs to write to this block "
6419 				"device or file use -noappend\n");
6420 			EXIT_MKSQUASHFS();
6421 		}
6422 
6423 		block_log = slog(block_size = sBlk.block_size);
6424 		noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags);
6425 		noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags);
6426 		noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags);
6427 		noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags);
6428 		no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags);
6429 		always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags);
6430 		duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags);
6431 		exportable = SQUASHFS_EXPORTABLE(sBlk.flags);
6432 		no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags);
6433 		comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags);
6434 	}
6435 
6436 	initialise_threads(readq, fragq, bwriteq, fwriteq, delete,
6437 		destination_file);
6438 
6439 	res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0);
6440 	if(res)
6441 		BAD_ERROR("compressor_init failed\n");
6442 
6443 	if(delete) {
6444 		int size;
6445 		void *comp_data = compressor_dump_options(comp, block_size,
6446 			&size);
6447 
6448 		printf("Creating %d.%d filesystem on %s, block size %d.\n",
6449 			SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size);
6450 
6451 		/*
6452 		 * store any compressor specific options after the superblock,
6453 		 * and set the COMP_OPT flag to show that the filesystem has
6454 		 * compressor specfic options
6455 		 */
6456 		if(comp_data) {
6457 			unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT;
6458 
6459 			SQUASHFS_INSWAP_SHORTS(&c_byte, 1);
6460 			write_destination(fd, sizeof(struct squashfs_super_block),
6461 				sizeof(c_byte), &c_byte);
6462 			write_destination(fd, sizeof(struct squashfs_super_block) +
6463 				sizeof(c_byte), size, comp_data);
6464 			bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte)
6465 				+ size;
6466 			comp_opts = TRUE;
6467 		} else
6468 			bytes = sizeof(struct squashfs_super_block);
6469 	} else {
6470 		unsigned int last_directory_block, inode_dir_offset,
6471 			inode_dir_file_size, root_inode_size,
6472 			inode_dir_start_block, uncompressed_data,
6473 			compressed_data, inode_dir_inode_number,
6474 			inode_dir_parent_inode;
6475 		unsigned int root_inode_start =
6476 			SQUASHFS_INODE_BLK(sBlk.root_inode),
6477 			root_inode_offset =
6478 			SQUASHFS_INODE_OFFSET(sBlk.root_inode);
6479 
6480 		if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table,
6481 				&data_cache, &directory_table,
6482 				&directory_data_cache, &last_directory_block,
6483 				&inode_dir_offset, &inode_dir_file_size,
6484 				&root_inode_size, &inode_dir_start_block,
6485 				&file_count, &sym_count, &dev_count, &dir_count,
6486 				&fifo_count, &sock_count, &total_bytes,
6487 				&total_inode_bytes, &total_directory_bytes,
6488 				&inode_dir_inode_number,
6489 				&inode_dir_parent_inode, add_old_root_entry,
6490 				&fragment_table, &inode_lookup_table)) == 0) {
6491 			ERROR("Failed to read existing filesystem - will not "
6492 				"overwrite - ABORTING!\n");
6493 			ERROR("To force Mksquashfs to write to this block "
6494 				"device or file use -noappend\n");
6495 			EXIT_MKSQUASHFS();
6496 		}
6497 		if((append_fragments = fragments = sBlk.fragments)) {
6498 			fragment_table = realloc((char *) fragment_table,
6499 				((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1))
6500 				 * sizeof(struct squashfs_fragment_entry));
6501 			if(fragment_table == NULL)
6502 				BAD_ERROR("Out of memory in save filesystem state\n");
6503 		}
6504 
6505 		printf("Appending to existing %d.%d filesystem on %s, block "
6506 			"size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1],
6507 			block_size);
6508 		printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, "
6509 			"-always-use-fragments,\n-exportable and -comp options "
6510 			"ignored\n");
6511 		printf("\nIf appending is not wanted, please re-run with "
6512 			"-noappend specified!\n\n");
6513 
6514 		compressed_data = (inode_dir_offset + inode_dir_file_size) &
6515 			~(SQUASHFS_METADATA_SIZE - 1);
6516 		uncompressed_data = (inode_dir_offset + inode_dir_file_size) &
6517 			(SQUASHFS_METADATA_SIZE - 1);
6518 
6519 		/* save original filesystem state for restoring ... */
6520 		sfragments = fragments;
6521 		sbytes = bytes;
6522 		sinode_count = sBlk.inodes;
6523 		scache_bytes = root_inode_offset + root_inode_size;
6524 		sdirectory_cache_bytes = uncompressed_data;
6525 		sdata_cache = malloc(scache_bytes);
6526 		if(sdata_cache == NULL)
6527 			BAD_ERROR("Out of memory in save filesystem state\n");
6528 		sdirectory_data_cache = malloc(sdirectory_cache_bytes);
6529 		if(sdirectory_data_cache == NULL)
6530 			BAD_ERROR("Out of memory in save filesystem state\n");
6531 		memcpy(sdata_cache, data_cache, scache_bytes);
6532 		memcpy(sdirectory_data_cache, directory_data_cache +
6533 			compressed_data, sdirectory_cache_bytes);
6534 		sinode_bytes = root_inode_start;
6535 		stotal_bytes = total_bytes;
6536 		stotal_inode_bytes = total_inode_bytes;
6537 		stotal_directory_bytes = total_directory_bytes +
6538 			compressed_data;
6539 		sfile_count = file_count;
6540 		ssym_count = sym_count;
6541 		sdev_count = dev_count;
6542 		sdir_count = dir_count + 1;
6543 		sfifo_count = fifo_count;
6544 		ssock_count = sock_count;
6545 		sdup_files = dup_files;
6546 		sid_count = id_count;
6547 		write_recovery_data(&sBlk);
6548 		save_xattrs();
6549 		appending = TRUE;
6550 
6551 		/*
6552 		 * set the filesystem state up to be able to append to the
6553 		 * original filesystem.  The filesystem state differs depending
6554 		 * on whether we're appending to the original root directory, or
6555 		 * if the original root directory becomes a sub-directory
6556 		 * (root-becomes specified on command line, here root_name !=
6557 		 * NULL)
6558 		 */
6559 		inode_bytes = inode_size = root_inode_start;
6560 		directory_size = last_directory_block;
6561 		cache_size = root_inode_offset + root_inode_size;
6562 		directory_cache_size = inode_dir_offset + inode_dir_file_size;
6563 		if(root_name) {
6564 			sdirectory_bytes = last_directory_block;
6565 			sdirectory_compressed_bytes = 0;
6566 			root_inode_number = inode_dir_parent_inode;
6567 			inode_no = sBlk.inodes + 2;
6568 			directory_bytes = last_directory_block;
6569 			directory_cache_bytes = uncompressed_data;
6570 			memmove(directory_data_cache, directory_data_cache +
6571 				compressed_data, uncompressed_data);
6572 			cache_bytes = root_inode_offset + root_inode_size;
6573 			add_old_root_entry(root_name, sBlk.root_inode,
6574 				inode_dir_inode_number, SQUASHFS_DIR_TYPE);
6575 			total_directory_bytes += compressed_data;
6576 			dir_count ++;
6577 		} else {
6578 			sdirectory_compressed_bytes = last_directory_block -
6579 				inode_dir_start_block;
6580 			sdirectory_compressed =
6581 				malloc(sdirectory_compressed_bytes);
6582 			if(sdirectory_compressed == NULL)
6583 				BAD_ERROR("Out of memory in save filesystem "
6584 					"state\n");
6585 			memcpy(sdirectory_compressed, directory_table +
6586 				inode_dir_start_block,
6587 				sdirectory_compressed_bytes);
6588 			sdirectory_bytes = inode_dir_start_block;
6589 			root_inode_number = inode_dir_inode_number;
6590 			inode_no = sBlk.inodes + 1;
6591 			directory_bytes = inode_dir_start_block;
6592 			directory_cache_bytes = inode_dir_offset;
6593 			cache_bytes = root_inode_offset;
6594 		}
6595 
6596 		inode_count = file_count + dir_count + sym_count + dev_count +
6597 			fifo_count + sock_count;
6598 	}
6599 
6600 	if(path)
6601 		paths = add_subdir(paths, path);
6602 
6603 	dump_actions();
6604 	dump_pseudos();
6605 
6606 	if(delete && !keep_as_directory && source == 1 &&
6607 			S_ISDIR(source_buf.st_mode))
6608 		dir_scan(&inode, source_path[0], scan1_readdir, progress);
6609 	else if(!keep_as_directory && source == 1 &&
6610 			S_ISDIR(source_buf.st_mode))
6611 		dir_scan(&inode, source_path[0], scan1_single_readdir, progress);
6612 	else
6613 		dir_scan(&inode, "", scan1_encomp_readdir, progress);
6614 	sBlk.root_inode = inode;
6615 	sBlk.inodes = inode_count;
6616 	sBlk.s_magic = SQUASHFS_MAGIC;
6617 	sBlk.s_major = SQUASHFS_MAJOR;
6618 	sBlk.s_minor = SQUASHFS_MINOR;
6619 	sBlk.block_size = block_size;
6620 	sBlk.block_log = block_log;
6621 	sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments,
6622 		always_use_fragments, duplicate_checking, exportable,
6623 		no_xattrs, comp_opts);
6624 	sBlk.mkfs_time = time(NULL);
6625 
6626 	disable_info();
6627 
6628 	while((fragment = get_frag_action(fragment)))
6629 		write_fragment(*fragment);
6630 	unlock_fragments();
6631 	pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
6632 	pthread_mutex_lock(&fragment_mutex);
6633 	while(fragments_outstanding) {
6634 		pthread_mutex_unlock(&fragment_mutex);
6635 		sched_yield();
6636 		pthread_mutex_lock(&fragment_mutex);
6637 	}
6638 	pthread_cleanup_pop(1);
6639 
6640 	queue_put(to_writer, NULL);
6641 	if(queue_get(from_writer) != 0)
6642 		EXIT_MKSQUASHFS();
6643 
6644 	set_progressbar_state(FALSE);
6645 	write_filesystem_tables(&sBlk, nopad);
6646 
6647 /* ANDROID CHANGES START*/
6648 #ifdef ANDROID
6649 	if (block_map_file)
6650 		fclose(block_map_file);
6651 #endif
6652 /* ANDROID CHANGES END */
6653 
6654 	return 0;
6655 }
6656