blob: 956671fbf92f6fa9be3ab668e2311273146f9fa5 [file] [log] [blame]
plougher1f413c82005-11-18 00:02:14 +00001/*
plougher16111452010-07-22 05:12:18 +00002 * Create a squashfs filesystem. This is a highly compressed read only
3 * filesystem.
plougher1f413c82005-11-18 00:02:14 +00004 *
Phillip Lougher43cc4282012-01-25 00:25:56 +00005 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
Phillip Loughercc064952014-01-03 04:26:34 +00006 * 2012, 2013, 2014
Phillip Lougher83d42a32012-10-31 23:42:22 +00007 * Phillip Lougher <phillip@squashfs.org.uk>
plougher1f413c82005-11-18 00:02:14 +00008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2,
12 * or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * mksquashfs.c
24 */
25
plougher324978d2006-02-27 04:53:29 +000026#define FALSE 0
plougher1f413c82005-11-18 00:02:14 +000027#define TRUE 1
Phillip Lougherfe2c7352012-12-23 06:55:41 +000028#define MAX_LINE 16384
plougher8cb05cd2005-12-11 23:32:35 +000029
plougher1f413c82005-11-18 00:02:14 +000030#include <pwd.h>
31#include <grp.h>
32#include <time.h>
33#include <unistd.h>
34#include <stdio.h>
plougherac28cd12010-02-24 02:25:03 +000035#include <stddef.h>
plougher1f413c82005-11-18 00:02:14 +000036#include <sys/types.h>
37#include <sys/stat.h>
38#include <fcntl.h>
39#include <errno.h>
40#include <dirent.h>
41#include <string.h>
plougher1f413c82005-11-18 00:02:14 +000042#include <stdlib.h>
43#include <signal.h>
44#include <setjmp.h>
plougher02bc3bc2007-02-25 12:12:01 +000045#include <sys/types.h>
plougher1f413c82005-11-18 00:02:14 +000046#include <sys/mman.h>
plougher5507dd92006-11-06 00:43:10 +000047#include <pthread.h>
plougher8f8e1a12007-10-18 02:50:21 +000048#include <regex.h>
49#include <fnmatch.h>
plougherba674e82009-09-10 03:50:00 +000050#include <sys/wait.h>
Phillip Lougher94c1fe02012-11-28 03:32:36 +000051#include <limits.h>
Phillip Lougherb22336d2012-12-20 18:44:22 +000052#include <ctype.h>
plougher1f413c82005-11-18 00:02:14 +000053
plougher68853292005-12-27 02:47:04 +000054#ifndef linux
plougher8cb05cd2005-12-11 23:32:35 +000055#define __BYTE_ORDER BYTE_ORDER
56#define __BIG_ENDIAN BIG_ENDIAN
57#define __LITTLE_ENDIAN LITTLE_ENDIAN
plougher5507dd92006-11-06 00:43:10 +000058#include <sys/sysctl.h>
plougher8cb05cd2005-12-11 23:32:35 +000059#else
60#include <endian.h>
plougher5507dd92006-11-06 00:43:10 +000061#include <sys/sysinfo.h>
plougher8cb05cd2005-12-11 23:32:35 +000062#endif
63
plougher860c1f32010-08-11 01:37:17 +000064#include "squashfs_fs.h"
plougher860c1f32010-08-11 01:37:17 +000065#include "squashfs_swap.h"
66#include "mksquashfs.h"
67#include "sort.h"
68#include "pseudo.h"
69#include "compressor.h"
70#include "xattr.h"
Phillip Lougher4bcb7f82011-08-28 03:18:26 +010071#include "action.h"
Phillip Lougher2477d0d2012-10-24 21:49:28 +010072#include "error.h"
Phillip Lougher9a0e8bb2013-03-10 02:39:24 +000073#include "progressbar.h"
Phillip Lougher2e9fab12013-04-09 03:26:33 +010074#include "info.h"
Phillip Lougher71f39642013-04-09 04:43:21 +010075#include "caches-queues-lists.h"
Phillip Lougher1bbd0cc2013-04-19 04:03:00 +010076#include "read_fs.h"
Phillip Lougher0d67c9d2013-04-19 05:06:50 +010077#include "restore.h"
plougher860c1f32010-08-11 01:37:17 +000078
plougher324978d2006-02-27 04:53:29 +000079int delete = FALSE;
plougher1f413c82005-11-18 00:02:14 +000080int fd;
Phillip Loughera709bff2013-03-28 17:48:31 +000081struct squashfs_super_block sBlk;
plougher1f413c82005-11-18 00:02:14 +000082
83/* filesystem flags for building */
plougher3d7e5182010-12-25 01:49:42 +000084int comp_opts = FALSE;
Phillip Lougher434d50c2014-01-21 03:05:36 +000085int no_xattrs = XATTR_DEF;
86int noX = FALSE;
87int duplicate_checking = TRUE;
88int noF = FALSE;
89int no_fragments = FALSE;
90int always_use_fragments = FALSE;
91int noI = FALSE;
92int noD = FALSE;
plougher1f288f62009-02-21 03:05:52 +000093int silent = TRUE;
plougher02bc3bc2007-02-25 12:12:01 +000094int exportable = TRUE;
plougher8dcc6992007-08-17 19:32:52 +000095int sparse_files = TRUE;
plougher8f8e1a12007-10-18 02:50:21 +000096int old_exclude = TRUE;
97int use_regex = FALSE;
Phillip Loughera709bff2013-03-28 17:48:31 +000098int nopad = FALSE;
plougher1f413c82005-11-18 00:02:14 +000099
Phillip Lougher434d50c2014-01-21 03:05:36 +0000100long long global_uid = -1, global_gid = -1;
101
plougher1f413c82005-11-18 00:02:14 +0000102/* superblock attributes */
103int block_size = SQUASHFS_FILE_SIZE, block_log;
plougher1b899fc2008-08-07 01:24:06 +0000104unsigned int id_count = 0;
plougherfd57dfe2009-03-30 01:17:52 +0000105int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0,
106 sock_count = 0;
plougher1f413c82005-11-18 00:02:14 +0000107
108/* write position within data section */
109long long bytes = 0, total_bytes = 0;
110
111/* in memory directory table - possibly compressed */
112char *directory_table = NULL;
113unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0;
114
115/* cached directory table */
116char *directory_data_cache = NULL;
117unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
118
119/* in memory inode table - possibly compressed */
120char *inode_table = NULL;
121unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0;
122
123/* cached inode table */
124char *data_cache = NULL;
125unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
126
plougher0e453652006-11-06 01:49:35 +0000127/* inode lookup table */
128squashfs_inode *inode_lookup_table = NULL;
129
plougher1f413c82005-11-18 00:02:14 +0000130/* in memory directory data */
131#define I_COUNT_SIZE 128
132#define DIR_ENTRIES 32
133#define INODE_HASH_SIZE 65536
134#define INODE_HASH_MASK (INODE_HASH_SIZE - 1)
135#define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK)
136
137struct cached_dir_index {
plougher2bd2b722010-12-31 10:52:15 +0000138 struct squashfs_dir_index index;
139 char *name;
plougher1f413c82005-11-18 00:02:14 +0000140};
141
142struct directory {
143 unsigned int start_block;
144 unsigned int size;
145 unsigned char *buff;
146 unsigned char *p;
147 unsigned int entry_count;
148 unsigned char *entry_count_p;
149 unsigned int i_count;
150 unsigned int i_size;
151 struct cached_dir_index *index;
152 unsigned char *index_count_p;
153 unsigned int inode_number;
154};
155
plougher1f413c82005-11-18 00:02:14 +0000156struct inode_info *inode_info[INODE_HASH_SIZE];
157
158/* hash tables used to do fast duplicate searches in duplicate check */
plougher5507dd92006-11-06 00:43:10 +0000159struct file_info *dupl[65536];
plougher1f413c82005-11-18 00:02:14 +0000160int dup_files = 0;
161
plougher8f8e1a12007-10-18 02:50:21 +0000162/* exclude file handling */
plougher1f413c82005-11-18 00:02:14 +0000163/* list of exclude dirs/files */
164struct exclude_info {
165 dev_t st_dev;
166 ino_t st_ino;
167};
168
169#define EXCLUDE_SIZE 8192
170int exclude = 0;
171struct exclude_info *exclude_paths = NULL;
plougher8f8e1a12007-10-18 02:50:21 +0000172int old_excluded(char *filename, struct stat *buf);
173
174struct path_entry {
175 char *name;
176 regex_t *preg;
177 struct pathname *paths;
178};
179
180struct pathname {
181 int names;
182 struct path_entry *name;
183};
184
plougherf9039c92007-10-22 03:54:16 +0000185struct pathnames {
186 int count;
187 struct pathname *path[0];
188};
189#define PATHS_ALLOC_SIZE 10
190
191struct pathnames *paths = NULL;
192struct pathname *path = NULL;
plougher05e50ef2007-10-23 12:34:20 +0000193struct pathname *stickypath = NULL;
Phillip Lougherb2abb1c2013-01-09 04:51:21 +0000194int excluded(char *name, struct pathnames *paths, struct pathnames **new);
plougher1f413c82005-11-18 00:02:14 +0000195
196/* fragment block data structures */
197int fragments = 0;
plougher76c64082008-03-08 01:32:23 +0000198
plougher1f413c82005-11-18 00:02:14 +0000199struct fragment {
200 unsigned int index;
201 int offset;
202 int size;
203};
plougher76c64082008-03-08 01:32:23 +0000204
plougher1f413c82005-11-18 00:02:14 +0000205#define FRAG_SIZE 32768
Phillip Lougher45702012013-04-30 05:19:54 +0100206#define FRAG_INDEX(n) (-(n + 2))
plougher76c64082008-03-08 01:32:23 +0000207
plougher8ed84b92010-12-31 10:37:24 +0000208struct squashfs_fragment_entry *fragment_table = NULL;
plougher5507dd92006-11-06 00:43:10 +0000209int fragments_outstanding = 0;
plougher1f413c82005-11-18 00:02:14 +0000210
Phillip Lougher7ffdf2a2013-04-14 02:53:30 +0100211int fragments_locked = FALSE;
Phillip Lougher7ffdf2a2013-04-14 02:53:30 +0100212
plougher1f413c82005-11-18 00:02:14 +0000213/* current inode number for directories and non directories */
Phillip Lougher539c2b12012-07-30 20:14:52 +0100214unsigned int inode_no = 1;
plougherdf70c3e2006-01-27 09:34:13 +0000215unsigned int root_inode_number = 0;
plougher1f413c82005-11-18 00:02:14 +0000216
217/* list of source dirs/files */
218int source = 0;
219char **source_path;
220
221/* list of root directory entries read from original filesystem */
222int old_root_entries = 0;
223struct old_root_entry_info {
ploughera326c182009-08-29 05:41:45 +0000224 char *name;
225 struct inode_info inode;
plougher1f413c82005-11-18 00:02:14 +0000226};
227struct old_root_entry_info *old_root_entry;
228
229/* in memory file info */
230struct file_info {
plougher5507dd92006-11-06 00:43:10 +0000231 long long file_size;
plougherf9c72b12006-01-23 13:52:40 +0000232 long long bytes;
plougher1f413c82005-11-18 00:02:14 +0000233 unsigned short checksum;
plougher5507dd92006-11-06 00:43:10 +0000234 unsigned short fragment_checksum;
plougher1f413c82005-11-18 00:02:14 +0000235 long long start;
236 unsigned int *block_list;
237 struct file_info *next;
238 struct fragment *fragment;
plougher5507dd92006-11-06 00:43:10 +0000239 char checksum_flag;
plougher1f413c82005-11-18 00:02:14 +0000240};
241
plougherfd57dfe2009-03-30 01:17:52 +0000242/* restore orignal filesystem state if appending to existing filesystem is
243 * cancelled */
plougherca2c93f2008-08-15 08:34:57 +0000244char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed;
plougher1f413c82005-11-18 00:02:14 +0000245
246long long sbytes, stotal_bytes;
247
248unsigned int sinode_bytes, scache_bytes, sdirectory_bytes,
plougherca2c93f2008-08-15 08:34:57 +0000249 sdirectory_cache_bytes, sdirectory_compressed_bytes,
plougher1f413c82005-11-18 00:02:14 +0000250 stotal_inode_bytes, stotal_directory_bytes,
plougher0e453652006-11-06 01:49:35 +0000251 sinode_count = 0, sfile_count, ssym_count, sdev_count,
plougher1f413c82005-11-18 00:02:14 +0000252 sdir_count, sfifo_count, ssock_count, sdup_files;
253int sfragments;
plougher5507dd92006-11-06 00:43:10 +0000254int threads;
plougher1f413c82005-11-18 00:02:14 +0000255
256/* flag whether destination file is a block device */
Phillip Lougher434d50c2014-01-21 03:05:36 +0000257int block_device = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000258
259/* flag indicating whether files are sorted using sort list(s) */
Phillip Lougher434d50c2014-01-21 03:05:36 +0000260int sorted = FALSE;
plougher1f413c82005-11-18 00:02:14 +0000261
plougher324978d2006-02-27 04:53:29 +0000262/* save destination file name for deleting on error */
263char *destination_file = NULL;
264
plougher99ac0cc2007-10-29 03:17:10 +0000265/* recovery file for abnormal exit on appending */
Phillip Lougheraf4c9232012-11-15 03:46:28 +0000266char *recovery_file = NULL;
plougher99ac0cc2007-10-29 03:17:10 +0000267int recover = TRUE;
268
plougher1b899fc2008-08-07 01:24:06 +0000269/* in memory uid tables */
270#define ID_ENTRIES 256
271#define ID_HASH(id) (id & (ID_ENTRIES - 1))
272#define ISA_UID 1
273#define ISA_GID 2
274struct id {
275 unsigned int id;
276 int index;
277 char flags;
278 struct id *next;
plougher5507dd92006-11-06 00:43:10 +0000279};
plougher1b899fc2008-08-07 01:24:06 +0000280struct id *id_hash_table[ID_ENTRIES];
281struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS];
282unsigned int uid_count = 0, guid_count = 0;
283unsigned int sid_count = 0, suid_count = 0, sguid_count = 0;
plougher5507dd92006-11-06 00:43:10 +0000284
ploughereb6eac92008-02-26 01:50:48 +0000285struct cache *reader_buffer, *writer_buffer, *fragment_buffer;
Phillip Loughercf478e92013-05-29 02:38:41 +0100286struct queue *to_reader, *to_deflate, *to_writer, *from_writer,
Phillip Lougher6164b5f2013-05-23 05:05:10 +0100287 *to_frag, *locked_fragment;
Phillip Lougher0e1656d2013-05-20 03:47:24 +0100288struct seq_queue *to_main;
Phillip Lougher0280d992014-01-27 05:54:07 +0000289pthread_t reader_thread, writer_thread, main_thread;
290pthread_t *deflator_thread, *frag_deflator_thread;
Phillip Loughercf135d32013-04-08 03:43:25 +0100291pthread_t *restore_thread = NULL;
plougher5507dd92006-11-06 00:43:10 +0000292pthread_mutex_t fragment_mutex;
plougher5507dd92006-11-06 00:43:10 +0000293pthread_mutex_t pos_mutex;
294
295/* user options that control parallelisation */
296int processors = -1;
297/* default size of output buffer in Mbytes */
298#define WRITER_BUFFER_DEFAULT 512
299/* default size of input buffer in Mbytes */
300#define READER_BUFFER_DEFAULT 64
plougher76c64082008-03-08 01:32:23 +0000301/* default size of fragment buffer in Mbytes */
302#define FRAGMENT_BUFFER_DEFAULT 64
plougher5507dd92006-11-06 00:43:10 +0000303int writer_buffer_size;
plougher5507dd92006-11-06 00:43:10 +0000304
plougherc5d59872010-11-22 01:36:01 +0000305/* compression operations */
Phillip Lougher1f6c7402014-01-06 04:16:48 +0000306static struct compressor *comp = NULL;
Phillip Lougher434d50c2014-01-21 03:05:36 +0000307int compressor_opt_parsed = FALSE;
plougher13fdddf2010-11-24 01:23:41 +0000308void *stream = NULL;
plougher7b8ee502009-07-29 07:54:30 +0000309
plougher860c1f32010-08-11 01:37:17 +0000310/* xattr stats */
311unsigned int xattr_bytes = 0, total_xattr_bytes = 0;
312
plougher49b57a92009-03-31 03:23:05 +0000313char *read_from_disk(long long start, unsigned int avail_bytes);
plougherfd57dfe2009-03-30 01:17:52 +0000314void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
315 int type);
plougherfd57dfe2009-03-30 01:17:52 +0000316struct file_info *duplicate(long long file_size, long long bytes,
317 unsigned int **block_list, long long *start, struct fragment **fragment,
318 struct file_buffer *file_buffer, int blocks, unsigned short checksum,
319 unsigned short fragment_checksum, int checksum_flag);
Phillip Lougherb38c1722012-02-09 23:26:19 +0000320struct dir_info *dir_scan1(char *, char *, struct pathnames *,
Phillip Lougher494479f2012-02-03 15:45:41 +0000321 struct dir_ent *(_readdir)(struct dir_info *), int);
Phillip Lougher24eeb772012-10-13 01:56:24 +0100322void dir_scan2(struct dir_info *dir, struct pseudo *pseudo);
Phillip Lougher23d83622012-10-14 02:35:22 +0100323void dir_scan3(struct dir_info *root, struct dir_info *dir);
324void dir_scan4(struct dir_info *dir);
Phillip Lougher2abfcf72012-11-11 03:59:56 +0000325void dir_scan5(struct dir_info *dir);
326void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info);
plougherfd57dfe2009-03-30 01:17:52 +0000327struct file_info *add_non_dup(long long file_size, long long bytes,
328 unsigned int *block_list, long long start, struct fragment *fragment,
329 unsigned short checksum, unsigned short fragment_checksum,
330 int checksum_flag);
ploughera0a49c32010-08-11 01:47:59 +0000331long long generic_write_table(int, void *, int, void *, int);
plougherca61d1c2010-07-20 18:32:32 +0000332void restorefs();
Phillip Lougher1eae83d2012-09-26 02:12:45 +0100333struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth);
Phillip Loughera709bff2013-03-28 17:48:31 +0000334void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad);
plougher5507dd92006-11-06 00:43:10 +0000335
336
Phillip Lougher9b7c3c22013-03-10 02:29:42 +0000337void prep_exit()
Phillip Lougher838d2962012-10-23 03:29:30 +0100338{
Phillip Loughercf135d32013-04-08 03:43:25 +0100339 if(restore_thread) {
340 if(pthread_self() == *restore_thread) {
341 /*
342 * Recursive failure when trying to restore filesystem!
343 * Nothing to do except to exit, otherwise we'll just
344 * appear to hang. The user should be able to restore
345 * from the recovery file (which is why it was added, in
346 * case of catastrophic failure in Mksquashfs)
347 */
348 exit(1);
349 } else {
350 /* signal the restore thread to restore */
Phillip Lougher8c740812014-01-30 01:56:17 +0000351 pthread_kill(*restore_thread, SIGUSR1);
Phillip Loughercf135d32013-04-08 03:43:25 +0100352 pthread_exit(NULL);
353 }
Phillip Lougher9e071912013-04-02 05:30:29 +0100354 }
Phillip Lougher838d2962012-10-23 03:29:30 +0100355 if(delete && destination_file && !block_device)
356 unlink(destination_file);
Phillip Lougher838d2962012-10-23 03:29:30 +0100357}
358
359
Phillip Loughere1668fe2012-12-30 05:48:12 +0000360int add_overflow(int a, int b)
361{
362 return (INT_MAX - a) < b;
363}
364
365
366int shift_overflow(int a, int shift)
367{
368 return (INT_MAX >> shift) < a;
369}
370
371
372int multiply_overflow(int a, int multiplier)
373{
374 return (INT_MAX / multiplier) < a;
375}
376
377
plougher50b31762009-03-31 04:14:46 +0000378#define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \
379 + (((char *)A) - data_cache)))
plougher1f413c82005-11-18 00:02:14 +0000380
381
382void restorefs()
383{
384 ERROR("Exiting - restoring original filesystem!\n\n");
plougher5507dd92006-11-06 00:43:10 +0000385
plougher1f413c82005-11-18 00:02:14 +0000386 bytes = sbytes;
387 memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes);
plougherfd57dfe2009-03-30 01:17:52 +0000388 memcpy(directory_data_cache, sdirectory_data_cache,
389 sdirectory_cache_bytes);
390 directory_cache_bytes = sdirectory_cache_bytes;
plougher1f413c82005-11-18 00:02:14 +0000391 inode_bytes = sinode_bytes;
392 directory_bytes = sdirectory_bytes;
plougherfd57dfe2009-03-30 01:17:52 +0000393 memcpy(directory_table + directory_bytes, sdirectory_compressed,
394 sdirectory_compressed_bytes);
plougherca2c93f2008-08-15 08:34:57 +0000395 directory_bytes += sdirectory_compressed_bytes;
plougher1f413c82005-11-18 00:02:14 +0000396 total_bytes = stotal_bytes;
397 total_inode_bytes = stotal_inode_bytes;
398 total_directory_bytes = stotal_directory_bytes;
399 inode_count = sinode_count;
400 file_count = sfile_count;
401 sym_count = ssym_count;
402 dev_count = sdev_count;
403 dir_count = sdir_count;
404 fifo_count = sfifo_count;
405 sock_count = ssock_count;
406 dup_files = sdup_files;
407 fragments = sfragments;
plougher1b899fc2008-08-07 01:24:06 +0000408 id_count = sid_count;
plougher21f63b32010-07-18 03:59:04 +0000409 restore_xattrs();
Phillip Loughera709bff2013-03-28 17:48:31 +0000410 write_filesystem_tables(&sBlk, nopad);
411 exit(1);
plougher1f413c82005-11-18 00:02:14 +0000412}
413
414
Phillip Lougherb8ec5202013-03-28 20:14:37 +0000415void sighandler()
plougher324978d2006-02-27 04:53:29 +0000416{
417 EXIT_MKSQUASHFS();
418}
419
420
plougher13fdddf2010-11-24 01:23:41 +0000421int mangle2(void *strm, char *d, char *s, int size,
plougher50b31762009-03-31 04:14:46 +0000422 int block_size, int uncompressed, int data_block)
plougher5507dd92006-11-06 00:43:10 +0000423{
plougher7b8ee502009-07-29 07:54:30 +0000424 int error, c_byte = 0;
plougher5507dd92006-11-06 00:43:10 +0000425
plougher7b8ee502009-07-29 07:54:30 +0000426 if(!uncompressed) {
plougherbfb876c2010-12-31 07:48:01 +0000427 c_byte = compressor_compress(comp, strm, d, s, size, block_size,
428 &error);
plougher7b8ee502009-07-29 07:54:30 +0000429 if(c_byte == -1)
430 BAD_ERROR("mangle2:: %s compress failed with error "
431 "code %d\n", comp->name, error);
plougher1f413c82005-11-18 00:02:14 +0000432 }
433
plougher7b8ee502009-07-29 07:54:30 +0000434 if(c_byte == 0 || c_byte >= size) {
plougher1f413c82005-11-18 00:02:14 +0000435 memcpy(d, s, size);
plougherfd57dfe2009-03-30 01:17:52 +0000436 return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK :
437 SQUASHFS_COMPRESSED_BIT);
plougher1f413c82005-11-18 00:02:14 +0000438 }
439
plougher7b8ee502009-07-29 07:54:30 +0000440 return c_byte;
plougher1f413c82005-11-18 00:02:14 +0000441}
442
443
plougher7b8ee502009-07-29 07:54:30 +0000444int mangle(char *d, char *s, int size, int block_size,
plougher50b31762009-03-31 04:14:46 +0000445 int uncompressed, int data_block)
plougher5507dd92006-11-06 00:43:10 +0000446{
plougher13fdddf2010-11-24 01:23:41 +0000447 return mangle2(stream, d, s, size, block_size, uncompressed,
plougher50b31762009-03-31 04:14:46 +0000448 data_block);
plougher5507dd92006-11-06 00:43:10 +0000449}
450
451
plougherac28cd12010-02-24 02:25:03 +0000452void *get_inode(int req_size)
plougher1f413c82005-11-18 00:02:14 +0000453{
454 int data_space;
455 unsigned short c_byte;
456
457 while(cache_bytes >= SQUASHFS_METADATA_SIZE) {
plougherfd57dfe2009-03-30 01:17:52 +0000458 if((inode_size - inode_bytes) <
459 ((SQUASHFS_METADATA_SIZE << 1)) + 2) {
plougher1eb2a662010-07-26 17:30:50 +0000460 void *it = realloc(inode_table, inode_size +
plougherfd57dfe2009-03-30 01:17:52 +0000461 (SQUASHFS_METADATA_SIZE << 1) + 2);
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000462 if(it == NULL)
463 MEM_ERROR();
plougher1eb2a662010-07-26 17:30:50 +0000464 inode_table = it;
plougher1f413c82005-11-18 00:02:14 +0000465 inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
466 }
467
plougherfd57dfe2009-03-30 01:17:52 +0000468 c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET,
plougher50b31762009-03-31 04:14:46 +0000469 data_cache, SQUASHFS_METADATA_SIZE,
470 SQUASHFS_METADATA_SIZE, noI, 0);
rlougher8f7d0b82007-11-08 15:33:29 +0000471 TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte);
plougherac28cd12010-02-24 02:25:03 +0000472 SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1);
plougher1b899fc2008-08-07 01:24:06 +0000473 inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET;
474 total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET;
plougher14dd5f32010-08-02 18:20:03 +0000475 memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE,
plougherfd57dfe2009-03-30 01:17:52 +0000476 cache_bytes - SQUASHFS_METADATA_SIZE);
plougher1f413c82005-11-18 00:02:14 +0000477 cache_bytes -= SQUASHFS_METADATA_SIZE;
478 }
479
480 data_space = (cache_size - cache_bytes);
481 if(data_space < req_size) {
plougherfd57dfe2009-03-30 01:17:52 +0000482 int realloc_size = cache_size == 0 ?
483 ((req_size + SQUASHFS_METADATA_SIZE) &
484 ~(SQUASHFS_METADATA_SIZE - 1)) : req_size -
485 data_space;
plougher1f413c82005-11-18 00:02:14 +0000486
plougher17248ca2010-07-27 00:24:35 +0000487 void *dc = realloc(data_cache, cache_size +
plougherfd57dfe2009-03-30 01:17:52 +0000488 realloc_size);
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000489 if(dc == NULL)
490 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +0000491 cache_size += realloc_size;
plougher17248ca2010-07-27 00:24:35 +0000492 data_cache = dc;
plougher1f413c82005-11-18 00:02:14 +0000493 }
494
495 cache_bytes += req_size;
496
plougherac28cd12010-02-24 02:25:03 +0000497 return data_cache + cache_bytes - req_size;
plougher1f413c82005-11-18 00:02:14 +0000498}
499
500
plougher8a8c4102009-03-29 22:28:49 +0000501int read_bytes(int fd, void *buff, int bytes)
plougher06a19d32009-03-29 22:00:03 +0000502{
503 int res, count;
504
505 for(count = 0; count < bytes; count += res) {
506 res = read(fd, buff + count, bytes - count);
507 if(res < 1) {
plougher96f85a12009-03-29 22:39:59 +0000508 if(res == 0)
509 goto bytes_read;
510 else if(errno != EINTR) {
plougher06a19d32009-03-29 22:00:03 +0000511 ERROR("Read failed because %s\n",
512 strerror(errno));
513 return -1;
514 } else
515 res = 0;
516 }
517 }
518
519bytes_read:
520 return count;
521}
522
523
plougher3306cb22010-06-18 04:22:44 +0000524int read_fs_bytes(int fd, long long byte, int bytes, void *buff)
plougher1f413c82005-11-18 00:02:14 +0000525{
526 off_t off = byte;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100527 int res = 1;
plougher1f413c82005-11-18 00:02:14 +0000528
plougher1d065e92010-06-18 03:58:27 +0000529 TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n",
plougherfd57dfe2009-03-30 01:17:52 +0000530 byte, bytes);
plougher06a19d32009-03-29 22:00:03 +0000531
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100532 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
plougher5507dd92006-11-06 00:43:10 +0000533 pthread_mutex_lock(&pos_mutex);
plougher1d065e92010-06-18 03:58:27 +0000534 if(lseek(fd, off, SEEK_SET) == -1) {
Phillip Lougher1d3177a2013-02-06 21:48:58 +0000535 ERROR("read_fs_bytes: Lseek on destination failed because %s, "
536 "offset=0x%llx\n", strerror(errno), off);
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100537 res = 0;
Phillip Lougher041cf6d2013-07-20 04:20:40 +0100538 } else if(read_bytes(fd, buff, bytes) < bytes) {
plougher1d065e92010-06-18 03:58:27 +0000539 ERROR("Read on destination failed\n");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100540 res = 0;
plougher1d065e92010-06-18 03:58:27 +0000541 }
542
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100543 pthread_cleanup_pop(1);
544 return res;
plougher1f413c82005-11-18 00:02:14 +0000545}
546
547
plougher628e7682009-03-29 22:12:24 +0000548int write_bytes(int fd, void *buff, int bytes)
plougher0dd6f122009-03-29 21:43:57 +0000549{
550 int res, count;
551
552 for(count = 0; count < bytes; count += res) {
553 res = write(fd, buff + count, bytes - count);
554 if(res == -1) {
555 if(errno != EINTR) {
556 ERROR("Write failed because %s\n",
557 strerror(errno));
558 return -1;
559 }
560 res = 0;
561 }
562 }
563
564 return 0;
565}
566
567
plougher29e2ace2010-12-31 08:50:00 +0000568void write_destination(int fd, long long byte, int bytes, void *buff)
plougher1f413c82005-11-18 00:02:14 +0000569{
570 off_t off = byte;
plougher1f413c82005-11-18 00:02:14 +0000571
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100572 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
573 pthread_mutex_lock(&pos_mutex);
plougher5507dd92006-11-06 00:43:10 +0000574
Phillip Loughera0c23462013-02-12 03:30:12 +0000575 if(lseek(fd, off, SEEK_SET) == -1) {
576 ERROR("write_destination: Lseek on destination "
Phillip Lougher1d3177a2013-02-06 21:48:58 +0000577 "failed because %s, offset=0x%llx\n", strerror(errno),
578 off);
Phillip Loughera0c23462013-02-12 03:30:12 +0000579 BAD_ERROR("Probably out of space on output %s\n",
580 block_device ? "block device" : "filesystem");
581 }
plougher1f413c82005-11-18 00:02:14 +0000582
plougher0dd6f122009-03-29 21:43:57 +0000583 if(write_bytes(fd, buff, bytes) == -1)
Phillip Loughere8b536f2013-02-12 22:01:21 +0000584 BAD_ERROR("Failed to write to output %s\n",
585 block_device ? "block device" : "filesystem");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +0100586
587 pthread_cleanup_pop(1);
plougher1f413c82005-11-18 00:02:14 +0000588}
589
590
591long long write_inodes()
592{
593 unsigned short c_byte;
594 int avail_bytes;
595 char *datap = data_cache;
596 long long start_bytes = bytes;
597
598 while(cache_bytes) {
plougherfd57dfe2009-03-30 01:17:52 +0000599 if(inode_size - inode_bytes <
600 ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
plougher1eb2a662010-07-26 17:30:50 +0000601 void *it = realloc(inode_table, inode_size +
plougherfd57dfe2009-03-30 01:17:52 +0000602 ((SQUASHFS_METADATA_SIZE << 1) + 2));
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000603 if(it == NULL)
604 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +0000605 inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
plougher1eb2a662010-07-26 17:30:50 +0000606 inode_table = it;
plougher1f413c82005-11-18 00:02:14 +0000607 }
plougherfd57dfe2009-03-30 01:17:52 +0000608 avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ?
609 SQUASHFS_METADATA_SIZE : cache_bytes;
610 c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap,
611 avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0);
rlougher8f7d0b82007-11-08 15:33:29 +0000612 TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte);
plougherac28cd12010-02-24 02:25:03 +0000613 SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1);
plougher1b899fc2008-08-07 01:24:06 +0000614 inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET;
615 total_inode_bytes += avail_bytes + BLOCK_OFFSET;
plougher1f413c82005-11-18 00:02:14 +0000616 datap += avail_bytes;
617 cache_bytes -= avail_bytes;
618 }
619
plougher29e2ace2010-12-31 08:50:00 +0000620 write_destination(fd, bytes, inode_bytes, inode_table);
plougher1f413c82005-11-18 00:02:14 +0000621 bytes += inode_bytes;
622
623 return start_bytes;
624}
625
626
627long long write_directories()
628{
629 unsigned short c_byte;
630 int avail_bytes;
631 char *directoryp = directory_data_cache;
632 long long start_bytes = bytes;
633
634 while(directory_cache_bytes) {
plougherfd57dfe2009-03-30 01:17:52 +0000635 if(directory_size - directory_bytes <
636 ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
plougher79d665a2010-07-27 00:19:23 +0000637 void *dt = realloc(directory_table,
plougherfd57dfe2009-03-30 01:17:52 +0000638 directory_size + ((SQUASHFS_METADATA_SIZE << 1)
639 + 2));
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000640 if(dt == NULL)
641 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +0000642 directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
plougher79d665a2010-07-27 00:19:23 +0000643 directory_table = dt;
plougher1f413c82005-11-18 00:02:14 +0000644 }
plougherfd57dfe2009-03-30 01:17:52 +0000645 avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ?
646 SQUASHFS_METADATA_SIZE : directory_cache_bytes;
plougher50b31762009-03-31 04:14:46 +0000647 c_byte = mangle(directory_table + directory_bytes +
648 BLOCK_OFFSET, directoryp, avail_bytes,
649 SQUASHFS_METADATA_SIZE, noI, 0);
plougherfd57dfe2009-03-30 01:17:52 +0000650 TRACE("Directory block @ 0x%x, size %d\n", directory_bytes,
651 c_byte);
plougherac28cd12010-02-24 02:25:03 +0000652 SQUASHFS_SWAP_SHORTS(&c_byte,
653 directory_table + directory_bytes, 1);
plougher50b31762009-03-31 04:14:46 +0000654 directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) +
655 BLOCK_OFFSET;
plougher1b899fc2008-08-07 01:24:06 +0000656 total_directory_bytes += avail_bytes + BLOCK_OFFSET;
plougher1f413c82005-11-18 00:02:14 +0000657 directoryp += avail_bytes;
658 directory_cache_bytes -= avail_bytes;
659 }
plougher29e2ace2010-12-31 08:50:00 +0000660 write_destination(fd, bytes, directory_bytes, directory_table);
plougher1f413c82005-11-18 00:02:14 +0000661 bytes += directory_bytes;
662
663 return start_bytes;
664}
665
666
plougher1b899fc2008-08-07 01:24:06 +0000667long long write_id_table()
plougher1f413c82005-11-18 00:02:14 +0000668{
plougher1b899fc2008-08-07 01:24:06 +0000669 unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count);
plougherac28cd12010-02-24 02:25:03 +0000670 unsigned int p[id_count];
plougher1f413c82005-11-18 00:02:14 +0000671 int i;
672
plougher1b899fc2008-08-07 01:24:06 +0000673 TRACE("write_id_table: ids %d, id_bytes %d\n", id_count, id_bytes);
plougherac28cd12010-02-24 02:25:03 +0000674 for(i = 0; i < id_count; i++) {
plougher1b899fc2008-08-07 01:24:06 +0000675 TRACE("write_id_table: id index %d, id %d", i, id_table[i]->id);
plougherac28cd12010-02-24 02:25:03 +0000676 SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1);
plougher1f413c82005-11-18 00:02:14 +0000677 }
678
ploughera0a49c32010-08-11 01:47:59 +0000679 return generic_write_table(id_bytes, p, 0, NULL, noI);
plougher1f413c82005-11-18 00:02:14 +0000680}
681
682
plougher1b899fc2008-08-07 01:24:06 +0000683struct id *get_id(unsigned int id)
plougher1f413c82005-11-18 00:02:14 +0000684{
plougher1b899fc2008-08-07 01:24:06 +0000685 int hash = ID_HASH(id);
686 struct id *entry = id_hash_table[hash];
plougher1f413c82005-11-18 00:02:14 +0000687
plougher1b899fc2008-08-07 01:24:06 +0000688 for(; entry; entry = entry->next)
689 if(entry->id == id)
690 break;
plougher1f413c82005-11-18 00:02:14 +0000691
plougher1b899fc2008-08-07 01:24:06 +0000692 return entry;
plougher1f413c82005-11-18 00:02:14 +0000693}
694
695
plougher1b899fc2008-08-07 01:24:06 +0000696struct id *create_id(unsigned int id)
697{
698 int hash = ID_HASH(id);
699 struct id *entry = malloc(sizeof(struct id));
700 if(entry == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000701 MEM_ERROR();
plougher1b899fc2008-08-07 01:24:06 +0000702 entry->id = id;
703 entry->index = id_count ++;
704 entry->flags = 0;
705 entry->next = id_hash_table[hash];
706 id_hash_table[hash] = entry;
707 id_table[entry->index] = entry;
708 return entry;
709}
710
711
712unsigned int get_uid(unsigned int uid)
713{
714 struct id *entry = get_id(uid);
715
716 if(entry == NULL) {
717 if(id_count == SQUASHFS_IDS)
718 BAD_ERROR("Out of uids!\n");
719 entry = create_id(uid);
720 }
721
722 if((entry->flags & ISA_UID) == 0) {
723 entry->flags |= ISA_UID;
724 uid_count ++;
725 }
726
727 return entry->index;
728}
729
730
731unsigned int get_guid(unsigned int guid)
732{
733 struct id *entry = get_id(guid);
734
735 if(entry == NULL) {
736 if(id_count == SQUASHFS_IDS)
737 BAD_ERROR("Out of gids!\n");
738 entry = create_id(guid);
739 }
740
741 if((entry->flags & ISA_GID) == 0) {
742 entry->flags |= ISA_GID;
743 guid_count ++;
744 }
745
746 return entry->index;
747}
748
749
Phillip Lougher5ef51692012-11-19 03:35:39 +0000750#define ALLOC_SIZE 128
Phillip Lougher494479f2012-02-03 15:45:41 +0000751
Phillip Lougher5ef51692012-11-19 03:35:39 +0000752char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size)
753{
754 if(pathname == NULL) {
755 pathname = malloc(ALLOC_SIZE);
756 if(pathname == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000757 MEM_ERROR();
Phillip Lougher5ef51692012-11-19 03:35:39 +0000758 }
759
760 for(;;) {
761 int res = snprintf(pathname, *size, "%s/%s",
762 dir_ent->our_dir->pathname,
763 dir_ent->source_name ? : dir_ent->name);
764
765 if(res < 0)
766 BAD_ERROR("snprintf failed in pathname\n");
767 else if(res >= *size) {
768 /*
769 * pathname is too small to contain the result, so
770 * increase it and try again
771 */
772 *size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1);
773 pathname = realloc(pathname, *size);
774 if(pathname == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000775 MEM_ERROR();
Phillip Lougher5ef51692012-11-19 03:35:39 +0000776 } else
777 break;
778 }
Phillip Lougher494479f2012-02-03 15:45:41 +0000779
780 return pathname;
781}
782
783
784char *pathname(struct dir_ent *dir_ent)
785{
Phillip Lougher5ef51692012-11-19 03:35:39 +0000786 static char *pathname = NULL;
787 static int size = ALLOC_SIZE;
Phillip Lougher494479f2012-02-03 15:45:41 +0000788
Phillip Lougher5ef51692012-11-19 03:35:39 +0000789 if (dir_ent->nonstandard_pathname)
790 return dir_ent->nonstandard_pathname;
791
792 return pathname = _pathname(dir_ent, pathname, &size);
Phillip Lougher494479f2012-02-03 15:45:41 +0000793}
794
795
796char *pathname_reader(struct dir_ent *dir_ent)
797{
Phillip Lougher5ef51692012-11-19 03:35:39 +0000798 static char *pathname = NULL;
799 static int size = ALLOC_SIZE;
Phillip Lougher494479f2012-02-03 15:45:41 +0000800
Phillip Lougher5ef51692012-11-19 03:35:39 +0000801 if (dir_ent->nonstandard_pathname)
802 return dir_ent->nonstandard_pathname;
803
804 return pathname = _pathname(dir_ent, pathname, &size);
Phillip Lougher494479f2012-02-03 15:45:41 +0000805}
806
807
Phillip Lougherb38c1722012-02-09 23:26:19 +0000808char *subpathname(struct dir_ent *dir_ent)
809{
Phillip Lougherd457ed32012-11-19 01:36:56 +0000810 static char *subpath = NULL;
811 static int size = ALLOC_SIZE;
812 int res;
Phillip Lougherb38c1722012-02-09 23:26:19 +0000813
Phillip Lougherd457ed32012-11-19 01:36:56 +0000814 if(subpath == NULL) {
815 subpath = malloc(ALLOC_SIZE);
816 if(subpath == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000817 MEM_ERROR();
Phillip Lougherd457ed32012-11-19 01:36:56 +0000818 }
819
820 for(;;) {
821 if(dir_ent->our_dir->subpath[0] != '\0')
822 res = snprintf(subpath, size, "%s/%s",
823 dir_ent->our_dir->subpath, dir_ent->name);
824 else
825 res = snprintf(subpath, size, "/%s", dir_ent->name);
826
827 if(res < 0)
828 BAD_ERROR("snprintf failed in subpathname\n");
829 else if(res >= size) {
830 /*
831 * subpath is too small to contain the result, so
832 * increase it and try again
833 */
834 size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1);
835 subpath = realloc(subpath, size);
836 if(subpath == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +0000837 MEM_ERROR();
Phillip Lougherd457ed32012-11-19 01:36:56 +0000838 } else
839 break;
Phillip Lougher90e08252012-10-05 04:33:35 +0100840 }
Phillip Lougherb38c1722012-02-09 23:26:19 +0000841
842 return subpath;
843}
844
845
Phillip Lougher539c2b12012-07-30 20:14:52 +0100846inline unsigned int get_inode_no(struct inode_info *inode)
Phillip Lougher53cef742012-07-25 05:12:50 +0100847{
Phillip Lougher539c2b12012-07-30 20:14:52 +0100848 return inode->inode_number;
Phillip Lougher53cef742012-07-25 05:12:50 +0100849}
850
851
Phillip Lougher539c2b12012-07-30 20:14:52 +0100852inline unsigned int get_parent_no(struct dir_info *dir)
Phillip Lougher53cef742012-07-25 05:12:50 +0100853{
Phillip Lougher1eae83d2012-09-26 02:12:45 +0100854 return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no;
Phillip Lougher53cef742012-07-25 05:12:50 +0100855}
856
857
plougher3c6bdb52010-05-01 02:30:59 +0000858int create_inode(squashfs_inode *i_no, struct dir_info *dir_info,
859 struct dir_ent *dir_ent, int type, long long byte_size,
860 long long start_block, unsigned int offset, unsigned int *block_list,
861 struct fragment *fragment, struct directory *dir_in, long long sparse)
plougher1f413c82005-11-18 00:02:14 +0000862{
863 struct stat *buf = &dir_ent->inode->buf;
plougher8973a122010-12-31 09:52:45 +0000864 union squashfs_inode_header inode_header;
plougher857d0dd2010-12-31 21:03:13 +0000865 struct squashfs_base_inode_header *base = &inode_header.base;
plougherac28cd12010-02-24 02:25:03 +0000866 void *inode;
Phillip Lougher494479f2012-02-03 15:45:41 +0000867 char *filename = pathname(dir_ent);
plougher1f413c82005-11-18 00:02:14 +0000868 int nlink = dir_ent->inode->nlink;
ploughere6e0e1b2010-05-12 17:17:06 +0000869 int xattr = read_xattrs(dir_ent);
plougher1f413c82005-11-18 00:02:14 +0000870
ploughere6e0e1b2010-05-12 17:17:06 +0000871 switch(type) {
872 case SQUASHFS_FILE_TYPE:
873 if(dir_ent->inode->nlink > 1 ||
874 byte_size >= (1LL << 32) ||
875 start_block >= (1LL << 32) ||
876 sparse || IS_XATTR(xattr))
877 type = SQUASHFS_LREG_TYPE;
878 break;
879 case SQUASHFS_DIR_TYPE:
880 if(dir_info->dir_is_ldir || IS_XATTR(xattr))
881 type = SQUASHFS_LDIR_TYPE;
882 break;
883 case SQUASHFS_SYMLINK_TYPE:
884 if(IS_XATTR(xattr))
885 type = SQUASHFS_LSYMLINK_TYPE;
886 break;
887 case SQUASHFS_BLKDEV_TYPE:
888 if(IS_XATTR(xattr))
889 type = SQUASHFS_LBLKDEV_TYPE;
890 break;
891 case SQUASHFS_CHRDEV_TYPE:
892 if(IS_XATTR(xattr))
893 type = SQUASHFS_LCHRDEV_TYPE;
894 break;
plougherc5d69322010-05-12 19:28:38 +0000895 case SQUASHFS_FIFO_TYPE:
896 if(IS_XATTR(xattr))
897 type = SQUASHFS_LFIFO_TYPE;
898 break;
899 case SQUASHFS_SOCKET_TYPE:
900 if(IS_XATTR(xattr))
901 type = SQUASHFS_LSOCKET_TYPE;
902 break;
ploughere6e0e1b2010-05-12 17:17:06 +0000903 }
904
plougher1f413c82005-11-18 00:02:14 +0000905 base->mode = SQUASHFS_MODE(buf->st_mode);
plougherfd57dfe2009-03-30 01:17:52 +0000906 base->uid = get_uid((unsigned int) global_uid == -1 ?
907 buf->st_uid : global_uid);
plougher1f413c82005-11-18 00:02:14 +0000908 base->inode_type = type;
plougherfd57dfe2009-03-30 01:17:52 +0000909 base->guid = get_guid((unsigned int) global_gid == -1 ?
910 buf->st_gid : global_gid);
plougher1f413c82005-11-18 00:02:14 +0000911 base->mtime = buf->st_mtime;
Phillip Lougher539c2b12012-07-30 20:14:52 +0100912 base->inode_number = get_inode_no(dir_ent->inode);
plougher1f413c82005-11-18 00:02:14 +0000913
914 if(type == SQUASHFS_FILE_TYPE) {
915 int i;
plougher8701ed62010-12-31 20:38:38 +0000916 struct squashfs_reg_inode_header *reg = &inode_header.reg;
917 size_t off = offsetof(struct squashfs_reg_inode_header, block_list);
plougher1f413c82005-11-18 00:02:14 +0000918
919 inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
plougher1f413c82005-11-18 00:02:14 +0000920 reg->file_size = byte_size;
921 reg->start_block = start_block;
922 reg->fragment = fragment->index;
923 reg->offset = fragment->offset;
plougherac28cd12010-02-24 02:25:03 +0000924 SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode);
925 SQUASHFS_SWAP_INTS(block_list, inode + off, offset);
plougher50b31762009-03-31 04:14:46 +0000926 TRACE("File inode, file_size %lld, start_block 0x%llx, blocks "
927 "%d, fragment %d, offset %d, size %d\n", byte_size,
plougherfd57dfe2009-03-30 01:17:52 +0000928 start_block, offset, fragment->index, fragment->offset,
929 fragment->size);
plougher1f413c82005-11-18 00:02:14 +0000930 for(i = 0; i < offset; i++)
931 TRACE("Block %d, size %d\n", i, block_list[i]);
932 }
933 else if(type == SQUASHFS_LREG_TYPE) {
934 int i;
plougher1e6ac4a2010-12-31 20:42:02 +0000935 struct squashfs_lreg_inode_header *reg = &inode_header.lreg;
936 size_t off = offsetof(struct squashfs_lreg_inode_header, block_list);
plougher1f413c82005-11-18 00:02:14 +0000937
938 inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int));
plougher1f413c82005-11-18 00:02:14 +0000939 reg->nlink = nlink;
940 reg->file_size = byte_size;
941 reg->start_block = start_block;
942 reg->fragment = fragment->index;
943 reg->offset = fragment->offset;
plougherf5a674d2009-03-25 05:38:27 +0000944 if(sparse && sparse >= byte_size)
945 sparse = byte_size - 1;
plougher1b899fc2008-08-07 01:24:06 +0000946 reg->sparse = sparse;
ploughere6e0e1b2010-05-12 17:17:06 +0000947 reg->xattr = xattr;
plougherac28cd12010-02-24 02:25:03 +0000948 SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode);
949 SQUASHFS_SWAP_INTS(block_list, inode + off, offset);
plougherfd57dfe2009-03-30 01:17:52 +0000950 TRACE("Long file inode, file_size %lld, start_block 0x%llx, "
plougher50b31762009-03-31 04:14:46 +0000951 "blocks %d, fragment %d, offset %d, size %d, nlink %d"
952 "\n", byte_size, start_block, offset, fragment->index,
plougherfd57dfe2009-03-30 01:17:52 +0000953 fragment->offset, fragment->size, nlink);
plougher1f413c82005-11-18 00:02:14 +0000954 for(i = 0; i < offset; i++)
955 TRACE("Block %d, size %d\n", i, block_list[i]);
956 }
957 else if(type == SQUASHFS_LDIR_TYPE) {
958 int i;
959 unsigned char *p;
plougher2611d392010-12-31 20:50:36 +0000960 struct squashfs_ldir_inode_header *dir = &inode_header.ldir;
plougher1f413c82005-11-18 00:02:14 +0000961 struct cached_dir_index *index = dir_in->index;
962 unsigned int i_count = dir_in->i_count;
963 unsigned int i_size = dir_in->i_size;
964
965 if(byte_size >= 1 << 27)
966 BAD_ERROR("directory greater than 2^27-1 bytes!\n");
967
968 inode = get_inode(sizeof(*dir) + i_size);
plougher1f413c82005-11-18 00:02:14 +0000969 dir->inode_type = SQUASHFS_LDIR_TYPE;
plougher1f413c82005-11-18 00:02:14 +0000970 dir->nlink = dir_ent->dir->directory_count + 2;
971 dir->file_size = byte_size;
972 dir->offset = offset;
973 dir->start_block = start_block;
974 dir->i_count = i_count;
Phillip Lougher539c2b12012-07-30 20:14:52 +0100975 dir->parent_inode = get_parent_no(dir_ent->our_dir);
ploughere6e0e1b2010-05-12 17:17:06 +0000976 dir->xattr = xattr;
plougher1f413c82005-11-18 00:02:14 +0000977
plougherac28cd12010-02-24 02:25:03 +0000978 SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode);
plougher2611d392010-12-31 20:50:36 +0000979 p = inode + offsetof(struct squashfs_ldir_inode_header, index);
plougher1f413c82005-11-18 00:02:14 +0000980 for(i = 0; i < i_count; i++) {
plougherac28cd12010-02-24 02:25:03 +0000981 SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p);
plougher2bd2b722010-12-31 10:52:15 +0000982 p += offsetof(struct squashfs_dir_index, name);
plougherac28cd12010-02-24 02:25:03 +0000983 memcpy(p, index[i].name, index[i].index.size + 1);
984 p += index[i].index.size + 1;
plougher1f413c82005-11-18 00:02:14 +0000985 }
plougher50b31762009-03-31 04:14:46 +0000986 TRACE("Long directory inode, file_size %lld, start_block "
987 "0x%llx, offset 0x%x, nlink %d\n", byte_size,
988 start_block, offset, dir_ent->dir->directory_count + 2);
plougher1f413c82005-11-18 00:02:14 +0000989 }
990 else if(type == SQUASHFS_DIR_TYPE) {
plougher9d80a602010-12-31 20:47:24 +0000991 struct squashfs_dir_inode_header *dir = &inode_header.dir;
plougher1f413c82005-11-18 00:02:14 +0000992
993 inode = get_inode(sizeof(*dir));
plougher1f413c82005-11-18 00:02:14 +0000994 dir->nlink = dir_ent->dir->directory_count + 2;
995 dir->file_size = byte_size;
996 dir->offset = offset;
997 dir->start_block = start_block;
Phillip Lougher539c2b12012-07-30 20:14:52 +0100998 dir->parent_inode = get_parent_no(dir_ent->our_dir);
plougherac28cd12010-02-24 02:25:03 +0000999 SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode);
plougherfd57dfe2009-03-30 01:17:52 +00001000 TRACE("Directory inode, file_size %lld, start_block 0x%llx, "
plougher50b31762009-03-31 04:14:46 +00001001 "offset 0x%x, nlink %d\n", byte_size, start_block,
1002 offset, dir_ent->dir->directory_count + 2);
plougher1f413c82005-11-18 00:02:14 +00001003 }
1004 else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) {
plougherc70c6332010-12-31 20:11:09 +00001005 struct squashfs_dev_inode_header *dev = &inode_header.dev;
plougher5b398502008-10-04 23:22:03 +00001006 unsigned int major = major(buf->st_rdev);
1007 unsigned int minor = minor(buf->st_rdev);
plougher1f413c82005-11-18 00:02:14 +00001008
plougher5b398502008-10-04 23:22:03 +00001009 if(major > 0xfff) {
plougherfd57dfe2009-03-30 01:17:52 +00001010 ERROR("Major %d out of range in device node %s, "
1011 "truncating to %d\n", major, filename,
1012 major & 0xfff);
plougher5b398502008-10-04 23:22:03 +00001013 major &= 0xfff;
1014 }
1015 if(minor > 0xfffff) {
plougherfd57dfe2009-03-30 01:17:52 +00001016 ERROR("Minor %d out of range in device node %s, "
1017 "truncating to %d\n", minor, filename,
1018 minor & 0xfffff);
plougher5b398502008-10-04 23:22:03 +00001019 minor &= 0xfffff;
1020 }
plougher1f413c82005-11-18 00:02:14 +00001021 inode = get_inode(sizeof(*dev));
1022 dev->nlink = nlink;
plougher5b398502008-10-04 23:22:03 +00001023 dev->rdev = (major << 8) | (minor & 0xff) |
1024 ((minor & ~0xff) << 12);
plougherac28cd12010-02-24 02:25:03 +00001025 SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode);
rlougher8f7d0b82007-11-08 15:33:29 +00001026 TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink);
plougher1f413c82005-11-18 00:02:14 +00001027 }
ploughere6e0e1b2010-05-12 17:17:06 +00001028 else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) {
plougher0b4ee5b2010-12-31 20:14:00 +00001029 struct squashfs_ldev_inode_header *dev = &inode_header.ldev;
ploughere6e0e1b2010-05-12 17:17:06 +00001030 unsigned int major = major(buf->st_rdev);
1031 unsigned int minor = minor(buf->st_rdev);
1032
1033 if(major > 0xfff) {
1034 ERROR("Major %d out of range in device node %s, "
1035 "truncating to %d\n", major, filename,
1036 major & 0xfff);
1037 major &= 0xfff;
1038 }
1039 if(minor > 0xfffff) {
1040 ERROR("Minor %d out of range in device node %s, "
1041 "truncating to %d\n", minor, filename,
1042 minor & 0xfffff);
1043 minor &= 0xfffff;
1044 }
1045 inode = get_inode(sizeof(*dev));
1046 dev->nlink = nlink;
1047 dev->rdev = (major << 8) | (minor & 0xff) |
1048 ((minor & ~0xff) << 12);
1049 dev->xattr = xattr;
1050 SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode);
1051 TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink);
1052 }
plougher1f413c82005-11-18 00:02:14 +00001053 else if(type == SQUASHFS_SYMLINK_TYPE) {
plougher5ae6e952010-12-31 20:44:34 +00001054 struct squashfs_symlink_inode_header *symlink = &inode_header.symlink;
plougher1f413c82005-11-18 00:02:14 +00001055 int byte;
Phillip Lougher1df6c112012-12-12 05:21:42 +00001056 char buff[65536]; /* overflow safe */
plougher5ae6e952010-12-31 20:44:34 +00001057 size_t off = offsetof(struct squashfs_symlink_inode_header, symlink);
plougher1f413c82005-11-18 00:02:14 +00001058
plougher5cf38b82010-12-16 04:55:32 +00001059 byte = readlink(filename, buff, 65536);
1060 if(byte == -1) {
plougherfd57dfe2009-03-30 01:17:52 +00001061 ERROR("Failed to read symlink %s, creating empty "
1062 "symlink\n", filename);
plougher29e37092007-04-15 01:24:51 +00001063 byte = 0;
plougher1f413c82005-11-18 00:02:14 +00001064 }
1065
1066 if(byte == 65536) {
plougher50b31762009-03-31 04:14:46 +00001067 ERROR("Symlink %s is greater than 65536 bytes! "
1068 "Creating empty symlink\n", filename);
plougher29e37092007-04-15 01:24:51 +00001069 byte = 0;
plougher1f413c82005-11-18 00:02:14 +00001070 }
1071
1072 inode = get_inode(sizeof(*symlink) + byte);
1073 symlink->nlink = nlink;
plougher1f413c82005-11-18 00:02:14 +00001074 symlink->symlink_size = byte;
plougherac28cd12010-02-24 02:25:03 +00001075 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
1076 strncpy(inode + off, buff, byte);
plougherfd57dfe2009-03-30 01:17:52 +00001077 TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte,
1078 nlink);
plougher1f413c82005-11-18 00:02:14 +00001079 }
ploughere6e0e1b2010-05-12 17:17:06 +00001080 else if(type == SQUASHFS_LSYMLINK_TYPE) {
plougher5ae6e952010-12-31 20:44:34 +00001081 struct squashfs_symlink_inode_header *symlink = &inode_header.symlink;
ploughere6e0e1b2010-05-12 17:17:06 +00001082 int byte;
Phillip Lougher1df6c112012-12-12 05:21:42 +00001083 char buff[65536]; /* overflow safe */
plougher5ae6e952010-12-31 20:44:34 +00001084 size_t off = offsetof(struct squashfs_symlink_inode_header, symlink);
ploughere6e0e1b2010-05-12 17:17:06 +00001085
plougherdf2b9aa2010-12-16 04:56:23 +00001086 byte = readlink(filename, buff, 65536);
1087 if(byte == -1) {
ploughere6e0e1b2010-05-12 17:17:06 +00001088 ERROR("Failed to read symlink %s, creating empty "
1089 "symlink\n", filename);
1090 byte = 0;
1091 }
1092
1093 if(byte == 65536) {
1094 ERROR("Symlink %s is greater than 65536 bytes! "
1095 "Creating empty symlink\n", filename);
1096 byte = 0;
1097 }
1098
1099 inode = get_inode(sizeof(*symlink) + byte +
1100 sizeof(unsigned int));
1101 symlink->nlink = nlink;
1102 symlink->symlink_size = byte;
1103 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode);
1104 strncpy(inode + off, buff, byte);
1105 SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1);
1106 TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte,
1107 nlink);
1108 }
plougher1f413c82005-11-18 00:02:14 +00001109 else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) {
ploughere56b9862010-12-31 10:58:35 +00001110 struct squashfs_ipc_inode_header *ipc = &inode_header.ipc;
plougher1f413c82005-11-18 00:02:14 +00001111
1112 inode = get_inode(sizeof(*ipc));
1113 ipc->nlink = nlink;
plougherac28cd12010-02-24 02:25:03 +00001114 SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode);
plougher50b31762009-03-31 04:14:46 +00001115 TRACE("ipc inode, type %s, nlink %d\n", type ==
1116 SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink);
plougherc5d69322010-05-12 19:28:38 +00001117 }
1118 else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) {
plougheraa0d1222010-12-31 20:06:24 +00001119 struct squashfs_lipc_inode_header *ipc = &inode_header.lipc;
plougherc5d69322010-05-12 19:28:38 +00001120
1121 inode = get_inode(sizeof(*ipc));
1122 ipc->nlink = nlink;
1123 ipc->xattr = xattr;
1124 SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode);
1125 TRACE("ipc inode, type %s, nlink %d\n", type ==
1126 SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink);
plougher1f413c82005-11-18 00:02:14 +00001127 } else
rlougher8f7d0b82007-11-08 15:33:29 +00001128 BAD_ERROR("Unrecognised inode %d in create_inode\n", type);
plougher1f413c82005-11-18 00:02:14 +00001129
1130 *i_no = MKINODE(inode);
1131 inode_count ++;
1132
plougherfd57dfe2009-03-30 01:17:52 +00001133 TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type,
1134 base->uid, base->guid);
plougher1f413c82005-11-18 00:02:14 +00001135
1136 return TRUE;
1137}
1138
1139
plougher50b31762009-03-31 04:14:46 +00001140void add_dir(squashfs_inode inode, unsigned int inode_number, char *name,
1141 int type, struct directory *dir)
plougher1f413c82005-11-18 00:02:14 +00001142{
plougher8cb05cd2005-12-11 23:32:35 +00001143 unsigned char *buff;
plougher9b393552010-12-31 20:55:43 +00001144 struct squashfs_dir_entry idir;
plougher1f413c82005-11-18 00:02:14 +00001145 unsigned int start_block = inode >> 16;
1146 unsigned int offset = inode & 0xffff;
plougher43d49082010-12-16 05:01:15 +00001147 unsigned int size = strlen(name);
plougher9b393552010-12-31 20:55:43 +00001148 size_t name_off = offsetof(struct squashfs_dir_entry, name);
plougher1f413c82005-11-18 00:02:14 +00001149
plougher43d49082010-12-16 05:01:15 +00001150 if(size > SQUASHFS_NAME_LEN) {
plougher1f413c82005-11-18 00:02:14 +00001151 size = SQUASHFS_NAME_LEN;
plougher50b31762009-03-31 04:14:46 +00001152 ERROR("Filename is greater than %d characters, truncating! ..."
1153 "\n", SQUASHFS_NAME_LEN);
plougher1f413c82005-11-18 00:02:14 +00001154 }
1155
plougher9b393552010-12-31 20:55:43 +00001156 if(dir->p + sizeof(struct squashfs_dir_entry) + size +
plougher520e1a12010-12-31 10:44:38 +00001157 sizeof(struct squashfs_dir_header)
1158 >= dir->buff + dir->size) {
plougherfd57dfe2009-03-30 01:17:52 +00001159 buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001160 if(buff == NULL)
1161 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001162
1163 dir->p = (dir->p - dir->buff) + buff;
1164 if(dir->entry_count_p)
plougherfd57dfe2009-03-30 01:17:52 +00001165 dir->entry_count_p = (dir->entry_count_p - dir->buff +
1166 buff);
plougher1f413c82005-11-18 00:02:14 +00001167 dir->index_count_p = dir->index_count_p - dir->buff + buff;
1168 dir->buff = buff;
1169 }
1170
plougherfd57dfe2009-03-30 01:17:52 +00001171 if(dir->entry_count == 256 || start_block != dir->start_block ||
1172 ((dir->entry_count_p != NULL) &&
plougher9b393552010-12-31 20:55:43 +00001173 ((dir->p + sizeof(struct squashfs_dir_entry) + size -
plougherfd57dfe2009-03-30 01:17:52 +00001174 dir->index_count_p) > SQUASHFS_METADATA_SIZE)) ||
plougher50b31762009-03-31 04:14:46 +00001175 ((long long) inode_number - dir->inode_number) > 32767
1176 || ((long long) inode_number - dir->inode_number)
1177 < -32768) {
plougher1f413c82005-11-18 00:02:14 +00001178 if(dir->entry_count_p) {
plougher520e1a12010-12-31 10:44:38 +00001179 struct squashfs_dir_header dir_header;
plougher1f413c82005-11-18 00:02:14 +00001180
plougher9b393552010-12-31 20:55:43 +00001181 if((dir->p + sizeof(struct squashfs_dir_entry) + size -
plougherfd57dfe2009-03-30 01:17:52 +00001182 dir->index_count_p) >
1183 SQUASHFS_METADATA_SIZE) {
1184 if(dir->i_count % I_COUNT_SIZE == 0) {
1185 dir->index = realloc(dir->index,
1186 (dir->i_count + I_COUNT_SIZE) *
1187 sizeof(struct cached_dir_index));
1188 if(dir->index == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001189 MEM_ERROR();
plougherfd57dfe2009-03-30 01:17:52 +00001190 }
1191 dir->index[dir->i_count].index.index =
1192 dir->p - dir->buff;
plougher1f413c82005-11-18 00:02:14 +00001193 dir->index[dir->i_count].index.size = size - 1;
1194 dir->index[dir->i_count++].name = name;
plougher2bd2b722010-12-31 10:52:15 +00001195 dir->i_size += sizeof(struct squashfs_dir_index)
1196 + size;
plougher1f413c82005-11-18 00:02:14 +00001197 dir->index_count_p = dir->p;
1198 }
1199
1200 dir_header.count = dir->entry_count - 1;
1201 dir_header.start_block = dir->start_block;
1202 dir_header.inode_number = dir->inode_number;
plougherfd57dfe2009-03-30 01:17:52 +00001203 SQUASHFS_SWAP_DIR_HEADER(&dir_header,
plougherac28cd12010-02-24 02:25:03 +00001204 dir->entry_count_p);
plougher1f413c82005-11-18 00:02:14 +00001205
1206 }
1207
1208
1209 dir->entry_count_p = dir->p;
1210 dir->start_block = start_block;
1211 dir->entry_count = 0;
1212 dir->inode_number = inode_number;
plougher520e1a12010-12-31 10:44:38 +00001213 dir->p += sizeof(struct squashfs_dir_header);
plougher1f413c82005-11-18 00:02:14 +00001214 }
1215
plougher1f413c82005-11-18 00:02:14 +00001216 idir.offset = offset;
1217 idir.type = type;
1218 idir.size = size - 1;
1219 idir.inode_number = ((long long) inode_number - dir->inode_number);
plougherac28cd12010-02-24 02:25:03 +00001220 SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p);
1221 strncpy((char *) dir->p + name_off, name, size);
plougher9b393552010-12-31 20:55:43 +00001222 dir->p += sizeof(struct squashfs_dir_entry) + size;
plougher1f413c82005-11-18 00:02:14 +00001223 dir->entry_count ++;
1224}
1225
1226
plougherfd57dfe2009-03-30 01:17:52 +00001227void write_dir(squashfs_inode *inode, struct dir_info *dir_info,
1228 struct directory *dir)
plougher1f413c82005-11-18 00:02:14 +00001229{
1230 unsigned int dir_size = dir->p - dir->buff;
plougher4627ca32010-12-16 05:03:55 +00001231 int data_space = directory_cache_size - directory_cache_bytes;
plougher1f413c82005-11-18 00:02:14 +00001232 unsigned int directory_block, directory_offset, i_count, index;
1233 unsigned short c_byte;
1234
1235 if(data_space < dir_size) {
plougherfd57dfe2009-03-30 01:17:52 +00001236 int realloc_size = directory_cache_size == 0 ?
1237 ((dir_size + SQUASHFS_METADATA_SIZE) &
1238 ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space;
plougher1f413c82005-11-18 00:02:14 +00001239
plougher17248ca2010-07-27 00:24:35 +00001240 void *dc = realloc(directory_data_cache,
plougherfd57dfe2009-03-30 01:17:52 +00001241 directory_cache_size + realloc_size);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001242 if(dc == NULL)
1243 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001244 directory_cache_size += realloc_size;
plougher17248ca2010-07-27 00:24:35 +00001245 directory_data_cache = dc;
plougher1f413c82005-11-18 00:02:14 +00001246 }
1247
1248 if(dir_size) {
plougher520e1a12010-12-31 10:44:38 +00001249 struct squashfs_dir_header dir_header;
plougher1f413c82005-11-18 00:02:14 +00001250
1251 dir_header.count = dir->entry_count - 1;
1252 dir_header.start_block = dir->start_block;
1253 dir_header.inode_number = dir->inode_number;
plougherac28cd12010-02-24 02:25:03 +00001254 SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p);
plougherfd57dfe2009-03-30 01:17:52 +00001255 memcpy(directory_data_cache + directory_cache_bytes, dir->buff,
1256 dir_size);
plougher1f413c82005-11-18 00:02:14 +00001257 }
1258 directory_offset = directory_cache_bytes;
1259 directory_block = directory_bytes;
1260 directory_cache_bytes += dir_size;
1261 i_count = 0;
1262 index = SQUASHFS_METADATA_SIZE - directory_offset;
1263
1264 while(1) {
plougherfd57dfe2009-03-30 01:17:52 +00001265 while(i_count < dir->i_count &&
1266 dir->index[i_count].index.index < index)
plougher50b31762009-03-31 04:14:46 +00001267 dir->index[i_count++].index.start_block =
1268 directory_bytes;
plougher1f413c82005-11-18 00:02:14 +00001269 index += SQUASHFS_METADATA_SIZE;
1270
1271 if(directory_cache_bytes < SQUASHFS_METADATA_SIZE)
1272 break;
1273
plougherfd57dfe2009-03-30 01:17:52 +00001274 if((directory_size - directory_bytes) <
1275 ((SQUASHFS_METADATA_SIZE << 1) + 2)) {
plougher79d665a2010-07-27 00:19:23 +00001276 void *dt = realloc(directory_table,
plougher50b31762009-03-31 04:14:46 +00001277 directory_size + (SQUASHFS_METADATA_SIZE << 1)
1278 + 2);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001279 if(dt == NULL)
1280 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001281 directory_size += SQUASHFS_METADATA_SIZE << 1;
plougher79d665a2010-07-27 00:19:23 +00001282 directory_table = dt;
plougher1f413c82005-11-18 00:02:14 +00001283 }
1284
plougher50b31762009-03-31 04:14:46 +00001285 c_byte = mangle(directory_table + directory_bytes +
1286 BLOCK_OFFSET, directory_data_cache,
1287 SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE,
1288 noI, 0);
plougherfd57dfe2009-03-30 01:17:52 +00001289 TRACE("Directory block @ 0x%x, size %d\n", directory_bytes,
1290 c_byte);
plougherac28cd12010-02-24 02:25:03 +00001291 SQUASHFS_SWAP_SHORTS(&c_byte,
1292 directory_table + directory_bytes, 1);
plougher50b31762009-03-31 04:14:46 +00001293 directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) +
1294 BLOCK_OFFSET;
plougher1b899fc2008-08-07 01:24:06 +00001295 total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET;
plougher14dd5f32010-08-02 18:20:03 +00001296 memmove(directory_data_cache, directory_data_cache +
plougherfd57dfe2009-03-30 01:17:52 +00001297 SQUASHFS_METADATA_SIZE, directory_cache_bytes -
1298 SQUASHFS_METADATA_SIZE);
plougher1f413c82005-11-18 00:02:14 +00001299 directory_cache_bytes -= SQUASHFS_METADATA_SIZE;
1300 }
1301
plougher3c6bdb52010-05-01 02:30:59 +00001302 create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE,
1303 dir_size + 3, directory_block, directory_offset, NULL, NULL,
1304 dir, 0);
plougher1f413c82005-11-18 00:02:14 +00001305
1306#ifdef SQUASHFS_TRACE
plougher1f288f62009-02-21 03:05:52 +00001307 {
plougher1f413c82005-11-18 00:02:14 +00001308 unsigned char *dirp;
1309 int count;
1310
1311 TRACE("Directory contents of inode 0x%llx\n", *inode);
1312 dirp = dir->buff;
1313 while(dirp < dir->p) {
1314 char buffer[SQUASHFS_NAME_LEN + 1];
plougher9b393552010-12-31 20:55:43 +00001315 struct squashfs_dir_entry idir, *idirp;
plougher520e1a12010-12-31 10:44:38 +00001316 struct squashfs_dir_header dirh;
1317 SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp,
plougher360514a2009-03-30 03:01:38 +00001318 &dirh);
plougher1f288f62009-02-21 03:05:52 +00001319 count = dirh.count + 1;
plougher520e1a12010-12-31 10:44:38 +00001320 dirp += sizeof(struct squashfs_dir_header);
plougher1f413c82005-11-18 00:02:14 +00001321
plougher50b31762009-03-31 04:14:46 +00001322 TRACE("\tStart block 0x%x, count %d\n",
1323 dirh.start_block, count);
plougher1f413c82005-11-18 00:02:14 +00001324
1325 while(count--) {
plougher9b393552010-12-31 20:55:43 +00001326 idirp = (struct squashfs_dir_entry *) dirp;
plougher1f288f62009-02-21 03:05:52 +00001327 SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir);
plougher1f413c82005-11-18 00:02:14 +00001328 strncpy(buffer, idirp->name, idir.size + 1);
1329 buffer[idir.size + 1] = '\0';
plougher50b31762009-03-31 04:14:46 +00001330 TRACE("\t\tname %s, inode offset 0x%x, type "
1331 "%d\n", buffer, idir.offset, idir.type);
plougher9b393552010-12-31 20:55:43 +00001332 dirp += sizeof(struct squashfs_dir_entry) + idir.size +
ploughere8b26f62010-12-16 05:06:00 +00001333 1;
plougher1f413c82005-11-18 00:02:14 +00001334 }
1335 }
1336 }
1337#endif
1338 dir_count ++;
plougher1f413c82005-11-18 00:02:14 +00001339}
1340
1341
plougher76c64082008-03-08 01:32:23 +00001342struct file_buffer *get_fragment(struct fragment *fragment)
plougher1f413c82005-11-18 00:02:14 +00001343{
plougher8ed84b92010-12-31 10:37:24 +00001344 struct squashfs_fragment_entry *disk_fragment;
plougher1d065e92010-06-18 03:58:27 +00001345 int res, size;
plougher76c64082008-03-08 01:32:23 +00001346 long long start_block;
1347 struct file_buffer *buffer, *compressed_buffer;
plougher5507dd92006-11-06 00:43:10 +00001348
plougher76c64082008-03-08 01:32:23 +00001349 if(fragment->index == SQUASHFS_INVALID_FRAG)
1350 return NULL;
plougher5507dd92006-11-06 00:43:10 +00001351
plougher76c64082008-03-08 01:32:23 +00001352 buffer = cache_lookup(fragment_buffer, fragment->index);
plougher1b899fc2008-08-07 01:24:06 +00001353 if(buffer)
plougher76c64082008-03-08 01:32:23 +00001354 return buffer;
plougher76c64082008-03-08 01:32:23 +00001355
Phillip Lougher45702012013-04-30 05:19:54 +01001356 compressed_buffer = cache_lookup(writer_buffer,
Phillip Lougherfd992722013-05-19 04:46:57 +01001357 FRAG_INDEX((long long) fragment->index));
plougher2ea89142008-03-11 01:34:19 +00001358
Phillip Lougher316ab632013-04-29 02:53:39 +01001359 buffer = cache_get(fragment_buffer, fragment->index);
plougher5507dd92006-11-06 00:43:10 +00001360
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001361 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001362 pthread_mutex_lock(&fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001363 disk_fragment = &fragment_table[fragment->index];
1364 size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size);
plougher76c64082008-03-08 01:32:23 +00001365 start_block = disk_fragment->start_block;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001366 pthread_cleanup_pop(1);
plougher1f413c82005-11-18 00:02:14 +00001367
plougher0f464442008-03-31 00:27:56 +00001368 if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) {
plougher1d065e92010-06-18 03:58:27 +00001369 int error;
plougher76c64082008-03-08 01:32:23 +00001370 char *data;
plougher1f413c82005-11-18 00:02:14 +00001371
plougher76c64082008-03-08 01:32:23 +00001372 if(compressed_buffer)
1373 data = compressed_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001374 else {
plougher49b57a92009-03-31 03:23:05 +00001375 data = read_from_disk(start_block, size);
Phillip Lougherac731382013-05-19 04:19:44 +01001376 if(data == NULL) {
1377 ERROR("Failed to read fragment from output"
1378 " filesystem\n");
1379 BAD_ERROR("Output filesystem corrupted?\n");
1380 }
1381 }
plougher1f413c82005-11-18 00:02:14 +00001382
plougherb48442b2010-12-31 07:57:54 +00001383 res = compressor_uncompress(comp, buffer->data, data, size,
1384 block_size, &error);
ploughera175ce22009-07-30 04:43:27 +00001385 if(res == -1)
1386 BAD_ERROR("%s uncompress failed with error code %d\n",
1387 comp->name, error);
plougher76c64082008-03-08 01:32:23 +00001388 } else if(compressed_buffer)
1389 memcpy(buffer->data, compressed_buffer->data, size);
plougher1d065e92010-06-18 03:58:27 +00001390 else {
1391 res = read_fs_bytes(fd, start_block, size, buffer->data);
Phillip Lougher94519382013-03-06 02:43:42 +00001392 if(res == 0) {
1393 ERROR("Failed to read fragment from output "
1394 "filesystem\n");
1395 BAD_ERROR("Output filesystem corrupted?\n");
1396 }
plougher1d065e92010-06-18 03:58:27 +00001397 }
plougher1f413c82005-11-18 00:02:14 +00001398
plougher03859fa2009-03-31 03:18:18 +00001399 cache_block_put(compressed_buffer);
1400
plougher76c64082008-03-08 01:32:23 +00001401 return buffer;
plougher1f413c82005-11-18 00:02:14 +00001402}
1403
plougher2ea89142008-03-11 01:34:19 +00001404
plougher4e8484a2008-03-15 23:30:16 +00001405int lock_fragments()
plougher5507dd92006-11-06 00:43:10 +00001406{
plougher4e8484a2008-03-15 23:30:16 +00001407 int count;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001408 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001409 pthread_mutex_lock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00001410 fragments_locked = TRUE;
plougher4e8484a2008-03-15 23:30:16 +00001411 count = fragments_outstanding;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001412 pthread_cleanup_pop(1);
plougher4e8484a2008-03-15 23:30:16 +00001413 return count;
plougher2ea89142008-03-11 01:34:19 +00001414}
1415
1416
1417void unlock_fragments()
1418{
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001419 int frg, size;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001420 struct file_buffer *write_buffer;
plougher2ea89142008-03-11 01:34:19 +00001421
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001422 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00001423 pthread_mutex_lock(&fragment_mutex);
Phillip Lougher71ad9642013-05-25 05:03:48 +01001424
1425 /*
1426 * Note queue_empty() is inherently racy with respect to concurrent
1427 * queue get and pushes. We avoid this because we're holding the
1428 * fragment_mutex which ensures no other threads can be using the
1429 * queue at this time.
1430 */
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001431 while(!queue_empty(locked_fragment)) {
1432 write_buffer = queue_get(locked_fragment);
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001433 frg = write_buffer->block;
1434 size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size);
1435 fragment_table[frg].start_block = bytes;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001436 write_buffer->block = bytes;
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001437 bytes += size;
plougher2ea89142008-03-11 01:34:19 +00001438 fragments_outstanding --;
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001439 queue_put(to_writer, write_buffer);
plougher50b31762009-03-31 04:14:46 +00001440 TRACE("fragment_locked writing fragment %d, compressed size %d"
Phillip Loughercd6cb7a2013-05-29 02:31:39 +01001441 "\n", frg, size);
plougher2ea89142008-03-11 01:34:19 +00001442 }
1443 fragments_locked = FALSE;
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001444 pthread_cleanup_pop(1);
plougher2ea89142008-03-11 01:34:19 +00001445}
1446
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001447/* Called with the fragment_mutex locked */
plougherb7a66812010-07-21 01:06:59 +00001448void add_pending_fragment(struct file_buffer *write_buffer, int c_byte,
plougher17b269c2009-03-30 01:33:07 +00001449 int fragment)
plougher2ea89142008-03-11 01:34:19 +00001450{
Phillip Lougher6164b5f2013-05-23 05:05:10 +01001451 fragment_table[fragment].size = c_byte;
1452 write_buffer->block = fragment;
1453
1454 queue_put(locked_fragment, write_buffer);
plougher5507dd92006-11-06 00:43:10 +00001455}
1456
1457
Phillip Lougher04b7b532011-06-11 02:14:15 +01001458void write_fragment(struct file_buffer *fragment)
plougher1f413c82005-11-18 00:02:14 +00001459{
Phillip Lougher04b7b532011-06-11 02:14:15 +01001460 if(fragment == NULL)
plougher1f413c82005-11-18 00:02:14 +00001461 return;
1462
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001463 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
plougher5507dd92006-11-06 00:43:10 +00001464 pthread_mutex_lock(&fragment_mutex);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001465 fragment_table[fragment->block].unused = 0;
1466 fragments_outstanding ++;
1467 queue_put(to_frag, fragment);
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001468 pthread_cleanup_pop(1);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001469}
1470
1471
1472struct file_buffer *allocate_fragment()
1473{
Phillip Lougher316ab632013-04-29 02:53:39 +01001474 struct file_buffer *fragment = cache_get(fragment_buffer, fragments);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001475
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001476 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
Phillip Lougherb9508be2011-06-13 02:25:51 +01001477 pthread_mutex_lock(&fragment_mutex);
1478
plougher5507dd92006-11-06 00:43:10 +00001479 if(fragments % FRAG_SIZE == 0) {
plougherfa89c332010-07-27 00:27:15 +00001480 void *ft = realloc(fragment_table, (fragments +
plougher8ed84b92010-12-31 10:37:24 +00001481 FRAG_SIZE) * sizeof(struct squashfs_fragment_entry));
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01001482 if(ft == NULL)
1483 MEM_ERROR();
plougherfa89c332010-07-27 00:27:15 +00001484 fragment_table = ft;
plougher5507dd92006-11-06 00:43:10 +00001485 }
Phillip Lougherb9508be2011-06-13 02:25:51 +01001486
1487 fragment->size = 0;
1488 fragment->block = fragments ++;
1489
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01001490 pthread_cleanup_pop(1);
1491
Phillip Lougherb9508be2011-06-13 02:25:51 +01001492 return fragment;
plougher5507dd92006-11-06 00:43:10 +00001493}
1494
ploughereb6eac92008-02-26 01:50:48 +00001495
plougher1f413c82005-11-18 00:02:14 +00001496static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0};
Phillip Lougherd2f045f2012-12-28 03:15:45 +00001497
1498
1499void free_fragment(struct fragment *fragment)
1500{
1501 if(fragment != &empty_fragment)
1502 free(fragment);
1503}
1504
1505
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001506struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer,
1507 struct dir_ent *dir_ent)
plougher1f413c82005-11-18 00:02:14 +00001508{
1509 struct fragment *ffrg;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001510 struct file_buffer **fragment;
plougher1f413c82005-11-18 00:02:14 +00001511
plougher5507dd92006-11-06 00:43:10 +00001512 if(file_buffer == NULL || file_buffer->size == 0)
plougher1f413c82005-11-18 00:02:14 +00001513 return &empty_fragment;
1514
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001515 fragment = eval_frag_actions(dir_ent);
1516
1517 if((*fragment) && (*fragment)->size + file_buffer->size > block_size) {
1518 write_fragment(*fragment);
1519 *fragment = NULL;
Phillip Lougher04b7b532011-06-11 02:14:15 +01001520 }
plougher1f413c82005-11-18 00:02:14 +00001521
ploughere7e6e832010-12-16 05:08:06 +00001522 ffrg = malloc(sizeof(struct fragment));
1523 if(ffrg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001524 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001525
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001526 if(*fragment == NULL)
1527 *fragment = allocate_fragment();
plougher5507dd92006-11-06 00:43:10 +00001528
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001529 ffrg->index = (*fragment)->block;
1530 ffrg->offset = (*fragment)->size;
plougher5507dd92006-11-06 00:43:10 +00001531 ffrg->size = file_buffer->size;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001532 memcpy((*fragment)->data + (*fragment)->size, file_buffer->data,
plougher17b269c2009-03-30 01:33:07 +00001533 file_buffer->size);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01001534 (*fragment)->size += file_buffer->size;
plougher1f413c82005-11-18 00:02:14 +00001535
1536 return ffrg;
1537}
1538
1539
ploughera0a49c32010-08-11 01:47:59 +00001540long long generic_write_table(int length, void *buffer, int length2,
1541 void *buffer2, int uncompressed)
plougher1f413c82005-11-18 00:02:14 +00001542{
plougher17b269c2009-03-30 01:33:07 +00001543 int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) /
1544 SQUASHFS_METADATA_SIZE;
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001545 long long *list, start_bytes;
Phillip Lougherc1305322012-11-02 22:28:02 +00001546 int compressed_size, i, list_size = meta_blocks * sizeof(long long);
plougher1f413c82005-11-18 00:02:14 +00001547 unsigned short c_byte;
plougher0e453652006-11-06 01:49:35 +00001548 char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2];
1549
plougher82ab2332009-04-21 00:21:21 +00001550#ifdef SQUASHFS_TRACE
plougher0e453652006-11-06 01:49:35 +00001551 long long obytes = bytes;
plougher40d8b952010-02-12 16:26:32 +00001552 int olength = length;
plougher82ab2332009-04-21 00:21:21 +00001553#endif
plougher1f413c82005-11-18 00:02:14 +00001554
Phillip Lougherc1305322012-11-02 22:28:02 +00001555 list = malloc(list_size);
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001556 if(list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001557 MEM_ERROR();
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001558
plougher1f413c82005-11-18 00:02:14 +00001559 for(i = 0; i < meta_blocks; i++) {
plougher17b269c2009-03-30 01:33:07 +00001560 int avail_bytes = length > SQUASHFS_METADATA_SIZE ?
1561 SQUASHFS_METADATA_SIZE : length;
1562 c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i *
1563 SQUASHFS_METADATA_SIZE , avail_bytes,
1564 SQUASHFS_METADATA_SIZE, uncompressed, 0);
plougherac28cd12010-02-24 02:25:03 +00001565 SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1);
plougher1f413c82005-11-18 00:02:14 +00001566 list[i] = bytes;
plougher50b31762009-03-31 04:14:46 +00001567 compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) +
1568 BLOCK_OFFSET;
plougher17b269c2009-03-30 01:33:07 +00001569 TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes,
1570 compressed_size);
plougher0dd6f122009-03-29 21:43:57 +00001571 write_destination(fd, bytes, compressed_size, cbuffer);
plougher1f413c82005-11-18 00:02:14 +00001572 bytes += compressed_size;
plougher10f7d572010-07-20 02:14:04 +00001573 total_bytes += avail_bytes;
plougher0e453652006-11-06 01:49:35 +00001574 length -= avail_bytes;
plougher1f413c82005-11-18 00:02:14 +00001575 }
1576
ploughere6e0e1b2010-05-12 17:17:06 +00001577 start_bytes = bytes;
1578 if(length2) {
ploughera0a49c32010-08-11 01:47:59 +00001579 write_destination(fd, bytes, length2, buffer2);
ploughere6e0e1b2010-05-12 17:17:06 +00001580 bytes += length2;
plougher10f7d572010-07-20 02:14:04 +00001581 total_bytes += length2;
ploughere6e0e1b2010-05-12 17:17:06 +00001582 }
1583
plougher1f288f62009-02-21 03:05:52 +00001584 SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks);
Phillip Lougherc1305322012-11-02 22:28:02 +00001585 write_destination(fd, bytes, list_size, list);
1586 bytes += list_size;
1587 total_bytes += list_size;
plougher1f413c82005-11-18 00:02:14 +00001588
plougher40d8b952010-02-12 16:26:32 +00001589 TRACE("generic_write_table: total uncompressed %d compressed %lld\n",
1590 olength, bytes - obytes);
plougher0e453652006-11-06 01:49:35 +00001591
Phillip Lougherd4e78ee2012-10-31 21:32:40 +00001592 free(list);
1593
plougher1f413c82005-11-18 00:02:14 +00001594 return start_bytes;
1595}
1596
1597
plougher0e453652006-11-06 01:49:35 +00001598long long write_fragment_table()
1599{
1600 unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments);
plougher0e453652006-11-06 01:49:35 +00001601 int i;
1602
plougher17b269c2009-03-30 01:33:07 +00001603 TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments,
1604 frag_bytes);
plougherac28cd12010-02-24 02:25:03 +00001605 for(i = 0; i < fragments; i++) {
plougher50b31762009-03-31 04:14:46 +00001606 TRACE("write_fragment_table: fragment %d, start_block 0x%llx, "
1607 "size %d\n", i, fragment_table[i].start_block,
plougher17b269c2009-03-30 01:33:07 +00001608 fragment_table[i].size);
Phillip Lougher162c24c2012-10-30 02:54:16 +00001609 SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]);
plougher0e453652006-11-06 01:49:35 +00001610 }
1611
Phillip Lougher162c24c2012-10-30 02:54:16 +00001612 return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF);
plougher0e453652006-11-06 01:49:35 +00001613}
1614
1615
plougher1f413c82005-11-18 00:02:14 +00001616char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE];
plougher5507dd92006-11-06 00:43:10 +00001617char *read_from_disk(long long start, unsigned int avail_bytes)
plougher1f413c82005-11-18 00:02:14 +00001618{
plougher1d065e92010-06-18 03:58:27 +00001619 int res;
1620
1621 res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer);
Phillip Lougherac731382013-05-19 04:19:44 +01001622 if(res == 0)
1623 return NULL;
plougher1d065e92010-06-18 03:58:27 +00001624
plougher1f413c82005-11-18 00:02:14 +00001625 return read_from_file_buffer;
1626}
1627
1628
plougher1b899fc2008-08-07 01:24:06 +00001629char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE];
1630char *read_from_disk2(long long start, unsigned int avail_bytes)
1631{
plougher1d065e92010-06-18 03:58:27 +00001632 int res;
1633
1634 res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2);
Phillip Lougherac731382013-05-19 04:19:44 +01001635 if(res == 0)
1636 return NULL;
plougher1d065e92010-06-18 03:58:27 +00001637
plougher1b899fc2008-08-07 01:24:06 +00001638 return read_from_file_buffer2;
1639}
1640
1641
plougher1f413c82005-11-18 00:02:14 +00001642/*
1643 * Compute 16 bit BSD checksum over the data
1644 */
plougher5507dd92006-11-06 00:43:10 +00001645unsigned short get_checksum(char *buff, int bytes, unsigned short chksum)
plougher1f413c82005-11-18 00:02:14 +00001646{
plougher5507dd92006-11-06 00:43:10 +00001647 unsigned char *b = (unsigned char *) buff;
plougher1f413c82005-11-18 00:02:14 +00001648
plougher5507dd92006-11-06 00:43:10 +00001649 while(bytes --) {
1650 chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1;
1651 chksum += *b++;
plougher1f413c82005-11-18 00:02:14 +00001652 }
1653
1654 return chksum;
1655}
1656
1657
plougher17b269c2009-03-30 01:33:07 +00001658unsigned short get_checksum_disk(long long start, long long l,
1659 unsigned int *blocks)
plougher5507dd92006-11-06 00:43:10 +00001660{
1661 unsigned short chksum = 0;
1662 unsigned int bytes;
plougher57e6d182008-05-06 23:51:29 +00001663 struct file_buffer *write_buffer;
1664 int i;
plougher5507dd92006-11-06 00:43:10 +00001665
plougher57e6d182008-05-06 23:51:29 +00001666 for(i = 0; l; i++) {
1667 bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]);
1668 if(bytes == 0) /* sparse block */
1669 continue;
1670 write_buffer = cache_lookup(writer_buffer, start);
1671 if(write_buffer) {
plougher50b31762009-03-31 04:14:46 +00001672 chksum = get_checksum(write_buffer->data, bytes,
1673 chksum);
plougher57e6d182008-05-06 23:51:29 +00001674 cache_block_put(write_buffer);
Phillip Lougherac731382013-05-19 04:19:44 +01001675 } else {
1676 void *data = read_from_disk(start, bytes);
1677 if(data == NULL) {
1678 ERROR("Failed to checksum data from output"
1679 " filesystem\n");
1680 BAD_ERROR("Output filesystem corrupted?\n");
1681 }
1682
1683 chksum = get_checksum(data, bytes, chksum);
1684 }
1685
plougher5507dd92006-11-06 00:43:10 +00001686 l -= bytes;
plougher5507dd92006-11-06 00:43:10 +00001687 start += bytes;
1688 }
1689
1690 return chksum;
1691}
1692
1693
plougher5507dd92006-11-06 00:43:10 +00001694unsigned short get_checksum_mem(char *buff, int bytes)
1695{
1696 return get_checksum(buff, bytes, 0);
1697}
1698
1699
1700unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer)
1701{
1702 if(file_buffer == NULL)
1703 return 0;
1704 else
1705 return get_checksum(file_buffer->data, file_buffer->size, 0);
1706}
1707
1708
plougher5507dd92006-11-06 00:43:10 +00001709#define DUP_HASH(a) (a & 0xffff)
plougher17b269c2009-03-30 01:33:07 +00001710void add_file(long long start, long long file_size, long long file_bytes,
plougher50b31762009-03-31 04:14:46 +00001711 unsigned int *block_listp, int blocks, unsigned int fragment,
1712 int offset, int bytes)
plougher1f413c82005-11-18 00:02:14 +00001713{
1714 struct fragment *frg;
plougher058eae42006-01-30 14:27:44 +00001715 unsigned int *block_list = block_listp;
plougher5507dd92006-11-06 00:43:10 +00001716 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
plougher058eae42006-01-30 14:27:44 +00001717
plougher5507dd92006-11-06 00:43:10 +00001718 if(!duplicate_checking || file_size == 0)
plougher1f413c82005-11-18 00:02:14 +00001719 return;
1720
plougher5507dd92006-11-06 00:43:10 +00001721 for(; dupl_ptr; dupl_ptr = dupl_ptr->next) {
1722 if(file_size != dupl_ptr->file_size)
1723 continue;
1724 if(blocks != 0 && start != dupl_ptr->start)
1725 continue;
1726 if(fragment != dupl_ptr->fragment->index)
1727 continue;
plougher17b269c2009-03-30 01:33:07 +00001728 if(fragment != SQUASHFS_INVALID_FRAG && (offset !=
1729 dupl_ptr->fragment->offset || bytes !=
1730 dupl_ptr->fragment->size))
plougher5507dd92006-11-06 00:43:10 +00001731 continue;
1732 return;
1733 }
1734
plougher3bb279c2010-12-16 05:10:03 +00001735 frg = malloc(sizeof(struct fragment));
1736 if(frg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001737 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00001738
1739 frg->index = fragment;
1740 frg->offset = offset;
1741 frg->size = bytes;
plougher1f413c82005-11-18 00:02:14 +00001742
plougher5507dd92006-11-06 00:43:10 +00001743 add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0, FALSE);
1744}
plougher1f413c82005-11-18 00:02:14 +00001745
plougher1f413c82005-11-18 00:02:14 +00001746
plougher5507dd92006-11-06 00:43:10 +00001747int pre_duplicate(long long file_size)
plougher1f413c82005-11-18 00:02:14 +00001748{
plougher5507dd92006-11-06 00:43:10 +00001749 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
plougher1f413c82005-11-18 00:02:14 +00001750
1751 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher5507dd92006-11-06 00:43:10 +00001752 if(dupl_ptr->file_size == file_size)
1753 return TRUE;
plougher1f413c82005-11-18 00:02:14 +00001754
plougher5507dd92006-11-06 00:43:10 +00001755 return FALSE;
1756}
1757
1758
1759int pre_duplicate_frag(long long file_size, unsigned short checksum)
1760{
1761 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
1762
1763 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher17b269c2009-03-30 01:33:07 +00001764 if(file_size == dupl_ptr->file_size && file_size ==
1765 dupl_ptr->fragment->size) {
plougher5507dd92006-11-06 00:43:10 +00001766 if(dupl_ptr->checksum_flag == FALSE) {
plougher17b269c2009-03-30 01:33:07 +00001767 struct file_buffer *frag_buffer =
1768 get_fragment(dupl_ptr->fragment);
1769 dupl_ptr->checksum =
1770 get_checksum_disk(dupl_ptr->start,
1771 dupl_ptr->bytes, dupl_ptr->block_list);
1772 dupl_ptr->fragment_checksum =
1773 get_checksum_mem(frag_buffer->data +
1774 dupl_ptr->fragment->offset, file_size);
plougher76c64082008-03-08 01:32:23 +00001775 cache_block_put(frag_buffer);
plougher5507dd92006-11-06 00:43:10 +00001776 dupl_ptr->checksum_flag = TRUE;
plougher1f413c82005-11-18 00:02:14 +00001777 }
plougher5507dd92006-11-06 00:43:10 +00001778 if(dupl_ptr->fragment_checksum == checksum)
1779 return TRUE;
1780 }
plougher1f413c82005-11-18 00:02:14 +00001781
plougher5507dd92006-11-06 00:43:10 +00001782 return FALSE;
1783}
1784
1785
plougher17b269c2009-03-30 01:33:07 +00001786struct file_info *add_non_dup(long long file_size, long long bytes,
1787 unsigned int *block_list, long long start, struct fragment *fragment,
1788 unsigned short checksum, unsigned short fragment_checksum,
1789 int checksum_flag)
plougher5507dd92006-11-06 00:43:10 +00001790{
plougher51ef9ae2010-12-16 05:14:28 +00001791 struct file_info *dupl_ptr = malloc(sizeof(struct file_info));
plougher5507dd92006-11-06 00:43:10 +00001792
Phillip Lougherec71c3c2013-02-21 22:25:53 +00001793 if(dupl_ptr == NULL)
1794 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00001795
1796 dupl_ptr->file_size = file_size;
1797 dupl_ptr->bytes = bytes;
1798 dupl_ptr->block_list = block_list;
1799 dupl_ptr->start = start;
1800 dupl_ptr->fragment = fragment;
1801 dupl_ptr->checksum = checksum;
1802 dupl_ptr->fragment_checksum = fragment_checksum;
1803 dupl_ptr->checksum_flag = checksum_flag;
1804 dupl_ptr->next = dupl[DUP_HASH(file_size)];
1805 dupl[DUP_HASH(file_size)] = dupl_ptr;
1806 dup_files ++;
1807
1808 return dupl_ptr;
1809}
1810
1811
plougher17b269c2009-03-30 01:33:07 +00001812struct file_info *duplicate(long long file_size, long long bytes,
1813 unsigned int **block_list, long long *start, struct fragment **fragment,
1814 struct file_buffer *file_buffer, int blocks, unsigned short checksum,
1815 unsigned short fragment_checksum, int checksum_flag)
plougher5507dd92006-11-06 00:43:10 +00001816{
1817 struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)];
1818 int frag_bytes = file_buffer ? file_buffer->size : 0;
1819
1820 for(; dupl_ptr; dupl_ptr = dupl_ptr->next)
plougher16111452010-07-22 05:12:18 +00001821 if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes
1822 && frag_bytes == dupl_ptr->fragment->size) {
plougher1b899fc2008-08-07 01:24:06 +00001823 long long target_start, dup_start = dupl_ptr->start;
plougher5507dd92006-11-06 00:43:10 +00001824 int block;
1825
plougher17b269c2009-03-30 01:33:07 +00001826 if(memcmp(*block_list, dupl_ptr->block_list, blocks *
1827 sizeof(unsigned int)) != 0)
plougherfbf9f752007-08-12 05:02:24 +00001828 continue;
1829
plougher5507dd92006-11-06 00:43:10 +00001830 if(checksum_flag == FALSE) {
plougher17b269c2009-03-30 01:33:07 +00001831 checksum = get_checksum_disk(*start, bytes,
1832 *block_list);
1833 fragment_checksum =
1834 get_checksum_mem_buffer(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00001835 checksum_flag = TRUE;
1836 }
1837
1838 if(dupl_ptr->checksum_flag == FALSE) {
plougher17b269c2009-03-30 01:33:07 +00001839 struct file_buffer *frag_buffer =
1840 get_fragment(dupl_ptr->fragment);
1841 dupl_ptr->checksum =
1842 get_checksum_disk(dupl_ptr->start,
1843 dupl_ptr->bytes, dupl_ptr->block_list);
1844 dupl_ptr->fragment_checksum =
1845 get_checksum_mem(frag_buffer->data +
1846 dupl_ptr->fragment->offset, frag_bytes);
plougher76c64082008-03-08 01:32:23 +00001847 cache_block_put(frag_buffer);
plougher5507dd92006-11-06 00:43:10 +00001848 dupl_ptr->checksum_flag = TRUE;
1849 }
1850
plougher17b269c2009-03-30 01:33:07 +00001851 if(checksum != dupl_ptr->checksum ||
1852 fragment_checksum !=
1853 dupl_ptr->fragment_checksum)
plougher5507dd92006-11-06 00:43:10 +00001854 continue;
1855
plougher1b899fc2008-08-07 01:24:06 +00001856 target_start = *start;
plougher5507dd92006-11-06 00:43:10 +00001857 for(block = 0; block < blocks; block ++) {
plougher17b269c2009-03-30 01:33:07 +00001858 int size = SQUASHFS_COMPRESSED_SIZE_BLOCK
1859 ((*block_list)[block]);
plougher1b899fc2008-08-07 01:24:06 +00001860 struct file_buffer *target_buffer = NULL;
1861 struct file_buffer *dup_buffer = NULL;
1862 char *target_data, *dup_data;
plougher0f464442008-03-31 00:27:56 +00001863 int res;
plougher5507dd92006-11-06 00:43:10 +00001864
plougher1b899fc2008-08-07 01:24:06 +00001865 if(size == 0)
plougherfbf9f752007-08-12 05:02:24 +00001866 continue;
plougher17b269c2009-03-30 01:33:07 +00001867 target_buffer = cache_lookup(writer_buffer,
1868 target_start);
plougher1b899fc2008-08-07 01:24:06 +00001869 if(target_buffer)
1870 target_data = target_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001871 else {
plougher50b31762009-03-31 04:14:46 +00001872 target_data =
1873 read_from_disk(target_start,
plougher17b269c2009-03-30 01:33:07 +00001874 size);
Phillip Lougherac731382013-05-19 04:19:44 +01001875 if(target_data == NULL) {
1876 ERROR("Failed to read data from"
1877 " output filesystem\n");
1878 BAD_ERROR("Output filesystem"
1879 " corrupted?\n");
1880 }
1881 }
plougher5507dd92006-11-06 00:43:10 +00001882
plougher360514a2009-03-30 03:01:38 +00001883 dup_buffer = cache_lookup(writer_buffer,
1884 dup_start);
plougher1b899fc2008-08-07 01:24:06 +00001885 if(dup_buffer)
1886 dup_data = dup_buffer->data;
Phillip Lougherac731382013-05-19 04:19:44 +01001887 else {
plougher360514a2009-03-30 03:01:38 +00001888 dup_data = read_from_disk2(dup_start,
1889 size);
Phillip Lougherac731382013-05-19 04:19:44 +01001890 if(dup_data == NULL) {
1891 ERROR("Failed to read data from"
1892 " output filesystem\n");
1893 BAD_ERROR("Output filesystem"
1894 " corrupted?\n");
1895 }
1896 }
plougher1b899fc2008-08-07 01:24:06 +00001897
1898 res = memcmp(target_data, dup_data, size);
1899 cache_block_put(target_buffer);
1900 cache_block_put(dup_buffer);
plougher0f464442008-03-31 00:27:56 +00001901 if(res != 0)
plougher5507dd92006-11-06 00:43:10 +00001902 break;
plougher1b899fc2008-08-07 01:24:06 +00001903 target_start += size;
1904 dup_start += size;
plougher5507dd92006-11-06 00:43:10 +00001905 }
1906 if(block == blocks) {
plougher17b269c2009-03-30 01:33:07 +00001907 struct file_buffer *frag_buffer =
1908 get_fragment(dupl_ptr->fragment);
plougher5507dd92006-11-06 00:43:10 +00001909
plougher17b269c2009-03-30 01:33:07 +00001910 if(frag_bytes == 0 ||
1911 memcmp(file_buffer->data,
1912 frag_buffer->data +
1913 dupl_ptr->fragment->offset,
1914 frag_bytes) == 0) {
plougher50b31762009-03-31 04:14:46 +00001915 TRACE("Found duplicate file, start "
1916 "0x%llx, size %lld, checksum "
1917 "0x%x, fragment %d, size %d, "
1918 "offset %d, checksum 0x%x\n",
1919 dupl_ptr->start,
plougher17b269c2009-03-30 01:33:07 +00001920 dupl_ptr->bytes,
1921 dupl_ptr->checksum,
1922 dupl_ptr->fragment->index,
1923 frag_bytes,
1924 dupl_ptr->fragment->offset,
1925 fragment_checksum);
plougher1f413c82005-11-18 00:02:14 +00001926 *block_list = dupl_ptr->block_list;
1927 *start = dupl_ptr->start;
1928 *fragment = dupl_ptr->fragment;
plougher76c64082008-03-08 01:32:23 +00001929 cache_block_put(frag_buffer);
plougher1f413c82005-11-18 00:02:14 +00001930 return 0;
1931 }
plougher76c64082008-03-08 01:32:23 +00001932 cache_block_put(frag_buffer);
plougher1f413c82005-11-18 00:02:14 +00001933 }
1934 }
1935
1936
plougher17b269c2009-03-30 01:33:07 +00001937 return add_non_dup(file_size, bytes, *block_list, *start, *fragment,
1938 checksum, fragment_checksum, checksum_flag);
plougher1f413c82005-11-18 00:02:14 +00001939}
1940
1941
Phillip Lougher2504b082011-09-07 02:28:45 +01001942inline int is_fragment(struct inode_info *inode)
1943{
1944 int file_size = inode->buf.st_size;
1945
Phillip Lougher63f531f2011-09-10 04:03:32 +01001946 /*
1947 * If this block is to be compressed differently to the
1948 * fragment compression then it cannot be a fragment
1949 */
1950 if(inode->noF != noF)
1951 return FALSE;
1952
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001953 return !inode->no_fragments && file_size && (file_size < block_size ||
Phillip Lougher2504b082011-09-07 02:28:45 +01001954 (inode->always_use_fragments && file_size & (block_size - 1)));
1955}
1956
1957
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001958void put_file_buffer(struct file_buffer *file_buffer)
1959{
1960 /*
1961 * Decide where to send the file buffer - only compressible non-
1962 * fragment blocks need to be send to the deflate threads, all
1963 * others can be sent directly to the main thread
1964 */
1965 if(file_buffer->error) {
1966 file_buffer->fragment = 0;
Phillip Lougher0e1656d2013-05-20 03:47:24 +01001967 seq_queue_put(to_main, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001968 } else if (file_buffer->file_size == 0 || file_buffer->fragment)
Phillip Lougher0e1656d2013-05-20 03:47:24 +01001969 seq_queue_put(to_main, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001970 else
Phillip Loughercf478e92013-05-29 02:38:41 +01001971 queue_put(to_deflate, file_buffer);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01001972}
1973
1974
plougher00d08172009-09-03 10:17:44 +00001975static int seq = 0;
1976void reader_read_process(struct dir_ent *dir_ent)
1977{
Phillip Lougher9b6e3412011-09-05 02:58:41 +01001978 struct inode_info *inode = dir_ent->inode;
plougher00d08172009-09-03 10:17:44 +00001979 struct file_buffer *prev_buffer = NULL, *file_buffer;
plougherba674e82009-09-10 03:50:00 +00001980 int status, res, byte, count = 0;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01001981 int file = get_pseudo_file(inode->pseudo_id)->fd;
1982 int child = get_pseudo_file(inode->pseudo_id)->child;
plougher00d08172009-09-03 10:17:44 +00001983 long long bytes = 0;
1984
1985 while(1) {
Phillip Lougher316ab632013-04-29 02:53:39 +01001986 file_buffer = cache_get_nohash(reader_buffer);
plougher00d08172009-09-03 10:17:44 +00001987 file_buffer->sequence = seq ++;
Phillip Lougher63f531f2011-09-10 04:03:32 +01001988 file_buffer->noD = inode->noD;
plougher00d08172009-09-03 10:17:44 +00001989
1990 byte = read_bytes(file, file_buffer->data, block_size);
1991 if(byte == -1)
1992 goto read_err;
1993
1994 file_buffer->size = byte;
1995 file_buffer->file_size = -1;
1996 file_buffer->block = count ++;
1997 file_buffer->error = FALSE;
1998 file_buffer->fragment = FALSE;
1999 bytes += byte;
2000
2001 if(byte == 0)
2002 break;
2003
plougher286b6b32009-09-19 03:29:27 +00002004 /*
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002005 * Update progress bar size. This is done
plougher286b6b32009-09-19 03:29:27 +00002006 * on every block rather than waiting for all blocks to be
2007 * read incase write_file_process() is running in parallel
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002008 * with this. Otherwise the current progress bar position
2009 * may get ahead of the progress bar size.
plougher286b6b32009-09-19 03:29:27 +00002010 */
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002011 progress_bar_size(1);
plougher286b6b32009-09-19 03:29:27 +00002012
plougher00d08172009-09-03 10:17:44 +00002013 if(prev_buffer)
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002014 put_file_buffer(prev_buffer);
plougher00d08172009-09-03 10:17:44 +00002015 prev_buffer = file_buffer;
2016 }
2017
plougher54d67292009-09-19 03:26:27 +00002018 /*
2019 * Update inode file size now that the size of the dynamic pseudo file
2020 * is known. This is needed for the -info option.
2021 */
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002022 inode->buf.st_size = bytes;
plougher54d67292009-09-19 03:26:27 +00002023
plougherba674e82009-09-10 03:50:00 +00002024 res = waitpid(child, &status, 0);
plougherd87d8d12009-09-10 04:08:16 +00002025 if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
plougherba674e82009-09-10 03:50:00 +00002026 goto read_err;
2027
plougher00d08172009-09-03 10:17:44 +00002028 if(prev_buffer == NULL)
2029 prev_buffer = file_buffer;
2030 else {
2031 cache_block_put(file_buffer);
2032 seq --;
2033 }
2034 prev_buffer->file_size = bytes;
Phillip Lougher2504b082011-09-07 02:28:45 +01002035 prev_buffer->fragment = is_fragment(inode);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002036 put_file_buffer(prev_buffer);
plougher00d08172009-09-03 10:17:44 +00002037
2038 return;
2039
2040read_err:
2041 if(prev_buffer) {
2042 cache_block_put(file_buffer);
2043 seq --;
2044 file_buffer = prev_buffer;
2045 }
2046 file_buffer->error = TRUE;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002047 put_file_buffer(file_buffer);
plougher00d08172009-09-03 10:17:44 +00002048}
2049
2050
plougher5507dd92006-11-06 00:43:10 +00002051void reader_read_file(struct dir_ent *dir_ent)
plougher1f413c82005-11-18 00:02:14 +00002052{
plougher018d2b32007-04-23 03:01:48 +00002053 struct stat *buf = &dir_ent->inode->buf, buf2;
plougher5507dd92006-11-06 00:43:10 +00002054 struct file_buffer *file_buffer;
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002055 int blocks, byte, count, expected, file, res;
plougher018d2b32007-04-23 03:01:48 +00002056 long long bytes, read_size;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002057 struct inode_info *inode = dir_ent->inode;
plougher5507dd92006-11-06 00:43:10 +00002058
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002059 if(inode->read)
plougher5507dd92006-11-06 00:43:10 +00002060 return;
2061
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002062 inode->read = TRUE;
plougher018d2b32007-04-23 03:01:48 +00002063again:
2064 bytes = 0;
2065 count = 0;
2066 file_buffer = NULL;
2067 read_size = buf->st_size;
2068 blocks = (read_size + block_size - 1) >> block_log;
plougher018d2b32007-04-23 03:01:48 +00002069
Phillip Lougher494479f2012-02-03 15:45:41 +00002070 file = open(pathname_reader(dir_ent), O_RDONLY);
plougherc1d258e2010-12-16 05:16:45 +00002071 if(file == -1) {
Phillip Lougher316ab632013-04-29 02:53:39 +01002072 file_buffer = cache_get_nohash(reader_buffer);
plougher1e380702010-08-11 01:52:49 +00002073 file_buffer->sequence = seq ++;
Phillip Lougher2302f692012-12-27 04:05:02 +00002074 goto read_err2;
plougher1e380702010-08-11 01:52:49 +00002075 }
plougher5507dd92006-11-06 00:43:10 +00002076
plougher018d2b32007-04-23 03:01:48 +00002077 do {
plougher110799c2009-03-30 01:50:40 +00002078 expected = read_size - ((long long) count * block_size) >
plougher50b31762009-03-31 04:14:46 +00002079 block_size ? block_size :
2080 read_size - ((long long) count * block_size);
plougher018d2b32007-04-23 03:01:48 +00002081
2082 if(file_buffer)
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002083 put_file_buffer(file_buffer);
Phillip Lougher316ab632013-04-29 02:53:39 +01002084 file_buffer = cache_get_nohash(reader_buffer);
plougher00d08172009-09-03 10:17:44 +00002085 file_buffer->sequence = seq ++;
Phillip Lougher63f531f2011-09-10 04:03:32 +01002086 file_buffer->noD = inode->noD;
plougher5507dd92006-11-06 00:43:10 +00002087
ploughera4c24ed2010-08-11 02:08:38 +00002088 /*
2089 * Always try to read block_size bytes from the file rather
2090 * than expected bytes (which will be less than the block_size
2091 * at the file tail) to check that the file hasn't grown
2092 * since being stated. If it is longer (or shorter) than
2093 * expected, then restat, and try again. Note the special
2094 * case where the file is an exact multiple of the block_size
2095 * is dealt with later.
2096 */
plougher110799c2009-03-30 01:50:40 +00002097 byte = file_buffer->size = read_bytes(file, file_buffer->data,
2098 block_size);
plougher018d2b32007-04-23 03:01:48 +00002099
plougher018d2b32007-04-23 03:01:48 +00002100 file_buffer->file_size = read_size;
2101
ploughera4c24ed2010-08-11 02:08:38 +00002102 if(byte == -1)
2103 goto read_err;
2104
plougher018d2b32007-04-23 03:01:48 +00002105 if(byte != expected)
2106 goto restat;
2107
2108 file_buffer->block = count;
plougher5507dd92006-11-06 00:43:10 +00002109 file_buffer->error = FALSE;
Phillip Lougher2504b082011-09-07 02:28:45 +01002110 file_buffer->fragment = FALSE;
plougher018d2b32007-04-23 03:01:48 +00002111
2112 bytes += byte;
2113 count ++;
2114 } while(count < blocks);
2115
2116 if(read_size != bytes)
2117 goto restat;
2118
2119 if(expected == block_size) {
ploughera4c24ed2010-08-11 02:08:38 +00002120 /*
2121 * Special case where we've not tried to read past the end of
2122 * the file. We expect to get EOF, i.e. the file isn't larger
2123 * than we expect.
2124 */
plougher018d2b32007-04-23 03:01:48 +00002125 char buffer;
ploughera4c24ed2010-08-11 02:08:38 +00002126 int res;
plougher018d2b32007-04-23 03:01:48 +00002127
ploughera4c24ed2010-08-11 02:08:38 +00002128 res = read_bytes(file, &buffer, 1);
2129 if(res == -1)
2130 goto read_err;
2131
2132 if(res != 0)
plougher018d2b32007-04-23 03:01:48 +00002133 goto restat;
plougher5507dd92006-11-06 00:43:10 +00002134 }
2135
Phillip Lougher2504b082011-09-07 02:28:45 +01002136 file_buffer->fragment = is_fragment(inode);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002137 put_file_buffer(file_buffer);
plougher018d2b32007-04-23 03:01:48 +00002138
plougher5507dd92006-11-06 00:43:10 +00002139 close(file);
plougher5507dd92006-11-06 00:43:10 +00002140
2141 return;
2142
plougher018d2b32007-04-23 03:01:48 +00002143restat:
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002144 res = fstat(file, &buf2);
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002145 if(res == -1) {
Phillip Lougherfe58b492012-12-15 01:27:07 +00002146 ERROR("Cannot stat dir/file %s because %s, ignoring\n",
Phillip Lougher66f7bfd2012-11-29 23:54:18 +00002147 pathname_reader(dir_ent), strerror(errno));
2148 goto read_err;
2149 }
2150
plougher018d2b32007-04-23 03:01:48 +00002151 if(read_size != buf2.st_size) {
Phillip Lougher2302f692012-12-27 04:05:02 +00002152 close(file);
plougher018d2b32007-04-23 03:01:48 +00002153 memcpy(buf, &buf2, sizeof(struct stat));
2154 file_buffer->error = 2;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002155 put_file_buffer(file_buffer);
plougher018d2b32007-04-23 03:01:48 +00002156 goto again;
2157 }
plougher1e380702010-08-11 01:52:49 +00002158read_err:
Phillip Lougher2302f692012-12-27 04:05:02 +00002159 close(file);
2160read_err2:
plougher1e380702010-08-11 01:52:49 +00002161 file_buffer->error = TRUE;
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002162 put_file_buffer(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002163}
2164
2165
2166void reader_scan(struct dir_info *dir) {
Phillip Lougherbf338362012-08-22 05:24:36 +01002167 struct dir_ent *dir_ent = dir->list;
plougher5507dd92006-11-06 00:43:10 +00002168
Phillip Lougherbf338362012-08-22 05:24:36 +01002169 for(; dir_ent; dir_ent = dir_ent->next) {
plougher5507dd92006-11-06 00:43:10 +00002170 struct stat *buf = &dir_ent->inode->buf;
ploughera326c182009-08-29 05:41:45 +00002171 if(dir_ent->inode->root_entry)
plougher5507dd92006-11-06 00:43:10 +00002172 continue;
2173
plougherb3977eb2010-05-02 02:08:48 +00002174 if(IS_PSEUDO_PROCESS(dir_ent->inode)) {
plougher00d08172009-09-03 10:17:44 +00002175 reader_read_process(dir_ent);
2176 continue;
2177 }
2178
plougher5507dd92006-11-06 00:43:10 +00002179 switch(buf->st_mode & S_IFMT) {
2180 case S_IFREG:
2181 reader_read_file(dir_ent);
2182 break;
2183 case S_IFDIR:
2184 reader_scan(dir_ent->dir);
2185 break;
2186 }
2187 }
2188}
2189
2190
2191void *reader(void *arg)
2192{
plougher5507dd92006-11-06 00:43:10 +00002193 if(!sorted)
2194 reader_scan(queue_get(to_reader));
2195 else {
2196 int i;
2197 struct priority_entry *entry;
2198
2199 queue_get(to_reader);
2200 for(i = 65535; i >= 0; i--)
plougher16111452010-07-22 05:12:18 +00002201 for(entry = priority_list[i]; entry;
2202 entry = entry->next)
plougher5507dd92006-11-06 00:43:10 +00002203 reader_read_file(entry->dir);
2204 }
rloughere4873e02007-11-08 17:50:42 +00002205
2206 pthread_exit(NULL);
plougher5507dd92006-11-06 00:43:10 +00002207}
2208
2209
2210void *writer(void *arg)
2211{
plougher5507dd92006-11-06 00:43:10 +00002212 while(1) {
2213 struct file_buffer *file_buffer = queue_get(to_writer);
2214 off_t off;
2215
2216 if(file_buffer == NULL) {
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002217 queue_put(from_writer, NULL);
plougher5507dd92006-11-06 00:43:10 +00002218 continue;
2219 }
2220
2221 off = file_buffer->block;
2222
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002223 pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
plougher5507dd92006-11-06 00:43:10 +00002224 pthread_mutex_lock(&pos_mutex);
2225
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002226 if(lseek(fd, off, SEEK_SET) == -1) {
Phillip Lougherd23000c2013-02-06 22:05:45 +00002227 ERROR("writer: Lseek on destination failed because "
2228 "%s, offset=0x%llx\n", strerror(errno), off);
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01002229 BAD_ERROR("Probably out of space on output "
Phillip Lougher3f4ccd22013-02-11 04:40:09 +00002230 "%s\n", block_device ? "block device" :
2231 "filesystem");
plougher5507dd92006-11-06 00:43:10 +00002232 }
2233
Phillip Lougherd2ffa3c2013-02-11 02:53:54 +00002234 if(write_bytes(fd, file_buffer->data,
Phillip Lougher9bfa0b82013-04-06 05:05:15 +01002235 file_buffer->size) == -1)
2236 BAD_ERROR("Failed to write to output %s\n",
Phillip Loughere9df4552013-02-14 23:06:29 +00002237 block_device ? "block device" : "filesystem");
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002238
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01002239 pthread_cleanup_pop(1);
2240
ploughereb6eac92008-02-26 01:50:48 +00002241 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002242 }
2243}
2244
2245
plougher5b09fd42007-08-06 10:28:41 +00002246int all_zero(struct file_buffer *file_buffer)
2247{
2248 int i;
2249 long entries = file_buffer->size / sizeof(long);
2250 long *p = (long *) file_buffer->data;
2251
2252 for(i = 0; i < entries && p[i] == 0; i++);
2253
2254 if(i == entries) {
plougher110799c2009-03-30 01:50:40 +00002255 for(i = file_buffer->size & ~(sizeof(long) - 1);
plougher50b31762009-03-31 04:14:46 +00002256 i < file_buffer->size && file_buffer->data[i] == 0;
2257 i++);
plougher5b09fd42007-08-06 10:28:41 +00002258
2259 return i == file_buffer->size;
2260 }
2261
2262 return 0;
2263}
2264
2265
plougher5507dd92006-11-06 00:43:10 +00002266void *deflator(void *arg)
2267{
plougher7b8ee502009-07-29 07:54:30 +00002268 void *stream = NULL;
Phillip Lougher3c838292013-03-26 04:24:53 +00002269 int res;
plougher5507dd92006-11-06 00:43:10 +00002270
plougher13fdddf2010-11-24 01:23:41 +00002271 res = compressor_init(comp, &stream, block_size, 1);
2272 if(res)
2273 BAD_ERROR("deflator:: compressor_init failed\n");
2274
plougher5507dd92006-11-06 00:43:10 +00002275 while(1) {
Phillip Loughercf478e92013-05-29 02:38:41 +01002276 struct file_buffer *file_buffer = queue_get(to_deflate);
plougher1b899fc2008-08-07 01:24:06 +00002277 struct file_buffer *write_buffer;
plougher5507dd92006-11-06 00:43:10 +00002278
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002279 if(sparse_files && all_zero(file_buffer)) {
Phillip Lougher48854382011-09-09 03:36:55 +01002280 file_buffer->c_byte = 0;
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002281 seq_queue_put(to_main, file_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002282 } else {
Phillip Lougher45702012013-04-30 05:19:54 +01002283 write_buffer = cache_get(writer_buffer, -1);
plougher13fdddf2010-11-24 01:23:41 +00002284 write_buffer->c_byte = mangle2(stream,
plougher50b31762009-03-31 04:14:46 +00002285 write_buffer->data, file_buffer->data,
Phillip Lougher63f531f2011-09-10 04:03:32 +01002286 file_buffer->size, block_size,
2287 file_buffer->noD, 1);
plougher1b899fc2008-08-07 01:24:06 +00002288 write_buffer->sequence = file_buffer->sequence;
2289 write_buffer->file_size = file_buffer->file_size;
2290 write_buffer->block = file_buffer->block;
plougher110799c2009-03-30 01:50:40 +00002291 write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK
2292 (write_buffer->c_byte);
plougher1b899fc2008-08-07 01:24:06 +00002293 write_buffer->fragment = FALSE;
2294 write_buffer->error = FALSE;
2295 cache_block_put(file_buffer);
Phillip Lougher0e1656d2013-05-20 03:47:24 +01002296 seq_queue_put(to_main, write_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002297 }
plougher5507dd92006-11-06 00:43:10 +00002298 }
2299}
2300
2301
2302void *frag_deflator(void *arg)
2303{
plougher7b8ee502009-07-29 07:54:30 +00002304 void *stream = NULL;
Phillip Lougher3c838292013-03-26 04:24:53 +00002305 int res;
plougher5507dd92006-11-06 00:43:10 +00002306
plougher13fdddf2010-11-24 01:23:41 +00002307 res = compressor_init(comp, &stream, block_size, 1);
2308 if(res)
2309 BAD_ERROR("frag_deflator:: compressor_init failed\n");
2310
Phillip Lougher53240e62013-04-30 23:51:29 +01002311 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
2312
plougher5507dd92006-11-06 00:43:10 +00002313 while(1) {
2314 int c_byte, compressed_size;
2315 struct file_buffer *file_buffer = queue_get(to_frag);
plougher110799c2009-03-30 01:50:40 +00002316 struct file_buffer *write_buffer =
Phillip Lougher45702012013-04-30 05:19:54 +01002317 cache_get(writer_buffer,
2318 FRAG_INDEX(file_buffer->block));
plougher5507dd92006-11-06 00:43:10 +00002319
plougher13fdddf2010-11-24 01:23:41 +00002320 c_byte = mangle2(stream, write_buffer->data, file_buffer->data,
plougher110799c2009-03-30 01:50:40 +00002321 file_buffer->size, block_size, noF, 1);
plougher5507dd92006-11-06 00:43:10 +00002322 compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
plougherd036a312008-03-08 01:38:27 +00002323 write_buffer->size = compressed_size;
plougherd1139d52008-04-28 03:07:07 +00002324 pthread_mutex_lock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00002325 if(fragments_locked == FALSE) {
plougher2ea89142008-03-11 01:34:19 +00002326 fragment_table[file_buffer->block].size = c_byte;
2327 fragment_table[file_buffer->block].start_block = bytes;
2328 write_buffer->block = bytes;
2329 bytes += compressed_size;
2330 fragments_outstanding --;
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002331 pthread_mutex_unlock(&fragment_mutex);
plougher2ea89142008-03-11 01:34:19 +00002332 queue_put(to_writer, write_buffer);
plougher110799c2009-03-30 01:50:40 +00002333 TRACE("Writing fragment %lld, uncompressed size %d, "
2334 "compressed size %d\n", file_buffer->block,
2335 file_buffer->size, compressed_size);
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002336 } else {
plougher110799c2009-03-30 01:50:40 +00002337 add_pending_fragment(write_buffer, c_byte,
2338 file_buffer->block);
Phillip Lougher39d7c4f2013-04-30 23:42:28 +01002339 pthread_mutex_unlock(&fragment_mutex);
2340 }
ploughereb6eac92008-02-26 01:50:48 +00002341 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002342 }
Phillip Lougher53240e62013-04-30 23:51:29 +01002343
2344 pthread_cleanup_pop(0);
plougher5507dd92006-11-06 00:43:10 +00002345}
2346
2347
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002348struct file_buffer *get_file_buffer()
plougher5507dd92006-11-06 00:43:10 +00002349{
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002350 struct file_buffer *file_buffer = seq_queue_get(to_main);
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002351
2352 if(file_buffer->fragment) {
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002353 if(sparse_files && all_zero(file_buffer)) {
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002354 file_buffer->c_byte = 0;
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002355 file_buffer->fragment = FALSE;
2356 } else
Phillip Lougher4bb21cb2013-05-19 04:27:38 +01002357 file_buffer->c_byte = file_buffer->size;
2358 }
2359
2360 return file_buffer;
plougher5507dd92006-11-06 00:43:10 +00002361}
2362
2363
plougher110799c2009-03-30 01:50:40 +00002364void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent,
2365 int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002366{
2367 file_count ++;
2368 *duplicate_file = FALSE;
plougherc1ace522010-05-01 03:00:45 +00002369 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0,
2370 NULL, &empty_fragment, NULL, 0);
plougher5507dd92006-11-06 00:43:10 +00002371}
2372
2373
plougher50b31762009-03-31 04:14:46 +00002374void write_file_frag_dup(squashfs_inode *inode, struct dir_ent *dir_ent,
2375 int size, int *duplicate_file, struct file_buffer *file_buffer,
plougher360514a2009-03-30 03:01:38 +00002376 unsigned short checksum)
plougher5507dd92006-11-06 00:43:10 +00002377{
plougher5507dd92006-11-06 00:43:10 +00002378 struct file_info *dupl_ptr;
plougher1f413c82005-11-18 00:02:14 +00002379 struct fragment *fragment;
plougher5507dd92006-11-06 00:43:10 +00002380 unsigned int *block_listp = NULL;
2381 long long start = 0;
plougherf9c72b12006-01-23 13:52:40 +00002382
plougher50b31762009-03-31 04:14:46 +00002383 dupl_ptr = duplicate(size, 0, &block_listp, &start, &fragment,
2384 file_buffer, 0, 0, checksum, TRUE);
plougher1f413c82005-11-18 00:02:14 +00002385
plougher5507dd92006-11-06 00:43:10 +00002386 if(dupl_ptr) {
2387 *duplicate_file = FALSE;
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002388 fragment = get_and_fill_fragment(file_buffer, dir_ent);
plougher5507dd92006-11-06 00:43:10 +00002389 dupl_ptr->fragment = fragment;
2390 } else
2391 *duplicate_file = TRUE;
2392
ploughereb6eac92008-02-26 01:50:48 +00002393 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002394
2395 total_bytes += size;
2396 file_count ++;
2397
plougher35a10602008-04-21 02:58:16 +00002398 inc_progress_bar();
plougher02bc3bc2007-02-25 12:12:01 +00002399
plougherc1ace522010-05-01 03:00:45 +00002400 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0,
plougher3c6bdb52010-05-01 02:30:59 +00002401 0, NULL, fragment, NULL, 0);
plougher5507dd92006-11-06 00:43:10 +00002402}
2403
2404
plougher110799c2009-03-30 01:50:40 +00002405void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, int size,
2406 struct file_buffer *file_buffer, int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002407{
2408 struct fragment *fragment;
2409 unsigned short checksum;
plougher5507dd92006-11-06 00:43:10 +00002410
2411 checksum = get_checksum_mem_buffer(file_buffer);
2412
plougher29e37092007-04-15 01:24:51 +00002413 if(pre_duplicate_frag(size, checksum)) {
plougher110799c2009-03-30 01:50:40 +00002414 write_file_frag_dup(inode, dir_ent, size, duplicate_file,
2415 file_buffer, checksum);
plougher29e37092007-04-15 01:24:51 +00002416 return;
2417 }
plougher5507dd92006-11-06 00:43:10 +00002418
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002419 fragment = get_and_fill_fragment(file_buffer, dir_ent);
plougher5507dd92006-11-06 00:43:10 +00002420
ploughereb6eac92008-02-26 01:50:48 +00002421 cache_block_put(file_buffer);
plougher5507dd92006-11-06 00:43:10 +00002422
2423 if(duplicate_checking)
2424 add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, TRUE);
2425
2426 total_bytes += size;
2427 file_count ++;
2428
2429 *duplicate_file = FALSE;
2430
plougher35a10602008-04-21 02:58:16 +00002431 inc_progress_bar();
plougher02bc3bc2007-02-25 12:12:01 +00002432
plougherc1ace522010-05-01 03:00:45 +00002433 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0,
plougher3c6bdb52010-05-01 02:30:59 +00002434 0, NULL, fragment, NULL, 0);
plougher29e37092007-04-15 01:24:51 +00002435
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002436 if(duplicate_checking == FALSE)
2437 free_fragment(fragment);
2438
plougher018d2b32007-04-23 03:01:48 +00002439 return;
plougher5507dd92006-11-06 00:43:10 +00002440}
2441
2442
plougher00d08172009-09-03 10:17:44 +00002443int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent,
2444 struct file_buffer *read_buffer, int *duplicate_file)
2445{
2446 long long read_size, file_bytes, start;
2447 struct fragment *fragment;
2448 unsigned int *block_list = NULL;
2449 int block = 0, status;
2450 long long sparse = 0;
2451 struct file_buffer *fragment_buffer = NULL;
2452
2453 *duplicate_file = FALSE;
2454
2455 lock_fragments();
2456
2457 file_bytes = 0;
2458 start = bytes;
2459 while (1) {
2460 read_size = read_buffer->file_size;
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002461 if(read_buffer->fragment)
plougher00d08172009-09-03 10:17:44 +00002462 fragment_buffer = read_buffer;
2463 else {
2464 block_list = realloc(block_list, (block + 1) *
2465 sizeof(unsigned int));
2466 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002467 MEM_ERROR();
plougher00d08172009-09-03 10:17:44 +00002468 block_list[block ++] = read_buffer->c_byte;
2469 if(read_buffer->c_byte) {
2470 read_buffer->block = bytes;
2471 bytes += read_buffer->size;
2472 cache_rehash(read_buffer, read_buffer->block);
2473 file_bytes += read_buffer->size;
2474 queue_put(to_writer, read_buffer);
2475 } else {
2476 sparse += read_buffer->size;
2477 cache_block_put(read_buffer);
2478 }
2479 }
plougher286b6b32009-09-19 03:29:27 +00002480 inc_progress_bar();
plougher00d08172009-09-03 10:17:44 +00002481
2482 if(read_size != -1)
2483 break;
2484
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002485 read_buffer = get_file_buffer();
plougher00d08172009-09-03 10:17:44 +00002486 if(read_buffer->error)
2487 goto read_err;
2488 }
2489
2490 unlock_fragments();
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002491 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher00d08172009-09-03 10:17:44 +00002492 cache_block_put(fragment_buffer);
2493
2494 if(duplicate_checking)
2495 add_non_dup(read_size, file_bytes, block_list, start, fragment,
2496 0, 0, FALSE);
2497 file_count ++;
2498 total_bytes += read_size;
2499
plougherc1ace522010-05-01 03:00:45 +00002500 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2501 block, block_list, fragment, NULL, sparse);
plougher00d08172009-09-03 10:17:44 +00002502
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002503 if(duplicate_checking == FALSE) {
plougher00d08172009-09-03 10:17:44 +00002504 free(block_list);
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002505 free_fragment(fragment);
2506 }
plougher00d08172009-09-03 10:17:44 +00002507
2508 return 0;
2509
2510read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002511 dec_progress_bar(block);
plougher00d08172009-09-03 10:17:44 +00002512 status = read_buffer->error;
2513 bytes = start;
2514 if(!block_device) {
2515 int res;
2516
2517 queue_put(to_writer, NULL);
2518 if(queue_get(from_writer) != 0)
2519 EXIT_MKSQUASHFS();
2520 res = ftruncate(fd, bytes);
2521 if(res != 0)
2522 BAD_ERROR("Failed to truncate dest file because %s\n",
2523 strerror(errno));
2524 }
2525 unlock_fragments();
2526 free(block_list);
2527 cache_block_put(read_buffer);
2528 return status;
2529}
2530
2531
plougher110799c2009-03-30 01:50:40 +00002532int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent,
plougher50b31762009-03-31 04:14:46 +00002533 long long read_size, struct file_buffer *read_buffer,
2534 int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002535{
plougher23377982007-11-12 04:04:48 +00002536 long long file_bytes, start;
plougher5507dd92006-11-06 00:43:10 +00002537 struct fragment *fragment;
plougher5507dd92006-11-06 00:43:10 +00002538 unsigned int *block_list;
plougher1b899fc2008-08-07 01:24:06 +00002539 int block, status;
2540 int blocks = (read_size + block_size - 1) >> block_log;
2541 long long sparse = 0;
2542 struct file_buffer *fragment_buffer = NULL;
plougher5507dd92006-11-06 00:43:10 +00002543
plougher29e37092007-04-15 01:24:51 +00002544 *duplicate_file = FALSE;
2545
plougher87139622010-12-16 05:19:30 +00002546 block_list = malloc(blocks * sizeof(unsigned int));
2547 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002548 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00002549
plougher2ea89142008-03-11 01:34:19 +00002550 lock_fragments();
plougher1f413c82005-11-18 00:02:14 +00002551
plougher5507dd92006-11-06 00:43:10 +00002552 file_bytes = 0;
2553 start = bytes;
plougher1b899fc2008-08-07 01:24:06 +00002554 for(block = 0; block < blocks;) {
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002555 if(read_buffer->fragment) {
Phillip Lougher2760d822013-01-13 21:14:06 +00002556 block_list[block] = 0;
plougher1b899fc2008-08-07 01:24:06 +00002557 fragment_buffer = read_buffer;
2558 blocks = read_size >> block_log;
plougher018d2b32007-04-23 03:01:48 +00002559 } else {
plougher1b899fc2008-08-07 01:24:06 +00002560 block_list[block] = read_buffer->c_byte;
2561 if(read_buffer->c_byte) {
2562 read_buffer->block = bytes;
2563 bytes += read_buffer->size;
2564 cache_rehash(read_buffer, read_buffer->block);
2565 file_bytes += read_buffer->size;
2566 queue_put(to_writer, read_buffer);
2567 } else {
2568 sparse += read_buffer->size;
2569 cache_block_put(read_buffer);
2570 }
2571 }
2572 inc_progress_bar();
2573
2574 if(++block < blocks) {
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002575 read_buffer = get_file_buffer();
plougher018d2b32007-04-23 03:01:48 +00002576 if(read_buffer->error)
2577 goto read_err;
2578 }
plougher1f413c82005-11-18 00:02:14 +00002579 }
2580
plougher2ea89142008-03-11 01:34:19 +00002581 unlock_fragments();
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002582 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher1b899fc2008-08-07 01:24:06 +00002583 cache_block_put(fragment_buffer);
plougher5507dd92006-11-06 00:43:10 +00002584
plougher1f413c82005-11-18 00:02:14 +00002585 if(duplicate_checking)
plougher50b31762009-03-31 04:14:46 +00002586 add_non_dup(read_size, file_bytes, block_list, start, fragment,
2587 0, 0, FALSE);
plougher1f413c82005-11-18 00:02:14 +00002588 file_count ++;
plougher5507dd92006-11-06 00:43:10 +00002589 total_bytes += read_size;
plougher29e37092007-04-15 01:24:51 +00002590
plougher360514a2009-03-30 03:01:38 +00002591 /*
2592 * sparse count is needed to ensure squashfs correctly reports a
plougher1b899fc2008-08-07 01:24:06 +00002593 * a smaller block count on stat calls to sparse files. This is
2594 * to ensure intelligent applications like cp correctly handle the
2595 * file as a sparse file. If the file in the original filesystem isn't
2596 * stored as a sparse file then still store it sparsely in squashfs, but
plougher360514a2009-03-30 03:01:38 +00002597 * report it as non-sparse on stat calls to preserve semantics
2598 */
plougher1b899fc2008-08-07 01:24:06 +00002599 if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2600 sparse = 0;
2601
plougherc1ace522010-05-01 03:00:45 +00002602 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start,
2603 blocks, block_list, fragment, NULL, sparse);
plougher29e37092007-04-15 01:24:51 +00002604
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002605 if(duplicate_checking == FALSE) {
plougherf9c72b12006-01-23 13:52:40 +00002606 free(block_list);
Phillip Lougherd2f045f2012-12-28 03:15:45 +00002607 free_fragment(fragment);
2608 }
plougher29e37092007-04-15 01:24:51 +00002609
plougher018d2b32007-04-23 03:01:48 +00002610 return 0;
plougher1f413c82005-11-18 00:02:14 +00002611
2612read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002613 dec_progress_bar(block);
plougher018d2b32007-04-23 03:01:48 +00002614 status = read_buffer->error;
plougher1b899fc2008-08-07 01:24:06 +00002615 bytes = start;
2616 if(!block_device) {
plougher12a159a2009-03-03 11:06:34 +00002617 int res;
2618
plougher5507dd92006-11-06 00:43:10 +00002619 queue_put(to_writer, NULL);
2620 if(queue_get(from_writer) != 0)
2621 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002622 res = ftruncate(fd, bytes);
2623 if(res != 0)
2624 BAD_ERROR("Failed to truncate dest file because %s\n",
2625 strerror(errno));
plougher5507dd92006-11-06 00:43:10 +00002626 }
plougher2ea89142008-03-11 01:34:19 +00002627 unlock_fragments();
plougherf9c72b12006-01-23 13:52:40 +00002628 free(block_list);
ploughereb6eac92008-02-26 01:50:48 +00002629 cache_block_put(read_buffer);
plougher018d2b32007-04-23 03:01:48 +00002630 return status;
plougher1f413c82005-11-18 00:02:14 +00002631}
2632
2633
plougher110799c2009-03-30 01:50:40 +00002634int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent,
plougher50b31762009-03-31 04:14:46 +00002635 long long read_size, struct file_buffer *read_buffer,
2636 int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002637{
plougher29e37092007-04-15 01:24:51 +00002638 int block, thresh;
plougher1b899fc2008-08-07 01:24:06 +00002639 long long file_bytes, dup_start, start;
plougher5507dd92006-11-06 00:43:10 +00002640 struct fragment *fragment;
2641 struct file_info *dupl_ptr;
2642 int blocks = (read_size + block_size - 1) >> block_log;
2643 unsigned int *block_list, *block_listp;
plougher1b899fc2008-08-07 01:24:06 +00002644 struct file_buffer **buffer_list;
plougher4e8484a2008-03-15 23:30:16 +00002645 int status, num_locked_fragments;
plougher1b899fc2008-08-07 01:24:06 +00002646 long long sparse = 0;
2647 struct file_buffer *fragment_buffer = NULL;
plougher5507dd92006-11-06 00:43:10 +00002648
plougher50b31762009-03-31 04:14:46 +00002649 block_list = malloc(blocks * sizeof(unsigned int));
2650 if(block_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002651 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00002652 block_listp = block_list;
2653
plougher50b31762009-03-31 04:14:46 +00002654 buffer_list = malloc(blocks * sizeof(struct file_buffer *));
2655 if(buffer_list == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002656 MEM_ERROR();
plougher5507dd92006-11-06 00:43:10 +00002657
plougher4e8484a2008-03-15 23:30:16 +00002658 num_locked_fragments = lock_fragments();
plougher5507dd92006-11-06 00:43:10 +00002659
2660 file_bytes = 0;
plougher1b899fc2008-08-07 01:24:06 +00002661 start = dup_start = bytes;
plougher110799c2009-03-30 01:50:40 +00002662 thresh = blocks > (writer_buffer_size - num_locked_fragments) ?
2663 blocks - (writer_buffer_size - num_locked_fragments): 0;
plougher1b899fc2008-08-07 01:24:06 +00002664
2665 for(block = 0; block < blocks;) {
Phillip Lougherb1ed0df2013-05-20 04:44:04 +01002666 if(read_buffer->fragment) {
Phillip Lougher38cb1ab2013-01-13 21:20:09 +00002667 block_list[block] = 0;
Phillip Lougher19de5b62013-01-13 21:31:51 +00002668 buffer_list[block] = NULL;
plougher1b899fc2008-08-07 01:24:06 +00002669 fragment_buffer = read_buffer;
2670 blocks = read_size >> block_log;
2671 } else {
2672 block_list[block] = read_buffer->c_byte;
2673
2674 if(read_buffer->c_byte) {
2675 read_buffer->block = bytes;
2676 bytes += read_buffer->size;
2677 file_bytes += read_buffer->size;
2678 cache_rehash(read_buffer, read_buffer->block);
2679 if(block < thresh) {
2680 buffer_list[block] = NULL;
2681 queue_put(to_writer, read_buffer);
2682 } else
2683 buffer_list[block] = read_buffer;
2684 } else {
2685 buffer_list[block] = NULL;
2686 sparse += read_buffer->size;
2687 cache_block_put(read_buffer);
2688 }
2689 }
2690 inc_progress_bar();
2691
2692 if(++block < blocks) {
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002693 read_buffer = get_file_buffer();
plougher018d2b32007-04-23 03:01:48 +00002694 if(read_buffer->error)
2695 goto read_err;
2696 }
plougher5507dd92006-11-06 00:43:10 +00002697 }
2698
plougher110799c2009-03-30 01:50:40 +00002699 dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start,
2700 &fragment, fragment_buffer, blocks, 0, 0, FALSE);
plougher5507dd92006-11-06 00:43:10 +00002701
2702 if(dupl_ptr) {
2703 *duplicate_file = FALSE;
2704 for(block = thresh; block < blocks; block ++)
plougher1b899fc2008-08-07 01:24:06 +00002705 if(buffer_list[block])
2706 queue_put(to_writer, buffer_list[block]);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01002707 fragment = get_and_fill_fragment(fragment_buffer, dir_ent);
plougher5507dd92006-11-06 00:43:10 +00002708 dupl_ptr->fragment = fragment;
2709 } else {
2710 *duplicate_file = TRUE;
2711 for(block = thresh; block < blocks; block ++)
plougher1b899fc2008-08-07 01:24:06 +00002712 cache_block_put(buffer_list[block]);
2713 bytes = start;
2714 if(thresh && !block_device) {
plougher12a159a2009-03-03 11:06:34 +00002715 int res;
2716
plougher1b899fc2008-08-07 01:24:06 +00002717 queue_put(to_writer, NULL);
2718 if(queue_get(from_writer) != 0)
2719 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002720 res = ftruncate(fd, bytes);
2721 if(res != 0)
2722 BAD_ERROR("Failed to truncate dest file because"
2723 " %s\n", strerror(errno));
plougher1b899fc2008-08-07 01:24:06 +00002724 }
plougher5507dd92006-11-06 00:43:10 +00002725 }
2726
plougher2ea89142008-03-11 01:34:19 +00002727 unlock_fragments();
plougher1b899fc2008-08-07 01:24:06 +00002728 cache_block_put(fragment_buffer);
plougher5507dd92006-11-06 00:43:10 +00002729 free(buffer_list);
2730 file_count ++;
2731 total_bytes += read_size;
2732
plougher360514a2009-03-30 03:01:38 +00002733 /*
2734 * sparse count is needed to ensure squashfs correctly reports a
plougher1b899fc2008-08-07 01:24:06 +00002735 * a smaller block count on stat calls to sparse files. This is
2736 * to ensure intelligent applications like cp correctly handle the
2737 * file as a sparse file. If the file in the original filesystem isn't
2738 * stored as a sparse file then still store it sparsely in squashfs, but
plougher360514a2009-03-30 03:01:38 +00002739 * report it as non-sparse on stat calls to preserve semantics
2740 */
plougher1b899fc2008-08-07 01:24:06 +00002741 if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size)
2742 sparse = 0;
2743
plougherc1ace522010-05-01 03:00:45 +00002744 create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size,
2745 dup_start, blocks, block_listp, fragment, NULL, sparse);
plougher29e37092007-04-15 01:24:51 +00002746
plougher5507dd92006-11-06 00:43:10 +00002747 if(*duplicate_file == TRUE)
2748 free(block_list);
plougher29e37092007-04-15 01:24:51 +00002749
plougher018d2b32007-04-23 03:01:48 +00002750 return 0;
plougher5507dd92006-11-06 00:43:10 +00002751
2752read_err:
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002753 dec_progress_bar(block);
plougher018d2b32007-04-23 03:01:48 +00002754 status = read_buffer->error;
plougher1b899fc2008-08-07 01:24:06 +00002755 bytes = start;
2756 if(thresh && !block_device) {
plougher12a159a2009-03-03 11:06:34 +00002757 int res;
2758
plougher5507dd92006-11-06 00:43:10 +00002759 queue_put(to_writer, NULL);
2760 if(queue_get(from_writer) != 0)
2761 EXIT_MKSQUASHFS();
plougher12a159a2009-03-03 11:06:34 +00002762 res = ftruncate(fd, bytes);
2763 if(res != 0)
2764 BAD_ERROR("Failed to truncate dest file because %s\n",
2765 strerror(errno));
plougher5507dd92006-11-06 00:43:10 +00002766 }
plougher2ea89142008-03-11 01:34:19 +00002767 unlock_fragments();
plougher5507dd92006-11-06 00:43:10 +00002768 for(blocks = thresh; blocks < block; blocks ++)
plougher1b899fc2008-08-07 01:24:06 +00002769 cache_block_put(buffer_list[blocks]);
plougher5507dd92006-11-06 00:43:10 +00002770 free(buffer_list);
2771 free(block_list);
ploughereb6eac92008-02-26 01:50:48 +00002772 cache_block_put(read_buffer);
plougher018d2b32007-04-23 03:01:48 +00002773 return status;
plougher5507dd92006-11-06 00:43:10 +00002774}
2775
2776
plougher110799c2009-03-30 01:50:40 +00002777void write_file(squashfs_inode *inode, struct dir_ent *dir_ent,
2778 int *duplicate_file)
plougher5507dd92006-11-06 00:43:10 +00002779{
plougher018d2b32007-04-23 03:01:48 +00002780 int status;
2781 struct file_buffer *read_buffer;
2782 long long read_size;
plougher5507dd92006-11-06 00:43:10 +00002783
plougher018d2b32007-04-23 03:01:48 +00002784again:
Phillip Lougherc6a1bc52013-05-20 04:08:42 +01002785 read_buffer = get_file_buffer();
plougher1b899fc2008-08-07 01:24:06 +00002786
plougher23377982007-11-12 04:04:48 +00002787 status = read_buffer->error;
2788 if(status) {
ploughereb6eac92008-02-26 01:50:48 +00002789 cache_block_put(read_buffer);
plougher018d2b32007-04-23 03:01:48 +00002790 goto file_err;
2791 }
2792
2793 read_size = read_buffer->file_size;
plougher5507dd92006-11-06 00:43:10 +00002794
plougher00d08172009-09-03 10:17:44 +00002795 if(read_size == -1)
2796 status = write_file_process(inode, dir_ent, read_buffer,
2797 duplicate_file);
2798 else if(read_size == 0) {
plougher29e37092007-04-15 01:24:51 +00002799 write_file_empty(inode, dir_ent, duplicate_file);
ploughereb6eac92008-02-26 01:50:48 +00002800 cache_block_put(read_buffer);
plougher1b899fc2008-08-07 01:24:06 +00002801 } else if(read_buffer->fragment && read_buffer->c_byte)
plougher110799c2009-03-30 01:50:40 +00002802 write_file_frag(inode, dir_ent, read_size, read_buffer,
2803 duplicate_file);
plougher29e37092007-04-15 01:24:51 +00002804 else if(pre_duplicate(read_size))
plougher110799c2009-03-30 01:50:40 +00002805 status = write_file_blocks_dup(inode, dir_ent, read_size,
2806 read_buffer, duplicate_file);
plougher29e37092007-04-15 01:24:51 +00002807 else
plougher50b31762009-03-31 04:14:46 +00002808 status = write_file_blocks(inode, dir_ent, read_size,
2809 read_buffer, duplicate_file);
plougher5507dd92006-11-06 00:43:10 +00002810
plougher018d2b32007-04-23 03:01:48 +00002811file_err:
2812 if(status == 2) {
plougher50b31762009-03-31 04:14:46 +00002813 ERROR("File %s changed size while reading filesystem, "
Phillip Lougher494479f2012-02-03 15:45:41 +00002814 "attempting to re-read\n", pathname(dir_ent));
plougher018d2b32007-04-23 03:01:48 +00002815 goto again;
2816 } else if(status == 1) {
plougher110799c2009-03-30 01:50:40 +00002817 ERROR("Failed to read file %s, creating empty file\n",
Phillip Lougher494479f2012-02-03 15:45:41 +00002818 pathname(dir_ent));
plougher29e37092007-04-15 01:24:51 +00002819 write_file_empty(inode, dir_ent, duplicate_file);
2820 }
plougher5507dd92006-11-06 00:43:10 +00002821}
2822
2823
Phillip Lougher3f81a772012-12-04 05:07:24 +00002824#define BUFF_SIZE 512
plougher1f413c82005-11-18 00:02:14 +00002825char *name;
2826char *basename_r();
2827
2828char *getbase(char *pathname)
2829{
Phillip Lougher3f81a772012-12-04 05:07:24 +00002830 static char *b_buffer = NULL;
2831 static int b_size = BUFF_SIZE;
plougher1f413c82005-11-18 00:02:14 +00002832 char *result;
2833
Phillip Lougher3f81a772012-12-04 05:07:24 +00002834 if(b_buffer == NULL) {
2835 b_buffer = malloc(b_size);
2836 if(b_buffer == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002837 MEM_ERROR();
Phillip Lougher3f81a772012-12-04 05:07:24 +00002838 }
2839
2840 while(1) {
2841 if(*pathname != '/') {
2842 result = getcwd(b_buffer, b_size);
2843 if(result == NULL && errno != ERANGE)
2844 BAD_ERROR("Getcwd failed in getbase\n");
2845
2846 /* enough room for pathname + "/" + '\0' terminator? */
2847 if(result && strlen(pathname) + 2 <=
2848 b_size - strlen(b_buffer)) {
2849 strcat(strcat(b_buffer, "/"), pathname);
2850 break;
2851 }
2852 } else if(strlen(pathname) < b_size) {
2853 strcpy(b_buffer, pathname);
2854 break;
2855 }
2856
2857 /* Buffer not large enough, realloc and try again */
2858 b_buffer = realloc(b_buffer, b_size += BUFF_SIZE);
2859 if(b_buffer == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002860 MEM_ERROR();
Phillip Lougher3f81a772012-12-04 05:07:24 +00002861 }
2862
plougher1f413c82005-11-18 00:02:14 +00002863 name = b_buffer;
2864 if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0))
2865 return NULL;
2866 else
2867 return result;
2868}
2869
2870
2871char *basename_r()
2872{
2873 char *s;
2874 char *p;
2875 int n = 1;
2876
2877 for(;;) {
2878 s = name;
2879 if(*name == '\0')
2880 return NULL;
2881 if(*name != '/') {
2882 while(*name != '\0' && *name != '/') name++;
2883 n = name - s;
2884 }
2885 while(*name == '/') name++;
2886 if(strncmp(s, ".", n) == 0)
2887 continue;
plougher110799c2009-03-30 01:50:40 +00002888 if((*name == '\0') || (strncmp(s, "..", n) == 0) ||
2889 ((p = basename_r()) == NULL)) {
plougher1f413c82005-11-18 00:02:14 +00002890 s[n] = '\0';
2891 return s;
2892 }
2893 if(strcmp(p, "..") == 0)
2894 continue;
2895 return p;
2896 }
2897}
2898
2899
Phillip Lougher81204c22012-07-25 03:30:30 +01002900struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id)
plougher1f413c82005-11-18 00:02:14 +00002901{
Phillip Lougherd6577802012-10-04 19:14:33 +01002902 int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino);
2903 struct inode_info *inode;
plougher1f413c82005-11-18 00:02:14 +00002904
Phillip Lougherd6577802012-10-04 19:14:33 +01002905 /*
2906 * Look-up inode in hash table, if it already exists we have a
2907 * hard-link, so increment the nlink count and return it.
2908 * Don't do the look-up for directories because we don't hard-link
2909 * directories.
2910 */
2911 if ((buf->st_mode & S_IFMT) != S_IFDIR) {
2912 for(inode = inode_info[ino_hash]; inode; inode = inode->next) {
2913 if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) {
2914 inode->nlink ++;
2915 return inode;
2916 }
plougher1f413c82005-11-18 00:02:14 +00002917 }
plougher1f413c82005-11-18 00:02:14 +00002918 }
2919
plougher288f6852010-12-16 05:22:55 +00002920 inode = malloc(sizeof(struct inode_info));
2921 if(inode == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002922 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00002923
2924 memcpy(&inode->buf, buf, sizeof(struct stat));
plougher5507dd92006-11-06 00:43:10 +00002925 inode->read = FALSE;
ploughera326c182009-08-29 05:41:45 +00002926 inode->root_entry = FALSE;
Phillip Lougher81204c22012-07-25 03:30:30 +01002927 inode->pseudo_file = pseudo;
2928 inode->pseudo_id = id;
plougher1f413c82005-11-18 00:02:14 +00002929 inode->inode = SQUASHFS_INVALID_BLK;
2930 inode->nlink = 1;
Phillip Lougher539c2b12012-07-30 20:14:52 +01002931 inode->inode_number = 0;
plougherdc86c3c2007-12-05 02:15:10 +00002932
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002933 /*
2934 * Copy filesystem wide defaults into inode, these filesystem
2935 * wide defaults may be altered on an individual inode basis by
2936 * user specified actions
2937 *
2938 */
2939 inode->no_fragments = no_fragments;
2940 inode->always_use_fragments = always_use_fragments;
Phillip Lougher63f531f2011-09-10 04:03:32 +01002941 inode->noD = noD;
2942 inode->noF = noF;
Phillip Lougher9b6e3412011-09-05 02:58:41 +01002943
plougherdc86c3c2007-12-05 02:15:10 +00002944 if((buf->st_mode & S_IFMT) == S_IFREG)
Phillip Lougher3b89ee82012-10-18 23:55:37 +01002945 progress_bar_size((buf->st_size + block_size - 1) >> block_log);
plougherdc86c3c2007-12-05 02:15:10 +00002946
Phillip Lougherd6577802012-10-04 19:14:33 +01002947 inode->next = inode_info[ino_hash];
2948 inode_info[ino_hash] = inode;
plougher1f413c82005-11-18 00:02:14 +00002949
2950 return inode;
2951}
2952
2953
Phillip Lougher81204c22012-07-25 03:30:30 +01002954inline struct inode_info *lookup_inode(struct stat *buf)
2955{
2956 return lookup_inode2(buf, 0, 0);
2957}
2958
2959
Phillip Lougher539c2b12012-07-30 20:14:52 +01002960inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this)
2961{
2962 if (inode->inode_number == 0)
2963 inode->inode_number = use_this ? : inode_no ++;
2964}
2965
2966
Phillip Lougher494479f2012-02-03 15:45:41 +00002967inline struct dir_ent *create_dir_entry(char *name, char *source_name,
2968 char *nonstandard_pathname, struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00002969{
Phillip Lougher494479f2012-02-03 15:45:41 +00002970 struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent));
2971 if(dir_ent == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00002972 MEM_ERROR();
Phillip Lougher494479f2012-02-03 15:45:41 +00002973
2974 dir_ent->name = name;
2975 dir_ent->source_name = source_name;
2976 dir_ent->nonstandard_pathname = nonstandard_pathname;
2977 dir_ent->our_dir = dir;
Phillip Lougherbf338362012-08-22 05:24:36 +01002978 dir_ent->next = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00002979
2980 return dir_ent;
2981}
2982
2983
2984inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir,
2985 struct inode_info *inode_info)
2986{
2987 struct dir_info *dir = dir_ent->our_dir;
2988
plougher1f413c82005-11-18 00:02:14 +00002989 if(sub_dir)
Phillip Lougher494479f2012-02-03 15:45:41 +00002990 sub_dir->dir_ent = dir_ent;
2991 dir_ent->inode = inode_info;
2992 dir_ent->dir = sub_dir;
2993
Phillip Lougherbf338362012-08-22 05:24:36 +01002994 dir_ent->next = dir->list;
2995 dir->list = dir_ent;
2996 dir->count++;
Phillip Lougher494479f2012-02-03 15:45:41 +00002997}
2998
2999
3000inline void add_dir_entry2(char *name, char *source_name,
3001 char *nonstandard_pathname, struct dir_info *sub_dir,
3002 struct inode_info *inode_info, struct dir_info *dir)
3003{
3004 struct dir_ent *dir_ent = create_dir_entry(name, source_name,
3005 nonstandard_pathname, dir);
3006
3007
3008 add_dir_entry(dir_ent, sub_dir, inode_info);
plougher1f413c82005-11-18 00:02:14 +00003009}
3010
3011
Phillip Lougher10a4b572012-02-18 23:26:10 +00003012inline void free_dir_entry(struct dir_ent *dir_ent)
3013{
Phillip Loughereb92b192012-10-01 03:39:00 +01003014 if(dir_ent->name)
Phillip Lougher10a4b572012-02-18 23:26:10 +00003015 free(dir_ent->name);
3016
Phillip Loughereb92b192012-10-01 03:39:00 +01003017 if(dir_ent->source_name)
Phillip Lougher10a4b572012-02-18 23:26:10 +00003018 free(dir_ent->source_name);
3019
3020 free(dir_ent);
3021}
3022
3023
Phillip Lougherad0f9212011-12-31 00:55:51 +00003024inline void add_excluded(struct dir_info *dir)
3025{
3026 dir->excluded ++;
3027}
3028
3029
Phillip Lougherabc3b492012-07-29 02:53:35 +01003030void dir_scan(squashfs_inode *inode, char *pathname,
Phillip Lougherbae0e422014-01-19 23:42:11 +00003031 struct dir_ent *(_readdir)(struct dir_info *), int progress)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003032{
3033 struct stat buf;
3034 struct dir_info *dir_info = dir_scan1(pathname, "", paths, _readdir, 1);
3035 struct dir_ent *dir_ent;
3036
3037 if(dir_info == NULL)
3038 return;
3039
Phillip Lougherf3d0f9c2012-11-14 05:36:58 +00003040 /*
3041 * Process most actions and any pseudo files
3042 */
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00003043 if(actions() || get_pseudo())
3044 dir_scan2(dir_info, get_pseudo());
Phillip Lougherf3d0f9c2012-11-14 05:36:58 +00003045
Phillip Loughere92cb512012-11-15 02:22:28 +00003046 /*
3047 * Process move actions
3048 */
3049 if(move_actions()) {
3050 dir_scan3(dir_info, dir_info);
3051 do_move_actions();
3052 }
3053
Phillip Lougher71ae14e2012-11-15 02:27:50 +00003054 /*
3055 * Process empty actions
3056 */
3057 if(empty_actions())
3058 dir_scan4(dir_info);
3059
Phillip Loughereb69dad2012-11-15 03:34:02 +00003060 /*
3061 * Sort directories and compute the inode numbers
3062 */
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003063 dir_scan5(dir_info);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003064
Phillip Lougherd0fe1d52012-10-09 03:34:34 +01003065 dir_ent = create_dir_entry("", NULL, pathname,
Phillip Lougher1eae83d2012-09-26 02:12:45 +01003066 scan1_opendir("", "", 0));
Phillip Lougherabc3b492012-07-29 02:53:35 +01003067
3068 if(pathname[0] == '\0') {
3069 /*
3070 * dummy top level directory, if multiple sources specified on
3071 * command line
3072 */
3073 memset(&buf, 0, sizeof(buf));
3074 buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR;
3075 buf.st_uid = getuid();
3076 buf.st_gid = getgid();
3077 buf.st_mtime = time(NULL);
3078 buf.st_dev = 0;
3079 buf.st_ino = 0;
3080 dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0);
3081 } else {
Phillip Lougher38a35ec2012-11-30 04:31:35 +00003082 if(lstat(pathname, &buf) == -1)
3083 /* source directory has disappeared? */
Phillip Lougherfe58b492012-12-15 01:27:07 +00003084 BAD_ERROR("Cannot stat source directory %s because %s\n",
Phillip Lougherabc3b492012-07-29 02:53:35 +01003085 pathname, strerror(errno));
Phillip Lougherabc3b492012-07-29 02:53:35 +01003086 dir_ent->inode = lookup_inode(&buf);
3087 }
3088
Phillip Lougher539c2b12012-07-30 20:14:52 +01003089 alloc_inode_no(dir_ent->inode, root_inode_number);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003090 dir_ent->dir = dir_info;
3091 dir_info->dir_ent = dir_ent;
3092
Andy Lutomirski3f31dcf2012-09-21 18:08:08 -07003093 eval_actions(dir_ent);
3094
Phillip Lougher19b877d2013-02-23 11:15:38 +00003095 if(sorted)
3096 generate_file_priorities(dir_info, 0,
Phillip Lougherabc3b492012-07-29 02:53:35 +01003097 &dir_info->dir_ent->inode->buf);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003098 queue_put(to_reader, dir_info);
3099 if(sorted)
3100 sort_files_and_write(dir_info);
Phillip Lougherbae0e422014-01-19 23:42:11 +00003101 set_progressbar_state(progress);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003102 dir_scan6(inode, dir_info);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003103 dir_ent->inode->inode = *inode;
3104 dir_ent->inode->type = SQUASHFS_DIR_TYPE;
plougher1f413c82005-11-18 00:02:14 +00003105}
3106
3107
Phillip Lougherabc3b492012-07-29 02:53:35 +01003108/*
3109 * dir_scan1 routines...
Phillip Loughere8630be2012-11-11 02:24:24 +00003110 * These scan the source directories into memory for processing.
3111 * Exclude actions are processed here (in contrast to the other actions)
3112 * because they affect what is scanned.
Phillip Lougherabc3b492012-07-29 02:53:35 +01003113 */
Phillip Lougherb38c1722012-02-09 23:26:19 +00003114struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth)
plougher1f413c82005-11-18 00:02:14 +00003115{
plougher1f413c82005-11-18 00:02:14 +00003116 struct dir_info *dir;
3117
plougher6da792b2010-12-16 05:25:39 +00003118 dir = malloc(sizeof(struct dir_info));
3119 if(dir == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003120 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00003121
Phillip Lougher04ef2442013-01-09 06:03:50 +00003122 if(pathname[0] != '\0') {
3123 dir->linuxdir = opendir(pathname);
3124 if(dir->linuxdir == NULL) {
3125 free(dir);
3126 return NULL;
3127 }
plougher1f413c82005-11-18 00:02:14 +00003128 }
Phillip Lougher04ef2442013-01-09 06:03:50 +00003129
Phillip Lougher3cb5d6c2013-01-09 05:50:29 +00003130 dir->pathname = strdup(pathname);
3131 dir->subpath = strdup(subpath);
Phillip Lougherded70fa2011-12-25 02:14:58 +00003132 dir->count = 0;
3133 dir->directory_count = 0;
plougher1f413c82005-11-18 00:02:14 +00003134 dir->dir_is_ldir = TRUE;
3135 dir->list = NULL;
Phillip Lougher0b5a1242011-12-25 03:22:42 +00003136 dir->depth = depth;
Phillip Lougherad0f9212011-12-31 00:55:51 +00003137 dir->excluded = 0;
plougher1f413c82005-11-18 00:02:14 +00003138
3139 return dir;
3140}
3141
3142
Phillip Lougher494479f2012-02-03 15:45:41 +00003143struct dir_ent *scan1_encomp_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003144{
plougher1f413c82005-11-18 00:02:14 +00003145 static int index = 0;
3146
plougher11266ba2010-12-16 05:34:30 +00003147 if(dir->count < old_root_entries) {
3148 int i;
3149
plougher1f413c82005-11-18 00:02:14 +00003150 for(i = 0; i < old_root_entries; i++) {
ploughera326c182009-08-29 05:41:45 +00003151 if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
plougher1f413c82005-11-18 00:02:14 +00003152 dir->directory_count ++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003153 add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
ploughera326c182009-08-29 05:41:45 +00003154 &old_root_entry[i].inode, dir);
plougher1f413c82005-11-18 00:02:14 +00003155 }
plougher11266ba2010-12-16 05:34:30 +00003156 }
plougher1f413c82005-11-18 00:02:14 +00003157
3158 while(index < source) {
Phillip Lougherc73ed322012-10-01 03:25:41 +01003159 char *basename = NULL;
3160 char *dir_name = getbase(source_path[index]);
Phillip Lougherbf338362012-08-22 05:24:36 +01003161 int pass = 1, res;
plougher89fe2c72010-12-16 05:31:34 +00003162
Phillip Lougherc73ed322012-10-01 03:25:41 +01003163 if(dir_name == NULL) {
plougher110799c2009-03-30 01:50:40 +00003164 ERROR("Bad source directory %s - skipping ...\n",
3165 source_path[index]);
plougher1f413c82005-11-18 00:02:14 +00003166 index ++;
3167 continue;
3168 }
Phillip Lougherc73ed322012-10-01 03:25:41 +01003169 dir_name = strdup(dir_name);
plougher1f413c82005-11-18 00:02:14 +00003170 for(;;) {
Phillip Lougherbf338362012-08-22 05:24:36 +01003171 struct dir_ent *dir_ent = dir->list;
3172
3173 for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3174 dir_ent = dir_ent->next);
3175 if(dir_ent == NULL)
plougher1f413c82005-11-18 00:02:14 +00003176 break;
plougher50b31762009-03-31 04:14:46 +00003177 ERROR("Source directory entry %s already used! - trying"
3178 " ", dir_name);
Phillip Lougherc73ed322012-10-01 03:25:41 +01003179 if(pass == 1)
3180 basename = dir_name;
3181 else
Phillip Lougherb38c1722012-02-09 23:26:19 +00003182 free(dir_name);
Phillip Lougher494479f2012-02-03 15:45:41 +00003183 res = asprintf(&dir_name, "%s_%d", basename, pass++);
3184 if(res == -1)
3185 BAD_ERROR("asprintf failed in "
3186 "scan1_encomp_readdir\n");
plougher1f413c82005-11-18 00:02:14 +00003187 ERROR("%s\n", dir_name);
3188 }
Phillip Lougherb38c1722012-02-09 23:26:19 +00003189 return create_dir_entry(dir_name, basename,
3190 source_path[index ++], dir);
plougher1f413c82005-11-18 00:02:14 +00003191 }
Phillip Lougher494479f2012-02-03 15:45:41 +00003192 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003193}
3194
3195
Phillip Lougher494479f2012-02-03 15:45:41 +00003196struct dir_ent *scan1_single_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003197{
3198 struct dirent *d_name;
plougher4925e172010-12-16 05:45:54 +00003199 int i;
plougher1f413c82005-11-18 00:02:14 +00003200
plougher4925e172010-12-16 05:45:54 +00003201 if(dir->count < old_root_entries) {
plougher1f413c82005-11-18 00:02:14 +00003202 for(i = 0; i < old_root_entries; i++) {
ploughera326c182009-08-29 05:41:45 +00003203 if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE)
plougher1f413c82005-11-18 00:02:14 +00003204 dir->directory_count ++;
Phillip Lougher494479f2012-02-03 15:45:41 +00003205 add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL,
ploughera326c182009-08-29 05:41:45 +00003206 &old_root_entry[i].inode, dir);
plougher1f413c82005-11-18 00:02:14 +00003207 }
plougher4925e172010-12-16 05:45:54 +00003208 }
plougher1f413c82005-11-18 00:02:14 +00003209
3210 if((d_name = readdir(dir->linuxdir)) != NULL) {
Phillip Lougherc73ed322012-10-01 03:25:41 +01003211 char *basename = NULL;
3212 char *dir_name = strdup(d_name->d_name);
Phillip Lougher494479f2012-02-03 15:45:41 +00003213 int pass = 1, res;
plougher4925e172010-12-16 05:45:54 +00003214
plougher1f413c82005-11-18 00:02:14 +00003215 for(;;) {
Phillip Lougherbf338362012-08-22 05:24:36 +01003216 struct dir_ent *dir_ent = dir->list;
3217
3218 for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0;
3219 dir_ent = dir_ent->next);
3220 if(dir_ent == NULL)
plougher1f413c82005-11-18 00:02:14 +00003221 break;
plougher50b31762009-03-31 04:14:46 +00003222 ERROR("Source directory entry %s already used! - trying"
3223 " ", dir_name);
Phillip Lougherc73ed322012-10-01 03:25:41 +01003224 if (pass == 1)
3225 basename = dir_name;
3226 else
Phillip Lougher494479f2012-02-03 15:45:41 +00003227 free(dir_name);
3228 res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++);
3229 if(res == -1)
3230 BAD_ERROR("asprintf failed in "
3231 "scan1_single_readdir\n");
plougher1f413c82005-11-18 00:02:14 +00003232 ERROR("%s\n", dir_name);
3233 }
Phillip Lougher494479f2012-02-03 15:45:41 +00003234 return create_dir_entry(dir_name, basename, NULL, dir);
plougher1f413c82005-11-18 00:02:14 +00003235 }
3236
Phillip Lougher494479f2012-02-03 15:45:41 +00003237 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003238}
3239
3240
Phillip Lougher494479f2012-02-03 15:45:41 +00003241struct dir_ent *scan1_readdir(struct dir_info *dir)
plougher1f413c82005-11-18 00:02:14 +00003242{
plougher480d9bf2010-12-18 02:28:39 +00003243 struct dirent *d_name = readdir(dir->linuxdir);
plougher1f413c82005-11-18 00:02:14 +00003244
Phillip Lougher494479f2012-02-03 15:45:41 +00003245 return d_name ?
3246 create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) :
3247 NULL;
plougher1f413c82005-11-18 00:02:14 +00003248}
3249
3250
plougher1f413c82005-11-18 00:02:14 +00003251void scan1_freedir(struct dir_info *dir)
3252{
plougherfbed12b2006-02-07 12:45:53 +00003253 if(dir->pathname[0] != '\0')
3254 closedir(dir->linuxdir);
plougher1f413c82005-11-18 00:02:14 +00003255}
3256
3257
Phillip Lougherb38c1722012-02-09 23:26:19 +00003258struct dir_info *dir_scan1(char *filename, char *subpath,
3259 struct pathnames *paths,
Phillip Lougher494479f2012-02-03 15:45:41 +00003260 struct dir_ent *(_readdir)(struct dir_info *), int depth)
plougher1f413c82005-11-18 00:02:14 +00003261{
Phillip Lougherb38c1722012-02-09 23:26:19 +00003262 struct dir_info *dir = scan1_opendir(filename, subpath, depth);
Phillip Lougher494479f2012-02-03 15:45:41 +00003263 struct dir_ent *dir_ent;
plougher1f413c82005-11-18 00:02:14 +00003264
plougher360b6e42010-12-18 02:40:28 +00003265 if(dir == NULL) {
Phillip Lougher494479f2012-02-03 15:45:41 +00003266 ERROR("Could not open %s, skipping...\n", filename);
Phillip Lougher87db5672013-01-09 05:40:46 +00003267 return NULL;
plougher1f413c82005-11-18 00:02:14 +00003268 }
3269
Phillip Lougher494479f2012-02-03 15:45:41 +00003270 while((dir_ent = _readdir(dir))) {
plougher360b6e42010-12-18 02:40:28 +00003271 struct dir_info *sub_dir;
3272 struct stat buf;
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00003273 struct pathnames *new = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003274 char *filename = pathname(dir_ent);
Phillip Lougher651d68d2013-01-13 05:38:13 +00003275 char *subpath = NULL;
Phillip Lougher494479f2012-02-03 15:45:41 +00003276 char *dir_name = dir_ent->name;
plougher1f413c82005-11-18 00:02:14 +00003277
Phillip Lougher494479f2012-02-03 15:45:41 +00003278 if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) {
Phillip Lougher10a4b572012-02-18 23:26:10 +00003279 free_dir_entry(dir_ent);
plougher1f413c82005-11-18 00:02:14 +00003280 continue;
Phillip Lougher494479f2012-02-03 15:45:41 +00003281 }
plougher1f413c82005-11-18 00:02:14 +00003282
3283 if(lstat(filename, &buf) == -1) {
Phillip Lougherfe58b492012-12-15 01:27:07 +00003284 ERROR("Cannot stat dir/file %s because %s, ignoring\n",
plougher110799c2009-03-30 01:50:40 +00003285 filename, strerror(errno));
Phillip Lougher10a4b572012-02-18 23:26:10 +00003286 free_dir_entry(dir_ent);
plougher1f413c82005-11-18 00:02:14 +00003287 continue;
3288 }
plougher29e37092007-04-15 01:24:51 +00003289
3290 if((buf.st_mode & S_IFMT) != S_IFREG &&
Phillip Loughere4ff36a2012-11-19 01:34:47 +00003291 (buf.st_mode & S_IFMT) != S_IFDIR &&
3292 (buf.st_mode & S_IFMT) != S_IFLNK &&
3293 (buf.st_mode & S_IFMT) != S_IFCHR &&
3294 (buf.st_mode & S_IFMT) != S_IFBLK &&
3295 (buf.st_mode & S_IFMT) != S_IFIFO &&
3296 (buf.st_mode & S_IFMT) != S_IFSOCK) {
plougher50b31762009-03-31 04:14:46 +00003297 ERROR("File %s has unrecognised filetype %d, ignoring"
3298 "\n", filename, buf.st_mode & S_IFMT);
Phillip Lougher10a4b572012-02-18 23:26:10 +00003299 free_dir_entry(dir_ent);
plougher29e37092007-04-15 01:24:51 +00003300 continue;
3301 }
3302
Phillip Lougherad0f9212011-12-31 00:55:51 +00003303 if((old_exclude && old_excluded(filename, &buf)) ||
Phillip Lougher651d68d2013-01-13 05:38:13 +00003304 (!old_exclude && excluded(dir_name, paths, &new))) {
Phillip Lougherad0f9212011-12-31 00:55:51 +00003305 add_excluded(dir);
Phillip Lougher10a4b572012-02-18 23:26:10 +00003306 free_dir_entry(dir_ent);
Phillip Lougher10d8de02011-08-29 00:14:48 +01003307 continue;
Phillip Lougherad0f9212011-12-31 00:55:51 +00003308 }
plougher1f413c82005-11-18 00:02:14 +00003309
Phillip Lougher651d68d2013-01-13 05:38:13 +00003310 if(exclude_actions()) {
3311 subpath = subpathname(dir_ent);
3312
3313 if(eval_exclude_actions(dir_name, filename, subpath,
3314 &buf, depth)) {
3315 add_excluded(dir);
3316 free_dir_entry(dir_ent);
3317 continue;
3318 }
3319 }
3320
plougher1f413c82005-11-18 00:02:14 +00003321 if((buf.st_mode & S_IFMT) == S_IFDIR) {
Phillip Lougher651d68d2013-01-13 05:38:13 +00003322 if(subpath == NULL)
3323 subpath = subpathname(dir_ent);
3324
Phillip Lougherb38c1722012-02-09 23:26:19 +00003325 sub_dir = dir_scan1(filename, subpath, new,
3326 scan1_readdir, depth + 1);
Phillip Lougher494479f2012-02-03 15:45:41 +00003327 if(sub_dir == NULL) {
Phillip Lougher10a4b572012-02-18 23:26:10 +00003328 free_dir_entry(dir_ent);
Phillip Lougher205c1e02013-01-07 01:37:56 +00003329 free(new);
plougher1f413c82005-11-18 00:02:14 +00003330 continue;
Phillip Lougher494479f2012-02-03 15:45:41 +00003331 }
Phillip Lougher6a78a2d2011-12-28 03:54:19 +00003332
plougher1f413c82005-11-18 00:02:14 +00003333 dir->directory_count ++;
3334 } else
3335 sub_dir = NULL;
3336
Phillip Lougher494479f2012-02-03 15:45:41 +00003337 add_dir_entry(dir_ent, sub_dir, lookup_inode(&buf));
Phillip Lougher205c1e02013-01-07 01:37:56 +00003338 free(new);
plougher1f413c82005-11-18 00:02:14 +00003339 }
3340
3341 scan1_freedir(dir);
plougher1f413c82005-11-18 00:02:14 +00003342
plougher1f413c82005-11-18 00:02:14 +00003343 return dir;
3344}
3345
plougher2ea89142008-03-11 01:34:19 +00003346
Phillip Lougherabc3b492012-07-29 02:53:35 +01003347/*
3348 * dir_scan2 routines...
Phillip Lougher539c2b12012-07-30 20:14:52 +01003349 * This processes most actions and any pseudo files
Phillip Lougherabc3b492012-07-29 02:53:35 +01003350 */
Phillip Lougherbf338362012-08-22 05:24:36 +01003351struct dir_ent *scan2_readdir(struct dir_info *dir, struct dir_ent *dir_ent)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003352{
Phillip Lougherbf338362012-08-22 05:24:36 +01003353 if (dir_ent == NULL)
3354 dir_ent = dir->list;
3355 else
3356 dir_ent = dir_ent->next;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003357
Phillip Lougherbf338362012-08-22 05:24:36 +01003358 for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next);
3359
3360 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003361}
3362
3363
3364struct dir_ent *scan2_lookup(struct dir_info *dir, char *name)
3365{
Phillip Lougherbf338362012-08-22 05:24:36 +01003366 struct dir_ent *dir_ent = dir->list;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003367
Phillip Lougherbf338362012-08-22 05:24:36 +01003368 for(; dir_ent && strcmp(dir_ent->name, name) != 0;
3369 dir_ent = dir_ent->next);
Phillip Lougherabc3b492012-07-29 02:53:35 +01003370
Phillip Lougherbf338362012-08-22 05:24:36 +01003371 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003372}
3373
3374
Phillip Lougher24eeb772012-10-13 01:56:24 +01003375void dir_scan2(struct dir_info *dir, struct pseudo *pseudo)
plougher43244f22009-04-05 02:04:51 +00003376{
Phillip Lougherbf338362012-08-22 05:24:36 +01003377 struct dir_ent *dir_ent = NULL;
plougher43244f22009-04-05 02:04:51 +00003378 struct pseudo_entry *pseudo_ent;
3379 struct stat buf;
plougher82ab2332009-04-21 00:21:21 +00003380 static int pseudo_ino = 1;
plougher43244f22009-04-05 02:04:51 +00003381
Phillip Lougherbf338362012-08-22 05:24:36 +01003382 while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
plougher43244f22009-04-05 02:04:51 +00003383 struct inode_info *inode_info = dir_ent->inode;
3384 struct stat *buf = &inode_info->buf;
3385 char *name = dir_ent->name;
3386
Phillip Lougher89d757c2011-09-20 00:33:19 +01003387 eval_actions(dir_ent);
3388
plougher43244f22009-04-05 02:04:51 +00003389 if((buf->st_mode & S_IFMT) == S_IFDIR)
Phillip Lougher24eeb772012-10-13 01:56:24 +01003390 dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo));
plougher43244f22009-04-05 02:04:51 +00003391 }
3392
3393 while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) {
3394 dir_ent = scan2_lookup(dir, pseudo_ent->name);
plougherdcd66c52010-09-17 03:44:17 +00003395 if(pseudo_ent->dev->type == 'm') {
plougherb34f9f62009-04-26 02:08:42 +00003396 struct stat *buf;
3397 if(dir_ent == NULL) {
plougherdcd66c52010-09-17 03:44:17 +00003398 ERROR("Pseudo modify file \"%s\" does not exist "
plougherf0dc2382010-05-01 23:55:06 +00003399 "in source filesystem. Ignoring.\n",
plougherb34f9f62009-04-26 02:08:42 +00003400 pseudo_ent->pathname);
3401 continue;
3402 }
ploughera326c182009-08-29 05:41:45 +00003403 if(dir_ent->inode->root_entry) {
plougherdcd66c52010-09-17 03:44:17 +00003404 ERROR("Pseudo modify file \"%s\" is a pre-existing"
plougherb34f9f62009-04-26 02:08:42 +00003405 " file in the filesystem being appended"
3406 " to. It cannot be modified. "
plougherf0dc2382010-05-01 23:55:06 +00003407 "Ignoring.\n", pseudo_ent->pathname);
plougherb34f9f62009-04-26 02:08:42 +00003408 continue;
3409 }
3410 buf = &dir_ent->inode->buf;
3411 buf->st_mode = (buf->st_mode & S_IFMT) |
3412 pseudo_ent->dev->mode;
3413 buf->st_uid = pseudo_ent->dev->uid;
3414 buf->st_gid = pseudo_ent->dev->gid;
3415 continue;
3416 }
3417
plougher43244f22009-04-05 02:04:51 +00003418 if(dir_ent) {
plougherf0dc2382010-05-01 23:55:06 +00003419 if(dir_ent->inode->root_entry)
3420 ERROR("Pseudo file \"%s\" is a pre-existing"
3421 " file in the filesystem being appended"
3422 " to. Ignoring.\n",
3423 pseudo_ent->pathname);
3424 else
3425 ERROR("Pseudo file \"%s\" exists in source "
plougherdcd66c52010-09-17 03:44:17 +00003426 "filesystem \"%s\".\nIgnoring, "
plougherf0dc2382010-05-01 23:55:06 +00003427 "exclude it (-e/-ef) to override.\n",
3428 pseudo_ent->pathname,
Phillip Lougher494479f2012-02-03 15:45:41 +00003429 pathname(dir_ent));
plougher43244f22009-04-05 02:04:51 +00003430 continue;
3431 }
3432
plougher43244f22009-04-05 02:04:51 +00003433 memset(&buf, 0, sizeof(buf));
3434 buf.st_mode = pseudo_ent->dev->mode;
3435 buf.st_uid = pseudo_ent->dev->uid;
3436 buf.st_gid = pseudo_ent->dev->gid;
3437 buf.st_rdev = makedev(pseudo_ent->dev->major,
3438 pseudo_ent->dev->minor);
plougher7e58f4d2009-04-05 12:06:19 +00003439 buf.st_mtime = time(NULL);
plougher1a3fbf22009-04-05 12:04:16 +00003440 buf.st_ino = pseudo_ino ++;
plougher43244f22009-04-05 02:04:51 +00003441
Phillip Lougher5d579292012-10-13 01:37:16 +01003442 if(pseudo_ent->dev->type == 'd') {
3443 struct dir_ent *dir_ent =
3444 create_dir_entry(pseudo_ent->name, NULL,
3445 pseudo_ent->pathname, dir);
3446 char *subpath = strdup(subpathname(dir_ent));
3447 struct dir_info *sub_dir = scan1_opendir("", subpath,
Phillip Lougher24eeb772012-10-13 01:56:24 +01003448 dir->depth + 1);
Phillip Lougher5d579292012-10-13 01:37:16 +01003449 if(sub_dir == NULL) {
3450 ERROR("Could not create pseudo directory \"%s\""
3451 ", skipping...\n",
3452 pseudo_ent->pathname);
3453 free(subpath);
3454 pseudo_ino --;
3455 continue;
3456 }
Phillip Lougher24eeb772012-10-13 01:56:24 +01003457 dir_scan2(sub_dir, pseudo_ent->pseudo);
Phillip Lougher5d579292012-10-13 01:37:16 +01003458 dir->directory_count ++;
3459 add_dir_entry(dir_ent, sub_dir,
3460 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0));
3461 } else if(pseudo_ent->dev->type == 'f') {
plougher00d08172009-09-03 10:17:44 +00003462#ifdef USE_TMP_FILE
plougher4ab7e512009-05-05 02:35:58 +00003463 struct stat buf2;
3464 int res = stat(pseudo_ent->dev->filename, &buf2);
3465 if(res == -1) {
3466 ERROR("Stat on pseudo file \"%s\" failed, "
Phillip Lougherfe58b492012-12-15 01:27:07 +00003467 "skipping...\n", pseudo_ent->pathname);
Phillip Lougher5d579292012-10-13 01:37:16 +01003468 pseudo_ino --;
plougher4ab7e512009-05-05 02:35:58 +00003469 continue;
3470 }
3471 buf.st_size = buf2.st_size;
Phillip Lougher494479f2012-02-03 15:45:41 +00003472 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003473 pseudo_ent->dev->filename, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003474 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir);
plougher00d08172009-09-03 10:17:44 +00003475#else
Phillip Lougher494479f2012-02-03 15:45:41 +00003476 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003477 pseudo_ent->pathname, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003478 lookup_inode2(&buf, PSEUDO_FILE_PROCESS,
3479 pseudo_ent->dev->pseudo_id), dir);
plougher00d08172009-09-03 10:17:44 +00003480#endif
plougherb85e9ad2010-05-02 01:46:12 +00003481 } else {
Phillip Lougher494479f2012-02-03 15:45:41 +00003482 add_dir_entry2(pseudo_ent->name, NULL,
Phillip Lougher5d579292012-10-13 01:37:16 +01003483 pseudo_ent->pathname, NULL,
Phillip Lougher81204c22012-07-25 03:30:30 +01003484 lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir);
plougherb85e9ad2010-05-02 01:46:12 +00003485 }
plougher43244f22009-04-05 02:04:51 +00003486 }
plougher43244f22009-04-05 02:04:51 +00003487}
3488
3489
Phillip Lougherabc3b492012-07-29 02:53:35 +01003490/*
3491 * dir_scan3 routines...
Phillip Lougher23d83622012-10-14 02:35:22 +01003492 * This processes the move action
3493 */
3494void dir_scan3(struct dir_info *root, struct dir_info *dir)
3495{
3496 struct dir_ent *dir_ent = NULL;
3497
3498 while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) {
3499
3500 eval_move_actions(root, dir_ent);
3501
3502 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
3503 dir_scan3(root, dir_ent->dir);
3504 }
3505}
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003506
3507
Phillip Lougher23d83622012-10-14 02:35:22 +01003508/*
3509 * dir_scan4 routines...
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003510 * This processes the empty action. This action has to be processed after
3511 * all other actions because the previous exclude and move actions and the
3512 * pseudo actions affect whether a directory is empty
3513 */
3514void dir_scan4(struct dir_info *dir)
3515{
3516 struct dir_ent *dir_ent = dir->list, *prev = NULL;
3517
3518 while(dir_ent) {
Phillip Lougher15e8f042012-11-16 00:57:39 +00003519 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) {
3520 dir_scan4(dir_ent->dir);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003521
Phillip Lougher15e8f042012-11-16 00:57:39 +00003522 if(eval_empty_actions(dir_ent)) {
3523 struct dir_ent *tmp = dir_ent;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003524
Phillip Lougher15e8f042012-11-16 00:57:39 +00003525 /*
3526 * delete sub-directory, this is by definition
3527 * empty
3528 */
3529 free(dir_ent->dir->pathname);
3530 free(dir_ent->dir->subpath);
3531 free(dir_ent->dir);
3532
3533 /* remove dir_ent from list */
3534 dir_ent = dir_ent->next;
3535 if(prev)
3536 prev->next = dir_ent;
3537 else
3538 dir->list = dir_ent;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003539
Phillip Lougher15e8f042012-11-16 00:57:39 +00003540 /* free it */
3541 free_dir_entry(tmp);
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003542
Phillip Lougher15e8f042012-11-16 00:57:39 +00003543 /* update counts */
3544 dir->directory_count --;
3545 dir->count --;
3546 add_excluded(dir);
3547 continue;
3548 }
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003549 }
Phillip Lougher15e8f042012-11-16 00:57:39 +00003550
3551 prev = dir_ent;
3552 dir_ent = dir_ent->next;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003553 }
3554}
3555
3556
3557/*
3558 * dir_scan5 routines...
Phillip Lougher539c2b12012-07-30 20:14:52 +01003559 * This sorts every directory and computes the inode numbers
3560 */
Phillip Lougher539c2b12012-07-30 20:14:52 +01003561
Phillip Lougher242242e2012-08-24 04:15:36 +01003562/*
3563 * Bottom up linked list merge sort.
3564 *
3565 * Qsort and other O(n log n) algorithms work well with arrays but not
3566 * linked lists. Merge sort another O(n log n) sort algorithm on the other hand
3567 * is not ideal for arrays (as it needs an additonal n storage locations
3568 * as sorting is not done in place), but it is ideal for linked lists because
3569 * it doesn't require any extra storage,
3570 */
Phillip Lougher539c2b12012-07-30 20:14:52 +01003571void sort_directory(struct dir_info *dir)
3572{
Phillip Lougher242242e2012-08-24 04:15:36 +01003573 struct dir_ent *cur, *l1, *l2, *next;
3574 int len1, len2, stride = 1;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003575
Phillip Lougher242242e2012-08-24 04:15:36 +01003576 if(dir->count < 2)
Phillip Lougherbf338362012-08-22 05:24:36 +01003577 return;
3578
Phillip Lougher242242e2012-08-24 04:15:36 +01003579 /*
3580 * We can consider our linked-list to be made up of stride length
3581 * sublists. Eacn iteration around this loop merges adjacent
3582 * stride length sublists into larger 2*stride sublists. We stop
3583 * when stride becomes equal to the entire list.
3584 *
3585 * Initially stride = 1 (by definition a sublist of 1 is sorted), and
3586 * these 1 element sublists are merged into 2 element sublists, which
3587 * are then merged into 4 element sublists and so on.
3588 */
3589 do {
3590 l2 = dir->list; /* head of current linked list */
3591 cur = NULL; /* empty output list */
Phillip Lougherbf338362012-08-22 05:24:36 +01003592
Phillip Lougher242242e2012-08-24 04:15:36 +01003593 /*
3594 * Iterate through the linked list, merging adjacent sublists.
3595 * On each interation l2 points to the next sublist pair to be
3596 * merged (if there's only one sublist left this is simply added
3597 * to the output list)
3598 */
3599 while(l2) {
3600 l1 = l2;
3601 for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next);
3602 len2 = stride;
Phillip Lougherbf338362012-08-22 05:24:36 +01003603
Phillip Lougher242242e2012-08-24 04:15:36 +01003604 /*
3605 * l1 points to first sublist.
3606 * l2 points to second sublist.
3607 * Merge them onto the output list
3608 */
3609 while(len1 && l2 && len2) {
3610 if(strcmp(l1->name, l2->name) <= 0) {
3611 next = l1;
3612 l1 = l1->next;
3613 len1 --;
3614 } else {
3615 next = l2;
3616 l2 = l2->next;
3617 len2 --;
3618 }
3619
3620 if(cur) {
3621 cur->next = next;
3622 cur = next;
3623 } else
3624 dir->list = cur = next;
3625 }
3626 /*
3627 * One sublist is now empty, copy the other one onto the
3628 * output list
3629 */
3630 for(; len1; len1 --, l1 = l1->next) {
3631 if(cur) {
3632 cur->next = l1;
3633 cur = l1;
3634 } else
3635 dir->list = cur = l1;
3636 }
3637 for(; l2 && len2; len2 --, l2 = l2->next) {
3638 if(cur) {
3639 cur->next = l2;
3640 cur = l2;
3641 } else
3642 dir->list = cur = l2;
3643 }
3644 }
3645 cur->next = NULL;
3646 stride = stride << 1;
3647 } while(stride < dir->count);
Phillip Lougher539c2b12012-07-30 20:14:52 +01003648}
3649
3650
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003651void dir_scan5(struct dir_info *dir)
Phillip Lougher539c2b12012-07-30 20:14:52 +01003652{
Phillip Lougher23d83622012-10-14 02:35:22 +01003653 struct dir_ent *dir_ent;
3654 unsigned int byte_count = 0;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003655
3656 sort_directory(dir);
3657
Phillip Lougher23d83622012-10-14 02:35:22 +01003658 for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) {
3659 byte_count += strlen(dir_ent->name) +
3660 sizeof(struct squashfs_dir_entry);
3661
3662 if(dir_ent->inode->root_entry)
3663 continue;
3664
Phillip Lougher539c2b12012-07-30 20:14:52 +01003665 alloc_inode_no(dir_ent->inode, 0);
Phillip Lougher23d83622012-10-14 02:35:22 +01003666
Phillip Lougher539c2b12012-07-30 20:14:52 +01003667 if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR)
Phillip Loughere69b5882012-11-19 01:31:09 +00003668 dir_scan5(dir_ent->dir);
Phillip Lougher539c2b12012-07-30 20:14:52 +01003669 }
Phillip Lougher23d83622012-10-14 02:35:22 +01003670
3671 if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE))
3672 dir->dir_is_ldir = FALSE;
Phillip Lougher539c2b12012-07-30 20:14:52 +01003673}
3674
3675
3676/*
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003677 * dir_scan6 routines...
Phillip Lougherabc3b492012-07-29 02:53:35 +01003678 * This generates the filesystem metadata and writes it out to the destination
3679 */
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003680void scan6_init_dir(struct directory *dir)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003681{
3682 dir->buff = malloc(SQUASHFS_METADATA_SIZE);
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003683 if(dir->buff == NULL)
3684 MEM_ERROR();
Phillip Lougherabc3b492012-07-29 02:53:35 +01003685
3686 dir->size = SQUASHFS_METADATA_SIZE;
3687 dir->p = dir->index_count_p = dir->buff;
3688 dir->entry_count = 256;
3689 dir->entry_count_p = NULL;
3690 dir->index = NULL;
3691 dir->i_count = dir->i_size = 0;
3692}
3693
3694
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003695struct dir_ent *scan6_readdir(struct directory *dir, struct dir_info *dir_info,
Phillip Lougherbf338362012-08-22 05:24:36 +01003696 struct dir_ent *dir_ent)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003697{
Phillip Lougherbf338362012-08-22 05:24:36 +01003698 if (dir_ent == NULL)
3699 dir_ent = dir_info->list;
3700 else
3701 dir_ent = dir_ent->next;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003702
Phillip Lougherbf338362012-08-22 05:24:36 +01003703 for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next)
3704 add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number,
3705 dir_ent->name, dir_ent->inode->type, dir);
3706
3707 return dir_ent;
Phillip Lougherabc3b492012-07-29 02:53:35 +01003708}
3709
3710
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003711void scan6_freedir(struct directory *dir)
Phillip Lougherabc3b492012-07-29 02:53:35 +01003712{
3713 if(dir->index)
3714 free(dir->index);
3715 free(dir->buff);
3716}
3717
3718
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003719void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info)
plougher1f413c82005-11-18 00:02:14 +00003720{
3721 int squashfs_type;
plougher1f413c82005-11-18 00:02:14 +00003722 int duplicate_file;
plougher1f413c82005-11-18 00:02:14 +00003723 struct directory dir;
Phillip Lougherbf338362012-08-22 05:24:36 +01003724 struct dir_ent *dir_ent = NULL;
plougher1f413c82005-11-18 00:02:14 +00003725
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003726 scan6_init_dir(&dir);
plougher1f413c82005-11-18 00:02:14 +00003727
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003728 while((dir_ent = scan6_readdir(&dir, dir_info, dir_ent)) != NULL) {
Phillip Lougher0a670882012-10-05 04:09:13 +01003729 struct stat *buf = &dir_ent->inode->buf;
plougher1f413c82005-11-18 00:02:14 +00003730
Phillip Lougher24551a82013-03-24 02:30:50 +00003731 update_info(dir_ent);
3732
plougher1f413c82005-11-18 00:02:14 +00003733 if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) {
3734 switch(buf->st_mode & S_IFMT) {
3735 case S_IFREG:
3736 squashfs_type = SQUASHFS_FILE_TYPE;
plougher110799c2009-03-30 01:50:40 +00003737 write_file(inode, dir_ent,
3738 &duplicate_file);
3739 INFO("file %s, uncompressed size %lld "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003740 "bytes %s\n",
3741 subpathname(dir_ent),
plougher82ab2332009-04-21 00:21:21 +00003742 (long long) buf->st_size,
3743 duplicate_file ? "DUPLICATE" :
3744 "");
plougher1f413c82005-11-18 00:02:14 +00003745 break;
3746
3747 case S_IFDIR:
3748 squashfs_type = SQUASHFS_DIR_TYPE;
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003749 dir_scan6(inode, dir_ent->dir);
plougher1f413c82005-11-18 00:02:14 +00003750 break;
3751
3752 case S_IFLNK:
3753 squashfs_type = SQUASHFS_SYMLINK_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003754 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003755 squashfs_type, 0, 0, 0, NULL,
3756 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00003757 INFO("symbolic link %s inode 0x%llx\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003758 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003759 sym_count ++;
3760 break;
3761
3762 case S_IFCHR:
3763 squashfs_type = SQUASHFS_CHRDEV_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003764 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003765 squashfs_type, 0, 0, 0, NULL,
3766 NULL, NULL, 0);
3767 INFO("character device %s inode 0x%llx"
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003768 "\n", subpathname(dir_ent),
3769 *inode);
plougher1f413c82005-11-18 00:02:14 +00003770 dev_count ++;
3771 break;
3772
3773 case S_IFBLK:
3774 squashfs_type = SQUASHFS_BLKDEV_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003775 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003776 squashfs_type, 0, 0, 0, NULL,
3777 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00003778 INFO("block device %s inode 0x%llx\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003779 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003780 dev_count ++;
3781 break;
3782
3783 case S_IFIFO:
3784 squashfs_type = SQUASHFS_FIFO_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003785 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003786 squashfs_type, 0, 0, 0, NULL,
3787 NULL, NULL, 0);
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003788 INFO("fifo %s inode 0x%llx\n",
3789 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003790 fifo_count ++;
3791 break;
3792
3793 case S_IFSOCK:
3794 squashfs_type = SQUASHFS_SOCKET_TYPE;
plougher3c6bdb52010-05-01 02:30:59 +00003795 create_inode(inode, NULL, dir_ent,
plougher50b31762009-03-31 04:14:46 +00003796 squashfs_type, 0, 0, 0, NULL,
3797 NULL, NULL, 0);
plougherb3604122009-03-30 02:07:20 +00003798 INFO("unix domain socket %s inode "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003799 "0x%llx\n",
3800 subpathname(dir_ent), *inode);
plougher1f413c82005-11-18 00:02:14 +00003801 sock_count ++;
3802 break;
3803
plougher23377982007-11-12 04:04:48 +00003804 default:
plougherb3604122009-03-30 02:07:20 +00003805 BAD_ERROR("%s unrecognised file type, "
Phillip Lougher494479f2012-02-03 15:45:41 +00003806 "mode is %x\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003807 subpathname(dir_ent),
plougherb3604122009-03-30 02:07:20 +00003808 buf->st_mode);
plougher29e37092007-04-15 01:24:51 +00003809 }
3810 dir_ent->inode->inode = *inode;
plougher1f413c82005-11-18 00:02:14 +00003811 dir_ent->inode->type = squashfs_type;
3812 } else {
3813 *inode = dir_ent->inode->inode;
3814 squashfs_type = dir_ent->inode->type;
plougher04b0d5f2006-02-10 00:42:06 +00003815 switch(squashfs_type) {
3816 case SQUASHFS_FILE_TYPE:
3817 if(!sorted)
plougher50b31762009-03-31 04:14:46 +00003818 INFO("file %s, uncompressed "
3819 "size %lld bytes LINK"
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003820 "\n",
3821 subpathname(dir_ent),
plougher82ab2332009-04-21 00:21:21 +00003822 (long long)
plougher50b31762009-03-31 04:14:46 +00003823 buf->st_size);
plougher04b0d5f2006-02-10 00:42:06 +00003824 break;
3825 case SQUASHFS_SYMLINK_TYPE:
plougherb3604122009-03-30 02:07:20 +00003826 INFO("symbolic link %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003827 "LINK\n", subpathname(dir_ent),
3828 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003829 break;
3830 case SQUASHFS_CHRDEV_TYPE:
plougherb3604122009-03-30 02:07:20 +00003831 INFO("character device %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003832 "LINK\n", subpathname(dir_ent),
3833 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003834 break;
plougher5507dd92006-11-06 00:43:10 +00003835 case SQUASHFS_BLKDEV_TYPE:
plougherb3604122009-03-30 02:07:20 +00003836 INFO("block device %s inode 0x%llx "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003837 "LINK\n", subpathname(dir_ent),
3838 *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003839 break;
3840 case SQUASHFS_FIFO_TYPE:
plougherb3604122009-03-30 02:07:20 +00003841 INFO("fifo %s inode 0x%llx LINK\n",
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003842 subpathname(dir_ent), *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003843 break;
3844 case SQUASHFS_SOCKET_TYPE:
plougher50b31762009-03-31 04:14:46 +00003845 INFO("unix domain socket %s inode "
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003846 "0x%llx LINK\n",
3847 subpathname(dir_ent), *inode);
plougher04b0d5f2006-02-10 00:42:06 +00003848 break;
3849 }
plougher1f413c82005-11-18 00:02:14 +00003850 }
3851
Phillip Lougher0366aec2012-10-05 04:06:04 +01003852 add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name,
3853 squashfs_type, &dir);
plougher1f413c82005-11-18 00:02:14 +00003854 }
3855
plougher29e37092007-04-15 01:24:51 +00003856 write_dir(inode, dir_info, &dir);
Phillip Lougher4980d7f2012-10-05 03:47:12 +01003857 INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent),
3858 *inode);
plougher1f413c82005-11-18 00:02:14 +00003859
Phillip Lougher2abfcf72012-11-11 03:59:56 +00003860 scan6_freedir(&dir);
plougher1f413c82005-11-18 00:02:14 +00003861}
3862
3863
3864unsigned int slog(unsigned int block)
3865{
3866 int i;
3867
plougher4c99cb72007-06-14 21:46:31 +00003868 for(i = 12; i <= 20; i++)
plougher1f413c82005-11-18 00:02:14 +00003869 if(block == (1 << i))
3870 return i;
3871 return 0;
3872}
3873
3874
plougher8f8e1a12007-10-18 02:50:21 +00003875int old_excluded(char *filename, struct stat *buf)
plougher1f413c82005-11-18 00:02:14 +00003876{
3877 int i;
3878
3879 for(i = 0; i < exclude; i++)
plougherb3604122009-03-30 02:07:20 +00003880 if((exclude_paths[i].st_dev == buf->st_dev) &&
3881 (exclude_paths[i].st_ino == buf->st_ino))
plougher1f413c82005-11-18 00:02:14 +00003882 return TRUE;
3883 return FALSE;
3884}
3885
3886
3887#define ADD_ENTRY(buf) \
plougher360514a2009-03-30 03:01:38 +00003888 if(exclude % EXCLUDE_SIZE == 0) { \
3889 exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \
3890 * sizeof(struct exclude_info)); \
3891 if(exclude_paths == NULL) \
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003892 MEM_ERROR(); \
plougher360514a2009-03-30 03:01:38 +00003893 } \
3894 exclude_paths[exclude].st_dev = buf.st_dev; \
plougher1f413c82005-11-18 00:02:14 +00003895 exclude_paths[exclude++].st_ino = buf.st_ino;
plougher8f8e1a12007-10-18 02:50:21 +00003896int old_add_exclude(char *path)
plougher1f413c82005-11-18 00:02:14 +00003897{
3898 int i;
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003899 char *filename;
plougher1f413c82005-11-18 00:02:14 +00003900 struct stat buf;
3901
plougherb3604122009-03-30 02:07:20 +00003902 if(path[0] == '/' || strncmp(path, "./", 2) == 0 ||
3903 strncmp(path, "../", 3) == 0) {
plougher1f413c82005-11-18 00:02:14 +00003904 if(lstat(path, &buf) == -1) {
plougherb3604122009-03-30 02:07:20 +00003905 ERROR("Cannot stat exclude dir/file %s because %s, "
Phillip Lougherfe58b492012-12-15 01:27:07 +00003906 "ignoring\n", path, strerror(errno));
plougher1f413c82005-11-18 00:02:14 +00003907 return TRUE;
3908 }
3909 ADD_ENTRY(buf);
3910 return TRUE;
3911 }
3912
3913 for(i = 0; i < source; i++) {
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003914 int res = asprintf(&filename, "%s/%s", source_path[i], path);
3915 if(res == -1)
3916 BAD_ERROR("asprintf failed in old_add_exclude\n");
plougher1f413c82005-11-18 00:02:14 +00003917 if(lstat(filename, &buf) == -1) {
plougher91fbb302008-05-06 02:29:36 +00003918 if(!(errno == ENOENT || errno == ENOTDIR))
plougherb3604122009-03-30 02:07:20 +00003919 ERROR("Cannot stat exclude dir/file %s because "
Phillip Lougherfe58b492012-12-15 01:27:07 +00003920 "%s, ignoring\n", filename,
plougher50b31762009-03-31 04:14:46 +00003921 strerror(errno));
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003922 free(filename);
plougher1f413c82005-11-18 00:02:14 +00003923 continue;
3924 }
Phillip Lougherdcb5af92012-11-30 03:05:31 +00003925 free(filename);
plougher1f413c82005-11-18 00:02:14 +00003926 ADD_ENTRY(buf);
3927 }
3928 return TRUE;
3929}
3930
3931
plougherb3604122009-03-30 02:07:20 +00003932void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
3933 int type)
plougher1f413c82005-11-18 00:02:14 +00003934{
plougherb3604122009-03-30 02:07:20 +00003935 old_root_entry = realloc(old_root_entry,
3936 sizeof(struct old_root_entry_info) * (old_root_entries + 1));
3937 if(old_root_entry == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00003938 MEM_ERROR();
plougher1f413c82005-11-18 00:02:14 +00003939
ploughera326c182009-08-29 05:41:45 +00003940 old_root_entry[old_root_entries].name = strdup(name);
3941 old_root_entry[old_root_entries].inode.inode = inode;
3942 old_root_entry[old_root_entries].inode.inode_number = inode_number;
3943 old_root_entry[old_root_entries].inode.type = type;
3944 old_root_entry[old_root_entries++].inode.root_entry = TRUE;
plougher1f413c82005-11-18 00:02:14 +00003945}
3946
3947
plougher5741d792010-09-04 03:20:50 +00003948void initialise_threads(int readb_mbytes, int writeb_mbytes,
Phillip Lougherc97dac52013-04-14 03:32:10 +01003949 int fragmentb_mbytes, int freelst)
plougher5507dd92006-11-06 00:43:10 +00003950{
3951 int i;
3952 sigset_t sigmask, old_mask;
Phillip Loughere1668fe2012-12-30 05:48:12 +00003953 int reader_buffer_size;
3954 int fragment_buffer_size;
plougher5741d792010-09-04 03:20:50 +00003955 /*
3956 * writer_buffer_size is global because it is needed in
3957 * write_file_blocks_dup()
3958 */
Phillip Loughere1668fe2012-12-30 05:48:12 +00003959
3960 /*
3961 * convert from queue size in Mbytes to queue size in
3962 * blocks.
3963 *
3964 * In doing so, check that the user supplied values do not
3965 * overflow a signed int
3966 */
3967 if(shift_overflow(readb_mbytes, 20 - block_log))
3968 BAD_ERROR("Read queue is too large\n");
3969 else
3970 reader_buffer_size = readb_mbytes << (20 - block_log);
3971
3972 if(shift_overflow(fragmentb_mbytes, 20 - block_log))
3973 BAD_ERROR("Fragment queue is too large\n");
3974 else
3975 fragment_buffer_size = fragmentb_mbytes << (20 - block_log);
3976
3977 if(shift_overflow(writeb_mbytes, 20 - block_log))
3978 BAD_ERROR("Write queue is too large\n");
3979 else
3980 writer_buffer_size = writeb_mbytes << (20 - block_log);
plougher5507dd92006-11-06 00:43:10 +00003981
Phillip Lougher9391cb12013-03-20 05:16:10 +00003982 /*
3983 * setup signal handlers for the main thread, these cleanup
3984 * deleting the destination file, if appending the
3985 * handlers for SIGTERM and SIGINT will be replaced with handlers
3986 * allowing the user to press ^C twice to restore the existing
3987 * filesystem.
3988 *
Phillip Lougherdd343392013-03-31 23:29:03 +01003989 * SIGUSR1 is an internal signal, which is used by the sub-threads
Phillip Lougher9391cb12013-03-20 05:16:10 +00003990 * to tell the main thread to terminate, deleting the destination file,
3991 * or if necessary restoring the filesystem on appending
3992 */
Phillip Lougherb8ec5202013-03-28 20:14:37 +00003993 signal(SIGTERM, sighandler);
3994 signal(SIGINT, sighandler);
Phillip Lougherdd343392013-03-31 23:29:03 +01003995 signal(SIGUSR1, sighandler);
Phillip Lougher9391cb12013-03-20 05:16:10 +00003996
Phillip Lougher7538d742013-04-22 05:33:27 +01003997 /* block SIGQUIT and SIGHUP, these are handled by the info thread */
Phillip Lougher24551a82013-03-24 02:30:50 +00003998 sigemptyset(&sigmask);
3999 sigaddset(&sigmask, SIGQUIT);
Phillip Lougher7538d742013-04-22 05:33:27 +01004000 sigaddset(&sigmask, SIGHUP);
Phillip Lougherb1259f72013-06-06 03:10:15 +01004001 if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
Phillip Lougher24551a82013-03-24 02:30:50 +00004002 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4003
Phillip Lougher9391cb12013-03-20 05:16:10 +00004004 /*
4005 * temporarily block these signals, so the created sub-threads
4006 * will ignore them, ensuring the main thread handles them
4007 */
plougher5507dd92006-11-06 00:43:10 +00004008 sigemptyset(&sigmask);
4009 sigaddset(&sigmask, SIGINT);
Phillip Lougher9391cb12013-03-20 05:16:10 +00004010 sigaddset(&sigmask, SIGTERM);
Phillip Lougherdd343392013-03-31 23:29:03 +01004011 sigaddset(&sigmask, SIGUSR1);
Phillip Lougher8f8d2752013-03-26 04:38:38 +00004012 if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1)
plougher5507dd92006-11-06 00:43:10 +00004013 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4014
plougher5507dd92006-11-06 00:43:10 +00004015 if(processors == -1) {
4016#ifndef linux
4017 int mib[2];
4018 size_t len = sizeof(processors);
4019
4020 mib[0] = CTL_HW;
4021#ifdef HW_AVAILCPU
4022 mib[1] = HW_AVAILCPU;
4023#else
4024 mib[1] = HW_NCPU;
4025#endif
4026
4027 if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) {
plougher360514a2009-03-30 03:01:38 +00004028 ERROR("Failed to get number of available processors. "
4029 "Defaulting to 1\n");
plougher5507dd92006-11-06 00:43:10 +00004030 processors = 1;
4031 }
4032#else
plougher9cc26b72010-08-17 01:05:55 +00004033 processors = sysconf(_SC_NPROCESSORS_ONLN);
plougher5507dd92006-11-06 00:43:10 +00004034#endif
4035 }
4036
Phillip Loughere1668fe2012-12-30 05:48:12 +00004037 if(multiply_overflow(processors, 2) ||
4038 add_overflow(processors * 2, 2) ||
4039 multiply_overflow(processors * 2 + 2,
4040 sizeof(pthread_t)))
4041 BAD_ERROR("Processors too large\n");
4042
Phillip Lougher0280d992014-01-27 05:54:07 +00004043 deflator_thread = malloc(processors * 2 * sizeof(pthread_t));
4044 if(deflator_thread == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004045 MEM_ERROR();
Phillip Loughere1668fe2012-12-30 05:48:12 +00004046
plougher5507dd92006-11-06 00:43:10 +00004047 frag_deflator_thread = &deflator_thread[processors];
4048
4049 to_reader = queue_init(1);
Phillip Loughercf478e92013-05-29 02:38:41 +01004050 to_deflate = queue_init(reader_buffer_size);
plougher5507dd92006-11-06 00:43:10 +00004051 to_writer = queue_init(writer_buffer_size);
4052 from_writer = queue_init(1);
plougher76c64082008-03-08 01:32:23 +00004053 to_frag = queue_init(fragment_buffer_size);
Phillip Lougher6164b5f2013-05-23 05:05:10 +01004054 locked_fragment = queue_init(fragment_buffer_size);
Phillip Lougher0e1656d2013-05-20 03:47:24 +01004055 to_main = seq_queue_init();
Phillip Lougher316ab632013-04-29 02:53:39 +01004056 reader_buffer = cache_init(block_size, reader_buffer_size, 0, 0);
4057 writer_buffer = cache_init(block_size, writer_buffer_size, 1, freelst);
4058 fragment_buffer = cache_init(block_size, fragment_buffer_size, 1,
4059 freelst);
Phillip Lougher0280d992014-01-27 05:54:07 +00004060 pthread_create(&reader_thread, NULL, reader, NULL);
4061 pthread_create(&writer_thread, NULL, writer, NULL);
Phillip Lougher3b89ee82012-10-18 23:55:37 +01004062 init_progress_bar();
Phillip Lougher24551a82013-03-24 02:30:50 +00004063 init_info();
plougher5507dd92006-11-06 00:43:10 +00004064 pthread_mutex_init(&fragment_mutex, NULL);
plougher5507dd92006-11-06 00:43:10 +00004065
4066 for(i = 0; i < processors; i++) {
plougher50b31762009-03-31 04:14:46 +00004067 if(pthread_create(&deflator_thread[i], NULL, deflator, NULL) !=
4068 0)
plougher5507dd92006-11-06 00:43:10 +00004069 BAD_ERROR("Failed to create thread\n");
plougher360514a2009-03-30 03:01:38 +00004070 if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator,
4071 NULL) != 0)
plougher5507dd92006-11-06 00:43:10 +00004072 BAD_ERROR("Failed to create thread\n");
4073 }
4074
Phillip Lougher0280d992014-01-27 05:54:07 +00004075 main_thread = pthread_self();
4076
plougher5507dd92006-11-06 00:43:10 +00004077 printf("Parallel mksquashfs: Using %d processor%s\n", processors,
4078 processors == 1 ? "" : "s");
4079
Phillip Lougher9391cb12013-03-20 05:16:10 +00004080 /* Restore the signal mask for the main thread */
Phillip Lougher8f8d2752013-03-26 04:38:38 +00004081 if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1)
plougher5507dd92006-11-06 00:43:10 +00004082 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
4083}
4084
4085
plougher0e453652006-11-06 01:49:35 +00004086long long write_inode_lookup_table()
4087{
4088 int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count);
plougher44f03282010-07-27 00:31:36 +00004089 void *it;
plougher02bc3bc2007-02-25 12:12:01 +00004090
4091 if(inode_count == sinode_count)
4092 goto skip_inode_hash_table;
plougher0e453652006-11-06 01:49:35 +00004093
plougher44f03282010-07-27 00:31:36 +00004094 it = realloc(inode_lookup_table, lookup_bytes);
4095 if(it == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004096 MEM_ERROR();
plougher44f03282010-07-27 00:31:36 +00004097 inode_lookup_table = it;
plougher0e453652006-11-06 01:49:35 +00004098
plougher0e453652006-11-06 01:49:35 +00004099 for(i = 0; i < INODE_HASH_SIZE; i ++) {
4100 struct inode_info *inode = inode_info[i];
4101
4102 for(inode = inode_info[i]; inode; inode = inode->next) {
plougher0e453652006-11-06 01:49:35 +00004103
Phillip Lougher539c2b12012-07-30 20:14:52 +01004104 inode_number = get_inode_no(inode);
plougher0e453652006-11-06 01:49:35 +00004105
plougher360514a2009-03-30 03:01:38 +00004106 SQUASHFS_SWAP_LONG_LONGS(&inode->inode,
4107 &inode_lookup_table[inode_number - 1], 1);
plougher0e453652006-11-06 01:49:35 +00004108
plougher0e453652006-11-06 01:49:35 +00004109 }
4110 }
4111
plougher02bc3bc2007-02-25 12:12:01 +00004112skip_inode_hash_table:
ploughera0a49c32010-08-11 01:47:59 +00004113 return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL,
4114 noI);
plougher0e453652006-11-06 01:49:35 +00004115}
4116
plougher2ea89142008-03-11 01:34:19 +00004117
Phillip Lougher8e44e052012-11-26 02:58:35 +00004118char *get_component(char *target, char **targname)
plougher8f8e1a12007-10-18 02:50:21 +00004119{
Phillip Lougher8e44e052012-11-26 02:58:35 +00004120 char *start;
4121
plougher8f8e1a12007-10-18 02:50:21 +00004122 while(*target == '/')
rlougherc4ebcf52007-11-08 17:52:49 +00004123 target ++;
plougher8f8e1a12007-10-18 02:50:21 +00004124
Phillip Lougher8e44e052012-11-26 02:58:35 +00004125 start = target;
plougher8f8e1a12007-10-18 02:50:21 +00004126 while(*target != '/' && *target!= '\0')
Phillip Lougher8e44e052012-11-26 02:58:35 +00004127 target ++;
plougher8f8e1a12007-10-18 02:50:21 +00004128
Phillip Lougher8e44e052012-11-26 02:58:35 +00004129 *targname = strndup(start, target - start);
plougher8f8e1a12007-10-18 02:50:21 +00004130
4131 return target;
4132}
4133
4134
4135void free_path(struct pathname *paths)
4136{
4137 int i;
4138
4139 for(i = 0; i < paths->names; i++) {
4140 if(paths->name[i].paths)
4141 free_path(paths->name[i].paths);
4142 free(paths->name[i].name);
4143 if(paths->name[i].preg) {
4144 regfree(paths->name[i].preg);
4145 free(paths->name[i].preg);
4146 }
4147 }
4148
4149 free(paths);
4150}
4151
4152
4153struct pathname *add_path(struct pathname *paths, char *target, char *alltarget)
4154{
Phillip Lougher8e44e052012-11-26 02:58:35 +00004155 char *targname;
plougher8f8e1a12007-10-18 02:50:21 +00004156 int i, error;
4157
Phillip Lougher8e44e052012-11-26 02:58:35 +00004158 target = get_component(target, &targname);
plougher8f8e1a12007-10-18 02:50:21 +00004159
4160 if(paths == NULL) {
plougherdec6ef12010-12-18 02:47:53 +00004161 paths = malloc(sizeof(struct pathname));
4162 if(paths == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004163 MEM_ERROR();
plougher8f8e1a12007-10-18 02:50:21 +00004164
4165 paths->names = 0;
4166 paths->name = NULL;
4167 }
4168
4169 for(i = 0; i < paths->names; i++)
4170 if(strcmp(paths->name[i].name, targname) == 0)
4171 break;
4172
4173 if(i == paths->names) {
4174 /* allocate new name entry */
4175 paths->names ++;
plougherb3604122009-03-30 02:07:20 +00004176 paths->name = realloc(paths->name, (i + 1) *
4177 sizeof(struct path_entry));
plougher6f2a8262010-07-27 00:37:19 +00004178 if(paths->name == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004179 MEM_ERROR();
Phillip Lougher8e44e052012-11-26 02:58:35 +00004180 paths->name[i].name = targname;
plougher8f8e1a12007-10-18 02:50:21 +00004181 paths->name[i].paths = NULL;
4182 if(use_regex) {
4183 paths->name[i].preg = malloc(sizeof(regex_t));
plougher5c60eab2010-07-21 01:12:19 +00004184 if(paths->name[i].preg == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004185 MEM_ERROR();
plougherb3604122009-03-30 02:07:20 +00004186 error = regcomp(paths->name[i].preg, targname,
4187 REG_EXTENDED|REG_NOSUB);
plougher23377982007-11-12 04:04:48 +00004188 if(error) {
Phillip Lougher62859d22012-11-30 05:53:56 +00004189 char str[1024]; /* overflow safe */
plougher8f8e1a12007-10-18 02:50:21 +00004190
4191 regerror(error, paths->name[i].preg, str, 1024);
plougherb3604122009-03-30 02:07:20 +00004192 BAD_ERROR("invalid regex %s in export %s, "
plougher50b31762009-03-31 04:14:46 +00004193 "because %s\n", targname, alltarget,
4194 str);
plougher8f8e1a12007-10-18 02:50:21 +00004195 }
4196 } else
4197 paths->name[i].preg = NULL;
4198
4199 if(target[0] == '\0')
4200 /* at leaf pathname component */
4201 paths->name[i].paths = NULL;
4202 else
4203 /* recurse adding child components */
plougher50b31762009-03-31 04:14:46 +00004204 paths->name[i].paths = add_path(NULL, target,
4205 alltarget);
plougher8f8e1a12007-10-18 02:50:21 +00004206 } else {
4207 /* existing matching entry */
Phillip Lougher8e44e052012-11-26 02:58:35 +00004208 free(targname);
4209
plougher8f8e1a12007-10-18 02:50:21 +00004210 if(paths->name[i].paths == NULL) {
plougher50b31762009-03-31 04:14:46 +00004211 /* No sub-directory which means this is the leaf
4212 * component of a pre-existing exclude which subsumes
4213 * the exclude currently being added, in which case stop
4214 * adding components */
plougher8f8e1a12007-10-18 02:50:21 +00004215 } else if(target[0] == '\0') {
plougherb3604122009-03-30 02:07:20 +00004216 /* at leaf pathname component and child components exist
plougher50b31762009-03-31 04:14:46 +00004217 * from more specific excludes, delete as they're
4218 * subsumed by this exclude */
plougher8f8e1a12007-10-18 02:50:21 +00004219 free_path(paths->name[i].paths);
4220 paths->name[i].paths = NULL;
4221 } else
4222 /* recurse adding child components */
4223 add_path(paths->name[i].paths, target, alltarget);
4224 }
4225
4226 return paths;
4227}
plougher2ea89142008-03-11 01:34:19 +00004228
4229
plougher05e50ef2007-10-23 12:34:20 +00004230void add_exclude(char *target)
plougher8f8e1a12007-10-18 02:50:21 +00004231{
plougher05e50ef2007-10-23 12:34:20 +00004232
plougherb3604122009-03-30 02:07:20 +00004233 if(target[0] == '/' || strncmp(target, "./", 2) == 0 ||
4234 strncmp(target, "../", 3) == 0)
4235 BAD_ERROR("/, ./ and ../ prefixed excludes not supported with "
4236 "-wildcards or -regex options\n");
plougher05e50ef2007-10-23 12:34:20 +00004237 else if(strncmp(target, "... ", 4) == 0)
4238 stickypath = add_path(stickypath, target + 4, target + 4);
4239 else
4240 path = add_path(path, target, target);
plougher8f8e1a12007-10-18 02:50:21 +00004241}
4242
4243
4244void display_path(int depth, struct pathname *paths)
4245{
4246 int i, n;
4247
4248 if(paths == NULL)
4249 return;
4250
4251 for(i = 0; i < paths->names; i++) {
4252 for(n = 0; n < depth; n++)
4253 printf("\t");
4254 printf("%d: %s\n", depth, paths->name[i].name);
4255 display_path(depth + 1, paths->name[i].paths);
4256 }
4257}
4258
4259
4260void display_path2(struct pathname *paths, char *string)
4261{
4262 int i;
Phillip Lougher91459c12012-11-30 05:24:44 +00004263 char *path;
plougher8f8e1a12007-10-18 02:50:21 +00004264
4265 if(paths == NULL) {
4266 printf("%s\n", string);
4267 return;
4268 }
4269
4270 for(i = 0; i < paths->names; i++) {
Phillip Lougher91459c12012-11-30 05:24:44 +00004271 int res = asprintf(&path, "%s/%s", string, paths->name[i].name);
4272 if(res == -1)
4273 BAD_ERROR("asprintf failed in display_path2\n");
plougher8f8e1a12007-10-18 02:50:21 +00004274 display_path2(paths->name[i].paths, path);
Phillip Lougher91459c12012-11-30 05:24:44 +00004275 free(path);
plougher8f8e1a12007-10-18 02:50:21 +00004276 }
4277}
4278
4279
plougherf9039c92007-10-22 03:54:16 +00004280struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path)
4281{
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004282 int count = paths == NULL ? 0 : paths->count;
4283
4284 if(count % PATHS_ALLOC_SIZE == 0) {
4285 paths = realloc(paths, sizeof(struct pathnames) +
4286 (count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *));
plougher6f2a8262010-07-27 00:37:19 +00004287 if(paths == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004288 MEM_ERROR();
plougher6f2a8262010-07-27 00:37:19 +00004289 }
plougherf9039c92007-10-22 03:54:16 +00004290
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004291 paths->path[count] = path;
4292 paths->count = count + 1;
plougherf9039c92007-10-22 03:54:16 +00004293 return paths;
4294}
4295
4296
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004297int excluded_match(char *name, struct pathname *path, struct pathnames **new)
plougherf9039c92007-10-22 03:54:16 +00004298{
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004299 int i;
plougher8f8e1a12007-10-18 02:50:21 +00004300
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004301 for(i = 0; i < path->names; i++) {
4302 int match = use_regex ?
4303 regexec(path->name[i].preg, name, (size_t) 0,
plougher50b31762009-03-31 04:14:46 +00004304 NULL, 0) == 0 :
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004305 fnmatch(path->name[i].name, name,
4306 FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0;
plougherf9039c92007-10-22 03:54:16 +00004307
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004308 if(match) {
4309 if(path->name[i].paths == NULL || new == NULL)
plougherb3604122009-03-30 02:07:20 +00004310 /* match on a leaf component, any subdirectories
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004311 * in the filesystem should be excluded */
4312 return TRUE;
4313 else
plougherb3604122009-03-30 02:07:20 +00004314 /* match on a non-leaf component, add any
plougher50b31762009-03-31 04:14:46 +00004315 * subdirectories to the new set of
4316 * subdirectories to scan for this name */
plougherf9039c92007-10-22 03:54:16 +00004317 *new = add_subdir(*new, path->name[i].paths);
4318 }
4319 }
4320
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004321 return FALSE;
4322}
4323
4324
4325int excluded(char *name, struct pathnames *paths, struct pathnames **new)
4326{
4327 int n;
4328
4329 if(stickypath && excluded_match(name, stickypath, NULL))
4330 return TRUE;
4331
4332 for(n = 0; paths && n < paths->count; n++) {
4333 int res = excluded_match(name, paths->path[n], new);
4334 if(res) {
4335 free(*new);
4336 *new = NULL;
4337 return TRUE;
4338 }
plougherf9039c92007-10-22 03:54:16 +00004339 }
4340
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00004341 /*
4342 * Either:
4343 * - no matching names found, return empty new search set, or
4344 * - one or more matches with sub-directories found (no leaf matches),
4345 * in which case return new search set.
4346 *
4347 * In either case return FALSE as we don't want to exclude this entry
4348 */
plougher8f8e1a12007-10-18 02:50:21 +00004349 return FALSE;
4350}
4351
4352
Phillip Lougher386128f2012-12-16 05:23:45 +00004353void process_exclude_file(char *argv)
4354{
4355 FILE *fd;
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004356 char buffer[MAX_LINE + 1]; /* overflow safe */
Phillip Lougherb22336d2012-12-20 18:44:22 +00004357 char *filename;
Phillip Lougher386128f2012-12-16 05:23:45 +00004358
Phillip Lougherb22336d2012-12-20 18:44:22 +00004359 fd = fopen(argv, "r");
4360 if(fd == NULL)
4361 BAD_ERROR("Failed to open exclude file \"%s\" because %s\n",
4362 argv, strerror(errno));
Phillip Lougher386128f2012-12-16 05:23:45 +00004363
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004364 while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) {
Phillip Lougherb22336d2012-12-20 18:44:22 +00004365 int len = strlen(filename);
4366
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004367 if(len == MAX_LINE && filename[len - 1] != '\n')
Phillip Lougherb22336d2012-12-20 18:44:22 +00004368 /* line too large */
4369 BAD_ERROR("Line too long when reading "
Phillip Lougherfe2c7352012-12-23 06:55:41 +00004370 "exclude file \"%s\", larger than %d "
Phillip Lougher83847c12012-12-23 07:22:36 +00004371 "bytes\n", argv, MAX_LINE);
Phillip Lougherb22336d2012-12-20 18:44:22 +00004372
4373 /*
4374 * Remove '\n' terminator if it exists (the last line
4375 * in the file may not be '\n' terminated)
4376 */
4377 if(len && filename[len - 1] == '\n')
4378 filename[len - 1] = '\0';
4379
4380 /* Skip any leading whitespace */
4381 while(isspace(*filename))
4382 filename ++;
4383
4384 /* if comment line, skip */
4385 if(*filename == '#')
4386 continue;
4387
4388 /*
4389 * check for initial backslash, to accommodate
4390 * filenames with leading space or leading # character
4391 */
4392 if(*filename == '\\')
4393 filename ++;
4394
4395 /* if line is now empty after skipping characters, skip it */
4396 if(*filename == '\0')
4397 continue;
4398
Phillip Lougher386128f2012-12-16 05:23:45 +00004399 if(old_exclude)
4400 old_add_exclude(filename);
4401 else
4402 add_exclude(filename);
Phillip Lougherb22336d2012-12-20 18:44:22 +00004403 }
4404
4405 if(ferror(fd))
4406 BAD_ERROR("Reading exclude file \"%s\" failed because %s\n",
4407 argv, strerror(errno));
Phillip Lougher386128f2012-12-16 05:23:45 +00004408
4409 fclose(fd);
4410}
4411
4412
plougher99ac0cc2007-10-29 03:17:10 +00004413#define RECOVER_ID "Squashfs recovery file v1.0\n"
4414#define RECOVER_ID_SIZE 28
4415
plougher64e83fd2010-12-31 21:21:26 +00004416void write_recovery_data(struct squashfs_super_block *sBlk)
plougher99ac0cc2007-10-29 03:17:10 +00004417{
plougher1d065e92010-06-18 03:58:27 +00004418 int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start;
plougher99ac0cc2007-10-29 03:17:10 +00004419 pid_t pid = getpid();
plougher44d54ef2010-02-08 22:13:49 +00004420 char *metadata;
plougher99ac0cc2007-10-29 03:17:10 +00004421 char header[] = RECOVER_ID;
4422
4423 if(recover == FALSE) {
4424 printf("No recovery data option specified.\n");
ploughereac18532007-10-29 05:26:06 +00004425 printf("Skipping saving recovery file.\n\n");
plougher99ac0cc2007-10-29 03:17:10 +00004426 return;
4427 }
4428
plougher1b879bc2010-12-18 02:49:42 +00004429 metadata = malloc(bytes);
4430 if(metadata == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004431 MEM_ERROR();
plougher44d54ef2010-02-08 22:13:49 +00004432
plougher1d065e92010-06-18 03:58:27 +00004433 res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata);
Phillip Lougher477f4332013-03-06 03:55:08 +00004434 if(res == 0) {
4435 ERROR("Failed to read append filesystem metadata\n");
4436 BAD_ERROR("Filesystem corrupted?\n");
4437 }
plougher99ac0cc2007-10-29 03:17:10 +00004438
Phillip Lougheraf4c9232012-11-15 03:46:28 +00004439 res = asprintf(&recovery_file, "squashfs_recovery_%s_%d",
plougher44d54ef2010-02-08 22:13:49 +00004440 getbase(destination_file), pid);
Phillip Lougheraf4c9232012-11-15 03:46:28 +00004441 if(res == -1)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004442 MEM_ERROR();
Phillip Lougheraf4c9232012-11-15 03:46:28 +00004443
plougherb3604122009-03-30 02:07:20 +00004444 recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
4445 if(recoverfd == -1)
4446 BAD_ERROR("Failed to create recovery file, because %s. "
4447 "Aborting\n", strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004448
plougher628e7682009-03-29 22:12:24 +00004449 if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1)
plougherb3604122009-03-30 02:07:20 +00004450 BAD_ERROR("Failed to write recovery file, because %s\n",
4451 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004452
plougher64e83fd2010-12-31 21:21:26 +00004453 if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1)
plougherb3604122009-03-30 02:07:20 +00004454 BAD_ERROR("Failed to write recovery file, because %s\n",
4455 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004456
plougher628e7682009-03-29 22:12:24 +00004457 if(write_bytes(recoverfd, metadata, bytes) == -1)
plougherb3604122009-03-30 02:07:20 +00004458 BAD_ERROR("Failed to write recovery file, because %s\n",
4459 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004460
4461 close(recoverfd);
plougher44d54ef2010-02-08 22:13:49 +00004462 free(metadata);
plougher99ac0cc2007-10-29 03:17:10 +00004463
4464 printf("Recovery file \"%s\" written\n", recovery_file);
4465 printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n");
plougherb3604122009-03-30 02:07:20 +00004466 printf("mksquashfs dummy %s -recover %s\n", destination_file,
4467 recovery_file);
plougher99ac0cc2007-10-29 03:17:10 +00004468 printf("to restore filesystem\n\n");
4469}
4470
4471
4472void read_recovery_data(char *recovery_file, char *destination_file)
4473{
4474 int fd, recoverfd, bytes;
plougher64e83fd2010-12-31 21:21:26 +00004475 struct squashfs_super_block orig_sBlk, sBlk;
plougher99ac0cc2007-10-29 03:17:10 +00004476 char *metadata;
plougher8a8c4102009-03-29 22:28:49 +00004477 int res;
plougher99ac0cc2007-10-29 03:17:10 +00004478 struct stat buf;
4479 char header[] = RECOVER_ID;
4480 char header2[RECOVER_ID_SIZE];
4481
plougher9e9d9dc2010-12-18 02:53:57 +00004482 recoverfd = open(recovery_file, O_RDONLY);
4483 if(recoverfd == -1)
plougherb3604122009-03-30 02:07:20 +00004484 BAD_ERROR("Failed to open recovery file because %s\n",
4485 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004486
4487 if(stat(destination_file, &buf) == -1)
plougherb3604122009-03-30 02:07:20 +00004488 BAD_ERROR("Failed to stat destination file, because %s\n",
4489 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004490
plougher9e9d9dc2010-12-18 02:53:57 +00004491 fd = open(destination_file, O_RDWR);
4492 if(fd == -1)
plougherb3604122009-03-30 02:07:20 +00004493 BAD_ERROR("Failed to open destination file because %s\n",
4494 strerror(errno));
plougher99ac0cc2007-10-29 03:17:10 +00004495
plougher8a8c4102009-03-29 22:28:49 +00004496 res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE);
4497 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00004498 BAD_ERROR("Failed to read recovery file, because %s\n",
4499 strerror(errno));
plougher8a8c4102009-03-29 22:28:49 +00004500 if(res < RECOVER_ID_SIZE)
4501 BAD_ERROR("Recovery file appears to be truncated\n");
plougher99ac0cc2007-10-29 03:17:10 +00004502 if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 )
4503 BAD_ERROR("Not a recovery file\n");
4504
plougher64e83fd2010-12-31 21:21:26 +00004505 res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block));
plougher8a8c4102009-03-29 22:28:49 +00004506 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00004507 BAD_ERROR("Failed to read recovery file, because %s\n",
4508 strerror(errno));
plougher64e83fd2010-12-31 21:21:26 +00004509 if(res < sizeof(struct squashfs_super_block))
plougher8a8c4102009-03-29 22:28:49 +00004510 BAD_ERROR("Recovery file appears to be truncated\n");
plougher99ac0cc2007-10-29 03:17:10 +00004511
plougher64e83fd2010-12-31 21:21:26 +00004512 res = read_fs_bytes(fd, 0, sizeof(struct squashfs_super_block), &orig_sBlk);
Phillip Lougher477f4332013-03-06 03:55:08 +00004513 if(res == 0) {
4514 ERROR("Failed to read superblock from output filesystem\n");
4515 BAD_ERROR("Output filesystem is empty!\n");
4516 }
plougher99ac0cc2007-10-29 03:17:10 +00004517
plougherb3604122009-03-30 02:07:20 +00004518 if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4,
plougher64e83fd2010-12-31 21:21:26 +00004519 sizeof(struct squashfs_super_block) - 4) != 0)
plougherb3604122009-03-30 02:07:20 +00004520 BAD_ERROR("Recovery file and destination file do not seem to "
4521 "match\n");
plougher99ac0cc2007-10-29 03:17:10 +00004522
4523 bytes = sBlk.bytes_used - sBlk.inode_table_start;
4524
plougher9e9d9dc2010-12-18 02:53:57 +00004525 metadata = malloc(bytes);
4526 if(metadata == NULL)
Phillip Lougherec71c3c2013-02-21 22:25:53 +00004527 MEM_ERROR();
plougher99ac0cc2007-10-29 03:17:10 +00004528
plougher8a8c4102009-03-29 22:28:49 +00004529 res = read_bytes(recoverfd, metadata, bytes);
4530 if(res == -1)
plougherb3604122009-03-30 02:07:20 +00004531 BAD_ERROR("Failed to read recovery file, because %s\n",
4532 strerror(errno));
plougher8a8c4102009-03-29 22:28:49 +00004533 if(res < bytes)
plougher99ac0cc2007-10-29 03:17:10 +00004534 BAD_ERROR("Recovery file appears to be truncated\n");
4535
plougher64e83fd2010-12-31 21:21:26 +00004536 write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk);
plougher99ac0cc2007-10-29 03:17:10 +00004537
plougher0dd6f122009-03-29 21:43:57 +00004538 write_destination(fd, sBlk.inode_table_start, bytes, metadata);
plougher99ac0cc2007-10-29 03:17:10 +00004539
4540 close(recoverfd);
4541 close(fd);
4542
plougherb3604122009-03-30 02:07:20 +00004543 printf("Successfully wrote recovery file \"%s\". Exiting\n",
4544 recovery_file);
plougher99ac0cc2007-10-29 03:17:10 +00004545
4546 exit(0);
4547}
4548
4549
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004550int parse_number(char *start, int *res, int size)
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004551{
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004552 char *end;
4553 long number = strtol(start, &end, 10);
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004554
Phillip Lougherf4f48382012-11-29 03:52:44 +00004555 /*
4556 * check for strtol underflow or overflow in conversion.
4557 * Note: strtol can validly return LONG_MIN and LONG_MAX
4558 * if the user entered these values, but, additional code
4559 * to distinguish this scenario is unnecessary, because for
4560 * our purposes LONG_MIN and LONG_MAX are too large anyway
4561 */
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004562 if(number == LONG_MIN || number == LONG_MAX)
4563 return 0;
4564
4565 /* reject negative numbers as invalid */
4566 if(number < 0)
4567 return 0;
4568
4569 /* check if long result will overflow signed int */
4570 if(number > INT_MAX)
4571 return 0;
4572
4573 if(size) {
Phillip Loughered345692012-11-28 03:56:16 +00004574 /*
4575 * Check for multiplier and trailing junk.
4576 * But first check that a number exists before the
4577 * multiplier
4578 */
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004579 if(end == start)
Phillip Loughered345692012-11-28 03:56:16 +00004580 return 0;
4581
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004582 switch(end[0]) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004583 case 'm':
4584 case 'M':
4585 if(multiply_overflow((int) number, 1048576))
4586 return 0;
4587 number *= 1048576;
Phillip Loughera9e65632012-11-28 04:17:27 +00004588
Phillip Lougherf968be82012-12-04 05:12:32 +00004589 if(end[1] != '\0')
Phillip Loughera9e65632012-11-28 04:17:27 +00004590 /* trailing junk after number */
4591 return 0;
4592
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004593 break;
4594 case 'k':
4595 case 'K':
4596 if(multiply_overflow((int) number, 1024))
4597 return 0;
4598 number *= 1024;
Phillip Loughera9e65632012-11-28 04:17:27 +00004599
Phillip Lougherf968be82012-12-04 05:12:32 +00004600 if(end[1] != '\0')
Phillip Loughera9e65632012-11-28 04:17:27 +00004601 /* trailing junk after number */
4602 return 0;
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004603 case '\0':
4604 break;
4605 default:
4606 /* trailing junk after number */
4607 return 0;
4608 }
Phillip Lougherda96f5b2012-11-28 04:32:13 +00004609 } else if(end[0] != '\0')
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004610 /* trailing junk after number */
4611 return 0;
4612
4613 *res = number;
4614 return 1;
4615}
4616
4617
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00004618void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad)
4619{
4620 int i;
4621
4622 sBlk->fragments = fragments;
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00004623 sBlk->no_ids = id_count;
4624 sBlk->inode_table_start = write_inodes();
4625 sBlk->directory_table_start = write_directories();
4626 sBlk->fragment_table_start = write_fragment_table();
4627 sBlk->lookup_table_start = exportable ? write_inode_lookup_table() :
4628 SQUASHFS_INVALID_BLK;
4629 sBlk->id_table_start = write_id_table();
4630 sBlk->xattr_id_table_start = write_xattrs();
4631
4632 TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start);
4633 TRACE("sBlk->directory_table_start 0x%llx\n",
4634 sBlk->directory_table_start);
4635 TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start);
4636 if(exportable)
4637 TRACE("sBlk->lookup_table_start 0x%llx\n",
4638 sBlk->lookup_table_start);
4639
4640 sBlk->bytes_used = bytes;
4641
4642 sBlk->compression = comp->id;
4643
4644 SQUASHFS_INSWAP_SUPER_BLOCK(sBlk);
4645 write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk);
4646
4647 if(!nopad && (i = bytes & (4096 - 1))) {
4648 char temp[4096] = {0};
4649 write_destination(fd, bytes, 4096 - i, temp);
4650 }
4651
4652 close(fd);
4653
4654 delete_pseudo_files();
4655
4656 if(recovery_file)
4657 unlink(recovery_file);
4658
4659 total_bytes += total_inode_bytes + total_directory_bytes +
4660 sizeof(struct squashfs_super_block) + total_xattr_bytes;
4661
4662 printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size"
4663 " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR,
4664 SQUASHFS_MINOR, comp->name, block_size);
4665 printf("\t%s data, %s metadata, %s fragments, %s xattrs\n",
4666 noD ? "uncompressed" : "compressed", noI ? "uncompressed" :
4667 "compressed", no_fragments ? "no" : noF ? "uncompressed" :
4668 "compressed", no_xattrs ? "no" : noX ? "uncompressed" :
4669 "compressed");
4670 printf("\tduplicates are %sremoved\n", duplicate_checking ? "" :
4671 "not ");
4672 printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0,
4673 bytes / (1024.0 * 1024.0));
4674 printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n",
4675 ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0);
4676 printf("Inode table size %d bytes (%.2f Kbytes)\n",
4677 inode_bytes, inode_bytes / 1024.0);
4678 printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n",
4679 ((float) inode_bytes / total_inode_bytes) * 100.0,
4680 total_inode_bytes);
4681 printf("Directory table size %d bytes (%.2f Kbytes)\n",
4682 directory_bytes, directory_bytes / 1024.0);
4683 printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n",
4684 ((float) directory_bytes / total_directory_bytes) * 100.0,
4685 total_directory_bytes);
4686 if(total_xattr_bytes) {
4687 printf("Xattr table size %d bytes (%.2f Kbytes)\n",
4688 xattr_bytes, xattr_bytes / 1024.0);
4689 printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n",
4690 ((float) xattr_bytes / total_xattr_bytes) * 100.0,
4691 total_xattr_bytes);
4692 }
4693 if(duplicate_checking)
4694 printf("Number of duplicate files found %d\n", file_count -
4695 dup_files);
4696 else
4697 printf("No duplicate files removed\n");
4698 printf("Number of inodes %d\n", inode_count);
4699 printf("Number of files %d\n", file_count);
4700 if(!no_fragments)
4701 printf("Number of fragments %d\n", fragments);
4702 printf("Number of symbolic links %d\n", sym_count);
4703 printf("Number of device nodes %d\n", dev_count);
4704 printf("Number of fifo nodes %d\n", fifo_count);
4705 printf("Number of socket nodes %d\n", sock_count);
4706 printf("Number of directories %d\n", dir_count);
4707 printf("Number of ids (unique uids + gids) %d\n", id_count);
4708 printf("Number of uids %d\n", uid_count);
4709
4710 for(i = 0; i < id_count; i++) {
4711 if(id_table[i]->flags & ISA_UID) {
4712 struct passwd *user = getpwuid(id_table[i]->id);
4713 printf("\t%s (%d)\n", user == NULL ? "unknown" :
4714 user->pw_name, id_table[i]->id);
4715 }
4716 }
4717
4718 printf("Number of gids %d\n", guid_count);
4719
4720 for(i = 0; i < id_count; i++) {
4721 if(id_table[i]->flags & ISA_GID) {
4722 struct group *group = getgrgid(id_table[i]->id);
4723 printf("\t%s (%d)\n", group == NULL ? "unknown" :
4724 group->gr_name, id_table[i]->id);
4725 }
4726 }
4727}
4728
4729
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004730int parse_num(char *arg, int *res)
4731{
4732 return parse_number(arg, res, 0);
4733}
4734
4735
plougher1f413c82005-11-18 00:02:14 +00004736#define VERSION() \
Phillip Lougher0280d992014-01-27 05:54:07 +00004737 printf("mksquashfs version 4.2-git (2014/01/25)\n");\
Phillip Loughercc064952014-01-03 04:26:34 +00004738 printf("copyright (C) 2014 Phillip Lougher "\
Phillip Lougher83d42a32012-10-31 23:42:22 +00004739 "<phillip@squashfs.org.uk>\n\n"); \
plougher16111452010-07-22 05:12:18 +00004740 printf("This program is free software; you can redistribute it and/or"\
4741 "\n");\
4742 printf("modify it under the terms of the GNU General Public License"\
4743 "\n");\
4744 printf("as published by the Free Software Foundation; either version "\
4745 "2,\n");\
plougher1f413c82005-11-18 00:02:14 +00004746 printf("or (at your option) any later version.\n\n");\
plougher16111452010-07-22 05:12:18 +00004747 printf("This program is distributed in the hope that it will be "\
4748 "useful,\n");\
4749 printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\
4750 "of\n");\
4751 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\
4752 "\n");\
plougher1f413c82005-11-18 00:02:14 +00004753 printf("GNU General Public License for more details.\n");
4754int main(int argc, char *argv[])
4755{
plougher324978d2006-02-27 04:53:29 +00004756 struct stat buf, source_buf;
plougher13fdddf2010-11-24 01:23:41 +00004757 int res, i;
plougher1f413c82005-11-18 00:02:14 +00004758 char *b, *root_name = NULL;
Phillip Loughera709bff2013-03-28 17:48:31 +00004759 int keep_as_directory = FALSE;
plougher1f413c82005-11-18 00:02:14 +00004760 squashfs_inode inode;
plougherb3604122009-03-30 02:07:20 +00004761 int readb_mbytes = READER_BUFFER_DEFAULT,
4762 writeb_mbytes = WRITER_BUFFER_DEFAULT,
4763 fragmentb_mbytes = FRAGMENT_BUFFER_DEFAULT;
Phillip Lougher569a9632013-04-22 03:30:30 +01004764 int progress = TRUE;
Phillip Lougherc6c73b22012-10-19 03:12:08 +01004765 int force_progress = FALSE;
Phillip Loughera709bff2013-03-28 17:48:31 +00004766 struct file_buffer **fragment = NULL;
plougher1f413c82005-11-18 00:02:14 +00004767
plougher1f413c82005-11-18 00:02:14 +00004768 block_log = slog(block_size);
4769 if(argc > 1 && strcmp(argv[1], "-version") == 0) {
4770 VERSION();
4771 exit(0);
4772 }
4773 for(i = 1; i < argc && argv[i][0] != '-'; i++);
4774 if(i < 3)
4775 goto printOptions;
4776 source_path = argv + 1;
4777 source = i - 2;
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004778
plougher4da4bd42010-11-21 05:01:54 +00004779 /*
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004780 * Scan the command line for -comp xxx option, this is to ensure
4781 * any -X compressor specific options are passed to the
4782 * correct compressor
plougher4da4bd42010-11-21 05:01:54 +00004783 */
plougher1f413c82005-11-18 00:02:14 +00004784 for(; i < argc; i++) {
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004785 struct compressor *prev_comp = comp;
4786
4787 if(strcmp(argv[i], "-comp") == 0) {
4788 if(++i == argc) {
4789 ERROR("%s: -comp missing compression type\n",
4790 argv[0]);
4791 exit(1);
4792 }
4793 comp = lookup_compressor(argv[i]);
4794 if(!comp->supported) {
4795 ERROR("%s: Compressor \"%s\" is not supported!"
4796 "\n", argv[0], argv[i]);
4797 ERROR("%s: Compressors available:\n", argv[0]);
4798 display_compressors("", COMP_DEFAULT);
4799 exit(1);
4800 }
4801 if(prev_comp != NULL && prev_comp != comp) {
4802 ERROR("%s: -comp multiple conflicting -comp"
4803 " options specified on command line"
4804 ", previously %s, now %s\n", argv[0],
4805 prev_comp->name, comp->name);
4806 exit(1);
4807 }
4808 compressor_opt_parsed = 1;
4809
4810 } else if(strcmp(argv[i], "-e") == 0)
4811 break;
4812 else if(strcmp(argv[i], "-root-becomes") == 0 ||
4813 strcmp(argv[i], "-ef") == 0 ||
4814 strcmp(argv[i], "-pf") == 0 ||
4815 strcmp(argv[i], "-af") == 0 ||
4816 strcmp(argv[i], "-comp") == 0)
4817 i++;
4818 }
4819
4820 /*
4821 * if no -comp option specified lookup default compressor. Note the
4822 * Makefile ensures the default compressor has been built, and so we
4823 * don't need to to check for failure here
4824 */
4825 if(comp == NULL)
4826 comp = lookup_compressor(COMP_DEFAULT);
4827
4828 for(i = source + 2; i < argc; i++) {
Phillip Lougher9523b042012-10-27 01:48:48 +01004829 if(strcmp(argv[i], "-action") == 0 ||
4830 strcmp(argv[i], "-a") ==0) {
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01004831 if(++i == argc) {
Phillip Lougher9523b042012-10-27 01:48:48 +01004832 ERROR("%s: %s missing action\n",
4833 argv[0], argv[i - 1]);
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01004834 exit(1);
4835 }
4836 res = parse_action(argv[i]);
4837 if(res == 0)
4838 exit(1);
4839
Phillip Lougherb73caba2012-12-24 21:58:41 +00004840 } else if(strcmp(argv[i], "-af") == 0) {
4841 if(++i == argc) {
4842 ERROR("%s: -af missing filename\n", argv[0]);
4843 exit(1);
4844 }
4845 if(read_action_file(argv[i]) == FALSE)
4846 exit(1);
4847
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004848 } else if(strcmp(argv[i], "-comp") == 0)
4849 /* parsed previously */
4850 i++;
plougherb5576ea2010-11-22 01:06:53 +00004851
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004852 else if(strncmp(argv[i], "-X", 2) == 0) {
Phillip Lougher774b7b32014-01-07 04:47:13 +00004853 int args;
4854
4855 if(strcmp(argv[i] + 2, "help") == 0)
4856 goto print_compressor_options;
4857
4858 args = compressor_options(comp, argv + i, argc - i);
plougherd8865672010-11-27 05:05:49 +00004859 if(args < 0) {
4860 if(args == -1) {
4861 ERROR("%s: Unrecognised compressor"
4862 " option %s\n", argv[0],
4863 argv[i]);
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004864 if(!compressor_opt_parsed)
4865 ERROR("%s: Did you forget to"
4866 " specify -comp?\n",
4867 argv[0]);
Phillip Lougher774b7b32014-01-07 04:47:13 +00004868print_compressor_options:
Phillip Lougherb1c3b6a2014-01-06 05:30:00 +00004869 ERROR("%s: selected compressor \"%s\""
4870 ". Options supported: %s\n",
4871 argv[0], comp->name,
4872 comp->usage ? "" : "none");
4873 if(comp->usage)
4874 comp->usage();
Phillip Lougher1f6c7402014-01-06 04:16:48 +00004875 }
plougherb5576ea2010-11-22 01:06:53 +00004876 exit(1);
4877 }
4878 i += args;
4879
plougherae9dcd82009-08-01 02:59:38 +00004880 } else if(strcmp(argv[i], "-pf") == 0) {
plougher43244f22009-04-05 02:04:51 +00004881 if(++i == argc) {
4882 ERROR("%s: -pf missing filename\n", argv[0]);
4883 exit(1);
4884 }
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00004885 if(read_pseudo_file(argv[i]) == FALSE)
plougher43244f22009-04-05 02:04:51 +00004886 exit(1);
plougher43244f22009-04-05 02:04:51 +00004887 } else if(strcmp(argv[i], "-p") == 0) {
4888 if(++i == argc) {
4889 ERROR("%s: -p missing pseudo file definition\n",
4890 argv[0]);
4891 exit(1);
4892 }
Phillip Lougher5ef2eba2012-12-24 20:33:13 +00004893 if(read_pseudo_def(argv[i]) == FALSE)
plougher43244f22009-04-05 02:04:51 +00004894 exit(1);
plougher43244f22009-04-05 02:04:51 +00004895 } else if(strcmp(argv[i], "-recover") == 0) {
plougher99ac0cc2007-10-29 03:17:10 +00004896 if(++i == argc) {
plougherb3604122009-03-30 02:07:20 +00004897 ERROR("%s: -recover missing recovery file\n",
4898 argv[0]);
plougher99ac0cc2007-10-29 03:17:10 +00004899 exit(1);
4900 }
4901 read_recovery_data(argv[i], argv[source + 1]);
4902 } else if(strcmp(argv[i], "-no-recovery") == 0)
4903 recover = FALSE;
4904 else if(strcmp(argv[i], "-wildcards") == 0) {
plougher934a9ed2007-10-19 00:21:10 +00004905 old_exclude = FALSE;
4906 use_regex = FALSE;
4907 } else if(strcmp(argv[i], "-regex") == 0) {
4908 old_exclude = FALSE;
4909 use_regex = TRUE;
4910 } else if(strcmp(argv[i], "-no-sparse") == 0)
plougher1f54edc2007-08-12 23:13:36 +00004911 sparse_files = FALSE;
4912 else if(strcmp(argv[i], "-no-progress") == 0)
plougher02bc3bc2007-02-25 12:12:01 +00004913 progress = FALSE;
Phillip Lougherc6c73b22012-10-19 03:12:08 +01004914 else if(strcmp(argv[i], "-progress") == 0)
4915 force_progress = TRUE;
plougher02bc3bc2007-02-25 12:12:01 +00004916 else if(strcmp(argv[i], "-no-exports") == 0)
4917 exportable = FALSE;
plougher0e453652006-11-06 01:49:35 +00004918 else if(strcmp(argv[i], "-processors") == 0) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004919 if((++i == argc) || !parse_num(argv[i], &processors)) {
plougher360514a2009-03-30 03:01:38 +00004920 ERROR("%s: -processors missing or invalid "
4921 "processor number\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004922 exit(1);
4923 }
4924 if(processors < 1) {
plougher360514a2009-03-30 03:01:38 +00004925 ERROR("%s: -processors should be 1 or larger\n",
4926 argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004927 exit(1);
4928 }
plougher0e453652006-11-06 01:49:35 +00004929 } else if(strcmp(argv[i], "-read-queue") == 0) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004930 if((++i == argc) ||
4931 !parse_num(argv[i], &readb_mbytes)) {
plougher50b31762009-03-31 04:14:46 +00004932 ERROR("%s: -read-queue missing or invalid "
4933 "queue size\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004934 exit(1);
4935 }
4936 if(readb_mbytes < 1) {
plougher360514a2009-03-30 03:01:38 +00004937 ERROR("%s: -read-queue should be 1 megabyte or "
4938 "larger\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004939 exit(1);
4940 }
plougher0e453652006-11-06 01:49:35 +00004941 } else if(strcmp(argv[i], "-write-queue") == 0) {
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004942 if((++i == argc) ||
4943 !parse_num(argv[i], &writeb_mbytes)) {
plougher50b31762009-03-31 04:14:46 +00004944 ERROR("%s: -write-queue missing or invalid "
4945 "queue size\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004946 exit(1);
4947 }
4948 if(writeb_mbytes < 1) {
plougher50b31762009-03-31 04:14:46 +00004949 ERROR("%s: -write-queue should be 1 megabyte "
4950 "or larger\n", argv[0]);
plougher5507dd92006-11-06 00:43:10 +00004951 exit(1);
4952 }
plougher217bad82008-04-05 11:36:41 +00004953 } else if(strcmp(argv[i], "-fragment-queue") == 0) {
plougher360514a2009-03-30 03:01:38 +00004954 if((++i == argc) ||
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004955 !parse_num(argv[i],
4956 &fragmentb_mbytes)) {
plougher360514a2009-03-30 03:01:38 +00004957 ERROR("%s: -fragment-queue missing or invalid "
4958 "queue size\n", argv[0]);
plougher217bad82008-04-05 11:36:41 +00004959 exit(1);
4960 }
4961 if(fragmentb_mbytes < 1) {
plougher50b31762009-03-31 04:14:46 +00004962 ERROR("%s: -fragment-queue should be 1 "
4963 "megabyte or larger\n", argv[0]);
plougher217bad82008-04-05 11:36:41 +00004964 exit(1);
4965 }
plougher5507dd92006-11-06 00:43:10 +00004966 } else if(strcmp(argv[i], "-b") == 0) {
plougher4c99cb72007-06-14 21:46:31 +00004967 if(++i == argc) {
4968 ERROR("%s: -b missing block size\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00004969 exit(1);
4970 }
Phillip Lougher94c1fe02012-11-28 03:32:36 +00004971 if(!parse_number(argv[i], &block_size, 1)) {
plougher4c99cb72007-06-14 21:46:31 +00004972 ERROR("%s: -b invalid block size\n", argv[0]);
4973 exit(1);
4974 }
plougher1f413c82005-11-18 00:02:14 +00004975 if((block_log = slog(block_size)) == 0) {
plougher50b31762009-03-31 04:14:46 +00004976 ERROR("%s: -b block size not power of two or "
4977 "not between 4096 and 1Mbyte\n",
4978 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00004979 exit(1);
4980 }
4981 } else if(strcmp(argv[i], "-ef") == 0) {
4982 if(++i == argc) {
4983 ERROR("%s: -ef missing filename\n", argv[0]);
4984 exit(1);
4985 }
plougher9b5bf8c2006-03-20 18:43:33 +00004986 } else if(strcmp(argv[i], "-no-duplicates") == 0)
plougher1f413c82005-11-18 00:02:14 +00004987 duplicate_checking = FALSE;
4988
4989 else if(strcmp(argv[i], "-no-fragments") == 0)
4990 no_fragments = TRUE;
4991
4992 else if(strcmp(argv[i], "-always-use-fragments") == 0)
4993 always_use_fragments = TRUE;
4994
4995 else if(strcmp(argv[i], "-sort") == 0) {
4996 if(++i == argc) {
4997 ERROR("%s: -sort missing filename\n", argv[0]);
4998 exit(1);
4999 }
5000 } else if(strcmp(argv[i], "-all-root") == 0 ||
5001 strcmp(argv[i], "-root-owned") == 0)
5002 global_uid = global_gid = 0;
5003
5004 else if(strcmp(argv[i], "-force-uid") == 0) {
5005 if(++i == argc) {
plougher50b31762009-03-31 04:14:46 +00005006 ERROR("%s: -force-uid missing uid or user\n",
5007 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005008 exit(1);
5009 }
5010 if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') {
plougher360514a2009-03-30 03:01:38 +00005011 if(global_uid < 0 || global_uid >
5012 (((long long) 1 << 32) - 1)) {
plougher50b31762009-03-31 04:14:46 +00005013 ERROR("%s: -force-uid uid out of range"
5014 "\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005015 exit(1);
5016 }
5017 } else {
5018 struct passwd *uid = getpwnam(argv[i]);
5019 if(uid)
5020 global_uid = uid->pw_uid;
5021 else {
plougher360514a2009-03-30 03:01:38 +00005022 ERROR("%s: -force-uid invalid uid or "
5023 "unknown user\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005024 exit(1);
5025 }
5026 }
5027 } else if(strcmp(argv[i], "-force-gid") == 0) {
5028 if(++i == argc) {
plougher360514a2009-03-30 03:01:38 +00005029 ERROR("%s: -force-gid missing gid or group\n",
5030 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005031 exit(1);
5032 }
5033 if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') {
plougher360514a2009-03-30 03:01:38 +00005034 if(global_gid < 0 || global_gid >
5035 (((long long) 1 << 32) - 1)) {
plougher50b31762009-03-31 04:14:46 +00005036 ERROR("%s: -force-gid gid out of range"
5037 "\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005038 exit(1);
5039 }
5040 } else {
5041 struct group *gid = getgrnam(argv[i]);
5042 if(gid)
5043 global_gid = gid->gr_gid;
5044 else {
plougher360514a2009-03-30 03:01:38 +00005045 ERROR("%s: -force-gid invalid gid or "
5046 "unknown group\n", argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005047 exit(1);
5048 }
5049 }
5050 } else if(strcmp(argv[i], "-noI") == 0 ||
5051 strcmp(argv[i], "-noInodeCompression") == 0)
5052 noI = TRUE;
5053
5054 else if(strcmp(argv[i], "-noD") == 0 ||
5055 strcmp(argv[i], "-noDataCompression") == 0)
5056 noD = TRUE;
5057
5058 else if(strcmp(argv[i], "-noF") == 0 ||
5059 strcmp(argv[i], "-noFragmentCompression") == 0)
5060 noF = TRUE;
5061
plougherb99d7832010-05-19 01:57:34 +00005062 else if(strcmp(argv[i], "-noX") == 0 ||
5063 strcmp(argv[i], "-noXattrCompression") == 0)
5064 noX = TRUE;
5065
plougherce564c62010-05-19 03:01:49 +00005066 else if(strcmp(argv[i], "-no-xattrs") == 0)
5067 no_xattrs = TRUE;
plougher6d89ac22010-05-19 02:59:23 +00005068
plougher30281c82010-08-25 05:06:11 +00005069 else if(strcmp(argv[i], "-xattrs") == 0)
5070 no_xattrs = FALSE;
5071
plougher1f413c82005-11-18 00:02:14 +00005072 else if(strcmp(argv[i], "-nopad") == 0)
5073 nopad = TRUE;
5074
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005075 else if(strcmp(argv[i], "-info") == 0)
plougher91fbb302008-05-06 02:29:36 +00005076 silent = FALSE;
plougher1f413c82005-11-18 00:02:14 +00005077
5078 else if(strcmp(argv[i], "-e") == 0)
5079 break;
5080
5081 else if(strcmp(argv[i], "-noappend") == 0)
5082 delete = TRUE;
5083
5084 else if(strcmp(argv[i], "-keep-as-directory") == 0)
5085 keep_as_directory = TRUE;
5086
5087 else if(strcmp(argv[i], "-root-becomes") == 0) {
5088 if(++i == argc) {
plougher50b31762009-03-31 04:14:46 +00005089 ERROR("%s: -root-becomes: missing name\n",
5090 argv[0]);
plougher1f413c82005-11-18 00:02:14 +00005091 exit(1);
5092 }
5093 root_name = argv[i];
5094 } else if(strcmp(argv[i], "-version") == 0) {
5095 VERSION();
5096 } else {
5097 ERROR("%s: invalid option\n\n", argv[0]);
5098printOptions:
plougher360514a2009-03-30 03:01:38 +00005099 ERROR("SYNTAX:%s source1 source2 ... dest [options] "
5100 "[-e list of exclude\ndirs/files]\n", argv[0]);
plougher37632562009-08-07 19:09:01 +00005101 ERROR("\nFilesystem build options:\n");
plougherff5ea8b2009-08-07 19:33:10 +00005102 ERROR("-comp <comp>\t\tselect <comp> compression\n");
plougher13df1782009-08-29 01:05:34 +00005103 ERROR("\t\t\tCompressors available:\n");
plougher764dab52009-08-24 18:28:04 +00005104 display_compressors("\t\t\t", COMP_DEFAULT);
plougher50b31762009-03-31 04:14:46 +00005105 ERROR("-b <block_size>\t\tset data block to "
5106 "<block_size>. Default %d bytes\n",
5107 SQUASHFS_FILE_SIZE);
plougher37632562009-08-07 19:09:01 +00005108 ERROR("-no-exports\t\tdon't make the filesystem "
5109 "exportable via NFS\n");
5110 ERROR("-no-sparse\t\tdon't detect sparse files\n");
plougher07d25c22010-08-25 17:41:57 +00005111 ERROR("-no-xattrs\t\tdon't store extended attributes"
plougher30281c82010-08-25 05:06:11 +00005112 NOXOPT_STR "\n");
plougher07d25c22010-08-25 17:41:57 +00005113 ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR
plougher30281c82010-08-25 05:06:11 +00005114 "\n");
plougher1f413c82005-11-18 00:02:14 +00005115 ERROR("-noI\t\t\tdo not compress inode table\n");
5116 ERROR("-noD\t\t\tdo not compress data blocks\n");
5117 ERROR("-noF\t\t\tdo not compress fragment blocks\n");
plougher16111452010-07-22 05:12:18 +00005118 ERROR("-noX\t\t\tdo not compress extended "
5119 "attributes\n");
plougher1f413c82005-11-18 00:02:14 +00005120 ERROR("-no-fragments\t\tdo not use fragments\n");
plougher360514a2009-03-30 03:01:38 +00005121 ERROR("-always-use-fragments\tuse fragment blocks for "
5122 "files larger than block size\n");
5123 ERROR("-no-duplicates\t\tdo not perform duplicate "
5124 "checking\n");
plougher1f413c82005-11-18 00:02:14 +00005125 ERROR("-all-root\t\tmake all files owned by root\n");
5126 ERROR("-force-uid uid\t\tset all file uids to uid\n");
5127 ERROR("-force-gid gid\t\tset all file gids to gid\n");
plougher50b31762009-03-31 04:14:46 +00005128 ERROR("-nopad\t\t\tdo not pad filesystem to a multiple "
5129 "of 4K\n");
plougher37632562009-08-07 19:09:01 +00005130 ERROR("-keep-as-directory\tif one source directory is "
5131 "specified, create a root\n");
5132 ERROR("\t\t\tdirectory containing that directory, "
5133 "rather than the\n");
5134 ERROR("\t\t\tcontents of the directory\n");
5135 ERROR("\nFilesystem filter options:\n");
plougher16111452010-07-22 05:12:18 +00005136 ERROR("-p <pseudo-definition>\tAdd pseudo file "
5137 "definition\n");
5138 ERROR("-pf <pseudo-file>\tAdd list of pseudo file "
5139 "definitions\n");
plougher360514a2009-03-30 03:01:38 +00005140 ERROR("-sort <sort_file>\tsort files according to "
5141 "priorities in <sort_file>. One\n");
5142 ERROR("\t\t\tfile or dir with priority per line. "
5143 "Priority -32768 to\n");
plougher1f413c82005-11-18 00:02:14 +00005144 ERROR("\t\t\t32767, default priority 0\n");
plougher50b31762009-03-31 04:14:46 +00005145 ERROR("-ef <exclude_file>\tlist of exclude dirs/files."
5146 " One per line\n");
plougher360514a2009-03-30 03:01:38 +00005147 ERROR("-wildcards\t\tAllow extended shell wildcards "
5148 "(globbing) to be used in\n\t\t\texclude "
5149 "dirs/files\n");
plougher50b31762009-03-31 04:14:46 +00005150 ERROR("-regex\t\t\tAllow POSIX regular expressions to "
5151 "be used in exclude\n\t\t\tdirs/files\n");
plougher37632562009-08-07 19:09:01 +00005152 ERROR("\nFilesystem append options:\n");
5153 ERROR("-noappend\t\tdo not append to existing "
5154 "filesystem\n");
5155 ERROR("-root-becomes <name>\twhen appending source "
5156 "files/directories, make the\n");
5157 ERROR("\t\t\toriginal root become a subdirectory in "
5158 "the new root\n");
5159 ERROR("\t\t\tcalled <name>, rather than adding the new "
5160 "source items\n");
5161 ERROR("\t\t\tto the original root\n");
5162 ERROR("\nMksquashfs runtime options:\n");
5163 ERROR("-version\t\tprint version, licence and "
5164 "copyright message\n");
5165 ERROR("-recover <name>\t\trecover filesystem data "
5166 "using recovery file <name>\n");
5167 ERROR("-no-recovery\t\tdon't generate a recovery "
5168 "file\n");
5169 ERROR("-info\t\t\tprint files written to filesystem\n");
5170 ERROR("-no-progress\t\tdon't display the progress "
5171 "bar\n");
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005172 ERROR("-progress\t\tdisplay progress bar when using "
5173 "the -info option\n");
plougher37632562009-08-07 19:09:01 +00005174 ERROR("-processors <number>\tUse <number> processors."
5175 " By default will use number of\n");
5176 ERROR("\t\t\tprocessors available\n");
5177 ERROR("-read-queue <size>\tSet input queue to <size> "
5178 "Mbytes. Default %d Mbytes\n",
5179 READER_BUFFER_DEFAULT);
5180 ERROR("-write-queue <size>\tSet output queue to <size> "
5181 "Mbytes. Default %d Mbytes\n",
5182 WRITER_BUFFER_DEFAULT);
plougher8bc376b2010-11-12 04:55:20 +00005183 ERROR("-fragment-queue <size>\tSet fragment queue to "
plougher37632562009-08-07 19:09:01 +00005184 "<size> Mbytes. Default %d Mbytes\n",
5185 FRAGMENT_BUFFER_DEFAULT);
5186 ERROR("\nMiscellaneous options:\n");
5187 ERROR("-root-owned\t\talternative name for -all-root"
5188 "\n");
5189 ERROR("-noInodeCompression\talternative name for -noI"
5190 "\n");
5191 ERROR("-noDataCompression\talternative name for -noD"
5192 "\n");
5193 ERROR("-noFragmentCompression\talternative name for "
5194 "-noF\n");
plougherb99d7832010-05-19 01:57:34 +00005195 ERROR("-noXattrCompression\talternative name for "
5196 "-noX\n");
Phillip Lougher774b7b32014-01-07 04:47:13 +00005197 ERROR("\n-Xhelp\t\t\tprint compressor options for"
5198 " selected compressor\n");
plougher4fb66822010-12-08 02:49:28 +00005199 ERROR("\nCompressors available and compressor specific "
5200 "options:\n");
5201 display_compressor_usage(COMP_DEFAULT);
plougher1f413c82005-11-18 00:02:14 +00005202 exit(1);
5203 }
5204 }
5205
plougherb747f4f2010-12-25 04:05:47 +00005206 /*
5207 * Some compressors may need the options to be checked for validity
5208 * once all the options have been processed
5209 */
5210 res = compressor_options_post(comp, block_size);
5211 if(res)
5212 EXIT_MKSQUASHFS();
5213
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005214 /*
5215 * If the -info option has been selected then disable the
5216 * progress bar unless it has been explicitly enabled with
5217 * the -progress option
5218 */
5219 if(!silent)
5220 progress = force_progress;
5221
5222#ifdef SQUASHFS_TRACE
Phillip Lougher989f5fe2013-04-22 03:16:29 +01005223 /*
5224 * Disable progress bar if full debug tracing is enabled.
5225 * The progress bar in this case just gets in the way of the
5226 * debug trace output
5227 */
Phillip Lougherc6c73b22012-10-19 03:12:08 +01005228 progress = FALSE;
5229#endif
5230
plougher91fbb302008-05-06 02:29:36 +00005231 for(i = 0; i < source; i++)
5232 if(lstat(source_path[i], &source_buf) == -1) {
plougher360514a2009-03-30 03:01:38 +00005233 fprintf(stderr, "Cannot stat source directory \"%s\" "
plougher50b31762009-03-31 04:14:46 +00005234 "because %s\n", source_path[i],
5235 strerror(errno));
plougher91fbb302008-05-06 02:29:36 +00005236 EXIT_MKSQUASHFS();
5237 }
plougher324978d2006-02-27 04:53:29 +00005238
5239 destination_file = argv[source + 1];
plougher1f413c82005-11-18 00:02:14 +00005240 if(stat(argv[source + 1], &buf) == -1) {
5241 if(errno == ENOENT) { /* Does not exist */
plougher360514a2009-03-30 03:01:38 +00005242 fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR,
plougher59dce672010-05-19 03:56:59 +00005243 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
plougher360514a2009-03-30 03:01:38 +00005244 if(fd == -1) {
plougher1f413c82005-11-18 00:02:14 +00005245 perror("Could not create destination file");
5246 exit(1);
5247 }
5248 delete = TRUE;
5249 } else {
5250 perror("Could not stat destination file");
5251 exit(1);
5252 }
5253
5254 } else {
5255 if(S_ISBLK(buf.st_mode)) {
5256 if((fd = open(argv[source + 1], O_RDWR)) == -1) {
plougher50b31762009-03-31 04:14:46 +00005257 perror("Could not open block device as "
5258 "destination");
plougher1f413c82005-11-18 00:02:14 +00005259 exit(1);
5260 }
5261 block_device = 1;
5262
5263 } else if(S_ISREG(buf.st_mode)) {
plougher360514a2009-03-30 03:01:38 +00005264 fd = open(argv[source + 1], (delete ? O_TRUNC : 0) |
5265 O_RDWR);
5266 if(fd == -1) {
plougher50b31762009-03-31 04:14:46 +00005267 perror("Could not open regular file for "
5268 "writing as destination");
plougher1f413c82005-11-18 00:02:14 +00005269 exit(1);
5270 }
plougher44d54ef2010-02-08 22:13:49 +00005271 }
plougher1f413c82005-11-18 00:02:14 +00005272 else {
5273 ERROR("Destination not block device or regular file\n");
5274 exit(1);
5275 }
5276
plougher324978d2006-02-27 04:53:29 +00005277 }
plougher1f413c82005-11-18 00:02:14 +00005278
plougher16111452010-07-22 05:12:18 +00005279 /*
5280 * process the exclude files - must be done afer destination file has
5281 * been possibly created
5282 */
plougher1f413c82005-11-18 00:02:14 +00005283 for(i = source + 2; i < argc; i++)
Phillip Lougher386128f2012-12-16 05:23:45 +00005284 if(strcmp(argv[i], "-ef") == 0)
5285 /*
5286 * Note presence of filename arg has already
5287 * been checked
5288 */
5289 process_exclude_file(argv[++i]);
5290 else if(strcmp(argv[i], "-e") == 0)
plougher1f413c82005-11-18 00:02:14 +00005291 break;
plougher8b9a7f62009-08-01 22:52:45 +00005292 else if(strcmp(argv[i], "-root-becomes") == 0 ||
plougher43244f22009-04-05 02:04:51 +00005293 strcmp(argv[i], "-sort") == 0 ||
5294 strcmp(argv[i], "-pf") == 0 ||
Phillip Lougherb73caba2012-12-24 21:58:41 +00005295 strcmp(argv[i], "-af") == 0 ||
plougher8b9a7f62009-08-01 22:52:45 +00005296 strcmp(argv[i], "-comp") == 0)
plougher1f413c82005-11-18 00:02:14 +00005297 i++;
5298
5299 if(i != argc) {
5300 if(++i == argc) {
5301 ERROR("%s: -e missing arguments\n", argv[0]);
plougher324978d2006-02-27 04:53:29 +00005302 EXIT_MKSQUASHFS();
plougher1f413c82005-11-18 00:02:14 +00005303 }
plougher8f8e1a12007-10-18 02:50:21 +00005304 while(i < argc)
5305 if(old_exclude)
5306 old_add_exclude(argv[i++]);
5307 else
plougher05e50ef2007-10-23 12:34:20 +00005308 add_exclude(argv[i++]);
plougher1f413c82005-11-18 00:02:14 +00005309 }
5310
5311 /* process the sort files - must be done afer the exclude files */
5312 for(i = source + 2; i < argc; i++)
5313 if(strcmp(argv[i], "-sort") == 0) {
ploughere1c9a742010-07-21 16:59:05 +00005314 int res = read_sort_file(argv[++i], source,
5315 source_path);
5316 if(res == FALSE)
5317 BAD_ERROR("Failed to read sort file\n");
plougher1f413c82005-11-18 00:02:14 +00005318 sorted ++;
5319 } else if(strcmp(argv[i], "-e") == 0)
5320 break;
plougher8b9a7f62009-08-01 22:52:45 +00005321 else if(strcmp(argv[i], "-root-becomes") == 0 ||
plougher43244f22009-04-05 02:04:51 +00005322 strcmp(argv[i], "-ef") == 0 ||
5323 strcmp(argv[i], "-pf") == 0 ||
Phillip Lougherb73caba2012-12-24 21:58:41 +00005324 strcmp(argv[i], "-af") == 0 ||
plougher8b9a7f62009-08-01 22:52:45 +00005325 strcmp(argv[i], "-comp") == 0)
plougher1f413c82005-11-18 00:02:14 +00005326 i++;
5327
plougher4c99cb72007-06-14 21:46:31 +00005328 if(!delete) {
ploughera175ce22009-07-30 04:43:27 +00005329 comp = read_super(fd, &sBlk, argv[source + 1]);
5330 if(comp == NULL) {
plougher360514a2009-03-30 03:01:38 +00005331 ERROR("Failed to read existing filesystem - will not "
5332 "overwrite - ABORTING!\n");
plougher50b31762009-03-31 04:14:46 +00005333 ERROR("To force Mksquashfs to write to this block "
5334 "device or file use -noappend\n");
plougher4c99cb72007-06-14 21:46:31 +00005335 EXIT_MKSQUASHFS();
5336 }
plougher1f413c82005-11-18 00:02:14 +00005337
plougher1f413c82005-11-18 00:02:14 +00005338 block_log = slog(block_size = sBlk.block_size);
5339 noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags);
5340 noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags);
5341 noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags);
plougher89c7a512010-12-15 08:35:51 +00005342 noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags);
plougher1f413c82005-11-18 00:02:14 +00005343 no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags);
5344 always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags);
5345 duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags);
plougher0e453652006-11-06 01:49:35 +00005346 exportable = SQUASHFS_EXPORTABLE(sBlk.flags);
plougherafae8252010-12-15 09:12:58 +00005347 no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags);
plougher3d7e5182010-12-25 01:49:42 +00005348 comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags);
plougher4c99cb72007-06-14 21:46:31 +00005349 }
5350
Phillip Lougherc97dac52013-04-14 03:32:10 +01005351 initialise_threads(readb_mbytes, writeb_mbytes, fragmentb_mbytes,
5352 delete);
plougher4c99cb72007-06-14 21:46:31 +00005353
plougher13fdddf2010-11-24 01:23:41 +00005354 res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0);
5355 if(res)
5356 BAD_ERROR("compressor_init failed\n");
5357
plougher4c99cb72007-06-14 21:46:31 +00005358 if(delete) {
plougher3d7e5182010-12-25 01:49:42 +00005359 int size;
Phillip Loughera45c9d22011-02-20 04:24:07 +00005360 void *comp_data = compressor_dump_options(comp, block_size,
5361 &size);
plougher3d7e5182010-12-25 01:49:42 +00005362
plougherdf6d8f02009-03-20 03:10:00 +00005363 printf("Creating %d.%d filesystem on %s, block size %d.\n",
plougher978f5882010-12-28 04:34:30 +00005364 SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size);
plougher3d7e5182010-12-25 01:49:42 +00005365
plougher871c72a2010-12-25 04:17:27 +00005366 /*
5367 * store any compressor specific options after the superblock,
5368 * and set the COMP_OPT flag to show that the filesystem has
5369 * compressor specfic options
5370 */
plougher3d7e5182010-12-25 01:49:42 +00005371 if(comp_data) {
5372 unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT;
5373
5374 SQUASHFS_INSWAP_SHORTS(&c_byte, 1);
plougher64e83fd2010-12-31 21:21:26 +00005375 write_destination(fd, sizeof(struct squashfs_super_block),
plougher29e2ace2010-12-31 08:50:00 +00005376 sizeof(c_byte), &c_byte);
plougher64e83fd2010-12-31 21:21:26 +00005377 write_destination(fd, sizeof(struct squashfs_super_block) +
plougher8e4dad42010-12-28 05:56:22 +00005378 sizeof(c_byte), size, comp_data);
plougher64e83fd2010-12-31 21:21:26 +00005379 bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte)
plougher3d7e5182010-12-25 01:49:42 +00005380 + size;
5381 comp_opts = TRUE;
plougher3d7e5182010-12-25 01:49:42 +00005382 } else
plougher64e83fd2010-12-31 21:21:26 +00005383 bytes = sizeof(struct squashfs_super_block);
plougher4c99cb72007-06-14 21:46:31 +00005384 } else {
plougher360514a2009-03-30 03:01:38 +00005385 unsigned int last_directory_block, inode_dir_offset,
5386 inode_dir_file_size, root_inode_size,
plougher50b31762009-03-31 04:14:46 +00005387 inode_dir_start_block, uncompressed_data,
5388 compressed_data, inode_dir_inode_number,
5389 inode_dir_parent_inode;
plougher360514a2009-03-30 03:01:38 +00005390 unsigned int root_inode_start =
5391 SQUASHFS_INODE_BLK(sBlk.root_inode),
5392 root_inode_offset =
5393 SQUASHFS_INODE_OFFSET(sBlk.root_inode);
Phillip Lougherb1259f72013-06-06 03:10:15 +01005394 sigset_t sigmask;
plougher4c99cb72007-06-14 21:46:31 +00005395
plougher360514a2009-03-30 03:01:38 +00005396 if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table,
5397 &data_cache, &directory_table,
5398 &directory_data_cache, &last_directory_block,
5399 &inode_dir_offset, &inode_dir_file_size,
5400 &root_inode_size, &inode_dir_start_block,
5401 &file_count, &sym_count, &dev_count, &dir_count,
5402 &fifo_count, &sock_count, &total_bytes,
5403 &total_inode_bytes, &total_directory_bytes,
plougher50b31762009-03-31 04:14:46 +00005404 &inode_dir_inode_number,
5405 &inode_dir_parent_inode, add_old_root_entry,
5406 &fragment_table, &inode_lookup_table)) == 0) {
plougher360514a2009-03-30 03:01:38 +00005407 ERROR("Failed to read existing filesystem - will not "
5408 "overwrite - ABORTING!\n");
plougher50b31762009-03-31 04:14:46 +00005409 ERROR("To force Mksquashfs to write to this block "
5410 "device or file use -noappend\n");
plougher324978d2006-02-27 04:53:29 +00005411 EXIT_MKSQUASHFS();
plougher1f413c82005-11-18 00:02:14 +00005412 }
plougher6f2a8262010-07-27 00:37:19 +00005413 if((fragments = sBlk.fragments)) {
plougher360514a2009-03-30 03:01:38 +00005414 fragment_table = realloc((char *) fragment_table,
plougher50b31762009-03-31 04:14:46 +00005415 ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1))
plougher8ed84b92010-12-31 10:37:24 +00005416 * sizeof(struct squashfs_fragment_entry));
plougher6f2a8262010-07-27 00:37:19 +00005417 if(fragment_table == NULL)
5418 BAD_ERROR("Out of memory in save filesystem state\n");
5419 }
plougher1f413c82005-11-18 00:02:14 +00005420
plougher50b31762009-03-31 04:14:46 +00005421 printf("Appending to existing %d.%d filesystem on %s, block "
plougher978f5882010-12-28 04:34:30 +00005422 "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1],
plougher360514a2009-03-30 03:01:38 +00005423 block_size);
plougher89c7a512010-12-15 08:35:51 +00005424 printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, "
plougherbb988032009-08-06 08:46:34 +00005425 "-always-use-fragments,\n-exportable and -comp options "
5426 "ignored\n");
plougher360514a2009-03-30 03:01:38 +00005427 printf("\nIf appending is not wanted, please re-run with "
5428 "-noappend specified!\n\n");
plougher1f413c82005-11-18 00:02:14 +00005429
plougher360514a2009-03-30 03:01:38 +00005430 compressed_data = (inode_dir_offset + inode_dir_file_size) &
5431 ~(SQUASHFS_METADATA_SIZE - 1);
5432 uncompressed_data = (inode_dir_offset + inode_dir_file_size) &
5433 (SQUASHFS_METADATA_SIZE - 1);
plougher1f413c82005-11-18 00:02:14 +00005434
5435 /* save original filesystem state for restoring ... */
5436 sfragments = fragments;
5437 sbytes = bytes;
5438 sinode_count = sBlk.inodes;
plougher23377982007-11-12 04:04:48 +00005439 scache_bytes = root_inode_offset + root_inode_size;
5440 sdirectory_cache_bytes = uncompressed_data;
ploughera2968ef2009-03-03 10:46:00 +00005441 sdata_cache = malloc(scache_bytes);
plougher332e43d2010-07-21 01:18:30 +00005442 if(sdata_cache == NULL)
5443 BAD_ERROR("Out of memory in save filesystem state\n");
ploughera2968ef2009-03-03 10:46:00 +00005444 sdirectory_data_cache = malloc(sdirectory_cache_bytes);
plougher332e43d2010-07-21 01:18:30 +00005445 if(sdirectory_data_cache == NULL)
5446 BAD_ERROR("Out of memory in save filesystem state\n");
plougher1f413c82005-11-18 00:02:14 +00005447 memcpy(sdata_cache, data_cache, scache_bytes);
plougher360514a2009-03-30 03:01:38 +00005448 memcpy(sdirectory_data_cache, directory_data_cache +
5449 compressed_data, sdirectory_cache_bytes);
plougher1f413c82005-11-18 00:02:14 +00005450 sinode_bytes = root_inode_start;
plougher1f413c82005-11-18 00:02:14 +00005451 stotal_bytes = total_bytes;
5452 stotal_inode_bytes = total_inode_bytes;
plougher50b31762009-03-31 04:14:46 +00005453 stotal_directory_bytes = total_directory_bytes +
5454 compressed_data;
plougher1f413c82005-11-18 00:02:14 +00005455 sfile_count = file_count;
5456 ssym_count = sym_count;
5457 sdev_count = dev_count;
5458 sdir_count = dir_count + 1;
5459 sfifo_count = fifo_count;
5460 ssock_count = sock_count;
5461 sdup_files = dup_files;
plougher1b899fc2008-08-07 01:24:06 +00005462 sid_count = id_count;
plougher99ac0cc2007-10-29 03:17:10 +00005463 write_recovery_data(&sBlk);
Phillip Lougher3bcf67d2013-02-24 10:56:38 +00005464 save_xattrs();
Phillip Lougher0280d992014-01-27 05:54:07 +00005465 restore_thread = init_restore_thread();
Phillip Loughera709bff2013-03-28 17:48:31 +00005466 sigemptyset(&sigmask);
5467 sigaddset(&sigmask, SIGINT);
5468 sigaddset(&sigmask, SIGTERM);
Phillip Lougherdd343392013-03-31 23:29:03 +01005469 sigaddset(&sigmask, SIGUSR1);
Phillip Lougherb1259f72013-06-06 03:10:15 +01005470 if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
Phillip Loughera709bff2013-03-28 17:48:31 +00005471 BAD_ERROR("Failed to set signal mask\n");
plougher0dd6f122009-03-29 21:43:57 +00005472 write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0");
plougher1f413c82005-11-18 00:02:14 +00005473
plougher360514a2009-03-30 03:01:38 +00005474 /*
5475 * set the filesystem state up to be able to append to the
plougher50b31762009-03-31 04:14:46 +00005476 * original filesystem. The filesystem state differs depending
5477 * on whether we're appending to the original root directory, or
5478 * if the original root directory becomes a sub-directory
5479 * (root-becomes specified on command line, here root_name !=
5480 * NULL)
plougher1f413c82005-11-18 00:02:14 +00005481 */
5482 inode_bytes = inode_size = root_inode_start;
5483 directory_size = last_directory_block;
5484 cache_size = root_inode_offset + root_inode_size;
5485 directory_cache_size = inode_dir_offset + inode_dir_file_size;
5486 if(root_name) {
plougherca2c93f2008-08-15 08:34:57 +00005487 sdirectory_bytes = last_directory_block;
5488 sdirectory_compressed_bytes = 0;
plougherdf70c3e2006-01-27 09:34:13 +00005489 root_inode_number = inode_dir_parent_inode;
Phillip Lougher539c2b12012-07-30 20:14:52 +01005490 inode_no = sBlk.inodes + 2;
plougher1f413c82005-11-18 00:02:14 +00005491 directory_bytes = last_directory_block;
5492 directory_cache_bytes = uncompressed_data;
plougher360514a2009-03-30 03:01:38 +00005493 memmove(directory_data_cache, directory_data_cache +
5494 compressed_data, uncompressed_data);
plougher1f413c82005-11-18 00:02:14 +00005495 cache_bytes = root_inode_offset + root_inode_size;
plougher360514a2009-03-30 03:01:38 +00005496 add_old_root_entry(root_name, sBlk.root_inode,
5497 inode_dir_inode_number, SQUASHFS_DIR_TYPE);
plougher1f413c82005-11-18 00:02:14 +00005498 total_directory_bytes += compressed_data;
5499 dir_count ++;
5500 } else {
plougher360514a2009-03-30 03:01:38 +00005501 sdirectory_compressed_bytes = last_directory_block -
5502 inode_dir_start_block;
5503 sdirectory_compressed =
5504 malloc(sdirectory_compressed_bytes);
plougher332e43d2010-07-21 01:18:30 +00005505 if(sdirectory_compressed == NULL)
plougher16111452010-07-22 05:12:18 +00005506 BAD_ERROR("Out of memory in save filesystem "
5507 "state\n");
plougher360514a2009-03-30 03:01:38 +00005508 memcpy(sdirectory_compressed, directory_table +
5509 inode_dir_start_block,
5510 sdirectory_compressed_bytes);
plougherca2c93f2008-08-15 08:34:57 +00005511 sdirectory_bytes = inode_dir_start_block;
plougherdf70c3e2006-01-27 09:34:13 +00005512 root_inode_number = inode_dir_inode_number;
Phillip Lougher539c2b12012-07-30 20:14:52 +01005513 inode_no = sBlk.inodes + 1;
plougher1f413c82005-11-18 00:02:14 +00005514 directory_bytes = inode_dir_start_block;
5515 directory_cache_bytes = inode_dir_offset;
5516 cache_bytes = root_inode_offset;
5517 }
5518
plougher360514a2009-03-30 03:01:38 +00005519 inode_count = file_count + dir_count + sym_count + dev_count +
5520 fifo_count + sock_count;
plougher1f413c82005-11-18 00:02:14 +00005521 }
5522
Phillip Lougherb2abb1c2013-01-09 04:51:21 +00005523 if(path)
5524 paths = add_subdir(paths, path);
plougherf9039c92007-10-22 03:54:16 +00005525
Phillip Lougher4bcb7f82011-08-28 03:18:26 +01005526 dump_actions();
5527
plougher360514a2009-03-30 03:01:38 +00005528 if(delete && !keep_as_directory && source == 1 &&
5529 S_ISDIR(source_buf.st_mode))
Phillip Lougherbae0e422014-01-19 23:42:11 +00005530 dir_scan(&inode, source_path[0], scan1_readdir, progress);
plougher50b31762009-03-31 04:14:46 +00005531 else if(!keep_as_directory && source == 1 &&
5532 S_ISDIR(source_buf.st_mode))
Phillip Lougherbae0e422014-01-19 23:42:11 +00005533 dir_scan(&inode, source_path[0], scan1_single_readdir, progress);
plougher1f413c82005-11-18 00:02:14 +00005534 else
Phillip Lougherbae0e422014-01-19 23:42:11 +00005535 dir_scan(&inode, "", scan1_encomp_readdir, progress);
plougher1f413c82005-11-18 00:02:14 +00005536 sBlk.root_inode = inode;
5537 sBlk.inodes = inode_count;
5538 sBlk.s_magic = SQUASHFS_MAGIC;
5539 sBlk.s_major = SQUASHFS_MAJOR;
plougher978f5882010-12-28 04:34:30 +00005540 sBlk.s_minor = SQUASHFS_MINOR;
plougher1f413c82005-11-18 00:02:14 +00005541 sBlk.block_size = block_size;
5542 sBlk.block_log = block_log;
plougher89c7a512010-12-15 08:35:51 +00005543 sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments,
plougherafae8252010-12-15 09:12:58 +00005544 always_use_fragments, duplicate_checking, exportable,
plougher3d7e5182010-12-25 01:49:42 +00005545 no_xattrs, comp_opts);
plougher1f413c82005-11-18 00:02:14 +00005546 sBlk.mkfs_time = time(NULL);
5547
Phillip Lougher83b8f862013-04-09 03:28:55 +01005548 disable_info();
plougherc9b11db2008-05-06 23:59:15 +00005549
Phillip Loughera709bff2013-03-28 17:48:31 +00005550 while((fragment = get_frag_action(fragment)))
5551 write_fragment(*fragment);
5552 unlock_fragments();
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01005553 pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex);
Phillip Loughera709bff2013-03-28 17:48:31 +00005554 pthread_mutex_lock(&fragment_mutex);
5555 while(fragments_outstanding) {
5556 pthread_mutex_unlock(&fragment_mutex);
5557 sched_yield();
5558 pthread_mutex_lock(&fragment_mutex);
5559 }
Phillip Lougherfd74d9a2013-03-31 22:49:50 +01005560 pthread_cleanup_pop(1);
Phillip Loughera709bff2013-03-28 17:48:31 +00005561
5562 queue_put(to_writer, NULL);
5563 if(queue_get(from_writer) != 0)
5564 EXIT_MKSQUASHFS();
5565
Phillip Loughercd1a5a62014-01-21 04:53:46 +00005566 set_progressbar_state(FALSE);
Phillip Lougherd6b1f0d2013-03-26 03:09:27 +00005567 write_filesystem_tables(&sBlk, nopad);
plougher99ac0cc2007-10-29 03:17:10 +00005568
plougher1f413c82005-11-18 00:02:14 +00005569 return 0;
5570}