plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1 | /* |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2 | * Create a squashfs filesystem. This is a highly compressed read only |
| 3 | * filesystem. |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4 | * |
Phillip Lougher | 43cc428 | 2012-01-25 00:25:56 +0000 | [diff] [blame] | 5 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, |
Phillip Lougher | cc06495 | 2014-01-03 04:26:34 +0000 | [diff] [blame] | 6 | * 2012, 2013, 2014 |
Phillip Lougher | 83d42a3 | 2012-10-31 23:42:22 +0000 | [diff] [blame] | 7 | * Phillip Lougher <phillip@squashfs.org.uk> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version 2, |
| 12 | * or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | * |
| 23 | * mksquashfs.c |
| 24 | */ |
| 25 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 26 | #define FALSE 0 |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 27 | #define TRUE 1 |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 28 | #define MAX_LINE 16384 |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 29 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 30 | #include <pwd.h> |
| 31 | #include <grp.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stdio.h> |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 35 | #include <stddef.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <errno.h> |
| 40 | #include <dirent.h> |
| 41 | #include <string.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 42 | #include <stdlib.h> |
| 43 | #include <signal.h> |
| 44 | #include <setjmp.h> |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 45 | #include <sys/types.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 46 | #include <sys/mman.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 47 | #include <pthread.h> |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 48 | #include <regex.h> |
| 49 | #include <fnmatch.h> |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 50 | #include <sys/wait.h> |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 51 | #include <limits.h> |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 52 | #include <ctype.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 53 | |
plougher | 6885329 | 2005-12-27 02:47:04 +0000 | [diff] [blame] | 54 | #ifndef linux |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 55 | #define __BYTE_ORDER BYTE_ORDER |
| 56 | #define __BIG_ENDIAN BIG_ENDIAN |
| 57 | #define __LITTLE_ENDIAN LITTLE_ENDIAN |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 58 | #include <sys/sysctl.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 59 | #else |
| 60 | #include <endian.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 61 | #include <sys/sysinfo.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 64 | #include "squashfs_fs.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 65 | #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 Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 71 | #include "action.h" |
Phillip Lougher | 2477d0d | 2012-10-24 21:49:28 +0100 | [diff] [blame] | 72 | #include "error.h" |
Phillip Lougher | 9a0e8bb | 2013-03-10 02:39:24 +0000 | [diff] [blame] | 73 | #include "progressbar.h" |
Phillip Lougher | 2e9fab1 | 2013-04-09 03:26:33 +0100 | [diff] [blame] | 74 | #include "info.h" |
Phillip Lougher | 71f3964 | 2013-04-09 04:43:21 +0100 | [diff] [blame] | 75 | #include "caches-queues-lists.h" |
Phillip Lougher | 1bbd0cc | 2013-04-19 04:03:00 +0100 | [diff] [blame] | 76 | #include "read_fs.h" |
Phillip Lougher | 0d67c9d | 2013-04-19 05:06:50 +0100 | [diff] [blame] | 77 | #include "restore.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 78 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 79 | int delete = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 80 | int fd; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 81 | struct squashfs_super_block sBlk; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 82 | |
| 83 | /* filesystem flags for building */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 84 | int comp_opts = FALSE; |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 85 | int no_xattrs = XATTR_DEF; |
| 86 | int noX = FALSE; |
| 87 | int duplicate_checking = TRUE; |
| 88 | int noF = FALSE; |
| 89 | int no_fragments = FALSE; |
| 90 | int always_use_fragments = FALSE; |
| 91 | int noI = FALSE; |
| 92 | int noD = FALSE; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 93 | int silent = TRUE; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 94 | int exportable = TRUE; |
plougher | 8dcc699 | 2007-08-17 19:32:52 +0000 | [diff] [blame] | 95 | int sparse_files = TRUE; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 96 | int old_exclude = TRUE; |
| 97 | int use_regex = FALSE; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 98 | int nopad = FALSE; |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 99 | int exit_on_error = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 100 | |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 101 | long long global_uid = -1, global_gid = -1; |
| 102 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 103 | /* superblock attributes */ |
| 104 | int block_size = SQUASHFS_FILE_SIZE, block_log; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 105 | unsigned int id_count = 0; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 106 | int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0, |
| 107 | sock_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 108 | |
| 109 | /* write position within data section */ |
| 110 | long long bytes = 0, total_bytes = 0; |
| 111 | |
| 112 | /* in memory directory table - possibly compressed */ |
| 113 | char *directory_table = NULL; |
| 114 | unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0; |
| 115 | |
| 116 | /* cached directory table */ |
| 117 | char *directory_data_cache = NULL; |
| 118 | unsigned int directory_cache_bytes = 0, directory_cache_size = 0; |
| 119 | |
| 120 | /* in memory inode table - possibly compressed */ |
| 121 | char *inode_table = NULL; |
| 122 | unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0; |
| 123 | |
| 124 | /* cached inode table */ |
| 125 | char *data_cache = NULL; |
| 126 | unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0; |
| 127 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 128 | /* inode lookup table */ |
| 129 | squashfs_inode *inode_lookup_table = NULL; |
| 130 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 131 | /* in memory directory data */ |
| 132 | #define I_COUNT_SIZE 128 |
| 133 | #define DIR_ENTRIES 32 |
| 134 | #define INODE_HASH_SIZE 65536 |
| 135 | #define INODE_HASH_MASK (INODE_HASH_SIZE - 1) |
| 136 | #define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK) |
| 137 | |
| 138 | struct cached_dir_index { |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 139 | struct squashfs_dir_index index; |
| 140 | char *name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | struct directory { |
| 144 | unsigned int start_block; |
| 145 | unsigned int size; |
| 146 | unsigned char *buff; |
| 147 | unsigned char *p; |
| 148 | unsigned int entry_count; |
| 149 | unsigned char *entry_count_p; |
| 150 | unsigned int i_count; |
| 151 | unsigned int i_size; |
| 152 | struct cached_dir_index *index; |
| 153 | unsigned char *index_count_p; |
| 154 | unsigned int inode_number; |
| 155 | }; |
| 156 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 157 | struct inode_info *inode_info[INODE_HASH_SIZE]; |
| 158 | |
| 159 | /* hash tables used to do fast duplicate searches in duplicate check */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 160 | struct file_info *dupl[65536]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 161 | int dup_files = 0; |
| 162 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 163 | /* exclude file handling */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 164 | /* list of exclude dirs/files */ |
| 165 | struct exclude_info { |
| 166 | dev_t st_dev; |
| 167 | ino_t st_ino; |
| 168 | }; |
| 169 | |
| 170 | #define EXCLUDE_SIZE 8192 |
| 171 | int exclude = 0; |
| 172 | struct exclude_info *exclude_paths = NULL; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 173 | int old_excluded(char *filename, struct stat *buf); |
| 174 | |
| 175 | struct path_entry { |
| 176 | char *name; |
| 177 | regex_t *preg; |
| 178 | struct pathname *paths; |
| 179 | }; |
| 180 | |
| 181 | struct pathname { |
| 182 | int names; |
| 183 | struct path_entry *name; |
| 184 | }; |
| 185 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 186 | struct pathnames { |
| 187 | int count; |
| 188 | struct pathname *path[0]; |
| 189 | }; |
| 190 | #define PATHS_ALLOC_SIZE 10 |
| 191 | |
| 192 | struct pathnames *paths = NULL; |
| 193 | struct pathname *path = NULL; |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 194 | struct pathname *stickypath = NULL; |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 195 | int excluded(char *name, struct pathnames *paths, struct pathnames **new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 196 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 197 | int fragments = 0; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 198 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 199 | #define FRAG_SIZE 32768 |
Phillip Lougher | 4570201 | 2013-04-30 05:19:54 +0100 | [diff] [blame] | 200 | #define FRAG_INDEX(n) (-(n + 2)) |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 201 | |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 202 | struct squashfs_fragment_entry *fragment_table = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 203 | int fragments_outstanding = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 204 | |
Phillip Lougher | 7ffdf2a | 2013-04-14 02:53:30 +0100 | [diff] [blame] | 205 | int fragments_locked = FALSE; |
Phillip Lougher | 7ffdf2a | 2013-04-14 02:53:30 +0100 | [diff] [blame] | 206 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 207 | /* current inode number for directories and non directories */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 208 | unsigned int inode_no = 1; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 209 | unsigned int root_inode_number = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 210 | |
| 211 | /* list of source dirs/files */ |
| 212 | int source = 0; |
| 213 | char **source_path; |
| 214 | |
| 215 | /* list of root directory entries read from original filesystem */ |
| 216 | int old_root_entries = 0; |
| 217 | struct old_root_entry_info { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 218 | char *name; |
| 219 | struct inode_info inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 220 | }; |
| 221 | struct old_root_entry_info *old_root_entry; |
| 222 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 223 | /* restore orignal filesystem state if appending to existing filesystem is |
| 224 | * cancelled */ |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 225 | char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 226 | |
| 227 | long long sbytes, stotal_bytes; |
| 228 | |
| 229 | unsigned int sinode_bytes, scache_bytes, sdirectory_bytes, |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 230 | sdirectory_cache_bytes, sdirectory_compressed_bytes, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 231 | stotal_inode_bytes, stotal_directory_bytes, |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 232 | sinode_count = 0, sfile_count, ssym_count, sdev_count, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 233 | sdir_count, sfifo_count, ssock_count, sdup_files; |
| 234 | int sfragments; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 235 | int threads; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 236 | |
| 237 | /* flag whether destination file is a block device */ |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 238 | int block_device = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 239 | |
| 240 | /* flag indicating whether files are sorted using sort list(s) */ |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 241 | int sorted = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 242 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 243 | /* save destination file name for deleting on error */ |
| 244 | char *destination_file = NULL; |
| 245 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 246 | /* recovery file for abnormal exit on appending */ |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 247 | char *recovery_file = NULL; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 248 | int recover = TRUE; |
| 249 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 250 | struct id *id_hash_table[ID_ENTRIES]; |
| 251 | struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS]; |
| 252 | unsigned int uid_count = 0, guid_count = 0; |
| 253 | unsigned int sid_count = 0, suid_count = 0, sguid_count = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 254 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 255 | struct cache *reader_buffer, *writer_buffer, *fragment_buffer; |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 256 | struct queue *to_reader, *to_deflate, *to_writer, *from_writer, |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 257 | *to_frag, *locked_fragment; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 258 | struct seq_queue *to_main; |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 259 | pthread_t reader_thread, writer_thread, main_thread; |
| 260 | pthread_t *deflator_thread, *frag_deflator_thread; |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 261 | pthread_t *restore_thread = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 262 | pthread_mutex_t fragment_mutex; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 263 | pthread_mutex_t pos_mutex; |
| 264 | |
| 265 | /* user options that control parallelisation */ |
| 266 | int processors = -1; |
| 267 | /* default size of output buffer in Mbytes */ |
| 268 | #define WRITER_BUFFER_DEFAULT 512 |
| 269 | /* default size of input buffer in Mbytes */ |
| 270 | #define READER_BUFFER_DEFAULT 64 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 271 | /* default size of fragment buffer in Mbytes */ |
| 272 | #define FRAGMENT_BUFFER_DEFAULT 64 |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 273 | int writer_buffer_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 274 | |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 275 | /* compression operations */ |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 276 | static struct compressor *comp = NULL; |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 277 | int compressor_opt_parsed = FALSE; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 278 | void *stream = NULL; |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 279 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 280 | /* xattr stats */ |
| 281 | unsigned int xattr_bytes = 0, total_xattr_bytes = 0; |
| 282 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame^] | 283 | /* fragment to file mapping used when appending */ |
| 284 | int append_fragments = 0; |
| 285 | struct append_file **file_mapping; |
| 286 | |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 287 | char *read_from_disk(long long start, unsigned int avail_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 288 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 289 | int type); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 290 | struct file_info *duplicate(long long file_size, long long bytes, |
| 291 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 292 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 293 | unsigned short fragment_checksum, int checksum_flag); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 294 | struct dir_info *dir_scan1(char *, char *, struct pathnames *, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 295 | struct dir_ent *(_readdir)(struct dir_info *), int); |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 296 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 297 | void dir_scan3(struct dir_info *root, struct dir_info *dir); |
| 298 | void dir_scan4(struct dir_info *dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 299 | void dir_scan5(struct dir_info *dir); |
| 300 | void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 301 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 302 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 303 | unsigned short checksum, unsigned short fragment_checksum, |
| 304 | int checksum_flag); |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 305 | long long generic_write_table(int, void *, int, void *, int); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 306 | void restorefs(); |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 307 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 308 | void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame^] | 309 | unsigned short get_checksum_mem(char *buff, int bytes); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 310 | |
| 311 | |
Phillip Lougher | 9b7c3c2 | 2013-03-10 02:29:42 +0000 | [diff] [blame] | 312 | void prep_exit() |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 313 | { |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 314 | if(restore_thread) { |
| 315 | if(pthread_self() == *restore_thread) { |
| 316 | /* |
| 317 | * Recursive failure when trying to restore filesystem! |
| 318 | * Nothing to do except to exit, otherwise we'll just |
| 319 | * appear to hang. The user should be able to restore |
| 320 | * from the recovery file (which is why it was added, in |
| 321 | * case of catastrophic failure in Mksquashfs) |
| 322 | */ |
| 323 | exit(1); |
| 324 | } else { |
| 325 | /* signal the restore thread to restore */ |
Phillip Lougher | 8c74081 | 2014-01-30 01:56:17 +0000 | [diff] [blame] | 326 | pthread_kill(*restore_thread, SIGUSR1); |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 327 | pthread_exit(NULL); |
| 328 | } |
Phillip Lougher | 9e07191 | 2013-04-02 05:30:29 +0100 | [diff] [blame] | 329 | } |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 330 | if(delete && destination_file && !block_device) |
| 331 | unlink(destination_file); |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 335 | int add_overflow(int a, int b) |
| 336 | { |
| 337 | return (INT_MAX - a) < b; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | int shift_overflow(int a, int shift) |
| 342 | { |
| 343 | return (INT_MAX >> shift) < a; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | int multiply_overflow(int a, int multiplier) |
| 348 | { |
| 349 | return (INT_MAX / multiplier) < a; |
| 350 | } |
| 351 | |
| 352 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 353 | #define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \ |
| 354 | + (((char *)A) - data_cache))) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 355 | |
| 356 | |
| 357 | void restorefs() |
| 358 | { |
| 359 | ERROR("Exiting - restoring original filesystem!\n\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 360 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 361 | bytes = sbytes; |
| 362 | memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 363 | memcpy(directory_data_cache, sdirectory_data_cache, |
| 364 | sdirectory_cache_bytes); |
| 365 | directory_cache_bytes = sdirectory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 366 | inode_bytes = sinode_bytes; |
| 367 | directory_bytes = sdirectory_bytes; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 368 | memcpy(directory_table + directory_bytes, sdirectory_compressed, |
| 369 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 370 | directory_bytes += sdirectory_compressed_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 371 | total_bytes = stotal_bytes; |
| 372 | total_inode_bytes = stotal_inode_bytes; |
| 373 | total_directory_bytes = stotal_directory_bytes; |
| 374 | inode_count = sinode_count; |
| 375 | file_count = sfile_count; |
| 376 | sym_count = ssym_count; |
| 377 | dev_count = sdev_count; |
| 378 | dir_count = sdir_count; |
| 379 | fifo_count = sfifo_count; |
| 380 | sock_count = ssock_count; |
| 381 | dup_files = sdup_files; |
| 382 | fragments = sfragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 383 | id_count = sid_count; |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 384 | restore_xattrs(); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 385 | write_filesystem_tables(&sBlk, nopad); |
| 386 | exit(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 390 | void sighandler() |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 391 | { |
| 392 | EXIT_MKSQUASHFS(); |
| 393 | } |
| 394 | |
| 395 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 396 | int mangle2(void *strm, char *d, char *s, int size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 397 | int block_size, int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 398 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 399 | int error, c_byte = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 400 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 401 | if(!uncompressed) { |
plougher | bfb876c | 2010-12-31 07:48:01 +0000 | [diff] [blame] | 402 | c_byte = compressor_compress(comp, strm, d, s, size, block_size, |
| 403 | &error); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 404 | if(c_byte == -1) |
| 405 | BAD_ERROR("mangle2:: %s compress failed with error " |
| 406 | "code %d\n", comp->name, error); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 407 | } |
| 408 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 409 | if(c_byte == 0 || c_byte >= size) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 410 | memcpy(d, s, size); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 411 | return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : |
| 412 | SQUASHFS_COMPRESSED_BIT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 413 | } |
| 414 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 415 | return c_byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 419 | int mangle(char *d, char *s, int size, int block_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 420 | int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 421 | { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 422 | return mangle2(stream, d, s, size, block_size, uncompressed, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 423 | data_block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 427 | void *get_inode(int req_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 428 | { |
| 429 | int data_space; |
| 430 | unsigned short c_byte; |
| 431 | |
| 432 | while(cache_bytes >= SQUASHFS_METADATA_SIZE) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 433 | if((inode_size - inode_bytes) < |
| 434 | ((SQUASHFS_METADATA_SIZE << 1)) + 2) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 435 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 436 | (SQUASHFS_METADATA_SIZE << 1) + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 437 | if(it == NULL) |
| 438 | MEM_ERROR(); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 439 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 440 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
| 441 | } |
| 442 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 443 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 444 | data_cache, SQUASHFS_METADATA_SIZE, |
| 445 | SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 446 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 447 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 448 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 449 | total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 450 | memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 451 | cache_bytes - SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 452 | cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 453 | } |
| 454 | |
| 455 | data_space = (cache_size - cache_bytes); |
| 456 | if(data_space < req_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 457 | int realloc_size = cache_size == 0 ? |
| 458 | ((req_size + SQUASHFS_METADATA_SIZE) & |
| 459 | ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - |
| 460 | data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 461 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 462 | void *dc = realloc(data_cache, cache_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 463 | realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 464 | if(dc == NULL) |
| 465 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 466 | cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 467 | data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | cache_bytes += req_size; |
| 471 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 472 | return data_cache + cache_bytes - req_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 476 | int read_bytes(int fd, void *buff, int bytes) |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 477 | { |
| 478 | int res, count; |
| 479 | |
| 480 | for(count = 0; count < bytes; count += res) { |
| 481 | res = read(fd, buff + count, bytes - count); |
| 482 | if(res < 1) { |
plougher | 96f85a1 | 2009-03-29 22:39:59 +0000 | [diff] [blame] | 483 | if(res == 0) |
| 484 | goto bytes_read; |
| 485 | else if(errno != EINTR) { |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 486 | ERROR("Read failed because %s\n", |
| 487 | strerror(errno)); |
| 488 | return -1; |
| 489 | } else |
| 490 | res = 0; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | bytes_read: |
| 495 | return count; |
| 496 | } |
| 497 | |
| 498 | |
plougher | 3306cb2 | 2010-06-18 04:22:44 +0000 | [diff] [blame] | 499 | int read_fs_bytes(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 500 | { |
| 501 | off_t off = byte; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 502 | int res = 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 503 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 504 | TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 505 | byte, bytes); |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 506 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 507 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 508 | pthread_mutex_lock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 509 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 510 | ERROR("read_fs_bytes: Lseek on destination failed because %s, " |
| 511 | "offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 512 | res = 0; |
Phillip Lougher | 041cf6d | 2013-07-20 04:20:40 +0100 | [diff] [blame] | 513 | } else if(read_bytes(fd, buff, bytes) < bytes) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 514 | ERROR("Read on destination failed\n"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 515 | res = 0; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 518 | pthread_cleanup_pop(1); |
| 519 | return res; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 523 | int write_bytes(int fd, void *buff, int bytes) |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 524 | { |
| 525 | int res, count; |
| 526 | |
| 527 | for(count = 0; count < bytes; count += res) { |
| 528 | res = write(fd, buff + count, bytes - count); |
| 529 | if(res == -1) { |
| 530 | if(errno != EINTR) { |
| 531 | ERROR("Write failed because %s\n", |
| 532 | strerror(errno)); |
| 533 | return -1; |
| 534 | } |
| 535 | res = 0; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 543 | void write_destination(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 544 | { |
| 545 | off_t off = byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 546 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 547 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
| 548 | pthread_mutex_lock(&pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 549 | |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 550 | if(lseek(fd, off, SEEK_SET) == -1) { |
| 551 | ERROR("write_destination: Lseek on destination " |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 552 | "failed because %s, offset=0x%llx\n", strerror(errno), |
| 553 | off); |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 554 | BAD_ERROR("Probably out of space on output %s\n", |
| 555 | block_device ? "block device" : "filesystem"); |
| 556 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 557 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 558 | if(write_bytes(fd, buff, bytes) == -1) |
Phillip Lougher | e8b536f | 2013-02-12 22:01:21 +0000 | [diff] [blame] | 559 | BAD_ERROR("Failed to write to output %s\n", |
| 560 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 561 | |
| 562 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
| 566 | long long write_inodes() |
| 567 | { |
| 568 | unsigned short c_byte; |
| 569 | int avail_bytes; |
| 570 | char *datap = data_cache; |
| 571 | long long start_bytes = bytes; |
| 572 | |
| 573 | while(cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 574 | if(inode_size - inode_bytes < |
| 575 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 576 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 577 | ((SQUASHFS_METADATA_SIZE << 1) + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 578 | if(it == NULL) |
| 579 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 580 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 581 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 582 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 583 | avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 584 | SQUASHFS_METADATA_SIZE : cache_bytes; |
| 585 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap, |
| 586 | avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 587 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 588 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 589 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 590 | total_inode_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 591 | datap += avail_bytes; |
| 592 | cache_bytes -= avail_bytes; |
| 593 | } |
| 594 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 595 | write_destination(fd, bytes, inode_bytes, inode_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 596 | bytes += inode_bytes; |
| 597 | |
| 598 | return start_bytes; |
| 599 | } |
| 600 | |
| 601 | |
| 602 | long long write_directories() |
| 603 | { |
| 604 | unsigned short c_byte; |
| 605 | int avail_bytes; |
| 606 | char *directoryp = directory_data_cache; |
| 607 | long long start_bytes = bytes; |
| 608 | |
| 609 | while(directory_cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 610 | if(directory_size - directory_bytes < |
| 611 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 612 | void *dt = realloc(directory_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 613 | directory_size + ((SQUASHFS_METADATA_SIZE << 1) |
| 614 | + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 615 | if(dt == NULL) |
| 616 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 617 | directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 618 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 619 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 620 | avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 621 | SQUASHFS_METADATA_SIZE : directory_cache_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 622 | c_byte = mangle(directory_table + directory_bytes + |
| 623 | BLOCK_OFFSET, directoryp, avail_bytes, |
| 624 | SQUASHFS_METADATA_SIZE, noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 625 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 626 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 627 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 628 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 629 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 630 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 631 | total_directory_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 632 | directoryp += avail_bytes; |
| 633 | directory_cache_bytes -= avail_bytes; |
| 634 | } |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 635 | write_destination(fd, bytes, directory_bytes, directory_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 636 | bytes += directory_bytes; |
| 637 | |
| 638 | return start_bytes; |
| 639 | } |
| 640 | |
| 641 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 642 | long long write_id_table() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 643 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 644 | unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 645 | unsigned int p[id_count]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 646 | int i; |
| 647 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 648 | TRACE("write_id_table: ids %d, id_bytes %d\n", id_count, id_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 649 | for(i = 0; i < id_count; i++) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 650 | TRACE("write_id_table: id index %d, id %d", i, id_table[i]->id); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 651 | SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 652 | } |
| 653 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 654 | return generic_write_table(id_bytes, p, 0, NULL, noI); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 658 | struct id *get_id(unsigned int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 659 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 660 | int hash = ID_HASH(id); |
| 661 | struct id *entry = id_hash_table[hash]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 662 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 663 | for(; entry; entry = entry->next) |
| 664 | if(entry->id == id) |
| 665 | break; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 666 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 667 | return entry; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 671 | struct id *create_id(unsigned int id) |
| 672 | { |
| 673 | int hash = ID_HASH(id); |
| 674 | struct id *entry = malloc(sizeof(struct id)); |
| 675 | if(entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 676 | MEM_ERROR(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 677 | entry->id = id; |
| 678 | entry->index = id_count ++; |
| 679 | entry->flags = 0; |
| 680 | entry->next = id_hash_table[hash]; |
| 681 | id_hash_table[hash] = entry; |
| 682 | id_table[entry->index] = entry; |
| 683 | return entry; |
| 684 | } |
| 685 | |
| 686 | |
| 687 | unsigned int get_uid(unsigned int uid) |
| 688 | { |
| 689 | struct id *entry = get_id(uid); |
| 690 | |
| 691 | if(entry == NULL) { |
| 692 | if(id_count == SQUASHFS_IDS) |
| 693 | BAD_ERROR("Out of uids!\n"); |
| 694 | entry = create_id(uid); |
| 695 | } |
| 696 | |
| 697 | if((entry->flags & ISA_UID) == 0) { |
| 698 | entry->flags |= ISA_UID; |
| 699 | uid_count ++; |
| 700 | } |
| 701 | |
| 702 | return entry->index; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | unsigned int get_guid(unsigned int guid) |
| 707 | { |
| 708 | struct id *entry = get_id(guid); |
| 709 | |
| 710 | if(entry == NULL) { |
| 711 | if(id_count == SQUASHFS_IDS) |
| 712 | BAD_ERROR("Out of gids!\n"); |
| 713 | entry = create_id(guid); |
| 714 | } |
| 715 | |
| 716 | if((entry->flags & ISA_GID) == 0) { |
| 717 | entry->flags |= ISA_GID; |
| 718 | guid_count ++; |
| 719 | } |
| 720 | |
| 721 | return entry->index; |
| 722 | } |
| 723 | |
| 724 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 725 | #define ALLOC_SIZE 128 |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 726 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 727 | char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size) |
| 728 | { |
| 729 | if(pathname == NULL) { |
| 730 | pathname = malloc(ALLOC_SIZE); |
| 731 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 732 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | for(;;) { |
| 736 | int res = snprintf(pathname, *size, "%s/%s", |
| 737 | dir_ent->our_dir->pathname, |
| 738 | dir_ent->source_name ? : dir_ent->name); |
| 739 | |
| 740 | if(res < 0) |
| 741 | BAD_ERROR("snprintf failed in pathname\n"); |
| 742 | else if(res >= *size) { |
| 743 | /* |
| 744 | * pathname is too small to contain the result, so |
| 745 | * increase it and try again |
| 746 | */ |
| 747 | *size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 748 | pathname = realloc(pathname, *size); |
| 749 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 750 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 751 | } else |
| 752 | break; |
| 753 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 754 | |
| 755 | return pathname; |
| 756 | } |
| 757 | |
| 758 | |
| 759 | char *pathname(struct dir_ent *dir_ent) |
| 760 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 761 | static char *pathname = NULL; |
| 762 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 763 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 764 | if (dir_ent->nonstandard_pathname) |
| 765 | return dir_ent->nonstandard_pathname; |
| 766 | |
| 767 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | |
| 771 | char *pathname_reader(struct dir_ent *dir_ent) |
| 772 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 773 | static char *pathname = NULL; |
| 774 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 775 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 776 | if (dir_ent->nonstandard_pathname) |
| 777 | return dir_ent->nonstandard_pathname; |
| 778 | |
| 779 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 783 | char *subpathname(struct dir_ent *dir_ent) |
| 784 | { |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 785 | static char *subpath = NULL; |
| 786 | static int size = ALLOC_SIZE; |
| 787 | int res; |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 788 | |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 789 | if(subpath == NULL) { |
| 790 | subpath = malloc(ALLOC_SIZE); |
| 791 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 792 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | for(;;) { |
| 796 | if(dir_ent->our_dir->subpath[0] != '\0') |
| 797 | res = snprintf(subpath, size, "%s/%s", |
| 798 | dir_ent->our_dir->subpath, dir_ent->name); |
| 799 | else |
| 800 | res = snprintf(subpath, size, "/%s", dir_ent->name); |
| 801 | |
| 802 | if(res < 0) |
| 803 | BAD_ERROR("snprintf failed in subpathname\n"); |
| 804 | else if(res >= size) { |
| 805 | /* |
| 806 | * subpath is too small to contain the result, so |
| 807 | * increase it and try again |
| 808 | */ |
| 809 | size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 810 | subpath = realloc(subpath, size); |
| 811 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 812 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 813 | } else |
| 814 | break; |
Phillip Lougher | 90e0825 | 2012-10-05 04:33:35 +0100 | [diff] [blame] | 815 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 816 | |
| 817 | return subpath; |
| 818 | } |
| 819 | |
| 820 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 821 | inline unsigned int get_inode_no(struct inode_info *inode) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 822 | { |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 823 | return inode->inode_number; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 827 | inline unsigned int get_parent_no(struct dir_info *dir) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 828 | { |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 829 | return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 833 | int create_inode(squashfs_inode *i_no, struct dir_info *dir_info, |
| 834 | struct dir_ent *dir_ent, int type, long long byte_size, |
| 835 | long long start_block, unsigned int offset, unsigned int *block_list, |
| 836 | struct fragment *fragment, struct directory *dir_in, long long sparse) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 837 | { |
| 838 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 8973a12 | 2010-12-31 09:52:45 +0000 | [diff] [blame] | 839 | union squashfs_inode_header inode_header; |
plougher | 857d0dd | 2010-12-31 21:03:13 +0000 | [diff] [blame] | 840 | struct squashfs_base_inode_header *base = &inode_header.base; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 841 | void *inode; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 842 | char *filename = pathname(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 843 | int nlink = dir_ent->inode->nlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 844 | int xattr = read_xattrs(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 845 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 846 | switch(type) { |
| 847 | case SQUASHFS_FILE_TYPE: |
| 848 | if(dir_ent->inode->nlink > 1 || |
| 849 | byte_size >= (1LL << 32) || |
| 850 | start_block >= (1LL << 32) || |
| 851 | sparse || IS_XATTR(xattr)) |
| 852 | type = SQUASHFS_LREG_TYPE; |
| 853 | break; |
| 854 | case SQUASHFS_DIR_TYPE: |
| 855 | if(dir_info->dir_is_ldir || IS_XATTR(xattr)) |
| 856 | type = SQUASHFS_LDIR_TYPE; |
| 857 | break; |
| 858 | case SQUASHFS_SYMLINK_TYPE: |
| 859 | if(IS_XATTR(xattr)) |
| 860 | type = SQUASHFS_LSYMLINK_TYPE; |
| 861 | break; |
| 862 | case SQUASHFS_BLKDEV_TYPE: |
| 863 | if(IS_XATTR(xattr)) |
| 864 | type = SQUASHFS_LBLKDEV_TYPE; |
| 865 | break; |
| 866 | case SQUASHFS_CHRDEV_TYPE: |
| 867 | if(IS_XATTR(xattr)) |
| 868 | type = SQUASHFS_LCHRDEV_TYPE; |
| 869 | break; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 870 | case SQUASHFS_FIFO_TYPE: |
| 871 | if(IS_XATTR(xattr)) |
| 872 | type = SQUASHFS_LFIFO_TYPE; |
| 873 | break; |
| 874 | case SQUASHFS_SOCKET_TYPE: |
| 875 | if(IS_XATTR(xattr)) |
| 876 | type = SQUASHFS_LSOCKET_TYPE; |
| 877 | break; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 878 | } |
| 879 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 880 | base->mode = SQUASHFS_MODE(buf->st_mode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 881 | base->uid = get_uid((unsigned int) global_uid == -1 ? |
| 882 | buf->st_uid : global_uid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 883 | base->inode_type = type; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 884 | base->guid = get_guid((unsigned int) global_gid == -1 ? |
| 885 | buf->st_gid : global_gid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 886 | base->mtime = buf->st_mtime; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 887 | base->inode_number = get_inode_no(dir_ent->inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 888 | |
| 889 | if(type == SQUASHFS_FILE_TYPE) { |
| 890 | int i; |
plougher | 8701ed6 | 2010-12-31 20:38:38 +0000 | [diff] [blame] | 891 | struct squashfs_reg_inode_header *reg = &inode_header.reg; |
| 892 | size_t off = offsetof(struct squashfs_reg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 893 | |
| 894 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 895 | reg->file_size = byte_size; |
| 896 | reg->start_block = start_block; |
| 897 | reg->fragment = fragment->index; |
| 898 | reg->offset = fragment->offset; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 899 | SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode); |
| 900 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 901 | TRACE("File inode, file_size %lld, start_block 0x%llx, blocks " |
| 902 | "%d, fragment %d, offset %d, size %d\n", byte_size, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 903 | start_block, offset, fragment->index, fragment->offset, |
| 904 | fragment->size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 905 | for(i = 0; i < offset; i++) |
| 906 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 907 | } |
| 908 | else if(type == SQUASHFS_LREG_TYPE) { |
| 909 | int i; |
plougher | 1e6ac4a | 2010-12-31 20:42:02 +0000 | [diff] [blame] | 910 | struct squashfs_lreg_inode_header *reg = &inode_header.lreg; |
| 911 | size_t off = offsetof(struct squashfs_lreg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 912 | |
| 913 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 914 | reg->nlink = nlink; |
| 915 | reg->file_size = byte_size; |
| 916 | reg->start_block = start_block; |
| 917 | reg->fragment = fragment->index; |
| 918 | reg->offset = fragment->offset; |
plougher | f5a674d | 2009-03-25 05:38:27 +0000 | [diff] [blame] | 919 | if(sparse && sparse >= byte_size) |
| 920 | sparse = byte_size - 1; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 921 | reg->sparse = sparse; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 922 | reg->xattr = xattr; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 923 | SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode); |
| 924 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 925 | TRACE("Long file inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 926 | "blocks %d, fragment %d, offset %d, size %d, nlink %d" |
| 927 | "\n", byte_size, start_block, offset, fragment->index, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 928 | fragment->offset, fragment->size, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 929 | for(i = 0; i < offset; i++) |
| 930 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 931 | } |
| 932 | else if(type == SQUASHFS_LDIR_TYPE) { |
| 933 | int i; |
| 934 | unsigned char *p; |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 935 | struct squashfs_ldir_inode_header *dir = &inode_header.ldir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 936 | struct cached_dir_index *index = dir_in->index; |
| 937 | unsigned int i_count = dir_in->i_count; |
| 938 | unsigned int i_size = dir_in->i_size; |
| 939 | |
| 940 | if(byte_size >= 1 << 27) |
| 941 | BAD_ERROR("directory greater than 2^27-1 bytes!\n"); |
| 942 | |
| 943 | inode = get_inode(sizeof(*dir) + i_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 944 | dir->inode_type = SQUASHFS_LDIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 945 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 946 | dir->file_size = byte_size; |
| 947 | dir->offset = offset; |
| 948 | dir->start_block = start_block; |
| 949 | dir->i_count = i_count; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 950 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 951 | dir->xattr = xattr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 952 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 953 | SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode); |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 954 | p = inode + offsetof(struct squashfs_ldir_inode_header, index); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 955 | for(i = 0; i < i_count; i++) { |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 956 | SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p); |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 957 | p += offsetof(struct squashfs_dir_index, name); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 958 | memcpy(p, index[i].name, index[i].index.size + 1); |
| 959 | p += index[i].index.size + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 960 | } |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 961 | TRACE("Long directory inode, file_size %lld, start_block " |
| 962 | "0x%llx, offset 0x%x, nlink %d\n", byte_size, |
| 963 | start_block, offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 964 | } |
| 965 | else if(type == SQUASHFS_DIR_TYPE) { |
plougher | 9d80a60 | 2010-12-31 20:47:24 +0000 | [diff] [blame] | 966 | struct squashfs_dir_inode_header *dir = &inode_header.dir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 967 | |
| 968 | inode = get_inode(sizeof(*dir)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 969 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 970 | dir->file_size = byte_size; |
| 971 | dir->offset = offset; |
| 972 | dir->start_block = start_block; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 973 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 974 | SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 975 | TRACE("Directory inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 976 | "offset 0x%x, nlink %d\n", byte_size, start_block, |
| 977 | offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 978 | } |
| 979 | else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) { |
plougher | c70c633 | 2010-12-31 20:11:09 +0000 | [diff] [blame] | 980 | struct squashfs_dev_inode_header *dev = &inode_header.dev; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 981 | unsigned int major = major(buf->st_rdev); |
| 982 | unsigned int minor = minor(buf->st_rdev); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 983 | |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 984 | if(major > 0xfff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 985 | ERROR("Major %d out of range in device node %s, " |
| 986 | "truncating to %d\n", major, filename, |
| 987 | major & 0xfff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 988 | major &= 0xfff; |
| 989 | } |
| 990 | if(minor > 0xfffff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 991 | ERROR("Minor %d out of range in device node %s, " |
| 992 | "truncating to %d\n", minor, filename, |
| 993 | minor & 0xfffff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 994 | minor &= 0xfffff; |
| 995 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 996 | inode = get_inode(sizeof(*dev)); |
| 997 | dev->nlink = nlink; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 998 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 999 | ((minor & ~0xff) << 12); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1000 | SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1001 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1002 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1003 | else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) { |
plougher | 0b4ee5b | 2010-12-31 20:14:00 +0000 | [diff] [blame] | 1004 | struct squashfs_ldev_inode_header *dev = &inode_header.ldev; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1005 | unsigned int major = major(buf->st_rdev); |
| 1006 | unsigned int minor = minor(buf->st_rdev); |
| 1007 | |
| 1008 | if(major > 0xfff) { |
| 1009 | ERROR("Major %d out of range in device node %s, " |
| 1010 | "truncating to %d\n", major, filename, |
| 1011 | major & 0xfff); |
| 1012 | major &= 0xfff; |
| 1013 | } |
| 1014 | if(minor > 0xfffff) { |
| 1015 | ERROR("Minor %d out of range in device node %s, " |
| 1016 | "truncating to %d\n", minor, filename, |
| 1017 | minor & 0xfffff); |
| 1018 | minor &= 0xfffff; |
| 1019 | } |
| 1020 | inode = get_inode(sizeof(*dev)); |
| 1021 | dev->nlink = nlink; |
| 1022 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1023 | ((minor & ~0xff) << 12); |
| 1024 | dev->xattr = xattr; |
| 1025 | SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode); |
| 1026 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
| 1027 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1028 | else if(type == SQUASHFS_SYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1029 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1030 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1031 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1032 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1033 | |
plougher | 5cf38b8 | 2010-12-16 04:55:32 +0000 | [diff] [blame] | 1034 | byte = readlink(filename, buff, 65536); |
| 1035 | if(byte == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1036 | ERROR_START("Failed to read symlink %s", filename); |
| 1037 | ERROR_EXIT(", creating empty symlink\n"); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1038 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | if(byte == 65536) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1042 | ERROR_START("Symlink %s is greater than 65536 bytes!", |
| 1043 | filename); |
| 1044 | ERROR_EXIT(" Creating empty symlink\n"); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1045 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | inode = get_inode(sizeof(*symlink) + byte); |
| 1049 | symlink->nlink = nlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1050 | symlink->symlink_size = byte; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1051 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1052 | strncpy(inode + off, buff, byte); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1053 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1054 | nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1055 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1056 | else if(type == SQUASHFS_LSYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1057 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1058 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1059 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1060 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1061 | |
plougher | df2b9aa | 2010-12-16 04:56:23 +0000 | [diff] [blame] | 1062 | byte = readlink(filename, buff, 65536); |
| 1063 | if(byte == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1064 | ERROR_START("Failed to read symlink %s", filename); |
| 1065 | ERROR_EXIT(", creating empty symlink\n"); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1066 | byte = 0; |
| 1067 | } |
| 1068 | |
| 1069 | if(byte == 65536) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1070 | ERROR_START("Symlink %s is greater than 65536 bytes!", |
| 1071 | filename); |
| 1072 | ERROR_EXIT(" Creating empty symlink\n"); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1073 | byte = 0; |
| 1074 | } |
| 1075 | |
| 1076 | inode = get_inode(sizeof(*symlink) + byte + |
| 1077 | sizeof(unsigned int)); |
| 1078 | symlink->nlink = nlink; |
| 1079 | symlink->symlink_size = byte; |
| 1080 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1081 | strncpy(inode + off, buff, byte); |
| 1082 | SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1); |
| 1083 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1084 | nlink); |
| 1085 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1086 | else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) { |
plougher | e56b986 | 2010-12-31 10:58:35 +0000 | [diff] [blame] | 1087 | struct squashfs_ipc_inode_header *ipc = &inode_header.ipc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1088 | |
| 1089 | inode = get_inode(sizeof(*ipc)); |
| 1090 | ipc->nlink = nlink; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1091 | SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1092 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1093 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1094 | } |
| 1095 | else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) { |
plougher | aa0d122 | 2010-12-31 20:06:24 +0000 | [diff] [blame] | 1096 | struct squashfs_lipc_inode_header *ipc = &inode_header.lipc; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1097 | |
| 1098 | inode = get_inode(sizeof(*ipc)); |
| 1099 | ipc->nlink = nlink; |
| 1100 | ipc->xattr = xattr; |
| 1101 | SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode); |
| 1102 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1103 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1104 | } else |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1105 | BAD_ERROR("Unrecognised inode %d in create_inode\n", type); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1106 | |
| 1107 | *i_no = MKINODE(inode); |
| 1108 | inode_count ++; |
| 1109 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1110 | TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type, |
| 1111 | base->uid, base->guid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1112 | |
| 1113 | return TRUE; |
| 1114 | } |
| 1115 | |
| 1116 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1117 | void add_dir(squashfs_inode inode, unsigned int inode_number, char *name, |
| 1118 | int type, struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1119 | { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 1120 | unsigned char *buff; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1121 | struct squashfs_dir_entry idir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1122 | unsigned int start_block = inode >> 16; |
| 1123 | unsigned int offset = inode & 0xffff; |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1124 | unsigned int size = strlen(name); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1125 | size_t name_off = offsetof(struct squashfs_dir_entry, name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1126 | |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1127 | if(size > SQUASHFS_NAME_LEN) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1128 | size = SQUASHFS_NAME_LEN; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1129 | ERROR("Filename is greater than %d characters, truncating! ..." |
| 1130 | "\n", SQUASHFS_NAME_LEN); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1133 | if(dir->p + sizeof(struct squashfs_dir_entry) + size + |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1134 | sizeof(struct squashfs_dir_header) |
| 1135 | >= dir->buff + dir->size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1136 | buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1137 | if(buff == NULL) |
| 1138 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1139 | |
| 1140 | dir->p = (dir->p - dir->buff) + buff; |
| 1141 | if(dir->entry_count_p) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1142 | dir->entry_count_p = (dir->entry_count_p - dir->buff + |
| 1143 | buff); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1144 | dir->index_count_p = dir->index_count_p - dir->buff + buff; |
| 1145 | dir->buff = buff; |
| 1146 | } |
| 1147 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1148 | if(dir->entry_count == 256 || start_block != dir->start_block || |
| 1149 | ((dir->entry_count_p != NULL) && |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1150 | ((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1151 | dir->index_count_p) > SQUASHFS_METADATA_SIZE)) || |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1152 | ((long long) inode_number - dir->inode_number) > 32767 |
| 1153 | || ((long long) inode_number - dir->inode_number) |
| 1154 | < -32768) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1155 | if(dir->entry_count_p) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1156 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1157 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1158 | if((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1159 | dir->index_count_p) > |
| 1160 | SQUASHFS_METADATA_SIZE) { |
| 1161 | if(dir->i_count % I_COUNT_SIZE == 0) { |
| 1162 | dir->index = realloc(dir->index, |
| 1163 | (dir->i_count + I_COUNT_SIZE) * |
| 1164 | sizeof(struct cached_dir_index)); |
| 1165 | if(dir->index == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1166 | MEM_ERROR(); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1167 | } |
| 1168 | dir->index[dir->i_count].index.index = |
| 1169 | dir->p - dir->buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1170 | dir->index[dir->i_count].index.size = size - 1; |
| 1171 | dir->index[dir->i_count++].name = name; |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1172 | dir->i_size += sizeof(struct squashfs_dir_index) |
| 1173 | + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1174 | dir->index_count_p = dir->p; |
| 1175 | } |
| 1176 | |
| 1177 | dir_header.count = dir->entry_count - 1; |
| 1178 | dir_header.start_block = dir->start_block; |
| 1179 | dir_header.inode_number = dir->inode_number; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1180 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1181 | dir->entry_count_p); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1182 | |
| 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | dir->entry_count_p = dir->p; |
| 1187 | dir->start_block = start_block; |
| 1188 | dir->entry_count = 0; |
| 1189 | dir->inode_number = inode_number; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1190 | dir->p += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1193 | idir.offset = offset; |
| 1194 | idir.type = type; |
| 1195 | idir.size = size - 1; |
| 1196 | idir.inode_number = ((long long) inode_number - dir->inode_number); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1197 | SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p); |
| 1198 | strncpy((char *) dir->p + name_off, name, size); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1199 | dir->p += sizeof(struct squashfs_dir_entry) + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1200 | dir->entry_count ++; |
| 1201 | } |
| 1202 | |
| 1203 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1204 | void write_dir(squashfs_inode *inode, struct dir_info *dir_info, |
| 1205 | struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1206 | { |
| 1207 | unsigned int dir_size = dir->p - dir->buff; |
plougher | 4627ca3 | 2010-12-16 05:03:55 +0000 | [diff] [blame] | 1208 | int data_space = directory_cache_size - directory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1209 | unsigned int directory_block, directory_offset, i_count, index; |
| 1210 | unsigned short c_byte; |
| 1211 | |
| 1212 | if(data_space < dir_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1213 | int realloc_size = directory_cache_size == 0 ? |
| 1214 | ((dir_size + SQUASHFS_METADATA_SIZE) & |
| 1215 | ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1216 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1217 | void *dc = realloc(directory_data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1218 | directory_cache_size + realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1219 | if(dc == NULL) |
| 1220 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1221 | directory_cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1222 | directory_data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | if(dir_size) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1226 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1227 | |
| 1228 | dir_header.count = dir->entry_count - 1; |
| 1229 | dir_header.start_block = dir->start_block; |
| 1230 | dir_header.inode_number = dir->inode_number; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1231 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1232 | memcpy(directory_data_cache + directory_cache_bytes, dir->buff, |
| 1233 | dir_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1234 | } |
| 1235 | directory_offset = directory_cache_bytes; |
| 1236 | directory_block = directory_bytes; |
| 1237 | directory_cache_bytes += dir_size; |
| 1238 | i_count = 0; |
| 1239 | index = SQUASHFS_METADATA_SIZE - directory_offset; |
| 1240 | |
| 1241 | while(1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1242 | while(i_count < dir->i_count && |
| 1243 | dir->index[i_count].index.index < index) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1244 | dir->index[i_count++].index.start_block = |
| 1245 | directory_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1246 | index += SQUASHFS_METADATA_SIZE; |
| 1247 | |
| 1248 | if(directory_cache_bytes < SQUASHFS_METADATA_SIZE) |
| 1249 | break; |
| 1250 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1251 | if((directory_size - directory_bytes) < |
| 1252 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1253 | void *dt = realloc(directory_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1254 | directory_size + (SQUASHFS_METADATA_SIZE << 1) |
| 1255 | + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1256 | if(dt == NULL) |
| 1257 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1258 | directory_size += SQUASHFS_METADATA_SIZE << 1; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1259 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1262 | c_byte = mangle(directory_table + directory_bytes + |
| 1263 | BLOCK_OFFSET, directory_data_cache, |
| 1264 | SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, |
| 1265 | noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1266 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1267 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1268 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1269 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1270 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1271 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1272 | total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 1273 | memmove(directory_data_cache, directory_data_cache + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1274 | SQUASHFS_METADATA_SIZE, directory_cache_bytes - |
| 1275 | SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1276 | directory_cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 1277 | } |
| 1278 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1279 | create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE, |
| 1280 | dir_size + 3, directory_block, directory_offset, NULL, NULL, |
| 1281 | dir, 0); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1282 | |
| 1283 | #ifdef SQUASHFS_TRACE |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1284 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1285 | unsigned char *dirp; |
| 1286 | int count; |
| 1287 | |
| 1288 | TRACE("Directory contents of inode 0x%llx\n", *inode); |
| 1289 | dirp = dir->buff; |
| 1290 | while(dirp < dir->p) { |
| 1291 | char buffer[SQUASHFS_NAME_LEN + 1]; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1292 | struct squashfs_dir_entry idir, *idirp; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1293 | struct squashfs_dir_header dirh; |
| 1294 | SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1295 | &dirh); |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1296 | count = dirh.count + 1; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1297 | dirp += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1298 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1299 | TRACE("\tStart block 0x%x, count %d\n", |
| 1300 | dirh.start_block, count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1301 | |
| 1302 | while(count--) { |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1303 | idirp = (struct squashfs_dir_entry *) dirp; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1304 | SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1305 | strncpy(buffer, idirp->name, idir.size + 1); |
| 1306 | buffer[idir.size + 1] = '\0'; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1307 | TRACE("\t\tname %s, inode offset 0x%x, type " |
| 1308 | "%d\n", buffer, idir.offset, idir.type); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1309 | dirp += sizeof(struct squashfs_dir_entry) + idir.size + |
plougher | e8b26f6 | 2010-12-16 05:06:00 +0000 | [diff] [blame] | 1310 | 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | #endif |
| 1315 | dir_count ++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1319 | struct file_buffer *get_fragment(struct fragment *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1320 | { |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1321 | struct squashfs_fragment_entry *disk_fragment; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1322 | int res, size; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1323 | long long start_block; |
| 1324 | struct file_buffer *buffer, *compressed_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1325 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1326 | if(fragment->index == SQUASHFS_INVALID_FRAG) |
| 1327 | return NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1328 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1329 | buffer = cache_lookup(fragment_buffer, fragment->index); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1330 | if(buffer) |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1331 | return buffer; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1332 | |
Phillip Lougher | 4570201 | 2013-04-30 05:19:54 +0100 | [diff] [blame] | 1333 | compressed_buffer = cache_lookup(writer_buffer, |
Phillip Lougher | fd99272 | 2013-05-19 04:46:57 +0100 | [diff] [blame] | 1334 | FRAG_INDEX((long long) fragment->index)); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1335 | |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 1336 | buffer = cache_get(fragment_buffer, fragment->index); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1337 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1338 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1339 | pthread_mutex_lock(&fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1340 | disk_fragment = &fragment_table[fragment->index]; |
| 1341 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1342 | start_block = disk_fragment->start_block; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1343 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1344 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1345 | if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1346 | int error; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1347 | char *data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1348 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1349 | if(compressed_buffer) |
| 1350 | data = compressed_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1351 | else { |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 1352 | data = read_from_disk(start_block, size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1353 | if(data == NULL) { |
| 1354 | ERROR("Failed to read fragment from output" |
| 1355 | " filesystem\n"); |
| 1356 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1357 | } |
| 1358 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1359 | |
plougher | b48442b | 2010-12-31 07:57:54 +0000 | [diff] [blame] | 1360 | res = compressor_uncompress(comp, buffer->data, data, size, |
| 1361 | block_size, &error); |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 1362 | if(res == -1) |
| 1363 | BAD_ERROR("%s uncompress failed with error code %d\n", |
| 1364 | comp->name, error); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1365 | } else if(compressed_buffer) |
| 1366 | memcpy(buffer->data, compressed_buffer->data, size); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1367 | else { |
| 1368 | res = read_fs_bytes(fd, start_block, size, buffer->data); |
Phillip Lougher | 9451938 | 2013-03-06 02:43:42 +0000 | [diff] [blame] | 1369 | if(res == 0) { |
| 1370 | ERROR("Failed to read fragment from output " |
| 1371 | "filesystem\n"); |
| 1372 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1373 | } |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1374 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1375 | |
plougher | 03859fa | 2009-03-31 03:18:18 +0000 | [diff] [blame] | 1376 | cache_block_put(compressed_buffer); |
| 1377 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1378 | return buffer; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1381 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame^] | 1382 | void get_fragment_checksum(struct file_info *file) |
| 1383 | { |
| 1384 | struct file_buffer *frag_buffer = get_fragment(file->fragment); |
| 1385 | struct append_file *append; |
| 1386 | int index = file->fragment->index; |
| 1387 | int offset = file->fragment->offset; |
| 1388 | int size = file->fragment->size; |
| 1389 | |
| 1390 | if(frag_buffer == NULL) { |
| 1391 | file->have_frag_checksum = TRUE; |
| 1392 | return; |
| 1393 | } |
| 1394 | |
| 1395 | if(index >= append_fragments) { |
| 1396 | /* |
| 1397 | * for fragment blocks generated by this mksquashfs run |
| 1398 | * just compute the checksum for this fragment. |
| 1399 | * Note this applies to file tail-end fragments only (if |
| 1400 | * the option -always-use-fragments specified). Fragments |
| 1401 | * for files smaller than the block size, always get their |
| 1402 | * checksums computed at generation time |
| 1403 | */ |
| 1404 | file->fragment_checksum = get_checksum_mem(frag_buffer->data + |
| 1405 | offset, size); |
| 1406 | cache_block_put(frag_buffer); |
| 1407 | file->have_frag_checksum = TRUE; |
| 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | for(append = file_mapping[index]; append; append = append->next) { |
| 1412 | int offset = append->file->fragment->offset; |
| 1413 | int size = append->file->fragment->size; |
| 1414 | |
| 1415 | append->file->fragment_checksum = |
| 1416 | get_checksum_mem(frag_buffer->data + offset, size); |
| 1417 | append->file->have_frag_checksum = TRUE; |
| 1418 | } |
| 1419 | |
| 1420 | cache_block_put(frag_buffer); |
| 1421 | } |
| 1422 | |
| 1423 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1424 | int lock_fragments() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1425 | { |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1426 | int count; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1427 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1428 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1429 | fragments_locked = TRUE; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1430 | count = fragments_outstanding; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1431 | pthread_cleanup_pop(1); |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1432 | return count; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | void unlock_fragments() |
| 1437 | { |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1438 | int frg, size; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1439 | struct file_buffer *write_buffer; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1440 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1441 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1442 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | 71ad964 | 2013-05-25 05:03:48 +0100 | [diff] [blame] | 1443 | |
| 1444 | /* |
| 1445 | * Note queue_empty() is inherently racy with respect to concurrent |
| 1446 | * queue get and pushes. We avoid this because we're holding the |
| 1447 | * fragment_mutex which ensures no other threads can be using the |
| 1448 | * queue at this time. |
| 1449 | */ |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1450 | while(!queue_empty(locked_fragment)) { |
| 1451 | write_buffer = queue_get(locked_fragment); |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1452 | frg = write_buffer->block; |
| 1453 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size); |
| 1454 | fragment_table[frg].start_block = bytes; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1455 | write_buffer->block = bytes; |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1456 | bytes += size; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1457 | fragments_outstanding --; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1458 | queue_put(to_writer, write_buffer); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1459 | TRACE("fragment_locked writing fragment %d, compressed size %d" |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1460 | "\n", frg, size); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1461 | } |
| 1462 | fragments_locked = FALSE; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1463 | pthread_cleanup_pop(1); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1466 | /* Called with the fragment_mutex locked */ |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1467 | void add_pending_fragment(struct file_buffer *write_buffer, int c_byte, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1468 | int fragment) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1469 | { |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1470 | fragment_table[fragment].size = c_byte; |
| 1471 | write_buffer->block = fragment; |
| 1472 | |
| 1473 | queue_put(locked_fragment, write_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1477 | void write_fragment(struct file_buffer *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1478 | { |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1479 | if(fragment == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1480 | return; |
| 1481 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1482 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1483 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1484 | fragment_table[fragment->block].unused = 0; |
| 1485 | fragments_outstanding ++; |
| 1486 | queue_put(to_frag, fragment); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1487 | pthread_cleanup_pop(1); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | struct file_buffer *allocate_fragment() |
| 1492 | { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 1493 | struct file_buffer *fragment = cache_get(fragment_buffer, fragments); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1494 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1495 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1496 | pthread_mutex_lock(&fragment_mutex); |
| 1497 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1498 | if(fragments % FRAG_SIZE == 0) { |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1499 | void *ft = realloc(fragment_table, (fragments + |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1500 | FRAG_SIZE) * sizeof(struct squashfs_fragment_entry)); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 1501 | if(ft == NULL) |
| 1502 | MEM_ERROR(); |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1503 | fragment_table = ft; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1504 | } |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1505 | |
| 1506 | fragment->size = 0; |
| 1507 | fragment->block = fragments ++; |
| 1508 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1509 | pthread_cleanup_pop(1); |
| 1510 | |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1511 | return fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 1514 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1515 | static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0}; |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 1516 | |
| 1517 | |
| 1518 | void free_fragment(struct fragment *fragment) |
| 1519 | { |
| 1520 | if(fragment != &empty_fragment) |
| 1521 | free(fragment); |
| 1522 | } |
| 1523 | |
| 1524 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1525 | struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer, |
| 1526 | struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1527 | { |
| 1528 | struct fragment *ffrg; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1529 | struct file_buffer **fragment; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1530 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1531 | if(file_buffer == NULL || file_buffer->size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1532 | return &empty_fragment; |
| 1533 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1534 | fragment = eval_frag_actions(dir_ent); |
| 1535 | |
| 1536 | if((*fragment) && (*fragment)->size + file_buffer->size > block_size) { |
| 1537 | write_fragment(*fragment); |
| 1538 | *fragment = NULL; |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1539 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1540 | |
plougher | e7e6e83 | 2010-12-16 05:08:06 +0000 | [diff] [blame] | 1541 | ffrg = malloc(sizeof(struct fragment)); |
| 1542 | if(ffrg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1543 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1544 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1545 | if(*fragment == NULL) |
| 1546 | *fragment = allocate_fragment(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1547 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1548 | ffrg->index = (*fragment)->block; |
| 1549 | ffrg->offset = (*fragment)->size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1550 | ffrg->size = file_buffer->size; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1551 | memcpy((*fragment)->data + (*fragment)->size, file_buffer->data, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1552 | file_buffer->size); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1553 | (*fragment)->size += file_buffer->size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1554 | |
| 1555 | return ffrg; |
| 1556 | } |
| 1557 | |
| 1558 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1559 | long long generic_write_table(int length, void *buffer, int length2, |
| 1560 | void *buffer2, int uncompressed) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1561 | { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1562 | int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) / |
| 1563 | SQUASHFS_METADATA_SIZE; |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1564 | long long *list, start_bytes; |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1565 | int compressed_size, i, list_size = meta_blocks * sizeof(long long); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1566 | unsigned short c_byte; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1567 | char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2]; |
| 1568 | |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1569 | #ifdef SQUASHFS_TRACE |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1570 | long long obytes = bytes; |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1571 | int olength = length; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1572 | #endif |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1573 | |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1574 | list = malloc(list_size); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1575 | if(list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1576 | MEM_ERROR(); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1577 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1578 | for(i = 0; i < meta_blocks; i++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1579 | int avail_bytes = length > SQUASHFS_METADATA_SIZE ? |
| 1580 | SQUASHFS_METADATA_SIZE : length; |
| 1581 | c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i * |
| 1582 | SQUASHFS_METADATA_SIZE , avail_bytes, |
| 1583 | SQUASHFS_METADATA_SIZE, uncompressed, 0); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1584 | SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1585 | list[i] = bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1586 | compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1587 | BLOCK_OFFSET; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1588 | TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes, |
| 1589 | compressed_size); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1590 | write_destination(fd, bytes, compressed_size, cbuffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1591 | bytes += compressed_size; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1592 | total_bytes += avail_bytes; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1593 | length -= avail_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1596 | start_bytes = bytes; |
| 1597 | if(length2) { |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1598 | write_destination(fd, bytes, length2, buffer2); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1599 | bytes += length2; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1600 | total_bytes += length2; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1603 | SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks); |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1604 | write_destination(fd, bytes, list_size, list); |
| 1605 | bytes += list_size; |
| 1606 | total_bytes += list_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1607 | |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1608 | TRACE("generic_write_table: total uncompressed %d compressed %lld\n", |
| 1609 | olength, bytes - obytes); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1610 | |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1611 | free(list); |
| 1612 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1613 | return start_bytes; |
| 1614 | } |
| 1615 | |
| 1616 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1617 | long long write_fragment_table() |
| 1618 | { |
| 1619 | unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1620 | int i; |
| 1621 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1622 | TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments, |
| 1623 | frag_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1624 | for(i = 0; i < fragments; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1625 | TRACE("write_fragment_table: fragment %d, start_block 0x%llx, " |
| 1626 | "size %d\n", i, fragment_table[i].start_block, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1627 | fragment_table[i].size); |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1628 | SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1631 | return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1635 | char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1636 | char *read_from_disk(long long start, unsigned int avail_bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1637 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1638 | int res; |
| 1639 | |
| 1640 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1641 | if(res == 0) |
| 1642 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1643 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1644 | return read_from_file_buffer; |
| 1645 | } |
| 1646 | |
| 1647 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1648 | char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE]; |
| 1649 | char *read_from_disk2(long long start, unsigned int avail_bytes) |
| 1650 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1651 | int res; |
| 1652 | |
| 1653 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1654 | if(res == 0) |
| 1655 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1656 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1657 | return read_from_file_buffer2; |
| 1658 | } |
| 1659 | |
| 1660 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1661 | /* |
| 1662 | * Compute 16 bit BSD checksum over the data |
| 1663 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1664 | unsigned short get_checksum(char *buff, int bytes, unsigned short chksum) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1665 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1666 | unsigned char *b = (unsigned char *) buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1667 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1668 | while(bytes --) { |
| 1669 | chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1; |
| 1670 | chksum += *b++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | return chksum; |
| 1674 | } |
| 1675 | |
| 1676 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1677 | unsigned short get_checksum_disk(long long start, long long l, |
| 1678 | unsigned int *blocks) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1679 | { |
| 1680 | unsigned short chksum = 0; |
| 1681 | unsigned int bytes; |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1682 | struct file_buffer *write_buffer; |
| 1683 | int i; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1684 | |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1685 | for(i = 0; l; i++) { |
| 1686 | bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]); |
| 1687 | if(bytes == 0) /* sparse block */ |
| 1688 | continue; |
| 1689 | write_buffer = cache_lookup(writer_buffer, start); |
| 1690 | if(write_buffer) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1691 | chksum = get_checksum(write_buffer->data, bytes, |
| 1692 | chksum); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1693 | cache_block_put(write_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1694 | } else { |
| 1695 | void *data = read_from_disk(start, bytes); |
| 1696 | if(data == NULL) { |
| 1697 | ERROR("Failed to checksum data from output" |
| 1698 | " filesystem\n"); |
| 1699 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1700 | } |
| 1701 | |
| 1702 | chksum = get_checksum(data, bytes, chksum); |
| 1703 | } |
| 1704 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1705 | l -= bytes; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1706 | start += bytes; |
| 1707 | } |
| 1708 | |
| 1709 | return chksum; |
| 1710 | } |
| 1711 | |
| 1712 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1713 | unsigned short get_checksum_mem(char *buff, int bytes) |
| 1714 | { |
| 1715 | return get_checksum(buff, bytes, 0); |
| 1716 | } |
| 1717 | |
| 1718 | |
| 1719 | unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer) |
| 1720 | { |
| 1721 | if(file_buffer == NULL) |
| 1722 | return 0; |
| 1723 | else |
| 1724 | return get_checksum(file_buffer->data, file_buffer->size, 0); |
| 1725 | } |
| 1726 | |
| 1727 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1728 | #define DUP_HASH(a) (a & 0xffff) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1729 | void add_file(long long start, long long file_size, long long file_bytes, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1730 | unsigned int *block_listp, int blocks, unsigned int fragment, |
| 1731 | int offset, int bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1732 | { |
| 1733 | struct fragment *frg; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1734 | unsigned int *block_list = block_listp; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1735 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame^] | 1736 | struct append_file *append_file; |
| 1737 | struct file_info *file; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1738 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1739 | if(!duplicate_checking || file_size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1740 | return; |
| 1741 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1742 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) { |
| 1743 | if(file_size != dupl_ptr->file_size) |
| 1744 | continue; |
| 1745 | if(blocks != 0 && start != dupl_ptr->start) |
| 1746 | continue; |
| 1747 | if(fragment != dupl_ptr->fragment->index) |
| 1748 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1749 | if(fragment != SQUASHFS_INVALID_FRAG && (offset != |
| 1750 | dupl_ptr->fragment->offset || bytes != |
| 1751 | dupl_ptr->fragment->size)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1752 | continue; |
| 1753 | return; |
| 1754 | } |
| 1755 | |
plougher | 3bb279c | 2010-12-16 05:10:03 +0000 | [diff] [blame] | 1756 | frg = malloc(sizeof(struct fragment)); |
| 1757 | if(frg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1758 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1759 | |
| 1760 | frg->index = fragment; |
| 1761 | frg->offset = offset; |
| 1762 | frg->size = bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1763 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame^] | 1764 | file = add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0, |
| 1765 | FALSE); |
| 1766 | |
| 1767 | if(fragment == SQUASHFS_INVALID_FRAG) |
| 1768 | return; |
| 1769 | |
| 1770 | append_file = malloc(sizeof(struct append_file)); |
| 1771 | if(append_file == NULL) |
| 1772 | MEM_ERROR(); |
| 1773 | |
| 1774 | append_file->file = file; |
| 1775 | append_file->next = file_mapping[fragment]; |
| 1776 | file_mapping[fragment] = append_file; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1777 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1778 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1779 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1780 | int pre_duplicate(long long file_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1781 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1782 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1783 | |
| 1784 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1785 | if(dupl_ptr->file_size == file_size) |
| 1786 | return TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1787 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1788 | return FALSE; |
| 1789 | } |
| 1790 | |
| 1791 | |
| 1792 | int pre_duplicate_frag(long long file_size, unsigned short checksum) |
| 1793 | { |
| 1794 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 1795 | |
| 1796 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1797 | if(file_size == dupl_ptr->file_size && file_size == |
| 1798 | dupl_ptr->fragment->size) { |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame^] | 1799 | if(!dupl_ptr->have_frag_checksum) |
| 1800 | get_fragment_checksum(dupl_ptr); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1801 | if(dupl_ptr->fragment_checksum == checksum) |
| 1802 | return TRUE; |
| 1803 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1804 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1805 | return FALSE; |
| 1806 | } |
| 1807 | |
| 1808 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1809 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 1810 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 1811 | unsigned short checksum, unsigned short fragment_checksum, |
| 1812 | int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1813 | { |
plougher | 51ef9ae | 2010-12-16 05:14:28 +0000 | [diff] [blame] | 1814 | struct file_info *dupl_ptr = malloc(sizeof(struct file_info)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1815 | |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1816 | if(dupl_ptr == NULL) |
| 1817 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1818 | |
| 1819 | dupl_ptr->file_size = file_size; |
| 1820 | dupl_ptr->bytes = bytes; |
| 1821 | dupl_ptr->block_list = block_list; |
| 1822 | dupl_ptr->start = start; |
| 1823 | dupl_ptr->fragment = fragment; |
| 1824 | dupl_ptr->checksum = checksum; |
| 1825 | dupl_ptr->fragment_checksum = fragment_checksum; |
Phillip Lougher | 557cefa | 2014-02-05 04:36:05 +0000 | [diff] [blame] | 1826 | dupl_ptr->have_frag_checksum = checksum_flag; |
| 1827 | dupl_ptr->have_checksum = checksum_flag; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1828 | dupl_ptr->next = dupl[DUP_HASH(file_size)]; |
| 1829 | dupl[DUP_HASH(file_size)] = dupl_ptr; |
| 1830 | dup_files ++; |
| 1831 | |
| 1832 | return dupl_ptr; |
| 1833 | } |
| 1834 | |
| 1835 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1836 | struct file_info *duplicate(long long file_size, long long bytes, |
| 1837 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 1838 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 1839 | unsigned short fragment_checksum, int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1840 | { |
| 1841 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 1842 | int frag_bytes = file_buffer ? file_buffer->size : 0; |
| 1843 | |
| 1844 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 1845 | if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes |
| 1846 | && frag_bytes == dupl_ptr->fragment->size) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1847 | long long target_start, dup_start = dupl_ptr->start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1848 | int block; |
| 1849 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1850 | if(memcmp(*block_list, dupl_ptr->block_list, blocks * |
| 1851 | sizeof(unsigned int)) != 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 1852 | continue; |
| 1853 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1854 | if(checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1855 | checksum = get_checksum_disk(*start, bytes, |
| 1856 | *block_list); |
| 1857 | fragment_checksum = |
| 1858 | get_checksum_mem_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1859 | checksum_flag = TRUE; |
| 1860 | } |
| 1861 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame^] | 1862 | if(!dupl_ptr->have_frag_checksum) |
| 1863 | get_fragment_checksum(dupl_ptr); |
Phillip Lougher | 557cefa | 2014-02-05 04:36:05 +0000 | [diff] [blame] | 1864 | |
| 1865 | if(!dupl_ptr->have_checksum) { |
| 1866 | dupl_ptr->checksum = |
| 1867 | get_checksum_disk(dupl_ptr->start, |
| 1868 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 1869 | dupl_ptr->have_checksum = TRUE; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1872 | if(checksum != dupl_ptr->checksum || |
| 1873 | fragment_checksum != |
| 1874 | dupl_ptr->fragment_checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1875 | continue; |
| 1876 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1877 | target_start = *start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1878 | for(block = 0; block < blocks; block ++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1879 | int size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 1880 | ((*block_list)[block]); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1881 | struct file_buffer *target_buffer = NULL; |
| 1882 | struct file_buffer *dup_buffer = NULL; |
| 1883 | char *target_data, *dup_data; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1884 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1885 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1886 | if(size == 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 1887 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1888 | target_buffer = cache_lookup(writer_buffer, |
| 1889 | target_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1890 | if(target_buffer) |
| 1891 | target_data = target_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1892 | else { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1893 | target_data = |
| 1894 | read_from_disk(target_start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1895 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1896 | if(target_data == NULL) { |
| 1897 | ERROR("Failed to read data from" |
| 1898 | " output filesystem\n"); |
| 1899 | BAD_ERROR("Output filesystem" |
| 1900 | " corrupted?\n"); |
| 1901 | } |
| 1902 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1903 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1904 | dup_buffer = cache_lookup(writer_buffer, |
| 1905 | dup_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1906 | if(dup_buffer) |
| 1907 | dup_data = dup_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1908 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1909 | dup_data = read_from_disk2(dup_start, |
| 1910 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1911 | if(dup_data == NULL) { |
| 1912 | ERROR("Failed to read data from" |
| 1913 | " output filesystem\n"); |
| 1914 | BAD_ERROR("Output filesystem" |
| 1915 | " corrupted?\n"); |
| 1916 | } |
| 1917 | } |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1918 | |
| 1919 | res = memcmp(target_data, dup_data, size); |
| 1920 | cache_block_put(target_buffer); |
| 1921 | cache_block_put(dup_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1922 | if(res != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1923 | break; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1924 | target_start += size; |
| 1925 | dup_start += size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1926 | } |
| 1927 | if(block == blocks) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1928 | struct file_buffer *frag_buffer = |
| 1929 | get_fragment(dupl_ptr->fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1930 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1931 | if(frag_bytes == 0 || |
| 1932 | memcmp(file_buffer->data, |
| 1933 | frag_buffer->data + |
| 1934 | dupl_ptr->fragment->offset, |
| 1935 | frag_bytes) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1936 | TRACE("Found duplicate file, start " |
| 1937 | "0x%llx, size %lld, checksum " |
| 1938 | "0x%x, fragment %d, size %d, " |
| 1939 | "offset %d, checksum 0x%x\n", |
| 1940 | dupl_ptr->start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1941 | dupl_ptr->bytes, |
| 1942 | dupl_ptr->checksum, |
| 1943 | dupl_ptr->fragment->index, |
| 1944 | frag_bytes, |
| 1945 | dupl_ptr->fragment->offset, |
| 1946 | fragment_checksum); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1947 | *block_list = dupl_ptr->block_list; |
| 1948 | *start = dupl_ptr->start; |
| 1949 | *fragment = dupl_ptr->fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1950 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1951 | return 0; |
| 1952 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1953 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1958 | return add_non_dup(file_size, bytes, *block_list, *start, *fragment, |
| 1959 | checksum, fragment_checksum, checksum_flag); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 1963 | inline int is_fragment(struct inode_info *inode) |
| 1964 | { |
| 1965 | int file_size = inode->buf.st_size; |
| 1966 | |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 1967 | /* |
| 1968 | * If this block is to be compressed differently to the |
| 1969 | * fragment compression then it cannot be a fragment |
| 1970 | */ |
| 1971 | if(inode->noF != noF) |
| 1972 | return FALSE; |
| 1973 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1974 | return !inode->no_fragments && file_size && (file_size < block_size || |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 1975 | (inode->always_use_fragments && file_size & (block_size - 1))); |
| 1976 | } |
| 1977 | |
| 1978 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1979 | void put_file_buffer(struct file_buffer *file_buffer) |
| 1980 | { |
| 1981 | /* |
| 1982 | * Decide where to send the file buffer - only compressible non- |
| 1983 | * fragment blocks need to be send to the deflate threads, all |
| 1984 | * others can be sent directly to the main thread |
| 1985 | */ |
| 1986 | if(file_buffer->error) { |
| 1987 | file_buffer->fragment = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 1988 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1989 | } else if (file_buffer->file_size == 0 || file_buffer->fragment) |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 1990 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1991 | else |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 1992 | queue_put(to_deflate, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 1996 | static int seq = 0; |
| 1997 | void reader_read_process(struct dir_ent *dir_ent) |
| 1998 | { |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 1999 | struct inode_info *inode = dir_ent->inode; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2000 | struct file_buffer *prev_buffer = NULL, *file_buffer; |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2001 | int status, res, byte, count = 0; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2002 | int file = get_pseudo_file(inode->pseudo_id)->fd; |
| 2003 | int child = get_pseudo_file(inode->pseudo_id)->child; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2004 | long long bytes = 0; |
| 2005 | |
| 2006 | while(1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2007 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2008 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2009 | file_buffer->noD = inode->noD; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2010 | |
| 2011 | byte = read_bytes(file, file_buffer->data, block_size); |
| 2012 | if(byte == -1) |
| 2013 | goto read_err; |
| 2014 | |
| 2015 | file_buffer->size = byte; |
| 2016 | file_buffer->file_size = -1; |
| 2017 | file_buffer->block = count ++; |
| 2018 | file_buffer->error = FALSE; |
| 2019 | file_buffer->fragment = FALSE; |
| 2020 | bytes += byte; |
| 2021 | |
| 2022 | if(byte == 0) |
| 2023 | break; |
| 2024 | |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2025 | /* |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2026 | * Update progress bar size. This is done |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2027 | * on every block rather than waiting for all blocks to be |
| 2028 | * read incase write_file_process() is running in parallel |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2029 | * with this. Otherwise the current progress bar position |
| 2030 | * may get ahead of the progress bar size. |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2031 | */ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2032 | progress_bar_size(1); |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2033 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2034 | if(prev_buffer) |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2035 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2036 | prev_buffer = file_buffer; |
| 2037 | } |
| 2038 | |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2039 | /* |
| 2040 | * Update inode file size now that the size of the dynamic pseudo file |
| 2041 | * is known. This is needed for the -info option. |
| 2042 | */ |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2043 | inode->buf.st_size = bytes; |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2044 | |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2045 | res = waitpid(child, &status, 0); |
plougher | d87d8d1 | 2009-09-10 04:08:16 +0000 | [diff] [blame] | 2046 | if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2047 | goto read_err; |
| 2048 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2049 | if(prev_buffer == NULL) |
| 2050 | prev_buffer = file_buffer; |
| 2051 | else { |
| 2052 | cache_block_put(file_buffer); |
| 2053 | seq --; |
| 2054 | } |
| 2055 | prev_buffer->file_size = bytes; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2056 | prev_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2057 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2058 | |
| 2059 | return; |
| 2060 | |
| 2061 | read_err: |
| 2062 | if(prev_buffer) { |
| 2063 | cache_block_put(file_buffer); |
| 2064 | seq --; |
| 2065 | file_buffer = prev_buffer; |
| 2066 | } |
| 2067 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2068 | put_file_buffer(file_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2072 | void reader_read_file(struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2073 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2074 | struct stat *buf = &dir_ent->inode->buf, buf2; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2075 | struct file_buffer *file_buffer; |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2076 | int blocks, byte, count, expected, file, res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2077 | long long bytes, read_size; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2078 | struct inode_info *inode = dir_ent->inode; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2079 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2080 | if(inode->read) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2081 | return; |
| 2082 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2083 | inode->read = TRUE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2084 | again: |
| 2085 | bytes = 0; |
| 2086 | count = 0; |
| 2087 | file_buffer = NULL; |
| 2088 | read_size = buf->st_size; |
| 2089 | blocks = (read_size + block_size - 1) >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2090 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2091 | file = open(pathname_reader(dir_ent), O_RDONLY); |
plougher | c1d258e | 2010-12-16 05:16:45 +0000 | [diff] [blame] | 2092 | if(file == -1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2093 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2094 | file_buffer->sequence = seq ++; |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2095 | goto read_err2; |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2096 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2097 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2098 | do { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2099 | expected = read_size - ((long long) count * block_size) > |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2100 | block_size ? block_size : |
| 2101 | read_size - ((long long) count * block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2102 | |
| 2103 | if(file_buffer) |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2104 | put_file_buffer(file_buffer); |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2105 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2106 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2107 | file_buffer->noD = inode->noD; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2108 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2109 | /* |
| 2110 | * Always try to read block_size bytes from the file rather |
| 2111 | * than expected bytes (which will be less than the block_size |
| 2112 | * at the file tail) to check that the file hasn't grown |
| 2113 | * since being stated. If it is longer (or shorter) than |
| 2114 | * expected, then restat, and try again. Note the special |
| 2115 | * case where the file is an exact multiple of the block_size |
| 2116 | * is dealt with later. |
| 2117 | */ |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2118 | byte = file_buffer->size = read_bytes(file, file_buffer->data, |
| 2119 | block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2120 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2121 | file_buffer->file_size = read_size; |
| 2122 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2123 | if(byte == -1) |
| 2124 | goto read_err; |
| 2125 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2126 | if(byte != expected) |
| 2127 | goto restat; |
| 2128 | |
| 2129 | file_buffer->block = count; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2130 | file_buffer->error = FALSE; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2131 | file_buffer->fragment = FALSE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2132 | |
| 2133 | bytes += byte; |
| 2134 | count ++; |
| 2135 | } while(count < blocks); |
| 2136 | |
| 2137 | if(read_size != bytes) |
| 2138 | goto restat; |
| 2139 | |
| 2140 | if(expected == block_size) { |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2141 | /* |
| 2142 | * Special case where we've not tried to read past the end of |
| 2143 | * the file. We expect to get EOF, i.e. the file isn't larger |
| 2144 | * than we expect. |
| 2145 | */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2146 | char buffer; |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2147 | int res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2148 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2149 | res = read_bytes(file, &buffer, 1); |
| 2150 | if(res == -1) |
| 2151 | goto read_err; |
| 2152 | |
| 2153 | if(res != 0) |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2154 | goto restat; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2157 | file_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2158 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2159 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2160 | close(file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2161 | |
| 2162 | return; |
| 2163 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2164 | restat: |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2165 | res = fstat(file, &buf2); |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2166 | if(res == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 2167 | ERROR("Cannot stat dir/file %s because %s\n", |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2168 | pathname_reader(dir_ent), strerror(errno)); |
| 2169 | goto read_err; |
| 2170 | } |
| 2171 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2172 | if(read_size != buf2.st_size) { |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2173 | close(file); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2174 | memcpy(buf, &buf2, sizeof(struct stat)); |
| 2175 | file_buffer->error = 2; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2176 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2177 | goto again; |
| 2178 | } |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2179 | read_err: |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2180 | close(file); |
| 2181 | read_err2: |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2182 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2183 | put_file_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | |
| 2187 | void reader_scan(struct dir_info *dir) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2188 | struct dir_ent *dir_ent = dir->list; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2189 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2190 | for(; dir_ent; dir_ent = dir_ent->next) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2191 | struct stat *buf = &dir_ent->inode->buf; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2192 | if(dir_ent->inode->root_entry) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2193 | continue; |
| 2194 | |
plougher | b3977eb | 2010-05-02 02:08:48 +0000 | [diff] [blame] | 2195 | if(IS_PSEUDO_PROCESS(dir_ent->inode)) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2196 | reader_read_process(dir_ent); |
| 2197 | continue; |
| 2198 | } |
| 2199 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2200 | switch(buf->st_mode & S_IFMT) { |
| 2201 | case S_IFREG: |
| 2202 | reader_read_file(dir_ent); |
| 2203 | break; |
| 2204 | case S_IFDIR: |
| 2205 | reader_scan(dir_ent->dir); |
| 2206 | break; |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | |
| 2212 | void *reader(void *arg) |
| 2213 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2214 | if(!sorted) |
| 2215 | reader_scan(queue_get(to_reader)); |
| 2216 | else { |
| 2217 | int i; |
| 2218 | struct priority_entry *entry; |
| 2219 | |
| 2220 | queue_get(to_reader); |
| 2221 | for(i = 65535; i >= 0; i--) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2222 | for(entry = priority_list[i]; entry; |
| 2223 | entry = entry->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2224 | reader_read_file(entry->dir); |
| 2225 | } |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2226 | |
| 2227 | pthread_exit(NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | |
| 2231 | void *writer(void *arg) |
| 2232 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2233 | while(1) { |
| 2234 | struct file_buffer *file_buffer = queue_get(to_writer); |
| 2235 | off_t off; |
| 2236 | |
| 2237 | if(file_buffer == NULL) { |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2238 | queue_put(from_writer, NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2239 | continue; |
| 2240 | } |
| 2241 | |
| 2242 | off = file_buffer->block; |
| 2243 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2244 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2245 | pthread_mutex_lock(&pos_mutex); |
| 2246 | |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2247 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | d23000c | 2013-02-06 22:05:45 +0000 | [diff] [blame] | 2248 | ERROR("writer: Lseek on destination failed because " |
| 2249 | "%s, offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2250 | BAD_ERROR("Probably out of space on output " |
Phillip Lougher | 3f4ccd2 | 2013-02-11 04:40:09 +0000 | [diff] [blame] | 2251 | "%s\n", block_device ? "block device" : |
| 2252 | "filesystem"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2255 | if(write_bytes(fd, file_buffer->data, |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2256 | file_buffer->size) == -1) |
| 2257 | BAD_ERROR("Failed to write to output %s\n", |
Phillip Lougher | e9df455 | 2013-02-14 23:06:29 +0000 | [diff] [blame] | 2258 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2259 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2260 | pthread_cleanup_pop(1); |
| 2261 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2262 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2267 | int all_zero(struct file_buffer *file_buffer) |
| 2268 | { |
| 2269 | int i; |
| 2270 | long entries = file_buffer->size / sizeof(long); |
| 2271 | long *p = (long *) file_buffer->data; |
| 2272 | |
| 2273 | for(i = 0; i < entries && p[i] == 0; i++); |
| 2274 | |
| 2275 | if(i == entries) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2276 | for(i = file_buffer->size & ~(sizeof(long) - 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2277 | i < file_buffer->size && file_buffer->data[i] == 0; |
| 2278 | i++); |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2279 | |
| 2280 | return i == file_buffer->size; |
| 2281 | } |
| 2282 | |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
| 2286 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2287 | void *deflator(void *arg) |
| 2288 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2289 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2290 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2291 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2292 | res = compressor_init(comp, &stream, block_size, 1); |
| 2293 | if(res) |
| 2294 | BAD_ERROR("deflator:: compressor_init failed\n"); |
| 2295 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2296 | while(1) { |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 2297 | struct file_buffer *file_buffer = queue_get(to_deflate); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2298 | struct file_buffer *write_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2299 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2300 | if(sparse_files && all_zero(file_buffer)) { |
Phillip Lougher | 4885438 | 2011-09-09 03:36:55 +0100 | [diff] [blame] | 2301 | file_buffer->c_byte = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2302 | seq_queue_put(to_main, file_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2303 | } else { |
Phillip Lougher | 4570201 | 2013-04-30 05:19:54 +0100 | [diff] [blame] | 2304 | write_buffer = cache_get(writer_buffer, -1); |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2305 | write_buffer->c_byte = mangle2(stream, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2306 | write_buffer->data, file_buffer->data, |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2307 | file_buffer->size, block_size, |
| 2308 | file_buffer->noD, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2309 | write_buffer->sequence = file_buffer->sequence; |
| 2310 | write_buffer->file_size = file_buffer->file_size; |
| 2311 | write_buffer->block = file_buffer->block; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2312 | write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2313 | (write_buffer->c_byte); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2314 | write_buffer->fragment = FALSE; |
| 2315 | write_buffer->error = FALSE; |
| 2316 | cache_block_put(file_buffer); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2317 | seq_queue_put(to_main, write_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2318 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | void *frag_deflator(void *arg) |
| 2324 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2325 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2326 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2327 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2328 | res = compressor_init(comp, &stream, block_size, 1); |
| 2329 | if(res) |
| 2330 | BAD_ERROR("frag_deflator:: compressor_init failed\n"); |
| 2331 | |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2332 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
| 2333 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2334 | while(1) { |
| 2335 | int c_byte, compressed_size; |
| 2336 | struct file_buffer *file_buffer = queue_get(to_frag); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2337 | struct file_buffer *write_buffer = |
Phillip Lougher | 4570201 | 2013-04-30 05:19:54 +0100 | [diff] [blame] | 2338 | cache_get(writer_buffer, |
| 2339 | FRAG_INDEX(file_buffer->block)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2340 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2341 | c_byte = mangle2(stream, write_buffer->data, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2342 | file_buffer->size, block_size, noF, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2343 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte); |
plougher | d036a31 | 2008-03-08 01:38:27 +0000 | [diff] [blame] | 2344 | write_buffer->size = compressed_size; |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2345 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2346 | if(fragments_locked == FALSE) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2347 | fragment_table[file_buffer->block].size = c_byte; |
| 2348 | fragment_table[file_buffer->block].start_block = bytes; |
| 2349 | write_buffer->block = bytes; |
| 2350 | bytes += compressed_size; |
| 2351 | fragments_outstanding --; |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2352 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2353 | queue_put(to_writer, write_buffer); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2354 | TRACE("Writing fragment %lld, uncompressed size %d, " |
| 2355 | "compressed size %d\n", file_buffer->block, |
| 2356 | file_buffer->size, compressed_size); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2357 | } else { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2358 | add_pending_fragment(write_buffer, c_byte, |
| 2359 | file_buffer->block); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2360 | pthread_mutex_unlock(&fragment_mutex); |
| 2361 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2362 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2363 | } |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2364 | |
| 2365 | pthread_cleanup_pop(0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2369 | struct file_buffer *get_file_buffer() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2370 | { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2371 | struct file_buffer *file_buffer = seq_queue_get(to_main); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2372 | |
| 2373 | if(file_buffer->fragment) { |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2374 | if(sparse_files && all_zero(file_buffer)) { |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2375 | file_buffer->c_byte = 0; |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2376 | file_buffer->fragment = FALSE; |
| 2377 | } else |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2378 | file_buffer->c_byte = file_buffer->size; |
| 2379 | } |
| 2380 | |
| 2381 | return file_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2385 | void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2386 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2387 | { |
| 2388 | file_count ++; |
| 2389 | *duplicate_file = FALSE; |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2390 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0, |
| 2391 | NULL, &empty_fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2395 | void write_file_frag_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2396 | int size, int *duplicate_file, struct file_buffer *file_buffer, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2397 | unsigned short checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2398 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2399 | struct file_info *dupl_ptr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2400 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2401 | unsigned int *block_listp = NULL; |
| 2402 | long long start = 0; |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2403 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2404 | dupl_ptr = duplicate(size, 0, &block_listp, &start, &fragment, |
| 2405 | file_buffer, 0, 0, checksum, TRUE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2406 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2407 | if(dupl_ptr) { |
| 2408 | *duplicate_file = FALSE; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2409 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2410 | dupl_ptr->fragment = fragment; |
| 2411 | } else |
| 2412 | *duplicate_file = TRUE; |
| 2413 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2414 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2415 | |
| 2416 | total_bytes += size; |
| 2417 | file_count ++; |
| 2418 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2419 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2420 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2421 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2422 | 0, NULL, fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2426 | void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, int size, |
| 2427 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2428 | { |
| 2429 | struct fragment *fragment; |
| 2430 | unsigned short checksum; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2431 | |
| 2432 | checksum = get_checksum_mem_buffer(file_buffer); |
| 2433 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2434 | if(pre_duplicate_frag(size, checksum)) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2435 | write_file_frag_dup(inode, dir_ent, size, duplicate_file, |
| 2436 | file_buffer, checksum); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2437 | return; |
| 2438 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2439 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2440 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2441 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2442 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2443 | |
| 2444 | if(duplicate_checking) |
| 2445 | add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, TRUE); |
| 2446 | |
| 2447 | total_bytes += size; |
| 2448 | file_count ++; |
| 2449 | |
| 2450 | *duplicate_file = FALSE; |
| 2451 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2452 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2453 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2454 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2455 | 0, NULL, fragment, NULL, 0); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2456 | |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2457 | if(duplicate_checking == FALSE) |
| 2458 | free_fragment(fragment); |
| 2459 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2460 | return; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2464 | int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2465 | struct file_buffer *read_buffer, int *duplicate_file) |
| 2466 | { |
| 2467 | long long read_size, file_bytes, start; |
| 2468 | struct fragment *fragment; |
| 2469 | unsigned int *block_list = NULL; |
| 2470 | int block = 0, status; |
| 2471 | long long sparse = 0; |
| 2472 | struct file_buffer *fragment_buffer = NULL; |
| 2473 | |
| 2474 | *duplicate_file = FALSE; |
| 2475 | |
| 2476 | lock_fragments(); |
| 2477 | |
| 2478 | file_bytes = 0; |
| 2479 | start = bytes; |
| 2480 | while (1) { |
| 2481 | read_size = read_buffer->file_size; |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2482 | if(read_buffer->fragment) |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2483 | fragment_buffer = read_buffer; |
| 2484 | else { |
| 2485 | block_list = realloc(block_list, (block + 1) * |
| 2486 | sizeof(unsigned int)); |
| 2487 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2488 | MEM_ERROR(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2489 | block_list[block ++] = read_buffer->c_byte; |
| 2490 | if(read_buffer->c_byte) { |
| 2491 | read_buffer->block = bytes; |
| 2492 | bytes += read_buffer->size; |
| 2493 | cache_rehash(read_buffer, read_buffer->block); |
| 2494 | file_bytes += read_buffer->size; |
| 2495 | queue_put(to_writer, read_buffer); |
| 2496 | } else { |
| 2497 | sparse += read_buffer->size; |
| 2498 | cache_block_put(read_buffer); |
| 2499 | } |
| 2500 | } |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2501 | inc_progress_bar(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2502 | |
| 2503 | if(read_size != -1) |
| 2504 | break; |
| 2505 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2506 | read_buffer = get_file_buffer(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2507 | if(read_buffer->error) |
| 2508 | goto read_err; |
| 2509 | } |
| 2510 | |
| 2511 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2512 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2513 | cache_block_put(fragment_buffer); |
| 2514 | |
| 2515 | if(duplicate_checking) |
| 2516 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2517 | 0, 0, FALSE); |
| 2518 | file_count ++; |
| 2519 | total_bytes += read_size; |
| 2520 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2521 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2522 | block, block_list, fragment, NULL, sparse); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2523 | |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2524 | if(duplicate_checking == FALSE) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2525 | free(block_list); |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2526 | free_fragment(fragment); |
| 2527 | } |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2528 | |
| 2529 | return 0; |
| 2530 | |
| 2531 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2532 | dec_progress_bar(block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2533 | status = read_buffer->error; |
| 2534 | bytes = start; |
| 2535 | if(!block_device) { |
| 2536 | int res; |
| 2537 | |
| 2538 | queue_put(to_writer, NULL); |
| 2539 | if(queue_get(from_writer) != 0) |
| 2540 | EXIT_MKSQUASHFS(); |
| 2541 | res = ftruncate(fd, bytes); |
| 2542 | if(res != 0) |
| 2543 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2544 | strerror(errno)); |
| 2545 | } |
| 2546 | unlock_fragments(); |
| 2547 | free(block_list); |
| 2548 | cache_block_put(read_buffer); |
| 2549 | return status; |
| 2550 | } |
| 2551 | |
| 2552 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2553 | int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2554 | long long read_size, struct file_buffer *read_buffer, |
| 2555 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2556 | { |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 2557 | long long file_bytes, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2558 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2559 | unsigned int *block_list; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2560 | int block, status; |
| 2561 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2562 | long long sparse = 0; |
| 2563 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2564 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2565 | *duplicate_file = FALSE; |
| 2566 | |
plougher | 8713962 | 2010-12-16 05:19:30 +0000 | [diff] [blame] | 2567 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2568 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2569 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2570 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2571 | lock_fragments(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2572 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2573 | file_bytes = 0; |
| 2574 | start = bytes; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2575 | for(block = 0; block < blocks;) { |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2576 | if(read_buffer->fragment) { |
Phillip Lougher | 2760d82 | 2013-01-13 21:14:06 +0000 | [diff] [blame] | 2577 | block_list[block] = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2578 | fragment_buffer = read_buffer; |
| 2579 | blocks = read_size >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2580 | } else { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2581 | block_list[block] = read_buffer->c_byte; |
| 2582 | if(read_buffer->c_byte) { |
| 2583 | read_buffer->block = bytes; |
| 2584 | bytes += read_buffer->size; |
| 2585 | cache_rehash(read_buffer, read_buffer->block); |
| 2586 | file_bytes += read_buffer->size; |
| 2587 | queue_put(to_writer, read_buffer); |
| 2588 | } else { |
| 2589 | sparse += read_buffer->size; |
| 2590 | cache_block_put(read_buffer); |
| 2591 | } |
| 2592 | } |
| 2593 | inc_progress_bar(); |
| 2594 | |
| 2595 | if(++block < blocks) { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2596 | read_buffer = get_file_buffer(); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2597 | if(read_buffer->error) |
| 2598 | goto read_err; |
| 2599 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2602 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2603 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2604 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2605 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2606 | if(duplicate_checking) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2607 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2608 | 0, 0, FALSE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2609 | file_count ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2610 | total_bytes += read_size; |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2611 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2612 | /* |
| 2613 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2614 | * a smaller block count on stat calls to sparse files. This is |
| 2615 | * to ensure intelligent applications like cp correctly handle the |
| 2616 | * file as a sparse file. If the file in the original filesystem isn't |
| 2617 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2618 | * report it as non-sparse on stat calls to preserve semantics |
| 2619 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2620 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2621 | sparse = 0; |
| 2622 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2623 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2624 | blocks, block_list, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2625 | |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2626 | if(duplicate_checking == FALSE) { |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2627 | free(block_list); |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2628 | free_fragment(fragment); |
| 2629 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2630 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2631 | return 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2632 | |
| 2633 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2634 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2635 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2636 | bytes = start; |
| 2637 | if(!block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2638 | int res; |
| 2639 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2640 | queue_put(to_writer, NULL); |
| 2641 | if(queue_get(from_writer) != 0) |
| 2642 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2643 | res = ftruncate(fd, bytes); |
| 2644 | if(res != 0) |
| 2645 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2646 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2647 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2648 | unlock_fragments(); |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2649 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2650 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2651 | return status; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2655 | int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2656 | long long read_size, struct file_buffer *read_buffer, |
| 2657 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2658 | { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2659 | int block, thresh; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2660 | long long file_bytes, dup_start, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2661 | struct fragment *fragment; |
| 2662 | struct file_info *dupl_ptr; |
| 2663 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2664 | unsigned int *block_list, *block_listp; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2665 | struct file_buffer **buffer_list; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 2666 | int status, num_locked_fragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2667 | long long sparse = 0; |
| 2668 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2669 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2670 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2671 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2672 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2673 | block_listp = block_list; |
| 2674 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2675 | buffer_list = malloc(blocks * sizeof(struct file_buffer *)); |
| 2676 | if(buffer_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2677 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2678 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 2679 | num_locked_fragments = lock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2680 | |
| 2681 | file_bytes = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2682 | start = dup_start = bytes; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2683 | thresh = blocks > (writer_buffer_size - num_locked_fragments) ? |
| 2684 | blocks - (writer_buffer_size - num_locked_fragments): 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2685 | |
| 2686 | for(block = 0; block < blocks;) { |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2687 | if(read_buffer->fragment) { |
Phillip Lougher | 38cb1ab | 2013-01-13 21:20:09 +0000 | [diff] [blame] | 2688 | block_list[block] = 0; |
Phillip Lougher | 19de5b6 | 2013-01-13 21:31:51 +0000 | [diff] [blame] | 2689 | buffer_list[block] = NULL; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2690 | fragment_buffer = read_buffer; |
| 2691 | blocks = read_size >> block_log; |
| 2692 | } else { |
| 2693 | block_list[block] = read_buffer->c_byte; |
| 2694 | |
| 2695 | if(read_buffer->c_byte) { |
| 2696 | read_buffer->block = bytes; |
| 2697 | bytes += read_buffer->size; |
| 2698 | file_bytes += read_buffer->size; |
| 2699 | cache_rehash(read_buffer, read_buffer->block); |
| 2700 | if(block < thresh) { |
| 2701 | buffer_list[block] = NULL; |
| 2702 | queue_put(to_writer, read_buffer); |
| 2703 | } else |
| 2704 | buffer_list[block] = read_buffer; |
| 2705 | } else { |
| 2706 | buffer_list[block] = NULL; |
| 2707 | sparse += read_buffer->size; |
| 2708 | cache_block_put(read_buffer); |
| 2709 | } |
| 2710 | } |
| 2711 | inc_progress_bar(); |
| 2712 | |
| 2713 | if(++block < blocks) { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2714 | read_buffer = get_file_buffer(); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2715 | if(read_buffer->error) |
| 2716 | goto read_err; |
| 2717 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2720 | dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start, |
| 2721 | &fragment, fragment_buffer, blocks, 0, 0, FALSE); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2722 | |
| 2723 | if(dupl_ptr) { |
| 2724 | *duplicate_file = FALSE; |
| 2725 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2726 | if(buffer_list[block]) |
| 2727 | queue_put(to_writer, buffer_list[block]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2728 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2729 | dupl_ptr->fragment = fragment; |
| 2730 | } else { |
| 2731 | *duplicate_file = TRUE; |
| 2732 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2733 | cache_block_put(buffer_list[block]); |
| 2734 | bytes = start; |
| 2735 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2736 | int res; |
| 2737 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2738 | queue_put(to_writer, NULL); |
| 2739 | if(queue_get(from_writer) != 0) |
| 2740 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2741 | res = ftruncate(fd, bytes); |
| 2742 | if(res != 0) |
| 2743 | BAD_ERROR("Failed to truncate dest file because" |
| 2744 | " %s\n", strerror(errno)); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2745 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2748 | unlock_fragments(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2749 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2750 | free(buffer_list); |
| 2751 | file_count ++; |
| 2752 | total_bytes += read_size; |
| 2753 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2754 | /* |
| 2755 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2756 | * a smaller block count on stat calls to sparse files. This is |
| 2757 | * to ensure intelligent applications like cp correctly handle the |
| 2758 | * file as a sparse file. If the file in the original filesystem isn't |
| 2759 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2760 | * report it as non-sparse on stat calls to preserve semantics |
| 2761 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2762 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2763 | sparse = 0; |
| 2764 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2765 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, |
| 2766 | dup_start, blocks, block_listp, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2767 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2768 | if(*duplicate_file == TRUE) |
| 2769 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2770 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2771 | return 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2772 | |
| 2773 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2774 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2775 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2776 | bytes = start; |
| 2777 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2778 | int res; |
| 2779 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2780 | queue_put(to_writer, NULL); |
| 2781 | if(queue_get(from_writer) != 0) |
| 2782 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2783 | res = ftruncate(fd, bytes); |
| 2784 | if(res != 0) |
| 2785 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2786 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2787 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2788 | unlock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2789 | for(blocks = thresh; blocks < block; blocks ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2790 | cache_block_put(buffer_list[blocks]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2791 | free(buffer_list); |
| 2792 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2793 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2794 | return status; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2798 | void write_file(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2799 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2800 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2801 | int status; |
| 2802 | struct file_buffer *read_buffer; |
| 2803 | long long read_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2804 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2805 | again: |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2806 | read_buffer = get_file_buffer(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2807 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 2808 | status = read_buffer->error; |
| 2809 | if(status) { |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2810 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2811 | goto file_err; |
| 2812 | } |
| 2813 | |
| 2814 | read_size = read_buffer->file_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2815 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2816 | if(read_size == -1) |
| 2817 | status = write_file_process(inode, dir_ent, read_buffer, |
| 2818 | duplicate_file); |
| 2819 | else if(read_size == 0) { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2820 | write_file_empty(inode, dir_ent, duplicate_file); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2821 | cache_block_put(read_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2822 | } else if(read_buffer->fragment && read_buffer->c_byte) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2823 | write_file_frag(inode, dir_ent, read_size, read_buffer, |
| 2824 | duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2825 | else if(pre_duplicate(read_size)) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2826 | status = write_file_blocks_dup(inode, dir_ent, read_size, |
| 2827 | read_buffer, duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2828 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2829 | status = write_file_blocks(inode, dir_ent, read_size, |
| 2830 | read_buffer, duplicate_file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2831 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2832 | file_err: |
| 2833 | if(status == 2) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2834 | ERROR("File %s changed size while reading filesystem, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2835 | "attempting to re-read\n", pathname(dir_ent)); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2836 | goto again; |
| 2837 | } else if(status == 1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 2838 | ERROR_START("Failed to read file %s", pathname(dir_ent)); |
| 2839 | ERROR_EXIT(", creating empty file\n"); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2840 | write_file_empty(inode, dir_ent, duplicate_file); |
| 2841 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2845 | #define BUFF_SIZE 512 |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2846 | char *name; |
| 2847 | char *basename_r(); |
| 2848 | |
| 2849 | char *getbase(char *pathname) |
| 2850 | { |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2851 | static char *b_buffer = NULL; |
| 2852 | static int b_size = BUFF_SIZE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2853 | char *result; |
| 2854 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2855 | if(b_buffer == NULL) { |
| 2856 | b_buffer = malloc(b_size); |
| 2857 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2858 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | while(1) { |
| 2862 | if(*pathname != '/') { |
| 2863 | result = getcwd(b_buffer, b_size); |
| 2864 | if(result == NULL && errno != ERANGE) |
| 2865 | BAD_ERROR("Getcwd failed in getbase\n"); |
| 2866 | |
| 2867 | /* enough room for pathname + "/" + '\0' terminator? */ |
| 2868 | if(result && strlen(pathname) + 2 <= |
| 2869 | b_size - strlen(b_buffer)) { |
| 2870 | strcat(strcat(b_buffer, "/"), pathname); |
| 2871 | break; |
| 2872 | } |
| 2873 | } else if(strlen(pathname) < b_size) { |
| 2874 | strcpy(b_buffer, pathname); |
| 2875 | break; |
| 2876 | } |
| 2877 | |
| 2878 | /* Buffer not large enough, realloc and try again */ |
| 2879 | b_buffer = realloc(b_buffer, b_size += BUFF_SIZE); |
| 2880 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2881 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2884 | name = b_buffer; |
| 2885 | if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0)) |
| 2886 | return NULL; |
| 2887 | else |
| 2888 | return result; |
| 2889 | } |
| 2890 | |
| 2891 | |
| 2892 | char *basename_r() |
| 2893 | { |
| 2894 | char *s; |
| 2895 | char *p; |
| 2896 | int n = 1; |
| 2897 | |
| 2898 | for(;;) { |
| 2899 | s = name; |
| 2900 | if(*name == '\0') |
| 2901 | return NULL; |
| 2902 | if(*name != '/') { |
| 2903 | while(*name != '\0' && *name != '/') name++; |
| 2904 | n = name - s; |
| 2905 | } |
| 2906 | while(*name == '/') name++; |
| 2907 | if(strncmp(s, ".", n) == 0) |
| 2908 | continue; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2909 | if((*name == '\0') || (strncmp(s, "..", n) == 0) || |
| 2910 | ((p = basename_r()) == NULL)) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2911 | s[n] = '\0'; |
| 2912 | return s; |
| 2913 | } |
| 2914 | if(strcmp(p, "..") == 0) |
| 2915 | continue; |
| 2916 | return p; |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2921 | struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2922 | { |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2923 | int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino); |
| 2924 | struct inode_info *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2925 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2926 | /* |
| 2927 | * Look-up inode in hash table, if it already exists we have a |
| 2928 | * hard-link, so increment the nlink count and return it. |
| 2929 | * Don't do the look-up for directories because we don't hard-link |
| 2930 | * directories. |
| 2931 | */ |
| 2932 | if ((buf->st_mode & S_IFMT) != S_IFDIR) { |
| 2933 | for(inode = inode_info[ino_hash]; inode; inode = inode->next) { |
| 2934 | if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) { |
| 2935 | inode->nlink ++; |
| 2936 | return inode; |
| 2937 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2938 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
plougher | 288f685 | 2010-12-16 05:22:55 +0000 | [diff] [blame] | 2941 | inode = malloc(sizeof(struct inode_info)); |
| 2942 | if(inode == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2943 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2944 | |
| 2945 | memcpy(&inode->buf, buf, sizeof(struct stat)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2946 | inode->read = FALSE; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2947 | inode->root_entry = FALSE; |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2948 | inode->pseudo_file = pseudo; |
| 2949 | inode->pseudo_id = id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2950 | inode->inode = SQUASHFS_INVALID_BLK; |
| 2951 | inode->nlink = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 2952 | inode->inode_number = 0; |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 2953 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2954 | /* |
| 2955 | * Copy filesystem wide defaults into inode, these filesystem |
| 2956 | * wide defaults may be altered on an individual inode basis by |
| 2957 | * user specified actions |
| 2958 | * |
| 2959 | */ |
| 2960 | inode->no_fragments = no_fragments; |
| 2961 | inode->always_use_fragments = always_use_fragments; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2962 | inode->noD = noD; |
| 2963 | inode->noF = noF; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2964 | |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 2965 | if((buf->st_mode & S_IFMT) == S_IFREG) |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2966 | progress_bar_size((buf->st_size + block_size - 1) >> block_log); |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 2967 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2968 | inode->next = inode_info[ino_hash]; |
| 2969 | inode_info[ino_hash] = inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2970 | |
| 2971 | return inode; |
| 2972 | } |
| 2973 | |
| 2974 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2975 | inline struct inode_info *lookup_inode(struct stat *buf) |
| 2976 | { |
| 2977 | return lookup_inode2(buf, 0, 0); |
| 2978 | } |
| 2979 | |
| 2980 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 2981 | inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this) |
| 2982 | { |
| 2983 | if (inode->inode_number == 0) |
| 2984 | inode->inode_number = use_this ? : inode_no ++; |
| 2985 | } |
| 2986 | |
| 2987 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2988 | inline struct dir_ent *create_dir_entry(char *name, char *source_name, |
| 2989 | char *nonstandard_pathname, struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2990 | { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2991 | struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent)); |
| 2992 | if(dir_ent == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2993 | MEM_ERROR(); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2994 | |
| 2995 | dir_ent->name = name; |
| 2996 | dir_ent->source_name = source_name; |
| 2997 | dir_ent->nonstandard_pathname = nonstandard_pathname; |
| 2998 | dir_ent->our_dir = dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2999 | dir_ent->next = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3000 | |
| 3001 | return dir_ent; |
| 3002 | } |
| 3003 | |
| 3004 | |
| 3005 | inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir, |
| 3006 | struct inode_info *inode_info) |
| 3007 | { |
| 3008 | struct dir_info *dir = dir_ent->our_dir; |
| 3009 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3010 | if(sub_dir) |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3011 | sub_dir->dir_ent = dir_ent; |
| 3012 | dir_ent->inode = inode_info; |
| 3013 | dir_ent->dir = sub_dir; |
| 3014 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3015 | dir_ent->next = dir->list; |
| 3016 | dir->list = dir_ent; |
| 3017 | dir->count++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | |
| 3021 | inline void add_dir_entry2(char *name, char *source_name, |
| 3022 | char *nonstandard_pathname, struct dir_info *sub_dir, |
| 3023 | struct inode_info *inode_info, struct dir_info *dir) |
| 3024 | { |
| 3025 | struct dir_ent *dir_ent = create_dir_entry(name, source_name, |
| 3026 | nonstandard_pathname, dir); |
| 3027 | |
| 3028 | |
| 3029 | add_dir_entry(dir_ent, sub_dir, inode_info); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3033 | inline void free_dir_entry(struct dir_ent *dir_ent) |
| 3034 | { |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3035 | if(dir_ent->name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3036 | free(dir_ent->name); |
| 3037 | |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3038 | if(dir_ent->source_name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3039 | free(dir_ent->source_name); |
| 3040 | |
| 3041 | free(dir_ent); |
| 3042 | } |
| 3043 | |
| 3044 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3045 | inline void add_excluded(struct dir_info *dir) |
| 3046 | { |
| 3047 | dir->excluded ++; |
| 3048 | } |
| 3049 | |
| 3050 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3051 | void dir_scan(squashfs_inode *inode, char *pathname, |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 3052 | struct dir_ent *(_readdir)(struct dir_info *), int progress) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3053 | { |
| 3054 | struct stat buf; |
| 3055 | struct dir_info *dir_info = dir_scan1(pathname, "", paths, _readdir, 1); |
| 3056 | struct dir_ent *dir_ent; |
| 3057 | |
| 3058 | if(dir_info == NULL) |
| 3059 | return; |
| 3060 | |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3061 | /* |
| 3062 | * Process most actions and any pseudo files |
| 3063 | */ |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 3064 | if(actions() || get_pseudo()) |
| 3065 | dir_scan2(dir_info, get_pseudo()); |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3066 | |
Phillip Lougher | e92cb51 | 2012-11-15 02:22:28 +0000 | [diff] [blame] | 3067 | /* |
| 3068 | * Process move actions |
| 3069 | */ |
| 3070 | if(move_actions()) { |
| 3071 | dir_scan3(dir_info, dir_info); |
| 3072 | do_move_actions(); |
| 3073 | } |
| 3074 | |
Phillip Lougher | 71ae14e | 2012-11-15 02:27:50 +0000 | [diff] [blame] | 3075 | /* |
| 3076 | * Process empty actions |
| 3077 | */ |
| 3078 | if(empty_actions()) |
| 3079 | dir_scan4(dir_info); |
| 3080 | |
Phillip Lougher | eb69dad | 2012-11-15 03:34:02 +0000 | [diff] [blame] | 3081 | /* |
| 3082 | * Sort directories and compute the inode numbers |
| 3083 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3084 | dir_scan5(dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3085 | |
Phillip Lougher | d0fe1d5 | 2012-10-09 03:34:34 +0100 | [diff] [blame] | 3086 | dir_ent = create_dir_entry("", NULL, pathname, |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 3087 | scan1_opendir("", "", 0)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3088 | |
| 3089 | if(pathname[0] == '\0') { |
| 3090 | /* |
| 3091 | * dummy top level directory, if multiple sources specified on |
| 3092 | * command line |
| 3093 | */ |
| 3094 | memset(&buf, 0, sizeof(buf)); |
| 3095 | buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR; |
| 3096 | buf.st_uid = getuid(); |
| 3097 | buf.st_gid = getgid(); |
| 3098 | buf.st_mtime = time(NULL); |
| 3099 | buf.st_dev = 0; |
| 3100 | buf.st_ino = 0; |
| 3101 | dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0); |
| 3102 | } else { |
Phillip Lougher | 38a35ec | 2012-11-30 04:31:35 +0000 | [diff] [blame] | 3103 | if(lstat(pathname, &buf) == -1) |
| 3104 | /* source directory has disappeared? */ |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3105 | BAD_ERROR("Cannot stat source directory %s because %s\n", |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3106 | pathname, strerror(errno)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3107 | dir_ent->inode = lookup_inode(&buf); |
| 3108 | } |
| 3109 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3110 | alloc_inode_no(dir_ent->inode, root_inode_number); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3111 | dir_ent->dir = dir_info; |
| 3112 | dir_info->dir_ent = dir_ent; |
| 3113 | |
Andy Lutomirski | 3f31dcf | 2012-09-21 18:08:08 -0700 | [diff] [blame] | 3114 | eval_actions(dir_ent); |
| 3115 | |
Phillip Lougher | 19b877d | 2013-02-23 11:15:38 +0000 | [diff] [blame] | 3116 | if(sorted) |
| 3117 | generate_file_priorities(dir_info, 0, |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3118 | &dir_info->dir_ent->inode->buf); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3119 | queue_put(to_reader, dir_info); |
| 3120 | if(sorted) |
| 3121 | sort_files_and_write(dir_info); |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 3122 | set_progressbar_state(progress); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3123 | dir_scan6(inode, dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3124 | dir_ent->inode->inode = *inode; |
| 3125 | dir_ent->inode->type = SQUASHFS_DIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3129 | /* |
| 3130 | * dir_scan1 routines... |
Phillip Lougher | e8630be | 2012-11-11 02:24:24 +0000 | [diff] [blame] | 3131 | * These scan the source directories into memory for processing. |
| 3132 | * Exclude actions are processed here (in contrast to the other actions) |
| 3133 | * because they affect what is scanned. |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3134 | */ |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3135 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3136 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3137 | struct dir_info *dir; |
| 3138 | |
plougher | 6da792b | 2010-12-16 05:25:39 +0000 | [diff] [blame] | 3139 | dir = malloc(sizeof(struct dir_info)); |
| 3140 | if(dir == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3141 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3142 | |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3143 | if(pathname[0] != '\0') { |
| 3144 | dir->linuxdir = opendir(pathname); |
| 3145 | if(dir->linuxdir == NULL) { |
| 3146 | free(dir); |
| 3147 | return NULL; |
| 3148 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3149 | } |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3150 | |
Phillip Lougher | 3cb5d6c | 2013-01-09 05:50:29 +0000 | [diff] [blame] | 3151 | dir->pathname = strdup(pathname); |
| 3152 | dir->subpath = strdup(subpath); |
Phillip Lougher | ded70fa | 2011-12-25 02:14:58 +0000 | [diff] [blame] | 3153 | dir->count = 0; |
| 3154 | dir->directory_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3155 | dir->dir_is_ldir = TRUE; |
| 3156 | dir->list = NULL; |
Phillip Lougher | 0b5a124 | 2011-12-25 03:22:42 +0000 | [diff] [blame] | 3157 | dir->depth = depth; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3158 | dir->excluded = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3159 | |
| 3160 | return dir; |
| 3161 | } |
| 3162 | |
| 3163 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3164 | struct dir_ent *scan1_encomp_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3165 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3166 | static int index = 0; |
| 3167 | |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3168 | if(dir->count < old_root_entries) { |
| 3169 | int i; |
| 3170 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3171 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3172 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3173 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3174 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3175 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3176 | } |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3177 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3178 | |
| 3179 | while(index < source) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3180 | char *basename = NULL; |
| 3181 | char *dir_name = getbase(source_path[index]); |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3182 | int pass = 1, res; |
plougher | 89fe2c7 | 2010-12-16 05:31:34 +0000 | [diff] [blame] | 3183 | |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3184 | if(dir_name == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3185 | ERROR_START("Bad source directory %s", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3186 | source_path[index]); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3187 | ERROR_EXIT(" - skipping ...\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3188 | index ++; |
| 3189 | continue; |
| 3190 | } |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3191 | dir_name = strdup(dir_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3192 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3193 | struct dir_ent *dir_ent = dir->list; |
| 3194 | |
| 3195 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3196 | dir_ent = dir_ent->next); |
| 3197 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3198 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3199 | ERROR("Source directory entry %s already used! - trying" |
| 3200 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3201 | if(pass == 1) |
| 3202 | basename = dir_name; |
| 3203 | else |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3204 | free(dir_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3205 | res = asprintf(&dir_name, "%s_%d", basename, pass++); |
| 3206 | if(res == -1) |
| 3207 | BAD_ERROR("asprintf failed in " |
| 3208 | "scan1_encomp_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3209 | ERROR("%s\n", dir_name); |
| 3210 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3211 | return create_dir_entry(dir_name, basename, |
| 3212 | source_path[index ++], dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3213 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3214 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3218 | struct dir_ent *scan1_single_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3219 | { |
| 3220 | struct dirent *d_name; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3221 | int i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3222 | |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3223 | if(dir->count < old_root_entries) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3224 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3225 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3226 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3227 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3228 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3229 | } |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3230 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3231 | |
| 3232 | if((d_name = readdir(dir->linuxdir)) != NULL) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3233 | char *basename = NULL; |
| 3234 | char *dir_name = strdup(d_name->d_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3235 | int pass = 1, res; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3236 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3237 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3238 | struct dir_ent *dir_ent = dir->list; |
| 3239 | |
| 3240 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3241 | dir_ent = dir_ent->next); |
| 3242 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3243 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3244 | ERROR("Source directory entry %s already used! - trying" |
| 3245 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3246 | if (pass == 1) |
| 3247 | basename = dir_name; |
| 3248 | else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3249 | free(dir_name); |
| 3250 | res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++); |
| 3251 | if(res == -1) |
| 3252 | BAD_ERROR("asprintf failed in " |
| 3253 | "scan1_single_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3254 | ERROR("%s\n", dir_name); |
| 3255 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3256 | return create_dir_entry(dir_name, basename, NULL, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3259 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3263 | struct dir_ent *scan1_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3264 | { |
plougher | 480d9bf | 2010-12-18 02:28:39 +0000 | [diff] [blame] | 3265 | struct dirent *d_name = readdir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3266 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3267 | return d_name ? |
| 3268 | create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) : |
| 3269 | NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3273 | void scan1_freedir(struct dir_info *dir) |
| 3274 | { |
plougher | fbed12b | 2006-02-07 12:45:53 +0000 | [diff] [blame] | 3275 | if(dir->pathname[0] != '\0') |
| 3276 | closedir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3280 | struct dir_info *dir_scan1(char *filename, char *subpath, |
| 3281 | struct pathnames *paths, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3282 | struct dir_ent *(_readdir)(struct dir_info *), int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3283 | { |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3284 | struct dir_info *dir = scan1_opendir(filename, subpath, depth); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3285 | struct dir_ent *dir_ent; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3286 | |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3287 | if(dir == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3288 | ERROR_START("Could not open %s", filename); |
| 3289 | ERROR_EXIT(", skipping...\n"); |
Phillip Lougher | 87db567 | 2013-01-09 05:40:46 +0000 | [diff] [blame] | 3290 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3293 | while((dir_ent = _readdir(dir))) { |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3294 | struct dir_info *sub_dir; |
| 3295 | struct stat buf; |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 3296 | struct pathnames *new = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3297 | char *filename = pathname(dir_ent); |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3298 | char *subpath = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3299 | char *dir_name = dir_ent->name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3300 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3301 | if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3302 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3303 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3304 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3305 | |
| 3306 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3307 | ERROR_START("Cannot stat dir/file %s because %s", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3308 | filename, strerror(errno)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3309 | ERROR_EXIT(", ignoring\n"); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3310 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3311 | continue; |
| 3312 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3313 | |
| 3314 | if((buf.st_mode & S_IFMT) != S_IFREG && |
Phillip Lougher | e4ff36a | 2012-11-19 01:34:47 +0000 | [diff] [blame] | 3315 | (buf.st_mode & S_IFMT) != S_IFDIR && |
| 3316 | (buf.st_mode & S_IFMT) != S_IFLNK && |
| 3317 | (buf.st_mode & S_IFMT) != S_IFCHR && |
| 3318 | (buf.st_mode & S_IFMT) != S_IFBLK && |
| 3319 | (buf.st_mode & S_IFMT) != S_IFIFO && |
| 3320 | (buf.st_mode & S_IFMT) != S_IFSOCK) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3321 | ERROR_START("File %s has unrecognised filetype %d", |
| 3322 | filename, buf.st_mode & S_IFMT); |
| 3323 | ERROR_EXIT(", ignoring\n"); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3324 | free_dir_entry(dir_ent); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3325 | continue; |
| 3326 | } |
| 3327 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3328 | if((old_exclude && old_excluded(filename, &buf)) || |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3329 | (!old_exclude && excluded(dir_name, paths, &new))) { |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3330 | add_excluded(dir); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3331 | free_dir_entry(dir_ent); |
Phillip Lougher | 10d8de0 | 2011-08-29 00:14:48 +0100 | [diff] [blame] | 3332 | continue; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3333 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3334 | |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3335 | if(exclude_actions()) { |
| 3336 | subpath = subpathname(dir_ent); |
| 3337 | |
| 3338 | if(eval_exclude_actions(dir_name, filename, subpath, |
| 3339 | &buf, depth)) { |
| 3340 | add_excluded(dir); |
| 3341 | free_dir_entry(dir_ent); |
| 3342 | continue; |
| 3343 | } |
| 3344 | } |
| 3345 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3346 | if((buf.st_mode & S_IFMT) == S_IFDIR) { |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3347 | if(subpath == NULL) |
| 3348 | subpath = subpathname(dir_ent); |
| 3349 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3350 | sub_dir = dir_scan1(filename, subpath, new, |
| 3351 | scan1_readdir, depth + 1); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3352 | if(sub_dir == NULL) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3353 | free_dir_entry(dir_ent); |
Phillip Lougher | 205c1e0 | 2013-01-07 01:37:56 +0000 | [diff] [blame] | 3354 | free(new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3355 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3356 | } |
Phillip Lougher | 6a78a2d | 2011-12-28 03:54:19 +0000 | [diff] [blame] | 3357 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3358 | dir->directory_count ++; |
| 3359 | } else |
| 3360 | sub_dir = NULL; |
| 3361 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3362 | add_dir_entry(dir_ent, sub_dir, lookup_inode(&buf)); |
Phillip Lougher | 205c1e0 | 2013-01-07 01:37:56 +0000 | [diff] [blame] | 3363 | free(new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | scan1_freedir(dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3367 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3368 | return dir; |
| 3369 | } |
| 3370 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3371 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3372 | /* |
| 3373 | * dir_scan2 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3374 | * This processes most actions and any pseudo files |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3375 | */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3376 | struct dir_ent *scan2_readdir(struct dir_info *dir, struct dir_ent *dir_ent) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3377 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3378 | if (dir_ent == NULL) |
| 3379 | dir_ent = dir->list; |
| 3380 | else |
| 3381 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3382 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3383 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next); |
| 3384 | |
| 3385 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | |
| 3389 | struct dir_ent *scan2_lookup(struct dir_info *dir, char *name) |
| 3390 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3391 | struct dir_ent *dir_ent = dir->list; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3392 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3393 | for(; dir_ent && strcmp(dir_ent->name, name) != 0; |
| 3394 | dir_ent = dir_ent->next); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3395 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3396 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3400 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3401 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3402 | struct dir_ent *dir_ent = NULL; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3403 | struct pseudo_entry *pseudo_ent; |
| 3404 | struct stat buf; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3405 | static int pseudo_ino = 1; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3406 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3407 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3408 | struct inode_info *inode_info = dir_ent->inode; |
| 3409 | struct stat *buf = &inode_info->buf; |
| 3410 | char *name = dir_ent->name; |
| 3411 | |
Phillip Lougher | 89d757c | 2011-09-20 00:33:19 +0100 | [diff] [blame] | 3412 | eval_actions(dir_ent); |
| 3413 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3414 | if((buf->st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3415 | dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) { |
| 3419 | dir_ent = scan2_lookup(dir, pseudo_ent->name); |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3420 | if(pseudo_ent->dev->type == 'm') { |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3421 | struct stat *buf; |
| 3422 | if(dir_ent == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3423 | ERROR_START("Pseudo modify file \"%s\" does " |
| 3424 | "not exist in source filesystem.", |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3425 | pseudo_ent->pathname); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3426 | ERROR_EXIT(" Ignoring.\n"); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3427 | continue; |
| 3428 | } |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3429 | if(dir_ent->inode->root_entry) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3430 | ERROR_START("Pseudo modify file \"%s\" is a " |
| 3431 | "pre-existing file in the filesystem " |
| 3432 | "being appended to. It cannot be "\ |
| 3433 | "modified.", pseudo_ent->pathname); |
| 3434 | ERROR_EXIT(" Ignoring.\n"); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3435 | continue; |
| 3436 | } |
| 3437 | buf = &dir_ent->inode->buf; |
| 3438 | buf->st_mode = (buf->st_mode & S_IFMT) | |
| 3439 | pseudo_ent->dev->mode; |
| 3440 | buf->st_uid = pseudo_ent->dev->uid; |
| 3441 | buf->st_gid = pseudo_ent->dev->gid; |
| 3442 | continue; |
| 3443 | } |
| 3444 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3445 | if(dir_ent) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3446 | if(dir_ent->inode->root_entry) { |
| 3447 | ERROR_START("Pseudo file \"%s\" is a " |
| 3448 | "pre-existing file in the filesystem " |
| 3449 | "being appended to.", |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3450 | pseudo_ent->pathname); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3451 | ERROR_EXIT(" Ignoring.\n"); |
| 3452 | } else { |
| 3453 | ERROR_START("Pseudo file \"%s\" exists in " |
| 3454 | "source filesystem \"%s\".", |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3455 | pseudo_ent->pathname, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3456 | pathname(dir_ent)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3457 | ERROR_EXIT("\nIgnoring, exclude it (-e/-ef) to " |
| 3458 | "override.\n"); |
| 3459 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3460 | continue; |
| 3461 | } |
| 3462 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3463 | memset(&buf, 0, sizeof(buf)); |
| 3464 | buf.st_mode = pseudo_ent->dev->mode; |
| 3465 | buf.st_uid = pseudo_ent->dev->uid; |
| 3466 | buf.st_gid = pseudo_ent->dev->gid; |
| 3467 | buf.st_rdev = makedev(pseudo_ent->dev->major, |
| 3468 | pseudo_ent->dev->minor); |
plougher | 7e58f4d | 2009-04-05 12:06:19 +0000 | [diff] [blame] | 3469 | buf.st_mtime = time(NULL); |
plougher | 1a3fbf2 | 2009-04-05 12:04:16 +0000 | [diff] [blame] | 3470 | buf.st_ino = pseudo_ino ++; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3471 | |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3472 | if(pseudo_ent->dev->type == 'd') { |
| 3473 | struct dir_ent *dir_ent = |
| 3474 | create_dir_entry(pseudo_ent->name, NULL, |
| 3475 | pseudo_ent->pathname, dir); |
| 3476 | char *subpath = strdup(subpathname(dir_ent)); |
| 3477 | struct dir_info *sub_dir = scan1_opendir("", subpath, |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3478 | dir->depth + 1); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3479 | if(sub_dir == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3480 | ERROR_START("Could not create pseudo directory " |
| 3481 | "\"%s\"", pseudo_ent->pathname); |
| 3482 | ERROR_EXIT(", skipping...\n"); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3483 | free(subpath); |
| 3484 | pseudo_ino --; |
| 3485 | continue; |
| 3486 | } |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3487 | dir_scan2(sub_dir, pseudo_ent->pseudo); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3488 | dir->directory_count ++; |
| 3489 | add_dir_entry(dir_ent, sub_dir, |
| 3490 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0)); |
| 3491 | } else if(pseudo_ent->dev->type == 'f') { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3492 | #ifdef USE_TMP_FILE |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3493 | struct stat buf2; |
| 3494 | int res = stat(pseudo_ent->dev->filename, &buf2); |
| 3495 | if(res == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3496 | ERROR_START("Stat on pseudo file \"%s\" failed" |
| 3497 | pseudo_ent->pathname); |
| 3498 | ERROR_EXIT(", skipping...\n"); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3499 | pseudo_ino --; |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3500 | continue; |
| 3501 | } |
| 3502 | buf.st_size = buf2.st_size; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3503 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3504 | pseudo_ent->dev->filename, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3505 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3506 | #else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3507 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3508 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3509 | lookup_inode2(&buf, PSEUDO_FILE_PROCESS, |
| 3510 | pseudo_ent->dev->pseudo_id), dir); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3511 | #endif |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3512 | } else { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3513 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3514 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3515 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3516 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3517 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3521 | /* |
| 3522 | * dir_scan3 routines... |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3523 | * This processes the move action |
| 3524 | */ |
| 3525 | void dir_scan3(struct dir_info *root, struct dir_info *dir) |
| 3526 | { |
| 3527 | struct dir_ent *dir_ent = NULL; |
| 3528 | |
| 3529 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
| 3530 | |
| 3531 | eval_move_actions(root, dir_ent); |
| 3532 | |
| 3533 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
| 3534 | dir_scan3(root, dir_ent->dir); |
| 3535 | } |
| 3536 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3537 | |
| 3538 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3539 | /* |
| 3540 | * dir_scan4 routines... |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3541 | * This processes the empty action. This action has to be processed after |
| 3542 | * all other actions because the previous exclude and move actions and the |
| 3543 | * pseudo actions affect whether a directory is empty |
| 3544 | */ |
| 3545 | void dir_scan4(struct dir_info *dir) |
| 3546 | { |
| 3547 | struct dir_ent *dir_ent = dir->list, *prev = NULL; |
| 3548 | |
| 3549 | while(dir_ent) { |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3550 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) { |
| 3551 | dir_scan4(dir_ent->dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3552 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3553 | if(eval_empty_actions(dir_ent)) { |
| 3554 | struct dir_ent *tmp = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3555 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3556 | /* |
| 3557 | * delete sub-directory, this is by definition |
| 3558 | * empty |
| 3559 | */ |
| 3560 | free(dir_ent->dir->pathname); |
| 3561 | free(dir_ent->dir->subpath); |
| 3562 | free(dir_ent->dir); |
| 3563 | |
| 3564 | /* remove dir_ent from list */ |
| 3565 | dir_ent = dir_ent->next; |
| 3566 | if(prev) |
| 3567 | prev->next = dir_ent; |
| 3568 | else |
| 3569 | dir->list = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3570 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3571 | /* free it */ |
| 3572 | free_dir_entry(tmp); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3573 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3574 | /* update counts */ |
| 3575 | dir->directory_count --; |
| 3576 | dir->count --; |
| 3577 | add_excluded(dir); |
| 3578 | continue; |
| 3579 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3580 | } |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3581 | |
| 3582 | prev = dir_ent; |
| 3583 | dir_ent = dir_ent->next; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3584 | } |
| 3585 | } |
| 3586 | |
| 3587 | |
| 3588 | /* |
| 3589 | * dir_scan5 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3590 | * This sorts every directory and computes the inode numbers |
| 3591 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3592 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3593 | /* |
| 3594 | * Bottom up linked list merge sort. |
| 3595 | * |
| 3596 | * Qsort and other O(n log n) algorithms work well with arrays but not |
| 3597 | * linked lists. Merge sort another O(n log n) sort algorithm on the other hand |
| 3598 | * is not ideal for arrays (as it needs an additonal n storage locations |
| 3599 | * as sorting is not done in place), but it is ideal for linked lists because |
| 3600 | * it doesn't require any extra storage, |
| 3601 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3602 | void sort_directory(struct dir_info *dir) |
| 3603 | { |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3604 | struct dir_ent *cur, *l1, *l2, *next; |
| 3605 | int len1, len2, stride = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3606 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3607 | if(dir->count < 2) |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3608 | return; |
| 3609 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3610 | /* |
| 3611 | * We can consider our linked-list to be made up of stride length |
| 3612 | * sublists. Eacn iteration around this loop merges adjacent |
| 3613 | * stride length sublists into larger 2*stride sublists. We stop |
| 3614 | * when stride becomes equal to the entire list. |
| 3615 | * |
| 3616 | * Initially stride = 1 (by definition a sublist of 1 is sorted), and |
| 3617 | * these 1 element sublists are merged into 2 element sublists, which |
| 3618 | * are then merged into 4 element sublists and so on. |
| 3619 | */ |
| 3620 | do { |
| 3621 | l2 = dir->list; /* head of current linked list */ |
| 3622 | cur = NULL; /* empty output list */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3623 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3624 | /* |
| 3625 | * Iterate through the linked list, merging adjacent sublists. |
| 3626 | * On each interation l2 points to the next sublist pair to be |
| 3627 | * merged (if there's only one sublist left this is simply added |
| 3628 | * to the output list) |
| 3629 | */ |
| 3630 | while(l2) { |
| 3631 | l1 = l2; |
| 3632 | for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next); |
| 3633 | len2 = stride; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3634 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3635 | /* |
| 3636 | * l1 points to first sublist. |
| 3637 | * l2 points to second sublist. |
| 3638 | * Merge them onto the output list |
| 3639 | */ |
| 3640 | while(len1 && l2 && len2) { |
| 3641 | if(strcmp(l1->name, l2->name) <= 0) { |
| 3642 | next = l1; |
| 3643 | l1 = l1->next; |
| 3644 | len1 --; |
| 3645 | } else { |
| 3646 | next = l2; |
| 3647 | l2 = l2->next; |
| 3648 | len2 --; |
| 3649 | } |
| 3650 | |
| 3651 | if(cur) { |
| 3652 | cur->next = next; |
| 3653 | cur = next; |
| 3654 | } else |
| 3655 | dir->list = cur = next; |
| 3656 | } |
| 3657 | /* |
| 3658 | * One sublist is now empty, copy the other one onto the |
| 3659 | * output list |
| 3660 | */ |
| 3661 | for(; len1; len1 --, l1 = l1->next) { |
| 3662 | if(cur) { |
| 3663 | cur->next = l1; |
| 3664 | cur = l1; |
| 3665 | } else |
| 3666 | dir->list = cur = l1; |
| 3667 | } |
| 3668 | for(; l2 && len2; len2 --, l2 = l2->next) { |
| 3669 | if(cur) { |
| 3670 | cur->next = l2; |
| 3671 | cur = l2; |
| 3672 | } else |
| 3673 | dir->list = cur = l2; |
| 3674 | } |
| 3675 | } |
| 3676 | cur->next = NULL; |
| 3677 | stride = stride << 1; |
| 3678 | } while(stride < dir->count); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3682 | void dir_scan5(struct dir_info *dir) |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3683 | { |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3684 | struct dir_ent *dir_ent; |
| 3685 | unsigned int byte_count = 0; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3686 | |
| 3687 | sort_directory(dir); |
| 3688 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3689 | for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) { |
| 3690 | byte_count += strlen(dir_ent->name) + |
| 3691 | sizeof(struct squashfs_dir_entry); |
| 3692 | |
| 3693 | if(dir_ent->inode->root_entry) |
| 3694 | continue; |
| 3695 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3696 | alloc_inode_no(dir_ent->inode, 0); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3697 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3698 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | e69b588 | 2012-11-19 01:31:09 +0000 | [diff] [blame] | 3699 | dir_scan5(dir_ent->dir); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3700 | } |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3701 | |
| 3702 | if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE)) |
| 3703 | dir->dir_is_ldir = FALSE; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | |
| 3707 | /* |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3708 | * dir_scan6 routines... |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3709 | * This generates the filesystem metadata and writes it out to the destination |
| 3710 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3711 | void scan6_init_dir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3712 | { |
| 3713 | dir->buff = malloc(SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3714 | if(dir->buff == NULL) |
| 3715 | MEM_ERROR(); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3716 | |
| 3717 | dir->size = SQUASHFS_METADATA_SIZE; |
| 3718 | dir->p = dir->index_count_p = dir->buff; |
| 3719 | dir->entry_count = 256; |
| 3720 | dir->entry_count_p = NULL; |
| 3721 | dir->index = NULL; |
| 3722 | dir->i_count = dir->i_size = 0; |
| 3723 | } |
| 3724 | |
| 3725 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3726 | struct dir_ent *scan6_readdir(struct directory *dir, struct dir_info *dir_info, |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3727 | struct dir_ent *dir_ent) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3728 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3729 | if (dir_ent == NULL) |
| 3730 | dir_ent = dir_info->list; |
| 3731 | else |
| 3732 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3733 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3734 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next) |
| 3735 | add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number, |
| 3736 | dir_ent->name, dir_ent->inode->type, dir); |
| 3737 | |
| 3738 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3742 | void scan6_freedir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3743 | { |
| 3744 | if(dir->index) |
| 3745 | free(dir->index); |
| 3746 | free(dir->buff); |
| 3747 | } |
| 3748 | |
| 3749 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3750 | void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3751 | { |
| 3752 | int squashfs_type; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3753 | int duplicate_file; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3754 | struct directory dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3755 | struct dir_ent *dir_ent = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3756 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3757 | scan6_init_dir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3758 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3759 | while((dir_ent = scan6_readdir(&dir, dir_info, dir_ent)) != NULL) { |
Phillip Lougher | 0a67088 | 2012-10-05 04:09:13 +0100 | [diff] [blame] | 3760 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3761 | |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 3762 | update_info(dir_ent); |
| 3763 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3764 | if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) { |
| 3765 | switch(buf->st_mode & S_IFMT) { |
| 3766 | case S_IFREG: |
| 3767 | squashfs_type = SQUASHFS_FILE_TYPE; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3768 | write_file(inode, dir_ent, |
| 3769 | &duplicate_file); |
| 3770 | INFO("file %s, uncompressed size %lld " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3771 | "bytes %s\n", |
| 3772 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3773 | (long long) buf->st_size, |
| 3774 | duplicate_file ? "DUPLICATE" : |
| 3775 | ""); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3776 | break; |
| 3777 | |
| 3778 | case S_IFDIR: |
| 3779 | squashfs_type = SQUASHFS_DIR_TYPE; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3780 | dir_scan6(inode, dir_ent->dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3781 | break; |
| 3782 | |
| 3783 | case S_IFLNK: |
| 3784 | squashfs_type = SQUASHFS_SYMLINK_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3785 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3786 | squashfs_type, 0, 0, 0, NULL, |
| 3787 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3788 | INFO("symbolic link %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3789 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3790 | sym_count ++; |
| 3791 | break; |
| 3792 | |
| 3793 | case S_IFCHR: |
| 3794 | squashfs_type = SQUASHFS_CHRDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3795 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3796 | squashfs_type, 0, 0, 0, NULL, |
| 3797 | NULL, NULL, 0); |
| 3798 | INFO("character device %s inode 0x%llx" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3799 | "\n", subpathname(dir_ent), |
| 3800 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3801 | dev_count ++; |
| 3802 | break; |
| 3803 | |
| 3804 | case S_IFBLK: |
| 3805 | squashfs_type = SQUASHFS_BLKDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3806 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3807 | squashfs_type, 0, 0, 0, NULL, |
| 3808 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3809 | INFO("block device %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3810 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3811 | dev_count ++; |
| 3812 | break; |
| 3813 | |
| 3814 | case S_IFIFO: |
| 3815 | squashfs_type = SQUASHFS_FIFO_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3816 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3817 | squashfs_type, 0, 0, 0, NULL, |
| 3818 | NULL, NULL, 0); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3819 | INFO("fifo %s inode 0x%llx\n", |
| 3820 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3821 | fifo_count ++; |
| 3822 | break; |
| 3823 | |
| 3824 | case S_IFSOCK: |
| 3825 | squashfs_type = SQUASHFS_SOCKET_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3826 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3827 | squashfs_type, 0, 0, 0, NULL, |
| 3828 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3829 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3830 | "0x%llx\n", |
| 3831 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3832 | sock_count ++; |
| 3833 | break; |
| 3834 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 3835 | default: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3836 | BAD_ERROR("%s unrecognised file type, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3837 | "mode is %x\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3838 | subpathname(dir_ent), |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3839 | buf->st_mode); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3840 | } |
| 3841 | dir_ent->inode->inode = *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3842 | dir_ent->inode->type = squashfs_type; |
| 3843 | } else { |
| 3844 | *inode = dir_ent->inode->inode; |
| 3845 | squashfs_type = dir_ent->inode->type; |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3846 | switch(squashfs_type) { |
| 3847 | case SQUASHFS_FILE_TYPE: |
| 3848 | if(!sorted) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3849 | INFO("file %s, uncompressed " |
| 3850 | "size %lld bytes LINK" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3851 | "\n", |
| 3852 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3853 | (long long) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3854 | buf->st_size); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3855 | break; |
| 3856 | case SQUASHFS_SYMLINK_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3857 | INFO("symbolic link %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3858 | "LINK\n", subpathname(dir_ent), |
| 3859 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3860 | break; |
| 3861 | case SQUASHFS_CHRDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3862 | INFO("character device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3863 | "LINK\n", subpathname(dir_ent), |
| 3864 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3865 | break; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3866 | case SQUASHFS_BLKDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3867 | INFO("block device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3868 | "LINK\n", subpathname(dir_ent), |
| 3869 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3870 | break; |
| 3871 | case SQUASHFS_FIFO_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3872 | INFO("fifo %s inode 0x%llx LINK\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3873 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3874 | break; |
| 3875 | case SQUASHFS_SOCKET_TYPE: |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3876 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3877 | "0x%llx LINK\n", |
| 3878 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3879 | break; |
| 3880 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Phillip Lougher | 0366aec | 2012-10-05 04:06:04 +0100 | [diff] [blame] | 3883 | add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name, |
| 3884 | squashfs_type, &dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3887 | write_dir(inode, dir_info, &dir); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3888 | INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent), |
| 3889 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3890 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3891 | scan6_freedir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | |
| 3895 | unsigned int slog(unsigned int block) |
| 3896 | { |
| 3897 | int i; |
| 3898 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 3899 | for(i = 12; i <= 20; i++) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3900 | if(block == (1 << i)) |
| 3901 | return i; |
| 3902 | return 0; |
| 3903 | } |
| 3904 | |
| 3905 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3906 | int old_excluded(char *filename, struct stat *buf) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3907 | { |
| 3908 | int i; |
| 3909 | |
| 3910 | for(i = 0; i < exclude; i++) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3911 | if((exclude_paths[i].st_dev == buf->st_dev) && |
| 3912 | (exclude_paths[i].st_ino == buf->st_ino)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3913 | return TRUE; |
| 3914 | return FALSE; |
| 3915 | } |
| 3916 | |
| 3917 | |
| 3918 | #define ADD_ENTRY(buf) \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3919 | if(exclude % EXCLUDE_SIZE == 0) { \ |
| 3920 | exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \ |
| 3921 | * sizeof(struct exclude_info)); \ |
| 3922 | if(exclude_paths == NULL) \ |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3923 | MEM_ERROR(); \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3924 | } \ |
| 3925 | exclude_paths[exclude].st_dev = buf.st_dev; \ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3926 | exclude_paths[exclude++].st_ino = buf.st_ino; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3927 | int old_add_exclude(char *path) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3928 | { |
| 3929 | int i; |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3930 | char *filename; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3931 | struct stat buf; |
| 3932 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3933 | if(path[0] == '/' || strncmp(path, "./", 2) == 0 || |
| 3934 | strncmp(path, "../", 3) == 0) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3935 | if(lstat(path, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3936 | ERROR_START("Cannot stat exclude dir/file %s because " |
| 3937 | "%s", path, strerror(errno)); |
| 3938 | ERROR_EXIT(", ignoring\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3939 | return TRUE; |
| 3940 | } |
| 3941 | ADD_ENTRY(buf); |
| 3942 | return TRUE; |
| 3943 | } |
| 3944 | |
| 3945 | for(i = 0; i < source; i++) { |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3946 | int res = asprintf(&filename, "%s/%s", source_path[i], path); |
| 3947 | if(res == -1) |
| 3948 | BAD_ERROR("asprintf failed in old_add_exclude\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3949 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3950 | if(!(errno == ENOENT || errno == ENOTDIR)) { |
| 3951 | ERROR_START("Cannot stat exclude dir/file %s " |
| 3952 | "because %s", filename, strerror(errno)); |
| 3953 | ERROR_EXIT(", ignoring\n"); |
| 3954 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3955 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3956 | continue; |
| 3957 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3958 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3959 | ADD_ENTRY(buf); |
| 3960 | } |
| 3961 | return TRUE; |
| 3962 | } |
| 3963 | |
| 3964 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3965 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 3966 | int type) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3967 | { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3968 | old_root_entry = realloc(old_root_entry, |
| 3969 | sizeof(struct old_root_entry_info) * (old_root_entries + 1)); |
| 3970 | if(old_root_entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3971 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3972 | |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3973 | old_root_entry[old_root_entries].name = strdup(name); |
| 3974 | old_root_entry[old_root_entries].inode.inode = inode; |
| 3975 | old_root_entry[old_root_entries].inode.inode_number = inode_number; |
| 3976 | old_root_entry[old_root_entries].inode.type = type; |
| 3977 | old_root_entry[old_root_entries++].inode.root_entry = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 3981 | void initialise_threads(int readb_mbytes, int writeb_mbytes, |
Phillip Lougher | c97dac5 | 2013-04-14 03:32:10 +0100 | [diff] [blame] | 3982 | int fragmentb_mbytes, int freelst) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3983 | { |
| 3984 | int i; |
| 3985 | sigset_t sigmask, old_mask; |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 3986 | int reader_buffer_size; |
| 3987 | int fragment_buffer_size; |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 3988 | /* |
| 3989 | * writer_buffer_size is global because it is needed in |
| 3990 | * write_file_blocks_dup() |
| 3991 | */ |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 3992 | |
| 3993 | /* |
| 3994 | * convert from queue size in Mbytes to queue size in |
| 3995 | * blocks. |
| 3996 | * |
| 3997 | * In doing so, check that the user supplied values do not |
| 3998 | * overflow a signed int |
| 3999 | */ |
| 4000 | if(shift_overflow(readb_mbytes, 20 - block_log)) |
| 4001 | BAD_ERROR("Read queue is too large\n"); |
| 4002 | else |
| 4003 | reader_buffer_size = readb_mbytes << (20 - block_log); |
| 4004 | |
| 4005 | if(shift_overflow(fragmentb_mbytes, 20 - block_log)) |
| 4006 | BAD_ERROR("Fragment queue is too large\n"); |
| 4007 | else |
| 4008 | fragment_buffer_size = fragmentb_mbytes << (20 - block_log); |
| 4009 | |
| 4010 | if(shift_overflow(writeb_mbytes, 20 - block_log)) |
| 4011 | BAD_ERROR("Write queue is too large\n"); |
| 4012 | else |
| 4013 | writer_buffer_size = writeb_mbytes << (20 - block_log); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4014 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4015 | /* |
| 4016 | * setup signal handlers for the main thread, these cleanup |
| 4017 | * deleting the destination file, if appending the |
| 4018 | * handlers for SIGTERM and SIGINT will be replaced with handlers |
| 4019 | * allowing the user to press ^C twice to restore the existing |
| 4020 | * filesystem. |
| 4021 | * |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4022 | * SIGUSR1 is an internal signal, which is used by the sub-threads |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4023 | * to tell the main thread to terminate, deleting the destination file, |
| 4024 | * or if necessary restoring the filesystem on appending |
| 4025 | */ |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 4026 | signal(SIGTERM, sighandler); |
| 4027 | signal(SIGINT, sighandler); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4028 | signal(SIGUSR1, sighandler); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4029 | |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 4030 | /* block SIGQUIT and SIGHUP, these are handled by the info thread */ |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4031 | sigemptyset(&sigmask); |
| 4032 | sigaddset(&sigmask, SIGQUIT); |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 4033 | sigaddset(&sigmask, SIGHUP); |
Phillip Lougher | b1259f7 | 2013-06-06 03:10:15 +0100 | [diff] [blame] | 4034 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4035 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4036 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4037 | /* |
| 4038 | * temporarily block these signals, so the created sub-threads |
| 4039 | * will ignore them, ensuring the main thread handles them |
| 4040 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4041 | sigemptyset(&sigmask); |
| 4042 | sigaddset(&sigmask, SIGINT); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4043 | sigaddset(&sigmask, SIGTERM); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4044 | sigaddset(&sigmask, SIGUSR1); |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4045 | if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4046 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4047 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4048 | if(processors == -1) { |
| 4049 | #ifndef linux |
| 4050 | int mib[2]; |
| 4051 | size_t len = sizeof(processors); |
| 4052 | |
| 4053 | mib[0] = CTL_HW; |
| 4054 | #ifdef HW_AVAILCPU |
| 4055 | mib[1] = HW_AVAILCPU; |
| 4056 | #else |
| 4057 | mib[1] = HW_NCPU; |
| 4058 | #endif |
| 4059 | |
| 4060 | if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 4061 | ERROR_START("Failed to get number of available " |
| 4062 | "processors."); |
| 4063 | ERROR_EXIT(" Defaulting to 1\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4064 | processors = 1; |
| 4065 | } |
| 4066 | #else |
plougher | 9cc26b7 | 2010-08-17 01:05:55 +0000 | [diff] [blame] | 4067 | processors = sysconf(_SC_NPROCESSORS_ONLN); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4068 | #endif |
| 4069 | } |
| 4070 | |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4071 | if(multiply_overflow(processors, 2) || |
| 4072 | add_overflow(processors * 2, 2) || |
| 4073 | multiply_overflow(processors * 2 + 2, |
| 4074 | sizeof(pthread_t))) |
| 4075 | BAD_ERROR("Processors too large\n"); |
| 4076 | |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4077 | deflator_thread = malloc(processors * 2 * sizeof(pthread_t)); |
| 4078 | if(deflator_thread == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4079 | MEM_ERROR(); |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4080 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4081 | frag_deflator_thread = &deflator_thread[processors]; |
| 4082 | |
| 4083 | to_reader = queue_init(1); |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 4084 | to_deflate = queue_init(reader_buffer_size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4085 | to_writer = queue_init(writer_buffer_size); |
| 4086 | from_writer = queue_init(1); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 4087 | to_frag = queue_init(fragment_buffer_size); |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 4088 | locked_fragment = queue_init(fragment_buffer_size); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 4089 | to_main = seq_queue_init(); |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 4090 | reader_buffer = cache_init(block_size, reader_buffer_size, 0, 0); |
| 4091 | writer_buffer = cache_init(block_size, writer_buffer_size, 1, freelst); |
| 4092 | fragment_buffer = cache_init(block_size, fragment_buffer_size, 1, |
| 4093 | freelst); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4094 | pthread_create(&reader_thread, NULL, reader, NULL); |
| 4095 | pthread_create(&writer_thread, NULL, writer, NULL); |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 4096 | init_progress_bar(); |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4097 | init_info(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4098 | pthread_mutex_init(&fragment_mutex, NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4099 | |
| 4100 | for(i = 0; i < processors; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4101 | if(pthread_create(&deflator_thread[i], NULL, deflator, NULL) != |
| 4102 | 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4103 | BAD_ERROR("Failed to create thread\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4104 | if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator, |
| 4105 | NULL) != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4106 | BAD_ERROR("Failed to create thread\n"); |
| 4107 | } |
| 4108 | |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4109 | main_thread = pthread_self(); |
| 4110 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4111 | printf("Parallel mksquashfs: Using %d processor%s\n", processors, |
| 4112 | processors == 1 ? "" : "s"); |
| 4113 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4114 | /* Restore the signal mask for the main thread */ |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4115 | if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4116 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4117 | } |
| 4118 | |
| 4119 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4120 | long long write_inode_lookup_table() |
| 4121 | { |
| 4122 | int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4123 | void *it; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4124 | |
| 4125 | if(inode_count == sinode_count) |
| 4126 | goto skip_inode_hash_table; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4127 | |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4128 | it = realloc(inode_lookup_table, lookup_bytes); |
| 4129 | if(it == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4130 | MEM_ERROR(); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4131 | inode_lookup_table = it; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4132 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4133 | for(i = 0; i < INODE_HASH_SIZE; i ++) { |
| 4134 | struct inode_info *inode = inode_info[i]; |
| 4135 | |
| 4136 | for(inode = inode_info[i]; inode; inode = inode->next) { |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4137 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4138 | inode_number = get_inode_no(inode); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4139 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4140 | SQUASHFS_SWAP_LONG_LONGS(&inode->inode, |
| 4141 | &inode_lookup_table[inode_number - 1], 1); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4142 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4143 | } |
| 4144 | } |
| 4145 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4146 | skip_inode_hash_table: |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 4147 | return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL, |
| 4148 | noI); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4151 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4152 | char *get_component(char *target, char **targname) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4153 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4154 | char *start; |
| 4155 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4156 | while(*target == '/') |
rlougher | c4ebcf5 | 2007-11-08 17:52:49 +0000 | [diff] [blame] | 4157 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4158 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4159 | start = target; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4160 | while(*target != '/' && *target!= '\0') |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4161 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4162 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4163 | *targname = strndup(start, target - start); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4164 | |
| 4165 | return target; |
| 4166 | } |
| 4167 | |
| 4168 | |
| 4169 | void free_path(struct pathname *paths) |
| 4170 | { |
| 4171 | int i; |
| 4172 | |
| 4173 | for(i = 0; i < paths->names; i++) { |
| 4174 | if(paths->name[i].paths) |
| 4175 | free_path(paths->name[i].paths); |
| 4176 | free(paths->name[i].name); |
| 4177 | if(paths->name[i].preg) { |
| 4178 | regfree(paths->name[i].preg); |
| 4179 | free(paths->name[i].preg); |
| 4180 | } |
| 4181 | } |
| 4182 | |
| 4183 | free(paths); |
| 4184 | } |
| 4185 | |
| 4186 | |
| 4187 | struct pathname *add_path(struct pathname *paths, char *target, char *alltarget) |
| 4188 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4189 | char *targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4190 | int i, error; |
| 4191 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4192 | target = get_component(target, &targname); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4193 | |
| 4194 | if(paths == NULL) { |
plougher | dec6ef1 | 2010-12-18 02:47:53 +0000 | [diff] [blame] | 4195 | paths = malloc(sizeof(struct pathname)); |
| 4196 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4197 | MEM_ERROR(); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4198 | |
| 4199 | paths->names = 0; |
| 4200 | paths->name = NULL; |
| 4201 | } |
| 4202 | |
| 4203 | for(i = 0; i < paths->names; i++) |
| 4204 | if(strcmp(paths->name[i].name, targname) == 0) |
| 4205 | break; |
| 4206 | |
| 4207 | if(i == paths->names) { |
| 4208 | /* allocate new name entry */ |
| 4209 | paths->names ++; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4210 | paths->name = realloc(paths->name, (i + 1) * |
| 4211 | sizeof(struct path_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4212 | if(paths->name == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4213 | MEM_ERROR(); |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4214 | paths->name[i].name = targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4215 | paths->name[i].paths = NULL; |
| 4216 | if(use_regex) { |
| 4217 | paths->name[i].preg = malloc(sizeof(regex_t)); |
plougher | 5c60eab | 2010-07-21 01:12:19 +0000 | [diff] [blame] | 4218 | if(paths->name[i].preg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4219 | MEM_ERROR(); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4220 | error = regcomp(paths->name[i].preg, targname, |
| 4221 | REG_EXTENDED|REG_NOSUB); |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4222 | if(error) { |
Phillip Lougher | 62859d2 | 2012-11-30 05:53:56 +0000 | [diff] [blame] | 4223 | char str[1024]; /* overflow safe */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4224 | |
| 4225 | regerror(error, paths->name[i].preg, str, 1024); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4226 | BAD_ERROR("invalid regex %s in export %s, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4227 | "because %s\n", targname, alltarget, |
| 4228 | str); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4229 | } |
| 4230 | } else |
| 4231 | paths->name[i].preg = NULL; |
| 4232 | |
| 4233 | if(target[0] == '\0') |
| 4234 | /* at leaf pathname component */ |
| 4235 | paths->name[i].paths = NULL; |
| 4236 | else |
| 4237 | /* recurse adding child components */ |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4238 | paths->name[i].paths = add_path(NULL, target, |
| 4239 | alltarget); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4240 | } else { |
| 4241 | /* existing matching entry */ |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4242 | free(targname); |
| 4243 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4244 | if(paths->name[i].paths == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4245 | /* No sub-directory which means this is the leaf |
| 4246 | * component of a pre-existing exclude which subsumes |
| 4247 | * the exclude currently being added, in which case stop |
| 4248 | * adding components */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4249 | } else if(target[0] == '\0') { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4250 | /* at leaf pathname component and child components exist |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4251 | * from more specific excludes, delete as they're |
| 4252 | * subsumed by this exclude */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4253 | free_path(paths->name[i].paths); |
| 4254 | paths->name[i].paths = NULL; |
| 4255 | } else |
| 4256 | /* recurse adding child components */ |
| 4257 | add_path(paths->name[i].paths, target, alltarget); |
| 4258 | } |
| 4259 | |
| 4260 | return paths; |
| 4261 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4262 | |
| 4263 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4264 | void add_exclude(char *target) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4265 | { |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4266 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4267 | if(target[0] == '/' || strncmp(target, "./", 2) == 0 || |
| 4268 | strncmp(target, "../", 3) == 0) |
| 4269 | BAD_ERROR("/, ./ and ../ prefixed excludes not supported with " |
| 4270 | "-wildcards or -regex options\n"); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4271 | else if(strncmp(target, "... ", 4) == 0) |
| 4272 | stickypath = add_path(stickypath, target + 4, target + 4); |
| 4273 | else |
| 4274 | path = add_path(path, target, target); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | |
| 4278 | void display_path(int depth, struct pathname *paths) |
| 4279 | { |
| 4280 | int i, n; |
| 4281 | |
| 4282 | if(paths == NULL) |
| 4283 | return; |
| 4284 | |
| 4285 | for(i = 0; i < paths->names; i++) { |
| 4286 | for(n = 0; n < depth; n++) |
| 4287 | printf("\t"); |
| 4288 | printf("%d: %s\n", depth, paths->name[i].name); |
| 4289 | display_path(depth + 1, paths->name[i].paths); |
| 4290 | } |
| 4291 | } |
| 4292 | |
| 4293 | |
| 4294 | void display_path2(struct pathname *paths, char *string) |
| 4295 | { |
| 4296 | int i; |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4297 | char *path; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4298 | |
| 4299 | if(paths == NULL) { |
| 4300 | printf("%s\n", string); |
| 4301 | return; |
| 4302 | } |
| 4303 | |
| 4304 | for(i = 0; i < paths->names; i++) { |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4305 | int res = asprintf(&path, "%s/%s", string, paths->name[i].name); |
| 4306 | if(res == -1) |
| 4307 | BAD_ERROR("asprintf failed in display_path2\n"); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4308 | display_path2(paths->name[i].paths, path); |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4309 | free(path); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4310 | } |
| 4311 | } |
| 4312 | |
| 4313 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4314 | struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path) |
| 4315 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4316 | int count = paths == NULL ? 0 : paths->count; |
| 4317 | |
| 4318 | if(count % PATHS_ALLOC_SIZE == 0) { |
| 4319 | paths = realloc(paths, sizeof(struct pathnames) + |
| 4320 | (count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4321 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4322 | MEM_ERROR(); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4323 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4324 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4325 | paths->path[count] = path; |
| 4326 | paths->count = count + 1; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4327 | return paths; |
| 4328 | } |
| 4329 | |
| 4330 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4331 | int excluded_match(char *name, struct pathname *path, struct pathnames **new) |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4332 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4333 | int i; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4334 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4335 | for(i = 0; i < path->names; i++) { |
| 4336 | int match = use_regex ? |
| 4337 | regexec(path->name[i].preg, name, (size_t) 0, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4338 | NULL, 0) == 0 : |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4339 | fnmatch(path->name[i].name, name, |
| 4340 | FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4341 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4342 | if(match) { |
| 4343 | if(path->name[i].paths == NULL || new == NULL) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4344 | /* match on a leaf component, any subdirectories |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4345 | * in the filesystem should be excluded */ |
| 4346 | return TRUE; |
| 4347 | else |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4348 | /* match on a non-leaf component, add any |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4349 | * subdirectories to the new set of |
| 4350 | * subdirectories to scan for this name */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4351 | *new = add_subdir(*new, path->name[i].paths); |
| 4352 | } |
| 4353 | } |
| 4354 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4355 | return FALSE; |
| 4356 | } |
| 4357 | |
| 4358 | |
| 4359 | int excluded(char *name, struct pathnames *paths, struct pathnames **new) |
| 4360 | { |
| 4361 | int n; |
| 4362 | |
| 4363 | if(stickypath && excluded_match(name, stickypath, NULL)) |
| 4364 | return TRUE; |
| 4365 | |
| 4366 | for(n = 0; paths && n < paths->count; n++) { |
| 4367 | int res = excluded_match(name, paths->path[n], new); |
| 4368 | if(res) { |
| 4369 | free(*new); |
| 4370 | *new = NULL; |
| 4371 | return TRUE; |
| 4372 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4375 | /* |
| 4376 | * Either: |
| 4377 | * - no matching names found, return empty new search set, or |
| 4378 | * - one or more matches with sub-directories found (no leaf matches), |
| 4379 | * in which case return new search set. |
| 4380 | * |
| 4381 | * In either case return FALSE as we don't want to exclude this entry |
| 4382 | */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4383 | return FALSE; |
| 4384 | } |
| 4385 | |
| 4386 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4387 | void process_exclude_file(char *argv) |
| 4388 | { |
| 4389 | FILE *fd; |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4390 | char buffer[MAX_LINE + 1]; /* overflow safe */ |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4391 | char *filename; |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4392 | |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4393 | fd = fopen(argv, "r"); |
| 4394 | if(fd == NULL) |
| 4395 | BAD_ERROR("Failed to open exclude file \"%s\" because %s\n", |
| 4396 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4397 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4398 | while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) { |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4399 | int len = strlen(filename); |
| 4400 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4401 | if(len == MAX_LINE && filename[len - 1] != '\n') |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4402 | /* line too large */ |
| 4403 | BAD_ERROR("Line too long when reading " |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4404 | "exclude file \"%s\", larger than %d " |
Phillip Lougher | 83847c1 | 2012-12-23 07:22:36 +0000 | [diff] [blame] | 4405 | "bytes\n", argv, MAX_LINE); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4406 | |
| 4407 | /* |
| 4408 | * Remove '\n' terminator if it exists (the last line |
| 4409 | * in the file may not be '\n' terminated) |
| 4410 | */ |
| 4411 | if(len && filename[len - 1] == '\n') |
| 4412 | filename[len - 1] = '\0'; |
| 4413 | |
| 4414 | /* Skip any leading whitespace */ |
| 4415 | while(isspace(*filename)) |
| 4416 | filename ++; |
| 4417 | |
| 4418 | /* if comment line, skip */ |
| 4419 | if(*filename == '#') |
| 4420 | continue; |
| 4421 | |
| 4422 | /* |
| 4423 | * check for initial backslash, to accommodate |
| 4424 | * filenames with leading space or leading # character |
| 4425 | */ |
| 4426 | if(*filename == '\\') |
| 4427 | filename ++; |
| 4428 | |
| 4429 | /* if line is now empty after skipping characters, skip it */ |
| 4430 | if(*filename == '\0') |
| 4431 | continue; |
| 4432 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4433 | if(old_exclude) |
| 4434 | old_add_exclude(filename); |
| 4435 | else |
| 4436 | add_exclude(filename); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | if(ferror(fd)) |
| 4440 | BAD_ERROR("Reading exclude file \"%s\" failed because %s\n", |
| 4441 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4442 | |
| 4443 | fclose(fd); |
| 4444 | } |
| 4445 | |
| 4446 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4447 | #define RECOVER_ID "Squashfs recovery file v1.0\n" |
| 4448 | #define RECOVER_ID_SIZE 28 |
| 4449 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4450 | void write_recovery_data(struct squashfs_super_block *sBlk) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4451 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4452 | int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4453 | pid_t pid = getpid(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4454 | char *metadata; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4455 | char header[] = RECOVER_ID; |
| 4456 | |
| 4457 | if(recover == FALSE) { |
| 4458 | printf("No recovery data option specified.\n"); |
plougher | eac1853 | 2007-10-29 05:26:06 +0000 | [diff] [blame] | 4459 | printf("Skipping saving recovery file.\n\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4460 | return; |
| 4461 | } |
| 4462 | |
plougher | 1b879bc | 2010-12-18 02:49:42 +0000 | [diff] [blame] | 4463 | metadata = malloc(bytes); |
| 4464 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4465 | MEM_ERROR(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4466 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4467 | res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata); |
Phillip Lougher | 477f433 | 2013-03-06 03:55:08 +0000 | [diff] [blame] | 4468 | if(res == 0) { |
| 4469 | ERROR("Failed to read append filesystem metadata\n"); |
| 4470 | BAD_ERROR("Filesystem corrupted?\n"); |
| 4471 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4472 | |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4473 | res = asprintf(&recovery_file, "squashfs_recovery_%s_%d", |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4474 | getbase(destination_file), pid); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4475 | if(res == -1) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4476 | MEM_ERROR(); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4477 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4478 | recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU); |
| 4479 | if(recoverfd == -1) |
| 4480 | BAD_ERROR("Failed to create recovery file, because %s. " |
| 4481 | "Aborting\n", strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4482 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4483 | if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4484 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4485 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4486 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4487 | if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4488 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4489 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4490 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4491 | if(write_bytes(recoverfd, metadata, bytes) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4492 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4493 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4494 | |
| 4495 | close(recoverfd); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4496 | free(metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4497 | |
| 4498 | printf("Recovery file \"%s\" written\n", recovery_file); |
| 4499 | printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4500 | printf("mksquashfs dummy %s -recover %s\n", destination_file, |
| 4501 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4502 | printf("to restore filesystem\n\n"); |
| 4503 | } |
| 4504 | |
| 4505 | |
| 4506 | void read_recovery_data(char *recovery_file, char *destination_file) |
| 4507 | { |
| 4508 | int fd, recoverfd, bytes; |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4509 | struct squashfs_super_block orig_sBlk, sBlk; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4510 | char *metadata; |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4511 | int res; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4512 | struct stat buf; |
| 4513 | char header[] = RECOVER_ID; |
| 4514 | char header2[RECOVER_ID_SIZE]; |
| 4515 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4516 | recoverfd = open(recovery_file, O_RDONLY); |
| 4517 | if(recoverfd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4518 | BAD_ERROR("Failed to open recovery file because %s\n", |
| 4519 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4520 | |
| 4521 | if(stat(destination_file, &buf) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4522 | BAD_ERROR("Failed to stat destination file, because %s\n", |
| 4523 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4524 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4525 | fd = open(destination_file, O_RDWR); |
| 4526 | if(fd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4527 | BAD_ERROR("Failed to open destination file because %s\n", |
| 4528 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4529 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4530 | res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE); |
| 4531 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4532 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4533 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4534 | if(res < RECOVER_ID_SIZE) |
| 4535 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4536 | if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 ) |
| 4537 | BAD_ERROR("Not a recovery file\n"); |
| 4538 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4539 | res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4540 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4541 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4542 | strerror(errno)); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4543 | if(res < sizeof(struct squashfs_super_block)) |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4544 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4545 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4546 | res = read_fs_bytes(fd, 0, sizeof(struct squashfs_super_block), &orig_sBlk); |
Phillip Lougher | 477f433 | 2013-03-06 03:55:08 +0000 | [diff] [blame] | 4547 | if(res == 0) { |
| 4548 | ERROR("Failed to read superblock from output filesystem\n"); |
| 4549 | BAD_ERROR("Output filesystem is empty!\n"); |
| 4550 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4551 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4552 | if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4, |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4553 | sizeof(struct squashfs_super_block) - 4) != 0) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4554 | BAD_ERROR("Recovery file and destination file do not seem to " |
| 4555 | "match\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4556 | |
| 4557 | bytes = sBlk.bytes_used - sBlk.inode_table_start; |
| 4558 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4559 | metadata = malloc(bytes); |
| 4560 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4561 | MEM_ERROR(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4562 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4563 | res = read_bytes(recoverfd, metadata, bytes); |
| 4564 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4565 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4566 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4567 | if(res < bytes) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4568 | BAD_ERROR("Recovery file appears to be truncated\n"); |
| 4569 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4570 | write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4571 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 4572 | write_destination(fd, sBlk.inode_table_start, bytes, metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4573 | |
| 4574 | close(recoverfd); |
| 4575 | close(fd); |
| 4576 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4577 | printf("Successfully wrote recovery file \"%s\". Exiting\n", |
| 4578 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4579 | |
| 4580 | exit(0); |
| 4581 | } |
| 4582 | |
| 4583 | |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4584 | int parse_number(char *start, int *res, int size) |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4585 | { |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4586 | char *end; |
| 4587 | long number = strtol(start, &end, 10); |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4588 | |
Phillip Lougher | f4f4838 | 2012-11-29 03:52:44 +0000 | [diff] [blame] | 4589 | /* |
| 4590 | * check for strtol underflow or overflow in conversion. |
| 4591 | * Note: strtol can validly return LONG_MIN and LONG_MAX |
| 4592 | * if the user entered these values, but, additional code |
| 4593 | * to distinguish this scenario is unnecessary, because for |
| 4594 | * our purposes LONG_MIN and LONG_MAX are too large anyway |
| 4595 | */ |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4596 | if(number == LONG_MIN || number == LONG_MAX) |
| 4597 | return 0; |
| 4598 | |
| 4599 | /* reject negative numbers as invalid */ |
| 4600 | if(number < 0) |
| 4601 | return 0; |
| 4602 | |
| 4603 | /* check if long result will overflow signed int */ |
| 4604 | if(number > INT_MAX) |
| 4605 | return 0; |
| 4606 | |
| 4607 | if(size) { |
Phillip Lougher | ed34569 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 4608 | /* |
| 4609 | * Check for multiplier and trailing junk. |
| 4610 | * But first check that a number exists before the |
| 4611 | * multiplier |
| 4612 | */ |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4613 | if(end == start) |
Phillip Lougher | ed34569 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 4614 | return 0; |
| 4615 | |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4616 | switch(end[0]) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4617 | case 'm': |
| 4618 | case 'M': |
| 4619 | if(multiply_overflow((int) number, 1048576)) |
| 4620 | return 0; |
| 4621 | number *= 1048576; |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4622 | |
Phillip Lougher | f968be8 | 2012-12-04 05:12:32 +0000 | [diff] [blame] | 4623 | if(end[1] != '\0') |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4624 | /* trailing junk after number */ |
| 4625 | return 0; |
| 4626 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4627 | break; |
| 4628 | case 'k': |
| 4629 | case 'K': |
| 4630 | if(multiply_overflow((int) number, 1024)) |
| 4631 | return 0; |
| 4632 | number *= 1024; |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4633 | |
Phillip Lougher | f968be8 | 2012-12-04 05:12:32 +0000 | [diff] [blame] | 4634 | if(end[1] != '\0') |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4635 | /* trailing junk after number */ |
| 4636 | return 0; |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4637 | case '\0': |
| 4638 | break; |
| 4639 | default: |
| 4640 | /* trailing junk after number */ |
| 4641 | return 0; |
| 4642 | } |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4643 | } else if(end[0] != '\0') |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4644 | /* trailing junk after number */ |
| 4645 | return 0; |
| 4646 | |
| 4647 | *res = number; |
| 4648 | return 1; |
| 4649 | } |
| 4650 | |
| 4651 | |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4652 | void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad) |
| 4653 | { |
| 4654 | int i; |
| 4655 | |
| 4656 | sBlk->fragments = fragments; |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4657 | sBlk->no_ids = id_count; |
| 4658 | sBlk->inode_table_start = write_inodes(); |
| 4659 | sBlk->directory_table_start = write_directories(); |
| 4660 | sBlk->fragment_table_start = write_fragment_table(); |
| 4661 | sBlk->lookup_table_start = exportable ? write_inode_lookup_table() : |
| 4662 | SQUASHFS_INVALID_BLK; |
| 4663 | sBlk->id_table_start = write_id_table(); |
| 4664 | sBlk->xattr_id_table_start = write_xattrs(); |
| 4665 | |
| 4666 | TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start); |
| 4667 | TRACE("sBlk->directory_table_start 0x%llx\n", |
| 4668 | sBlk->directory_table_start); |
| 4669 | TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start); |
| 4670 | if(exportable) |
| 4671 | TRACE("sBlk->lookup_table_start 0x%llx\n", |
| 4672 | sBlk->lookup_table_start); |
| 4673 | |
| 4674 | sBlk->bytes_used = bytes; |
| 4675 | |
| 4676 | sBlk->compression = comp->id; |
| 4677 | |
| 4678 | SQUASHFS_INSWAP_SUPER_BLOCK(sBlk); |
| 4679 | write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk); |
| 4680 | |
| 4681 | if(!nopad && (i = bytes & (4096 - 1))) { |
| 4682 | char temp[4096] = {0}; |
| 4683 | write_destination(fd, bytes, 4096 - i, temp); |
| 4684 | } |
| 4685 | |
| 4686 | close(fd); |
| 4687 | |
| 4688 | delete_pseudo_files(); |
| 4689 | |
| 4690 | if(recovery_file) |
| 4691 | unlink(recovery_file); |
| 4692 | |
| 4693 | total_bytes += total_inode_bytes + total_directory_bytes + |
| 4694 | sizeof(struct squashfs_super_block) + total_xattr_bytes; |
| 4695 | |
| 4696 | printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size" |
| 4697 | " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR, |
| 4698 | SQUASHFS_MINOR, comp->name, block_size); |
| 4699 | printf("\t%s data, %s metadata, %s fragments, %s xattrs\n", |
| 4700 | noD ? "uncompressed" : "compressed", noI ? "uncompressed" : |
| 4701 | "compressed", no_fragments ? "no" : noF ? "uncompressed" : |
| 4702 | "compressed", no_xattrs ? "no" : noX ? "uncompressed" : |
| 4703 | "compressed"); |
| 4704 | printf("\tduplicates are %sremoved\n", duplicate_checking ? "" : |
| 4705 | "not "); |
| 4706 | printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, |
| 4707 | bytes / (1024.0 * 1024.0)); |
| 4708 | printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n", |
| 4709 | ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0); |
| 4710 | printf("Inode table size %d bytes (%.2f Kbytes)\n", |
| 4711 | inode_bytes, inode_bytes / 1024.0); |
| 4712 | printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n", |
| 4713 | ((float) inode_bytes / total_inode_bytes) * 100.0, |
| 4714 | total_inode_bytes); |
| 4715 | printf("Directory table size %d bytes (%.2f Kbytes)\n", |
| 4716 | directory_bytes, directory_bytes / 1024.0); |
| 4717 | printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n", |
| 4718 | ((float) directory_bytes / total_directory_bytes) * 100.0, |
| 4719 | total_directory_bytes); |
| 4720 | if(total_xattr_bytes) { |
| 4721 | printf("Xattr table size %d bytes (%.2f Kbytes)\n", |
| 4722 | xattr_bytes, xattr_bytes / 1024.0); |
| 4723 | printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n", |
| 4724 | ((float) xattr_bytes / total_xattr_bytes) * 100.0, |
| 4725 | total_xattr_bytes); |
| 4726 | } |
| 4727 | if(duplicate_checking) |
| 4728 | printf("Number of duplicate files found %d\n", file_count - |
| 4729 | dup_files); |
| 4730 | else |
| 4731 | printf("No duplicate files removed\n"); |
| 4732 | printf("Number of inodes %d\n", inode_count); |
| 4733 | printf("Number of files %d\n", file_count); |
| 4734 | if(!no_fragments) |
| 4735 | printf("Number of fragments %d\n", fragments); |
| 4736 | printf("Number of symbolic links %d\n", sym_count); |
| 4737 | printf("Number of device nodes %d\n", dev_count); |
| 4738 | printf("Number of fifo nodes %d\n", fifo_count); |
| 4739 | printf("Number of socket nodes %d\n", sock_count); |
| 4740 | printf("Number of directories %d\n", dir_count); |
| 4741 | printf("Number of ids (unique uids + gids) %d\n", id_count); |
| 4742 | printf("Number of uids %d\n", uid_count); |
| 4743 | |
| 4744 | for(i = 0; i < id_count; i++) { |
| 4745 | if(id_table[i]->flags & ISA_UID) { |
| 4746 | struct passwd *user = getpwuid(id_table[i]->id); |
| 4747 | printf("\t%s (%d)\n", user == NULL ? "unknown" : |
| 4748 | user->pw_name, id_table[i]->id); |
| 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | printf("Number of gids %d\n", guid_count); |
| 4753 | |
| 4754 | for(i = 0; i < id_count; i++) { |
| 4755 | if(id_table[i]->flags & ISA_GID) { |
| 4756 | struct group *group = getgrgid(id_table[i]->id); |
| 4757 | printf("\t%s (%d)\n", group == NULL ? "unknown" : |
| 4758 | group->gr_name, id_table[i]->id); |
| 4759 | } |
| 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4764 | int parse_num(char *arg, int *res) |
| 4765 | { |
| 4766 | return parse_number(arg, res, 0); |
| 4767 | } |
| 4768 | |
| 4769 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4770 | #define VERSION() \ |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 4771 | printf("mksquashfs version 4.2-git (2014/01/29)\n");\ |
Phillip Lougher | cc06495 | 2014-01-03 04:26:34 +0000 | [diff] [blame] | 4772 | printf("copyright (C) 2014 Phillip Lougher "\ |
Phillip Lougher | 83d42a3 | 2012-10-31 23:42:22 +0000 | [diff] [blame] | 4773 | "<phillip@squashfs.org.uk>\n\n"); \ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4774 | printf("This program is free software; you can redistribute it and/or"\ |
| 4775 | "\n");\ |
| 4776 | printf("modify it under the terms of the GNU General Public License"\ |
| 4777 | "\n");\ |
| 4778 | printf("as published by the Free Software Foundation; either version "\ |
| 4779 | "2,\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4780 | printf("or (at your option) any later version.\n\n");\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4781 | printf("This program is distributed in the hope that it will be "\ |
| 4782 | "useful,\n");\ |
| 4783 | printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\ |
| 4784 | "of\n");\ |
| 4785 | printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\ |
| 4786 | "\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4787 | printf("GNU General Public License for more details.\n"); |
| 4788 | int main(int argc, char *argv[]) |
| 4789 | { |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4790 | struct stat buf, source_buf; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 4791 | int res, i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4792 | char *b, *root_name = NULL; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 4793 | int keep_as_directory = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4794 | squashfs_inode inode; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4795 | int readb_mbytes = READER_BUFFER_DEFAULT, |
| 4796 | writeb_mbytes = WRITER_BUFFER_DEFAULT, |
| 4797 | fragmentb_mbytes = FRAGMENT_BUFFER_DEFAULT; |
Phillip Lougher | 569a963 | 2013-04-22 03:30:30 +0100 | [diff] [blame] | 4798 | int progress = TRUE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 4799 | int force_progress = FALSE; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 4800 | struct file_buffer **fragment = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4801 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4802 | block_log = slog(block_size); |
| 4803 | if(argc > 1 && strcmp(argv[1], "-version") == 0) { |
| 4804 | VERSION(); |
| 4805 | exit(0); |
| 4806 | } |
| 4807 | for(i = 1; i < argc && argv[i][0] != '-'; i++); |
| 4808 | if(i < 3) |
| 4809 | goto printOptions; |
| 4810 | source_path = argv + 1; |
| 4811 | source = i - 2; |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4812 | |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 4813 | /* |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4814 | * Scan the command line for -comp xxx option, this is to ensure |
| 4815 | * any -X compressor specific options are passed to the |
| 4816 | * correct compressor |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 4817 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4818 | for(; i < argc; i++) { |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4819 | struct compressor *prev_comp = comp; |
| 4820 | |
| 4821 | if(strcmp(argv[i], "-comp") == 0) { |
| 4822 | if(++i == argc) { |
| 4823 | ERROR("%s: -comp missing compression type\n", |
| 4824 | argv[0]); |
| 4825 | exit(1); |
| 4826 | } |
| 4827 | comp = lookup_compressor(argv[i]); |
| 4828 | if(!comp->supported) { |
| 4829 | ERROR("%s: Compressor \"%s\" is not supported!" |
| 4830 | "\n", argv[0], argv[i]); |
| 4831 | ERROR("%s: Compressors available:\n", argv[0]); |
| 4832 | display_compressors("", COMP_DEFAULT); |
| 4833 | exit(1); |
| 4834 | } |
| 4835 | if(prev_comp != NULL && prev_comp != comp) { |
| 4836 | ERROR("%s: -comp multiple conflicting -comp" |
| 4837 | " options specified on command line" |
| 4838 | ", previously %s, now %s\n", argv[0], |
| 4839 | prev_comp->name, comp->name); |
| 4840 | exit(1); |
| 4841 | } |
| 4842 | compressor_opt_parsed = 1; |
| 4843 | |
| 4844 | } else if(strcmp(argv[i], "-e") == 0) |
| 4845 | break; |
| 4846 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
| 4847 | strcmp(argv[i], "-ef") == 0 || |
| 4848 | strcmp(argv[i], "-pf") == 0 || |
| 4849 | strcmp(argv[i], "-af") == 0 || |
| 4850 | strcmp(argv[i], "-comp") == 0) |
| 4851 | i++; |
| 4852 | } |
| 4853 | |
| 4854 | /* |
| 4855 | * if no -comp option specified lookup default compressor. Note the |
| 4856 | * Makefile ensures the default compressor has been built, and so we |
| 4857 | * don't need to to check for failure here |
| 4858 | */ |
| 4859 | if(comp == NULL) |
| 4860 | comp = lookup_compressor(COMP_DEFAULT); |
| 4861 | |
| 4862 | for(i = source + 2; i < argc; i++) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 4863 | if(strcmp(argv[i], "-action") == 0 || |
| 4864 | strcmp(argv[i], "-a") ==0) { |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 4865 | if(++i == argc) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 4866 | ERROR("%s: %s missing action\n", |
| 4867 | argv[0], argv[i - 1]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 4868 | exit(1); |
| 4869 | } |
| 4870 | res = parse_action(argv[i]); |
| 4871 | if(res == 0) |
| 4872 | exit(1); |
| 4873 | |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 4874 | } else if(strcmp(argv[i], "-af") == 0) { |
| 4875 | if(++i == argc) { |
| 4876 | ERROR("%s: -af missing filename\n", argv[0]); |
| 4877 | exit(1); |
| 4878 | } |
| 4879 | if(read_action_file(argv[i]) == FALSE) |
| 4880 | exit(1); |
| 4881 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4882 | } else if(strcmp(argv[i], "-comp") == 0) |
| 4883 | /* parsed previously */ |
| 4884 | i++; |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 4885 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4886 | else if(strncmp(argv[i], "-X", 2) == 0) { |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 4887 | int args; |
| 4888 | |
| 4889 | if(strcmp(argv[i] + 2, "help") == 0) |
| 4890 | goto print_compressor_options; |
| 4891 | |
| 4892 | args = compressor_options(comp, argv + i, argc - i); |
plougher | d886567 | 2010-11-27 05:05:49 +0000 | [diff] [blame] | 4893 | if(args < 0) { |
| 4894 | if(args == -1) { |
| 4895 | ERROR("%s: Unrecognised compressor" |
| 4896 | " option %s\n", argv[0], |
| 4897 | argv[i]); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4898 | if(!compressor_opt_parsed) |
| 4899 | ERROR("%s: Did you forget to" |
| 4900 | " specify -comp?\n", |
| 4901 | argv[0]); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 4902 | print_compressor_options: |
Phillip Lougher | b1c3b6a | 2014-01-06 05:30:00 +0000 | [diff] [blame] | 4903 | ERROR("%s: selected compressor \"%s\"" |
| 4904 | ". Options supported: %s\n", |
| 4905 | argv[0], comp->name, |
| 4906 | comp->usage ? "" : "none"); |
| 4907 | if(comp->usage) |
| 4908 | comp->usage(); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4909 | } |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 4910 | exit(1); |
| 4911 | } |
| 4912 | i += args; |
| 4913 | |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 4914 | } else if(strcmp(argv[i], "-pf") == 0) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4915 | if(++i == argc) { |
| 4916 | ERROR("%s: -pf missing filename\n", argv[0]); |
| 4917 | exit(1); |
| 4918 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 4919 | if(read_pseudo_file(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4920 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4921 | } else if(strcmp(argv[i], "-p") == 0) { |
| 4922 | if(++i == argc) { |
| 4923 | ERROR("%s: -p missing pseudo file definition\n", |
| 4924 | argv[0]); |
| 4925 | exit(1); |
| 4926 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 4927 | if(read_pseudo_def(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4928 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4929 | } else if(strcmp(argv[i], "-recover") == 0) { |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4930 | if(++i == argc) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4931 | ERROR("%s: -recover missing recovery file\n", |
| 4932 | argv[0]); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4933 | exit(1); |
| 4934 | } |
| 4935 | read_recovery_data(argv[i], argv[source + 1]); |
| 4936 | } else if(strcmp(argv[i], "-no-recovery") == 0) |
| 4937 | recover = FALSE; |
| 4938 | else if(strcmp(argv[i], "-wildcards") == 0) { |
plougher | 934a9ed | 2007-10-19 00:21:10 +0000 | [diff] [blame] | 4939 | old_exclude = FALSE; |
| 4940 | use_regex = FALSE; |
| 4941 | } else if(strcmp(argv[i], "-regex") == 0) { |
| 4942 | old_exclude = FALSE; |
| 4943 | use_regex = TRUE; |
| 4944 | } else if(strcmp(argv[i], "-no-sparse") == 0) |
plougher | 1f54edc | 2007-08-12 23:13:36 +0000 | [diff] [blame] | 4945 | sparse_files = FALSE; |
| 4946 | else if(strcmp(argv[i], "-no-progress") == 0) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4947 | progress = FALSE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 4948 | else if(strcmp(argv[i], "-progress") == 0) |
| 4949 | force_progress = TRUE; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4950 | else if(strcmp(argv[i], "-no-exports") == 0) |
| 4951 | exportable = FALSE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4952 | else if(strcmp(argv[i], "-processors") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4953 | if((++i == argc) || !parse_num(argv[i], &processors)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4954 | ERROR("%s: -processors missing or invalid " |
| 4955 | "processor number\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4956 | exit(1); |
| 4957 | } |
| 4958 | if(processors < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4959 | ERROR("%s: -processors should be 1 or larger\n", |
| 4960 | argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4961 | exit(1); |
| 4962 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4963 | } else if(strcmp(argv[i], "-read-queue") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4964 | if((++i == argc) || |
| 4965 | !parse_num(argv[i], &readb_mbytes)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4966 | ERROR("%s: -read-queue missing or invalid " |
| 4967 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4968 | exit(1); |
| 4969 | } |
| 4970 | if(readb_mbytes < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4971 | ERROR("%s: -read-queue should be 1 megabyte or " |
| 4972 | "larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4973 | exit(1); |
| 4974 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4975 | } else if(strcmp(argv[i], "-write-queue") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4976 | if((++i == argc) || |
| 4977 | !parse_num(argv[i], &writeb_mbytes)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4978 | ERROR("%s: -write-queue missing or invalid " |
| 4979 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4980 | exit(1); |
| 4981 | } |
| 4982 | if(writeb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4983 | ERROR("%s: -write-queue should be 1 megabyte " |
| 4984 | "or larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4985 | exit(1); |
| 4986 | } |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4987 | } else if(strcmp(argv[i], "-fragment-queue") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4988 | if((++i == argc) || |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4989 | !parse_num(argv[i], |
| 4990 | &fragmentb_mbytes)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4991 | ERROR("%s: -fragment-queue missing or invalid " |
| 4992 | "queue size\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4993 | exit(1); |
| 4994 | } |
| 4995 | if(fragmentb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4996 | ERROR("%s: -fragment-queue should be 1 " |
| 4997 | "megabyte or larger\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4998 | exit(1); |
| 4999 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5000 | } else if(strcmp(argv[i], "-b") == 0) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5001 | if(++i == argc) { |
| 5002 | ERROR("%s: -b missing block size\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5003 | exit(1); |
| 5004 | } |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5005 | if(!parse_number(argv[i], &block_size, 1)) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5006 | ERROR("%s: -b invalid block size\n", argv[0]); |
| 5007 | exit(1); |
| 5008 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5009 | if((block_log = slog(block_size)) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5010 | ERROR("%s: -b block size not power of two or " |
| 5011 | "not between 4096 and 1Mbyte\n", |
| 5012 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5013 | exit(1); |
| 5014 | } |
| 5015 | } else if(strcmp(argv[i], "-ef") == 0) { |
| 5016 | if(++i == argc) { |
| 5017 | ERROR("%s: -ef missing filename\n", argv[0]); |
| 5018 | exit(1); |
| 5019 | } |
plougher | 9b5bf8c | 2006-03-20 18:43:33 +0000 | [diff] [blame] | 5020 | } else if(strcmp(argv[i], "-no-duplicates") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5021 | duplicate_checking = FALSE; |
| 5022 | |
| 5023 | else if(strcmp(argv[i], "-no-fragments") == 0) |
| 5024 | no_fragments = TRUE; |
| 5025 | |
| 5026 | else if(strcmp(argv[i], "-always-use-fragments") == 0) |
| 5027 | always_use_fragments = TRUE; |
| 5028 | |
| 5029 | else if(strcmp(argv[i], "-sort") == 0) { |
| 5030 | if(++i == argc) { |
| 5031 | ERROR("%s: -sort missing filename\n", argv[0]); |
| 5032 | exit(1); |
| 5033 | } |
| 5034 | } else if(strcmp(argv[i], "-all-root") == 0 || |
| 5035 | strcmp(argv[i], "-root-owned") == 0) |
| 5036 | global_uid = global_gid = 0; |
| 5037 | |
| 5038 | else if(strcmp(argv[i], "-force-uid") == 0) { |
| 5039 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5040 | ERROR("%s: -force-uid missing uid or user\n", |
| 5041 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5042 | exit(1); |
| 5043 | } |
| 5044 | if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5045 | if(global_uid < 0 || global_uid > |
| 5046 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5047 | ERROR("%s: -force-uid uid out of range" |
| 5048 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5049 | exit(1); |
| 5050 | } |
| 5051 | } else { |
| 5052 | struct passwd *uid = getpwnam(argv[i]); |
| 5053 | if(uid) |
| 5054 | global_uid = uid->pw_uid; |
| 5055 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5056 | ERROR("%s: -force-uid invalid uid or " |
| 5057 | "unknown user\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5058 | exit(1); |
| 5059 | } |
| 5060 | } |
| 5061 | } else if(strcmp(argv[i], "-force-gid") == 0) { |
| 5062 | if(++i == argc) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5063 | ERROR("%s: -force-gid missing gid or group\n", |
| 5064 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5065 | exit(1); |
| 5066 | } |
| 5067 | if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5068 | if(global_gid < 0 || global_gid > |
| 5069 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5070 | ERROR("%s: -force-gid gid out of range" |
| 5071 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5072 | exit(1); |
| 5073 | } |
| 5074 | } else { |
| 5075 | struct group *gid = getgrnam(argv[i]); |
| 5076 | if(gid) |
| 5077 | global_gid = gid->gr_gid; |
| 5078 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5079 | ERROR("%s: -force-gid invalid gid or " |
| 5080 | "unknown group\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5081 | exit(1); |
| 5082 | } |
| 5083 | } |
| 5084 | } else if(strcmp(argv[i], "-noI") == 0 || |
| 5085 | strcmp(argv[i], "-noInodeCompression") == 0) |
| 5086 | noI = TRUE; |
| 5087 | |
| 5088 | else if(strcmp(argv[i], "-noD") == 0 || |
| 5089 | strcmp(argv[i], "-noDataCompression") == 0) |
| 5090 | noD = TRUE; |
| 5091 | |
| 5092 | else if(strcmp(argv[i], "-noF") == 0 || |
| 5093 | strcmp(argv[i], "-noFragmentCompression") == 0) |
| 5094 | noF = TRUE; |
| 5095 | |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5096 | else if(strcmp(argv[i], "-noX") == 0 || |
| 5097 | strcmp(argv[i], "-noXattrCompression") == 0) |
| 5098 | noX = TRUE; |
| 5099 | |
plougher | ce564c6 | 2010-05-19 03:01:49 +0000 | [diff] [blame] | 5100 | else if(strcmp(argv[i], "-no-xattrs") == 0) |
| 5101 | no_xattrs = TRUE; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 5102 | |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5103 | else if(strcmp(argv[i], "-xattrs") == 0) |
| 5104 | no_xattrs = FALSE; |
| 5105 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5106 | else if(strcmp(argv[i], "-nopad") == 0) |
| 5107 | nopad = TRUE; |
| 5108 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5109 | else if(strcmp(argv[i], "-info") == 0) |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5110 | silent = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5111 | |
| 5112 | else if(strcmp(argv[i], "-e") == 0) |
| 5113 | break; |
| 5114 | |
| 5115 | else if(strcmp(argv[i], "-noappend") == 0) |
| 5116 | delete = TRUE; |
| 5117 | |
| 5118 | else if(strcmp(argv[i], "-keep-as-directory") == 0) |
| 5119 | keep_as_directory = TRUE; |
| 5120 | |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 5121 | else if(strcmp(argv[i], "-exit-on-error") == 0) |
| 5122 | exit_on_error = TRUE; |
| 5123 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5124 | else if(strcmp(argv[i], "-root-becomes") == 0) { |
| 5125 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5126 | ERROR("%s: -root-becomes: missing name\n", |
| 5127 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5128 | exit(1); |
| 5129 | } |
| 5130 | root_name = argv[i]; |
| 5131 | } else if(strcmp(argv[i], "-version") == 0) { |
| 5132 | VERSION(); |
| 5133 | } else { |
| 5134 | ERROR("%s: invalid option\n\n", argv[0]); |
| 5135 | printOptions: |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5136 | ERROR("SYNTAX:%s source1 source2 ... dest [options] " |
| 5137 | "[-e list of exclude\ndirs/files]\n", argv[0]); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5138 | ERROR("\nFilesystem build options:\n"); |
plougher | ff5ea8b | 2009-08-07 19:33:10 +0000 | [diff] [blame] | 5139 | ERROR("-comp <comp>\t\tselect <comp> compression\n"); |
plougher | 13df178 | 2009-08-29 01:05:34 +0000 | [diff] [blame] | 5140 | ERROR("\t\t\tCompressors available:\n"); |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 5141 | display_compressors("\t\t\t", COMP_DEFAULT); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5142 | ERROR("-b <block_size>\t\tset data block to " |
| 5143 | "<block_size>. Default %d bytes\n", |
| 5144 | SQUASHFS_FILE_SIZE); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5145 | ERROR("-no-exports\t\tdon't make the filesystem " |
| 5146 | "exportable via NFS\n"); |
| 5147 | ERROR("-no-sparse\t\tdon't detect sparse files\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5148 | ERROR("-no-xattrs\t\tdon't store extended attributes" |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5149 | NOXOPT_STR "\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5150 | ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5151 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5152 | ERROR("-noI\t\t\tdo not compress inode table\n"); |
| 5153 | ERROR("-noD\t\t\tdo not compress data blocks\n"); |
| 5154 | ERROR("-noF\t\t\tdo not compress fragment blocks\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5155 | ERROR("-noX\t\t\tdo not compress extended " |
| 5156 | "attributes\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5157 | ERROR("-no-fragments\t\tdo not use fragments\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5158 | ERROR("-always-use-fragments\tuse fragment blocks for " |
| 5159 | "files larger than block size\n"); |
| 5160 | ERROR("-no-duplicates\t\tdo not perform duplicate " |
| 5161 | "checking\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5162 | ERROR("-all-root\t\tmake all files owned by root\n"); |
| 5163 | ERROR("-force-uid uid\t\tset all file uids to uid\n"); |
| 5164 | ERROR("-force-gid gid\t\tset all file gids to gid\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5165 | ERROR("-nopad\t\t\tdo not pad filesystem to a multiple " |
| 5166 | "of 4K\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5167 | ERROR("-keep-as-directory\tif one source directory is " |
| 5168 | "specified, create a root\n"); |
| 5169 | ERROR("\t\t\tdirectory containing that directory, " |
| 5170 | "rather than the\n"); |
| 5171 | ERROR("\t\t\tcontents of the directory\n"); |
| 5172 | ERROR("\nFilesystem filter options:\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5173 | ERROR("-p <pseudo-definition>\tAdd pseudo file " |
| 5174 | "definition\n"); |
| 5175 | ERROR("-pf <pseudo-file>\tAdd list of pseudo file " |
| 5176 | "definitions\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5177 | ERROR("-sort <sort_file>\tsort files according to " |
| 5178 | "priorities in <sort_file>. One\n"); |
| 5179 | ERROR("\t\t\tfile or dir with priority per line. " |
| 5180 | "Priority -32768 to\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5181 | ERROR("\t\t\t32767, default priority 0\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5182 | ERROR("-ef <exclude_file>\tlist of exclude dirs/files." |
| 5183 | " One per line\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5184 | ERROR("-wildcards\t\tAllow extended shell wildcards " |
| 5185 | "(globbing) to be used in\n\t\t\texclude " |
| 5186 | "dirs/files\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5187 | ERROR("-regex\t\t\tAllow POSIX regular expressions to " |
| 5188 | "be used in exclude\n\t\t\tdirs/files\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5189 | ERROR("\nFilesystem append options:\n"); |
| 5190 | ERROR("-noappend\t\tdo not append to existing " |
| 5191 | "filesystem\n"); |
| 5192 | ERROR("-root-becomes <name>\twhen appending source " |
| 5193 | "files/directories, make the\n"); |
| 5194 | ERROR("\t\t\toriginal root become a subdirectory in " |
| 5195 | "the new root\n"); |
| 5196 | ERROR("\t\t\tcalled <name>, rather than adding the new " |
| 5197 | "source items\n"); |
| 5198 | ERROR("\t\t\tto the original root\n"); |
| 5199 | ERROR("\nMksquashfs runtime options:\n"); |
| 5200 | ERROR("-version\t\tprint version, licence and " |
| 5201 | "copyright message\n"); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 5202 | ERROR("-exit-on-error\t\ttreat normally ignored errors " |
| 5203 | "as fatal\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5204 | ERROR("-recover <name>\t\trecover filesystem data " |
| 5205 | "using recovery file <name>\n"); |
| 5206 | ERROR("-no-recovery\t\tdon't generate a recovery " |
| 5207 | "file\n"); |
| 5208 | ERROR("-info\t\t\tprint files written to filesystem\n"); |
| 5209 | ERROR("-no-progress\t\tdon't display the progress " |
| 5210 | "bar\n"); |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5211 | ERROR("-progress\t\tdisplay progress bar when using " |
| 5212 | "the -info option\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5213 | ERROR("-processors <number>\tUse <number> processors." |
| 5214 | " By default will use number of\n"); |
| 5215 | ERROR("\t\t\tprocessors available\n"); |
| 5216 | ERROR("-read-queue <size>\tSet input queue to <size> " |
| 5217 | "Mbytes. Default %d Mbytes\n", |
| 5218 | READER_BUFFER_DEFAULT); |
| 5219 | ERROR("-write-queue <size>\tSet output queue to <size> " |
| 5220 | "Mbytes. Default %d Mbytes\n", |
| 5221 | WRITER_BUFFER_DEFAULT); |
plougher | 8bc376b | 2010-11-12 04:55:20 +0000 | [diff] [blame] | 5222 | ERROR("-fragment-queue <size>\tSet fragment queue to " |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5223 | "<size> Mbytes. Default %d Mbytes\n", |
| 5224 | FRAGMENT_BUFFER_DEFAULT); |
| 5225 | ERROR("\nMiscellaneous options:\n"); |
| 5226 | ERROR("-root-owned\t\talternative name for -all-root" |
| 5227 | "\n"); |
| 5228 | ERROR("-noInodeCompression\talternative name for -noI" |
| 5229 | "\n"); |
| 5230 | ERROR("-noDataCompression\talternative name for -noD" |
| 5231 | "\n"); |
| 5232 | ERROR("-noFragmentCompression\talternative name for " |
| 5233 | "-noF\n"); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5234 | ERROR("-noXattrCompression\talternative name for " |
| 5235 | "-noX\n"); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 5236 | ERROR("\n-Xhelp\t\t\tprint compressor options for" |
| 5237 | " selected compressor\n"); |
plougher | 4fb6682 | 2010-12-08 02:49:28 +0000 | [diff] [blame] | 5238 | ERROR("\nCompressors available and compressor specific " |
| 5239 | "options:\n"); |
| 5240 | display_compressor_usage(COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5241 | exit(1); |
| 5242 | } |
| 5243 | } |
| 5244 | |
plougher | b747f4f | 2010-12-25 04:05:47 +0000 | [diff] [blame] | 5245 | /* |
| 5246 | * Some compressors may need the options to be checked for validity |
| 5247 | * once all the options have been processed |
| 5248 | */ |
| 5249 | res = compressor_options_post(comp, block_size); |
| 5250 | if(res) |
| 5251 | EXIT_MKSQUASHFS(); |
| 5252 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5253 | /* |
| 5254 | * If the -info option has been selected then disable the |
| 5255 | * progress bar unless it has been explicitly enabled with |
| 5256 | * the -progress option |
| 5257 | */ |
| 5258 | if(!silent) |
| 5259 | progress = force_progress; |
| 5260 | |
| 5261 | #ifdef SQUASHFS_TRACE |
Phillip Lougher | 989f5fe | 2013-04-22 03:16:29 +0100 | [diff] [blame] | 5262 | /* |
| 5263 | * Disable progress bar if full debug tracing is enabled. |
| 5264 | * The progress bar in this case just gets in the way of the |
| 5265 | * debug trace output |
| 5266 | */ |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5267 | progress = FALSE; |
| 5268 | #endif |
| 5269 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5270 | for(i = 0; i < source; i++) |
| 5271 | if(lstat(source_path[i], &source_buf) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5272 | fprintf(stderr, "Cannot stat source directory \"%s\" " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5273 | "because %s\n", source_path[i], |
| 5274 | strerror(errno)); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5275 | EXIT_MKSQUASHFS(); |
| 5276 | } |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5277 | |
| 5278 | destination_file = argv[source + 1]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5279 | if(stat(argv[source + 1], &buf) == -1) { |
| 5280 | if(errno == ENOENT) { /* Does not exist */ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5281 | fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, |
plougher | 59dce67 | 2010-05-19 03:56:59 +0000 | [diff] [blame] | 5282 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5283 | if(fd == -1) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5284 | perror("Could not create destination file"); |
| 5285 | exit(1); |
| 5286 | } |
| 5287 | delete = TRUE; |
| 5288 | } else { |
| 5289 | perror("Could not stat destination file"); |
| 5290 | exit(1); |
| 5291 | } |
| 5292 | |
| 5293 | } else { |
| 5294 | if(S_ISBLK(buf.st_mode)) { |
| 5295 | if((fd = open(argv[source + 1], O_RDWR)) == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5296 | perror("Could not open block device as " |
| 5297 | "destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5298 | exit(1); |
| 5299 | } |
| 5300 | block_device = 1; |
| 5301 | |
| 5302 | } else if(S_ISREG(buf.st_mode)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5303 | fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | |
| 5304 | O_RDWR); |
| 5305 | if(fd == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5306 | perror("Could not open regular file for " |
| 5307 | "writing as destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5308 | exit(1); |
| 5309 | } |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 5310 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5311 | else { |
| 5312 | ERROR("Destination not block device or regular file\n"); |
| 5313 | exit(1); |
| 5314 | } |
| 5315 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5316 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5317 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5318 | /* |
| 5319 | * process the exclude files - must be done afer destination file has |
| 5320 | * been possibly created |
| 5321 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5322 | for(i = source + 2; i < argc; i++) |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 5323 | if(strcmp(argv[i], "-ef") == 0) |
| 5324 | /* |
| 5325 | * Note presence of filename arg has already |
| 5326 | * been checked |
| 5327 | */ |
| 5328 | process_exclude_file(argv[++i]); |
| 5329 | else if(strcmp(argv[i], "-e") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5330 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5331 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5332 | strcmp(argv[i], "-sort") == 0 || |
| 5333 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5334 | strcmp(argv[i], "-af") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5335 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5336 | i++; |
| 5337 | |
| 5338 | if(i != argc) { |
| 5339 | if(++i == argc) { |
| 5340 | ERROR("%s: -e missing arguments\n", argv[0]); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5341 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5342 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 5343 | while(i < argc) |
| 5344 | if(old_exclude) |
| 5345 | old_add_exclude(argv[i++]); |
| 5346 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5347 | add_exclude(argv[i++]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | /* process the sort files - must be done afer the exclude files */ |
| 5351 | for(i = source + 2; i < argc; i++) |
| 5352 | if(strcmp(argv[i], "-sort") == 0) { |
plougher | e1c9a74 | 2010-07-21 16:59:05 +0000 | [diff] [blame] | 5353 | int res = read_sort_file(argv[++i], source, |
| 5354 | source_path); |
| 5355 | if(res == FALSE) |
| 5356 | BAD_ERROR("Failed to read sort file\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5357 | sorted ++; |
| 5358 | } else if(strcmp(argv[i], "-e") == 0) |
| 5359 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5360 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5361 | strcmp(argv[i], "-ef") == 0 || |
| 5362 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5363 | strcmp(argv[i], "-af") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5364 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5365 | i++; |
| 5366 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5367 | if(!delete) { |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 5368 | comp = read_super(fd, &sBlk, argv[source + 1]); |
| 5369 | if(comp == NULL) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5370 | ERROR("Failed to read existing filesystem - will not " |
| 5371 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5372 | ERROR("To force Mksquashfs to write to this block " |
| 5373 | "device or file use -noappend\n"); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5374 | EXIT_MKSQUASHFS(); |
| 5375 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5376 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5377 | block_log = slog(block_size = sBlk.block_size); |
| 5378 | noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags); |
| 5379 | noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags); |
| 5380 | noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5381 | noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5382 | no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags); |
| 5383 | always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags); |
| 5384 | duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5385 | exportable = SQUASHFS_EXPORTABLE(sBlk.flags); |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5386 | no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5387 | comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
Phillip Lougher | c97dac5 | 2013-04-14 03:32:10 +0100 | [diff] [blame] | 5390 | initialise_threads(readb_mbytes, writeb_mbytes, fragmentb_mbytes, |
| 5391 | delete); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5392 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 5393 | res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0); |
| 5394 | if(res) |
| 5395 | BAD_ERROR("compressor_init failed\n"); |
| 5396 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5397 | if(delete) { |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5398 | int size; |
Phillip Lougher | a45c9d2 | 2011-02-20 04:24:07 +0000 | [diff] [blame] | 5399 | void *comp_data = compressor_dump_options(comp, block_size, |
| 5400 | &size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5401 | |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 5402 | printf("Creating %d.%d filesystem on %s, block size %d.\n", |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5403 | SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5404 | |
plougher | 871c72a | 2010-12-25 04:17:27 +0000 | [diff] [blame] | 5405 | /* |
| 5406 | * store any compressor specific options after the superblock, |
| 5407 | * and set the COMP_OPT flag to show that the filesystem has |
| 5408 | * compressor specfic options |
| 5409 | */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5410 | if(comp_data) { |
| 5411 | unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT; |
| 5412 | |
| 5413 | SQUASHFS_INSWAP_SHORTS(&c_byte, 1); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5414 | write_destination(fd, sizeof(struct squashfs_super_block), |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 5415 | sizeof(c_byte), &c_byte); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5416 | write_destination(fd, sizeof(struct squashfs_super_block) + |
plougher | 8e4dad4 | 2010-12-28 05:56:22 +0000 | [diff] [blame] | 5417 | sizeof(c_byte), size, comp_data); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5418 | bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte) |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5419 | + size; |
| 5420 | comp_opts = TRUE; |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5421 | } else |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5422 | bytes = sizeof(struct squashfs_super_block); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5423 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5424 | unsigned int last_directory_block, inode_dir_offset, |
| 5425 | inode_dir_file_size, root_inode_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5426 | inode_dir_start_block, uncompressed_data, |
| 5427 | compressed_data, inode_dir_inode_number, |
| 5428 | inode_dir_parent_inode; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5429 | unsigned int root_inode_start = |
| 5430 | SQUASHFS_INODE_BLK(sBlk.root_inode), |
| 5431 | root_inode_offset = |
| 5432 | SQUASHFS_INODE_OFFSET(sBlk.root_inode); |
Phillip Lougher | b1259f7 | 2013-06-06 03:10:15 +0100 | [diff] [blame] | 5433 | sigset_t sigmask; |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5434 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5435 | if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, |
| 5436 | &data_cache, &directory_table, |
| 5437 | &directory_data_cache, &last_directory_block, |
| 5438 | &inode_dir_offset, &inode_dir_file_size, |
| 5439 | &root_inode_size, &inode_dir_start_block, |
| 5440 | &file_count, &sym_count, &dev_count, &dir_count, |
| 5441 | &fifo_count, &sock_count, &total_bytes, |
| 5442 | &total_inode_bytes, &total_directory_bytes, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5443 | &inode_dir_inode_number, |
| 5444 | &inode_dir_parent_inode, add_old_root_entry, |
| 5445 | &fragment_table, &inode_lookup_table)) == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5446 | ERROR("Failed to read existing filesystem - will not " |
| 5447 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5448 | ERROR("To force Mksquashfs to write to this block " |
| 5449 | "device or file use -noappend\n"); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5450 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5451 | } |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame^] | 5452 | if((append_fragments = fragments = sBlk.fragments)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5453 | fragment_table = realloc((char *) fragment_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5454 | ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 5455 | * sizeof(struct squashfs_fragment_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5456 | if(fragment_table == NULL) |
| 5457 | BAD_ERROR("Out of memory in save filesystem state\n"); |
| 5458 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5459 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5460 | printf("Appending to existing %d.%d filesystem on %s, block " |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5461 | "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5462 | block_size); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5463 | printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, " |
plougher | bb98803 | 2009-08-06 08:46:34 +0000 | [diff] [blame] | 5464 | "-always-use-fragments,\n-exportable and -comp options " |
| 5465 | "ignored\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5466 | printf("\nIf appending is not wanted, please re-run with " |
| 5467 | "-noappend specified!\n\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5468 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5469 | compressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5470 | ~(SQUASHFS_METADATA_SIZE - 1); |
| 5471 | uncompressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5472 | (SQUASHFS_METADATA_SIZE - 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5473 | |
| 5474 | /* save original filesystem state for restoring ... */ |
| 5475 | sfragments = fragments; |
| 5476 | sbytes = bytes; |
| 5477 | sinode_count = sBlk.inodes; |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 5478 | scache_bytes = root_inode_offset + root_inode_size; |
| 5479 | sdirectory_cache_bytes = uncompressed_data; |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5480 | sdata_cache = malloc(scache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5481 | if(sdata_cache == NULL) |
| 5482 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5483 | sdirectory_data_cache = malloc(sdirectory_cache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5484 | if(sdirectory_data_cache == NULL) |
| 5485 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5486 | memcpy(sdata_cache, data_cache, scache_bytes); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5487 | memcpy(sdirectory_data_cache, directory_data_cache + |
| 5488 | compressed_data, sdirectory_cache_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5489 | sinode_bytes = root_inode_start; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5490 | stotal_bytes = total_bytes; |
| 5491 | stotal_inode_bytes = total_inode_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5492 | stotal_directory_bytes = total_directory_bytes + |
| 5493 | compressed_data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5494 | sfile_count = file_count; |
| 5495 | ssym_count = sym_count; |
| 5496 | sdev_count = dev_count; |
| 5497 | sdir_count = dir_count + 1; |
| 5498 | sfifo_count = fifo_count; |
| 5499 | ssock_count = sock_count; |
| 5500 | sdup_files = dup_files; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5501 | sid_count = id_count; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5502 | write_recovery_data(&sBlk); |
Phillip Lougher | 3bcf67d | 2013-02-24 10:56:38 +0000 | [diff] [blame] | 5503 | save_xattrs(); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 5504 | restore_thread = init_restore_thread(); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5505 | sigemptyset(&sigmask); |
| 5506 | sigaddset(&sigmask, SIGINT); |
| 5507 | sigaddset(&sigmask, SIGTERM); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 5508 | sigaddset(&sigmask, SIGUSR1); |
Phillip Lougher | b1259f7 | 2013-06-06 03:10:15 +0100 | [diff] [blame] | 5509 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5510 | BAD_ERROR("Failed to set signal mask\n"); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 5511 | write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5512 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5513 | /* |
| 5514 | * set the filesystem state up to be able to append to the |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5515 | * original filesystem. The filesystem state differs depending |
| 5516 | * on whether we're appending to the original root directory, or |
| 5517 | * if the original root directory becomes a sub-directory |
| 5518 | * (root-becomes specified on command line, here root_name != |
| 5519 | * NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5520 | */ |
| 5521 | inode_bytes = inode_size = root_inode_start; |
| 5522 | directory_size = last_directory_block; |
| 5523 | cache_size = root_inode_offset + root_inode_size; |
| 5524 | directory_cache_size = inode_dir_offset + inode_dir_file_size; |
| 5525 | if(root_name) { |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5526 | sdirectory_bytes = last_directory_block; |
| 5527 | sdirectory_compressed_bytes = 0; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5528 | root_inode_number = inode_dir_parent_inode; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5529 | inode_no = sBlk.inodes + 2; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5530 | directory_bytes = last_directory_block; |
| 5531 | directory_cache_bytes = uncompressed_data; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5532 | memmove(directory_data_cache, directory_data_cache + |
| 5533 | compressed_data, uncompressed_data); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5534 | cache_bytes = root_inode_offset + root_inode_size; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5535 | add_old_root_entry(root_name, sBlk.root_inode, |
| 5536 | inode_dir_inode_number, SQUASHFS_DIR_TYPE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5537 | total_directory_bytes += compressed_data; |
| 5538 | dir_count ++; |
| 5539 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5540 | sdirectory_compressed_bytes = last_directory_block - |
| 5541 | inode_dir_start_block; |
| 5542 | sdirectory_compressed = |
| 5543 | malloc(sdirectory_compressed_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5544 | if(sdirectory_compressed == NULL) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5545 | BAD_ERROR("Out of memory in save filesystem " |
| 5546 | "state\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5547 | memcpy(sdirectory_compressed, directory_table + |
| 5548 | inode_dir_start_block, |
| 5549 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5550 | sdirectory_bytes = inode_dir_start_block; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5551 | root_inode_number = inode_dir_inode_number; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5552 | inode_no = sBlk.inodes + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5553 | directory_bytes = inode_dir_start_block; |
| 5554 | directory_cache_bytes = inode_dir_offset; |
| 5555 | cache_bytes = root_inode_offset; |
| 5556 | } |
| 5557 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5558 | inode_count = file_count + dir_count + sym_count + dev_count + |
| 5559 | fifo_count + sock_count; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5560 | } |
| 5561 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 5562 | if(path) |
| 5563 | paths = add_subdir(paths, path); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5564 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5565 | dump_actions(); |
| 5566 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5567 | if(delete && !keep_as_directory && source == 1 && |
| 5568 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5569 | dir_scan(&inode, source_path[0], scan1_readdir, progress); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5570 | else if(!keep_as_directory && source == 1 && |
| 5571 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5572 | dir_scan(&inode, source_path[0], scan1_single_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5573 | else |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5574 | dir_scan(&inode, "", scan1_encomp_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5575 | sBlk.root_inode = inode; |
| 5576 | sBlk.inodes = inode_count; |
| 5577 | sBlk.s_magic = SQUASHFS_MAGIC; |
| 5578 | sBlk.s_major = SQUASHFS_MAJOR; |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5579 | sBlk.s_minor = SQUASHFS_MINOR; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5580 | sBlk.block_size = block_size; |
| 5581 | sBlk.block_log = block_log; |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5582 | sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments, |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5583 | always_use_fragments, duplicate_checking, exportable, |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5584 | no_xattrs, comp_opts); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5585 | sBlk.mkfs_time = time(NULL); |
| 5586 | |
Phillip Lougher | 83b8f86 | 2013-04-09 03:28:55 +0100 | [diff] [blame] | 5587 | disable_info(); |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 5588 | |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5589 | while((fragment = get_frag_action(fragment))) |
| 5590 | write_fragment(*fragment); |
| 5591 | unlock_fragments(); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 5592 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5593 | pthread_mutex_lock(&fragment_mutex); |
| 5594 | while(fragments_outstanding) { |
| 5595 | pthread_mutex_unlock(&fragment_mutex); |
| 5596 | sched_yield(); |
| 5597 | pthread_mutex_lock(&fragment_mutex); |
| 5598 | } |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 5599 | pthread_cleanup_pop(1); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5600 | |
| 5601 | queue_put(to_writer, NULL); |
| 5602 | if(queue_get(from_writer) != 0) |
| 5603 | EXIT_MKSQUASHFS(); |
| 5604 | |
Phillip Lougher | cd1a5a6 | 2014-01-21 04:53:46 +0000 | [diff] [blame] | 5605 | set_progressbar_state(FALSE); |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 5606 | write_filesystem_tables(&sBlk, nopad); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5607 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5608 | return 0; |
| 5609 | } |