blob: cd1a5820be568b01ffaddf2811774d8132fc249c [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"
Phillip Lougher9de84ac2014-02-20 05:18:48 +000078#include "process_fragments.h"
plougher860c1f32010-08-11 01:37:17 +000079
plougher324978d2006-02-27 04:53:29 +000080int delete = FALSE;
plougher1f413c82005-11-18 00:02:14 +000081int fd;
Phillip Loughera709bff2013-03-28 17:48:31 +000082struct squashfs_super_block sBlk;
plougher1f413c82005-11-18 00:02:14 +000083
84/* filesystem flags for building */
plougher3d7e5182010-12-25 01:49:42 +000085int comp_opts = FALSE;
Phillip Lougher434d50c2014-01-21 03:05:36 +000086int no_xattrs = XATTR_DEF;
87int noX = FALSE;
88int duplicate_checking = TRUE;
89int noF = FALSE;
90int no_fragments = FALSE;
91int always_use_fragments = FALSE;
92int noI = FALSE;
93int noD = FALSE;
plougher1f288f62009-02-21 03:05:52 +000094int silent = TRUE;
plougher02bc3bc2007-02-25 12:12:01 +000095int exportable = TRUE;
plougher8dcc6992007-08-17 19:32:52 +000096int sparse_files = TRUE;
plougher8f8e1a12007-10-18 02:50:21 +000097int old_exclude = TRUE;
98int use_regex = FALSE;
Phillip Loughera709bff2013-03-28 17:48:31 +000099int nopad = FALSE;
Phillip Lougher85776df2014-01-31 03:10:46 +0000100int exit_on_error = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000101
Phillip Lougher434d50c2014-01-21 03:05:36 +0000102long long global_uid = -1, global_gid = -1;
103
plougher1f413c82005-11-18 00:02:14 +0000104/* superblock attributes */
105int block_size = SQUASHFS_FILE_SIZE, block_log;
plougher1b899fc2008-08-07 01:24:06 +0000106unsigned int id_count = 0;
plougherfd57dfe2009-03-30 01:17:52 +0000107int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0,
108 sock_count = 0;
plougher1f413c82005-11-18 00:02:14 +0000109
110/* write position within data section */
111long long bytes = 0, total_bytes = 0;
112
113/* in memory directory table - possibly compressed */
114char *directory_table = NULL;
115unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0;
116
117/* cached directory table */
118char *directory_data_cache = NULL;
119unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
120
121/* in memory inode table - possibly compressed */
122char *inode_table = NULL;
123unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0;
124
125/* cached inode table */
126char *data_cache = NULL;
127unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
128
plougher0e453652006-11-06 01:49:35 +0000129/* inode lookup table */
130squashfs_inode *inode_lookup_table = NULL;
131
plougher1f413c82005-11-18 00:02:14 +0000132/* in memory directory data */
133#define I_COUNT_SIZE 128
134#define DIR_ENTRIES 32
135#define INODE_HASH_SIZE 65536
136#define INODE_HASH_MASK (INODE_HASH_SIZE - 1)
137#define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK)
138
139struct cached_dir_index {
plougher2bd2b722010-12-31 10:52:15 +0000140 struct squashfs_dir_index index;
141 char *name;
plougher1f413c82005-11-18 00:02:14 +0000142};
143
144struct directory {
145 unsigned int start_block;
146 unsigned int size;
147 unsigned char *buff;
148 unsigned char *p;
149 unsigned int entry_count;
150 unsigned char *entry_count_p;
151 unsigned int i_count;
152 unsigned int i_size;
153 struct cached_dir_index *index;
154 unsigned char *index_count_p;
155 unsigned int inode_number;
156};
157
plougher1f413c82005-11-18 00:02:14 +0000158struct inode_info *inode_info[INODE_HASH_SIZE];
159
160/* hash tables used to do fast duplicate searches in duplicate check */
plougher5507dd92006-11-06 00:43:10 +0000161struct file_info *dupl[65536];
plougher1f413c82005-11-18 00:02:14 +0000162int dup_files = 0;
163
plougher8f8e1a12007-10-18 02:50:21 +0000164/* exclude file handling */
plougher1f413c82005-11-18 00:02:14 +0000165/* list of exclude dirs/files */
166struct exclude_info {
167 dev_t st_dev;
168 ino_t st_ino;
169};
170
171#define EXCLUDE_SIZE 8192
172int exclude = 0;
173struct exclude_info *exclude_paths = NULL;
plougher8f8e1a12007-10-18 02:50:21 +0000174int old_excluded(char *filename, struct stat *buf);
175
176struct path_entry {
177 char *name;
178 regex_t *preg;
179 struct pathname *paths;
180};
181
182struct pathname {
183 int names;
184 struct path_entry *name;
185};
186
plougherf9039c92007-10-22 03:54:16 +0000187struct pathnames {
188 int count;
189 struct pathname *path[0];
190};
191#define PATHS_ALLOC_SIZE 10
192
193struct pathnames *paths = NULL;
194struct pathname *path = NULL;
plougher05e50ef2007-10-23 12:34:20 +0000195struct pathname *stickypath = NULL;
Phillip Lougherb2abb1c2013-01-09 04:51:21 +0000196int excluded(char *name, struct pathnames *paths, struct pathnames **new);
plougher1f413c82005-11-18 00:02:14 +0000197
plougher1f413c82005-11-18 00:02:14 +0000198int fragments = 0;
plougher76c64082008-03-08 01:32:23 +0000199
plougher1f413c82005-11-18 00:02:14 +0000200#define FRAG_SIZE 32768
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 */
Phillip Lougher3794e342014-04-10 02:15:45 +0100225int appending = FALSE;
plougherca2c93f2008-08-15 08:34:57 +0000226char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed;
plougher1f413c82005-11-18 00:02:14 +0000227
228long long sbytes, stotal_bytes;
229
230unsigned int sinode_bytes, scache_bytes, sdirectory_bytes,
plougherca2c93f2008-08-15 08:34:57 +0000231 sdirectory_cache_bytes, sdirectory_compressed_bytes,
plougher1f413c82005-11-18 00:02:14 +0000232 stotal_inode_bytes, stotal_directory_bytes,
plougher0e453652006-11-06 01:49:35 +0000233 sinode_count = 0, sfile_count, ssym_count, sdev_count,
plougher1f413c82005-11-18 00:02:14 +0000234 sdir_count, sfifo_count, ssock_count, sdup_files;
235int sfragments;
plougher5507dd92006-11-06 00:43:10 +0000236int threads;
plougher1f413c82005-11-18 00:02:14 +0000237
238/* flag whether destination file is a block device */
Phillip Lougher434d50c2014-01-21 03:05:36 +0000239int block_device = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000240
241/* flag indicating whether files are sorted using sort list(s) */
Phillip Lougher434d50c2014-01-21 03:05:36 +0000242int sorted = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000243
plougher324978d2006-02-27 04:53:29 +0000244/* save destination file name for deleting on error */
245char *destination_file = NULL;
246
plougher99ac0cc2007-10-29 03:17:10 +0000247/* recovery file for abnormal exit on appending */
Phillip Lougheraf4c9232012-11-15 03:46:28 +0000248char *recovery_file = NULL;
plougher99ac0cc2007-10-29 03:17:10 +0000249int recover = TRUE;
250
plougher1b899fc2008-08-07 01:24:06 +0000251struct id *id_hash_table[ID_ENTRIES];
252struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS];
253unsigned int uid_count = 0, guid_count = 0;
254unsigned int sid_count = 0, suid_count = 0, sguid_count = 0;
plougher5507dd92006-11-06 00:43:10 +0000255
Phillip Lougher943acad2014-04-17 03:31:01 +0100256struct cache *reader_buffer, *fragment_buffer, *reserve_cache;
257struct cache *bwriter_buffer, *fwriter_buffer;
Phillip Loughercf478e92013-05-29 02:38:41 +0100258struct queue *to_reader, *to_deflate, *to_writer, *from_writer,
Phillip Lougher9de84ac2014-02-20 05:18:48 +0000259 *to_frag, *locked_fragment, *to_process_frag;
Phillip Lougher0e1656d2013-05-20 03:47:24 +0100260struct seq_queue *to_main;
Phillip Lougher0280d992014-01-27 05:54:07 +0000261pthread_t reader_thread, writer_thread, main_thread;
Phillip Lougher9de84ac2014-02-20 05:18:48 +0000262pthread_t *deflator_thread, *frag_deflator_thread, *frag_thread;
Phillip Loughercf135d32013-04-08 03:43:25 +0100263pthread_t *restore_thread = NULL;
Phillip Lougher94e658f2014-03-13 02:59:04 +0000264pthread_mutex_t fragment_mutex = PTHREAD_MUTEX_INITIALIZER;
Phillip Lougherea8cb652014-03-13 02:57:19 +0000265pthread_mutex_t pos_mutex = PTHREAD_MUTEX_INITIALIZER;
Phillip Lougher8bb17b02014-03-30 23:59:55 +0100266pthread_mutex_t dup_mutex = PTHREAD_MUTEX_INITIALIZER;
plougher5507dd92006-11-06 00:43:10 +0000267
268/* user options that control parallelisation */
269int processors = -1;
Phillip Lougherc3af83a2014-04-21 21:38:16 +0100270int bwriter_size;
plougher5507dd92006-11-06 00:43:10 +0000271
plougherc5d59872010-11-22 01:36:01 +0000272/* compression operations */
Phillip Lougher8bb17b02014-03-30 23:59:55 +0100273struct compressor *comp = NULL;
Phillip Lougher434d50c2014-01-21 03:05:36 +0000274int compressor_opt_parsed = FALSE;
plougher13fdddf2010-11-24 01:23:41 +0000275void *stream = NULL;
plougher7b8ee502009-07-29 07:54:30 +0000276
plougher860c1f32010-08-11 01:37:17 +0000277/* xattr stats */
278unsigned int xattr_bytes = 0, total_xattr_bytes = 0;
279
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +0000280/* fragment to file mapping used when appending */
281int append_fragments = 0;
282struct append_file **file_mapping;
283
Phillip Lougher8bb17b02014-03-30 23:59:55 +0100284static char *read_from_disk(long long start, unsigned int avail_bytes);
plougherfd57dfe2009-03-30 01:17:52 +0000285void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
286 int type);
plougherfd57dfe2009-03-30 01:17:52 +0000287struct file_info *duplicate(long long file_size, long long bytes,
288 unsigned int **block_list, long long *start, struct fragment **fragment,
289 struct file_buffer *file_buffer, int blocks, unsigned short checksum,
Phillip Lougherc6424dc2014-03-13 02:01:28 +0000290 int checksum_flag);
Phillip Lougherb38c1722012-02-09 23:26:19 +0000291struct dir_info *dir_scan1(char *, char *, struct pathnames *,
Phillip Lougher494479f2012-02-03 15:45:41 +0000292 struct dir_ent *(_readdir)(struct dir_info *), int);
Phillip Lougher24eeb772012-10-13 01:56:24 +0100293void dir_scan2(struct dir_info *dir, struct pseudo *pseudo);
Phillip Lougher23d83622012-10-14 02:35:22 +0100294void dir_scan3(struct dir_info *root, struct dir_info *dir);
295void dir_scan4(struct dir_info *dir);
Phillip Lougher2abfcf72012-11-11 03:59:56 +0000296void dir_scan5(struct dir_info *dir);
297void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info);
plougherfd57dfe2009-03-30 01:17:52 +0000298struct file_info *add_non_dup(long long file_size, long long bytes,
299 unsigned int *block_list, long long start, struct fragment *fragment,
300 unsigned short checksum, unsigned short fragment_checksum,
Phillip Lougherc6424dc2014-03-13 02:01:28 +0000301 int checksum_flag, int checksum_frag_flag);
ploughera0a49c32010-08-11 01:47:59 +0000302long long generic_write_table(int, void *, int, void *, int);
plougherca61d1c2010-07-20 18:32:32 +0000303void restorefs();
Phillip Lougher1eae83d2012-09-26 02:12:45 +0100304struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth);
Phillip Loughera709bff2013-03-28 17:48:31 +0000305void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad);
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +0000306unsigned short get_checksum_mem(char *buff, int bytes);
Phillip Lougherc3af83a2014-04-21 21:38:16 +0100307int get_physical_memory();
plougher5507dd92006-11-06 00:43:10 +0000308
309
Phillip Lougher9b7c3c22013-03-10 02:29:42 +0000310void prep_exit()
Phillip Lougher838d2962012-10-23 03:29:30 +0100311{
Phillip Loughercf135d32013-04-08 03:43:25 +0100312 if(restore_thread) {
313 if(pthread_self() == *restore_thread) {
314 /*
315 * Recursive failure when trying to restore filesystem!
316 * Nothing to do except to exit, otherwise we'll just
317 * appear to hang. The user should be able to restore
318 * from the recovery file (which is why it was added, in
319 * case of catastrophic failure in Mksquashfs)
320 */
321 exit(1);
322 } else {
323 /* signal the restore thread to restore */
Phillip Lougher8c740812014-01-30 01:56:17 +0000324 pthread_kill(*restore_thread, SIGUSR1);
Phillip Loughercf135d32013-04-08 03:43:25 +0100325 pthread_exit(NULL);
326 }
Phillip Lougher3794e342014-04-10 02:15:45 +0100327 } else if(delete) {
328 if(destination_file && !block_device)
329 unlink(destination_file);
330 } else if(recovery_file)
331 unlink(recovery_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
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001319static struct 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;
plougher76c64082008-03-08 01:32:23 +00001322 struct file_buffer *buffer, *compressed_buffer;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001323 long long start_block;
1324 int res, size, index = fragment->index;
1325 char locked;
1326
1327 /*
1328 * Lookup fragment block in cache.
1329 * If the fragment block doesn't exist, then get the compressed version
1330 * from the writer cache or off disk, and decompress it.
1331 *
1332 * This routine has two things which complicate the code:
1333 *
1334 * 1. Multiple threads can simultaneously lookup/create the
1335 * same buffer. This means a buffer needs to be "locked"
1336 * when it is being filled in, to prevent other threads from
1337 * using it when it is not ready. This is because we now do
1338 * fragment duplicate checking in parallel.
1339 * 2. We have two caches which need to be checked for the
1340 * presence of fragment blocks: the normal fragment cache
1341 * and a "reserve" cache. The reserve cache is used to
1342 * prevent an unnecessary pipeline stall when the fragment cache
1343 * is full of fragments waiting to be compressed.
1344 */
plougher5507dd92006-11-06 00:43:10 +00001345
plougher76c64082008-03-08 01:32:23 +00001346 if(fragment->index == SQUASHFS_INVALID_FRAG)
1347 return NULL;
plougher5507dd92006-11-06 00:43:10 +00001348
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001349 pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1350 pthread_mutex_lock(&dup_mutex);
1351
1352again:
1353 buffer = cache_lookup_nowait(fragment_buffer, index, &locked);
1354 if(buffer) {
1355 pthread_mutex_unlock(&dup_mutex);
1356 if(locked)
1357 /* got a buffer being filled in. Wait for it */
1358 cache_wait_unlock(buffer);
1359 goto finished;
1360 }
1361
1362 /* not in fragment cache, is it in the reserve cache? */
1363 buffer = cache_lookup_nowait(reserve_cache, index, &locked);
1364 if(buffer) {
1365 pthread_mutex_unlock(&dup_mutex);
1366 if(locked)
1367 /* got a buffer being filled in. Wait for it */
1368 cache_wait_unlock(buffer);
1369 goto finished;
1370 }
1371
1372 /* in neither cache, try to get it from the fragment cache */
1373 buffer = cache_get_nowait(fragment_buffer, index);
1374 if(!buffer) {
1375 /*
1376 * no room, get it from the reserve cache, this is
1377 * dimensioned so it will always have space (no more than
1378 * processors + 1 can have an outstanding reserve buffer)
1379 */
1380 buffer = cache_get_nowait(reserve_cache, index);
1381 if(!buffer) {
1382 /* failsafe */
1383 ERROR("no space in reserve cache\n");
1384 goto again;
1385 }
1386 }
1387
1388 pthread_mutex_unlock(&dup_mutex);
plougher76c64082008-03-08 01:32:23 +00001389
Phillip Lougher943acad2014-04-17 03:31:01 +01001390 compressed_buffer = cache_lookup(fwriter_buffer, index);
plougher5507dd92006-11-06 00:43:10 +00001391
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001392 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001393 pthread_mutex_lock(&fragment_mutex);
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001394 disk_fragment = &fragment_table[index];
plougher5507dd92006-11-06 00:43:10 +00001395 size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size);
plougher76c64082008-03-08 01:32:23 +00001396 start_block = disk_fragment->start_block;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001397 pthread_cleanup_pop(1);
plougher1f413c82005-11-18 00:02:14 +00001398
plougher0f464442008-03-31 00:27:56 +00001399 if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) {
plougher1d065e92010-06-18 03:58:27 +00001400 int error;
plougher76c64082008-03-08 01:32:23 +00001401 char *data;
plougher1f413c82005-11-18 00:02:14 +00001402
plougher76c64082008-03-08 01:32:23 +00001403 if(compressed_buffer)
1404 data = compressed_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001405 else {
plougher49b57a92009-03-31 03:23:05 +00001406 data = read_from_disk(start_block, size);
Phillip Lougherac731382013-05-19 04:19:44 +01001407 if(data == NULL) {
1408 ERROR("Failed to read fragment from output"
1409 " filesystem\n");
1410 BAD_ERROR("Output filesystem corrupted?\n");
1411 }
1412 }
plougher1f413c82005-11-18 00:02:14 +00001413
plougherb48442b2010-12-31 07:57:54 +00001414 res = compressor_uncompress(comp, buffer->data, data, size,
1415 block_size, &error);
ploughera175ce22009-07-30 04:43:27 +00001416 if(res == -1)
1417 BAD_ERROR("%s uncompress failed with error code %d\n",
1418 comp->name, error);
plougher76c64082008-03-08 01:32:23 +00001419 } else if(compressed_buffer)
1420 memcpy(buffer->data, compressed_buffer->data, size);
plougher1d065e92010-06-18 03:58:27 +00001421 else {
1422 res = read_fs_bytes(fd, start_block, size, buffer->data);
Phillip Lougher94519382013-03-06 02:43:42 +00001423 if(res == 0) {
1424 ERROR("Failed to read fragment from output "
1425 "filesystem\n");
1426 BAD_ERROR("Output filesystem corrupted?\n");
1427 }
plougher1d065e92010-06-18 03:58:27 +00001428 }
plougher1f413c82005-11-18 00:02:14 +00001429
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001430 cache_unlock(buffer);
plougher03859fa2009-03-31 03:18:18 +00001431 cache_block_put(compressed_buffer);
1432
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001433finished:
1434 pthread_cleanup_pop(0);
1435
plougher76c64082008-03-08 01:32:23 +00001436 return buffer;
plougher1f413c82005-11-18 00:02:14 +00001437}
1438
plougher2ea89142008-03-11 01:34:19 +00001439
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001440unsigned short get_fragment_checksum(struct file_info *file)
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001441{
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001442 struct file_buffer *frag_buffer;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001443 struct append_file *append;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001444 int res, index = file->fragment->index;
1445 unsigned short checksum;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001446
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001447 if(index == SQUASHFS_INVALID_FRAG)
1448 return 0;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001449
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001450 pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1451 pthread_mutex_lock(&dup_mutex);
1452 res = file->have_frag_checksum;
1453 checksum = file->fragment_checksum;
Phillip Lougherc4a8a792014-04-10 02:23:26 +01001454 pthread_cleanup_pop(1);
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001455
1456 if(res)
1457 return checksum;
1458
1459 frag_buffer = get_fragment(file->fragment);
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001460
Phillip Lougherc4a8a792014-04-10 02:23:26 +01001461 pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1462
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001463 for(append = file_mapping[index]; append; append = append->next) {
1464 int offset = append->file->fragment->offset;
1465 int size = append->file->fragment->size;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001466 unsigned short cksum =
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001467 get_checksum_mem(frag_buffer->data + offset, size);
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001468
1469 if(file == append->file)
1470 checksum = cksum;
1471
1472 pthread_mutex_lock(&dup_mutex);
1473 append->file->fragment_checksum = cksum;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001474 append->file->have_frag_checksum = TRUE;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001475 pthread_mutex_unlock(&dup_mutex);
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001476 }
1477
1478 cache_block_put(frag_buffer);
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001479 pthread_cleanup_pop(0);
1480
1481 return checksum;
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001482}
1483
1484
Phillip Lougher943acad2014-04-17 03:31:01 +01001485void lock_fragments()
plougher5507dd92006-11-06 00:43:10 +00001486{
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001487 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001488 pthread_mutex_lock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00001489 fragments_locked = TRUE;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001490 pthread_cleanup_pop(1);
plougher2ea89142008-03-11 01:34:19 +00001491}
1492
1493
1494void unlock_fragments()
1495{
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001496 int frg, size;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001497 struct file_buffer *write_buffer;
plougher2ea89142008-03-11 01:34:19 +00001498
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001499 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00001500 pthread_mutex_lock(&fragment_mutex);
Phillip Lougher71ad9642013-05-25 05:03:48 +01001501
1502 /*
1503 * Note queue_empty() is inherently racy with respect to concurrent
1504 * queue get and pushes. We avoid this because we're holding the
1505 * fragment_mutex which ensures no other threads can be using the
1506 * queue at this time.
1507 */
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001508 while(!queue_empty(locked_fragment)) {
1509 write_buffer = queue_get(locked_fragment);
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001510 frg = write_buffer->block;
1511 size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size);
1512 fragment_table[frg].start_block = bytes;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001513 write_buffer->block = bytes;
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001514 bytes += size;
plougher2ea89142008-03-11 01:34:19 +00001515 fragments_outstanding --;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001516 queue_put(to_writer, write_buffer);
plougher50b31762009-03-31 04:14:46 +00001517 TRACE("fragment_locked writing fragment %d, compressed size %d"
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001518 "\n", frg, size);
plougher2ea89142008-03-11 01:34:19 +00001519 }
1520 fragments_locked = FALSE;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001521 pthread_cleanup_pop(1);
plougher2ea89142008-03-11 01:34:19 +00001522}
1523
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001524/* Called with the fragment_mutex locked */
plougherb7a66812010-07-21 01:06:59 +00001525void add_pending_fragment(struct file_buffer *write_buffer, int c_byte,
plougher17b269c2009-03-30 01:33:07 +00001526 int fragment)
plougher2ea89142008-03-11 01:34:19 +00001527{
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001528 fragment_table[fragment].size = c_byte;
1529 write_buffer->block = fragment;
1530
1531 queue_put(locked_fragment, write_buffer);
plougher5507dd92006-11-06 00:43:10 +00001532}
1533
1534
Phillip Lougher04b7b532011-06-11 02:14:15 +01001535void write_fragment(struct file_buffer *fragment)
plougher1f413c82005-11-18 00:02:14 +00001536{
Phillip Lougher04b7b532011-06-11 02:14:15 +01001537 if(fragment == NULL)
plougher1f413c82005-11-18 00:02:14 +00001538 return;
1539
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001540 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001541 pthread_mutex_lock(&fragment_mutex);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001542 fragment_table[fragment->block].unused = 0;
1543 fragments_outstanding ++;
1544 queue_put(to_frag, fragment);
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001545 pthread_cleanup_pop(1);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001546}
1547
1548
1549struct file_buffer *allocate_fragment()
1550{
Phillip Lougher316ab632013-04-29 02:53:39 +01001551 struct file_buffer *fragment = cache_get(fragment_buffer, fragments);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001552
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001553 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001554 pthread_mutex_lock(&fragment_mutex);
1555
plougher5507dd92006-11-06 00:43:10 +00001556 if(fragments % FRAG_SIZE == 0) {
plougherfa89c332010-07-27 00:27:15 +00001557 void *ft = realloc(fragment_table, (fragments +
plougher8ed84b92010-12-31 10:37:24 +00001558 FRAG_SIZE) * sizeof(struct squashfs_fragment_entry));
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01001559 if(ft == NULL)
1560 MEM_ERROR();
plougherfa89c332010-07-27 00:27:15 +00001561 fragment_table = ft;
plougher5507dd92006-11-06 00:43:10 +00001562 }
Phillip Lougherb9508be2011-06-13 02:25:51 +01001563
1564 fragment->size = 0;
1565 fragment->block = fragments ++;
1566
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001567 pthread_cleanup_pop(1);
1568
Phillip Lougherb9508be2011-06-13 02:25:51 +01001569 return fragment;
plougher5507dd92006-11-06 00:43:10 +00001570}
1571
ploughereb6eac92008-02-26 01:50:48 +00001572
plougher1f413c82005-11-18 00:02:14 +00001573static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0};
Phillip Lougherd2f045f2012-12-28 03:15:45 +00001574
1575
1576void free_fragment(struct fragment *fragment)
1577{
1578 if(fragment != &empty_fragment)
1579 free(fragment);
1580}
1581
1582
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001583struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer,
1584 struct dir_ent *dir_ent)
plougher1f413c82005-11-18 00:02:14 +00001585{
1586 struct fragment *ffrg;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001587 struct file_buffer **fragment;
plougher1f413c82005-11-18 00:02:14 +00001588
plougher5507dd92006-11-06 00:43:10 +00001589 if(file_buffer == NULL || file_buffer->size == 0)
plougher1f413c82005-11-18 00:02:14 +00001590 return &empty_fragment;
1591
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001592 fragment = eval_frag_actions(dir_ent);
1593
1594 if((*fragment) && (*fragment)->size + file_buffer->size > block_size) {
1595 write_fragment(*fragment);
1596 *fragment = NULL;
Phillip Lougher04b7b532011-06-11 02:14:15 +01001597 }
plougher1f413c82005-11-18 00:02:14 +00001598
ploughere7e6e832010-12-16 05:08:06 +00001599 ffrg = malloc(sizeof(struct fragment));
1600 if(ffrg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001601 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001602
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001603 if(*fragment == NULL)
1604 *fragment = allocate_fragment();
plougher5507dd92006-11-06 00:43:10 +00001605
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001606 ffrg->index = (*fragment)->block;
1607 ffrg->offset = (*fragment)->size;
plougher5507dd92006-11-06 00:43:10 +00001608 ffrg->size = file_buffer->size;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001609 memcpy((*fragment)->data + (*fragment)->size, file_buffer->data,
plougher17b269c2009-03-30 01:33:07 +00001610 file_buffer->size);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001611 (*fragment)->size += file_buffer->size;
plougher1f413c82005-11-18 00:02:14 +00001612
1613 return ffrg;
1614}
1615
1616
ploughera0a49c32010-08-11 01:47:59 +00001617long long generic_write_table(int length, void *buffer, int length2,
1618 void *buffer2, int uncompressed)
plougher1f413c82005-11-18 00:02:14 +00001619{
plougher17b269c2009-03-30 01:33:07 +00001620 int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) /
1621 SQUASHFS_METADATA_SIZE;
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001622 long long *list, start_bytes;
Phillip Lougherc1305322012-11-02 22:28:02 +00001623 int compressed_size, i, list_size = meta_blocks * sizeof(long long);
plougher1f413c82005-11-18 00:02:14 +00001624 unsigned short c_byte;
plougher0e453652006-11-06 01:49:35 +00001625 char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2];
1626
plougher82ab2332009-04-21 00:21:21 +00001627#ifdef SQUASHFS_TRACE
plougher0e453652006-11-06 01:49:35 +00001628 long long obytes = bytes;
plougher40d8b952010-02-12 16:26:32 +00001629 int olength = length;
plougher82ab2332009-04-21 00:21:21 +00001630#endif
plougher1f413c82005-11-18 00:02:14 +00001631
Phillip Lougherc1305322012-11-02 22:28:02 +00001632 list = malloc(list_size);
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001633 if(list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001634 MEM_ERROR();
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001635
plougher1f413c82005-11-18 00:02:14 +00001636 for(i = 0; i < meta_blocks; i++) {
plougher17b269c2009-03-30 01:33:07 +00001637 int avail_bytes = length > SQUASHFS_METADATA_SIZE ?
1638 SQUASHFS_METADATA_SIZE : length;
1639 c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i *
1640 SQUASHFS_METADATA_SIZE , avail_bytes,
1641 SQUASHFS_METADATA_SIZE, uncompressed, 0);
plougherac28cd12010-02-24 02:25:03 +00001642 SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1);
plougher1f413c82005-11-18 00:02:14 +00001643 list[i] = bytes;
plougher50b31762009-03-31 04:14:46 +00001644 compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) +
1645 BLOCK_OFFSET;
plougher17b269c2009-03-30 01:33:07 +00001646 TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes,
1647 compressed_size);
plougher0dd6f122009-03-29 21:43:57 +00001648 write_destination(fd, bytes, compressed_size, cbuffer);
plougher1f413c82005-11-18 00:02:14 +00001649 bytes += compressed_size;
plougher10f7d572010-07-20 02:14:04 +00001650 total_bytes += avail_bytes;
plougher0e453652006-11-06 01:49:35 +00001651 length -= avail_bytes;
plougher1f413c82005-11-18 00:02:14 +00001652 }
1653
ploughere6e0e1b2010-05-12 17:17:06 +00001654 start_bytes = bytes;
1655 if(length2) {
ploughera0a49c32010-08-11 01:47:59 +00001656 write_destination(fd, bytes, length2, buffer2);
ploughere6e0e1b2010-05-12 17:17:06 +00001657 bytes += length2;
plougher10f7d572010-07-20 02:14:04 +00001658 total_bytes += length2;
ploughere6e0e1b2010-05-12 17:17:06 +00001659 }
1660
plougher1f288f62009-02-21 03:05:52 +00001661 SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks);
Phillip Lougherc1305322012-11-02 22:28:02 +00001662 write_destination(fd, bytes, list_size, list);
1663 bytes += list_size;
1664 total_bytes += list_size;
plougher1f413c82005-11-18 00:02:14 +00001665
plougher40d8b952010-02-12 16:26:32 +00001666 TRACE("generic_write_table: total uncompressed %d compressed %lld\n",
1667 olength, bytes - obytes);
plougher0e453652006-11-06 01:49:35 +00001668
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001669 free(list);
1670
plougher1f413c82005-11-18 00:02:14 +00001671 return start_bytes;
1672}
1673
1674
plougher0e453652006-11-06 01:49:35 +00001675long long write_fragment_table()
1676{
1677 unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments);
plougher0e453652006-11-06 01:49:35 +00001678 int i;
1679
plougher17b269c2009-03-30 01:33:07 +00001680 TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments,
1681 frag_bytes);
plougherac28cd12010-02-24 02:25:03 +00001682 for(i = 0; i < fragments; i++) {
plougher50b31762009-03-31 04:14:46 +00001683 TRACE("write_fragment_table: fragment %d, start_block 0x%llx, "
1684 "size %d\n", i, fragment_table[i].start_block,
plougher17b269c2009-03-30 01:33:07 +00001685 fragment_table[i].size);
Phillip Lougher162c24c2012-10-30 02:54:16 +00001686 SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]);
plougher0e453652006-11-06 01:49:35 +00001687 }
1688
Phillip Lougher162c24c2012-10-30 02:54:16 +00001689 return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF);
plougher0e453652006-11-06 01:49:35 +00001690}
1691
1692
plougher1f413c82005-11-18 00:02:14 +00001693char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE];
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001694static char *read_from_disk(long long start, unsigned int avail_bytes)
plougher1f413c82005-11-18 00:02:14 +00001695{
plougher1d065e92010-06-18 03:58:27 +00001696 int res;
1697
1698 res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer);
Phillip Lougherac731382013-05-19 04:19:44 +01001699 if(res == 0)
1700 return NULL;
plougher1d065e92010-06-18 03:58:27 +00001701
plougher1f413c82005-11-18 00:02:14 +00001702 return read_from_file_buffer;
1703}
1704
1705
plougher1b899fc2008-08-07 01:24:06 +00001706char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE];
1707char *read_from_disk2(long long start, unsigned int avail_bytes)
1708{
plougher1d065e92010-06-18 03:58:27 +00001709 int res;
1710
1711 res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2);
Phillip Lougherac731382013-05-19 04:19:44 +01001712 if(res == 0)
1713 return NULL;
plougher1d065e92010-06-18 03:58:27 +00001714
plougher1b899fc2008-08-07 01:24:06 +00001715 return read_from_file_buffer2;
1716}
1717
1718
plougher1f413c82005-11-18 00:02:14 +00001719/*
1720 * Compute 16 bit BSD checksum over the data
1721 */
plougher5507dd92006-11-06 00:43:10 +00001722unsigned short get_checksum(char *buff, int bytes, unsigned short chksum)
plougher1f413c82005-11-18 00:02:14 +00001723{
plougher5507dd92006-11-06 00:43:10 +00001724 unsigned char *b = (unsigned char *) buff;
plougher1f413c82005-11-18 00:02:14 +00001725
plougher5507dd92006-11-06 00:43:10 +00001726 while(bytes --) {
1727 chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1;
1728 chksum += *b++;
plougher1f413c82005-11-18 00:02:14 +00001729 }
1730
1731 return chksum;
1732}
1733
1734
plougher17b269c2009-03-30 01:33:07 +00001735unsigned short get_checksum_disk(long long start, long long l,
1736 unsigned int *blocks)
plougher5507dd92006-11-06 00:43:10 +00001737{
1738 unsigned short chksum = 0;
1739 unsigned int bytes;
plougher57e6d182008-05-06 23:51:29 +00001740 struct file_buffer *write_buffer;
1741 int i;
plougher5507dd92006-11-06 00:43:10 +00001742
plougher57e6d182008-05-06 23:51:29 +00001743 for(i = 0; l; i++) {
1744 bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]);
1745 if(bytes == 0) /* sparse block */
1746 continue;
Phillip Lougher943acad2014-04-17 03:31:01 +01001747 write_buffer = cache_lookup(bwriter_buffer, start);
plougher57e6d182008-05-06 23:51:29 +00001748 if(write_buffer) {
plougher50b31762009-03-31 04:14:46 +00001749 chksum = get_checksum(write_buffer->data, bytes,
1750 chksum);
plougher57e6d182008-05-06 23:51:29 +00001751 cache_block_put(write_buffer);
Phillip Lougherac731382013-05-19 04:19:44 +01001752 } else {
1753 void *data = read_from_disk(start, bytes);
1754 if(data == NULL) {
1755 ERROR("Failed to checksum data from output"
1756 " filesystem\n");
1757 BAD_ERROR("Output filesystem corrupted?\n");
1758 }
1759
1760 chksum = get_checksum(data, bytes, chksum);
1761 }
1762
plougher5507dd92006-11-06 00:43:10 +00001763 l -= bytes;
plougher5507dd92006-11-06 00:43:10 +00001764 start += bytes;
1765 }
1766
1767 return chksum;
1768}
1769
1770
plougher5507dd92006-11-06 00:43:10 +00001771unsigned short get_checksum_mem(char *buff, int bytes)
1772{
1773 return get_checksum(buff, bytes, 0);
1774}
1775
1776
1777unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer)
1778{
1779 if(file_buffer == NULL)
1780 return 0;
1781 else
1782 return get_checksum(file_buffer->data, file_buffer->size, 0);
1783}
1784
1785
plougher5507dd92006-11-06 00:43:10 +00001786#define DUP_HASH(a) (a & 0xffff)
plougher17b269c2009-03-30 01:33:07 +00001787void add_file(long long start, long long file_size, long long file_bytes,
plougher50b31762009-03-31 04:14:46 +00001788 unsigned int *block_listp, int blocks, unsigned int fragment,
1789 int offset, int bytes)
plougher1f413c82005-11-18 00:02:14 +00001790{
1791 struct fragment *frg;
plougher058eae42006-01-30 14:27:44 +00001792 unsigned int *block_list = block_listp;
plougher5507dd92006-11-06 00:43:10 +00001793 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001794 struct append_file *append_file;
1795 struct file_info *file;
plougher058eae42006-01-30 14:27:44 +00001796
plougher5507dd92006-11-06 00:43:10 +00001797 if(!duplicate_checking || file_size == 0)
plougher1f413c82005-11-18 00:02:14 +00001798 return;
1799
plougher5507dd92006-11-06 00:43:10 +00001800 for(; dupl_ptr; dupl_ptr = dupl_ptr->next) {
1801 if(file_size != dupl_ptr->file_size)
1802 continue;
1803 if(blocks != 0 && start != dupl_ptr->start)
1804 continue;
1805 if(fragment != dupl_ptr->fragment->index)
1806 continue;
plougher17b269c2009-03-30 01:33:07 +00001807 if(fragment != SQUASHFS_INVALID_FRAG && (offset !=
1808 dupl_ptr->fragment->offset || bytes !=
1809 dupl_ptr->fragment->size))
plougher5507dd92006-11-06 00:43:10 +00001810 continue;
1811 return;
1812 }
1813
plougher3bb279c2010-12-16 05:10:03 +00001814 frg = malloc(sizeof(struct fragment));
1815 if(frg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001816 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001817
1818 frg->index = fragment;
1819 frg->offset = offset;
1820 frg->size = bytes;
plougher1f413c82005-11-18 00:02:14 +00001821
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001822 file = add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00001823 FALSE, FALSE);
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00001824
1825 if(fragment == SQUASHFS_INVALID_FRAG)
1826 return;
1827
1828 append_file = malloc(sizeof(struct append_file));
1829 if(append_file == NULL)
1830 MEM_ERROR();
1831
1832 append_file->file = file;
1833 append_file->next = file_mapping[fragment];
1834 file_mapping[fragment] = append_file;
plougher5507dd92006-11-06 00:43:10 +00001835}
plougher1f413c82005-11-18 00:02:14 +00001836
plougher1f413c82005-11-18 00:02:14 +00001837
plougher5507dd92006-11-06 00:43:10 +00001838int pre_duplicate(long long file_size)
plougher1f413c82005-11-18 00:02:14 +00001839{
plougher5507dd92006-11-06 00:43:10 +00001840 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
plougher1f413c82005-11-18 00:02:14 +00001841
1842 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher5507dd92006-11-06 00:43:10 +00001843 if(dupl_ptr->file_size == file_size)
1844 return TRUE;
plougher1f413c82005-11-18 00:02:14 +00001845
plougher5507dd92006-11-06 00:43:10 +00001846 return FALSE;
1847}
1848
1849
plougher17b269c2009-03-30 01:33:07 +00001850struct file_info *add_non_dup(long long file_size, long long bytes,
1851 unsigned int *block_list, long long start, struct fragment *fragment,
1852 unsigned short checksum, unsigned short fragment_checksum,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00001853 int checksum_flag, int checksum_frag_flag)
plougher5507dd92006-11-06 00:43:10 +00001854{
plougher51ef9ae2010-12-16 05:14:28 +00001855 struct file_info *dupl_ptr = malloc(sizeof(struct file_info));
plougher5507dd92006-11-06 00:43:10 +00001856
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001857 if(dupl_ptr == NULL)
1858 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00001859
1860 dupl_ptr->file_size = file_size;
1861 dupl_ptr->bytes = bytes;
1862 dupl_ptr->block_list = block_list;
1863 dupl_ptr->start = start;
1864 dupl_ptr->fragment = fragment;
1865 dupl_ptr->checksum = checksum;
1866 dupl_ptr->fragment_checksum = fragment_checksum;
Phillip Lougherc6424dc2014-03-13 02:01:28 +00001867 dupl_ptr->have_frag_checksum = checksum_frag_flag;
Phillip Lougher557cefa2014-02-05 04:36:05 +00001868 dupl_ptr->have_checksum = checksum_flag;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001869
1870 pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex);
1871 pthread_mutex_lock(&dup_mutex);
plougher5507dd92006-11-06 00:43:10 +00001872 dupl_ptr->next = dupl[DUP_HASH(file_size)];
1873 dupl[DUP_HASH(file_size)] = dupl_ptr;
1874 dup_files ++;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001875 pthread_cleanup_pop(1);
plougher5507dd92006-11-06 00:43:10 +00001876
1877 return dupl_ptr;
1878}
1879
1880
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001881struct fragment *frag_duplicate(struct file_buffer *file_buffer, char *dont_put)
1882{
1883 struct file_info *dupl_ptr;
1884 struct file_buffer *buffer;
1885 struct file_info *dupl_start = file_buffer->dupl_start;
1886 long long file_size = file_buffer->file_size;
1887 unsigned short checksum = file_buffer->checksum;
1888 int res;
1889
1890 if(file_buffer->duplicate) {
1891 TRACE("Found duplicate file, fragment %d, size %d, offset %d, "
1892 "checksum 0x%x\n", dupl_start->fragment->index,
1893 file_size, dupl_start->fragment->offset, checksum);
1894 *dont_put = TRUE;
1895 return dupl_start->fragment;
1896 } else {
1897 *dont_put = FALSE;
1898 dupl_ptr = dupl[DUP_HASH(file_size)];
1899 }
1900
1901 for(; dupl_ptr && dupl_ptr != dupl_start; dupl_ptr = dupl_ptr->next) {
1902 if(file_size == dupl_ptr->file_size && file_size ==
1903 dupl_ptr->fragment->size) {
1904 if(get_fragment_checksum(dupl_ptr) == checksum) {
1905 buffer = get_fragment(dupl_ptr->fragment);
1906 res = memcmp(file_buffer->data, buffer->data +
1907 dupl_ptr->fragment->offset, file_size);
1908 cache_block_put(buffer);
1909 if(res == 0)
1910 break;
1911 }
1912 }
1913 }
1914
1915 if(!dupl_ptr || dupl_ptr == dupl_start)
1916 return NULL;
1917
1918 TRACE("Found duplicate file, fragment %d, size %d, offset %d, "
1919 "checksum 0x%x\n", dupl_ptr->fragment->index, file_size,
1920 dupl_ptr->fragment->offset, checksum);
1921
1922 return dupl_ptr->fragment;
1923}
1924
1925
plougher17b269c2009-03-30 01:33:07 +00001926struct file_info *duplicate(long long file_size, long long bytes,
1927 unsigned int **block_list, long long *start, struct fragment **fragment,
1928 struct file_buffer *file_buffer, int blocks, unsigned short checksum,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00001929 int checksum_flag)
plougher5507dd92006-11-06 00:43:10 +00001930{
1931 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
1932 int frag_bytes = file_buffer ? file_buffer->size : 0;
Phillip Lougherc6424dc2014-03-13 02:01:28 +00001933 unsigned short fragment_checksum = file_buffer ?
1934 file_buffer->checksum : 0;
plougher5507dd92006-11-06 00:43:10 +00001935
1936 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher16111452010-07-22 05:12:18 +00001937 if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes
1938 && frag_bytes == dupl_ptr->fragment->size) {
plougher1b899fc2008-08-07 01:24:06 +00001939 long long target_start, dup_start = dupl_ptr->start;
plougher5507dd92006-11-06 00:43:10 +00001940 int block;
1941
plougher17b269c2009-03-30 01:33:07 +00001942 if(memcmp(*block_list, dupl_ptr->block_list, blocks *
1943 sizeof(unsigned int)) != 0)
plougherfbf9f752007-08-12 05:02:24 +00001944 continue;
1945
plougher5507dd92006-11-06 00:43:10 +00001946 if(checksum_flag == FALSE) {
plougher17b269c2009-03-30 01:33:07 +00001947 checksum = get_checksum_disk(*start, bytes,
1948 *block_list);
plougher5507dd92006-11-06 00:43:10 +00001949 checksum_flag = TRUE;
1950 }
1951
Phillip Lougher557cefa2014-02-05 04:36:05 +00001952 if(!dupl_ptr->have_checksum) {
1953 dupl_ptr->checksum =
1954 get_checksum_disk(dupl_ptr->start,
1955 dupl_ptr->bytes, dupl_ptr->block_list);
1956 dupl_ptr->have_checksum = TRUE;
plougher5507dd92006-11-06 00:43:10 +00001957 }
1958
plougher17b269c2009-03-30 01:33:07 +00001959 if(checksum != dupl_ptr->checksum ||
1960 fragment_checksum !=
Phillip Lougher8bb17b02014-03-30 23:59:55 +01001961 get_fragment_checksum(dupl_ptr))
plougher5507dd92006-11-06 00:43:10 +00001962 continue;
1963
plougher1b899fc2008-08-07 01:24:06 +00001964 target_start = *start;
plougher5507dd92006-11-06 00:43:10 +00001965 for(block = 0; block < blocks; block ++) {
plougher17b269c2009-03-30 01:33:07 +00001966 int size = SQUASHFS_COMPRESSED_SIZE_BLOCK
1967 ((*block_list)[block]);
plougher1b899fc2008-08-07 01:24:06 +00001968 struct file_buffer *target_buffer = NULL;
1969 struct file_buffer *dup_buffer = NULL;
1970 char *target_data, *dup_data;
plougher0f464442008-03-31 00:27:56 +00001971 int res;
plougher5507dd92006-11-06 00:43:10 +00001972
plougher1b899fc2008-08-07 01:24:06 +00001973 if(size == 0)
plougherfbf9f752007-08-12 05:02:24 +00001974 continue;
Phillip Lougher943acad2014-04-17 03:31:01 +01001975 target_buffer = cache_lookup(bwriter_buffer,
plougher17b269c2009-03-30 01:33:07 +00001976 target_start);
plougher1b899fc2008-08-07 01:24:06 +00001977 if(target_buffer)
1978 target_data = target_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001979 else {
plougher50b31762009-03-31 04:14:46 +00001980 target_data =
1981 read_from_disk(target_start,
plougher17b269c2009-03-30 01:33:07 +00001982 size);
Phillip Lougherac731382013-05-19 04:19:44 +01001983 if(target_data == NULL) {
1984 ERROR("Failed to read data from"
1985 " output filesystem\n");
1986 BAD_ERROR("Output filesystem"
1987 " corrupted?\n");
1988 }
1989 }
plougher5507dd92006-11-06 00:43:10 +00001990
Phillip Lougher943acad2014-04-17 03:31:01 +01001991 dup_buffer = cache_lookup(bwriter_buffer,
plougher360514a2009-03-30 03:01:38 +00001992 dup_start);
plougher1b899fc2008-08-07 01:24:06 +00001993 if(dup_buffer)
1994 dup_data = dup_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001995 else {
plougher360514a2009-03-30 03:01:38 +00001996 dup_data = read_from_disk2(dup_start,
1997 size);
Phillip Lougherac731382013-05-19 04:19:44 +01001998 if(dup_data == NULL) {
1999 ERROR("Failed to read data from"
2000 " output filesystem\n");
2001 BAD_ERROR("Output filesystem"
2002 " corrupted?\n");
2003 }
2004 }
plougher1b899fc2008-08-07 01:24:06 +00002005
2006 res = memcmp(target_data, dup_data, size);
2007 cache_block_put(target_buffer);
2008 cache_block_put(dup_buffer);
plougher0f464442008-03-31 00:27:56 +00002009 if(res != 0)
plougher5507dd92006-11-06 00:43:10 +00002010 break;
plougher1b899fc2008-08-07 01:24:06 +00002011 target_start += size;
2012 dup_start += size;
plougher5507dd92006-11-06 00:43:10 +00002013 }
2014 if(block == blocks) {
plougher17b269c2009-03-30 01:33:07 +00002015 struct file_buffer *frag_buffer =
2016 get_fragment(dupl_ptr->fragment);
plougher5507dd92006-11-06 00:43:10 +00002017
plougher17b269c2009-03-30 01:33:07 +00002018 if(frag_bytes == 0 ||
2019 memcmp(file_buffer->data,
2020 frag_buffer->data +
2021 dupl_ptr->fragment->offset,
2022 frag_bytes) == 0) {
plougher50b31762009-03-31 04:14:46 +00002023 TRACE("Found duplicate file, start "
2024 "0x%llx, size %lld, checksum "
2025 "0x%x, fragment %d, size %d, "
2026 "offset %d, checksum 0x%x\n",
2027 dupl_ptr->start,
plougher17b269c2009-03-30 01:33:07 +00002028 dupl_ptr->bytes,
2029 dupl_ptr->checksum,
2030 dupl_ptr->fragment->index,
2031 frag_bytes,
2032 dupl_ptr->fragment->offset,
2033 fragment_checksum);
plougher1f413c82005-11-18 00:02:14 +00002034 *block_list = dupl_ptr->block_list;
2035 *start = dupl_ptr->start;
2036 *fragment = dupl_ptr->fragment;
plougher76c64082008-03-08 01:32:23 +00002037 cache_block_put(frag_buffer);
plougher1f413c82005-11-18 00:02:14 +00002038 return 0;
2039 }
plougher76c64082008-03-08 01:32:23 +00002040 cache_block_put(frag_buffer);
plougher1f413c82005-11-18 00:02:14 +00002041 }
2042 }
2043
2044
plougher17b269c2009-03-30 01:33:07 +00002045 return add_non_dup(file_size, bytes, *block_list, *start, *fragment,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002046 checksum, fragment_checksum, checksum_flag, TRUE);
plougher1f413c82005-11-18 00:02:14 +00002047}
2048
2049
Phillip Lougher2504b082011-09-07 02:28:45 +01002050inline int is_fragment(struct inode_info *inode)
2051{
2052 int file_size = inode->buf.st_size;
2053
Phillip Lougher63f531f2011-09-10 04:03:32 +01002054 /*
2055 * If this block is to be compressed differently to the
2056 * fragment compression then it cannot be a fragment
2057 */
2058 if(inode->noF != noF)
2059 return FALSE;
2060
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002061 return !inode->no_fragments && file_size && (file_size < block_size ||
Phillip Lougher2504b082011-09-07 02:28:45 +01002062 (inode->always_use_fragments && file_size & (block_size - 1)));
2063}
2064
2065
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002066void put_file_buffer(struct file_buffer *file_buffer)
2067{
2068 /*
Phillip Lougher9de84ac2014-02-20 05:18:48 +00002069 * Decide where to send the file buffer:
2070 * - compressible non-fragment blocks go to the deflate threads,
2071 * - fragments go to the process fragment threads,
2072 * - all others go directly to the main thread
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002073 */
2074 if(file_buffer->error) {
2075 file_buffer->fragment = 0;
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002076 seq_queue_put(to_main, file_buffer);
Phillip Lougher9de84ac2014-02-20 05:18:48 +00002077 } else if (file_buffer->file_size == 0)
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002078 seq_queue_put(to_main, file_buffer);
Phillip Lougher9de84ac2014-02-20 05:18:48 +00002079 else if(file_buffer->fragment)
2080 queue_put(to_process_frag, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002081 else
Phillip Loughercf478e92013-05-29 02:38:41 +01002082 queue_put(to_deflate, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002083}
2084
2085
plougher00d08172009-09-03 10:17:44 +00002086static int seq = 0;
2087void reader_read_process(struct dir_ent *dir_ent)
2088{
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002089 long long bytes = 0;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002090 struct inode_info *inode = dir_ent->inode;
plougher00d08172009-09-03 10:17:44 +00002091 struct file_buffer *prev_buffer = NULL, *file_buffer;
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002092 int status, byte, res, child;
2093 int file = pseudo_exec_file(get_pseudo_file(inode->pseudo_id), &child);
2094
2095 if(!file) {
2096 file_buffer = cache_get_nohash(reader_buffer);
2097 file_buffer->sequence = seq ++;
2098 goto read_err;
2099 }
plougher00d08172009-09-03 10:17:44 +00002100
2101 while(1) {
Phillip Lougher316ab632013-04-29 02:53:39 +01002102 file_buffer = cache_get_nohash(reader_buffer);
plougher00d08172009-09-03 10:17:44 +00002103 file_buffer->sequence = seq ++;
Phillip Lougher63f531f2011-09-10 04:03:32 +01002104 file_buffer->noD = inode->noD;
plougher00d08172009-09-03 10:17:44 +00002105
2106 byte = read_bytes(file, file_buffer->data, block_size);
2107 if(byte == -1)
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002108 goto read_err2;
plougher00d08172009-09-03 10:17:44 +00002109
2110 file_buffer->size = byte;
2111 file_buffer->file_size = -1;
plougher00d08172009-09-03 10:17:44 +00002112 file_buffer->error = FALSE;
2113 file_buffer->fragment = FALSE;
2114 bytes += byte;
2115
2116 if(byte == 0)
2117 break;
2118
plougher286b6b32009-09-19 03:29:27 +00002119 /*
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002120 * Update progress bar size. This is done
plougher286b6b32009-09-19 03:29:27 +00002121 * on every block rather than waiting for all blocks to be
2122 * read incase write_file_process() is running in parallel
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002123 * with this. Otherwise the current progress bar position
2124 * may get ahead of the progress bar size.
plougher286b6b32009-09-19 03:29:27 +00002125 */
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002126 progress_bar_size(1);
plougher286b6b32009-09-19 03:29:27 +00002127
plougher00d08172009-09-03 10:17:44 +00002128 if(prev_buffer)
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002129 put_file_buffer(prev_buffer);
plougher00d08172009-09-03 10:17:44 +00002130 prev_buffer = file_buffer;
2131 }
2132
plougher54d67292009-09-19 03:26:27 +00002133 /*
2134 * Update inode file size now that the size of the dynamic pseudo file
2135 * is known. This is needed for the -info option.
2136 */
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002137 inode->buf.st_size = bytes;
plougher54d67292009-09-19 03:26:27 +00002138
plougherba674e82009-09-10 03:50:00 +00002139 res = waitpid(child, &status, 0);
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002140 close(file);
2141
plougherd87d8d12009-09-10 04:08:16 +00002142 if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
plougherba674e82009-09-10 03:50:00 +00002143 goto read_err;
2144
plougher00d08172009-09-03 10:17:44 +00002145 if(prev_buffer == NULL)
2146 prev_buffer = file_buffer;
2147 else {
2148 cache_block_put(file_buffer);
2149 seq --;
2150 }
2151 prev_buffer->file_size = bytes;
Phillip Lougher2504b082011-09-07 02:28:45 +01002152 prev_buffer->fragment = is_fragment(inode);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002153 put_file_buffer(prev_buffer);
plougher00d08172009-09-03 10:17:44 +00002154
2155 return;
2156
Phillip Loughera8c9ff22014-04-29 03:53:18 +01002157read_err2:
2158 close(file);
plougher00d08172009-09-03 10:17:44 +00002159read_err:
2160 if(prev_buffer) {
2161 cache_block_put(file_buffer);
2162 seq --;
2163 file_buffer = prev_buffer;
2164 }
2165 file_buffer->error = TRUE;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002166 put_file_buffer(file_buffer);
plougher00d08172009-09-03 10:17:44 +00002167}
2168
2169
plougher5507dd92006-11-06 00:43:10 +00002170void reader_read_file(struct dir_ent *dir_ent)
plougher1f413c82005-11-18 00:02:14 +00002171{
plougher018d2b32007-04-23 03:01:48 +00002172 struct stat *buf = &dir_ent->inode->buf, buf2;
plougher5507dd92006-11-06 00:43:10 +00002173 struct file_buffer *file_buffer;
Phillip Lougherce119e72014-03-11 04:14:00 +00002174 int blocks, file, res;
plougher018d2b32007-04-23 03:01:48 +00002175 long long bytes, read_size;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002176 struct inode_info *inode = dir_ent->inode;
plougher5507dd92006-11-06 00:43:10 +00002177
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002178 if(inode->read)
plougher5507dd92006-11-06 00:43:10 +00002179 return;
2180
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002181 inode->read = TRUE;
plougher018d2b32007-04-23 03:01:48 +00002182again:
2183 bytes = 0;
plougher018d2b32007-04-23 03:01:48 +00002184 read_size = buf->st_size;
2185 blocks = (read_size + block_size - 1) >> block_log;
plougher018d2b32007-04-23 03:01:48 +00002186
Phillip Lougher494479f2012-02-03 15:45:41 +00002187 file = open(pathname_reader(dir_ent), O_RDONLY);
plougherc1d258e2010-12-16 05:16:45 +00002188 if(file == -1) {
Phillip Lougher316ab632013-04-29 02:53:39 +01002189 file_buffer = cache_get_nohash(reader_buffer);
plougher1e380702010-08-11 01:52:49 +00002190 file_buffer->sequence = seq ++;
Phillip Lougher2302f692012-12-27 04:05:02 +00002191 goto read_err2;
plougher1e380702010-08-11 01:52:49 +00002192 }
plougher5507dd92006-11-06 00:43:10 +00002193
plougher018d2b32007-04-23 03:01:48 +00002194 do {
Phillip Lougher316ab632013-04-29 02:53:39 +01002195 file_buffer = cache_get_nohash(reader_buffer);
Phillip Lougherce119e72014-03-11 04:14:00 +00002196 file_buffer->file_size = read_size;
plougher00d08172009-09-03 10:17:44 +00002197 file_buffer->sequence = seq ++;
Phillip Lougher63f531f2011-09-10 04:03:32 +01002198 file_buffer->noD = inode->noD;
Phillip Lougherce119e72014-03-11 04:14:00 +00002199 file_buffer->error = FALSE;
plougher5507dd92006-11-06 00:43:10 +00002200
ploughera4c24ed2010-08-11 02:08:38 +00002201 /*
2202 * Always try to read block_size bytes from the file rather
2203 * than expected bytes (which will be less than the block_size
2204 * at the file tail) to check that the file hasn't grown
2205 * since being stated. If it is longer (or shorter) than
2206 * expected, then restat, and try again. Note the special
2207 * case where the file is an exact multiple of the block_size
2208 * is dealt with later.
2209 */
Phillip Lougherce119e72014-03-11 04:14:00 +00002210 file_buffer->size = read_bytes(file, file_buffer->data,
plougher110799c2009-03-30 01:50:40 +00002211 block_size);
Phillip Lougherce119e72014-03-11 04:14:00 +00002212 if(file_buffer->size == -1)
ploughera4c24ed2010-08-11 02:08:38 +00002213 goto read_err;
2214
Phillip Lougherce119e72014-03-11 04:14:00 +00002215 bytes += file_buffer->size;
plougher018d2b32007-04-23 03:01:48 +00002216
Phillip Lougherce119e72014-03-11 04:14:00 +00002217 if(blocks > 1) {
2218 /* non-tail block should be exactly block_size */
2219 if(file_buffer->size < block_size)
2220 goto restat;
plougher018d2b32007-04-23 03:01:48 +00002221
Phillip Lougherce119e72014-03-11 04:14:00 +00002222 file_buffer->fragment = FALSE;
2223 put_file_buffer(file_buffer);
2224 }
Phillip Lougher8678d302014-03-02 03:02:19 +00002225 } while(-- blocks > 0);
plougher018d2b32007-04-23 03:01:48 +00002226
Phillip Lougherce119e72014-03-11 04:14:00 +00002227 /* Overall size including tail should match */
plougher018d2b32007-04-23 03:01:48 +00002228 if(read_size != bytes)
2229 goto restat;
2230
Phillip Lougher191dfcd2014-03-11 02:58:52 +00002231 if(read_size && read_size % block_size == 0) {
ploughera4c24ed2010-08-11 02:08:38 +00002232 /*
2233 * Special case where we've not tried to read past the end of
2234 * the file. We expect to get EOF, i.e. the file isn't larger
2235 * than we expect.
2236 */
plougher018d2b32007-04-23 03:01:48 +00002237 char buffer;
ploughera4c24ed2010-08-11 02:08:38 +00002238 int res;
plougher018d2b32007-04-23 03:01:48 +00002239
ploughera4c24ed2010-08-11 02:08:38 +00002240 res = read_bytes(file, &buffer, 1);
2241 if(res == -1)
2242 goto read_err;
2243
2244 if(res != 0)
plougher018d2b32007-04-23 03:01:48 +00002245 goto restat;
plougher5507dd92006-11-06 00:43:10 +00002246 }
2247
Phillip Lougher2504b082011-09-07 02:28:45 +01002248 file_buffer->fragment = is_fragment(inode);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002249 put_file_buffer(file_buffer);
plougher018d2b32007-04-23 03:01:48 +00002250
plougher5507dd92006-11-06 00:43:10 +00002251 close(file);
plougher5507dd92006-11-06 00:43:10 +00002252
2253 return;
2254
plougher018d2b32007-04-23 03:01:48 +00002255restat:
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002256 res = fstat(file, &buf2);
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002257 if(res == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00002258 ERROR("Cannot stat dir/file %s because %s\n",
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002259 pathname_reader(dir_ent), strerror(errno));
2260 goto read_err;
2261 }
2262
plougher018d2b32007-04-23 03:01:48 +00002263 if(read_size != buf2.st_size) {
Phillip Lougher2302f692012-12-27 04:05:02 +00002264 close(file);
plougher018d2b32007-04-23 03:01:48 +00002265 memcpy(buf, &buf2, sizeof(struct stat));
2266 file_buffer->error = 2;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002267 put_file_buffer(file_buffer);
plougher018d2b32007-04-23 03:01:48 +00002268 goto again;
2269 }
plougher1e380702010-08-11 01:52:49 +00002270read_err:
Phillip Lougher2302f692012-12-27 04:05:02 +00002271 close(file);
2272read_err2:
plougher1e380702010-08-11 01:52:49 +00002273 file_buffer->error = TRUE;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002274 put_file_buffer(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002275}
2276
2277
2278void reader_scan(struct dir_info *dir) {
Phillip Lougherbf338362012-08-22 05:24:36 +01002279 struct dir_ent *dir_ent = dir->list;
plougher5507dd92006-11-06 00:43:10 +00002280
Phillip Lougherbf338362012-08-22 05:24:36 +01002281 for(; dir_ent; dir_ent = dir_ent->next) {
plougher5507dd92006-11-06 00:43:10 +00002282 struct stat *buf = &dir_ent->inode->buf;
ploughera326c182009-08-29 05:41:45 +00002283 if(dir_ent->inode->root_entry)
plougher5507dd92006-11-06 00:43:10 +00002284 continue;
2285
plougherb3977eb2010-05-02 02:08:48 +00002286 if(IS_PSEUDO_PROCESS(dir_ent->inode)) {
plougher00d08172009-09-03 10:17:44 +00002287 reader_read_process(dir_ent);
2288 continue;
2289 }
2290
plougher5507dd92006-11-06 00:43:10 +00002291 switch(buf->st_mode & S_IFMT) {
2292 case S_IFREG:
2293 reader_read_file(dir_ent);
2294 break;
2295 case S_IFDIR:
2296 reader_scan(dir_ent->dir);
2297 break;
2298 }
2299 }
2300}
2301
2302
2303void *reader(void *arg)
2304{
plougher5507dd92006-11-06 00:43:10 +00002305 if(!sorted)
2306 reader_scan(queue_get(to_reader));
2307 else {
2308 int i;
2309 struct priority_entry *entry;
2310
2311 queue_get(to_reader);
2312 for(i = 65535; i >= 0; i--)
plougher16111452010-07-22 05:12:18 +00002313 for(entry = priority_list[i]; entry;
2314 entry = entry->next)
plougher5507dd92006-11-06 00:43:10 +00002315 reader_read_file(entry->dir);
2316 }
rloughere4873e02007-11-08 17:50:42 +00002317
2318 pthread_exit(NULL);
plougher5507dd92006-11-06 00:43:10 +00002319}
2320
2321
2322void *writer(void *arg)
2323{
plougher5507dd92006-11-06 00:43:10 +00002324 while(1) {
2325 struct file_buffer *file_buffer = queue_get(to_writer);
2326 off_t off;
2327
2328 if(file_buffer == NULL) {
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002329 queue_put(from_writer, NULL);
plougher5507dd92006-11-06 00:43:10 +00002330 continue;
2331 }
2332
2333 off = file_buffer->block;
2334
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002335 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
plougher5507dd92006-11-06 00:43:10 +00002336 pthread_mutex_lock(&pos_mutex);
2337
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002338 if(lseek(fd, off, SEEK_SET) == -1) {
Phillip Lougherd23000c2013-02-06 22:05:45 +00002339 ERROR("writer: Lseek on destination failed because "
2340 "%s, offset=0x%llx\n", strerror(errno), off);
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01002341 BAD_ERROR("Probably out of space on output "
Phillip Lougher3f4ccd22013-02-11 04:40:09 +00002342 "%s\n", block_device ? "block device" :
2343 "filesystem");
plougher5507dd92006-11-06 00:43:10 +00002344 }
2345
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002346 if(write_bytes(fd, file_buffer->data,
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01002347 file_buffer->size) == -1)
2348 BAD_ERROR("Failed to write to output %s\n",
Phillip Loughere9df4552013-02-14 23:06:29 +00002349 block_device ? "block device" : "filesystem");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002350
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002351 pthread_cleanup_pop(1);
2352
ploughereb6eac92008-02-26 01:50:48 +00002353 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002354 }
2355}
2356
2357
plougher5b09fd42007-08-06 10:28:41 +00002358int all_zero(struct file_buffer *file_buffer)
2359{
2360 int i;
2361 long entries = file_buffer->size / sizeof(long);
2362 long *p = (long *) file_buffer->data;
2363
2364 for(i = 0; i < entries && p[i] == 0; i++);
2365
2366 if(i == entries) {
plougher110799c2009-03-30 01:50:40 +00002367 for(i = file_buffer->size & ~(sizeof(long) - 1);
plougher50b31762009-03-31 04:14:46 +00002368 i < file_buffer->size && file_buffer->data[i] == 0;
2369 i++);
plougher5b09fd42007-08-06 10:28:41 +00002370
2371 return i == file_buffer->size;
2372 }
2373
2374 return 0;
2375}
2376
2377
plougher5507dd92006-11-06 00:43:10 +00002378void *deflator(void *arg)
2379{
Phillip Lougher943acad2014-04-17 03:31:01 +01002380 struct file_buffer *write_buffer = cache_get_nohash(bwriter_buffer);
plougher7b8ee502009-07-29 07:54:30 +00002381 void *stream = NULL;
Phillip Lougher3c838292013-03-26 04:24:53 +00002382 int res;
plougher5507dd92006-11-06 00:43:10 +00002383
plougher13fdddf2010-11-24 01:23:41 +00002384 res = compressor_init(comp, &stream, block_size, 1);
2385 if(res)
2386 BAD_ERROR("deflator:: compressor_init failed\n");
2387
plougher5507dd92006-11-06 00:43:10 +00002388 while(1) {
Phillip Loughercf478e92013-05-29 02:38:41 +01002389 struct file_buffer *file_buffer = queue_get(to_deflate);
plougher5507dd92006-11-06 00:43:10 +00002390
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002391 if(sparse_files && all_zero(file_buffer)) {
Phillip Lougher48854382011-09-09 03:36:55 +01002392 file_buffer->c_byte = 0;
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002393 seq_queue_put(to_main, file_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002394 } else {
plougher13fdddf2010-11-24 01:23:41 +00002395 write_buffer->c_byte = mangle2(stream,
plougher50b31762009-03-31 04:14:46 +00002396 write_buffer->data, file_buffer->data,
Phillip Lougher63f531f2011-09-10 04:03:32 +01002397 file_buffer->size, block_size,
2398 file_buffer->noD, 1);
plougher1b899fc2008-08-07 01:24:06 +00002399 write_buffer->sequence = file_buffer->sequence;
2400 write_buffer->file_size = file_buffer->file_size;
2401 write_buffer->block = file_buffer->block;
plougher110799c2009-03-30 01:50:40 +00002402 write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK
2403 (write_buffer->c_byte);
plougher1b899fc2008-08-07 01:24:06 +00002404 write_buffer->fragment = FALSE;
2405 write_buffer->error = FALSE;
2406 cache_block_put(file_buffer);
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002407 seq_queue_put(to_main, write_buffer);
Phillip Lougher943acad2014-04-17 03:31:01 +01002408 write_buffer = cache_get_nohash(bwriter_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002409 }
plougher5507dd92006-11-06 00:43:10 +00002410 }
2411}
2412
2413
2414void *frag_deflator(void *arg)
2415{
plougher7b8ee502009-07-29 07:54:30 +00002416 void *stream = NULL;
Phillip Lougher3c838292013-03-26 04:24:53 +00002417 int res;
plougher5507dd92006-11-06 00:43:10 +00002418
plougher13fdddf2010-11-24 01:23:41 +00002419 res = compressor_init(comp, &stream, block_size, 1);
2420 if(res)
2421 BAD_ERROR("frag_deflator:: compressor_init failed\n");
2422
Phillip Lougher53240e62013-04-30 23:51:29 +01002423 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
2424
plougher5507dd92006-11-06 00:43:10 +00002425 while(1) {
2426 int c_byte, compressed_size;
2427 struct file_buffer *file_buffer = queue_get(to_frag);
plougher110799c2009-03-30 01:50:40 +00002428 struct file_buffer *write_buffer =
Phillip Lougher943acad2014-04-17 03:31:01 +01002429 cache_get(fwriter_buffer, file_buffer->block);
plougher5507dd92006-11-06 00:43:10 +00002430
plougher13fdddf2010-11-24 01:23:41 +00002431 c_byte = mangle2(stream, write_buffer->data, file_buffer->data,
plougher110799c2009-03-30 01:50:40 +00002432 file_buffer->size, block_size, noF, 1);
plougher5507dd92006-11-06 00:43:10 +00002433 compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
plougherd036a312008-03-08 01:38:27 +00002434 write_buffer->size = compressed_size;
plougherd1139d52008-04-28 03:07:07 +00002435 pthread_mutex_lock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00002436 if(fragments_locked == FALSE) {
plougher2ea89142008-03-11 01:34:19 +00002437 fragment_table[file_buffer->block].size = c_byte;
2438 fragment_table[file_buffer->block].start_block = bytes;
2439 write_buffer->block = bytes;
2440 bytes += compressed_size;
2441 fragments_outstanding --;
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002442 pthread_mutex_unlock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00002443 queue_put(to_writer, write_buffer);
plougher110799c2009-03-30 01:50:40 +00002444 TRACE("Writing fragment %lld, uncompressed size %d, "
2445 "compressed size %d\n", file_buffer->block,
2446 file_buffer->size, compressed_size);
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002447 } else {
plougher110799c2009-03-30 01:50:40 +00002448 add_pending_fragment(write_buffer, c_byte,
2449 file_buffer->block);
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002450 pthread_mutex_unlock(&fragment_mutex);
2451 }
ploughereb6eac92008-02-26 01:50:48 +00002452 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002453 }
Phillip Lougher53240e62013-04-30 23:51:29 +01002454
2455 pthread_cleanup_pop(0);
plougher5507dd92006-11-06 00:43:10 +00002456}
2457
2458
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002459struct file_buffer *get_file_buffer()
plougher5507dd92006-11-06 00:43:10 +00002460{
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002461 struct file_buffer *file_buffer = seq_queue_get(to_main);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002462
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002463 return file_buffer;
plougher5507dd92006-11-06 00:43:10 +00002464}
2465
2466
plougher110799c2009-03-30 01:50:40 +00002467void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent,
Phillip Lougher948bf3a2014-03-02 02:23:54 +00002468 struct file_buffer *file_buffer, int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002469{
2470 file_count ++;
2471 *duplicate_file = FALSE;
Phillip Lougher948bf3a2014-03-02 02:23:54 +00002472 cache_block_put(file_buffer);
plougherc1ace522010-05-01 03:00:45 +00002473 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0,
2474 NULL, &empty_fragment, NULL, 0);
plougher5507dd92006-11-06 00:43:10 +00002475}
2476
2477
Phillip Lougher8531f872014-03-01 03:22:41 +00002478void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent,
plougher110799c2009-03-30 01:50:40 +00002479 struct file_buffer *file_buffer, int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002480{
Phillip Lougher8531f872014-03-01 03:22:41 +00002481 int size = file_buffer->file_size;
plougher5507dd92006-11-06 00:43:10 +00002482 struct fragment *fragment;
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002483 unsigned short checksum = file_buffer->checksum;
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002484 char dont_put;
plougher5507dd92006-11-06 00:43:10 +00002485
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002486 fragment = frag_duplicate(file_buffer, &dont_put);
2487 *duplicate_file = !fragment;
2488 if(!fragment) {
2489 fragment = get_and_fill_fragment(file_buffer, dir_ent);
2490 if(duplicate_checking)
2491 add_non_dup(size, 0, NULL, 0, fragment, 0, checksum,
2492 TRUE, TRUE);
plougher29e37092007-04-15 01:24:51 +00002493 }
plougher5507dd92006-11-06 00:43:10 +00002494
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002495 if(dont_put)
2496 free(file_buffer);
2497 else
2498 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002499
2500 total_bytes += size;
2501 file_count ++;
2502
plougher35a10602008-04-21 02:58:16 +00002503 inc_progress_bar();
plougher02bc3bc2007-02-25 12:12:01 +00002504
plougherc1ace522010-05-01 03:00:45 +00002505 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0,
plougher3c6bdb52010-05-01 02:30:59 +00002506 0, NULL, fragment, NULL, 0);
plougher29e37092007-04-15 01:24:51 +00002507
Phillip Lougher8bb17b02014-03-30 23:59:55 +01002508 if(!duplicate_checking)
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002509 free_fragment(fragment);
plougher5507dd92006-11-06 00:43:10 +00002510}
2511
2512
plougher00d08172009-09-03 10:17:44 +00002513int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent,
2514 struct file_buffer *read_buffer, int *duplicate_file)
2515{
2516 long long read_size, file_bytes, start;
2517 struct fragment *fragment;
2518 unsigned int *block_list = NULL;
2519 int block = 0, status;
2520 long long sparse = 0;
2521 struct file_buffer *fragment_buffer = NULL;
2522
2523 *duplicate_file = FALSE;
2524
2525 lock_fragments();
2526
2527 file_bytes = 0;
2528 start = bytes;
2529 while (1) {
2530 read_size = read_buffer->file_size;
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002531 if(read_buffer->fragment)
plougher00d08172009-09-03 10:17:44 +00002532 fragment_buffer = read_buffer;
2533 else {
2534 block_list = realloc(block_list, (block + 1) *
2535 sizeof(unsigned int));
2536 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002537 MEM_ERROR();
plougher00d08172009-09-03 10:17:44 +00002538 block_list[block ++] = read_buffer->c_byte;
2539 if(read_buffer->c_byte) {
2540 read_buffer->block = bytes;
2541 bytes += read_buffer->size;
Phillip Lougher84e20d72014-02-23 05:23:41 +00002542 cache_hash(read_buffer, read_buffer->block);
plougher00d08172009-09-03 10:17:44 +00002543 file_bytes += read_buffer->size;
2544 queue_put(to_writer, read_buffer);
2545 } else {
2546 sparse += read_buffer->size;
2547 cache_block_put(read_buffer);
2548 }
2549 }
plougher286b6b32009-09-19 03:29:27 +00002550 inc_progress_bar();
plougher00d08172009-09-03 10:17:44 +00002551
2552 if(read_size != -1)
2553 break;
2554
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002555 read_buffer = get_file_buffer();
plougher00d08172009-09-03 10:17:44 +00002556 if(read_buffer->error)
2557 goto read_err;
2558 }
2559
2560 unlock_fragments();
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002561 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher00d08172009-09-03 10:17:44 +00002562
2563 if(duplicate_checking)
2564 add_non_dup(read_size, file_bytes, block_list, start, fragment,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002565 0, fragment_buffer ? fragment_buffer->checksum : 0,
2566 FALSE, TRUE);
2567 cache_block_put(fragment_buffer);
plougher00d08172009-09-03 10:17:44 +00002568 file_count ++;
2569 total_bytes += read_size;
2570
plougherc1ace522010-05-01 03:00:45 +00002571 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2572 block, block_list, fragment, NULL, sparse);
plougher00d08172009-09-03 10:17:44 +00002573
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002574 if(duplicate_checking == FALSE) {
plougher00d08172009-09-03 10:17:44 +00002575 free(block_list);
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002576 free_fragment(fragment);
2577 }
plougher00d08172009-09-03 10:17:44 +00002578
2579 return 0;
2580
2581read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002582 dec_progress_bar(block);
plougher00d08172009-09-03 10:17:44 +00002583 status = read_buffer->error;
2584 bytes = start;
2585 if(!block_device) {
2586 int res;
2587
2588 queue_put(to_writer, NULL);
2589 if(queue_get(from_writer) != 0)
2590 EXIT_MKSQUASHFS();
2591 res = ftruncate(fd, bytes);
2592 if(res != 0)
2593 BAD_ERROR("Failed to truncate dest file because %s\n",
2594 strerror(errno));
2595 }
2596 unlock_fragments();
2597 free(block_list);
2598 cache_block_put(read_buffer);
2599 return status;
2600}
2601
2602
plougher110799c2009-03-30 01:50:40 +00002603int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent,
Phillip Lougher1807caa2014-03-01 03:28:33 +00002604 struct file_buffer *read_buffer, int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002605{
plougher29e37092007-04-15 01:24:51 +00002606 int block, thresh;
Phillip Lougher1807caa2014-03-01 03:28:33 +00002607 long long read_size = read_buffer->file_size;
plougher1b899fc2008-08-07 01:24:06 +00002608 long long file_bytes, dup_start, start;
plougher5507dd92006-11-06 00:43:10 +00002609 struct fragment *fragment;
2610 struct file_info *dupl_ptr;
2611 int blocks = (read_size + block_size - 1) >> block_log;
2612 unsigned int *block_list, *block_listp;
plougher1b899fc2008-08-07 01:24:06 +00002613 struct file_buffer **buffer_list;
Phillip Lougher943acad2014-04-17 03:31:01 +01002614 int status;
plougher1b899fc2008-08-07 01:24:06 +00002615 long long sparse = 0;
2616 struct file_buffer *fragment_buffer = NULL;
plougher5507dd92006-11-06 00:43:10 +00002617
plougher50b31762009-03-31 04:14:46 +00002618 block_list = malloc(blocks * sizeof(unsigned int));
2619 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002620 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00002621 block_listp = block_list;
2622
plougher50b31762009-03-31 04:14:46 +00002623 buffer_list = malloc(blocks * sizeof(struct file_buffer *));
2624 if(buffer_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002625 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00002626
Phillip Lougher943acad2014-04-17 03:31:01 +01002627 lock_fragments();
plougher5507dd92006-11-06 00:43:10 +00002628
2629 file_bytes = 0;
plougher1b899fc2008-08-07 01:24:06 +00002630 start = dup_start = bytes;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01002631 thresh = blocks > bwriter_size ? blocks - bwriter_size : 0;
plougher1b899fc2008-08-07 01:24:06 +00002632
2633 for(block = 0; block < blocks;) {
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002634 if(read_buffer->fragment) {
Phillip Lougher38cb1ab2013-01-13 21:20:09 +00002635 block_list[block] = 0;
Phillip Lougher19de5b62013-01-13 21:31:51 +00002636 buffer_list[block] = NULL;
plougher1b899fc2008-08-07 01:24:06 +00002637 fragment_buffer = read_buffer;
2638 blocks = read_size >> block_log;
2639 } else {
2640 block_list[block] = read_buffer->c_byte;
2641
2642 if(read_buffer->c_byte) {
2643 read_buffer->block = bytes;
2644 bytes += read_buffer->size;
2645 file_bytes += read_buffer->size;
Phillip Lougher84e20d72014-02-23 05:23:41 +00002646 cache_hash(read_buffer, read_buffer->block);
plougher1b899fc2008-08-07 01:24:06 +00002647 if(block < thresh) {
2648 buffer_list[block] = NULL;
2649 queue_put(to_writer, read_buffer);
2650 } else
2651 buffer_list[block] = read_buffer;
2652 } else {
2653 buffer_list[block] = NULL;
2654 sparse += read_buffer->size;
2655 cache_block_put(read_buffer);
2656 }
2657 }
2658 inc_progress_bar();
2659
2660 if(++block < blocks) {
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002661 read_buffer = get_file_buffer();
plougher018d2b32007-04-23 03:01:48 +00002662 if(read_buffer->error)
2663 goto read_err;
2664 }
plougher5507dd92006-11-06 00:43:10 +00002665 }
2666
plougher110799c2009-03-30 01:50:40 +00002667 dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002668 &fragment, fragment_buffer, blocks, 0, FALSE);
plougher5507dd92006-11-06 00:43:10 +00002669
2670 if(dupl_ptr) {
2671 *duplicate_file = FALSE;
2672 for(block = thresh; block < blocks; block ++)
plougher1b899fc2008-08-07 01:24:06 +00002673 if(buffer_list[block])
2674 queue_put(to_writer, buffer_list[block]);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002675 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher5507dd92006-11-06 00:43:10 +00002676 dupl_ptr->fragment = fragment;
2677 } else {
2678 *duplicate_file = TRUE;
2679 for(block = thresh; block < blocks; block ++)
plougher1b899fc2008-08-07 01:24:06 +00002680 cache_block_put(buffer_list[block]);
2681 bytes = start;
2682 if(thresh && !block_device) {
plougher12a159a2009-03-03 11:06:34 +00002683 int res;
2684
plougher1b899fc2008-08-07 01:24:06 +00002685 queue_put(to_writer, NULL);
2686 if(queue_get(from_writer) != 0)
2687 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002688 res = ftruncate(fd, bytes);
2689 if(res != 0)
2690 BAD_ERROR("Failed to truncate dest file because"
2691 " %s\n", strerror(errno));
plougher1b899fc2008-08-07 01:24:06 +00002692 }
plougher5507dd92006-11-06 00:43:10 +00002693 }
2694
plougher2ea89142008-03-11 01:34:19 +00002695 unlock_fragments();
plougher1b899fc2008-08-07 01:24:06 +00002696 cache_block_put(fragment_buffer);
plougher5507dd92006-11-06 00:43:10 +00002697 free(buffer_list);
2698 file_count ++;
2699 total_bytes += read_size;
2700
plougher360514a2009-03-30 03:01:38 +00002701 /*
2702 * sparse count is needed to ensure squashfs correctly reports a
plougher1b899fc2008-08-07 01:24:06 +00002703 * a smaller block count on stat calls to sparse files. This is
2704 * to ensure intelligent applications like cp correctly handle the
2705 * file as a sparse file. If the file in the original filesystem isn't
2706 * stored as a sparse file then still store it sparsely in squashfs, but
plougher360514a2009-03-30 03:01:38 +00002707 * report it as non-sparse on stat calls to preserve semantics
2708 */
plougher1b899fc2008-08-07 01:24:06 +00002709 if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2710 sparse = 0;
2711
plougherc1ace522010-05-01 03:00:45 +00002712 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size,
2713 dup_start, blocks, block_listp, fragment, NULL, sparse);
plougher29e37092007-04-15 01:24:51 +00002714
plougher5507dd92006-11-06 00:43:10 +00002715 if(*duplicate_file == TRUE)
2716 free(block_list);
plougher29e37092007-04-15 01:24:51 +00002717
plougher018d2b32007-04-23 03:01:48 +00002718 return 0;
plougher5507dd92006-11-06 00:43:10 +00002719
2720read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002721 dec_progress_bar(block);
plougher018d2b32007-04-23 03:01:48 +00002722 status = read_buffer->error;
plougher1b899fc2008-08-07 01:24:06 +00002723 bytes = start;
2724 if(thresh && !block_device) {
plougher12a159a2009-03-03 11:06:34 +00002725 int res;
2726
plougher5507dd92006-11-06 00:43:10 +00002727 queue_put(to_writer, NULL);
2728 if(queue_get(from_writer) != 0)
2729 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002730 res = ftruncate(fd, bytes);
2731 if(res != 0)
2732 BAD_ERROR("Failed to truncate dest file because %s\n",
2733 strerror(errno));
plougher5507dd92006-11-06 00:43:10 +00002734 }
plougher2ea89142008-03-11 01:34:19 +00002735 unlock_fragments();
plougher5507dd92006-11-06 00:43:10 +00002736 for(blocks = thresh; blocks < block; blocks ++)
plougher1b899fc2008-08-07 01:24:06 +00002737 cache_block_put(buffer_list[blocks]);
plougher5507dd92006-11-06 00:43:10 +00002738 free(buffer_list);
2739 free(block_list);
ploughereb6eac92008-02-26 01:50:48 +00002740 cache_block_put(read_buffer);
plougher018d2b32007-04-23 03:01:48 +00002741 return status;
plougher5507dd92006-11-06 00:43:10 +00002742}
2743
2744
Phillip Lougher75bd21d2014-03-02 03:33:56 +00002745int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent,
2746 struct file_buffer *read_buffer, int *dup)
2747{
2748 long long read_size = read_buffer->file_size;
2749 long long file_bytes, start;
2750 struct fragment *fragment;
2751 unsigned int *block_list;
2752 int block, status;
2753 int blocks = (read_size + block_size - 1) >> block_log;
2754 long long sparse = 0;
2755 struct file_buffer *fragment_buffer = NULL;
2756
2757 if(pre_duplicate(read_size))
2758 return write_file_blocks_dup(inode, dir_ent, read_buffer, dup);
2759
2760 *dup = FALSE;
2761
2762 block_list = malloc(blocks * sizeof(unsigned int));
2763 if(block_list == NULL)
2764 MEM_ERROR();
2765
2766 lock_fragments();
2767
2768 file_bytes = 0;
2769 start = bytes;
2770 for(block = 0; block < blocks;) {
2771 if(read_buffer->fragment) {
2772 block_list[block] = 0;
2773 fragment_buffer = read_buffer;
2774 blocks = read_size >> block_log;
2775 } else {
2776 block_list[block] = read_buffer->c_byte;
2777 if(read_buffer->c_byte) {
2778 read_buffer->block = bytes;
2779 bytes += read_buffer->size;
2780 cache_hash(read_buffer, read_buffer->block);
2781 file_bytes += read_buffer->size;
2782 queue_put(to_writer, read_buffer);
2783 } else {
2784 sparse += read_buffer->size;
2785 cache_block_put(read_buffer);
2786 }
2787 }
2788 inc_progress_bar();
2789
2790 if(++block < blocks) {
2791 read_buffer = get_file_buffer();
2792 if(read_buffer->error)
2793 goto read_err;
2794 }
2795 }
2796
2797 unlock_fragments();
2798 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
Phillip Lougher75bd21d2014-03-02 03:33:56 +00002799
2800 if(duplicate_checking)
2801 add_non_dup(read_size, file_bytes, block_list, start, fragment,
Phillip Lougherc6424dc2014-03-13 02:01:28 +00002802 0, fragment_buffer ? fragment_buffer->checksum : 0,
2803 FALSE, TRUE);
2804 cache_block_put(fragment_buffer);
Phillip Lougher75bd21d2014-03-02 03:33:56 +00002805 file_count ++;
2806 total_bytes += read_size;
2807
2808 /*
2809 * sparse count is needed to ensure squashfs correctly reports a
2810 * a smaller block count on stat calls to sparse files. This is
2811 * to ensure intelligent applications like cp correctly handle the
2812 * file as a sparse file. If the file in the original filesystem isn't
2813 * stored as a sparse file then still store it sparsely in squashfs, but
2814 * report it as non-sparse on stat calls to preserve semantics
2815 */
2816 if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2817 sparse = 0;
2818
2819 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2820 blocks, block_list, fragment, NULL, sparse);
2821
2822 if(duplicate_checking == FALSE) {
2823 free(block_list);
2824 free_fragment(fragment);
2825 }
2826
2827 return 0;
2828
2829read_err:
2830 dec_progress_bar(block);
2831 status = read_buffer->error;
2832 bytes = start;
2833 if(!block_device) {
2834 int res;
2835
2836 queue_put(to_writer, NULL);
2837 if(queue_get(from_writer) != 0)
2838 EXIT_MKSQUASHFS();
2839 res = ftruncate(fd, bytes);
2840 if(res != 0)
2841 BAD_ERROR("Failed to truncate dest file because %s\n",
2842 strerror(errno));
2843 }
2844 unlock_fragments();
2845 free(block_list);
2846 cache_block_put(read_buffer);
2847 return status;
2848}
2849
2850
Phillip Lougherc3489132014-03-02 01:41:36 +00002851void write_file(squashfs_inode *inode, struct dir_ent *dir, int *dup)
plougher5507dd92006-11-06 00:43:10 +00002852{
plougher018d2b32007-04-23 03:01:48 +00002853 int status;
2854 struct file_buffer *read_buffer;
plougher5507dd92006-11-06 00:43:10 +00002855
plougher018d2b32007-04-23 03:01:48 +00002856again:
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002857 read_buffer = get_file_buffer();
Phillip Lougherc3489132014-03-02 01:41:36 +00002858 status = read_buffer->error;
plougher1b899fc2008-08-07 01:24:06 +00002859
Phillip Lougherc3489132014-03-02 01:41:36 +00002860 if(status)
ploughereb6eac92008-02-26 01:50:48 +00002861 cache_block_put(read_buffer);
Phillip Lougherc3489132014-03-02 01:41:36 +00002862 else if(read_buffer->file_size == -1)
2863 status = write_file_process(inode, dir, read_buffer, dup);
Phillip Lougher948bf3a2014-03-02 02:23:54 +00002864 else if(read_buffer->file_size == 0)
2865 write_file_empty(inode, dir, read_buffer, dup);
2866 else if(read_buffer->fragment && read_buffer->c_byte)
Phillip Lougherc3489132014-03-02 01:41:36 +00002867 write_file_frag(inode, dir, read_buffer, dup);
plougher29e37092007-04-15 01:24:51 +00002868 else
Phillip Lougherc3489132014-03-02 01:41:36 +00002869 status = write_file_blocks(inode, dir, read_buffer, dup);
plougher5507dd92006-11-06 00:43:10 +00002870
plougher018d2b32007-04-23 03:01:48 +00002871 if(status == 2) {
plougher50b31762009-03-31 04:14:46 +00002872 ERROR("File %s changed size while reading filesystem, "
Phillip Lougherc3489132014-03-02 01:41:36 +00002873 "attempting to re-read\n", pathname(dir));
plougher018d2b32007-04-23 03:01:48 +00002874 goto again;
2875 } else if(status == 1) {
Phillip Lougherc3489132014-03-02 01:41:36 +00002876 ERROR_START("Failed to read file %s", pathname(dir));
Phillip Lougher85776df2014-01-31 03:10:46 +00002877 ERROR_EXIT(", creating empty file\n");
Phillip Lougher948bf3a2014-03-02 02:23:54 +00002878 write_file_empty(inode, dir, NULL, dup);
plougher29e37092007-04-15 01:24:51 +00002879 }
plougher5507dd92006-11-06 00:43:10 +00002880}
2881
2882
Phillip Lougher3f81a772012-12-04 05:07:24 +00002883#define BUFF_SIZE 512
plougher1f413c82005-11-18 00:02:14 +00002884char *name;
2885char *basename_r();
2886
2887char *getbase(char *pathname)
2888{
Phillip Lougher3f81a772012-12-04 05:07:24 +00002889 static char *b_buffer = NULL;
2890 static int b_size = BUFF_SIZE;
plougher1f413c82005-11-18 00:02:14 +00002891 char *result;
2892
Phillip Lougher3f81a772012-12-04 05:07:24 +00002893 if(b_buffer == NULL) {
2894 b_buffer = malloc(b_size);
2895 if(b_buffer == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002896 MEM_ERROR();
Phillip Lougher3f81a772012-12-04 05:07:24 +00002897 }
2898
2899 while(1) {
2900 if(*pathname != '/') {
2901 result = getcwd(b_buffer, b_size);
2902 if(result == NULL && errno != ERANGE)
2903 BAD_ERROR("Getcwd failed in getbase\n");
2904
2905 /* enough room for pathname + "/" + '\0' terminator? */
2906 if(result && strlen(pathname) + 2 <=
2907 b_size - strlen(b_buffer)) {
2908 strcat(strcat(b_buffer, "/"), pathname);
2909 break;
2910 }
2911 } else if(strlen(pathname) < b_size) {
2912 strcpy(b_buffer, pathname);
2913 break;
2914 }
2915
2916 /* Buffer not large enough, realloc and try again */
2917 b_buffer = realloc(b_buffer, b_size += BUFF_SIZE);
2918 if(b_buffer == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002919 MEM_ERROR();
Phillip Lougher3f81a772012-12-04 05:07:24 +00002920 }
2921
plougher1f413c82005-11-18 00:02:14 +00002922 name = b_buffer;
2923 if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0))
2924 return NULL;
2925 else
2926 return result;
2927}
2928
2929
2930char *basename_r()
2931{
2932 char *s;
2933 char *p;
2934 int n = 1;
2935
2936 for(;;) {
2937 s = name;
2938 if(*name == '\0')
2939 return NULL;
2940 if(*name != '/') {
2941 while(*name != '\0' && *name != '/') name++;
2942 n = name - s;
2943 }
2944 while(*name == '/') name++;
2945 if(strncmp(s, ".", n) == 0)
2946 continue;
plougher110799c2009-03-30 01:50:40 +00002947 if((*name == '\0') || (strncmp(s, "..", n) == 0) ||
2948 ((p = basename_r()) == NULL)) {
plougher1f413c82005-11-18 00:02:14 +00002949 s[n] = '\0';
2950 return s;
2951 }
2952 if(strcmp(p, "..") == 0)
2953 continue;
2954 return p;
2955 }
2956}
2957
2958
Phillip Lougher81204c22012-07-25 03:30:30 +01002959struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id)
plougher1f413c82005-11-18 00:02:14 +00002960{
Phillip Lougherd6577802012-10-04 19:14:33 +01002961 int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino);
2962 struct inode_info *inode;
plougher1f413c82005-11-18 00:02:14 +00002963
Phillip Lougherd6577802012-10-04 19:14:33 +01002964 /*
2965 * Look-up inode in hash table, if it already exists we have a
2966 * hard-link, so increment the nlink count and return it.
2967 * Don't do the look-up for directories because we don't hard-link
2968 * directories.
2969 */
2970 if ((buf->st_mode & S_IFMT) != S_IFDIR) {
2971 for(inode = inode_info[ino_hash]; inode; inode = inode->next) {
2972 if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) {
2973 inode->nlink ++;
2974 return inode;
2975 }
plougher1f413c82005-11-18 00:02:14 +00002976 }
plougher1f413c82005-11-18 00:02:14 +00002977 }
2978
plougher288f6852010-12-16 05:22:55 +00002979 inode = malloc(sizeof(struct inode_info));
2980 if(inode == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002981 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00002982
2983 memcpy(&inode->buf, buf, sizeof(struct stat));
plougher5507dd92006-11-06 00:43:10 +00002984 inode->read = FALSE;
ploughera326c182009-08-29 05:41:45 +00002985 inode->root_entry = FALSE;
Phillip Lougher81204c22012-07-25 03:30:30 +01002986 inode->pseudo_file = pseudo;
2987 inode->pseudo_id = id;
plougher1f413c82005-11-18 00:02:14 +00002988 inode->inode = SQUASHFS_INVALID_BLK;
2989 inode->nlink = 1;
Phillip Lougher539c2b12012-07-30 20:14:52 +01002990 inode->inode_number = 0;
plougherdc86c3c2007-12-05 02:15:10 +00002991
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002992 /*
2993 * Copy filesystem wide defaults into inode, these filesystem
2994 * wide defaults may be altered on an individual inode basis by
2995 * user specified actions
2996 *
2997 */
2998 inode->no_fragments = no_fragments;
2999 inode->always_use_fragments = always_use_fragments;
Phillip Lougher63f531f2011-09-10 04:03:32 +01003000 inode->noD = noD;
3001 inode->noF = noF;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01003002
plougherdc86c3c2007-12-05 02:15:10 +00003003 if((buf->st_mode & S_IFMT) == S_IFREG)
Phillip Lougher3b89ee82012-10-18 23:55:37 +01003004 progress_bar_size((buf->st_size + block_size - 1) >> block_log);
plougherdc86c3c2007-12-05 02:15:10 +00003005
Phillip Lougherd6577802012-10-04 19:14:33 +01003006 inode->next = inode_info[ino_hash];
3007 inode_info[ino_hash] = inode;
plougher1f413c82005-11-18 00:02:14 +00003008
3009 return inode;
3010}
3011
3012
Phillip Lougher81204c22012-07-25 03:30:30 +01003013inline struct inode_info *lookup_inode(struct stat *buf)
3014{
3015 return lookup_inode2(buf, 0, 0);
3016}
3017
3018
Phillip Lougher539c2b12012-07-30 20:14:52 +01003019inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this)
3020{
3021 if (inode->inode_number == 0)
3022 inode->inode_number = use_this ? : inode_no ++;
3023}
3024
3025
Phillip Lougher494479f2012-02-03 15:45:41 +00003026inline struct dir_ent *create_dir_entry(char *name, char *source_name,
3027 char *nonstandard_pathname, struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003028{
Phillip Lougher494479f2012-02-03 15:45:41 +00003029 struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent));
3030 if(dir_ent == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003031 MEM_ERROR();
Phillip Lougher494479f2012-02-03 15:45:41 +00003032
3033 dir_ent->name = name;
3034 dir_ent->source_name = source_name;
3035 dir_ent->nonstandard_pathname = nonstandard_pathname;
3036 dir_ent->our_dir = dir;
Phillip Lougherbf338362012-08-22 05:24:36 +01003037 dir_ent->next = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003038
3039 return dir_ent;
3040}
3041
3042
3043inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir,
3044 struct inode_info *inode_info)
3045{
3046 struct dir_info *dir = dir_ent->our_dir;
3047
plougher1f413c82005-11-18 00:02:14 +00003048 if(sub_dir)
Phillip Lougher494479f2012-02-03 15:45:41 +00003049 sub_dir->dir_ent = dir_ent;
3050 dir_ent->inode = inode_info;
3051 dir_ent->dir = sub_dir;
3052
Phillip Lougherbf338362012-08-22 05:24:36 +01003053 dir_ent->next = dir->list;
3054 dir->list = dir_ent;
3055 dir->count++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003056}
3057
3058
3059inline void add_dir_entry2(char *name, char *source_name,
3060 char *nonstandard_pathname, struct dir_info *sub_dir,
3061 struct inode_info *inode_info, struct dir_info *dir)
3062{
3063 struct dir_ent *dir_ent = create_dir_entry(name, source_name,
3064 nonstandard_pathname, dir);
3065
3066
3067 add_dir_entry(dir_ent, sub_dir, inode_info);
plougher1f413c82005-11-18 00:02:14 +00003068}
3069
3070
Phillip Lougher10a4b572012-02-18 23:26:10 +00003071inline void free_dir_entry(struct dir_ent *dir_ent)
3072{
Phillip Loughereb92b192012-10-01 03:39:00 +01003073 if(dir_ent->name)
Phillip Lougher10a4b572012-02-18 23:26:10 +00003074 free(dir_ent->name);
3075
Phillip Loughereb92b192012-10-01 03:39:00 +01003076 if(dir_ent->source_name)
Phillip Lougher10a4b572012-02-18 23:26:10 +00003077 free(dir_ent->source_name);
3078
3079 free(dir_ent);
3080}
3081
3082
Phillip Lougherad0f9212011-12-31 00:55:51 +00003083inline void add_excluded(struct dir_info *dir)
3084{
3085 dir->excluded ++;
3086}
3087
3088
Phillip Lougherabc3b492012-07-29 02:53:35 +01003089void dir_scan(squashfs_inode *inode, char *pathname,
Phillip Lougherbae0e422014-01-19 23:42:11 +00003090 struct dir_ent *(_readdir)(struct dir_info *), int progress)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003091{
3092 struct stat buf;
3093 struct dir_info *dir_info = dir_scan1(pathname, "", paths, _readdir, 1);
3094 struct dir_ent *dir_ent;
3095
3096 if(dir_info == NULL)
3097 return;
3098
Phillip Lougherf3d0f9c2012-11-14 05:36:58 +00003099 /*
3100 * Process most actions and any pseudo files
3101 */
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00003102 if(actions() || get_pseudo())
3103 dir_scan2(dir_info, get_pseudo());
Phillip Lougherf3d0f9c2012-11-14 05:36:58 +00003104
Phillip Loughere92cb512012-11-15 02:22:28 +00003105 /*
3106 * Process move actions
3107 */
3108 if(move_actions()) {
3109 dir_scan3(dir_info, dir_info);
3110 do_move_actions();
3111 }
3112
Phillip Lougher71ae14e2012-11-15 02:27:50 +00003113 /*
3114 * Process empty actions
3115 */
3116 if(empty_actions())
3117 dir_scan4(dir_info);
3118
Phillip Loughereb69dad2012-11-15 03:34:02 +00003119 /*
3120 * Sort directories and compute the inode numbers
3121 */
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003122 dir_scan5(dir_info);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003123
Phillip Lougherd0fe1d52012-10-09 03:34:34 +01003124 dir_ent = create_dir_entry("", NULL, pathname,
Phillip Lougher1eae83d2012-09-26 02:12:45 +01003125 scan1_opendir("", "", 0));
Phillip Lougherabc3b492012-07-29 02:53:35 +01003126
3127 if(pathname[0] == '\0') {
3128 /*
3129 * dummy top level directory, if multiple sources specified on
3130 * command line
3131 */
3132 memset(&buf, 0, sizeof(buf));
3133 buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR;
3134 buf.st_uid = getuid();
3135 buf.st_gid = getgid();
3136 buf.st_mtime = time(NULL);
3137 buf.st_dev = 0;
3138 buf.st_ino = 0;
3139 dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0);
3140 } else {
Phillip Lougher38a35ec2012-11-30 04:31:35 +00003141 if(lstat(pathname, &buf) == -1)
3142 /* source directory has disappeared? */
Phillip Lougherfe58b492012-12-15 01:27:07 +00003143 BAD_ERROR("Cannot stat source directory %s because %s\n",
Phillip Lougherabc3b492012-07-29 02:53:35 +01003144 pathname, strerror(errno));
Phillip Lougherabc3b492012-07-29 02:53:35 +01003145 dir_ent->inode = lookup_inode(&buf);
3146 }
3147
Phillip Lougher539c2b12012-07-30 20:14:52 +01003148 alloc_inode_no(dir_ent->inode, root_inode_number);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003149 dir_ent->dir = dir_info;
3150 dir_info->dir_ent = dir_ent;
3151
Andy Lutomirski3f31dcf2012-09-21 18:08:08 -07003152 eval_actions(dir_ent);
3153
Phillip Lougher19b877d2013-02-23 11:15:38 +00003154 if(sorted)
3155 generate_file_priorities(dir_info, 0,
Phillip Lougherabc3b492012-07-29 02:53:35 +01003156 &dir_info->dir_ent->inode->buf);
Phillip Lougher3794e342014-04-10 02:15:45 +01003157
3158 if(appending) {
3159 sigset_t sigmask;
3160
3161 restore_thread = init_restore_thread();
3162 sigemptyset(&sigmask);
3163 sigaddset(&sigmask, SIGINT);
3164 sigaddset(&sigmask, SIGTERM);
3165 sigaddset(&sigmask, SIGUSR1);
3166 if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
3167 BAD_ERROR("Failed to set signal mask\n");
3168 write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0");
3169 }
3170
Phillip Lougherabc3b492012-07-29 02:53:35 +01003171 queue_put(to_reader, dir_info);
Phillip Lougher3794e342014-04-10 02:15:45 +01003172
Phillip Lougherabc3b492012-07-29 02:53:35 +01003173 if(sorted)
3174 sort_files_and_write(dir_info);
Phillip Lougher3794e342014-04-10 02:15:45 +01003175
Phillip Lougherbae0e422014-01-19 23:42:11 +00003176 set_progressbar_state(progress);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003177 dir_scan6(inode, dir_info);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003178 dir_ent->inode->inode = *inode;
3179 dir_ent->inode->type = SQUASHFS_DIR_TYPE;
plougher1f413c82005-11-18 00:02:14 +00003180}
3181
3182
Phillip Lougherabc3b492012-07-29 02:53:35 +01003183/*
3184 * dir_scan1 routines...
Phillip Loughere8630be2012-11-11 02:24:24 +00003185 * These scan the source directories into memory for processing.
3186 * Exclude actions are processed here (in contrast to the other actions)
3187 * because they affect what is scanned.
Phillip Lougherabc3b492012-07-29 02:53:35 +01003188 */
Phillip Lougherb38c1722012-02-09 23:26:19 +00003189struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth)
plougher1f413c82005-11-18 00:02:14 +00003190{
plougher1f413c82005-11-18 00:02:14 +00003191 struct dir_info *dir;
3192
plougher6da792b2010-12-16 05:25:39 +00003193 dir = malloc(sizeof(struct dir_info));
3194 if(dir == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003195 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00003196
Phillip Lougher04ef2442013-01-09 06:03:50 +00003197 if(pathname[0] != '\0') {
3198 dir->linuxdir = opendir(pathname);
3199 if(dir->linuxdir == NULL) {
3200 free(dir);
3201 return NULL;
3202 }
plougher1f413c82005-11-18 00:02:14 +00003203 }
Phillip Lougher04ef2442013-01-09 06:03:50 +00003204
Phillip Lougher3cb5d6c2013-01-09 05:50:29 +00003205 dir->pathname = strdup(pathname);
3206 dir->subpath = strdup(subpath);
Phillip Lougherded70fa2011-12-25 02:14:58 +00003207 dir->count = 0;
3208 dir->directory_count = 0;
plougher1f413c82005-11-18 00:02:14 +00003209 dir->dir_is_ldir = TRUE;
3210 dir->list = NULL;
Phillip Lougher0b5a1242011-12-25 03:22:42 +00003211 dir->depth = depth;
Phillip Lougherad0f9212011-12-31 00:55:51 +00003212 dir->excluded = 0;
plougher1f413c82005-11-18 00:02:14 +00003213
3214 return dir;
3215}
3216
3217
Phillip Lougher494479f2012-02-03 15:45:41 +00003218struct dir_ent *scan1_encomp_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003219{
plougher1f413c82005-11-18 00:02:14 +00003220 static int index = 0;
3221
plougher11266ba2010-12-16 05:34:30 +00003222 if(dir->count < old_root_entries) {
3223 int i;
3224
plougher1f413c82005-11-18 00:02:14 +00003225 for(i = 0; i < old_root_entries; i++) {
ploughera326c182009-08-29 05:41:45 +00003226 if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
plougher1f413c82005-11-18 00:02:14 +00003227 dir->directory_count ++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003228 add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
ploughera326c182009-08-29 05:41:45 +00003229 &old_root_entry[i].inode, dir);
plougher1f413c82005-11-18 00:02:14 +00003230 }
plougher11266ba2010-12-16 05:34:30 +00003231 }
plougher1f413c82005-11-18 00:02:14 +00003232
3233 while(index < source) {
Phillip Lougherc73ed322012-10-01 03:25:41 +01003234 char *basename = NULL;
3235 char *dir_name = getbase(source_path[index]);
Phillip Lougherbf338362012-08-22 05:24:36 +01003236 int pass = 1, res;
plougher89fe2c72010-12-16 05:31:34 +00003237
Phillip Lougherc73ed322012-10-01 03:25:41 +01003238 if(dir_name == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003239 ERROR_START("Bad source directory %s",
plougher110799c2009-03-30 01:50:40 +00003240 source_path[index]);
Phillip Lougher85776df2014-01-31 03:10:46 +00003241 ERROR_EXIT(" - skipping ...\n");
plougher1f413c82005-11-18 00:02:14 +00003242 index ++;
3243 continue;
3244 }
Phillip Lougherc73ed322012-10-01 03:25:41 +01003245 dir_name = strdup(dir_name);
plougher1f413c82005-11-18 00:02:14 +00003246 for(;;) {
Phillip Lougherbf338362012-08-22 05:24:36 +01003247 struct dir_ent *dir_ent = dir->list;
3248
3249 for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3250 dir_ent = dir_ent->next);
3251 if(dir_ent == NULL)
plougher1f413c82005-11-18 00:02:14 +00003252 break;
plougher50b31762009-03-31 04:14:46 +00003253 ERROR("Source directory entry %s already used! - trying"
3254 " ", dir_name);
Phillip Lougherc73ed322012-10-01 03:25:41 +01003255 if(pass == 1)
3256 basename = dir_name;
3257 else
Phillip Lougherb38c1722012-02-09 23:26:19 +00003258 free(dir_name);
Phillip Lougher494479f2012-02-03 15:45:41 +00003259 res = asprintf(&dir_name, "%s_%d", basename, pass++);
3260 if(res == -1)
3261 BAD_ERROR("asprintf failed in "
3262 "scan1_encomp_readdir\n");
plougher1f413c82005-11-18 00:02:14 +00003263 ERROR("%s\n", dir_name);
3264 }
Phillip Lougherb38c1722012-02-09 23:26:19 +00003265 return create_dir_entry(dir_name, basename,
3266 source_path[index ++], dir);
plougher1f413c82005-11-18 00:02:14 +00003267 }
Phillip Lougher494479f2012-02-03 15:45:41 +00003268 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003269}
3270
3271
Phillip Lougher494479f2012-02-03 15:45:41 +00003272struct dir_ent *scan1_single_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003273{
3274 struct dirent *d_name;
plougher4925e172010-12-16 05:45:54 +00003275 int i;
plougher1f413c82005-11-18 00:02:14 +00003276
plougher4925e172010-12-16 05:45:54 +00003277 if(dir->count < old_root_entries) {
plougher1f413c82005-11-18 00:02:14 +00003278 for(i = 0; i < old_root_entries; i++) {
ploughera326c182009-08-29 05:41:45 +00003279 if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
plougher1f413c82005-11-18 00:02:14 +00003280 dir->directory_count ++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003281 add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
ploughera326c182009-08-29 05:41:45 +00003282 &old_root_entry[i].inode, dir);
plougher1f413c82005-11-18 00:02:14 +00003283 }
plougher4925e172010-12-16 05:45:54 +00003284 }
plougher1f413c82005-11-18 00:02:14 +00003285
3286 if((d_name = readdir(dir->linuxdir)) != NULL) {
Phillip Lougherc73ed322012-10-01 03:25:41 +01003287 char *basename = NULL;
3288 char *dir_name = strdup(d_name->d_name);
Phillip Lougher494479f2012-02-03 15:45:41 +00003289 int pass = 1, res;
plougher4925e172010-12-16 05:45:54 +00003290
plougher1f413c82005-11-18 00:02:14 +00003291 for(;;) {
Phillip Lougherbf338362012-08-22 05:24:36 +01003292 struct dir_ent *dir_ent = dir->list;
3293
3294 for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3295 dir_ent = dir_ent->next);
3296 if(dir_ent == NULL)
plougher1f413c82005-11-18 00:02:14 +00003297 break;
plougher50b31762009-03-31 04:14:46 +00003298 ERROR("Source directory entry %s already used! - trying"
3299 " ", dir_name);
Phillip Lougherc73ed322012-10-01 03:25:41 +01003300 if (pass == 1)
3301 basename = dir_name;
3302 else
Phillip Lougher494479f2012-02-03 15:45:41 +00003303 free(dir_name);
3304 res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++);
3305 if(res == -1)
3306 BAD_ERROR("asprintf failed in "
3307 "scan1_single_readdir\n");
plougher1f413c82005-11-18 00:02:14 +00003308 ERROR("%s\n", dir_name);
3309 }
Phillip Lougher494479f2012-02-03 15:45:41 +00003310 return create_dir_entry(dir_name, basename, NULL, dir);
plougher1f413c82005-11-18 00:02:14 +00003311 }
3312
Phillip Lougher494479f2012-02-03 15:45:41 +00003313 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003314}
3315
3316
Phillip Lougher494479f2012-02-03 15:45:41 +00003317struct dir_ent *scan1_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003318{
plougher480d9bf2010-12-18 02:28:39 +00003319 struct dirent *d_name = readdir(dir->linuxdir);
plougher1f413c82005-11-18 00:02:14 +00003320
Phillip Lougher494479f2012-02-03 15:45:41 +00003321 return d_name ?
3322 create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) :
3323 NULL;
plougher1f413c82005-11-18 00:02:14 +00003324}
3325
3326
plougher1f413c82005-11-18 00:02:14 +00003327void scan1_freedir(struct dir_info *dir)
3328{
plougherfbed12b2006-02-07 12:45:53 +00003329 if(dir->pathname[0] != '\0')
3330 closedir(dir->linuxdir);
plougher1f413c82005-11-18 00:02:14 +00003331}
3332
3333
Phillip Lougherb38c1722012-02-09 23:26:19 +00003334struct dir_info *dir_scan1(char *filename, char *subpath,
3335 struct pathnames *paths,
Phillip Lougher494479f2012-02-03 15:45:41 +00003336 struct dir_ent *(_readdir)(struct dir_info *), int depth)
plougher1f413c82005-11-18 00:02:14 +00003337{
Phillip Lougherb38c1722012-02-09 23:26:19 +00003338 struct dir_info *dir = scan1_opendir(filename, subpath, depth);
Phillip Lougher494479f2012-02-03 15:45:41 +00003339 struct dir_ent *dir_ent;
plougher1f413c82005-11-18 00:02:14 +00003340
plougher360b6e42010-12-18 02:40:28 +00003341 if(dir == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003342 ERROR_START("Could not open %s", filename);
3343 ERROR_EXIT(", skipping...\n");
Phillip Lougher87db5672013-01-09 05:40:46 +00003344 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003345 }
3346
Phillip Lougher494479f2012-02-03 15:45:41 +00003347 while((dir_ent = _readdir(dir))) {
plougher360b6e42010-12-18 02:40:28 +00003348 struct dir_info *sub_dir;
3349 struct stat buf;
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00003350 struct pathnames *new = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003351 char *filename = pathname(dir_ent);
Phillip Lougher651d68d2013-01-13 05:38:13 +00003352 char *subpath = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003353 char *dir_name = dir_ent->name;
plougher1f413c82005-11-18 00:02:14 +00003354
Phillip Lougher494479f2012-02-03 15:45:41 +00003355 if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) {
Phillip Lougher10a4b572012-02-18 23:26:10 +00003356 free_dir_entry(dir_ent);
plougher1f413c82005-11-18 00:02:14 +00003357 continue;
Phillip Lougher494479f2012-02-03 15:45:41 +00003358 }
plougher1f413c82005-11-18 00:02:14 +00003359
3360 if(lstat(filename, &buf) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003361 ERROR_START("Cannot stat dir/file %s because %s",
plougher110799c2009-03-30 01:50:40 +00003362 filename, strerror(errno));
Phillip Lougher85776df2014-01-31 03:10:46 +00003363 ERROR_EXIT(", ignoring\n");
Phillip Lougher10a4b572012-02-18 23:26:10 +00003364 free_dir_entry(dir_ent);
plougher1f413c82005-11-18 00:02:14 +00003365 continue;
3366 }
plougher29e37092007-04-15 01:24:51 +00003367
3368 if((buf.st_mode & S_IFMT) != S_IFREG &&
Phillip Loughere4ff36a2012-11-19 01:34:47 +00003369 (buf.st_mode & S_IFMT) != S_IFDIR &&
3370 (buf.st_mode & S_IFMT) != S_IFLNK &&
3371 (buf.st_mode & S_IFMT) != S_IFCHR &&
3372 (buf.st_mode & S_IFMT) != S_IFBLK &&
3373 (buf.st_mode & S_IFMT) != S_IFIFO &&
3374 (buf.st_mode & S_IFMT) != S_IFSOCK) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003375 ERROR_START("File %s has unrecognised filetype %d",
3376 filename, buf.st_mode & S_IFMT);
3377 ERROR_EXIT(", ignoring\n");
Phillip Lougher10a4b572012-02-18 23:26:10 +00003378 free_dir_entry(dir_ent);
plougher29e37092007-04-15 01:24:51 +00003379 continue;
3380 }
3381
Phillip Lougherad0f9212011-12-31 00:55:51 +00003382 if((old_exclude && old_excluded(filename, &buf)) ||
Phillip Lougher651d68d2013-01-13 05:38:13 +00003383 (!old_exclude && excluded(dir_name, paths, &new))) {
Phillip Lougherad0f9212011-12-31 00:55:51 +00003384 add_excluded(dir);
Phillip Lougher10a4b572012-02-18 23:26:10 +00003385 free_dir_entry(dir_ent);
Phillip Lougher10d8de02011-08-29 00:14:48 +01003386 continue;
Phillip Lougherad0f9212011-12-31 00:55:51 +00003387 }
plougher1f413c82005-11-18 00:02:14 +00003388
Phillip Lougher651d68d2013-01-13 05:38:13 +00003389 if(exclude_actions()) {
3390 subpath = subpathname(dir_ent);
3391
3392 if(eval_exclude_actions(dir_name, filename, subpath,
3393 &buf, depth)) {
3394 add_excluded(dir);
3395 free_dir_entry(dir_ent);
3396 continue;
3397 }
3398 }
3399
plougher1f413c82005-11-18 00:02:14 +00003400 if((buf.st_mode & S_IFMT) == S_IFDIR) {
Phillip Lougher651d68d2013-01-13 05:38:13 +00003401 if(subpath == NULL)
3402 subpath = subpathname(dir_ent);
3403
Phillip Lougherb38c1722012-02-09 23:26:19 +00003404 sub_dir = dir_scan1(filename, subpath, new,
3405 scan1_readdir, depth + 1);
Phillip Lougher494479f2012-02-03 15:45:41 +00003406 if(sub_dir == NULL) {
Phillip Lougher10a4b572012-02-18 23:26:10 +00003407 free_dir_entry(dir_ent);
Phillip Lougher205c1e02013-01-07 01:37:56 +00003408 free(new);
plougher1f413c82005-11-18 00:02:14 +00003409 continue;
Phillip Lougher494479f2012-02-03 15:45:41 +00003410 }
Phillip Lougher6a78a2d2011-12-28 03:54:19 +00003411
plougher1f413c82005-11-18 00:02:14 +00003412 dir->directory_count ++;
3413 } else
3414 sub_dir = NULL;
3415
Phillip Lougher494479f2012-02-03 15:45:41 +00003416 add_dir_entry(dir_ent, sub_dir, lookup_inode(&buf));
Phillip Lougher205c1e02013-01-07 01:37:56 +00003417 free(new);
plougher1f413c82005-11-18 00:02:14 +00003418 }
3419
3420 scan1_freedir(dir);
plougher1f413c82005-11-18 00:02:14 +00003421
plougher1f413c82005-11-18 00:02:14 +00003422 return dir;
3423}
3424
plougher2ea89142008-03-11 01:34:19 +00003425
Phillip Lougherabc3b492012-07-29 02:53:35 +01003426/*
3427 * dir_scan2 routines...
Phillip Lougher539c2b12012-07-30 20:14:52 +01003428 * This processes most actions and any pseudo files
Phillip Lougherabc3b492012-07-29 02:53:35 +01003429 */
Phillip Lougherbf338362012-08-22 05:24:36 +01003430struct dir_ent *scan2_readdir(struct dir_info *dir, struct dir_ent *dir_ent)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003431{
Phillip Lougherbf338362012-08-22 05:24:36 +01003432 if (dir_ent == NULL)
3433 dir_ent = dir->list;
3434 else
3435 dir_ent = dir_ent->next;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003436
Phillip Lougherbf338362012-08-22 05:24:36 +01003437 for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next);
3438
3439 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003440}
3441
3442
3443struct dir_ent *scan2_lookup(struct dir_info *dir, char *name)
3444{
Phillip Lougherbf338362012-08-22 05:24:36 +01003445 struct dir_ent *dir_ent = dir->list;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003446
Phillip Lougherbf338362012-08-22 05:24:36 +01003447 for(; dir_ent && strcmp(dir_ent->name, name) != 0;
3448 dir_ent = dir_ent->next);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003449
Phillip Lougherbf338362012-08-22 05:24:36 +01003450 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003451}
3452
3453
Phillip Lougher24eeb772012-10-13 01:56:24 +01003454void dir_scan2(struct dir_info *dir, struct pseudo *pseudo)
plougher43244f22009-04-05 02:04:51 +00003455{
Phillip Lougherbf338362012-08-22 05:24:36 +01003456 struct dir_ent *dir_ent = NULL;
plougher43244f22009-04-05 02:04:51 +00003457 struct pseudo_entry *pseudo_ent;
3458 struct stat buf;
plougher82ab2332009-04-21 00:21:21 +00003459 static int pseudo_ino = 1;
plougher43244f22009-04-05 02:04:51 +00003460
Phillip Lougherbf338362012-08-22 05:24:36 +01003461 while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
plougher43244f22009-04-05 02:04:51 +00003462 struct inode_info *inode_info = dir_ent->inode;
3463 struct stat *buf = &inode_info->buf;
3464 char *name = dir_ent->name;
3465
Phillip Lougher89d757c2011-09-20 00:33:19 +01003466 eval_actions(dir_ent);
3467
plougher43244f22009-04-05 02:04:51 +00003468 if((buf->st_mode & S_IFMT) == S_IFDIR)
Phillip Lougher24eeb772012-10-13 01:56:24 +01003469 dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo));
plougher43244f22009-04-05 02:04:51 +00003470 }
3471
3472 while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) {
3473 dir_ent = scan2_lookup(dir, pseudo_ent->name);
plougherdcd66c52010-09-17 03:44:17 +00003474 if(pseudo_ent->dev->type == 'm') {
plougherb34f9f62009-04-26 02:08:42 +00003475 struct stat *buf;
3476 if(dir_ent == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003477 ERROR_START("Pseudo modify file \"%s\" does "
3478 "not exist in source filesystem.",
plougherb34f9f62009-04-26 02:08:42 +00003479 pseudo_ent->pathname);
Phillip Lougher85776df2014-01-31 03:10:46 +00003480 ERROR_EXIT(" Ignoring.\n");
plougherb34f9f62009-04-26 02:08:42 +00003481 continue;
3482 }
ploughera326c182009-08-29 05:41:45 +00003483 if(dir_ent->inode->root_entry) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003484 ERROR_START("Pseudo modify file \"%s\" is a "
3485 "pre-existing file in the filesystem "
3486 "being appended to. It cannot be "\
3487 "modified.", pseudo_ent->pathname);
3488 ERROR_EXIT(" Ignoring.\n");
plougherb34f9f62009-04-26 02:08:42 +00003489 continue;
3490 }
3491 buf = &dir_ent->inode->buf;
3492 buf->st_mode = (buf->st_mode & S_IFMT) |
3493 pseudo_ent->dev->mode;
3494 buf->st_uid = pseudo_ent->dev->uid;
3495 buf->st_gid = pseudo_ent->dev->gid;
3496 continue;
3497 }
3498
plougher43244f22009-04-05 02:04:51 +00003499 if(dir_ent) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003500 if(dir_ent->inode->root_entry) {
3501 ERROR_START("Pseudo file \"%s\" is a "
3502 "pre-existing file in the filesystem "
3503 "being appended to.",
plougherf0dc2382010-05-01 23:55:06 +00003504 pseudo_ent->pathname);
Phillip Lougher85776df2014-01-31 03:10:46 +00003505 ERROR_EXIT(" Ignoring.\n");
3506 } else {
3507 ERROR_START("Pseudo file \"%s\" exists in "
3508 "source filesystem \"%s\".",
plougherf0dc2382010-05-01 23:55:06 +00003509 pseudo_ent->pathname,
Phillip Lougher494479f2012-02-03 15:45:41 +00003510 pathname(dir_ent));
Phillip Lougher85776df2014-01-31 03:10:46 +00003511 ERROR_EXIT("\nIgnoring, exclude it (-e/-ef) to "
3512 "override.\n");
3513 }
plougher43244f22009-04-05 02:04:51 +00003514 continue;
3515 }
3516
plougher43244f22009-04-05 02:04:51 +00003517 memset(&buf, 0, sizeof(buf));
3518 buf.st_mode = pseudo_ent->dev->mode;
3519 buf.st_uid = pseudo_ent->dev->uid;
3520 buf.st_gid = pseudo_ent->dev->gid;
3521 buf.st_rdev = makedev(pseudo_ent->dev->major,
3522 pseudo_ent->dev->minor);
plougher7e58f4d2009-04-05 12:06:19 +00003523 buf.st_mtime = time(NULL);
plougher1a3fbf22009-04-05 12:04:16 +00003524 buf.st_ino = pseudo_ino ++;
plougher43244f22009-04-05 02:04:51 +00003525
Phillip Lougher5d579292012-10-13 01:37:16 +01003526 if(pseudo_ent->dev->type == 'd') {
3527 struct dir_ent *dir_ent =
3528 create_dir_entry(pseudo_ent->name, NULL,
3529 pseudo_ent->pathname, dir);
3530 char *subpath = strdup(subpathname(dir_ent));
3531 struct dir_info *sub_dir = scan1_opendir("", subpath,
Phillip Lougher24eeb772012-10-13 01:56:24 +01003532 dir->depth + 1);
Phillip Lougher5d579292012-10-13 01:37:16 +01003533 if(sub_dir == NULL) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003534 ERROR_START("Could not create pseudo directory "
3535 "\"%s\"", pseudo_ent->pathname);
3536 ERROR_EXIT(", skipping...\n");
Phillip Lougher5d579292012-10-13 01:37:16 +01003537 free(subpath);
3538 pseudo_ino --;
3539 continue;
3540 }
Phillip Lougher24eeb772012-10-13 01:56:24 +01003541 dir_scan2(sub_dir, pseudo_ent->pseudo);
Phillip Lougher5d579292012-10-13 01:37:16 +01003542 dir->directory_count ++;
3543 add_dir_entry(dir_ent, sub_dir,
3544 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0));
3545 } else if(pseudo_ent->dev->type == 'f') {
Phillip Lougher494479f2012-02-03 15:45:41 +00003546 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003547 pseudo_ent->pathname, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003548 lookup_inode2(&buf, PSEUDO_FILE_PROCESS,
3549 pseudo_ent->dev->pseudo_id), dir);
plougherb85e9ad2010-05-02 01:46:12 +00003550 } else {
Phillip Lougher494479f2012-02-03 15:45:41 +00003551 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003552 pseudo_ent->pathname, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003553 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir);
plougherb85e9ad2010-05-02 01:46:12 +00003554 }
plougher43244f22009-04-05 02:04:51 +00003555 }
plougher43244f22009-04-05 02:04:51 +00003556}
3557
3558
Phillip Lougherabc3b492012-07-29 02:53:35 +01003559/*
3560 * dir_scan3 routines...
Phillip Lougher23d83622012-10-14 02:35:22 +01003561 * This processes the move action
3562 */
3563void dir_scan3(struct dir_info *root, struct dir_info *dir)
3564{
3565 struct dir_ent *dir_ent = NULL;
3566
3567 while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
3568
3569 eval_move_actions(root, dir_ent);
3570
3571 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
3572 dir_scan3(root, dir_ent->dir);
3573 }
3574}
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003575
3576
Phillip Lougher23d83622012-10-14 02:35:22 +01003577/*
3578 * dir_scan4 routines...
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003579 * This processes the empty action. This action has to be processed after
3580 * all other actions because the previous exclude and move actions and the
3581 * pseudo actions affect whether a directory is empty
3582 */
3583void dir_scan4(struct dir_info *dir)
3584{
3585 struct dir_ent *dir_ent = dir->list, *prev = NULL;
3586
3587 while(dir_ent) {
Phillip Lougher15e8f042012-11-16 00:57:39 +00003588 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) {
3589 dir_scan4(dir_ent->dir);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003590
Phillip Lougher15e8f042012-11-16 00:57:39 +00003591 if(eval_empty_actions(dir_ent)) {
3592 struct dir_ent *tmp = dir_ent;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003593
Phillip Lougher15e8f042012-11-16 00:57:39 +00003594 /*
3595 * delete sub-directory, this is by definition
3596 * empty
3597 */
3598 free(dir_ent->dir->pathname);
3599 free(dir_ent->dir->subpath);
3600 free(dir_ent->dir);
3601
3602 /* remove dir_ent from list */
3603 dir_ent = dir_ent->next;
3604 if(prev)
3605 prev->next = dir_ent;
3606 else
3607 dir->list = dir_ent;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003608
Phillip Lougher15e8f042012-11-16 00:57:39 +00003609 /* free it */
3610 free_dir_entry(tmp);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003611
Phillip Lougher15e8f042012-11-16 00:57:39 +00003612 /* update counts */
3613 dir->directory_count --;
3614 dir->count --;
3615 add_excluded(dir);
3616 continue;
3617 }
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003618 }
Phillip Lougher15e8f042012-11-16 00:57:39 +00003619
3620 prev = dir_ent;
3621 dir_ent = dir_ent->next;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003622 }
3623}
3624
3625
3626/*
3627 * dir_scan5 routines...
Phillip Lougher539c2b12012-07-30 20:14:52 +01003628 * This sorts every directory and computes the inode numbers
3629 */
Phillip Lougher539c2b12012-07-30 20:14:52 +01003630
Phillip Lougher242242e2012-08-24 04:15:36 +01003631/*
3632 * Bottom up linked list merge sort.
3633 *
3634 * Qsort and other O(n log n) algorithms work well with arrays but not
3635 * linked lists. Merge sort another O(n log n) sort algorithm on the other hand
3636 * is not ideal for arrays (as it needs an additonal n storage locations
3637 * as sorting is not done in place), but it is ideal for linked lists because
3638 * it doesn't require any extra storage,
3639 */
Phillip Lougher539c2b12012-07-30 20:14:52 +01003640void sort_directory(struct dir_info *dir)
3641{
Phillip Lougher242242e2012-08-24 04:15:36 +01003642 struct dir_ent *cur, *l1, *l2, *next;
3643 int len1, len2, stride = 1;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003644
Phillip Lougherd3459a82014-04-18 01:37:54 +01003645 if(dir->list == NULL || dir->count < 2)
Phillip Lougherbf338362012-08-22 05:24:36 +01003646 return;
3647
Phillip Lougher242242e2012-08-24 04:15:36 +01003648 /*
3649 * We can consider our linked-list to be made up of stride length
3650 * sublists. Eacn iteration around this loop merges adjacent
3651 * stride length sublists into larger 2*stride sublists. We stop
3652 * when stride becomes equal to the entire list.
3653 *
3654 * Initially stride = 1 (by definition a sublist of 1 is sorted), and
3655 * these 1 element sublists are merged into 2 element sublists, which
3656 * are then merged into 4 element sublists and so on.
3657 */
3658 do {
3659 l2 = dir->list; /* head of current linked list */
3660 cur = NULL; /* empty output list */
Phillip Lougherbf338362012-08-22 05:24:36 +01003661
Phillip Lougher242242e2012-08-24 04:15:36 +01003662 /*
3663 * Iterate through the linked list, merging adjacent sublists.
3664 * On each interation l2 points to the next sublist pair to be
3665 * merged (if there's only one sublist left this is simply added
3666 * to the output list)
3667 */
3668 while(l2) {
3669 l1 = l2;
3670 for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next);
3671 len2 = stride;
Phillip Lougherbf338362012-08-22 05:24:36 +01003672
Phillip Lougher242242e2012-08-24 04:15:36 +01003673 /*
3674 * l1 points to first sublist.
3675 * l2 points to second sublist.
3676 * Merge them onto the output list
3677 */
3678 while(len1 && l2 && len2) {
3679 if(strcmp(l1->name, l2->name) <= 0) {
3680 next = l1;
3681 l1 = l1->next;
3682 len1 --;
3683 } else {
3684 next = l2;
3685 l2 = l2->next;
3686 len2 --;
3687 }
3688
3689 if(cur) {
3690 cur->next = next;
3691 cur = next;
3692 } else
3693 dir->list = cur = next;
3694 }
3695 /*
3696 * One sublist is now empty, copy the other one onto the
3697 * output list
3698 */
3699 for(; len1; len1 --, l1 = l1->next) {
3700 if(cur) {
3701 cur->next = l1;
3702 cur = l1;
3703 } else
3704 dir->list = cur = l1;
3705 }
3706 for(; l2 && len2; len2 --, l2 = l2->next) {
3707 if(cur) {
3708 cur->next = l2;
3709 cur = l2;
3710 } else
3711 dir->list = cur = l2;
3712 }
3713 }
3714 cur->next = NULL;
3715 stride = stride << 1;
3716 } while(stride < dir->count);
Phillip Lougher539c2b12012-07-30 20:14:52 +01003717}
3718
3719
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003720void dir_scan5(struct dir_info *dir)
Phillip Lougher539c2b12012-07-30 20:14:52 +01003721{
Phillip Lougher23d83622012-10-14 02:35:22 +01003722 struct dir_ent *dir_ent;
3723 unsigned int byte_count = 0;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003724
3725 sort_directory(dir);
3726
Phillip Lougher23d83622012-10-14 02:35:22 +01003727 for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) {
3728 byte_count += strlen(dir_ent->name) +
3729 sizeof(struct squashfs_dir_entry);
3730
3731 if(dir_ent->inode->root_entry)
3732 continue;
3733
Phillip Lougher539c2b12012-07-30 20:14:52 +01003734 alloc_inode_no(dir_ent->inode, 0);
Phillip Lougher23d83622012-10-14 02:35:22 +01003735
Phillip Lougher539c2b12012-07-30 20:14:52 +01003736 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
Phillip Loughere69b5882012-11-19 01:31:09 +00003737 dir_scan5(dir_ent->dir);
Phillip Lougher539c2b12012-07-30 20:14:52 +01003738 }
Phillip Lougher23d83622012-10-14 02:35:22 +01003739
3740 if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE))
3741 dir->dir_is_ldir = FALSE;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003742}
3743
3744
3745/*
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003746 * dir_scan6 routines...
Phillip Lougherabc3b492012-07-29 02:53:35 +01003747 * This generates the filesystem metadata and writes it out to the destination
3748 */
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003749void scan6_init_dir(struct directory *dir)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003750{
3751 dir->buff = malloc(SQUASHFS_METADATA_SIZE);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003752 if(dir->buff == NULL)
3753 MEM_ERROR();
Phillip Lougherabc3b492012-07-29 02:53:35 +01003754
3755 dir->size = SQUASHFS_METADATA_SIZE;
3756 dir->p = dir->index_count_p = dir->buff;
3757 dir->entry_count = 256;
3758 dir->entry_count_p = NULL;
3759 dir->index = NULL;
3760 dir->i_count = dir->i_size = 0;
3761}
3762
3763
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003764struct dir_ent *scan6_readdir(struct directory *dir, struct dir_info *dir_info,
Phillip Lougherbf338362012-08-22 05:24:36 +01003765 struct dir_ent *dir_ent)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003766{
Phillip Lougherbf338362012-08-22 05:24:36 +01003767 if (dir_ent == NULL)
3768 dir_ent = dir_info->list;
3769 else
3770 dir_ent = dir_ent->next;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003771
Phillip Lougherbf338362012-08-22 05:24:36 +01003772 for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next)
3773 add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number,
3774 dir_ent->name, dir_ent->inode->type, dir);
3775
3776 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003777}
3778
3779
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003780void scan6_freedir(struct directory *dir)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003781{
3782 if(dir->index)
3783 free(dir->index);
3784 free(dir->buff);
3785}
3786
3787
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003788void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info)
plougher1f413c82005-11-18 00:02:14 +00003789{
3790 int squashfs_type;
plougher1f413c82005-11-18 00:02:14 +00003791 int duplicate_file;
plougher1f413c82005-11-18 00:02:14 +00003792 struct directory dir;
Phillip Lougherbf338362012-08-22 05:24:36 +01003793 struct dir_ent *dir_ent = NULL;
plougher1f413c82005-11-18 00:02:14 +00003794
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003795 scan6_init_dir(&dir);
plougher1f413c82005-11-18 00:02:14 +00003796
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003797 while((dir_ent = scan6_readdir(&dir, dir_info, dir_ent)) != NULL) {
Phillip Lougher0a670882012-10-05 04:09:13 +01003798 struct stat *buf = &dir_ent->inode->buf;
plougher1f413c82005-11-18 00:02:14 +00003799
Phillip Lougher24551a82013-03-24 02:30:50 +00003800 update_info(dir_ent);
3801
plougher1f413c82005-11-18 00:02:14 +00003802 if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) {
3803 switch(buf->st_mode & S_IFMT) {
3804 case S_IFREG:
3805 squashfs_type = SQUASHFS_FILE_TYPE;
plougher110799c2009-03-30 01:50:40 +00003806 write_file(inode, dir_ent,
3807 &duplicate_file);
3808 INFO("file %s, uncompressed size %lld "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003809 "bytes %s\n",
3810 subpathname(dir_ent),
plougher82ab2332009-04-21 00:21:21 +00003811 (long long) buf->st_size,
3812 duplicate_file ? "DUPLICATE" :
3813 "");
plougher1f413c82005-11-18 00:02:14 +00003814 break;
3815
3816 case S_IFDIR:
3817 squashfs_type = SQUASHFS_DIR_TYPE;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003818 dir_scan6(inode, dir_ent->dir);
plougher1f413c82005-11-18 00:02:14 +00003819 break;
3820
3821 case S_IFLNK:
3822 squashfs_type = SQUASHFS_SYMLINK_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003823 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003824 squashfs_type, 0, 0, 0, NULL,
3825 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00003826 INFO("symbolic link %s inode 0x%llx\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003827 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003828 sym_count ++;
3829 break;
3830
3831 case S_IFCHR:
3832 squashfs_type = SQUASHFS_CHRDEV_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003833 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003834 squashfs_type, 0, 0, 0, NULL,
3835 NULL, NULL, 0);
3836 INFO("character device %s inode 0x%llx"
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003837 "\n", subpathname(dir_ent),
3838 *inode);
plougher1f413c82005-11-18 00:02:14 +00003839 dev_count ++;
3840 break;
3841
3842 case S_IFBLK:
3843 squashfs_type = SQUASHFS_BLKDEV_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003844 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003845 squashfs_type, 0, 0, 0, NULL,
3846 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00003847 INFO("block device %s inode 0x%llx\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003848 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003849 dev_count ++;
3850 break;
3851
3852 case S_IFIFO:
3853 squashfs_type = SQUASHFS_FIFO_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003854 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003855 squashfs_type, 0, 0, 0, NULL,
3856 NULL, NULL, 0);
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003857 INFO("fifo %s inode 0x%llx\n",
3858 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003859 fifo_count ++;
3860 break;
3861
3862 case S_IFSOCK:
3863 squashfs_type = SQUASHFS_SOCKET_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003864 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003865 squashfs_type, 0, 0, 0, NULL,
3866 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00003867 INFO("unix domain socket %s inode "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003868 "0x%llx\n",
3869 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003870 sock_count ++;
3871 break;
3872
plougher23377982007-11-12 04:04:48 +00003873 default:
plougherb3604122009-03-30 02:07:20 +00003874 BAD_ERROR("%s unrecognised file type, "
Phillip Lougher494479f2012-02-03 15:45:41 +00003875 "mode is %x\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003876 subpathname(dir_ent),
plougherb3604122009-03-30 02:07:20 +00003877 buf->st_mode);
plougher29e37092007-04-15 01:24:51 +00003878 }
3879 dir_ent->inode->inode = *inode;
plougher1f413c82005-11-18 00:02:14 +00003880 dir_ent->inode->type = squashfs_type;
3881 } else {
3882 *inode = dir_ent->inode->inode;
3883 squashfs_type = dir_ent->inode->type;
plougher04b0d5f2006-02-10 00:42:06 +00003884 switch(squashfs_type) {
3885 case SQUASHFS_FILE_TYPE:
3886 if(!sorted)
plougher50b31762009-03-31 04:14:46 +00003887 INFO("file %s, uncompressed "
3888 "size %lld bytes LINK"
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003889 "\n",
3890 subpathname(dir_ent),
plougher82ab2332009-04-21 00:21:21 +00003891 (long long)
plougher50b31762009-03-31 04:14:46 +00003892 buf->st_size);
plougher04b0d5f2006-02-10 00:42:06 +00003893 break;
3894 case SQUASHFS_SYMLINK_TYPE:
plougherb3604122009-03-30 02:07:20 +00003895 INFO("symbolic link %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003896 "LINK\n", subpathname(dir_ent),
3897 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003898 break;
3899 case SQUASHFS_CHRDEV_TYPE:
plougherb3604122009-03-30 02:07:20 +00003900 INFO("character device %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003901 "LINK\n", subpathname(dir_ent),
3902 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003903 break;
plougher5507dd92006-11-06 00:43:10 +00003904 case SQUASHFS_BLKDEV_TYPE:
plougherb3604122009-03-30 02:07:20 +00003905 INFO("block device %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003906 "LINK\n", subpathname(dir_ent),
3907 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003908 break;
3909 case SQUASHFS_FIFO_TYPE:
plougherb3604122009-03-30 02:07:20 +00003910 INFO("fifo %s inode 0x%llx LINK\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003911 subpathname(dir_ent), *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003912 break;
3913 case SQUASHFS_SOCKET_TYPE:
plougher50b31762009-03-31 04:14:46 +00003914 INFO("unix domain socket %s inode "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003915 "0x%llx LINK\n",
3916 subpathname(dir_ent), *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003917 break;
3918 }
plougher1f413c82005-11-18 00:02:14 +00003919 }
3920
Phillip Lougher0366aec2012-10-05 04:06:04 +01003921 add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name,
3922 squashfs_type, &dir);
plougher1f413c82005-11-18 00:02:14 +00003923 }
3924
plougher29e37092007-04-15 01:24:51 +00003925 write_dir(inode, dir_info, &dir);
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003926 INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent),
3927 *inode);
plougher1f413c82005-11-18 00:02:14 +00003928
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003929 scan6_freedir(&dir);
plougher1f413c82005-11-18 00:02:14 +00003930}
3931
3932
3933unsigned int slog(unsigned int block)
3934{
3935 int i;
3936
plougher4c99cb72007-06-14 21:46:31 +00003937 for(i = 12; i <= 20; i++)
plougher1f413c82005-11-18 00:02:14 +00003938 if(block == (1 << i))
3939 return i;
3940 return 0;
3941}
3942
3943
plougher8f8e1a12007-10-18 02:50:21 +00003944int old_excluded(char *filename, struct stat *buf)
plougher1f413c82005-11-18 00:02:14 +00003945{
3946 int i;
3947
3948 for(i = 0; i < exclude; i++)
plougherb3604122009-03-30 02:07:20 +00003949 if((exclude_paths[i].st_dev == buf->st_dev) &&
3950 (exclude_paths[i].st_ino == buf->st_ino))
plougher1f413c82005-11-18 00:02:14 +00003951 return TRUE;
3952 return FALSE;
3953}
3954
3955
3956#define ADD_ENTRY(buf) \
plougher360514a2009-03-30 03:01:38 +00003957 if(exclude % EXCLUDE_SIZE == 0) { \
3958 exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \
3959 * sizeof(struct exclude_info)); \
3960 if(exclude_paths == NULL) \
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003961 MEM_ERROR(); \
plougher360514a2009-03-30 03:01:38 +00003962 } \
3963 exclude_paths[exclude].st_dev = buf.st_dev; \
plougher1f413c82005-11-18 00:02:14 +00003964 exclude_paths[exclude++].st_ino = buf.st_ino;
plougher8f8e1a12007-10-18 02:50:21 +00003965int old_add_exclude(char *path)
plougher1f413c82005-11-18 00:02:14 +00003966{
3967 int i;
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003968 char *filename;
plougher1f413c82005-11-18 00:02:14 +00003969 struct stat buf;
3970
plougherb3604122009-03-30 02:07:20 +00003971 if(path[0] == '/' || strncmp(path, "./", 2) == 0 ||
3972 strncmp(path, "../", 3) == 0) {
plougher1f413c82005-11-18 00:02:14 +00003973 if(lstat(path, &buf) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003974 ERROR_START("Cannot stat exclude dir/file %s because "
3975 "%s", path, strerror(errno));
3976 ERROR_EXIT(", ignoring\n");
plougher1f413c82005-11-18 00:02:14 +00003977 return TRUE;
3978 }
3979 ADD_ENTRY(buf);
3980 return TRUE;
3981 }
3982
3983 for(i = 0; i < source; i++) {
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003984 int res = asprintf(&filename, "%s/%s", source_path[i], path);
3985 if(res == -1)
3986 BAD_ERROR("asprintf failed in old_add_exclude\n");
plougher1f413c82005-11-18 00:02:14 +00003987 if(lstat(filename, &buf) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00003988 if(!(errno == ENOENT || errno == ENOTDIR)) {
3989 ERROR_START("Cannot stat exclude dir/file %s "
3990 "because %s", filename, strerror(errno));
3991 ERROR_EXIT(", ignoring\n");
3992 }
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003993 free(filename);
plougher1f413c82005-11-18 00:02:14 +00003994 continue;
3995 }
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003996 free(filename);
plougher1f413c82005-11-18 00:02:14 +00003997 ADD_ENTRY(buf);
3998 }
3999 return TRUE;
4000}
4001
4002
plougherb3604122009-03-30 02:07:20 +00004003void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
4004 int type)
plougher1f413c82005-11-18 00:02:14 +00004005{
plougherb3604122009-03-30 02:07:20 +00004006 old_root_entry = realloc(old_root_entry,
4007 sizeof(struct old_root_entry_info) * (old_root_entries + 1));
4008 if(old_root_entry == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004009 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00004010
ploughera326c182009-08-29 05:41:45 +00004011 old_root_entry[old_root_entries].name = strdup(name);
4012 old_root_entry[old_root_entries].inode.inode = inode;
4013 old_root_entry[old_root_entries].inode.inode_number = inode_number;
4014 old_root_entry[old_root_entries].inode.type = type;
4015 old_root_entry[old_root_entries++].inode.root_entry = TRUE;
plougher1f413c82005-11-18 00:02:14 +00004016}
4017
4018
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004019void initialise_threads(int readq, int fragq, int bwriteq, int fwriteq,
4020 int freelst, char *destination_file)
plougher5507dd92006-11-06 00:43:10 +00004021{
4022 int i;
4023 sigset_t sigmask, old_mask;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004024 int total_mem = readq;
Phillip Loughercf4c7bc2014-04-13 04:48:11 +01004025 int reader_size;
4026 int fragment_size;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004027 int fwriter_size;
plougher5741d792010-09-04 03:20:50 +00004028 /*
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004029 * bwriter_size is global because it is needed in
plougher5741d792010-09-04 03:20:50 +00004030 * write_file_blocks_dup()
4031 */
Phillip Loughere1668fe2012-12-30 05:48:12 +00004032
4033 /*
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004034 * Never allow the total size of the queues to be larger than
4035 * physical memory
4036 *
4037 * When adding together the possibly user supplied values, make
4038 * sure they've not been deliberately contrived to overflow an int
4039 */
4040 if(add_overflow(total_mem, fragq))
4041 BAD_ERROR("Queue sizes rediculously too large\n");
4042 total_mem += fragq;
4043 if(add_overflow(total_mem, bwriteq))
4044 BAD_ERROR("Queue sizes rediculously too large\n");
4045 total_mem += bwriteq;
4046 if(add_overflow(total_mem, fwriteq))
4047 BAD_ERROR("Queue sizes rediculously too large\n");
4048 total_mem += fwriteq;
4049
Phillip Lougher78784bb2014-04-28 04:08:40 +01004050 if(total_mem > get_physical_memory()) {
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004051 ERROR("Total queue sizes larger than physical memory.\n");
4052 ERROR("Mksquashfs will exhaust physical memory and thrash.\n");
4053 BAD_ERROR("Queues too large\n");
4054 }
4055
4056 /*
Phillip Loughere1668fe2012-12-30 05:48:12 +00004057 * convert from queue size in Mbytes to queue size in
4058 * blocks.
4059 *
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004060 * This isn't going to overflow an int unless there exists
4061 * systems with more than 8 Petabytes of RAM!
Phillip Loughere1668fe2012-12-30 05:48:12 +00004062 */
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004063 reader_size = readq << (20 - block_log);
4064 fragment_size = fragq << (20 - block_log);
4065 bwriter_size = bwriteq << (20 - block_log);
4066 fwriter_size = fwriteq << (20 - block_log);
plougher5507dd92006-11-06 00:43:10 +00004067
Phillip Lougher9391cb12013-03-20 05:16:10 +00004068 /*
4069 * setup signal handlers for the main thread, these cleanup
4070 * deleting the destination file, if appending the
4071 * handlers for SIGTERM and SIGINT will be replaced with handlers
4072 * allowing the user to press ^C twice to restore the existing
4073 * filesystem.
4074 *
Phillip Lougherdd343392013-03-31 23:29:03 +01004075 * SIGUSR1 is an internal signal, which is used by the sub-threads
Phillip Lougher9391cb12013-03-20 05:16:10 +00004076 * to tell the main thread to terminate, deleting the destination file,
4077 * or if necessary restoring the filesystem on appending
4078 */
Phillip Lougherb8ec5202013-03-28 20:14:37 +00004079 signal(SIGTERM, sighandler);
4080 signal(SIGINT, sighandler);
Phillip Lougherdd343392013-03-31 23:29:03 +01004081 signal(SIGUSR1, sighandler);
Phillip Lougher9391cb12013-03-20 05:16:10 +00004082
Phillip Lougher7538d742013-04-22 05:33:27 +01004083 /* block SIGQUIT and SIGHUP, these are handled by the info thread */
Phillip Lougher24551a82013-03-24 02:30:50 +00004084 sigemptyset(&sigmask);
4085 sigaddset(&sigmask, SIGQUIT);
Phillip Lougher7538d742013-04-22 05:33:27 +01004086 sigaddset(&sigmask, SIGHUP);
Phillip Lougherb1259f72013-06-06 03:10:15 +01004087 if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
Phillip Lougher24551a82013-03-24 02:30:50 +00004088 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4089
Phillip Lougher9391cb12013-03-20 05:16:10 +00004090 /*
4091 * temporarily block these signals, so the created sub-threads
4092 * will ignore them, ensuring the main thread handles them
4093 */
plougher5507dd92006-11-06 00:43:10 +00004094 sigemptyset(&sigmask);
4095 sigaddset(&sigmask, SIGINT);
Phillip Lougher9391cb12013-03-20 05:16:10 +00004096 sigaddset(&sigmask, SIGTERM);
Phillip Lougherdd343392013-03-31 23:29:03 +01004097 sigaddset(&sigmask, SIGUSR1);
Phillip Lougher8f8d2752013-03-26 04:38:38 +00004098 if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1)
plougher5507dd92006-11-06 00:43:10 +00004099 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4100
plougher5507dd92006-11-06 00:43:10 +00004101 if(processors == -1) {
4102#ifndef linux
4103 int mib[2];
4104 size_t len = sizeof(processors);
4105
4106 mib[0] = CTL_HW;
4107#ifdef HW_AVAILCPU
4108 mib[1] = HW_AVAILCPU;
4109#else
4110 mib[1] = HW_NCPU;
4111#endif
4112
4113 if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) {
Phillip Lougher85776df2014-01-31 03:10:46 +00004114 ERROR_START("Failed to get number of available "
4115 "processors.");
4116 ERROR_EXIT(" Defaulting to 1\n");
plougher5507dd92006-11-06 00:43:10 +00004117 processors = 1;
4118 }
4119#else
plougher9cc26b72010-08-17 01:05:55 +00004120 processors = sysconf(_SC_NPROCESSORS_ONLN);
plougher5507dd92006-11-06 00:43:10 +00004121#endif
4122 }
4123
Phillip Lougher9de84ac2014-02-20 05:18:48 +00004124 if(multiply_overflow(processors, 3) ||
4125 multiply_overflow(processors * 3, sizeof(pthread_t)))
Phillip Loughere1668fe2012-12-30 05:48:12 +00004126 BAD_ERROR("Processors too large\n");
4127
Phillip Lougher9de84ac2014-02-20 05:18:48 +00004128 deflator_thread = malloc(processors * 3 * sizeof(pthread_t));
Phillip Lougher0280d992014-01-27 05:54:07 +00004129 if(deflator_thread == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004130 MEM_ERROR();
Phillip Loughere1668fe2012-12-30 05:48:12 +00004131
plougher5507dd92006-11-06 00:43:10 +00004132 frag_deflator_thread = &deflator_thread[processors];
Phillip Lougher9de84ac2014-02-20 05:18:48 +00004133 frag_thread = &frag_deflator_thread[processors];
plougher5507dd92006-11-06 00:43:10 +00004134
4135 to_reader = queue_init(1);
Phillip Loughercf4c7bc2014-04-13 04:48:11 +01004136 to_deflate = queue_init(reader_size);
4137 to_process_frag = queue_init(reader_size);
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004138 to_writer = queue_init(bwriter_size + fwriter_size);
plougher5507dd92006-11-06 00:43:10 +00004139 from_writer = queue_init(1);
Phillip Loughercf4c7bc2014-04-13 04:48:11 +01004140 to_frag = queue_init(fragment_size);
4141 locked_fragment = queue_init(fragment_size);
Phillip Lougher0e1656d2013-05-20 03:47:24 +01004142 to_main = seq_queue_init();
Phillip Loughercf4c7bc2014-04-13 04:48:11 +01004143 reader_buffer = cache_init(block_size, reader_size, 0, 0);
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004144 bwriter_buffer = cache_init(block_size, bwriter_size, 1, freelst);
4145 fwriter_buffer = cache_init(block_size, fwriter_size, 1, freelst);
Phillip Loughercf4c7bc2014-04-13 04:48:11 +01004146 fragment_buffer = cache_init(block_size, fragment_size, 1, freelst);
Phillip Lougher8bb17b02014-03-30 23:59:55 +01004147 reserve_cache = cache_init(block_size, processors + 1, 1, freelst);
Phillip Lougher0280d992014-01-27 05:54:07 +00004148 pthread_create(&reader_thread, NULL, reader, NULL);
4149 pthread_create(&writer_thread, NULL, writer, NULL);
Phillip Lougher3b89ee82012-10-18 23:55:37 +01004150 init_progress_bar();
Phillip Lougher24551a82013-03-24 02:30:50 +00004151 init_info();
plougher5507dd92006-11-06 00:43:10 +00004152
4153 for(i = 0; i < processors; i++) {
Phillip Lougher943acad2014-04-17 03:31:01 +01004154 if(pthread_create(&deflator_thread[i], NULL, deflator, NULL))
plougher5507dd92006-11-06 00:43:10 +00004155 BAD_ERROR("Failed to create thread\n");
plougher360514a2009-03-30 03:01:38 +00004156 if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator,
4157 NULL) != 0)
plougher5507dd92006-11-06 00:43:10 +00004158 BAD_ERROR("Failed to create thread\n");
Phillip Lougher8bb17b02014-03-30 23:59:55 +01004159 if(pthread_create(&frag_thread[i], NULL, frag_thrd,
4160 (void *) destination_file) != 0)
Phillip Lougher9de84ac2014-02-20 05:18:48 +00004161 BAD_ERROR("Failed to create thread\n");
plougher5507dd92006-11-06 00:43:10 +00004162 }
4163
Phillip Lougher0280d992014-01-27 05:54:07 +00004164 main_thread = pthread_self();
4165
plougher5507dd92006-11-06 00:43:10 +00004166 printf("Parallel mksquashfs: Using %d processor%s\n", processors,
4167 processors == 1 ? "" : "s");
4168
Phillip Lougher9391cb12013-03-20 05:16:10 +00004169 /* Restore the signal mask for the main thread */
Phillip Lougher8f8d2752013-03-26 04:38:38 +00004170 if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1)
plougher5507dd92006-11-06 00:43:10 +00004171 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4172}
4173
4174
plougher0e453652006-11-06 01:49:35 +00004175long long write_inode_lookup_table()
4176{
4177 int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count);
plougher44f03282010-07-27 00:31:36 +00004178 void *it;
plougher02bc3bc2007-02-25 12:12:01 +00004179
4180 if(inode_count == sinode_count)
4181 goto skip_inode_hash_table;
plougher0e453652006-11-06 01:49:35 +00004182
plougher44f03282010-07-27 00:31:36 +00004183 it = realloc(inode_lookup_table, lookup_bytes);
4184 if(it == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004185 MEM_ERROR();
plougher44f03282010-07-27 00:31:36 +00004186 inode_lookup_table = it;
plougher0e453652006-11-06 01:49:35 +00004187
plougher0e453652006-11-06 01:49:35 +00004188 for(i = 0; i < INODE_HASH_SIZE; i ++) {
Phillip Lougherf2f83b62014-04-18 01:30:47 +01004189 struct inode_info *inode;
plougher0e453652006-11-06 01:49:35 +00004190
4191 for(inode = inode_info[i]; inode; inode = inode->next) {
plougher0e453652006-11-06 01:49:35 +00004192
Phillip Lougher539c2b12012-07-30 20:14:52 +01004193 inode_number = get_inode_no(inode);
plougher0e453652006-11-06 01:49:35 +00004194
plougher360514a2009-03-30 03:01:38 +00004195 SQUASHFS_SWAP_LONG_LONGS(&inode->inode,
4196 &inode_lookup_table[inode_number - 1], 1);
plougher0e453652006-11-06 01:49:35 +00004197
plougher0e453652006-11-06 01:49:35 +00004198 }
4199 }
4200
plougher02bc3bc2007-02-25 12:12:01 +00004201skip_inode_hash_table:
ploughera0a49c32010-08-11 01:47:59 +00004202 return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL,
4203 noI);
plougher0e453652006-11-06 01:49:35 +00004204}
4205
plougher2ea89142008-03-11 01:34:19 +00004206
Phillip Lougher8e44e052012-11-26 02:58:35 +00004207char *get_component(char *target, char **targname)
plougher8f8e1a12007-10-18 02:50:21 +00004208{
Phillip Lougher8e44e052012-11-26 02:58:35 +00004209 char *start;
4210
plougher8f8e1a12007-10-18 02:50:21 +00004211 while(*target == '/')
rlougherc4ebcf52007-11-08 17:52:49 +00004212 target ++;
plougher8f8e1a12007-10-18 02:50:21 +00004213
Phillip Lougher8e44e052012-11-26 02:58:35 +00004214 start = target;
Phillip Lougher66d8b8e2014-04-20 21:01:33 +01004215 while(*target != '/' && *target != '\0')
Phillip Lougher8e44e052012-11-26 02:58:35 +00004216 target ++;
plougher8f8e1a12007-10-18 02:50:21 +00004217
Phillip Lougher8e44e052012-11-26 02:58:35 +00004218 *targname = strndup(start, target - start);
plougher8f8e1a12007-10-18 02:50:21 +00004219
Phillip Lougher66d8b8e2014-04-20 21:01:33 +01004220 while(*target == '/')
4221 target ++;
4222
plougher8f8e1a12007-10-18 02:50:21 +00004223 return target;
4224}
4225
4226
4227void free_path(struct pathname *paths)
4228{
4229 int i;
4230
4231 for(i = 0; i < paths->names; i++) {
4232 if(paths->name[i].paths)
4233 free_path(paths->name[i].paths);
4234 free(paths->name[i].name);
4235 if(paths->name[i].preg) {
4236 regfree(paths->name[i].preg);
4237 free(paths->name[i].preg);
4238 }
4239 }
4240
4241 free(paths);
4242}
4243
4244
4245struct pathname *add_path(struct pathname *paths, char *target, char *alltarget)
4246{
Phillip Lougher8e44e052012-11-26 02:58:35 +00004247 char *targname;
plougher8f8e1a12007-10-18 02:50:21 +00004248 int i, error;
4249
Phillip Lougher8e44e052012-11-26 02:58:35 +00004250 target = get_component(target, &targname);
plougher8f8e1a12007-10-18 02:50:21 +00004251
4252 if(paths == NULL) {
plougherdec6ef12010-12-18 02:47:53 +00004253 paths = malloc(sizeof(struct pathname));
4254 if(paths == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004255 MEM_ERROR();
plougher8f8e1a12007-10-18 02:50:21 +00004256
4257 paths->names = 0;
4258 paths->name = NULL;
4259 }
4260
4261 for(i = 0; i < paths->names; i++)
4262 if(strcmp(paths->name[i].name, targname) == 0)
4263 break;
4264
4265 if(i == paths->names) {
4266 /* allocate new name entry */
4267 paths->names ++;
plougherb3604122009-03-30 02:07:20 +00004268 paths->name = realloc(paths->name, (i + 1) *
4269 sizeof(struct path_entry));
plougher6f2a8262010-07-27 00:37:19 +00004270 if(paths->name == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004271 MEM_ERROR();
Phillip Lougher8e44e052012-11-26 02:58:35 +00004272 paths->name[i].name = targname;
plougher8f8e1a12007-10-18 02:50:21 +00004273 paths->name[i].paths = NULL;
4274 if(use_regex) {
4275 paths->name[i].preg = malloc(sizeof(regex_t));
plougher5c60eab2010-07-21 01:12:19 +00004276 if(paths->name[i].preg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004277 MEM_ERROR();
plougherb3604122009-03-30 02:07:20 +00004278 error = regcomp(paths->name[i].preg, targname,
4279 REG_EXTENDED|REG_NOSUB);
plougher23377982007-11-12 04:04:48 +00004280 if(error) {
Phillip Lougher62859d22012-11-30 05:53:56 +00004281 char str[1024]; /* overflow safe */
plougher8f8e1a12007-10-18 02:50:21 +00004282
4283 regerror(error, paths->name[i].preg, str, 1024);
plougherb3604122009-03-30 02:07:20 +00004284 BAD_ERROR("invalid regex %s in export %s, "
plougher50b31762009-03-31 04:14:46 +00004285 "because %s\n", targname, alltarget,
4286 str);
plougher8f8e1a12007-10-18 02:50:21 +00004287 }
4288 } else
4289 paths->name[i].preg = NULL;
4290
4291 if(target[0] == '\0')
4292 /* at leaf pathname component */
4293 paths->name[i].paths = NULL;
4294 else
4295 /* recurse adding child components */
plougher50b31762009-03-31 04:14:46 +00004296 paths->name[i].paths = add_path(NULL, target,
4297 alltarget);
plougher8f8e1a12007-10-18 02:50:21 +00004298 } else {
4299 /* existing matching entry */
Phillip Lougher8e44e052012-11-26 02:58:35 +00004300 free(targname);
4301
plougher8f8e1a12007-10-18 02:50:21 +00004302 if(paths->name[i].paths == NULL) {
plougher50b31762009-03-31 04:14:46 +00004303 /* No sub-directory which means this is the leaf
4304 * component of a pre-existing exclude which subsumes
4305 * the exclude currently being added, in which case stop
4306 * adding components */
plougher8f8e1a12007-10-18 02:50:21 +00004307 } else if(target[0] == '\0') {
plougherb3604122009-03-30 02:07:20 +00004308 /* at leaf pathname component and child components exist
plougher50b31762009-03-31 04:14:46 +00004309 * from more specific excludes, delete as they're
4310 * subsumed by this exclude */
plougher8f8e1a12007-10-18 02:50:21 +00004311 free_path(paths->name[i].paths);
4312 paths->name[i].paths = NULL;
4313 } else
4314 /* recurse adding child components */
4315 add_path(paths->name[i].paths, target, alltarget);
4316 }
4317
4318 return paths;
4319}
plougher2ea89142008-03-11 01:34:19 +00004320
4321
plougher05e50ef2007-10-23 12:34:20 +00004322void add_exclude(char *target)
plougher8f8e1a12007-10-18 02:50:21 +00004323{
plougher05e50ef2007-10-23 12:34:20 +00004324
plougherb3604122009-03-30 02:07:20 +00004325 if(target[0] == '/' || strncmp(target, "./", 2) == 0 ||
4326 strncmp(target, "../", 3) == 0)
4327 BAD_ERROR("/, ./ and ../ prefixed excludes not supported with "
4328 "-wildcards or -regex options\n");
plougher05e50ef2007-10-23 12:34:20 +00004329 else if(strncmp(target, "... ", 4) == 0)
4330 stickypath = add_path(stickypath, target + 4, target + 4);
4331 else
4332 path = add_path(path, target, target);
plougher8f8e1a12007-10-18 02:50:21 +00004333}
4334
4335
4336void display_path(int depth, struct pathname *paths)
4337{
4338 int i, n;
4339
4340 if(paths == NULL)
4341 return;
4342
4343 for(i = 0; i < paths->names; i++) {
4344 for(n = 0; n < depth; n++)
4345 printf("\t");
4346 printf("%d: %s\n", depth, paths->name[i].name);
4347 display_path(depth + 1, paths->name[i].paths);
4348 }
4349}
4350
4351
4352void display_path2(struct pathname *paths, char *string)
4353{
4354 int i;
Phillip Lougher91459c12012-11-30 05:24:44 +00004355 char *path;
plougher8f8e1a12007-10-18 02:50:21 +00004356
4357 if(paths == NULL) {
4358 printf("%s\n", string);
4359 return;
4360 }
4361
4362 for(i = 0; i < paths->names; i++) {
Phillip Lougher91459c12012-11-30 05:24:44 +00004363 int res = asprintf(&path, "%s/%s", string, paths->name[i].name);
4364 if(res == -1)
4365 BAD_ERROR("asprintf failed in display_path2\n");
plougher8f8e1a12007-10-18 02:50:21 +00004366 display_path2(paths->name[i].paths, path);
Phillip Lougher91459c12012-11-30 05:24:44 +00004367 free(path);
plougher8f8e1a12007-10-18 02:50:21 +00004368 }
4369}
4370
4371
plougherf9039c92007-10-22 03:54:16 +00004372struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path)
4373{
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004374 int count = paths == NULL ? 0 : paths->count;
4375
4376 if(count % PATHS_ALLOC_SIZE == 0) {
4377 paths = realloc(paths, sizeof(struct pathnames) +
4378 (count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *));
plougher6f2a8262010-07-27 00:37:19 +00004379 if(paths == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004380 MEM_ERROR();
plougher6f2a8262010-07-27 00:37:19 +00004381 }
plougherf9039c92007-10-22 03:54:16 +00004382
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004383 paths->path[count] = path;
4384 paths->count = count + 1;
plougherf9039c92007-10-22 03:54:16 +00004385 return paths;
4386}
4387
4388
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004389int excluded_match(char *name, struct pathname *path, struct pathnames **new)
plougherf9039c92007-10-22 03:54:16 +00004390{
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004391 int i;
plougher8f8e1a12007-10-18 02:50:21 +00004392
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004393 for(i = 0; i < path->names; i++) {
4394 int match = use_regex ?
4395 regexec(path->name[i].preg, name, (size_t) 0,
plougher50b31762009-03-31 04:14:46 +00004396 NULL, 0) == 0 :
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004397 fnmatch(path->name[i].name, name,
4398 FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0;
plougherf9039c92007-10-22 03:54:16 +00004399
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004400 if(match) {
4401 if(path->name[i].paths == NULL || new == NULL)
plougherb3604122009-03-30 02:07:20 +00004402 /* match on a leaf component, any subdirectories
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004403 * in the filesystem should be excluded */
4404 return TRUE;
4405 else
plougherb3604122009-03-30 02:07:20 +00004406 /* match on a non-leaf component, add any
plougher50b31762009-03-31 04:14:46 +00004407 * subdirectories to the new set of
4408 * subdirectories to scan for this name */
plougherf9039c92007-10-22 03:54:16 +00004409 *new = add_subdir(*new, path->name[i].paths);
4410 }
4411 }
4412
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004413 return FALSE;
4414}
4415
4416
4417int excluded(char *name, struct pathnames *paths, struct pathnames **new)
4418{
4419 int n;
4420
4421 if(stickypath && excluded_match(name, stickypath, NULL))
4422 return TRUE;
4423
4424 for(n = 0; paths && n < paths->count; n++) {
4425 int res = excluded_match(name, paths->path[n], new);
4426 if(res) {
4427 free(*new);
4428 *new = NULL;
4429 return TRUE;
4430 }
plougherf9039c92007-10-22 03:54:16 +00004431 }
4432
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004433 /*
4434 * Either:
4435 * - no matching names found, return empty new search set, or
4436 * - one or more matches with sub-directories found (no leaf matches),
4437 * in which case return new search set.
4438 *
4439 * In either case return FALSE as we don't want to exclude this entry
4440 */
plougher8f8e1a12007-10-18 02:50:21 +00004441 return FALSE;
4442}
4443
4444
Phillip Lougher386128f2012-12-16 05:23:45 +00004445void process_exclude_file(char *argv)
4446{
4447 FILE *fd;
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004448 char buffer[MAX_LINE + 1]; /* overflow safe */
Phillip Lougherb22336d2012-12-20 18:44:22 +00004449 char *filename;
Phillip Lougher386128f2012-12-16 05:23:45 +00004450
Phillip Lougherb22336d2012-12-20 18:44:22 +00004451 fd = fopen(argv, "r");
4452 if(fd == NULL)
4453 BAD_ERROR("Failed to open exclude file \"%s\" because %s\n",
4454 argv, strerror(errno));
Phillip Lougher386128f2012-12-16 05:23:45 +00004455
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004456 while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) {
Phillip Lougherb22336d2012-12-20 18:44:22 +00004457 int len = strlen(filename);
4458
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004459 if(len == MAX_LINE && filename[len - 1] != '\n')
Phillip Lougherb22336d2012-12-20 18:44:22 +00004460 /* line too large */
4461 BAD_ERROR("Line too long when reading "
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004462 "exclude file \"%s\", larger than %d "
Phillip Lougher83847c12012-12-23 07:22:36 +00004463 "bytes\n", argv, MAX_LINE);
Phillip Lougherb22336d2012-12-20 18:44:22 +00004464
4465 /*
4466 * Remove '\n' terminator if it exists (the last line
4467 * in the file may not be '\n' terminated)
4468 */
4469 if(len && filename[len - 1] == '\n')
4470 filename[len - 1] = '\0';
4471
4472 /* Skip any leading whitespace */
4473 while(isspace(*filename))
4474 filename ++;
4475
4476 /* if comment line, skip */
4477 if(*filename == '#')
4478 continue;
4479
4480 /*
4481 * check for initial backslash, to accommodate
4482 * filenames with leading space or leading # character
4483 */
4484 if(*filename == '\\')
4485 filename ++;
4486
4487 /* if line is now empty after skipping characters, skip it */
4488 if(*filename == '\0')
4489 continue;
4490
Phillip Lougher386128f2012-12-16 05:23:45 +00004491 if(old_exclude)
4492 old_add_exclude(filename);
4493 else
4494 add_exclude(filename);
Phillip Lougherb22336d2012-12-20 18:44:22 +00004495 }
4496
4497 if(ferror(fd))
4498 BAD_ERROR("Reading exclude file \"%s\" failed because %s\n",
4499 argv, strerror(errno));
Phillip Lougher386128f2012-12-16 05:23:45 +00004500
4501 fclose(fd);
4502}
4503
4504
plougher99ac0cc2007-10-29 03:17:10 +00004505#define RECOVER_ID "Squashfs recovery file v1.0\n"
4506#define RECOVER_ID_SIZE 28
4507
plougher64e83fd2010-12-31 21:21:26 +00004508void write_recovery_data(struct squashfs_super_block *sBlk)
plougher99ac0cc2007-10-29 03:17:10 +00004509{
plougher1d065e92010-06-18 03:58:27 +00004510 int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start;
plougher99ac0cc2007-10-29 03:17:10 +00004511 pid_t pid = getpid();
plougher44d54ef2010-02-08 22:13:49 +00004512 char *metadata;
plougher99ac0cc2007-10-29 03:17:10 +00004513 char header[] = RECOVER_ID;
4514
4515 if(recover == FALSE) {
4516 printf("No recovery data option specified.\n");
ploughereac18532007-10-29 05:26:06 +00004517 printf("Skipping saving recovery file.\n\n");
plougher99ac0cc2007-10-29 03:17:10 +00004518 return;
4519 }
4520
plougher1b879bc2010-12-18 02:49:42 +00004521 metadata = malloc(bytes);
4522 if(metadata == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004523 MEM_ERROR();
plougher44d54ef2010-02-08 22:13:49 +00004524
plougher1d065e92010-06-18 03:58:27 +00004525 res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata);
Phillip Lougher477f4332013-03-06 03:55:08 +00004526 if(res == 0) {
4527 ERROR("Failed to read append filesystem metadata\n");
4528 BAD_ERROR("Filesystem corrupted?\n");
4529 }
plougher99ac0cc2007-10-29 03:17:10 +00004530
Phillip Lougheraf4c9232012-11-15 03:46:28 +00004531 res = asprintf(&recovery_file, "squashfs_recovery_%s_%d",
plougher44d54ef2010-02-08 22:13:49 +00004532 getbase(destination_file), pid);
Phillip Lougheraf4c9232012-11-15 03:46:28 +00004533 if(res == -1)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004534 MEM_ERROR();
Phillip Lougheraf4c9232012-11-15 03:46:28 +00004535
plougherb3604122009-03-30 02:07:20 +00004536 recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
4537 if(recoverfd == -1)
4538 BAD_ERROR("Failed to create recovery file, because %s. "
4539 "Aborting\n", strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004540
plougher628e7682009-03-29 22:12:24 +00004541 if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1)
plougherb3604122009-03-30 02:07:20 +00004542 BAD_ERROR("Failed to write recovery file, because %s\n",
4543 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004544
plougher64e83fd2010-12-31 21:21:26 +00004545 if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1)
plougherb3604122009-03-30 02:07:20 +00004546 BAD_ERROR("Failed to write recovery file, because %s\n",
4547 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004548
plougher628e7682009-03-29 22:12:24 +00004549 if(write_bytes(recoverfd, metadata, bytes) == -1)
plougherb3604122009-03-30 02:07:20 +00004550 BAD_ERROR("Failed to write recovery file, because %s\n",
4551 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004552
4553 close(recoverfd);
plougher44d54ef2010-02-08 22:13:49 +00004554 free(metadata);
plougher99ac0cc2007-10-29 03:17:10 +00004555
4556 printf("Recovery file \"%s\" written\n", recovery_file);
4557 printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n");
plougherb3604122009-03-30 02:07:20 +00004558 printf("mksquashfs dummy %s -recover %s\n", destination_file,
4559 recovery_file);
plougher99ac0cc2007-10-29 03:17:10 +00004560 printf("to restore filesystem\n\n");
4561}
4562
4563
4564void read_recovery_data(char *recovery_file, char *destination_file)
4565{
4566 int fd, recoverfd, bytes;
plougher64e83fd2010-12-31 21:21:26 +00004567 struct squashfs_super_block orig_sBlk, sBlk;
plougher99ac0cc2007-10-29 03:17:10 +00004568 char *metadata;
plougher8a8c4102009-03-29 22:28:49 +00004569 int res;
plougher99ac0cc2007-10-29 03:17:10 +00004570 struct stat buf;
4571 char header[] = RECOVER_ID;
4572 char header2[RECOVER_ID_SIZE];
4573
plougher9e9d9dc2010-12-18 02:53:57 +00004574 recoverfd = open(recovery_file, O_RDONLY);
4575 if(recoverfd == -1)
plougherb3604122009-03-30 02:07:20 +00004576 BAD_ERROR("Failed to open recovery file because %s\n",
4577 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004578
4579 if(stat(destination_file, &buf) == -1)
plougherb3604122009-03-30 02:07:20 +00004580 BAD_ERROR("Failed to stat destination file, because %s\n",
4581 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004582
plougher9e9d9dc2010-12-18 02:53:57 +00004583 fd = open(destination_file, O_RDWR);
4584 if(fd == -1)
plougherb3604122009-03-30 02:07:20 +00004585 BAD_ERROR("Failed to open destination file because %s\n",
4586 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004587
plougher8a8c4102009-03-29 22:28:49 +00004588 res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE);
4589 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00004590 BAD_ERROR("Failed to read recovery file, because %s\n",
4591 strerror(errno));
plougher8a8c4102009-03-29 22:28:49 +00004592 if(res < RECOVER_ID_SIZE)
4593 BAD_ERROR("Recovery file appears to be truncated\n");
plougher99ac0cc2007-10-29 03:17:10 +00004594 if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 )
4595 BAD_ERROR("Not a recovery file\n");
4596
plougher64e83fd2010-12-31 21:21:26 +00004597 res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block));
plougher8a8c4102009-03-29 22:28:49 +00004598 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00004599 BAD_ERROR("Failed to read recovery file, because %s\n",
4600 strerror(errno));
plougher64e83fd2010-12-31 21:21:26 +00004601 if(res < sizeof(struct squashfs_super_block))
plougher8a8c4102009-03-29 22:28:49 +00004602 BAD_ERROR("Recovery file appears to be truncated\n");
plougher99ac0cc2007-10-29 03:17:10 +00004603
plougher64e83fd2010-12-31 21:21:26 +00004604 res = read_fs_bytes(fd, 0, sizeof(struct squashfs_super_block), &orig_sBlk);
Phillip Lougher477f4332013-03-06 03:55:08 +00004605 if(res == 0) {
4606 ERROR("Failed to read superblock from output filesystem\n");
4607 BAD_ERROR("Output filesystem is empty!\n");
4608 }
plougher99ac0cc2007-10-29 03:17:10 +00004609
plougherb3604122009-03-30 02:07:20 +00004610 if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4,
plougher64e83fd2010-12-31 21:21:26 +00004611 sizeof(struct squashfs_super_block) - 4) != 0)
plougherb3604122009-03-30 02:07:20 +00004612 BAD_ERROR("Recovery file and destination file do not seem to "
4613 "match\n");
plougher99ac0cc2007-10-29 03:17:10 +00004614
4615 bytes = sBlk.bytes_used - sBlk.inode_table_start;
4616
plougher9e9d9dc2010-12-18 02:53:57 +00004617 metadata = malloc(bytes);
4618 if(metadata == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004619 MEM_ERROR();
plougher99ac0cc2007-10-29 03:17:10 +00004620
plougher8a8c4102009-03-29 22:28:49 +00004621 res = read_bytes(recoverfd, metadata, bytes);
4622 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00004623 BAD_ERROR("Failed to read recovery file, because %s\n",
4624 strerror(errno));
plougher8a8c4102009-03-29 22:28:49 +00004625 if(res < bytes)
plougher99ac0cc2007-10-29 03:17:10 +00004626 BAD_ERROR("Recovery file appears to be truncated\n");
4627
plougher64e83fd2010-12-31 21:21:26 +00004628 write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk);
plougher99ac0cc2007-10-29 03:17:10 +00004629
plougher0dd6f122009-03-29 21:43:57 +00004630 write_destination(fd, sBlk.inode_table_start, bytes, metadata);
plougher99ac0cc2007-10-29 03:17:10 +00004631
4632 close(recoverfd);
4633 close(fd);
4634
plougherb3604122009-03-30 02:07:20 +00004635 printf("Successfully wrote recovery file \"%s\". Exiting\n",
4636 recovery_file);
plougher99ac0cc2007-10-29 03:17:10 +00004637
4638 exit(0);
4639}
4640
4641
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004642int parse_number(char *start, int *res, int size)
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004643{
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004644 char *end;
4645 long number = strtol(start, &end, 10);
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004646
Phillip Lougherf4f48382012-11-29 03:52:44 +00004647 /*
4648 * check for strtol underflow or overflow in conversion.
4649 * Note: strtol can validly return LONG_MIN and LONG_MAX
4650 * if the user entered these values, but, additional code
4651 * to distinguish this scenario is unnecessary, because for
4652 * our purposes LONG_MIN and LONG_MAX are too large anyway
4653 */
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004654 if(number == LONG_MIN || number == LONG_MAX)
4655 return 0;
4656
4657 /* reject negative numbers as invalid */
4658 if(number < 0)
4659 return 0;
4660
4661 /* check if long result will overflow signed int */
4662 if(number > INT_MAX)
4663 return 0;
4664
4665 if(size) {
Phillip Loughered345692012-11-28 03:56:16 +00004666 /*
4667 * Check for multiplier and trailing junk.
4668 * But first check that a number exists before the
4669 * multiplier
4670 */
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004671 if(end == start)
Phillip Loughered345692012-11-28 03:56:16 +00004672 return 0;
4673
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004674 switch(end[0]) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004675 case 'm':
4676 case 'M':
4677 if(multiply_overflow((int) number, 1048576))
4678 return 0;
4679 number *= 1048576;
Phillip Loughera9e65632012-11-28 04:17:27 +00004680
Phillip Lougherf968be82012-12-04 05:12:32 +00004681 if(end[1] != '\0')
Phillip Loughera9e65632012-11-28 04:17:27 +00004682 /* trailing junk after number */
4683 return 0;
4684
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004685 break;
4686 case 'k':
4687 case 'K':
4688 if(multiply_overflow((int) number, 1024))
4689 return 0;
4690 number *= 1024;
Phillip Loughera9e65632012-11-28 04:17:27 +00004691
Phillip Lougherf968be82012-12-04 05:12:32 +00004692 if(end[1] != '\0')
Phillip Loughera9e65632012-11-28 04:17:27 +00004693 /* trailing junk after number */
4694 return 0;
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004695 case '\0':
4696 break;
4697 default:
4698 /* trailing junk after number */
4699 return 0;
4700 }
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004701 } else if(end[0] != '\0')
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004702 /* trailing junk after number */
4703 return 0;
4704
4705 *res = number;
4706 return 1;
4707}
4708
4709
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00004710void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad)
4711{
4712 int i;
4713
4714 sBlk->fragments = fragments;
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00004715 sBlk->no_ids = id_count;
4716 sBlk->inode_table_start = write_inodes();
4717 sBlk->directory_table_start = write_directories();
4718 sBlk->fragment_table_start = write_fragment_table();
4719 sBlk->lookup_table_start = exportable ? write_inode_lookup_table() :
4720 SQUASHFS_INVALID_BLK;
4721 sBlk->id_table_start = write_id_table();
4722 sBlk->xattr_id_table_start = write_xattrs();
4723
4724 TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start);
4725 TRACE("sBlk->directory_table_start 0x%llx\n",
4726 sBlk->directory_table_start);
4727 TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start);
4728 if(exportable)
4729 TRACE("sBlk->lookup_table_start 0x%llx\n",
4730 sBlk->lookup_table_start);
4731
4732 sBlk->bytes_used = bytes;
4733
4734 sBlk->compression = comp->id;
4735
4736 SQUASHFS_INSWAP_SUPER_BLOCK(sBlk);
4737 write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk);
4738
4739 if(!nopad && (i = bytes & (4096 - 1))) {
4740 char temp[4096] = {0};
4741 write_destination(fd, bytes, 4096 - i, temp);
4742 }
4743
4744 close(fd);
4745
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00004746 if(recovery_file)
4747 unlink(recovery_file);
4748
4749 total_bytes += total_inode_bytes + total_directory_bytes +
4750 sizeof(struct squashfs_super_block) + total_xattr_bytes;
4751
4752 printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size"
4753 " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR,
4754 SQUASHFS_MINOR, comp->name, block_size);
4755 printf("\t%s data, %s metadata, %s fragments, %s xattrs\n",
4756 noD ? "uncompressed" : "compressed", noI ? "uncompressed" :
4757 "compressed", no_fragments ? "no" : noF ? "uncompressed" :
4758 "compressed", no_xattrs ? "no" : noX ? "uncompressed" :
4759 "compressed");
4760 printf("\tduplicates are %sremoved\n", duplicate_checking ? "" :
4761 "not ");
4762 printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0,
4763 bytes / (1024.0 * 1024.0));
4764 printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n",
4765 ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0);
4766 printf("Inode table size %d bytes (%.2f Kbytes)\n",
4767 inode_bytes, inode_bytes / 1024.0);
4768 printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n",
4769 ((float) inode_bytes / total_inode_bytes) * 100.0,
4770 total_inode_bytes);
4771 printf("Directory table size %d bytes (%.2f Kbytes)\n",
4772 directory_bytes, directory_bytes / 1024.0);
4773 printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n",
4774 ((float) directory_bytes / total_directory_bytes) * 100.0,
4775 total_directory_bytes);
4776 if(total_xattr_bytes) {
4777 printf("Xattr table size %d bytes (%.2f Kbytes)\n",
4778 xattr_bytes, xattr_bytes / 1024.0);
4779 printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n",
4780 ((float) xattr_bytes / total_xattr_bytes) * 100.0,
4781 total_xattr_bytes);
4782 }
4783 if(duplicate_checking)
4784 printf("Number of duplicate files found %d\n", file_count -
4785 dup_files);
4786 else
4787 printf("No duplicate files removed\n");
4788 printf("Number of inodes %d\n", inode_count);
4789 printf("Number of files %d\n", file_count);
4790 if(!no_fragments)
4791 printf("Number of fragments %d\n", fragments);
4792 printf("Number of symbolic links %d\n", sym_count);
4793 printf("Number of device nodes %d\n", dev_count);
4794 printf("Number of fifo nodes %d\n", fifo_count);
4795 printf("Number of socket nodes %d\n", sock_count);
4796 printf("Number of directories %d\n", dir_count);
4797 printf("Number of ids (unique uids + gids) %d\n", id_count);
4798 printf("Number of uids %d\n", uid_count);
4799
4800 for(i = 0; i < id_count; i++) {
4801 if(id_table[i]->flags & ISA_UID) {
4802 struct passwd *user = getpwuid(id_table[i]->id);
4803 printf("\t%s (%d)\n", user == NULL ? "unknown" :
4804 user->pw_name, id_table[i]->id);
4805 }
4806 }
4807
4808 printf("Number of gids %d\n", guid_count);
4809
4810 for(i = 0; i < id_count; i++) {
4811 if(id_table[i]->flags & ISA_GID) {
4812 struct group *group = getgrgid(id_table[i]->id);
4813 printf("\t%s (%d)\n", group == NULL ? "unknown" :
4814 group->gr_name, id_table[i]->id);
4815 }
4816 }
4817}
4818
4819
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004820int parse_num(char *arg, int *res)
4821{
4822 return parse_number(arg, res, 0);
4823}
4824
4825
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004826int get_physical_memory()
4827{
4828 /* Long longs are used here because with PAE, a 32-bit
4829 machine can have more than 4GB of physical memory */
4830
4831 long long num_pages = sysconf(_SC_PHYS_PAGES);
4832 long long page_size = sysconf(_SC_PAGESIZE);
Phillip Lougher48d42fc2014-04-24 03:04:44 +01004833 int phys_mem = num_pages * page_size >> 20;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004834
4835 if(phys_mem < SQUASHFS_LOWMEM)
4836 BAD_ERROR("Mksquashfs requires more physical memory than is "
4837 "available!\n");
4838
Phillip Lougher78784bb2014-04-28 04:08:40 +01004839 return phys_mem;
Phillip Lougher48d42fc2014-04-24 03:04:44 +01004840}
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004841
Phillip Lougher48d42fc2014-04-24 03:04:44 +01004842
4843void calculate_queue_sizes(int mem, int *readq, int *fragq, int *bwriteq,
4844 int *fwriteq)
4845{
4846 *readq = mem / SQUASHFS_READQ_MEM;
4847 *bwriteq = mem / SQUASHFS_BWRITEQ_MEM;
4848 *fwriteq = mem / SQUASHFS_FWRITEQ_MEM;
4849 *fragq = mem - *readq - *bwriteq - *fwriteq;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004850}
4851
4852
plougher1f413c82005-11-18 00:02:14 +00004853#define VERSION() \
Phillip Loughera8c9ff22014-04-29 03:53:18 +01004854 printf("mksquashfs version 4.2-git (2014/04/27)\n");\
Phillip Loughercc064952014-01-03 04:26:34 +00004855 printf("copyright (C) 2014 Phillip Lougher "\
Phillip Lougher83d42a32012-10-31 23:42:22 +00004856 "<phillip@squashfs.org.uk>\n\n"); \
plougher16111452010-07-22 05:12:18 +00004857 printf("This program is free software; you can redistribute it and/or"\
4858 "\n");\
4859 printf("modify it under the terms of the GNU General Public License"\
4860 "\n");\
4861 printf("as published by the Free Software Foundation; either version "\
4862 "2,\n");\
plougher1f413c82005-11-18 00:02:14 +00004863 printf("or (at your option) any later version.\n\n");\
plougher16111452010-07-22 05:12:18 +00004864 printf("This program is distributed in the hope that it will be "\
4865 "useful,\n");\
4866 printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\
4867 "of\n");\
4868 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\
4869 "\n");\
plougher1f413c82005-11-18 00:02:14 +00004870 printf("GNU General Public License for more details.\n");
4871int main(int argc, char *argv[])
4872{
plougher324978d2006-02-27 04:53:29 +00004873 struct stat buf, source_buf;
plougher13fdddf2010-11-24 01:23:41 +00004874 int res, i;
plougher1f413c82005-11-18 00:02:14 +00004875 char *b, *root_name = NULL;
Phillip Loughera709bff2013-03-28 17:48:31 +00004876 int keep_as_directory = FALSE;
plougher1f413c82005-11-18 00:02:14 +00004877 squashfs_inode inode;
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004878 int readq;
4879 int fragq;
4880 int bwriteq;
4881 int fwriteq;
Phillip Lougher78784bb2014-04-28 04:08:40 +01004882 int total_mem = get_physical_memory() / SQUASHFS_TAKE;
Phillip Lougher569a9632013-04-22 03:30:30 +01004883 int progress = TRUE;
Phillip Lougherc6c73b22012-10-19 03:12:08 +01004884 int force_progress = FALSE;
Phillip Loughera709bff2013-03-28 17:48:31 +00004885 struct file_buffer **fragment = NULL;
plougher1f413c82005-11-18 00:02:14 +00004886
plougher1f413c82005-11-18 00:02:14 +00004887 if(argc > 1 && strcmp(argv[1], "-version") == 0) {
4888 VERSION();
4889 exit(0);
4890 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004891
4892 block_log = slog(block_size);
Phillip Lougher48d42fc2014-04-24 03:04:44 +01004893 calculate_queue_sizes(total_mem, &readq, &fragq, &bwriteq, &fwriteq);
Phillip Lougherc3af83a2014-04-21 21:38:16 +01004894
plougher1f413c82005-11-18 00:02:14 +00004895 for(i = 1; i < argc && argv[i][0] != '-'; i++);
4896 if(i < 3)
4897 goto printOptions;
4898 source_path = argv + 1;
4899 source = i - 2;
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004900
plougher4da4bd42010-11-21 05:01:54 +00004901 /*
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004902 * Scan the command line for -comp xxx option, this is to ensure
4903 * any -X compressor specific options are passed to the
4904 * correct compressor
plougher4da4bd42010-11-21 05:01:54 +00004905 */
plougher1f413c82005-11-18 00:02:14 +00004906 for(; i < argc; i++) {
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004907 struct compressor *prev_comp = comp;
4908
4909 if(strcmp(argv[i], "-comp") == 0) {
4910 if(++i == argc) {
4911 ERROR("%s: -comp missing compression type\n",
4912 argv[0]);
4913 exit(1);
4914 }
4915 comp = lookup_compressor(argv[i]);
4916 if(!comp->supported) {
4917 ERROR("%s: Compressor \"%s\" is not supported!"
4918 "\n", argv[0], argv[i]);
4919 ERROR("%s: Compressors available:\n", argv[0]);
4920 display_compressors("", COMP_DEFAULT);
4921 exit(1);
4922 }
4923 if(prev_comp != NULL && prev_comp != comp) {
4924 ERROR("%s: -comp multiple conflicting -comp"
4925 " options specified on command line"
4926 ", previously %s, now %s\n", argv[0],
4927 prev_comp->name, comp->name);
4928 exit(1);
4929 }
4930 compressor_opt_parsed = 1;
4931
4932 } else if(strcmp(argv[i], "-e") == 0)
4933 break;
4934 else if(strcmp(argv[i], "-root-becomes") == 0 ||
4935 strcmp(argv[i], "-ef") == 0 ||
4936 strcmp(argv[i], "-pf") == 0 ||
4937 strcmp(argv[i], "-af") == 0 ||
4938 strcmp(argv[i], "-comp") == 0)
4939 i++;
4940 }
4941
4942 /*
4943 * if no -comp option specified lookup default compressor. Note the
4944 * Makefile ensures the default compressor has been built, and so we
4945 * don't need to to check for failure here
4946 */
4947 if(comp == NULL)
4948 comp = lookup_compressor(COMP_DEFAULT);
4949
4950 for(i = source + 2; i < argc; i++) {
Phillip Lougher9523b042012-10-27 01:48:48 +01004951 if(strcmp(argv[i], "-action") == 0 ||
4952 strcmp(argv[i], "-a") ==0) {
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01004953 if(++i == argc) {
Phillip Lougher9523b042012-10-27 01:48:48 +01004954 ERROR("%s: %s missing action\n",
4955 argv[0], argv[i - 1]);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01004956 exit(1);
4957 }
4958 res = parse_action(argv[i]);
4959 if(res == 0)
4960 exit(1);
4961
Phillip Lougherb73caba2012-12-24 21:58:41 +00004962 } else if(strcmp(argv[i], "-af") == 0) {
4963 if(++i == argc) {
4964 ERROR("%s: -af missing filename\n", argv[0]);
4965 exit(1);
4966 }
4967 if(read_action_file(argv[i]) == FALSE)
4968 exit(1);
4969
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004970 } else if(strcmp(argv[i], "-comp") == 0)
4971 /* parsed previously */
4972 i++;
plougherb5576ea2010-11-22 01:06:53 +00004973
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004974 else if(strncmp(argv[i], "-X", 2) == 0) {
Phillip Lougher774b7b32014-01-07 04:47:13 +00004975 int args;
4976
4977 if(strcmp(argv[i] + 2, "help") == 0)
4978 goto print_compressor_options;
4979
4980 args = compressor_options(comp, argv + i, argc - i);
plougherd8865672010-11-27 05:05:49 +00004981 if(args < 0) {
4982 if(args == -1) {
4983 ERROR("%s: Unrecognised compressor"
4984 " option %s\n", argv[0],
4985 argv[i]);
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004986 if(!compressor_opt_parsed)
4987 ERROR("%s: Did you forget to"
4988 " specify -comp?\n",
4989 argv[0]);
Phillip Lougher774b7b32014-01-07 04:47:13 +00004990print_compressor_options:
Phillip Lougherb1c3b6a2014-01-06 05:30:00 +00004991 ERROR("%s: selected compressor \"%s\""
4992 ". Options supported: %s\n",
4993 argv[0], comp->name,
4994 comp->usage ? "" : "none");
4995 if(comp->usage)
4996 comp->usage();
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004997 }
plougherb5576ea2010-11-22 01:06:53 +00004998 exit(1);
4999 }
5000 i += args;
5001
plougherae9dcd82009-08-01 02:59:38 +00005002 } else if(strcmp(argv[i], "-pf") == 0) {
plougher43244f22009-04-05 02:04:51 +00005003 if(++i == argc) {
5004 ERROR("%s: -pf missing filename\n", argv[0]);
5005 exit(1);
5006 }
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00005007 if(read_pseudo_file(argv[i]) == FALSE)
plougher43244f22009-04-05 02:04:51 +00005008 exit(1);
plougher43244f22009-04-05 02:04:51 +00005009 } else if(strcmp(argv[i], "-p") == 0) {
5010 if(++i == argc) {
5011 ERROR("%s: -p missing pseudo file definition\n",
5012 argv[0]);
5013 exit(1);
5014 }
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00005015 if(read_pseudo_def(argv[i]) == FALSE)
plougher43244f22009-04-05 02:04:51 +00005016 exit(1);
plougher43244f22009-04-05 02:04:51 +00005017 } else if(strcmp(argv[i], "-recover") == 0) {
plougher99ac0cc2007-10-29 03:17:10 +00005018 if(++i == argc) {
plougherb3604122009-03-30 02:07:20 +00005019 ERROR("%s: -recover missing recovery file\n",
5020 argv[0]);
plougher99ac0cc2007-10-29 03:17:10 +00005021 exit(1);
5022 }
5023 read_recovery_data(argv[i], argv[source + 1]);
5024 } else if(strcmp(argv[i], "-no-recovery") == 0)
5025 recover = FALSE;
5026 else if(strcmp(argv[i], "-wildcards") == 0) {
plougher934a9ed2007-10-19 00:21:10 +00005027 old_exclude = FALSE;
5028 use_regex = FALSE;
5029 } else if(strcmp(argv[i], "-regex") == 0) {
5030 old_exclude = FALSE;
5031 use_regex = TRUE;
5032 } else if(strcmp(argv[i], "-no-sparse") == 0)
plougher1f54edc2007-08-12 23:13:36 +00005033 sparse_files = FALSE;
5034 else if(strcmp(argv[i], "-no-progress") == 0)
plougher02bc3bc2007-02-25 12:12:01 +00005035 progress = FALSE;
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005036 else if(strcmp(argv[i], "-progress") == 0)
5037 force_progress = TRUE;
plougher02bc3bc2007-02-25 12:12:01 +00005038 else if(strcmp(argv[i], "-no-exports") == 0)
5039 exportable = FALSE;
plougher0e453652006-11-06 01:49:35 +00005040 else if(strcmp(argv[i], "-processors") == 0) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00005041 if((++i == argc) || !parse_num(argv[i], &processors)) {
plougher360514a2009-03-30 03:01:38 +00005042 ERROR("%s: -processors missing or invalid "
5043 "processor number\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005044 exit(1);
5045 }
5046 if(processors < 1) {
plougher360514a2009-03-30 03:01:38 +00005047 ERROR("%s: -processors should be 1 or larger\n",
5048 argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005049 exit(1);
5050 }
plougher0e453652006-11-06 01:49:35 +00005051 } else if(strcmp(argv[i], "-read-queue") == 0) {
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005052 if((++i == argc) || !parse_num(argv[i], &readq)) {
plougher50b31762009-03-31 04:14:46 +00005053 ERROR("%s: -read-queue missing or invalid "
5054 "queue size\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005055 exit(1);
5056 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005057 if(readq < 1) {
plougher360514a2009-03-30 03:01:38 +00005058 ERROR("%s: -read-queue should be 1 megabyte or "
5059 "larger\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005060 exit(1);
5061 }
plougher0e453652006-11-06 01:49:35 +00005062 } else if(strcmp(argv[i], "-write-queue") == 0) {
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005063 if((++i == argc) || !parse_num(argv[i], &bwriteq)) {
plougher50b31762009-03-31 04:14:46 +00005064 ERROR("%s: -write-queue missing or invalid "
5065 "queue size\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005066 exit(1);
5067 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005068 if(bwriteq < 2) {
5069 ERROR("%s: -write-queue should be 2 megabytes "
plougher50b31762009-03-31 04:14:46 +00005070 "or larger\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00005071 exit(1);
5072 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005073 fwriteq = bwriteq >> 1;
5074 bwriteq -= fwriteq;
plougher217bad82008-04-05 11:36:41 +00005075 } else if(strcmp(argv[i], "-fragment-queue") == 0) {
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005076 if((++i == argc) || !parse_num(argv[i], &fragq)) {
plougher360514a2009-03-30 03:01:38 +00005077 ERROR("%s: -fragment-queue missing or invalid "
5078 "queue size\n", argv[0]);
plougher217bad82008-04-05 11:36:41 +00005079 exit(1);
5080 }
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005081 if(fragq < 1) {
plougher50b31762009-03-31 04:14:46 +00005082 ERROR("%s: -fragment-queue should be 1 "
5083 "megabyte or larger\n", argv[0]);
plougher217bad82008-04-05 11:36:41 +00005084 exit(1);
5085 }
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005086 } else if(strcmp(argv[i], "-mem") == 0) {
5087 if((++i == argc) || !parse_num(argv[i], &total_mem)) {
5088 ERROR("%s: -mem missing or invalid mem size\n",
5089 argv[0]);
5090 exit(1);
5091 }
5092 if(total_mem < SQUASHFS_LOWMEM) {
5093 ERROR("%s: -mem should be %d Mbytes or "
5094 "larger\n", SQUASHFS_LOWMEM, argv[0]);
5095 exit(1);
5096 }
5097 calculate_queue_sizes(total_mem, &readq, &fragq,
5098 &bwriteq, &fwriteq);
plougher5507dd92006-11-06 00:43:10 +00005099 } else if(strcmp(argv[i], "-b") == 0) {
plougher4c99cb72007-06-14 21:46:31 +00005100 if(++i == argc) {
5101 ERROR("%s: -b missing block size\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005102 exit(1);
5103 }
Phillip Lougher94c1fe02012-11-28 03:32:36 +00005104 if(!parse_number(argv[i], &block_size, 1)) {
plougher4c99cb72007-06-14 21:46:31 +00005105 ERROR("%s: -b invalid block size\n", argv[0]);
5106 exit(1);
5107 }
plougher1f413c82005-11-18 00:02:14 +00005108 if((block_log = slog(block_size)) == 0) {
plougher50b31762009-03-31 04:14:46 +00005109 ERROR("%s: -b block size not power of two or "
5110 "not between 4096 and 1Mbyte\n",
5111 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005112 exit(1);
5113 }
5114 } else if(strcmp(argv[i], "-ef") == 0) {
5115 if(++i == argc) {
5116 ERROR("%s: -ef missing filename\n", argv[0]);
5117 exit(1);
5118 }
plougher9b5bf8c2006-03-20 18:43:33 +00005119 } else if(strcmp(argv[i], "-no-duplicates") == 0)
plougher1f413c82005-11-18 00:02:14 +00005120 duplicate_checking = FALSE;
5121
5122 else if(strcmp(argv[i], "-no-fragments") == 0)
5123 no_fragments = TRUE;
5124
5125 else if(strcmp(argv[i], "-always-use-fragments") == 0)
5126 always_use_fragments = TRUE;
5127
5128 else if(strcmp(argv[i], "-sort") == 0) {
5129 if(++i == argc) {
5130 ERROR("%s: -sort missing filename\n", argv[0]);
5131 exit(1);
5132 }
5133 } else if(strcmp(argv[i], "-all-root") == 0 ||
5134 strcmp(argv[i], "-root-owned") == 0)
5135 global_uid = global_gid = 0;
5136
5137 else if(strcmp(argv[i], "-force-uid") == 0) {
5138 if(++i == argc) {
plougher50b31762009-03-31 04:14:46 +00005139 ERROR("%s: -force-uid missing uid or user\n",
5140 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005141 exit(1);
5142 }
5143 if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') {
plougher360514a2009-03-30 03:01:38 +00005144 if(global_uid < 0 || global_uid >
5145 (((long long) 1 << 32) - 1)) {
plougher50b31762009-03-31 04:14:46 +00005146 ERROR("%s: -force-uid uid out of range"
5147 "\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005148 exit(1);
5149 }
5150 } else {
5151 struct passwd *uid = getpwnam(argv[i]);
5152 if(uid)
5153 global_uid = uid->pw_uid;
5154 else {
plougher360514a2009-03-30 03:01:38 +00005155 ERROR("%s: -force-uid invalid uid or "
5156 "unknown user\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005157 exit(1);
5158 }
5159 }
5160 } else if(strcmp(argv[i], "-force-gid") == 0) {
5161 if(++i == argc) {
plougher360514a2009-03-30 03:01:38 +00005162 ERROR("%s: -force-gid missing gid or group\n",
5163 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005164 exit(1);
5165 }
5166 if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') {
plougher360514a2009-03-30 03:01:38 +00005167 if(global_gid < 0 || global_gid >
5168 (((long long) 1 << 32) - 1)) {
plougher50b31762009-03-31 04:14:46 +00005169 ERROR("%s: -force-gid gid out of range"
5170 "\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005171 exit(1);
5172 }
5173 } else {
5174 struct group *gid = getgrnam(argv[i]);
5175 if(gid)
5176 global_gid = gid->gr_gid;
5177 else {
plougher360514a2009-03-30 03:01:38 +00005178 ERROR("%s: -force-gid invalid gid or "
5179 "unknown group\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005180 exit(1);
5181 }
5182 }
5183 } else if(strcmp(argv[i], "-noI") == 0 ||
5184 strcmp(argv[i], "-noInodeCompression") == 0)
5185 noI = TRUE;
5186
5187 else if(strcmp(argv[i], "-noD") == 0 ||
5188 strcmp(argv[i], "-noDataCompression") == 0)
5189 noD = TRUE;
5190
5191 else if(strcmp(argv[i], "-noF") == 0 ||
5192 strcmp(argv[i], "-noFragmentCompression") == 0)
5193 noF = TRUE;
5194
plougherb99d7832010-05-19 01:57:34 +00005195 else if(strcmp(argv[i], "-noX") == 0 ||
5196 strcmp(argv[i], "-noXattrCompression") == 0)
5197 noX = TRUE;
5198
plougherce564c62010-05-19 03:01:49 +00005199 else if(strcmp(argv[i], "-no-xattrs") == 0)
5200 no_xattrs = TRUE;
plougher6d89ac22010-05-19 02:59:23 +00005201
plougher30281c82010-08-25 05:06:11 +00005202 else if(strcmp(argv[i], "-xattrs") == 0)
5203 no_xattrs = FALSE;
5204
plougher1f413c82005-11-18 00:02:14 +00005205 else if(strcmp(argv[i], "-nopad") == 0)
5206 nopad = TRUE;
5207
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005208 else if(strcmp(argv[i], "-info") == 0)
plougher91fbb302008-05-06 02:29:36 +00005209 silent = FALSE;
plougher1f413c82005-11-18 00:02:14 +00005210
5211 else if(strcmp(argv[i], "-e") == 0)
5212 break;
5213
5214 else if(strcmp(argv[i], "-noappend") == 0)
5215 delete = TRUE;
5216
5217 else if(strcmp(argv[i], "-keep-as-directory") == 0)
5218 keep_as_directory = TRUE;
5219
Phillip Lougher85776df2014-01-31 03:10:46 +00005220 else if(strcmp(argv[i], "-exit-on-error") == 0)
5221 exit_on_error = TRUE;
5222
plougher1f413c82005-11-18 00:02:14 +00005223 else if(strcmp(argv[i], "-root-becomes") == 0) {
5224 if(++i == argc) {
plougher50b31762009-03-31 04:14:46 +00005225 ERROR("%s: -root-becomes: missing name\n",
5226 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005227 exit(1);
5228 }
5229 root_name = argv[i];
5230 } else if(strcmp(argv[i], "-version") == 0) {
5231 VERSION();
5232 } else {
5233 ERROR("%s: invalid option\n\n", argv[0]);
5234printOptions:
plougher360514a2009-03-30 03:01:38 +00005235 ERROR("SYNTAX:%s source1 source2 ... dest [options] "
5236 "[-e list of exclude\ndirs/files]\n", argv[0]);
plougher37632562009-08-07 19:09:01 +00005237 ERROR("\nFilesystem build options:\n");
plougherff5ea8b2009-08-07 19:33:10 +00005238 ERROR("-comp <comp>\t\tselect <comp> compression\n");
plougher13df1782009-08-29 01:05:34 +00005239 ERROR("\t\t\tCompressors available:\n");
plougher764dab52009-08-24 18:28:04 +00005240 display_compressors("\t\t\t", COMP_DEFAULT);
plougher50b31762009-03-31 04:14:46 +00005241 ERROR("-b <block_size>\t\tset data block to "
Phillip Lougherb74a59c2014-04-30 03:10:44 +01005242 "<block_size>. Default 128 Kbytes\n");
5243 ERROR("\t\t\tOptionally a suffix of K or M can be"
5244 " given to specify\n\t\t\tKbytes or Mbytes"
5245 " respectively\n");
plougher37632562009-08-07 19:09:01 +00005246 ERROR("-no-exports\t\tdon't make the filesystem "
5247 "exportable via NFS\n");
5248 ERROR("-no-sparse\t\tdon't detect sparse files\n");
plougher07d25c22010-08-25 17:41:57 +00005249 ERROR("-no-xattrs\t\tdon't store extended attributes"
plougher30281c82010-08-25 05:06:11 +00005250 NOXOPT_STR "\n");
plougher07d25c22010-08-25 17:41:57 +00005251 ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR
plougher30281c82010-08-25 05:06:11 +00005252 "\n");
plougher1f413c82005-11-18 00:02:14 +00005253 ERROR("-noI\t\t\tdo not compress inode table\n");
5254 ERROR("-noD\t\t\tdo not compress data blocks\n");
5255 ERROR("-noF\t\t\tdo not compress fragment blocks\n");
plougher16111452010-07-22 05:12:18 +00005256 ERROR("-noX\t\t\tdo not compress extended "
5257 "attributes\n");
plougher1f413c82005-11-18 00:02:14 +00005258 ERROR("-no-fragments\t\tdo not use fragments\n");
plougher360514a2009-03-30 03:01:38 +00005259 ERROR("-always-use-fragments\tuse fragment blocks for "
5260 "files larger than block size\n");
5261 ERROR("-no-duplicates\t\tdo not perform duplicate "
5262 "checking\n");
plougher1f413c82005-11-18 00:02:14 +00005263 ERROR("-all-root\t\tmake all files owned by root\n");
5264 ERROR("-force-uid uid\t\tset all file uids to uid\n");
5265 ERROR("-force-gid gid\t\tset all file gids to gid\n");
plougher50b31762009-03-31 04:14:46 +00005266 ERROR("-nopad\t\t\tdo not pad filesystem to a multiple "
5267 "of 4K\n");
plougher37632562009-08-07 19:09:01 +00005268 ERROR("-keep-as-directory\tif one source directory is "
5269 "specified, create a root\n");
5270 ERROR("\t\t\tdirectory containing that directory, "
5271 "rather than the\n");
5272 ERROR("\t\t\tcontents of the directory\n");
5273 ERROR("\nFilesystem filter options:\n");
plougher16111452010-07-22 05:12:18 +00005274 ERROR("-p <pseudo-definition>\tAdd pseudo file "
5275 "definition\n");
5276 ERROR("-pf <pseudo-file>\tAdd list of pseudo file "
5277 "definitions\n");
plougher360514a2009-03-30 03:01:38 +00005278 ERROR("-sort <sort_file>\tsort files according to "
5279 "priorities in <sort_file>. One\n");
5280 ERROR("\t\t\tfile or dir with priority per line. "
5281 "Priority -32768 to\n");
plougher1f413c82005-11-18 00:02:14 +00005282 ERROR("\t\t\t32767, default priority 0\n");
plougher50b31762009-03-31 04:14:46 +00005283 ERROR("-ef <exclude_file>\tlist of exclude dirs/files."
5284 " One per line\n");
plougher360514a2009-03-30 03:01:38 +00005285 ERROR("-wildcards\t\tAllow extended shell wildcards "
5286 "(globbing) to be used in\n\t\t\texclude "
5287 "dirs/files\n");
plougher50b31762009-03-31 04:14:46 +00005288 ERROR("-regex\t\t\tAllow POSIX regular expressions to "
5289 "be used in exclude\n\t\t\tdirs/files\n");
plougher37632562009-08-07 19:09:01 +00005290 ERROR("\nFilesystem append options:\n");
5291 ERROR("-noappend\t\tdo not append to existing "
5292 "filesystem\n");
5293 ERROR("-root-becomes <name>\twhen appending source "
5294 "files/directories, make the\n");
5295 ERROR("\t\t\toriginal root become a subdirectory in "
5296 "the new root\n");
5297 ERROR("\t\t\tcalled <name>, rather than adding the new "
5298 "source items\n");
5299 ERROR("\t\t\tto the original root\n");
5300 ERROR("\nMksquashfs runtime options:\n");
5301 ERROR("-version\t\tprint version, licence and "
5302 "copyright message\n");
Phillip Lougher85776df2014-01-31 03:10:46 +00005303 ERROR("-exit-on-error\t\ttreat normally ignored errors "
5304 "as fatal\n");
plougher37632562009-08-07 19:09:01 +00005305 ERROR("-recover <name>\t\trecover filesystem data "
5306 "using recovery file <name>\n");
5307 ERROR("-no-recovery\t\tdon't generate a recovery "
5308 "file\n");
5309 ERROR("-info\t\t\tprint files written to filesystem\n");
5310 ERROR("-no-progress\t\tdon't display the progress "
5311 "bar\n");
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005312 ERROR("-progress\t\tdisplay progress bar when using "
5313 "the -info option\n");
plougher37632562009-08-07 19:09:01 +00005314 ERROR("-processors <number>\tUse <number> processors."
5315 " By default will use number of\n");
5316 ERROR("\t\t\tprocessors available\n");
Phillip Lougher48d42fc2014-04-24 03:04:44 +01005317 ERROR("-mem <size>\t\tUse <size> Mbytes. Currently "
5318 "set to %d Mbytes\n", total_mem);
plougher37632562009-08-07 19:09:01 +00005319 ERROR("\nMiscellaneous options:\n");
5320 ERROR("-root-owned\t\talternative name for -all-root"
5321 "\n");
5322 ERROR("-noInodeCompression\talternative name for -noI"
5323 "\n");
5324 ERROR("-noDataCompression\talternative name for -noD"
5325 "\n");
5326 ERROR("-noFragmentCompression\talternative name for "
5327 "-noF\n");
plougherb99d7832010-05-19 01:57:34 +00005328 ERROR("-noXattrCompression\talternative name for "
5329 "-noX\n");
Phillip Lougher774b7b32014-01-07 04:47:13 +00005330 ERROR("\n-Xhelp\t\t\tprint compressor options for"
5331 " selected compressor\n");
plougher4fb66822010-12-08 02:49:28 +00005332 ERROR("\nCompressors available and compressor specific "
5333 "options:\n");
5334 display_compressor_usage(COMP_DEFAULT);
plougher1f413c82005-11-18 00:02:14 +00005335 exit(1);
5336 }
5337 }
5338
plougherb747f4f2010-12-25 04:05:47 +00005339 /*
5340 * Some compressors may need the options to be checked for validity
5341 * once all the options have been processed
5342 */
5343 res = compressor_options_post(comp, block_size);
5344 if(res)
5345 EXIT_MKSQUASHFS();
5346
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005347 /*
5348 * If the -info option has been selected then disable the
5349 * progress bar unless it has been explicitly enabled with
5350 * the -progress option
5351 */
5352 if(!silent)
5353 progress = force_progress;
5354
5355#ifdef SQUASHFS_TRACE
Phillip Lougher989f5fe2013-04-22 03:16:29 +01005356 /*
5357 * Disable progress bar if full debug tracing is enabled.
5358 * The progress bar in this case just gets in the way of the
5359 * debug trace output
5360 */
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005361 progress = FALSE;
5362#endif
5363
plougher91fbb302008-05-06 02:29:36 +00005364 for(i = 0; i < source; i++)
5365 if(lstat(source_path[i], &source_buf) == -1) {
plougher360514a2009-03-30 03:01:38 +00005366 fprintf(stderr, "Cannot stat source directory \"%s\" "
plougher50b31762009-03-31 04:14:46 +00005367 "because %s\n", source_path[i],
5368 strerror(errno));
plougher91fbb302008-05-06 02:29:36 +00005369 EXIT_MKSQUASHFS();
5370 }
plougher324978d2006-02-27 04:53:29 +00005371
5372 destination_file = argv[source + 1];
plougher1f413c82005-11-18 00:02:14 +00005373 if(stat(argv[source + 1], &buf) == -1) {
5374 if(errno == ENOENT) { /* Does not exist */
plougher360514a2009-03-30 03:01:38 +00005375 fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR,
plougher59dce672010-05-19 03:56:59 +00005376 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
plougher360514a2009-03-30 03:01:38 +00005377 if(fd == -1) {
plougher1f413c82005-11-18 00:02:14 +00005378 perror("Could not create destination file");
5379 exit(1);
5380 }
5381 delete = TRUE;
5382 } else {
5383 perror("Could not stat destination file");
5384 exit(1);
5385 }
5386
5387 } else {
5388 if(S_ISBLK(buf.st_mode)) {
5389 if((fd = open(argv[source + 1], O_RDWR)) == -1) {
plougher50b31762009-03-31 04:14:46 +00005390 perror("Could not open block device as "
5391 "destination");
plougher1f413c82005-11-18 00:02:14 +00005392 exit(1);
5393 }
5394 block_device = 1;
5395
5396 } else if(S_ISREG(buf.st_mode)) {
plougher360514a2009-03-30 03:01:38 +00005397 fd = open(argv[source + 1], (delete ? O_TRUNC : 0) |
5398 O_RDWR);
5399 if(fd == -1) {
plougher50b31762009-03-31 04:14:46 +00005400 perror("Could not open regular file for "
5401 "writing as destination");
plougher1f413c82005-11-18 00:02:14 +00005402 exit(1);
5403 }
plougher44d54ef2010-02-08 22:13:49 +00005404 }
plougher1f413c82005-11-18 00:02:14 +00005405 else {
5406 ERROR("Destination not block device or regular file\n");
5407 exit(1);
5408 }
5409
plougher324978d2006-02-27 04:53:29 +00005410 }
plougher1f413c82005-11-18 00:02:14 +00005411
plougher16111452010-07-22 05:12:18 +00005412 /*
5413 * process the exclude files - must be done afer destination file has
5414 * been possibly created
5415 */
plougher1f413c82005-11-18 00:02:14 +00005416 for(i = source + 2; i < argc; i++)
Phillip Lougher386128f2012-12-16 05:23:45 +00005417 if(strcmp(argv[i], "-ef") == 0)
5418 /*
5419 * Note presence of filename arg has already
5420 * been checked
5421 */
5422 process_exclude_file(argv[++i]);
5423 else if(strcmp(argv[i], "-e") == 0)
plougher1f413c82005-11-18 00:02:14 +00005424 break;
plougher8b9a7f62009-08-01 22:52:45 +00005425 else if(strcmp(argv[i], "-root-becomes") == 0 ||
plougher43244f22009-04-05 02:04:51 +00005426 strcmp(argv[i], "-sort") == 0 ||
5427 strcmp(argv[i], "-pf") == 0 ||
Phillip Lougherb73caba2012-12-24 21:58:41 +00005428 strcmp(argv[i], "-af") == 0 ||
plougher8b9a7f62009-08-01 22:52:45 +00005429 strcmp(argv[i], "-comp") == 0)
plougher1f413c82005-11-18 00:02:14 +00005430 i++;
5431
5432 if(i != argc) {
5433 if(++i == argc) {
5434 ERROR("%s: -e missing arguments\n", argv[0]);
plougher324978d2006-02-27 04:53:29 +00005435 EXIT_MKSQUASHFS();
plougher1f413c82005-11-18 00:02:14 +00005436 }
plougher8f8e1a12007-10-18 02:50:21 +00005437 while(i < argc)
5438 if(old_exclude)
5439 old_add_exclude(argv[i++]);
5440 else
plougher05e50ef2007-10-23 12:34:20 +00005441 add_exclude(argv[i++]);
plougher1f413c82005-11-18 00:02:14 +00005442 }
5443
5444 /* process the sort files - must be done afer the exclude files */
5445 for(i = source + 2; i < argc; i++)
5446 if(strcmp(argv[i], "-sort") == 0) {
ploughere1c9a742010-07-21 16:59:05 +00005447 int res = read_sort_file(argv[++i], source,
5448 source_path);
5449 if(res == FALSE)
5450 BAD_ERROR("Failed to read sort file\n");
plougher1f413c82005-11-18 00:02:14 +00005451 sorted ++;
5452 } else if(strcmp(argv[i], "-e") == 0)
5453 break;
plougher8b9a7f62009-08-01 22:52:45 +00005454 else if(strcmp(argv[i], "-root-becomes") == 0 ||
plougher43244f22009-04-05 02:04:51 +00005455 strcmp(argv[i], "-ef") == 0 ||
5456 strcmp(argv[i], "-pf") == 0 ||
Phillip Lougherb73caba2012-12-24 21:58:41 +00005457 strcmp(argv[i], "-af") == 0 ||
plougher8b9a7f62009-08-01 22:52:45 +00005458 strcmp(argv[i], "-comp") == 0)
plougher1f413c82005-11-18 00:02:14 +00005459 i++;
5460
plougher4c99cb72007-06-14 21:46:31 +00005461 if(!delete) {
ploughera175ce22009-07-30 04:43:27 +00005462 comp = read_super(fd, &sBlk, argv[source + 1]);
5463 if(comp == NULL) {
plougher360514a2009-03-30 03:01:38 +00005464 ERROR("Failed to read existing filesystem - will not "
5465 "overwrite - ABORTING!\n");
plougher50b31762009-03-31 04:14:46 +00005466 ERROR("To force Mksquashfs to write to this block "
5467 "device or file use -noappend\n");
plougher4c99cb72007-06-14 21:46:31 +00005468 EXIT_MKSQUASHFS();
5469 }
plougher1f413c82005-11-18 00:02:14 +00005470
plougher1f413c82005-11-18 00:02:14 +00005471 block_log = slog(block_size = sBlk.block_size);
5472 noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags);
5473 noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags);
5474 noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags);
plougher89c7a512010-12-15 08:35:51 +00005475 noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags);
plougher1f413c82005-11-18 00:02:14 +00005476 no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags);
5477 always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags);
5478 duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags);
plougher0e453652006-11-06 01:49:35 +00005479 exportable = SQUASHFS_EXPORTABLE(sBlk.flags);
plougherafae8252010-12-15 09:12:58 +00005480 no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags);
plougher3d7e5182010-12-25 01:49:42 +00005481 comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags);
plougher4c99cb72007-06-14 21:46:31 +00005482 }
5483
Phillip Lougherc3af83a2014-04-21 21:38:16 +01005484 initialise_threads(readq, fragq, bwriteq, fwriteq, delete,
5485 destination_file);
plougher4c99cb72007-06-14 21:46:31 +00005486
plougher13fdddf2010-11-24 01:23:41 +00005487 res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0);
5488 if(res)
5489 BAD_ERROR("compressor_init failed\n");
5490
plougher4c99cb72007-06-14 21:46:31 +00005491 if(delete) {
plougher3d7e5182010-12-25 01:49:42 +00005492 int size;
Phillip Loughera45c9d22011-02-20 04:24:07 +00005493 void *comp_data = compressor_dump_options(comp, block_size,
5494 &size);
plougher3d7e5182010-12-25 01:49:42 +00005495
plougherdf6d8f02009-03-20 03:10:00 +00005496 printf("Creating %d.%d filesystem on %s, block size %d.\n",
plougher978f5882010-12-28 04:34:30 +00005497 SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size);
plougher3d7e5182010-12-25 01:49:42 +00005498
plougher871c72a2010-12-25 04:17:27 +00005499 /*
5500 * store any compressor specific options after the superblock,
5501 * and set the COMP_OPT flag to show that the filesystem has
5502 * compressor specfic options
5503 */
plougher3d7e5182010-12-25 01:49:42 +00005504 if(comp_data) {
5505 unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT;
5506
5507 SQUASHFS_INSWAP_SHORTS(&c_byte, 1);
plougher64e83fd2010-12-31 21:21:26 +00005508 write_destination(fd, sizeof(struct squashfs_super_block),
plougher29e2ace2010-12-31 08:50:00 +00005509 sizeof(c_byte), &c_byte);
plougher64e83fd2010-12-31 21:21:26 +00005510 write_destination(fd, sizeof(struct squashfs_super_block) +
plougher8e4dad42010-12-28 05:56:22 +00005511 sizeof(c_byte), size, comp_data);
plougher64e83fd2010-12-31 21:21:26 +00005512 bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte)
plougher3d7e5182010-12-25 01:49:42 +00005513 + size;
5514 comp_opts = TRUE;
plougher3d7e5182010-12-25 01:49:42 +00005515 } else
plougher64e83fd2010-12-31 21:21:26 +00005516 bytes = sizeof(struct squashfs_super_block);
plougher4c99cb72007-06-14 21:46:31 +00005517 } else {
plougher360514a2009-03-30 03:01:38 +00005518 unsigned int last_directory_block, inode_dir_offset,
5519 inode_dir_file_size, root_inode_size,
plougher50b31762009-03-31 04:14:46 +00005520 inode_dir_start_block, uncompressed_data,
5521 compressed_data, inode_dir_inode_number,
5522 inode_dir_parent_inode;
plougher360514a2009-03-30 03:01:38 +00005523 unsigned int root_inode_start =
5524 SQUASHFS_INODE_BLK(sBlk.root_inode),
5525 root_inode_offset =
5526 SQUASHFS_INODE_OFFSET(sBlk.root_inode);
plougher4c99cb72007-06-14 21:46:31 +00005527
plougher360514a2009-03-30 03:01:38 +00005528 if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table,
5529 &data_cache, &directory_table,
5530 &directory_data_cache, &last_directory_block,
5531 &inode_dir_offset, &inode_dir_file_size,
5532 &root_inode_size, &inode_dir_start_block,
5533 &file_count, &sym_count, &dev_count, &dir_count,
5534 &fifo_count, &sock_count, &total_bytes,
5535 &total_inode_bytes, &total_directory_bytes,
plougher50b31762009-03-31 04:14:46 +00005536 &inode_dir_inode_number,
5537 &inode_dir_parent_inode, add_old_root_entry,
5538 &fragment_table, &inode_lookup_table)) == 0) {
plougher360514a2009-03-30 03:01:38 +00005539 ERROR("Failed to read existing filesystem - will not "
5540 "overwrite - ABORTING!\n");
plougher50b31762009-03-31 04:14:46 +00005541 ERROR("To force Mksquashfs to write to this block "
5542 "device or file use -noappend\n");
plougher324978d2006-02-27 04:53:29 +00005543 EXIT_MKSQUASHFS();
plougher1f413c82005-11-18 00:02:14 +00005544 }
Phillip Lougherb4fc3bf2014-02-06 01:42:06 +00005545 if((append_fragments = fragments = sBlk.fragments)) {
plougher360514a2009-03-30 03:01:38 +00005546 fragment_table = realloc((char *) fragment_table,
plougher50b31762009-03-31 04:14:46 +00005547 ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1))
plougher8ed84b92010-12-31 10:37:24 +00005548 * sizeof(struct squashfs_fragment_entry));
plougher6f2a8262010-07-27 00:37:19 +00005549 if(fragment_table == NULL)
5550 BAD_ERROR("Out of memory in save filesystem state\n");
5551 }
plougher1f413c82005-11-18 00:02:14 +00005552
plougher50b31762009-03-31 04:14:46 +00005553 printf("Appending to existing %d.%d filesystem on %s, block "
plougher978f5882010-12-28 04:34:30 +00005554 "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1],
plougher360514a2009-03-30 03:01:38 +00005555 block_size);
plougher89c7a512010-12-15 08:35:51 +00005556 printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, "
plougherbb988032009-08-06 08:46:34 +00005557 "-always-use-fragments,\n-exportable and -comp options "
5558 "ignored\n");
plougher360514a2009-03-30 03:01:38 +00005559 printf("\nIf appending is not wanted, please re-run with "
5560 "-noappend specified!\n\n");
plougher1f413c82005-11-18 00:02:14 +00005561
plougher360514a2009-03-30 03:01:38 +00005562 compressed_data = (inode_dir_offset + inode_dir_file_size) &
5563 ~(SQUASHFS_METADATA_SIZE - 1);
5564 uncompressed_data = (inode_dir_offset + inode_dir_file_size) &
5565 (SQUASHFS_METADATA_SIZE - 1);
plougher1f413c82005-11-18 00:02:14 +00005566
5567 /* save original filesystem state for restoring ... */
5568 sfragments = fragments;
5569 sbytes = bytes;
5570 sinode_count = sBlk.inodes;
plougher23377982007-11-12 04:04:48 +00005571 scache_bytes = root_inode_offset + root_inode_size;
5572 sdirectory_cache_bytes = uncompressed_data;
ploughera2968ef2009-03-03 10:46:00 +00005573 sdata_cache = malloc(scache_bytes);
plougher332e43d2010-07-21 01:18:30 +00005574 if(sdata_cache == NULL)
5575 BAD_ERROR("Out of memory in save filesystem state\n");
ploughera2968ef2009-03-03 10:46:00 +00005576 sdirectory_data_cache = malloc(sdirectory_cache_bytes);
plougher332e43d2010-07-21 01:18:30 +00005577 if(sdirectory_data_cache == NULL)
5578 BAD_ERROR("Out of memory in save filesystem state\n");
plougher1f413c82005-11-18 00:02:14 +00005579 memcpy(sdata_cache, data_cache, scache_bytes);
plougher360514a2009-03-30 03:01:38 +00005580 memcpy(sdirectory_data_cache, directory_data_cache +
5581 compressed_data, sdirectory_cache_bytes);
plougher1f413c82005-11-18 00:02:14 +00005582 sinode_bytes = root_inode_start;
plougher1f413c82005-11-18 00:02:14 +00005583 stotal_bytes = total_bytes;
5584 stotal_inode_bytes = total_inode_bytes;
plougher50b31762009-03-31 04:14:46 +00005585 stotal_directory_bytes = total_directory_bytes +
5586 compressed_data;
plougher1f413c82005-11-18 00:02:14 +00005587 sfile_count = file_count;
5588 ssym_count = sym_count;
5589 sdev_count = dev_count;
5590 sdir_count = dir_count + 1;
5591 sfifo_count = fifo_count;
5592 ssock_count = sock_count;
5593 sdup_files = dup_files;
plougher1b899fc2008-08-07 01:24:06 +00005594 sid_count = id_count;
plougher99ac0cc2007-10-29 03:17:10 +00005595 write_recovery_data(&sBlk);
Phillip Lougher3bcf67d2013-02-24 10:56:38 +00005596 save_xattrs();
Phillip Lougher3794e342014-04-10 02:15:45 +01005597 appending = TRUE;
plougher1f413c82005-11-18 00:02:14 +00005598
plougher360514a2009-03-30 03:01:38 +00005599 /*
5600 * set the filesystem state up to be able to append to the
plougher50b31762009-03-31 04:14:46 +00005601 * original filesystem. The filesystem state differs depending
5602 * on whether we're appending to the original root directory, or
5603 * if the original root directory becomes a sub-directory
5604 * (root-becomes specified on command line, here root_name !=
5605 * NULL)
plougher1f413c82005-11-18 00:02:14 +00005606 */
5607 inode_bytes = inode_size = root_inode_start;
5608 directory_size = last_directory_block;
5609 cache_size = root_inode_offset + root_inode_size;
5610 directory_cache_size = inode_dir_offset + inode_dir_file_size;
5611 if(root_name) {
plougherca2c93f2008-08-15 08:34:57 +00005612 sdirectory_bytes = last_directory_block;
5613 sdirectory_compressed_bytes = 0;
plougherdf70c3e2006-01-27 09:34:13 +00005614 root_inode_number = inode_dir_parent_inode;
Phillip Lougher539c2b12012-07-30 20:14:52 +01005615 inode_no = sBlk.inodes + 2;
plougher1f413c82005-11-18 00:02:14 +00005616 directory_bytes = last_directory_block;
5617 directory_cache_bytes = uncompressed_data;
plougher360514a2009-03-30 03:01:38 +00005618 memmove(directory_data_cache, directory_data_cache +
5619 compressed_data, uncompressed_data);
plougher1f413c82005-11-18 00:02:14 +00005620 cache_bytes = root_inode_offset + root_inode_size;
plougher360514a2009-03-30 03:01:38 +00005621 add_old_root_entry(root_name, sBlk.root_inode,
5622 inode_dir_inode_number, SQUASHFS_DIR_TYPE);
plougher1f413c82005-11-18 00:02:14 +00005623 total_directory_bytes += compressed_data;
5624 dir_count ++;
5625 } else {
plougher360514a2009-03-30 03:01:38 +00005626 sdirectory_compressed_bytes = last_directory_block -
5627 inode_dir_start_block;
5628 sdirectory_compressed =
5629 malloc(sdirectory_compressed_bytes);
plougher332e43d2010-07-21 01:18:30 +00005630 if(sdirectory_compressed == NULL)
plougher16111452010-07-22 05:12:18 +00005631 BAD_ERROR("Out of memory in save filesystem "
5632 "state\n");
plougher360514a2009-03-30 03:01:38 +00005633 memcpy(sdirectory_compressed, directory_table +
5634 inode_dir_start_block,
5635 sdirectory_compressed_bytes);
plougherca2c93f2008-08-15 08:34:57 +00005636 sdirectory_bytes = inode_dir_start_block;
plougherdf70c3e2006-01-27 09:34:13 +00005637 root_inode_number = inode_dir_inode_number;
Phillip Lougher539c2b12012-07-30 20:14:52 +01005638 inode_no = sBlk.inodes + 1;
plougher1f413c82005-11-18 00:02:14 +00005639 directory_bytes = inode_dir_start_block;
5640 directory_cache_bytes = inode_dir_offset;
5641 cache_bytes = root_inode_offset;
5642 }
5643
plougher360514a2009-03-30 03:01:38 +00005644 inode_count = file_count + dir_count + sym_count + dev_count +
5645 fifo_count + sock_count;
plougher1f413c82005-11-18 00:02:14 +00005646 }
5647
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00005648 if(path)
5649 paths = add_subdir(paths, path);
plougherf9039c92007-10-22 03:54:16 +00005650
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01005651 dump_actions();
Phillip Lougher7a28f7a2014-04-20 23:47:08 +01005652 dump_pseudos();
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01005653
plougher360514a2009-03-30 03:01:38 +00005654 if(delete && !keep_as_directory && source == 1 &&
5655 S_ISDIR(source_buf.st_mode))
Phillip Lougherbae0e422014-01-19 23:42:11 +00005656 dir_scan(&inode, source_path[0], scan1_readdir, progress);
plougher50b31762009-03-31 04:14:46 +00005657 else if(!keep_as_directory && source == 1 &&
5658 S_ISDIR(source_buf.st_mode))
Phillip Lougherbae0e422014-01-19 23:42:11 +00005659 dir_scan(&inode, source_path[0], scan1_single_readdir, progress);
plougher1f413c82005-11-18 00:02:14 +00005660 else
Phillip Lougherbae0e422014-01-19 23:42:11 +00005661 dir_scan(&inode, "", scan1_encomp_readdir, progress);
plougher1f413c82005-11-18 00:02:14 +00005662 sBlk.root_inode = inode;
5663 sBlk.inodes = inode_count;
5664 sBlk.s_magic = SQUASHFS_MAGIC;
5665 sBlk.s_major = SQUASHFS_MAJOR;
plougher978f5882010-12-28 04:34:30 +00005666 sBlk.s_minor = SQUASHFS_MINOR;
plougher1f413c82005-11-18 00:02:14 +00005667 sBlk.block_size = block_size;
5668 sBlk.block_log = block_log;
plougher89c7a512010-12-15 08:35:51 +00005669 sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments,
plougherafae8252010-12-15 09:12:58 +00005670 always_use_fragments, duplicate_checking, exportable,
plougher3d7e5182010-12-25 01:49:42 +00005671 no_xattrs, comp_opts);
plougher1f413c82005-11-18 00:02:14 +00005672 sBlk.mkfs_time = time(NULL);
5673
Phillip Lougher83b8f862013-04-09 03:28:55 +01005674 disable_info();
plougherc9b11db2008-05-06 23:59:15 +00005675
Phillip Loughera709bff2013-03-28 17:48:31 +00005676 while((fragment = get_frag_action(fragment)))
5677 write_fragment(*fragment);
5678 unlock_fragments();
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01005679 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
Phillip Loughera709bff2013-03-28 17:48:31 +00005680 pthread_mutex_lock(&fragment_mutex);
5681 while(fragments_outstanding) {
5682 pthread_mutex_unlock(&fragment_mutex);
5683 sched_yield();
5684 pthread_mutex_lock(&fragment_mutex);
5685 }
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01005686 pthread_cleanup_pop(1);
Phillip Loughera709bff2013-03-28 17:48:31 +00005687
5688 queue_put(to_writer, NULL);
5689 if(queue_get(from_writer) != 0)
5690 EXIT_MKSQUASHFS();
5691
Phillip Loughercd1a5a62014-01-21 04:53:46 +00005692 set_progressbar_state(FALSE);
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00005693 write_filesystem_tables(&sBlk, nopad);
plougher99ac0cc2007-10-29 03:17:10 +00005694
plougher1f413c82005-11-18 00:02:14 +00005695 return 0;
5696}