blob: 905c661afa86e671683d3a55b0b7a785a66ea09c [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,
Phillip Loughercc064952014-01-03 04:26:34 +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>
49#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
plougher68853292005-12-27 02:47:04 +000054#ifndef linux
plougher8cb05cd2005-12-11 23:32:35 +000055#define __BYTE_ORDER BYTE_ORDER
56#define __BIG_ENDIAN BIG_ENDIAN
57#define __LITTLE_ENDIAN LITTLE_ENDIAN
plougher5507dd92006-11-06 00:43:10 +000058#include <sys/sysctl.h>
plougher8cb05cd2005-12-11 23:32:35 +000059#else
60#include <endian.h>
plougher5507dd92006-11-06 00:43:10 +000061#include <sys/sysinfo.h>
plougher8cb05cd2005-12-11 23:32:35 +000062#endif
63
plougher860c1f32010-08-11 01:37:17 +000064#include "squashfs_fs.h"
plougher860c1f32010-08-11 01:37:17 +000065#include "squashfs_swap.h"
66#include "mksquashfs.h"
67#include "sort.h"
68#include "pseudo.h"
69#include "compressor.h"
70#include "xattr.h"
Phillip Lougher4bcb7f82011-08-28 03:18:26 +010071#include "action.h"
Phillip Lougher2477d0d2012-10-24 21:49:28 +010072#include "error.h"
Phillip Lougher9a0e8bb2013-03-10 02:39:24 +000073#include "progressbar.h"
Phillip Lougher2e9fab12013-04-09 03:26:33 +010074#include "info.h"
Phillip Lougher71f39642013-04-09 04:43:21 +010075#include "caches-queues-lists.h"
Phillip Lougher1bbd0cc2013-04-19 04:03:00 +010076#include "read_fs.h"
Phillip Lougher0d67c9d2013-04-19 05:06:50 +010077#include "restore.h"
plougher860c1f32010-08-11 01:37:17 +000078
plougher324978d2006-02-27 04:53:29 +000079int delete = FALSE;
plougher1f413c82005-11-18 00:02:14 +000080int fd;
Phillip Loughera709bff2013-03-28 17:48:31 +000081struct squashfs_super_block sBlk;
plougher1f413c82005-11-18 00:02:14 +000082
83/* filesystem flags for building */
plougher3d7e5182010-12-25 01:49:42 +000084int comp_opts = FALSE;
Phillip Lougher434d50c2014-01-21 03:05:36 +000085int no_xattrs = XATTR_DEF;
86int noX = FALSE;
87int duplicate_checking = TRUE;
88int noF = FALSE;
89int no_fragments = FALSE;
90int always_use_fragments = FALSE;
91int noI = FALSE;
92int noD = FALSE;
plougher1f288f62009-02-21 03:05:52 +000093int silent = TRUE;
plougher02bc3bc2007-02-25 12:12:01 +000094int exportable = TRUE;
plougher8dcc6992007-08-17 19:32:52 +000095int sparse_files = TRUE;
plougher8f8e1a12007-10-18 02:50:21 +000096int old_exclude = TRUE;
97int use_regex = FALSE;
Phillip Loughera709bff2013-03-28 17:48:31 +000098int nopad = FALSE;
Phillip Lougher85776df2014-01-31 03:10:46 +000099int exit_on_error = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000100
Phillip Lougher434d50c2014-01-21 03:05:36 +0000101long long global_uid = -1, global_gid = -1;
102
plougher1f413c82005-11-18 00:02:14 +0000103/* superblock attributes */
104int block_size = SQUASHFS_FILE_SIZE, block_log;
plougher1b899fc2008-08-07 01:24:06 +0000105unsigned int id_count = 0;
plougherfd57dfe2009-03-30 01:17:52 +0000106int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0,
107 sock_count = 0;
plougher1f413c82005-11-18 00:02:14 +0000108
109/* write position within data section */
110long long bytes = 0, total_bytes = 0;
111
112/* in memory directory table - possibly compressed */
113char *directory_table = NULL;
114unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0;
115
116/* cached directory table */
117char *directory_data_cache = NULL;
118unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
119
120/* in memory inode table - possibly compressed */
121char *inode_table = NULL;
122unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0;
123
124/* cached inode table */
125char *data_cache = NULL;
126unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
127
plougher0e453652006-11-06 01:49:35 +0000128/* inode lookup table */
129squashfs_inode *inode_lookup_table = NULL;
130
plougher1f413c82005-11-18 00:02:14 +0000131/* in memory directory data */
132#define I_COUNT_SIZE 128
133#define DIR_ENTRIES 32
134#define INODE_HASH_SIZE 65536
135#define INODE_HASH_MASK (INODE_HASH_SIZE - 1)
136#define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK)
137
138struct cached_dir_index {
plougher2bd2b722010-12-31 10:52:15 +0000139 struct squashfs_dir_index index;
140 char *name;
plougher1f413c82005-11-18 00:02:14 +0000141};
142
143struct directory {
144 unsigned int start_block;
145 unsigned int size;
146 unsigned char *buff;
147 unsigned char *p;
148 unsigned int entry_count;
149 unsigned char *entry_count_p;
150 unsigned int i_count;
151 unsigned int i_size;
152 struct cached_dir_index *index;
153 unsigned char *index_count_p;
154 unsigned int inode_number;
155};
156
plougher1f413c82005-11-18 00:02:14 +0000157struct inode_info *inode_info[INODE_HASH_SIZE];
158
159/* hash tables used to do fast duplicate searches in duplicate check */
plougher5507dd92006-11-06 00:43:10 +0000160struct file_info *dupl[65536];
plougher1f413c82005-11-18 00:02:14 +0000161int dup_files = 0;
162
plougher8f8e1a12007-10-18 02:50:21 +0000163/* exclude file handling */
plougher1f413c82005-11-18 00:02:14 +0000164/* list of exclude dirs/files */
165struct exclude_info {
166 dev_t st_dev;
167 ino_t st_ino;
168};
169
170#define EXCLUDE_SIZE 8192
171int exclude = 0;
172struct exclude_info *exclude_paths = NULL;
plougher8f8e1a12007-10-18 02:50:21 +0000173int old_excluded(char *filename, struct stat *buf);
174
175struct path_entry {
176 char *name;
177 regex_t *preg;
178 struct pathname *paths;
179};
180
181struct pathname {
182 int names;
183 struct path_entry *name;
184};
185
plougherf9039c92007-10-22 03:54:16 +0000186struct pathnames {
187 int count;
188 struct pathname *path[0];
189};
190#define PATHS_ALLOC_SIZE 10
191
192struct pathnames *paths = NULL;
193struct pathname *path = NULL;
plougher05e50ef2007-10-23 12:34:20 +0000194struct pathname *stickypath = NULL;
Phillip Lougherb2abb1c2013-01-09 04:51:21 +0000195int excluded(char *name, struct pathnames *paths, struct pathnames **new);
plougher1f413c82005-11-18 00:02:14 +0000196
plougher1f413c82005-11-18 00:02:14 +0000197int fragments = 0;
plougher76c64082008-03-08 01:32:23 +0000198
plougher1f413c82005-11-18 00:02:14 +0000199#define FRAG_SIZE 32768
Phillip Lougher45702012013-04-30 05:19:54 +0100200#define FRAG_INDEX(n) (-(n + 2))
plougher76c64082008-03-08 01:32:23 +0000201
plougher8ed84b92010-12-31 10:37:24 +0000202struct squashfs_fragment_entry *fragment_table = NULL;
plougher5507dd92006-11-06 00:43:10 +0000203int fragments_outstanding = 0;
plougher1f413c82005-11-18 00:02:14 +0000204
Phillip Lougher7ffdf2a2013-04-14 02:53:30 +0100205int fragments_locked = FALSE;
Phillip Lougher7ffdf2a2013-04-14 02:53:30 +0100206
plougher1f413c82005-11-18 00:02:14 +0000207/* current inode number for directories and non directories */
Phillip Lougher539c2b12012-07-30 20:14:52 +0100208unsigned int inode_no = 1;
plougherdf70c3e2006-01-27 09:34:13 +0000209unsigned int root_inode_number = 0;
plougher1f413c82005-11-18 00:02:14 +0000210
211/* list of source dirs/files */
212int source = 0;
213char **source_path;
214
215/* list of root directory entries read from original filesystem */
216int old_root_entries = 0;
217struct old_root_entry_info {
ploughera326c182009-08-29 05:41:45 +0000218 char *name;
219 struct inode_info inode;
plougher1f413c82005-11-18 00:02:14 +0000220};
221struct old_root_entry_info *old_root_entry;
222
plougherfd57dfe2009-03-30 01:17:52 +0000223/* restore orignal filesystem state if appending to existing filesystem is
224 * cancelled */
plougherca2c93f2008-08-15 08:34:57 +0000225char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed;
plougher1f413c82005-11-18 00:02:14 +0000226
227long long sbytes, stotal_bytes;
228
229unsigned int sinode_bytes, scache_bytes, sdirectory_bytes,
plougherca2c93f2008-08-15 08:34:57 +0000230 sdirectory_cache_bytes, sdirectory_compressed_bytes,
plougher1f413c82005-11-18 00:02:14 +0000231 stotal_inode_bytes, stotal_directory_bytes,
plougher0e453652006-11-06 01:49:35 +0000232 sinode_count = 0, sfile_count, ssym_count, sdev_count,
plougher1f413c82005-11-18 00:02:14 +0000233 sdir_count, sfifo_count, ssock_count, sdup_files;
234int sfragments;
plougher5507dd92006-11-06 00:43:10 +0000235int threads;
plougher1f413c82005-11-18 00:02:14 +0000236
237/* flag whether destination file is a block device */
Phillip Lougher434d50c2014-01-21 03:05:36 +0000238int block_device = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000239
240/* flag indicating whether files are sorted using sort list(s) */
Phillip Lougher434d50c2014-01-21 03:05:36 +0000241int sorted = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000242
plougher324978d2006-02-27 04:53:29 +0000243/* save destination file name for deleting on error */
244char *destination_file = NULL;
245
plougher99ac0cc2007-10-29 03:17:10 +0000246/* recovery file for abnormal exit on appending */
Phillip Lougheraf4c9232012-11-15 03:46:28 +0000247char *recovery_file = NULL;
plougher99ac0cc2007-10-29 03:17:10 +0000248int recover = TRUE;
249
plougher1b899fc2008-08-07 01:24:06 +0000250struct id *id_hash_table[ID_ENTRIES];
251struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS];
252unsigned int uid_count = 0, guid_count = 0;
253unsigned int sid_count = 0, suid_count = 0, sguid_count = 0;
plougher5507dd92006-11-06 00:43:10 +0000254
ploughereb6eac92008-02-26 01:50:48 +0000255struct cache *reader_buffer, *writer_buffer, *fragment_buffer;
Phillip Loughercf478e92013-05-29 02:38:41 +0100256struct queue *to_reader, *to_deflate, *to_writer, *from_writer,
Phillip Lougher6164b5f2013-05-23 05:05:10 +0100257 *to_frag, *locked_fragment;
Phillip Lougher0e1656d2013-05-20 03:47:24 +0100258struct seq_queue *to_main;
Phillip Lougher0280d992014-01-27 05:54:07 +0000259pthread_t reader_thread, writer_thread, main_thread;
260pthread_t *deflator_thread, *frag_deflator_thread;
Phillip Loughercf135d32013-04-08 03:43:25 +0100261pthread_t *restore_thread = NULL;
plougher5507dd92006-11-06 00:43:10 +0000262pthread_mutex_t fragment_mutex;
plougher5507dd92006-11-06 00:43:10 +0000263pthread_mutex_t pos_mutex;
264
265/* user options that control parallelisation */
266int processors = -1;
267/* default size of output buffer in Mbytes */
268#define WRITER_BUFFER_DEFAULT 512
269/* default size of input buffer in Mbytes */
270#define READER_BUFFER_DEFAULT 64
plougher76c64082008-03-08 01:32:23 +0000271/* default size of fragment buffer in Mbytes */
272#define FRAGMENT_BUFFER_DEFAULT 64
plougher5507dd92006-11-06 00:43:10 +0000273int writer_buffer_size;
plougher5507dd92006-11-06 00:43:10 +0000274
plougherc5d59872010-11-22 01:36:01 +0000275/* compression operations */
Phillip Lougher1f6c7402014-01-06 04:16:48 +0000276static struct compressor *comp = NULL;
Phillip Lougher434d50c2014-01-21 03:05:36 +0000277int compressor_opt_parsed = FALSE;
plougher13fdddf2010-11-24 01:23:41 +0000278void *stream = NULL;
plougher7b8ee502009-07-29 07:54:30 +0000279
plougher860c1f32010-08-11 01:37:17 +0000280/* xattr stats */
281unsigned int xattr_bytes = 0, total_xattr_bytes = 0;
282
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +0000283/* fragment to file mapping used when appending */
284int append_fragments = 0;
285struct append_file **file_mapping;
286
plougher49b57a92009-03-31 03:23:05 +0000287char *read_from_disk(long long start, unsigned int avail_bytes);
plougherfd57dfe2009-03-30 01:17:52 +0000288void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
289 int type);
plougherfd57dfe2009-03-30 01:17:52 +0000290struct file_info *duplicate(long long file_size, long long bytes,
291 unsigned int **block_list, long long *start, struct fragment **fragment,
292 struct file_buffer *file_buffer, int blocks, unsigned short checksum,
293 unsigned short fragment_checksum, int checksum_flag);
Phillip Lougherb38c1722012-02-09 23:26:19 +0000294struct dir_info *dir_scan1(char *, char *, struct pathnames *,
Phillip Lougher494479f2012-02-03 15:45:41 +0000295 struct dir_ent *(_readdir)(struct dir_info *), int);
Phillip Lougher24eeb772012-10-13 01:56:24 +0100296void dir_scan2(struct dir_info *dir, struct pseudo *pseudo);
Phillip Lougher23d83622012-10-14 02:35:22 +0100297void dir_scan3(struct dir_info *root, struct dir_info *dir);
298void dir_scan4(struct dir_info *dir);
Phillip Lougher2abfcf72012-11-11 03:59:56 +0000299void dir_scan5(struct dir_info *dir);
300void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info);
plougherfd57dfe2009-03-30 01:17:52 +0000301struct file_info *add_non_dup(long long file_size, long long bytes,
302 unsigned int *block_list, long long start, struct fragment *fragment,
303 unsigned short checksum, unsigned short fragment_checksum,
304 int checksum_flag);
ploughera0a49c32010-08-11 01:47:59 +0000305long long generic_write_table(int, void *, int, void *, int);
plougherca61d1c2010-07-20 18:32:32 +0000306void restorefs();
Phillip Lougher1eae83d2012-09-26 02:12:45 +0100307struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth);
Phillip Loughera709bff2013-03-28 17:48:31 +0000308void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad);
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +0000309unsigned short get_checksum_mem(char *buff, int bytes);
plougher5507dd92006-11-06 00:43:10 +0000310
311
Phillip Lougher9b7c3c22013-03-10 02:29:42 +0000312void prep_exit()
Phillip Lougher838d2962012-10-23 03:29:30 +0100313{
Phillip Loughercf135d32013-04-08 03:43:25 +0100314 if(restore_thread) {
315 if(pthread_self() == *restore_thread) {
316 /*
317 * Recursive failure when trying to restore filesystem!
318 * Nothing to do except to exit, otherwise we'll just
319 * appear to hang. The user should be able to restore
320 * from the recovery file (which is why it was added, in
321 * case of catastrophic failure in Mksquashfs)
322 */
323 exit(1);
324 } else {
325 /* signal the restore thread to restore */
Phillip Lougher8c740812014-01-30 01:56:17 +0000326 pthread_kill(*restore_thread, SIGUSR1);
Phillip Loughercf135d32013-04-08 03:43:25 +0100327 pthread_exit(NULL);
328 }
Phillip Lougher9e071912013-04-02 05:30:29 +0100329 }
Phillip Lougher838d2962012-10-23 03:29:30 +0100330 if(delete && destination_file && !block_device)
331 unlink(destination_file);
Phillip Lougher838d2962012-10-23 03:29:30 +0100332}
333
334
Phillip Loughere1668fe2012-12-30 05:48:12 +0000335int add_overflow(int a, int b)
336{
337 return (INT_MAX - a) < b;
338}
339
340
341int shift_overflow(int a, int shift)
342{
343 return (INT_MAX >> shift) < a;
344}
345
346
347int multiply_overflow(int a, int multiplier)
348{
349 return (INT_MAX / multiplier) < a;
350}
351
352
plougher50b31762009-03-31 04:14:46 +0000353#define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \
354 + (((char *)A) - data_cache)))
plougher1f413c82005-11-18 00:02:14 +0000355
356
357void restorefs()
358{
359 ERROR("Exiting - restoring original filesystem!\n\n");
plougher5507dd92006-11-06 00:43:10 +0000360
plougher1f413c82005-11-18 00:02:14 +0000361 bytes = sbytes;
362 memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes);
plougherfd57dfe2009-03-30 01:17:52 +0000363 memcpy(directory_data_cache, sdirectory_data_cache,
364 sdirectory_cache_bytes);
365 directory_cache_bytes = sdirectory_cache_bytes;
plougher1f413c82005-11-18 00:02:14 +0000366 inode_bytes = sinode_bytes;
367 directory_bytes = sdirectory_bytes;
plougherfd57dfe2009-03-30 01:17:52 +0000368 memcpy(directory_table + directory_bytes, sdirectory_compressed,
369 sdirectory_compressed_bytes);
plougherca2c93f2008-08-15 08:34:57 +0000370 directory_bytes += sdirectory_compressed_bytes;
plougher1f413c82005-11-18 00:02:14 +0000371 total_bytes = stotal_bytes;
372 total_inode_bytes = stotal_inode_bytes;
373 total_directory_bytes = stotal_directory_bytes;
374 inode_count = sinode_count;
375 file_count = sfile_count;
376 sym_count = ssym_count;
377 dev_count = sdev_count;
378 dir_count = sdir_count;
379 fifo_count = sfifo_count;
380 sock_count = ssock_count;
381 dup_files = sdup_files;
382 fragments = sfragments;
plougher1b899fc2008-08-07 01:24:06 +0000383 id_count = sid_count;
plougher21f63b32010-07-18 03:59:04 +0000384 restore_xattrs();
Phillip Loughera709bff2013-03-28 17:48:31 +0000385 write_filesystem_tables(&sBlk, nopad);
386 exit(1);
plougher1f413c82005-11-18 00:02:14 +0000387}
388
389
Phillip Lougherb8ec5202013-03-28 20:14:37 +0000390void sighandler()
plougher324978d2006-02-27 04:53:29 +0000391{
392 EXIT_MKSQUASHFS();
393}
394
395
plougher13fdddf2010-11-24 01:23:41 +0000396int mangle2(void *strm, char *d, char *s, int size,
plougher50b31762009-03-31 04:14:46 +0000397 int block_size, int uncompressed, int data_block)
plougher5507dd92006-11-06 00:43:10 +0000398{
plougher7b8ee502009-07-29 07:54:30 +0000399 int error, c_byte = 0;
plougher5507dd92006-11-06 00:43:10 +0000400
plougher7b8ee502009-07-29 07:54:30 +0000401 if(!uncompressed) {
plougherbfb876c2010-12-31 07:48:01 +0000402 c_byte = compressor_compress(comp, strm, d, s, size, block_size,
403 &error);
plougher7b8ee502009-07-29 07:54:30 +0000404 if(c_byte == -1)
405 BAD_ERROR("mangle2:: %s compress failed with error "
406 "code %d\n", comp->name, error);
plougher1f413c82005-11-18 00:02:14 +0000407 }
408
plougher7b8ee502009-07-29 07:54:30 +0000409 if(c_byte == 0 || c_byte >= size) {
plougher1f413c82005-11-18 00:02:14 +0000410 memcpy(d, s, size);
plougherfd57dfe2009-03-30 01:17:52 +0000411 return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK :
412 SQUASHFS_COMPRESSED_BIT);
plougher1f413c82005-11-18 00:02:14 +0000413 }
414
plougher7b8ee502009-07-29 07:54:30 +0000415 return c_byte;
plougher1f413c82005-11-18 00:02:14 +0000416}
417
418
plougher7b8ee502009-07-29 07:54:30 +0000419int mangle(char *d, char *s, int size, int block_size,
plougher50b31762009-03-31 04:14:46 +0000420 int uncompressed, int data_block)
plougher5507dd92006-11-06 00:43:10 +0000421{
plougher13fdddf2010-11-24 01:23:41 +0000422 return mangle2(stream, d, s, size, block_size, uncompressed,
plougher50b31762009-03-31 04:14:46 +0000423 data_block);
plougher5507dd92006-11-06 00:43:10 +0000424}
425
426
plougherac28cd12010-02-24 02:25:03 +0000427void *get_inode(int req_size)
plougher1f413c82005-11-18 00:02:14 +0000428{
429 int data_space;
430 unsigned short c_byte;
431
432 while(cache_bytes >= SQUASHFS_METADATA_SIZE) {
plougherfd57dfe2009-03-30 01:17:52 +0000433 if((inode_size - inode_bytes) <
434 ((SQUASHFS_METADATA_SIZE << 1)) + 2) {
plougher1eb2a662010-07-26 17:30:50 +0000435 void *it = realloc(inode_table, inode_size +
plougherfd57dfe2009-03-30 01:17:52 +0000436 (SQUASHFS_METADATA_SIZE << 1) + 2);
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000437 if(it == NULL)
438 MEM_ERROR();
plougher1eb2a662010-07-26 17:30:50 +0000439 inode_table = it;
plougher1f413c82005-11-18 00:02:14 +0000440 inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
441 }
442
plougherfd57dfe2009-03-30 01:17:52 +0000443 c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET,
plougher50b31762009-03-31 04:14:46 +0000444 data_cache, SQUASHFS_METADATA_SIZE,
445 SQUASHFS_METADATA_SIZE, noI, 0);
rlougher8f7d0b82007-11-08 15:33:29 +0000446 TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte);
plougherac28cd12010-02-24 02:25:03 +0000447 SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1);
plougher1b899fc2008-08-07 01:24:06 +0000448 inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET;
449 total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET;
plougher14dd5f32010-08-02 18:20:03 +0000450 memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE,
plougherfd57dfe2009-03-30 01:17:52 +0000451 cache_bytes - SQUASHFS_METADATA_SIZE);
plougher1f413c82005-11-18 00:02:14 +0000452 cache_bytes -= SQUASHFS_METADATA_SIZE;
453 }
454
455 data_space = (cache_size - cache_bytes);
456 if(data_space < req_size) {
plougherfd57dfe2009-03-30 01:17:52 +0000457 int realloc_size = cache_size == 0 ?
458 ((req_size + SQUASHFS_METADATA_SIZE) &
459 ~(SQUASHFS_METADATA_SIZE - 1)) : req_size -
460 data_space;
plougher1f413c82005-11-18 00:02:14 +0000461
plougher17248ca2010-07-27 00:24:35 +0000462 void *dc = realloc(data_cache, cache_size +
plougherfd57dfe2009-03-30 01:17:52 +0000463 realloc_size);
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000464 if(dc == NULL)
465 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +0000466 cache_size += realloc_size;
plougher17248ca2010-07-27 00:24:35 +0000467 data_cache = dc;
plougher1f413c82005-11-18 00:02:14 +0000468 }
469
470 cache_bytes += req_size;
471
plougherac28cd12010-02-24 02:25:03 +0000472 return data_cache + cache_bytes - req_size;
plougher1f413c82005-11-18 00:02:14 +0000473}
474
475
plougher8a8c4102009-03-29 22:28:49 +0000476int read_bytes(int fd, void *buff, int bytes)
plougher06a19d32009-03-29 22:00:03 +0000477{
478 int res, count;
479
480 for(count = 0; count < bytes; count += res) {
481 res = read(fd, buff + count, bytes - count);
482 if(res < 1) {
plougher96f85a12009-03-29 22:39:59 +0000483 if(res == 0)
484 goto bytes_read;
485 else if(errno != EINTR) {
plougher06a19d32009-03-29 22:00:03 +0000486 ERROR("Read failed because %s\n",
487 strerror(errno));
488 return -1;
489 } else
490 res = 0;
491 }
492 }
493
494bytes_read:
495 return count;
496}
497
498
plougher3306cb22010-06-18 04:22:44 +0000499int read_fs_bytes(int fd, long long byte, int bytes, void *buff)
plougher1f413c82005-11-18 00:02:14 +0000500{
501 off_t off = byte;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100502 int res = 1;
plougher1f413c82005-11-18 00:02:14 +0000503
plougher1d065e92010-06-18 03:58:27 +0000504 TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n",
plougherfd57dfe2009-03-30 01:17:52 +0000505 byte, bytes);
plougher06a19d32009-03-29 22:00:03 +0000506
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100507 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
plougher5507dd92006-11-06 00:43:10 +0000508 pthread_mutex_lock(&pos_mutex);
plougher1d065e92010-06-18 03:58:27 +0000509 if(lseek(fd, off, SEEK_SET) == -1) {
Phillip Lougher1d3177a2013-02-06 21:48:58 +0000510 ERROR("read_fs_bytes: Lseek on destination failed because %s, "
511 "offset=0x%llx\n", strerror(errno), off);
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100512 res = 0;
Phillip Lougher041cf6d2013-07-20 04:20:40 +0100513 } else if(read_bytes(fd, buff, bytes) < bytes) {
plougher1d065e92010-06-18 03:58:27 +0000514 ERROR("Read on destination failed\n");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100515 res = 0;
plougher1d065e92010-06-18 03:58:27 +0000516 }
517
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100518 pthread_cleanup_pop(1);
519 return res;
plougher1f413c82005-11-18 00:02:14 +0000520}
521
522
plougher628e7682009-03-29 22:12:24 +0000523int write_bytes(int fd, void *buff, int bytes)
plougher0dd6f122009-03-29 21:43:57 +0000524{
525 int res, count;
526
527 for(count = 0; count < bytes; count += res) {
528 res = write(fd, buff + count, bytes - count);
529 if(res == -1) {
530 if(errno != EINTR) {
531 ERROR("Write failed because %s\n",
532 strerror(errno));
533 return -1;
534 }
535 res = 0;
536 }
537 }
538
539 return 0;
540}
541
542
plougher29e2ace2010-12-31 08:50:00 +0000543void write_destination(int fd, long long byte, int bytes, void *buff)
plougher1f413c82005-11-18 00:02:14 +0000544{
545 off_t off = byte;
plougher1f413c82005-11-18 00:02:14 +0000546
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100547 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
548 pthread_mutex_lock(&pos_mutex);
plougher5507dd92006-11-06 00:43:10 +0000549
Phillip Loughera0c23462013-02-12 03:30:12 +0000550 if(lseek(fd, off, SEEK_SET) == -1) {
551 ERROR("write_destination: Lseek on destination "
Phillip Lougher1d3177a2013-02-06 21:48:58 +0000552 "failed because %s, offset=0x%llx\n", strerror(errno),
553 off);
Phillip Loughera0c23462013-02-12 03:30:12 +0000554 BAD_ERROR("Probably out of space on output %s\n",
555 block_device ? "block device" : "filesystem");
556 }
plougher1f413c82005-11-18 00:02:14 +0000557
plougher0dd6f122009-03-29 21:43:57 +0000558 if(write_bytes(fd, buff, bytes) == -1)
Phillip Loughere8b536f2013-02-12 22:01:21 +0000559 BAD_ERROR("Failed to write to output %s\n",
560 block_device ? "block device" : "filesystem");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100561
562 pthread_cleanup_pop(1);
plougher1f413c82005-11-18 00:02:14 +0000563}
564
565
566long long write_inodes()
567{
568 unsigned short c_byte;
569 int avail_bytes;
570 char *datap = data_cache;
571 long long start_bytes = bytes;
572
573 while(cache_bytes) {
plougherfd57dfe2009-03-30 01:17:52 +0000574 if(inode_size - inode_bytes <
575 ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
plougher1eb2a662010-07-26 17:30:50 +0000576 void *it = realloc(inode_table, inode_size +
plougherfd57dfe2009-03-30 01:17:52 +0000577 ((SQUASHFS_METADATA_SIZE << 1) + 2));
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000578 if(it == NULL)
579 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +0000580 inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
plougher1eb2a662010-07-26 17:30:50 +0000581 inode_table = it;
plougher1f413c82005-11-18 00:02:14 +0000582 }
plougherfd57dfe2009-03-30 01:17:52 +0000583 avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ?
584 SQUASHFS_METADATA_SIZE : cache_bytes;
585 c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap,
586 avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0);
rlougher8f7d0b82007-11-08 15:33:29 +0000587 TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte);
plougherac28cd12010-02-24 02:25:03 +0000588 SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1);
plougher1b899fc2008-08-07 01:24:06 +0000589 inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET;
590 total_inode_bytes += avail_bytes + BLOCK_OFFSET;
plougher1f413c82005-11-18 00:02:14 +0000591 datap += avail_bytes;
592 cache_bytes -= avail_bytes;
593 }
594
plougher29e2ace2010-12-31 08:50:00 +0000595 write_destination(fd, bytes, inode_bytes, inode_table);
plougher1f413c82005-11-18 00:02:14 +0000596 bytes += inode_bytes;
597
598 return start_bytes;
599}
600
601
602long long write_directories()
603{
604 unsigned short c_byte;
605 int avail_bytes;
606 char *directoryp = directory_data_cache;
607 long long start_bytes = bytes;
608
609 while(directory_cache_bytes) {
plougherfd57dfe2009-03-30 01:17:52 +0000610 if(directory_size - directory_bytes <
611 ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
plougher79d665a2010-07-27 00:19:23 +0000612 void *dt = realloc(directory_table,
plougherfd57dfe2009-03-30 01:17:52 +0000613 directory_size + ((SQUASHFS_METADATA_SIZE << 1)
614 + 2));
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000615 if(dt == NULL)
616 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +0000617 directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
plougher79d665a2010-07-27 00:19:23 +0000618 directory_table = dt;
plougher1f413c82005-11-18 00:02:14 +0000619 }
plougherfd57dfe2009-03-30 01:17:52 +0000620 avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ?
621 SQUASHFS_METADATA_SIZE : directory_cache_bytes;
plougher50b31762009-03-31 04:14:46 +0000622 c_byte = mangle(directory_table + directory_bytes +
623 BLOCK_OFFSET, directoryp, avail_bytes,
624 SQUASHFS_METADATA_SIZE, noI, 0);
plougherfd57dfe2009-03-30 01:17:52 +0000625 TRACE("Directory block @ 0x%x, size %d\n", directory_bytes,
626 c_byte);
plougherac28cd12010-02-24 02:25:03 +0000627 SQUASHFS_SWAP_SHORTS(&c_byte,
628 directory_table + directory_bytes, 1);
plougher50b31762009-03-31 04:14:46 +0000629 directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) +
630 BLOCK_OFFSET;
plougher1b899fc2008-08-07 01:24:06 +0000631 total_directory_bytes += avail_bytes + BLOCK_OFFSET;
plougher1f413c82005-11-18 00:02:14 +0000632 directoryp += avail_bytes;
633 directory_cache_bytes -= avail_bytes;
634 }
plougher29e2ace2010-12-31 08:50:00 +0000635 write_destination(fd, bytes, directory_bytes, directory_table);
plougher1f413c82005-11-18 00:02:14 +0000636 bytes += directory_bytes;
637
638 return start_bytes;
639}
640
641
plougher1b899fc2008-08-07 01:24:06 +0000642long long write_id_table()
plougher1f413c82005-11-18 00:02:14 +0000643{
plougher1b899fc2008-08-07 01:24:06 +0000644 unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count);
plougherac28cd12010-02-24 02:25:03 +0000645 unsigned int p[id_count];
plougher1f413c82005-11-18 00:02:14 +0000646 int i;
647
plougher1b899fc2008-08-07 01:24:06 +0000648 TRACE("write_id_table: ids %d, id_bytes %d\n", id_count, id_bytes);
plougherac28cd12010-02-24 02:25:03 +0000649 for(i = 0; i < id_count; i++) {
plougher1b899fc2008-08-07 01:24:06 +0000650 TRACE("write_id_table: id index %d, id %d", i, id_table[i]->id);
plougherac28cd12010-02-24 02:25:03 +0000651 SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1);
plougher1f413c82005-11-18 00:02:14 +0000652 }
653
ploughera0a49c32010-08-11 01:47:59 +0000654 return generic_write_table(id_bytes, p, 0, NULL, noI);
plougher1f413c82005-11-18 00:02:14 +0000655}
656
657
plougher1b899fc2008-08-07 01:24:06 +0000658struct id *get_id(unsigned int id)
plougher1f413c82005-11-18 00:02:14 +0000659{
plougher1b899fc2008-08-07 01:24:06 +0000660 int hash = ID_HASH(id);
661 struct id *entry = id_hash_table[hash];
plougher1f413c82005-11-18 00:02:14 +0000662
plougher1b899fc2008-08-07 01:24:06 +0000663 for(; entry; entry = entry->next)
664 if(entry->id == id)
665 break;
plougher1f413c82005-11-18 00:02:14 +0000666
plougher1b899fc2008-08-07 01:24:06 +0000667 return entry;
plougher1f413c82005-11-18 00:02:14 +0000668}
669
670
plougher1b899fc2008-08-07 01:24:06 +0000671struct id *create_id(unsigned int id)
672{
673 int hash = ID_HASH(id);
674 struct id *entry = malloc(sizeof(struct id));
675 if(entry == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000676 MEM_ERROR();
plougher1b899fc2008-08-07 01:24:06 +0000677 entry->id = id;
678 entry->index = id_count ++;
679 entry->flags = 0;
680 entry->next = id_hash_table[hash];
681 id_hash_table[hash] = entry;
682 id_table[entry->index] = entry;
683 return entry;
684}
685
686
687unsigned int get_uid(unsigned int uid)
688{
689 struct id *entry = get_id(uid);
690
691 if(entry == NULL) {
692 if(id_count == SQUASHFS_IDS)
693 BAD_ERROR("Out of uids!\n");
694 entry = create_id(uid);
695 }
696
697 if((entry->flags & ISA_UID) == 0) {
698 entry->flags |= ISA_UID;
699 uid_count ++;
700 }
701
702 return entry->index;
703}
704
705
706unsigned int get_guid(unsigned int guid)
707{
708 struct id *entry = get_id(guid);
709
710 if(entry == NULL) {
711 if(id_count == SQUASHFS_IDS)
712 BAD_ERROR("Out of gids!\n");
713 entry = create_id(guid);
714 }
715
716 if((entry->flags & ISA_GID) == 0) {
717 entry->flags |= ISA_GID;
718 guid_count ++;
719 }
720
721 return entry->index;
722}
723
724
Phillip Lougher5ef51692012-11-19 03:35:39 +0000725#define ALLOC_SIZE 128
Phillip Lougher494479f2012-02-03 15:45:41 +0000726
Phillip Lougher5ef51692012-11-19 03:35:39 +0000727char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size)
728{
729 if(pathname == NULL) {
730 pathname = malloc(ALLOC_SIZE);
731 if(pathname == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000732 MEM_ERROR();
Phillip Lougher5ef51692012-11-19 03:35:39 +0000733 }
734
735 for(;;) {
736 int res = snprintf(pathname, *size, "%s/%s",
737 dir_ent->our_dir->pathname,
738 dir_ent->source_name ? : dir_ent->name);
739
740 if(res < 0)
741 BAD_ERROR("snprintf failed in pathname\n");
742 else if(res >= *size) {
743 /*
744 * pathname is too small to contain the result, so
745 * increase it and try again
746 */
747 *size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1);
748 pathname = realloc(pathname, *size);
749 if(pathname == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000750 MEM_ERROR();
Phillip Lougher5ef51692012-11-19 03:35:39 +0000751 } else
752 break;
753 }
Phillip Lougher494479f2012-02-03 15:45:41 +0000754
755 return pathname;
756}
757
758
759char *pathname(struct dir_ent *dir_ent)
760{
Phillip Lougher5ef51692012-11-19 03:35:39 +0000761 static char *pathname = NULL;
762 static int size = ALLOC_SIZE;
Phillip Lougher494479f2012-02-03 15:45:41 +0000763
Phillip Lougher5ef51692012-11-19 03:35:39 +0000764 if (dir_ent->nonstandard_pathname)
765 return dir_ent->nonstandard_pathname;
766
767 return pathname = _pathname(dir_ent, pathname, &size);
Phillip Lougher494479f2012-02-03 15:45:41 +0000768}
769
770
771char *pathname_reader(struct dir_ent *dir_ent)
772{
Phillip Lougher5ef51692012-11-19 03:35:39 +0000773 static char *pathname = NULL;
774 static int size = ALLOC_SIZE;
Phillip Lougher494479f2012-02-03 15:45:41 +0000775
Phillip Lougher5ef51692012-11-19 03:35:39 +0000776 if (dir_ent->nonstandard_pathname)
777 return dir_ent->nonstandard_pathname;
778
779 return pathname = _pathname(dir_ent, pathname, &size);
Phillip Lougher494479f2012-02-03 15:45:41 +0000780}
781
782
Phillip Lougherb38c1722012-02-09 23:26:19 +0000783char *subpathname(struct dir_ent *dir_ent)
784{
Phillip Lougherd457ed32012-11-19 01:36:56 +0000785 static char *subpath = NULL;
786 static int size = ALLOC_SIZE;
787 int res;
Phillip Lougherb38c1722012-02-09 23:26:19 +0000788
Phillip Lougherd457ed32012-11-19 01:36:56 +0000789 if(subpath == NULL) {
790 subpath = malloc(ALLOC_SIZE);
791 if(subpath == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000792 MEM_ERROR();
Phillip Lougherd457ed32012-11-19 01:36:56 +0000793 }
794
795 for(;;) {
796 if(dir_ent->our_dir->subpath[0] != '\0')
797 res = snprintf(subpath, size, "%s/%s",
798 dir_ent->our_dir->subpath, dir_ent->name);
799 else
800 res = snprintf(subpath, size, "/%s", dir_ent->name);
801
802 if(res < 0)
803 BAD_ERROR("snprintf failed in subpathname\n");
804 else if(res >= size) {
805 /*
806 * subpath is too small to contain the result, so
807 * increase it and try again
808 */
809 size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1);
810 subpath = realloc(subpath, size);
811 if(subpath == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000812 MEM_ERROR();
Phillip Lougherd457ed32012-11-19 01:36:56 +0000813 } else
814 break;
Phillip Lougher90e08252012-10-05 04:33:35 +0100815 }
Phillip Lougherb38c1722012-02-09 23:26:19 +0000816
817 return subpath;
818}
819
820
Phillip Lougher539c2b12012-07-30 20:14:52 +0100821inline unsigned int get_inode_no(struct inode_info *inode)
Phillip Lougher53cef742012-07-25 05:12:50 +0100822{
Phillip Lougher539c2b12012-07-30 20:14:52 +0100823 return inode->inode_number;
Phillip Lougher53cef742012-07-25 05:12:50 +0100824}
825
826
Phillip Lougher539c2b12012-07-30 20:14:52 +0100827inline unsigned int get_parent_no(struct dir_info *dir)
Phillip Lougher53cef742012-07-25 05:12:50 +0100828{
Phillip Lougher1eae83d2012-09-26 02:12:45 +0100829 return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no;
Phillip Lougher53cef742012-07-25 05:12:50 +0100830}
831
832
plougher3c6bdb52010-05-01 02:30:59 +0000833int create_inode(squashfs_inode *i_no, struct dir_info *dir_info,
834 struct dir_ent *dir_ent, int type, long long byte_size,
835 long long start_block, unsigned int offset, unsigned int *block_list,
836 struct fragment *fragment, struct directory *dir_in, long long sparse)
plougher1f413c82005-11-18 00:02:14 +0000837{
838 struct stat *buf = &dir_ent->inode->buf;
plougher8973a122010-12-31 09:52:45 +0000839 union squashfs_inode_header inode_header;
plougher857d0dd2010-12-31 21:03:13 +0000840 struct squashfs_base_inode_header *base = &inode_header.base;
plougherac28cd12010-02-24 02:25:03 +0000841 void *inode;
Phillip Lougher494479f2012-02-03 15:45:41 +0000842 char *filename = pathname(dir_ent);
plougher1f413c82005-11-18 00:02:14 +0000843 int nlink = dir_ent->inode->nlink;
ploughere6e0e1b2010-05-12 17:17:06 +0000844 int xattr = read_xattrs(dir_ent);
plougher1f413c82005-11-18 00:02:14 +0000845
ploughere6e0e1b2010-05-12 17:17:06 +0000846 switch(type) {
847 case SQUASHFS_FILE_TYPE:
848 if(dir_ent->inode->nlink > 1 ||
849 byte_size >= (1LL << 32) ||
850 start_block >= (1LL << 32) ||
851 sparse || IS_XATTR(xattr))
852 type = SQUASHFS_LREG_TYPE;
853 break;
854 case SQUASHFS_DIR_TYPE:
855 if(dir_info->dir_is_ldir || IS_XATTR(xattr))
856 type = SQUASHFS_LDIR_TYPE;
857 break;
858 case SQUASHFS_SYMLINK_TYPE:
859 if(IS_XATTR(xattr))
860 type = SQUASHFS_LSYMLINK_TYPE;
861 break;
862 case SQUASHFS_BLKDEV_TYPE:
863 if(IS_XATTR(xattr))
864 type = SQUASHFS_LBLKDEV_TYPE;
865 break;
866 case SQUASHFS_CHRDEV_TYPE:
867 if(IS_XATTR(xattr))
868 type = SQUASHFS_LCHRDEV_TYPE;
869 break;
plougherc5d69322010-05-12 19:28:38 +0000870 case SQUASHFS_FIFO_TYPE:
871 if(IS_XATTR(xattr))
872 type = SQUASHFS_LFIFO_TYPE;
873 break;
874 case SQUASHFS_SOCKET_TYPE:
875 if(IS_XATTR(xattr))
876 type = SQUASHFS_LSOCKET_TYPE;
877 break;
ploughere6e0e1b2010-05-12 17:17:06 +0000878 }
879
plougher1f413c82005-11-18 00:02:14 +0000880 base->mode = SQUASHFS_MODE(buf->st_mode);
plougherfd57dfe2009-03-30 01:17:52 +0000881 base->uid = get_uid((unsigned int) global_uid == -1 ?
882 buf->st_uid : global_uid);
plougher1f413c82005-11-18 00:02:14 +0000883 base->inode_type = type;
plougherfd57dfe2009-03-30 01:17:52 +0000884 base->guid = get_guid((unsigned int) global_gid == -1 ?
885 buf->st_gid : global_gid);
plougher1f413c82005-11-18 00:02:14 +0000886 base->mtime = buf->st_mtime;
Phillip Lougher539c2b12012-07-30 20:14:52 +0100887 base->inode_number = get_inode_no(dir_ent->inode);
plougher1f413c82005-11-18 00:02:14 +0000888
889 if(type == SQUASHFS_FILE_TYPE) {
890 int i;
plougher8701ed62010-12-31 20:38:38 +0000891 struct squashfs_reg_inode_header *reg = &inode_header.reg;
892 size_t off = offsetof(struct squashfs_reg_inode_header, block_list);
plougher1f413c82005-11-18 00:02:14 +0000893
894 inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
plougher1f413c82005-11-18 00:02:14 +0000895 reg->file_size = byte_size;
896 reg->start_block = start_block;
897 reg->fragment = fragment->index;
898 reg->offset = fragment->offset;
plougherac28cd12010-02-24 02:25:03 +0000899 SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode);
900 SQUASHFS_SWAP_INTS(block_list, inode + off, offset);
plougher50b31762009-03-31 04:14:46 +0000901 TRACE("File inode, file_size %lld, start_block 0x%llx, blocks "
902 "%d, fragment %d, offset %d, size %d\n", byte_size,
plougherfd57dfe2009-03-30 01:17:52 +0000903 start_block, offset, fragment->index, fragment->offset,
904 fragment->size);
plougher1f413c82005-11-18 00:02:14 +0000905 for(i = 0; i < offset; i++)
906 TRACE("Block %d, size %d\n", i, block_list[i]);
907 }
908 else if(type == SQUASHFS_LREG_TYPE) {
909 int i;
plougher1e6ac4a2010-12-31 20:42:02 +0000910 struct squashfs_lreg_inode_header *reg = &inode_header.lreg;
911 size_t off = offsetof(struct squashfs_lreg_inode_header, block_list);
plougher1f413c82005-11-18 00:02:14 +0000912
913 inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
plougher1f413c82005-11-18 00:02:14 +0000914 reg->nlink = nlink;
915 reg->file_size = byte_size;
916 reg->start_block = start_block;
917 reg->fragment = fragment->index;
918 reg->offset = fragment->offset;
plougherf5a674d2009-03-25 05:38:27 +0000919 if(sparse && sparse >= byte_size)
920 sparse = byte_size - 1;
plougher1b899fc2008-08-07 01:24:06 +0000921 reg->sparse = sparse;
ploughere6e0e1b2010-05-12 17:17:06 +0000922 reg->xattr = xattr;
plougherac28cd12010-02-24 02:25:03 +0000923 SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode);
924 SQUASHFS_SWAP_INTS(block_list, inode + off, offset);
plougherfd57dfe2009-03-30 01:17:52 +0000925 TRACE("Long file inode, file_size %lld, start_block 0x%llx, "
plougher50b31762009-03-31 04:14:46 +0000926 "blocks %d, fragment %d, offset %d, size %d, nlink %d"
927 "\n", byte_size, start_block, offset, fragment->index,
plougherfd57dfe2009-03-30 01:17:52 +0000928 fragment->offset, fragment->size, nlink);
plougher1f413c82005-11-18 00:02:14 +0000929 for(i = 0; i < offset; i++)
930 TRACE("Block %d, size %d\n", i, block_list[i]);
931 }
932 else if(type == SQUASHFS_LDIR_TYPE) {
933 int i;
934 unsigned char *p;
plougher2611d392010-12-31 20:50:36 +0000935 struct squashfs_ldir_inode_header *dir = &inode_header.ldir;
plougher1f413c82005-11-18 00:02:14 +0000936 struct cached_dir_index *index = dir_in->index;
937 unsigned int i_count = dir_in->i_count;
938 unsigned int i_size = dir_in->i_size;
939
940 if(byte_size >= 1 << 27)
941 BAD_ERROR("directory greater than 2^27-1 bytes!\n");
942
943 inode = get_inode(sizeof(*dir) + i_size);
plougher1f413c82005-11-18 00:02:14 +0000944 dir->inode_type = SQUASHFS_LDIR_TYPE;
plougher1f413c82005-11-18 00:02:14 +0000945 dir->nlink = dir_ent->dir->directory_count + 2;
946 dir->file_size = byte_size;
947 dir->offset = offset;
948 dir->start_block = start_block;
949 dir->i_count = i_count;
Phillip Lougher539c2b12012-07-30 20:14:52 +0100950 dir->parent_inode = get_parent_no(dir_ent->our_dir);
ploughere6e0e1b2010-05-12 17:17:06 +0000951 dir->xattr = xattr;
plougher1f413c82005-11-18 00:02:14 +0000952
plougherac28cd12010-02-24 02:25:03 +0000953 SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode);
plougher2611d392010-12-31 20:50:36 +0000954 p = inode + offsetof(struct squashfs_ldir_inode_header, index);
plougher1f413c82005-11-18 00:02:14 +0000955 for(i = 0; i < i_count; i++) {
plougherac28cd12010-02-24 02:25:03 +0000956 SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p);
plougher2bd2b722010-12-31 10:52:15 +0000957 p += offsetof(struct squashfs_dir_index, name);
plougherac28cd12010-02-24 02:25:03 +0000958 memcpy(p, index[i].name, index[i].index.size + 1);
959 p += index[i].index.size + 1;
plougher1f413c82005-11-18 00:02:14 +0000960 }
plougher50b31762009-03-31 04:14:46 +0000961 TRACE("Long directory inode, file_size %lld, start_block "
962 "0x%llx, offset 0x%x, nlink %d\n", byte_size,
963 start_block, offset, dir_ent->dir->directory_count + 2);
plougher1f413c82005-11-18 00:02:14 +0000964 }
965 else if(type == SQUASHFS_DIR_TYPE) {
plougher9d80a602010-12-31 20:47:24 +0000966 struct squashfs_dir_inode_header *dir = &inode_header.dir;
plougher1f413c82005-11-18 00:02:14 +0000967
968 inode = get_inode(sizeof(*dir));
plougher1f413c82005-11-18 00:02:14 +0000969 dir->nlink = dir_ent->dir->directory_count + 2;
970 dir->file_size = byte_size;
971 dir->offset = offset;
972 dir->start_block = start_block;
Phillip Lougher539c2b12012-07-30 20:14:52 +0100973 dir->parent_inode = get_parent_no(dir_ent->our_dir);
plougherac28cd12010-02-24 02:25:03 +0000974 SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode);
plougherfd57dfe2009-03-30 01:17:52 +0000975 TRACE("Directory inode, file_size %lld, start_block 0x%llx, "
plougher50b31762009-03-31 04:14:46 +0000976 "offset 0x%x, nlink %d\n", byte_size, start_block,
977 offset, dir_ent->dir->directory_count + 2);
plougher1f413c82005-11-18 00:02:14 +0000978 }
979 else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) {
plougherc70c6332010-12-31 20:11:09 +0000980 struct squashfs_dev_inode_header *dev = &inode_header.dev;
plougher5b398502008-10-04 23:22:03 +0000981 unsigned int major = major(buf->st_rdev);
982 unsigned int minor = minor(buf->st_rdev);
plougher1f413c82005-11-18 00:02:14 +0000983
plougher5b398502008-10-04 23:22:03 +0000984 if(major > 0xfff) {
plougherfd57dfe2009-03-30 01:17:52 +0000985 ERROR("Major %d out of range in device node %s, "
986 "truncating to %d\n", major, filename,
987 major & 0xfff);
plougher5b398502008-10-04 23:22:03 +0000988 major &= 0xfff;
989 }
990 if(minor > 0xfffff) {
plougherfd57dfe2009-03-30 01:17:52 +0000991 ERROR("Minor %d out of range in device node %s, "
992 "truncating to %d\n", minor, filename,
993 minor & 0xfffff);
plougher5b398502008-10-04 23:22:03 +0000994 minor &= 0xfffff;
995 }
plougher1f413c82005-11-18 00:02:14 +0000996 inode = get_inode(sizeof(*dev));
997 dev->nlink = nlink;
plougher5b398502008-10-04 23:22:03 +0000998 dev->rdev = (major << 8) | (minor & 0xff) |
999 ((minor & ~0xff) << 12);
plougherac28cd12010-02-24 02:25:03 +00001000 SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode);
rlougher8f7d0b82007-11-08 15:33:29 +00001001 TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink);
plougher1f413c82005-11-18 00:02:14 +00001002 }
ploughere6e0e1b2010-05-12 17:17:06 +00001003 else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) {
plougher0b4ee5b2010-12-31 20:14:00 +00001004 struct squashfs_ldev_inode_header *dev = &inode_header.ldev;
ploughere6e0e1b2010-05-12 17:17:06 +00001005 unsigned int major = major(buf->st_rdev);
1006 unsigned int minor = minor(buf->st_rdev);
1007
1008 if(major > 0xfff) {
1009 ERROR("Major %d out of range in device node %s, "
1010 "truncating to %d\n", major, filename,
1011 major & 0xfff);
1012 major &= 0xfff;
1013 }
1014 if(minor > 0xfffff) {
1015 ERROR("Minor %d out of range in device node %s, "
1016 "truncating to %d\n", minor, filename,
1017 minor & 0xfffff);
1018 minor &= 0xfffff;
1019 }
1020 inode = get_inode(sizeof(*dev));
1021 dev->nlink = nlink;
1022 dev->rdev = (major << 8) | (minor & 0xff) |
1023 ((minor & ~0xff) << 12);
1024 dev->xattr = xattr;
1025 SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode);
1026 TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink);
1027 }
plougher1f413c82005-11-18 00:02:14 +00001028 else if(type == SQUASHFS_SYMLINK_TYPE) {
plougher5ae6e952010-12-31 20:44:34 +00001029 struct squashfs_symlink_inode_header *symlink = &inode_header.symlink;
plougher1f413c82005-11-18 00:02:14 +00001030 int byte;
Phillip Lougher1df6c112012-12-12 05:21:42 +00001031 char buff[65536]; /* overflow safe */
plougher5ae6e952010-12-31 20:44:34 +00001032 size_t off = offsetof(struct squashfs_symlink_inode_header, symlink);
plougher1f413c82005-11-18 00:02:14 +00001033
plougher5cf38b82010-12-16 04:55:32 +00001034 byte = readlink(filename, buff, 65536);
1035 if(byte == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00001036 ERROR_START("Failed to read symlink %s", filename);
1037 ERROR_EXIT(", creating empty symlink\n");
plougher29e37092007-04-15 01:24:51 +00001038 byte = 0;
plougher1f413c82005-11-18 00:02:14 +00001039 }
1040
1041 if(byte == 65536) {
Phillip Lougher85776df2014-01-31 03:10:46 +00001042 ERROR_START("Symlink %s is greater than 65536 bytes!",
1043 filename);
1044 ERROR_EXIT(" Creating empty symlink\n");
plougher29e37092007-04-15 01:24:51 +00001045 byte = 0;
plougher1f413c82005-11-18 00:02:14 +00001046 }
1047
1048 inode = get_inode(sizeof(*symlink) + byte);
1049 symlink->nlink = nlink;
plougher1f413c82005-11-18 00:02:14 +00001050 symlink->symlink_size = byte;
plougherac28cd12010-02-24 02:25:03 +00001051 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
1052 strncpy(inode + off, buff, byte);
plougherfd57dfe2009-03-30 01:17:52 +00001053 TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte,
1054 nlink);
plougher1f413c82005-11-18 00:02:14 +00001055 }
ploughere6e0e1b2010-05-12 17:17:06 +00001056 else if(type == SQUASHFS_LSYMLINK_TYPE) {
plougher5ae6e952010-12-31 20:44:34 +00001057 struct squashfs_symlink_inode_header *symlink = &inode_header.symlink;
ploughere6e0e1b2010-05-12 17:17:06 +00001058 int byte;
Phillip Lougher1df6c112012-12-12 05:21:42 +00001059 char buff[65536]; /* overflow safe */
plougher5ae6e952010-12-31 20:44:34 +00001060 size_t off = offsetof(struct squashfs_symlink_inode_header, symlink);
ploughere6e0e1b2010-05-12 17:17:06 +00001061
plougherdf2b9aa2010-12-16 04:56:23 +00001062 byte = readlink(filename, buff, 65536);
1063 if(byte == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00001064 ERROR_START("Failed to read symlink %s", filename);
1065 ERROR_EXIT(", creating empty symlink\n");
ploughere6e0e1b2010-05-12 17:17:06 +00001066 byte = 0;
1067 }
1068
1069 if(byte == 65536) {
Phillip Lougher85776df2014-01-31 03:10:46 +00001070 ERROR_START("Symlink %s is greater than 65536 bytes!",
1071 filename);
1072 ERROR_EXIT(" Creating empty symlink\n");
ploughere6e0e1b2010-05-12 17:17:06 +00001073 byte = 0;
1074 }
1075
1076 inode = get_inode(sizeof(*symlink) + byte +
1077 sizeof(unsigned int));
1078 symlink->nlink = nlink;
1079 symlink->symlink_size = byte;
1080 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
1081 strncpy(inode + off, buff, byte);
1082 SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1);
1083 TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte,
1084 nlink);
1085 }
plougher1f413c82005-11-18 00:02:14 +00001086 else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) {
ploughere56b9862010-12-31 10:58:35 +00001087 struct squashfs_ipc_inode_header *ipc = &inode_header.ipc;
plougher1f413c82005-11-18 00:02:14 +00001088
1089 inode = get_inode(sizeof(*ipc));
1090 ipc->nlink = nlink;
plougherac28cd12010-02-24 02:25:03 +00001091 SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode);
plougher50b31762009-03-31 04:14:46 +00001092 TRACE("ipc inode, type %s, nlink %d\n", type ==
1093 SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink);
plougherc5d69322010-05-12 19:28:38 +00001094 }
1095 else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) {
plougheraa0d1222010-12-31 20:06:24 +00001096 struct squashfs_lipc_inode_header *ipc = &inode_header.lipc;
plougherc5d69322010-05-12 19:28:38 +00001097
1098 inode = get_inode(sizeof(*ipc));
1099 ipc->nlink = nlink;
1100 ipc->xattr = xattr;
1101 SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode);
1102 TRACE("ipc inode, type %s, nlink %d\n", type ==
1103 SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink);
plougher1f413c82005-11-18 00:02:14 +00001104 } else
rlougher8f7d0b82007-11-08 15:33:29 +00001105 BAD_ERROR("Unrecognised inode %d in create_inode\n", type);
plougher1f413c82005-11-18 00:02:14 +00001106
1107 *i_no = MKINODE(inode);
1108 inode_count ++;
1109
plougherfd57dfe2009-03-30 01:17:52 +00001110 TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type,
1111 base->uid, base->guid);
plougher1f413c82005-11-18 00:02:14 +00001112
1113 return TRUE;
1114}
1115
1116
plougher50b31762009-03-31 04:14:46 +00001117void add_dir(squashfs_inode inode, unsigned int inode_number, char *name,
1118 int type, struct directory *dir)
plougher1f413c82005-11-18 00:02:14 +00001119{
plougher8cb05cd2005-12-11 23:32:35 +00001120 unsigned char *buff;
plougher9b393552010-12-31 20:55:43 +00001121 struct squashfs_dir_entry idir;
plougher1f413c82005-11-18 00:02:14 +00001122 unsigned int start_block = inode >> 16;
1123 unsigned int offset = inode & 0xffff;
plougher43d49082010-12-16 05:01:15 +00001124 unsigned int size = strlen(name);
plougher9b393552010-12-31 20:55:43 +00001125 size_t name_off = offsetof(struct squashfs_dir_entry, name);
plougher1f413c82005-11-18 00:02:14 +00001126
plougher43d49082010-12-16 05:01:15 +00001127 if(size > SQUASHFS_NAME_LEN) {
plougher1f413c82005-11-18 00:02:14 +00001128 size = SQUASHFS_NAME_LEN;
plougher50b31762009-03-31 04:14:46 +00001129 ERROR("Filename is greater than %d characters, truncating! ..."
1130 "\n", SQUASHFS_NAME_LEN);
plougher1f413c82005-11-18 00:02:14 +00001131 }
1132
plougher9b393552010-12-31 20:55:43 +00001133 if(dir->p + sizeof(struct squashfs_dir_entry) + size +
plougher520e1a12010-12-31 10:44:38 +00001134 sizeof(struct squashfs_dir_header)
1135 >= dir->buff + dir->size) {
plougherfd57dfe2009-03-30 01:17:52 +00001136 buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001137 if(buff == NULL)
1138 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001139
1140 dir->p = (dir->p - dir->buff) + buff;
1141 if(dir->entry_count_p)
plougherfd57dfe2009-03-30 01:17:52 +00001142 dir->entry_count_p = (dir->entry_count_p - dir->buff +
1143 buff);
plougher1f413c82005-11-18 00:02:14 +00001144 dir->index_count_p = dir->index_count_p - dir->buff + buff;
1145 dir->buff = buff;
1146 }
1147
plougherfd57dfe2009-03-30 01:17:52 +00001148 if(dir->entry_count == 256 || start_block != dir->start_block ||
1149 ((dir->entry_count_p != NULL) &&
plougher9b393552010-12-31 20:55:43 +00001150 ((dir->p + sizeof(struct squashfs_dir_entry) + size -
plougherfd57dfe2009-03-30 01:17:52 +00001151 dir->index_count_p) > SQUASHFS_METADATA_SIZE)) ||
plougher50b31762009-03-31 04:14:46 +00001152 ((long long) inode_number - dir->inode_number) > 32767
1153 || ((long long) inode_number - dir->inode_number)
1154 < -32768) {
plougher1f413c82005-11-18 00:02:14 +00001155 if(dir->entry_count_p) {
plougher520e1a12010-12-31 10:44:38 +00001156 struct squashfs_dir_header dir_header;
plougher1f413c82005-11-18 00:02:14 +00001157
plougher9b393552010-12-31 20:55:43 +00001158 if((dir->p + sizeof(struct squashfs_dir_entry) + size -
plougherfd57dfe2009-03-30 01:17:52 +00001159 dir->index_count_p) >
1160 SQUASHFS_METADATA_SIZE) {
1161 if(dir->i_count % I_COUNT_SIZE == 0) {
1162 dir->index = realloc(dir->index,
1163 (dir->i_count + I_COUNT_SIZE) *
1164 sizeof(struct cached_dir_index));
1165 if(dir->index == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001166 MEM_ERROR();
plougherfd57dfe2009-03-30 01:17:52 +00001167 }
1168 dir->index[dir->i_count].index.index =
1169 dir->p - dir->buff;
plougher1f413c82005-11-18 00:02:14 +00001170 dir->index[dir->i_count].index.size = size - 1;
1171 dir->index[dir->i_count++].name = name;
plougher2bd2b722010-12-31 10:52:15 +00001172 dir->i_size += sizeof(struct squashfs_dir_index)
1173 + size;
plougher1f413c82005-11-18 00:02:14 +00001174 dir->index_count_p = dir->p;
1175 }
1176
1177 dir_header.count = dir->entry_count - 1;
1178 dir_header.start_block = dir->start_block;
1179 dir_header.inode_number = dir->inode_number;
plougherfd57dfe2009-03-30 01:17:52 +00001180 SQUASHFS_SWAP_DIR_HEADER(&dir_header,
plougherac28cd12010-02-24 02:25:03 +00001181 dir->entry_count_p);
plougher1f413c82005-11-18 00:02:14 +00001182
1183 }
1184
1185
1186 dir->entry_count_p = dir->p;
1187 dir->start_block = start_block;
1188 dir->entry_count = 0;
1189 dir->inode_number = inode_number;
plougher520e1a12010-12-31 10:44:38 +00001190 dir->p += sizeof(struct squashfs_dir_header);
plougher1f413c82005-11-18 00:02:14 +00001191 }
1192
plougher1f413c82005-11-18 00:02:14 +00001193 idir.offset = offset;
1194 idir.type = type;
1195 idir.size = size - 1;
1196 idir.inode_number = ((long long) inode_number - dir->inode_number);
plougherac28cd12010-02-24 02:25:03 +00001197 SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p);
1198 strncpy((char *) dir->p + name_off, name, size);
plougher9b393552010-12-31 20:55:43 +00001199 dir->p += sizeof(struct squashfs_dir_entry) + size;
plougher1f413c82005-11-18 00:02:14 +00001200 dir->entry_count ++;
1201}
1202
1203
plougherfd57dfe2009-03-30 01:17:52 +00001204void write_dir(squashfs_inode *inode, struct dir_info *dir_info,
1205 struct directory *dir)
plougher1f413c82005-11-18 00:02:14 +00001206{
1207 unsigned int dir_size = dir->p - dir->buff;
plougher4627ca32010-12-16 05:03:55 +00001208 int data_space = directory_cache_size - directory_cache_bytes;
plougher1f413c82005-11-18 00:02:14 +00001209 unsigned int directory_block, directory_offset, i_count, index;
1210 unsigned short c_byte;
1211
1212 if(data_space < dir_size) {
plougherfd57dfe2009-03-30 01:17:52 +00001213 int realloc_size = directory_cache_size == 0 ?
1214 ((dir_size + SQUASHFS_METADATA_SIZE) &
1215 ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space;
plougher1f413c82005-11-18 00:02:14 +00001216
plougher17248ca2010-07-27 00:24:35 +00001217 void *dc = realloc(directory_data_cache,
plougherfd57dfe2009-03-30 01:17:52 +00001218 directory_cache_size + realloc_size);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001219 if(dc == NULL)
1220 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001221 directory_cache_size += realloc_size;
plougher17248ca2010-07-27 00:24:35 +00001222 directory_data_cache = dc;
plougher1f413c82005-11-18 00:02:14 +00001223 }
1224
1225 if(dir_size) {
plougher520e1a12010-12-31 10:44:38 +00001226 struct squashfs_dir_header dir_header;
plougher1f413c82005-11-18 00:02:14 +00001227
1228 dir_header.count = dir->entry_count - 1;
1229 dir_header.start_block = dir->start_block;
1230 dir_header.inode_number = dir->inode_number;
plougherac28cd12010-02-24 02:25:03 +00001231 SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p);
plougherfd57dfe2009-03-30 01:17:52 +00001232 memcpy(directory_data_cache + directory_cache_bytes, dir->buff,
1233 dir_size);
plougher1f413c82005-11-18 00:02:14 +00001234 }
1235 directory_offset = directory_cache_bytes;
1236 directory_block = directory_bytes;
1237 directory_cache_bytes += dir_size;
1238 i_count = 0;
1239 index = SQUASHFS_METADATA_SIZE - directory_offset;
1240
1241 while(1) {
plougherfd57dfe2009-03-30 01:17:52 +00001242 while(i_count < dir->i_count &&
1243 dir->index[i_count].index.index < index)
plougher50b31762009-03-31 04:14:46 +00001244 dir->index[i_count++].index.start_block =
1245 directory_bytes;
plougher1f413c82005-11-18 00:02:14 +00001246 index += SQUASHFS_METADATA_SIZE;
1247
1248 if(directory_cache_bytes < SQUASHFS_METADATA_SIZE)
1249 break;
1250
plougherfd57dfe2009-03-30 01:17:52 +00001251 if((directory_size - directory_bytes) <
1252 ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
plougher79d665a2010-07-27 00:19:23 +00001253 void *dt = realloc(directory_table,
plougher50b31762009-03-31 04:14:46 +00001254 directory_size + (SQUASHFS_METADATA_SIZE << 1)
1255 + 2);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001256 if(dt == NULL)
1257 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001258 directory_size += SQUASHFS_METADATA_SIZE << 1;
plougher79d665a2010-07-27 00:19:23 +00001259 directory_table = dt;
plougher1f413c82005-11-18 00:02:14 +00001260 }
1261
plougher50b31762009-03-31 04:14:46 +00001262 c_byte = mangle(directory_table + directory_bytes +
1263 BLOCK_OFFSET, directory_data_cache,
1264 SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE,
1265 noI, 0);
plougherfd57dfe2009-03-30 01:17:52 +00001266 TRACE("Directory block @ 0x%x, size %d\n", directory_bytes,
1267 c_byte);
plougherac28cd12010-02-24 02:25:03 +00001268 SQUASHFS_SWAP_SHORTS(&c_byte,
1269 directory_table + directory_bytes, 1);
plougher50b31762009-03-31 04:14:46 +00001270 directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) +
1271 BLOCK_OFFSET;
plougher1b899fc2008-08-07 01:24:06 +00001272 total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET;
plougher14dd5f32010-08-02 18:20:03 +00001273 memmove(directory_data_cache, directory_data_cache +
plougherfd57dfe2009-03-30 01:17:52 +00001274 SQUASHFS_METADATA_SIZE, directory_cache_bytes -
1275 SQUASHFS_METADATA_SIZE);
plougher1f413c82005-11-18 00:02:14 +00001276 directory_cache_bytes -= SQUASHFS_METADATA_SIZE;
1277 }
1278
plougher3c6bdb52010-05-01 02:30:59 +00001279 create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE,
1280 dir_size + 3, directory_block, directory_offset, NULL, NULL,
1281 dir, 0);
plougher1f413c82005-11-18 00:02:14 +00001282
1283#ifdef SQUASHFS_TRACE
plougher1f288f62009-02-21 03:05:52 +00001284 {
plougher1f413c82005-11-18 00:02:14 +00001285 unsigned char *dirp;
1286 int count;
1287
1288 TRACE("Directory contents of inode 0x%llx\n", *inode);
1289 dirp = dir->buff;
1290 while(dirp < dir->p) {
1291 char buffer[SQUASHFS_NAME_LEN + 1];
plougher9b393552010-12-31 20:55:43 +00001292 struct squashfs_dir_entry idir, *idirp;
plougher520e1a12010-12-31 10:44:38 +00001293 struct squashfs_dir_header dirh;
1294 SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp,
plougher360514a2009-03-30 03:01:38 +00001295 &dirh);
plougher1f288f62009-02-21 03:05:52 +00001296 count = dirh.count + 1;
plougher520e1a12010-12-31 10:44:38 +00001297 dirp += sizeof(struct squashfs_dir_header);
plougher1f413c82005-11-18 00:02:14 +00001298
plougher50b31762009-03-31 04:14:46 +00001299 TRACE("\tStart block 0x%x, count %d\n",
1300 dirh.start_block, count);
plougher1f413c82005-11-18 00:02:14 +00001301
1302 while(count--) {
plougher9b393552010-12-31 20:55:43 +00001303 idirp = (struct squashfs_dir_entry *) dirp;
plougher1f288f62009-02-21 03:05:52 +00001304 SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir);
plougher1f413c82005-11-18 00:02:14 +00001305 strncpy(buffer, idirp->name, idir.size + 1);
1306 buffer[idir.size + 1] = '\0';
plougher50b31762009-03-31 04:14:46 +00001307 TRACE("\t\tname %s, inode offset 0x%x, type "
1308 "%d\n", buffer, idir.offset, idir.type);
plougher9b393552010-12-31 20:55:43 +00001309 dirp += sizeof(struct squashfs_dir_entry) + idir.size +
ploughere8b26f62010-12-16 05:06:00 +00001310 1;
plougher1f413c82005-11-18 00:02:14 +00001311 }
1312 }
1313 }
1314#endif
1315 dir_count ++;
plougher1f413c82005-11-18 00:02:14 +00001316}
1317
1318
plougher76c64082008-03-08 01:32:23 +00001319struct file_buffer *get_fragment(struct fragment *fragment)
plougher1f413c82005-11-18 00:02:14 +00001320{
plougher8ed84b92010-12-31 10:37:24 +00001321 struct squashfs_fragment_entry *disk_fragment;
plougher1d065e92010-06-18 03:58:27 +00001322 int res, size;
plougher76c64082008-03-08 01:32:23 +00001323 long long start_block;
1324 struct file_buffer *buffer, *compressed_buffer;
plougher5507dd92006-11-06 00:43:10 +00001325
plougher76c64082008-03-08 01:32:23 +00001326 if(fragment->index == SQUASHFS_INVALID_FRAG)
1327 return NULL;
plougher5507dd92006-11-06 00:43:10 +00001328
plougher76c64082008-03-08 01:32:23 +00001329 buffer = cache_lookup(fragment_buffer, fragment->index);
plougher1b899fc2008-08-07 01:24:06 +00001330 if(buffer)
plougher76c64082008-03-08 01:32:23 +00001331 return buffer;
plougher76c64082008-03-08 01:32:23 +00001332
Phillip Lougher45702012013-04-30 05:19:54 +01001333 compressed_buffer = cache_lookup(writer_buffer,
Phillip Lougherfd992722013-05-19 04:46:57 +01001334 FRAG_INDEX((long long) fragment->index));
plougher2ea89142008-03-11 01:34:19 +00001335
Phillip Lougher316ab632013-04-29 02:53:39 +01001336 buffer = cache_get(fragment_buffer, fragment->index);
plougher5507dd92006-11-06 00:43:10 +00001337
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001338 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001339 pthread_mutex_lock(&fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001340 disk_fragment = &fragment_table[fragment->index];
1341 size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size);
plougher76c64082008-03-08 01:32:23 +00001342 start_block = disk_fragment->start_block;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001343 pthread_cleanup_pop(1);
plougher1f413c82005-11-18 00:02:14 +00001344
plougher0f464442008-03-31 00:27:56 +00001345 if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) {
plougher1d065e92010-06-18 03:58:27 +00001346 int error;
plougher76c64082008-03-08 01:32:23 +00001347 char *data;
plougher1f413c82005-11-18 00:02:14 +00001348
plougher76c64082008-03-08 01:32:23 +00001349 if(compressed_buffer)
1350 data = compressed_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001351 else {
plougher49b57a92009-03-31 03:23:05 +00001352 data = read_from_disk(start_block, size);
Phillip Lougherac731382013-05-19 04:19:44 +01001353 if(data == NULL) {
1354 ERROR("Failed to read fragment from output"
1355 " filesystem\n");
1356 BAD_ERROR("Output filesystem corrupted?\n");
1357 }
1358 }
plougher1f413c82005-11-18 00:02:14 +00001359
plougherb48442b2010-12-31 07:57:54 +00001360 res = compressor_uncompress(comp, buffer->data, data, size,
1361 block_size, &error);
ploughera175ce22009-07-30 04:43:27 +00001362 if(res == -1)
1363 BAD_ERROR("%s uncompress failed with error code %d\n",
1364 comp->name, error);
plougher76c64082008-03-08 01:32:23 +00001365 } else if(compressed_buffer)
1366 memcpy(buffer->data, compressed_buffer->data, size);
plougher1d065e92010-06-18 03:58:27 +00001367 else {
1368 res = read_fs_bytes(fd, start_block, size, buffer->data);
Phillip Lougher94519382013-03-06 02:43:42 +00001369 if(res == 0) {
1370 ERROR("Failed to read fragment from output "
1371 "filesystem\n");
1372 BAD_ERROR("Output filesystem corrupted?\n");
1373 }
plougher1d065e92010-06-18 03:58:27 +00001374 }
plougher1f413c82005-11-18 00:02:14 +00001375
plougher03859fa2009-03-31 03:18:18 +00001376 cache_block_put(compressed_buffer);
1377
plougher76c64082008-03-08 01:32:23 +00001378 return buffer;
plougher1f413c82005-11-18 00:02:14 +00001379}
1380
plougher2ea89142008-03-11 01:34:19 +00001381
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001382void get_fragment_checksum(struct file_info *file)
1383{
1384 struct file_buffer *frag_buffer = get_fragment(file->fragment);
1385 struct append_file *append;
1386 int index = file->fragment->index;
1387 int offset = file->fragment->offset;
1388 int size = file->fragment->size;
1389
1390 if(frag_buffer == NULL) {
1391 file->have_frag_checksum = TRUE;
1392 return;
1393 }
1394
1395 if(index >= append_fragments) {
1396 /*
1397 * for fragment blocks generated by this mksquashfs run
1398 * just compute the checksum for this fragment.
1399 * Note this applies to file tail-end fragments only (if
1400 * the option -always-use-fragments specified). Fragments
1401 * for files smaller than the block size, always get their
1402 * checksums computed at generation time
1403 */
1404 file->fragment_checksum = get_checksum_mem(frag_buffer->data +
1405 offset, size);
1406 cache_block_put(frag_buffer);
1407 file->have_frag_checksum = TRUE;
1408 return;
1409 }
1410
1411 for(append = file_mapping[index]; append; append = append->next) {
1412 int offset = append->file->fragment->offset;
1413 int size = append->file->fragment->size;
1414
1415 append->file->fragment_checksum =
1416 get_checksum_mem(frag_buffer->data + offset, size);
1417 append->file->have_frag_checksum = TRUE;
1418 }
1419
1420 cache_block_put(frag_buffer);
1421}
1422
1423
plougher4e8484a2008-03-15 23:30:16 +00001424int lock_fragments()
plougher5507dd92006-11-06 00:43:10 +00001425{
plougher4e8484a2008-03-15 23:30:16 +00001426 int count;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001427 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001428 pthread_mutex_lock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00001429 fragments_locked = TRUE;
plougher4e8484a2008-03-15 23:30:16 +00001430 count = fragments_outstanding;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001431 pthread_cleanup_pop(1);
plougher4e8484a2008-03-15 23:30:16 +00001432 return count;
plougher2ea89142008-03-11 01:34:19 +00001433}
1434
1435
1436void unlock_fragments()
1437{
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001438 int frg, size;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001439 struct file_buffer *write_buffer;
plougher2ea89142008-03-11 01:34:19 +00001440
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001441 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00001442 pthread_mutex_lock(&fragment_mutex);
Phillip Lougher71ad9642013-05-25 05:03:48 +01001443
1444 /*
1445 * Note queue_empty() is inherently racy with respect to concurrent
1446 * queue get and pushes. We avoid this because we're holding the
1447 * fragment_mutex which ensures no other threads can be using the
1448 * queue at this time.
1449 */
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001450 while(!queue_empty(locked_fragment)) {
1451 write_buffer = queue_get(locked_fragment);
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001452 frg = write_buffer->block;
1453 size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size);
1454 fragment_table[frg].start_block = bytes;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001455 write_buffer->block = bytes;
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001456 bytes += size;
plougher2ea89142008-03-11 01:34:19 +00001457 fragments_outstanding --;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001458 queue_put(to_writer, write_buffer);
plougher50b31762009-03-31 04:14:46 +00001459 TRACE("fragment_locked writing fragment %d, compressed size %d"
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001460 "\n", frg, size);
plougher2ea89142008-03-11 01:34:19 +00001461 }
1462 fragments_locked = FALSE;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001463 pthread_cleanup_pop(1);
plougher2ea89142008-03-11 01:34:19 +00001464}
1465
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001466/* Called with the fragment_mutex locked */
plougherb7a66812010-07-21 01:06:59 +00001467void add_pending_fragment(struct file_buffer *write_buffer, int c_byte,
plougher17b269c2009-03-30 01:33:07 +00001468 int fragment)
plougher2ea89142008-03-11 01:34:19 +00001469{
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001470 fragment_table[fragment].size = c_byte;
1471 write_buffer->block = fragment;
1472
1473 queue_put(locked_fragment, write_buffer);
plougher5507dd92006-11-06 00:43:10 +00001474}
1475
1476
Phillip Lougher04b7b532011-06-11 02:14:15 +01001477void write_fragment(struct file_buffer *fragment)
plougher1f413c82005-11-18 00:02:14 +00001478{
Phillip Lougher04b7b532011-06-11 02:14:15 +01001479 if(fragment == NULL)
plougher1f413c82005-11-18 00:02:14 +00001480 return;
1481
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001482 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001483 pthread_mutex_lock(&fragment_mutex);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001484 fragment_table[fragment->block].unused = 0;
1485 fragments_outstanding ++;
1486 queue_put(to_frag, fragment);
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001487 pthread_cleanup_pop(1);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001488}
1489
1490
1491struct file_buffer *allocate_fragment()
1492{
Phillip Lougher316ab632013-04-29 02:53:39 +01001493 struct file_buffer *fragment = cache_get(fragment_buffer, fragments);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001494
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001495 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001496 pthread_mutex_lock(&fragment_mutex);
1497
plougher5507dd92006-11-06 00:43:10 +00001498 if(fragments % FRAG_SIZE == 0) {
plougherfa89c332010-07-27 00:27:15 +00001499 void *ft = realloc(fragment_table, (fragments +
plougher8ed84b92010-12-31 10:37:24 +00001500 FRAG_SIZE) * sizeof(struct squashfs_fragment_entry));
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01001501 if(ft == NULL)
1502 MEM_ERROR();
plougherfa89c332010-07-27 00:27:15 +00001503 fragment_table = ft;
plougher5507dd92006-11-06 00:43:10 +00001504 }
Phillip Lougherb9508be2011-06-13 02:25:51 +01001505
1506 fragment->size = 0;
1507 fragment->block = fragments ++;
1508
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001509 pthread_cleanup_pop(1);
1510
Phillip Lougherb9508be2011-06-13 02:25:51 +01001511 return fragment;
plougher5507dd92006-11-06 00:43:10 +00001512}
1513
ploughereb6eac92008-02-26 01:50:48 +00001514
plougher1f413c82005-11-18 00:02:14 +00001515static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0};
Phillip Lougherd2f045f2012-12-28 03:15:45 +00001516
1517
1518void free_fragment(struct fragment *fragment)
1519{
1520 if(fragment != &empty_fragment)
1521 free(fragment);
1522}
1523
1524
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001525struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer,
1526 struct dir_ent *dir_ent)
plougher1f413c82005-11-18 00:02:14 +00001527{
1528 struct fragment *ffrg;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001529 struct file_buffer **fragment;
plougher1f413c82005-11-18 00:02:14 +00001530
plougher5507dd92006-11-06 00:43:10 +00001531 if(file_buffer == NULL || file_buffer->size == 0)
plougher1f413c82005-11-18 00:02:14 +00001532 return &empty_fragment;
1533
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001534 fragment = eval_frag_actions(dir_ent);
1535
1536 if((*fragment) && (*fragment)->size + file_buffer->size > block_size) {
1537 write_fragment(*fragment);
1538 *fragment = NULL;
Phillip Lougher04b7b532011-06-11 02:14:15 +01001539 }
plougher1f413c82005-11-18 00:02:14 +00001540
ploughere7e6e832010-12-16 05:08:06 +00001541 ffrg = malloc(sizeof(struct fragment));
1542 if(ffrg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001543 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001544
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001545 if(*fragment == NULL)
1546 *fragment = allocate_fragment();
plougher5507dd92006-11-06 00:43:10 +00001547
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001548 ffrg->index = (*fragment)->block;
1549 ffrg->offset = (*fragment)->size;
plougher5507dd92006-11-06 00:43:10 +00001550 ffrg->size = file_buffer->size;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001551 memcpy((*fragment)->data + (*fragment)->size, file_buffer->data,
plougher17b269c2009-03-30 01:33:07 +00001552 file_buffer->size);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001553 (*fragment)->size += file_buffer->size;
plougher1f413c82005-11-18 00:02:14 +00001554
1555 return ffrg;
1556}
1557
1558
ploughera0a49c32010-08-11 01:47:59 +00001559long long generic_write_table(int length, void *buffer, int length2,
1560 void *buffer2, int uncompressed)
plougher1f413c82005-11-18 00:02:14 +00001561{
plougher17b269c2009-03-30 01:33:07 +00001562 int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) /
1563 SQUASHFS_METADATA_SIZE;
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001564 long long *list, start_bytes;
Phillip Lougherc1305322012-11-02 22:28:02 +00001565 int compressed_size, i, list_size = meta_blocks * sizeof(long long);
plougher1f413c82005-11-18 00:02:14 +00001566 unsigned short c_byte;
plougher0e453652006-11-06 01:49:35 +00001567 char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2];
1568
plougher82ab2332009-04-21 00:21:21 +00001569#ifdef SQUASHFS_TRACE
plougher0e453652006-11-06 01:49:35 +00001570 long long obytes = bytes;
plougher40d8b952010-02-12 16:26:32 +00001571 int olength = length;
plougher82ab2332009-04-21 00:21:21 +00001572#endif
plougher1f413c82005-11-18 00:02:14 +00001573
Phillip Lougherc1305322012-11-02 22:28:02 +00001574 list = malloc(list_size);
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001575 if(list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001576 MEM_ERROR();
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001577
plougher1f413c82005-11-18 00:02:14 +00001578 for(i = 0; i < meta_blocks; i++) {
plougher17b269c2009-03-30 01:33:07 +00001579 int avail_bytes = length > SQUASHFS_METADATA_SIZE ?
1580 SQUASHFS_METADATA_SIZE : length;
1581 c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i *
1582 SQUASHFS_METADATA_SIZE , avail_bytes,
1583 SQUASHFS_METADATA_SIZE, uncompressed, 0);
plougherac28cd12010-02-24 02:25:03 +00001584 SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1);
plougher1f413c82005-11-18 00:02:14 +00001585 list[i] = bytes;
plougher50b31762009-03-31 04:14:46 +00001586 compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) +
1587 BLOCK_OFFSET;
plougher17b269c2009-03-30 01:33:07 +00001588 TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes,
1589 compressed_size);
plougher0dd6f122009-03-29 21:43:57 +00001590 write_destination(fd, bytes, compressed_size, cbuffer);
plougher1f413c82005-11-18 00:02:14 +00001591 bytes += compressed_size;
plougher10f7d572010-07-20 02:14:04 +00001592 total_bytes += avail_bytes;
plougher0e453652006-11-06 01:49:35 +00001593 length -= avail_bytes;
plougher1f413c82005-11-18 00:02:14 +00001594 }
1595
ploughere6e0e1b2010-05-12 17:17:06 +00001596 start_bytes = bytes;
1597 if(length2) {
ploughera0a49c32010-08-11 01:47:59 +00001598 write_destination(fd, bytes, length2, buffer2);
ploughere6e0e1b2010-05-12 17:17:06 +00001599 bytes += length2;
plougher10f7d572010-07-20 02:14:04 +00001600 total_bytes += length2;
ploughere6e0e1b2010-05-12 17:17:06 +00001601 }
1602
plougher1f288f62009-02-21 03:05:52 +00001603 SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks);
Phillip Lougherc1305322012-11-02 22:28:02 +00001604 write_destination(fd, bytes, list_size, list);
1605 bytes += list_size;
1606 total_bytes += list_size;
plougher1f413c82005-11-18 00:02:14 +00001607
plougher40d8b952010-02-12 16:26:32 +00001608 TRACE("generic_write_table: total uncompressed %d compressed %lld\n",
1609 olength, bytes - obytes);
plougher0e453652006-11-06 01:49:35 +00001610
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001611 free(list);
1612
plougher1f413c82005-11-18 00:02:14 +00001613 return start_bytes;
1614}
1615
1616
plougher0e453652006-11-06 01:49:35 +00001617long long write_fragment_table()
1618{
1619 unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments);
plougher0e453652006-11-06 01:49:35 +00001620 int i;
1621
plougher17b269c2009-03-30 01:33:07 +00001622 TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments,
1623 frag_bytes);
plougherac28cd12010-02-24 02:25:03 +00001624 for(i = 0; i < fragments; i++) {
plougher50b31762009-03-31 04:14:46 +00001625 TRACE("write_fragment_table: fragment %d, start_block 0x%llx, "
1626 "size %d\n", i, fragment_table[i].start_block,
plougher17b269c2009-03-30 01:33:07 +00001627 fragment_table[i].size);
Phillip Lougher162c24c2012-10-30 02:54:16 +00001628 SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]);
plougher0e453652006-11-06 01:49:35 +00001629 }
1630
Phillip Lougher162c24c2012-10-30 02:54:16 +00001631 return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF);
plougher0e453652006-11-06 01:49:35 +00001632}
1633
1634
plougher1f413c82005-11-18 00:02:14 +00001635char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE];
plougher5507dd92006-11-06 00:43:10 +00001636char *read_from_disk(long long start, unsigned int avail_bytes)
plougher1f413c82005-11-18 00:02:14 +00001637{
plougher1d065e92010-06-18 03:58:27 +00001638 int res;
1639
1640 res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer);
Phillip Lougherac731382013-05-19 04:19:44 +01001641 if(res == 0)
1642 return NULL;
plougher1d065e92010-06-18 03:58:27 +00001643
plougher1f413c82005-11-18 00:02:14 +00001644 return read_from_file_buffer;
1645}
1646
1647
plougher1b899fc2008-08-07 01:24:06 +00001648char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE];
1649char *read_from_disk2(long long start, unsigned int avail_bytes)
1650{
plougher1d065e92010-06-18 03:58:27 +00001651 int res;
1652
1653 res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2);
Phillip Lougherac731382013-05-19 04:19:44 +01001654 if(res == 0)
1655 return NULL;
plougher1d065e92010-06-18 03:58:27 +00001656
plougher1b899fc2008-08-07 01:24:06 +00001657 return read_from_file_buffer2;
1658}
1659
1660
plougher1f413c82005-11-18 00:02:14 +00001661/*
1662 * Compute 16 bit BSD checksum over the data
1663 */
plougher5507dd92006-11-06 00:43:10 +00001664unsigned short get_checksum(char *buff, int bytes, unsigned short chksum)
plougher1f413c82005-11-18 00:02:14 +00001665{
plougher5507dd92006-11-06 00:43:10 +00001666 unsigned char *b = (unsigned char *) buff;
plougher1f413c82005-11-18 00:02:14 +00001667
plougher5507dd92006-11-06 00:43:10 +00001668 while(bytes --) {
1669 chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1;
1670 chksum += *b++;
plougher1f413c82005-11-18 00:02:14 +00001671 }
1672
1673 return chksum;
1674}
1675
1676
plougher17b269c2009-03-30 01:33:07 +00001677unsigned short get_checksum_disk(long long start, long long l,
1678 unsigned int *blocks)
plougher5507dd92006-11-06 00:43:10 +00001679{
1680 unsigned short chksum = 0;
1681 unsigned int bytes;
plougher57e6d182008-05-06 23:51:29 +00001682 struct file_buffer *write_buffer;
1683 int i;
plougher5507dd92006-11-06 00:43:10 +00001684
plougher57e6d182008-05-06 23:51:29 +00001685 for(i = 0; l; i++) {
1686 bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]);
1687 if(bytes == 0) /* sparse block */
1688 continue;
1689 write_buffer = cache_lookup(writer_buffer, start);
1690 if(write_buffer) {
plougher50b31762009-03-31 04:14:46 +00001691 chksum = get_checksum(write_buffer->data, bytes,
1692 chksum);
plougher57e6d182008-05-06 23:51:29 +00001693 cache_block_put(write_buffer);
Phillip Lougherac731382013-05-19 04:19:44 +01001694 } else {
1695 void *data = read_from_disk(start, bytes);
1696 if(data == NULL) {
1697 ERROR("Failed to checksum data from output"
1698 " filesystem\n");
1699 BAD_ERROR("Output filesystem corrupted?\n");
1700 }
1701
1702 chksum = get_checksum(data, bytes, chksum);
1703 }
1704
plougher5507dd92006-11-06 00:43:10 +00001705 l -= bytes;
plougher5507dd92006-11-06 00:43:10 +00001706 start += bytes;
1707 }
1708
1709 return chksum;
1710}
1711
1712
plougher5507dd92006-11-06 00:43:10 +00001713unsigned short get_checksum_mem(char *buff, int bytes)
1714{
1715 return get_checksum(buff, bytes, 0);
1716}
1717
1718
1719unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer)
1720{
1721 if(file_buffer == NULL)
1722 return 0;
1723 else
1724 return get_checksum(file_buffer->data, file_buffer->size, 0);
1725}
1726
1727
plougher5507dd92006-11-06 00:43:10 +00001728#define DUP_HASH(a) (a & 0xffff)
plougher17b269c2009-03-30 01:33:07 +00001729void add_file(long long start, long long file_size, long long file_bytes,
plougher50b31762009-03-31 04:14:46 +00001730 unsigned int *block_listp, int blocks, unsigned int fragment,
1731 int offset, int bytes)
plougher1f413c82005-11-18 00:02:14 +00001732{
1733 struct fragment *frg;
plougher058eae42006-01-30 14:27:44 +00001734 unsigned int *block_list = block_listp;
plougher5507dd92006-11-06 00:43:10 +00001735 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001736 struct append_file *append_file;
1737 struct file_info *file;
plougher058eae42006-01-30 14:27:44 +00001738
plougher5507dd92006-11-06 00:43:10 +00001739 if(!duplicate_checking || file_size == 0)
plougher1f413c82005-11-18 00:02:14 +00001740 return;
1741
plougher5507dd92006-11-06 00:43:10 +00001742 for(; dupl_ptr; dupl_ptr = dupl_ptr->next) {
1743 if(file_size != dupl_ptr->file_size)
1744 continue;
1745 if(blocks != 0 && start != dupl_ptr->start)
1746 continue;
1747 if(fragment != dupl_ptr->fragment->index)
1748 continue;
plougher17b269c2009-03-30 01:33:07 +00001749 if(fragment != SQUASHFS_INVALID_FRAG && (offset !=
1750 dupl_ptr->fragment->offset || bytes !=
1751 dupl_ptr->fragment->size))
plougher5507dd92006-11-06 00:43:10 +00001752 continue;
1753 return;
1754 }
1755
plougher3bb279c2010-12-16 05:10:03 +00001756 frg = malloc(sizeof(struct fragment));
1757 if(frg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001758 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001759
1760 frg->index = fragment;
1761 frg->offset = offset;
1762 frg->size = bytes;
plougher1f413c82005-11-18 00:02:14 +00001763
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001764 file = add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0,
1765 FALSE);
1766
1767 if(fragment == SQUASHFS_INVALID_FRAG)
1768 return;
1769
1770 append_file = malloc(sizeof(struct append_file));
1771 if(append_file == NULL)
1772 MEM_ERROR();
1773
1774 append_file->file = file;
1775 append_file->next = file_mapping[fragment];
1776 file_mapping[fragment] = append_file;
plougher5507dd92006-11-06 00:43:10 +00001777}
plougher1f413c82005-11-18 00:02:14 +00001778
plougher1f413c82005-11-18 00:02:14 +00001779
plougher5507dd92006-11-06 00:43:10 +00001780int pre_duplicate(long long file_size)
plougher1f413c82005-11-18 00:02:14 +00001781{
plougher5507dd92006-11-06 00:43:10 +00001782 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
plougher1f413c82005-11-18 00:02:14 +00001783
1784 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher5507dd92006-11-06 00:43:10 +00001785 if(dupl_ptr->file_size == file_size)
1786 return TRUE;
plougher1f413c82005-11-18 00:02:14 +00001787
plougher5507dd92006-11-06 00:43:10 +00001788 return FALSE;
1789}
1790
1791
1792int pre_duplicate_frag(long long file_size, unsigned short checksum)
1793{
1794 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
1795
1796 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher17b269c2009-03-30 01:33:07 +00001797 if(file_size == dupl_ptr->file_size && file_size ==
1798 dupl_ptr->fragment->size) {
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001799 if(!dupl_ptr->have_frag_checksum)
1800 get_fragment_checksum(dupl_ptr);
plougher5507dd92006-11-06 00:43:10 +00001801 if(dupl_ptr->fragment_checksum == checksum)
1802 return TRUE;
1803 }
plougher1f413c82005-11-18 00:02:14 +00001804
plougher5507dd92006-11-06 00:43:10 +00001805 return FALSE;
1806}
1807
1808
plougher17b269c2009-03-30 01:33:07 +00001809struct file_info *add_non_dup(long long file_size, long long bytes,
1810 unsigned int *block_list, long long start, struct fragment *fragment,
1811 unsigned short checksum, unsigned short fragment_checksum,
1812 int checksum_flag)
plougher5507dd92006-11-06 00:43:10 +00001813{
plougher51ef9ae2010-12-16 05:14:28 +00001814 struct file_info *dupl_ptr = malloc(sizeof(struct file_info));
plougher5507dd92006-11-06 00:43:10 +00001815
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001816 if(dupl_ptr == NULL)
1817 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00001818
1819 dupl_ptr->file_size = file_size;
1820 dupl_ptr->bytes = bytes;
1821 dupl_ptr->block_list = block_list;
1822 dupl_ptr->start = start;
1823 dupl_ptr->fragment = fragment;
1824 dupl_ptr->checksum = checksum;
1825 dupl_ptr->fragment_checksum = fragment_checksum;
Phillip Lougher557cefa2014-02-05 04:36:05 +00001826 dupl_ptr->have_frag_checksum = checksum_flag;
1827 dupl_ptr->have_checksum = checksum_flag;
plougher5507dd92006-11-06 00:43:10 +00001828 dupl_ptr->next = dupl[DUP_HASH(file_size)];
1829 dupl[DUP_HASH(file_size)] = dupl_ptr;
1830 dup_files ++;
1831
1832 return dupl_ptr;
1833}
1834
1835
plougher17b269c2009-03-30 01:33:07 +00001836struct file_info *duplicate(long long file_size, long long bytes,
1837 unsigned int **block_list, long long *start, struct fragment **fragment,
1838 struct file_buffer *file_buffer, int blocks, unsigned short checksum,
1839 unsigned short fragment_checksum, int checksum_flag)
plougher5507dd92006-11-06 00:43:10 +00001840{
1841 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
1842 int frag_bytes = file_buffer ? file_buffer->size : 0;
1843
1844 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher16111452010-07-22 05:12:18 +00001845 if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes
1846 && frag_bytes == dupl_ptr->fragment->size) {
plougher1b899fc2008-08-07 01:24:06 +00001847 long long target_start, dup_start = dupl_ptr->start;
plougher5507dd92006-11-06 00:43:10 +00001848 int block;
1849
plougher17b269c2009-03-30 01:33:07 +00001850 if(memcmp(*block_list, dupl_ptr->block_list, blocks *
1851 sizeof(unsigned int)) != 0)
plougherfbf9f752007-08-12 05:02:24 +00001852 continue;
1853
plougher5507dd92006-11-06 00:43:10 +00001854 if(checksum_flag == FALSE) {
plougher17b269c2009-03-30 01:33:07 +00001855 checksum = get_checksum_disk(*start, bytes,
1856 *block_list);
1857 fragment_checksum =
1858 get_checksum_mem_buffer(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00001859 checksum_flag = TRUE;
1860 }
1861
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001862 if(!dupl_ptr->have_frag_checksum)
1863 get_fragment_checksum(dupl_ptr);
Phillip Lougher557cefa2014-02-05 04:36:05 +00001864
1865 if(!dupl_ptr->have_checksum) {
1866 dupl_ptr->checksum =
1867 get_checksum_disk(dupl_ptr->start,
1868 dupl_ptr->bytes, dupl_ptr->block_list);
1869 dupl_ptr->have_checksum = TRUE;
plougher5507dd92006-11-06 00:43:10 +00001870 }
1871
plougher17b269c2009-03-30 01:33:07 +00001872 if(checksum != dupl_ptr->checksum ||
1873 fragment_checksum !=
1874 dupl_ptr->fragment_checksum)
plougher5507dd92006-11-06 00:43:10 +00001875 continue;
1876
plougher1b899fc2008-08-07 01:24:06 +00001877 target_start = *start;
plougher5507dd92006-11-06 00:43:10 +00001878 for(block = 0; block < blocks; block ++) {
plougher17b269c2009-03-30 01:33:07 +00001879 int size = SQUASHFS_COMPRESSED_SIZE_BLOCK
1880 ((*block_list)[block]);
plougher1b899fc2008-08-07 01:24:06 +00001881 struct file_buffer *target_buffer = NULL;
1882 struct file_buffer *dup_buffer = NULL;
1883 char *target_data, *dup_data;
plougher0f464442008-03-31 00:27:56 +00001884 int res;
plougher5507dd92006-11-06 00:43:10 +00001885
plougher1b899fc2008-08-07 01:24:06 +00001886 if(size == 0)
plougherfbf9f752007-08-12 05:02:24 +00001887 continue;
plougher17b269c2009-03-30 01:33:07 +00001888 target_buffer = cache_lookup(writer_buffer,
1889 target_start);
plougher1b899fc2008-08-07 01:24:06 +00001890 if(target_buffer)
1891 target_data = target_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001892 else {
plougher50b31762009-03-31 04:14:46 +00001893 target_data =
1894 read_from_disk(target_start,
plougher17b269c2009-03-30 01:33:07 +00001895 size);
Phillip Lougherac731382013-05-19 04:19:44 +01001896 if(target_data == NULL) {
1897 ERROR("Failed to read data from"
1898 " output filesystem\n");
1899 BAD_ERROR("Output filesystem"
1900 " corrupted?\n");
1901 }
1902 }
plougher5507dd92006-11-06 00:43:10 +00001903
plougher360514a2009-03-30 03:01:38 +00001904 dup_buffer = cache_lookup(writer_buffer,
1905 dup_start);
plougher1b899fc2008-08-07 01:24:06 +00001906 if(dup_buffer)
1907 dup_data = dup_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001908 else {
plougher360514a2009-03-30 03:01:38 +00001909 dup_data = read_from_disk2(dup_start,
1910 size);
Phillip Lougherac731382013-05-19 04:19:44 +01001911 if(dup_data == NULL) {
1912 ERROR("Failed to read data from"
1913 " output filesystem\n");
1914 BAD_ERROR("Output filesystem"
1915 " corrupted?\n");
1916 }
1917 }
plougher1b899fc2008-08-07 01:24:06 +00001918
1919 res = memcmp(target_data, dup_data, size);
1920 cache_block_put(target_buffer);
1921 cache_block_put(dup_buffer);
plougher0f464442008-03-31 00:27:56 +00001922 if(res != 0)
plougher5507dd92006-11-06 00:43:10 +00001923 break;
plougher1b899fc2008-08-07 01:24:06 +00001924 target_start += size;
1925 dup_start += size;
plougher5507dd92006-11-06 00:43:10 +00001926 }
1927 if(block == blocks) {
plougher17b269c2009-03-30 01:33:07 +00001928 struct file_buffer *frag_buffer =
1929 get_fragment(dupl_ptr->fragment);
plougher5507dd92006-11-06 00:43:10 +00001930
plougher17b269c2009-03-30 01:33:07 +00001931 if(frag_bytes == 0 ||
1932 memcmp(file_buffer->data,
1933 frag_buffer->data +
1934 dupl_ptr->fragment->offset,
1935 frag_bytes) == 0) {
plougher50b31762009-03-31 04:14:46 +00001936 TRACE("Found duplicate file, start "
1937 "0x%llx, size %lld, checksum "
1938 "0x%x, fragment %d, size %d, "
1939 "offset %d, checksum 0x%x\n",
1940 dupl_ptr->start,
plougher17b269c2009-03-30 01:33:07 +00001941 dupl_ptr->bytes,
1942 dupl_ptr->checksum,
1943 dupl_ptr->fragment->index,
1944 frag_bytes,
1945 dupl_ptr->fragment->offset,
1946 fragment_checksum);
plougher1f413c82005-11-18 00:02:14 +00001947 *block_list = dupl_ptr->block_list;
1948 *start = dupl_ptr->start;
1949 *fragment = dupl_ptr->fragment;
plougher76c64082008-03-08 01:32:23 +00001950 cache_block_put(frag_buffer);
plougher1f413c82005-11-18 00:02:14 +00001951 return 0;
1952 }
plougher76c64082008-03-08 01:32:23 +00001953 cache_block_put(frag_buffer);
plougher1f413c82005-11-18 00:02:14 +00001954 }
1955 }
1956
1957
plougher17b269c2009-03-30 01:33:07 +00001958 return add_non_dup(file_size, bytes, *block_list, *start, *fragment,
1959 checksum, fragment_checksum, checksum_flag);
plougher1f413c82005-11-18 00:02:14 +00001960}
1961
1962
Phillip Lougher2504b082011-09-07 02:28:45 +01001963inline int is_fragment(struct inode_info *inode)
1964{
1965 int file_size = inode->buf.st_size;
1966
Phillip Lougher63f531f2011-09-10 04:03:32 +01001967 /*
1968 * If this block is to be compressed differently to the
1969 * fragment compression then it cannot be a fragment
1970 */
1971 if(inode->noF != noF)
1972 return FALSE;
1973
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001974 return !inode->no_fragments && file_size && (file_size < block_size ||
Phillip Lougher2504b082011-09-07 02:28:45 +01001975 (inode->always_use_fragments && file_size & (block_size - 1)));
1976}
1977
1978
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001979void put_file_buffer(struct file_buffer *file_buffer)
1980{
1981 /*
1982 * Decide where to send the file buffer - only compressible non-
1983 * fragment blocks need to be send to the deflate threads, all
1984 * others can be sent directly to the main thread
1985 */
1986 if(file_buffer->error) {
1987 file_buffer->fragment = 0;
Phillip Lougher0e1656d2013-05-20 03:47:24 +01001988 seq_queue_put(to_main, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001989 } else if (file_buffer->file_size == 0 || file_buffer->fragment)
Phillip Lougher0e1656d2013-05-20 03:47:24 +01001990 seq_queue_put(to_main, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001991 else
Phillip Loughercf478e92013-05-29 02:38:41 +01001992 queue_put(to_deflate, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001993}
1994
1995
plougher00d08172009-09-03 10:17:44 +00001996static int seq = 0;
1997void reader_read_process(struct dir_ent *dir_ent)
1998{
Phillip Lougher9b6e3412011-09-05 02:58:41 +01001999 struct inode_info *inode = dir_ent->inode;
plougher00d08172009-09-03 10:17:44 +00002000 struct file_buffer *prev_buffer = NULL, *file_buffer;
plougherba674e82009-09-10 03:50:00 +00002001 int status, res, byte, count = 0;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002002 int file = get_pseudo_file(inode->pseudo_id)->fd;
2003 int child = get_pseudo_file(inode->pseudo_id)->child;
plougher00d08172009-09-03 10:17:44 +00002004 long long bytes = 0;
2005
2006 while(1) {
Phillip Lougher316ab632013-04-29 02:53:39 +01002007 file_buffer = cache_get_nohash(reader_buffer);
plougher00d08172009-09-03 10:17:44 +00002008 file_buffer->sequence = seq ++;
Phillip Lougher63f531f2011-09-10 04:03:32 +01002009 file_buffer->noD = inode->noD;
plougher00d08172009-09-03 10:17:44 +00002010
2011 byte = read_bytes(file, file_buffer->data, block_size);
2012 if(byte == -1)
2013 goto read_err;
2014
2015 file_buffer->size = byte;
2016 file_buffer->file_size = -1;
2017 file_buffer->block = count ++;
2018 file_buffer->error = FALSE;
2019 file_buffer->fragment = FALSE;
2020 bytes += byte;
2021
2022 if(byte == 0)
2023 break;
2024
plougher286b6b32009-09-19 03:29:27 +00002025 /*
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002026 * Update progress bar size. This is done
plougher286b6b32009-09-19 03:29:27 +00002027 * on every block rather than waiting for all blocks to be
2028 * read incase write_file_process() is running in parallel
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002029 * with this. Otherwise the current progress bar position
2030 * may get ahead of the progress bar size.
plougher286b6b32009-09-19 03:29:27 +00002031 */
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002032 progress_bar_size(1);
plougher286b6b32009-09-19 03:29:27 +00002033
plougher00d08172009-09-03 10:17:44 +00002034 if(prev_buffer)
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002035 put_file_buffer(prev_buffer);
plougher00d08172009-09-03 10:17:44 +00002036 prev_buffer = file_buffer;
2037 }
2038
plougher54d67292009-09-19 03:26:27 +00002039 /*
2040 * Update inode file size now that the size of the dynamic pseudo file
2041 * is known. This is needed for the -info option.
2042 */
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002043 inode->buf.st_size = bytes;
plougher54d67292009-09-19 03:26:27 +00002044
plougherba674e82009-09-10 03:50:00 +00002045 res = waitpid(child, &status, 0);
plougherd87d8d12009-09-10 04:08:16 +00002046 if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
plougherba674e82009-09-10 03:50:00 +00002047 goto read_err;
2048
plougher00d08172009-09-03 10:17:44 +00002049 if(prev_buffer == NULL)
2050 prev_buffer = file_buffer;
2051 else {
2052 cache_block_put(file_buffer);
2053 seq --;
2054 }
2055 prev_buffer->file_size = bytes;
Phillip Lougher2504b082011-09-07 02:28:45 +01002056 prev_buffer->fragment = is_fragment(inode);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002057 put_file_buffer(prev_buffer);
plougher00d08172009-09-03 10:17:44 +00002058
2059 return;
2060
2061read_err:
2062 if(prev_buffer) {
2063 cache_block_put(file_buffer);
2064 seq --;
2065 file_buffer = prev_buffer;
2066 }
2067 file_buffer->error = TRUE;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002068 put_file_buffer(file_buffer);
plougher00d08172009-09-03 10:17:44 +00002069}
2070
2071
plougher5507dd92006-11-06 00:43:10 +00002072void reader_read_file(struct dir_ent *dir_ent)
plougher1f413c82005-11-18 00:02:14 +00002073{
plougher018d2b32007-04-23 03:01:48 +00002074 struct stat *buf = &dir_ent->inode->buf, buf2;
plougher5507dd92006-11-06 00:43:10 +00002075 struct file_buffer *file_buffer;
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002076 int blocks, byte, count, expected, file, res;
plougher018d2b32007-04-23 03:01:48 +00002077 long long bytes, read_size;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002078 struct inode_info *inode = dir_ent->inode;
plougher5507dd92006-11-06 00:43:10 +00002079
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002080 if(inode->read)
plougher5507dd92006-11-06 00:43:10 +00002081 return;
2082
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002083 inode->read = TRUE;
plougher018d2b32007-04-23 03:01:48 +00002084again:
2085 bytes = 0;
2086 count = 0;
2087 file_buffer = NULL;
2088 read_size = buf->st_size;
2089 blocks = (read_size + block_size - 1) >> block_log;
plougher018d2b32007-04-23 03:01:48 +00002090
Phillip Lougher494479f2012-02-03 15:45:41 +00002091 file = open(pathname_reader(dir_ent), O_RDONLY);
plougherc1d258e2010-12-16 05:16:45 +00002092 if(file == -1) {
Phillip Lougher316ab632013-04-29 02:53:39 +01002093 file_buffer = cache_get_nohash(reader_buffer);
plougher1e380702010-08-11 01:52:49 +00002094 file_buffer->sequence = seq ++;
Phillip Lougher2302f692012-12-27 04:05:02 +00002095 goto read_err2;
plougher1e380702010-08-11 01:52:49 +00002096 }
plougher5507dd92006-11-06 00:43:10 +00002097
plougher018d2b32007-04-23 03:01:48 +00002098 do {
plougher110799c2009-03-30 01:50:40 +00002099 expected = read_size - ((long long) count * block_size) >
plougher50b31762009-03-31 04:14:46 +00002100 block_size ? block_size :
2101 read_size - ((long long) count * block_size);
plougher018d2b32007-04-23 03:01:48 +00002102
2103 if(file_buffer)
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002104 put_file_buffer(file_buffer);
Phillip Lougher316ab632013-04-29 02:53:39 +01002105 file_buffer = cache_get_nohash(reader_buffer);
plougher00d08172009-09-03 10:17:44 +00002106 file_buffer->sequence = seq ++;
Phillip Lougher63f531f2011-09-10 04:03:32 +01002107 file_buffer->noD = inode->noD;
plougher5507dd92006-11-06 00:43:10 +00002108
ploughera4c24ed2010-08-11 02:08:38 +00002109 /*
2110 * Always try to read block_size bytes from the file rather
2111 * than expected bytes (which will be less than the block_size
2112 * at the file tail) to check that the file hasn't grown
2113 * since being stated. If it is longer (or shorter) than
2114 * expected, then restat, and try again. Note the special
2115 * case where the file is an exact multiple of the block_size
2116 * is dealt with later.
2117 */
plougher110799c2009-03-30 01:50:40 +00002118 byte = file_buffer->size = read_bytes(file, file_buffer->data,
2119 block_size);
plougher018d2b32007-04-23 03:01:48 +00002120
plougher018d2b32007-04-23 03:01:48 +00002121 file_buffer->file_size = read_size;
2122
ploughera4c24ed2010-08-11 02:08:38 +00002123 if(byte == -1)
2124 goto read_err;
2125
plougher018d2b32007-04-23 03:01:48 +00002126 if(byte != expected)
2127 goto restat;
2128
2129 file_buffer->block = count;
plougher5507dd92006-11-06 00:43:10 +00002130 file_buffer->error = FALSE;
Phillip Lougher2504b082011-09-07 02:28:45 +01002131 file_buffer->fragment = FALSE;
plougher018d2b32007-04-23 03:01:48 +00002132
2133 bytes += byte;
2134 count ++;
2135 } while(count < blocks);
2136
2137 if(read_size != bytes)
2138 goto restat;
2139
2140 if(expected == block_size) {
ploughera4c24ed2010-08-11 02:08:38 +00002141 /*
2142 * Special case where we've not tried to read past the end of
2143 * the file. We expect to get EOF, i.e. the file isn't larger
2144 * than we expect.
2145 */
plougher018d2b32007-04-23 03:01:48 +00002146 char buffer;
ploughera4c24ed2010-08-11 02:08:38 +00002147 int res;
plougher018d2b32007-04-23 03:01:48 +00002148
ploughera4c24ed2010-08-11 02:08:38 +00002149 res = read_bytes(file, &buffer, 1);
2150 if(res == -1)
2151 goto read_err;
2152
2153 if(res != 0)
plougher018d2b32007-04-23 03:01:48 +00002154 goto restat;
plougher5507dd92006-11-06 00:43:10 +00002155 }
2156
Phillip Lougher2504b082011-09-07 02:28:45 +01002157 file_buffer->fragment = is_fragment(inode);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002158 put_file_buffer(file_buffer);
plougher018d2b32007-04-23 03:01:48 +00002159
plougher5507dd92006-11-06 00:43:10 +00002160 close(file);
plougher5507dd92006-11-06 00:43:10 +00002161
2162 return;
2163
plougher018d2b32007-04-23 03:01:48 +00002164restat:
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002165 res = fstat(file, &buf2);
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002166 if(res == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00002167 ERROR("Cannot stat dir/file %s because %s\n",
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002168 pathname_reader(dir_ent), strerror(errno));
2169 goto read_err;
2170 }
2171
plougher018d2b32007-04-23 03:01:48 +00002172 if(read_size != buf2.st_size) {
Phillip Lougher2302f692012-12-27 04:05:02 +00002173 close(file);
plougher018d2b32007-04-23 03:01:48 +00002174 memcpy(buf, &buf2, sizeof(struct stat));
2175 file_buffer->error = 2;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002176 put_file_buffer(file_buffer);
plougher018d2b32007-04-23 03:01:48 +00002177 goto again;
2178 }
plougher1e380702010-08-11 01:52:49 +00002179read_err:
Phillip Lougher2302f692012-12-27 04:05:02 +00002180 close(file);
2181read_err2:
plougher1e380702010-08-11 01:52:49 +00002182 file_buffer->error = TRUE;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002183 put_file_buffer(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002184}
2185
2186
2187void reader_scan(struct dir_info *dir) {
Phillip Lougherbf338362012-08-22 05:24:36 +01002188 struct dir_ent *dir_ent = dir->list;
plougher5507dd92006-11-06 00:43:10 +00002189
Phillip Lougherbf338362012-08-22 05:24:36 +01002190 for(; dir_ent; dir_ent = dir_ent->next) {
plougher5507dd92006-11-06 00:43:10 +00002191 struct stat *buf = &dir_ent->inode->buf;
ploughera326c182009-08-29 05:41:45 +00002192 if(dir_ent->inode->root_entry)
plougher5507dd92006-11-06 00:43:10 +00002193 continue;
2194
plougherb3977eb2010-05-02 02:08:48 +00002195 if(IS_PSEUDO_PROCESS(dir_ent->inode)) {
plougher00d08172009-09-03 10:17:44 +00002196 reader_read_process(dir_ent);
2197 continue;
2198 }
2199
plougher5507dd92006-11-06 00:43:10 +00002200 switch(buf->st_mode & S_IFMT) {
2201 case S_IFREG:
2202 reader_read_file(dir_ent);
2203 break;
2204 case S_IFDIR:
2205 reader_scan(dir_ent->dir);
2206 break;
2207 }
2208 }
2209}
2210
2211
2212void *reader(void *arg)
2213{
plougher5507dd92006-11-06 00:43:10 +00002214 if(!sorted)
2215 reader_scan(queue_get(to_reader));
2216 else {
2217 int i;
2218 struct priority_entry *entry;
2219
2220 queue_get(to_reader);
2221 for(i = 65535; i >= 0; i--)
plougher16111452010-07-22 05:12:18 +00002222 for(entry = priority_list[i]; entry;
2223 entry = entry->next)
plougher5507dd92006-11-06 00:43:10 +00002224 reader_read_file(entry->dir);
2225 }
rloughere4873e02007-11-08 17:50:42 +00002226
2227 pthread_exit(NULL);
plougher5507dd92006-11-06 00:43:10 +00002228}
2229
2230
2231void *writer(void *arg)
2232{
plougher5507dd92006-11-06 00:43:10 +00002233 while(1) {
2234 struct file_buffer *file_buffer = queue_get(to_writer);
2235 off_t off;
2236
2237 if(file_buffer == NULL) {
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002238 queue_put(from_writer, NULL);
plougher5507dd92006-11-06 00:43:10 +00002239 continue;
2240 }
2241
2242 off = file_buffer->block;
2243
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002244 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
plougher5507dd92006-11-06 00:43:10 +00002245 pthread_mutex_lock(&pos_mutex);
2246
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002247 if(lseek(fd, off, SEEK_SET) == -1) {
Phillip Lougherd23000c2013-02-06 22:05:45 +00002248 ERROR("writer: Lseek on destination failed because "
2249 "%s, offset=0x%llx\n", strerror(errno), off);
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01002250 BAD_ERROR("Probably out of space on output "
Phillip Lougher3f4ccd22013-02-11 04:40:09 +00002251 "%s\n", block_device ? "block device" :
2252 "filesystem");
plougher5507dd92006-11-06 00:43:10 +00002253 }
2254
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002255 if(write_bytes(fd, file_buffer->data,
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01002256 file_buffer->size) == -1)
2257 BAD_ERROR("Failed to write to output %s\n",
Phillip Loughere9df4552013-02-14 23:06:29 +00002258 block_device ? "block device" : "filesystem");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002259
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002260 pthread_cleanup_pop(1);
2261
ploughereb6eac92008-02-26 01:50:48 +00002262 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002263 }
2264}
2265
2266
plougher5b09fd42007-08-06 10:28:41 +00002267int all_zero(struct file_buffer *file_buffer)
2268{
2269 int i;
2270 long entries = file_buffer->size / sizeof(long);
2271 long *p = (long *) file_buffer->data;
2272
2273 for(i = 0; i < entries && p[i] == 0; i++);
2274
2275 if(i == entries) {
plougher110799c2009-03-30 01:50:40 +00002276 for(i = file_buffer->size & ~(sizeof(long) - 1);
plougher50b31762009-03-31 04:14:46 +00002277 i < file_buffer->size && file_buffer->data[i] == 0;
2278 i++);
plougher5b09fd42007-08-06 10:28:41 +00002279
2280 return i == file_buffer->size;
2281 }
2282
2283 return 0;
2284}
2285
2286
plougher5507dd92006-11-06 00:43:10 +00002287void *deflator(void *arg)
2288{
plougher7b8ee502009-07-29 07:54:30 +00002289 void *stream = NULL;
Phillip Lougher3c838292013-03-26 04:24:53 +00002290 int res;
plougher5507dd92006-11-06 00:43:10 +00002291
plougher13fdddf2010-11-24 01:23:41 +00002292 res = compressor_init(comp, &stream, block_size, 1);
2293 if(res)
2294 BAD_ERROR("deflator:: compressor_init failed\n");
2295
plougher5507dd92006-11-06 00:43:10 +00002296 while(1) {
Phillip Loughercf478e92013-05-29 02:38:41 +01002297 struct file_buffer *file_buffer = queue_get(to_deflate);
plougher1b899fc2008-08-07 01:24:06 +00002298 struct file_buffer *write_buffer;
plougher5507dd92006-11-06 00:43:10 +00002299
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002300 if(sparse_files && all_zero(file_buffer)) {
Phillip Lougher48854382011-09-09 03:36:55 +01002301 file_buffer->c_byte = 0;
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002302 seq_queue_put(to_main, file_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002303 } else {
Phillip Lougher45702012013-04-30 05:19:54 +01002304 write_buffer = cache_get(writer_buffer, -1);
plougher13fdddf2010-11-24 01:23:41 +00002305 write_buffer->c_byte = mangle2(stream,
plougher50b31762009-03-31 04:14:46 +00002306 write_buffer->data, file_buffer->data,
Phillip Lougher63f531f2011-09-10 04:03:32 +01002307 file_buffer->size, block_size,
2308 file_buffer->noD, 1);
plougher1b899fc2008-08-07 01:24:06 +00002309 write_buffer->sequence = file_buffer->sequence;
2310 write_buffer->file_size = file_buffer->file_size;
2311 write_buffer->block = file_buffer->block;
plougher110799c2009-03-30 01:50:40 +00002312 write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK
2313 (write_buffer->c_byte);
plougher1b899fc2008-08-07 01:24:06 +00002314 write_buffer->fragment = FALSE;
2315 write_buffer->error = FALSE;
2316 cache_block_put(file_buffer);
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002317 seq_queue_put(to_main, write_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002318 }
plougher5507dd92006-11-06 00:43:10 +00002319 }
2320}
2321
2322
2323void *frag_deflator(void *arg)
2324{
plougher7b8ee502009-07-29 07:54:30 +00002325 void *stream = NULL;
Phillip Lougher3c838292013-03-26 04:24:53 +00002326 int res;
plougher5507dd92006-11-06 00:43:10 +00002327
plougher13fdddf2010-11-24 01:23:41 +00002328 res = compressor_init(comp, &stream, block_size, 1);
2329 if(res)
2330 BAD_ERROR("frag_deflator:: compressor_init failed\n");
2331
Phillip Lougher53240e62013-04-30 23:51:29 +01002332 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
2333
plougher5507dd92006-11-06 00:43:10 +00002334 while(1) {
2335 int c_byte, compressed_size;
2336 struct file_buffer *file_buffer = queue_get(to_frag);
plougher110799c2009-03-30 01:50:40 +00002337 struct file_buffer *write_buffer =
Phillip Lougher45702012013-04-30 05:19:54 +01002338 cache_get(writer_buffer,
2339 FRAG_INDEX(file_buffer->block));
plougher5507dd92006-11-06 00:43:10 +00002340
plougher13fdddf2010-11-24 01:23:41 +00002341 c_byte = mangle2(stream, write_buffer->data, file_buffer->data,
plougher110799c2009-03-30 01:50:40 +00002342 file_buffer->size, block_size, noF, 1);
plougher5507dd92006-11-06 00:43:10 +00002343 compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
plougherd036a312008-03-08 01:38:27 +00002344 write_buffer->size = compressed_size;
plougherd1139d52008-04-28 03:07:07 +00002345 pthread_mutex_lock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00002346 if(fragments_locked == FALSE) {
plougher2ea89142008-03-11 01:34:19 +00002347 fragment_table[file_buffer->block].size = c_byte;
2348 fragment_table[file_buffer->block].start_block = bytes;
2349 write_buffer->block = bytes;
2350 bytes += compressed_size;
2351 fragments_outstanding --;
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002352 pthread_mutex_unlock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00002353 queue_put(to_writer, write_buffer);
plougher110799c2009-03-30 01:50:40 +00002354 TRACE("Writing fragment %lld, uncompressed size %d, "
2355 "compressed size %d\n", file_buffer->block,
2356 file_buffer->size, compressed_size);
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002357 } else {
plougher110799c2009-03-30 01:50:40 +00002358 add_pending_fragment(write_buffer, c_byte,
2359 file_buffer->block);
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002360 pthread_mutex_unlock(&fragment_mutex);
2361 }
ploughereb6eac92008-02-26 01:50:48 +00002362 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002363 }
Phillip Lougher53240e62013-04-30 23:51:29 +01002364
2365 pthread_cleanup_pop(0);
plougher5507dd92006-11-06 00:43:10 +00002366}
2367
2368
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002369struct file_buffer *get_file_buffer()
plougher5507dd92006-11-06 00:43:10 +00002370{
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002371 struct file_buffer *file_buffer = seq_queue_get(to_main);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002372
2373 if(file_buffer->fragment) {
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002374 if(sparse_files && all_zero(file_buffer)) {
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002375 file_buffer->c_byte = 0;
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002376 file_buffer->fragment = FALSE;
2377 } else
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002378 file_buffer->c_byte = file_buffer->size;
2379 }
2380
2381 return file_buffer;
plougher5507dd92006-11-06 00:43:10 +00002382}
2383
2384
plougher110799c2009-03-30 01:50:40 +00002385void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent,
2386 int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002387{
2388 file_count ++;
2389 *duplicate_file = FALSE;
plougherc1ace522010-05-01 03:00:45 +00002390 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0,
2391 NULL, &empty_fragment, NULL, 0);
plougher5507dd92006-11-06 00:43:10 +00002392}
2393
2394
plougher50b31762009-03-31 04:14:46 +00002395void write_file_frag_dup(squashfs_inode *inode, struct dir_ent *dir_ent,
2396 int size, int *duplicate_file, struct file_buffer *file_buffer,
plougher360514a2009-03-30 03:01:38 +00002397 unsigned short checksum)
plougher5507dd92006-11-06 00:43:10 +00002398{
plougher5507dd92006-11-06 00:43:10 +00002399 struct file_info *dupl_ptr;
plougher1f413c82005-11-18 00:02:14 +00002400 struct fragment *fragment;
plougher5507dd92006-11-06 00:43:10 +00002401 unsigned int *block_listp = NULL;
2402 long long start = 0;
plougherf9c72b12006-01-23 13:52:40 +00002403
plougher50b31762009-03-31 04:14:46 +00002404 dupl_ptr = duplicate(size, 0, &block_listp, &start, &fragment,
2405 file_buffer, 0, 0, checksum, TRUE);
plougher1f413c82005-11-18 00:02:14 +00002406
plougher5507dd92006-11-06 00:43:10 +00002407 if(dupl_ptr) {
2408 *duplicate_file = FALSE;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002409 fragment = get_and_fill_fragment(file_buffer, dir_ent);
plougher5507dd92006-11-06 00:43:10 +00002410 dupl_ptr->fragment = fragment;
2411 } else
2412 *duplicate_file = TRUE;
2413
ploughereb6eac92008-02-26 01:50:48 +00002414 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002415
2416 total_bytes += size;
2417 file_count ++;
2418
plougher35a10602008-04-21 02:58:16 +00002419 inc_progress_bar();
plougher02bc3bc2007-02-25 12:12:01 +00002420
plougherc1ace522010-05-01 03:00:45 +00002421 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0,
plougher3c6bdb52010-05-01 02:30:59 +00002422 0, NULL, fragment, NULL, 0);
plougher5507dd92006-11-06 00:43:10 +00002423}
2424
2425
plougher110799c2009-03-30 01:50:40 +00002426void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, int size,
2427 struct file_buffer *file_buffer, int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002428{
2429 struct fragment *fragment;
2430 unsigned short checksum;
plougher5507dd92006-11-06 00:43:10 +00002431
2432 checksum = get_checksum_mem_buffer(file_buffer);
2433
plougher29e37092007-04-15 01:24:51 +00002434 if(pre_duplicate_frag(size, checksum)) {
plougher110799c2009-03-30 01:50:40 +00002435 write_file_frag_dup(inode, dir_ent, size, duplicate_file,
2436 file_buffer, checksum);
plougher29e37092007-04-15 01:24:51 +00002437 return;
2438 }
plougher5507dd92006-11-06 00:43:10 +00002439
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002440 fragment = get_and_fill_fragment(file_buffer, dir_ent);
plougher5507dd92006-11-06 00:43:10 +00002441
ploughereb6eac92008-02-26 01:50:48 +00002442 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002443
2444 if(duplicate_checking)
2445 add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, TRUE);
2446
2447 total_bytes += size;
2448 file_count ++;
2449
2450 *duplicate_file = FALSE;
2451
plougher35a10602008-04-21 02:58:16 +00002452 inc_progress_bar();
plougher02bc3bc2007-02-25 12:12:01 +00002453
plougherc1ace522010-05-01 03:00:45 +00002454 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0,
plougher3c6bdb52010-05-01 02:30:59 +00002455 0, NULL, fragment, NULL, 0);
plougher29e37092007-04-15 01:24:51 +00002456
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002457 if(duplicate_checking == FALSE)
2458 free_fragment(fragment);
2459
plougher018d2b32007-04-23 03:01:48 +00002460 return;
plougher5507dd92006-11-06 00:43:10 +00002461}
2462
2463
plougher00d08172009-09-03 10:17:44 +00002464int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent,
2465 struct file_buffer *read_buffer, int *duplicate_file)
2466{
2467 long long read_size, file_bytes, start;
2468 struct fragment *fragment;
2469 unsigned int *block_list = NULL;
2470 int block = 0, status;
2471 long long sparse = 0;
2472 struct file_buffer *fragment_buffer = NULL;
2473
2474 *duplicate_file = FALSE;
2475
2476 lock_fragments();
2477
2478 file_bytes = 0;
2479 start = bytes;
2480 while (1) {
2481 read_size = read_buffer->file_size;
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002482 if(read_buffer->fragment)
plougher00d08172009-09-03 10:17:44 +00002483 fragment_buffer = read_buffer;
2484 else {
2485 block_list = realloc(block_list, (block + 1) *
2486 sizeof(unsigned int));
2487 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002488 MEM_ERROR();
plougher00d08172009-09-03 10:17:44 +00002489 block_list[block ++] = read_buffer->c_byte;
2490 if(read_buffer->c_byte) {
2491 read_buffer->block = bytes;
2492 bytes += read_buffer->size;
2493 cache_rehash(read_buffer, read_buffer->block);
2494 file_bytes += read_buffer->size;
2495 queue_put(to_writer, read_buffer);
2496 } else {
2497 sparse += read_buffer->size;
2498 cache_block_put(read_buffer);
2499 }
2500 }
plougher286b6b32009-09-19 03:29:27 +00002501 inc_progress_bar();
plougher00d08172009-09-03 10:17:44 +00002502
2503 if(read_size != -1)
2504 break;
2505
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002506 read_buffer = get_file_buffer();
plougher00d08172009-09-03 10:17:44 +00002507 if(read_buffer->error)
2508 goto read_err;
2509 }
2510
2511 unlock_fragments();
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002512 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher00d08172009-09-03 10:17:44 +00002513 cache_block_put(fragment_buffer);
2514
2515 if(duplicate_checking)
2516 add_non_dup(read_size, file_bytes, block_list, start, fragment,
2517 0, 0, FALSE);
2518 file_count ++;
2519 total_bytes += read_size;
2520
plougherc1ace522010-05-01 03:00:45 +00002521 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2522 block, block_list, fragment, NULL, sparse);
plougher00d08172009-09-03 10:17:44 +00002523
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002524 if(duplicate_checking == FALSE) {
plougher00d08172009-09-03 10:17:44 +00002525 free(block_list);
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002526 free_fragment(fragment);
2527 }
plougher00d08172009-09-03 10:17:44 +00002528
2529 return 0;
2530
2531read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002532 dec_progress_bar(block);
plougher00d08172009-09-03 10:17:44 +00002533 status = read_buffer->error;
2534 bytes = start;
2535 if(!block_device) {
2536 int res;
2537
2538 queue_put(to_writer, NULL);
2539 if(queue_get(from_writer) != 0)
2540 EXIT_MKSQUASHFS();
2541 res = ftruncate(fd, bytes);
2542 if(res != 0)
2543 BAD_ERROR("Failed to truncate dest file because %s\n",
2544 strerror(errno));
2545 }
2546 unlock_fragments();
2547 free(block_list);
2548 cache_block_put(read_buffer);
2549 return status;
2550}
2551
2552
plougher110799c2009-03-30 01:50:40 +00002553int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent,
plougher50b31762009-03-31 04:14:46 +00002554 long long read_size, struct file_buffer *read_buffer,
2555 int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002556{
plougher23377982007-11-12 04:04:48 +00002557 long long file_bytes, start;
plougher5507dd92006-11-06 00:43:10 +00002558 struct fragment *fragment;
plougher5507dd92006-11-06 00:43:10 +00002559 unsigned int *block_list;
plougher1b899fc2008-08-07 01:24:06 +00002560 int block, status;
2561 int blocks = (read_size + block_size - 1) >> block_log;
2562 long long sparse = 0;
2563 struct file_buffer *fragment_buffer = NULL;
plougher5507dd92006-11-06 00:43:10 +00002564
plougher29e37092007-04-15 01:24:51 +00002565 *duplicate_file = FALSE;
2566
plougher87139622010-12-16 05:19:30 +00002567 block_list = malloc(blocks * sizeof(unsigned int));
2568 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002569 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00002570
plougher2ea89142008-03-11 01:34:19 +00002571 lock_fragments();
plougher1f413c82005-11-18 00:02:14 +00002572
plougher5507dd92006-11-06 00:43:10 +00002573 file_bytes = 0;
2574 start = bytes;
plougher1b899fc2008-08-07 01:24:06 +00002575 for(block = 0; block < blocks;) {
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002576 if(read_buffer->fragment) {
Phillip Lougher2760d822013-01-13 21:14:06 +00002577 block_list[block] = 0;
plougher1b899fc2008-08-07 01:24:06 +00002578 fragment_buffer = read_buffer;
2579 blocks = read_size >> block_log;
plougher018d2b32007-04-23 03:01:48 +00002580 } else {
plougher1b899fc2008-08-07 01:24:06 +00002581 block_list[block] = read_buffer->c_byte;
2582 if(read_buffer->c_byte) {
2583 read_buffer->block = bytes;
2584 bytes += read_buffer->size;
2585 cache_rehash(read_buffer, read_buffer->block);
2586 file_bytes += read_buffer->size;
2587 queue_put(to_writer, read_buffer);
2588 } else {
2589 sparse += read_buffer->size;
2590 cache_block_put(read_buffer);
2591 }
2592 }
2593 inc_progress_bar();
2594
2595 if(++block < blocks) {
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002596 read_buffer = get_file_buffer();
plougher018d2b32007-04-23 03:01:48 +00002597 if(read_buffer->error)
2598 goto read_err;
2599 }
plougher1f413c82005-11-18 00:02:14 +00002600 }
2601
plougher2ea89142008-03-11 01:34:19 +00002602 unlock_fragments();
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002603 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher1b899fc2008-08-07 01:24:06 +00002604 cache_block_put(fragment_buffer);
plougher5507dd92006-11-06 00:43:10 +00002605
plougher1f413c82005-11-18 00:02:14 +00002606 if(duplicate_checking)
plougher50b31762009-03-31 04:14:46 +00002607 add_non_dup(read_size, file_bytes, block_list, start, fragment,
2608 0, 0, FALSE);
plougher1f413c82005-11-18 00:02:14 +00002609 file_count ++;
plougher5507dd92006-11-06 00:43:10 +00002610 total_bytes += read_size;
plougher29e37092007-04-15 01:24:51 +00002611
plougher360514a2009-03-30 03:01:38 +00002612 /*
2613 * sparse count is needed to ensure squashfs correctly reports a
plougher1b899fc2008-08-07 01:24:06 +00002614 * a smaller block count on stat calls to sparse files. This is
2615 * to ensure intelligent applications like cp correctly handle the
2616 * file as a sparse file. If the file in the original filesystem isn't
2617 * stored as a sparse file then still store it sparsely in squashfs, but
plougher360514a2009-03-30 03:01:38 +00002618 * report it as non-sparse on stat calls to preserve semantics
2619 */
plougher1b899fc2008-08-07 01:24:06 +00002620 if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2621 sparse = 0;
2622
plougherc1ace522010-05-01 03:00:45 +00002623 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2624 blocks, block_list, fragment, NULL, sparse);
plougher29e37092007-04-15 01:24:51 +00002625
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002626 if(duplicate_checking == FALSE) {
plougherf9c72b12006-01-23 13:52:40 +00002627 free(block_list);
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002628 free_fragment(fragment);
2629 }
plougher29e37092007-04-15 01:24:51 +00002630
plougher018d2b32007-04-23 03:01:48 +00002631 return 0;
plougher1f413c82005-11-18 00:02:14 +00002632
2633read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002634 dec_progress_bar(block);
plougher018d2b32007-04-23 03:01:48 +00002635 status = read_buffer->error;
plougher1b899fc2008-08-07 01:24:06 +00002636 bytes = start;
2637 if(!block_device) {
plougher12a159a2009-03-03 11:06:34 +00002638 int res;
2639
plougher5507dd92006-11-06 00:43:10 +00002640 queue_put(to_writer, NULL);
2641 if(queue_get(from_writer) != 0)
2642 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002643 res = ftruncate(fd, bytes);
2644 if(res != 0)
2645 BAD_ERROR("Failed to truncate dest file because %s\n",
2646 strerror(errno));
plougher5507dd92006-11-06 00:43:10 +00002647 }
plougher2ea89142008-03-11 01:34:19 +00002648 unlock_fragments();
plougherf9c72b12006-01-23 13:52:40 +00002649 free(block_list);
ploughereb6eac92008-02-26 01:50:48 +00002650 cache_block_put(read_buffer);
plougher018d2b32007-04-23 03:01:48 +00002651 return status;
plougher1f413c82005-11-18 00:02:14 +00002652}
2653
2654
plougher110799c2009-03-30 01:50:40 +00002655int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent,
plougher50b31762009-03-31 04:14:46 +00002656 long long read_size, struct file_buffer *read_buffer,
2657 int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002658{
plougher29e37092007-04-15 01:24:51 +00002659 int block, thresh;
plougher1b899fc2008-08-07 01:24:06 +00002660 long long file_bytes, dup_start, start;
plougher5507dd92006-11-06 00:43:10 +00002661 struct fragment *fragment;
2662 struct file_info *dupl_ptr;
2663 int blocks = (read_size + block_size - 1) >> block_log;
2664 unsigned int *block_list, *block_listp;
plougher1b899fc2008-08-07 01:24:06 +00002665 struct file_buffer **buffer_list;
plougher4e8484a2008-03-15 23:30:16 +00002666 int status, num_locked_fragments;
plougher1b899fc2008-08-07 01:24:06 +00002667 long long sparse = 0;
2668 struct file_buffer *fragment_buffer = NULL;
plougher5507dd92006-11-06 00:43:10 +00002669
plougher50b31762009-03-31 04:14:46 +00002670 block_list = malloc(blocks * sizeof(unsigned int));
2671 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002672 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00002673 block_listp = block_list;
2674
plougher50b31762009-03-31 04:14:46 +00002675 buffer_list = malloc(blocks * sizeof(struct file_buffer *));
2676 if(buffer_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002677 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00002678
plougher4e8484a2008-03-15 23:30:16 +00002679 num_locked_fragments = lock_fragments();
plougher5507dd92006-11-06 00:43:10 +00002680
2681 file_bytes = 0;
plougher1b899fc2008-08-07 01:24:06 +00002682 start = dup_start = bytes;
plougher110799c2009-03-30 01:50:40 +00002683 thresh = blocks > (writer_buffer_size - num_locked_fragments) ?
2684 blocks - (writer_buffer_size - num_locked_fragments): 0;
plougher1b899fc2008-08-07 01:24:06 +00002685
2686 for(block = 0; block < blocks;) {
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002687 if(read_buffer->fragment) {
Phillip Lougher38cb1ab2013-01-13 21:20:09 +00002688 block_list[block] = 0;
Phillip Lougher19de5b62013-01-13 21:31:51 +00002689 buffer_list[block] = NULL;
plougher1b899fc2008-08-07 01:24:06 +00002690 fragment_buffer = read_buffer;
2691 blocks = read_size >> block_log;
2692 } else {
2693 block_list[block] = read_buffer->c_byte;
2694
2695 if(read_buffer->c_byte) {
2696 read_buffer->block = bytes;
2697 bytes += read_buffer->size;
2698 file_bytes += read_buffer->size;
2699 cache_rehash(read_buffer, read_buffer->block);
2700 if(block < thresh) {
2701 buffer_list[block] = NULL;
2702 queue_put(to_writer, read_buffer);
2703 } else
2704 buffer_list[block] = read_buffer;
2705 } else {
2706 buffer_list[block] = NULL;
2707 sparse += read_buffer->size;
2708 cache_block_put(read_buffer);
2709 }
2710 }
2711 inc_progress_bar();
2712
2713 if(++block < blocks) {
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002714 read_buffer = get_file_buffer();
plougher018d2b32007-04-23 03:01:48 +00002715 if(read_buffer->error)
2716 goto read_err;
2717 }
plougher5507dd92006-11-06 00:43:10 +00002718 }
2719
plougher110799c2009-03-30 01:50:40 +00002720 dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start,
2721 &fragment, fragment_buffer, blocks, 0, 0, FALSE);
plougher5507dd92006-11-06 00:43:10 +00002722
2723 if(dupl_ptr) {
2724 *duplicate_file = FALSE;
2725 for(block = thresh; block < blocks; block ++)
plougher1b899fc2008-08-07 01:24:06 +00002726 if(buffer_list[block])
2727 queue_put(to_writer, buffer_list[block]);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002728 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher5507dd92006-11-06 00:43:10 +00002729 dupl_ptr->fragment = fragment;
2730 } else {
2731 *duplicate_file = TRUE;
2732 for(block = thresh; block < blocks; block ++)
plougher1b899fc2008-08-07 01:24:06 +00002733 cache_block_put(buffer_list[block]);
2734 bytes = start;
2735 if(thresh && !block_device) {
plougher12a159a2009-03-03 11:06:34 +00002736 int res;
2737
plougher1b899fc2008-08-07 01:24:06 +00002738 queue_put(to_writer, NULL);
2739 if(queue_get(from_writer) != 0)
2740 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002741 res = ftruncate(fd, bytes);
2742 if(res != 0)
2743 BAD_ERROR("Failed to truncate dest file because"
2744 " %s\n", strerror(errno));
plougher1b899fc2008-08-07 01:24:06 +00002745 }
plougher5507dd92006-11-06 00:43:10 +00002746 }
2747
plougher2ea89142008-03-11 01:34:19 +00002748 unlock_fragments();
plougher1b899fc2008-08-07 01:24:06 +00002749 cache_block_put(fragment_buffer);
plougher5507dd92006-11-06 00:43:10 +00002750 free(buffer_list);
2751 file_count ++;
2752 total_bytes += read_size;
2753
plougher360514a2009-03-30 03:01:38 +00002754 /*
2755 * sparse count is needed to ensure squashfs correctly reports a
plougher1b899fc2008-08-07 01:24:06 +00002756 * a smaller block count on stat calls to sparse files. This is
2757 * to ensure intelligent applications like cp correctly handle the
2758 * file as a sparse file. If the file in the original filesystem isn't
2759 * stored as a sparse file then still store it sparsely in squashfs, but
plougher360514a2009-03-30 03:01:38 +00002760 * report it as non-sparse on stat calls to preserve semantics
2761 */
plougher1b899fc2008-08-07 01:24:06 +00002762 if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2763 sparse = 0;
2764
plougherc1ace522010-05-01 03:00:45 +00002765 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size,
2766 dup_start, blocks, block_listp, fragment, NULL, sparse);
plougher29e37092007-04-15 01:24:51 +00002767
plougher5507dd92006-11-06 00:43:10 +00002768 if(*duplicate_file == TRUE)
2769 free(block_list);
plougher29e37092007-04-15 01:24:51 +00002770
plougher018d2b32007-04-23 03:01:48 +00002771 return 0;
plougher5507dd92006-11-06 00:43:10 +00002772
2773read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002774 dec_progress_bar(block);
plougher018d2b32007-04-23 03:01:48 +00002775 status = read_buffer->error;
plougher1b899fc2008-08-07 01:24:06 +00002776 bytes = start;
2777 if(thresh && !block_device) {
plougher12a159a2009-03-03 11:06:34 +00002778 int res;
2779
plougher5507dd92006-11-06 00:43:10 +00002780 queue_put(to_writer, NULL);
2781 if(queue_get(from_writer) != 0)
2782 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002783 res = ftruncate(fd, bytes);
2784 if(res != 0)
2785 BAD_ERROR("Failed to truncate dest file because %s\n",
2786 strerror(errno));
plougher5507dd92006-11-06 00:43:10 +00002787 }
plougher2ea89142008-03-11 01:34:19 +00002788 unlock_fragments();
plougher5507dd92006-11-06 00:43:10 +00002789 for(blocks = thresh; blocks < block; blocks ++)
plougher1b899fc2008-08-07 01:24:06 +00002790 cache_block_put(buffer_list[blocks]);
plougher5507dd92006-11-06 00:43:10 +00002791 free(buffer_list);
2792 free(block_list);
ploughereb6eac92008-02-26 01:50:48 +00002793 cache_block_put(read_buffer);
plougher018d2b32007-04-23 03:01:48 +00002794 return status;
plougher5507dd92006-11-06 00:43:10 +00002795}
2796
2797
plougher110799c2009-03-30 01:50:40 +00002798void write_file(squashfs_inode *inode, struct dir_ent *dir_ent,
2799 int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002800{
plougher018d2b32007-04-23 03:01:48 +00002801 int status;
2802 struct file_buffer *read_buffer;
2803 long long read_size;
plougher5507dd92006-11-06 00:43:10 +00002804
plougher018d2b32007-04-23 03:01:48 +00002805again:
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002806 read_buffer = get_file_buffer();
plougher1b899fc2008-08-07 01:24:06 +00002807
plougher23377982007-11-12 04:04:48 +00002808 status = read_buffer->error;
2809 if(status) {
ploughereb6eac92008-02-26 01:50:48 +00002810 cache_block_put(read_buffer);
plougher018d2b32007-04-23 03:01:48 +00002811 goto file_err;
2812 }
2813
2814 read_size = read_buffer->file_size;
plougher5507dd92006-11-06 00:43:10 +00002815
plougher00d08172009-09-03 10:17:44 +00002816 if(read_size == -1)
2817 status = write_file_process(inode, dir_ent, read_buffer,
2818 duplicate_file);
2819 else if(read_size == 0) {
plougher29e37092007-04-15 01:24:51 +00002820 write_file_empty(inode, dir_ent, duplicate_file);
ploughereb6eac92008-02-26 01:50:48 +00002821 cache_block_put(read_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002822 } else if(read_buffer->fragment && read_buffer->c_byte)
plougher110799c2009-03-30 01:50:40 +00002823 write_file_frag(inode, dir_ent, read_size, read_buffer,
2824 duplicate_file);
plougher29e37092007-04-15 01:24:51 +00002825 else if(pre_duplicate(read_size))
plougher110799c2009-03-30 01:50:40 +00002826 status = write_file_blocks_dup(inode, dir_ent, read_size,
2827 read_buffer, duplicate_file);
plougher29e37092007-04-15 01:24:51 +00002828 else
plougher50b31762009-03-31 04:14:46 +00002829 status = write_file_blocks(inode, dir_ent, read_size,
2830 read_buffer, duplicate_file);
plougher5507dd92006-11-06 00:43:10 +00002831
plougher018d2b32007-04-23 03:01:48 +00002832file_err:
2833 if(status == 2) {
plougher50b31762009-03-31 04:14:46 +00002834 ERROR("File %s changed size while reading filesystem, "
Phillip Lougher494479f2012-02-03 15:45:41 +00002835 "attempting to re-read\n", pathname(dir_ent));
plougher018d2b32007-04-23 03:01:48 +00002836 goto again;
2837 } else if(status == 1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00002838 ERROR_START("Failed to read file %s", pathname(dir_ent));
2839 ERROR_EXIT(", creating empty file\n");
plougher29e37092007-04-15 01:24:51 +00002840 write_file_empty(inode, dir_ent, duplicate_file);
2841 }
plougher5507dd92006-11-06 00:43:10 +00002842}
2843
2844
Phillip Lougher3f81a772012-12-04 05:07:24 +00002845#define BUFF_SIZE 512
plougher1f413c82005-11-18 00:02:14 +00002846char *name;
2847char *basename_r();
2848
2849char *getbase(char *pathname)
2850{
Phillip Lougher3f81a772012-12-04 05:07:24 +00002851 static char *b_buffer = NULL;
2852 static int b_size = BUFF_SIZE;
plougher1f413c82005-11-18 00:02:14 +00002853 char *result;
2854
Phillip Lougher3f81a772012-12-04 05:07:24 +00002855 if(b_buffer == NULL) {
2856 b_buffer = malloc(b_size);
2857 if(b_buffer == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002858 MEM_ERROR();
Phillip Lougher3f81a772012-12-04 05:07:24 +00002859 }
2860
2861 while(1) {
2862 if(*pathname != '/') {
2863 result = getcwd(b_buffer, b_size);
2864 if(result == NULL && errno != ERANGE)
2865 BAD_ERROR("Getcwd failed in getbase\n");
2866
2867 /* enough room for pathname + "/" + '\0' terminator? */
2868 if(result && strlen(pathname) + 2 <=
2869 b_size - strlen(b_buffer)) {
2870 strcat(strcat(b_buffer, "/"), pathname);
2871 break;
2872 }
2873 } else if(strlen(pathname) < b_size) {
2874 strcpy(b_buffer, pathname);
2875 break;
2876 }
2877
2878 /* Buffer not large enough, realloc and try again */
2879 b_buffer = realloc(b_buffer, b_size += BUFF_SIZE);
2880 if(b_buffer == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002881 MEM_ERROR();
Phillip Lougher3f81a772012-12-04 05:07:24 +00002882 }
2883
plougher1f413c82005-11-18 00:02:14 +00002884 name = b_buffer;
2885 if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0))
2886 return NULL;
2887 else
2888 return result;
2889}
2890
2891
2892char *basename_r()
2893{
2894 char *s;
2895 char *p;
2896 int n = 1;
2897
2898 for(;;) {
2899 s = name;
2900 if(*name == '\0')
2901 return NULL;
2902 if(*name != '/') {
2903 while(*name != '\0' && *name != '/') name++;
2904 n = name - s;
2905 }
2906 while(*name == '/') name++;
2907 if(strncmp(s, ".", n) == 0)
2908 continue;
plougher110799c2009-03-30 01:50:40 +00002909 if((*name == '\0') || (strncmp(s, "..", n) == 0) ||
2910 ((p = basename_r()) == NULL)) {
plougher1f413c82005-11-18 00:02:14 +00002911 s[n] = '\0';
2912 return s;
2913 }
2914 if(strcmp(p, "..") == 0)
2915 continue;
2916 return p;
2917 }
2918}
2919
2920
Phillip Lougher81204c22012-07-25 03:30:30 +01002921struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id)
plougher1f413c82005-11-18 00:02:14 +00002922{
Phillip Lougherd6577802012-10-04 19:14:33 +01002923 int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino);
2924 struct inode_info *inode;
plougher1f413c82005-11-18 00:02:14 +00002925
Phillip Lougherd6577802012-10-04 19:14:33 +01002926 /*
2927 * Look-up inode in hash table, if it already exists we have a
2928 * hard-link, so increment the nlink count and return it.
2929 * Don't do the look-up for directories because we don't hard-link
2930 * directories.
2931 */
2932 if ((buf->st_mode & S_IFMT) != S_IFDIR) {
2933 for(inode = inode_info[ino_hash]; inode; inode = inode->next) {
2934 if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) {
2935 inode->nlink ++;
2936 return inode;
2937 }
plougher1f413c82005-11-18 00:02:14 +00002938 }
plougher1f413c82005-11-18 00:02:14 +00002939 }
2940
plougher288f6852010-12-16 05:22:55 +00002941 inode = malloc(sizeof(struct inode_info));
2942 if(inode == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002943 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00002944
2945 memcpy(&inode->buf, buf, sizeof(struct stat));
plougher5507dd92006-11-06 00:43:10 +00002946 inode->read = FALSE;
ploughera326c182009-08-29 05:41:45 +00002947 inode->root_entry = FALSE;
Phillip Lougher81204c22012-07-25 03:30:30 +01002948 inode->pseudo_file = pseudo;
2949 inode->pseudo_id = id;
plougher1f413c82005-11-18 00:02:14 +00002950 inode->inode = SQUASHFS_INVALID_BLK;
2951 inode->nlink = 1;
Phillip Lougher539c2b12012-07-30 20:14:52 +01002952 inode->inode_number = 0;
plougherdc86c3c2007-12-05 02:15:10 +00002953
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002954 /*
2955 * Copy filesystem wide defaults into inode, these filesystem
2956 * wide defaults may be altered on an individual inode basis by
2957 * user specified actions
2958 *
2959 */
2960 inode->no_fragments = no_fragments;
2961 inode->always_use_fragments = always_use_fragments;
Phillip Lougher63f531f2011-09-10 04:03:32 +01002962 inode->noD = noD;
2963 inode->noF = noF;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002964
plougherdc86c3c2007-12-05 02:15:10 +00002965 if((buf->st_mode & S_IFMT) == S_IFREG)
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002966 progress_bar_size((buf->st_size + block_size - 1) >> block_log);
plougherdc86c3c2007-12-05 02:15:10 +00002967
Phillip Lougherd6577802012-10-04 19:14:33 +01002968 inode->next = inode_info[ino_hash];
2969 inode_info[ino_hash] = inode;
plougher1f413c82005-11-18 00:02:14 +00002970
2971 return inode;
2972}
2973
2974
Phillip Lougher81204c22012-07-25 03:30:30 +01002975inline struct inode_info *lookup_inode(struct stat *buf)
2976{
2977 return lookup_inode2(buf, 0, 0);
2978}
2979
2980
Phillip Lougher539c2b12012-07-30 20:14:52 +01002981inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this)
2982{
2983 if (inode->inode_number == 0)
2984 inode->inode_number = use_this ? : inode_no ++;
2985}
2986
2987
Phillip Lougher494479f2012-02-03 15:45:41 +00002988inline struct dir_ent *create_dir_entry(char *name, char *source_name,
2989 char *nonstandard_pathname, struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00002990{
Phillip Lougher494479f2012-02-03 15:45:41 +00002991 struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent));
2992 if(dir_ent == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002993 MEM_ERROR();
Phillip Lougher494479f2012-02-03 15:45:41 +00002994
2995 dir_ent->name = name;
2996 dir_ent->source_name = source_name;
2997 dir_ent->nonstandard_pathname = nonstandard_pathname;
2998 dir_ent->our_dir = dir;
Phillip Lougherbf338362012-08-22 05:24:36 +01002999 dir_ent->next = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003000
3001 return dir_ent;
3002}
3003
3004
3005inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir,
3006 struct inode_info *inode_info)
3007{
3008 struct dir_info *dir = dir_ent->our_dir;
3009
plougher1f413c82005-11-18 00:02:14 +00003010 if(sub_dir)
Phillip Lougher494479f2012-02-03 15:45:41 +00003011 sub_dir->dir_ent = dir_ent;
3012 dir_ent->inode = inode_info;
3013 dir_ent->dir = sub_dir;
3014
Phillip Lougherbf338362012-08-22 05:24:36 +01003015 dir_ent->next = dir->list;
3016 dir->list = dir_ent;
3017 dir->count++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003018}
3019
3020
3021inline void add_dir_entry2(char *name, char *source_name,
3022 char *nonstandard_pathname, struct dir_info *sub_dir,
3023 struct inode_info *inode_info, struct dir_info *dir)
3024{
3025 struct dir_ent *dir_ent = create_dir_entry(name, source_name,
3026 nonstandard_pathname, dir);
3027
3028
3029 add_dir_entry(dir_ent, sub_dir, inode_info);
plougher1f413c82005-11-18 00:02:14 +00003030}
3031
3032
Phillip Lougher10a4b572012-02-18 23:26:10 +00003033inline void free_dir_entry(struct dir_ent *dir_ent)
3034{
Phillip Loughereb92b192012-10-01 03:39:00 +01003035 if(dir_ent->name)
Phillip Lougher10a4b572012-02-18 23:26:10 +00003036 free(dir_ent->name);
3037
Phillip Loughereb92b192012-10-01 03:39:00 +01003038 if(dir_ent->source_name)
Phillip Lougher10a4b572012-02-18 23:26:10 +00003039 free(dir_ent->source_name);
3040
3041 free(dir_ent);
3042}
3043
3044
Phillip Lougherad0f9212011-12-31 00:55:51 +00003045inline void add_excluded(struct dir_info *dir)
3046{
3047 dir->excluded ++;
3048}
3049
3050
Phillip Lougherabc3b492012-07-29 02:53:35 +01003051void dir_scan(squashfs_inode *inode, char *pathname,
Phillip Lougherbae0e422014-01-19 23:42:11 +00003052 struct dir_ent *(_readdir)(struct dir_info *), int progress)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003053{
3054 struct stat buf;
3055 struct dir_info *dir_info = dir_scan1(pathname, "", paths, _readdir, 1);
3056 struct dir_ent *dir_ent;
3057
3058 if(dir_info == NULL)
3059 return;
3060
Phillip Lougherf3d0f9c2012-11-14 05:36:58 +00003061 /*
3062 * Process most actions and any pseudo files
3063 */
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00003064 if(actions() || get_pseudo())
3065 dir_scan2(dir_info, get_pseudo());
Phillip Lougherf3d0f9c2012-11-14 05:36:58 +00003066
Phillip Loughere92cb512012-11-15 02:22:28 +00003067 /*
3068 * Process move actions
3069 */
3070 if(move_actions()) {
3071 dir_scan3(dir_info, dir_info);
3072 do_move_actions();
3073 }
3074
Phillip Lougher71ae14e2012-11-15 02:27:50 +00003075 /*
3076 * Process empty actions
3077 */
3078 if(empty_actions())
3079 dir_scan4(dir_info);
3080
Phillip Loughereb69dad2012-11-15 03:34:02 +00003081 /*
3082 * Sort directories and compute the inode numbers
3083 */
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003084 dir_scan5(dir_info);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003085
Phillip Lougherd0fe1d52012-10-09 03:34:34 +01003086 dir_ent = create_dir_entry("", NULL, pathname,
Phillip Lougher1eae83d2012-09-26 02:12:45 +01003087 scan1_opendir("", "", 0));
Phillip Lougherabc3b492012-07-29 02:53:35 +01003088
3089 if(pathname[0] == '\0') {
3090 /*
3091 * dummy top level directory, if multiple sources specified on
3092 * command line
3093 */
3094 memset(&buf, 0, sizeof(buf));
3095 buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR;
3096 buf.st_uid = getuid();
3097 buf.st_gid = getgid();
3098 buf.st_mtime = time(NULL);
3099 buf.st_dev = 0;
3100 buf.st_ino = 0;
3101 dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0);
3102 } else {
Phillip Lougher38a35ec2012-11-30 04:31:35 +00003103 if(lstat(pathname, &buf) == -1)
3104 /* source directory has disappeared? */
Phillip Lougherfe58b492012-12-15 01:27:07 +00003105 BAD_ERROR("Cannot stat source directory %s because %s\n",
Phillip Lougherabc3b492012-07-29 02:53:35 +01003106 pathname, strerror(errno));
Phillip Lougherabc3b492012-07-29 02:53:35 +01003107 dir_ent->inode = lookup_inode(&buf);
3108 }
3109
Phillip Lougher539c2b12012-07-30 20:14:52 +01003110 alloc_inode_no(dir_ent->inode, root_inode_number);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003111 dir_ent->dir = dir_info;
3112 dir_info->dir_ent = dir_ent;
3113
Andy Lutomirski3f31dcf2012-09-21 18:08:08 -07003114 eval_actions(dir_ent);
3115
Phillip Lougher19b877d2013-02-23 11:15:38 +00003116 if(sorted)
3117 generate_file_priorities(dir_info, 0,
Phillip Lougherabc3b492012-07-29 02:53:35 +01003118 &dir_info->dir_ent->inode->buf);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003119 queue_put(to_reader, dir_info);
3120 if(sorted)
3121 sort_files_and_write(dir_info);
Phillip Lougherbae0e422014-01-19 23:42:11 +00003122 set_progressbar_state(progress);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003123 dir_scan6(inode, dir_info);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003124 dir_ent->inode->inode = *inode;
3125 dir_ent->inode->type = SQUASHFS_DIR_TYPE;
plougher1f413c82005-11-18 00:02:14 +00003126}
3127
3128
Phillip Lougherabc3b492012-07-29 02:53:35 +01003129/*
3130 * dir_scan1 routines...
Phillip Loughere8630be2012-11-11 02:24:24 +00003131 * These scan the source directories into memory for processing.
3132 * Exclude actions are processed here (in contrast to the other actions)
3133 * because they affect what is scanned.
Phillip Lougherabc3b492012-07-29 02:53:35 +01003134 */
Phillip Lougherb38c1722012-02-09 23:26:19 +00003135struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth)
plougher1f413c82005-11-18 00:02:14 +00003136{
plougher1f413c82005-11-18 00:02:14 +00003137 struct dir_info *dir;
3138
plougher6da792b2010-12-16 05:25:39 +00003139 dir = malloc(sizeof(struct dir_info));
3140 if(dir == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003141 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00003142
Phillip Lougher04ef2442013-01-09 06:03:50 +00003143 if(pathname[0] != '\0') {
3144 dir->linuxdir = opendir(pathname);
3145 if(dir->linuxdir == NULL) {
3146 free(dir);
3147 return NULL;
3148 }
plougher1f413c82005-11-18 00:02:14 +00003149 }
Phillip Lougher04ef2442013-01-09 06:03:50 +00003150
Phillip Lougher3cb5d6c2013-01-09 05:50:29 +00003151 dir->pathname = strdup(pathname);
3152 dir->subpath = strdup(subpath);
Phillip Lougherded70fa2011-12-25 02:14:58 +00003153 dir->count = 0;
3154 dir->directory_count = 0;
plougher1f413c82005-11-18 00:02:14 +00003155 dir->dir_is_ldir = TRUE;
3156 dir->list = NULL;
Phillip Lougher0b5a1242011-12-25 03:22:42 +00003157 dir->depth = depth;
Phillip Lougherad0f9212011-12-31 00:55:51 +00003158 dir->excluded = 0;
plougher1f413c82005-11-18 00:02:14 +00003159
3160 return dir;
3161}
3162
3163
Phillip Lougher494479f2012-02-03 15:45:41 +00003164struct dir_ent *scan1_encomp_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003165{
plougher1f413c82005-11-18 00:02:14 +00003166 static int index = 0;
3167
plougher11266ba2010-12-16 05:34:30 +00003168 if(dir->count < old_root_entries) {
3169 int i;
3170
plougher1f413c82005-11-18 00:02:14 +00003171 for(i = 0; i < old_root_entries; i++) {
ploughera326c182009-08-29 05:41:45 +00003172 if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
plougher1f413c82005-11-18 00:02:14 +00003173 dir->directory_count ++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003174 add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
ploughera326c182009-08-29 05:41:45 +00003175 &old_root_entry[i].inode, dir);
plougher1f413c82005-11-18 00:02:14 +00003176 }
plougher11266ba2010-12-16 05:34:30 +00003177 }
plougher1f413c82005-11-18 00:02:14 +00003178
3179 while(index < source) {
Phillip Lougherc73ed322012-10-01 03:25:41 +01003180 char *basename = NULL;
3181 char *dir_name = getbase(source_path[index]);
Phillip Lougherbf338362012-08-22 05:24:36 +01003182 int pass = 1, res;
plougher89fe2c72010-12-16 05:31:34 +00003183
Phillip Lougherc73ed322012-10-01 03:25:41 +01003184 if(dir_name == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003185 ERROR_START("Bad source directory %s",
plougher110799c2009-03-30 01:50:40 +00003186 source_path[index]);
Phillip Lougher85776df2014-01-31 03:10:46 +00003187 ERROR_EXIT(" - skipping ...\n");
plougher1f413c82005-11-18 00:02:14 +00003188 index ++;
3189 continue;
3190 }
Phillip Lougherc73ed322012-10-01 03:25:41 +01003191 dir_name = strdup(dir_name);
plougher1f413c82005-11-18 00:02:14 +00003192 for(;;) {
Phillip Lougherbf338362012-08-22 05:24:36 +01003193 struct dir_ent *dir_ent = dir->list;
3194
3195 for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3196 dir_ent = dir_ent->next);
3197 if(dir_ent == NULL)
plougher1f413c82005-11-18 00:02:14 +00003198 break;
plougher50b31762009-03-31 04:14:46 +00003199 ERROR("Source directory entry %s already used! - trying"
3200 " ", dir_name);
Phillip Lougherc73ed322012-10-01 03:25:41 +01003201 if(pass == 1)
3202 basename = dir_name;
3203 else
Phillip Lougherb38c1722012-02-09 23:26:19 +00003204 free(dir_name);
Phillip Lougher494479f2012-02-03 15:45:41 +00003205 res = asprintf(&dir_name, "%s_%d", basename, pass++);
3206 if(res == -1)
3207 BAD_ERROR("asprintf failed in "
3208 "scan1_encomp_readdir\n");
plougher1f413c82005-11-18 00:02:14 +00003209 ERROR("%s\n", dir_name);
3210 }
Phillip Lougherb38c1722012-02-09 23:26:19 +00003211 return create_dir_entry(dir_name, basename,
3212 source_path[index ++], dir);
plougher1f413c82005-11-18 00:02:14 +00003213 }
Phillip Lougher494479f2012-02-03 15:45:41 +00003214 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003215}
3216
3217
Phillip Lougher494479f2012-02-03 15:45:41 +00003218struct dir_ent *scan1_single_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003219{
3220 struct dirent *d_name;
plougher4925e172010-12-16 05:45:54 +00003221 int i;
plougher1f413c82005-11-18 00:02:14 +00003222
plougher4925e172010-12-16 05:45:54 +00003223 if(dir->count < old_root_entries) {
plougher1f413c82005-11-18 00:02:14 +00003224 for(i = 0; i < old_root_entries; i++) {
ploughera326c182009-08-29 05:41:45 +00003225 if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
plougher1f413c82005-11-18 00:02:14 +00003226 dir->directory_count ++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003227 add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
ploughera326c182009-08-29 05:41:45 +00003228 &old_root_entry[i].inode, dir);
plougher1f413c82005-11-18 00:02:14 +00003229 }
plougher4925e172010-12-16 05:45:54 +00003230 }
plougher1f413c82005-11-18 00:02:14 +00003231
3232 if((d_name = readdir(dir->linuxdir)) != NULL) {
Phillip Lougherc73ed322012-10-01 03:25:41 +01003233 char *basename = NULL;
3234 char *dir_name = strdup(d_name->d_name);
Phillip Lougher494479f2012-02-03 15:45:41 +00003235 int pass = 1, res;
plougher4925e172010-12-16 05:45:54 +00003236
plougher1f413c82005-11-18 00:02:14 +00003237 for(;;) {
Phillip Lougherbf338362012-08-22 05:24:36 +01003238 struct dir_ent *dir_ent = dir->list;
3239
3240 for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3241 dir_ent = dir_ent->next);
3242 if(dir_ent == NULL)
plougher1f413c82005-11-18 00:02:14 +00003243 break;
plougher50b31762009-03-31 04:14:46 +00003244 ERROR("Source directory entry %s already used! - trying"
3245 " ", dir_name);
Phillip Lougherc73ed322012-10-01 03:25:41 +01003246 if (pass == 1)
3247 basename = dir_name;
3248 else
Phillip Lougher494479f2012-02-03 15:45:41 +00003249 free(dir_name);
3250 res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++);
3251 if(res == -1)
3252 BAD_ERROR("asprintf failed in "
3253 "scan1_single_readdir\n");
plougher1f413c82005-11-18 00:02:14 +00003254 ERROR("%s\n", dir_name);
3255 }
Phillip Lougher494479f2012-02-03 15:45:41 +00003256 return create_dir_entry(dir_name, basename, NULL, dir);
plougher1f413c82005-11-18 00:02:14 +00003257 }
3258
Phillip Lougher494479f2012-02-03 15:45:41 +00003259 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003260}
3261
3262
Phillip Lougher494479f2012-02-03 15:45:41 +00003263struct dir_ent *scan1_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003264{
plougher480d9bf2010-12-18 02:28:39 +00003265 struct dirent *d_name = readdir(dir->linuxdir);
plougher1f413c82005-11-18 00:02:14 +00003266
Phillip Lougher494479f2012-02-03 15:45:41 +00003267 return d_name ?
3268 create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) :
3269 NULL;
plougher1f413c82005-11-18 00:02:14 +00003270}
3271
3272
plougher1f413c82005-11-18 00:02:14 +00003273void scan1_freedir(struct dir_info *dir)
3274{
plougherfbed12b2006-02-07 12:45:53 +00003275 if(dir->pathname[0] != '\0')
3276 closedir(dir->linuxdir);
plougher1f413c82005-11-18 00:02:14 +00003277}
3278
3279
Phillip Lougherb38c1722012-02-09 23:26:19 +00003280struct dir_info *dir_scan1(char *filename, char *subpath,
3281 struct pathnames *paths,
Phillip Lougher494479f2012-02-03 15:45:41 +00003282 struct dir_ent *(_readdir)(struct dir_info *), int depth)
plougher1f413c82005-11-18 00:02:14 +00003283{
Phillip Lougherb38c1722012-02-09 23:26:19 +00003284 struct dir_info *dir = scan1_opendir(filename, subpath, depth);
Phillip Lougher494479f2012-02-03 15:45:41 +00003285 struct dir_ent *dir_ent;
plougher1f413c82005-11-18 00:02:14 +00003286
plougher360b6e42010-12-18 02:40:28 +00003287 if(dir == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003288 ERROR_START("Could not open %s", filename);
3289 ERROR_EXIT(", skipping...\n");
Phillip Lougher87db5672013-01-09 05:40:46 +00003290 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003291 }
3292
Phillip Lougher494479f2012-02-03 15:45:41 +00003293 while((dir_ent = _readdir(dir))) {
plougher360b6e42010-12-18 02:40:28 +00003294 struct dir_info *sub_dir;
3295 struct stat buf;
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00003296 struct pathnames *new = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003297 char *filename = pathname(dir_ent);
Phillip Lougher651d68d2013-01-13 05:38:13 +00003298 char *subpath = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003299 char *dir_name = dir_ent->name;
plougher1f413c82005-11-18 00:02:14 +00003300
Phillip Lougher494479f2012-02-03 15:45:41 +00003301 if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) {
Phillip Lougher10a4b572012-02-18 23:26:10 +00003302 free_dir_entry(dir_ent);
plougher1f413c82005-11-18 00:02:14 +00003303 continue;
Phillip Lougher494479f2012-02-03 15:45:41 +00003304 }
plougher1f413c82005-11-18 00:02:14 +00003305
3306 if(lstat(filename, &buf) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003307 ERROR_START("Cannot stat dir/file %s because %s",
plougher110799c2009-03-30 01:50:40 +00003308 filename, strerror(errno));
Phillip Lougher85776df2014-01-31 03:10:46 +00003309 ERROR_EXIT(", ignoring\n");
Phillip Lougher10a4b572012-02-18 23:26:10 +00003310 free_dir_entry(dir_ent);
plougher1f413c82005-11-18 00:02:14 +00003311 continue;
3312 }
plougher29e37092007-04-15 01:24:51 +00003313
3314 if((buf.st_mode & S_IFMT) != S_IFREG &&
Phillip Loughere4ff36a2012-11-19 01:34:47 +00003315 (buf.st_mode & S_IFMT) != S_IFDIR &&
3316 (buf.st_mode & S_IFMT) != S_IFLNK &&
3317 (buf.st_mode & S_IFMT) != S_IFCHR &&
3318 (buf.st_mode & S_IFMT) != S_IFBLK &&
3319 (buf.st_mode & S_IFMT) != S_IFIFO &&
3320 (buf.st_mode & S_IFMT) != S_IFSOCK) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003321 ERROR_START("File %s has unrecognised filetype %d",
3322 filename, buf.st_mode & S_IFMT);
3323 ERROR_EXIT(", ignoring\n");
Phillip Lougher10a4b572012-02-18 23:26:10 +00003324 free_dir_entry(dir_ent);
plougher29e37092007-04-15 01:24:51 +00003325 continue;
3326 }
3327
Phillip Lougherad0f9212011-12-31 00:55:51 +00003328 if((old_exclude && old_excluded(filename, &buf)) ||
Phillip Lougher651d68d2013-01-13 05:38:13 +00003329 (!old_exclude && excluded(dir_name, paths, &new))) {
Phillip Lougherad0f9212011-12-31 00:55:51 +00003330 add_excluded(dir);
Phillip Lougher10a4b572012-02-18 23:26:10 +00003331 free_dir_entry(dir_ent);
Phillip Lougher10d8de02011-08-29 00:14:48 +01003332 continue;
Phillip Lougherad0f9212011-12-31 00:55:51 +00003333 }
plougher1f413c82005-11-18 00:02:14 +00003334
Phillip Lougher651d68d2013-01-13 05:38:13 +00003335 if(exclude_actions()) {
3336 subpath = subpathname(dir_ent);
3337
3338 if(eval_exclude_actions(dir_name, filename, subpath,
3339 &buf, depth)) {
3340 add_excluded(dir);
3341 free_dir_entry(dir_ent);
3342 continue;
3343 }
3344 }
3345
plougher1f413c82005-11-18 00:02:14 +00003346 if((buf.st_mode & S_IFMT) == S_IFDIR) {
Phillip Lougher651d68d2013-01-13 05:38:13 +00003347 if(subpath == NULL)
3348 subpath = subpathname(dir_ent);
3349
Phillip Lougherb38c1722012-02-09 23:26:19 +00003350 sub_dir = dir_scan1(filename, subpath, new,
3351 scan1_readdir, depth + 1);
Phillip Lougher494479f2012-02-03 15:45:41 +00003352 if(sub_dir == NULL) {
Phillip Lougher10a4b572012-02-18 23:26:10 +00003353 free_dir_entry(dir_ent);
Phillip Lougher205c1e02013-01-07 01:37:56 +00003354 free(new);
plougher1f413c82005-11-18 00:02:14 +00003355 continue;
Phillip Lougher494479f2012-02-03 15:45:41 +00003356 }
Phillip Lougher6a78a2d2011-12-28 03:54:19 +00003357
plougher1f413c82005-11-18 00:02:14 +00003358 dir->directory_count ++;
3359 } else
3360 sub_dir = NULL;
3361
Phillip Lougher494479f2012-02-03 15:45:41 +00003362 add_dir_entry(dir_ent, sub_dir, lookup_inode(&buf));
Phillip Lougher205c1e02013-01-07 01:37:56 +00003363 free(new);
plougher1f413c82005-11-18 00:02:14 +00003364 }
3365
3366 scan1_freedir(dir);
plougher1f413c82005-11-18 00:02:14 +00003367
plougher1f413c82005-11-18 00:02:14 +00003368 return dir;
3369}
3370
plougher2ea89142008-03-11 01:34:19 +00003371
Phillip Lougherabc3b492012-07-29 02:53:35 +01003372/*
3373 * dir_scan2 routines...
Phillip Lougher539c2b12012-07-30 20:14:52 +01003374 * This processes most actions and any pseudo files
Phillip Lougherabc3b492012-07-29 02:53:35 +01003375 */
Phillip Lougherbf338362012-08-22 05:24:36 +01003376struct dir_ent *scan2_readdir(struct dir_info *dir, struct dir_ent *dir_ent)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003377{
Phillip Lougherbf338362012-08-22 05:24:36 +01003378 if (dir_ent == NULL)
3379 dir_ent = dir->list;
3380 else
3381 dir_ent = dir_ent->next;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003382
Phillip Lougherbf338362012-08-22 05:24:36 +01003383 for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next);
3384
3385 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003386}
3387
3388
3389struct dir_ent *scan2_lookup(struct dir_info *dir, char *name)
3390{
Phillip Lougherbf338362012-08-22 05:24:36 +01003391 struct dir_ent *dir_ent = dir->list;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003392
Phillip Lougherbf338362012-08-22 05:24:36 +01003393 for(; dir_ent && strcmp(dir_ent->name, name) != 0;
3394 dir_ent = dir_ent->next);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003395
Phillip Lougherbf338362012-08-22 05:24:36 +01003396 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003397}
3398
3399
Phillip Lougher24eeb772012-10-13 01:56:24 +01003400void dir_scan2(struct dir_info *dir, struct pseudo *pseudo)
plougher43244f22009-04-05 02:04:51 +00003401{
Phillip Lougherbf338362012-08-22 05:24:36 +01003402 struct dir_ent *dir_ent = NULL;
plougher43244f22009-04-05 02:04:51 +00003403 struct pseudo_entry *pseudo_ent;
3404 struct stat buf;
plougher82ab2332009-04-21 00:21:21 +00003405 static int pseudo_ino = 1;
plougher43244f22009-04-05 02:04:51 +00003406
Phillip Lougherbf338362012-08-22 05:24:36 +01003407 while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
plougher43244f22009-04-05 02:04:51 +00003408 struct inode_info *inode_info = dir_ent->inode;
3409 struct stat *buf = &inode_info->buf;
3410 char *name = dir_ent->name;
3411
Phillip Lougher89d757c2011-09-20 00:33:19 +01003412 eval_actions(dir_ent);
3413
plougher43244f22009-04-05 02:04:51 +00003414 if((buf->st_mode & S_IFMT) == S_IFDIR)
Phillip Lougher24eeb772012-10-13 01:56:24 +01003415 dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo));
plougher43244f22009-04-05 02:04:51 +00003416 }
3417
3418 while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) {
3419 dir_ent = scan2_lookup(dir, pseudo_ent->name);
plougherdcd66c52010-09-17 03:44:17 +00003420 if(pseudo_ent->dev->type == 'm') {
plougherb34f9f62009-04-26 02:08:42 +00003421 struct stat *buf;
3422 if(dir_ent == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003423 ERROR_START("Pseudo modify file \"%s\" does "
3424 "not exist in source filesystem.",
plougherb34f9f62009-04-26 02:08:42 +00003425 pseudo_ent->pathname);
Phillip Lougher85776df2014-01-31 03:10:46 +00003426 ERROR_EXIT(" Ignoring.\n");
plougherb34f9f62009-04-26 02:08:42 +00003427 continue;
3428 }
ploughera326c182009-08-29 05:41:45 +00003429 if(dir_ent->inode->root_entry) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003430 ERROR_START("Pseudo modify file \"%s\" is a "
3431 "pre-existing file in the filesystem "
3432 "being appended to. It cannot be "\
3433 "modified.", pseudo_ent->pathname);
3434 ERROR_EXIT(" Ignoring.\n");
plougherb34f9f62009-04-26 02:08:42 +00003435 continue;
3436 }
3437 buf = &dir_ent->inode->buf;
3438 buf->st_mode = (buf->st_mode & S_IFMT) |
3439 pseudo_ent->dev->mode;
3440 buf->st_uid = pseudo_ent->dev->uid;
3441 buf->st_gid = pseudo_ent->dev->gid;
3442 continue;
3443 }
3444
plougher43244f22009-04-05 02:04:51 +00003445 if(dir_ent) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003446 if(dir_ent->inode->root_entry) {
3447 ERROR_START("Pseudo file \"%s\" is a "
3448 "pre-existing file in the filesystem "
3449 "being appended to.",
plougherf0dc2382010-05-01 23:55:06 +00003450 pseudo_ent->pathname);
Phillip Lougher85776df2014-01-31 03:10:46 +00003451 ERROR_EXIT(" Ignoring.\n");
3452 } else {
3453 ERROR_START("Pseudo file \"%s\" exists in "
3454 "source filesystem \"%s\".",
plougherf0dc2382010-05-01 23:55:06 +00003455 pseudo_ent->pathname,
Phillip Lougher494479f2012-02-03 15:45:41 +00003456 pathname(dir_ent));
Phillip Lougher85776df2014-01-31 03:10:46 +00003457 ERROR_EXIT("\nIgnoring, exclude it (-e/-ef) to "
3458 "override.\n");
3459 }
plougher43244f22009-04-05 02:04:51 +00003460 continue;
3461 }
3462
plougher43244f22009-04-05 02:04:51 +00003463 memset(&buf, 0, sizeof(buf));
3464 buf.st_mode = pseudo_ent->dev->mode;
3465 buf.st_uid = pseudo_ent->dev->uid;
3466 buf.st_gid = pseudo_ent->dev->gid;
3467 buf.st_rdev = makedev(pseudo_ent->dev->major,
3468 pseudo_ent->dev->minor);
plougher7e58f4d2009-04-05 12:06:19 +00003469 buf.st_mtime = time(NULL);
plougher1a3fbf22009-04-05 12:04:16 +00003470 buf.st_ino = pseudo_ino ++;
plougher43244f22009-04-05 02:04:51 +00003471
Phillip Lougher5d579292012-10-13 01:37:16 +01003472 if(pseudo_ent->dev->type == 'd') {
3473 struct dir_ent *dir_ent =
3474 create_dir_entry(pseudo_ent->name, NULL,
3475 pseudo_ent->pathname, dir);
3476 char *subpath = strdup(subpathname(dir_ent));
3477 struct dir_info *sub_dir = scan1_opendir("", subpath,
Phillip Lougher24eeb772012-10-13 01:56:24 +01003478 dir->depth + 1);
Phillip Lougher5d579292012-10-13 01:37:16 +01003479 if(sub_dir == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003480 ERROR_START("Could not create pseudo directory "
3481 "\"%s\"", pseudo_ent->pathname);
3482 ERROR_EXIT(", skipping...\n");
Phillip Lougher5d579292012-10-13 01:37:16 +01003483 free(subpath);
3484 pseudo_ino --;
3485 continue;
3486 }
Phillip Lougher24eeb772012-10-13 01:56:24 +01003487 dir_scan2(sub_dir, pseudo_ent->pseudo);
Phillip Lougher5d579292012-10-13 01:37:16 +01003488 dir->directory_count ++;
3489 add_dir_entry(dir_ent, sub_dir,
3490 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0));
3491 } else if(pseudo_ent->dev->type == 'f') {
plougher00d08172009-09-03 10:17:44 +00003492#ifdef USE_TMP_FILE
plougher4ab7e512009-05-05 02:35:58 +00003493 struct stat buf2;
3494 int res = stat(pseudo_ent->dev->filename, &buf2);
3495 if(res == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003496 ERROR_START("Stat on pseudo file \"%s\" failed"
3497 pseudo_ent->pathname);
3498 ERROR_EXIT(", skipping...\n");
Phillip Lougher5d579292012-10-13 01:37:16 +01003499 pseudo_ino --;
plougher4ab7e512009-05-05 02:35:58 +00003500 continue;
3501 }
3502 buf.st_size = buf2.st_size;
Phillip Lougher494479f2012-02-03 15:45:41 +00003503 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003504 pseudo_ent->dev->filename, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003505 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir);
plougher00d08172009-09-03 10:17:44 +00003506#else
Phillip Lougher494479f2012-02-03 15:45:41 +00003507 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003508 pseudo_ent->pathname, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003509 lookup_inode2(&buf, PSEUDO_FILE_PROCESS,
3510 pseudo_ent->dev->pseudo_id), dir);
plougher00d08172009-09-03 10:17:44 +00003511#endif
plougherb85e9ad2010-05-02 01:46:12 +00003512 } else {
Phillip Lougher494479f2012-02-03 15:45:41 +00003513 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003514 pseudo_ent->pathname, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003515 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir);
plougherb85e9ad2010-05-02 01:46:12 +00003516 }
plougher43244f22009-04-05 02:04:51 +00003517 }
plougher43244f22009-04-05 02:04:51 +00003518}
3519
3520
Phillip Lougherabc3b492012-07-29 02:53:35 +01003521/*
3522 * dir_scan3 routines...
Phillip Lougher23d83622012-10-14 02:35:22 +01003523 * This processes the move action
3524 */
3525void dir_scan3(struct dir_info *root, struct dir_info *dir)
3526{
3527 struct dir_ent *dir_ent = NULL;
3528
3529 while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
3530
3531 eval_move_actions(root, dir_ent);
3532
3533 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
3534 dir_scan3(root, dir_ent->dir);
3535 }
3536}
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003537
3538
Phillip Lougher23d83622012-10-14 02:35:22 +01003539/*
3540 * dir_scan4 routines...
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003541 * This processes the empty action. This action has to be processed after
3542 * all other actions because the previous exclude and move actions and the
3543 * pseudo actions affect whether a directory is empty
3544 */
3545void dir_scan4(struct dir_info *dir)
3546{
3547 struct dir_ent *dir_ent = dir->list, *prev = NULL;
3548
3549 while(dir_ent) {
Phillip Lougher15e8f042012-11-16 00:57:39 +00003550 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) {
3551 dir_scan4(dir_ent->dir);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003552
Phillip Lougher15e8f042012-11-16 00:57:39 +00003553 if(eval_empty_actions(dir_ent)) {
3554 struct dir_ent *tmp = dir_ent;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003555
Phillip Lougher15e8f042012-11-16 00:57:39 +00003556 /*
3557 * delete sub-directory, this is by definition
3558 * empty
3559 */
3560 free(dir_ent->dir->pathname);
3561 free(dir_ent->dir->subpath);
3562 free(dir_ent->dir);
3563
3564 /* remove dir_ent from list */
3565 dir_ent = dir_ent->next;
3566 if(prev)
3567 prev->next = dir_ent;
3568 else
3569 dir->list = dir_ent;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003570
Phillip Lougher15e8f042012-11-16 00:57:39 +00003571 /* free it */
3572 free_dir_entry(tmp);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003573
Phillip Lougher15e8f042012-11-16 00:57:39 +00003574 /* update counts */
3575 dir->directory_count --;
3576 dir->count --;
3577 add_excluded(dir);
3578 continue;
3579 }
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003580 }
Phillip Lougher15e8f042012-11-16 00:57:39 +00003581
3582 prev = dir_ent;
3583 dir_ent = dir_ent->next;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003584 }
3585}
3586
3587
3588/*
3589 * dir_scan5 routines...
Phillip Lougher539c2b12012-07-30 20:14:52 +01003590 * This sorts every directory and computes the inode numbers
3591 */
Phillip Lougher539c2b12012-07-30 20:14:52 +01003592
Phillip Lougher242242e2012-08-24 04:15:36 +01003593/*
3594 * Bottom up linked list merge sort.
3595 *
3596 * Qsort and other O(n log n) algorithms work well with arrays but not
3597 * linked lists. Merge sort another O(n log n) sort algorithm on the other hand
3598 * is not ideal for arrays (as it needs an additonal n storage locations
3599 * as sorting is not done in place), but it is ideal for linked lists because
3600 * it doesn't require any extra storage,
3601 */
Phillip Lougher539c2b12012-07-30 20:14:52 +01003602void sort_directory(struct dir_info *dir)
3603{
Phillip Lougher242242e2012-08-24 04:15:36 +01003604 struct dir_ent *cur, *l1, *l2, *next;
3605 int len1, len2, stride = 1;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003606
Phillip Lougher242242e2012-08-24 04:15:36 +01003607 if(dir->count < 2)
Phillip Lougherbf338362012-08-22 05:24:36 +01003608 return;
3609
Phillip Lougher242242e2012-08-24 04:15:36 +01003610 /*
3611 * We can consider our linked-list to be made up of stride length
3612 * sublists. Eacn iteration around this loop merges adjacent
3613 * stride length sublists into larger 2*stride sublists. We stop
3614 * when stride becomes equal to the entire list.
3615 *
3616 * Initially stride = 1 (by definition a sublist of 1 is sorted), and
3617 * these 1 element sublists are merged into 2 element sublists, which
3618 * are then merged into 4 element sublists and so on.
3619 */
3620 do {
3621 l2 = dir->list; /* head of current linked list */
3622 cur = NULL; /* empty output list */
Phillip Lougherbf338362012-08-22 05:24:36 +01003623
Phillip Lougher242242e2012-08-24 04:15:36 +01003624 /*
3625 * Iterate through the linked list, merging adjacent sublists.
3626 * On each interation l2 points to the next sublist pair to be
3627 * merged (if there's only one sublist left this is simply added
3628 * to the output list)
3629 */
3630 while(l2) {
3631 l1 = l2;
3632 for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next);
3633 len2 = stride;
Phillip Lougherbf338362012-08-22 05:24:36 +01003634
Phillip Lougher242242e2012-08-24 04:15:36 +01003635 /*
3636 * l1 points to first sublist.
3637 * l2 points to second sublist.
3638 * Merge them onto the output list
3639 */
3640 while(len1 && l2 && len2) {
3641 if(strcmp(l1->name, l2->name) <= 0) {
3642 next = l1;
3643 l1 = l1->next;
3644 len1 --;
3645 } else {
3646 next = l2;
3647 l2 = l2->next;
3648 len2 --;
3649 }
3650
3651 if(cur) {
3652 cur->next = next;
3653 cur = next;
3654 } else
3655 dir->list = cur = next;
3656 }
3657 /*
3658 * One sublist is now empty, copy the other one onto the
3659 * output list
3660 */
3661 for(; len1; len1 --, l1 = l1->next) {
3662 if(cur) {
3663 cur->next = l1;
3664 cur = l1;
3665 } else
3666 dir->list = cur = l1;
3667 }
3668 for(; l2 && len2; len2 --, l2 = l2->next) {
3669 if(cur) {
3670 cur->next = l2;
3671 cur = l2;
3672 } else
3673 dir->list = cur = l2;
3674 }
3675 }
3676 cur->next = NULL;
3677 stride = stride << 1;
3678 } while(stride < dir->count);
Phillip Lougher539c2b12012-07-30 20:14:52 +01003679}
3680
3681
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003682void dir_scan5(struct dir_info *dir)
Phillip Lougher539c2b12012-07-30 20:14:52 +01003683{
Phillip Lougher23d83622012-10-14 02:35:22 +01003684 struct dir_ent *dir_ent;
3685 unsigned int byte_count = 0;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003686
3687 sort_directory(dir);
3688
Phillip Lougher23d83622012-10-14 02:35:22 +01003689 for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) {
3690 byte_count += strlen(dir_ent->name) +
3691 sizeof(struct squashfs_dir_entry);
3692
3693 if(dir_ent->inode->root_entry)
3694 continue;
3695
Phillip Lougher539c2b12012-07-30 20:14:52 +01003696 alloc_inode_no(dir_ent->inode, 0);
Phillip Lougher23d83622012-10-14 02:35:22 +01003697
Phillip Lougher539c2b12012-07-30 20:14:52 +01003698 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
Phillip Loughere69b5882012-11-19 01:31:09 +00003699 dir_scan5(dir_ent->dir);
Phillip Lougher539c2b12012-07-30 20:14:52 +01003700 }
Phillip Lougher23d83622012-10-14 02:35:22 +01003701
3702 if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE))
3703 dir->dir_is_ldir = FALSE;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003704}
3705
3706
3707/*
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003708 * dir_scan6 routines...
Phillip Lougherabc3b492012-07-29 02:53:35 +01003709 * This generates the filesystem metadata and writes it out to the destination
3710 */
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003711void scan6_init_dir(struct directory *dir)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003712{
3713 dir->buff = malloc(SQUASHFS_METADATA_SIZE);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003714 if(dir->buff == NULL)
3715 MEM_ERROR();
Phillip Lougherabc3b492012-07-29 02:53:35 +01003716
3717 dir->size = SQUASHFS_METADATA_SIZE;
3718 dir->p = dir->index_count_p = dir->buff;
3719 dir->entry_count = 256;
3720 dir->entry_count_p = NULL;
3721 dir->index = NULL;
3722 dir->i_count = dir->i_size = 0;
3723}
3724
3725
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003726struct dir_ent *scan6_readdir(struct directory *dir, struct dir_info *dir_info,
Phillip Lougherbf338362012-08-22 05:24:36 +01003727 struct dir_ent *dir_ent)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003728{
Phillip Lougherbf338362012-08-22 05:24:36 +01003729 if (dir_ent == NULL)
3730 dir_ent = dir_info->list;
3731 else
3732 dir_ent = dir_ent->next;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003733
Phillip Lougherbf338362012-08-22 05:24:36 +01003734 for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next)
3735 add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number,
3736 dir_ent->name, dir_ent->inode->type, dir);
3737
3738 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003739}
3740
3741
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003742void scan6_freedir(struct directory *dir)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003743{
3744 if(dir->index)
3745 free(dir->index);
3746 free(dir->buff);
3747}
3748
3749
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003750void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info)
plougher1f413c82005-11-18 00:02:14 +00003751{
3752 int squashfs_type;
plougher1f413c82005-11-18 00:02:14 +00003753 int duplicate_file;
plougher1f413c82005-11-18 00:02:14 +00003754 struct directory dir;
Phillip Lougherbf338362012-08-22 05:24:36 +01003755 struct dir_ent *dir_ent = NULL;
plougher1f413c82005-11-18 00:02:14 +00003756
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003757 scan6_init_dir(&dir);
plougher1f413c82005-11-18 00:02:14 +00003758
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003759 while((dir_ent = scan6_readdir(&dir, dir_info, dir_ent)) != NULL) {
Phillip Lougher0a670882012-10-05 04:09:13 +01003760 struct stat *buf = &dir_ent->inode->buf;
plougher1f413c82005-11-18 00:02:14 +00003761
Phillip Lougher24551a82013-03-24 02:30:50 +00003762 update_info(dir_ent);
3763
plougher1f413c82005-11-18 00:02:14 +00003764 if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) {
3765 switch(buf->st_mode & S_IFMT) {
3766 case S_IFREG:
3767 squashfs_type = SQUASHFS_FILE_TYPE;
plougher110799c2009-03-30 01:50:40 +00003768 write_file(inode, dir_ent,
3769 &duplicate_file);
3770 INFO("file %s, uncompressed size %lld "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003771 "bytes %s\n",
3772 subpathname(dir_ent),
plougher82ab2332009-04-21 00:21:21 +00003773 (long long) buf->st_size,
3774 duplicate_file ? "DUPLICATE" :
3775 "");
plougher1f413c82005-11-18 00:02:14 +00003776 break;
3777
3778 case S_IFDIR:
3779 squashfs_type = SQUASHFS_DIR_TYPE;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003780 dir_scan6(inode, dir_ent->dir);
plougher1f413c82005-11-18 00:02:14 +00003781 break;
3782
3783 case S_IFLNK:
3784 squashfs_type = SQUASHFS_SYMLINK_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003785 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003786 squashfs_type, 0, 0, 0, NULL,
3787 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00003788 INFO("symbolic link %s inode 0x%llx\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003789 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003790 sym_count ++;
3791 break;
3792
3793 case S_IFCHR:
3794 squashfs_type = SQUASHFS_CHRDEV_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003795 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003796 squashfs_type, 0, 0, 0, NULL,
3797 NULL, NULL, 0);
3798 INFO("character device %s inode 0x%llx"
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003799 "\n", subpathname(dir_ent),
3800 *inode);
plougher1f413c82005-11-18 00:02:14 +00003801 dev_count ++;
3802 break;
3803
3804 case S_IFBLK:
3805 squashfs_type = SQUASHFS_BLKDEV_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003806 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003807 squashfs_type, 0, 0, 0, NULL,
3808 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00003809 INFO("block device %s inode 0x%llx\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003810 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003811 dev_count ++;
3812 break;
3813
3814 case S_IFIFO:
3815 squashfs_type = SQUASHFS_FIFO_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003816 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003817 squashfs_type, 0, 0, 0, NULL,
3818 NULL, NULL, 0);
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003819 INFO("fifo %s inode 0x%llx\n",
3820 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003821 fifo_count ++;
3822 break;
3823
3824 case S_IFSOCK:
3825 squashfs_type = SQUASHFS_SOCKET_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003826 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003827 squashfs_type, 0, 0, 0, NULL,
3828 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00003829 INFO("unix domain socket %s inode "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003830 "0x%llx\n",
3831 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003832 sock_count ++;
3833 break;
3834
plougher23377982007-11-12 04:04:48 +00003835 default:
plougherb3604122009-03-30 02:07:20 +00003836 BAD_ERROR("%s unrecognised file type, "
Phillip Lougher494479f2012-02-03 15:45:41 +00003837 "mode is %x\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003838 subpathname(dir_ent),
plougherb3604122009-03-30 02:07:20 +00003839 buf->st_mode);
plougher29e37092007-04-15 01:24:51 +00003840 }
3841 dir_ent->inode->inode = *inode;
plougher1f413c82005-11-18 00:02:14 +00003842 dir_ent->inode->type = squashfs_type;
3843 } else {
3844 *inode = dir_ent->inode->inode;
3845 squashfs_type = dir_ent->inode->type;
plougher04b0d5f2006-02-10 00:42:06 +00003846 switch(squashfs_type) {
3847 case SQUASHFS_FILE_TYPE:
3848 if(!sorted)
plougher50b31762009-03-31 04:14:46 +00003849 INFO("file %s, uncompressed "
3850 "size %lld bytes LINK"
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003851 "\n",
3852 subpathname(dir_ent),
plougher82ab2332009-04-21 00:21:21 +00003853 (long long)
plougher50b31762009-03-31 04:14:46 +00003854 buf->st_size);
plougher04b0d5f2006-02-10 00:42:06 +00003855 break;
3856 case SQUASHFS_SYMLINK_TYPE:
plougherb3604122009-03-30 02:07:20 +00003857 INFO("symbolic link %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003858 "LINK\n", subpathname(dir_ent),
3859 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003860 break;
3861 case SQUASHFS_CHRDEV_TYPE:
plougherb3604122009-03-30 02:07:20 +00003862 INFO("character device %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003863 "LINK\n", subpathname(dir_ent),
3864 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003865 break;
plougher5507dd92006-11-06 00:43:10 +00003866 case SQUASHFS_BLKDEV_TYPE:
plougherb3604122009-03-30 02:07:20 +00003867 INFO("block device %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003868 "LINK\n", subpathname(dir_ent),
3869 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003870 break;
3871 case SQUASHFS_FIFO_TYPE:
plougherb3604122009-03-30 02:07:20 +00003872 INFO("fifo %s inode 0x%llx LINK\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003873 subpathname(dir_ent), *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003874 break;
3875 case SQUASHFS_SOCKET_TYPE:
plougher50b31762009-03-31 04:14:46 +00003876 INFO("unix domain socket %s inode "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003877 "0x%llx LINK\n",
3878 subpathname(dir_ent), *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003879 break;
3880 }
plougher1f413c82005-11-18 00:02:14 +00003881 }
3882
Phillip Lougher0366aec2012-10-05 04:06:04 +01003883 add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name,
3884 squashfs_type, &dir);
plougher1f413c82005-11-18 00:02:14 +00003885 }
3886
plougher29e37092007-04-15 01:24:51 +00003887 write_dir(inode, dir_info, &dir);
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003888 INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent),
3889 *inode);
plougher1f413c82005-11-18 00:02:14 +00003890
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003891 scan6_freedir(&dir);
plougher1f413c82005-11-18 00:02:14 +00003892}
3893
3894
3895unsigned int slog(unsigned int block)
3896{
3897 int i;
3898
plougher4c99cb72007-06-14 21:46:31 +00003899 for(i = 12; i <= 20; i++)
plougher1f413c82005-11-18 00:02:14 +00003900 if(block == (1 << i))
3901 return i;
3902 return 0;
3903}
3904
3905
plougher8f8e1a12007-10-18 02:50:21 +00003906int old_excluded(char *filename, struct stat *buf)
plougher1f413c82005-11-18 00:02:14 +00003907{
3908 int i;
3909
3910 for(i = 0; i < exclude; i++)
plougherb3604122009-03-30 02:07:20 +00003911 if((exclude_paths[i].st_dev == buf->st_dev) &&
3912 (exclude_paths[i].st_ino == buf->st_ino))
plougher1f413c82005-11-18 00:02:14 +00003913 return TRUE;
3914 return FALSE;
3915}
3916
3917
3918#define ADD_ENTRY(buf) \
plougher360514a2009-03-30 03:01:38 +00003919 if(exclude % EXCLUDE_SIZE == 0) { \
3920 exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \
3921 * sizeof(struct exclude_info)); \
3922 if(exclude_paths == NULL) \
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003923 MEM_ERROR(); \
plougher360514a2009-03-30 03:01:38 +00003924 } \
3925 exclude_paths[exclude].st_dev = buf.st_dev; \
plougher1f413c82005-11-18 00:02:14 +00003926 exclude_paths[exclude++].st_ino = buf.st_ino;
plougher8f8e1a12007-10-18 02:50:21 +00003927int old_add_exclude(char *path)
plougher1f413c82005-11-18 00:02:14 +00003928{
3929 int i;
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003930 char *filename;
plougher1f413c82005-11-18 00:02:14 +00003931 struct stat buf;
3932
plougherb3604122009-03-30 02:07:20 +00003933 if(path[0] == '/' || strncmp(path, "./", 2) == 0 ||
3934 strncmp(path, "../", 3) == 0) {
plougher1f413c82005-11-18 00:02:14 +00003935 if(lstat(path, &buf) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003936 ERROR_START("Cannot stat exclude dir/file %s because "
3937 "%s", path, strerror(errno));
3938 ERROR_EXIT(", ignoring\n");
plougher1f413c82005-11-18 00:02:14 +00003939 return TRUE;
3940 }
3941 ADD_ENTRY(buf);
3942 return TRUE;
3943 }
3944
3945 for(i = 0; i < source; i++) {
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003946 int res = asprintf(&filename, "%s/%s", source_path[i], path);
3947 if(res == -1)
3948 BAD_ERROR("asprintf failed in old_add_exclude\n");
plougher1f413c82005-11-18 00:02:14 +00003949 if(lstat(filename, &buf) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003950 if(!(errno == ENOENT || errno == ENOTDIR)) {
3951 ERROR_START("Cannot stat exclude dir/file %s "
3952 "because %s", filename, strerror(errno));
3953 ERROR_EXIT(", ignoring\n");
3954 }
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003955 free(filename);
plougher1f413c82005-11-18 00:02:14 +00003956 continue;
3957 }
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003958 free(filename);
plougher1f413c82005-11-18 00:02:14 +00003959 ADD_ENTRY(buf);
3960 }
3961 return TRUE;
3962}
3963
3964
plougherb3604122009-03-30 02:07:20 +00003965void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
3966 int type)
plougher1f413c82005-11-18 00:02:14 +00003967{
plougherb3604122009-03-30 02:07:20 +00003968 old_root_entry = realloc(old_root_entry,
3969 sizeof(struct old_root_entry_info) * (old_root_entries + 1));
3970 if(old_root_entry == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003971 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00003972
ploughera326c182009-08-29 05:41:45 +00003973 old_root_entry[old_root_entries].name = strdup(name);
3974 old_root_entry[old_root_entries].inode.inode = inode;
3975 old_root_entry[old_root_entries].inode.inode_number = inode_number;
3976 old_root_entry[old_root_entries].inode.type = type;
3977 old_root_entry[old_root_entries++].inode.root_entry = TRUE;
plougher1f413c82005-11-18 00:02:14 +00003978}
3979
3980
plougher5741d792010-09-04 03:20:50 +00003981void initialise_threads(int readb_mbytes, int writeb_mbytes,
Phillip Lougherc97dac52013-04-14 03:32:10 +01003982 int fragmentb_mbytes, int freelst)
plougher5507dd92006-11-06 00:43:10 +00003983{
3984 int i;
3985 sigset_t sigmask, old_mask;
Phillip Loughere1668fe2012-12-30 05:48:12 +00003986 int reader_buffer_size;
3987 int fragment_buffer_size;
plougher5741d792010-09-04 03:20:50 +00003988 /*
3989 * writer_buffer_size is global because it is needed in
3990 * write_file_blocks_dup()
3991 */
Phillip Loughere1668fe2012-12-30 05:48:12 +00003992
3993 /*
3994 * convert from queue size in Mbytes to queue size in
3995 * blocks.
3996 *
3997 * In doing so, check that the user supplied values do not
3998 * overflow a signed int
3999 */
4000 if(shift_overflow(readb_mbytes, 20 - block_log))
4001 BAD_ERROR("Read queue is too large\n");
4002 else
4003 reader_buffer_size = readb_mbytes << (20 - block_log);
4004
4005 if(shift_overflow(fragmentb_mbytes, 20 - block_log))
4006 BAD_ERROR("Fragment queue is too large\n");
4007 else
4008 fragment_buffer_size = fragmentb_mbytes << (20 - block_log);
4009
4010 if(shift_overflow(writeb_mbytes, 20 - block_log))
4011 BAD_ERROR("Write queue is too large\n");
4012 else
4013 writer_buffer_size = writeb_mbytes << (20 - block_log);
plougher5507dd92006-11-06 00:43:10 +00004014
Phillip Lougher9391cb12013-03-20 05:16:10 +00004015 /*
4016 * setup signal handlers for the main thread, these cleanup
4017 * deleting the destination file, if appending the
4018 * handlers for SIGTERM and SIGINT will be replaced with handlers
4019 * allowing the user to press ^C twice to restore the existing
4020 * filesystem.
4021 *
Phillip Lougherdd343392013-03-31 23:29:03 +01004022 * SIGUSR1 is an internal signal, which is used by the sub-threads
Phillip Lougher9391cb12013-03-20 05:16:10 +00004023 * to tell the main thread to terminate, deleting the destination file,
4024 * or if necessary restoring the filesystem on appending
4025 */
Phillip Lougherb8ec5202013-03-28 20:14:37 +00004026 signal(SIGTERM, sighandler);
4027 signal(SIGINT, sighandler);
Phillip Lougherdd343392013-03-31 23:29:03 +01004028 signal(SIGUSR1, sighandler);
Phillip Lougher9391cb12013-03-20 05:16:10 +00004029
Phillip Lougher7538d742013-04-22 05:33:27 +01004030 /* block SIGQUIT and SIGHUP, these are handled by the info thread */
Phillip Lougher24551a82013-03-24 02:30:50 +00004031 sigemptyset(&sigmask);
4032 sigaddset(&sigmask, SIGQUIT);
Phillip Lougher7538d742013-04-22 05:33:27 +01004033 sigaddset(&sigmask, SIGHUP);
Phillip Lougherb1259f72013-06-06 03:10:15 +01004034 if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
Phillip Lougher24551a82013-03-24 02:30:50 +00004035 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4036
Phillip Lougher9391cb12013-03-20 05:16:10 +00004037 /*
4038 * temporarily block these signals, so the created sub-threads
4039 * will ignore them, ensuring the main thread handles them
4040 */
plougher5507dd92006-11-06 00:43:10 +00004041 sigemptyset(&sigmask);
4042 sigaddset(&sigmask, SIGINT);
Phillip Lougher9391cb12013-03-20 05:16:10 +00004043 sigaddset(&sigmask, SIGTERM);
Phillip Lougherdd343392013-03-31 23:29:03 +01004044 sigaddset(&sigmask, SIGUSR1);
Phillip Lougher8f8d2752013-03-26 04:38:38 +00004045 if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1)
plougher5507dd92006-11-06 00:43:10 +00004046 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4047
plougher5507dd92006-11-06 00:43:10 +00004048 if(processors == -1) {
4049#ifndef linux
4050 int mib[2];
4051 size_t len = sizeof(processors);
4052
4053 mib[0] = CTL_HW;
4054#ifdef HW_AVAILCPU
4055 mib[1] = HW_AVAILCPU;
4056#else
4057 mib[1] = HW_NCPU;
4058#endif
4059
4060 if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00004061 ERROR_START("Failed to get number of available "
4062 "processors.");
4063 ERROR_EXIT(" Defaulting to 1\n");
plougher5507dd92006-11-06 00:43:10 +00004064 processors = 1;
4065 }
4066#else
plougher9cc26b72010-08-17 01:05:55 +00004067 processors = sysconf(_SC_NPROCESSORS_ONLN);
plougher5507dd92006-11-06 00:43:10 +00004068#endif
4069 }
4070
Phillip Loughere1668fe2012-12-30 05:48:12 +00004071 if(multiply_overflow(processors, 2) ||
4072 add_overflow(processors * 2, 2) ||
4073 multiply_overflow(processors * 2 + 2,
4074 sizeof(pthread_t)))
4075 BAD_ERROR("Processors too large\n");
4076
Phillip Lougher0280d992014-01-27 05:54:07 +00004077 deflator_thread = malloc(processors * 2 * sizeof(pthread_t));
4078 if(deflator_thread == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004079 MEM_ERROR();
Phillip Loughere1668fe2012-12-30 05:48:12 +00004080
plougher5507dd92006-11-06 00:43:10 +00004081 frag_deflator_thread = &deflator_thread[processors];
4082
4083 to_reader = queue_init(1);
Phillip Loughercf478e92013-05-29 02:38:41 +01004084 to_deflate = queue_init(reader_buffer_size);
plougher5507dd92006-11-06 00:43:10 +00004085 to_writer = queue_init(writer_buffer_size);
4086 from_writer = queue_init(1);
plougher76c64082008-03-08 01:32:23 +00004087 to_frag = queue_init(fragment_buffer_size);
Phillip Lougher6164b5f2013-05-23 05:05:10 +01004088 locked_fragment = queue_init(fragment_buffer_size);
Phillip Lougher0e1656d2013-05-20 03:47:24 +01004089 to_main = seq_queue_init();
Phillip Lougher316ab632013-04-29 02:53:39 +01004090 reader_buffer = cache_init(block_size, reader_buffer_size, 0, 0);
4091 writer_buffer = cache_init(block_size, writer_buffer_size, 1, freelst);
4092 fragment_buffer = cache_init(block_size, fragment_buffer_size, 1,
4093 freelst);
Phillip Lougher0280d992014-01-27 05:54:07 +00004094 pthread_create(&reader_thread, NULL, reader, NULL);
4095 pthread_create(&writer_thread, NULL, writer, NULL);
Phillip Lougher3b89ee82012-10-18 23:55:37 +01004096 init_progress_bar();
Phillip Lougher24551a82013-03-24 02:30:50 +00004097 init_info();
plougher5507dd92006-11-06 00:43:10 +00004098 pthread_mutex_init(&fragment_mutex, NULL);
plougher5507dd92006-11-06 00:43:10 +00004099
4100 for(i = 0; i < processors; i++) {
plougher50b31762009-03-31 04:14:46 +00004101 if(pthread_create(&deflator_thread[i], NULL, deflator, NULL) !=
4102 0)
plougher5507dd92006-11-06 00:43:10 +00004103 BAD_ERROR("Failed to create thread\n");
plougher360514a2009-03-30 03:01:38 +00004104 if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator,
4105 NULL) != 0)
plougher5507dd92006-11-06 00:43:10 +00004106 BAD_ERROR("Failed to create thread\n");
4107 }
4108
Phillip Lougher0280d992014-01-27 05:54:07 +00004109 main_thread = pthread_self();
4110
plougher5507dd92006-11-06 00:43:10 +00004111 printf("Parallel mksquashfs: Using %d processor%s\n", processors,
4112 processors == 1 ? "" : "s");
4113
Phillip Lougher9391cb12013-03-20 05:16:10 +00004114 /* Restore the signal mask for the main thread */
Phillip Lougher8f8d2752013-03-26 04:38:38 +00004115 if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1)
plougher5507dd92006-11-06 00:43:10 +00004116 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4117}
4118
4119
plougher0e453652006-11-06 01:49:35 +00004120long long write_inode_lookup_table()
4121{
4122 int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count);
plougher44f03282010-07-27 00:31:36 +00004123 void *it;
plougher02bc3bc2007-02-25 12:12:01 +00004124
4125 if(inode_count == sinode_count)
4126 goto skip_inode_hash_table;
plougher0e453652006-11-06 01:49:35 +00004127
plougher44f03282010-07-27 00:31:36 +00004128 it = realloc(inode_lookup_table, lookup_bytes);
4129 if(it == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004130 MEM_ERROR();
plougher44f03282010-07-27 00:31:36 +00004131 inode_lookup_table = it;
plougher0e453652006-11-06 01:49:35 +00004132
plougher0e453652006-11-06 01:49:35 +00004133 for(i = 0; i < INODE_HASH_SIZE; i ++) {
4134 struct inode_info *inode = inode_info[i];
4135
4136 for(inode = inode_info[i]; inode; inode = inode->next) {
plougher0e453652006-11-06 01:49:35 +00004137
Phillip Lougher539c2b12012-07-30 20:14:52 +01004138 inode_number = get_inode_no(inode);
plougher0e453652006-11-06 01:49:35 +00004139
plougher360514a2009-03-30 03:01:38 +00004140 SQUASHFS_SWAP_LONG_LONGS(&inode->inode,
4141 &inode_lookup_table[inode_number - 1], 1);
plougher0e453652006-11-06 01:49:35 +00004142
plougher0e453652006-11-06 01:49:35 +00004143 }
4144 }
4145
plougher02bc3bc2007-02-25 12:12:01 +00004146skip_inode_hash_table:
ploughera0a49c32010-08-11 01:47:59 +00004147 return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL,
4148 noI);
plougher0e453652006-11-06 01:49:35 +00004149}
4150
plougher2ea89142008-03-11 01:34:19 +00004151
Phillip Lougher8e44e052012-11-26 02:58:35 +00004152char *get_component(char *target, char **targname)
plougher8f8e1a12007-10-18 02:50:21 +00004153{
Phillip Lougher8e44e052012-11-26 02:58:35 +00004154 char *start;
4155
plougher8f8e1a12007-10-18 02:50:21 +00004156 while(*target == '/')
rlougherc4ebcf52007-11-08 17:52:49 +00004157 target ++;
plougher8f8e1a12007-10-18 02:50:21 +00004158
Phillip Lougher8e44e052012-11-26 02:58:35 +00004159 start = target;
plougher8f8e1a12007-10-18 02:50:21 +00004160 while(*target != '/' && *target!= '\0')
Phillip Lougher8e44e052012-11-26 02:58:35 +00004161 target ++;
plougher8f8e1a12007-10-18 02:50:21 +00004162
Phillip Lougher8e44e052012-11-26 02:58:35 +00004163 *targname = strndup(start, target - start);
plougher8f8e1a12007-10-18 02:50:21 +00004164
4165 return target;
4166}
4167
4168
4169void free_path(struct pathname *paths)
4170{
4171 int i;
4172
4173 for(i = 0; i < paths->names; i++) {
4174 if(paths->name[i].paths)
4175 free_path(paths->name[i].paths);
4176 free(paths->name[i].name);
4177 if(paths->name[i].preg) {
4178 regfree(paths->name[i].preg);
4179 free(paths->name[i].preg);
4180 }
4181 }
4182
4183 free(paths);
4184}
4185
4186
4187struct pathname *add_path(struct pathname *paths, char *target, char *alltarget)
4188{
Phillip Lougher8e44e052012-11-26 02:58:35 +00004189 char *targname;
plougher8f8e1a12007-10-18 02:50:21 +00004190 int i, error;
4191
Phillip Lougher8e44e052012-11-26 02:58:35 +00004192 target = get_component(target, &targname);
plougher8f8e1a12007-10-18 02:50:21 +00004193
4194 if(paths == NULL) {
plougherdec6ef12010-12-18 02:47:53 +00004195 paths = malloc(sizeof(struct pathname));
4196 if(paths == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004197 MEM_ERROR();
plougher8f8e1a12007-10-18 02:50:21 +00004198
4199 paths->names = 0;
4200 paths->name = NULL;
4201 }
4202
4203 for(i = 0; i < paths->names; i++)
4204 if(strcmp(paths->name[i].name, targname) == 0)
4205 break;
4206
4207 if(i == paths->names) {
4208 /* allocate new name entry */
4209 paths->names ++;
plougherb3604122009-03-30 02:07:20 +00004210 paths->name = realloc(paths->name, (i + 1) *
4211 sizeof(struct path_entry));
plougher6f2a8262010-07-27 00:37:19 +00004212 if(paths->name == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004213 MEM_ERROR();
Phillip Lougher8e44e052012-11-26 02:58:35 +00004214 paths->name[i].name = targname;
plougher8f8e1a12007-10-18 02:50:21 +00004215 paths->name[i].paths = NULL;
4216 if(use_regex) {
4217 paths->name[i].preg = malloc(sizeof(regex_t));
plougher5c60eab2010-07-21 01:12:19 +00004218 if(paths->name[i].preg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004219 MEM_ERROR();
plougherb3604122009-03-30 02:07:20 +00004220 error = regcomp(paths->name[i].preg, targname,
4221 REG_EXTENDED|REG_NOSUB);
plougher23377982007-11-12 04:04:48 +00004222 if(error) {
Phillip Lougher62859d22012-11-30 05:53:56 +00004223 char str[1024]; /* overflow safe */
plougher8f8e1a12007-10-18 02:50:21 +00004224
4225 regerror(error, paths->name[i].preg, str, 1024);
plougherb3604122009-03-30 02:07:20 +00004226 BAD_ERROR("invalid regex %s in export %s, "
plougher50b31762009-03-31 04:14:46 +00004227 "because %s\n", targname, alltarget,
4228 str);
plougher8f8e1a12007-10-18 02:50:21 +00004229 }
4230 } else
4231 paths->name[i].preg = NULL;
4232
4233 if(target[0] == '\0')
4234 /* at leaf pathname component */
4235 paths->name[i].paths = NULL;
4236 else
4237 /* recurse adding child components */
plougher50b31762009-03-31 04:14:46 +00004238 paths->name[i].paths = add_path(NULL, target,
4239 alltarget);
plougher8f8e1a12007-10-18 02:50:21 +00004240 } else {
4241 /* existing matching entry */
Phillip Lougher8e44e052012-11-26 02:58:35 +00004242 free(targname);
4243
plougher8f8e1a12007-10-18 02:50:21 +00004244 if(paths->name[i].paths == NULL) {
plougher50b31762009-03-31 04:14:46 +00004245 /* No sub-directory which means this is the leaf
4246 * component of a pre-existing exclude which subsumes
4247 * the exclude currently being added, in which case stop
4248 * adding components */
plougher8f8e1a12007-10-18 02:50:21 +00004249 } else if(target[0] == '\0') {
plougherb3604122009-03-30 02:07:20 +00004250 /* at leaf pathname component and child components exist
plougher50b31762009-03-31 04:14:46 +00004251 * from more specific excludes, delete as they're
4252 * subsumed by this exclude */
plougher8f8e1a12007-10-18 02:50:21 +00004253 free_path(paths->name[i].paths);
4254 paths->name[i].paths = NULL;
4255 } else
4256 /* recurse adding child components */
4257 add_path(paths->name[i].paths, target, alltarget);
4258 }
4259
4260 return paths;
4261}
plougher2ea89142008-03-11 01:34:19 +00004262
4263
plougher05e50ef2007-10-23 12:34:20 +00004264void add_exclude(char *target)
plougher8f8e1a12007-10-18 02:50:21 +00004265{
plougher05e50ef2007-10-23 12:34:20 +00004266
plougherb3604122009-03-30 02:07:20 +00004267 if(target[0] == '/' || strncmp(target, "./", 2) == 0 ||
4268 strncmp(target, "../", 3) == 0)
4269 BAD_ERROR("/, ./ and ../ prefixed excludes not supported with "
4270 "-wildcards or -regex options\n");
plougher05e50ef2007-10-23 12:34:20 +00004271 else if(strncmp(target, "... ", 4) == 0)
4272 stickypath = add_path(stickypath, target + 4, target + 4);
4273 else
4274 path = add_path(path, target, target);
plougher8f8e1a12007-10-18 02:50:21 +00004275}
4276
4277
4278void display_path(int depth, struct pathname *paths)
4279{
4280 int i, n;
4281
4282 if(paths == NULL)
4283 return;
4284
4285 for(i = 0; i < paths->names; i++) {
4286 for(n = 0; n < depth; n++)
4287 printf("\t");
4288 printf("%d: %s\n", depth, paths->name[i].name);
4289 display_path(depth + 1, paths->name[i].paths);
4290 }
4291}
4292
4293
4294void display_path2(struct pathname *paths, char *string)
4295{
4296 int i;
Phillip Lougher91459c12012-11-30 05:24:44 +00004297 char *path;
plougher8f8e1a12007-10-18 02:50:21 +00004298
4299 if(paths == NULL) {
4300 printf("%s\n", string);
4301 return;
4302 }
4303
4304 for(i = 0; i < paths->names; i++) {
Phillip Lougher91459c12012-11-30 05:24:44 +00004305 int res = asprintf(&path, "%s/%s", string, paths->name[i].name);
4306 if(res == -1)
4307 BAD_ERROR("asprintf failed in display_path2\n");
plougher8f8e1a12007-10-18 02:50:21 +00004308 display_path2(paths->name[i].paths, path);
Phillip Lougher91459c12012-11-30 05:24:44 +00004309 free(path);
plougher8f8e1a12007-10-18 02:50:21 +00004310 }
4311}
4312
4313
plougherf9039c92007-10-22 03:54:16 +00004314struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path)
4315{
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004316 int count = paths == NULL ? 0 : paths->count;
4317
4318 if(count % PATHS_ALLOC_SIZE == 0) {
4319 paths = realloc(paths, sizeof(struct pathnames) +
4320 (count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *));
plougher6f2a8262010-07-27 00:37:19 +00004321 if(paths == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004322 MEM_ERROR();
plougher6f2a8262010-07-27 00:37:19 +00004323 }
plougherf9039c92007-10-22 03:54:16 +00004324
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004325 paths->path[count] = path;
4326 paths->count = count + 1;
plougherf9039c92007-10-22 03:54:16 +00004327 return paths;
4328}
4329
4330
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004331int excluded_match(char *name, struct pathname *path, struct pathnames **new)
plougherf9039c92007-10-22 03:54:16 +00004332{
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004333 int i;
plougher8f8e1a12007-10-18 02:50:21 +00004334
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004335 for(i = 0; i < path->names; i++) {
4336 int match = use_regex ?
4337 regexec(path->name[i].preg, name, (size_t) 0,
plougher50b31762009-03-31 04:14:46 +00004338 NULL, 0) == 0 :
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004339 fnmatch(path->name[i].name, name,
4340 FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0;
plougherf9039c92007-10-22 03:54:16 +00004341
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004342 if(match) {
4343 if(path->name[i].paths == NULL || new == NULL)
plougherb3604122009-03-30 02:07:20 +00004344 /* match on a leaf component, any subdirectories
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004345 * in the filesystem should be excluded */
4346 return TRUE;
4347 else
plougherb3604122009-03-30 02:07:20 +00004348 /* match on a non-leaf component, add any
plougher50b31762009-03-31 04:14:46 +00004349 * subdirectories to the new set of
4350 * subdirectories to scan for this name */
plougherf9039c92007-10-22 03:54:16 +00004351 *new = add_subdir(*new, path->name[i].paths);
4352 }
4353 }
4354
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004355 return FALSE;
4356}
4357
4358
4359int excluded(char *name, struct pathnames *paths, struct pathnames **new)
4360{
4361 int n;
4362
4363 if(stickypath && excluded_match(name, stickypath, NULL))
4364 return TRUE;
4365
4366 for(n = 0; paths && n < paths->count; n++) {
4367 int res = excluded_match(name, paths->path[n], new);
4368 if(res) {
4369 free(*new);
4370 *new = NULL;
4371 return TRUE;
4372 }
plougherf9039c92007-10-22 03:54:16 +00004373 }
4374
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004375 /*
4376 * Either:
4377 * - no matching names found, return empty new search set, or
4378 * - one or more matches with sub-directories found (no leaf matches),
4379 * in which case return new search set.
4380 *
4381 * In either case return FALSE as we don't want to exclude this entry
4382 */
plougher8f8e1a12007-10-18 02:50:21 +00004383 return FALSE;
4384}
4385
4386
Phillip Lougher386128f2012-12-16 05:23:45 +00004387void process_exclude_file(char *argv)
4388{
4389 FILE *fd;
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004390 char buffer[MAX_LINE + 1]; /* overflow safe */
Phillip Lougherb22336d2012-12-20 18:44:22 +00004391 char *filename;
Phillip Lougher386128f2012-12-16 05:23:45 +00004392
Phillip Lougherb22336d2012-12-20 18:44:22 +00004393 fd = fopen(argv, "r");
4394 if(fd == NULL)
4395 BAD_ERROR("Failed to open exclude file \"%s\" because %s\n",
4396 argv, strerror(errno));
Phillip Lougher386128f2012-12-16 05:23:45 +00004397
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004398 while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) {
Phillip Lougherb22336d2012-12-20 18:44:22 +00004399 int len = strlen(filename);
4400
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004401 if(len == MAX_LINE && filename[len - 1] != '\n')
Phillip Lougherb22336d2012-12-20 18:44:22 +00004402 /* line too large */
4403 BAD_ERROR("Line too long when reading "
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004404 "exclude file \"%s\", larger than %d "
Phillip Lougher83847c12012-12-23 07:22:36 +00004405 "bytes\n", argv, MAX_LINE);
Phillip Lougherb22336d2012-12-20 18:44:22 +00004406
4407 /*
4408 * Remove '\n' terminator if it exists (the last line
4409 * in the file may not be '\n' terminated)
4410 */
4411 if(len && filename[len - 1] == '\n')
4412 filename[len - 1] = '\0';
4413
4414 /* Skip any leading whitespace */
4415 while(isspace(*filename))
4416 filename ++;
4417
4418 /* if comment line, skip */
4419 if(*filename == '#')
4420 continue;
4421
4422 /*
4423 * check for initial backslash, to accommodate
4424 * filenames with leading space or leading # character
4425 */
4426 if(*filename == '\\')
4427 filename ++;
4428
4429 /* if line is now empty after skipping characters, skip it */
4430 if(*filename == '\0')
4431 continue;
4432
Phillip Lougher386128f2012-12-16 05:23:45 +00004433 if(old_exclude)
4434 old_add_exclude(filename);
4435 else
4436 add_exclude(filename);
Phillip Lougherb22336d2012-12-20 18:44:22 +00004437 }
4438
4439 if(ferror(fd))
4440 BAD_ERROR("Reading exclude file \"%s\" failed because %s\n",
4441 argv, strerror(errno));
Phillip Lougher386128f2012-12-16 05:23:45 +00004442
4443 fclose(fd);
4444}
4445
4446
plougher99ac0cc2007-10-29 03:17:10 +00004447#define RECOVER_ID "Squashfs recovery file v1.0\n"
4448#define RECOVER_ID_SIZE 28
4449
plougher64e83fd2010-12-31 21:21:26 +00004450void write_recovery_data(struct squashfs_super_block *sBlk)
plougher99ac0cc2007-10-29 03:17:10 +00004451{
plougher1d065e92010-06-18 03:58:27 +00004452 int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start;
plougher99ac0cc2007-10-29 03:17:10 +00004453 pid_t pid = getpid();
plougher44d54ef2010-02-08 22:13:49 +00004454 char *metadata;
plougher99ac0cc2007-10-29 03:17:10 +00004455 char header[] = RECOVER_ID;
4456
4457 if(recover == FALSE) {
4458 printf("No recovery data option specified.\n");
ploughereac18532007-10-29 05:26:06 +00004459 printf("Skipping saving recovery file.\n\n");
plougher99ac0cc2007-10-29 03:17:10 +00004460 return;
4461 }
4462
plougher1b879bc2010-12-18 02:49:42 +00004463 metadata = malloc(bytes);
4464 if(metadata == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004465 MEM_ERROR();
plougher44d54ef2010-02-08 22:13:49 +00004466
plougher1d065e92010-06-18 03:58:27 +00004467 res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata);
Phillip Lougher477f4332013-03-06 03:55:08 +00004468 if(res == 0) {
4469 ERROR("Failed to read append filesystem metadata\n");
4470 BAD_ERROR("Filesystem corrupted?\n");
4471 }
plougher99ac0cc2007-10-29 03:17:10 +00004472
Phillip Lougheraf4c9232012-11-15 03:46:28 +00004473 res = asprintf(&recovery_file, "squashfs_recovery_%s_%d",
plougher44d54ef2010-02-08 22:13:49 +00004474 getbase(destination_file), pid);
Phillip Lougheraf4c9232012-11-15 03:46:28 +00004475 if(res == -1)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004476 MEM_ERROR();
Phillip Lougheraf4c9232012-11-15 03:46:28 +00004477
plougherb3604122009-03-30 02:07:20 +00004478 recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
4479 if(recoverfd == -1)
4480 BAD_ERROR("Failed to create recovery file, because %s. "
4481 "Aborting\n", strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004482
plougher628e7682009-03-29 22:12:24 +00004483 if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1)
plougherb3604122009-03-30 02:07:20 +00004484 BAD_ERROR("Failed to write recovery file, because %s\n",
4485 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004486
plougher64e83fd2010-12-31 21:21:26 +00004487 if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1)
plougherb3604122009-03-30 02:07:20 +00004488 BAD_ERROR("Failed to write recovery file, because %s\n",
4489 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004490
plougher628e7682009-03-29 22:12:24 +00004491 if(write_bytes(recoverfd, metadata, bytes) == -1)
plougherb3604122009-03-30 02:07:20 +00004492 BAD_ERROR("Failed to write recovery file, because %s\n",
4493 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004494
4495 close(recoverfd);
plougher44d54ef2010-02-08 22:13:49 +00004496 free(metadata);
plougher99ac0cc2007-10-29 03:17:10 +00004497
4498 printf("Recovery file \"%s\" written\n", recovery_file);
4499 printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n");
plougherb3604122009-03-30 02:07:20 +00004500 printf("mksquashfs dummy %s -recover %s\n", destination_file,
4501 recovery_file);
plougher99ac0cc2007-10-29 03:17:10 +00004502 printf("to restore filesystem\n\n");
4503}
4504
4505
4506void read_recovery_data(char *recovery_file, char *destination_file)
4507{
4508 int fd, recoverfd, bytes;
plougher64e83fd2010-12-31 21:21:26 +00004509 struct squashfs_super_block orig_sBlk, sBlk;
plougher99ac0cc2007-10-29 03:17:10 +00004510 char *metadata;
plougher8a8c4102009-03-29 22:28:49 +00004511 int res;
plougher99ac0cc2007-10-29 03:17:10 +00004512 struct stat buf;
4513 char header[] = RECOVER_ID;
4514 char header2[RECOVER_ID_SIZE];
4515
plougher9e9d9dc2010-12-18 02:53:57 +00004516 recoverfd = open(recovery_file, O_RDONLY);
4517 if(recoverfd == -1)
plougherb3604122009-03-30 02:07:20 +00004518 BAD_ERROR("Failed to open recovery file because %s\n",
4519 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004520
4521 if(stat(destination_file, &buf) == -1)
plougherb3604122009-03-30 02:07:20 +00004522 BAD_ERROR("Failed to stat destination file, because %s\n",
4523 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004524
plougher9e9d9dc2010-12-18 02:53:57 +00004525 fd = open(destination_file, O_RDWR);
4526 if(fd == -1)
plougherb3604122009-03-30 02:07:20 +00004527 BAD_ERROR("Failed to open destination file because %s\n",
4528 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004529
plougher8a8c4102009-03-29 22:28:49 +00004530 res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE);
4531 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00004532 BAD_ERROR("Failed to read recovery file, because %s\n",
4533 strerror(errno));
plougher8a8c4102009-03-29 22:28:49 +00004534 if(res < RECOVER_ID_SIZE)
4535 BAD_ERROR("Recovery file appears to be truncated\n");
plougher99ac0cc2007-10-29 03:17:10 +00004536 if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 )
4537 BAD_ERROR("Not a recovery file\n");
4538
plougher64e83fd2010-12-31 21:21:26 +00004539 res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block));
plougher8a8c4102009-03-29 22:28:49 +00004540 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00004541 BAD_ERROR("Failed to read recovery file, because %s\n",
4542 strerror(errno));
plougher64e83fd2010-12-31 21:21:26 +00004543 if(res < sizeof(struct squashfs_super_block))
plougher8a8c4102009-03-29 22:28:49 +00004544 BAD_ERROR("Recovery file appears to be truncated\n");
plougher99ac0cc2007-10-29 03:17:10 +00004545
plougher64e83fd2010-12-31 21:21:26 +00004546 res = read_fs_bytes(fd, 0, sizeof(struct squashfs_super_block), &orig_sBlk);
Phillip Lougher477f4332013-03-06 03:55:08 +00004547 if(res == 0) {
4548 ERROR("Failed to read superblock from output filesystem\n");
4549 BAD_ERROR("Output filesystem is empty!\n");
4550 }
plougher99ac0cc2007-10-29 03:17:10 +00004551
plougherb3604122009-03-30 02:07:20 +00004552 if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4,
plougher64e83fd2010-12-31 21:21:26 +00004553 sizeof(struct squashfs_super_block) - 4) != 0)
plougherb3604122009-03-30 02:07:20 +00004554 BAD_ERROR("Recovery file and destination file do not seem to "
4555 "match\n");
plougher99ac0cc2007-10-29 03:17:10 +00004556
4557 bytes = sBlk.bytes_used - sBlk.inode_table_start;
4558
plougher9e9d9dc2010-12-18 02:53:57 +00004559 metadata = malloc(bytes);
4560 if(metadata == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004561 MEM_ERROR();
plougher99ac0cc2007-10-29 03:17:10 +00004562
plougher8a8c4102009-03-29 22:28:49 +00004563 res = read_bytes(recoverfd, metadata, bytes);
4564 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00004565 BAD_ERROR("Failed to read recovery file, because %s\n",
4566 strerror(errno));
plougher8a8c4102009-03-29 22:28:49 +00004567 if(res < bytes)
plougher99ac0cc2007-10-29 03:17:10 +00004568 BAD_ERROR("Recovery file appears to be truncated\n");
4569
plougher64e83fd2010-12-31 21:21:26 +00004570 write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk);
plougher99ac0cc2007-10-29 03:17:10 +00004571
plougher0dd6f122009-03-29 21:43:57 +00004572 write_destination(fd, sBlk.inode_table_start, bytes, metadata);
plougher99ac0cc2007-10-29 03:17:10 +00004573
4574 close(recoverfd);
4575 close(fd);
4576
plougherb3604122009-03-30 02:07:20 +00004577 printf("Successfully wrote recovery file \"%s\". Exiting\n",
4578 recovery_file);
plougher99ac0cc2007-10-29 03:17:10 +00004579
4580 exit(0);
4581}
4582
4583
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004584int parse_number(char *start, int *res, int size)
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004585{
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004586 char *end;
4587 long number = strtol(start, &end, 10);
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004588
Phillip Lougherf4f48382012-11-29 03:52:44 +00004589 /*
4590 * check for strtol underflow or overflow in conversion.
4591 * Note: strtol can validly return LONG_MIN and LONG_MAX
4592 * if the user entered these values, but, additional code
4593 * to distinguish this scenario is unnecessary, because for
4594 * our purposes LONG_MIN and LONG_MAX are too large anyway
4595 */
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004596 if(number == LONG_MIN || number == LONG_MAX)
4597 return 0;
4598
4599 /* reject negative numbers as invalid */
4600 if(number < 0)
4601 return 0;
4602
4603 /* check if long result will overflow signed int */
4604 if(number > INT_MAX)
4605 return 0;
4606
4607 if(size) {
Phillip Loughered345692012-11-28 03:56:16 +00004608 /*
4609 * Check for multiplier and trailing junk.
4610 * But first check that a number exists before the
4611 * multiplier
4612 */
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004613 if(end == start)
Phillip Loughered345692012-11-28 03:56:16 +00004614 return 0;
4615
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004616 switch(end[0]) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004617 case 'm':
4618 case 'M':
4619 if(multiply_overflow((int) number, 1048576))
4620 return 0;
4621 number *= 1048576;
Phillip Loughera9e65632012-11-28 04:17:27 +00004622
Phillip Lougherf968be82012-12-04 05:12:32 +00004623 if(end[1] != '\0')
Phillip Loughera9e65632012-11-28 04:17:27 +00004624 /* trailing junk after number */
4625 return 0;
4626
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004627 break;
4628 case 'k':
4629 case 'K':
4630 if(multiply_overflow((int) number, 1024))
4631 return 0;
4632 number *= 1024;
Phillip Loughera9e65632012-11-28 04:17:27 +00004633
Phillip Lougherf968be82012-12-04 05:12:32 +00004634 if(end[1] != '\0')
Phillip Loughera9e65632012-11-28 04:17:27 +00004635 /* trailing junk after number */
4636 return 0;
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004637 case '\0':
4638 break;
4639 default:
4640 /* trailing junk after number */
4641 return 0;
4642 }
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004643 } else if(end[0] != '\0')
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004644 /* trailing junk after number */
4645 return 0;
4646
4647 *res = number;
4648 return 1;
4649}
4650
4651
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00004652void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad)
4653{
4654 int i;
4655
4656 sBlk->fragments = fragments;
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00004657 sBlk->no_ids = id_count;
4658 sBlk->inode_table_start = write_inodes();
4659 sBlk->directory_table_start = write_directories();
4660 sBlk->fragment_table_start = write_fragment_table();
4661 sBlk->lookup_table_start = exportable ? write_inode_lookup_table() :
4662 SQUASHFS_INVALID_BLK;
4663 sBlk->id_table_start = write_id_table();
4664 sBlk->xattr_id_table_start = write_xattrs();
4665
4666 TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start);
4667 TRACE("sBlk->directory_table_start 0x%llx\n",
4668 sBlk->directory_table_start);
4669 TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start);
4670 if(exportable)
4671 TRACE("sBlk->lookup_table_start 0x%llx\n",
4672 sBlk->lookup_table_start);
4673
4674 sBlk->bytes_used = bytes;
4675
4676 sBlk->compression = comp->id;
4677
4678 SQUASHFS_INSWAP_SUPER_BLOCK(sBlk);
4679 write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk);
4680
4681 if(!nopad && (i = bytes & (4096 - 1))) {
4682 char temp[4096] = {0};
4683 write_destination(fd, bytes, 4096 - i, temp);
4684 }
4685
4686 close(fd);
4687
4688 delete_pseudo_files();
4689
4690 if(recovery_file)
4691 unlink(recovery_file);
4692
4693 total_bytes += total_inode_bytes + total_directory_bytes +
4694 sizeof(struct squashfs_super_block) + total_xattr_bytes;
4695
4696 printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size"
4697 " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR,
4698 SQUASHFS_MINOR, comp->name, block_size);
4699 printf("\t%s data, %s metadata, %s fragments, %s xattrs\n",
4700 noD ? "uncompressed" : "compressed", noI ? "uncompressed" :
4701 "compressed", no_fragments ? "no" : noF ? "uncompressed" :
4702 "compressed", no_xattrs ? "no" : noX ? "uncompressed" :
4703 "compressed");
4704 printf("\tduplicates are %sremoved\n", duplicate_checking ? "" :
4705 "not ");
4706 printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0,
4707 bytes / (1024.0 * 1024.0));
4708 printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n",
4709 ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0);
4710 printf("Inode table size %d bytes (%.2f Kbytes)\n",
4711 inode_bytes, inode_bytes / 1024.0);
4712 printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n",
4713 ((float) inode_bytes / total_inode_bytes) * 100.0,
4714 total_inode_bytes);
4715 printf("Directory table size %d bytes (%.2f Kbytes)\n",
4716 directory_bytes, directory_bytes / 1024.0);
4717 printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n",
4718 ((float) directory_bytes / total_directory_bytes) * 100.0,
4719 total_directory_bytes);
4720 if(total_xattr_bytes) {
4721 printf("Xattr table size %d bytes (%.2f Kbytes)\n",
4722 xattr_bytes, xattr_bytes / 1024.0);
4723 printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n",
4724 ((float) xattr_bytes / total_xattr_bytes) * 100.0,
4725 total_xattr_bytes);
4726 }
4727 if(duplicate_checking)
4728 printf("Number of duplicate files found %d\n", file_count -
4729 dup_files);
4730 else
4731 printf("No duplicate files removed\n");
4732 printf("Number of inodes %d\n", inode_count);
4733 printf("Number of files %d\n", file_count);
4734 if(!no_fragments)
4735 printf("Number of fragments %d\n", fragments);
4736 printf("Number of symbolic links %d\n", sym_count);
4737 printf("Number of device nodes %d\n", dev_count);
4738 printf("Number of fifo nodes %d\n", fifo_count);
4739 printf("Number of socket nodes %d\n", sock_count);
4740 printf("Number of directories %d\n", dir_count);
4741 printf("Number of ids (unique uids + gids) %d\n", id_count);
4742 printf("Number of uids %d\n", uid_count);
4743
4744 for(i = 0; i < id_count; i++) {
4745 if(id_table[i]->flags & ISA_UID) {
4746 struct passwd *user = getpwuid(id_table[i]->id);
4747 printf("\t%s (%d)\n", user == NULL ? "unknown" :
4748 user->pw_name, id_table[i]->id);
4749 }
4750 }
4751
4752 printf("Number of gids %d\n", guid_count);
4753
4754 for(i = 0; i < id_count; i++) {
4755 if(id_table[i]->flags & ISA_GID) {
4756 struct group *group = getgrgid(id_table[i]->id);
4757 printf("\t%s (%d)\n", group == NULL ? "unknown" :
4758 group->gr_name, id_table[i]->id);
4759 }
4760 }
4761}
4762
4763
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004764int parse_num(char *arg, int *res)
4765{
4766 return parse_number(arg, res, 0);
4767}
4768
4769
plougher1f413c82005-11-18 00:02:14 +00004770#define VERSION() \
Phillip Lougher85776df2014-01-31 03:10:46 +00004771 printf("mksquashfs version 4.2-git (2014/01/29)\n");\
Phillip Loughercc064952014-01-03 04:26:34 +00004772 printf("copyright (C) 2014 Phillip Lougher "\
Phillip Lougher83d42a32012-10-31 23:42:22 +00004773 "<phillip@squashfs.org.uk>\n\n"); \
plougher16111452010-07-22 05:12:18 +00004774 printf("This program is free software; you can redistribute it and/or"\
4775 "\n");\
4776 printf("modify it under the terms of the GNU General Public License"\
4777 "\n");\
4778 printf("as published by the Free Software Foundation; either version "\
4779 "2,\n");\
plougher1f413c82005-11-18 00:02:14 +00004780 printf("or (at your option) any later version.\n\n");\
plougher16111452010-07-22 05:12:18 +00004781 printf("This program is distributed in the hope that it will be "\
4782 "useful,\n");\
4783 printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\
4784 "of\n");\
4785 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\
4786 "\n");\
plougher1f413c82005-11-18 00:02:14 +00004787 printf("GNU General Public License for more details.\n");
4788int main(int argc, char *argv[])
4789{
plougher324978d2006-02-27 04:53:29 +00004790 struct stat buf, source_buf;
plougher13fdddf2010-11-24 01:23:41 +00004791 int res, i;
plougher1f413c82005-11-18 00:02:14 +00004792 char *b, *root_name = NULL;
Phillip Loughera709bff2013-03-28 17:48:31 +00004793 int keep_as_directory = FALSE;
plougher1f413c82005-11-18 00:02:14 +00004794 squashfs_inode inode;
plougherb3604122009-03-30 02:07:20 +00004795 int readb_mbytes = READER_BUFFER_DEFAULT,
4796 writeb_mbytes = WRITER_BUFFER_DEFAULT,
4797 fragmentb_mbytes = FRAGMENT_BUFFER_DEFAULT;
Phillip Lougher569a9632013-04-22 03:30:30 +01004798 int progress = TRUE;
Phillip Lougherc6c73b22012-10-19 03:12:08 +01004799 int force_progress = FALSE;
Phillip Loughera709bff2013-03-28 17:48:31 +00004800 struct file_buffer **fragment = NULL;
plougher1f413c82005-11-18 00:02:14 +00004801
plougher1f413c82005-11-18 00:02:14 +00004802 block_log = slog(block_size);
4803 if(argc > 1 && strcmp(argv[1], "-version") == 0) {
4804 VERSION();
4805 exit(0);
4806 }
4807 for(i = 1; i < argc && argv[i][0] != '-'; i++);
4808 if(i < 3)
4809 goto printOptions;
4810 source_path = argv + 1;
4811 source = i - 2;
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004812
plougher4da4bd42010-11-21 05:01:54 +00004813 /*
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004814 * Scan the command line for -comp xxx option, this is to ensure
4815 * any -X compressor specific options are passed to the
4816 * correct compressor
plougher4da4bd42010-11-21 05:01:54 +00004817 */
plougher1f413c82005-11-18 00:02:14 +00004818 for(; i < argc; i++) {
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004819 struct compressor *prev_comp = comp;
4820
4821 if(strcmp(argv[i], "-comp") == 0) {
4822 if(++i == argc) {
4823 ERROR("%s: -comp missing compression type\n",
4824 argv[0]);
4825 exit(1);
4826 }
4827 comp = lookup_compressor(argv[i]);
4828 if(!comp->supported) {
4829 ERROR("%s: Compressor \"%s\" is not supported!"
4830 "\n", argv[0], argv[i]);
4831 ERROR("%s: Compressors available:\n", argv[0]);
4832 display_compressors("", COMP_DEFAULT);
4833 exit(1);
4834 }
4835 if(prev_comp != NULL && prev_comp != comp) {
4836 ERROR("%s: -comp multiple conflicting -comp"
4837 " options specified on command line"
4838 ", previously %s, now %s\n", argv[0],
4839 prev_comp->name, comp->name);
4840 exit(1);
4841 }
4842 compressor_opt_parsed = 1;
4843
4844 } else if(strcmp(argv[i], "-e") == 0)
4845 break;
4846 else if(strcmp(argv[i], "-root-becomes") == 0 ||
4847 strcmp(argv[i], "-ef") == 0 ||
4848 strcmp(argv[i], "-pf") == 0 ||
4849 strcmp(argv[i], "-af") == 0 ||
4850 strcmp(argv[i], "-comp") == 0)
4851 i++;
4852 }
4853
4854 /*
4855 * if no -comp option specified lookup default compressor. Note the
4856 * Makefile ensures the default compressor has been built, and so we
4857 * don't need to to check for failure here
4858 */
4859 if(comp == NULL)
4860 comp = lookup_compressor(COMP_DEFAULT);
4861
4862 for(i = source + 2; i < argc; i++) {
Phillip Lougher9523b042012-10-27 01:48:48 +01004863 if(strcmp(argv[i], "-action") == 0 ||
4864 strcmp(argv[i], "-a") ==0) {
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01004865 if(++i == argc) {
Phillip Lougher9523b042012-10-27 01:48:48 +01004866 ERROR("%s: %s missing action\n",
4867 argv[0], argv[i - 1]);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01004868 exit(1);
4869 }
4870 res = parse_action(argv[i]);
4871 if(res == 0)
4872 exit(1);
4873
Phillip Lougherb73caba2012-12-24 21:58:41 +00004874 } else if(strcmp(argv[i], "-af") == 0) {
4875 if(++i == argc) {
4876 ERROR("%s: -af missing filename\n", argv[0]);
4877 exit(1);
4878 }
4879 if(read_action_file(argv[i]) == FALSE)
4880 exit(1);
4881
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004882 } else if(strcmp(argv[i], "-comp") == 0)
4883 /* parsed previously */
4884 i++;
plougherb5576ea2010-11-22 01:06:53 +00004885
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004886 else if(strncmp(argv[i], "-X", 2) == 0) {
Phillip Lougher774b7b32014-01-07 04:47:13 +00004887 int args;
4888
4889 if(strcmp(argv[i] + 2, "help") == 0)
4890 goto print_compressor_options;
4891
4892 args = compressor_options(comp, argv + i, argc - i);
plougherd8865672010-11-27 05:05:49 +00004893 if(args < 0) {
4894 if(args == -1) {
4895 ERROR("%s: Unrecognised compressor"
4896 " option %s\n", argv[0],
4897 argv[i]);
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004898 if(!compressor_opt_parsed)
4899 ERROR("%s: Did you forget to"
4900 " specify -comp?\n",
4901 argv[0]);
Phillip Lougher774b7b32014-01-07 04:47:13 +00004902print_compressor_options:
Phillip Lougherb1c3b6a2014-01-06 05:30:00 +00004903 ERROR("%s: selected compressor \"%s\""
4904 ". Options supported: %s\n",
4905 argv[0], comp->name,
4906 comp->usage ? "" : "none");
4907 if(comp->usage)
4908 comp->usage();
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004909 }
plougherb5576ea2010-11-22 01:06:53 +00004910 exit(1);
4911 }
4912 i += args;
4913
plougherae9dcd82009-08-01 02:59:38 +00004914 } else if(strcmp(argv[i], "-pf") == 0) {
plougher43244f22009-04-05 02:04:51 +00004915 if(++i == argc) {
4916 ERROR("%s: -pf missing filename\n", argv[0]);
4917 exit(1);
4918 }
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00004919 if(read_pseudo_file(argv[i]) == FALSE)
plougher43244f22009-04-05 02:04:51 +00004920 exit(1);
plougher43244f22009-04-05 02:04:51 +00004921 } else if(strcmp(argv[i], "-p") == 0) {
4922 if(++i == argc) {
4923 ERROR("%s: -p missing pseudo file definition\n",
4924 argv[0]);
4925 exit(1);
4926 }
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00004927 if(read_pseudo_def(argv[i]) == FALSE)
plougher43244f22009-04-05 02:04:51 +00004928 exit(1);
plougher43244f22009-04-05 02:04:51 +00004929 } else if(strcmp(argv[i], "-recover") == 0) {
plougher99ac0cc2007-10-29 03:17:10 +00004930 if(++i == argc) {
plougherb3604122009-03-30 02:07:20 +00004931 ERROR("%s: -recover missing recovery file\n",
4932 argv[0]);
plougher99ac0cc2007-10-29 03:17:10 +00004933 exit(1);
4934 }
4935 read_recovery_data(argv[i], argv[source + 1]);
4936 } else if(strcmp(argv[i], "-no-recovery") == 0)
4937 recover = FALSE;
4938 else if(strcmp(argv[i], "-wildcards") == 0) {
plougher934a9ed2007-10-19 00:21:10 +00004939 old_exclude = FALSE;
4940 use_regex = FALSE;
4941 } else if(strcmp(argv[i], "-regex") == 0) {
4942 old_exclude = FALSE;
4943 use_regex = TRUE;
4944 } else if(strcmp(argv[i], "-no-sparse") == 0)
plougher1f54edc2007-08-12 23:13:36 +00004945 sparse_files = FALSE;
4946 else if(strcmp(argv[i], "-no-progress") == 0)
plougher02bc3bc2007-02-25 12:12:01 +00004947 progress = FALSE;
Phillip Lougherc6c73b22012-10-19 03:12:08 +01004948 else if(strcmp(argv[i], "-progress") == 0)
4949 force_progress = TRUE;
plougher02bc3bc2007-02-25 12:12:01 +00004950 else if(strcmp(argv[i], "-no-exports") == 0)
4951 exportable = FALSE;
plougher0e453652006-11-06 01:49:35 +00004952 else if(strcmp(argv[i], "-processors") == 0) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004953 if((++i == argc) || !parse_num(argv[i], &processors)) {
plougher360514a2009-03-30 03:01:38 +00004954 ERROR("%s: -processors missing or invalid "
4955 "processor number\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004956 exit(1);
4957 }
4958 if(processors < 1) {
plougher360514a2009-03-30 03:01:38 +00004959 ERROR("%s: -processors should be 1 or larger\n",
4960 argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004961 exit(1);
4962 }
plougher0e453652006-11-06 01:49:35 +00004963 } else if(strcmp(argv[i], "-read-queue") == 0) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004964 if((++i == argc) ||
4965 !parse_num(argv[i], &readb_mbytes)) {
plougher50b31762009-03-31 04:14:46 +00004966 ERROR("%s: -read-queue missing or invalid "
4967 "queue size\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004968 exit(1);
4969 }
4970 if(readb_mbytes < 1) {
plougher360514a2009-03-30 03:01:38 +00004971 ERROR("%s: -read-queue should be 1 megabyte or "
4972 "larger\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004973 exit(1);
4974 }
plougher0e453652006-11-06 01:49:35 +00004975 } else if(strcmp(argv[i], "-write-queue") == 0) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004976 if((++i == argc) ||
4977 !parse_num(argv[i], &writeb_mbytes)) {
plougher50b31762009-03-31 04:14:46 +00004978 ERROR("%s: -write-queue missing or invalid "
4979 "queue size\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004980 exit(1);
4981 }
4982 if(writeb_mbytes < 1) {
plougher50b31762009-03-31 04:14:46 +00004983 ERROR("%s: -write-queue should be 1 megabyte "
4984 "or larger\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004985 exit(1);
4986 }
plougher217bad82008-04-05 11:36:41 +00004987 } else if(strcmp(argv[i], "-fragment-queue") == 0) {
plougher360514a2009-03-30 03:01:38 +00004988 if((++i == argc) ||
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004989 !parse_num(argv[i],
4990 &fragmentb_mbytes)) {
plougher360514a2009-03-30 03:01:38 +00004991 ERROR("%s: -fragment-queue missing or invalid "
4992 "queue size\n", argv[0]);
plougher217bad82008-04-05 11:36:41 +00004993 exit(1);
4994 }
4995 if(fragmentb_mbytes < 1) {
plougher50b31762009-03-31 04:14:46 +00004996 ERROR("%s: -fragment-queue should be 1 "
4997 "megabyte or larger\n", argv[0]);
plougher217bad82008-04-05 11:36:41 +00004998 exit(1);
4999 }
plougher5507dd92006-11-06 00:43:10 +00005000 } else if(strcmp(argv[i], "-b") == 0) {
plougher4c99cb72007-06-14 21:46:31 +00005001 if(++i == argc) {
5002 ERROR("%s: -b missing block size\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005003 exit(1);
5004 }
Phillip Lougher94c1fe02012-11-28 03:32:36 +00005005 if(!parse_number(argv[i], &block_size, 1)) {
plougher4c99cb72007-06-14 21:46:31 +00005006 ERROR("%s: -b invalid block size\n", argv[0]);
5007 exit(1);
5008 }
plougher1f413c82005-11-18 00:02:14 +00005009 if((block_log = slog(block_size)) == 0) {
plougher50b31762009-03-31 04:14:46 +00005010 ERROR("%s: -b block size not power of two or "
5011 "not between 4096 and 1Mbyte\n",
5012 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005013 exit(1);
5014 }
5015 } else if(strcmp(argv[i], "-ef") == 0) {
5016 if(++i == argc) {
5017 ERROR("%s: -ef missing filename\n", argv[0]);
5018 exit(1);
5019 }
plougher9b5bf8c2006-03-20 18:43:33 +00005020 } else if(strcmp(argv[i], "-no-duplicates") == 0)
plougher1f413c82005-11-18 00:02:14 +00005021 duplicate_checking = FALSE;
5022
5023 else if(strcmp(argv[i], "-no-fragments") == 0)
5024 no_fragments = TRUE;
5025
5026 else if(strcmp(argv[i], "-always-use-fragments") == 0)
5027 always_use_fragments = TRUE;
5028
5029 else if(strcmp(argv[i], "-sort") == 0) {
5030 if(++i == argc) {
5031 ERROR("%s: -sort missing filename\n", argv[0]);
5032 exit(1);
5033 }
5034 } else if(strcmp(argv[i], "-all-root") == 0 ||
5035 strcmp(argv[i], "-root-owned") == 0)
5036 global_uid = global_gid = 0;
5037
5038 else if(strcmp(argv[i], "-force-uid") == 0) {
5039 if(++i == argc) {
plougher50b31762009-03-31 04:14:46 +00005040 ERROR("%s: -force-uid missing uid or user\n",
5041 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005042 exit(1);
5043 }
5044 if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') {
plougher360514a2009-03-30 03:01:38 +00005045 if(global_uid < 0 || global_uid >
5046 (((long long) 1 << 32) - 1)) {
plougher50b31762009-03-31 04:14:46 +00005047 ERROR("%s: -force-uid uid out of range"
5048 "\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005049 exit(1);
5050 }
5051 } else {
5052 struct passwd *uid = getpwnam(argv[i]);
5053 if(uid)
5054 global_uid = uid->pw_uid;
5055 else {
plougher360514a2009-03-30 03:01:38 +00005056 ERROR("%s: -force-uid invalid uid or "
5057 "unknown user\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005058 exit(1);
5059 }
5060 }
5061 } else if(strcmp(argv[i], "-force-gid") == 0) {
5062 if(++i == argc) {
plougher360514a2009-03-30 03:01:38 +00005063 ERROR("%s: -force-gid missing gid or group\n",
5064 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005065 exit(1);
5066 }
5067 if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') {
plougher360514a2009-03-30 03:01:38 +00005068 if(global_gid < 0 || global_gid >
5069 (((long long) 1 << 32) - 1)) {
plougher50b31762009-03-31 04:14:46 +00005070 ERROR("%s: -force-gid gid out of range"
5071 "\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005072 exit(1);
5073 }
5074 } else {
5075 struct group *gid = getgrnam(argv[i]);
5076 if(gid)
5077 global_gid = gid->gr_gid;
5078 else {
plougher360514a2009-03-30 03:01:38 +00005079 ERROR("%s: -force-gid invalid gid or "
5080 "unknown group\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005081 exit(1);
5082 }
5083 }
5084 } else if(strcmp(argv[i], "-noI") == 0 ||
5085 strcmp(argv[i], "-noInodeCompression") == 0)
5086 noI = TRUE;
5087
5088 else if(strcmp(argv[i], "-noD") == 0 ||
5089 strcmp(argv[i], "-noDataCompression") == 0)
5090 noD = TRUE;
5091
5092 else if(strcmp(argv[i], "-noF") == 0 ||
5093 strcmp(argv[i], "-noFragmentCompression") == 0)
5094 noF = TRUE;
5095
plougherb99d7832010-05-19 01:57:34 +00005096 else if(strcmp(argv[i], "-noX") == 0 ||
5097 strcmp(argv[i], "-noXattrCompression") == 0)
5098 noX = TRUE;
5099
plougherce564c62010-05-19 03:01:49 +00005100 else if(strcmp(argv[i], "-no-xattrs") == 0)
5101 no_xattrs = TRUE;
plougher6d89ac22010-05-19 02:59:23 +00005102
plougher30281c82010-08-25 05:06:11 +00005103 else if(strcmp(argv[i], "-xattrs") == 0)
5104 no_xattrs = FALSE;
5105
plougher1f413c82005-11-18 00:02:14 +00005106 else if(strcmp(argv[i], "-nopad") == 0)
5107 nopad = TRUE;
5108
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005109 else if(strcmp(argv[i], "-info") == 0)
plougher91fbb302008-05-06 02:29:36 +00005110 silent = FALSE;
plougher1f413c82005-11-18 00:02:14 +00005111
5112 else if(strcmp(argv[i], "-e") == 0)
5113 break;
5114
5115 else if(strcmp(argv[i], "-noappend") == 0)
5116 delete = TRUE;
5117
5118 else if(strcmp(argv[i], "-keep-as-directory") == 0)
5119 keep_as_directory = TRUE;
5120
Phillip Lougher85776df2014-01-31 03:10:46 +00005121 else if(strcmp(argv[i], "-exit-on-error") == 0)
5122 exit_on_error = TRUE;
5123
plougher1f413c82005-11-18 00:02:14 +00005124 else if(strcmp(argv[i], "-root-becomes") == 0) {
5125 if(++i == argc) {
plougher50b31762009-03-31 04:14:46 +00005126 ERROR("%s: -root-becomes: missing name\n",
5127 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005128 exit(1);
5129 }
5130 root_name = argv[i];
5131 } else if(strcmp(argv[i], "-version") == 0) {
5132 VERSION();
5133 } else {
5134 ERROR("%s: invalid option\n\n", argv[0]);
5135printOptions:
plougher360514a2009-03-30 03:01:38 +00005136 ERROR("SYNTAX:%s source1 source2 ... dest [options] "
5137 "[-e list of exclude\ndirs/files]\n", argv[0]);
plougher37632562009-08-07 19:09:01 +00005138 ERROR("\nFilesystem build options:\n");
plougherff5ea8b2009-08-07 19:33:10 +00005139 ERROR("-comp <comp>\t\tselect <comp> compression\n");
plougher13df1782009-08-29 01:05:34 +00005140 ERROR("\t\t\tCompressors available:\n");
plougher764dab52009-08-24 18:28:04 +00005141 display_compressors("\t\t\t", COMP_DEFAULT);
plougher50b31762009-03-31 04:14:46 +00005142 ERROR("-b <block_size>\t\tset data block to "
5143 "<block_size>. Default %d bytes\n",
5144 SQUASHFS_FILE_SIZE);
plougher37632562009-08-07 19:09:01 +00005145 ERROR("-no-exports\t\tdon't make the filesystem "
5146 "exportable via NFS\n");
5147 ERROR("-no-sparse\t\tdon't detect sparse files\n");
plougher07d25c22010-08-25 17:41:57 +00005148 ERROR("-no-xattrs\t\tdon't store extended attributes"
plougher30281c82010-08-25 05:06:11 +00005149 NOXOPT_STR "\n");
plougher07d25c22010-08-25 17:41:57 +00005150 ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR
plougher30281c82010-08-25 05:06:11 +00005151 "\n");
plougher1f413c82005-11-18 00:02:14 +00005152 ERROR("-noI\t\t\tdo not compress inode table\n");
5153 ERROR("-noD\t\t\tdo not compress data blocks\n");
5154 ERROR("-noF\t\t\tdo not compress fragment blocks\n");
plougher16111452010-07-22 05:12:18 +00005155 ERROR("-noX\t\t\tdo not compress extended "
5156 "attributes\n");
plougher1f413c82005-11-18 00:02:14 +00005157 ERROR("-no-fragments\t\tdo not use fragments\n");
plougher360514a2009-03-30 03:01:38 +00005158 ERROR("-always-use-fragments\tuse fragment blocks for "
5159 "files larger than block size\n");
5160 ERROR("-no-duplicates\t\tdo not perform duplicate "
5161 "checking\n");
plougher1f413c82005-11-18 00:02:14 +00005162 ERROR("-all-root\t\tmake all files owned by root\n");
5163 ERROR("-force-uid uid\t\tset all file uids to uid\n");
5164 ERROR("-force-gid gid\t\tset all file gids to gid\n");
plougher50b31762009-03-31 04:14:46 +00005165 ERROR("-nopad\t\t\tdo not pad filesystem to a multiple "
5166 "of 4K\n");
plougher37632562009-08-07 19:09:01 +00005167 ERROR("-keep-as-directory\tif one source directory is "
5168 "specified, create a root\n");
5169 ERROR("\t\t\tdirectory containing that directory, "
5170 "rather than the\n");
5171 ERROR("\t\t\tcontents of the directory\n");
5172 ERROR("\nFilesystem filter options:\n");
plougher16111452010-07-22 05:12:18 +00005173 ERROR("-p <pseudo-definition>\tAdd pseudo file "
5174 "definition\n");
5175 ERROR("-pf <pseudo-file>\tAdd list of pseudo file "
5176 "definitions\n");
plougher360514a2009-03-30 03:01:38 +00005177 ERROR("-sort <sort_file>\tsort files according to "
5178 "priorities in <sort_file>. One\n");
5179 ERROR("\t\t\tfile or dir with priority per line. "
5180 "Priority -32768 to\n");
plougher1f413c82005-11-18 00:02:14 +00005181 ERROR("\t\t\t32767, default priority 0\n");
plougher50b31762009-03-31 04:14:46 +00005182 ERROR("-ef <exclude_file>\tlist of exclude dirs/files."
5183 " One per line\n");
plougher360514a2009-03-30 03:01:38 +00005184 ERROR("-wildcards\t\tAllow extended shell wildcards "
5185 "(globbing) to be used in\n\t\t\texclude "
5186 "dirs/files\n");
plougher50b31762009-03-31 04:14:46 +00005187 ERROR("-regex\t\t\tAllow POSIX regular expressions to "
5188 "be used in exclude\n\t\t\tdirs/files\n");
plougher37632562009-08-07 19:09:01 +00005189 ERROR("\nFilesystem append options:\n");
5190 ERROR("-noappend\t\tdo not append to existing "
5191 "filesystem\n");
5192 ERROR("-root-becomes <name>\twhen appending source "
5193 "files/directories, make the\n");
5194 ERROR("\t\t\toriginal root become a subdirectory in "
5195 "the new root\n");
5196 ERROR("\t\t\tcalled <name>, rather than adding the new "
5197 "source items\n");
5198 ERROR("\t\t\tto the original root\n");
5199 ERROR("\nMksquashfs runtime options:\n");
5200 ERROR("-version\t\tprint version, licence and "
5201 "copyright message\n");
Phillip Lougher85776df2014-01-31 03:10:46 +00005202 ERROR("-exit-on-error\t\ttreat normally ignored errors "
5203 "as fatal\n");
plougher37632562009-08-07 19:09:01 +00005204 ERROR("-recover <name>\t\trecover filesystem data "
5205 "using recovery file <name>\n");
5206 ERROR("-no-recovery\t\tdon't generate a recovery "
5207 "file\n");
5208 ERROR("-info\t\t\tprint files written to filesystem\n");
5209 ERROR("-no-progress\t\tdon't display the progress "
5210 "bar\n");
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005211 ERROR("-progress\t\tdisplay progress bar when using "
5212 "the -info option\n");
plougher37632562009-08-07 19:09:01 +00005213 ERROR("-processors <number>\tUse <number> processors."
5214 " By default will use number of\n");
5215 ERROR("\t\t\tprocessors available\n");
5216 ERROR("-read-queue <size>\tSet input queue to <size> "
5217 "Mbytes. Default %d Mbytes\n",
5218 READER_BUFFER_DEFAULT);
5219 ERROR("-write-queue <size>\tSet output queue to <size> "
5220 "Mbytes. Default %d Mbytes\n",
5221 WRITER_BUFFER_DEFAULT);
plougher8bc376b2010-11-12 04:55:20 +00005222 ERROR("-fragment-queue <size>\tSet fragment queue to "
plougher37632562009-08-07 19:09:01 +00005223 "<size> Mbytes. Default %d Mbytes\n",
5224 FRAGMENT_BUFFER_DEFAULT);
5225 ERROR("\nMiscellaneous options:\n");
5226 ERROR("-root-owned\t\talternative name for -all-root"
5227 "\n");
5228 ERROR("-noInodeCompression\talternative name for -noI"
5229 "\n");
5230 ERROR("-noDataCompression\talternative name for -noD"
5231 "\n");
5232 ERROR("-noFragmentCompression\talternative name for "
5233 "-noF\n");
plougherb99d7832010-05-19 01:57:34 +00005234 ERROR("-noXattrCompression\talternative name for "
5235 "-noX\n");
Phillip Lougher774b7b32014-01-07 04:47:13 +00005236 ERROR("\n-Xhelp\t\t\tprint compressor options for"
5237 " selected compressor\n");
plougher4fb66822010-12-08 02:49:28 +00005238 ERROR("\nCompressors available and compressor specific "
5239 "options:\n");
5240 display_compressor_usage(COMP_DEFAULT);
plougher1f413c82005-11-18 00:02:14 +00005241 exit(1);
5242 }
5243 }
5244
plougherb747f4f2010-12-25 04:05:47 +00005245 /*
5246 * Some compressors may need the options to be checked for validity
5247 * once all the options have been processed
5248 */
5249 res = compressor_options_post(comp, block_size);
5250 if(res)
5251 EXIT_MKSQUASHFS();
5252
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005253 /*
5254 * If the -info option has been selected then disable the
5255 * progress bar unless it has been explicitly enabled with
5256 * the -progress option
5257 */
5258 if(!silent)
5259 progress = force_progress;
5260
5261#ifdef SQUASHFS_TRACE
Phillip Lougher989f5fe2013-04-22 03:16:29 +01005262 /*
5263 * Disable progress bar if full debug tracing is enabled.
5264 * The progress bar in this case just gets in the way of the
5265 * debug trace output
5266 */
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005267 progress = FALSE;
5268#endif
5269
plougher91fbb302008-05-06 02:29:36 +00005270 for(i = 0; i < source; i++)
5271 if(lstat(source_path[i], &source_buf) == -1) {
plougher360514a2009-03-30 03:01:38 +00005272 fprintf(stderr, "Cannot stat source directory \"%s\" "
plougher50b31762009-03-31 04:14:46 +00005273 "because %s\n", source_path[i],
5274 strerror(errno));
plougher91fbb302008-05-06 02:29:36 +00005275 EXIT_MKSQUASHFS();
5276 }
plougher324978d2006-02-27 04:53:29 +00005277
5278 destination_file = argv[source + 1];
plougher1f413c82005-11-18 00:02:14 +00005279 if(stat(argv[source + 1], &buf) == -1) {
5280 if(errno == ENOENT) { /* Does not exist */
plougher360514a2009-03-30 03:01:38 +00005281 fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR,
plougher59dce672010-05-19 03:56:59 +00005282 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
plougher360514a2009-03-30 03:01:38 +00005283 if(fd == -1) {
plougher1f413c82005-11-18 00:02:14 +00005284 perror("Could not create destination file");
5285 exit(1);
5286 }
5287 delete = TRUE;
5288 } else {
5289 perror("Could not stat destination file");
5290 exit(1);
5291 }
5292
5293 } else {
5294 if(S_ISBLK(buf.st_mode)) {
5295 if((fd = open(argv[source + 1], O_RDWR)) == -1) {
plougher50b31762009-03-31 04:14:46 +00005296 perror("Could not open block device as "
5297 "destination");
plougher1f413c82005-11-18 00:02:14 +00005298 exit(1);
5299 }
5300 block_device = 1;
5301
5302 } else if(S_ISREG(buf.st_mode)) {
plougher360514a2009-03-30 03:01:38 +00005303 fd = open(argv[source + 1], (delete ? O_TRUNC : 0) |
5304 O_RDWR);
5305 if(fd == -1) {
plougher50b31762009-03-31 04:14:46 +00005306 perror("Could not open regular file for "
5307 "writing as destination");
plougher1f413c82005-11-18 00:02:14 +00005308 exit(1);
5309 }
plougher44d54ef2010-02-08 22:13:49 +00005310 }
plougher1f413c82005-11-18 00:02:14 +00005311 else {
5312 ERROR("Destination not block device or regular file\n");
5313 exit(1);
5314 }
5315
plougher324978d2006-02-27 04:53:29 +00005316 }
plougher1f413c82005-11-18 00:02:14 +00005317
plougher16111452010-07-22 05:12:18 +00005318 /*
5319 * process the exclude files - must be done afer destination file has
5320 * been possibly created
5321 */
plougher1f413c82005-11-18 00:02:14 +00005322 for(i = source + 2; i < argc; i++)
Phillip Lougher386128f2012-12-16 05:23:45 +00005323 if(strcmp(argv[i], "-ef") == 0)
5324 /*
5325 * Note presence of filename arg has already
5326 * been checked
5327 */
5328 process_exclude_file(argv[++i]);
5329 else if(strcmp(argv[i], "-e") == 0)
plougher1f413c82005-11-18 00:02:14 +00005330 break;
plougher8b9a7f62009-08-01 22:52:45 +00005331 else if(strcmp(argv[i], "-root-becomes") == 0 ||
plougher43244f22009-04-05 02:04:51 +00005332 strcmp(argv[i], "-sort") == 0 ||
5333 strcmp(argv[i], "-pf") == 0 ||
Phillip Lougherb73caba2012-12-24 21:58:41 +00005334 strcmp(argv[i], "-af") == 0 ||
plougher8b9a7f62009-08-01 22:52:45 +00005335 strcmp(argv[i], "-comp") == 0)
plougher1f413c82005-11-18 00:02:14 +00005336 i++;
5337
5338 if(i != argc) {
5339 if(++i == argc) {
5340 ERROR("%s: -e missing arguments\n", argv[0]);
plougher324978d2006-02-27 04:53:29 +00005341 EXIT_MKSQUASHFS();
plougher1f413c82005-11-18 00:02:14 +00005342 }
plougher8f8e1a12007-10-18 02:50:21 +00005343 while(i < argc)
5344 if(old_exclude)
5345 old_add_exclude(argv[i++]);
5346 else
plougher05e50ef2007-10-23 12:34:20 +00005347 add_exclude(argv[i++]);
plougher1f413c82005-11-18 00:02:14 +00005348 }
5349
5350 /* process the sort files - must be done afer the exclude files */
5351 for(i = source + 2; i < argc; i++)
5352 if(strcmp(argv[i], "-sort") == 0) {
ploughere1c9a742010-07-21 16:59:05 +00005353 int res = read_sort_file(argv[++i], source,
5354 source_path);
5355 if(res == FALSE)
5356 BAD_ERROR("Failed to read sort file\n");
plougher1f413c82005-11-18 00:02:14 +00005357 sorted ++;
5358 } else if(strcmp(argv[i], "-e") == 0)
5359 break;
plougher8b9a7f62009-08-01 22:52:45 +00005360 else if(strcmp(argv[i], "-root-becomes") == 0 ||
plougher43244f22009-04-05 02:04:51 +00005361 strcmp(argv[i], "-ef") == 0 ||
5362 strcmp(argv[i], "-pf") == 0 ||
Phillip Lougherb73caba2012-12-24 21:58:41 +00005363 strcmp(argv[i], "-af") == 0 ||
plougher8b9a7f62009-08-01 22:52:45 +00005364 strcmp(argv[i], "-comp") == 0)
plougher1f413c82005-11-18 00:02:14 +00005365 i++;
5366
plougher4c99cb72007-06-14 21:46:31 +00005367 if(!delete) {
ploughera175ce22009-07-30 04:43:27 +00005368 comp = read_super(fd, &sBlk, argv[source + 1]);
5369 if(comp == NULL) {
plougher360514a2009-03-30 03:01:38 +00005370 ERROR("Failed to read existing filesystem - will not "
5371 "overwrite - ABORTING!\n");
plougher50b31762009-03-31 04:14:46 +00005372 ERROR("To force Mksquashfs to write to this block "
5373 "device or file use -noappend\n");
plougher4c99cb72007-06-14 21:46:31 +00005374 EXIT_MKSQUASHFS();
5375 }
plougher1f413c82005-11-18 00:02:14 +00005376
plougher1f413c82005-11-18 00:02:14 +00005377 block_log = slog(block_size = sBlk.block_size);
5378 noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags);
5379 noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags);
5380 noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags);
plougher89c7a512010-12-15 08:35:51 +00005381 noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags);
plougher1f413c82005-11-18 00:02:14 +00005382 no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags);
5383 always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags);
5384 duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags);
plougher0e453652006-11-06 01:49:35 +00005385 exportable = SQUASHFS_EXPORTABLE(sBlk.flags);
plougherafae8252010-12-15 09:12:58 +00005386 no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags);
plougher3d7e5182010-12-25 01:49:42 +00005387 comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags);
plougher4c99cb72007-06-14 21:46:31 +00005388 }
5389
Phillip Lougherc97dac52013-04-14 03:32:10 +01005390 initialise_threads(readb_mbytes, writeb_mbytes, fragmentb_mbytes,
5391 delete);
plougher4c99cb72007-06-14 21:46:31 +00005392
plougher13fdddf2010-11-24 01:23:41 +00005393 res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0);
5394 if(res)
5395 BAD_ERROR("compressor_init failed\n");
5396
plougher4c99cb72007-06-14 21:46:31 +00005397 if(delete) {
plougher3d7e5182010-12-25 01:49:42 +00005398 int size;
Phillip Loughera45c9d22011-02-20 04:24:07 +00005399 void *comp_data = compressor_dump_options(comp, block_size,
5400 &size);
plougher3d7e5182010-12-25 01:49:42 +00005401
plougherdf6d8f02009-03-20 03:10:00 +00005402 printf("Creating %d.%d filesystem on %s, block size %d.\n",
plougher978f5882010-12-28 04:34:30 +00005403 SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size);
plougher3d7e5182010-12-25 01:49:42 +00005404
plougher871c72a2010-12-25 04:17:27 +00005405 /*
5406 * store any compressor specific options after the superblock,
5407 * and set the COMP_OPT flag to show that the filesystem has
5408 * compressor specfic options
5409 */
plougher3d7e5182010-12-25 01:49:42 +00005410 if(comp_data) {
5411 unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT;
5412
5413 SQUASHFS_INSWAP_SHORTS(&c_byte, 1);
plougher64e83fd2010-12-31 21:21:26 +00005414 write_destination(fd, sizeof(struct squashfs_super_block),
plougher29e2ace2010-12-31 08:50:00 +00005415 sizeof(c_byte), &c_byte);
plougher64e83fd2010-12-31 21:21:26 +00005416 write_destination(fd, sizeof(struct squashfs_super_block) +
plougher8e4dad42010-12-28 05:56:22 +00005417 sizeof(c_byte), size, comp_data);
plougher64e83fd2010-12-31 21:21:26 +00005418 bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte)
plougher3d7e5182010-12-25 01:49:42 +00005419 + size;
5420 comp_opts = TRUE;
plougher3d7e5182010-12-25 01:49:42 +00005421 } else
plougher64e83fd2010-12-31 21:21:26 +00005422 bytes = sizeof(struct squashfs_super_block);
plougher4c99cb72007-06-14 21:46:31 +00005423 } else {
plougher360514a2009-03-30 03:01:38 +00005424 unsigned int last_directory_block, inode_dir_offset,
5425 inode_dir_file_size, root_inode_size,
plougher50b31762009-03-31 04:14:46 +00005426 inode_dir_start_block, uncompressed_data,
5427 compressed_data, inode_dir_inode_number,
5428 inode_dir_parent_inode;
plougher360514a2009-03-30 03:01:38 +00005429 unsigned int root_inode_start =
5430 SQUASHFS_INODE_BLK(sBlk.root_inode),
5431 root_inode_offset =
5432 SQUASHFS_INODE_OFFSET(sBlk.root_inode);
Phillip Lougherb1259f72013-06-06 03:10:15 +01005433 sigset_t sigmask;
plougher4c99cb72007-06-14 21:46:31 +00005434
plougher360514a2009-03-30 03:01:38 +00005435 if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table,
5436 &data_cache, &directory_table,
5437 &directory_data_cache, &last_directory_block,
5438 &inode_dir_offset, &inode_dir_file_size,
5439 &root_inode_size, &inode_dir_start_block,
5440 &file_count, &sym_count, &dev_count, &dir_count,
5441 &fifo_count, &sock_count, &total_bytes,
5442 &total_inode_bytes, &total_directory_bytes,
plougher50b31762009-03-31 04:14:46 +00005443 &inode_dir_inode_number,
5444 &inode_dir_parent_inode, add_old_root_entry,
5445 &fragment_table, &inode_lookup_table)) == 0) {
plougher360514a2009-03-30 03:01:38 +00005446 ERROR("Failed to read existing filesystem - will not "
5447 "overwrite - ABORTING!\n");
plougher50b31762009-03-31 04:14:46 +00005448 ERROR("To force Mksquashfs to write to this block "
5449 "device or file use -noappend\n");
plougher324978d2006-02-27 04:53:29 +00005450 EXIT_MKSQUASHFS();
plougher1f413c82005-11-18 00:02:14 +00005451 }
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00005452 if((append_fragments = fragments = sBlk.fragments)) {
plougher360514a2009-03-30 03:01:38 +00005453 fragment_table = realloc((char *) fragment_table,
plougher50b31762009-03-31 04:14:46 +00005454 ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1))
plougher8ed84b92010-12-31 10:37:24 +00005455 * sizeof(struct squashfs_fragment_entry));
plougher6f2a8262010-07-27 00:37:19 +00005456 if(fragment_table == NULL)
5457 BAD_ERROR("Out of memory in save filesystem state\n");
5458 }
plougher1f413c82005-11-18 00:02:14 +00005459
plougher50b31762009-03-31 04:14:46 +00005460 printf("Appending to existing %d.%d filesystem on %s, block "
plougher978f5882010-12-28 04:34:30 +00005461 "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1],
plougher360514a2009-03-30 03:01:38 +00005462 block_size);
plougher89c7a512010-12-15 08:35:51 +00005463 printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, "
plougherbb988032009-08-06 08:46:34 +00005464 "-always-use-fragments,\n-exportable and -comp options "
5465 "ignored\n");
plougher360514a2009-03-30 03:01:38 +00005466 printf("\nIf appending is not wanted, please re-run with "
5467 "-noappend specified!\n\n");
plougher1f413c82005-11-18 00:02:14 +00005468
plougher360514a2009-03-30 03:01:38 +00005469 compressed_data = (inode_dir_offset + inode_dir_file_size) &
5470 ~(SQUASHFS_METADATA_SIZE - 1);
5471 uncompressed_data = (inode_dir_offset + inode_dir_file_size) &
5472 (SQUASHFS_METADATA_SIZE - 1);
plougher1f413c82005-11-18 00:02:14 +00005473
5474 /* save original filesystem state for restoring ... */
5475 sfragments = fragments;
5476 sbytes = bytes;
5477 sinode_count = sBlk.inodes;
plougher23377982007-11-12 04:04:48 +00005478 scache_bytes = root_inode_offset + root_inode_size;
5479 sdirectory_cache_bytes = uncompressed_data;
ploughera2968ef2009-03-03 10:46:00 +00005480 sdata_cache = malloc(scache_bytes);
plougher332e43d2010-07-21 01:18:30 +00005481 if(sdata_cache == NULL)
5482 BAD_ERROR("Out of memory in save filesystem state\n");
ploughera2968ef2009-03-03 10:46:00 +00005483 sdirectory_data_cache = malloc(sdirectory_cache_bytes);
plougher332e43d2010-07-21 01:18:30 +00005484 if(sdirectory_data_cache == NULL)
5485 BAD_ERROR("Out of memory in save filesystem state\n");
plougher1f413c82005-11-18 00:02:14 +00005486 memcpy(sdata_cache, data_cache, scache_bytes);
plougher360514a2009-03-30 03:01:38 +00005487 memcpy(sdirectory_data_cache, directory_data_cache +
5488 compressed_data, sdirectory_cache_bytes);
plougher1f413c82005-11-18 00:02:14 +00005489 sinode_bytes = root_inode_start;
plougher1f413c82005-11-18 00:02:14 +00005490 stotal_bytes = total_bytes;
5491 stotal_inode_bytes = total_inode_bytes;
plougher50b31762009-03-31 04:14:46 +00005492 stotal_directory_bytes = total_directory_bytes +
5493 compressed_data;
plougher1f413c82005-11-18 00:02:14 +00005494 sfile_count = file_count;
5495 ssym_count = sym_count;
5496 sdev_count = dev_count;
5497 sdir_count = dir_count + 1;
5498 sfifo_count = fifo_count;
5499 ssock_count = sock_count;
5500 sdup_files = dup_files;
plougher1b899fc2008-08-07 01:24:06 +00005501 sid_count = id_count;
plougher99ac0cc2007-10-29 03:17:10 +00005502 write_recovery_data(&sBlk);
Phillip Lougher3bcf67d2013-02-24 10:56:38 +00005503 save_xattrs();
Phillip Lougher0280d992014-01-27 05:54:07 +00005504 restore_thread = init_restore_thread();
Phillip Loughera709bff2013-03-28 17:48:31 +00005505 sigemptyset(&sigmask);
5506 sigaddset(&sigmask, SIGINT);
5507 sigaddset(&sigmask, SIGTERM);
Phillip Lougherdd343392013-03-31 23:29:03 +01005508 sigaddset(&sigmask, SIGUSR1);
Phillip Lougherb1259f72013-06-06 03:10:15 +01005509 if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
Phillip Loughera709bff2013-03-28 17:48:31 +00005510 BAD_ERROR("Failed to set signal mask\n");
plougher0dd6f122009-03-29 21:43:57 +00005511 write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0");
plougher1f413c82005-11-18 00:02:14 +00005512
plougher360514a2009-03-30 03:01:38 +00005513 /*
5514 * set the filesystem state up to be able to append to the
plougher50b31762009-03-31 04:14:46 +00005515 * original filesystem. The filesystem state differs depending
5516 * on whether we're appending to the original root directory, or
5517 * if the original root directory becomes a sub-directory
5518 * (root-becomes specified on command line, here root_name !=
5519 * NULL)
plougher1f413c82005-11-18 00:02:14 +00005520 */
5521 inode_bytes = inode_size = root_inode_start;
5522 directory_size = last_directory_block;
5523 cache_size = root_inode_offset + root_inode_size;
5524 directory_cache_size = inode_dir_offset + inode_dir_file_size;
5525 if(root_name) {
plougherca2c93f2008-08-15 08:34:57 +00005526 sdirectory_bytes = last_directory_block;
5527 sdirectory_compressed_bytes = 0;
plougherdf70c3e2006-01-27 09:34:13 +00005528 root_inode_number = inode_dir_parent_inode;
Phillip Lougher539c2b12012-07-30 20:14:52 +01005529 inode_no = sBlk.inodes + 2;
plougher1f413c82005-11-18 00:02:14 +00005530 directory_bytes = last_directory_block;
5531 directory_cache_bytes = uncompressed_data;
plougher360514a2009-03-30 03:01:38 +00005532 memmove(directory_data_cache, directory_data_cache +
5533 compressed_data, uncompressed_data);
plougher1f413c82005-11-18 00:02:14 +00005534 cache_bytes = root_inode_offset + root_inode_size;
plougher360514a2009-03-30 03:01:38 +00005535 add_old_root_entry(root_name, sBlk.root_inode,
5536 inode_dir_inode_number, SQUASHFS_DIR_TYPE);
plougher1f413c82005-11-18 00:02:14 +00005537 total_directory_bytes += compressed_data;
5538 dir_count ++;
5539 } else {
plougher360514a2009-03-30 03:01:38 +00005540 sdirectory_compressed_bytes = last_directory_block -
5541 inode_dir_start_block;
5542 sdirectory_compressed =
5543 malloc(sdirectory_compressed_bytes);
plougher332e43d2010-07-21 01:18:30 +00005544 if(sdirectory_compressed == NULL)
plougher16111452010-07-22 05:12:18 +00005545 BAD_ERROR("Out of memory in save filesystem "
5546 "state\n");
plougher360514a2009-03-30 03:01:38 +00005547 memcpy(sdirectory_compressed, directory_table +
5548 inode_dir_start_block,
5549 sdirectory_compressed_bytes);
plougherca2c93f2008-08-15 08:34:57 +00005550 sdirectory_bytes = inode_dir_start_block;
plougherdf70c3e2006-01-27 09:34:13 +00005551 root_inode_number = inode_dir_inode_number;
Phillip Lougher539c2b12012-07-30 20:14:52 +01005552 inode_no = sBlk.inodes + 1;
plougher1f413c82005-11-18 00:02:14 +00005553 directory_bytes = inode_dir_start_block;
5554 directory_cache_bytes = inode_dir_offset;
5555 cache_bytes = root_inode_offset;
5556 }
5557
plougher360514a2009-03-30 03:01:38 +00005558 inode_count = file_count + dir_count + sym_count + dev_count +
5559 fifo_count + sock_count;
plougher1f413c82005-11-18 00:02:14 +00005560 }
5561
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00005562 if(path)
5563 paths = add_subdir(paths, path);
plougherf9039c92007-10-22 03:54:16 +00005564
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01005565 dump_actions();
5566
plougher360514a2009-03-30 03:01:38 +00005567 if(delete && !keep_as_directory && source == 1 &&
5568 S_ISDIR(source_buf.st_mode))
Phillip Lougherbae0e422014-01-19 23:42:11 +00005569 dir_scan(&inode, source_path[0], scan1_readdir, progress);
plougher50b31762009-03-31 04:14:46 +00005570 else if(!keep_as_directory && source == 1 &&
5571 S_ISDIR(source_buf.st_mode))
Phillip Lougherbae0e422014-01-19 23:42:11 +00005572 dir_scan(&inode, source_path[0], scan1_single_readdir, progress);
plougher1f413c82005-11-18 00:02:14 +00005573 else
Phillip Lougherbae0e422014-01-19 23:42:11 +00005574 dir_scan(&inode, "", scan1_encomp_readdir, progress);
plougher1f413c82005-11-18 00:02:14 +00005575 sBlk.root_inode = inode;
5576 sBlk.inodes = inode_count;
5577 sBlk.s_magic = SQUASHFS_MAGIC;
5578 sBlk.s_major = SQUASHFS_MAJOR;
plougher978f5882010-12-28 04:34:30 +00005579 sBlk.s_minor = SQUASHFS_MINOR;
plougher1f413c82005-11-18 00:02:14 +00005580 sBlk.block_size = block_size;
5581 sBlk.block_log = block_log;
plougher89c7a512010-12-15 08:35:51 +00005582 sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments,
plougherafae8252010-12-15 09:12:58 +00005583 always_use_fragments, duplicate_checking, exportable,
plougher3d7e5182010-12-25 01:49:42 +00005584 no_xattrs, comp_opts);
plougher1f413c82005-11-18 00:02:14 +00005585 sBlk.mkfs_time = time(NULL);
5586
Phillip Lougher83b8f862013-04-09 03:28:55 +01005587 disable_info();
plougherc9b11db2008-05-06 23:59:15 +00005588
Phillip Loughera709bff2013-03-28 17:48:31 +00005589 while((fragment = get_frag_action(fragment)))
5590 write_fragment(*fragment);
5591 unlock_fragments();
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01005592 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
Phillip Loughera709bff2013-03-28 17:48:31 +00005593 pthread_mutex_lock(&fragment_mutex);
5594 while(fragments_outstanding) {
5595 pthread_mutex_unlock(&fragment_mutex);
5596 sched_yield();
5597 pthread_mutex_lock(&fragment_mutex);
5598 }
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01005599 pthread_cleanup_pop(1);
Phillip Loughera709bff2013-03-28 17:48:31 +00005600
5601 queue_put(to_writer, NULL);
5602 if(queue_get(from_writer) != 0)
5603 EXIT_MKSQUASHFS();
5604
Phillip Loughercd1a5a62014-01-21 04:53:46 +00005605 set_progressbar_state(FALSE);
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00005606 write_filesystem_tables(&sBlk, nopad);
plougher99ac0cc2007-10-29 03:17:10 +00005607
plougher1f413c82005-11-18 00:02:14 +00005608 return 0;
5609}