blob: 6fe955568d0ca294d1fac99fe0459522efca615d [file] [log] [blame]
plougher1f413c82005-11-18 00:02:14 +00001/*
plougher16111452010-07-22 05:12:18 +00002 * Create a squashfs filesystem. This is a highly compressed read only
3 * filesystem.
plougher1f413c82005-11-18 00:02:14 +00004 *
Phillip Lougher43cc4282012-01-25 00:25:56 +00005 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
Colin Crossbe754902017-12-09 22:27:51 +00006 * 2012, 2013, 2014
Phillip Lougher83d42a32012-10-31 23:42:22 +00007 * Phillip Lougher <phillip@squashfs.org.uk>
plougher1f413c82005-11-18 00:02:14 +00008 *
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
plougher324978d2006-02-27 04:53:29 +000026#define FALSE 0
plougher1f413c82005-11-18 00:02:14 +000027#define TRUE 1
Phillip Lougherfe2c7352012-12-23 06:55:41 +000028#define MAX_LINE 16384
plougher8cb05cd2005-12-11 23:32:35 +000029
plougher1f413c82005-11-18 00:02:14 +000030#include <pwd.h>
31#include <grp.h>
32#include <time.h>
33#include <unistd.h>
34#include <stdio.h>
plougherac28cd12010-02-24 02:25:03 +000035#include <stddef.h>
plougher1f413c82005-11-18 00:02:14 +000036#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>
plougher1f413c82005-11-18 00:02:14 +000042#include <stdlib.h>
43#include <signal.h>
44#include <setjmp.h>
plougher02bc3bc2007-02-25 12:12:01 +000045#include <sys/types.h>
plougher1f413c82005-11-18 00:02:14 +000046#include <sys/mman.h>
plougher5507dd92006-11-06 00:43:10 +000047#include <pthread.h>
plougher8f8e1a12007-10-18 02:50:21 +000048#include <regex.h>
Colin Crossbe754902017-12-09 22:27:51 +000049#include <fnmatch.h>
plougherba674e82009-09-10 03:50:00 +000050#include <sys/wait.h>
Phillip Lougher94c1fe02012-11-28 03:32:36 +000051#include <limits.h>
Phillip Lougherb22336d2012-12-20 18:44:22 +000052#include <ctype.h>
plougher1f413c82005-11-18 00:02:14 +000053
Colin Crossbe754902017-12-09 22:27:51 +000054#ifndef FNM_EXTMATCH /* glibc extension */
55 #define FNM_EXTMATCH 0
56#endif
57
plougher68853292005-12-27 02:47:04 +000058#ifndef linux
plougher8cb05cd2005-12-11 23:32:35 +000059#define __BYTE_ORDER BYTE_ORDER
60#define __BIG_ENDIAN BIG_ENDIAN
61#define __LITTLE_ENDIAN LITTLE_ENDIAN
plougher5507dd92006-11-06 00:43:10 +000062#include <sys/sysctl.h>
plougher8cb05cd2005-12-11 23:32:35 +000063#else
64#include <endian.h>
plougher5507dd92006-11-06 00:43:10 +000065#include <sys/sysinfo.h>
plougher8cb05cd2005-12-11 23:32:35 +000066#endif
67
plougher860c1f32010-08-11 01:37:17 +000068#include "squashfs_fs.h"
plougher860c1f32010-08-11 01:37:17 +000069#include "squashfs_swap.h"
70#include "mksquashfs.h"
71#include "sort.h"
72#include "pseudo.h"
73#include "compressor.h"
74#include "xattr.h"
Phillip Lougher4bcb7f82011-08-28 03:18:26 +010075#include "action.h"
Phillip Lougher2477d0d2012-10-24 21:49:28 +010076#include "error.h"
Phillip Lougher9a0e8bb2013-03-10 02:39:24 +000077#include "progressbar.h"
Phillip Lougher2e9fab12013-04-09 03:26:33 +010078#include "info.h"
Phillip Lougher71f39642013-04-09 04:43:21 +010079#include "caches-queues-lists.h"
Phillip Lougher1bbd0cc2013-04-19 04:03:00 +010080#include "read_fs.h"
Phillip Lougher0d67c9d2013-04-19 05:06:50 +010081#include "restore.h"
Phillip Lougher9de84ac2014-02-20 05:18:48 +000082#include "process_fragments.h"
plougher860c1f32010-08-11 01:37:17 +000083
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -080084/* ANDROID CHANGES START*/
85#ifdef ANDROID
86#include "android.h"
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -070087#include "private/android_filesystem_config.h"
88#include "private/canned_fs_config.h"
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -080089int android_config = FALSE;
Mohamad Ayyashe0316292015-02-24 19:21:29 -080090char *context_file = NULL;
Mohamad Ayyash18fe5f62015-03-02 16:00:56 -080091char *mount_point = NULL;
Thierry Strudel1a710ff2015-07-09 16:34:38 -070092char *target_out_path = NULL;
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -070093fs_config_func_t fs_config_func = NULL;
Mohamad Ayyash4dfe4eb2016-06-03 18:49:44 -070094int compress_thresh_per = 0;
Mohamad Ayyash305fca62016-06-13 15:44:16 -070095int align_4k_blocks = TRUE;
96FILE *block_map_file = NULL;
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -080097#endif
98/* ANDROID CHANGES END */
99
plougher324978d2006-02-27 04:53:29 +0000100int delete = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000101int fd;
Phillip Loughera709bff2013-03-28 17:48:31 +0000102struct squashfs_super_block sBlk;
plougher1f413c82005-11-18 00:02:14 +0000103
104/* filesystem flags for building */
plougher3d7e5182010-12-25 01:49:42 +0000105int comp_opts = FALSE;
Phillip Lougher434d50c2014-01-21 03:05:36 +0000106int no_xattrs = XATTR_DEF;
107int noX = FALSE;
108int duplicate_checking = TRUE;
109int noF = FALSE;
110int no_fragments = FALSE;
111int always_use_fragments = FALSE;
112int noI = FALSE;
113int noD = FALSE;
plougher1f288f62009-02-21 03:05:52 +0000114int silent = TRUE;
plougher02bc3bc2007-02-25 12:12:01 +0000115int exportable = TRUE;
plougher8dcc6992007-08-17 19:32:52 +0000116int sparse_files = TRUE;
plougher8f8e1a12007-10-18 02:50:21 +0000117int old_exclude = TRUE;
118int use_regex = FALSE;
Phillip Loughera709bff2013-03-28 17:48:31 +0000119int nopad = FALSE;
Phillip Lougher85776df2014-01-31 03:10:46 +0000120int exit_on_error = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000121
Phillip Lougher434d50c2014-01-21 03:05:36 +0000122long long global_uid = -1, global_gid = -1;
123
plougher1f413c82005-11-18 00:02:14 +0000124/* superblock attributes */
125int block_size = SQUASHFS_FILE_SIZE, block_log;
plougher1b899fc2008-08-07 01:24:06 +0000126unsigned int id_count = 0;
plougherfd57dfe2009-03-30 01:17:52 +0000127int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0,
128 sock_count = 0;
plougher1f413c82005-11-18 00:02:14 +0000129
Mohan Srinivasan58e18d72016-07-26 15:06:29 -0700130/* ANDROID CHANGES START*/
131#ifdef ANDROID
132int whitelisted_count = 0;
133#endif
134/* ANDROID CHANGES END */
135
plougher1f413c82005-11-18 00:02:14 +0000136/* write position within data section */
137long long bytes = 0, total_bytes = 0;
138
139/* in memory directory table - possibly compressed */
140char *directory_table = NULL;
141unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0;
142
143/* cached directory table */
144char *directory_data_cache = NULL;
145unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
146
147/* in memory inode table - possibly compressed */
148char *inode_table = NULL;
149unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0;
150
151/* cached inode table */
152char *data_cache = NULL;
153unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
154
plougher0e453652006-11-06 01:49:35 +0000155/* inode lookup table */
156squashfs_inode *inode_lookup_table = NULL;
157
plougher1f413c82005-11-18 00:02:14 +0000158/* in memory directory data */
159#define I_COUNT_SIZE 128
160#define DIR_ENTRIES 32
161#define INODE_HASH_SIZE 65536
162#define INODE_HASH_MASK (INODE_HASH_SIZE - 1)
163#define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK)
164
165struct cached_dir_index {
plougher2bd2b722010-12-31 10:52:15 +0000166 struct squashfs_dir_index index;
167 char *name;
plougher1f413c82005-11-18 00:02:14 +0000168};
169
170struct directory {
171 unsigned int start_block;
172 unsigned int size;
173 unsigned char *buff;
174 unsigned char *p;
175 unsigned int entry_count;
176 unsigned char *entry_count_p;
177 unsigned int i_count;
178 unsigned int i_size;
179 struct cached_dir_index *index;
180 unsigned char *index_count_p;
181 unsigned int inode_number;
182};
183
plougher1f413c82005-11-18 00:02:14 +0000184struct inode_info *inode_info[INODE_HASH_SIZE];
185
186/* hash tables used to do fast duplicate searches in duplicate check */
plougher5507dd92006-11-06 00:43:10 +0000187struct file_info *dupl[65536];
plougher1f413c82005-11-18 00:02:14 +0000188int dup_files = 0;
189
plougher8f8e1a12007-10-18 02:50:21 +0000190/* exclude file handling */
plougher1f413c82005-11-18 00:02:14 +0000191/* list of exclude dirs/files */
192struct exclude_info {
193 dev_t st_dev;
194 ino_t st_ino;
195};
196
197#define EXCLUDE_SIZE 8192
198int exclude = 0;
199struct exclude_info *exclude_paths = NULL;
plougher8f8e1a12007-10-18 02:50:21 +0000200int old_excluded(char *filename, struct stat *buf);
201
202struct path_entry {
203 char *name;
204 regex_t *preg;
205 struct pathname *paths;
206};
207
208struct pathname {
209 int names;
210 struct path_entry *name;
211};
212
plougherf9039c92007-10-22 03:54:16 +0000213struct pathnames {
214 int count;
215 struct pathname *path[0];
216};
217#define PATHS_ALLOC_SIZE 10
218
219struct pathnames *paths = NULL;
220struct pathname *path = NULL;
plougher05e50ef2007-10-23 12:34:20 +0000221struct pathname *stickypath = NULL;
Phillip Lougherb2abb1c2013-01-09 04:51:21 +0000222int excluded(char *name, struct pathnames *paths, struct pathnames **new);
plougher1f413c82005-11-18 00:02:14 +0000223
plougher1f413c82005-11-18 00:02:14 +0000224int fragments = 0;
plougher76c64082008-03-08 01:32:23 +0000225
plougher1f413c82005-11-18 00:02:14 +0000226#define FRAG_SIZE 32768
plougher76c64082008-03-08 01:32:23 +0000227
plougher8ed84b92010-12-31 10:37:24 +0000228struct squashfs_fragment_entry *fragment_table = NULL;
plougher5507dd92006-11-06 00:43:10 +0000229int fragments_outstanding = 0;
plougher1f413c82005-11-18 00:02:14 +0000230
Phillip Lougher7ffdf2a2013-04-14 02:53:30 +0100231int fragments_locked = FALSE;
Phillip Lougher7ffdf2a2013-04-14 02:53:30 +0100232
plougher1f413c82005-11-18 00:02:14 +0000233/* current inode number for directories and non directories */
Phillip Lougher539c2b12012-07-30 20:14:52 +0100234unsigned int inode_no = 1;
plougherdf70c3e2006-01-27 09:34:13 +0000235unsigned int root_inode_number = 0;
plougher1f413c82005-11-18 00:02:14 +0000236
237/* list of source dirs/files */
238int source = 0;
239char **source_path;
240
241/* list of root directory entries read from original filesystem */
242int old_root_entries = 0;
243struct old_root_entry_info {
ploughera326c182009-08-29 05:41:45 +0000244 char *name;
245 struct inode_info inode;
plougher1f413c82005-11-18 00:02:14 +0000246};
247struct old_root_entry_info *old_root_entry;
248
plougherfd57dfe2009-03-30 01:17:52 +0000249/* restore orignal filesystem state if appending to existing filesystem is
250 * cancelled */
Phillip Lougher3794e342014-04-10 02:15:45 +0100251int appending = FALSE;
plougherca2c93f2008-08-15 08:34:57 +0000252char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed;
plougher1f413c82005-11-18 00:02:14 +0000253
254long long sbytes, stotal_bytes;
255
256unsigned int sinode_bytes, scache_bytes, sdirectory_bytes,
plougherca2c93f2008-08-15 08:34:57 +0000257 sdirectory_cache_bytes, sdirectory_compressed_bytes,
plougher1f413c82005-11-18 00:02:14 +0000258 stotal_inode_bytes, stotal_directory_bytes,
plougher0e453652006-11-06 01:49:35 +0000259 sinode_count = 0, sfile_count, ssym_count, sdev_count,
plougher1f413c82005-11-18 00:02:14 +0000260 sdir_count, sfifo_count, ssock_count, sdup_files;
261int sfragments;
plougher5507dd92006-11-06 00:43:10 +0000262int threads;
plougher1f413c82005-11-18 00:02:14 +0000263
264/* flag whether destination file is a block device */
Phillip Lougher434d50c2014-01-21 03:05:36 +0000265int block_device = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000266
267/* flag indicating whether files are sorted using sort list(s) */
Phillip Lougher434d50c2014-01-21 03:05:36 +0000268int sorted = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000269
plougher324978d2006-02-27 04:53:29 +0000270/* save destination file name for deleting on error */
271char *destination_file = NULL;
272
plougher99ac0cc2007-10-29 03:17:10 +0000273/* recovery file for abnormal exit on appending */
Phillip Lougheraf4c9232012-11-15 03:46:28 +0000274char *recovery_file = NULL;
plougher99ac0cc2007-10-29 03:17:10 +0000275int recover = TRUE;
276
Hidehiko Abef240b142018-05-08 22:45:47 +0900277/* uid/gid mapping tables */
278#define UGID_ENTRIES 340
279
280struct ugid_map_entry {
281 unsigned int child_id;
282 unsigned int parent_id;
283 unsigned int length;
284};
285struct ugid_map_entry uid_mapping[UGID_ENTRIES], gid_mapping[UGID_ENTRIES];
286unsigned int uid_map_count = 0, gid_map_count = 0;
287
288
plougher1b899fc2008-08-07 01:24:06 +0000289struct id *id_hash_table[ID_ENTRIES];
290struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS];
291unsigned int uid_count = 0, guid_count = 0;
292unsigned int sid_count = 0, suid_count = 0, sguid_count = 0;
plougher5507dd92006-11-06 00:43:10 +0000293
Phillip Lougher943acad2014-04-17 03:31:01 +0100294struct cache *reader_buffer, *fragment_buffer, *reserve_cache;
295struct cache *bwriter_buffer, *fwriter_buffer;
Phillip Loughercf478e92013-05-29 02:38:41 +0100296struct queue *to_reader, *to_deflate, *to_writer, *from_writer,
Phillip Lougher9de84ac2014-02-20 05:18:48 +0000297 *to_frag, *locked_fragment, *to_process_frag;
Phillip Lougher0e1656d2013-05-20 03:47:24 +0100298struct seq_queue *to_main;
Phillip Lougher0280d992014-01-27 05:54:07 +0000299pthread_t reader_thread, writer_thread, main_thread;
Phillip Lougher9de84ac2014-02-20 05:18:48 +0000300pthread_t *deflator_thread, *frag_deflator_thread, *frag_thread;
Phillip Loughercf135d32013-04-08 03:43:25 +0100301pthread_t *restore_thread = NULL;
Phillip Lougher94e658f2014-03-13 02:59:04 +0000302pthread_mutex_t fragment_mutex = PTHREAD_MUTEX_INITIALIZER;
Phillip Lougherea8cb652014-03-13 02:57:19 +0000303pthread_mutex_t pos_mutex = PTHREAD_MUTEX_INITIALIZER;
Phillip Lougher8bb17b02014-03-30 23:59:55 +0100304pthread_mutex_t dup_mutex = PTHREAD_MUTEX_INITIALIZER;
plougher5507dd92006-11-06 00:43:10 +0000305
306/* user options that control parallelisation */
307int processors = -1;
Phillip Lougherc3af83a2014-04-21 21:38:16 +0100308int bwriter_size;
plougher5507dd92006-11-06 00:43:10 +0000309
plougherc5d59872010-11-22 01:36:01 +0000310/* compression operations */
Phillip Lougher8bb17b02014-03-30 23:59:55 +0100311struct compressor *comp = NULL;
Phillip Lougher434d50c2014-01-21 03:05:36 +0000312int compressor_opt_parsed = FALSE;
plougher13fdddf2010-11-24 01:23:41 +0000313void *stream = NULL;
plougher7b8ee502009-07-29 07:54:30 +0000314
plougher860c1f32010-08-11 01:37:17 +0000315/* xattr stats */
316unsigned int xattr_bytes = 0, total_xattr_bytes = 0;
317
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +0000318/* fragment to file mapping used when appending */
319int append_fragments = 0;
320struct append_file **file_mapping;
321
Phillip Lougher22d67da2014-08-08 02:55:33 +0100322/* root of the in-core directory structure */
323struct dir_info *root_dir;
324
Phillip Lougher8bb17b02014-03-30 23:59:55 +0100325static char *read_from_disk(long long start, unsigned int avail_bytes);
plougherfd57dfe2009-03-30 01:17:52 +0000326void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
327 int type);
plougherfd57dfe2009-03-30 01:17:52 +0000328struct file_info *duplicate(long long file_size, long long bytes,
329 unsigned int **block_list, long long *start, struct fragment **fragment,
330 struct file_buffer *file_buffer, int blocks, unsigned short checksum,
Phillip Lougherc6424dc2014-03-13 02:01:28 +0000331 int checksum_flag);
Phillip Lougherb38c1722012-02-09 23:26:19 +0000332struct dir_info *dir_scan1(char *, char *, struct pathnames *,
Phillip Lougher494479f2012-02-03 15:45:41 +0000333 struct dir_ent *(_readdir)(struct dir_info *), int);
Phillip Lougher24eeb772012-10-13 01:56:24 +0100334void dir_scan2(struct dir_info *dir, struct pseudo *pseudo);
Phillip Lougher22d67da2014-08-08 02:55:33 +0100335void dir_scan3(struct dir_info *dir);
Phillip Lougher23d83622012-10-14 02:35:22 +0100336void dir_scan4(struct dir_info *dir);
Phillip Lougher2abfcf72012-11-11 03:59:56 +0000337void dir_scan5(struct dir_info *dir);
Phillip Lougher070c0f72014-07-27 01:03:43 +0100338void dir_scan6(struct dir_info *dir);
339void dir_scan7(squashfs_inode *inode, struct dir_info *dir_info);
plougherfd57dfe2009-03-30 01:17:52 +0000340struct file_info *add_non_dup(long long file_size, long long bytes,
341 unsigned int *block_list, long long start, struct fragment *fragment,
342 unsigned short checksum, unsigned short fragment_checksum,
Phillip Lougherc6424dc2014-03-13 02:01:28 +0000343 int checksum_flag, int checksum_frag_flag);
ploughera0a49c32010-08-11 01:47:59 +0000344long long generic_write_table(int, void *, int, void *, int);
plougherca61d1c2010-07-20 18:32:32 +0000345void restorefs();
Phillip Lougher1eae83d2012-09-26 02:12:45 +0100346struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth);
Phillip Loughera709bff2013-03-28 17:48:31 +0000347void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad);
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +0000348unsigned short get_checksum_mem(char *buff, int bytes);
Phillip Lougher285a2fd2014-06-10 21:51:52 +0100349void check_usable_phys_mem(int total_mem);
plougher5507dd92006-11-06 00:43:10 +0000350
Mohan Srinivasan58e18d72016-07-26 15:06:29 -0700351/* ANDROID CHANGES START*/
352#ifdef ANDROID
353static int whitelisted(struct stat *buf);
354static void add_whitelist_entry(char *filename, struct stat *buf);
355static int add_whitelist(char *path);
356static void process_whitelist_file(char *argv);
357
358#define WHITELIST_SIZE 8192
359int whitelist = 0;
360
361struct whitelist_info {
362 dev_t st_dev;
363 ino_t st_ino;
364};
365char *whitelist_filename = NULL;
366struct whitelist_info *whitelist_paths = NULL;
367#endif
368/* ANDROID CHANGES END */
plougher5507dd92006-11-06 00:43:10 +0000369
Phillip Lougher9b7c3c22013-03-10 02:29:42 +0000370void prep_exit()
Phillip Lougher838d2962012-10-23 03:29:30 +0100371{
Phillip Loughercf135d32013-04-08 03:43:25 +0100372 if(restore_thread) {
373 if(pthread_self() == *restore_thread) {
374 /*
375 * Recursive failure when trying to restore filesystem!
376 * Nothing to do except to exit, otherwise we'll just
377 * appear to hang. The user should be able to restore
378 * from the recovery file (which is why it was added, in
379 * case of catastrophic failure in Mksquashfs)
380 */
381 exit(1);
382 } else {
383 /* signal the restore thread to restore */
Phillip Lougher8c740812014-01-30 01:56:17 +0000384 pthread_kill(*restore_thread, SIGUSR1);
Phillip Loughercf135d32013-04-08 03:43:25 +0100385 pthread_exit(NULL);
386 }
Phillip Lougher3794e342014-04-10 02:15:45 +0100387 } else if(delete) {
388 if(destination_file && !block_device)
389 unlink(destination_file);
390 } else if(recovery_file)
391 unlink(recovery_file);
Phillip Lougher838d2962012-10-23 03:29:30 +0100392}
393
394
Phillip Loughere1668fe2012-12-30 05:48:12 +0000395int add_overflow(int a, int b)
396{
397 return (INT_MAX - a) < b;
398}
399
400
401int shift_overflow(int a, int shift)
402{
403 return (INT_MAX >> shift) < a;
404}
405
406
407int multiply_overflow(int a, int multiplier)
408{
409 return (INT_MAX / multiplier) < a;
410}
411
412
Phillip Lougher57e2f692014-05-05 23:50:24 +0100413int multiply_overflowll(long long a, int multiplier)
414{
415 return (LLONG_MAX / multiplier) < a;
416}
417
418
plougher50b31762009-03-31 04:14:46 +0000419#define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \
420 + (((char *)A) - data_cache)))
plougher1f413c82005-11-18 00:02:14 +0000421
422
423void restorefs()
424{
425 ERROR("Exiting - restoring original filesystem!\n\n");
plougher5507dd92006-11-06 00:43:10 +0000426
plougher1f413c82005-11-18 00:02:14 +0000427 bytes = sbytes;
428 memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes);
plougherfd57dfe2009-03-30 01:17:52 +0000429 memcpy(directory_data_cache, sdirectory_data_cache,
430 sdirectory_cache_bytes);
431 directory_cache_bytes = sdirectory_cache_bytes;
plougher1f413c82005-11-18 00:02:14 +0000432 inode_bytes = sinode_bytes;
433 directory_bytes = sdirectory_bytes;
plougherfd57dfe2009-03-30 01:17:52 +0000434 memcpy(directory_table + directory_bytes, sdirectory_compressed,
435 sdirectory_compressed_bytes);
plougherca2c93f2008-08-15 08:34:57 +0000436 directory_bytes += sdirectory_compressed_bytes;
plougher1f413c82005-11-18 00:02:14 +0000437 total_bytes = stotal_bytes;
438 total_inode_bytes = stotal_inode_bytes;
439 total_directory_bytes = stotal_directory_bytes;
440 inode_count = sinode_count;
441 file_count = sfile_count;
442 sym_count = ssym_count;
443 dev_count = sdev_count;
444 dir_count = sdir_count;
445 fifo_count = sfifo_count;
446 sock_count = ssock_count;
447 dup_files = sdup_files;
448 fragments = sfragments;
plougher1b899fc2008-08-07 01:24:06 +0000449 id_count = sid_count;
plougher21f63b32010-07-18 03:59:04 +0000450 restore_xattrs();
Phillip Loughera709bff2013-03-28 17:48:31 +0000451 write_filesystem_tables(&sBlk, nopad);
452 exit(1);
plougher1f413c82005-11-18 00:02:14 +0000453}
454
455
Phillip Lougherb8ec5202013-03-28 20:14:37 +0000456void sighandler()
plougher324978d2006-02-27 04:53:29 +0000457{
458 EXIT_MKSQUASHFS();
459}
460
461
plougher13fdddf2010-11-24 01:23:41 +0000462int mangle2(void *strm, char *d, char *s, int size,
plougher50b31762009-03-31 04:14:46 +0000463 int block_size, int uncompressed, int data_block)
plougher5507dd92006-11-06 00:43:10 +0000464{
plougher7b8ee502009-07-29 07:54:30 +0000465 int error, c_byte = 0;
plougher5507dd92006-11-06 00:43:10 +0000466
plougher7b8ee502009-07-29 07:54:30 +0000467 if(!uncompressed) {
plougherbfb876c2010-12-31 07:48:01 +0000468 c_byte = compressor_compress(comp, strm, d, s, size, block_size,
469 &error);
plougher7b8ee502009-07-29 07:54:30 +0000470 if(c_byte == -1)
471 BAD_ERROR("mangle2:: %s compress failed with error "
472 "code %d\n", comp->name, error);
plougher1f413c82005-11-18 00:02:14 +0000473 }
474
Colin Crossbe754902017-12-09 22:27:51 +0000475 if(c_byte == 0 || c_byte >= size ||
476 (c_byte > (size * ((100.0 - compress_thresh_per) / 100.0)))) {
plougher1f413c82005-11-18 00:02:14 +0000477 memcpy(d, s, size);
plougherfd57dfe2009-03-30 01:17:52 +0000478 return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK :
479 SQUASHFS_COMPRESSED_BIT);
plougher1f413c82005-11-18 00:02:14 +0000480 }
481
plougher7b8ee502009-07-29 07:54:30 +0000482 return c_byte;
plougher1f413c82005-11-18 00:02:14 +0000483}
484
485
plougher7b8ee502009-07-29 07:54:30 +0000486int mangle(char *d, char *s, int size, int block_size,
plougher50b31762009-03-31 04:14:46 +0000487 int uncompressed, int data_block)
plougher5507dd92006-11-06 00:43:10 +0000488{
plougher13fdddf2010-11-24 01:23:41 +0000489 return mangle2(stream, d, s, size, block_size, uncompressed,
plougher50b31762009-03-31 04:14:46 +0000490 data_block);
plougher5507dd92006-11-06 00:43:10 +0000491}
492
493
plougherac28cd12010-02-24 02:25:03 +0000494void *get_inode(int req_size)
plougher1f413c82005-11-18 00:02:14 +0000495{
496 int data_space;
497 unsigned short c_byte;
498
499 while(cache_bytes >= SQUASHFS_METADATA_SIZE) {
plougherfd57dfe2009-03-30 01:17:52 +0000500 if((inode_size - inode_bytes) <
501 ((SQUASHFS_METADATA_SIZE << 1)) + 2) {
plougher1eb2a662010-07-26 17:30:50 +0000502 void *it = realloc(inode_table, inode_size +
plougherfd57dfe2009-03-30 01:17:52 +0000503 (SQUASHFS_METADATA_SIZE << 1) + 2);
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000504 if(it == NULL)
505 MEM_ERROR();
plougher1eb2a662010-07-26 17:30:50 +0000506 inode_table = it;
plougher1f413c82005-11-18 00:02:14 +0000507 inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
508 }
509
plougherfd57dfe2009-03-30 01:17:52 +0000510 c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET,
plougher50b31762009-03-31 04:14:46 +0000511 data_cache, SQUASHFS_METADATA_SIZE,
512 SQUASHFS_METADATA_SIZE, noI, 0);
rlougher8f7d0b82007-11-08 15:33:29 +0000513 TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte);
plougherac28cd12010-02-24 02:25:03 +0000514 SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1);
plougher1b899fc2008-08-07 01:24:06 +0000515 inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET;
516 total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET;
plougher14dd5f32010-08-02 18:20:03 +0000517 memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE,
plougherfd57dfe2009-03-30 01:17:52 +0000518 cache_bytes - SQUASHFS_METADATA_SIZE);
plougher1f413c82005-11-18 00:02:14 +0000519 cache_bytes -= SQUASHFS_METADATA_SIZE;
520 }
521
522 data_space = (cache_size - cache_bytes);
523 if(data_space < req_size) {
plougherfd57dfe2009-03-30 01:17:52 +0000524 int realloc_size = cache_size == 0 ?
525 ((req_size + SQUASHFS_METADATA_SIZE) &
526 ~(SQUASHFS_METADATA_SIZE - 1)) : req_size -
527 data_space;
plougher1f413c82005-11-18 00:02:14 +0000528
plougher17248ca2010-07-27 00:24:35 +0000529 void *dc = realloc(data_cache, cache_size +
plougherfd57dfe2009-03-30 01:17:52 +0000530 realloc_size);
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000531 if(dc == NULL)
532 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +0000533 cache_size += realloc_size;
plougher17248ca2010-07-27 00:24:35 +0000534 data_cache = dc;
plougher1f413c82005-11-18 00:02:14 +0000535 }
536
537 cache_bytes += req_size;
538
plougherac28cd12010-02-24 02:25:03 +0000539 return data_cache + cache_bytes - req_size;
plougher1f413c82005-11-18 00:02:14 +0000540}
541
542
plougher8a8c4102009-03-29 22:28:49 +0000543int read_bytes(int fd, void *buff, int bytes)
plougher06a19d32009-03-29 22:00:03 +0000544{
545 int res, count;
546
547 for(count = 0; count < bytes; count += res) {
548 res = read(fd, buff + count, bytes - count);
549 if(res < 1) {
plougher96f85a12009-03-29 22:39:59 +0000550 if(res == 0)
551 goto bytes_read;
552 else if(errno != EINTR) {
plougher06a19d32009-03-29 22:00:03 +0000553 ERROR("Read failed because %s\n",
554 strerror(errno));
555 return -1;
556 } else
557 res = 0;
558 }
559 }
560
561bytes_read:
562 return count;
563}
564
565
plougher3306cb22010-06-18 04:22:44 +0000566int read_fs_bytes(int fd, long long byte, int bytes, void *buff)
plougher1f413c82005-11-18 00:02:14 +0000567{
568 off_t off = byte;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100569 int res = 1;
plougher1f413c82005-11-18 00:02:14 +0000570
plougher1d065e92010-06-18 03:58:27 +0000571 TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n",
plougherfd57dfe2009-03-30 01:17:52 +0000572 byte, bytes);
plougher06a19d32009-03-29 22:00:03 +0000573
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100574 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
plougher5507dd92006-11-06 00:43:10 +0000575 pthread_mutex_lock(&pos_mutex);
Colin Crossbe754902017-12-09 22:27:51 +0000576 if(lseek(fd, off, SEEK_SET) == -1) {
Phillip Lougher1d3177a2013-02-06 21:48:58 +0000577 ERROR("read_fs_bytes: Lseek on destination failed because %s, "
Colin Crossbe754902017-12-09 22:27:51 +0000578 "offset=0x%llx\n", strerror(errno), off);
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100579 res = 0;
Phillip Lougher041cf6d2013-07-20 04:20:40 +0100580 } else if(read_bytes(fd, buff, bytes) < bytes) {
plougher1d065e92010-06-18 03:58:27 +0000581 ERROR("Read on destination failed\n");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100582 res = 0;
plougher1d065e92010-06-18 03:58:27 +0000583 }
584
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100585 pthread_cleanup_pop(1);
586 return res;
plougher1f413c82005-11-18 00:02:14 +0000587}
588
589
plougher628e7682009-03-29 22:12:24 +0000590int write_bytes(int fd, void *buff, int bytes)
plougher0dd6f122009-03-29 21:43:57 +0000591{
592 int res, count;
593
594 for(count = 0; count < bytes; count += res) {
595 res = write(fd, buff + count, bytes - count);
596 if(res == -1) {
597 if(errno != EINTR) {
598 ERROR("Write failed because %s\n",
599 strerror(errno));
600 return -1;
601 }
602 res = 0;
603 }
604 }
605
606 return 0;
607}
608
609
plougher29e2ace2010-12-31 08:50:00 +0000610void write_destination(int fd, long long byte, int bytes, void *buff)
plougher1f413c82005-11-18 00:02:14 +0000611{
612 off_t off = byte;
plougher1f413c82005-11-18 00:02:14 +0000613
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100614 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
615 pthread_mutex_lock(&pos_mutex);
plougher5507dd92006-11-06 00:43:10 +0000616
Colin Crossbe754902017-12-09 22:27:51 +0000617 if(lseek(fd, off, SEEK_SET) == -1) {
Phillip Loughera0c23462013-02-12 03:30:12 +0000618 ERROR("write_destination: Lseek on destination "
Phillip Lougher1d3177a2013-02-06 21:48:58 +0000619 "failed because %s, offset=0x%llx\n", strerror(errno),
Colin Crossbe754902017-12-09 22:27:51 +0000620 off);
Phillip Loughera0c23462013-02-12 03:30:12 +0000621 BAD_ERROR("Probably out of space on output %s\n",
622 block_device ? "block device" : "filesystem");
623 }
plougher1f413c82005-11-18 00:02:14 +0000624
plougher0dd6f122009-03-29 21:43:57 +0000625 if(write_bytes(fd, buff, bytes) == -1)
Phillip Loughere8b536f2013-02-12 22:01:21 +0000626 BAD_ERROR("Failed to write to output %s\n",
627 block_device ? "block device" : "filesystem");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100628
629 pthread_cleanup_pop(1);
plougher1f413c82005-11-18 00:02:14 +0000630}
631
632
633long long write_inodes()
634{
635 unsigned short c_byte;
636 int avail_bytes;
637 char *datap = data_cache;
638 long long start_bytes = bytes;
639
640 while(cache_bytes) {
plougherfd57dfe2009-03-30 01:17:52 +0000641 if(inode_size - inode_bytes <
642 ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
plougher1eb2a662010-07-26 17:30:50 +0000643 void *it = realloc(inode_table, inode_size +
plougherfd57dfe2009-03-30 01:17:52 +0000644 ((SQUASHFS_METADATA_SIZE << 1) + 2));
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000645 if(it == NULL)
646 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +0000647 inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
plougher1eb2a662010-07-26 17:30:50 +0000648 inode_table = it;
plougher1f413c82005-11-18 00:02:14 +0000649 }
plougherfd57dfe2009-03-30 01:17:52 +0000650 avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ?
651 SQUASHFS_METADATA_SIZE : cache_bytes;
652 c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap,
653 avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0);
rlougher8f7d0b82007-11-08 15:33:29 +0000654 TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte);
plougherac28cd12010-02-24 02:25:03 +0000655 SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1);
plougher1b899fc2008-08-07 01:24:06 +0000656 inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET;
657 total_inode_bytes += avail_bytes + BLOCK_OFFSET;
plougher1f413c82005-11-18 00:02:14 +0000658 datap += avail_bytes;
659 cache_bytes -= avail_bytes;
660 }
661
plougher29e2ace2010-12-31 08:50:00 +0000662 write_destination(fd, bytes, inode_bytes, inode_table);
plougher1f413c82005-11-18 00:02:14 +0000663 bytes += inode_bytes;
664
665 return start_bytes;
666}
667
668
669long long write_directories()
670{
671 unsigned short c_byte;
672 int avail_bytes;
673 char *directoryp = directory_data_cache;
674 long long start_bytes = bytes;
675
676 while(directory_cache_bytes) {
plougherfd57dfe2009-03-30 01:17:52 +0000677 if(directory_size - directory_bytes <
678 ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
plougher79d665a2010-07-27 00:19:23 +0000679 void *dt = realloc(directory_table,
plougherfd57dfe2009-03-30 01:17:52 +0000680 directory_size + ((SQUASHFS_METADATA_SIZE << 1)
681 + 2));
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000682 if(dt == NULL)
683 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +0000684 directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
plougher79d665a2010-07-27 00:19:23 +0000685 directory_table = dt;
plougher1f413c82005-11-18 00:02:14 +0000686 }
plougherfd57dfe2009-03-30 01:17:52 +0000687 avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ?
688 SQUASHFS_METADATA_SIZE : directory_cache_bytes;
plougher50b31762009-03-31 04:14:46 +0000689 c_byte = mangle(directory_table + directory_bytes +
690 BLOCK_OFFSET, directoryp, avail_bytes,
691 SQUASHFS_METADATA_SIZE, noI, 0);
plougherfd57dfe2009-03-30 01:17:52 +0000692 TRACE("Directory block @ 0x%x, size %d\n", directory_bytes,
693 c_byte);
plougherac28cd12010-02-24 02:25:03 +0000694 SQUASHFS_SWAP_SHORTS(&c_byte,
695 directory_table + directory_bytes, 1);
plougher50b31762009-03-31 04:14:46 +0000696 directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) +
697 BLOCK_OFFSET;
plougher1b899fc2008-08-07 01:24:06 +0000698 total_directory_bytes += avail_bytes + BLOCK_OFFSET;
plougher1f413c82005-11-18 00:02:14 +0000699 directoryp += avail_bytes;
700 directory_cache_bytes -= avail_bytes;
701 }
plougher29e2ace2010-12-31 08:50:00 +0000702 write_destination(fd, bytes, directory_bytes, directory_table);
plougher1f413c82005-11-18 00:02:14 +0000703 bytes += directory_bytes;
704
705 return start_bytes;
706}
707
708
plougher1b899fc2008-08-07 01:24:06 +0000709long long write_id_table()
plougher1f413c82005-11-18 00:02:14 +0000710{
plougher1b899fc2008-08-07 01:24:06 +0000711 unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count);
plougherac28cd12010-02-24 02:25:03 +0000712 unsigned int p[id_count];
plougher1f413c82005-11-18 00:02:14 +0000713 int i;
714
plougher1b899fc2008-08-07 01:24:06 +0000715 TRACE("write_id_table: ids %d, id_bytes %d\n", id_count, id_bytes);
plougherac28cd12010-02-24 02:25:03 +0000716 for(i = 0; i < id_count; i++) {
plougher1b899fc2008-08-07 01:24:06 +0000717 TRACE("write_id_table: id index %d, id %d", i, id_table[i]->id);
plougherac28cd12010-02-24 02:25:03 +0000718 SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1);
plougher1f413c82005-11-18 00:02:14 +0000719 }
720
ploughera0a49c32010-08-11 01:47:59 +0000721 return generic_write_table(id_bytes, p, 0, NULL, noI);
plougher1f413c82005-11-18 00:02:14 +0000722}
723
724
plougher1b899fc2008-08-07 01:24:06 +0000725struct id *get_id(unsigned int id)
plougher1f413c82005-11-18 00:02:14 +0000726{
plougher1b899fc2008-08-07 01:24:06 +0000727 int hash = ID_HASH(id);
728 struct id *entry = id_hash_table[hash];
plougher1f413c82005-11-18 00:02:14 +0000729
plougher1b899fc2008-08-07 01:24:06 +0000730 for(; entry; entry = entry->next)
731 if(entry->id == id)
732 break;
plougher1f413c82005-11-18 00:02:14 +0000733
plougher1b899fc2008-08-07 01:24:06 +0000734 return entry;
plougher1f413c82005-11-18 00:02:14 +0000735}
736
737
plougher1b899fc2008-08-07 01:24:06 +0000738struct id *create_id(unsigned int id)
739{
740 int hash = ID_HASH(id);
741 struct id *entry = malloc(sizeof(struct id));
742 if(entry == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000743 MEM_ERROR();
plougher1b899fc2008-08-07 01:24:06 +0000744 entry->id = id;
745 entry->index = id_count ++;
746 entry->flags = 0;
747 entry->next = id_hash_table[hash];
748 id_hash_table[hash] = entry;
749 id_table[entry->index] = entry;
750 return entry;
751}
752
753
Hidehiko Abef240b142018-05-08 22:45:47 +0900754int resolve_child_ugid(unsigned int *ugid,
755 const struct ugid_map_entry *ugid_mapping,
756 unsigned int ugid_map_count)
plougher1b899fc2008-08-07 01:24:06 +0000757{
Hidehiko Abef240b142018-05-08 22:45:47 +0900758 unsigned int i;
759
760 for (i = 0; i < ugid_map_count; i++) {
761 if (ugid_mapping[i].parent_id <= *ugid &&
762 *ugid <
763 ugid_mapping[i].parent_id + ugid_mapping[i].length) {
764 *ugid = ugid_mapping[i].child_id + *ugid -
765 ugid_mapping[i].parent_id;
766 return 1;
767 }
768 }
769
770 return 0;
771}
772
773
774unsigned int get_uid(unsigned int uid, int resolve)
775{
776 struct id *entry;
777
778 if (resolve && !resolve_child_ugid(&uid, uid_mapping, uid_map_count))
779 BAD_ERROR("uid not found in mapping: %d\n", uid);
780 entry = get_id(uid);
plougher1b899fc2008-08-07 01:24:06 +0000781
782 if(entry == NULL) {
783 if(id_count == SQUASHFS_IDS)
784 BAD_ERROR("Out of uids!\n");
785 entry = create_id(uid);
786 }
787
788 if((entry->flags & ISA_UID) == 0) {
789 entry->flags |= ISA_UID;
790 uid_count ++;
791 }
792
793 return entry->index;
794}
795
796
Hidehiko Abef240b142018-05-08 22:45:47 +0900797unsigned int get_guid(unsigned int guid, int resolve)
plougher1b899fc2008-08-07 01:24:06 +0000798{
Hidehiko Abef240b142018-05-08 22:45:47 +0900799 struct id *entry;
800
801 if (resolve && !resolve_child_ugid(&guid, gid_mapping, gid_map_count))
802 BAD_ERROR("gid not found in mapping: %d\n", guid);
803 entry = get_id(guid);
plougher1b899fc2008-08-07 01:24:06 +0000804
805 if(entry == NULL) {
806 if(id_count == SQUASHFS_IDS)
807 BAD_ERROR("Out of gids!\n");
808 entry = create_id(guid);
809 }
810
811 if((entry->flags & ISA_GID) == 0) {
812 entry->flags |= ISA_GID;
813 guid_count ++;
814 }
815
816 return entry->index;
817}
818
819
Phillip Lougher5ef51692012-11-19 03:35:39 +0000820#define ALLOC_SIZE 128
Phillip Lougher494479f2012-02-03 15:45:41 +0000821
Phillip Lougher5ef51692012-11-19 03:35:39 +0000822char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size)
823{
824 if(pathname == NULL) {
825 pathname = malloc(ALLOC_SIZE);
826 if(pathname == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000827 MEM_ERROR();
Phillip Lougher5ef51692012-11-19 03:35:39 +0000828 }
829
830 for(;;) {
831 int res = snprintf(pathname, *size, "%s/%s",
832 dir_ent->our_dir->pathname,
833 dir_ent->source_name ? : dir_ent->name);
834
835 if(res < 0)
836 BAD_ERROR("snprintf failed in pathname\n");
837 else if(res >= *size) {
838 /*
839 * pathname is too small to contain the result, so
840 * increase it and try again
841 */
842 *size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1);
843 pathname = realloc(pathname, *size);
844 if(pathname == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000845 MEM_ERROR();
Phillip Lougher5ef51692012-11-19 03:35:39 +0000846 } else
847 break;
848 }
Phillip Lougher494479f2012-02-03 15:45:41 +0000849
850 return pathname;
851}
852
853
854char *pathname(struct dir_ent *dir_ent)
855{
Phillip Lougher5ef51692012-11-19 03:35:39 +0000856 static char *pathname = NULL;
857 static int size = ALLOC_SIZE;
Phillip Lougher494479f2012-02-03 15:45:41 +0000858
Phillip Lougher5ef51692012-11-19 03:35:39 +0000859 if (dir_ent->nonstandard_pathname)
860 return dir_ent->nonstandard_pathname;
861
862 return pathname = _pathname(dir_ent, pathname, &size);
Phillip Lougher494479f2012-02-03 15:45:41 +0000863}
864
865
866char *pathname_reader(struct dir_ent *dir_ent)
867{
Phillip Lougher5ef51692012-11-19 03:35:39 +0000868 static char *pathname = NULL;
869 static int size = ALLOC_SIZE;
Phillip Lougher494479f2012-02-03 15:45:41 +0000870
Phillip Lougher5ef51692012-11-19 03:35:39 +0000871 if (dir_ent->nonstandard_pathname)
872 return dir_ent->nonstandard_pathname;
873
874 return pathname = _pathname(dir_ent, pathname, &size);
Phillip Lougher494479f2012-02-03 15:45:41 +0000875}
876
877
Phillip Lougherb38c1722012-02-09 23:26:19 +0000878char *subpathname(struct dir_ent *dir_ent)
879{
Phillip Lougherd457ed32012-11-19 01:36:56 +0000880 static char *subpath = NULL;
881 static int size = ALLOC_SIZE;
882 int res;
Phillip Lougherb38c1722012-02-09 23:26:19 +0000883
Phillip Lougherd457ed32012-11-19 01:36:56 +0000884 if(subpath == NULL) {
885 subpath = malloc(ALLOC_SIZE);
886 if(subpath == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000887 MEM_ERROR();
Phillip Lougherd457ed32012-11-19 01:36:56 +0000888 }
889
890 for(;;) {
891 if(dir_ent->our_dir->subpath[0] != '\0')
892 res = snprintf(subpath, size, "%s/%s",
893 dir_ent->our_dir->subpath, dir_ent->name);
894 else
895 res = snprintf(subpath, size, "/%s", dir_ent->name);
896
897 if(res < 0)
898 BAD_ERROR("snprintf failed in subpathname\n");
899 else if(res >= size) {
900 /*
901 * subpath is too small to contain the result, so
902 * increase it and try again
903 */
904 size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1);
905 subpath = realloc(subpath, size);
906 if(subpath == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000907 MEM_ERROR();
Phillip Lougherd457ed32012-11-19 01:36:56 +0000908 } else
909 break;
Phillip Lougher90e08252012-10-05 04:33:35 +0100910 }
Phillip Lougherb38c1722012-02-09 23:26:19 +0000911
912 return subpath;
913}
914
915
Colin Crossbe754902017-12-09 22:27:51 +0000916static inline unsigned int get_inode_no(struct inode_info *inode)
Phillip Lougher53cef742012-07-25 05:12:50 +0100917{
Phillip Lougher539c2b12012-07-30 20:14:52 +0100918 return inode->inode_number;
Phillip Lougher53cef742012-07-25 05:12:50 +0100919}
920
921
Colin Crossbe754902017-12-09 22:27:51 +0000922static inline unsigned int get_parent_no(struct dir_info *dir)
Phillip Lougher53cef742012-07-25 05:12:50 +0100923{
Phillip Lougher1eae83d2012-09-26 02:12:45 +0100924 return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no;
Phillip Lougher53cef742012-07-25 05:12:50 +0100925}
926
Mohamad Ayyash305fca62016-06-13 15:44:16 -0700927
928/* ANDROID CHANGES START*/
929#ifdef ANDROID
Alex Deymo4445cd12016-12-08 15:19:49 -0800930
931/* Round up the passed |n| value to the smallest multiple of 4096 greater or
932 * equal than |n| and return the 4K-block number for that value. */
933static unsigned long long round_up_block(unsigned long long n) {
934 const unsigned long long kMapBlockSize = 4096;
935 return (n + kMapBlockSize - 1) / kMapBlockSize;
936}
937
Mohamad Ayyash305fca62016-06-13 15:44:16 -0700938static inline void write_block_map_entry(char *sub_path, unsigned long long start_block, unsigned long long total_size,
939 char * mount_point, FILE *block_map_file) {
940 if (block_map_file) {
Alex Deymo4445cd12016-12-08 15:19:49 -0800941 /* We assign each 4K block based on what file the first byte of the block
942 * belongs to. The current file consists of the chunk of bytes in the
943 * interval [start_block, start_block + total_size), (closed on the left end
944 * and open on the right end). We then compute the first block whose first
945 * byte is equal to or greater than start_block as |round_start| and then
946 * the first block whose first byte is *past* this interval, as
947 * |round_end + 1|. This means that the blocks that should be assigned to
948 * the current file are in the interval [round_start, round_end + 1), or
949 * simply [round_start, round_end].
950 */
951 unsigned long long round_start = round_up_block(start_block);
952 unsigned long long round_end = round_up_block(start_block + total_size) - 1;
Mohamad Ayyash305fca62016-06-13 15:44:16 -0700953 if (round_start && total_size && round_start <= round_end) {
954 fprintf(block_map_file, "/%s", mount_point);
955 if (sub_path[0] != '/') fprintf(block_map_file, "/");
956 if (round_start == round_end)
957 fprintf(block_map_file, "%s %lld\n", sub_path, round_start);
958 else
959 fprintf(block_map_file, "%s %lld-%lld\n", sub_path, round_start, round_end);
960 }
961 }
962}
963#endif
964/* ANDROID CHANGES END */
Phillip Lougher53cef742012-07-25 05:12:50 +0100965
plougher3c6bdb52010-05-01 02:30:59 +0000966int create_inode(squashfs_inode *i_no, struct dir_info *dir_info,
967 struct dir_ent *dir_ent, int type, long long byte_size,
968 long long start_block, unsigned int offset, unsigned int *block_list,
969 struct fragment *fragment, struct directory *dir_in, long long sparse)
plougher1f413c82005-11-18 00:02:14 +0000970{
971 struct stat *buf = &dir_ent->inode->buf;
plougher8973a122010-12-31 09:52:45 +0000972 union squashfs_inode_header inode_header;
plougher857d0dd2010-12-31 21:03:13 +0000973 struct squashfs_base_inode_header *base = &inode_header.base;
plougherac28cd12010-02-24 02:25:03 +0000974 void *inode;
Phillip Lougher494479f2012-02-03 15:45:41 +0000975 char *filename = pathname(dir_ent);
plougher1f413c82005-11-18 00:02:14 +0000976 int nlink = dir_ent->inode->nlink;
ploughere6e0e1b2010-05-12 17:17:06 +0000977 int xattr = read_xattrs(dir_ent);
plougher1f413c82005-11-18 00:02:14 +0000978
ploughere6e0e1b2010-05-12 17:17:06 +0000979 switch(type) {
980 case SQUASHFS_FILE_TYPE:
981 if(dir_ent->inode->nlink > 1 ||
982 byte_size >= (1LL << 32) ||
983 start_block >= (1LL << 32) ||
984 sparse || IS_XATTR(xattr))
985 type = SQUASHFS_LREG_TYPE;
986 break;
987 case SQUASHFS_DIR_TYPE:
988 if(dir_info->dir_is_ldir || IS_XATTR(xattr))
989 type = SQUASHFS_LDIR_TYPE;
990 break;
991 case SQUASHFS_SYMLINK_TYPE:
992 if(IS_XATTR(xattr))
993 type = SQUASHFS_LSYMLINK_TYPE;
994 break;
995 case SQUASHFS_BLKDEV_TYPE:
996 if(IS_XATTR(xattr))
997 type = SQUASHFS_LBLKDEV_TYPE;
998 break;
999 case SQUASHFS_CHRDEV_TYPE:
1000 if(IS_XATTR(xattr))
1001 type = SQUASHFS_LCHRDEV_TYPE;
1002 break;
plougherc5d69322010-05-12 19:28:38 +00001003 case SQUASHFS_FIFO_TYPE:
1004 if(IS_XATTR(xattr))
1005 type = SQUASHFS_LFIFO_TYPE;
1006 break;
1007 case SQUASHFS_SOCKET_TYPE:
1008 if(IS_XATTR(xattr))
1009 type = SQUASHFS_LSOCKET_TYPE;
1010 break;
ploughere6e0e1b2010-05-12 17:17:06 +00001011 }
1012
plougher1f413c82005-11-18 00:02:14 +00001013 base->mode = SQUASHFS_MODE(buf->st_mode);
plougherfd57dfe2009-03-30 01:17:52 +00001014 base->uid = get_uid((unsigned int) global_uid == -1 ?
Hidehiko Abef240b142018-05-08 22:45:47 +09001015 buf->st_uid : global_uid, 1);
plougher1f413c82005-11-18 00:02:14 +00001016 base->inode_type = type;
plougherfd57dfe2009-03-30 01:17:52 +00001017 base->guid = get_guid((unsigned int) global_gid == -1 ?
Hidehiko Abef240b142018-05-08 22:45:47 +09001018 buf->st_gid : global_gid, 1);
plougher1f413c82005-11-18 00:02:14 +00001019 base->mtime = buf->st_mtime;
Phillip Lougher539c2b12012-07-30 20:14:52 +01001020 base->inode_number = get_inode_no(dir_ent->inode);
plougher1f413c82005-11-18 00:02:14 +00001021
1022 if(type == SQUASHFS_FILE_TYPE) {
1023 int i;
plougher8701ed62010-12-31 20:38:38 +00001024 struct squashfs_reg_inode_header *reg = &inode_header.reg;
1025 size_t off = offsetof(struct squashfs_reg_inode_header, block_list);
Mohamad Ayyash305fca62016-06-13 15:44:16 -07001026/* ANDROID CHANGES START*/
1027#ifdef ANDROID
1028 unsigned long long total_size = 0;
1029 char *sub_path;
1030#endif
1031/* ANDROID CHANGES END */
plougher1f413c82005-11-18 00:02:14 +00001032
1033 inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
plougher1f413c82005-11-18 00:02:14 +00001034 reg->file_size = byte_size;
1035 reg->start_block = start_block;
1036 reg->fragment = fragment->index;
1037 reg->offset = fragment->offset;
plougherac28cd12010-02-24 02:25:03 +00001038 SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode);
1039 SQUASHFS_SWAP_INTS(block_list, inode + off, offset);
plougher50b31762009-03-31 04:14:46 +00001040 TRACE("File inode, file_size %lld, start_block 0x%llx, blocks "
1041 "%d, fragment %d, offset %d, size %d\n", byte_size,
plougherfd57dfe2009-03-30 01:17:52 +00001042 start_block, offset, fragment->index, fragment->offset,
1043 fragment->size);
Colin Crossbe754902017-12-09 22:27:51 +00001044 for(i = 0; i < offset; i++) {
plougher1f413c82005-11-18 00:02:14 +00001045 TRACE("Block %d, size %d\n", i, block_list[i]);
Colin Crossbe754902017-12-09 22:27:51 +00001046 total_size += SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]);
1047 }
Mohamad Ayyash305fca62016-06-13 15:44:16 -07001048/* ANDROID CHANGES START*/
1049#ifdef ANDROID
1050 sub_path = subpathname(dir_ent);
1051 if (block_map_file && fragment->index == -1) {
1052 write_block_map_entry(sub_path, start_block, total_size, mount_point, block_map_file);
1053 }
1054#endif
1055/* ANDROID CHANGES END */
plougher1f413c82005-11-18 00:02:14 +00001056 }
1057 else if(type == SQUASHFS_LREG_TYPE) {
Mohamad Ayyash305fca62016-06-13 15:44:16 -07001058/* ANDROID CHANGES START*/
1059#ifdef ANDROID
1060 unsigned long long total_size = 0;
1061 char *sub_path;
1062#endif
1063/* ANDROID CHANGES END */
plougher1f413c82005-11-18 00:02:14 +00001064 int i;
plougher1e6ac4a2010-12-31 20:42:02 +00001065 struct squashfs_lreg_inode_header *reg = &inode_header.lreg;
1066 size_t off = offsetof(struct squashfs_lreg_inode_header, block_list);
plougher1f413c82005-11-18 00:02:14 +00001067
1068 inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
plougher1f413c82005-11-18 00:02:14 +00001069 reg->nlink = nlink;
1070 reg->file_size = byte_size;
1071 reg->start_block = start_block;
1072 reg->fragment = fragment->index;
1073 reg->offset = fragment->offset;
plougherf5a674d2009-03-25 05:38:27 +00001074 if(sparse && sparse >= byte_size)
1075 sparse = byte_size - 1;
plougher1b899fc2008-08-07 01:24:06 +00001076 reg->sparse = sparse;
ploughere6e0e1b2010-05-12 17:17:06 +00001077 reg->xattr = xattr;
plougherac28cd12010-02-24 02:25:03 +00001078 SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode);
1079 SQUASHFS_SWAP_INTS(block_list, inode + off, offset);
plougherfd57dfe2009-03-30 01:17:52 +00001080 TRACE("Long file inode, file_size %lld, start_block 0x%llx, "
plougher50b31762009-03-31 04:14:46 +00001081 "blocks %d, fragment %d, offset %d, size %d, nlink %d"
1082 "\n", byte_size, start_block, offset, fragment->index,
plougherfd57dfe2009-03-30 01:17:52 +00001083 fragment->offset, fragment->size, nlink);
Colin Crossbe754902017-12-09 22:27:51 +00001084 for(i = 0; i < offset; i++) {
plougher1f413c82005-11-18 00:02:14 +00001085 TRACE("Block %d, size %d\n", i, block_list[i]);
Colin Crossbe754902017-12-09 22:27:51 +00001086 total_size += SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]);
1087 }
Mohamad Ayyash305fca62016-06-13 15:44:16 -07001088/* ANDROID CHANGES START*/
1089#ifdef ANDROID
1090 sub_path = subpathname(dir_ent);
1091 if (block_map_file && fragment->index == -1) {
1092 write_block_map_entry(sub_path, start_block, total_size, mount_point, block_map_file);
1093 }
1094#endif
1095/* ANDROID CHANGES END */
plougher1f413c82005-11-18 00:02:14 +00001096 }
1097 else if(type == SQUASHFS_LDIR_TYPE) {
1098 int i;
1099 unsigned char *p;
plougher2611d392010-12-31 20:50:36 +00001100 struct squashfs_ldir_inode_header *dir = &inode_header.ldir;
plougher1f413c82005-11-18 00:02:14 +00001101 struct cached_dir_index *index = dir_in->index;
1102 unsigned int i_count = dir_in->i_count;
1103 unsigned int i_size = dir_in->i_size;
1104
1105 if(byte_size >= 1 << 27)
1106 BAD_ERROR("directory greater than 2^27-1 bytes!\n");
1107
1108 inode = get_inode(sizeof(*dir) + i_size);
plougher1f413c82005-11-18 00:02:14 +00001109 dir->inode_type = SQUASHFS_LDIR_TYPE;
plougher1f413c82005-11-18 00:02:14 +00001110 dir->nlink = dir_ent->dir->directory_count + 2;
1111 dir->file_size = byte_size;
1112 dir->offset = offset;
1113 dir->start_block = start_block;
1114 dir->i_count = i_count;
Phillip Lougher539c2b12012-07-30 20:14:52 +01001115 dir->parent_inode = get_parent_no(dir_ent->our_dir);
ploughere6e0e1b2010-05-12 17:17:06 +00001116 dir->xattr = xattr;
plougher1f413c82005-11-18 00:02:14 +00001117
plougherac28cd12010-02-24 02:25:03 +00001118 SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode);
plougher2611d392010-12-31 20:50:36 +00001119 p = inode + offsetof(struct squashfs_ldir_inode_header, index);
plougher1f413c82005-11-18 00:02:14 +00001120 for(i = 0; i < i_count; i++) {
plougherac28cd12010-02-24 02:25:03 +00001121 SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p);
plougher2bd2b722010-12-31 10:52:15 +00001122 p += offsetof(struct squashfs_dir_index, name);
plougherac28cd12010-02-24 02:25:03 +00001123 memcpy(p, index[i].name, index[i].index.size + 1);
1124 p += index[i].index.size + 1;
plougher1f413c82005-11-18 00:02:14 +00001125 }
plougher50b31762009-03-31 04:14:46 +00001126 TRACE("Long directory inode, file_size %lld, start_block "
1127 "0x%llx, offset 0x%x, nlink %d\n", byte_size,
1128 start_block, offset, dir_ent->dir->directory_count + 2);
plougher1f413c82005-11-18 00:02:14 +00001129 }
1130 else if(type == SQUASHFS_DIR_TYPE) {
plougher9d80a602010-12-31 20:47:24 +00001131 struct squashfs_dir_inode_header *dir = &inode_header.dir;
plougher1f413c82005-11-18 00:02:14 +00001132
1133 inode = get_inode(sizeof(*dir));
plougher1f413c82005-11-18 00:02:14 +00001134 dir->nlink = dir_ent->dir->directory_count + 2;
1135 dir->file_size = byte_size;
1136 dir->offset = offset;
1137 dir->start_block = start_block;
Phillip Lougher539c2b12012-07-30 20:14:52 +01001138 dir->parent_inode = get_parent_no(dir_ent->our_dir);
plougherac28cd12010-02-24 02:25:03 +00001139 SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode);
plougherfd57dfe2009-03-30 01:17:52 +00001140 TRACE("Directory inode, file_size %lld, start_block 0x%llx, "
plougher50b31762009-03-31 04:14:46 +00001141 "offset 0x%x, nlink %d\n", byte_size, start_block,
1142 offset, dir_ent->dir->directory_count + 2);
plougher1f413c82005-11-18 00:02:14 +00001143 }
1144 else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) {
plougherc70c6332010-12-31 20:11:09 +00001145 struct squashfs_dev_inode_header *dev = &inode_header.dev;
plougher5b398502008-10-04 23:22:03 +00001146 unsigned int major = major(buf->st_rdev);
1147 unsigned int minor = minor(buf->st_rdev);
plougher1f413c82005-11-18 00:02:14 +00001148
plougher5b398502008-10-04 23:22:03 +00001149 if(major > 0xfff) {
plougherfd57dfe2009-03-30 01:17:52 +00001150 ERROR("Major %d out of range in device node %s, "
1151 "truncating to %d\n", major, filename,
1152 major & 0xfff);
plougher5b398502008-10-04 23:22:03 +00001153 major &= 0xfff;
1154 }
1155 if(minor > 0xfffff) {
plougherfd57dfe2009-03-30 01:17:52 +00001156 ERROR("Minor %d out of range in device node %s, "
1157 "truncating to %d\n", minor, filename,
1158 minor & 0xfffff);
plougher5b398502008-10-04 23:22:03 +00001159 minor &= 0xfffff;
1160 }
plougher1f413c82005-11-18 00:02:14 +00001161 inode = get_inode(sizeof(*dev));
1162 dev->nlink = nlink;
plougher5b398502008-10-04 23:22:03 +00001163 dev->rdev = (major << 8) | (minor & 0xff) |
1164 ((minor & ~0xff) << 12);
plougherac28cd12010-02-24 02:25:03 +00001165 SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode);
rlougher8f7d0b82007-11-08 15:33:29 +00001166 TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink);
plougher1f413c82005-11-18 00:02:14 +00001167 }
ploughere6e0e1b2010-05-12 17:17:06 +00001168 else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) {
plougher0b4ee5b2010-12-31 20:14:00 +00001169 struct squashfs_ldev_inode_header *dev = &inode_header.ldev;
ploughere6e0e1b2010-05-12 17:17:06 +00001170 unsigned int major = major(buf->st_rdev);
1171 unsigned int minor = minor(buf->st_rdev);
1172
1173 if(major > 0xfff) {
1174 ERROR("Major %d out of range in device node %s, "
1175 "truncating to %d\n", major, filename,
1176 major & 0xfff);
1177 major &= 0xfff;
1178 }
1179 if(minor > 0xfffff) {
1180 ERROR("Minor %d out of range in device node %s, "
1181 "truncating to %d\n", minor, filename,
1182 minor & 0xfffff);
1183 minor &= 0xfffff;
1184 }
1185 inode = get_inode(sizeof(*dev));
1186 dev->nlink = nlink;
1187 dev->rdev = (major << 8) | (minor & 0xff) |
1188 ((minor & ~0xff) << 12);
1189 dev->xattr = xattr;
1190 SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode);
1191 TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink);
1192 }
plougher1f413c82005-11-18 00:02:14 +00001193 else if(type == SQUASHFS_SYMLINK_TYPE) {
plougher5ae6e952010-12-31 20:44:34 +00001194 struct squashfs_symlink_inode_header *symlink = &inode_header.symlink;
Phillip Loughere19ce452014-07-27 04:16:54 +01001195 int byte = strlen(dir_ent->inode->symlink);
plougher5ae6e952010-12-31 20:44:34 +00001196 size_t off = offsetof(struct squashfs_symlink_inode_header, symlink);
plougher1f413c82005-11-18 00:02:14 +00001197
plougher1f413c82005-11-18 00:02:14 +00001198 inode = get_inode(sizeof(*symlink) + byte);
1199 symlink->nlink = nlink;
plougher1f413c82005-11-18 00:02:14 +00001200 symlink->symlink_size = byte;
plougherac28cd12010-02-24 02:25:03 +00001201 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
Phillip Loughere19ce452014-07-27 04:16:54 +01001202 strncpy(inode + off, dir_ent->inode->symlink, byte);
plougherfd57dfe2009-03-30 01:17:52 +00001203 TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte,
1204 nlink);
plougher1f413c82005-11-18 00:02:14 +00001205 }
ploughere6e0e1b2010-05-12 17:17:06 +00001206 else if(type == SQUASHFS_LSYMLINK_TYPE) {
plougher5ae6e952010-12-31 20:44:34 +00001207 struct squashfs_symlink_inode_header *symlink = &inode_header.symlink;
Phillip Loughere19ce452014-07-27 04:16:54 +01001208 int byte = strlen(dir_ent->inode->symlink);
plougher5ae6e952010-12-31 20:44:34 +00001209 size_t off = offsetof(struct squashfs_symlink_inode_header, symlink);
ploughere6e0e1b2010-05-12 17:17:06 +00001210
ploughere6e0e1b2010-05-12 17:17:06 +00001211 inode = get_inode(sizeof(*symlink) + byte +
1212 sizeof(unsigned int));
1213 symlink->nlink = nlink;
1214 symlink->symlink_size = byte;
1215 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
Phillip Loughere19ce452014-07-27 04:16:54 +01001216 strncpy(inode + off, dir_ent->inode->symlink, byte);
ploughere6e0e1b2010-05-12 17:17:06 +00001217 SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1);
1218 TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte,
1219 nlink);
1220 }
plougher1f413c82005-11-18 00:02:14 +00001221 else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) {
ploughere56b9862010-12-31 10:58:35 +00001222 struct squashfs_ipc_inode_header *ipc = &inode_header.ipc;
plougher1f413c82005-11-18 00:02:14 +00001223
1224 inode = get_inode(sizeof(*ipc));
1225 ipc->nlink = nlink;
plougherac28cd12010-02-24 02:25:03 +00001226 SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode);
plougher50b31762009-03-31 04:14:46 +00001227 TRACE("ipc inode, type %s, nlink %d\n", type ==
1228 SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink);
plougherc5d69322010-05-12 19:28:38 +00001229 }
1230 else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) {
plougheraa0d1222010-12-31 20:06:24 +00001231 struct squashfs_lipc_inode_header *ipc = &inode_header.lipc;
plougherc5d69322010-05-12 19:28:38 +00001232
1233 inode = get_inode(sizeof(*ipc));
1234 ipc->nlink = nlink;
1235 ipc->xattr = xattr;
1236 SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode);
1237 TRACE("ipc inode, type %s, nlink %d\n", type ==
1238 SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink);
plougher1f413c82005-11-18 00:02:14 +00001239 } else
rlougher8f7d0b82007-11-08 15:33:29 +00001240 BAD_ERROR("Unrecognised inode %d in create_inode\n", type);
plougher1f413c82005-11-18 00:02:14 +00001241
1242 *i_no = MKINODE(inode);
1243 inode_count ++;
1244
plougherfd57dfe2009-03-30 01:17:52 +00001245 TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type,
1246 base->uid, base->guid);
plougher1f413c82005-11-18 00:02:14 +00001247
1248 return TRUE;
1249}
1250
1251
plougher50b31762009-03-31 04:14:46 +00001252void add_dir(squashfs_inode inode, unsigned int inode_number, char *name,
1253 int type, struct directory *dir)
plougher1f413c82005-11-18 00:02:14 +00001254{
plougher8cb05cd2005-12-11 23:32:35 +00001255 unsigned char *buff;
plougher9b393552010-12-31 20:55:43 +00001256 struct squashfs_dir_entry idir;
plougher1f413c82005-11-18 00:02:14 +00001257 unsigned int start_block = inode >> 16;
1258 unsigned int offset = inode & 0xffff;
plougher43d49082010-12-16 05:01:15 +00001259 unsigned int size = strlen(name);
plougher9b393552010-12-31 20:55:43 +00001260 size_t name_off = offsetof(struct squashfs_dir_entry, name);
plougher1f413c82005-11-18 00:02:14 +00001261
plougher43d49082010-12-16 05:01:15 +00001262 if(size > SQUASHFS_NAME_LEN) {
plougher1f413c82005-11-18 00:02:14 +00001263 size = SQUASHFS_NAME_LEN;
plougher50b31762009-03-31 04:14:46 +00001264 ERROR("Filename is greater than %d characters, truncating! ..."
1265 "\n", SQUASHFS_NAME_LEN);
plougher1f413c82005-11-18 00:02:14 +00001266 }
1267
plougher9b393552010-12-31 20:55:43 +00001268 if(dir->p + sizeof(struct squashfs_dir_entry) + size +
plougher520e1a12010-12-31 10:44:38 +00001269 sizeof(struct squashfs_dir_header)
1270 >= dir->buff + dir->size) {
plougherfd57dfe2009-03-30 01:17:52 +00001271 buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001272 if(buff == NULL)
1273 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001274
1275 dir->p = (dir->p - dir->buff) + buff;
1276 if(dir->entry_count_p)
plougherfd57dfe2009-03-30 01:17:52 +00001277 dir->entry_count_p = (dir->entry_count_p - dir->buff +
1278 buff);
plougher1f413c82005-11-18 00:02:14 +00001279 dir->index_count_p = dir->index_count_p - dir->buff + buff;
1280 dir->buff = buff;
1281 }
1282
plougherfd57dfe2009-03-30 01:17:52 +00001283 if(dir->entry_count == 256 || start_block != dir->start_block ||
1284 ((dir->entry_count_p != NULL) &&
plougher9b393552010-12-31 20:55:43 +00001285 ((dir->p + sizeof(struct squashfs_dir_entry) + size -
plougherfd57dfe2009-03-30 01:17:52 +00001286 dir->index_count_p) > SQUASHFS_METADATA_SIZE)) ||
plougher50b31762009-03-31 04:14:46 +00001287 ((long long) inode_number - dir->inode_number) > 32767
1288 || ((long long) inode_number - dir->inode_number)
1289 < -32768) {
plougher1f413c82005-11-18 00:02:14 +00001290 if(dir->entry_count_p) {
plougher520e1a12010-12-31 10:44:38 +00001291 struct squashfs_dir_header dir_header;
plougher1f413c82005-11-18 00:02:14 +00001292
plougher9b393552010-12-31 20:55:43 +00001293 if((dir->p + sizeof(struct squashfs_dir_entry) + size -
plougherfd57dfe2009-03-30 01:17:52 +00001294 dir->index_count_p) >
1295 SQUASHFS_METADATA_SIZE) {
1296 if(dir->i_count % I_COUNT_SIZE == 0) {
1297 dir->index = realloc(dir->index,
1298 (dir->i_count + I_COUNT_SIZE) *
1299 sizeof(struct cached_dir_index));
1300 if(dir->index == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001301 MEM_ERROR();
plougherfd57dfe2009-03-30 01:17:52 +00001302 }
1303 dir->index[dir->i_count].index.index =
1304 dir->p - dir->buff;
plougher1f413c82005-11-18 00:02:14 +00001305 dir->index[dir->i_count].index.size = size - 1;
1306 dir->index[dir->i_count++].name = name;
plougher2bd2b722010-12-31 10:52:15 +00001307 dir->i_size += sizeof(struct squashfs_dir_index)
1308 + size;
plougher1f413c82005-11-18 00:02:14 +00001309 dir->index_count_p = dir->p;
1310 }
1311
1312 dir_header.count = dir->entry_count - 1;
1313 dir_header.start_block = dir->start_block;
1314 dir_header.inode_number = dir->inode_number;
plougherfd57dfe2009-03-30 01:17:52 +00001315 SQUASHFS_SWAP_DIR_HEADER(&dir_header,
plougherac28cd12010-02-24 02:25:03 +00001316 dir->entry_count_p);
plougher1f413c82005-11-18 00:02:14 +00001317
1318 }
1319
1320
1321 dir->entry_count_p = dir->p;
1322 dir->start_block = start_block;
1323 dir->entry_count = 0;
1324 dir->inode_number = inode_number;
plougher520e1a12010-12-31 10:44:38 +00001325 dir->p += sizeof(struct squashfs_dir_header);
plougher1f413c82005-11-18 00:02:14 +00001326 }
1327
plougher1f413c82005-11-18 00:02:14 +00001328 idir.offset = offset;
1329 idir.type = type;
1330 idir.size = size - 1;
1331 idir.inode_number = ((long long) inode_number - dir->inode_number);
plougherac28cd12010-02-24 02:25:03 +00001332 SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p);
1333 strncpy((char *) dir->p + name_off, name, size);
plougher9b393552010-12-31 20:55:43 +00001334 dir->p += sizeof(struct squashfs_dir_entry) + size;
plougher1f413c82005-11-18 00:02:14 +00001335 dir->entry_count ++;
1336}
1337
1338
plougherfd57dfe2009-03-30 01:17:52 +00001339void write_dir(squashfs_inode *inode, struct dir_info *dir_info,
1340 struct directory *dir)
plougher1f413c82005-11-18 00:02:14 +00001341{
1342 unsigned int dir_size = dir->p - dir->buff;
plougher4627ca32010-12-16 05:03:55 +00001343 int data_space = directory_cache_size - directory_cache_bytes;
plougher1f413c82005-11-18 00:02:14 +00001344 unsigned int directory_block, directory_offset, i_count, index;
1345 unsigned short c_byte;
1346
1347 if(data_space < dir_size) {
plougherfd57dfe2009-03-30 01:17:52 +00001348 int realloc_size = directory_cache_size == 0 ?
1349 ((dir_size + SQUASHFS_METADATA_SIZE) &
1350 ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space;
plougher1f413c82005-11-18 00:02:14 +00001351
plougher17248ca2010-07-27 00:24:35 +00001352 void *dc = realloc(directory_data_cache,
plougherfd57dfe2009-03-30 01:17:52 +00001353 directory_cache_size + realloc_size);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001354 if(dc == NULL)
1355 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001356 directory_cache_size += realloc_size;
plougher17248ca2010-07-27 00:24:35 +00001357 directory_data_cache = dc;
plougher1f413c82005-11-18 00:02:14 +00001358 }
1359
1360 if(dir_size) {
plougher520e1a12010-12-31 10:44:38 +00001361 struct squashfs_dir_header dir_header;
plougher1f413c82005-11-18 00:02:14 +00001362
1363 dir_header.count = dir->entry_count - 1;
1364 dir_header.start_block = dir->start_block;
1365 dir_header.inode_number = dir->inode_number;
plougherac28cd12010-02-24 02:25:03 +00001366 SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p);
plougherfd57dfe2009-03-30 01:17:52 +00001367 memcpy(directory_data_cache + directory_cache_bytes, dir->buff,
1368 dir_size);
plougher1f413c82005-11-18 00:02:14 +00001369 }
1370 directory_offset = directory_cache_bytes;
1371 directory_block = directory_bytes;
1372 directory_cache_bytes += dir_size;
1373 i_count = 0;
1374 index = SQUASHFS_METADATA_SIZE - directory_offset;
1375
1376 while(1) {
plougherfd57dfe2009-03-30 01:17:52 +00001377 while(i_count < dir->i_count &&
1378 dir->index[i_count].index.index < index)
plougher50b31762009-03-31 04:14:46 +00001379 dir->index[i_count++].index.start_block =
1380 directory_bytes;
plougher1f413c82005-11-18 00:02:14 +00001381 index += SQUASHFS_METADATA_SIZE;
1382
1383 if(directory_cache_bytes < SQUASHFS_METADATA_SIZE)
1384 break;
1385
plougherfd57dfe2009-03-30 01:17:52 +00001386 if((directory_size - directory_bytes) <
1387 ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
plougher79d665a2010-07-27 00:19:23 +00001388 void *dt = realloc(directory_table,
plougher50b31762009-03-31 04:14:46 +00001389 directory_size + (SQUASHFS_METADATA_SIZE << 1)
1390 + 2);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001391 if(dt == NULL)
1392 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001393 directory_size += SQUASHFS_METADATA_SIZE << 1;
plougher79d665a2010-07-27 00:19:23 +00001394 directory_table = dt;
plougher1f413c82005-11-18 00:02:14 +00001395 }
1396
plougher50b31762009-03-31 04:14:46 +00001397 c_byte = mangle(directory_table + directory_bytes +
1398 BLOCK_OFFSET, directory_data_cache,
1399 SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE,
1400 noI, 0);
plougherfd57dfe2009-03-30 01:17:52 +00001401 TRACE("Directory block @ 0x%x, size %d\n", directory_bytes,
1402 c_byte);
plougherac28cd12010-02-24 02:25:03 +00001403 SQUASHFS_SWAP_SHORTS(&c_byte,
1404 directory_table + directory_bytes, 1);
plougher50b31762009-03-31 04:14:46 +00001405 directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) +
1406 BLOCK_OFFSET;
plougher1b899fc2008-08-07 01:24:06 +00001407 total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET;
plougher14dd5f32010-08-02 18:20:03 +00001408 memmove(directory_data_cache, directory_data_cache +
plougherfd57dfe2009-03-30 01:17:52 +00001409 SQUASHFS_METADATA_SIZE, directory_cache_bytes -
1410 SQUASHFS_METADATA_SIZE);
plougher1f413c82005-11-18 00:02:14 +00001411 directory_cache_bytes -= SQUASHFS_METADATA_SIZE;
1412 }
1413
plougher3c6bdb52010-05-01 02:30:59 +00001414 create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE,
1415 dir_size + 3, directory_block, directory_offset, NULL, NULL,
1416 dir, 0);
plougher1f413c82005-11-18 00:02:14 +00001417
1418#ifdef SQUASHFS_TRACE
plougher1f288f62009-02-21 03:05:52 +00001419 {
plougher1f413c82005-11-18 00:02:14 +00001420 unsigned char *dirp;
1421 int count;
1422
1423 TRACE("Directory contents of inode 0x%llx\n", *inode);
1424 dirp = dir->buff;
1425 while(dirp < dir->p) {
1426 char buffer[SQUASHFS_NAME_LEN + 1];
plougher9b393552010-12-31 20:55:43 +00001427 struct squashfs_dir_entry idir, *idirp;
plougher520e1a12010-12-31 10:44:38 +00001428 struct squashfs_dir_header dirh;
1429 SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp,
plougher360514a2009-03-30 03:01:38 +00001430 &dirh);
plougher1f288f62009-02-21 03:05:52 +00001431 count = dirh.count + 1;
plougher520e1a12010-12-31 10:44:38 +00001432 dirp += sizeof(struct squashfs_dir_header);
plougher1f413c82005-11-18 00:02:14 +00001433
plougher50b31762009-03-31 04:14:46 +00001434 TRACE("\tStart block 0x%x, count %d\n",
1435 dirh.start_block, count);
plougher1f413c82005-11-18 00:02:14 +00001436
1437 while(count--) {
plougher9b393552010-12-31 20:55:43 +00001438 idirp = (struct squashfs_dir_entry *) dirp;
plougher1f288f62009-02-21 03:05:52 +00001439 SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir);
plougher1f413c82005-11-18 00:02:14 +00001440 strncpy(buffer, idirp->name, idir.size + 1);
1441 buffer[idir.size + 1] = '\0';
plougher50b31762009-03-31 04:14:46 +00001442 TRACE("\t\tname %s, inode offset 0x%x, type "
1443 "%d\n", buffer, idir.offset, idir.type);
plougher9b393552010-12-31 20:55:43 +00001444 dirp += sizeof(struct squashfs_dir_entry) + idir.size +
ploughere8b26f62010-12-16 05:06:00 +00001445 1;
plougher1f413c82005-11-18 00:02:14 +00001446 }
1447 }
1448 }
1449#endif
1450 dir_count ++;
plougher1f413c82005-11-18 00:02:14 +00001451}
1452
1453
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001454static struct file_buffer *get_fragment(struct fragment *fragment)
plougher1f413c82005-11-18 00:02:14 +00001455{
plougher8ed84b92010-12-31 10:37:24 +00001456 struct squashfs_fragment_entry *disk_fragment;
plougher76c64082008-03-08 01:32:23 +00001457 struct file_buffer *buffer, *compressed_buffer;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001458 long long start_block;
1459 int res, size, index = fragment->index;
1460 char locked;
1461
1462 /*
1463 * Lookup fragment block in cache.
1464 * If the fragment block doesn't exist, then get the compressed version
1465 * from the writer cache or off disk, and decompress it.
1466 *
1467 * This routine has two things which complicate the code:
1468 *
1469 * 1. Multiple threads can simultaneously lookup/create the
1470 * same buffer. This means a buffer needs to be "locked"
1471 * when it is being filled in, to prevent other threads from
1472 * using it when it is not ready. This is because we now do
1473 * fragment duplicate checking in parallel.
1474 * 2. We have two caches which need to be checked for the
1475 * presence of fragment blocks: the normal fragment cache
1476 * and a "reserve" cache. The reserve cache is used to
1477 * prevent an unnecessary pipeline stall when the fragment cache
1478 * is full of fragments waiting to be compressed.
1479 */
plougher5507dd92006-11-06 00:43:10 +00001480
plougher76c64082008-03-08 01:32:23 +00001481 if(fragment->index == SQUASHFS_INVALID_FRAG)
1482 return NULL;
plougher5507dd92006-11-06 00:43:10 +00001483
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001484 pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1485 pthread_mutex_lock(&dup_mutex);
1486
1487again:
1488 buffer = cache_lookup_nowait(fragment_buffer, index, &locked);
1489 if(buffer) {
1490 pthread_mutex_unlock(&dup_mutex);
1491 if(locked)
1492 /* got a buffer being filled in. Wait for it */
1493 cache_wait_unlock(buffer);
1494 goto finished;
1495 }
1496
1497 /* not in fragment cache, is it in the reserve cache? */
1498 buffer = cache_lookup_nowait(reserve_cache, index, &locked);
1499 if(buffer) {
1500 pthread_mutex_unlock(&dup_mutex);
1501 if(locked)
1502 /* got a buffer being filled in. Wait for it */
1503 cache_wait_unlock(buffer);
1504 goto finished;
1505 }
1506
1507 /* in neither cache, try to get it from the fragment cache */
1508 buffer = cache_get_nowait(fragment_buffer, index);
1509 if(!buffer) {
1510 /*
1511 * no room, get it from the reserve cache, this is
1512 * dimensioned so it will always have space (no more than
1513 * processors + 1 can have an outstanding reserve buffer)
1514 */
1515 buffer = cache_get_nowait(reserve_cache, index);
1516 if(!buffer) {
1517 /* failsafe */
1518 ERROR("no space in reserve cache\n");
1519 goto again;
1520 }
1521 }
1522
1523 pthread_mutex_unlock(&dup_mutex);
plougher76c64082008-03-08 01:32:23 +00001524
Phillip Lougher943acad2014-04-17 03:31:01 +01001525 compressed_buffer = cache_lookup(fwriter_buffer, index);
plougher5507dd92006-11-06 00:43:10 +00001526
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001527 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001528 pthread_mutex_lock(&fragment_mutex);
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001529 disk_fragment = &fragment_table[index];
plougher5507dd92006-11-06 00:43:10 +00001530 size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size);
plougher76c64082008-03-08 01:32:23 +00001531 start_block = disk_fragment->start_block;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001532 pthread_cleanup_pop(1);
plougher1f413c82005-11-18 00:02:14 +00001533
plougher0f464442008-03-31 00:27:56 +00001534 if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) {
plougher1d065e92010-06-18 03:58:27 +00001535 int error;
plougher76c64082008-03-08 01:32:23 +00001536 char *data;
plougher1f413c82005-11-18 00:02:14 +00001537
plougher76c64082008-03-08 01:32:23 +00001538 if(compressed_buffer)
1539 data = compressed_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001540 else {
plougher49b57a92009-03-31 03:23:05 +00001541 data = read_from_disk(start_block, size);
Phillip Lougherac731382013-05-19 04:19:44 +01001542 if(data == NULL) {
1543 ERROR("Failed to read fragment from output"
1544 " filesystem\n");
1545 BAD_ERROR("Output filesystem corrupted?\n");
1546 }
1547 }
plougher1f413c82005-11-18 00:02:14 +00001548
plougherb48442b2010-12-31 07:57:54 +00001549 res = compressor_uncompress(comp, buffer->data, data, size,
1550 block_size, &error);
ploughera175ce22009-07-30 04:43:27 +00001551 if(res == -1)
1552 BAD_ERROR("%s uncompress failed with error code %d\n",
1553 comp->name, error);
plougher76c64082008-03-08 01:32:23 +00001554 } else if(compressed_buffer)
1555 memcpy(buffer->data, compressed_buffer->data, size);
plougher1d065e92010-06-18 03:58:27 +00001556 else {
1557 res = read_fs_bytes(fd, start_block, size, buffer->data);
Phillip Lougher94519382013-03-06 02:43:42 +00001558 if(res == 0) {
1559 ERROR("Failed to read fragment from output "
1560 "filesystem\n");
1561 BAD_ERROR("Output filesystem corrupted?\n");
1562 }
plougher1d065e92010-06-18 03:58:27 +00001563 }
plougher1f413c82005-11-18 00:02:14 +00001564
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001565 cache_unlock(buffer);
plougher03859fa2009-03-31 03:18:18 +00001566 cache_block_put(compressed_buffer);
1567
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001568finished:
1569 pthread_cleanup_pop(0);
1570
plougher76c64082008-03-08 01:32:23 +00001571 return buffer;
plougher1f413c82005-11-18 00:02:14 +00001572}
1573
plougher2ea89142008-03-11 01:34:19 +00001574
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001575unsigned short get_fragment_checksum(struct file_info *file)
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001576{
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001577 struct file_buffer *frag_buffer;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001578 struct append_file *append;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001579 int res, index = file->fragment->index;
1580 unsigned short checksum;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001581
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001582 if(index == SQUASHFS_INVALID_FRAG)
1583 return 0;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001584
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001585 pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1586 pthread_mutex_lock(&dup_mutex);
1587 res = file->have_frag_checksum;
1588 checksum = file->fragment_checksum;
Phillip Lougherc4a8a792014-04-10 02:23:26 +01001589 pthread_cleanup_pop(1);
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001590
1591 if(res)
1592 return checksum;
1593
1594 frag_buffer = get_fragment(file->fragment);
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001595
Phillip Lougherc4a8a792014-04-10 02:23:26 +01001596 pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1597
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001598 for(append = file_mapping[index]; append; append = append->next) {
1599 int offset = append->file->fragment->offset;
1600 int size = append->file->fragment->size;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001601 unsigned short cksum =
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001602 get_checksum_mem(frag_buffer->data + offset, size);
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001603
1604 if(file == append->file)
1605 checksum = cksum;
1606
1607 pthread_mutex_lock(&dup_mutex);
1608 append->file->fragment_checksum = cksum;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001609 append->file->have_frag_checksum = TRUE;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001610 pthread_mutex_unlock(&dup_mutex);
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001611 }
1612
1613 cache_block_put(frag_buffer);
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001614 pthread_cleanup_pop(0);
1615
1616 return checksum;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001617}
1618
1619
Phillip Lougher943acad2014-04-17 03:31:01 +01001620void lock_fragments()
plougher5507dd92006-11-06 00:43:10 +00001621{
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001622 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001623 pthread_mutex_lock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00001624 fragments_locked = TRUE;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001625 pthread_cleanup_pop(1);
plougher2ea89142008-03-11 01:34:19 +00001626}
1627
1628
1629void unlock_fragments()
1630{
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001631 int frg, size;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001632 struct file_buffer *write_buffer;
plougher2ea89142008-03-11 01:34:19 +00001633
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001634 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00001635 pthread_mutex_lock(&fragment_mutex);
Phillip Lougher71ad9642013-05-25 05:03:48 +01001636
1637 /*
1638 * Note queue_empty() is inherently racy with respect to concurrent
1639 * queue get and pushes. We avoid this because we're holding the
1640 * fragment_mutex which ensures no other threads can be using the
1641 * queue at this time.
1642 */
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001643 while(!queue_empty(locked_fragment)) {
1644 write_buffer = queue_get(locked_fragment);
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001645 frg = write_buffer->block;
1646 size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size);
1647 fragment_table[frg].start_block = bytes;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001648 write_buffer->block = bytes;
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001649 bytes += size;
plougher2ea89142008-03-11 01:34:19 +00001650 fragments_outstanding --;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001651 queue_put(to_writer, write_buffer);
plougher50b31762009-03-31 04:14:46 +00001652 TRACE("fragment_locked writing fragment %d, compressed size %d"
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001653 "\n", frg, size);
plougher2ea89142008-03-11 01:34:19 +00001654 }
1655 fragments_locked = FALSE;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001656 pthread_cleanup_pop(1);
plougher2ea89142008-03-11 01:34:19 +00001657}
1658
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001659/* Called with the fragment_mutex locked */
plougherb7a66812010-07-21 01:06:59 +00001660void add_pending_fragment(struct file_buffer *write_buffer, int c_byte,
plougher17b269c2009-03-30 01:33:07 +00001661 int fragment)
plougher2ea89142008-03-11 01:34:19 +00001662{
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001663 fragment_table[fragment].size = c_byte;
1664 write_buffer->block = fragment;
1665
1666 queue_put(locked_fragment, write_buffer);
plougher5507dd92006-11-06 00:43:10 +00001667}
1668
1669
Phillip Lougher04b7b532011-06-11 02:14:15 +01001670void write_fragment(struct file_buffer *fragment)
plougher1f413c82005-11-18 00:02:14 +00001671{
Phillip Lougher04b7b532011-06-11 02:14:15 +01001672 if(fragment == NULL)
plougher1f413c82005-11-18 00:02:14 +00001673 return;
1674
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001675 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001676 pthread_mutex_lock(&fragment_mutex);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001677 fragment_table[fragment->block].unused = 0;
1678 fragments_outstanding ++;
1679 queue_put(to_frag, fragment);
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001680 pthread_cleanup_pop(1);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001681}
1682
1683
1684struct file_buffer *allocate_fragment()
1685{
Phillip Lougher316ab632013-04-29 02:53:39 +01001686 struct file_buffer *fragment = cache_get(fragment_buffer, fragments);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001687
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001688 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001689 pthread_mutex_lock(&fragment_mutex);
1690
plougher5507dd92006-11-06 00:43:10 +00001691 if(fragments % FRAG_SIZE == 0) {
plougherfa89c332010-07-27 00:27:15 +00001692 void *ft = realloc(fragment_table, (fragments +
plougher8ed84b92010-12-31 10:37:24 +00001693 FRAG_SIZE) * sizeof(struct squashfs_fragment_entry));
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01001694 if(ft == NULL)
1695 MEM_ERROR();
plougherfa89c332010-07-27 00:27:15 +00001696 fragment_table = ft;
plougher5507dd92006-11-06 00:43:10 +00001697 }
Phillip Lougherb9508be2011-06-13 02:25:51 +01001698
1699 fragment->size = 0;
1700 fragment->block = fragments ++;
1701
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001702 pthread_cleanup_pop(1);
1703
Phillip Lougherb9508be2011-06-13 02:25:51 +01001704 return fragment;
plougher5507dd92006-11-06 00:43:10 +00001705}
1706
ploughereb6eac92008-02-26 01:50:48 +00001707
plougher1f413c82005-11-18 00:02:14 +00001708static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0};
Phillip Lougherd2f045f2012-12-28 03:15:45 +00001709
1710
1711void free_fragment(struct fragment *fragment)
1712{
1713 if(fragment != &empty_fragment)
1714 free(fragment);
1715}
1716
1717
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001718struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer,
1719 struct dir_ent *dir_ent)
plougher1f413c82005-11-18 00:02:14 +00001720{
1721 struct fragment *ffrg;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001722 struct file_buffer **fragment;
plougher1f413c82005-11-18 00:02:14 +00001723
plougher5507dd92006-11-06 00:43:10 +00001724 if(file_buffer == NULL || file_buffer->size == 0)
plougher1f413c82005-11-18 00:02:14 +00001725 return &empty_fragment;
1726
Phillip Lougher22d67da2014-08-08 02:55:33 +01001727 fragment = eval_frag_actions(root_dir, dir_ent);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001728
1729 if((*fragment) && (*fragment)->size + file_buffer->size > block_size) {
1730 write_fragment(*fragment);
1731 *fragment = NULL;
Phillip Lougher04b7b532011-06-11 02:14:15 +01001732 }
plougher1f413c82005-11-18 00:02:14 +00001733
ploughere7e6e832010-12-16 05:08:06 +00001734 ffrg = malloc(sizeof(struct fragment));
1735 if(ffrg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001736 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001737
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001738 if(*fragment == NULL)
1739 *fragment = allocate_fragment();
plougher5507dd92006-11-06 00:43:10 +00001740
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001741 ffrg->index = (*fragment)->block;
1742 ffrg->offset = (*fragment)->size;
plougher5507dd92006-11-06 00:43:10 +00001743 ffrg->size = file_buffer->size;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001744 memcpy((*fragment)->data + (*fragment)->size, file_buffer->data,
plougher17b269c2009-03-30 01:33:07 +00001745 file_buffer->size);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001746 (*fragment)->size += file_buffer->size;
plougher1f413c82005-11-18 00:02:14 +00001747
1748 return ffrg;
1749}
1750
1751
ploughera0a49c32010-08-11 01:47:59 +00001752long long generic_write_table(int length, void *buffer, int length2,
1753 void *buffer2, int uncompressed)
plougher1f413c82005-11-18 00:02:14 +00001754{
plougher17b269c2009-03-30 01:33:07 +00001755 int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) /
1756 SQUASHFS_METADATA_SIZE;
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001757 long long *list, start_bytes;
Phillip Lougherc1305322012-11-02 22:28:02 +00001758 int compressed_size, i, list_size = meta_blocks * sizeof(long long);
plougher1f413c82005-11-18 00:02:14 +00001759 unsigned short c_byte;
plougher0e453652006-11-06 01:49:35 +00001760 char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2];
1761
plougher82ab2332009-04-21 00:21:21 +00001762#ifdef SQUASHFS_TRACE
plougher0e453652006-11-06 01:49:35 +00001763 long long obytes = bytes;
plougher40d8b952010-02-12 16:26:32 +00001764 int olength = length;
plougher82ab2332009-04-21 00:21:21 +00001765#endif
plougher1f413c82005-11-18 00:02:14 +00001766
Phillip Lougherc1305322012-11-02 22:28:02 +00001767 list = malloc(list_size);
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001768 if(list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001769 MEM_ERROR();
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001770
plougher1f413c82005-11-18 00:02:14 +00001771 for(i = 0; i < meta_blocks; i++) {
plougher17b269c2009-03-30 01:33:07 +00001772 int avail_bytes = length > SQUASHFS_METADATA_SIZE ?
1773 SQUASHFS_METADATA_SIZE : length;
1774 c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i *
1775 SQUASHFS_METADATA_SIZE , avail_bytes,
1776 SQUASHFS_METADATA_SIZE, uncompressed, 0);
plougherac28cd12010-02-24 02:25:03 +00001777 SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1);
plougher1f413c82005-11-18 00:02:14 +00001778 list[i] = bytes;
plougher50b31762009-03-31 04:14:46 +00001779 compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) +
1780 BLOCK_OFFSET;
plougher17b269c2009-03-30 01:33:07 +00001781 TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes,
1782 compressed_size);
plougher0dd6f122009-03-29 21:43:57 +00001783 write_destination(fd, bytes, compressed_size, cbuffer);
plougher1f413c82005-11-18 00:02:14 +00001784 bytes += compressed_size;
plougher10f7d572010-07-20 02:14:04 +00001785 total_bytes += avail_bytes;
plougher0e453652006-11-06 01:49:35 +00001786 length -= avail_bytes;
plougher1f413c82005-11-18 00:02:14 +00001787 }
1788
ploughere6e0e1b2010-05-12 17:17:06 +00001789 start_bytes = bytes;
1790 if(length2) {
ploughera0a49c32010-08-11 01:47:59 +00001791 write_destination(fd, bytes, length2, buffer2);
ploughere6e0e1b2010-05-12 17:17:06 +00001792 bytes += length2;
plougher10f7d572010-07-20 02:14:04 +00001793 total_bytes += length2;
ploughere6e0e1b2010-05-12 17:17:06 +00001794 }
1795
plougher1f288f62009-02-21 03:05:52 +00001796 SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks);
Phillip Lougherc1305322012-11-02 22:28:02 +00001797 write_destination(fd, bytes, list_size, list);
1798 bytes += list_size;
1799 total_bytes += list_size;
plougher1f413c82005-11-18 00:02:14 +00001800
plougher40d8b952010-02-12 16:26:32 +00001801 TRACE("generic_write_table: total uncompressed %d compressed %lld\n",
1802 olength, bytes - obytes);
plougher0e453652006-11-06 01:49:35 +00001803
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001804 free(list);
1805
plougher1f413c82005-11-18 00:02:14 +00001806 return start_bytes;
1807}
1808
1809
plougher0e453652006-11-06 01:49:35 +00001810long long write_fragment_table()
1811{
1812 unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments);
plougher0e453652006-11-06 01:49:35 +00001813 int i;
1814
plougher17b269c2009-03-30 01:33:07 +00001815 TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments,
1816 frag_bytes);
plougherac28cd12010-02-24 02:25:03 +00001817 for(i = 0; i < fragments; i++) {
plougher50b31762009-03-31 04:14:46 +00001818 TRACE("write_fragment_table: fragment %d, start_block 0x%llx, "
1819 "size %d\n", i, fragment_table[i].start_block,
plougher17b269c2009-03-30 01:33:07 +00001820 fragment_table[i].size);
Phillip Lougher162c24c2012-10-30 02:54:16 +00001821 SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]);
plougher0e453652006-11-06 01:49:35 +00001822 }
1823
Phillip Lougher162c24c2012-10-30 02:54:16 +00001824 return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF);
plougher0e453652006-11-06 01:49:35 +00001825}
1826
1827
plougher1f413c82005-11-18 00:02:14 +00001828char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE];
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001829static char *read_from_disk(long long start, unsigned int avail_bytes)
plougher1f413c82005-11-18 00:02:14 +00001830{
plougher1d065e92010-06-18 03:58:27 +00001831 int res;
1832
1833 res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer);
Phillip Lougherac731382013-05-19 04:19:44 +01001834 if(res == 0)
1835 return NULL;
plougher1d065e92010-06-18 03:58:27 +00001836
plougher1f413c82005-11-18 00:02:14 +00001837 return read_from_file_buffer;
1838}
1839
1840
plougher1b899fc2008-08-07 01:24:06 +00001841char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE];
1842char *read_from_disk2(long long start, unsigned int avail_bytes)
1843{
plougher1d065e92010-06-18 03:58:27 +00001844 int res;
1845
1846 res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2);
Phillip Lougherac731382013-05-19 04:19:44 +01001847 if(res == 0)
1848 return NULL;
plougher1d065e92010-06-18 03:58:27 +00001849
plougher1b899fc2008-08-07 01:24:06 +00001850 return read_from_file_buffer2;
1851}
1852
1853
plougher1f413c82005-11-18 00:02:14 +00001854/*
1855 * Compute 16 bit BSD checksum over the data
1856 */
plougher5507dd92006-11-06 00:43:10 +00001857unsigned short get_checksum(char *buff, int bytes, unsigned short chksum)
plougher1f413c82005-11-18 00:02:14 +00001858{
plougher5507dd92006-11-06 00:43:10 +00001859 unsigned char *b = (unsigned char *) buff;
plougher1f413c82005-11-18 00:02:14 +00001860
plougher5507dd92006-11-06 00:43:10 +00001861 while(bytes --) {
1862 chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1;
1863 chksum += *b++;
plougher1f413c82005-11-18 00:02:14 +00001864 }
1865
1866 return chksum;
1867}
1868
1869
plougher17b269c2009-03-30 01:33:07 +00001870unsigned short get_checksum_disk(long long start, long long l,
1871 unsigned int *blocks)
plougher5507dd92006-11-06 00:43:10 +00001872{
1873 unsigned short chksum = 0;
1874 unsigned int bytes;
plougher57e6d182008-05-06 23:51:29 +00001875 struct file_buffer *write_buffer;
1876 int i;
plougher5507dd92006-11-06 00:43:10 +00001877
plougher57e6d182008-05-06 23:51:29 +00001878 for(i = 0; l; i++) {
1879 bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]);
1880 if(bytes == 0) /* sparse block */
1881 continue;
Phillip Lougher943acad2014-04-17 03:31:01 +01001882 write_buffer = cache_lookup(bwriter_buffer, start);
plougher57e6d182008-05-06 23:51:29 +00001883 if(write_buffer) {
plougher50b31762009-03-31 04:14:46 +00001884 chksum = get_checksum(write_buffer->data, bytes,
1885 chksum);
plougher57e6d182008-05-06 23:51:29 +00001886 cache_block_put(write_buffer);
Phillip Lougherac731382013-05-19 04:19:44 +01001887 } else {
1888 void *data = read_from_disk(start, bytes);
1889 if(data == NULL) {
1890 ERROR("Failed to checksum data from output"
1891 " filesystem\n");
1892 BAD_ERROR("Output filesystem corrupted?\n");
1893 }
1894
1895 chksum = get_checksum(data, bytes, chksum);
1896 }
1897
plougher5507dd92006-11-06 00:43:10 +00001898 l -= bytes;
plougher5507dd92006-11-06 00:43:10 +00001899 start += bytes;
1900 }
1901
1902 return chksum;
1903}
1904
1905
plougher5507dd92006-11-06 00:43:10 +00001906unsigned short get_checksum_mem(char *buff, int bytes)
1907{
1908 return get_checksum(buff, bytes, 0);
1909}
1910
1911
1912unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer)
1913{
1914 if(file_buffer == NULL)
1915 return 0;
1916 else
1917 return get_checksum(file_buffer->data, file_buffer->size, 0);
1918}
1919
1920
plougher5507dd92006-11-06 00:43:10 +00001921#define DUP_HASH(a) (a & 0xffff)
plougher17b269c2009-03-30 01:33:07 +00001922void add_file(long long start, long long file_size, long long file_bytes,
plougher50b31762009-03-31 04:14:46 +00001923 unsigned int *block_listp, int blocks, unsigned int fragment,
1924 int offset, int bytes)
plougher1f413c82005-11-18 00:02:14 +00001925{
1926 struct fragment *frg;
plougher058eae42006-01-30 14:27:44 +00001927 unsigned int *block_list = block_listp;
plougher5507dd92006-11-06 00:43:10 +00001928 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001929 struct append_file *append_file;
1930 struct file_info *file;
plougher058eae42006-01-30 14:27:44 +00001931
plougher5507dd92006-11-06 00:43:10 +00001932 if(!duplicate_checking || file_size == 0)
plougher1f413c82005-11-18 00:02:14 +00001933 return;
1934
plougher5507dd92006-11-06 00:43:10 +00001935 for(; dupl_ptr; dupl_ptr = dupl_ptr->next) {
1936 if(file_size != dupl_ptr->file_size)
1937 continue;
1938 if(blocks != 0 && start != dupl_ptr->start)
1939 continue;
1940 if(fragment != dupl_ptr->fragment->index)
1941 continue;
plougher17b269c2009-03-30 01:33:07 +00001942 if(fragment != SQUASHFS_INVALID_FRAG && (offset !=
1943 dupl_ptr->fragment->offset || bytes !=
1944 dupl_ptr->fragment->size))
plougher5507dd92006-11-06 00:43:10 +00001945 continue;
1946 return;
1947 }
1948
plougher3bb279c2010-12-16 05:10:03 +00001949 frg = malloc(sizeof(struct fragment));
1950 if(frg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001951 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001952
1953 frg->index = fragment;
1954 frg->offset = offset;
1955 frg->size = bytes;
plougher1f413c82005-11-18 00:02:14 +00001956
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001957 file = add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00001958 FALSE, FALSE);
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001959
1960 if(fragment == SQUASHFS_INVALID_FRAG)
1961 return;
1962
1963 append_file = malloc(sizeof(struct append_file));
1964 if(append_file == NULL)
1965 MEM_ERROR();
1966
1967 append_file->file = file;
1968 append_file->next = file_mapping[fragment];
1969 file_mapping[fragment] = append_file;
plougher5507dd92006-11-06 00:43:10 +00001970}
plougher1f413c82005-11-18 00:02:14 +00001971
plougher1f413c82005-11-18 00:02:14 +00001972
plougher5507dd92006-11-06 00:43:10 +00001973int pre_duplicate(long long file_size)
plougher1f413c82005-11-18 00:02:14 +00001974{
plougher5507dd92006-11-06 00:43:10 +00001975 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
plougher1f413c82005-11-18 00:02:14 +00001976
1977 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher5507dd92006-11-06 00:43:10 +00001978 if(dupl_ptr->file_size == file_size)
1979 return TRUE;
plougher1f413c82005-11-18 00:02:14 +00001980
plougher5507dd92006-11-06 00:43:10 +00001981 return FALSE;
1982}
1983
1984
plougher17b269c2009-03-30 01:33:07 +00001985struct file_info *add_non_dup(long long file_size, long long bytes,
1986 unsigned int *block_list, long long start, struct fragment *fragment,
1987 unsigned short checksum, unsigned short fragment_checksum,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00001988 int checksum_flag, int checksum_frag_flag)
plougher5507dd92006-11-06 00:43:10 +00001989{
plougher51ef9ae2010-12-16 05:14:28 +00001990 struct file_info *dupl_ptr = malloc(sizeof(struct file_info));
plougher5507dd92006-11-06 00:43:10 +00001991
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001992 if(dupl_ptr == NULL)
1993 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00001994
1995 dupl_ptr->file_size = file_size;
1996 dupl_ptr->bytes = bytes;
1997 dupl_ptr->block_list = block_list;
1998 dupl_ptr->start = start;
1999 dupl_ptr->fragment = fragment;
2000 dupl_ptr->checksum = checksum;
2001 dupl_ptr->fragment_checksum = fragment_checksum;
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002002 dupl_ptr->have_frag_checksum = checksum_frag_flag;
Phillip Lougher557cefa2014-02-05 04:36:05 +00002003 dupl_ptr->have_checksum = checksum_flag;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002004
2005 pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
2006 pthread_mutex_lock(&dup_mutex);
plougher5507dd92006-11-06 00:43:10 +00002007 dupl_ptr->next = dupl[DUP_HASH(file_size)];
2008 dupl[DUP_HASH(file_size)] = dupl_ptr;
2009 dup_files ++;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002010 pthread_cleanup_pop(1);
plougher5507dd92006-11-06 00:43:10 +00002011
2012 return dupl_ptr;
2013}
2014
2015
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002016struct fragment *frag_duplicate(struct file_buffer *file_buffer, char *dont_put)
2017{
2018 struct file_info *dupl_ptr;
2019 struct file_buffer *buffer;
2020 struct file_info *dupl_start = file_buffer->dupl_start;
2021 long long file_size = file_buffer->file_size;
2022 unsigned short checksum = file_buffer->checksum;
2023 int res;
2024
2025 if(file_buffer->duplicate) {
2026 TRACE("Found duplicate file, fragment %d, size %d, offset %d, "
2027 "checksum 0x%x\n", dupl_start->fragment->index,
2028 file_size, dupl_start->fragment->offset, checksum);
2029 *dont_put = TRUE;
2030 return dupl_start->fragment;
2031 } else {
2032 *dont_put = FALSE;
2033 dupl_ptr = dupl[DUP_HASH(file_size)];
2034 }
2035
2036 for(; dupl_ptr && dupl_ptr != dupl_start; dupl_ptr = dupl_ptr->next) {
2037 if(file_size == dupl_ptr->file_size && file_size ==
2038 dupl_ptr->fragment->size) {
2039 if(get_fragment_checksum(dupl_ptr) == checksum) {
2040 buffer = get_fragment(dupl_ptr->fragment);
2041 res = memcmp(file_buffer->data, buffer->data +
2042 dupl_ptr->fragment->offset, file_size);
2043 cache_block_put(buffer);
2044 if(res == 0)
2045 break;
2046 }
2047 }
2048 }
2049
2050 if(!dupl_ptr || dupl_ptr == dupl_start)
2051 return NULL;
2052
2053 TRACE("Found duplicate file, fragment %d, size %d, offset %d, "
2054 "checksum 0x%x\n", dupl_ptr->fragment->index, file_size,
2055 dupl_ptr->fragment->offset, checksum);
2056
2057 return dupl_ptr->fragment;
2058}
2059
2060
plougher17b269c2009-03-30 01:33:07 +00002061struct file_info *duplicate(long long file_size, long long bytes,
2062 unsigned int **block_list, long long *start, struct fragment **fragment,
2063 struct file_buffer *file_buffer, int blocks, unsigned short checksum,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002064 int checksum_flag)
plougher5507dd92006-11-06 00:43:10 +00002065{
2066 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
2067 int frag_bytes = file_buffer ? file_buffer->size : 0;
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002068 unsigned short fragment_checksum = file_buffer ?
2069 file_buffer->checksum : 0;
plougher5507dd92006-11-06 00:43:10 +00002070
2071 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher16111452010-07-22 05:12:18 +00002072 if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes
2073 && frag_bytes == dupl_ptr->fragment->size) {
plougher1b899fc2008-08-07 01:24:06 +00002074 long long target_start, dup_start = dupl_ptr->start;
plougher5507dd92006-11-06 00:43:10 +00002075 int block;
2076
plougher17b269c2009-03-30 01:33:07 +00002077 if(memcmp(*block_list, dupl_ptr->block_list, blocks *
2078 sizeof(unsigned int)) != 0)
plougherfbf9f752007-08-12 05:02:24 +00002079 continue;
2080
plougher5507dd92006-11-06 00:43:10 +00002081 if(checksum_flag == FALSE) {
plougher17b269c2009-03-30 01:33:07 +00002082 checksum = get_checksum_disk(*start, bytes,
2083 *block_list);
plougher5507dd92006-11-06 00:43:10 +00002084 checksum_flag = TRUE;
2085 }
2086
Phillip Lougher557cefa2014-02-05 04:36:05 +00002087 if(!dupl_ptr->have_checksum) {
2088 dupl_ptr->checksum =
2089 get_checksum_disk(dupl_ptr->start,
2090 dupl_ptr->bytes, dupl_ptr->block_list);
2091 dupl_ptr->have_checksum = TRUE;
plougher5507dd92006-11-06 00:43:10 +00002092 }
2093
plougher17b269c2009-03-30 01:33:07 +00002094 if(checksum != dupl_ptr->checksum ||
2095 fragment_checksum !=
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002096 get_fragment_checksum(dupl_ptr))
plougher5507dd92006-11-06 00:43:10 +00002097 continue;
2098
plougher1b899fc2008-08-07 01:24:06 +00002099 target_start = *start;
plougher5507dd92006-11-06 00:43:10 +00002100 for(block = 0; block < blocks; block ++) {
plougher17b269c2009-03-30 01:33:07 +00002101 int size = SQUASHFS_COMPRESSED_SIZE_BLOCK
2102 ((*block_list)[block]);
plougher1b899fc2008-08-07 01:24:06 +00002103 struct file_buffer *target_buffer = NULL;
2104 struct file_buffer *dup_buffer = NULL;
2105 char *target_data, *dup_data;
plougher0f464442008-03-31 00:27:56 +00002106 int res;
plougher5507dd92006-11-06 00:43:10 +00002107
plougher1b899fc2008-08-07 01:24:06 +00002108 if(size == 0)
plougherfbf9f752007-08-12 05:02:24 +00002109 continue;
Phillip Lougher943acad2014-04-17 03:31:01 +01002110 target_buffer = cache_lookup(bwriter_buffer,
plougher17b269c2009-03-30 01:33:07 +00002111 target_start);
plougher1b899fc2008-08-07 01:24:06 +00002112 if(target_buffer)
2113 target_data = target_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01002114 else {
plougher50b31762009-03-31 04:14:46 +00002115 target_data =
2116 read_from_disk(target_start,
plougher17b269c2009-03-30 01:33:07 +00002117 size);
Phillip Lougherac731382013-05-19 04:19:44 +01002118 if(target_data == NULL) {
2119 ERROR("Failed to read data from"
2120 " output filesystem\n");
2121 BAD_ERROR("Output filesystem"
2122 " corrupted?\n");
2123 }
2124 }
plougher5507dd92006-11-06 00:43:10 +00002125
Phillip Lougher943acad2014-04-17 03:31:01 +01002126 dup_buffer = cache_lookup(bwriter_buffer,
plougher360514a2009-03-30 03:01:38 +00002127 dup_start);
plougher1b899fc2008-08-07 01:24:06 +00002128 if(dup_buffer)
2129 dup_data = dup_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01002130 else {
plougher360514a2009-03-30 03:01:38 +00002131 dup_data = read_from_disk2(dup_start,
2132 size);
Phillip Lougherac731382013-05-19 04:19:44 +01002133 if(dup_data == NULL) {
2134 ERROR("Failed to read data from"
2135 " output filesystem\n");
2136 BAD_ERROR("Output filesystem"
2137 " corrupted?\n");
2138 }
2139 }
plougher1b899fc2008-08-07 01:24:06 +00002140
2141 res = memcmp(target_data, dup_data, size);
2142 cache_block_put(target_buffer);
2143 cache_block_put(dup_buffer);
plougher0f464442008-03-31 00:27:56 +00002144 if(res != 0)
plougher5507dd92006-11-06 00:43:10 +00002145 break;
plougher1b899fc2008-08-07 01:24:06 +00002146 target_start += size;
2147 dup_start += size;
plougher5507dd92006-11-06 00:43:10 +00002148 }
2149 if(block == blocks) {
plougher17b269c2009-03-30 01:33:07 +00002150 struct file_buffer *frag_buffer =
2151 get_fragment(dupl_ptr->fragment);
plougher5507dd92006-11-06 00:43:10 +00002152
plougher17b269c2009-03-30 01:33:07 +00002153 if(frag_bytes == 0 ||
2154 memcmp(file_buffer->data,
2155 frag_buffer->data +
2156 dupl_ptr->fragment->offset,
2157 frag_bytes) == 0) {
plougher50b31762009-03-31 04:14:46 +00002158 TRACE("Found duplicate file, start "
2159 "0x%llx, size %lld, checksum "
2160 "0x%x, fragment %d, size %d, "
2161 "offset %d, checksum 0x%x\n",
2162 dupl_ptr->start,
plougher17b269c2009-03-30 01:33:07 +00002163 dupl_ptr->bytes,
2164 dupl_ptr->checksum,
2165 dupl_ptr->fragment->index,
2166 frag_bytes,
2167 dupl_ptr->fragment->offset,
2168 fragment_checksum);
plougher1f413c82005-11-18 00:02:14 +00002169 *block_list = dupl_ptr->block_list;
2170 *start = dupl_ptr->start;
2171 *fragment = dupl_ptr->fragment;
plougher76c64082008-03-08 01:32:23 +00002172 cache_block_put(frag_buffer);
plougher1f413c82005-11-18 00:02:14 +00002173 return 0;
2174 }
plougher76c64082008-03-08 01:32:23 +00002175 cache_block_put(frag_buffer);
plougher1f413c82005-11-18 00:02:14 +00002176 }
2177 }
2178
2179
plougher17b269c2009-03-30 01:33:07 +00002180 return add_non_dup(file_size, bytes, *block_list, *start, *fragment,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002181 checksum, fragment_checksum, checksum_flag, TRUE);
plougher1f413c82005-11-18 00:02:14 +00002182}
2183
2184
Colin Crossbe754902017-12-09 22:27:51 +00002185static inline int is_fragment(struct inode_info *inode)
Phillip Lougher2504b082011-09-07 02:28:45 +01002186{
Guan, Xin1eda7b82014-09-13 13:15:26 +02002187 off_t file_size = inode->buf.st_size;
Phillip Lougher2504b082011-09-07 02:28:45 +01002188
Phillip Lougher63f531f2011-09-10 04:03:32 +01002189 /*
2190 * If this block is to be compressed differently to the
2191 * fragment compression then it cannot be a fragment
2192 */
2193 if(inode->noF != noF)
2194 return FALSE;
2195
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002196 return !inode->no_fragments && file_size && (file_size < block_size ||
Phillip Lougher2504b082011-09-07 02:28:45 +01002197 (inode->always_use_fragments && file_size & (block_size - 1)));
2198}
2199
2200
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002201void put_file_buffer(struct file_buffer *file_buffer)
2202{
2203 /*
Phillip Lougher9de84ac2014-02-20 05:18:48 +00002204 * Decide where to send the file buffer:
2205 * - compressible non-fragment blocks go to the deflate threads,
2206 * - fragments go to the process fragment threads,
2207 * - all others go directly to the main thread
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002208 */
2209 if(file_buffer->error) {
2210 file_buffer->fragment = 0;
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002211 seq_queue_put(to_main, file_buffer);
Phillip Lougher9de84ac2014-02-20 05:18:48 +00002212 } else if (file_buffer->file_size == 0)
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002213 seq_queue_put(to_main, file_buffer);
Phillip Lougher9de84ac2014-02-20 05:18:48 +00002214 else if(file_buffer->fragment)
2215 queue_put(to_process_frag, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002216 else
Phillip Loughercf478e92013-05-29 02:38:41 +01002217 queue_put(to_deflate, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002218}
2219
2220
plougher00d08172009-09-03 10:17:44 +00002221static int seq = 0;
2222void reader_read_process(struct dir_ent *dir_ent)
2223{
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002224 long long bytes = 0;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002225 struct inode_info *inode = dir_ent->inode;
plougher00d08172009-09-03 10:17:44 +00002226 struct file_buffer *prev_buffer = NULL, *file_buffer;
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002227 int status, byte, res, child;
2228 int file = pseudo_exec_file(get_pseudo_file(inode->pseudo_id), &child);
2229
2230 if(!file) {
2231 file_buffer = cache_get_nohash(reader_buffer);
2232 file_buffer->sequence = seq ++;
2233 goto read_err;
2234 }
plougher00d08172009-09-03 10:17:44 +00002235
2236 while(1) {
Phillip Lougher316ab632013-04-29 02:53:39 +01002237 file_buffer = cache_get_nohash(reader_buffer);
plougher00d08172009-09-03 10:17:44 +00002238 file_buffer->sequence = seq ++;
Phillip Lougher63f531f2011-09-10 04:03:32 +01002239 file_buffer->noD = inode->noD;
plougher00d08172009-09-03 10:17:44 +00002240
2241 byte = read_bytes(file, file_buffer->data, block_size);
2242 if(byte == -1)
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002243 goto read_err2;
plougher00d08172009-09-03 10:17:44 +00002244
2245 file_buffer->size = byte;
2246 file_buffer->file_size = -1;
plougher00d08172009-09-03 10:17:44 +00002247 file_buffer->error = FALSE;
2248 file_buffer->fragment = FALSE;
2249 bytes += byte;
2250
2251 if(byte == 0)
2252 break;
2253
plougher286b6b32009-09-19 03:29:27 +00002254 /*
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002255 * Update progress bar size. This is done
plougher286b6b32009-09-19 03:29:27 +00002256 * on every block rather than waiting for all blocks to be
2257 * read incase write_file_process() is running in parallel
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002258 * with this. Otherwise the current progress bar position
2259 * may get ahead of the progress bar size.
plougher286b6b32009-09-19 03:29:27 +00002260 */
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002261 progress_bar_size(1);
plougher286b6b32009-09-19 03:29:27 +00002262
plougher00d08172009-09-03 10:17:44 +00002263 if(prev_buffer)
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002264 put_file_buffer(prev_buffer);
plougher00d08172009-09-03 10:17:44 +00002265 prev_buffer = file_buffer;
2266 }
2267
plougher54d67292009-09-19 03:26:27 +00002268 /*
2269 * Update inode file size now that the size of the dynamic pseudo file
2270 * is known. This is needed for the -info option.
2271 */
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002272 inode->buf.st_size = bytes;
plougher54d67292009-09-19 03:26:27 +00002273
plougherba674e82009-09-10 03:50:00 +00002274 res = waitpid(child, &status, 0);
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002275 close(file);
2276
plougherd87d8d12009-09-10 04:08:16 +00002277 if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
plougherba674e82009-09-10 03:50:00 +00002278 goto read_err;
2279
plougher00d08172009-09-03 10:17:44 +00002280 if(prev_buffer == NULL)
2281 prev_buffer = file_buffer;
2282 else {
2283 cache_block_put(file_buffer);
2284 seq --;
2285 }
2286 prev_buffer->file_size = bytes;
Phillip Lougher2504b082011-09-07 02:28:45 +01002287 prev_buffer->fragment = is_fragment(inode);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002288 put_file_buffer(prev_buffer);
plougher00d08172009-09-03 10:17:44 +00002289
2290 return;
2291
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002292read_err2:
2293 close(file);
plougher00d08172009-09-03 10:17:44 +00002294read_err:
2295 if(prev_buffer) {
2296 cache_block_put(file_buffer);
2297 seq --;
2298 file_buffer = prev_buffer;
2299 }
2300 file_buffer->error = TRUE;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002301 put_file_buffer(file_buffer);
plougher00d08172009-09-03 10:17:44 +00002302}
2303
2304
plougher5507dd92006-11-06 00:43:10 +00002305void reader_read_file(struct dir_ent *dir_ent)
plougher1f413c82005-11-18 00:02:14 +00002306{
plougher018d2b32007-04-23 03:01:48 +00002307 struct stat *buf = &dir_ent->inode->buf, buf2;
plougher5507dd92006-11-06 00:43:10 +00002308 struct file_buffer *file_buffer;
Phillip Lougherce119e72014-03-11 04:14:00 +00002309 int blocks, file, res;
plougher018d2b32007-04-23 03:01:48 +00002310 long long bytes, read_size;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002311 struct inode_info *inode = dir_ent->inode;
plougher5507dd92006-11-06 00:43:10 +00002312
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002313 if(inode->read)
plougher5507dd92006-11-06 00:43:10 +00002314 return;
2315
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002316 inode->read = TRUE;
plougher018d2b32007-04-23 03:01:48 +00002317again:
2318 bytes = 0;
plougher018d2b32007-04-23 03:01:48 +00002319 read_size = buf->st_size;
2320 blocks = (read_size + block_size - 1) >> block_log;
plougher018d2b32007-04-23 03:01:48 +00002321
Phillip Lougher494479f2012-02-03 15:45:41 +00002322 file = open(pathname_reader(dir_ent), O_RDONLY);
plougherc1d258e2010-12-16 05:16:45 +00002323 if(file == -1) {
Phillip Lougher316ab632013-04-29 02:53:39 +01002324 file_buffer = cache_get_nohash(reader_buffer);
plougher1e380702010-08-11 01:52:49 +00002325 file_buffer->sequence = seq ++;
Phillip Lougher2302f692012-12-27 04:05:02 +00002326 goto read_err2;
plougher1e380702010-08-11 01:52:49 +00002327 }
plougher5507dd92006-11-06 00:43:10 +00002328
plougher018d2b32007-04-23 03:01:48 +00002329 do {
Phillip Lougher316ab632013-04-29 02:53:39 +01002330 file_buffer = cache_get_nohash(reader_buffer);
Phillip Lougherce119e72014-03-11 04:14:00 +00002331 file_buffer->file_size = read_size;
plougher00d08172009-09-03 10:17:44 +00002332 file_buffer->sequence = seq ++;
Phillip Lougher63f531f2011-09-10 04:03:32 +01002333 file_buffer->noD = inode->noD;
Phillip Lougherce119e72014-03-11 04:14:00 +00002334 file_buffer->error = FALSE;
plougher5507dd92006-11-06 00:43:10 +00002335
ploughera4c24ed2010-08-11 02:08:38 +00002336 /*
2337 * Always try to read block_size bytes from the file rather
2338 * than expected bytes (which will be less than the block_size
2339 * at the file tail) to check that the file hasn't grown
2340 * since being stated. If it is longer (or shorter) than
2341 * expected, then restat, and try again. Note the special
2342 * case where the file is an exact multiple of the block_size
2343 * is dealt with later.
2344 */
Phillip Lougherce119e72014-03-11 04:14:00 +00002345 file_buffer->size = read_bytes(file, file_buffer->data,
plougher110799c2009-03-30 01:50:40 +00002346 block_size);
Phillip Lougherce119e72014-03-11 04:14:00 +00002347 if(file_buffer->size == -1)
ploughera4c24ed2010-08-11 02:08:38 +00002348 goto read_err;
2349
Phillip Lougherce119e72014-03-11 04:14:00 +00002350 bytes += file_buffer->size;
plougher018d2b32007-04-23 03:01:48 +00002351
Phillip Lougherce119e72014-03-11 04:14:00 +00002352 if(blocks > 1) {
2353 /* non-tail block should be exactly block_size */
2354 if(file_buffer->size < block_size)
2355 goto restat;
plougher018d2b32007-04-23 03:01:48 +00002356
Phillip Lougherce119e72014-03-11 04:14:00 +00002357 file_buffer->fragment = FALSE;
2358 put_file_buffer(file_buffer);
2359 }
Phillip Lougher8678d302014-03-02 03:02:19 +00002360 } while(-- blocks > 0);
plougher018d2b32007-04-23 03:01:48 +00002361
Phillip Lougherce119e72014-03-11 04:14:00 +00002362 /* Overall size including tail should match */
plougher018d2b32007-04-23 03:01:48 +00002363 if(read_size != bytes)
2364 goto restat;
2365
Phillip Lougher191dfcd2014-03-11 02:58:52 +00002366 if(read_size && read_size % block_size == 0) {
ploughera4c24ed2010-08-11 02:08:38 +00002367 /*
2368 * Special case where we've not tried to read past the end of
2369 * the file. We expect to get EOF, i.e. the file isn't larger
2370 * than we expect.
2371 */
plougher018d2b32007-04-23 03:01:48 +00002372 char buffer;
ploughera4c24ed2010-08-11 02:08:38 +00002373 int res;
plougher018d2b32007-04-23 03:01:48 +00002374
ploughera4c24ed2010-08-11 02:08:38 +00002375 res = read_bytes(file, &buffer, 1);
2376 if(res == -1)
2377 goto read_err;
2378
2379 if(res != 0)
plougher018d2b32007-04-23 03:01:48 +00002380 goto restat;
plougher5507dd92006-11-06 00:43:10 +00002381 }
2382
Phillip Lougher2504b082011-09-07 02:28:45 +01002383 file_buffer->fragment = is_fragment(inode);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002384 put_file_buffer(file_buffer);
plougher018d2b32007-04-23 03:01:48 +00002385
plougher5507dd92006-11-06 00:43:10 +00002386 close(file);
plougher5507dd92006-11-06 00:43:10 +00002387
2388 return;
2389
plougher018d2b32007-04-23 03:01:48 +00002390restat:
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002391 res = fstat(file, &buf2);
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002392 if(res == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00002393 ERROR("Cannot stat dir/file %s because %s\n",
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002394 pathname_reader(dir_ent), strerror(errno));
2395 goto read_err;
2396 }
2397
plougher018d2b32007-04-23 03:01:48 +00002398 if(read_size != buf2.st_size) {
Phillip Lougher2302f692012-12-27 04:05:02 +00002399 close(file);
plougher018d2b32007-04-23 03:01:48 +00002400 memcpy(buf, &buf2, sizeof(struct stat));
2401 file_buffer->error = 2;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002402 put_file_buffer(file_buffer);
plougher018d2b32007-04-23 03:01:48 +00002403 goto again;
2404 }
plougher1e380702010-08-11 01:52:49 +00002405read_err:
Phillip Lougher2302f692012-12-27 04:05:02 +00002406 close(file);
2407read_err2:
plougher1e380702010-08-11 01:52:49 +00002408 file_buffer->error = TRUE;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002409 put_file_buffer(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002410}
2411
2412
2413void reader_scan(struct dir_info *dir) {
Phillip Lougherbf338362012-08-22 05:24:36 +01002414 struct dir_ent *dir_ent = dir->list;
plougher5507dd92006-11-06 00:43:10 +00002415
Phillip Lougherbf338362012-08-22 05:24:36 +01002416 for(; dir_ent; dir_ent = dir_ent->next) {
plougher5507dd92006-11-06 00:43:10 +00002417 struct stat *buf = &dir_ent->inode->buf;
ploughera326c182009-08-29 05:41:45 +00002418 if(dir_ent->inode->root_entry)
plougher5507dd92006-11-06 00:43:10 +00002419 continue;
2420
plougherb3977eb2010-05-02 02:08:48 +00002421 if(IS_PSEUDO_PROCESS(dir_ent->inode)) {
plougher00d08172009-09-03 10:17:44 +00002422 reader_read_process(dir_ent);
2423 continue;
2424 }
2425
plougher5507dd92006-11-06 00:43:10 +00002426 switch(buf->st_mode & S_IFMT) {
2427 case S_IFREG:
2428 reader_read_file(dir_ent);
2429 break;
2430 case S_IFDIR:
2431 reader_scan(dir_ent->dir);
2432 break;
2433 }
2434 }
2435}
2436
2437
2438void *reader(void *arg)
2439{
plougher5507dd92006-11-06 00:43:10 +00002440 if(!sorted)
2441 reader_scan(queue_get(to_reader));
2442 else {
2443 int i;
2444 struct priority_entry *entry;
2445
2446 queue_get(to_reader);
2447 for(i = 65535; i >= 0; i--)
plougher16111452010-07-22 05:12:18 +00002448 for(entry = priority_list[i]; entry;
2449 entry = entry->next)
plougher5507dd92006-11-06 00:43:10 +00002450 reader_read_file(entry->dir);
2451 }
rloughere4873e02007-11-08 17:50:42 +00002452
2453 pthread_exit(NULL);
plougher5507dd92006-11-06 00:43:10 +00002454}
2455
2456
2457void *writer(void *arg)
2458{
plougher5507dd92006-11-06 00:43:10 +00002459 while(1) {
2460 struct file_buffer *file_buffer = queue_get(to_writer);
2461 off_t off;
2462
2463 if(file_buffer == NULL) {
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002464 queue_put(from_writer, NULL);
plougher5507dd92006-11-06 00:43:10 +00002465 continue;
2466 }
2467
2468 off = file_buffer->block;
2469
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002470 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
plougher5507dd92006-11-06 00:43:10 +00002471 pthread_mutex_lock(&pos_mutex);
2472
Colin Crossbe754902017-12-09 22:27:51 +00002473 if(lseek(fd, off, SEEK_SET) == -1) {
Phillip Lougherd23000c2013-02-06 22:05:45 +00002474 ERROR("writer: Lseek on destination failed because "
Colin Crossbe754902017-12-09 22:27:51 +00002475 "%s, offset=0x%llx\n", strerror(errno), off);
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01002476 BAD_ERROR("Probably out of space on output "
Phillip Lougher3f4ccd22013-02-11 04:40:09 +00002477 "%s\n", block_device ? "block device" :
2478 "filesystem");
plougher5507dd92006-11-06 00:43:10 +00002479 }
2480
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002481 if(write_bytes(fd, file_buffer->data,
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01002482 file_buffer->size) == -1)
2483 BAD_ERROR("Failed to write to output %s\n",
Phillip Loughere9df4552013-02-14 23:06:29 +00002484 block_device ? "block device" : "filesystem");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002485
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002486 pthread_cleanup_pop(1);
2487
ploughereb6eac92008-02-26 01:50:48 +00002488 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002489 }
2490}
2491
2492
plougher5b09fd42007-08-06 10:28:41 +00002493int all_zero(struct file_buffer *file_buffer)
2494{
2495 int i;
2496 long entries = file_buffer->size / sizeof(long);
2497 long *p = (long *) file_buffer->data;
2498
2499 for(i = 0; i < entries && p[i] == 0; i++);
2500
2501 if(i == entries) {
plougher110799c2009-03-30 01:50:40 +00002502 for(i = file_buffer->size & ~(sizeof(long) - 1);
plougher50b31762009-03-31 04:14:46 +00002503 i < file_buffer->size && file_buffer->data[i] == 0;
2504 i++);
plougher5b09fd42007-08-06 10:28:41 +00002505
2506 return i == file_buffer->size;
2507 }
2508
2509 return 0;
2510}
2511
2512
plougher5507dd92006-11-06 00:43:10 +00002513void *deflator(void *arg)
2514{
Phillip Lougher943acad2014-04-17 03:31:01 +01002515 struct file_buffer *write_buffer = cache_get_nohash(bwriter_buffer);
plougher7b8ee502009-07-29 07:54:30 +00002516 void *stream = NULL;
Phillip Lougher3c838292013-03-26 04:24:53 +00002517 int res;
plougher5507dd92006-11-06 00:43:10 +00002518
plougher13fdddf2010-11-24 01:23:41 +00002519 res = compressor_init(comp, &stream, block_size, 1);
2520 if(res)
2521 BAD_ERROR("deflator:: compressor_init failed\n");
2522
plougher5507dd92006-11-06 00:43:10 +00002523 while(1) {
Phillip Loughercf478e92013-05-29 02:38:41 +01002524 struct file_buffer *file_buffer = queue_get(to_deflate);
plougher5507dd92006-11-06 00:43:10 +00002525
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002526 if(sparse_files && all_zero(file_buffer)) {
Phillip Lougher48854382011-09-09 03:36:55 +01002527 file_buffer->c_byte = 0;
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002528 seq_queue_put(to_main, file_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002529 } else {
plougher13fdddf2010-11-24 01:23:41 +00002530 write_buffer->c_byte = mangle2(stream,
plougher50b31762009-03-31 04:14:46 +00002531 write_buffer->data, file_buffer->data,
Phillip Lougher63f531f2011-09-10 04:03:32 +01002532 file_buffer->size, block_size,
2533 file_buffer->noD, 1);
plougher1b899fc2008-08-07 01:24:06 +00002534 write_buffer->sequence = file_buffer->sequence;
2535 write_buffer->file_size = file_buffer->file_size;
2536 write_buffer->block = file_buffer->block;
plougher110799c2009-03-30 01:50:40 +00002537 write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK
2538 (write_buffer->c_byte);
plougher1b899fc2008-08-07 01:24:06 +00002539 write_buffer->fragment = FALSE;
2540 write_buffer->error = FALSE;
2541 cache_block_put(file_buffer);
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002542 seq_queue_put(to_main, write_buffer);
Phillip Lougher943acad2014-04-17 03:31:01 +01002543 write_buffer = cache_get_nohash(bwriter_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002544 }
plougher5507dd92006-11-06 00:43:10 +00002545 }
2546}
2547
2548
2549void *frag_deflator(void *arg)
2550{
plougher7b8ee502009-07-29 07:54:30 +00002551 void *stream = NULL;
Phillip Lougher3c838292013-03-26 04:24:53 +00002552 int res;
plougher5507dd92006-11-06 00:43:10 +00002553
plougher13fdddf2010-11-24 01:23:41 +00002554 res = compressor_init(comp, &stream, block_size, 1);
2555 if(res)
2556 BAD_ERROR("frag_deflator:: compressor_init failed\n");
2557
Phillip Lougher53240e62013-04-30 23:51:29 +01002558 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
2559
plougher5507dd92006-11-06 00:43:10 +00002560 while(1) {
2561 int c_byte, compressed_size;
2562 struct file_buffer *file_buffer = queue_get(to_frag);
plougher110799c2009-03-30 01:50:40 +00002563 struct file_buffer *write_buffer =
Phillip Lougher943acad2014-04-17 03:31:01 +01002564 cache_get(fwriter_buffer, file_buffer->block);
plougher5507dd92006-11-06 00:43:10 +00002565
plougher13fdddf2010-11-24 01:23:41 +00002566 c_byte = mangle2(stream, write_buffer->data, file_buffer->data,
plougher110799c2009-03-30 01:50:40 +00002567 file_buffer->size, block_size, noF, 1);
plougher5507dd92006-11-06 00:43:10 +00002568 compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
plougherd036a312008-03-08 01:38:27 +00002569 write_buffer->size = compressed_size;
plougherd1139d52008-04-28 03:07:07 +00002570 pthread_mutex_lock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00002571 if(fragments_locked == FALSE) {
plougher2ea89142008-03-11 01:34:19 +00002572 fragment_table[file_buffer->block].size = c_byte;
2573 fragment_table[file_buffer->block].start_block = bytes;
2574 write_buffer->block = bytes;
2575 bytes += compressed_size;
2576 fragments_outstanding --;
plougher2ea89142008-03-11 01:34:19 +00002577 queue_put(to_writer, write_buffer);
Phillip Lougher1ec844d2014-09-18 01:28:11 +01002578 pthread_mutex_unlock(&fragment_mutex);
plougher110799c2009-03-30 01:50:40 +00002579 TRACE("Writing fragment %lld, uncompressed size %d, "
2580 "compressed size %d\n", file_buffer->block,
2581 file_buffer->size, compressed_size);
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002582 } else {
plougher110799c2009-03-30 01:50:40 +00002583 add_pending_fragment(write_buffer, c_byte,
2584 file_buffer->block);
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002585 pthread_mutex_unlock(&fragment_mutex);
2586 }
ploughereb6eac92008-02-26 01:50:48 +00002587 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002588 }
Phillip Lougher53240e62013-04-30 23:51:29 +01002589
2590 pthread_cleanup_pop(0);
plougher5507dd92006-11-06 00:43:10 +00002591}
2592
2593
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002594struct file_buffer *get_file_buffer()
plougher5507dd92006-11-06 00:43:10 +00002595{
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002596 struct file_buffer *file_buffer = seq_queue_get(to_main);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002597
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002598 return file_buffer;
plougher5507dd92006-11-06 00:43:10 +00002599}
2600
2601
plougher110799c2009-03-30 01:50:40 +00002602void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent,
Phillip Lougher948bf3a2014-03-02 02:23:54 +00002603 struct file_buffer *file_buffer, int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002604{
2605 file_count ++;
2606 *duplicate_file = FALSE;
Phillip Lougher948bf3a2014-03-02 02:23:54 +00002607 cache_block_put(file_buffer);
plougherc1ace522010-05-01 03:00:45 +00002608 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0,
2609 NULL, &empty_fragment, NULL, 0);
plougher5507dd92006-11-06 00:43:10 +00002610}
2611
2612
Phillip Lougher8531f872014-03-01 03:22:41 +00002613void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent,
plougher110799c2009-03-30 01:50:40 +00002614 struct file_buffer *file_buffer, int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002615{
Phillip Lougher8531f872014-03-01 03:22:41 +00002616 int size = file_buffer->file_size;
plougher5507dd92006-11-06 00:43:10 +00002617 struct fragment *fragment;
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002618 unsigned short checksum = file_buffer->checksum;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002619 char dont_put;
plougher5507dd92006-11-06 00:43:10 +00002620
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002621 fragment = frag_duplicate(file_buffer, &dont_put);
2622 *duplicate_file = !fragment;
2623 if(!fragment) {
2624 fragment = get_and_fill_fragment(file_buffer, dir_ent);
2625 if(duplicate_checking)
2626 add_non_dup(size, 0, NULL, 0, fragment, 0, checksum,
2627 TRUE, TRUE);
plougher29e37092007-04-15 01:24:51 +00002628 }
plougher5507dd92006-11-06 00:43:10 +00002629
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002630 if(dont_put)
2631 free(file_buffer);
2632 else
2633 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002634
2635 total_bytes += size;
2636 file_count ++;
2637
plougher35a10602008-04-21 02:58:16 +00002638 inc_progress_bar();
plougher02bc3bc2007-02-25 12:12:01 +00002639
plougherc1ace522010-05-01 03:00:45 +00002640 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0,
plougher3c6bdb52010-05-01 02:30:59 +00002641 0, NULL, fragment, NULL, 0);
plougher29e37092007-04-15 01:24:51 +00002642
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002643 if(!duplicate_checking)
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002644 free_fragment(fragment);
plougher5507dd92006-11-06 00:43:10 +00002645}
2646
2647
plougher00d08172009-09-03 10:17:44 +00002648int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent,
2649 struct file_buffer *read_buffer, int *duplicate_file)
2650{
2651 long long read_size, file_bytes, start;
2652 struct fragment *fragment;
2653 unsigned int *block_list = NULL;
2654 int block = 0, status;
2655 long long sparse = 0;
2656 struct file_buffer *fragment_buffer = NULL;
2657
2658 *duplicate_file = FALSE;
2659
2660 lock_fragments();
2661
2662 file_bytes = 0;
2663 start = bytes;
2664 while (1) {
2665 read_size = read_buffer->file_size;
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002666 if(read_buffer->fragment)
plougher00d08172009-09-03 10:17:44 +00002667 fragment_buffer = read_buffer;
2668 else {
2669 block_list = realloc(block_list, (block + 1) *
2670 sizeof(unsigned int));
2671 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002672 MEM_ERROR();
plougher00d08172009-09-03 10:17:44 +00002673 block_list[block ++] = read_buffer->c_byte;
2674 if(read_buffer->c_byte) {
2675 read_buffer->block = bytes;
2676 bytes += read_buffer->size;
Phillip Lougher84e20d72014-02-23 05:23:41 +00002677 cache_hash(read_buffer, read_buffer->block);
plougher00d08172009-09-03 10:17:44 +00002678 file_bytes += read_buffer->size;
2679 queue_put(to_writer, read_buffer);
2680 } else {
2681 sparse += read_buffer->size;
2682 cache_block_put(read_buffer);
2683 }
2684 }
plougher286b6b32009-09-19 03:29:27 +00002685 inc_progress_bar();
plougher00d08172009-09-03 10:17:44 +00002686
2687 if(read_size != -1)
2688 break;
2689
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002690 read_buffer = get_file_buffer();
plougher00d08172009-09-03 10:17:44 +00002691 if(read_buffer->error)
2692 goto read_err;
2693 }
2694
2695 unlock_fragments();
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002696 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher00d08172009-09-03 10:17:44 +00002697
2698 if(duplicate_checking)
2699 add_non_dup(read_size, file_bytes, block_list, start, fragment,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002700 0, fragment_buffer ? fragment_buffer->checksum : 0,
2701 FALSE, TRUE);
2702 cache_block_put(fragment_buffer);
plougher00d08172009-09-03 10:17:44 +00002703 file_count ++;
2704 total_bytes += read_size;
2705
plougherc1ace522010-05-01 03:00:45 +00002706 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2707 block, block_list, fragment, NULL, sparse);
plougher00d08172009-09-03 10:17:44 +00002708
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002709 if(duplicate_checking == FALSE) {
plougher00d08172009-09-03 10:17:44 +00002710 free(block_list);
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002711 free_fragment(fragment);
2712 }
plougher00d08172009-09-03 10:17:44 +00002713
2714 return 0;
2715
2716read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002717 dec_progress_bar(block);
plougher00d08172009-09-03 10:17:44 +00002718 status = read_buffer->error;
2719 bytes = start;
2720 if(!block_device) {
2721 int res;
2722
2723 queue_put(to_writer, NULL);
2724 if(queue_get(from_writer) != 0)
2725 EXIT_MKSQUASHFS();
2726 res = ftruncate(fd, bytes);
2727 if(res != 0)
2728 BAD_ERROR("Failed to truncate dest file because %s\n",
2729 strerror(errno));
2730 }
2731 unlock_fragments();
2732 free(block_list);
2733 cache_block_put(read_buffer);
2734 return status;
2735}
2736
2737
plougher110799c2009-03-30 01:50:40 +00002738int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent,
Phillip Lougher1807caa2014-03-01 03:28:33 +00002739 struct file_buffer *read_buffer, int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002740{
plougher29e37092007-04-15 01:24:51 +00002741 int block, thresh;
Phillip Lougher1807caa2014-03-01 03:28:33 +00002742 long long read_size = read_buffer->file_size;
plougher1b899fc2008-08-07 01:24:06 +00002743 long long file_bytes, dup_start, start;
plougher5507dd92006-11-06 00:43:10 +00002744 struct fragment *fragment;
2745 struct file_info *dupl_ptr;
2746 int blocks = (read_size + block_size - 1) >> block_log;
2747 unsigned int *block_list, *block_listp;
plougher1b899fc2008-08-07 01:24:06 +00002748 struct file_buffer **buffer_list;
Phillip Lougher943acad2014-04-17 03:31:01 +01002749 int status;
plougher1b899fc2008-08-07 01:24:06 +00002750 long long sparse = 0;
2751 struct file_buffer *fragment_buffer = NULL;
plougher5507dd92006-11-06 00:43:10 +00002752
plougher50b31762009-03-31 04:14:46 +00002753 block_list = malloc(blocks * sizeof(unsigned int));
2754 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002755 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00002756 block_listp = block_list;
2757
plougher50b31762009-03-31 04:14:46 +00002758 buffer_list = malloc(blocks * sizeof(struct file_buffer *));
2759 if(buffer_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002760 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00002761
Phillip Lougher943acad2014-04-17 03:31:01 +01002762 lock_fragments();
plougher5507dd92006-11-06 00:43:10 +00002763
2764 file_bytes = 0;
plougher1b899fc2008-08-07 01:24:06 +00002765 start = dup_start = bytes;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01002766 thresh = blocks > bwriter_size ? blocks - bwriter_size : 0;
plougher1b899fc2008-08-07 01:24:06 +00002767
2768 for(block = 0; block < blocks;) {
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002769 if(read_buffer->fragment) {
Phillip Lougher38cb1ab2013-01-13 21:20:09 +00002770 block_list[block] = 0;
Phillip Lougher19de5b62013-01-13 21:31:51 +00002771 buffer_list[block] = NULL;
plougher1b899fc2008-08-07 01:24:06 +00002772 fragment_buffer = read_buffer;
2773 blocks = read_size >> block_log;
2774 } else {
2775 block_list[block] = read_buffer->c_byte;
2776
2777 if(read_buffer->c_byte) {
2778 read_buffer->block = bytes;
2779 bytes += read_buffer->size;
2780 file_bytes += read_buffer->size;
Phillip Lougher84e20d72014-02-23 05:23:41 +00002781 cache_hash(read_buffer, read_buffer->block);
plougher1b899fc2008-08-07 01:24:06 +00002782 if(block < thresh) {
2783 buffer_list[block] = NULL;
2784 queue_put(to_writer, read_buffer);
2785 } else
2786 buffer_list[block] = read_buffer;
2787 } else {
2788 buffer_list[block] = NULL;
2789 sparse += read_buffer->size;
2790 cache_block_put(read_buffer);
2791 }
2792 }
2793 inc_progress_bar();
2794
2795 if(++block < blocks) {
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002796 read_buffer = get_file_buffer();
plougher018d2b32007-04-23 03:01:48 +00002797 if(read_buffer->error)
2798 goto read_err;
2799 }
plougher5507dd92006-11-06 00:43:10 +00002800 }
2801
plougher110799c2009-03-30 01:50:40 +00002802 dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002803 &fragment, fragment_buffer, blocks, 0, FALSE);
plougher5507dd92006-11-06 00:43:10 +00002804
2805 if(dupl_ptr) {
2806 *duplicate_file = FALSE;
2807 for(block = thresh; block < blocks; block ++)
plougher1b899fc2008-08-07 01:24:06 +00002808 if(buffer_list[block])
2809 queue_put(to_writer, buffer_list[block]);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002810 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher5507dd92006-11-06 00:43:10 +00002811 dupl_ptr->fragment = fragment;
2812 } else {
2813 *duplicate_file = TRUE;
2814 for(block = thresh; block < blocks; block ++)
plougher1b899fc2008-08-07 01:24:06 +00002815 cache_block_put(buffer_list[block]);
2816 bytes = start;
2817 if(thresh && !block_device) {
plougher12a159a2009-03-03 11:06:34 +00002818 int res;
2819
plougher1b899fc2008-08-07 01:24:06 +00002820 queue_put(to_writer, NULL);
2821 if(queue_get(from_writer) != 0)
2822 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002823 res = ftruncate(fd, bytes);
2824 if(res != 0)
2825 BAD_ERROR("Failed to truncate dest file because"
2826 " %s\n", strerror(errno));
plougher1b899fc2008-08-07 01:24:06 +00002827 }
plougher5507dd92006-11-06 00:43:10 +00002828 }
2829
plougher2ea89142008-03-11 01:34:19 +00002830 unlock_fragments();
plougher1b899fc2008-08-07 01:24:06 +00002831 cache_block_put(fragment_buffer);
plougher5507dd92006-11-06 00:43:10 +00002832 free(buffer_list);
2833 file_count ++;
2834 total_bytes += read_size;
2835
plougher360514a2009-03-30 03:01:38 +00002836 /*
2837 * sparse count is needed to ensure squashfs correctly reports a
plougher1b899fc2008-08-07 01:24:06 +00002838 * a smaller block count on stat calls to sparse files. This is
2839 * to ensure intelligent applications like cp correctly handle the
2840 * file as a sparse file. If the file in the original filesystem isn't
2841 * stored as a sparse file then still store it sparsely in squashfs, but
plougher360514a2009-03-30 03:01:38 +00002842 * report it as non-sparse on stat calls to preserve semantics
2843 */
plougher1b899fc2008-08-07 01:24:06 +00002844 if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2845 sparse = 0;
2846
plougherc1ace522010-05-01 03:00:45 +00002847 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size,
2848 dup_start, blocks, block_listp, fragment, NULL, sparse);
plougher29e37092007-04-15 01:24:51 +00002849
plougher5507dd92006-11-06 00:43:10 +00002850 if(*duplicate_file == TRUE)
2851 free(block_list);
plougher29e37092007-04-15 01:24:51 +00002852
plougher018d2b32007-04-23 03:01:48 +00002853 return 0;
plougher5507dd92006-11-06 00:43:10 +00002854
2855read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002856 dec_progress_bar(block);
plougher018d2b32007-04-23 03:01:48 +00002857 status = read_buffer->error;
plougher1b899fc2008-08-07 01:24:06 +00002858 bytes = start;
2859 if(thresh && !block_device) {
plougher12a159a2009-03-03 11:06:34 +00002860 int res;
2861
plougher5507dd92006-11-06 00:43:10 +00002862 queue_put(to_writer, NULL);
2863 if(queue_get(from_writer) != 0)
2864 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002865 res = ftruncate(fd, bytes);
2866 if(res != 0)
2867 BAD_ERROR("Failed to truncate dest file because %s\n",
2868 strerror(errno));
plougher5507dd92006-11-06 00:43:10 +00002869 }
plougher2ea89142008-03-11 01:34:19 +00002870 unlock_fragments();
plougher5507dd92006-11-06 00:43:10 +00002871 for(blocks = thresh; blocks < block; blocks ++)
plougher1b899fc2008-08-07 01:24:06 +00002872 cache_block_put(buffer_list[blocks]);
plougher5507dd92006-11-06 00:43:10 +00002873 free(buffer_list);
2874 free(block_list);
ploughereb6eac92008-02-26 01:50:48 +00002875 cache_block_put(read_buffer);
plougher018d2b32007-04-23 03:01:48 +00002876 return status;
plougher5507dd92006-11-06 00:43:10 +00002877}
2878
2879
Phillip Lougher75bd21d2014-03-02 03:33:56 +00002880int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent,
2881 struct file_buffer *read_buffer, int *dup)
2882{
2883 long long read_size = read_buffer->file_size;
2884 long long file_bytes, start;
2885 struct fragment *fragment;
2886 unsigned int *block_list;
2887 int block, status;
2888 int blocks = (read_size + block_size - 1) >> block_log;
2889 long long sparse = 0;
2890 struct file_buffer *fragment_buffer = NULL;
2891
2892 if(pre_duplicate(read_size))
2893 return write_file_blocks_dup(inode, dir_ent, read_buffer, dup);
2894
2895 *dup = FALSE;
2896
2897 block_list = malloc(blocks * sizeof(unsigned int));
2898 if(block_list == NULL)
2899 MEM_ERROR();
2900
2901 lock_fragments();
2902
2903 file_bytes = 0;
Mohamad Ayyash305fca62016-06-13 15:44:16 -07002904/* ANDROID CHANGES START*/
2905#ifdef ANDROID
2906 if (align_4k_blocks && bytes % 4096) {
2907 bytes += 4096 - (bytes % 4096);
2908 }
2909#endif
2910/* ANDROID CHANGES END */
Phillip Lougher75bd21d2014-03-02 03:33:56 +00002911 start = bytes;
2912 for(block = 0; block < blocks;) {
2913 if(read_buffer->fragment) {
2914 block_list[block] = 0;
2915 fragment_buffer = read_buffer;
2916 blocks = read_size >> block_log;
2917 } else {
2918 block_list[block] = read_buffer->c_byte;
2919 if(read_buffer->c_byte) {
2920 read_buffer->block = bytes;
2921 bytes += read_buffer->size;
2922 cache_hash(read_buffer, read_buffer->block);
2923 file_bytes += read_buffer->size;
2924 queue_put(to_writer, read_buffer);
2925 } else {
2926 sparse += read_buffer->size;
2927 cache_block_put(read_buffer);
2928 }
2929 }
2930 inc_progress_bar();
2931
2932 if(++block < blocks) {
2933 read_buffer = get_file_buffer();
2934 if(read_buffer->error)
2935 goto read_err;
2936 }
2937 }
2938
2939 unlock_fragments();
2940 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
Phillip Lougher75bd21d2014-03-02 03:33:56 +00002941
2942 if(duplicate_checking)
2943 add_non_dup(read_size, file_bytes, block_list, start, fragment,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002944 0, fragment_buffer ? fragment_buffer->checksum : 0,
2945 FALSE, TRUE);
2946 cache_block_put(fragment_buffer);
Phillip Lougher75bd21d2014-03-02 03:33:56 +00002947 file_count ++;
2948 total_bytes += read_size;
2949
2950 /*
2951 * sparse count is needed to ensure squashfs correctly reports a
2952 * a smaller block count on stat calls to sparse files. This is
2953 * to ensure intelligent applications like cp correctly handle the
2954 * file as a sparse file. If the file in the original filesystem isn't
2955 * stored as a sparse file then still store it sparsely in squashfs, but
2956 * report it as non-sparse on stat calls to preserve semantics
2957 */
2958 if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2959 sparse = 0;
2960
2961 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2962 blocks, block_list, fragment, NULL, sparse);
2963
2964 if(duplicate_checking == FALSE) {
2965 free(block_list);
2966 free_fragment(fragment);
2967 }
2968
2969 return 0;
2970
2971read_err:
2972 dec_progress_bar(block);
2973 status = read_buffer->error;
2974 bytes = start;
2975 if(!block_device) {
2976 int res;
2977
2978 queue_put(to_writer, NULL);
2979 if(queue_get(from_writer) != 0)
2980 EXIT_MKSQUASHFS();
2981 res = ftruncate(fd, bytes);
2982 if(res != 0)
2983 BAD_ERROR("Failed to truncate dest file because %s\n",
2984 strerror(errno));
2985 }
2986 unlock_fragments();
2987 free(block_list);
2988 cache_block_put(read_buffer);
2989 return status;
2990}
2991
2992
Phillip Lougherc3489132014-03-02 01:41:36 +00002993void write_file(squashfs_inode *inode, struct dir_ent *dir, int *dup)
plougher5507dd92006-11-06 00:43:10 +00002994{
plougher018d2b32007-04-23 03:01:48 +00002995 int status;
2996 struct file_buffer *read_buffer;
plougher5507dd92006-11-06 00:43:10 +00002997
plougher018d2b32007-04-23 03:01:48 +00002998again:
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002999 read_buffer = get_file_buffer();
Phillip Lougherc3489132014-03-02 01:41:36 +00003000 status = read_buffer->error;
plougher1b899fc2008-08-07 01:24:06 +00003001
Phillip Lougherc3489132014-03-02 01:41:36 +00003002 if(status)
ploughereb6eac92008-02-26 01:50:48 +00003003 cache_block_put(read_buffer);
Phillip Lougherc3489132014-03-02 01:41:36 +00003004 else if(read_buffer->file_size == -1)
3005 status = write_file_process(inode, dir, read_buffer, dup);
Phillip Lougher948bf3a2014-03-02 02:23:54 +00003006 else if(read_buffer->file_size == 0)
3007 write_file_empty(inode, dir, read_buffer, dup);
3008 else if(read_buffer->fragment && read_buffer->c_byte)
Phillip Lougherc3489132014-03-02 01:41:36 +00003009 write_file_frag(inode, dir, read_buffer, dup);
plougher29e37092007-04-15 01:24:51 +00003010 else
Phillip Lougherc3489132014-03-02 01:41:36 +00003011 status = write_file_blocks(inode, dir, read_buffer, dup);
plougher5507dd92006-11-06 00:43:10 +00003012
plougher018d2b32007-04-23 03:01:48 +00003013 if(status == 2) {
plougher50b31762009-03-31 04:14:46 +00003014 ERROR("File %s changed size while reading filesystem, "
Phillip Lougherc3489132014-03-02 01:41:36 +00003015 "attempting to re-read\n", pathname(dir));
plougher018d2b32007-04-23 03:01:48 +00003016 goto again;
3017 } else if(status == 1) {
Phillip Lougherc3489132014-03-02 01:41:36 +00003018 ERROR_START("Failed to read file %s", pathname(dir));
Phillip Lougher85776df2014-01-31 03:10:46 +00003019 ERROR_EXIT(", creating empty file\n");
Phillip Lougher948bf3a2014-03-02 02:23:54 +00003020 write_file_empty(inode, dir, NULL, dup);
plougher29e37092007-04-15 01:24:51 +00003021 }
plougher5507dd92006-11-06 00:43:10 +00003022}
3023
3024
Phillip Lougher3f81a772012-12-04 05:07:24 +00003025#define BUFF_SIZE 512
plougher1f413c82005-11-18 00:02:14 +00003026char *name;
3027char *basename_r();
3028
3029char *getbase(char *pathname)
3030{
Phillip Lougher3f81a772012-12-04 05:07:24 +00003031 static char *b_buffer = NULL;
3032 static int b_size = BUFF_SIZE;
plougher1f413c82005-11-18 00:02:14 +00003033 char *result;
3034
Phillip Lougher3f81a772012-12-04 05:07:24 +00003035 if(b_buffer == NULL) {
3036 b_buffer = malloc(b_size);
3037 if(b_buffer == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003038 MEM_ERROR();
Phillip Lougher3f81a772012-12-04 05:07:24 +00003039 }
3040
3041 while(1) {
3042 if(*pathname != '/') {
3043 result = getcwd(b_buffer, b_size);
3044 if(result == NULL && errno != ERANGE)
3045 BAD_ERROR("Getcwd failed in getbase\n");
3046
3047 /* enough room for pathname + "/" + '\0' terminator? */
3048 if(result && strlen(pathname) + 2 <=
3049 b_size - strlen(b_buffer)) {
3050 strcat(strcat(b_buffer, "/"), pathname);
3051 break;
3052 }
3053 } else if(strlen(pathname) < b_size) {
3054 strcpy(b_buffer, pathname);
3055 break;
3056 }
3057
3058 /* Buffer not large enough, realloc and try again */
3059 b_buffer = realloc(b_buffer, b_size += BUFF_SIZE);
3060 if(b_buffer == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003061 MEM_ERROR();
Phillip Lougher3f81a772012-12-04 05:07:24 +00003062 }
3063
plougher1f413c82005-11-18 00:02:14 +00003064 name = b_buffer;
3065 if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0))
3066 return NULL;
3067 else
3068 return result;
3069}
3070
3071
3072char *basename_r()
3073{
3074 char *s;
3075 char *p;
3076 int n = 1;
3077
3078 for(;;) {
3079 s = name;
3080 if(*name == '\0')
3081 return NULL;
3082 if(*name != '/') {
3083 while(*name != '\0' && *name != '/') name++;
3084 n = name - s;
3085 }
3086 while(*name == '/') name++;
3087 if(strncmp(s, ".", n) == 0)
3088 continue;
plougher110799c2009-03-30 01:50:40 +00003089 if((*name == '\0') || (strncmp(s, "..", n) == 0) ||
3090 ((p = basename_r()) == NULL)) {
plougher1f413c82005-11-18 00:02:14 +00003091 s[n] = '\0';
3092 return s;
3093 }
3094 if(strcmp(p, "..") == 0)
3095 continue;
3096 return p;
3097 }
3098}
3099
3100
Phillip Loughere19ce452014-07-27 04:16:54 +01003101struct inode_info *lookup_inode3(struct stat *buf, int pseudo, int id,
3102 char *symlink, int bytes)
plougher1f413c82005-11-18 00:02:14 +00003103{
Phillip Lougherd6577802012-10-04 19:14:33 +01003104 int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino);
3105 struct inode_info *inode;
plougher1f413c82005-11-18 00:02:14 +00003106
Phillip Lougherd6577802012-10-04 19:14:33 +01003107 /*
3108 * Look-up inode in hash table, if it already exists we have a
3109 * hard-link, so increment the nlink count and return it.
3110 * Don't do the look-up for directories because we don't hard-link
3111 * directories.
3112 */
3113 if ((buf->st_mode & S_IFMT) != S_IFDIR) {
3114 for(inode = inode_info[ino_hash]; inode; inode = inode->next) {
3115 if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) {
3116 inode->nlink ++;
3117 return inode;
3118 }
plougher1f413c82005-11-18 00:02:14 +00003119 }
plougher1f413c82005-11-18 00:02:14 +00003120 }
3121
Phillip Loughere19ce452014-07-27 04:16:54 +01003122 inode = malloc(sizeof(struct inode_info) + bytes);
plougher288f6852010-12-16 05:22:55 +00003123 if(inode == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003124 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00003125
Phillip Loughere19ce452014-07-27 04:16:54 +01003126 if(bytes)
3127 memcpy(&inode->symlink, symlink, bytes);
plougher1f413c82005-11-18 00:02:14 +00003128 memcpy(&inode->buf, buf, sizeof(struct stat));
plougher5507dd92006-11-06 00:43:10 +00003129 inode->read = FALSE;
ploughera326c182009-08-29 05:41:45 +00003130 inode->root_entry = FALSE;
Phillip Lougher81204c22012-07-25 03:30:30 +01003131 inode->pseudo_file = pseudo;
3132 inode->pseudo_id = id;
plougher1f413c82005-11-18 00:02:14 +00003133 inode->inode = SQUASHFS_INVALID_BLK;
3134 inode->nlink = 1;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003135 inode->inode_number = 0;
plougherdc86c3c2007-12-05 02:15:10 +00003136
Phillip Lougher9b6e3412011-09-05 02:58:41 +01003137 /*
3138 * Copy filesystem wide defaults into inode, these filesystem
3139 * wide defaults may be altered on an individual inode basis by
3140 * user specified actions
3141 *
3142 */
3143 inode->no_fragments = no_fragments;
3144 inode->always_use_fragments = always_use_fragments;
Mohan Srinivasan58e18d72016-07-26 15:06:29 -07003145
3146/* ANDROID CHANGES START*/
3147#ifdef ANDROID
3148 /* Check the whitelist */
3149 inode->noD = whitelisted(buf);
3150#else
Phillip Lougher63f531f2011-09-10 04:03:32 +01003151 inode->noD = noD;
Mohan Srinivasan58e18d72016-07-26 15:06:29 -07003152#endif
3153/* ANDROID CHANGES END */
3154
Phillip Lougher63f531f2011-09-10 04:03:32 +01003155 inode->noF = noF;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01003156
Phillip Lougherd6577802012-10-04 19:14:33 +01003157 inode->next = inode_info[ino_hash];
3158 inode_info[ino_hash] = inode;
plougher1f413c82005-11-18 00:02:14 +00003159
3160 return inode;
3161}
3162
3163
Colin Crossbe754902017-12-09 22:27:51 +00003164static inline struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id)
Phillip Loughere19ce452014-07-27 04:16:54 +01003165{
3166 return lookup_inode3(buf, pseudo, id, NULL, 0);
3167}
3168
3169
Colin Crossbe754902017-12-09 22:27:51 +00003170static inline struct inode_info *lookup_inode(struct stat *buf)
Phillip Lougher81204c22012-07-25 03:30:30 +01003171{
3172 return lookup_inode2(buf, 0, 0);
3173}
3174
3175
Colin Crossbe754902017-12-09 22:27:51 +00003176static inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this)
Phillip Lougher539c2b12012-07-30 20:14:52 +01003177{
Phillip Lougher070c0f72014-07-27 01:03:43 +01003178 if (inode->inode_number == 0) {
Phillip Lougher539c2b12012-07-30 20:14:52 +01003179 inode->inode_number = use_this ? : inode_no ++;
Phillip Lougher070c0f72014-07-27 01:03:43 +01003180 if((inode->buf.st_mode & S_IFMT) == S_IFREG)
3181 progress_bar_size((inode->buf.st_size + block_size - 1)
3182 >> block_log);
3183 }
Phillip Lougher539c2b12012-07-30 20:14:52 +01003184}
3185
3186
Colin Crossbe754902017-12-09 22:27:51 +00003187static inline struct dir_ent *create_dir_entry(char *name, char *source_name,
Phillip Lougher494479f2012-02-03 15:45:41 +00003188 char *nonstandard_pathname, struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003189{
Phillip Lougher494479f2012-02-03 15:45:41 +00003190 struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent));
3191 if(dir_ent == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003192 MEM_ERROR();
Phillip Lougher494479f2012-02-03 15:45:41 +00003193
3194 dir_ent->name = name;
3195 dir_ent->source_name = source_name;
3196 dir_ent->nonstandard_pathname = nonstandard_pathname;
3197 dir_ent->our_dir = dir;
Phillip Lougher070c0f72014-07-27 01:03:43 +01003198 dir_ent->inode = NULL;
Phillip Lougherbf338362012-08-22 05:24:36 +01003199 dir_ent->next = NULL;
Mohamad Ayyash531e36a2016-03-09 16:10:54 -08003200/* ANDROID CHANGES START*/
3201#ifdef ANDROID
3202 dir_ent->capabilities = 0;
3203#endif
3204/* ANDROID CHANGES END */
Phillip Lougher494479f2012-02-03 15:45:41 +00003205
3206 return dir_ent;
3207}
3208
3209
Mohamad Ayyash93302632016-03-31 18:12:57 -07003210static inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir,
Phillip Lougher494479f2012-02-03 15:45:41 +00003211 struct inode_info *inode_info)
3212{
3213 struct dir_info *dir = dir_ent->our_dir;
3214
plougher1f413c82005-11-18 00:02:14 +00003215 if(sub_dir)
Phillip Lougher494479f2012-02-03 15:45:41 +00003216 sub_dir->dir_ent = dir_ent;
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -08003217
3218/* ANDROID CHANGES START*/
3219#ifdef ANDROID
Mohamad Ayyash18fe5f62015-03-02 16:00:56 -08003220 if (android_config) {
3221 if (mount_point) {
3222 char *mounted_path;
Todd Poynor65ced842015-12-16 20:05:24 -08003223 char *rel_path;
3224
Mohamad Ayyash18fe5f62015-03-02 16:00:56 -08003225 alloc_mounted_path(mount_point, subpathname(dir_ent), &mounted_path);
Todd Poynor65ced842015-12-16 20:05:24 -08003226 rel_path = mounted_path;
3227 while (rel_path && *rel_path == '/')
3228 rel_path++;
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -07003229 android_fs_config(fs_config_func, rel_path, &inode_info->buf, target_out_path, &dir_ent->capabilities);
Mohamad Ayyash18fe5f62015-03-02 16:00:56 -08003230 free(mounted_path);
3231 } else {
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -07003232 android_fs_config(fs_config_func, pathname(dir_ent), &inode_info->buf, target_out_path, &dir_ent->capabilities);
Mohamad Ayyash18fe5f62015-03-02 16:00:56 -08003233 }
3234 }
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -08003235#endif
3236/* ANDROID CHANGES END */
3237
Phillip Lougher494479f2012-02-03 15:45:41 +00003238 dir_ent->inode = inode_info;
3239 dir_ent->dir = sub_dir;
3240
Phillip Lougherbf338362012-08-22 05:24:36 +01003241 dir_ent->next = dir->list;
3242 dir->list = dir_ent;
3243 dir->count++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003244}
3245
Colin Crossbe754902017-12-09 22:27:51 +00003246static inline void add_dir_entry2(char *name, char *source_name,
Phillip Lougher494479f2012-02-03 15:45:41 +00003247 char *nonstandard_pathname, struct dir_info *sub_dir,
3248 struct inode_info *inode_info, struct dir_info *dir)
3249{
3250 struct dir_ent *dir_ent = create_dir_entry(name, source_name,
3251 nonstandard_pathname, dir);
3252
3253
3254 add_dir_entry(dir_ent, sub_dir, inode_info);
plougher1f413c82005-11-18 00:02:14 +00003255}
3256
3257
Colin Crossbe754902017-12-09 22:27:51 +00003258static inline void free_dir_entry(struct dir_ent *dir_ent)
Phillip Lougher10a4b572012-02-18 23:26:10 +00003259{
Phillip Loughereb92b192012-10-01 03:39:00 +01003260 if(dir_ent->name)
Phillip Lougher10a4b572012-02-18 23:26:10 +00003261 free(dir_ent->name);
3262
Phillip Loughereb92b192012-10-01 03:39:00 +01003263 if(dir_ent->source_name)
Phillip Lougher10a4b572012-02-18 23:26:10 +00003264 free(dir_ent->source_name);
3265
Phillip Lougher070c0f72014-07-27 01:03:43 +01003266 if(dir_ent->nonstandard_pathname)
3267 free(dir_ent->nonstandard_pathname);
3268
3269 /* if this entry has been associated with an inode, then we need
3270 * to update the inode nlink count. Orphaned inodes are harmless, and
3271 * is easier to leave them than go to the bother of deleting them */
3272 if(dir_ent->inode && !dir_ent->inode->root_entry)
3273 dir_ent->inode->nlink --;
3274
Phillip Lougher10a4b572012-02-18 23:26:10 +00003275 free(dir_ent);
3276}
3277
3278
Colin Crossbe754902017-12-09 22:27:51 +00003279static inline void add_excluded(struct dir_info *dir)
Phillip Lougherad0f9212011-12-31 00:55:51 +00003280{
3281 dir->excluded ++;
3282}
3283
3284
Phillip Lougherabc3b492012-07-29 02:53:35 +01003285void dir_scan(squashfs_inode *inode, char *pathname,
Phillip Lougherbae0e422014-01-19 23:42:11 +00003286 struct dir_ent *(_readdir)(struct dir_info *), int progress)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003287{
3288 struct stat buf;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003289 struct dir_ent *dir_ent;
Mohamad Ayyash531e36a2016-03-09 16:10:54 -08003290/* ANDROID CHANGES START*/
3291#ifdef ANDROID
3292 uint64_t caps = 0;
3293#endif
3294/* ANDROID CHANGES END */
Phillip Lougherabc3b492012-07-29 02:53:35 +01003295
Phillip Lougher22d67da2014-08-08 02:55:33 +01003296 root_dir = dir_scan1(pathname, "", paths, _readdir, 1);
3297 if(root_dir == NULL)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003298 return;
3299
Phillip Lougher49bf8602014-07-29 05:03:02 +01003300 /* Create root directory dir_ent and associated inode, and connect
3301 * it to the root directory dir_info structure */
3302 dir_ent = create_dir_entry("", NULL, pathname,
3303 scan1_opendir("", "", 0));
3304
3305 if(pathname[0] == '\0') {
3306 /*
3307 * dummy top level directory, if multiple sources specified on
3308 * command line
3309 */
3310 memset(&buf, 0, sizeof(buf));
3311 buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR;
3312 buf.st_uid = getuid();
3313 buf.st_gid = getgid();
3314 buf.st_mtime = time(NULL);
3315 buf.st_dev = 0;
3316 buf.st_ino = 0;
3317 dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0);
3318 } else {
3319 if(lstat(pathname, &buf) == -1)
3320 /* source directory has disappeared? */
3321 BAD_ERROR("Cannot stat source directory %s because %s\n",
3322 pathname, strerror(errno));
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -08003323/* ANDROID CHANGES START*/
3324#ifdef ANDROID
Luis Hector Chavez1cdde052018-02-07 16:12:09 +00003325 buf.st_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_IFDIR; // root mode
Mohamad Ayyash6595c1f2016-05-25 01:34:17 -07003326 buf.st_uid = 0;
3327 buf.st_gid = 0;
3328 buf.st_mtime = time(NULL);
3329 buf.st_dev = 0;
3330 buf.st_ino = 0;
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -08003331#endif
3332/* ANDROID CHANGES END */
Phillip Lougher49bf8602014-07-29 05:03:02 +01003333 dir_ent->inode = lookup_inode(&buf);
3334 }
3335
Mohamad Ayyash531e36a2016-03-09 16:10:54 -08003336/* ANDROID CHANGES START*/
3337#ifdef ANDROID
3338 dir_ent->capabilities = caps;
Luis Hector Chavez1cdde052018-02-07 16:12:09 +00003339 if (android_config) {
3340 android_fs_config(fs_config_func, "", &dir_ent->inode->buf, target_out_path, &dir_ent->capabilities);
3341 }
Mohamad Ayyash531e36a2016-03-09 16:10:54 -08003342#endif
3343/* ANDROID CHANGES END */
3344
Phillip Lougher22d67da2014-08-08 02:55:33 +01003345 dir_ent->dir = root_dir;
3346 root_dir->dir_ent = dir_ent;
Phillip Lougher49bf8602014-07-29 05:03:02 +01003347
Phillip Lougherf3d0f9c2012-11-14 05:36:58 +00003348 /*
3349 * Process most actions and any pseudo files
3350 */
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00003351 if(actions() || get_pseudo())
Phillip Lougher22d67da2014-08-08 02:55:33 +01003352 dir_scan2(root_dir, get_pseudo());
Phillip Lougherf3d0f9c2012-11-14 05:36:58 +00003353
Phillip Loughere92cb512012-11-15 02:22:28 +00003354 /*
3355 * Process move actions
3356 */
3357 if(move_actions()) {
Phillip Lougher22d67da2014-08-08 02:55:33 +01003358 dir_scan3(root_dir);
Phillip Loughere92cb512012-11-15 02:22:28 +00003359 do_move_actions();
3360 }
3361
Phillip Lougher71ae14e2012-11-15 02:27:50 +00003362 /*
Phillip Lougher070c0f72014-07-27 01:03:43 +01003363 * Process prune actions
3364 */
3365 if(prune_actions())
Phillip Lougher22d67da2014-08-08 02:55:33 +01003366 dir_scan4(root_dir);
Phillip Lougher070c0f72014-07-27 01:03:43 +01003367
3368 /*
Phillip Lougher71ae14e2012-11-15 02:27:50 +00003369 * Process empty actions
3370 */
3371 if(empty_actions())
Phillip Lougher22d67da2014-08-08 02:55:33 +01003372 dir_scan5(root_dir);
Phillip Lougher71ae14e2012-11-15 02:27:50 +00003373
Phillip Loughereb69dad2012-11-15 03:34:02 +00003374 /*
3375 * Sort directories and compute the inode numbers
3376 */
Phillip Lougher22d67da2014-08-08 02:55:33 +01003377 dir_scan6(root_dir);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003378
Phillip Lougher539c2b12012-07-30 20:14:52 +01003379 alloc_inode_no(dir_ent->inode, root_inode_number);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003380
Phillip Lougher22d67da2014-08-08 02:55:33 +01003381 eval_actions(root_dir, dir_ent);
Andy Lutomirski3f31dcf2012-09-21 18:08:08 -07003382
Phillip Lougher19b877d2013-02-23 11:15:38 +00003383 if(sorted)
Phillip Lougher22d67da2014-08-08 02:55:33 +01003384 generate_file_priorities(root_dir, 0,
3385 &root_dir->dir_ent->inode->buf);
Phillip Lougher3794e342014-04-10 02:15:45 +01003386
3387 if(appending) {
3388 sigset_t sigmask;
3389
3390 restore_thread = init_restore_thread();
3391 sigemptyset(&sigmask);
3392 sigaddset(&sigmask, SIGINT);
3393 sigaddset(&sigmask, SIGTERM);
3394 sigaddset(&sigmask, SIGUSR1);
3395 if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
3396 BAD_ERROR("Failed to set signal mask\n");
3397 write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0");
3398 }
3399
Phillip Lougher22d67da2014-08-08 02:55:33 +01003400 queue_put(to_reader, root_dir);
Phillip Lougher3794e342014-04-10 02:15:45 +01003401
Phillip Lougher99639442014-09-12 23:28:59 +01003402 set_progressbar_state(progress);
3403
Phillip Lougherabc3b492012-07-29 02:53:35 +01003404 if(sorted)
Phillip Lougher22d67da2014-08-08 02:55:33 +01003405 sort_files_and_write(root_dir);
Phillip Lougher3794e342014-04-10 02:15:45 +01003406
Phillip Lougher22d67da2014-08-08 02:55:33 +01003407 dir_scan7(inode, root_dir);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003408 dir_ent->inode->inode = *inode;
3409 dir_ent->inode->type = SQUASHFS_DIR_TYPE;
plougher1f413c82005-11-18 00:02:14 +00003410}
3411
3412
Phillip Lougherabc3b492012-07-29 02:53:35 +01003413/*
3414 * dir_scan1 routines...
Phillip Loughere8630be2012-11-11 02:24:24 +00003415 * These scan the source directories into memory for processing.
3416 * Exclude actions are processed here (in contrast to the other actions)
3417 * because they affect what is scanned.
Phillip Lougherabc3b492012-07-29 02:53:35 +01003418 */
Phillip Lougherb38c1722012-02-09 23:26:19 +00003419struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth)
plougher1f413c82005-11-18 00:02:14 +00003420{
plougher1f413c82005-11-18 00:02:14 +00003421 struct dir_info *dir;
3422
plougher6da792b2010-12-16 05:25:39 +00003423 dir = malloc(sizeof(struct dir_info));
3424 if(dir == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003425 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00003426
Phillip Lougher04ef2442013-01-09 06:03:50 +00003427 if(pathname[0] != '\0') {
3428 dir->linuxdir = opendir(pathname);
3429 if(dir->linuxdir == NULL) {
3430 free(dir);
3431 return NULL;
3432 }
plougher1f413c82005-11-18 00:02:14 +00003433 }
Phillip Lougher04ef2442013-01-09 06:03:50 +00003434
Phillip Lougher3cb5d6c2013-01-09 05:50:29 +00003435 dir->pathname = strdup(pathname);
3436 dir->subpath = strdup(subpath);
Phillip Lougherded70fa2011-12-25 02:14:58 +00003437 dir->count = 0;
3438 dir->directory_count = 0;
plougher1f413c82005-11-18 00:02:14 +00003439 dir->dir_is_ldir = TRUE;
3440 dir->list = NULL;
Phillip Lougher0b5a1242011-12-25 03:22:42 +00003441 dir->depth = depth;
Phillip Lougherad0f9212011-12-31 00:55:51 +00003442 dir->excluded = 0;
plougher1f413c82005-11-18 00:02:14 +00003443
3444 return dir;
3445}
3446
3447
Phillip Lougher494479f2012-02-03 15:45:41 +00003448struct dir_ent *scan1_encomp_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003449{
plougher1f413c82005-11-18 00:02:14 +00003450 static int index = 0;
3451
plougher11266ba2010-12-16 05:34:30 +00003452 if(dir->count < old_root_entries) {
3453 int i;
3454
plougher1f413c82005-11-18 00:02:14 +00003455 for(i = 0; i < old_root_entries; i++) {
ploughera326c182009-08-29 05:41:45 +00003456 if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
plougher1f413c82005-11-18 00:02:14 +00003457 dir->directory_count ++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003458 add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
ploughera326c182009-08-29 05:41:45 +00003459 &old_root_entry[i].inode, dir);
plougher1f413c82005-11-18 00:02:14 +00003460 }
plougher11266ba2010-12-16 05:34:30 +00003461 }
plougher1f413c82005-11-18 00:02:14 +00003462
3463 while(index < source) {
Phillip Lougherc73ed322012-10-01 03:25:41 +01003464 char *basename = NULL;
3465 char *dir_name = getbase(source_path[index]);
Phillip Lougherbf338362012-08-22 05:24:36 +01003466 int pass = 1, res;
plougher89fe2c72010-12-16 05:31:34 +00003467
Phillip Lougherc73ed322012-10-01 03:25:41 +01003468 if(dir_name == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003469 ERROR_START("Bad source directory %s",
plougher110799c2009-03-30 01:50:40 +00003470 source_path[index]);
Phillip Lougher85776df2014-01-31 03:10:46 +00003471 ERROR_EXIT(" - skipping ...\n");
plougher1f413c82005-11-18 00:02:14 +00003472 index ++;
3473 continue;
3474 }
Phillip Lougherc73ed322012-10-01 03:25:41 +01003475 dir_name = strdup(dir_name);
plougher1f413c82005-11-18 00:02:14 +00003476 for(;;) {
Phillip Lougherbf338362012-08-22 05:24:36 +01003477 struct dir_ent *dir_ent = dir->list;
3478
3479 for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3480 dir_ent = dir_ent->next);
3481 if(dir_ent == NULL)
plougher1f413c82005-11-18 00:02:14 +00003482 break;
plougher50b31762009-03-31 04:14:46 +00003483 ERROR("Source directory entry %s already used! - trying"
3484 " ", dir_name);
Phillip Lougherc73ed322012-10-01 03:25:41 +01003485 if(pass == 1)
3486 basename = dir_name;
3487 else
Phillip Lougherb38c1722012-02-09 23:26:19 +00003488 free(dir_name);
Phillip Lougher494479f2012-02-03 15:45:41 +00003489 res = asprintf(&dir_name, "%s_%d", basename, pass++);
3490 if(res == -1)
3491 BAD_ERROR("asprintf failed in "
3492 "scan1_encomp_readdir\n");
plougher1f413c82005-11-18 00:02:14 +00003493 ERROR("%s\n", dir_name);
3494 }
Phillip Lougherb38c1722012-02-09 23:26:19 +00003495 return create_dir_entry(dir_name, basename,
Phillip Lougher4be6d452014-09-03 02:20:55 +01003496 strdup(source_path[index ++]), dir);
plougher1f413c82005-11-18 00:02:14 +00003497 }
Phillip Lougher494479f2012-02-03 15:45:41 +00003498 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003499}
3500
3501
Phillip Lougher494479f2012-02-03 15:45:41 +00003502struct dir_ent *scan1_single_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003503{
3504 struct dirent *d_name;
plougher4925e172010-12-16 05:45:54 +00003505 int i;
plougher1f413c82005-11-18 00:02:14 +00003506
plougher4925e172010-12-16 05:45:54 +00003507 if(dir->count < old_root_entries) {
plougher1f413c82005-11-18 00:02:14 +00003508 for(i = 0; i < old_root_entries; i++) {
ploughera326c182009-08-29 05:41:45 +00003509 if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
plougher1f413c82005-11-18 00:02:14 +00003510 dir->directory_count ++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003511 add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
ploughera326c182009-08-29 05:41:45 +00003512 &old_root_entry[i].inode, dir);
plougher1f413c82005-11-18 00:02:14 +00003513 }
plougher4925e172010-12-16 05:45:54 +00003514 }
plougher1f413c82005-11-18 00:02:14 +00003515
3516 if((d_name = readdir(dir->linuxdir)) != NULL) {
Phillip Lougherc73ed322012-10-01 03:25:41 +01003517 char *basename = NULL;
3518 char *dir_name = strdup(d_name->d_name);
Phillip Lougher494479f2012-02-03 15:45:41 +00003519 int pass = 1, res;
plougher4925e172010-12-16 05:45:54 +00003520
plougher1f413c82005-11-18 00:02:14 +00003521 for(;;) {
Phillip Lougherbf338362012-08-22 05:24:36 +01003522 struct dir_ent *dir_ent = dir->list;
3523
3524 for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3525 dir_ent = dir_ent->next);
3526 if(dir_ent == NULL)
plougher1f413c82005-11-18 00:02:14 +00003527 break;
plougher50b31762009-03-31 04:14:46 +00003528 ERROR("Source directory entry %s already used! - trying"
3529 " ", dir_name);
Phillip Lougherc73ed322012-10-01 03:25:41 +01003530 if (pass == 1)
3531 basename = dir_name;
3532 else
Phillip Lougher494479f2012-02-03 15:45:41 +00003533 free(dir_name);
3534 res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++);
3535 if(res == -1)
3536 BAD_ERROR("asprintf failed in "
3537 "scan1_single_readdir\n");
plougher1f413c82005-11-18 00:02:14 +00003538 ERROR("%s\n", dir_name);
3539 }
Phillip Lougher494479f2012-02-03 15:45:41 +00003540 return create_dir_entry(dir_name, basename, NULL, dir);
plougher1f413c82005-11-18 00:02:14 +00003541 }
3542
Phillip Lougher494479f2012-02-03 15:45:41 +00003543 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003544}
3545
3546
Phillip Lougher494479f2012-02-03 15:45:41 +00003547struct dir_ent *scan1_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003548{
plougher480d9bf2010-12-18 02:28:39 +00003549 struct dirent *d_name = readdir(dir->linuxdir);
plougher1f413c82005-11-18 00:02:14 +00003550
Phillip Lougher494479f2012-02-03 15:45:41 +00003551 return d_name ?
3552 create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) :
3553 NULL;
plougher1f413c82005-11-18 00:02:14 +00003554}
3555
3556
plougher1f413c82005-11-18 00:02:14 +00003557void scan1_freedir(struct dir_info *dir)
3558{
plougherfbed12b2006-02-07 12:45:53 +00003559 if(dir->pathname[0] != '\0')
3560 closedir(dir->linuxdir);
plougher1f413c82005-11-18 00:02:14 +00003561}
3562
3563
Phillip Lougherb38c1722012-02-09 23:26:19 +00003564struct dir_info *dir_scan1(char *filename, char *subpath,
3565 struct pathnames *paths,
Phillip Lougher494479f2012-02-03 15:45:41 +00003566 struct dir_ent *(_readdir)(struct dir_info *), int depth)
plougher1f413c82005-11-18 00:02:14 +00003567{
Phillip Lougherb38c1722012-02-09 23:26:19 +00003568 struct dir_info *dir = scan1_opendir(filename, subpath, depth);
Phillip Lougher494479f2012-02-03 15:45:41 +00003569 struct dir_ent *dir_ent;
plougher1f413c82005-11-18 00:02:14 +00003570
plougher360b6e42010-12-18 02:40:28 +00003571 if(dir == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003572 ERROR_START("Could not open %s", filename);
3573 ERROR_EXIT(", skipping...\n");
Phillip Lougher87db5672013-01-09 05:40:46 +00003574 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003575 }
3576
Phillip Lougher494479f2012-02-03 15:45:41 +00003577 while((dir_ent = _readdir(dir))) {
plougher360b6e42010-12-18 02:40:28 +00003578 struct dir_info *sub_dir;
3579 struct stat buf;
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00003580 struct pathnames *new = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003581 char *filename = pathname(dir_ent);
Phillip Lougher651d68d2013-01-13 05:38:13 +00003582 char *subpath = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003583 char *dir_name = dir_ent->name;
plougher1f413c82005-11-18 00:02:14 +00003584
Phillip Lougher494479f2012-02-03 15:45:41 +00003585 if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) {
Phillip Lougher10a4b572012-02-18 23:26:10 +00003586 free_dir_entry(dir_ent);
plougher1f413c82005-11-18 00:02:14 +00003587 continue;
Phillip Lougher494479f2012-02-03 15:45:41 +00003588 }
plougher1f413c82005-11-18 00:02:14 +00003589
3590 if(lstat(filename, &buf) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003591 ERROR_START("Cannot stat dir/file %s because %s",
plougher110799c2009-03-30 01:50:40 +00003592 filename, strerror(errno));
Phillip Lougher85776df2014-01-31 03:10:46 +00003593 ERROR_EXIT(", ignoring\n");
Phillip Lougher10a4b572012-02-18 23:26:10 +00003594 free_dir_entry(dir_ent);
plougher1f413c82005-11-18 00:02:14 +00003595 continue;
3596 }
plougher29e37092007-04-15 01:24:51 +00003597
3598 if((buf.st_mode & S_IFMT) != S_IFREG &&
Phillip Loughere4ff36a2012-11-19 01:34:47 +00003599 (buf.st_mode & S_IFMT) != S_IFDIR &&
3600 (buf.st_mode & S_IFMT) != S_IFLNK &&
3601 (buf.st_mode & S_IFMT) != S_IFCHR &&
3602 (buf.st_mode & S_IFMT) != S_IFBLK &&
3603 (buf.st_mode & S_IFMT) != S_IFIFO &&
3604 (buf.st_mode & S_IFMT) != S_IFSOCK) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003605 ERROR_START("File %s has unrecognised filetype %d",
3606 filename, buf.st_mode & S_IFMT);
3607 ERROR_EXIT(", ignoring\n");
Phillip Lougher10a4b572012-02-18 23:26:10 +00003608 free_dir_entry(dir_ent);
plougher29e37092007-04-15 01:24:51 +00003609 continue;
3610 }
3611
Phillip Lougherad0f9212011-12-31 00:55:51 +00003612 if((old_exclude && old_excluded(filename, &buf)) ||
Phillip Lougher651d68d2013-01-13 05:38:13 +00003613 (!old_exclude && excluded(dir_name, paths, &new))) {
Phillip Lougherad0f9212011-12-31 00:55:51 +00003614 add_excluded(dir);
Phillip Lougher10a4b572012-02-18 23:26:10 +00003615 free_dir_entry(dir_ent);
Phillip Lougher10d8de02011-08-29 00:14:48 +01003616 continue;
Phillip Lougherad0f9212011-12-31 00:55:51 +00003617 }
plougher1f413c82005-11-18 00:02:14 +00003618
Phillip Lougher651d68d2013-01-13 05:38:13 +00003619 if(exclude_actions()) {
3620 subpath = subpathname(dir_ent);
3621
3622 if(eval_exclude_actions(dir_name, filename, subpath,
Phillip Lougheraa83ecb2014-07-12 02:47:15 +01003623 &buf, depth, dir_ent)) {
Phillip Lougher651d68d2013-01-13 05:38:13 +00003624 add_excluded(dir);
3625 free_dir_entry(dir_ent);
3626 continue;
3627 }
3628 }
3629
Phillip Loughere19ce452014-07-27 04:16:54 +01003630 switch(buf.st_mode & S_IFMT) {
3631 case S_IFDIR:
Phillip Lougher651d68d2013-01-13 05:38:13 +00003632 if(subpath == NULL)
3633 subpath = subpathname(dir_ent);
3634
Phillip Lougherb38c1722012-02-09 23:26:19 +00003635 sub_dir = dir_scan1(filename, subpath, new,
3636 scan1_readdir, depth + 1);
Phillip Loughere19ce452014-07-27 04:16:54 +01003637 if(sub_dir) {
3638 dir->directory_count ++;
3639 add_dir_entry(dir_ent, sub_dir,
3640 lookup_inode(&buf));
3641 } else
Phillip Lougher10a4b572012-02-18 23:26:10 +00003642 free_dir_entry(dir_ent);
Phillip Loughere19ce452014-07-27 04:16:54 +01003643 break;
3644 case S_IFLNK: {
3645 int byte;
3646 static char buff[65536]; /* overflow safe */
3647
3648 byte = readlink(filename, buff, 65536);
3649 if(byte == -1) {
3650 ERROR_START("Failed to read symlink %s",
3651 filename);
3652 ERROR_EXIT(", ignoring\n");
3653 } else if(byte == 65536) {
3654 ERROR_START("Symlink %s is greater than 65536 "
3655 "bytes!", filename);
3656 ERROR_EXIT(", ignoring\n");
3657 } else {
3658 /* readlink doesn't 0 terminate the returned
3659 * path */
3660 buff[byte] = '\0';
3661 add_dir_entry(dir_ent, NULL, lookup_inode3(&buf,
3662 0, 0, buff, byte + 1));
Phillip Lougher494479f2012-02-03 15:45:41 +00003663 }
Phillip Loughere19ce452014-07-27 04:16:54 +01003664 break;
3665 }
3666 default:
3667 add_dir_entry(dir_ent, NULL, lookup_inode(&buf));
3668 }
Phillip Lougher6a78a2d2011-12-28 03:54:19 +00003669
Phillip Lougher205c1e02013-01-07 01:37:56 +00003670 free(new);
plougher1f413c82005-11-18 00:02:14 +00003671 }
3672
3673 scan1_freedir(dir);
plougher1f413c82005-11-18 00:02:14 +00003674
plougher1f413c82005-11-18 00:02:14 +00003675 return dir;
3676}
3677
plougher2ea89142008-03-11 01:34:19 +00003678
Phillip Lougherabc3b492012-07-29 02:53:35 +01003679/*
3680 * dir_scan2 routines...
Phillip Lougher539c2b12012-07-30 20:14:52 +01003681 * This processes most actions and any pseudo files
Phillip Lougherabc3b492012-07-29 02:53:35 +01003682 */
Phillip Lougherbf338362012-08-22 05:24:36 +01003683struct dir_ent *scan2_readdir(struct dir_info *dir, struct dir_ent *dir_ent)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003684{
Phillip Lougherbf338362012-08-22 05:24:36 +01003685 if (dir_ent == NULL)
3686 dir_ent = dir->list;
3687 else
3688 dir_ent = dir_ent->next;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003689
Phillip Lougherbf338362012-08-22 05:24:36 +01003690 for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next);
3691
3692 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003693}
3694
3695
3696struct dir_ent *scan2_lookup(struct dir_info *dir, char *name)
3697{
Phillip Lougherbf338362012-08-22 05:24:36 +01003698 struct dir_ent *dir_ent = dir->list;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003699
Phillip Lougherbf338362012-08-22 05:24:36 +01003700 for(; dir_ent && strcmp(dir_ent->name, name) != 0;
3701 dir_ent = dir_ent->next);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003702
Phillip Lougherbf338362012-08-22 05:24:36 +01003703 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003704}
3705
3706
Phillip Lougher24eeb772012-10-13 01:56:24 +01003707void dir_scan2(struct dir_info *dir, struct pseudo *pseudo)
plougher43244f22009-04-05 02:04:51 +00003708{
Phillip Lougherbf338362012-08-22 05:24:36 +01003709 struct dir_ent *dir_ent = NULL;
plougher43244f22009-04-05 02:04:51 +00003710 struct pseudo_entry *pseudo_ent;
3711 struct stat buf;
plougher82ab2332009-04-21 00:21:21 +00003712 static int pseudo_ino = 1;
plougher43244f22009-04-05 02:04:51 +00003713
Phillip Lougherbf338362012-08-22 05:24:36 +01003714 while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
plougher43244f22009-04-05 02:04:51 +00003715 struct inode_info *inode_info = dir_ent->inode;
3716 struct stat *buf = &inode_info->buf;
3717 char *name = dir_ent->name;
3718
Phillip Lougher22d67da2014-08-08 02:55:33 +01003719 eval_actions(root_dir, dir_ent);
Phillip Lougher89d757c2011-09-20 00:33:19 +01003720
plougher43244f22009-04-05 02:04:51 +00003721 if((buf->st_mode & S_IFMT) == S_IFDIR)
Phillip Lougher24eeb772012-10-13 01:56:24 +01003722 dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo));
plougher43244f22009-04-05 02:04:51 +00003723 }
3724
3725 while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) {
3726 dir_ent = scan2_lookup(dir, pseudo_ent->name);
plougherdcd66c52010-09-17 03:44:17 +00003727 if(pseudo_ent->dev->type == 'm') {
plougherb34f9f62009-04-26 02:08:42 +00003728 struct stat *buf;
3729 if(dir_ent == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003730 ERROR_START("Pseudo modify file \"%s\" does "
3731 "not exist in source filesystem.",
plougherb34f9f62009-04-26 02:08:42 +00003732 pseudo_ent->pathname);
Phillip Lougher85776df2014-01-31 03:10:46 +00003733 ERROR_EXIT(" Ignoring.\n");
plougherb34f9f62009-04-26 02:08:42 +00003734 continue;
3735 }
ploughera326c182009-08-29 05:41:45 +00003736 if(dir_ent->inode->root_entry) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003737 ERROR_START("Pseudo modify file \"%s\" is a "
3738 "pre-existing file in the filesystem "
3739 "being appended to. It cannot be "\
3740 "modified.", pseudo_ent->pathname);
3741 ERROR_EXIT(" Ignoring.\n");
plougherb34f9f62009-04-26 02:08:42 +00003742 continue;
3743 }
3744 buf = &dir_ent->inode->buf;
3745 buf->st_mode = (buf->st_mode & S_IFMT) |
3746 pseudo_ent->dev->mode;
3747 buf->st_uid = pseudo_ent->dev->uid;
3748 buf->st_gid = pseudo_ent->dev->gid;
3749 continue;
3750 }
3751
plougher43244f22009-04-05 02:04:51 +00003752 if(dir_ent) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003753 if(dir_ent->inode->root_entry) {
3754 ERROR_START("Pseudo file \"%s\" is a "
3755 "pre-existing file in the filesystem "
3756 "being appended to.",
plougherf0dc2382010-05-01 23:55:06 +00003757 pseudo_ent->pathname);
Phillip Lougher85776df2014-01-31 03:10:46 +00003758 ERROR_EXIT(" Ignoring.\n");
3759 } else {
3760 ERROR_START("Pseudo file \"%s\" exists in "
3761 "source filesystem \"%s\".",
plougherf0dc2382010-05-01 23:55:06 +00003762 pseudo_ent->pathname,
Phillip Lougher494479f2012-02-03 15:45:41 +00003763 pathname(dir_ent));
Phillip Lougher85776df2014-01-31 03:10:46 +00003764 ERROR_EXIT("\nIgnoring, exclude it (-e/-ef) to "
3765 "override.\n");
3766 }
plougher43244f22009-04-05 02:04:51 +00003767 continue;
3768 }
3769
plougher43244f22009-04-05 02:04:51 +00003770 memset(&buf, 0, sizeof(buf));
3771 buf.st_mode = pseudo_ent->dev->mode;
3772 buf.st_uid = pseudo_ent->dev->uid;
3773 buf.st_gid = pseudo_ent->dev->gid;
3774 buf.st_rdev = makedev(pseudo_ent->dev->major,
3775 pseudo_ent->dev->minor);
plougher7e58f4d2009-04-05 12:06:19 +00003776 buf.st_mtime = time(NULL);
plougher1a3fbf22009-04-05 12:04:16 +00003777 buf.st_ino = pseudo_ino ++;
plougher43244f22009-04-05 02:04:51 +00003778
Phillip Lougher5d579292012-10-13 01:37:16 +01003779 if(pseudo_ent->dev->type == 'd') {
3780 struct dir_ent *dir_ent =
3781 create_dir_entry(pseudo_ent->name, NULL,
3782 pseudo_ent->pathname, dir);
3783 char *subpath = strdup(subpathname(dir_ent));
3784 struct dir_info *sub_dir = scan1_opendir("", subpath,
Phillip Lougher24eeb772012-10-13 01:56:24 +01003785 dir->depth + 1);
Phillip Lougher5d579292012-10-13 01:37:16 +01003786 if(sub_dir == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003787 ERROR_START("Could not create pseudo directory "
3788 "\"%s\"", pseudo_ent->pathname);
3789 ERROR_EXIT(", skipping...\n");
Phillip Lougher5d579292012-10-13 01:37:16 +01003790 free(subpath);
3791 pseudo_ino --;
3792 continue;
3793 }
Phillip Lougher24eeb772012-10-13 01:56:24 +01003794 dir_scan2(sub_dir, pseudo_ent->pseudo);
Phillip Lougher5d579292012-10-13 01:37:16 +01003795 dir->directory_count ++;
3796 add_dir_entry(dir_ent, sub_dir,
3797 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0));
3798 } else if(pseudo_ent->dev->type == 'f') {
Phillip Lougher494479f2012-02-03 15:45:41 +00003799 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003800 pseudo_ent->pathname, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003801 lookup_inode2(&buf, PSEUDO_FILE_PROCESS,
3802 pseudo_ent->dev->pseudo_id), dir);
plougherb85e9ad2010-05-02 01:46:12 +00003803 } else {
Phillip Lougher494479f2012-02-03 15:45:41 +00003804 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003805 pseudo_ent->pathname, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003806 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir);
plougherb85e9ad2010-05-02 01:46:12 +00003807 }
plougher43244f22009-04-05 02:04:51 +00003808 }
plougher43244f22009-04-05 02:04:51 +00003809}
3810
3811
Phillip Lougherabc3b492012-07-29 02:53:35 +01003812/*
3813 * dir_scan3 routines...
Phillip Lougher23d83622012-10-14 02:35:22 +01003814 * This processes the move action
3815 */
Phillip Lougher22d67da2014-08-08 02:55:33 +01003816void dir_scan3(struct dir_info *dir)
Phillip Lougher23d83622012-10-14 02:35:22 +01003817{
3818 struct dir_ent *dir_ent = NULL;
3819
3820 while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
3821
Phillip Lougher22d67da2014-08-08 02:55:33 +01003822 eval_move_actions(root_dir, dir_ent);
Phillip Lougher23d83622012-10-14 02:35:22 +01003823
3824 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
Phillip Lougher22d67da2014-08-08 02:55:33 +01003825 dir_scan3(dir_ent->dir);
Phillip Lougher23d83622012-10-14 02:35:22 +01003826 }
3827}
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003828
3829
Phillip Lougher23d83622012-10-14 02:35:22 +01003830/*
3831 * dir_scan4 routines...
Phillip Lougher070c0f72014-07-27 01:03:43 +01003832 * This processes the prune action. This action is designed to do fine
3833 * grained tuning of the in-core directory structure after the exclude,
3834 * move and pseudo actions have been performed. This allows complex
3835 * tests to be performed which are impossible at exclude time (i.e.
3836 * tests which rely on the in-core directory structure)
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003837 */
Phillip Lougher070c0f72014-07-27 01:03:43 +01003838void free_dir(struct dir_info *dir)
3839{
3840 struct dir_ent *dir_ent = dir->list;
3841
3842 while(dir_ent) {
3843 struct dir_ent *tmp = dir_ent;
3844
3845 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
3846 free_dir(dir_ent->dir);
3847
3848 dir_ent = dir_ent->next;
3849 free_dir_entry(tmp);
3850 }
3851
3852 free(dir->pathname);
3853 free(dir->subpath);
3854 free(dir);
3855}
3856
3857
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003858void dir_scan4(struct dir_info *dir)
3859{
3860 struct dir_ent *dir_ent = dir->list, *prev = NULL;
3861
3862 while(dir_ent) {
Phillip Lougher070c0f72014-07-27 01:03:43 +01003863 if(dir_ent->inode->root_entry) {
3864 prev = dir_ent;
3865 dir_ent = dir_ent->next;
3866 continue;
3867 }
3868
3869 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
Phillip Lougher15e8f042012-11-16 00:57:39 +00003870 dir_scan4(dir_ent->dir);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003871
Phillip Lougher22d67da2014-08-08 02:55:33 +01003872 if(eval_prune_actions(root_dir, dir_ent)) {
Phillip Lougher070c0f72014-07-27 01:03:43 +01003873 struct dir_ent *tmp = dir_ent;
3874
3875 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) {
3876 free_dir(dir_ent->dir);
3877 dir->directory_count --;
3878 }
3879
3880 dir->count --;
3881
3882 /* remove dir_ent from list */
3883 dir_ent = dir_ent->next;
3884 if(prev)
3885 prev->next = dir_ent;
3886 else
3887 dir->list = dir_ent;
3888
3889 /* free it */
3890 free_dir_entry(tmp);
3891
3892 add_excluded(dir);
3893 continue;
3894 }
3895
3896 prev = dir_ent;
3897 dir_ent = dir_ent->next;
3898 }
3899}
3900
3901
3902/*
3903 * dir_scan5 routines...
3904 * This processes the empty action. This action has to be processed after
3905 * all other actions because the previous exclude and move actions and the
3906 * pseudo actions affect whether a directory is empty
3907 */
3908void dir_scan5(struct dir_info *dir)
3909{
3910 struct dir_ent *dir_ent = dir->list, *prev = NULL;
3911
3912 while(dir_ent) {
Phillip Lougher97b7c4b2014-08-04 04:43:15 +01003913 if(dir_ent->inode->root_entry) {
3914 prev = dir_ent;
3915 dir_ent = dir_ent->next;
3916 continue;
3917 }
3918
Phillip Lougher070c0f72014-07-27 01:03:43 +01003919 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) {
3920 dir_scan5(dir_ent->dir);
3921
Phillip Lougher22d67da2014-08-08 02:55:33 +01003922 if(eval_empty_actions(root_dir, dir_ent)) {
Phillip Lougher15e8f042012-11-16 00:57:39 +00003923 struct dir_ent *tmp = dir_ent;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003924
Phillip Lougher15e8f042012-11-16 00:57:39 +00003925 /*
3926 * delete sub-directory, this is by definition
3927 * empty
3928 */
3929 free(dir_ent->dir->pathname);
3930 free(dir_ent->dir->subpath);
3931 free(dir_ent->dir);
3932
3933 /* remove dir_ent from list */
3934 dir_ent = dir_ent->next;
3935 if(prev)
3936 prev->next = dir_ent;
3937 else
3938 dir->list = dir_ent;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003939
Phillip Lougher15e8f042012-11-16 00:57:39 +00003940 /* free it */
3941 free_dir_entry(tmp);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003942
Phillip Lougher15e8f042012-11-16 00:57:39 +00003943 /* update counts */
3944 dir->directory_count --;
3945 dir->count --;
3946 add_excluded(dir);
3947 continue;
3948 }
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003949 }
Phillip Lougher15e8f042012-11-16 00:57:39 +00003950
3951 prev = dir_ent;
3952 dir_ent = dir_ent->next;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003953 }
3954}
3955
3956
3957/*
Phillip Lougher070c0f72014-07-27 01:03:43 +01003958 * dir_scan6 routines...
Phillip Lougher539c2b12012-07-30 20:14:52 +01003959 * This sorts every directory and computes the inode numbers
3960 */
Phillip Lougher539c2b12012-07-30 20:14:52 +01003961
Phillip Lougher242242e2012-08-24 04:15:36 +01003962/*
3963 * Bottom up linked list merge sort.
3964 *
3965 * Qsort and other O(n log n) algorithms work well with arrays but not
3966 * linked lists. Merge sort another O(n log n) sort algorithm on the other hand
3967 * is not ideal for arrays (as it needs an additonal n storage locations
3968 * as sorting is not done in place), but it is ideal for linked lists because
3969 * it doesn't require any extra storage,
3970 */
Phillip Lougher539c2b12012-07-30 20:14:52 +01003971void sort_directory(struct dir_info *dir)
3972{
Phillip Lougher242242e2012-08-24 04:15:36 +01003973 struct dir_ent *cur, *l1, *l2, *next;
3974 int len1, len2, stride = 1;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003975
Phillip Lougherd3459a82014-04-18 01:37:54 +01003976 if(dir->list == NULL || dir->count < 2)
Phillip Lougherbf338362012-08-22 05:24:36 +01003977 return;
3978
Phillip Lougher242242e2012-08-24 04:15:36 +01003979 /*
3980 * We can consider our linked-list to be made up of stride length
3981 * sublists. Eacn iteration around this loop merges adjacent
3982 * stride length sublists into larger 2*stride sublists. We stop
3983 * when stride becomes equal to the entire list.
3984 *
3985 * Initially stride = 1 (by definition a sublist of 1 is sorted), and
3986 * these 1 element sublists are merged into 2 element sublists, which
3987 * are then merged into 4 element sublists and so on.
3988 */
3989 do {
3990 l2 = dir->list; /* head of current linked list */
3991 cur = NULL; /* empty output list */
Phillip Lougherbf338362012-08-22 05:24:36 +01003992
Phillip Lougher242242e2012-08-24 04:15:36 +01003993 /*
3994 * Iterate through the linked list, merging adjacent sublists.
3995 * On each interation l2 points to the next sublist pair to be
3996 * merged (if there's only one sublist left this is simply added
3997 * to the output list)
3998 */
3999 while(l2) {
4000 l1 = l2;
4001 for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next);
4002 len2 = stride;
Phillip Lougherbf338362012-08-22 05:24:36 +01004003
Phillip Lougher242242e2012-08-24 04:15:36 +01004004 /*
4005 * l1 points to first sublist.
4006 * l2 points to second sublist.
4007 * Merge them onto the output list
4008 */
4009 while(len1 && l2 && len2) {
4010 if(strcmp(l1->name, l2->name) <= 0) {
4011 next = l1;
4012 l1 = l1->next;
4013 len1 --;
4014 } else {
4015 next = l2;
4016 l2 = l2->next;
4017 len2 --;
4018 }
4019
4020 if(cur) {
4021 cur->next = next;
4022 cur = next;
4023 } else
4024 dir->list = cur = next;
4025 }
4026 /*
4027 * One sublist is now empty, copy the other one onto the
4028 * output list
4029 */
4030 for(; len1; len1 --, l1 = l1->next) {
4031 if(cur) {
4032 cur->next = l1;
4033 cur = l1;
4034 } else
4035 dir->list = cur = l1;
4036 }
4037 for(; l2 && len2; len2 --, l2 = l2->next) {
4038 if(cur) {
4039 cur->next = l2;
4040 cur = l2;
4041 } else
4042 dir->list = cur = l2;
4043 }
4044 }
4045 cur->next = NULL;
4046 stride = stride << 1;
4047 } while(stride < dir->count);
Phillip Lougher539c2b12012-07-30 20:14:52 +01004048}
4049
4050
Phillip Lougher070c0f72014-07-27 01:03:43 +01004051void dir_scan6(struct dir_info *dir)
Phillip Lougher539c2b12012-07-30 20:14:52 +01004052{
Phillip Lougher23d83622012-10-14 02:35:22 +01004053 struct dir_ent *dir_ent;
4054 unsigned int byte_count = 0;
Phillip Lougher539c2b12012-07-30 20:14:52 +01004055
4056 sort_directory(dir);
4057
Phillip Lougher23d83622012-10-14 02:35:22 +01004058 for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) {
4059 byte_count += strlen(dir_ent->name) +
4060 sizeof(struct squashfs_dir_entry);
4061
4062 if(dir_ent->inode->root_entry)
4063 continue;
4064
Phillip Lougher539c2b12012-07-30 20:14:52 +01004065 alloc_inode_no(dir_ent->inode, 0);
Phillip Lougher23d83622012-10-14 02:35:22 +01004066
Phillip Lougher539c2b12012-07-30 20:14:52 +01004067 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
Phillip Lougher070c0f72014-07-27 01:03:43 +01004068 dir_scan6(dir_ent->dir);
Phillip Lougher539c2b12012-07-30 20:14:52 +01004069 }
Phillip Lougher23d83622012-10-14 02:35:22 +01004070
4071 if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE))
4072 dir->dir_is_ldir = FALSE;
Phillip Lougher539c2b12012-07-30 20:14:52 +01004073}
4074
4075
4076/*
Phillip Lougher2abfcf72012-11-11 03:59:56 +00004077 * dir_scan6 routines...
Phillip Lougherabc3b492012-07-29 02:53:35 +01004078 * This generates the filesystem metadata and writes it out to the destination
4079 */
Phillip Lougher070c0f72014-07-27 01:03:43 +01004080void scan7_init_dir(struct directory *dir)
Phillip Lougherabc3b492012-07-29 02:53:35 +01004081{
4082 dir->buff = malloc(SQUASHFS_METADATA_SIZE);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004083 if(dir->buff == NULL)
4084 MEM_ERROR();
Phillip Lougherabc3b492012-07-29 02:53:35 +01004085
4086 dir->size = SQUASHFS_METADATA_SIZE;
4087 dir->p = dir->index_count_p = dir->buff;
4088 dir->entry_count = 256;
4089 dir->entry_count_p = NULL;
4090 dir->index = NULL;
4091 dir->i_count = dir->i_size = 0;
4092}
4093
4094
Phillip Lougher070c0f72014-07-27 01:03:43 +01004095struct dir_ent *scan7_readdir(struct directory *dir, struct dir_info *dir_info,
Phillip Lougherbf338362012-08-22 05:24:36 +01004096 struct dir_ent *dir_ent)
Phillip Lougherabc3b492012-07-29 02:53:35 +01004097{
Phillip Lougherbf338362012-08-22 05:24:36 +01004098 if (dir_ent == NULL)
4099 dir_ent = dir_info->list;
4100 else
4101 dir_ent = dir_ent->next;
Phillip Lougherabc3b492012-07-29 02:53:35 +01004102
Phillip Lougherbf338362012-08-22 05:24:36 +01004103 for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next)
4104 add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number,
4105 dir_ent->name, dir_ent->inode->type, dir);
4106
4107 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01004108}
4109
4110
Phillip Lougher070c0f72014-07-27 01:03:43 +01004111void scan7_freedir(struct directory *dir)
Phillip Lougherabc3b492012-07-29 02:53:35 +01004112{
4113 if(dir->index)
4114 free(dir->index);
4115 free(dir->buff);
4116}
4117
4118
Phillip Lougher070c0f72014-07-27 01:03:43 +01004119void dir_scan7(squashfs_inode *inode, struct dir_info *dir_info)
plougher1f413c82005-11-18 00:02:14 +00004120{
4121 int squashfs_type;
plougher1f413c82005-11-18 00:02:14 +00004122 int duplicate_file;
plougher1f413c82005-11-18 00:02:14 +00004123 struct directory dir;
Phillip Lougherbf338362012-08-22 05:24:36 +01004124 struct dir_ent *dir_ent = NULL;
plougher1f413c82005-11-18 00:02:14 +00004125
Phillip Lougher070c0f72014-07-27 01:03:43 +01004126 scan7_init_dir(&dir);
plougher1f413c82005-11-18 00:02:14 +00004127
Phillip Lougher070c0f72014-07-27 01:03:43 +01004128 while((dir_ent = scan7_readdir(&dir, dir_info, dir_ent)) != NULL) {
Phillip Lougher0a670882012-10-05 04:09:13 +01004129 struct stat *buf = &dir_ent->inode->buf;
plougher1f413c82005-11-18 00:02:14 +00004130
Phillip Lougher24551a82013-03-24 02:30:50 +00004131 update_info(dir_ent);
4132
plougher1f413c82005-11-18 00:02:14 +00004133 if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) {
4134 switch(buf->st_mode & S_IFMT) {
4135 case S_IFREG:
4136 squashfs_type = SQUASHFS_FILE_TYPE;
plougher110799c2009-03-30 01:50:40 +00004137 write_file(inode, dir_ent,
4138 &duplicate_file);
4139 INFO("file %s, uncompressed size %lld "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004140 "bytes %s\n",
4141 subpathname(dir_ent),
plougher82ab2332009-04-21 00:21:21 +00004142 (long long) buf->st_size,
4143 duplicate_file ? "DUPLICATE" :
4144 "");
plougher1f413c82005-11-18 00:02:14 +00004145 break;
4146
4147 case S_IFDIR:
4148 squashfs_type = SQUASHFS_DIR_TYPE;
Phillip Lougher070c0f72014-07-27 01:03:43 +01004149 dir_scan7(inode, dir_ent->dir);
plougher1f413c82005-11-18 00:02:14 +00004150 break;
4151
4152 case S_IFLNK:
4153 squashfs_type = SQUASHFS_SYMLINK_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00004154 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00004155 squashfs_type, 0, 0, 0, NULL,
4156 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00004157 INFO("symbolic link %s inode 0x%llx\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004158 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00004159 sym_count ++;
4160 break;
4161
4162 case S_IFCHR:
4163 squashfs_type = SQUASHFS_CHRDEV_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00004164 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00004165 squashfs_type, 0, 0, 0, NULL,
4166 NULL, NULL, 0);
4167 INFO("character device %s inode 0x%llx"
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004168 "\n", subpathname(dir_ent),
4169 *inode);
plougher1f413c82005-11-18 00:02:14 +00004170 dev_count ++;
4171 break;
4172
4173 case S_IFBLK:
4174 squashfs_type = SQUASHFS_BLKDEV_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00004175 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00004176 squashfs_type, 0, 0, 0, NULL,
4177 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00004178 INFO("block device %s inode 0x%llx\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004179 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00004180 dev_count ++;
4181 break;
4182
4183 case S_IFIFO:
4184 squashfs_type = SQUASHFS_FIFO_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00004185 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00004186 squashfs_type, 0, 0, 0, NULL,
4187 NULL, NULL, 0);
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004188 INFO("fifo %s inode 0x%llx\n",
4189 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00004190 fifo_count ++;
4191 break;
4192
4193 case S_IFSOCK:
4194 squashfs_type = SQUASHFS_SOCKET_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00004195 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00004196 squashfs_type, 0, 0, 0, NULL,
4197 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00004198 INFO("unix domain socket %s inode "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004199 "0x%llx\n",
4200 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00004201 sock_count ++;
4202 break;
4203
plougher23377982007-11-12 04:04:48 +00004204 default:
plougherb3604122009-03-30 02:07:20 +00004205 BAD_ERROR("%s unrecognised file type, "
Phillip Lougher494479f2012-02-03 15:45:41 +00004206 "mode is %x\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004207 subpathname(dir_ent),
plougherb3604122009-03-30 02:07:20 +00004208 buf->st_mode);
plougher29e37092007-04-15 01:24:51 +00004209 }
4210 dir_ent->inode->inode = *inode;
plougher1f413c82005-11-18 00:02:14 +00004211 dir_ent->inode->type = squashfs_type;
4212 } else {
4213 *inode = dir_ent->inode->inode;
4214 squashfs_type = dir_ent->inode->type;
plougher04b0d5f2006-02-10 00:42:06 +00004215 switch(squashfs_type) {
4216 case SQUASHFS_FILE_TYPE:
4217 if(!sorted)
plougher50b31762009-03-31 04:14:46 +00004218 INFO("file %s, uncompressed "
4219 "size %lld bytes LINK"
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004220 "\n",
4221 subpathname(dir_ent),
plougher82ab2332009-04-21 00:21:21 +00004222 (long long)
plougher50b31762009-03-31 04:14:46 +00004223 buf->st_size);
plougher04b0d5f2006-02-10 00:42:06 +00004224 break;
4225 case SQUASHFS_SYMLINK_TYPE:
plougherb3604122009-03-30 02:07:20 +00004226 INFO("symbolic link %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004227 "LINK\n", subpathname(dir_ent),
4228 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00004229 break;
4230 case SQUASHFS_CHRDEV_TYPE:
plougherb3604122009-03-30 02:07:20 +00004231 INFO("character device %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004232 "LINK\n", subpathname(dir_ent),
4233 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00004234 break;
plougher5507dd92006-11-06 00:43:10 +00004235 case SQUASHFS_BLKDEV_TYPE:
plougherb3604122009-03-30 02:07:20 +00004236 INFO("block device %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004237 "LINK\n", subpathname(dir_ent),
4238 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00004239 break;
4240 case SQUASHFS_FIFO_TYPE:
plougherb3604122009-03-30 02:07:20 +00004241 INFO("fifo %s inode 0x%llx LINK\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004242 subpathname(dir_ent), *inode);
plougher04b0d5f2006-02-10 00:42:06 +00004243 break;
4244 case SQUASHFS_SOCKET_TYPE:
plougher50b31762009-03-31 04:14:46 +00004245 INFO("unix domain socket %s inode "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004246 "0x%llx LINK\n",
4247 subpathname(dir_ent), *inode);
plougher04b0d5f2006-02-10 00:42:06 +00004248 break;
4249 }
plougher1f413c82005-11-18 00:02:14 +00004250 }
4251
Phillip Lougher0366aec2012-10-05 04:06:04 +01004252 add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name,
4253 squashfs_type, &dir);
plougher1f413c82005-11-18 00:02:14 +00004254 }
4255
plougher29e37092007-04-15 01:24:51 +00004256 write_dir(inode, dir_info, &dir);
Phillip Lougher4980d7f2012-10-05 03:47:12 +01004257 INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent),
4258 *inode);
plougher1f413c82005-11-18 00:02:14 +00004259
Phillip Lougher070c0f72014-07-27 01:03:43 +01004260 scan7_freedir(&dir);
plougher1f413c82005-11-18 00:02:14 +00004261}
4262
4263
4264unsigned int slog(unsigned int block)
4265{
4266 int i;
4267
plougher4c99cb72007-06-14 21:46:31 +00004268 for(i = 12; i <= 20; i++)
plougher1f413c82005-11-18 00:02:14 +00004269 if(block == (1 << i))
4270 return i;
4271 return 0;
4272}
4273
4274
plougher8f8e1a12007-10-18 02:50:21 +00004275int old_excluded(char *filename, struct stat *buf)
plougher1f413c82005-11-18 00:02:14 +00004276{
4277 int i;
4278
4279 for(i = 0; i < exclude; i++)
plougherb3604122009-03-30 02:07:20 +00004280 if((exclude_paths[i].st_dev == buf->st_dev) &&
4281 (exclude_paths[i].st_ino == buf->st_ino))
plougher1f413c82005-11-18 00:02:14 +00004282 return TRUE;
4283 return FALSE;
4284}
4285
4286
4287#define ADD_ENTRY(buf) \
plougher360514a2009-03-30 03:01:38 +00004288 if(exclude % EXCLUDE_SIZE == 0) { \
4289 exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \
4290 * sizeof(struct exclude_info)); \
4291 if(exclude_paths == NULL) \
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004292 MEM_ERROR(); \
plougher360514a2009-03-30 03:01:38 +00004293 } \
4294 exclude_paths[exclude].st_dev = buf.st_dev; \
plougher1f413c82005-11-18 00:02:14 +00004295 exclude_paths[exclude++].st_ino = buf.st_ino;
plougher8f8e1a12007-10-18 02:50:21 +00004296int old_add_exclude(char *path)
plougher1f413c82005-11-18 00:02:14 +00004297{
4298 int i;
Phillip Lougherdcb5af92012-11-30 03:05:31 +00004299 char *filename;
plougher1f413c82005-11-18 00:02:14 +00004300 struct stat buf;
4301
plougherb3604122009-03-30 02:07:20 +00004302 if(path[0] == '/' || strncmp(path, "./", 2) == 0 ||
4303 strncmp(path, "../", 3) == 0) {
plougher1f413c82005-11-18 00:02:14 +00004304 if(lstat(path, &buf) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00004305 ERROR_START("Cannot stat exclude dir/file %s because "
4306 "%s", path, strerror(errno));
4307 ERROR_EXIT(", ignoring\n");
plougher1f413c82005-11-18 00:02:14 +00004308 return TRUE;
4309 }
4310 ADD_ENTRY(buf);
4311 return TRUE;
4312 }
4313
4314 for(i = 0; i < source; i++) {
Phillip Lougherdcb5af92012-11-30 03:05:31 +00004315 int res = asprintf(&filename, "%s/%s", source_path[i], path);
4316 if(res == -1)
4317 BAD_ERROR("asprintf failed in old_add_exclude\n");
plougher1f413c82005-11-18 00:02:14 +00004318 if(lstat(filename, &buf) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00004319 if(!(errno == ENOENT || errno == ENOTDIR)) {
4320 ERROR_START("Cannot stat exclude dir/file %s "
4321 "because %s", filename, strerror(errno));
4322 ERROR_EXIT(", ignoring\n");
4323 }
Phillip Lougherdcb5af92012-11-30 03:05:31 +00004324 free(filename);
plougher1f413c82005-11-18 00:02:14 +00004325 continue;
4326 }
Phillip Lougherdcb5af92012-11-30 03:05:31 +00004327 free(filename);
plougher1f413c82005-11-18 00:02:14 +00004328 ADD_ENTRY(buf);
4329 }
4330 return TRUE;
4331}
4332
4333
plougherb3604122009-03-30 02:07:20 +00004334void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
4335 int type)
plougher1f413c82005-11-18 00:02:14 +00004336{
plougherb3604122009-03-30 02:07:20 +00004337 old_root_entry = realloc(old_root_entry,
4338 sizeof(struct old_root_entry_info) * (old_root_entries + 1));
4339 if(old_root_entry == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004340 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00004341
ploughera326c182009-08-29 05:41:45 +00004342 old_root_entry[old_root_entries].name = strdup(name);
4343 old_root_entry[old_root_entries].inode.inode = inode;
4344 old_root_entry[old_root_entries].inode.inode_number = inode_number;
4345 old_root_entry[old_root_entries].inode.type = type;
4346 old_root_entry[old_root_entries++].inode.root_entry = TRUE;
plougher1f413c82005-11-18 00:02:14 +00004347}
4348
4349
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004350void initialise_threads(int readq, int fragq, int bwriteq, int fwriteq,
4351 int freelst, char *destination_file)
plougher5507dd92006-11-06 00:43:10 +00004352{
4353 int i;
4354 sigset_t sigmask, old_mask;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004355 int total_mem = readq;
Phillip Loughercf4c7bc2014-04-13 04:48:11 +01004356 int reader_size;
4357 int fragment_size;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004358 int fwriter_size;
plougher5741d792010-09-04 03:20:50 +00004359 /*
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004360 * bwriter_size is global because it is needed in
plougher5741d792010-09-04 03:20:50 +00004361 * write_file_blocks_dup()
4362 */
Phillip Loughere1668fe2012-12-30 05:48:12 +00004363
4364 /*
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004365 * Never allow the total size of the queues to be larger than
4366 * physical memory
4367 *
4368 * When adding together the possibly user supplied values, make
4369 * sure they've not been deliberately contrived to overflow an int
4370 */
4371 if(add_overflow(total_mem, fragq))
4372 BAD_ERROR("Queue sizes rediculously too large\n");
4373 total_mem += fragq;
4374 if(add_overflow(total_mem, bwriteq))
4375 BAD_ERROR("Queue sizes rediculously too large\n");
4376 total_mem += bwriteq;
4377 if(add_overflow(total_mem, fwriteq))
4378 BAD_ERROR("Queue sizes rediculously too large\n");
4379 total_mem += fwriteq;
4380
Phillip Lougher285a2fd2014-06-10 21:51:52 +01004381 check_usable_phys_mem(total_mem);
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004382
4383 /*
Phillip Loughere1668fe2012-12-30 05:48:12 +00004384 * convert from queue size in Mbytes to queue size in
4385 * blocks.
4386 *
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004387 * This isn't going to overflow an int unless there exists
4388 * systems with more than 8 Petabytes of RAM!
Phillip Loughere1668fe2012-12-30 05:48:12 +00004389 */
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004390 reader_size = readq << (20 - block_log);
4391 fragment_size = fragq << (20 - block_log);
4392 bwriter_size = bwriteq << (20 - block_log);
4393 fwriter_size = fwriteq << (20 - block_log);
plougher5507dd92006-11-06 00:43:10 +00004394
Phillip Lougher9391cb12013-03-20 05:16:10 +00004395 /*
4396 * setup signal handlers for the main thread, these cleanup
4397 * deleting the destination file, if appending the
4398 * handlers for SIGTERM and SIGINT will be replaced with handlers
4399 * allowing the user to press ^C twice to restore the existing
4400 * filesystem.
4401 *
Phillip Lougherdd343392013-03-31 23:29:03 +01004402 * SIGUSR1 is an internal signal, which is used by the sub-threads
Phillip Lougher9391cb12013-03-20 05:16:10 +00004403 * to tell the main thread to terminate, deleting the destination file,
4404 * or if necessary restoring the filesystem on appending
4405 */
Phillip Lougherb8ec5202013-03-28 20:14:37 +00004406 signal(SIGTERM, sighandler);
4407 signal(SIGINT, sighandler);
Phillip Lougherdd343392013-03-31 23:29:03 +01004408 signal(SIGUSR1, sighandler);
Phillip Lougher9391cb12013-03-20 05:16:10 +00004409
Phillip Lougher7538d742013-04-22 05:33:27 +01004410 /* block SIGQUIT and SIGHUP, these are handled by the info thread */
Phillip Lougher24551a82013-03-24 02:30:50 +00004411 sigemptyset(&sigmask);
4412 sigaddset(&sigmask, SIGQUIT);
Phillip Lougher7538d742013-04-22 05:33:27 +01004413 sigaddset(&sigmask, SIGHUP);
Colin Crossbe754902017-12-09 22:27:51 +00004414 sigaddset(&sigmask, SIGALRM);
Phillip Lougherb1259f72013-06-06 03:10:15 +01004415 if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
Phillip Lougher24551a82013-03-24 02:30:50 +00004416 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4417
Phillip Lougher9391cb12013-03-20 05:16:10 +00004418 /*
4419 * temporarily block these signals, so the created sub-threads
4420 * will ignore them, ensuring the main thread handles them
4421 */
plougher5507dd92006-11-06 00:43:10 +00004422 sigemptyset(&sigmask);
4423 sigaddset(&sigmask, SIGINT);
Phillip Lougher9391cb12013-03-20 05:16:10 +00004424 sigaddset(&sigmask, SIGTERM);
Phillip Lougherdd343392013-03-31 23:29:03 +01004425 sigaddset(&sigmask, SIGUSR1);
Phillip Lougher8f8d2752013-03-26 04:38:38 +00004426 if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1)
plougher5507dd92006-11-06 00:43:10 +00004427 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4428
plougher5507dd92006-11-06 00:43:10 +00004429 if(processors == -1) {
4430#ifndef linux
4431 int mib[2];
4432 size_t len = sizeof(processors);
4433
4434 mib[0] = CTL_HW;
4435#ifdef HW_AVAILCPU
4436 mib[1] = HW_AVAILCPU;
4437#else
4438 mib[1] = HW_NCPU;
4439#endif
4440
4441 if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00004442 ERROR_START("Failed to get number of available "
4443 "processors.");
4444 ERROR_EXIT(" Defaulting to 1\n");
plougher5507dd92006-11-06 00:43:10 +00004445 processors = 1;
4446 }
4447#else
plougher9cc26b72010-08-17 01:05:55 +00004448 processors = sysconf(_SC_NPROCESSORS_ONLN);
plougher5507dd92006-11-06 00:43:10 +00004449#endif
4450 }
4451
Phillip Lougher9de84ac2014-02-20 05:18:48 +00004452 if(multiply_overflow(processors, 3) ||
4453 multiply_overflow(processors * 3, sizeof(pthread_t)))
Phillip Loughere1668fe2012-12-30 05:48:12 +00004454 BAD_ERROR("Processors too large\n");
4455
Phillip Lougher9de84ac2014-02-20 05:18:48 +00004456 deflator_thread = malloc(processors * 3 * sizeof(pthread_t));
Phillip Lougher0280d992014-01-27 05:54:07 +00004457 if(deflator_thread == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004458 MEM_ERROR();
Phillip Loughere1668fe2012-12-30 05:48:12 +00004459
plougher5507dd92006-11-06 00:43:10 +00004460 frag_deflator_thread = &deflator_thread[processors];
Phillip Lougher9de84ac2014-02-20 05:18:48 +00004461 frag_thread = &frag_deflator_thread[processors];
plougher5507dd92006-11-06 00:43:10 +00004462
4463 to_reader = queue_init(1);
Phillip Loughercf4c7bc2014-04-13 04:48:11 +01004464 to_deflate = queue_init(reader_size);
4465 to_process_frag = queue_init(reader_size);
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004466 to_writer = queue_init(bwriter_size + fwriter_size);
plougher5507dd92006-11-06 00:43:10 +00004467 from_writer = queue_init(1);
Phillip Loughercf4c7bc2014-04-13 04:48:11 +01004468 to_frag = queue_init(fragment_size);
4469 locked_fragment = queue_init(fragment_size);
Phillip Lougher0e1656d2013-05-20 03:47:24 +01004470 to_main = seq_queue_init();
Phillip Loughercf4c7bc2014-04-13 04:48:11 +01004471 reader_buffer = cache_init(block_size, reader_size, 0, 0);
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004472 bwriter_buffer = cache_init(block_size, bwriter_size, 1, freelst);
4473 fwriter_buffer = cache_init(block_size, fwriter_size, 1, freelst);
Phillip Lougher1ac37152014-05-10 03:09:28 +01004474 fragment_buffer = cache_init(block_size, fragment_size, 1, 0);
4475 reserve_cache = cache_init(block_size, processors + 1, 1, 0);
Phillip Lougher0280d992014-01-27 05:54:07 +00004476 pthread_create(&reader_thread, NULL, reader, NULL);
4477 pthread_create(&writer_thread, NULL, writer, NULL);
Phillip Lougher3b89ee82012-10-18 23:55:37 +01004478 init_progress_bar();
Phillip Lougher24551a82013-03-24 02:30:50 +00004479 init_info();
plougher5507dd92006-11-06 00:43:10 +00004480
4481 for(i = 0; i < processors; i++) {
Phillip Lougher943acad2014-04-17 03:31:01 +01004482 if(pthread_create(&deflator_thread[i], NULL, deflator, NULL))
plougher5507dd92006-11-06 00:43:10 +00004483 BAD_ERROR("Failed to create thread\n");
plougher360514a2009-03-30 03:01:38 +00004484 if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator,
4485 NULL) != 0)
plougher5507dd92006-11-06 00:43:10 +00004486 BAD_ERROR("Failed to create thread\n");
Phillip Lougher8bb17b02014-03-30 23:59:55 +01004487 if(pthread_create(&frag_thread[i], NULL, frag_thrd,
4488 (void *) destination_file) != 0)
Phillip Lougher9de84ac2014-02-20 05:18:48 +00004489 BAD_ERROR("Failed to create thread\n");
plougher5507dd92006-11-06 00:43:10 +00004490 }
4491
Phillip Lougher0280d992014-01-27 05:54:07 +00004492 main_thread = pthread_self();
4493
Colin Crossbe754902017-12-09 22:27:51 +00004494 printf("Parallel mksquashfs: Using %d processor%s\n", processors,
plougher5507dd92006-11-06 00:43:10 +00004495 processors == 1 ? "" : "s");
4496
Phillip Lougher9391cb12013-03-20 05:16:10 +00004497 /* Restore the signal mask for the main thread */
Phillip Lougher8f8d2752013-03-26 04:38:38 +00004498 if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1)
plougher5507dd92006-11-06 00:43:10 +00004499 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4500}
4501
4502
plougher0e453652006-11-06 01:49:35 +00004503long long write_inode_lookup_table()
4504{
4505 int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count);
plougher44f03282010-07-27 00:31:36 +00004506 void *it;
plougher02bc3bc2007-02-25 12:12:01 +00004507
4508 if(inode_count == sinode_count)
4509 goto skip_inode_hash_table;
plougher0e453652006-11-06 01:49:35 +00004510
plougher44f03282010-07-27 00:31:36 +00004511 it = realloc(inode_lookup_table, lookup_bytes);
4512 if(it == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004513 MEM_ERROR();
plougher44f03282010-07-27 00:31:36 +00004514 inode_lookup_table = it;
plougher0e453652006-11-06 01:49:35 +00004515
plougher0e453652006-11-06 01:49:35 +00004516 for(i = 0; i < INODE_HASH_SIZE; i ++) {
Phillip Lougherf2f83b62014-04-18 01:30:47 +01004517 struct inode_info *inode;
plougher0e453652006-11-06 01:49:35 +00004518
4519 for(inode = inode_info[i]; inode; inode = inode->next) {
plougher0e453652006-11-06 01:49:35 +00004520
Phillip Lougher539c2b12012-07-30 20:14:52 +01004521 inode_number = get_inode_no(inode);
plougher0e453652006-11-06 01:49:35 +00004522
Phillip Lougher9f595962014-07-07 04:37:47 +01004523 /* The empty action will produce orphaned inode
4524 * entries in the inode_info[] table. These
4525 * entries because they are orphaned will not be
4526 * allocated an inode number in dir_scan5(), so
4527 * skip any entries with the default dummy inode
4528 * number of 0 */
4529 if(inode_number == 0)
4530 continue;
4531
plougher360514a2009-03-30 03:01:38 +00004532 SQUASHFS_SWAP_LONG_LONGS(&inode->inode,
4533 &inode_lookup_table[inode_number - 1], 1);
plougher0e453652006-11-06 01:49:35 +00004534
plougher0e453652006-11-06 01:49:35 +00004535 }
4536 }
4537
plougher02bc3bc2007-02-25 12:12:01 +00004538skip_inode_hash_table:
ploughera0a49c32010-08-11 01:47:59 +00004539 return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL,
4540 noI);
plougher0e453652006-11-06 01:49:35 +00004541}
4542
plougher2ea89142008-03-11 01:34:19 +00004543
Phillip Lougher8e44e052012-11-26 02:58:35 +00004544char *get_component(char *target, char **targname)
plougher8f8e1a12007-10-18 02:50:21 +00004545{
Phillip Lougher8e44e052012-11-26 02:58:35 +00004546 char *start;
4547
plougher8f8e1a12007-10-18 02:50:21 +00004548 while(*target == '/')
rlougherc4ebcf52007-11-08 17:52:49 +00004549 target ++;
plougher8f8e1a12007-10-18 02:50:21 +00004550
Phillip Lougher8e44e052012-11-26 02:58:35 +00004551 start = target;
Phillip Lougher66d8b8e2014-04-20 21:01:33 +01004552 while(*target != '/' && *target != '\0')
Phillip Lougher8e44e052012-11-26 02:58:35 +00004553 target ++;
plougher8f8e1a12007-10-18 02:50:21 +00004554
Phillip Lougher8e44e052012-11-26 02:58:35 +00004555 *targname = strndup(start, target - start);
plougher8f8e1a12007-10-18 02:50:21 +00004556
Phillip Lougher66d8b8e2014-04-20 21:01:33 +01004557 while(*target == '/')
4558 target ++;
4559
plougher8f8e1a12007-10-18 02:50:21 +00004560 return target;
4561}
4562
4563
4564void free_path(struct pathname *paths)
4565{
4566 int i;
4567
4568 for(i = 0; i < paths->names; i++) {
4569 if(paths->name[i].paths)
4570 free_path(paths->name[i].paths);
4571 free(paths->name[i].name);
4572 if(paths->name[i].preg) {
4573 regfree(paths->name[i].preg);
4574 free(paths->name[i].preg);
4575 }
4576 }
4577
4578 free(paths);
4579}
4580
4581
4582struct pathname *add_path(struct pathname *paths, char *target, char *alltarget)
4583{
Phillip Lougher8e44e052012-11-26 02:58:35 +00004584 char *targname;
plougher8f8e1a12007-10-18 02:50:21 +00004585 int i, error;
4586
Phillip Lougher8e44e052012-11-26 02:58:35 +00004587 target = get_component(target, &targname);
plougher8f8e1a12007-10-18 02:50:21 +00004588
4589 if(paths == NULL) {
plougherdec6ef12010-12-18 02:47:53 +00004590 paths = malloc(sizeof(struct pathname));
4591 if(paths == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004592 MEM_ERROR();
plougher8f8e1a12007-10-18 02:50:21 +00004593
4594 paths->names = 0;
4595 paths->name = NULL;
4596 }
4597
4598 for(i = 0; i < paths->names; i++)
4599 if(strcmp(paths->name[i].name, targname) == 0)
4600 break;
4601
4602 if(i == paths->names) {
4603 /* allocate new name entry */
4604 paths->names ++;
plougherb3604122009-03-30 02:07:20 +00004605 paths->name = realloc(paths->name, (i + 1) *
4606 sizeof(struct path_entry));
plougher6f2a8262010-07-27 00:37:19 +00004607 if(paths->name == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004608 MEM_ERROR();
Phillip Lougher8e44e052012-11-26 02:58:35 +00004609 paths->name[i].name = targname;
plougher8f8e1a12007-10-18 02:50:21 +00004610 paths->name[i].paths = NULL;
4611 if(use_regex) {
4612 paths->name[i].preg = malloc(sizeof(regex_t));
plougher5c60eab2010-07-21 01:12:19 +00004613 if(paths->name[i].preg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004614 MEM_ERROR();
plougherb3604122009-03-30 02:07:20 +00004615 error = regcomp(paths->name[i].preg, targname,
4616 REG_EXTENDED|REG_NOSUB);
plougher23377982007-11-12 04:04:48 +00004617 if(error) {
Phillip Lougher62859d22012-11-30 05:53:56 +00004618 char str[1024]; /* overflow safe */
plougher8f8e1a12007-10-18 02:50:21 +00004619
4620 regerror(error, paths->name[i].preg, str, 1024);
plougherb3604122009-03-30 02:07:20 +00004621 BAD_ERROR("invalid regex %s in export %s, "
plougher50b31762009-03-31 04:14:46 +00004622 "because %s\n", targname, alltarget,
4623 str);
plougher8f8e1a12007-10-18 02:50:21 +00004624 }
4625 } else
4626 paths->name[i].preg = NULL;
4627
4628 if(target[0] == '\0')
4629 /* at leaf pathname component */
4630 paths->name[i].paths = NULL;
4631 else
4632 /* recurse adding child components */
plougher50b31762009-03-31 04:14:46 +00004633 paths->name[i].paths = add_path(NULL, target,
4634 alltarget);
plougher8f8e1a12007-10-18 02:50:21 +00004635 } else {
4636 /* existing matching entry */
Phillip Lougher8e44e052012-11-26 02:58:35 +00004637 free(targname);
4638
plougher8f8e1a12007-10-18 02:50:21 +00004639 if(paths->name[i].paths == NULL) {
plougher50b31762009-03-31 04:14:46 +00004640 /* No sub-directory which means this is the leaf
4641 * component of a pre-existing exclude which subsumes
4642 * the exclude currently being added, in which case stop
4643 * adding components */
plougher8f8e1a12007-10-18 02:50:21 +00004644 } else if(target[0] == '\0') {
plougherb3604122009-03-30 02:07:20 +00004645 /* at leaf pathname component and child components exist
plougher50b31762009-03-31 04:14:46 +00004646 * from more specific excludes, delete as they're
4647 * subsumed by this exclude */
plougher8f8e1a12007-10-18 02:50:21 +00004648 free_path(paths->name[i].paths);
4649 paths->name[i].paths = NULL;
4650 } else
4651 /* recurse adding child components */
4652 add_path(paths->name[i].paths, target, alltarget);
4653 }
4654
4655 return paths;
4656}
plougher2ea89142008-03-11 01:34:19 +00004657
4658
plougher05e50ef2007-10-23 12:34:20 +00004659void add_exclude(char *target)
plougher8f8e1a12007-10-18 02:50:21 +00004660{
plougher05e50ef2007-10-23 12:34:20 +00004661
plougherb3604122009-03-30 02:07:20 +00004662 if(target[0] == '/' || strncmp(target, "./", 2) == 0 ||
4663 strncmp(target, "../", 3) == 0)
4664 BAD_ERROR("/, ./ and ../ prefixed excludes not supported with "
4665 "-wildcards or -regex options\n");
plougher05e50ef2007-10-23 12:34:20 +00004666 else if(strncmp(target, "... ", 4) == 0)
4667 stickypath = add_path(stickypath, target + 4, target + 4);
4668 else
4669 path = add_path(path, target, target);
plougher8f8e1a12007-10-18 02:50:21 +00004670}
4671
4672
4673void display_path(int depth, struct pathname *paths)
4674{
4675 int i, n;
4676
4677 if(paths == NULL)
4678 return;
4679
4680 for(i = 0; i < paths->names; i++) {
4681 for(n = 0; n < depth; n++)
4682 printf("\t");
4683 printf("%d: %s\n", depth, paths->name[i].name);
4684 display_path(depth + 1, paths->name[i].paths);
4685 }
4686}
4687
4688
4689void display_path2(struct pathname *paths, char *string)
4690{
4691 int i;
Phillip Lougher91459c12012-11-30 05:24:44 +00004692 char *path;
plougher8f8e1a12007-10-18 02:50:21 +00004693
4694 if(paths == NULL) {
4695 printf("%s\n", string);
4696 return;
4697 }
4698
4699 for(i = 0; i < paths->names; i++) {
Phillip Lougher91459c12012-11-30 05:24:44 +00004700 int res = asprintf(&path, "%s/%s", string, paths->name[i].name);
4701 if(res == -1)
4702 BAD_ERROR("asprintf failed in display_path2\n");
plougher8f8e1a12007-10-18 02:50:21 +00004703 display_path2(paths->name[i].paths, path);
Phillip Lougher91459c12012-11-30 05:24:44 +00004704 free(path);
plougher8f8e1a12007-10-18 02:50:21 +00004705 }
4706}
4707
4708
plougherf9039c92007-10-22 03:54:16 +00004709struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path)
4710{
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004711 int count = paths == NULL ? 0 : paths->count;
4712
4713 if(count % PATHS_ALLOC_SIZE == 0) {
4714 paths = realloc(paths, sizeof(struct pathnames) +
4715 (count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *));
plougher6f2a8262010-07-27 00:37:19 +00004716 if(paths == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004717 MEM_ERROR();
plougher6f2a8262010-07-27 00:37:19 +00004718 }
plougherf9039c92007-10-22 03:54:16 +00004719
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004720 paths->path[count] = path;
4721 paths->count = count + 1;
plougherf9039c92007-10-22 03:54:16 +00004722 return paths;
4723}
4724
4725
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004726int excluded_match(char *name, struct pathname *path, struct pathnames **new)
plougherf9039c92007-10-22 03:54:16 +00004727{
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004728 int i;
plougher8f8e1a12007-10-18 02:50:21 +00004729
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004730 for(i = 0; i < path->names; i++) {
4731 int match = use_regex ?
4732 regexec(path->name[i].preg, name, (size_t) 0,
plougher50b31762009-03-31 04:14:46 +00004733 NULL, 0) == 0 :
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004734 fnmatch(path->name[i].name, name,
4735 FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0;
plougherf9039c92007-10-22 03:54:16 +00004736
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004737 if(match) {
4738 if(path->name[i].paths == NULL || new == NULL)
plougherb3604122009-03-30 02:07:20 +00004739 /* match on a leaf component, any subdirectories
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004740 * in the filesystem should be excluded */
4741 return TRUE;
4742 else
plougherb3604122009-03-30 02:07:20 +00004743 /* match on a non-leaf component, add any
plougher50b31762009-03-31 04:14:46 +00004744 * subdirectories to the new set of
4745 * subdirectories to scan for this name */
plougherf9039c92007-10-22 03:54:16 +00004746 *new = add_subdir(*new, path->name[i].paths);
4747 }
4748 }
4749
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004750 return FALSE;
4751}
4752
4753
4754int excluded(char *name, struct pathnames *paths, struct pathnames **new)
4755{
4756 int n;
4757
4758 if(stickypath && excluded_match(name, stickypath, NULL))
4759 return TRUE;
4760
4761 for(n = 0; paths && n < paths->count; n++) {
4762 int res = excluded_match(name, paths->path[n], new);
4763 if(res) {
4764 free(*new);
4765 *new = NULL;
4766 return TRUE;
4767 }
plougherf9039c92007-10-22 03:54:16 +00004768 }
4769
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004770 /*
4771 * Either:
4772 * - no matching names found, return empty new search set, or
4773 * - one or more matches with sub-directories found (no leaf matches),
4774 * in which case return new search set.
4775 *
4776 * In either case return FALSE as we don't want to exclude this entry
4777 */
plougher8f8e1a12007-10-18 02:50:21 +00004778 return FALSE;
4779}
4780
4781
Phillip Lougher386128f2012-12-16 05:23:45 +00004782void process_exclude_file(char *argv)
4783{
4784 FILE *fd;
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004785 char buffer[MAX_LINE + 1]; /* overflow safe */
Phillip Lougherb22336d2012-12-20 18:44:22 +00004786 char *filename;
Phillip Lougher386128f2012-12-16 05:23:45 +00004787
Phillip Lougherb22336d2012-12-20 18:44:22 +00004788 fd = fopen(argv, "r");
4789 if(fd == NULL)
4790 BAD_ERROR("Failed to open exclude file \"%s\" because %s\n",
4791 argv, strerror(errno));
Phillip Lougher386128f2012-12-16 05:23:45 +00004792
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004793 while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) {
Phillip Lougherb22336d2012-12-20 18:44:22 +00004794 int len = strlen(filename);
4795
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004796 if(len == MAX_LINE && filename[len - 1] != '\n')
Phillip Lougherb22336d2012-12-20 18:44:22 +00004797 /* line too large */
4798 BAD_ERROR("Line too long when reading "
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004799 "exclude file \"%s\", larger than %d "
Phillip Lougher83847c12012-12-23 07:22:36 +00004800 "bytes\n", argv, MAX_LINE);
Phillip Lougherb22336d2012-12-20 18:44:22 +00004801
4802 /*
4803 * Remove '\n' terminator if it exists (the last line
4804 * in the file may not be '\n' terminated)
4805 */
4806 if(len && filename[len - 1] == '\n')
4807 filename[len - 1] = '\0';
4808
4809 /* Skip any leading whitespace */
4810 while(isspace(*filename))
4811 filename ++;
4812
4813 /* if comment line, skip */
4814 if(*filename == '#')
4815 continue;
4816
4817 /*
4818 * check for initial backslash, to accommodate
4819 * filenames with leading space or leading # character
4820 */
4821 if(*filename == '\\')
4822 filename ++;
4823
4824 /* if line is now empty after skipping characters, skip it */
4825 if(*filename == '\0')
4826 continue;
4827
Phillip Lougher386128f2012-12-16 05:23:45 +00004828 if(old_exclude)
4829 old_add_exclude(filename);
4830 else
4831 add_exclude(filename);
Phillip Lougherb22336d2012-12-20 18:44:22 +00004832 }
4833
4834 if(ferror(fd))
4835 BAD_ERROR("Reading exclude file \"%s\" failed because %s\n",
4836 argv, strerror(errno));
Phillip Lougher386128f2012-12-16 05:23:45 +00004837
4838 fclose(fd);
4839}
4840
Mohan Srinivasan58e18d72016-07-26 15:06:29 -07004841/* ANDROID CHANGES START*/
4842#ifdef ANDROID
4843/*
4844 * Return TRUE (don't compress) if the (regular) file is in the
4845 * whitelist. Else return the Global noD value.
4846 *
4847 * Note : These functions are lifted 100% from the existing exclude
4848 * file code. For maintainability, I've kept this code separate from
4849 * the exclude code instead of having common code for both paths.
4850 */
4851static int
4852whitelisted(struct stat *buf)
4853{
4854 int i;
4855
4856 /*
4857 * only regular files in the whitelist
4858 */
4859 if (!S_ISREG(buf->st_mode))
4860 return noD;
4861 for (i = 0; i < whitelist; i++) {
4862 if ((whitelist_paths[i].st_dev == buf->st_dev) &&
4863 (whitelist_paths[i].st_ino == buf->st_ino)) {
4864 /* Don't compress */
4865 whitelisted_count++;
4866 return TRUE;
4867 }
4868 }
4869 return noD;
4870}
4871
4872static void
4873add_whitelist_entry(char *filename, struct stat *buf)
4874{
4875 if (!S_ISREG(buf->st_mode)) {
4876 BAD_ERROR("Cannot whitelist %s only regular files can be whitelisted",
4877 filename);
4878 }
4879 if (whitelist % WHITELIST_SIZE == 0) {
4880 whitelist_paths = realloc(whitelist_paths,
4881 (whitelist + WHITELIST_SIZE)
4882 * sizeof(struct whitelist_info));
4883 if (whitelist_paths == NULL)
4884 MEM_ERROR();
4885 }
4886 whitelist_paths[whitelist].st_dev = buf->st_dev;
4887 whitelist_paths[whitelist++].st_ino = buf->st_ino;
4888}
4889
4890static int
4891add_whitelist(char *path)
4892{
4893 int i;
4894 char *filename;
4895 struct stat buf;
4896
4897 /* Absolute of (filesystem) relative path */
4898 if (path[0] == '/' || strncmp(path, "./", 2) == 0 ||
4899 strncmp(path, "../", 3) == 0) {
4900 if(lstat(path, &buf) == -1) {
4901 BAD_ERROR("Cannot stat whitelist dir/file %s because "
4902 "%s", path, strerror(errno));
4903 }
4904 add_whitelist_entry(path, &buf);
4905 return TRUE;
4906 }
4907
4908 /* pathname relative to mksquashfs source dirs */
4909 for(i = 0; i < source; i++) {
4910 int res = asprintf(&filename, "%s/%s", source_path[i], path);
4911 if(res == -1)
4912 BAD_ERROR("asprintf failed in add_whitelist\n");
4913 if(lstat(filename, &buf) == -1) {
4914 if(!(errno == ENOENT || errno == ENOTDIR)) {
4915 BAD_ERROR("Cannot stat whitelist dir/file %s "
4916 "because %s", filename, strerror(errno));
4917 }
4918 free(filename);
4919 continue;
4920 }
4921 add_whitelist_entry(filename, &buf);
4922 free(filename);
4923 }
4924 return TRUE;
4925}
4926
4927static void
4928process_whitelist_file(char *argv)
4929{
4930 FILE *fd;
4931 char buffer[MAX_LINE + 1]; /* overflow safe */
4932 char *filename;
4933
4934 fd = fopen(argv, "r");
4935 if(fd == NULL)
4936 BAD_ERROR("Failed to open whitelist file \"%s\" because %s\n",
4937 argv, strerror(errno));
4938
4939 while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) {
4940 int len = strlen(filename);
4941
4942 if(len == MAX_LINE && filename[len - 1] != '\n')
4943 /* line too large */
4944 BAD_ERROR("Line too long when reading "
4945 "whitelist file \"%s\", larger than %d "
4946 "bytes\n", argv, MAX_LINE);
4947
4948 /*
4949 * Remove '\n' terminator if it exists (the last line
4950 * in the file may not be '\n' terminated)
4951 */
4952 if(len && filename[len - 1] == '\n')
4953 filename[len - 1] = '\0';
4954
4955 /* Skip any leading whitespace */
4956 while(isspace(*filename))
4957 filename ++;
4958
4959 /* if comment line, skip */
4960 if(*filename == '#')
4961 continue;
4962
4963 /*
4964 * check for initial backslash, to accommodate
4965 * filenames with leading space or leading # character
4966 */
4967 if(*filename == '\\')
4968 filename ++;
4969
4970 /* if line is now empty after skipping characters, skip it */
4971 if(*filename == '\0')
4972 continue;
4973
4974 add_whitelist(filename);
4975 }
4976
4977 if(ferror(fd))
4978 BAD_ERROR("Reading whitelist file \"%s\" failed because %s\n",
4979 argv, strerror(errno));
4980
4981 fclose(fd);
4982}
4983#endif
4984/* ANDROID CHANGES END */
Phillip Lougher386128f2012-12-16 05:23:45 +00004985
plougher99ac0cc2007-10-29 03:17:10 +00004986#define RECOVER_ID "Squashfs recovery file v1.0\n"
4987#define RECOVER_ID_SIZE 28
4988
plougher64e83fd2010-12-31 21:21:26 +00004989void write_recovery_data(struct squashfs_super_block *sBlk)
plougher99ac0cc2007-10-29 03:17:10 +00004990{
plougher1d065e92010-06-18 03:58:27 +00004991 int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start;
plougher99ac0cc2007-10-29 03:17:10 +00004992 pid_t pid = getpid();
plougher44d54ef2010-02-08 22:13:49 +00004993 char *metadata;
plougher99ac0cc2007-10-29 03:17:10 +00004994 char header[] = RECOVER_ID;
4995
4996 if(recover == FALSE) {
4997 printf("No recovery data option specified.\n");
ploughereac18532007-10-29 05:26:06 +00004998 printf("Skipping saving recovery file.\n\n");
plougher99ac0cc2007-10-29 03:17:10 +00004999 return;
5000 }
5001
plougher1b879bc2010-12-18 02:49:42 +00005002 metadata = malloc(bytes);
5003 if(metadata == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00005004 MEM_ERROR();
plougher44d54ef2010-02-08 22:13:49 +00005005
plougher1d065e92010-06-18 03:58:27 +00005006 res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata);
Phillip Lougher477f4332013-03-06 03:55:08 +00005007 if(res == 0) {
5008 ERROR("Failed to read append filesystem metadata\n");
5009 BAD_ERROR("Filesystem corrupted?\n");
5010 }
plougher99ac0cc2007-10-29 03:17:10 +00005011
Phillip Lougheraf4c9232012-11-15 03:46:28 +00005012 res = asprintf(&recovery_file, "squashfs_recovery_%s_%d",
plougher44d54ef2010-02-08 22:13:49 +00005013 getbase(destination_file), pid);
Phillip Lougheraf4c9232012-11-15 03:46:28 +00005014 if(res == -1)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00005015 MEM_ERROR();
Phillip Lougheraf4c9232012-11-15 03:46:28 +00005016
plougherb3604122009-03-30 02:07:20 +00005017 recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
5018 if(recoverfd == -1)
5019 BAD_ERROR("Failed to create recovery file, because %s. "
5020 "Aborting\n", strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00005021
plougher628e7682009-03-29 22:12:24 +00005022 if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1)
plougherb3604122009-03-30 02:07:20 +00005023 BAD_ERROR("Failed to write recovery file, because %s\n",
5024 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00005025
plougher64e83fd2010-12-31 21:21:26 +00005026 if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1)
plougherb3604122009-03-30 02:07:20 +00005027 BAD_ERROR("Failed to write recovery file, because %s\n",
5028 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00005029
plougher628e7682009-03-29 22:12:24 +00005030 if(write_bytes(recoverfd, metadata, bytes) == -1)
plougherb3604122009-03-30 02:07:20 +00005031 BAD_ERROR("Failed to write recovery file, because %s\n",
5032 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00005033
5034 close(recoverfd);
plougher44d54ef2010-02-08 22:13:49 +00005035 free(metadata);
plougher99ac0cc2007-10-29 03:17:10 +00005036
5037 printf("Recovery file \"%s\" written\n", recovery_file);
5038 printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n");
plougherb3604122009-03-30 02:07:20 +00005039 printf("mksquashfs dummy %s -recover %s\n", destination_file,
5040 recovery_file);
plougher99ac0cc2007-10-29 03:17:10 +00005041 printf("to restore filesystem\n\n");
5042}
5043
5044
5045void read_recovery_data(char *recovery_file, char *destination_file)
5046{
5047 int fd, recoverfd, bytes;
plougher64e83fd2010-12-31 21:21:26 +00005048 struct squashfs_super_block orig_sBlk, sBlk;
plougher99ac0cc2007-10-29 03:17:10 +00005049 char *metadata;
plougher8a8c4102009-03-29 22:28:49 +00005050 int res;
plougher99ac0cc2007-10-29 03:17:10 +00005051 struct stat buf;
5052 char header[] = RECOVER_ID;
5053 char header2[RECOVER_ID_SIZE];
5054
plougher9e9d9dc2010-12-18 02:53:57 +00005055 recoverfd = open(recovery_file, O_RDONLY);
5056 if(recoverfd == -1)
plougherb3604122009-03-30 02:07:20 +00005057 BAD_ERROR("Failed to open recovery file because %s\n",
5058 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00005059
5060 if(stat(destination_file, &buf) == -1)
plougherb3604122009-03-30 02:07:20 +00005061 BAD_ERROR("Failed to stat destination file, because %s\n",
5062 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00005063
plougher9e9d9dc2010-12-18 02:53:57 +00005064 fd = open(destination_file, O_RDWR);
5065 if(fd == -1)
plougherb3604122009-03-30 02:07:20 +00005066 BAD_ERROR("Failed to open destination file because %s\n",
5067 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00005068
plougher8a8c4102009-03-29 22:28:49 +00005069 res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE);
5070 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00005071 BAD_ERROR("Failed to read recovery file, because %s\n",
5072 strerror(errno));
plougher8a8c4102009-03-29 22:28:49 +00005073 if(res < RECOVER_ID_SIZE)
5074 BAD_ERROR("Recovery file appears to be truncated\n");
plougher99ac0cc2007-10-29 03:17:10 +00005075 if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 )
5076 BAD_ERROR("Not a recovery file\n");
5077
plougher64e83fd2010-12-31 21:21:26 +00005078 res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block));
plougher8a8c4102009-03-29 22:28:49 +00005079 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00005080 BAD_ERROR("Failed to read recovery file, because %s\n",
5081 strerror(errno));
plougher64e83fd2010-12-31 21:21:26 +00005082 if(res < sizeof(struct squashfs_super_block))
plougher8a8c4102009-03-29 22:28:49 +00005083 BAD_ERROR("Recovery file appears to be truncated\n");
plougher99ac0cc2007-10-29 03:17:10 +00005084
plougher64e83fd2010-12-31 21:21:26 +00005085 res = read_fs_bytes(fd, 0, sizeof(struct squashfs_super_block), &orig_sBlk);
Phillip Lougher477f4332013-03-06 03:55:08 +00005086 if(res == 0) {
5087 ERROR("Failed to read superblock from output filesystem\n");
5088 BAD_ERROR("Output filesystem is empty!\n");
5089 }
plougher99ac0cc2007-10-29 03:17:10 +00005090
plougherb3604122009-03-30 02:07:20 +00005091 if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4,
plougher64e83fd2010-12-31 21:21:26 +00005092 sizeof(struct squashfs_super_block) - 4) != 0)
plougherb3604122009-03-30 02:07:20 +00005093 BAD_ERROR("Recovery file and destination file do not seem to "
5094 "match\n");
plougher99ac0cc2007-10-29 03:17:10 +00005095
5096 bytes = sBlk.bytes_used - sBlk.inode_table_start;
5097
plougher9e9d9dc2010-12-18 02:53:57 +00005098 metadata = malloc(bytes);
5099 if(metadata == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00005100 MEM_ERROR();
plougher99ac0cc2007-10-29 03:17:10 +00005101
plougher8a8c4102009-03-29 22:28:49 +00005102 res = read_bytes(recoverfd, metadata, bytes);
5103 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00005104 BAD_ERROR("Failed to read recovery file, because %s\n",
5105 strerror(errno));
plougher8a8c4102009-03-29 22:28:49 +00005106 if(res < bytes)
plougher99ac0cc2007-10-29 03:17:10 +00005107 BAD_ERROR("Recovery file appears to be truncated\n");
5108
plougher64e83fd2010-12-31 21:21:26 +00005109 write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk);
plougher99ac0cc2007-10-29 03:17:10 +00005110
plougher0dd6f122009-03-29 21:43:57 +00005111 write_destination(fd, sBlk.inode_table_start, bytes, metadata);
plougher99ac0cc2007-10-29 03:17:10 +00005112
5113 close(recoverfd);
5114 close(fd);
5115
plougherb3604122009-03-30 02:07:20 +00005116 printf("Successfully wrote recovery file \"%s\". Exiting\n",
5117 recovery_file);
plougher99ac0cc2007-10-29 03:17:10 +00005118
5119 exit(0);
5120}
5121
5122
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00005123void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad)
5124{
5125 int i;
5126
5127 sBlk->fragments = fragments;
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00005128 sBlk->no_ids = id_count;
5129 sBlk->inode_table_start = write_inodes();
5130 sBlk->directory_table_start = write_directories();
5131 sBlk->fragment_table_start = write_fragment_table();
5132 sBlk->lookup_table_start = exportable ? write_inode_lookup_table() :
5133 SQUASHFS_INVALID_BLK;
5134 sBlk->id_table_start = write_id_table();
5135 sBlk->xattr_id_table_start = write_xattrs();
5136
5137 TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start);
5138 TRACE("sBlk->directory_table_start 0x%llx\n",
5139 sBlk->directory_table_start);
5140 TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start);
5141 if(exportable)
5142 TRACE("sBlk->lookup_table_start 0x%llx\n",
5143 sBlk->lookup_table_start);
5144
5145 sBlk->bytes_used = bytes;
5146
5147 sBlk->compression = comp->id;
5148
5149 SQUASHFS_INSWAP_SUPER_BLOCK(sBlk);
5150 write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk);
5151
5152 if(!nopad && (i = bytes & (4096 - 1))) {
5153 char temp[4096] = {0};
5154 write_destination(fd, bytes, 4096 - i, temp);
5155 }
5156
5157 close(fd);
5158
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00005159 if(recovery_file)
5160 unlink(recovery_file);
5161
5162 total_bytes += total_inode_bytes + total_directory_bytes +
5163 sizeof(struct squashfs_super_block) + total_xattr_bytes;
5164
5165 printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size"
5166 " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR,
5167 SQUASHFS_MINOR, comp->name, block_size);
5168 printf("\t%s data, %s metadata, %s fragments, %s xattrs\n",
5169 noD ? "uncompressed" : "compressed", noI ? "uncompressed" :
5170 "compressed", no_fragments ? "no" : noF ? "uncompressed" :
5171 "compressed", no_xattrs ? "no" : noX ? "uncompressed" :
5172 "compressed");
5173 printf("\tduplicates are %sremoved\n", duplicate_checking ? "" :
5174 "not ");
5175 printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0,
5176 bytes / (1024.0 * 1024.0));
5177 printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n",
5178 ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0);
5179 printf("Inode table size %d bytes (%.2f Kbytes)\n",
5180 inode_bytes, inode_bytes / 1024.0);
5181 printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n",
5182 ((float) inode_bytes / total_inode_bytes) * 100.0,
5183 total_inode_bytes);
5184 printf("Directory table size %d bytes (%.2f Kbytes)\n",
5185 directory_bytes, directory_bytes / 1024.0);
5186 printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n",
5187 ((float) directory_bytes / total_directory_bytes) * 100.0,
5188 total_directory_bytes);
5189 if(total_xattr_bytes) {
5190 printf("Xattr table size %d bytes (%.2f Kbytes)\n",
5191 xattr_bytes, xattr_bytes / 1024.0);
5192 printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n",
5193 ((float) xattr_bytes / total_xattr_bytes) * 100.0,
5194 total_xattr_bytes);
5195 }
5196 if(duplicate_checking)
5197 printf("Number of duplicate files found %d\n", file_count -
5198 dup_files);
5199 else
5200 printf("No duplicate files removed\n");
5201 printf("Number of inodes %d\n", inode_count);
5202 printf("Number of files %d\n", file_count);
5203 if(!no_fragments)
5204 printf("Number of fragments %d\n", fragments);
5205 printf("Number of symbolic links %d\n", sym_count);
5206 printf("Number of device nodes %d\n", dev_count);
5207 printf("Number of fifo nodes %d\n", fifo_count);
5208 printf("Number of socket nodes %d\n", sock_count);
5209 printf("Number of directories %d\n", dir_count);
5210 printf("Number of ids (unique uids + gids) %d\n", id_count);
5211 printf("Number of uids %d\n", uid_count);
5212
5213 for(i = 0; i < id_count; i++) {
5214 if(id_table[i]->flags & ISA_UID) {
5215 struct passwd *user = getpwuid(id_table[i]->id);
5216 printf("\t%s (%d)\n", user == NULL ? "unknown" :
5217 user->pw_name, id_table[i]->id);
5218 }
5219 }
5220
5221 printf("Number of gids %d\n", guid_count);
5222
5223 for(i = 0; i < id_count; i++) {
5224 if(id_table[i]->flags & ISA_GID) {
5225 struct group *group = getgrgid(id_table[i]->id);
5226 printf("\t%s (%d)\n", group == NULL ? "unknown" :
5227 group->gr_name, id_table[i]->id);
5228 }
5229 }
Colin Crossbe754902017-12-09 22:27:51 +00005230
5231 printf("Number of whitelisted (uncompressed) files %d\n",
5232 whitelisted_count);
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00005233}
5234
5235
Phillip Lougher57e2f692014-05-05 23:50:24 +01005236int parse_numberll(char *start, long long *res, int size)
5237{
5238 char *end;
5239 long long number;
5240
5241 errno = 0; /* To distinguish success/failure after call */
5242
5243 number = strtoll(start, &end, 10);
5244
5245 /*
5246 * check for strtoll underflow or overflow in conversion, and other
5247 * errors.
5248 */
5249 if((errno == ERANGE && (number == LLONG_MIN || number == LLONG_MAX)) ||
5250 (errno != 0 && number == 0))
5251 return 0;
5252
5253 /* reject negative numbers as invalid */
5254 if(number < 0)
5255 return 0;
5256
5257 if(size) {
5258 /*
5259 * Check for multiplier and trailing junk.
5260 * But first check that a number exists before the
5261 * multiplier
5262 */
5263 if(end == start)
5264 return 0;
5265
5266 switch(end[0]) {
5267 case 'g':
5268 case 'G':
5269 if(multiply_overflowll(number, 1073741824))
5270 return 0;
5271 number *= 1073741824;
5272
5273 if(end[1] != '\0')
5274 /* trailing junk after multiplier, but
5275 * allow it to be "bytes" */
5276 if(strcmp(end + 1, "bytes"))
5277 return 0;
5278
5279 break;
5280 case 'm':
5281 case 'M':
5282 if(multiply_overflowll(number, 1048576))
5283 return 0;
5284 number *= 1048576;
5285
5286 if(end[1] != '\0')
5287 /* trailing junk after multiplier, but
5288 * allow it to be "bytes" */
5289 if(strcmp(end + 1, "bytes"))
5290 return 0;
5291
5292 break;
5293 case 'k':
5294 case 'K':
5295 if(multiply_overflowll(number, 1024))
5296 return 0;
5297 number *= 1024;
5298
5299 if(end[1] != '\0')
5300 /* trailing junk after multiplier, but
5301 * allow it to be "bytes" */
5302 if(strcmp(end + 1, "bytes"))
5303 return 0;
5304
5305 break;
5306 case '\0':
5307 break;
5308 default:
5309 /* trailing junk after number */
5310 return 0;
5311 }
5312 } else if(end[0] != '\0')
5313 /* trailing junk after number */
5314 return 0;
5315
5316 *res = number;
5317 return 1;
5318}
5319
5320
5321int parse_number(char *start, int *res, int size)
5322{
5323 long long number;
5324
5325 if(!parse_numberll(start, &number, size))
5326 return 0;
5327
5328 /* check if long result will overflow signed int */
5329 if(number > INT_MAX)
5330 return 0;
5331
5332 *res = (int) number;
5333 return 1;
5334}
5335
5336
Phillip Lougher94c1fe02012-11-28 03:32:36 +00005337int parse_num(char *arg, int *res)
5338{
5339 return parse_number(arg, res, 0);
5340}
5341
5342
Hidehiko Abef240b142018-05-08 22:45:47 +09005343int parse_ugid_map(char *map_str,
5344 struct ugid_map_entry ugid_mapping[UGID_ENTRIES],
5345 unsigned int *ugid_map_count)
5346{
5347 char *line_state, *token_state;
5348 char *line, *line_str, *token, *token_str;
5349 long long numbers[3];
5350 int i;
5351
5352 for (*ugid_map_count = 0, line_str = map_str;;
5353 ++*ugid_map_count, line_str = NULL) {
5354 line = strtok_r(line_str, "\n", &line_state);
5355 if (line == NULL)
5356 break;
5357 ERROR("line: %s\n", line);
5358 if (*ugid_map_count >= UGID_ENTRIES) {
5359 ERROR("Too many entries for u/gid mapping\n");
5360 return -1;
5361 }
5362
5363 for (i = 0, token_str = line; i < 3; i++, token_str = NULL) {
5364 token = strtok_r(token_str, " ", &token_state);
5365 ERROR("token: %d, %s\n", i, token);
5366 if (token == NULL ||
5367 !parse_numberll(token, &numbers[i], 0) ||
5368 numbers[i] < 0 || numbers[i] > ULONG_MAX) {
5369 ERROR("Malformed u/gid mapping line1\n");
5370 return -1;
5371 }
5372 }
5373
5374 if (numbers[0] + numbers[2] > ULONG_MAX) {
5375 ERROR("u/gid mapping overflow\n");
5376 return -1;
5377 }
5378
5379 if (numbers[1] + numbers[2] > ULONG_MAX) {
5380 ERROR("u/gid mapping overflow\n");
5381 return -1;
5382 }
5383
5384 if (strtok_r(NULL, " ", &token_state) != NULL) {
5385 ERROR("Malformed u/gid mapping line2\n");
5386 return -1;
5387 }
5388
5389 ugid_mapping[*ugid_map_count].child_id =
5390 (unsigned int)numbers[0];
5391 ugid_mapping[*ugid_map_count].parent_id =
5392 (unsigned int)numbers[1];
5393 ugid_mapping[*ugid_map_count].length = (unsigned int)numbers[2];
5394 }
5395
5396 return 0;
5397}
5398
5399
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005400int get_physical_memory()
5401{
Colin Crossbe754902017-12-09 22:27:51 +00005402 int phys_mem;
5403#ifndef linux
5404 #ifdef HW_MEMSIZE
5405 #define SYSCTL_PHYSMEM HW_MEMSIZE
5406 #elif defined(HW_PHYSMEM64)
5407 #define SYSCTL_PHYSMEM HW_PHYSMEM64
5408 #else
5409 #define SYSCTL_PHYSMEM HW_PHYSMEM
5410 #endif
5411
5412 int mib[2];
5413 uint64_t sysctl_physmem = 0;
5414 size_t sysctl_len = sizeof(sysctl_physmem);
5415
5416 mib[0] = CTL_HW;
5417 mib[1] = SYSCTL_PHYSMEM;
5418
5419 if(sysctl(mib, 2, &sysctl_physmem, &sysctl_len, NULL, 0) == 0) {
5420 /* some systems use 32-bit values, work with what we're given */
5421 if (sysctl_len == 4)
5422 sysctl_physmem = *(uint32_t*)&sysctl_physmem;
5423 phys_mem = sysctl_physmem >> 20;
5424 } else {
5425 ERROR_START("Failed to get amount of available "
5426 "memory.");
5427 ERROR_EXIT(" Defaulting to least viable amount\n");
5428 phys_mem = SQUASHFS_LOWMEM;
5429 }
5430 #undef SYSCTL_PHYSMEM
5431#else
5432 /* Long longs are used here because with PAE, a 32-bit
5433 machine can have more than 4GB of physical memory */
5434
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005435 long long num_pages = sysconf(_SC_PHYS_PAGES);
5436 long long page_size = sysconf(_SC_PAGESIZE);
Colin Crossbe754902017-12-09 22:27:51 +00005437 phys_mem = num_pages * page_size >> 20;
Phillip Lougher97ad5e82014-06-16 02:21:33 +01005438 if(num_pages == -1 || page_size == -1)
5439 return 0;
5440
Colin Crossbe754902017-12-09 22:27:51 +00005441#endif
5442
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005443 if(phys_mem < SQUASHFS_LOWMEM)
5444 BAD_ERROR("Mksquashfs requires more physical memory than is "
5445 "available!\n");
5446
Phillip Lougher78784bb2014-04-28 04:08:40 +01005447 return phys_mem;
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005448}
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005449
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005450
Phillip Lougher285a2fd2014-06-10 21:51:52 +01005451void check_usable_phys_mem(int total_mem)
5452{
5453 /*
5454 * We want to allow users to use as much of their physical
5455 * memory as they wish. However, for practical reasons there are
5456 * limits which need to be imposed, to protect users from themselves
5457 * and to prevent people from using Mksquashfs as a DOS attack by using
5458 * all physical memory. Mksquashfs uses memory to cache data from disk
5459 * to optimise performance. It is pointless to ask it to use more
5460 * than 75% of physical memory, as this causes thrashing and it is thus
5461 * self-defeating.
5462 */
5463 int mem = get_physical_memory();
5464
5465 mem = (mem >> 1) + (mem >> 2); /* 75% */
5466
Phillip Lougher97ad5e82014-06-16 02:21:33 +01005467 if(total_mem > mem && mem) {
Phillip Lougher285a2fd2014-06-10 21:51:52 +01005468 ERROR("Total memory requested is more than 75%% of physical "
5469 "memory.\n");
5470 ERROR("Mksquashfs uses memory to cache data from disk to "
5471 "optimise performance.\n");
5472 ERROR("It is pointless to ask it to use more than this amount "
5473 "of memory, as this\n");
5474 ERROR("causes thrashing and it is thus self-defeating.\n");
5475 BAD_ERROR("Requested memory size too large\n");
5476 }
5477
5478 if(sizeof(void *) == 4 && total_mem > 2048) {
5479 /*
5480 * If we're running on a kernel with PAE or on a 64-bit kernel,
5481 * then the 75% physical memory limit can still easily exceed
5482 * the addressable memory by this process.
5483 *
5484 * Due to the typical kernel/user-space split (1GB/3GB, or
5485 * 2GB/2GB), we have to conservatively assume the 32-bit
5486 * processes can only address 2-3GB. So refuse if the user
5487 * tries to allocate more than 2GB.
5488 */
5489 ERROR("Total memory requested may exceed maximum "
5490 "addressable memory by this process\n");
5491 BAD_ERROR("Requested memory size too large\n");
5492 }
5493}
5494
5495
5496int get_default_phys_mem()
5497{
Phillip Lougher97ad5e82014-06-16 02:21:33 +01005498 /*
5499 * get_physical_memory() relies on /proc being mounted.
5500 * If it fails, issue a warning, and use
5501 * SQUASHFS_LOWMEM / SQUASHFS_TAKE as default,
5502 * and allow a larger value to be set with -mem.
5503 */
5504 int mem = get_physical_memory();
5505
5506 if(mem == 0) {
5507 mem = SQUASHFS_LOWMEM / SQUASHFS_TAKE;
5508
5509 ERROR("Warning: Cannot get size of physical memory, probably "
5510 "because /proc is missing.\n");
5511 ERROR("Warning: Defaulting to minimal use of %d Mbytes, use "
5512 "-mem to set a better value,\n", mem);
5513 ERROR("Warning: or fix /proc.\n");
5514 } else
5515 mem /= SQUASHFS_TAKE;
Phillip Lougher285a2fd2014-06-10 21:51:52 +01005516
5517 if(sizeof(void *) == 4 && mem > 640) {
5518 /*
5519 * If we're running on a kernel with PAE or on a 64-bit kernel,
5520 * the default memory usage can exceed the addressable
5521 * memory by this process.
5522 * Due to the typical kernel/user-space split (1GB/3GB, or
5523 * 2GB/2GB), we have to conservatively assume the 32-bit
5524 * processes can only address 2-3GB. So limit the default
5525 * usage to 640M, which gives room for other data.
5526 */
5527 mem = 640;
5528 }
5529
5530 return mem;
5531}
5532
5533
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005534void calculate_queue_sizes(int mem, int *readq, int *fragq, int *bwriteq,
5535 int *fwriteq)
5536{
5537 *readq = mem / SQUASHFS_READQ_MEM;
5538 *bwriteq = mem / SQUASHFS_BWRITEQ_MEM;
5539 *fwriteq = mem / SQUASHFS_FWRITEQ_MEM;
5540 *fragq = mem - *readq - *bwriteq - *fwriteq;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005541}
5542
5543
plougher1f413c82005-11-18 00:02:14 +00005544#define VERSION() \
Colin Crossbe754902017-12-09 22:27:51 +00005545 printf("mksquashfs version 4.3-git (2014/09/12)\n");\
5546 printf("copyright (C) 2014 Phillip Lougher "\
Phillip Lougher83d42a32012-10-31 23:42:22 +00005547 "<phillip@squashfs.org.uk>\n\n"); \
plougher16111452010-07-22 05:12:18 +00005548 printf("This program is free software; you can redistribute it and/or"\
5549 "\n");\
5550 printf("modify it under the terms of the GNU General Public License"\
5551 "\n");\
5552 printf("as published by the Free Software Foundation; either version "\
5553 "2,\n");\
plougher1f413c82005-11-18 00:02:14 +00005554 printf("or (at your option) any later version.\n\n");\
plougher16111452010-07-22 05:12:18 +00005555 printf("This program is distributed in the hope that it will be "\
5556 "useful,\n");\
5557 printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\
5558 "of\n");\
5559 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\
5560 "\n");\
plougher1f413c82005-11-18 00:02:14 +00005561 printf("GNU General Public License for more details.\n");
5562int main(int argc, char *argv[])
5563{
plougher324978d2006-02-27 04:53:29 +00005564 struct stat buf, source_buf;
plougher13fdddf2010-11-24 01:23:41 +00005565 int res, i;
plougher1f413c82005-11-18 00:02:14 +00005566 char *b, *root_name = NULL;
Phillip Loughera709bff2013-03-28 17:48:31 +00005567 int keep_as_directory = FALSE;
plougher1f413c82005-11-18 00:02:14 +00005568 squashfs_inode inode;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005569 int readq;
5570 int fragq;
5571 int bwriteq;
5572 int fwriteq;
Phillip Lougher285a2fd2014-06-10 21:51:52 +01005573 int total_mem = get_default_phys_mem();
Phillip Lougher569a9632013-04-22 03:30:30 +01005574 int progress = TRUE;
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005575 int force_progress = FALSE;
Phillip Loughera709bff2013-03-28 17:48:31 +00005576 struct file_buffer **fragment = NULL;
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -07005577/* ANDROID CHANGES START*/
5578#ifdef ANDROID
5579 const char *fs_config_file = NULL;
5580#endif
5581/* ANDROID CHANGES END */
plougher1f413c82005-11-18 00:02:14 +00005582
plougher1f413c82005-11-18 00:02:14 +00005583 if(argc > 1 && strcmp(argv[1], "-version") == 0) {
5584 VERSION();
5585 exit(0);
5586 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005587
5588 block_log = slog(block_size);
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005589 calculate_queue_sizes(total_mem, &readq, &fragq, &bwriteq, &fwriteq);
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005590
plougher1f413c82005-11-18 00:02:14 +00005591 for(i = 1; i < argc && argv[i][0] != '-'; i++);
5592 if(i < 3)
5593 goto printOptions;
5594 source_path = argv + 1;
5595 source = i - 2;
Phillip Lougher1f6c7402014-01-06 04:16:48 +00005596
plougher4da4bd42010-11-21 05:01:54 +00005597 /*
Phillip Lougher1f6c7402014-01-06 04:16:48 +00005598 * Scan the command line for -comp xxx option, this is to ensure
5599 * any -X compressor specific options are passed to the
5600 * correct compressor
plougher4da4bd42010-11-21 05:01:54 +00005601 */
plougher1f413c82005-11-18 00:02:14 +00005602 for(; i < argc; i++) {
Phillip Lougher1f6c7402014-01-06 04:16:48 +00005603 struct compressor *prev_comp = comp;
5604
5605 if(strcmp(argv[i], "-comp") == 0) {
5606 if(++i == argc) {
5607 ERROR("%s: -comp missing compression type\n",
5608 argv[0]);
5609 exit(1);
5610 }
5611 comp = lookup_compressor(argv[i]);
5612 if(!comp->supported) {
5613 ERROR("%s: Compressor \"%s\" is not supported!"
5614 "\n", argv[0], argv[i]);
5615 ERROR("%s: Compressors available:\n", argv[0]);
5616 display_compressors("", COMP_DEFAULT);
5617 exit(1);
5618 }
5619 if(prev_comp != NULL && prev_comp != comp) {
5620 ERROR("%s: -comp multiple conflicting -comp"
5621 " options specified on command line"
5622 ", previously %s, now %s\n", argv[0],
5623 prev_comp->name, comp->name);
5624 exit(1);
5625 }
5626 compressor_opt_parsed = 1;
5627
5628 } else if(strcmp(argv[i], "-e") == 0)
5629 break;
5630 else if(strcmp(argv[i], "-root-becomes") == 0 ||
5631 strcmp(argv[i], "-ef") == 0 ||
5632 strcmp(argv[i], "-pf") == 0 ||
Phillip Lougher089a81a2014-08-24 03:46:46 +01005633 strcmp(argv[i], "-vaf") == 0 ||
Phillip Lougher1f6c7402014-01-06 04:16:48 +00005634 strcmp(argv[i], "-comp") == 0)
5635 i++;
5636 }
5637
5638 /*
5639 * if no -comp option specified lookup default compressor. Note the
5640 * Makefile ensures the default compressor has been built, and so we
5641 * don't need to to check for failure here
5642 */
5643 if(comp == NULL)
5644 comp = lookup_compressor(COMP_DEFAULT);
5645
5646 for(i = source + 2; i < argc; i++) {
Phillip Lougher9523b042012-10-27 01:48:48 +01005647 if(strcmp(argv[i], "-action") == 0 ||
5648 strcmp(argv[i], "-a") ==0) {
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01005649 if(++i == argc) {
Phillip Lougher9523b042012-10-27 01:48:48 +01005650 ERROR("%s: %s missing action\n",
5651 argv[0], argv[i - 1]);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01005652 exit(1);
5653 }
Phillip Loughera2289822014-08-27 05:40:39 +01005654 res = parse_action(argv[i], ACTION_LOG_NONE);
Phillip Lougher06052c62014-08-18 03:03:14 +01005655 if(res == 0)
5656 exit(1);
5657
Phillip Lougherb6704812014-08-27 06:14:38 +01005658 } else if(strcmp(argv[i], "-verbose-action") == 0 ||
Phillip Lougher06052c62014-08-18 03:03:14 +01005659 strcmp(argv[i], "-va") ==0) {
5660 if(++i == argc) {
5661 ERROR("%s: %s missing action\n",
5662 argv[0], argv[i - 1]);
5663 exit(1);
5664 }
Phillip Loughera2289822014-08-27 05:40:39 +01005665 res = parse_action(argv[i], ACTION_LOG_VERBOSE);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01005666 if(res == 0)
5667 exit(1);
5668
Phillip Lougherb6704812014-08-27 06:14:38 +01005669 } else if(strcmp(argv[i], "-true-action") == 0 ||
Phillip Lougher33e5d112014-08-27 05:50:21 +01005670 strcmp(argv[i], "-ta") ==0) {
5671 if(++i == argc) {
5672 ERROR("%s: %s missing action\n",
5673 argv[0], argv[i - 1]);
5674 exit(1);
5675 }
5676 res = parse_action(argv[i], ACTION_LOG_TRUE);
5677 if(res == 0)
5678 exit(1);
5679
Phillip Lougherb6704812014-08-27 06:14:38 +01005680 } else if(strcmp(argv[i], "-false-action") == 0 ||
Phillip Lougherfcfaf0a2014-08-27 05:52:20 +01005681 strcmp(argv[i], "-fa") ==0) {
5682 if(++i == argc) {
5683 ERROR("%s: %s missing action\n",
5684 argv[0], argv[i - 1]);
5685 exit(1);
5686 }
5687 res = parse_action(argv[i], ACTION_LOG_FALSE);
5688 if(res == 0)
5689 exit(1);
5690
Phillip Lougherb6704812014-08-27 06:14:38 +01005691 } else if(strcmp(argv[i], "-action-file") == 0 ||
Phillip Lougherbd432262014-08-27 05:56:13 +01005692 strcmp(argv[i], "-af") ==0) {
Phillip Lougherb73caba2012-12-24 21:58:41 +00005693 if(++i == argc) {
Phillip Lougherbd432262014-08-27 05:56:13 +01005694 ERROR("%s: %s missing filename\n", argv[0],
5695 argv[i - 1]);
Phillip Lougherb73caba2012-12-24 21:58:41 +00005696 exit(1);
5697 }
Phillip Lougherb7c5da72014-08-27 06:01:10 +01005698 if(read_action_file(argv[i], ACTION_LOG_NONE) == FALSE)
Phillip Lougher089a81a2014-08-24 03:46:46 +01005699 exit(1);
5700
Phillip Lougherb6704812014-08-27 06:14:38 +01005701 } else if(strcmp(argv[i], "-verbose-action-file") == 0 ||
Phillip Lougherdfddb9b2014-08-27 05:58:31 +01005702 strcmp(argv[i], "-vaf") ==0) {
Phillip Lougher089a81a2014-08-24 03:46:46 +01005703 if(++i == argc) {
Phillip Lougherdfddb9b2014-08-27 05:58:31 +01005704 ERROR("%s: %s missing filename\n", argv[0],
5705 argv[i - 1]);
Phillip Lougher089a81a2014-08-24 03:46:46 +01005706 exit(1);
5707 }
Phillip Lougherb7c5da72014-08-27 06:01:10 +01005708 if(read_action_file(argv[i], ACTION_LOG_VERBOSE) == FALSE)
Phillip Lougherb73caba2012-12-24 21:58:41 +00005709 exit(1);
5710
Phillip Lougher3ead06b2014-08-27 06:11:41 +01005711 } else if(strcmp(argv[i], "-true-action-file") == 0 ||
Phillip Loughera5541d82014-08-27 06:09:30 +01005712 strcmp(argv[i], "-taf") ==0) {
5713 if(++i == argc) {
5714 ERROR("%s: %s missing filename\n", argv[0],
5715 argv[i - 1]);
5716 exit(1);
5717 }
5718 if(read_action_file(argv[i], ACTION_LOG_TRUE) == FALSE)
5719 exit(1);
5720
Phillip Lougher35a00de2014-08-27 06:32:11 +01005721 } else if(strcmp(argv[i], "-false-action-file") == 0 ||
5722 strcmp(argv[i], "-faf") ==0) {
5723 if(++i == argc) {
5724 ERROR("%s: %s missing filename\n", argv[0],
5725 argv[i - 1]);
5726 exit(1);
5727 }
5728 if(read_action_file(argv[i], ACTION_LOG_FALSE) == FALSE)
5729 exit(1);
5730
Phillip Lougher1f6c7402014-01-06 04:16:48 +00005731 } else if(strcmp(argv[i], "-comp") == 0)
5732 /* parsed previously */
5733 i++;
plougherb5576ea2010-11-22 01:06:53 +00005734
Phillip Lougher1f6c7402014-01-06 04:16:48 +00005735 else if(strncmp(argv[i], "-X", 2) == 0) {
Phillip Lougher774b7b32014-01-07 04:47:13 +00005736 int args;
5737
5738 if(strcmp(argv[i] + 2, "help") == 0)
5739 goto print_compressor_options;
5740
5741 args = compressor_options(comp, argv + i, argc - i);
plougherd8865672010-11-27 05:05:49 +00005742 if(args < 0) {
5743 if(args == -1) {
5744 ERROR("%s: Unrecognised compressor"
5745 " option %s\n", argv[0],
5746 argv[i]);
Phillip Lougher1f6c7402014-01-06 04:16:48 +00005747 if(!compressor_opt_parsed)
5748 ERROR("%s: Did you forget to"
5749 " specify -comp?\n",
5750 argv[0]);
Phillip Lougher774b7b32014-01-07 04:47:13 +00005751print_compressor_options:
Phillip Lougherb1c3b6a2014-01-06 05:30:00 +00005752 ERROR("%s: selected compressor \"%s\""
5753 ". Options supported: %s\n",
5754 argv[0], comp->name,
5755 comp->usage ? "" : "none");
5756 if(comp->usage)
5757 comp->usage();
Phillip Lougher1f6c7402014-01-06 04:16:48 +00005758 }
plougherb5576ea2010-11-22 01:06:53 +00005759 exit(1);
5760 }
5761 i += args;
5762
plougherae9dcd82009-08-01 02:59:38 +00005763 } else if(strcmp(argv[i], "-pf") == 0) {
plougher43244f22009-04-05 02:04:51 +00005764 if(++i == argc) {
5765 ERROR("%s: -pf missing filename\n", argv[0]);
5766 exit(1);
5767 }
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00005768 if(read_pseudo_file(argv[i]) == FALSE)
plougher43244f22009-04-05 02:04:51 +00005769 exit(1);
plougher43244f22009-04-05 02:04:51 +00005770 } else if(strcmp(argv[i], "-p") == 0) {
5771 if(++i == argc) {
5772 ERROR("%s: -p missing pseudo file definition\n",
5773 argv[0]);
5774 exit(1);
5775 }
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00005776 if(read_pseudo_def(argv[i]) == FALSE)
plougher43244f22009-04-05 02:04:51 +00005777 exit(1);
plougher43244f22009-04-05 02:04:51 +00005778 } else if(strcmp(argv[i], "-recover") == 0) {
plougher99ac0cc2007-10-29 03:17:10 +00005779 if(++i == argc) {
plougherb3604122009-03-30 02:07:20 +00005780 ERROR("%s: -recover missing recovery file\n",
5781 argv[0]);
plougher99ac0cc2007-10-29 03:17:10 +00005782 exit(1);
5783 }
5784 read_recovery_data(argv[i], argv[source + 1]);
5785 } else if(strcmp(argv[i], "-no-recovery") == 0)
5786 recover = FALSE;
5787 else if(strcmp(argv[i], "-wildcards") == 0) {
plougher934a9ed2007-10-19 00:21:10 +00005788 old_exclude = FALSE;
5789 use_regex = FALSE;
5790 } else if(strcmp(argv[i], "-regex") == 0) {
5791 old_exclude = FALSE;
5792 use_regex = TRUE;
5793 } else if(strcmp(argv[i], "-no-sparse") == 0)
plougher1f54edc2007-08-12 23:13:36 +00005794 sparse_files = FALSE;
5795 else if(strcmp(argv[i], "-no-progress") == 0)
plougher02bc3bc2007-02-25 12:12:01 +00005796 progress = FALSE;
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005797 else if(strcmp(argv[i], "-progress") == 0)
5798 force_progress = TRUE;
plougher02bc3bc2007-02-25 12:12:01 +00005799 else if(strcmp(argv[i], "-no-exports") == 0)
5800 exportable = FALSE;
plougher0e453652006-11-06 01:49:35 +00005801 else if(strcmp(argv[i], "-processors") == 0) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00005802 if((++i == argc) || !parse_num(argv[i], &processors)) {
plougher360514a2009-03-30 03:01:38 +00005803 ERROR("%s: -processors missing or invalid "
5804 "processor number\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005805 exit(1);
5806 }
5807 if(processors < 1) {
plougher360514a2009-03-30 03:01:38 +00005808 ERROR("%s: -processors should be 1 or larger\n",
5809 argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005810 exit(1);
5811 }
plougher0e453652006-11-06 01:49:35 +00005812 } else if(strcmp(argv[i], "-read-queue") == 0) {
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005813 if((++i == argc) || !parse_num(argv[i], &readq)) {
plougher50b31762009-03-31 04:14:46 +00005814 ERROR("%s: -read-queue missing or invalid "
5815 "queue size\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005816 exit(1);
5817 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005818 if(readq < 1) {
plougher360514a2009-03-30 03:01:38 +00005819 ERROR("%s: -read-queue should be 1 megabyte or "
5820 "larger\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005821 exit(1);
5822 }
plougher0e453652006-11-06 01:49:35 +00005823 } else if(strcmp(argv[i], "-write-queue") == 0) {
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005824 if((++i == argc) || !parse_num(argv[i], &bwriteq)) {
plougher50b31762009-03-31 04:14:46 +00005825 ERROR("%s: -write-queue missing or invalid "
5826 "queue size\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005827 exit(1);
5828 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005829 if(bwriteq < 2) {
5830 ERROR("%s: -write-queue should be 2 megabytes "
plougher50b31762009-03-31 04:14:46 +00005831 "or larger\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005832 exit(1);
5833 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005834 fwriteq = bwriteq >> 1;
5835 bwriteq -= fwriteq;
plougher217bad82008-04-05 11:36:41 +00005836 } else if(strcmp(argv[i], "-fragment-queue") == 0) {
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005837 if((++i == argc) || !parse_num(argv[i], &fragq)) {
plougher360514a2009-03-30 03:01:38 +00005838 ERROR("%s: -fragment-queue missing or invalid "
5839 "queue size\n", argv[0]);
plougher217bad82008-04-05 11:36:41 +00005840 exit(1);
5841 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005842 if(fragq < 1) {
plougher50b31762009-03-31 04:14:46 +00005843 ERROR("%s: -fragment-queue should be 1 "
5844 "megabyte or larger\n", argv[0]);
plougher217bad82008-04-05 11:36:41 +00005845 exit(1);
5846 }
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005847 } else if(strcmp(argv[i], "-mem") == 0) {
Phillip Lougher57e2f692014-05-05 23:50:24 +01005848 long long number;
5849
5850 if((++i == argc) ||
5851 !parse_numberll(argv[i], &number, 1)) {
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005852 ERROR("%s: -mem missing or invalid mem size\n",
5853 argv[0]);
5854 exit(1);
5855 }
Phillip Lougher368becc2014-06-11 04:51:37 +01005856
5857 /*
5858 * convert from bytes to Mbytes, ensuring the value
5859 * does not overflow a signed int
5860 */
5861 if(number >= (1LL << 51)) {
5862 ERROR("%s: -mem invalid mem size\n", argv[0]);
5863 exit(1);
5864 }
5865
Phillip Lougher57e2f692014-05-05 23:50:24 +01005866 total_mem = number / 1048576;
Phillip Lougher07def8b2014-05-11 19:56:11 +01005867 if(total_mem < (SQUASHFS_LOWMEM / SQUASHFS_TAKE)) {
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005868 ERROR("%s: -mem should be %d Mbytes or "
Phillip Lougher07def8b2014-05-11 19:56:11 +01005869 "larger\n", argv[0],
5870 SQUASHFS_LOWMEM / SQUASHFS_TAKE);
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005871 exit(1);
5872 }
5873 calculate_queue_sizes(total_mem, &readq, &fragq,
5874 &bwriteq, &fwriteq);
plougher5507dd92006-11-06 00:43:10 +00005875 } else if(strcmp(argv[i], "-b") == 0) {
plougher4c99cb72007-06-14 21:46:31 +00005876 if(++i == argc) {
5877 ERROR("%s: -b missing block size\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005878 exit(1);
5879 }
Phillip Lougher94c1fe02012-11-28 03:32:36 +00005880 if(!parse_number(argv[i], &block_size, 1)) {
plougher4c99cb72007-06-14 21:46:31 +00005881 ERROR("%s: -b invalid block size\n", argv[0]);
5882 exit(1);
5883 }
plougher1f413c82005-11-18 00:02:14 +00005884 if((block_log = slog(block_size)) == 0) {
plougher50b31762009-03-31 04:14:46 +00005885 ERROR("%s: -b block size not power of two or "
5886 "not between 4096 and 1Mbyte\n",
5887 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005888 exit(1);
5889 }
5890 } else if(strcmp(argv[i], "-ef") == 0) {
5891 if(++i == argc) {
5892 ERROR("%s: -ef missing filename\n", argv[0]);
5893 exit(1);
5894 }
plougher9b5bf8c2006-03-20 18:43:33 +00005895 } else if(strcmp(argv[i], "-no-duplicates") == 0)
plougher1f413c82005-11-18 00:02:14 +00005896 duplicate_checking = FALSE;
5897
5898 else if(strcmp(argv[i], "-no-fragments") == 0)
5899 no_fragments = TRUE;
5900
5901 else if(strcmp(argv[i], "-always-use-fragments") == 0)
5902 always_use_fragments = TRUE;
5903
5904 else if(strcmp(argv[i], "-sort") == 0) {
5905 if(++i == argc) {
5906 ERROR("%s: -sort missing filename\n", argv[0]);
5907 exit(1);
5908 }
5909 } else if(strcmp(argv[i], "-all-root") == 0 ||
5910 strcmp(argv[i], "-root-owned") == 0)
5911 global_uid = global_gid = 0;
5912
5913 else if(strcmp(argv[i], "-force-uid") == 0) {
5914 if(++i == argc) {
plougher50b31762009-03-31 04:14:46 +00005915 ERROR("%s: -force-uid missing uid or user\n",
5916 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005917 exit(1);
5918 }
5919 if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') {
plougher360514a2009-03-30 03:01:38 +00005920 if(global_uid < 0 || global_uid >
5921 (((long long) 1 << 32) - 1)) {
plougher50b31762009-03-31 04:14:46 +00005922 ERROR("%s: -force-uid uid out of range"
5923 "\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005924 exit(1);
5925 }
5926 } else {
5927 struct passwd *uid = getpwnam(argv[i]);
5928 if(uid)
5929 global_uid = uid->pw_uid;
5930 else {
plougher360514a2009-03-30 03:01:38 +00005931 ERROR("%s: -force-uid invalid uid or "
5932 "unknown user\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005933 exit(1);
5934 }
5935 }
5936 } else if(strcmp(argv[i], "-force-gid") == 0) {
5937 if(++i == argc) {
plougher360514a2009-03-30 03:01:38 +00005938 ERROR("%s: -force-gid missing gid or group\n",
5939 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005940 exit(1);
5941 }
5942 if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') {
plougher360514a2009-03-30 03:01:38 +00005943 if(global_gid < 0 || global_gid >
5944 (((long long) 1 << 32) - 1)) {
plougher50b31762009-03-31 04:14:46 +00005945 ERROR("%s: -force-gid gid out of range"
5946 "\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005947 exit(1);
5948 }
5949 } else {
5950 struct group *gid = getgrnam(argv[i]);
5951 if(gid)
5952 global_gid = gid->gr_gid;
5953 else {
plougher360514a2009-03-30 03:01:38 +00005954 ERROR("%s: -force-gid invalid gid or "
5955 "unknown group\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005956 exit(1);
5957 }
5958 }
5959 } else if(strcmp(argv[i], "-noI") == 0 ||
5960 strcmp(argv[i], "-noInodeCompression") == 0)
5961 noI = TRUE;
5962
5963 else if(strcmp(argv[i], "-noD") == 0 ||
5964 strcmp(argv[i], "-noDataCompression") == 0)
5965 noD = TRUE;
5966
5967 else if(strcmp(argv[i], "-noF") == 0 ||
5968 strcmp(argv[i], "-noFragmentCompression") == 0)
5969 noF = TRUE;
5970
plougherb99d7832010-05-19 01:57:34 +00005971 else if(strcmp(argv[i], "-noX") == 0 ||
5972 strcmp(argv[i], "-noXattrCompression") == 0)
5973 noX = TRUE;
5974
plougherce564c62010-05-19 03:01:49 +00005975 else if(strcmp(argv[i], "-no-xattrs") == 0)
5976 no_xattrs = TRUE;
plougher6d89ac22010-05-19 02:59:23 +00005977
plougher30281c82010-08-25 05:06:11 +00005978 else if(strcmp(argv[i], "-xattrs") == 0)
5979 no_xattrs = FALSE;
5980
Mohamad Ayyashe0316292015-02-24 19:21:29 -08005981/* ANDROID CHANGES START*/
5982#ifdef ANDROID
5983 else if(strcmp(argv[i], "-context-file") == 0) {
5984 if(++i == argc) {
5985 ERROR("%s: -context-file: missing file name\n",
5986 argv[0]);
5987 exit(1);
5988 }
5989 context_file = argv[i];
5990 }
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -07005991 else if(strcmp(argv[i], "-fs-config-file") == 0) {
5992 if(++i == argc) {
5993 ERROR("%s: -fs-config-file: missing file name\n",
5994 argv[0]);
5995 exit(1);
5996 }
5997 fs_config_file = argv[i];
Mohan Srinivasan58e18d72016-07-26 15:06:29 -07005998 } else if(strcmp(argv[i], "-whitelist") == 0) {
5999 if(++i == argc) {
6000 ERROR("%s: -whitelist missing filename\n", argv[0]);
6001 exit(1);
6002 }
6003 whitelist_filename = argv[i];
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -07006004 }
Mohamad Ayyash4dfe4eb2016-06-03 18:49:44 -07006005 else if(strcmp(argv[i], "-t") == 0) {
6006 if(++i == argc) {
6007 ERROR("%s: -t missing compression threshold percentage\n", argv[0]);
6008 exit(1);
6009 }
6010 if(!parse_number(argv[i], &compress_thresh_per, 1)) {
6011 ERROR("%s: -t invalid compression threshold percentage\n", argv[0]);
6012 exit(1);
6013 }
6014 if(compress_thresh_per > 100 || compress_thresh_per < 0) {
6015 ERROR("%s: -t compression threshold percentage not between 0 and 100\n",
6016 argv[0]);
6017 exit(1);
6018 }
6019 }
Mohamad Ayyashe0316292015-02-24 19:21:29 -08006020#endif
6021/* ANDROID CHANGES END */
plougher1f413c82005-11-18 00:02:14 +00006022 else if(strcmp(argv[i], "-nopad") == 0)
6023 nopad = TRUE;
6024
Phillip Lougherc6c73b22012-10-19 03:12:08 +01006025 else if(strcmp(argv[i], "-info") == 0)
plougher91fbb302008-05-06 02:29:36 +00006026 silent = FALSE;
plougher1f413c82005-11-18 00:02:14 +00006027
6028 else if(strcmp(argv[i], "-e") == 0)
6029 break;
6030
6031 else if(strcmp(argv[i], "-noappend") == 0)
6032 delete = TRUE;
6033
6034 else if(strcmp(argv[i], "-keep-as-directory") == 0)
6035 keep_as_directory = TRUE;
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -08006036/* ANDROID CHANGES START*/
6037#ifdef ANDROID
6038 else if(strcmp(argv[i], "-android-fs-config") == 0)
6039 android_config = TRUE;
Mohamad Ayyash18fe5f62015-03-02 16:00:56 -08006040 else if(strcmp(argv[i], "-mount-point") == 0) {
6041 if(++i == argc) {
6042 ERROR("%s: -mount-point: missing mount point name\n",
6043 argv[0]);
6044 exit(1);
6045 }
6046 mount_point = argv[i];
6047 }
Thierry Strudel1a710ff2015-07-09 16:34:38 -07006048 else if(strcmp(argv[i], "-product-out") == 0) {
6049 if(++i == argc) {
6050 ERROR("%s: -product-out: missing path name\n",
6051 argv[0]);
6052 exit(1);
6053 }
6054 target_out_path = argv[i];
6055 }
Mohamad Ayyash305fca62016-06-13 15:44:16 -07006056 else if(strcmp(argv[i], "-disable-4k-align") == 0)
6057 align_4k_blocks = FALSE;
6058 else if(strcmp(argv[i], "-block-map") == 0) {
6059 if(++i == argc) {
6060 ERROR("%s: -block-map: missing path name\n",
6061 argv[0]);
6062 exit(1);
6063 }
6064 block_map_file = fopen(argv[i], "w");
6065 if (block_map_file == NULL) {
6066 ERROR("%s: -block-map: failed to open %s\n",
6067 argv[0], argv[i]);
6068 exit(1);
6069 }
6070 if (!align_4k_blocks) {
6071 ERROR("WARNING: Using block maps with unaligned 4k blocks "
6072 "is not ideal as block map offsets are multiples of 4k, "
6073 "consider not passing -disable-4k-align\n");
6074 }
6075 }
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -08006076#endif
6077/* ANDROID CHANGES END */
Colin Crossbe754902017-12-09 22:27:51 +00006078
Phillip Lougher85776df2014-01-31 03:10:46 +00006079 else if(strcmp(argv[i], "-exit-on-error") == 0)
6080 exit_on_error = TRUE;
6081
plougher1f413c82005-11-18 00:02:14 +00006082 else if(strcmp(argv[i], "-root-becomes") == 0) {
6083 if(++i == argc) {
plougher50b31762009-03-31 04:14:46 +00006084 ERROR("%s: -root-becomes: missing name\n",
6085 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00006086 exit(1);
6087 }
6088 root_name = argv[i];
Hidehiko Abef240b142018-05-08 22:45:47 +09006089 } else if (strcmp(argv[i], "-uid-map") == 0) {
6090 if (++i == argc) {
6091 ERROR("%s: -uid-map: missing mapping\n",
6092 argv[0]);
6093 exit(1);
6094 }
6095 if (parse_ugid_map(argv[i], uid_mapping,
6096 &uid_map_count) != 0) {
6097 ERROR("%s: -uid-map: invalid mapping\n",
6098 argv[0]);
6099 exit(1);
6100 }
6101 } else if (strcmp(argv[i], "-gid-map") == 0) {
6102 if (++i == argc) {
6103 ERROR("%s: -gid-map: missing mapping\n",
6104 argv[0]);
6105 exit(1);
6106 }
6107 if (parse_ugid_map(argv[i], gid_mapping,
6108 &gid_map_count) != 0) {
6109 ERROR("%s: -gid-map: invalid mapping\n",
6110 argv[0]);
6111 exit(1);
6112 }
plougher1f413c82005-11-18 00:02:14 +00006113 } else if(strcmp(argv[i], "-version") == 0) {
6114 VERSION();
6115 } else {
6116 ERROR("%s: invalid option\n\n", argv[0]);
6117printOptions:
plougher360514a2009-03-30 03:01:38 +00006118 ERROR("SYNTAX:%s source1 source2 ... dest [options] "
6119 "[-e list of exclude\ndirs/files]\n", argv[0]);
plougher37632562009-08-07 19:09:01 +00006120 ERROR("\nFilesystem build options:\n");
plougherff5ea8b2009-08-07 19:33:10 +00006121 ERROR("-comp <comp>\t\tselect <comp> compression\n");
plougher13df1782009-08-29 01:05:34 +00006122 ERROR("\t\t\tCompressors available:\n");
plougher764dab52009-08-24 18:28:04 +00006123 display_compressors("\t\t\t", COMP_DEFAULT);
plougher50b31762009-03-31 04:14:46 +00006124 ERROR("-b <block_size>\t\tset data block to "
Phillip Lougherb74a59c2014-04-30 03:10:44 +01006125 "<block_size>. Default 128 Kbytes\n");
6126 ERROR("\t\t\tOptionally a suffix of K or M can be"
6127 " given to specify\n\t\t\tKbytes or Mbytes"
6128 " respectively\n");
plougher37632562009-08-07 19:09:01 +00006129 ERROR("-no-exports\t\tdon't make the filesystem "
6130 "exportable via NFS\n");
6131 ERROR("-no-sparse\t\tdon't detect sparse files\n");
plougher07d25c22010-08-25 17:41:57 +00006132 ERROR("-no-xattrs\t\tdon't store extended attributes"
plougher30281c82010-08-25 05:06:11 +00006133 NOXOPT_STR "\n");
plougher07d25c22010-08-25 17:41:57 +00006134 ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR
plougher30281c82010-08-25 05:06:11 +00006135 "\n");
Mohamad Ayyashe0316292015-02-24 19:21:29 -08006136/* ANDROID CHANGES START*/
6137#ifdef ANDROID
6138 ERROR("-context-file <file>\tApply selinux security "
6139 "xattrs from context-file instead\n\t\t\t"
6140 "of reading xattrs from file system\n");
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -07006141 ERROR("-fs-config-file <file>\tAndroid specific "
6142 "filesystem config file\n");
Mohamad Ayyash4dfe4eb2016-06-03 18:49:44 -07006143 ERROR("-t <compress_thresh>\tset minimum "
6144 "acceptable compression ratio of a block to\n\t\t\t"
6145 "<compress_thresh_per> otherwise don't compress. "
6146 "Default 0%\n");
Mohan Srinivasan58e18d72016-07-26 15:06:29 -07006147 ERROR("-whitelist <file>\tAndroid specific whitelist "
6148 "one entry per line (no wildcards)\n");
Mohamad Ayyashe0316292015-02-24 19:21:29 -08006149#endif
6150/* ANDROID CHANGES END */
plougher1f413c82005-11-18 00:02:14 +00006151 ERROR("-noI\t\t\tdo not compress inode table\n");
6152 ERROR("-noD\t\t\tdo not compress data blocks\n");
6153 ERROR("-noF\t\t\tdo not compress fragment blocks\n");
plougher16111452010-07-22 05:12:18 +00006154 ERROR("-noX\t\t\tdo not compress extended "
6155 "attributes\n");
plougher1f413c82005-11-18 00:02:14 +00006156 ERROR("-no-fragments\t\tdo not use fragments\n");
plougher360514a2009-03-30 03:01:38 +00006157 ERROR("-always-use-fragments\tuse fragment blocks for "
6158 "files larger than block size\n");
6159 ERROR("-no-duplicates\t\tdo not perform duplicate "
6160 "checking\n");
plougher1f413c82005-11-18 00:02:14 +00006161 ERROR("-all-root\t\tmake all files owned by root\n");
6162 ERROR("-force-uid uid\t\tset all file uids to uid\n");
6163 ERROR("-force-gid gid\t\tset all file gids to gid\n");
plougher50b31762009-03-31 04:14:46 +00006164 ERROR("-nopad\t\t\tdo not pad filesystem to a multiple "
6165 "of 4K\n");
plougher37632562009-08-07 19:09:01 +00006166 ERROR("-keep-as-directory\tif one source directory is "
6167 "specified, create a root\n");
6168 ERROR("\t\t\tdirectory containing that directory, "
6169 "rather than the\n");
6170 ERROR("\t\t\tcontents of the directory\n");
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -08006171/* ANDROID CHANGES START*/
6172#ifdef ANDROID
6173 ERROR("-android-fs-config\tuse android fs config "
6174 "for mode, uid, and gids of inodes\n");
Mohamad Ayyash18fe5f62015-03-02 16:00:56 -08006175 ERROR("-mount-point <name>\tNeed to be provided when "
6176 "android-fs-config or context-file\n\t\t\tare "
6177 "enabled and source directory is not mount point\n");
Thierry Strudel1a710ff2015-07-09 16:34:38 -07006178 ERROR("-product-out <path>\tPRODUCT_OUT directory to "
Mohamad Ayyash4dfe4eb2016-06-03 18:49:44 -07006179 "read device specific FS rules files from\n");
Mohamad Ayyash305fca62016-06-13 15:44:16 -07006180 ERROR("-disable-4k-align \tDon't 4k align data blocks. Default is false\n");
6181 ERROR("-block-map <path>\tGenerate a block map for non-fragment files\n");
Mohamad Ayyash829ffbe2015-02-24 13:04:49 -08006182#endif
6183/* ANDROID CHANGES END */
plougher37632562009-08-07 19:09:01 +00006184 ERROR("\nFilesystem filter options:\n");
plougher16111452010-07-22 05:12:18 +00006185 ERROR("-p <pseudo-definition>\tAdd pseudo file "
6186 "definition\n");
6187 ERROR("-pf <pseudo-file>\tAdd list of pseudo file "
6188 "definitions\n");
plougher360514a2009-03-30 03:01:38 +00006189 ERROR("-sort <sort_file>\tsort files according to "
6190 "priorities in <sort_file>. One\n");
6191 ERROR("\t\t\tfile or dir with priority per line. "
6192 "Priority -32768 to\n");
plougher1f413c82005-11-18 00:02:14 +00006193 ERROR("\t\t\t32767, default priority 0\n");
plougher50b31762009-03-31 04:14:46 +00006194 ERROR("-ef <exclude_file>\tlist of exclude dirs/files."
6195 " One per line\n");
plougher360514a2009-03-30 03:01:38 +00006196 ERROR("-wildcards\t\tAllow extended shell wildcards "
6197 "(globbing) to be used in\n\t\t\texclude "
6198 "dirs/files\n");
plougher50b31762009-03-31 04:14:46 +00006199 ERROR("-regex\t\t\tAllow POSIX regular expressions to "
6200 "be used in exclude\n\t\t\tdirs/files\n");
Hidehiko Abef240b142018-05-08 22:45:47 +09006201 ERROR("-uid-map <mapping>\tUser ID mapping.\n");
6202 ERROR("\t\t\tFollows the format described in "
6203 "user_namespaces(7).\n");
6204 ERROR("-gid-map <mapping>\tGroup ID mapping.\n");
6205 ERROR("\t\t\tFollows the format described in "
6206 "user_namespaces(7).\n");
plougher37632562009-08-07 19:09:01 +00006207 ERROR("\nFilesystem append options:\n");
6208 ERROR("-noappend\t\tdo not append to existing "
6209 "filesystem\n");
6210 ERROR("-root-becomes <name>\twhen appending source "
6211 "files/directories, make the\n");
6212 ERROR("\t\t\toriginal root become a subdirectory in "
6213 "the new root\n");
6214 ERROR("\t\t\tcalled <name>, rather than adding the new "
6215 "source items\n");
6216 ERROR("\t\t\tto the original root\n");
6217 ERROR("\nMksquashfs runtime options:\n");
6218 ERROR("-version\t\tprint version, licence and "
6219 "copyright message\n");
Phillip Lougher85776df2014-01-31 03:10:46 +00006220 ERROR("-exit-on-error\t\ttreat normally ignored errors "
6221 "as fatal\n");
plougher37632562009-08-07 19:09:01 +00006222 ERROR("-recover <name>\t\trecover filesystem data "
6223 "using recovery file <name>\n");
6224 ERROR("-no-recovery\t\tdon't generate a recovery "
6225 "file\n");
6226 ERROR("-info\t\t\tprint files written to filesystem\n");
6227 ERROR("-no-progress\t\tdon't display the progress "
6228 "bar\n");
Phillip Lougherc6c73b22012-10-19 03:12:08 +01006229 ERROR("-progress\t\tdisplay progress bar when using "
6230 "the -info option\n");
plougher37632562009-08-07 19:09:01 +00006231 ERROR("-processors <number>\tUse <number> processors."
6232 " By default will use number of\n");
6233 ERROR("\t\t\tprocessors available\n");
Phillip Lougher57e2f692014-05-05 23:50:24 +01006234 ERROR("-mem <size>\t\tUse <size> physical memory. "
6235 "Currently set to %dM\n", total_mem);
6236 ERROR("\t\t\tOptionally a suffix of K, M or G can be"
6237 " given to specify\n\t\t\tKbytes, Mbytes or"
6238 " Gbytes respectively\n");
plougher37632562009-08-07 19:09:01 +00006239 ERROR("\nMiscellaneous options:\n");
6240 ERROR("-root-owned\t\talternative name for -all-root"
6241 "\n");
6242 ERROR("-noInodeCompression\talternative name for -noI"
6243 "\n");
6244 ERROR("-noDataCompression\talternative name for -noD"
6245 "\n");
6246 ERROR("-noFragmentCompression\talternative name for "
6247 "-noF\n");
plougherb99d7832010-05-19 01:57:34 +00006248 ERROR("-noXattrCompression\talternative name for "
6249 "-noX\n");
Phillip Lougher774b7b32014-01-07 04:47:13 +00006250 ERROR("\n-Xhelp\t\t\tprint compressor options for"
6251 " selected compressor\n");
plougher4fb66822010-12-08 02:49:28 +00006252 ERROR("\nCompressors available and compressor specific "
6253 "options:\n");
6254 display_compressor_usage(COMP_DEFAULT);
plougher1f413c82005-11-18 00:02:14 +00006255 exit(1);
6256 }
6257 }
6258
Hidehiko Abef240b142018-05-08 22:45:47 +09006259 if (!uid_map_count) {
6260 uid_mapping[0].child_id = 0;
6261 uid_mapping[0].parent_id = 0;
6262 uid_mapping[0].length = 4294967295u;
6263 uid_map_count = 1;
6264 }
6265 if (!gid_map_count) {
6266 gid_mapping[0].child_id = 0;
6267 gid_mapping[0].parent_id = 0;
6268 gid_mapping[0].length = 4294967295u;
6269 gid_map_count = 1;
6270 }
6271
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -07006272/* ANDROID CHANGES START*/
6273#ifdef ANDROID
6274 if (fs_config_file) {
6275 if (load_canned_fs_config(fs_config_file) < 0) {
6276 fprintf(stderr, "failed to load %s\n", fs_config_file);
6277 exit(1);
6278 }
6279 fs_config_func = canned_fs_config;
6280 } else if (mount_point) {
6281 fs_config_func = fs_config;
6282 }
Mohan Srinivasan58e18d72016-07-26 15:06:29 -07006283 if (whitelist_filename)
6284 process_whitelist_file(whitelist_filename);
Mohamad Ayyashec7a8b42016-04-07 22:12:10 -07006285#endif
6286/* ANDROID CHANGES END */
6287
plougherb747f4f2010-12-25 04:05:47 +00006288 /*
6289 * Some compressors may need the options to be checked for validity
6290 * once all the options have been processed
6291 */
6292 res = compressor_options_post(comp, block_size);
6293 if(res)
6294 EXIT_MKSQUASHFS();
6295
Phillip Lougherc6c73b22012-10-19 03:12:08 +01006296 /*
6297 * If the -info option has been selected then disable the
6298 * progress bar unless it has been explicitly enabled with
6299 * the -progress option
6300 */
6301 if(!silent)
6302 progress = force_progress;
6303
6304#ifdef SQUASHFS_TRACE
Phillip Lougher989f5fe2013-04-22 03:16:29 +01006305 /*
6306 * Disable progress bar if full debug tracing is enabled.
6307 * The progress bar in this case just gets in the way of the
6308 * debug trace output
6309 */
Phillip Lougherc6c73b22012-10-19 03:12:08 +01006310 progress = FALSE;
6311#endif
6312
plougher91fbb302008-05-06 02:29:36 +00006313 for(i = 0; i < source; i++)
6314 if(lstat(source_path[i], &source_buf) == -1) {
plougher360514a2009-03-30 03:01:38 +00006315 fprintf(stderr, "Cannot stat source directory \"%s\" "
plougher50b31762009-03-31 04:14:46 +00006316 "because %s\n", source_path[i],
6317 strerror(errno));
plougher91fbb302008-05-06 02:29:36 +00006318 EXIT_MKSQUASHFS();
6319 }
plougher324978d2006-02-27 04:53:29 +00006320
6321 destination_file = argv[source + 1];
plougher1f413c82005-11-18 00:02:14 +00006322 if(stat(argv[source + 1], &buf) == -1) {
6323 if(errno == ENOENT) { /* Does not exist */
plougher360514a2009-03-30 03:01:38 +00006324 fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR,
plougher59dce672010-05-19 03:56:59 +00006325 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
plougher360514a2009-03-30 03:01:38 +00006326 if(fd == -1) {
plougher1f413c82005-11-18 00:02:14 +00006327 perror("Could not create destination file");
6328 exit(1);
6329 }
6330 delete = TRUE;
6331 } else {
6332 perror("Could not stat destination file");
6333 exit(1);
6334 }
6335
6336 } else {
6337 if(S_ISBLK(buf.st_mode)) {
6338 if((fd = open(argv[source + 1], O_RDWR)) == -1) {
plougher50b31762009-03-31 04:14:46 +00006339 perror("Could not open block device as "
6340 "destination");
plougher1f413c82005-11-18 00:02:14 +00006341 exit(1);
6342 }
6343 block_device = 1;
6344
6345 } else if(S_ISREG(buf.st_mode)) {
plougher360514a2009-03-30 03:01:38 +00006346 fd = open(argv[source + 1], (delete ? O_TRUNC : 0) |
6347 O_RDWR);
6348 if(fd == -1) {
plougher50b31762009-03-31 04:14:46 +00006349 perror("Could not open regular file for "
6350 "writing as destination");
plougher1f413c82005-11-18 00:02:14 +00006351 exit(1);
6352 }
plougher44d54ef2010-02-08 22:13:49 +00006353 }
plougher1f413c82005-11-18 00:02:14 +00006354 else {
6355 ERROR("Destination not block device or regular file\n");
6356 exit(1);
6357 }
6358
plougher324978d2006-02-27 04:53:29 +00006359 }
plougher1f413c82005-11-18 00:02:14 +00006360
plougher16111452010-07-22 05:12:18 +00006361 /*
6362 * process the exclude files - must be done afer destination file has
6363 * been possibly created
6364 */
plougher1f413c82005-11-18 00:02:14 +00006365 for(i = source + 2; i < argc; i++)
Phillip Lougher386128f2012-12-16 05:23:45 +00006366 if(strcmp(argv[i], "-ef") == 0)
6367 /*
6368 * Note presence of filename arg has already
6369 * been checked
6370 */
6371 process_exclude_file(argv[++i]);
6372 else if(strcmp(argv[i], "-e") == 0)
plougher1f413c82005-11-18 00:02:14 +00006373 break;
plougher8b9a7f62009-08-01 22:52:45 +00006374 else if(strcmp(argv[i], "-root-becomes") == 0 ||
plougher43244f22009-04-05 02:04:51 +00006375 strcmp(argv[i], "-sort") == 0 ||
6376 strcmp(argv[i], "-pf") == 0 ||
Phillip Lougherb73caba2012-12-24 21:58:41 +00006377 strcmp(argv[i], "-af") == 0 ||
Phillip Lougher089a81a2014-08-24 03:46:46 +01006378 strcmp(argv[i], "-vaf") == 0 ||
plougher8b9a7f62009-08-01 22:52:45 +00006379 strcmp(argv[i], "-comp") == 0)
plougher1f413c82005-11-18 00:02:14 +00006380 i++;
6381
6382 if(i != argc) {
6383 if(++i == argc) {
6384 ERROR("%s: -e missing arguments\n", argv[0]);
plougher324978d2006-02-27 04:53:29 +00006385 EXIT_MKSQUASHFS();
plougher1f413c82005-11-18 00:02:14 +00006386 }
plougher8f8e1a12007-10-18 02:50:21 +00006387 while(i < argc)
6388 if(old_exclude)
6389 old_add_exclude(argv[i++]);
6390 else
plougher05e50ef2007-10-23 12:34:20 +00006391 add_exclude(argv[i++]);
plougher1f413c82005-11-18 00:02:14 +00006392 }
6393
6394 /* process the sort files - must be done afer the exclude files */
6395 for(i = source + 2; i < argc; i++)
6396 if(strcmp(argv[i], "-sort") == 0) {
ploughere1c9a742010-07-21 16:59:05 +00006397 int res = read_sort_file(argv[++i], source,
6398 source_path);
6399 if(res == FALSE)
6400 BAD_ERROR("Failed to read sort file\n");
plougher1f413c82005-11-18 00:02:14 +00006401 sorted ++;
6402 } else if(strcmp(argv[i], "-e") == 0)
6403 break;
plougher8b9a7f62009-08-01 22:52:45 +00006404 else if(strcmp(argv[i], "-root-becomes") == 0 ||
plougher43244f22009-04-05 02:04:51 +00006405 strcmp(argv[i], "-ef") == 0 ||
6406 strcmp(argv[i], "-pf") == 0 ||
Phillip Lougherb73caba2012-12-24 21:58:41 +00006407 strcmp(argv[i], "-af") == 0 ||
Phillip Lougher089a81a2014-08-24 03:46:46 +01006408 strcmp(argv[i], "-vaf") == 0 ||
plougher8b9a7f62009-08-01 22:52:45 +00006409 strcmp(argv[i], "-comp") == 0)
plougher1f413c82005-11-18 00:02:14 +00006410 i++;
6411
plougher4c99cb72007-06-14 21:46:31 +00006412 if(!delete) {
ploughera175ce22009-07-30 04:43:27 +00006413 comp = read_super(fd, &sBlk, argv[source + 1]);
6414 if(comp == NULL) {
plougher360514a2009-03-30 03:01:38 +00006415 ERROR("Failed to read existing filesystem - will not "
6416 "overwrite - ABORTING!\n");
plougher50b31762009-03-31 04:14:46 +00006417 ERROR("To force Mksquashfs to write to this block "
6418 "device or file use -noappend\n");
plougher4c99cb72007-06-14 21:46:31 +00006419 EXIT_MKSQUASHFS();
6420 }
plougher1f413c82005-11-18 00:02:14 +00006421
plougher1f413c82005-11-18 00:02:14 +00006422 block_log = slog(block_size = sBlk.block_size);
6423 noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags);
6424 noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags);
6425 noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags);
plougher89c7a512010-12-15 08:35:51 +00006426 noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags);
plougher1f413c82005-11-18 00:02:14 +00006427 no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags);
6428 always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags);
6429 duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags);
plougher0e453652006-11-06 01:49:35 +00006430 exportable = SQUASHFS_EXPORTABLE(sBlk.flags);
plougherafae8252010-12-15 09:12:58 +00006431 no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags);
plougher3d7e5182010-12-25 01:49:42 +00006432 comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags);
plougher4c99cb72007-06-14 21:46:31 +00006433 }
6434
Phillip Lougherc3af83a2014-04-21 21:38:16 +01006435 initialise_threads(readq, fragq, bwriteq, fwriteq, delete,
6436 destination_file);
plougher4c99cb72007-06-14 21:46:31 +00006437
plougher13fdddf2010-11-24 01:23:41 +00006438 res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0);
6439 if(res)
6440 BAD_ERROR("compressor_init failed\n");
6441
plougher4c99cb72007-06-14 21:46:31 +00006442 if(delete) {
plougher3d7e5182010-12-25 01:49:42 +00006443 int size;
Phillip Loughera45c9d22011-02-20 04:24:07 +00006444 void *comp_data = compressor_dump_options(comp, block_size,
6445 &size);
plougher3d7e5182010-12-25 01:49:42 +00006446
Colin Crossbe754902017-12-09 22:27:51 +00006447 printf("Creating %d.%d filesystem on %s, block size %d.\n",
6448 SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size);
plougher3d7e5182010-12-25 01:49:42 +00006449
plougher871c72a2010-12-25 04:17:27 +00006450 /*
6451 * store any compressor specific options after the superblock,
6452 * and set the COMP_OPT flag to show that the filesystem has
6453 * compressor specfic options
6454 */
plougher3d7e5182010-12-25 01:49:42 +00006455 if(comp_data) {
6456 unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT;
6457
6458 SQUASHFS_INSWAP_SHORTS(&c_byte, 1);
plougher64e83fd2010-12-31 21:21:26 +00006459 write_destination(fd, sizeof(struct squashfs_super_block),
plougher29e2ace2010-12-31 08:50:00 +00006460 sizeof(c_byte), &c_byte);
plougher64e83fd2010-12-31 21:21:26 +00006461 write_destination(fd, sizeof(struct squashfs_super_block) +
plougher8e4dad42010-12-28 05:56:22 +00006462 sizeof(c_byte), size, comp_data);
plougher64e83fd2010-12-31 21:21:26 +00006463 bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte)
plougher3d7e5182010-12-25 01:49:42 +00006464 + size;
6465 comp_opts = TRUE;
plougher3d7e5182010-12-25 01:49:42 +00006466 } else
plougher64e83fd2010-12-31 21:21:26 +00006467 bytes = sizeof(struct squashfs_super_block);
plougher4c99cb72007-06-14 21:46:31 +00006468 } else {
plougher360514a2009-03-30 03:01:38 +00006469 unsigned int last_directory_block, inode_dir_offset,
6470 inode_dir_file_size, root_inode_size,
plougher50b31762009-03-31 04:14:46 +00006471 inode_dir_start_block, uncompressed_data,
6472 compressed_data, inode_dir_inode_number,
6473 inode_dir_parent_inode;
plougher360514a2009-03-30 03:01:38 +00006474 unsigned int root_inode_start =
6475 SQUASHFS_INODE_BLK(sBlk.root_inode),
6476 root_inode_offset =
6477 SQUASHFS_INODE_OFFSET(sBlk.root_inode);
plougher4c99cb72007-06-14 21:46:31 +00006478
plougher360514a2009-03-30 03:01:38 +00006479 if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table,
6480 &data_cache, &directory_table,
6481 &directory_data_cache, &last_directory_block,
6482 &inode_dir_offset, &inode_dir_file_size,
6483 &root_inode_size, &inode_dir_start_block,
6484 &file_count, &sym_count, &dev_count, &dir_count,
6485 &fifo_count, &sock_count, &total_bytes,
6486 &total_inode_bytes, &total_directory_bytes,
plougher50b31762009-03-31 04:14:46 +00006487 &inode_dir_inode_number,
6488 &inode_dir_parent_inode, add_old_root_entry,
6489 &fragment_table, &inode_lookup_table)) == 0) {
plougher360514a2009-03-30 03:01:38 +00006490 ERROR("Failed to read existing filesystem - will not "
6491 "overwrite - ABORTING!\n");
plougher50b31762009-03-31 04:14:46 +00006492 ERROR("To force Mksquashfs to write to this block "
6493 "device or file use -noappend\n");
plougher324978d2006-02-27 04:53:29 +00006494 EXIT_MKSQUASHFS();
plougher1f413c82005-11-18 00:02:14 +00006495 }
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00006496 if((append_fragments = fragments = sBlk.fragments)) {
plougher360514a2009-03-30 03:01:38 +00006497 fragment_table = realloc((char *) fragment_table,
plougher50b31762009-03-31 04:14:46 +00006498 ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1))
plougher8ed84b92010-12-31 10:37:24 +00006499 * sizeof(struct squashfs_fragment_entry));
plougher6f2a8262010-07-27 00:37:19 +00006500 if(fragment_table == NULL)
6501 BAD_ERROR("Out of memory in save filesystem state\n");
6502 }
plougher1f413c82005-11-18 00:02:14 +00006503
plougher50b31762009-03-31 04:14:46 +00006504 printf("Appending to existing %d.%d filesystem on %s, block "
plougher978f5882010-12-28 04:34:30 +00006505 "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1],
plougher360514a2009-03-30 03:01:38 +00006506 block_size);
plougher89c7a512010-12-15 08:35:51 +00006507 printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, "
plougherbb988032009-08-06 08:46:34 +00006508 "-always-use-fragments,\n-exportable and -comp options "
6509 "ignored\n");
plougher360514a2009-03-30 03:01:38 +00006510 printf("\nIf appending is not wanted, please re-run with "
6511 "-noappend specified!\n\n");
plougher1f413c82005-11-18 00:02:14 +00006512
plougher360514a2009-03-30 03:01:38 +00006513 compressed_data = (inode_dir_offset + inode_dir_file_size) &
6514 ~(SQUASHFS_METADATA_SIZE - 1);
6515 uncompressed_data = (inode_dir_offset + inode_dir_file_size) &
6516 (SQUASHFS_METADATA_SIZE - 1);
plougher1f413c82005-11-18 00:02:14 +00006517
6518 /* save original filesystem state for restoring ... */
6519 sfragments = fragments;
6520 sbytes = bytes;
6521 sinode_count = sBlk.inodes;
plougher23377982007-11-12 04:04:48 +00006522 scache_bytes = root_inode_offset + root_inode_size;
6523 sdirectory_cache_bytes = uncompressed_data;
ploughera2968ef2009-03-03 10:46:00 +00006524 sdata_cache = malloc(scache_bytes);
plougher332e43d2010-07-21 01:18:30 +00006525 if(sdata_cache == NULL)
6526 BAD_ERROR("Out of memory in save filesystem state\n");
ploughera2968ef2009-03-03 10:46:00 +00006527 sdirectory_data_cache = malloc(sdirectory_cache_bytes);
plougher332e43d2010-07-21 01:18:30 +00006528 if(sdirectory_data_cache == NULL)
6529 BAD_ERROR("Out of memory in save filesystem state\n");
plougher1f413c82005-11-18 00:02:14 +00006530 memcpy(sdata_cache, data_cache, scache_bytes);
plougher360514a2009-03-30 03:01:38 +00006531 memcpy(sdirectory_data_cache, directory_data_cache +
6532 compressed_data, sdirectory_cache_bytes);
plougher1f413c82005-11-18 00:02:14 +00006533 sinode_bytes = root_inode_start;
plougher1f413c82005-11-18 00:02:14 +00006534 stotal_bytes = total_bytes;
6535 stotal_inode_bytes = total_inode_bytes;
plougher50b31762009-03-31 04:14:46 +00006536 stotal_directory_bytes = total_directory_bytes +
6537 compressed_data;
plougher1f413c82005-11-18 00:02:14 +00006538 sfile_count = file_count;
6539 ssym_count = sym_count;
6540 sdev_count = dev_count;
6541 sdir_count = dir_count + 1;
6542 sfifo_count = fifo_count;
6543 ssock_count = sock_count;
6544 sdup_files = dup_files;
plougher1b899fc2008-08-07 01:24:06 +00006545 sid_count = id_count;
plougher99ac0cc2007-10-29 03:17:10 +00006546 write_recovery_data(&sBlk);
Phillip Lougher3bcf67d2013-02-24 10:56:38 +00006547 save_xattrs();
Phillip Lougher3794e342014-04-10 02:15:45 +01006548 appending = TRUE;
plougher1f413c82005-11-18 00:02:14 +00006549
plougher360514a2009-03-30 03:01:38 +00006550 /*
6551 * set the filesystem state up to be able to append to the
plougher50b31762009-03-31 04:14:46 +00006552 * original filesystem. The filesystem state differs depending
6553 * on whether we're appending to the original root directory, or
6554 * if the original root directory becomes a sub-directory
6555 * (root-becomes specified on command line, here root_name !=
6556 * NULL)
plougher1f413c82005-11-18 00:02:14 +00006557 */
6558 inode_bytes = inode_size = root_inode_start;
6559 directory_size = last_directory_block;
6560 cache_size = root_inode_offset + root_inode_size;
6561 directory_cache_size = inode_dir_offset + inode_dir_file_size;
6562 if(root_name) {
plougherca2c93f2008-08-15 08:34:57 +00006563 sdirectory_bytes = last_directory_block;
6564 sdirectory_compressed_bytes = 0;
plougherdf70c3e2006-01-27 09:34:13 +00006565 root_inode_number = inode_dir_parent_inode;
Phillip Lougher539c2b12012-07-30 20:14:52 +01006566 inode_no = sBlk.inodes + 2;
plougher1f413c82005-11-18 00:02:14 +00006567 directory_bytes = last_directory_block;
6568 directory_cache_bytes = uncompressed_data;
plougher360514a2009-03-30 03:01:38 +00006569 memmove(directory_data_cache, directory_data_cache +
6570 compressed_data, uncompressed_data);
plougher1f413c82005-11-18 00:02:14 +00006571 cache_bytes = root_inode_offset + root_inode_size;
plougher360514a2009-03-30 03:01:38 +00006572 add_old_root_entry(root_name, sBlk.root_inode,
6573 inode_dir_inode_number, SQUASHFS_DIR_TYPE);
plougher1f413c82005-11-18 00:02:14 +00006574 total_directory_bytes += compressed_data;
6575 dir_count ++;
6576 } else {
plougher360514a2009-03-30 03:01:38 +00006577 sdirectory_compressed_bytes = last_directory_block -
6578 inode_dir_start_block;
6579 sdirectory_compressed =
6580 malloc(sdirectory_compressed_bytes);
plougher332e43d2010-07-21 01:18:30 +00006581 if(sdirectory_compressed == NULL)
plougher16111452010-07-22 05:12:18 +00006582 BAD_ERROR("Out of memory in save filesystem "
6583 "state\n");
plougher360514a2009-03-30 03:01:38 +00006584 memcpy(sdirectory_compressed, directory_table +
6585 inode_dir_start_block,
6586 sdirectory_compressed_bytes);
plougherca2c93f2008-08-15 08:34:57 +00006587 sdirectory_bytes = inode_dir_start_block;
plougherdf70c3e2006-01-27 09:34:13 +00006588 root_inode_number = inode_dir_inode_number;
Phillip Lougher539c2b12012-07-30 20:14:52 +01006589 inode_no = sBlk.inodes + 1;
plougher1f413c82005-11-18 00:02:14 +00006590 directory_bytes = inode_dir_start_block;
6591 directory_cache_bytes = inode_dir_offset;
6592 cache_bytes = root_inode_offset;
6593 }
6594
plougher360514a2009-03-30 03:01:38 +00006595 inode_count = file_count + dir_count + sym_count + dev_count +
6596 fifo_count + sock_count;
plougher1f413c82005-11-18 00:02:14 +00006597 }
6598
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00006599 if(path)
6600 paths = add_subdir(paths, path);
plougherf9039c92007-10-22 03:54:16 +00006601
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01006602 dump_actions();
Phillip Lougher7a28f7a2014-04-20 23:47:08 +01006603 dump_pseudos();
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01006604
plougher360514a2009-03-30 03:01:38 +00006605 if(delete && !keep_as_directory && source == 1 &&
6606 S_ISDIR(source_buf.st_mode))
Phillip Lougherbae0e422014-01-19 23:42:11 +00006607 dir_scan(&inode, source_path[0], scan1_readdir, progress);
plougher50b31762009-03-31 04:14:46 +00006608 else if(!keep_as_directory && source == 1 &&
6609 S_ISDIR(source_buf.st_mode))
Phillip Lougherbae0e422014-01-19 23:42:11 +00006610 dir_scan(&inode, source_path[0], scan1_single_readdir, progress);
plougher1f413c82005-11-18 00:02:14 +00006611 else
Phillip Lougherbae0e422014-01-19 23:42:11 +00006612 dir_scan(&inode, "", scan1_encomp_readdir, progress);
plougher1f413c82005-11-18 00:02:14 +00006613 sBlk.root_inode = inode;
6614 sBlk.inodes = inode_count;
6615 sBlk.s_magic = SQUASHFS_MAGIC;
6616 sBlk.s_major = SQUASHFS_MAJOR;
plougher978f5882010-12-28 04:34:30 +00006617 sBlk.s_minor = SQUASHFS_MINOR;
plougher1f413c82005-11-18 00:02:14 +00006618 sBlk.block_size = block_size;
6619 sBlk.block_log = block_log;
plougher89c7a512010-12-15 08:35:51 +00006620 sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments,
plougherafae8252010-12-15 09:12:58 +00006621 always_use_fragments, duplicate_checking, exportable,
plougher3d7e5182010-12-25 01:49:42 +00006622 no_xattrs, comp_opts);
plougher1f413c82005-11-18 00:02:14 +00006623 sBlk.mkfs_time = time(NULL);
6624
Phillip Lougher83b8f862013-04-09 03:28:55 +01006625 disable_info();
plougherc9b11db2008-05-06 23:59:15 +00006626
Phillip Loughera709bff2013-03-28 17:48:31 +00006627 while((fragment = get_frag_action(fragment)))
6628 write_fragment(*fragment);
6629 unlock_fragments();
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01006630 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
Phillip Loughera709bff2013-03-28 17:48:31 +00006631 pthread_mutex_lock(&fragment_mutex);
6632 while(fragments_outstanding) {
6633 pthread_mutex_unlock(&fragment_mutex);
6634 sched_yield();
6635 pthread_mutex_lock(&fragment_mutex);
6636 }
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01006637 pthread_cleanup_pop(1);
Phillip Loughera709bff2013-03-28 17:48:31 +00006638
6639 queue_put(to_writer, NULL);
6640 if(queue_get(from_writer) != 0)
6641 EXIT_MKSQUASHFS();
6642
Phillip Loughercd1a5a62014-01-21 04:53:46 +00006643 set_progressbar_state(FALSE);
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00006644 write_filesystem_tables(&sBlk, nopad);
plougher99ac0cc2007-10-29 03:17:10 +00006645
Mohamad Ayyash305fca62016-06-13 15:44:16 -07006646/* ANDROID CHANGES START*/
6647#ifdef ANDROID
6648 if (block_map_file)
6649 fclose(block_map_file);
6650#endif
6651/* ANDROID CHANGES END */
6652
plougher1f413c82005-11-18 00:02:14 +00006653 return 0;
6654}