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; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 99 | |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 100 | long long global_uid = -1, global_gid = -1; |
| 101 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 102 | /* superblock attributes */ |
| 103 | int block_size = SQUASHFS_FILE_SIZE, block_log; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 104 | unsigned int id_count = 0; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 105 | int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0, |
| 106 | sock_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 107 | |
| 108 | /* write position within data section */ |
| 109 | long long bytes = 0, total_bytes = 0; |
| 110 | |
| 111 | /* in memory directory table - possibly compressed */ |
| 112 | char *directory_table = NULL; |
| 113 | unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0; |
| 114 | |
| 115 | /* cached directory table */ |
| 116 | char *directory_data_cache = NULL; |
| 117 | unsigned int directory_cache_bytes = 0, directory_cache_size = 0; |
| 118 | |
| 119 | /* in memory inode table - possibly compressed */ |
| 120 | char *inode_table = NULL; |
| 121 | unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0; |
| 122 | |
| 123 | /* cached inode table */ |
| 124 | char *data_cache = NULL; |
| 125 | unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0; |
| 126 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 127 | /* inode lookup table */ |
| 128 | squashfs_inode *inode_lookup_table = NULL; |
| 129 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 130 | /* in memory directory data */ |
| 131 | #define I_COUNT_SIZE 128 |
| 132 | #define DIR_ENTRIES 32 |
| 133 | #define INODE_HASH_SIZE 65536 |
| 134 | #define INODE_HASH_MASK (INODE_HASH_SIZE - 1) |
| 135 | #define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK) |
| 136 | |
| 137 | struct cached_dir_index { |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 138 | struct squashfs_dir_index index; |
| 139 | char *name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | struct directory { |
| 143 | unsigned int start_block; |
| 144 | unsigned int size; |
| 145 | unsigned char *buff; |
| 146 | unsigned char *p; |
| 147 | unsigned int entry_count; |
| 148 | unsigned char *entry_count_p; |
| 149 | unsigned int i_count; |
| 150 | unsigned int i_size; |
| 151 | struct cached_dir_index *index; |
| 152 | unsigned char *index_count_p; |
| 153 | unsigned int inode_number; |
| 154 | }; |
| 155 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 156 | struct inode_info *inode_info[INODE_HASH_SIZE]; |
| 157 | |
| 158 | /* hash tables used to do fast duplicate searches in duplicate check */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 159 | struct file_info *dupl[65536]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 160 | int dup_files = 0; |
| 161 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 162 | /* exclude file handling */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 163 | /* list of exclude dirs/files */ |
| 164 | struct exclude_info { |
| 165 | dev_t st_dev; |
| 166 | ino_t st_ino; |
| 167 | }; |
| 168 | |
| 169 | #define EXCLUDE_SIZE 8192 |
| 170 | int exclude = 0; |
| 171 | struct exclude_info *exclude_paths = NULL; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 172 | int old_excluded(char *filename, struct stat *buf); |
| 173 | |
| 174 | struct path_entry { |
| 175 | char *name; |
| 176 | regex_t *preg; |
| 177 | struct pathname *paths; |
| 178 | }; |
| 179 | |
| 180 | struct pathname { |
| 181 | int names; |
| 182 | struct path_entry *name; |
| 183 | }; |
| 184 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 185 | struct pathnames { |
| 186 | int count; |
| 187 | struct pathname *path[0]; |
| 188 | }; |
| 189 | #define PATHS_ALLOC_SIZE 10 |
| 190 | |
| 191 | struct pathnames *paths = NULL; |
| 192 | struct pathname *path = NULL; |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 193 | struct pathname *stickypath = NULL; |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 194 | int excluded(char *name, struct pathnames *paths, struct pathnames **new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 195 | |
| 196 | /* fragment block data structures */ |
| 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 | struct fragment { |
| 200 | unsigned int index; |
| 201 | int offset; |
| 202 | int size; |
| 203 | }; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 204 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 205 | #define FRAG_SIZE 32768 |
Phillip Lougher | 4570201 | 2013-04-30 05:19:54 +0100 | [diff] [blame] | 206 | #define FRAG_INDEX(n) (-(n + 2)) |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 207 | |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 208 | struct squashfs_fragment_entry *fragment_table = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 209 | int fragments_outstanding = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 210 | |
Phillip Lougher | 7ffdf2a | 2013-04-14 02:53:30 +0100 | [diff] [blame] | 211 | int fragments_locked = FALSE; |
Phillip Lougher | 7ffdf2a | 2013-04-14 02:53:30 +0100 | [diff] [blame] | 212 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 213 | /* current inode number for directories and non directories */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 214 | unsigned int inode_no = 1; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 215 | unsigned int root_inode_number = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 216 | |
| 217 | /* list of source dirs/files */ |
| 218 | int source = 0; |
| 219 | char **source_path; |
| 220 | |
| 221 | /* list of root directory entries read from original filesystem */ |
| 222 | int old_root_entries = 0; |
| 223 | struct old_root_entry_info { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 224 | char *name; |
| 225 | struct inode_info inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 226 | }; |
| 227 | struct old_root_entry_info *old_root_entry; |
| 228 | |
| 229 | /* in memory file info */ |
| 230 | struct file_info { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 231 | long long file_size; |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 232 | long long bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 233 | unsigned short checksum; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 234 | unsigned short fragment_checksum; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 235 | long long start; |
| 236 | unsigned int *block_list; |
| 237 | struct file_info *next; |
| 238 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 239 | char checksum_flag; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 242 | /* restore orignal filesystem state if appending to existing filesystem is |
| 243 | * cancelled */ |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 244 | char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 245 | |
| 246 | long long sbytes, stotal_bytes; |
| 247 | |
| 248 | unsigned int sinode_bytes, scache_bytes, sdirectory_bytes, |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 249 | sdirectory_cache_bytes, sdirectory_compressed_bytes, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 250 | stotal_inode_bytes, stotal_directory_bytes, |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 251 | sinode_count = 0, sfile_count, ssym_count, sdev_count, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 252 | sdir_count, sfifo_count, ssock_count, sdup_files; |
| 253 | int sfragments; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 254 | int threads; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 255 | |
| 256 | /* flag whether destination file is a block device */ |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 257 | int block_device = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 258 | |
| 259 | /* flag indicating whether files are sorted using sort list(s) */ |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 260 | int sorted = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 261 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 262 | /* save destination file name for deleting on error */ |
| 263 | char *destination_file = NULL; |
| 264 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 265 | /* recovery file for abnormal exit on appending */ |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 266 | char *recovery_file = NULL; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 267 | int recover = TRUE; |
| 268 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 269 | /* in memory uid tables */ |
| 270 | #define ID_ENTRIES 256 |
| 271 | #define ID_HASH(id) (id & (ID_ENTRIES - 1)) |
| 272 | #define ISA_UID 1 |
| 273 | #define ISA_GID 2 |
| 274 | struct id { |
| 275 | unsigned int id; |
| 276 | int index; |
| 277 | char flags; |
| 278 | struct id *next; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 279 | }; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 280 | struct id *id_hash_table[ID_ENTRIES]; |
| 281 | struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS]; |
| 282 | unsigned int uid_count = 0, guid_count = 0; |
| 283 | unsigned int sid_count = 0, suid_count = 0, sguid_count = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 284 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 285 | struct cache *reader_buffer, *writer_buffer, *fragment_buffer; |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 286 | struct queue *to_reader, *to_deflate, *to_writer, *from_writer, |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 287 | *to_frag, *locked_fragment; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 288 | struct seq_queue *to_main; |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 289 | pthread_t reader_thread, writer_thread, main_thread; |
| 290 | pthread_t *deflator_thread, *frag_deflator_thread; |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 291 | pthread_t *restore_thread = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 292 | pthread_mutex_t fragment_mutex; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 293 | pthread_mutex_t pos_mutex; |
| 294 | |
| 295 | /* user options that control parallelisation */ |
| 296 | int processors = -1; |
| 297 | /* default size of output buffer in Mbytes */ |
| 298 | #define WRITER_BUFFER_DEFAULT 512 |
| 299 | /* default size of input buffer in Mbytes */ |
| 300 | #define READER_BUFFER_DEFAULT 64 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 301 | /* default size of fragment buffer in Mbytes */ |
| 302 | #define FRAGMENT_BUFFER_DEFAULT 64 |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 303 | int writer_buffer_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 304 | |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 305 | /* compression operations */ |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 306 | static struct compressor *comp = NULL; |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 307 | int compressor_opt_parsed = FALSE; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 308 | void *stream = NULL; |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 309 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 310 | /* xattr stats */ |
| 311 | unsigned int xattr_bytes = 0, total_xattr_bytes = 0; |
| 312 | |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 313 | char *read_from_disk(long long start, unsigned int avail_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 314 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 315 | int type); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 316 | struct file_info *duplicate(long long file_size, long long bytes, |
| 317 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 318 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 319 | unsigned short fragment_checksum, int checksum_flag); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 320 | struct dir_info *dir_scan1(char *, char *, struct pathnames *, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 321 | struct dir_ent *(_readdir)(struct dir_info *), int); |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 322 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 323 | void dir_scan3(struct dir_info *root, struct dir_info *dir); |
| 324 | void dir_scan4(struct dir_info *dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 325 | void dir_scan5(struct dir_info *dir); |
| 326 | void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 327 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 328 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 329 | unsigned short checksum, unsigned short fragment_checksum, |
| 330 | int checksum_flag); |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 331 | long long generic_write_table(int, void *, int, void *, int); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 332 | void restorefs(); |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 333 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 334 | void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 335 | |
| 336 | |
Phillip Lougher | 9b7c3c2 | 2013-03-10 02:29:42 +0000 | [diff] [blame] | 337 | void prep_exit() |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 338 | { |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 339 | if(restore_thread) { |
| 340 | if(pthread_self() == *restore_thread) { |
| 341 | /* |
| 342 | * Recursive failure when trying to restore filesystem! |
| 343 | * Nothing to do except to exit, otherwise we'll just |
| 344 | * appear to hang. The user should be able to restore |
| 345 | * from the recovery file (which is why it was added, in |
| 346 | * case of catastrophic failure in Mksquashfs) |
| 347 | */ |
| 348 | exit(1); |
| 349 | } else { |
| 350 | /* signal the restore thread to restore */ |
Phillip Lougher | 8c74081 | 2014-01-30 01:56:17 +0000 | [diff] [blame^] | 351 | pthread_kill(*restore_thread, SIGUSR1); |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 352 | pthread_exit(NULL); |
| 353 | } |
Phillip Lougher | 9e07191 | 2013-04-02 05:30:29 +0100 | [diff] [blame] | 354 | } |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 355 | if(delete && destination_file && !block_device) |
| 356 | unlink(destination_file); |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 360 | int add_overflow(int a, int b) |
| 361 | { |
| 362 | return (INT_MAX - a) < b; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | int shift_overflow(int a, int shift) |
| 367 | { |
| 368 | return (INT_MAX >> shift) < a; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | int multiply_overflow(int a, int multiplier) |
| 373 | { |
| 374 | return (INT_MAX / multiplier) < a; |
| 375 | } |
| 376 | |
| 377 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 378 | #define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \ |
| 379 | + (((char *)A) - data_cache))) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 380 | |
| 381 | |
| 382 | void restorefs() |
| 383 | { |
| 384 | ERROR("Exiting - restoring original filesystem!\n\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 385 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 386 | bytes = sbytes; |
| 387 | memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 388 | memcpy(directory_data_cache, sdirectory_data_cache, |
| 389 | sdirectory_cache_bytes); |
| 390 | directory_cache_bytes = sdirectory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 391 | inode_bytes = sinode_bytes; |
| 392 | directory_bytes = sdirectory_bytes; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 393 | memcpy(directory_table + directory_bytes, sdirectory_compressed, |
| 394 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 395 | directory_bytes += sdirectory_compressed_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 396 | total_bytes = stotal_bytes; |
| 397 | total_inode_bytes = stotal_inode_bytes; |
| 398 | total_directory_bytes = stotal_directory_bytes; |
| 399 | inode_count = sinode_count; |
| 400 | file_count = sfile_count; |
| 401 | sym_count = ssym_count; |
| 402 | dev_count = sdev_count; |
| 403 | dir_count = sdir_count; |
| 404 | fifo_count = sfifo_count; |
| 405 | sock_count = ssock_count; |
| 406 | dup_files = sdup_files; |
| 407 | fragments = sfragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 408 | id_count = sid_count; |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 409 | restore_xattrs(); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 410 | write_filesystem_tables(&sBlk, nopad); |
| 411 | exit(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 415 | void sighandler() |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 416 | { |
| 417 | EXIT_MKSQUASHFS(); |
| 418 | } |
| 419 | |
| 420 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 421 | int mangle2(void *strm, char *d, char *s, int size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 422 | int block_size, int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 423 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 424 | int error, c_byte = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 425 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 426 | if(!uncompressed) { |
plougher | bfb876c | 2010-12-31 07:48:01 +0000 | [diff] [blame] | 427 | c_byte = compressor_compress(comp, strm, d, s, size, block_size, |
| 428 | &error); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 429 | if(c_byte == -1) |
| 430 | BAD_ERROR("mangle2:: %s compress failed with error " |
| 431 | "code %d\n", comp->name, error); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 432 | } |
| 433 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 434 | if(c_byte == 0 || c_byte >= size) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 435 | memcpy(d, s, size); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 436 | return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : |
| 437 | SQUASHFS_COMPRESSED_BIT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 438 | } |
| 439 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 440 | return c_byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 444 | int mangle(char *d, char *s, int size, int block_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 445 | int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 446 | { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 447 | return mangle2(stream, d, s, size, block_size, uncompressed, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 448 | data_block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 452 | void *get_inode(int req_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 453 | { |
| 454 | int data_space; |
| 455 | unsigned short c_byte; |
| 456 | |
| 457 | while(cache_bytes >= SQUASHFS_METADATA_SIZE) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 458 | if((inode_size - inode_bytes) < |
| 459 | ((SQUASHFS_METADATA_SIZE << 1)) + 2) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 460 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 461 | (SQUASHFS_METADATA_SIZE << 1) + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 462 | if(it == NULL) |
| 463 | MEM_ERROR(); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 464 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 465 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
| 466 | } |
| 467 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 468 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 469 | data_cache, SQUASHFS_METADATA_SIZE, |
| 470 | SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 471 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 472 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 473 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 474 | total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 475 | memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 476 | cache_bytes - SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 477 | cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 478 | } |
| 479 | |
| 480 | data_space = (cache_size - cache_bytes); |
| 481 | if(data_space < req_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 482 | int realloc_size = cache_size == 0 ? |
| 483 | ((req_size + SQUASHFS_METADATA_SIZE) & |
| 484 | ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - |
| 485 | data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 486 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 487 | void *dc = realloc(data_cache, cache_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 488 | realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 489 | if(dc == NULL) |
| 490 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 491 | cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 492 | data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | cache_bytes += req_size; |
| 496 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 497 | return data_cache + cache_bytes - req_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 501 | int read_bytes(int fd, void *buff, int bytes) |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 502 | { |
| 503 | int res, count; |
| 504 | |
| 505 | for(count = 0; count < bytes; count += res) { |
| 506 | res = read(fd, buff + count, bytes - count); |
| 507 | if(res < 1) { |
plougher | 96f85a1 | 2009-03-29 22:39:59 +0000 | [diff] [blame] | 508 | if(res == 0) |
| 509 | goto bytes_read; |
| 510 | else if(errno != EINTR) { |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 511 | ERROR("Read failed because %s\n", |
| 512 | strerror(errno)); |
| 513 | return -1; |
| 514 | } else |
| 515 | res = 0; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | bytes_read: |
| 520 | return count; |
| 521 | } |
| 522 | |
| 523 | |
plougher | 3306cb2 | 2010-06-18 04:22:44 +0000 | [diff] [blame] | 524 | int read_fs_bytes(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 525 | { |
| 526 | off_t off = byte; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 527 | int res = 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 528 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 529 | TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 530 | byte, bytes); |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 531 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 532 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 533 | pthread_mutex_lock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 534 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 535 | ERROR("read_fs_bytes: Lseek on destination failed because %s, " |
| 536 | "offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 537 | res = 0; |
Phillip Lougher | 041cf6d | 2013-07-20 04:20:40 +0100 | [diff] [blame] | 538 | } else if(read_bytes(fd, buff, bytes) < bytes) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 539 | ERROR("Read on destination failed\n"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 540 | res = 0; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 543 | pthread_cleanup_pop(1); |
| 544 | return res; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 548 | int write_bytes(int fd, void *buff, int bytes) |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 549 | { |
| 550 | int res, count; |
| 551 | |
| 552 | for(count = 0; count < bytes; count += res) { |
| 553 | res = write(fd, buff + count, bytes - count); |
| 554 | if(res == -1) { |
| 555 | if(errno != EINTR) { |
| 556 | ERROR("Write failed because %s\n", |
| 557 | strerror(errno)); |
| 558 | return -1; |
| 559 | } |
| 560 | res = 0; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 568 | void write_destination(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 569 | { |
| 570 | off_t off = byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 571 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 572 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
| 573 | pthread_mutex_lock(&pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 574 | |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 575 | if(lseek(fd, off, SEEK_SET) == -1) { |
| 576 | ERROR("write_destination: Lseek on destination " |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 577 | "failed because %s, offset=0x%llx\n", strerror(errno), |
| 578 | off); |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 579 | BAD_ERROR("Probably out of space on output %s\n", |
| 580 | block_device ? "block device" : "filesystem"); |
| 581 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 582 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 583 | if(write_bytes(fd, buff, bytes) == -1) |
Phillip Lougher | e8b536f | 2013-02-12 22:01:21 +0000 | [diff] [blame] | 584 | BAD_ERROR("Failed to write to output %s\n", |
| 585 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 586 | |
| 587 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
| 591 | long long write_inodes() |
| 592 | { |
| 593 | unsigned short c_byte; |
| 594 | int avail_bytes; |
| 595 | char *datap = data_cache; |
| 596 | long long start_bytes = bytes; |
| 597 | |
| 598 | while(cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 599 | if(inode_size - inode_bytes < |
| 600 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 601 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 602 | ((SQUASHFS_METADATA_SIZE << 1) + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 603 | if(it == NULL) |
| 604 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 605 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 606 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 607 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 608 | avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 609 | SQUASHFS_METADATA_SIZE : cache_bytes; |
| 610 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap, |
| 611 | avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 612 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 613 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 614 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 615 | total_inode_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 616 | datap += avail_bytes; |
| 617 | cache_bytes -= avail_bytes; |
| 618 | } |
| 619 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 620 | write_destination(fd, bytes, inode_bytes, inode_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 621 | bytes += inode_bytes; |
| 622 | |
| 623 | return start_bytes; |
| 624 | } |
| 625 | |
| 626 | |
| 627 | long long write_directories() |
| 628 | { |
| 629 | unsigned short c_byte; |
| 630 | int avail_bytes; |
| 631 | char *directoryp = directory_data_cache; |
| 632 | long long start_bytes = bytes; |
| 633 | |
| 634 | while(directory_cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 635 | if(directory_size - directory_bytes < |
| 636 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 637 | void *dt = realloc(directory_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 638 | directory_size + ((SQUASHFS_METADATA_SIZE << 1) |
| 639 | + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 640 | if(dt == NULL) |
| 641 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 642 | directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 643 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 644 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 645 | avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 646 | SQUASHFS_METADATA_SIZE : directory_cache_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 647 | c_byte = mangle(directory_table + directory_bytes + |
| 648 | BLOCK_OFFSET, directoryp, avail_bytes, |
| 649 | SQUASHFS_METADATA_SIZE, noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 650 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 651 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 652 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 653 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 654 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 655 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 656 | total_directory_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 657 | directoryp += avail_bytes; |
| 658 | directory_cache_bytes -= avail_bytes; |
| 659 | } |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 660 | write_destination(fd, bytes, directory_bytes, directory_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 661 | bytes += directory_bytes; |
| 662 | |
| 663 | return start_bytes; |
| 664 | } |
| 665 | |
| 666 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 667 | long long write_id_table() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 668 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 669 | unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 670 | unsigned int p[id_count]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 671 | int i; |
| 672 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 673 | 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] | 674 | for(i = 0; i < id_count; i++) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 675 | 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] | 676 | SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 677 | } |
| 678 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 679 | return generic_write_table(id_bytes, p, 0, NULL, noI); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 683 | struct id *get_id(unsigned int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 684 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 685 | int hash = ID_HASH(id); |
| 686 | struct id *entry = id_hash_table[hash]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 687 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 688 | for(; entry; entry = entry->next) |
| 689 | if(entry->id == id) |
| 690 | break; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 691 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 692 | return entry; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 696 | struct id *create_id(unsigned int id) |
| 697 | { |
| 698 | int hash = ID_HASH(id); |
| 699 | struct id *entry = malloc(sizeof(struct id)); |
| 700 | if(entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 701 | MEM_ERROR(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 702 | entry->id = id; |
| 703 | entry->index = id_count ++; |
| 704 | entry->flags = 0; |
| 705 | entry->next = id_hash_table[hash]; |
| 706 | id_hash_table[hash] = entry; |
| 707 | id_table[entry->index] = entry; |
| 708 | return entry; |
| 709 | } |
| 710 | |
| 711 | |
| 712 | unsigned int get_uid(unsigned int uid) |
| 713 | { |
| 714 | struct id *entry = get_id(uid); |
| 715 | |
| 716 | if(entry == NULL) { |
| 717 | if(id_count == SQUASHFS_IDS) |
| 718 | BAD_ERROR("Out of uids!\n"); |
| 719 | entry = create_id(uid); |
| 720 | } |
| 721 | |
| 722 | if((entry->flags & ISA_UID) == 0) { |
| 723 | entry->flags |= ISA_UID; |
| 724 | uid_count ++; |
| 725 | } |
| 726 | |
| 727 | return entry->index; |
| 728 | } |
| 729 | |
| 730 | |
| 731 | unsigned int get_guid(unsigned int guid) |
| 732 | { |
| 733 | struct id *entry = get_id(guid); |
| 734 | |
| 735 | if(entry == NULL) { |
| 736 | if(id_count == SQUASHFS_IDS) |
| 737 | BAD_ERROR("Out of gids!\n"); |
| 738 | entry = create_id(guid); |
| 739 | } |
| 740 | |
| 741 | if((entry->flags & ISA_GID) == 0) { |
| 742 | entry->flags |= ISA_GID; |
| 743 | guid_count ++; |
| 744 | } |
| 745 | |
| 746 | return entry->index; |
| 747 | } |
| 748 | |
| 749 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 750 | #define ALLOC_SIZE 128 |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 751 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 752 | char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size) |
| 753 | { |
| 754 | if(pathname == NULL) { |
| 755 | pathname = malloc(ALLOC_SIZE); |
| 756 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 757 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | for(;;) { |
| 761 | int res = snprintf(pathname, *size, "%s/%s", |
| 762 | dir_ent->our_dir->pathname, |
| 763 | dir_ent->source_name ? : dir_ent->name); |
| 764 | |
| 765 | if(res < 0) |
| 766 | BAD_ERROR("snprintf failed in pathname\n"); |
| 767 | else if(res >= *size) { |
| 768 | /* |
| 769 | * pathname is too small to contain the result, so |
| 770 | * increase it and try again |
| 771 | */ |
| 772 | *size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 773 | pathname = realloc(pathname, *size); |
| 774 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 775 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 776 | } else |
| 777 | break; |
| 778 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 779 | |
| 780 | return pathname; |
| 781 | } |
| 782 | |
| 783 | |
| 784 | char *pathname(struct dir_ent *dir_ent) |
| 785 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 786 | static char *pathname = NULL; |
| 787 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 788 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 789 | if (dir_ent->nonstandard_pathname) |
| 790 | return dir_ent->nonstandard_pathname; |
| 791 | |
| 792 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | |
| 796 | char *pathname_reader(struct dir_ent *dir_ent) |
| 797 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 798 | static char *pathname = NULL; |
| 799 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 800 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 801 | if (dir_ent->nonstandard_pathname) |
| 802 | return dir_ent->nonstandard_pathname; |
| 803 | |
| 804 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 808 | char *subpathname(struct dir_ent *dir_ent) |
| 809 | { |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 810 | static char *subpath = NULL; |
| 811 | static int size = ALLOC_SIZE; |
| 812 | int res; |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 813 | |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 814 | if(subpath == NULL) { |
| 815 | subpath = malloc(ALLOC_SIZE); |
| 816 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 817 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | for(;;) { |
| 821 | if(dir_ent->our_dir->subpath[0] != '\0') |
| 822 | res = snprintf(subpath, size, "%s/%s", |
| 823 | dir_ent->our_dir->subpath, dir_ent->name); |
| 824 | else |
| 825 | res = snprintf(subpath, size, "/%s", dir_ent->name); |
| 826 | |
| 827 | if(res < 0) |
| 828 | BAD_ERROR("snprintf failed in subpathname\n"); |
| 829 | else if(res >= size) { |
| 830 | /* |
| 831 | * subpath is too small to contain the result, so |
| 832 | * increase it and try again |
| 833 | */ |
| 834 | size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 835 | subpath = realloc(subpath, size); |
| 836 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 837 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 838 | } else |
| 839 | break; |
Phillip Lougher | 90e0825 | 2012-10-05 04:33:35 +0100 | [diff] [blame] | 840 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 841 | |
| 842 | return subpath; |
| 843 | } |
| 844 | |
| 845 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 846 | inline unsigned int get_inode_no(struct inode_info *inode) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 847 | { |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 848 | return inode->inode_number; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 852 | inline unsigned int get_parent_no(struct dir_info *dir) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 853 | { |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 854 | return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 858 | int create_inode(squashfs_inode *i_no, struct dir_info *dir_info, |
| 859 | struct dir_ent *dir_ent, int type, long long byte_size, |
| 860 | long long start_block, unsigned int offset, unsigned int *block_list, |
| 861 | struct fragment *fragment, struct directory *dir_in, long long sparse) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 862 | { |
| 863 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 8973a12 | 2010-12-31 09:52:45 +0000 | [diff] [blame] | 864 | union squashfs_inode_header inode_header; |
plougher | 857d0dd | 2010-12-31 21:03:13 +0000 | [diff] [blame] | 865 | struct squashfs_base_inode_header *base = &inode_header.base; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 866 | void *inode; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 867 | char *filename = pathname(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 868 | int nlink = dir_ent->inode->nlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 869 | int xattr = read_xattrs(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 870 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 871 | switch(type) { |
| 872 | case SQUASHFS_FILE_TYPE: |
| 873 | if(dir_ent->inode->nlink > 1 || |
| 874 | byte_size >= (1LL << 32) || |
| 875 | start_block >= (1LL << 32) || |
| 876 | sparse || IS_XATTR(xattr)) |
| 877 | type = SQUASHFS_LREG_TYPE; |
| 878 | break; |
| 879 | case SQUASHFS_DIR_TYPE: |
| 880 | if(dir_info->dir_is_ldir || IS_XATTR(xattr)) |
| 881 | type = SQUASHFS_LDIR_TYPE; |
| 882 | break; |
| 883 | case SQUASHFS_SYMLINK_TYPE: |
| 884 | if(IS_XATTR(xattr)) |
| 885 | type = SQUASHFS_LSYMLINK_TYPE; |
| 886 | break; |
| 887 | case SQUASHFS_BLKDEV_TYPE: |
| 888 | if(IS_XATTR(xattr)) |
| 889 | type = SQUASHFS_LBLKDEV_TYPE; |
| 890 | break; |
| 891 | case SQUASHFS_CHRDEV_TYPE: |
| 892 | if(IS_XATTR(xattr)) |
| 893 | type = SQUASHFS_LCHRDEV_TYPE; |
| 894 | break; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 895 | case SQUASHFS_FIFO_TYPE: |
| 896 | if(IS_XATTR(xattr)) |
| 897 | type = SQUASHFS_LFIFO_TYPE; |
| 898 | break; |
| 899 | case SQUASHFS_SOCKET_TYPE: |
| 900 | if(IS_XATTR(xattr)) |
| 901 | type = SQUASHFS_LSOCKET_TYPE; |
| 902 | break; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 903 | } |
| 904 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 905 | base->mode = SQUASHFS_MODE(buf->st_mode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 906 | base->uid = get_uid((unsigned int) global_uid == -1 ? |
| 907 | buf->st_uid : global_uid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 908 | base->inode_type = type; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 909 | base->guid = get_guid((unsigned int) global_gid == -1 ? |
| 910 | buf->st_gid : global_gid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 911 | base->mtime = buf->st_mtime; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 912 | base->inode_number = get_inode_no(dir_ent->inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 913 | |
| 914 | if(type == SQUASHFS_FILE_TYPE) { |
| 915 | int i; |
plougher | 8701ed6 | 2010-12-31 20:38:38 +0000 | [diff] [blame] | 916 | struct squashfs_reg_inode_header *reg = &inode_header.reg; |
| 917 | size_t off = offsetof(struct squashfs_reg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 918 | |
| 919 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 920 | reg->file_size = byte_size; |
| 921 | reg->start_block = start_block; |
| 922 | reg->fragment = fragment->index; |
| 923 | reg->offset = fragment->offset; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 924 | SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode); |
| 925 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 926 | TRACE("File inode, file_size %lld, start_block 0x%llx, blocks " |
| 927 | "%d, fragment %d, offset %d, size %d\n", byte_size, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 928 | start_block, offset, fragment->index, fragment->offset, |
| 929 | fragment->size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 930 | for(i = 0; i < offset; i++) |
| 931 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 932 | } |
| 933 | else if(type == SQUASHFS_LREG_TYPE) { |
| 934 | int i; |
plougher | 1e6ac4a | 2010-12-31 20:42:02 +0000 | [diff] [blame] | 935 | struct squashfs_lreg_inode_header *reg = &inode_header.lreg; |
| 936 | size_t off = offsetof(struct squashfs_lreg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 937 | |
| 938 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 939 | reg->nlink = nlink; |
| 940 | reg->file_size = byte_size; |
| 941 | reg->start_block = start_block; |
| 942 | reg->fragment = fragment->index; |
| 943 | reg->offset = fragment->offset; |
plougher | f5a674d | 2009-03-25 05:38:27 +0000 | [diff] [blame] | 944 | if(sparse && sparse >= byte_size) |
| 945 | sparse = byte_size - 1; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 946 | reg->sparse = sparse; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 947 | reg->xattr = xattr; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 948 | SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode); |
| 949 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 950 | TRACE("Long file inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 951 | "blocks %d, fragment %d, offset %d, size %d, nlink %d" |
| 952 | "\n", byte_size, start_block, offset, fragment->index, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 953 | fragment->offset, fragment->size, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 954 | for(i = 0; i < offset; i++) |
| 955 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 956 | } |
| 957 | else if(type == SQUASHFS_LDIR_TYPE) { |
| 958 | int i; |
| 959 | unsigned char *p; |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 960 | struct squashfs_ldir_inode_header *dir = &inode_header.ldir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 961 | struct cached_dir_index *index = dir_in->index; |
| 962 | unsigned int i_count = dir_in->i_count; |
| 963 | unsigned int i_size = dir_in->i_size; |
| 964 | |
| 965 | if(byte_size >= 1 << 27) |
| 966 | BAD_ERROR("directory greater than 2^27-1 bytes!\n"); |
| 967 | |
| 968 | inode = get_inode(sizeof(*dir) + i_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 969 | dir->inode_type = SQUASHFS_LDIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 970 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 971 | dir->file_size = byte_size; |
| 972 | dir->offset = offset; |
| 973 | dir->start_block = start_block; |
| 974 | dir->i_count = i_count; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 975 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 976 | dir->xattr = xattr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 977 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 978 | SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode); |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 979 | p = inode + offsetof(struct squashfs_ldir_inode_header, index); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 980 | for(i = 0; i < i_count; i++) { |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 981 | SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p); |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 982 | p += offsetof(struct squashfs_dir_index, name); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 983 | memcpy(p, index[i].name, index[i].index.size + 1); |
| 984 | p += index[i].index.size + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 985 | } |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 986 | TRACE("Long directory inode, file_size %lld, start_block " |
| 987 | "0x%llx, offset 0x%x, nlink %d\n", byte_size, |
| 988 | start_block, offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 989 | } |
| 990 | else if(type == SQUASHFS_DIR_TYPE) { |
plougher | 9d80a60 | 2010-12-31 20:47:24 +0000 | [diff] [blame] | 991 | struct squashfs_dir_inode_header *dir = &inode_header.dir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 992 | |
| 993 | inode = get_inode(sizeof(*dir)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 994 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 995 | dir->file_size = byte_size; |
| 996 | dir->offset = offset; |
| 997 | dir->start_block = start_block; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 998 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 999 | SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1000 | TRACE("Directory inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1001 | "offset 0x%x, nlink %d\n", byte_size, start_block, |
| 1002 | offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1003 | } |
| 1004 | else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) { |
plougher | c70c633 | 2010-12-31 20:11:09 +0000 | [diff] [blame] | 1005 | struct squashfs_dev_inode_header *dev = &inode_header.dev; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1006 | unsigned int major = major(buf->st_rdev); |
| 1007 | unsigned int minor = minor(buf->st_rdev); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1008 | |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1009 | if(major > 0xfff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1010 | ERROR("Major %d out of range in device node %s, " |
| 1011 | "truncating to %d\n", major, filename, |
| 1012 | major & 0xfff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1013 | major &= 0xfff; |
| 1014 | } |
| 1015 | if(minor > 0xfffff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1016 | ERROR("Minor %d out of range in device node %s, " |
| 1017 | "truncating to %d\n", minor, filename, |
| 1018 | minor & 0xfffff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1019 | minor &= 0xfffff; |
| 1020 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1021 | inode = get_inode(sizeof(*dev)); |
| 1022 | dev->nlink = nlink; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1023 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1024 | ((minor & ~0xff) << 12); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1025 | SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1026 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1027 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1028 | else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) { |
plougher | 0b4ee5b | 2010-12-31 20:14:00 +0000 | [diff] [blame] | 1029 | struct squashfs_ldev_inode_header *dev = &inode_header.ldev; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1030 | unsigned int major = major(buf->st_rdev); |
| 1031 | unsigned int minor = minor(buf->st_rdev); |
| 1032 | |
| 1033 | if(major > 0xfff) { |
| 1034 | ERROR("Major %d out of range in device node %s, " |
| 1035 | "truncating to %d\n", major, filename, |
| 1036 | major & 0xfff); |
| 1037 | major &= 0xfff; |
| 1038 | } |
| 1039 | if(minor > 0xfffff) { |
| 1040 | ERROR("Minor %d out of range in device node %s, " |
| 1041 | "truncating to %d\n", minor, filename, |
| 1042 | minor & 0xfffff); |
| 1043 | minor &= 0xfffff; |
| 1044 | } |
| 1045 | inode = get_inode(sizeof(*dev)); |
| 1046 | dev->nlink = nlink; |
| 1047 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1048 | ((minor & ~0xff) << 12); |
| 1049 | dev->xattr = xattr; |
| 1050 | SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode); |
| 1051 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
| 1052 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1053 | else if(type == SQUASHFS_SYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1054 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1055 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1056 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1057 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1058 | |
plougher | 5cf38b8 | 2010-12-16 04:55:32 +0000 | [diff] [blame] | 1059 | byte = readlink(filename, buff, 65536); |
| 1060 | if(byte == -1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1061 | ERROR("Failed to read symlink %s, creating empty " |
| 1062 | "symlink\n", filename); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1063 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | if(byte == 65536) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1067 | ERROR("Symlink %s is greater than 65536 bytes! " |
| 1068 | "Creating empty symlink\n", filename); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1069 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | inode = get_inode(sizeof(*symlink) + byte); |
| 1073 | symlink->nlink = nlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1074 | symlink->symlink_size = byte; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1075 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1076 | strncpy(inode + off, buff, byte); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1077 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1078 | nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1079 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1080 | else if(type == SQUASHFS_LSYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1081 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1082 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1083 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1084 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1085 | |
plougher | df2b9aa | 2010-12-16 04:56:23 +0000 | [diff] [blame] | 1086 | byte = readlink(filename, buff, 65536); |
| 1087 | if(byte == -1) { |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1088 | ERROR("Failed to read symlink %s, creating empty " |
| 1089 | "symlink\n", filename); |
| 1090 | byte = 0; |
| 1091 | } |
| 1092 | |
| 1093 | if(byte == 65536) { |
| 1094 | ERROR("Symlink %s is greater than 65536 bytes! " |
| 1095 | "Creating empty symlink\n", filename); |
| 1096 | byte = 0; |
| 1097 | } |
| 1098 | |
| 1099 | inode = get_inode(sizeof(*symlink) + byte + |
| 1100 | sizeof(unsigned int)); |
| 1101 | symlink->nlink = nlink; |
| 1102 | symlink->symlink_size = byte; |
| 1103 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1104 | strncpy(inode + off, buff, byte); |
| 1105 | SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1); |
| 1106 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1107 | nlink); |
| 1108 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1109 | else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) { |
plougher | e56b986 | 2010-12-31 10:58:35 +0000 | [diff] [blame] | 1110 | struct squashfs_ipc_inode_header *ipc = &inode_header.ipc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1111 | |
| 1112 | inode = get_inode(sizeof(*ipc)); |
| 1113 | ipc->nlink = nlink; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1114 | SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1115 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1116 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1117 | } |
| 1118 | else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) { |
plougher | aa0d122 | 2010-12-31 20:06:24 +0000 | [diff] [blame] | 1119 | struct squashfs_lipc_inode_header *ipc = &inode_header.lipc; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1120 | |
| 1121 | inode = get_inode(sizeof(*ipc)); |
| 1122 | ipc->nlink = nlink; |
| 1123 | ipc->xattr = xattr; |
| 1124 | SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode); |
| 1125 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1126 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1127 | } else |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1128 | BAD_ERROR("Unrecognised inode %d in create_inode\n", type); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1129 | |
| 1130 | *i_no = MKINODE(inode); |
| 1131 | inode_count ++; |
| 1132 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1133 | TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type, |
| 1134 | base->uid, base->guid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1135 | |
| 1136 | return TRUE; |
| 1137 | } |
| 1138 | |
| 1139 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1140 | void add_dir(squashfs_inode inode, unsigned int inode_number, char *name, |
| 1141 | int type, struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1142 | { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 1143 | unsigned char *buff; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1144 | struct squashfs_dir_entry idir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1145 | unsigned int start_block = inode >> 16; |
| 1146 | unsigned int offset = inode & 0xffff; |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1147 | unsigned int size = strlen(name); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1148 | size_t name_off = offsetof(struct squashfs_dir_entry, name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1149 | |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1150 | if(size > SQUASHFS_NAME_LEN) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1151 | size = SQUASHFS_NAME_LEN; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1152 | ERROR("Filename is greater than %d characters, truncating! ..." |
| 1153 | "\n", SQUASHFS_NAME_LEN); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1156 | if(dir->p + sizeof(struct squashfs_dir_entry) + size + |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1157 | sizeof(struct squashfs_dir_header) |
| 1158 | >= dir->buff + dir->size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1159 | buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1160 | if(buff == NULL) |
| 1161 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1162 | |
| 1163 | dir->p = (dir->p - dir->buff) + buff; |
| 1164 | if(dir->entry_count_p) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1165 | dir->entry_count_p = (dir->entry_count_p - dir->buff + |
| 1166 | buff); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1167 | dir->index_count_p = dir->index_count_p - dir->buff + buff; |
| 1168 | dir->buff = buff; |
| 1169 | } |
| 1170 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1171 | if(dir->entry_count == 256 || start_block != dir->start_block || |
| 1172 | ((dir->entry_count_p != NULL) && |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1173 | ((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1174 | dir->index_count_p) > SQUASHFS_METADATA_SIZE)) || |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1175 | ((long long) inode_number - dir->inode_number) > 32767 |
| 1176 | || ((long long) inode_number - dir->inode_number) |
| 1177 | < -32768) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1178 | if(dir->entry_count_p) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1179 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1180 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1181 | if((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1182 | dir->index_count_p) > |
| 1183 | SQUASHFS_METADATA_SIZE) { |
| 1184 | if(dir->i_count % I_COUNT_SIZE == 0) { |
| 1185 | dir->index = realloc(dir->index, |
| 1186 | (dir->i_count + I_COUNT_SIZE) * |
| 1187 | sizeof(struct cached_dir_index)); |
| 1188 | if(dir->index == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1189 | MEM_ERROR(); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1190 | } |
| 1191 | dir->index[dir->i_count].index.index = |
| 1192 | dir->p - dir->buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1193 | dir->index[dir->i_count].index.size = size - 1; |
| 1194 | dir->index[dir->i_count++].name = name; |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1195 | dir->i_size += sizeof(struct squashfs_dir_index) |
| 1196 | + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1197 | dir->index_count_p = dir->p; |
| 1198 | } |
| 1199 | |
| 1200 | dir_header.count = dir->entry_count - 1; |
| 1201 | dir_header.start_block = dir->start_block; |
| 1202 | dir_header.inode_number = dir->inode_number; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1203 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1204 | dir->entry_count_p); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1205 | |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | dir->entry_count_p = dir->p; |
| 1210 | dir->start_block = start_block; |
| 1211 | dir->entry_count = 0; |
| 1212 | dir->inode_number = inode_number; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1213 | dir->p += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1216 | idir.offset = offset; |
| 1217 | idir.type = type; |
| 1218 | idir.size = size - 1; |
| 1219 | idir.inode_number = ((long long) inode_number - dir->inode_number); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1220 | SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p); |
| 1221 | strncpy((char *) dir->p + name_off, name, size); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1222 | dir->p += sizeof(struct squashfs_dir_entry) + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1223 | dir->entry_count ++; |
| 1224 | } |
| 1225 | |
| 1226 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1227 | void write_dir(squashfs_inode *inode, struct dir_info *dir_info, |
| 1228 | struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1229 | { |
| 1230 | unsigned int dir_size = dir->p - dir->buff; |
plougher | 4627ca3 | 2010-12-16 05:03:55 +0000 | [diff] [blame] | 1231 | int data_space = directory_cache_size - directory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1232 | unsigned int directory_block, directory_offset, i_count, index; |
| 1233 | unsigned short c_byte; |
| 1234 | |
| 1235 | if(data_space < dir_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1236 | int realloc_size = directory_cache_size == 0 ? |
| 1237 | ((dir_size + SQUASHFS_METADATA_SIZE) & |
| 1238 | ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1239 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1240 | void *dc = realloc(directory_data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1241 | directory_cache_size + realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1242 | if(dc == NULL) |
| 1243 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1244 | directory_cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1245 | directory_data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | if(dir_size) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1249 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1250 | |
| 1251 | dir_header.count = dir->entry_count - 1; |
| 1252 | dir_header.start_block = dir->start_block; |
| 1253 | dir_header.inode_number = dir->inode_number; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1254 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1255 | memcpy(directory_data_cache + directory_cache_bytes, dir->buff, |
| 1256 | dir_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1257 | } |
| 1258 | directory_offset = directory_cache_bytes; |
| 1259 | directory_block = directory_bytes; |
| 1260 | directory_cache_bytes += dir_size; |
| 1261 | i_count = 0; |
| 1262 | index = SQUASHFS_METADATA_SIZE - directory_offset; |
| 1263 | |
| 1264 | while(1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1265 | while(i_count < dir->i_count && |
| 1266 | dir->index[i_count].index.index < index) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1267 | dir->index[i_count++].index.start_block = |
| 1268 | directory_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1269 | index += SQUASHFS_METADATA_SIZE; |
| 1270 | |
| 1271 | if(directory_cache_bytes < SQUASHFS_METADATA_SIZE) |
| 1272 | break; |
| 1273 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1274 | if((directory_size - directory_bytes) < |
| 1275 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1276 | void *dt = realloc(directory_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1277 | directory_size + (SQUASHFS_METADATA_SIZE << 1) |
| 1278 | + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1279 | if(dt == NULL) |
| 1280 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1281 | directory_size += SQUASHFS_METADATA_SIZE << 1; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1282 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1285 | c_byte = mangle(directory_table + directory_bytes + |
| 1286 | BLOCK_OFFSET, directory_data_cache, |
| 1287 | SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, |
| 1288 | noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1289 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1290 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1291 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1292 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1293 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1294 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1295 | total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 1296 | memmove(directory_data_cache, directory_data_cache + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1297 | SQUASHFS_METADATA_SIZE, directory_cache_bytes - |
| 1298 | SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1299 | directory_cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 1300 | } |
| 1301 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1302 | create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE, |
| 1303 | dir_size + 3, directory_block, directory_offset, NULL, NULL, |
| 1304 | dir, 0); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1305 | |
| 1306 | #ifdef SQUASHFS_TRACE |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1307 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1308 | unsigned char *dirp; |
| 1309 | int count; |
| 1310 | |
| 1311 | TRACE("Directory contents of inode 0x%llx\n", *inode); |
| 1312 | dirp = dir->buff; |
| 1313 | while(dirp < dir->p) { |
| 1314 | char buffer[SQUASHFS_NAME_LEN + 1]; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1315 | struct squashfs_dir_entry idir, *idirp; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1316 | struct squashfs_dir_header dirh; |
| 1317 | SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1318 | &dirh); |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1319 | count = dirh.count + 1; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1320 | dirp += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1321 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1322 | TRACE("\tStart block 0x%x, count %d\n", |
| 1323 | dirh.start_block, count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1324 | |
| 1325 | while(count--) { |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1326 | idirp = (struct squashfs_dir_entry *) dirp; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1327 | SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1328 | strncpy(buffer, idirp->name, idir.size + 1); |
| 1329 | buffer[idir.size + 1] = '\0'; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1330 | TRACE("\t\tname %s, inode offset 0x%x, type " |
| 1331 | "%d\n", buffer, idir.offset, idir.type); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1332 | dirp += sizeof(struct squashfs_dir_entry) + idir.size + |
plougher | e8b26f6 | 2010-12-16 05:06:00 +0000 | [diff] [blame] | 1333 | 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | #endif |
| 1338 | dir_count ++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1342 | struct file_buffer *get_fragment(struct fragment *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1343 | { |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1344 | struct squashfs_fragment_entry *disk_fragment; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1345 | int res, size; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1346 | long long start_block; |
| 1347 | struct file_buffer *buffer, *compressed_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1348 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1349 | if(fragment->index == SQUASHFS_INVALID_FRAG) |
| 1350 | return NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1351 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1352 | buffer = cache_lookup(fragment_buffer, fragment->index); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1353 | if(buffer) |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1354 | return buffer; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1355 | |
Phillip Lougher | 4570201 | 2013-04-30 05:19:54 +0100 | [diff] [blame] | 1356 | compressed_buffer = cache_lookup(writer_buffer, |
Phillip Lougher | fd99272 | 2013-05-19 04:46:57 +0100 | [diff] [blame] | 1357 | FRAG_INDEX((long long) fragment->index)); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1358 | |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 1359 | buffer = cache_get(fragment_buffer, fragment->index); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1360 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1361 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1362 | pthread_mutex_lock(&fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1363 | disk_fragment = &fragment_table[fragment->index]; |
| 1364 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1365 | start_block = disk_fragment->start_block; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1366 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1367 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1368 | if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1369 | int error; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1370 | char *data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1371 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1372 | if(compressed_buffer) |
| 1373 | data = compressed_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1374 | else { |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 1375 | data = read_from_disk(start_block, size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1376 | if(data == NULL) { |
| 1377 | ERROR("Failed to read fragment from output" |
| 1378 | " filesystem\n"); |
| 1379 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1380 | } |
| 1381 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1382 | |
plougher | b48442b | 2010-12-31 07:57:54 +0000 | [diff] [blame] | 1383 | res = compressor_uncompress(comp, buffer->data, data, size, |
| 1384 | block_size, &error); |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 1385 | if(res == -1) |
| 1386 | BAD_ERROR("%s uncompress failed with error code %d\n", |
| 1387 | comp->name, error); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1388 | } else if(compressed_buffer) |
| 1389 | memcpy(buffer->data, compressed_buffer->data, size); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1390 | else { |
| 1391 | res = read_fs_bytes(fd, start_block, size, buffer->data); |
Phillip Lougher | 9451938 | 2013-03-06 02:43:42 +0000 | [diff] [blame] | 1392 | if(res == 0) { |
| 1393 | ERROR("Failed to read fragment from output " |
| 1394 | "filesystem\n"); |
| 1395 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1396 | } |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1397 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1398 | |
plougher | 03859fa | 2009-03-31 03:18:18 +0000 | [diff] [blame] | 1399 | cache_block_put(compressed_buffer); |
| 1400 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1401 | return buffer; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1404 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1405 | int lock_fragments() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1406 | { |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1407 | int count; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1408 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1409 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1410 | fragments_locked = TRUE; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1411 | count = fragments_outstanding; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1412 | pthread_cleanup_pop(1); |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1413 | return count; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | void unlock_fragments() |
| 1418 | { |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1419 | int frg, size; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1420 | struct file_buffer *write_buffer; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1421 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1422 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1423 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | 71ad964 | 2013-05-25 05:03:48 +0100 | [diff] [blame] | 1424 | |
| 1425 | /* |
| 1426 | * Note queue_empty() is inherently racy with respect to concurrent |
| 1427 | * queue get and pushes. We avoid this because we're holding the |
| 1428 | * fragment_mutex which ensures no other threads can be using the |
| 1429 | * queue at this time. |
| 1430 | */ |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1431 | while(!queue_empty(locked_fragment)) { |
| 1432 | write_buffer = queue_get(locked_fragment); |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1433 | frg = write_buffer->block; |
| 1434 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size); |
| 1435 | fragment_table[frg].start_block = bytes; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1436 | write_buffer->block = bytes; |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1437 | bytes += size; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1438 | fragments_outstanding --; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1439 | queue_put(to_writer, write_buffer); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1440 | TRACE("fragment_locked writing fragment %d, compressed size %d" |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1441 | "\n", frg, size); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1442 | } |
| 1443 | fragments_locked = FALSE; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1444 | pthread_cleanup_pop(1); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1447 | /* Called with the fragment_mutex locked */ |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1448 | void add_pending_fragment(struct file_buffer *write_buffer, int c_byte, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1449 | int fragment) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1450 | { |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1451 | fragment_table[fragment].size = c_byte; |
| 1452 | write_buffer->block = fragment; |
| 1453 | |
| 1454 | queue_put(locked_fragment, write_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1458 | void write_fragment(struct file_buffer *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1459 | { |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1460 | if(fragment == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1461 | return; |
| 1462 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1463 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1464 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1465 | fragment_table[fragment->block].unused = 0; |
| 1466 | fragments_outstanding ++; |
| 1467 | queue_put(to_frag, fragment); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1468 | pthread_cleanup_pop(1); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | |
| 1472 | struct file_buffer *allocate_fragment() |
| 1473 | { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 1474 | struct file_buffer *fragment = cache_get(fragment_buffer, fragments); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1475 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1476 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1477 | pthread_mutex_lock(&fragment_mutex); |
| 1478 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1479 | if(fragments % FRAG_SIZE == 0) { |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1480 | void *ft = realloc(fragment_table, (fragments + |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1481 | FRAG_SIZE) * sizeof(struct squashfs_fragment_entry)); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 1482 | if(ft == NULL) |
| 1483 | MEM_ERROR(); |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1484 | fragment_table = ft; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1485 | } |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1486 | |
| 1487 | fragment->size = 0; |
| 1488 | fragment->block = fragments ++; |
| 1489 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1490 | pthread_cleanup_pop(1); |
| 1491 | |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1492 | return fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 1495 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1496 | static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0}; |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 1497 | |
| 1498 | |
| 1499 | void free_fragment(struct fragment *fragment) |
| 1500 | { |
| 1501 | if(fragment != &empty_fragment) |
| 1502 | free(fragment); |
| 1503 | } |
| 1504 | |
| 1505 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1506 | struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer, |
| 1507 | struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1508 | { |
| 1509 | struct fragment *ffrg; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1510 | struct file_buffer **fragment; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1511 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1512 | if(file_buffer == NULL || file_buffer->size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1513 | return &empty_fragment; |
| 1514 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1515 | fragment = eval_frag_actions(dir_ent); |
| 1516 | |
| 1517 | if((*fragment) && (*fragment)->size + file_buffer->size > block_size) { |
| 1518 | write_fragment(*fragment); |
| 1519 | *fragment = NULL; |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1520 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1521 | |
plougher | e7e6e83 | 2010-12-16 05:08:06 +0000 | [diff] [blame] | 1522 | ffrg = malloc(sizeof(struct fragment)); |
| 1523 | if(ffrg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1524 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1525 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1526 | if(*fragment == NULL) |
| 1527 | *fragment = allocate_fragment(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1528 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1529 | ffrg->index = (*fragment)->block; |
| 1530 | ffrg->offset = (*fragment)->size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1531 | ffrg->size = file_buffer->size; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1532 | memcpy((*fragment)->data + (*fragment)->size, file_buffer->data, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1533 | file_buffer->size); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1534 | (*fragment)->size += file_buffer->size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1535 | |
| 1536 | return ffrg; |
| 1537 | } |
| 1538 | |
| 1539 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1540 | long long generic_write_table(int length, void *buffer, int length2, |
| 1541 | void *buffer2, int uncompressed) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1542 | { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1543 | int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) / |
| 1544 | SQUASHFS_METADATA_SIZE; |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1545 | long long *list, start_bytes; |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1546 | int compressed_size, i, list_size = meta_blocks * sizeof(long long); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1547 | unsigned short c_byte; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1548 | char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2]; |
| 1549 | |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1550 | #ifdef SQUASHFS_TRACE |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1551 | long long obytes = bytes; |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1552 | int olength = length; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1553 | #endif |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1554 | |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1555 | list = malloc(list_size); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1556 | if(list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1557 | MEM_ERROR(); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1558 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1559 | for(i = 0; i < meta_blocks; i++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1560 | int avail_bytes = length > SQUASHFS_METADATA_SIZE ? |
| 1561 | SQUASHFS_METADATA_SIZE : length; |
| 1562 | c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i * |
| 1563 | SQUASHFS_METADATA_SIZE , avail_bytes, |
| 1564 | SQUASHFS_METADATA_SIZE, uncompressed, 0); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1565 | SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1566 | list[i] = bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1567 | compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1568 | BLOCK_OFFSET; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1569 | TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes, |
| 1570 | compressed_size); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1571 | write_destination(fd, bytes, compressed_size, cbuffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1572 | bytes += compressed_size; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1573 | total_bytes += avail_bytes; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1574 | length -= avail_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1577 | start_bytes = bytes; |
| 1578 | if(length2) { |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1579 | write_destination(fd, bytes, length2, buffer2); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1580 | bytes += length2; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1581 | total_bytes += length2; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1584 | SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks); |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1585 | write_destination(fd, bytes, list_size, list); |
| 1586 | bytes += list_size; |
| 1587 | total_bytes += list_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1588 | |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1589 | TRACE("generic_write_table: total uncompressed %d compressed %lld\n", |
| 1590 | olength, bytes - obytes); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1591 | |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1592 | free(list); |
| 1593 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1594 | return start_bytes; |
| 1595 | } |
| 1596 | |
| 1597 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1598 | long long write_fragment_table() |
| 1599 | { |
| 1600 | unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1601 | int i; |
| 1602 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1603 | TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments, |
| 1604 | frag_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1605 | for(i = 0; i < fragments; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1606 | TRACE("write_fragment_table: fragment %d, start_block 0x%llx, " |
| 1607 | "size %d\n", i, fragment_table[i].start_block, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1608 | fragment_table[i].size); |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1609 | SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1612 | return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1616 | char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1617 | char *read_from_disk(long long start, unsigned int avail_bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1618 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1619 | int res; |
| 1620 | |
| 1621 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1622 | if(res == 0) |
| 1623 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1624 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1625 | return read_from_file_buffer; |
| 1626 | } |
| 1627 | |
| 1628 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1629 | char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE]; |
| 1630 | char *read_from_disk2(long long start, unsigned int avail_bytes) |
| 1631 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1632 | int res; |
| 1633 | |
| 1634 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1635 | if(res == 0) |
| 1636 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1637 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1638 | return read_from_file_buffer2; |
| 1639 | } |
| 1640 | |
| 1641 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1642 | /* |
| 1643 | * Compute 16 bit BSD checksum over the data |
| 1644 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1645 | unsigned short get_checksum(char *buff, int bytes, unsigned short chksum) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1646 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1647 | unsigned char *b = (unsigned char *) buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1648 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1649 | while(bytes --) { |
| 1650 | chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1; |
| 1651 | chksum += *b++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | return chksum; |
| 1655 | } |
| 1656 | |
| 1657 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1658 | unsigned short get_checksum_disk(long long start, long long l, |
| 1659 | unsigned int *blocks) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1660 | { |
| 1661 | unsigned short chksum = 0; |
| 1662 | unsigned int bytes; |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1663 | struct file_buffer *write_buffer; |
| 1664 | int i; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1665 | |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1666 | for(i = 0; l; i++) { |
| 1667 | bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]); |
| 1668 | if(bytes == 0) /* sparse block */ |
| 1669 | continue; |
| 1670 | write_buffer = cache_lookup(writer_buffer, start); |
| 1671 | if(write_buffer) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1672 | chksum = get_checksum(write_buffer->data, bytes, |
| 1673 | chksum); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1674 | cache_block_put(write_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1675 | } else { |
| 1676 | void *data = read_from_disk(start, bytes); |
| 1677 | if(data == NULL) { |
| 1678 | ERROR("Failed to checksum data from output" |
| 1679 | " filesystem\n"); |
| 1680 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1681 | } |
| 1682 | |
| 1683 | chksum = get_checksum(data, bytes, chksum); |
| 1684 | } |
| 1685 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1686 | l -= bytes; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1687 | start += bytes; |
| 1688 | } |
| 1689 | |
| 1690 | return chksum; |
| 1691 | } |
| 1692 | |
| 1693 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1694 | unsigned short get_checksum_mem(char *buff, int bytes) |
| 1695 | { |
| 1696 | return get_checksum(buff, bytes, 0); |
| 1697 | } |
| 1698 | |
| 1699 | |
| 1700 | unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer) |
| 1701 | { |
| 1702 | if(file_buffer == NULL) |
| 1703 | return 0; |
| 1704 | else |
| 1705 | return get_checksum(file_buffer->data, file_buffer->size, 0); |
| 1706 | } |
| 1707 | |
| 1708 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1709 | #define DUP_HASH(a) (a & 0xffff) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1710 | 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] | 1711 | unsigned int *block_listp, int blocks, unsigned int fragment, |
| 1712 | int offset, int bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1713 | { |
| 1714 | struct fragment *frg; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1715 | unsigned int *block_list = block_listp; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1716 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1717 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1718 | if(!duplicate_checking || file_size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1719 | return; |
| 1720 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1721 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) { |
| 1722 | if(file_size != dupl_ptr->file_size) |
| 1723 | continue; |
| 1724 | if(blocks != 0 && start != dupl_ptr->start) |
| 1725 | continue; |
| 1726 | if(fragment != dupl_ptr->fragment->index) |
| 1727 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1728 | if(fragment != SQUASHFS_INVALID_FRAG && (offset != |
| 1729 | dupl_ptr->fragment->offset || bytes != |
| 1730 | dupl_ptr->fragment->size)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1731 | continue; |
| 1732 | return; |
| 1733 | } |
| 1734 | |
plougher | 3bb279c | 2010-12-16 05:10:03 +0000 | [diff] [blame] | 1735 | frg = malloc(sizeof(struct fragment)); |
| 1736 | if(frg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1737 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1738 | |
| 1739 | frg->index = fragment; |
| 1740 | frg->offset = offset; |
| 1741 | frg->size = bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1742 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1743 | add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0, FALSE); |
| 1744 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1745 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1746 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1747 | int pre_duplicate(long long file_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1748 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1749 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1750 | |
| 1751 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1752 | if(dupl_ptr->file_size == file_size) |
| 1753 | return TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1754 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1755 | return FALSE; |
| 1756 | } |
| 1757 | |
| 1758 | |
| 1759 | int pre_duplicate_frag(long long file_size, unsigned short checksum) |
| 1760 | { |
| 1761 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 1762 | |
| 1763 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1764 | if(file_size == dupl_ptr->file_size && file_size == |
| 1765 | dupl_ptr->fragment->size) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1766 | if(dupl_ptr->checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1767 | struct file_buffer *frag_buffer = |
| 1768 | get_fragment(dupl_ptr->fragment); |
| 1769 | dupl_ptr->checksum = |
| 1770 | get_checksum_disk(dupl_ptr->start, |
| 1771 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 1772 | dupl_ptr->fragment_checksum = |
| 1773 | get_checksum_mem(frag_buffer->data + |
| 1774 | dupl_ptr->fragment->offset, file_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1775 | cache_block_put(frag_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1776 | dupl_ptr->checksum_flag = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1777 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1778 | if(dupl_ptr->fragment_checksum == checksum) |
| 1779 | return TRUE; |
| 1780 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1781 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1782 | return FALSE; |
| 1783 | } |
| 1784 | |
| 1785 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1786 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 1787 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 1788 | unsigned short checksum, unsigned short fragment_checksum, |
| 1789 | int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1790 | { |
plougher | 51ef9ae | 2010-12-16 05:14:28 +0000 | [diff] [blame] | 1791 | struct file_info *dupl_ptr = malloc(sizeof(struct file_info)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1792 | |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1793 | if(dupl_ptr == NULL) |
| 1794 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1795 | |
| 1796 | dupl_ptr->file_size = file_size; |
| 1797 | dupl_ptr->bytes = bytes; |
| 1798 | dupl_ptr->block_list = block_list; |
| 1799 | dupl_ptr->start = start; |
| 1800 | dupl_ptr->fragment = fragment; |
| 1801 | dupl_ptr->checksum = checksum; |
| 1802 | dupl_ptr->fragment_checksum = fragment_checksum; |
| 1803 | dupl_ptr->checksum_flag = checksum_flag; |
| 1804 | dupl_ptr->next = dupl[DUP_HASH(file_size)]; |
| 1805 | dupl[DUP_HASH(file_size)] = dupl_ptr; |
| 1806 | dup_files ++; |
| 1807 | |
| 1808 | return dupl_ptr; |
| 1809 | } |
| 1810 | |
| 1811 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1812 | struct file_info *duplicate(long long file_size, long long bytes, |
| 1813 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 1814 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 1815 | unsigned short fragment_checksum, int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1816 | { |
| 1817 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 1818 | int frag_bytes = file_buffer ? file_buffer->size : 0; |
| 1819 | |
| 1820 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 1821 | if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes |
| 1822 | && frag_bytes == dupl_ptr->fragment->size) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1823 | long long target_start, dup_start = dupl_ptr->start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1824 | int block; |
| 1825 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1826 | if(memcmp(*block_list, dupl_ptr->block_list, blocks * |
| 1827 | sizeof(unsigned int)) != 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 1828 | continue; |
| 1829 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1830 | if(checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1831 | checksum = get_checksum_disk(*start, bytes, |
| 1832 | *block_list); |
| 1833 | fragment_checksum = |
| 1834 | get_checksum_mem_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1835 | checksum_flag = TRUE; |
| 1836 | } |
| 1837 | |
| 1838 | if(dupl_ptr->checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1839 | struct file_buffer *frag_buffer = |
| 1840 | get_fragment(dupl_ptr->fragment); |
| 1841 | dupl_ptr->checksum = |
| 1842 | get_checksum_disk(dupl_ptr->start, |
| 1843 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 1844 | dupl_ptr->fragment_checksum = |
| 1845 | get_checksum_mem(frag_buffer->data + |
| 1846 | dupl_ptr->fragment->offset, frag_bytes); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1847 | cache_block_put(frag_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1848 | dupl_ptr->checksum_flag = TRUE; |
| 1849 | } |
| 1850 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1851 | if(checksum != dupl_ptr->checksum || |
| 1852 | fragment_checksum != |
| 1853 | dupl_ptr->fragment_checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1854 | continue; |
| 1855 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1856 | target_start = *start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1857 | for(block = 0; block < blocks; block ++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1858 | int size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 1859 | ((*block_list)[block]); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1860 | struct file_buffer *target_buffer = NULL; |
| 1861 | struct file_buffer *dup_buffer = NULL; |
| 1862 | char *target_data, *dup_data; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1863 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1864 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1865 | if(size == 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 1866 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1867 | target_buffer = cache_lookup(writer_buffer, |
| 1868 | target_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1869 | if(target_buffer) |
| 1870 | target_data = target_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1871 | else { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1872 | target_data = |
| 1873 | read_from_disk(target_start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1874 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1875 | if(target_data == NULL) { |
| 1876 | ERROR("Failed to read data from" |
| 1877 | " output filesystem\n"); |
| 1878 | BAD_ERROR("Output filesystem" |
| 1879 | " corrupted?\n"); |
| 1880 | } |
| 1881 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1882 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1883 | dup_buffer = cache_lookup(writer_buffer, |
| 1884 | dup_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1885 | if(dup_buffer) |
| 1886 | dup_data = dup_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1887 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1888 | dup_data = read_from_disk2(dup_start, |
| 1889 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1890 | if(dup_data == NULL) { |
| 1891 | ERROR("Failed to read data from" |
| 1892 | " output filesystem\n"); |
| 1893 | BAD_ERROR("Output filesystem" |
| 1894 | " corrupted?\n"); |
| 1895 | } |
| 1896 | } |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1897 | |
| 1898 | res = memcmp(target_data, dup_data, size); |
| 1899 | cache_block_put(target_buffer); |
| 1900 | cache_block_put(dup_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1901 | if(res != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1902 | break; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1903 | target_start += size; |
| 1904 | dup_start += size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1905 | } |
| 1906 | if(block == blocks) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1907 | struct file_buffer *frag_buffer = |
| 1908 | get_fragment(dupl_ptr->fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1909 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1910 | if(frag_bytes == 0 || |
| 1911 | memcmp(file_buffer->data, |
| 1912 | frag_buffer->data + |
| 1913 | dupl_ptr->fragment->offset, |
| 1914 | frag_bytes) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1915 | TRACE("Found duplicate file, start " |
| 1916 | "0x%llx, size %lld, checksum " |
| 1917 | "0x%x, fragment %d, size %d, " |
| 1918 | "offset %d, checksum 0x%x\n", |
| 1919 | dupl_ptr->start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1920 | dupl_ptr->bytes, |
| 1921 | dupl_ptr->checksum, |
| 1922 | dupl_ptr->fragment->index, |
| 1923 | frag_bytes, |
| 1924 | dupl_ptr->fragment->offset, |
| 1925 | fragment_checksum); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1926 | *block_list = dupl_ptr->block_list; |
| 1927 | *start = dupl_ptr->start; |
| 1928 | *fragment = dupl_ptr->fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1929 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1930 | return 0; |
| 1931 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1932 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1937 | return add_non_dup(file_size, bytes, *block_list, *start, *fragment, |
| 1938 | checksum, fragment_checksum, checksum_flag); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 1942 | inline int is_fragment(struct inode_info *inode) |
| 1943 | { |
| 1944 | int file_size = inode->buf.st_size; |
| 1945 | |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 1946 | /* |
| 1947 | * If this block is to be compressed differently to the |
| 1948 | * fragment compression then it cannot be a fragment |
| 1949 | */ |
| 1950 | if(inode->noF != noF) |
| 1951 | return FALSE; |
| 1952 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1953 | return !inode->no_fragments && file_size && (file_size < block_size || |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 1954 | (inode->always_use_fragments && file_size & (block_size - 1))); |
| 1955 | } |
| 1956 | |
| 1957 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1958 | void put_file_buffer(struct file_buffer *file_buffer) |
| 1959 | { |
| 1960 | /* |
| 1961 | * Decide where to send the file buffer - only compressible non- |
| 1962 | * fragment blocks need to be send to the deflate threads, all |
| 1963 | * others can be sent directly to the main thread |
| 1964 | */ |
| 1965 | if(file_buffer->error) { |
| 1966 | file_buffer->fragment = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 1967 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1968 | } else if (file_buffer->file_size == 0 || file_buffer->fragment) |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 1969 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1970 | else |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 1971 | queue_put(to_deflate, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 1975 | static int seq = 0; |
| 1976 | void reader_read_process(struct dir_ent *dir_ent) |
| 1977 | { |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 1978 | struct inode_info *inode = dir_ent->inode; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 1979 | struct file_buffer *prev_buffer = NULL, *file_buffer; |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 1980 | int status, res, byte, count = 0; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 1981 | int file = get_pseudo_file(inode->pseudo_id)->fd; |
| 1982 | int child = get_pseudo_file(inode->pseudo_id)->child; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 1983 | long long bytes = 0; |
| 1984 | |
| 1985 | while(1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 1986 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 1987 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 1988 | file_buffer->noD = inode->noD; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 1989 | |
| 1990 | byte = read_bytes(file, file_buffer->data, block_size); |
| 1991 | if(byte == -1) |
| 1992 | goto read_err; |
| 1993 | |
| 1994 | file_buffer->size = byte; |
| 1995 | file_buffer->file_size = -1; |
| 1996 | file_buffer->block = count ++; |
| 1997 | file_buffer->error = FALSE; |
| 1998 | file_buffer->fragment = FALSE; |
| 1999 | bytes += byte; |
| 2000 | |
| 2001 | if(byte == 0) |
| 2002 | break; |
| 2003 | |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2004 | /* |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2005 | * Update progress bar size. This is done |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2006 | * on every block rather than waiting for all blocks to be |
| 2007 | * read incase write_file_process() is running in parallel |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2008 | * with this. Otherwise the current progress bar position |
| 2009 | * may get ahead of the progress bar size. |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2010 | */ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2011 | progress_bar_size(1); |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2012 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2013 | if(prev_buffer) |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2014 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2015 | prev_buffer = file_buffer; |
| 2016 | } |
| 2017 | |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2018 | /* |
| 2019 | * Update inode file size now that the size of the dynamic pseudo file |
| 2020 | * is known. This is needed for the -info option. |
| 2021 | */ |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2022 | inode->buf.st_size = bytes; |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2023 | |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2024 | res = waitpid(child, &status, 0); |
plougher | d87d8d1 | 2009-09-10 04:08:16 +0000 | [diff] [blame] | 2025 | if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2026 | goto read_err; |
| 2027 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2028 | if(prev_buffer == NULL) |
| 2029 | prev_buffer = file_buffer; |
| 2030 | else { |
| 2031 | cache_block_put(file_buffer); |
| 2032 | seq --; |
| 2033 | } |
| 2034 | prev_buffer->file_size = bytes; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2035 | prev_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2036 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2037 | |
| 2038 | return; |
| 2039 | |
| 2040 | read_err: |
| 2041 | if(prev_buffer) { |
| 2042 | cache_block_put(file_buffer); |
| 2043 | seq --; |
| 2044 | file_buffer = prev_buffer; |
| 2045 | } |
| 2046 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2047 | put_file_buffer(file_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2051 | void reader_read_file(struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2052 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2053 | struct stat *buf = &dir_ent->inode->buf, buf2; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2054 | struct file_buffer *file_buffer; |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2055 | int blocks, byte, count, expected, file, res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2056 | long long bytes, read_size; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2057 | struct inode_info *inode = dir_ent->inode; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2058 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2059 | if(inode->read) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2060 | return; |
| 2061 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2062 | inode->read = TRUE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2063 | again: |
| 2064 | bytes = 0; |
| 2065 | count = 0; |
| 2066 | file_buffer = NULL; |
| 2067 | read_size = buf->st_size; |
| 2068 | blocks = (read_size + block_size - 1) >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2069 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2070 | file = open(pathname_reader(dir_ent), O_RDONLY); |
plougher | c1d258e | 2010-12-16 05:16:45 +0000 | [diff] [blame] | 2071 | if(file == -1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2072 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2073 | file_buffer->sequence = seq ++; |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2074 | goto read_err2; |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2075 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2076 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2077 | do { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2078 | expected = read_size - ((long long) count * block_size) > |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2079 | block_size ? block_size : |
| 2080 | read_size - ((long long) count * block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2081 | |
| 2082 | if(file_buffer) |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2083 | put_file_buffer(file_buffer); |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2084 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2085 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2086 | file_buffer->noD = inode->noD; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2087 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2088 | /* |
| 2089 | * Always try to read block_size bytes from the file rather |
| 2090 | * than expected bytes (which will be less than the block_size |
| 2091 | * at the file tail) to check that the file hasn't grown |
| 2092 | * since being stated. If it is longer (or shorter) than |
| 2093 | * expected, then restat, and try again. Note the special |
| 2094 | * case where the file is an exact multiple of the block_size |
| 2095 | * is dealt with later. |
| 2096 | */ |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2097 | byte = file_buffer->size = read_bytes(file, file_buffer->data, |
| 2098 | block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2099 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2100 | file_buffer->file_size = read_size; |
| 2101 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2102 | if(byte == -1) |
| 2103 | goto read_err; |
| 2104 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2105 | if(byte != expected) |
| 2106 | goto restat; |
| 2107 | |
| 2108 | file_buffer->block = count; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2109 | file_buffer->error = FALSE; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2110 | file_buffer->fragment = FALSE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2111 | |
| 2112 | bytes += byte; |
| 2113 | count ++; |
| 2114 | } while(count < blocks); |
| 2115 | |
| 2116 | if(read_size != bytes) |
| 2117 | goto restat; |
| 2118 | |
| 2119 | if(expected == block_size) { |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2120 | /* |
| 2121 | * Special case where we've not tried to read past the end of |
| 2122 | * the file. We expect to get EOF, i.e. the file isn't larger |
| 2123 | * than we expect. |
| 2124 | */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2125 | char buffer; |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2126 | int res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2127 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2128 | res = read_bytes(file, &buffer, 1); |
| 2129 | if(res == -1) |
| 2130 | goto read_err; |
| 2131 | |
| 2132 | if(res != 0) |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2133 | goto restat; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2136 | file_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2137 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2138 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2139 | close(file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2140 | |
| 2141 | return; |
| 2142 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2143 | restat: |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2144 | res = fstat(file, &buf2); |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2145 | if(res == -1) { |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 2146 | ERROR("Cannot stat dir/file %s because %s, ignoring\n", |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2147 | pathname_reader(dir_ent), strerror(errno)); |
| 2148 | goto read_err; |
| 2149 | } |
| 2150 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2151 | if(read_size != buf2.st_size) { |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2152 | close(file); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2153 | memcpy(buf, &buf2, sizeof(struct stat)); |
| 2154 | file_buffer->error = 2; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2155 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2156 | goto again; |
| 2157 | } |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2158 | read_err: |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2159 | close(file); |
| 2160 | read_err2: |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2161 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2162 | put_file_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | |
| 2166 | void reader_scan(struct dir_info *dir) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2167 | struct dir_ent *dir_ent = dir->list; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2168 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2169 | for(; dir_ent; dir_ent = dir_ent->next) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2170 | struct stat *buf = &dir_ent->inode->buf; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2171 | if(dir_ent->inode->root_entry) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2172 | continue; |
| 2173 | |
plougher | b3977eb | 2010-05-02 02:08:48 +0000 | [diff] [blame] | 2174 | if(IS_PSEUDO_PROCESS(dir_ent->inode)) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2175 | reader_read_process(dir_ent); |
| 2176 | continue; |
| 2177 | } |
| 2178 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2179 | switch(buf->st_mode & S_IFMT) { |
| 2180 | case S_IFREG: |
| 2181 | reader_read_file(dir_ent); |
| 2182 | break; |
| 2183 | case S_IFDIR: |
| 2184 | reader_scan(dir_ent->dir); |
| 2185 | break; |
| 2186 | } |
| 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | |
| 2191 | void *reader(void *arg) |
| 2192 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2193 | if(!sorted) |
| 2194 | reader_scan(queue_get(to_reader)); |
| 2195 | else { |
| 2196 | int i; |
| 2197 | struct priority_entry *entry; |
| 2198 | |
| 2199 | queue_get(to_reader); |
| 2200 | for(i = 65535; i >= 0; i--) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2201 | for(entry = priority_list[i]; entry; |
| 2202 | entry = entry->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2203 | reader_read_file(entry->dir); |
| 2204 | } |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2205 | |
| 2206 | pthread_exit(NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | |
| 2210 | void *writer(void *arg) |
| 2211 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2212 | while(1) { |
| 2213 | struct file_buffer *file_buffer = queue_get(to_writer); |
| 2214 | off_t off; |
| 2215 | |
| 2216 | if(file_buffer == NULL) { |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2217 | queue_put(from_writer, NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2218 | continue; |
| 2219 | } |
| 2220 | |
| 2221 | off = file_buffer->block; |
| 2222 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2223 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2224 | pthread_mutex_lock(&pos_mutex); |
| 2225 | |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2226 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | d23000c | 2013-02-06 22:05:45 +0000 | [diff] [blame] | 2227 | ERROR("writer: Lseek on destination failed because " |
| 2228 | "%s, offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2229 | BAD_ERROR("Probably out of space on output " |
Phillip Lougher | 3f4ccd2 | 2013-02-11 04:40:09 +0000 | [diff] [blame] | 2230 | "%s\n", block_device ? "block device" : |
| 2231 | "filesystem"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2234 | if(write_bytes(fd, file_buffer->data, |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2235 | file_buffer->size) == -1) |
| 2236 | BAD_ERROR("Failed to write to output %s\n", |
Phillip Lougher | e9df455 | 2013-02-14 23:06:29 +0000 | [diff] [blame] | 2237 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2238 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2239 | pthread_cleanup_pop(1); |
| 2240 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2241 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2246 | int all_zero(struct file_buffer *file_buffer) |
| 2247 | { |
| 2248 | int i; |
| 2249 | long entries = file_buffer->size / sizeof(long); |
| 2250 | long *p = (long *) file_buffer->data; |
| 2251 | |
| 2252 | for(i = 0; i < entries && p[i] == 0; i++); |
| 2253 | |
| 2254 | if(i == entries) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2255 | for(i = file_buffer->size & ~(sizeof(long) - 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2256 | i < file_buffer->size && file_buffer->data[i] == 0; |
| 2257 | i++); |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2258 | |
| 2259 | return i == file_buffer->size; |
| 2260 | } |
| 2261 | |
| 2262 | return 0; |
| 2263 | } |
| 2264 | |
| 2265 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2266 | void *deflator(void *arg) |
| 2267 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2268 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2269 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2270 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2271 | res = compressor_init(comp, &stream, block_size, 1); |
| 2272 | if(res) |
| 2273 | BAD_ERROR("deflator:: compressor_init failed\n"); |
| 2274 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2275 | while(1) { |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 2276 | struct file_buffer *file_buffer = queue_get(to_deflate); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2277 | struct file_buffer *write_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2278 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2279 | if(sparse_files && all_zero(file_buffer)) { |
Phillip Lougher | 4885438 | 2011-09-09 03:36:55 +0100 | [diff] [blame] | 2280 | file_buffer->c_byte = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2281 | seq_queue_put(to_main, file_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2282 | } else { |
Phillip Lougher | 4570201 | 2013-04-30 05:19:54 +0100 | [diff] [blame] | 2283 | write_buffer = cache_get(writer_buffer, -1); |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2284 | write_buffer->c_byte = mangle2(stream, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2285 | write_buffer->data, file_buffer->data, |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2286 | file_buffer->size, block_size, |
| 2287 | file_buffer->noD, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2288 | write_buffer->sequence = file_buffer->sequence; |
| 2289 | write_buffer->file_size = file_buffer->file_size; |
| 2290 | write_buffer->block = file_buffer->block; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2291 | write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2292 | (write_buffer->c_byte); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2293 | write_buffer->fragment = FALSE; |
| 2294 | write_buffer->error = FALSE; |
| 2295 | cache_block_put(file_buffer); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2296 | seq_queue_put(to_main, write_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2297 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | |
| 2302 | void *frag_deflator(void *arg) |
| 2303 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2304 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2305 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2306 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2307 | res = compressor_init(comp, &stream, block_size, 1); |
| 2308 | if(res) |
| 2309 | BAD_ERROR("frag_deflator:: compressor_init failed\n"); |
| 2310 | |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2311 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
| 2312 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2313 | while(1) { |
| 2314 | int c_byte, compressed_size; |
| 2315 | struct file_buffer *file_buffer = queue_get(to_frag); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2316 | struct file_buffer *write_buffer = |
Phillip Lougher | 4570201 | 2013-04-30 05:19:54 +0100 | [diff] [blame] | 2317 | cache_get(writer_buffer, |
| 2318 | FRAG_INDEX(file_buffer->block)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2319 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2320 | c_byte = mangle2(stream, write_buffer->data, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2321 | file_buffer->size, block_size, noF, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2322 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte); |
plougher | d036a31 | 2008-03-08 01:38:27 +0000 | [diff] [blame] | 2323 | write_buffer->size = compressed_size; |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2324 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2325 | if(fragments_locked == FALSE) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2326 | fragment_table[file_buffer->block].size = c_byte; |
| 2327 | fragment_table[file_buffer->block].start_block = bytes; |
| 2328 | write_buffer->block = bytes; |
| 2329 | bytes += compressed_size; |
| 2330 | fragments_outstanding --; |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2331 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2332 | queue_put(to_writer, write_buffer); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2333 | TRACE("Writing fragment %lld, uncompressed size %d, " |
| 2334 | "compressed size %d\n", file_buffer->block, |
| 2335 | file_buffer->size, compressed_size); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2336 | } else { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2337 | add_pending_fragment(write_buffer, c_byte, |
| 2338 | file_buffer->block); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2339 | pthread_mutex_unlock(&fragment_mutex); |
| 2340 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2341 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2342 | } |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2343 | |
| 2344 | pthread_cleanup_pop(0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2348 | struct file_buffer *get_file_buffer() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2349 | { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2350 | struct file_buffer *file_buffer = seq_queue_get(to_main); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2351 | |
| 2352 | if(file_buffer->fragment) { |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2353 | if(sparse_files && all_zero(file_buffer)) { |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2354 | file_buffer->c_byte = 0; |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2355 | file_buffer->fragment = FALSE; |
| 2356 | } else |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2357 | file_buffer->c_byte = file_buffer->size; |
| 2358 | } |
| 2359 | |
| 2360 | return file_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2364 | void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2365 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2366 | { |
| 2367 | file_count ++; |
| 2368 | *duplicate_file = FALSE; |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2369 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0, |
| 2370 | NULL, &empty_fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2374 | void write_file_frag_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2375 | int size, int *duplicate_file, struct file_buffer *file_buffer, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2376 | unsigned short checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2377 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2378 | struct file_info *dupl_ptr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2379 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2380 | unsigned int *block_listp = NULL; |
| 2381 | long long start = 0; |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2382 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2383 | dupl_ptr = duplicate(size, 0, &block_listp, &start, &fragment, |
| 2384 | file_buffer, 0, 0, checksum, TRUE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2385 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2386 | if(dupl_ptr) { |
| 2387 | *duplicate_file = FALSE; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2388 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2389 | dupl_ptr->fragment = fragment; |
| 2390 | } else |
| 2391 | *duplicate_file = TRUE; |
| 2392 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2393 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2394 | |
| 2395 | total_bytes += size; |
| 2396 | file_count ++; |
| 2397 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2398 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2399 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2400 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2401 | 0, NULL, fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2405 | void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, int size, |
| 2406 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2407 | { |
| 2408 | struct fragment *fragment; |
| 2409 | unsigned short checksum; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2410 | |
| 2411 | checksum = get_checksum_mem_buffer(file_buffer); |
| 2412 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2413 | if(pre_duplicate_frag(size, checksum)) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2414 | write_file_frag_dup(inode, dir_ent, size, duplicate_file, |
| 2415 | file_buffer, checksum); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2416 | return; |
| 2417 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2418 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2419 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2420 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2421 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2422 | |
| 2423 | if(duplicate_checking) |
| 2424 | add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, TRUE); |
| 2425 | |
| 2426 | total_bytes += size; |
| 2427 | file_count ++; |
| 2428 | |
| 2429 | *duplicate_file = FALSE; |
| 2430 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2431 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2432 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2433 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2434 | 0, NULL, fragment, NULL, 0); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2435 | |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2436 | if(duplicate_checking == FALSE) |
| 2437 | free_fragment(fragment); |
| 2438 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2439 | return; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2443 | int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2444 | struct file_buffer *read_buffer, int *duplicate_file) |
| 2445 | { |
| 2446 | long long read_size, file_bytes, start; |
| 2447 | struct fragment *fragment; |
| 2448 | unsigned int *block_list = NULL; |
| 2449 | int block = 0, status; |
| 2450 | long long sparse = 0; |
| 2451 | struct file_buffer *fragment_buffer = NULL; |
| 2452 | |
| 2453 | *duplicate_file = FALSE; |
| 2454 | |
| 2455 | lock_fragments(); |
| 2456 | |
| 2457 | file_bytes = 0; |
| 2458 | start = bytes; |
| 2459 | while (1) { |
| 2460 | read_size = read_buffer->file_size; |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2461 | if(read_buffer->fragment) |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2462 | fragment_buffer = read_buffer; |
| 2463 | else { |
| 2464 | block_list = realloc(block_list, (block + 1) * |
| 2465 | sizeof(unsigned int)); |
| 2466 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2467 | MEM_ERROR(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2468 | block_list[block ++] = read_buffer->c_byte; |
| 2469 | if(read_buffer->c_byte) { |
| 2470 | read_buffer->block = bytes; |
| 2471 | bytes += read_buffer->size; |
| 2472 | cache_rehash(read_buffer, read_buffer->block); |
| 2473 | file_bytes += read_buffer->size; |
| 2474 | queue_put(to_writer, read_buffer); |
| 2475 | } else { |
| 2476 | sparse += read_buffer->size; |
| 2477 | cache_block_put(read_buffer); |
| 2478 | } |
| 2479 | } |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2480 | inc_progress_bar(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2481 | |
| 2482 | if(read_size != -1) |
| 2483 | break; |
| 2484 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2485 | read_buffer = get_file_buffer(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2486 | if(read_buffer->error) |
| 2487 | goto read_err; |
| 2488 | } |
| 2489 | |
| 2490 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2491 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2492 | cache_block_put(fragment_buffer); |
| 2493 | |
| 2494 | if(duplicate_checking) |
| 2495 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2496 | 0, 0, FALSE); |
| 2497 | file_count ++; |
| 2498 | total_bytes += read_size; |
| 2499 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2500 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2501 | block, block_list, fragment, NULL, sparse); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2502 | |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2503 | if(duplicate_checking == FALSE) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2504 | free(block_list); |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2505 | free_fragment(fragment); |
| 2506 | } |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2507 | |
| 2508 | return 0; |
| 2509 | |
| 2510 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2511 | dec_progress_bar(block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2512 | status = read_buffer->error; |
| 2513 | bytes = start; |
| 2514 | if(!block_device) { |
| 2515 | int res; |
| 2516 | |
| 2517 | queue_put(to_writer, NULL); |
| 2518 | if(queue_get(from_writer) != 0) |
| 2519 | EXIT_MKSQUASHFS(); |
| 2520 | res = ftruncate(fd, bytes); |
| 2521 | if(res != 0) |
| 2522 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2523 | strerror(errno)); |
| 2524 | } |
| 2525 | unlock_fragments(); |
| 2526 | free(block_list); |
| 2527 | cache_block_put(read_buffer); |
| 2528 | return status; |
| 2529 | } |
| 2530 | |
| 2531 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2532 | int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2533 | long long read_size, struct file_buffer *read_buffer, |
| 2534 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2535 | { |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 2536 | long long file_bytes, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2537 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2538 | unsigned int *block_list; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2539 | int block, status; |
| 2540 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2541 | long long sparse = 0; |
| 2542 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2543 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2544 | *duplicate_file = FALSE; |
| 2545 | |
plougher | 8713962 | 2010-12-16 05:19:30 +0000 | [diff] [blame] | 2546 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2547 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2548 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2549 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2550 | lock_fragments(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2551 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2552 | file_bytes = 0; |
| 2553 | start = bytes; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2554 | for(block = 0; block < blocks;) { |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2555 | if(read_buffer->fragment) { |
Phillip Lougher | 2760d82 | 2013-01-13 21:14:06 +0000 | [diff] [blame] | 2556 | block_list[block] = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2557 | fragment_buffer = read_buffer; |
| 2558 | blocks = read_size >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2559 | } else { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2560 | block_list[block] = read_buffer->c_byte; |
| 2561 | if(read_buffer->c_byte) { |
| 2562 | read_buffer->block = bytes; |
| 2563 | bytes += read_buffer->size; |
| 2564 | cache_rehash(read_buffer, read_buffer->block); |
| 2565 | file_bytes += read_buffer->size; |
| 2566 | queue_put(to_writer, read_buffer); |
| 2567 | } else { |
| 2568 | sparse += read_buffer->size; |
| 2569 | cache_block_put(read_buffer); |
| 2570 | } |
| 2571 | } |
| 2572 | inc_progress_bar(); |
| 2573 | |
| 2574 | if(++block < blocks) { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2575 | read_buffer = get_file_buffer(); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2576 | if(read_buffer->error) |
| 2577 | goto read_err; |
| 2578 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2581 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2582 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2583 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2584 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2585 | if(duplicate_checking) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2586 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2587 | 0, 0, FALSE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2588 | file_count ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2589 | total_bytes += read_size; |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2590 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2591 | /* |
| 2592 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2593 | * a smaller block count on stat calls to sparse files. This is |
| 2594 | * to ensure intelligent applications like cp correctly handle the |
| 2595 | * file as a sparse file. If the file in the original filesystem isn't |
| 2596 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2597 | * report it as non-sparse on stat calls to preserve semantics |
| 2598 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2599 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2600 | sparse = 0; |
| 2601 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2602 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2603 | blocks, block_list, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2604 | |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2605 | if(duplicate_checking == FALSE) { |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2606 | free(block_list); |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2607 | free_fragment(fragment); |
| 2608 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2609 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2610 | return 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2611 | |
| 2612 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2613 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2614 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2615 | bytes = start; |
| 2616 | if(!block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2617 | int res; |
| 2618 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2619 | queue_put(to_writer, NULL); |
| 2620 | if(queue_get(from_writer) != 0) |
| 2621 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2622 | res = ftruncate(fd, bytes); |
| 2623 | if(res != 0) |
| 2624 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2625 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2626 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2627 | unlock_fragments(); |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2628 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2629 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2630 | return status; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2634 | int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2635 | long long read_size, struct file_buffer *read_buffer, |
| 2636 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2637 | { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2638 | int block, thresh; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2639 | long long file_bytes, dup_start, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2640 | struct fragment *fragment; |
| 2641 | struct file_info *dupl_ptr; |
| 2642 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2643 | unsigned int *block_list, *block_listp; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2644 | struct file_buffer **buffer_list; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 2645 | int status, num_locked_fragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2646 | long long sparse = 0; |
| 2647 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2648 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2649 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2650 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2651 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2652 | block_listp = block_list; |
| 2653 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2654 | buffer_list = malloc(blocks * sizeof(struct file_buffer *)); |
| 2655 | if(buffer_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2656 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2657 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 2658 | num_locked_fragments = lock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2659 | |
| 2660 | file_bytes = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2661 | start = dup_start = bytes; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2662 | thresh = blocks > (writer_buffer_size - num_locked_fragments) ? |
| 2663 | blocks - (writer_buffer_size - num_locked_fragments): 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2664 | |
| 2665 | for(block = 0; block < blocks;) { |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2666 | if(read_buffer->fragment) { |
Phillip Lougher | 38cb1ab | 2013-01-13 21:20:09 +0000 | [diff] [blame] | 2667 | block_list[block] = 0; |
Phillip Lougher | 19de5b6 | 2013-01-13 21:31:51 +0000 | [diff] [blame] | 2668 | buffer_list[block] = NULL; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2669 | fragment_buffer = read_buffer; |
| 2670 | blocks = read_size >> block_log; |
| 2671 | } else { |
| 2672 | block_list[block] = read_buffer->c_byte; |
| 2673 | |
| 2674 | if(read_buffer->c_byte) { |
| 2675 | read_buffer->block = bytes; |
| 2676 | bytes += read_buffer->size; |
| 2677 | file_bytes += read_buffer->size; |
| 2678 | cache_rehash(read_buffer, read_buffer->block); |
| 2679 | if(block < thresh) { |
| 2680 | buffer_list[block] = NULL; |
| 2681 | queue_put(to_writer, read_buffer); |
| 2682 | } else |
| 2683 | buffer_list[block] = read_buffer; |
| 2684 | } else { |
| 2685 | buffer_list[block] = NULL; |
| 2686 | sparse += read_buffer->size; |
| 2687 | cache_block_put(read_buffer); |
| 2688 | } |
| 2689 | } |
| 2690 | inc_progress_bar(); |
| 2691 | |
| 2692 | if(++block < blocks) { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2693 | read_buffer = get_file_buffer(); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2694 | if(read_buffer->error) |
| 2695 | goto read_err; |
| 2696 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2699 | dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start, |
| 2700 | &fragment, fragment_buffer, blocks, 0, 0, FALSE); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2701 | |
| 2702 | if(dupl_ptr) { |
| 2703 | *duplicate_file = FALSE; |
| 2704 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2705 | if(buffer_list[block]) |
| 2706 | queue_put(to_writer, buffer_list[block]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2707 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2708 | dupl_ptr->fragment = fragment; |
| 2709 | } else { |
| 2710 | *duplicate_file = TRUE; |
| 2711 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2712 | cache_block_put(buffer_list[block]); |
| 2713 | bytes = start; |
| 2714 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2715 | int res; |
| 2716 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2717 | queue_put(to_writer, NULL); |
| 2718 | if(queue_get(from_writer) != 0) |
| 2719 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2720 | res = ftruncate(fd, bytes); |
| 2721 | if(res != 0) |
| 2722 | BAD_ERROR("Failed to truncate dest file because" |
| 2723 | " %s\n", strerror(errno)); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2724 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2727 | unlock_fragments(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2728 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2729 | free(buffer_list); |
| 2730 | file_count ++; |
| 2731 | total_bytes += read_size; |
| 2732 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2733 | /* |
| 2734 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2735 | * a smaller block count on stat calls to sparse files. This is |
| 2736 | * to ensure intelligent applications like cp correctly handle the |
| 2737 | * file as a sparse file. If the file in the original filesystem isn't |
| 2738 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2739 | * report it as non-sparse on stat calls to preserve semantics |
| 2740 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2741 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2742 | sparse = 0; |
| 2743 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2744 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, |
| 2745 | dup_start, blocks, block_listp, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2746 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2747 | if(*duplicate_file == TRUE) |
| 2748 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2749 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2750 | return 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2751 | |
| 2752 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2753 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2754 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2755 | bytes = start; |
| 2756 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2757 | int res; |
| 2758 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2759 | queue_put(to_writer, NULL); |
| 2760 | if(queue_get(from_writer) != 0) |
| 2761 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2762 | res = ftruncate(fd, bytes); |
| 2763 | if(res != 0) |
| 2764 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2765 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2766 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2767 | unlock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2768 | for(blocks = thresh; blocks < block; blocks ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2769 | cache_block_put(buffer_list[blocks]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2770 | free(buffer_list); |
| 2771 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2772 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2773 | return status; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2777 | void write_file(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2778 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2779 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2780 | int status; |
| 2781 | struct file_buffer *read_buffer; |
| 2782 | long long read_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2783 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2784 | again: |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2785 | read_buffer = get_file_buffer(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2786 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 2787 | status = read_buffer->error; |
| 2788 | if(status) { |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2789 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2790 | goto file_err; |
| 2791 | } |
| 2792 | |
| 2793 | read_size = read_buffer->file_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2794 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2795 | if(read_size == -1) |
| 2796 | status = write_file_process(inode, dir_ent, read_buffer, |
| 2797 | duplicate_file); |
| 2798 | else if(read_size == 0) { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2799 | write_file_empty(inode, dir_ent, duplicate_file); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2800 | cache_block_put(read_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2801 | } else if(read_buffer->fragment && read_buffer->c_byte) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2802 | write_file_frag(inode, dir_ent, read_size, read_buffer, |
| 2803 | duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2804 | else if(pre_duplicate(read_size)) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2805 | status = write_file_blocks_dup(inode, dir_ent, read_size, |
| 2806 | read_buffer, duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2807 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2808 | status = write_file_blocks(inode, dir_ent, read_size, |
| 2809 | read_buffer, duplicate_file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2810 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2811 | file_err: |
| 2812 | if(status == 2) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2813 | ERROR("File %s changed size while reading filesystem, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2814 | "attempting to re-read\n", pathname(dir_ent)); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2815 | goto again; |
| 2816 | } else if(status == 1) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2817 | ERROR("Failed to read file %s, creating empty file\n", |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2818 | pathname(dir_ent)); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2819 | write_file_empty(inode, dir_ent, duplicate_file); |
| 2820 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2824 | #define BUFF_SIZE 512 |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2825 | char *name; |
| 2826 | char *basename_r(); |
| 2827 | |
| 2828 | char *getbase(char *pathname) |
| 2829 | { |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2830 | static char *b_buffer = NULL; |
| 2831 | static int b_size = BUFF_SIZE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2832 | char *result; |
| 2833 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2834 | if(b_buffer == NULL) { |
| 2835 | b_buffer = malloc(b_size); |
| 2836 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2837 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | while(1) { |
| 2841 | if(*pathname != '/') { |
| 2842 | result = getcwd(b_buffer, b_size); |
| 2843 | if(result == NULL && errno != ERANGE) |
| 2844 | BAD_ERROR("Getcwd failed in getbase\n"); |
| 2845 | |
| 2846 | /* enough room for pathname + "/" + '\0' terminator? */ |
| 2847 | if(result && strlen(pathname) + 2 <= |
| 2848 | b_size - strlen(b_buffer)) { |
| 2849 | strcat(strcat(b_buffer, "/"), pathname); |
| 2850 | break; |
| 2851 | } |
| 2852 | } else if(strlen(pathname) < b_size) { |
| 2853 | strcpy(b_buffer, pathname); |
| 2854 | break; |
| 2855 | } |
| 2856 | |
| 2857 | /* Buffer not large enough, realloc and try again */ |
| 2858 | b_buffer = realloc(b_buffer, b_size += BUFF_SIZE); |
| 2859 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2860 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2863 | name = b_buffer; |
| 2864 | if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0)) |
| 2865 | return NULL; |
| 2866 | else |
| 2867 | return result; |
| 2868 | } |
| 2869 | |
| 2870 | |
| 2871 | char *basename_r() |
| 2872 | { |
| 2873 | char *s; |
| 2874 | char *p; |
| 2875 | int n = 1; |
| 2876 | |
| 2877 | for(;;) { |
| 2878 | s = name; |
| 2879 | if(*name == '\0') |
| 2880 | return NULL; |
| 2881 | if(*name != '/') { |
| 2882 | while(*name != '\0' && *name != '/') name++; |
| 2883 | n = name - s; |
| 2884 | } |
| 2885 | while(*name == '/') name++; |
| 2886 | if(strncmp(s, ".", n) == 0) |
| 2887 | continue; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2888 | if((*name == '\0') || (strncmp(s, "..", n) == 0) || |
| 2889 | ((p = basename_r()) == NULL)) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2890 | s[n] = '\0'; |
| 2891 | return s; |
| 2892 | } |
| 2893 | if(strcmp(p, "..") == 0) |
| 2894 | continue; |
| 2895 | return p; |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2900 | struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2901 | { |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2902 | int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino); |
| 2903 | struct inode_info *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2904 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2905 | /* |
| 2906 | * Look-up inode in hash table, if it already exists we have a |
| 2907 | * hard-link, so increment the nlink count and return it. |
| 2908 | * Don't do the look-up for directories because we don't hard-link |
| 2909 | * directories. |
| 2910 | */ |
| 2911 | if ((buf->st_mode & S_IFMT) != S_IFDIR) { |
| 2912 | for(inode = inode_info[ino_hash]; inode; inode = inode->next) { |
| 2913 | if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) { |
| 2914 | inode->nlink ++; |
| 2915 | return inode; |
| 2916 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2917 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
plougher | 288f685 | 2010-12-16 05:22:55 +0000 | [diff] [blame] | 2920 | inode = malloc(sizeof(struct inode_info)); |
| 2921 | if(inode == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2922 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2923 | |
| 2924 | memcpy(&inode->buf, buf, sizeof(struct stat)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2925 | inode->read = FALSE; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2926 | inode->root_entry = FALSE; |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2927 | inode->pseudo_file = pseudo; |
| 2928 | inode->pseudo_id = id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2929 | inode->inode = SQUASHFS_INVALID_BLK; |
| 2930 | inode->nlink = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 2931 | inode->inode_number = 0; |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 2932 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2933 | /* |
| 2934 | * Copy filesystem wide defaults into inode, these filesystem |
| 2935 | * wide defaults may be altered on an individual inode basis by |
| 2936 | * user specified actions |
| 2937 | * |
| 2938 | */ |
| 2939 | inode->no_fragments = no_fragments; |
| 2940 | inode->always_use_fragments = always_use_fragments; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2941 | inode->noD = noD; |
| 2942 | inode->noF = noF; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2943 | |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 2944 | if((buf->st_mode & S_IFMT) == S_IFREG) |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2945 | progress_bar_size((buf->st_size + block_size - 1) >> block_log); |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 2946 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2947 | inode->next = inode_info[ino_hash]; |
| 2948 | inode_info[ino_hash] = inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2949 | |
| 2950 | return inode; |
| 2951 | } |
| 2952 | |
| 2953 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2954 | inline struct inode_info *lookup_inode(struct stat *buf) |
| 2955 | { |
| 2956 | return lookup_inode2(buf, 0, 0); |
| 2957 | } |
| 2958 | |
| 2959 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 2960 | inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this) |
| 2961 | { |
| 2962 | if (inode->inode_number == 0) |
| 2963 | inode->inode_number = use_this ? : inode_no ++; |
| 2964 | } |
| 2965 | |
| 2966 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2967 | inline struct dir_ent *create_dir_entry(char *name, char *source_name, |
| 2968 | char *nonstandard_pathname, struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2969 | { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2970 | struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent)); |
| 2971 | if(dir_ent == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2972 | MEM_ERROR(); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2973 | |
| 2974 | dir_ent->name = name; |
| 2975 | dir_ent->source_name = source_name; |
| 2976 | dir_ent->nonstandard_pathname = nonstandard_pathname; |
| 2977 | dir_ent->our_dir = dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2978 | dir_ent->next = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2979 | |
| 2980 | return dir_ent; |
| 2981 | } |
| 2982 | |
| 2983 | |
| 2984 | inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir, |
| 2985 | struct inode_info *inode_info) |
| 2986 | { |
| 2987 | struct dir_info *dir = dir_ent->our_dir; |
| 2988 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2989 | if(sub_dir) |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2990 | sub_dir->dir_ent = dir_ent; |
| 2991 | dir_ent->inode = inode_info; |
| 2992 | dir_ent->dir = sub_dir; |
| 2993 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2994 | dir_ent->next = dir->list; |
| 2995 | dir->list = dir_ent; |
| 2996 | dir->count++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | |
| 3000 | inline void add_dir_entry2(char *name, char *source_name, |
| 3001 | char *nonstandard_pathname, struct dir_info *sub_dir, |
| 3002 | struct inode_info *inode_info, struct dir_info *dir) |
| 3003 | { |
| 3004 | struct dir_ent *dir_ent = create_dir_entry(name, source_name, |
| 3005 | nonstandard_pathname, dir); |
| 3006 | |
| 3007 | |
| 3008 | add_dir_entry(dir_ent, sub_dir, inode_info); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3012 | inline void free_dir_entry(struct dir_ent *dir_ent) |
| 3013 | { |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3014 | if(dir_ent->name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3015 | free(dir_ent->name); |
| 3016 | |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3017 | if(dir_ent->source_name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3018 | free(dir_ent->source_name); |
| 3019 | |
| 3020 | free(dir_ent); |
| 3021 | } |
| 3022 | |
| 3023 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3024 | inline void add_excluded(struct dir_info *dir) |
| 3025 | { |
| 3026 | dir->excluded ++; |
| 3027 | } |
| 3028 | |
| 3029 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3030 | void dir_scan(squashfs_inode *inode, char *pathname, |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 3031 | struct dir_ent *(_readdir)(struct dir_info *), int progress) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3032 | { |
| 3033 | struct stat buf; |
| 3034 | struct dir_info *dir_info = dir_scan1(pathname, "", paths, _readdir, 1); |
| 3035 | struct dir_ent *dir_ent; |
| 3036 | |
| 3037 | if(dir_info == NULL) |
| 3038 | return; |
| 3039 | |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3040 | /* |
| 3041 | * Process most actions and any pseudo files |
| 3042 | */ |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 3043 | if(actions() || get_pseudo()) |
| 3044 | dir_scan2(dir_info, get_pseudo()); |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3045 | |
Phillip Lougher | e92cb51 | 2012-11-15 02:22:28 +0000 | [diff] [blame] | 3046 | /* |
| 3047 | * Process move actions |
| 3048 | */ |
| 3049 | if(move_actions()) { |
| 3050 | dir_scan3(dir_info, dir_info); |
| 3051 | do_move_actions(); |
| 3052 | } |
| 3053 | |
Phillip Lougher | 71ae14e | 2012-11-15 02:27:50 +0000 | [diff] [blame] | 3054 | /* |
| 3055 | * Process empty actions |
| 3056 | */ |
| 3057 | if(empty_actions()) |
| 3058 | dir_scan4(dir_info); |
| 3059 | |
Phillip Lougher | eb69dad | 2012-11-15 03:34:02 +0000 | [diff] [blame] | 3060 | /* |
| 3061 | * Sort directories and compute the inode numbers |
| 3062 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3063 | dir_scan5(dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3064 | |
Phillip Lougher | d0fe1d5 | 2012-10-09 03:34:34 +0100 | [diff] [blame] | 3065 | dir_ent = create_dir_entry("", NULL, pathname, |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 3066 | scan1_opendir("", "", 0)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3067 | |
| 3068 | if(pathname[0] == '\0') { |
| 3069 | /* |
| 3070 | * dummy top level directory, if multiple sources specified on |
| 3071 | * command line |
| 3072 | */ |
| 3073 | memset(&buf, 0, sizeof(buf)); |
| 3074 | buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR; |
| 3075 | buf.st_uid = getuid(); |
| 3076 | buf.st_gid = getgid(); |
| 3077 | buf.st_mtime = time(NULL); |
| 3078 | buf.st_dev = 0; |
| 3079 | buf.st_ino = 0; |
| 3080 | dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0); |
| 3081 | } else { |
Phillip Lougher | 38a35ec | 2012-11-30 04:31:35 +0000 | [diff] [blame] | 3082 | if(lstat(pathname, &buf) == -1) |
| 3083 | /* source directory has disappeared? */ |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3084 | BAD_ERROR("Cannot stat source directory %s because %s\n", |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3085 | pathname, strerror(errno)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3086 | dir_ent->inode = lookup_inode(&buf); |
| 3087 | } |
| 3088 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3089 | alloc_inode_no(dir_ent->inode, root_inode_number); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3090 | dir_ent->dir = dir_info; |
| 3091 | dir_info->dir_ent = dir_ent; |
| 3092 | |
Andy Lutomirski | 3f31dcf | 2012-09-21 18:08:08 -0700 | [diff] [blame] | 3093 | eval_actions(dir_ent); |
| 3094 | |
Phillip Lougher | 19b877d | 2013-02-23 11:15:38 +0000 | [diff] [blame] | 3095 | if(sorted) |
| 3096 | generate_file_priorities(dir_info, 0, |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3097 | &dir_info->dir_ent->inode->buf); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3098 | queue_put(to_reader, dir_info); |
| 3099 | if(sorted) |
| 3100 | sort_files_and_write(dir_info); |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 3101 | set_progressbar_state(progress); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3102 | dir_scan6(inode, dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3103 | dir_ent->inode->inode = *inode; |
| 3104 | dir_ent->inode->type = SQUASHFS_DIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3108 | /* |
| 3109 | * dir_scan1 routines... |
Phillip Lougher | e8630be | 2012-11-11 02:24:24 +0000 | [diff] [blame] | 3110 | * These scan the source directories into memory for processing. |
| 3111 | * Exclude actions are processed here (in contrast to the other actions) |
| 3112 | * because they affect what is scanned. |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3113 | */ |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3114 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3115 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3116 | struct dir_info *dir; |
| 3117 | |
plougher | 6da792b | 2010-12-16 05:25:39 +0000 | [diff] [blame] | 3118 | dir = malloc(sizeof(struct dir_info)); |
| 3119 | if(dir == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3120 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3121 | |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3122 | if(pathname[0] != '\0') { |
| 3123 | dir->linuxdir = opendir(pathname); |
| 3124 | if(dir->linuxdir == NULL) { |
| 3125 | free(dir); |
| 3126 | return NULL; |
| 3127 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3128 | } |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3129 | |
Phillip Lougher | 3cb5d6c | 2013-01-09 05:50:29 +0000 | [diff] [blame] | 3130 | dir->pathname = strdup(pathname); |
| 3131 | dir->subpath = strdup(subpath); |
Phillip Lougher | ded70fa | 2011-12-25 02:14:58 +0000 | [diff] [blame] | 3132 | dir->count = 0; |
| 3133 | dir->directory_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3134 | dir->dir_is_ldir = TRUE; |
| 3135 | dir->list = NULL; |
Phillip Lougher | 0b5a124 | 2011-12-25 03:22:42 +0000 | [diff] [blame] | 3136 | dir->depth = depth; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3137 | dir->excluded = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3138 | |
| 3139 | return dir; |
| 3140 | } |
| 3141 | |
| 3142 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3143 | struct dir_ent *scan1_encomp_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3144 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3145 | static int index = 0; |
| 3146 | |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3147 | if(dir->count < old_root_entries) { |
| 3148 | int i; |
| 3149 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3150 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3151 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3152 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3153 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3154 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3155 | } |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3156 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3157 | |
| 3158 | while(index < source) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3159 | char *basename = NULL; |
| 3160 | char *dir_name = getbase(source_path[index]); |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3161 | int pass = 1, res; |
plougher | 89fe2c7 | 2010-12-16 05:31:34 +0000 | [diff] [blame] | 3162 | |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3163 | if(dir_name == NULL) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3164 | ERROR("Bad source directory %s - skipping ...\n", |
| 3165 | source_path[index]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3166 | index ++; |
| 3167 | continue; |
| 3168 | } |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3169 | dir_name = strdup(dir_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3170 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3171 | struct dir_ent *dir_ent = dir->list; |
| 3172 | |
| 3173 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3174 | dir_ent = dir_ent->next); |
| 3175 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3176 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3177 | ERROR("Source directory entry %s already used! - trying" |
| 3178 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3179 | if(pass == 1) |
| 3180 | basename = dir_name; |
| 3181 | else |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3182 | free(dir_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3183 | res = asprintf(&dir_name, "%s_%d", basename, pass++); |
| 3184 | if(res == -1) |
| 3185 | BAD_ERROR("asprintf failed in " |
| 3186 | "scan1_encomp_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3187 | ERROR("%s\n", dir_name); |
| 3188 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3189 | return create_dir_entry(dir_name, basename, |
| 3190 | source_path[index ++], dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3191 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3192 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3196 | struct dir_ent *scan1_single_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3197 | { |
| 3198 | struct dirent *d_name; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3199 | int i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3200 | |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3201 | if(dir->count < old_root_entries) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3202 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3203 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3204 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3205 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3206 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3207 | } |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3208 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3209 | |
| 3210 | if((d_name = readdir(dir->linuxdir)) != NULL) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3211 | char *basename = NULL; |
| 3212 | char *dir_name = strdup(d_name->d_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3213 | int pass = 1, res; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3214 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3215 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3216 | struct dir_ent *dir_ent = dir->list; |
| 3217 | |
| 3218 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3219 | dir_ent = dir_ent->next); |
| 3220 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3221 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3222 | ERROR("Source directory entry %s already used! - trying" |
| 3223 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3224 | if (pass == 1) |
| 3225 | basename = dir_name; |
| 3226 | else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3227 | free(dir_name); |
| 3228 | res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++); |
| 3229 | if(res == -1) |
| 3230 | BAD_ERROR("asprintf failed in " |
| 3231 | "scan1_single_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3232 | ERROR("%s\n", dir_name); |
| 3233 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3234 | return create_dir_entry(dir_name, basename, NULL, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3237 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3241 | struct dir_ent *scan1_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3242 | { |
plougher | 480d9bf | 2010-12-18 02:28:39 +0000 | [diff] [blame] | 3243 | struct dirent *d_name = readdir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3244 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3245 | return d_name ? |
| 3246 | create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) : |
| 3247 | NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3251 | void scan1_freedir(struct dir_info *dir) |
| 3252 | { |
plougher | fbed12b | 2006-02-07 12:45:53 +0000 | [diff] [blame] | 3253 | if(dir->pathname[0] != '\0') |
| 3254 | closedir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3258 | struct dir_info *dir_scan1(char *filename, char *subpath, |
| 3259 | struct pathnames *paths, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3260 | struct dir_ent *(_readdir)(struct dir_info *), int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3261 | { |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3262 | struct dir_info *dir = scan1_opendir(filename, subpath, depth); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3263 | struct dir_ent *dir_ent; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3264 | |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3265 | if(dir == NULL) { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3266 | ERROR("Could not open %s, skipping...\n", filename); |
Phillip Lougher | 87db567 | 2013-01-09 05:40:46 +0000 | [diff] [blame] | 3267 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3270 | while((dir_ent = _readdir(dir))) { |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3271 | struct dir_info *sub_dir; |
| 3272 | struct stat buf; |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 3273 | struct pathnames *new = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3274 | char *filename = pathname(dir_ent); |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3275 | char *subpath = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3276 | char *dir_name = dir_ent->name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3277 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3278 | if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3279 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3280 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3281 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3282 | |
| 3283 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3284 | ERROR("Cannot stat dir/file %s because %s, ignoring\n", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3285 | filename, strerror(errno)); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3286 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3287 | continue; |
| 3288 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3289 | |
| 3290 | if((buf.st_mode & S_IFMT) != S_IFREG && |
Phillip Lougher | e4ff36a | 2012-11-19 01:34:47 +0000 | [diff] [blame] | 3291 | (buf.st_mode & S_IFMT) != S_IFDIR && |
| 3292 | (buf.st_mode & S_IFMT) != S_IFLNK && |
| 3293 | (buf.st_mode & S_IFMT) != S_IFCHR && |
| 3294 | (buf.st_mode & S_IFMT) != S_IFBLK && |
| 3295 | (buf.st_mode & S_IFMT) != S_IFIFO && |
| 3296 | (buf.st_mode & S_IFMT) != S_IFSOCK) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3297 | ERROR("File %s has unrecognised filetype %d, ignoring" |
| 3298 | "\n", filename, buf.st_mode & S_IFMT); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3299 | free_dir_entry(dir_ent); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3300 | continue; |
| 3301 | } |
| 3302 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3303 | if((old_exclude && old_excluded(filename, &buf)) || |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3304 | (!old_exclude && excluded(dir_name, paths, &new))) { |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3305 | add_excluded(dir); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3306 | free_dir_entry(dir_ent); |
Phillip Lougher | 10d8de0 | 2011-08-29 00:14:48 +0100 | [diff] [blame] | 3307 | continue; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3308 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3309 | |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3310 | if(exclude_actions()) { |
| 3311 | subpath = subpathname(dir_ent); |
| 3312 | |
| 3313 | if(eval_exclude_actions(dir_name, filename, subpath, |
| 3314 | &buf, depth)) { |
| 3315 | add_excluded(dir); |
| 3316 | free_dir_entry(dir_ent); |
| 3317 | continue; |
| 3318 | } |
| 3319 | } |
| 3320 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3321 | if((buf.st_mode & S_IFMT) == S_IFDIR) { |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3322 | if(subpath == NULL) |
| 3323 | subpath = subpathname(dir_ent); |
| 3324 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3325 | sub_dir = dir_scan1(filename, subpath, new, |
| 3326 | scan1_readdir, depth + 1); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3327 | if(sub_dir == NULL) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3328 | free_dir_entry(dir_ent); |
Phillip Lougher | 205c1e0 | 2013-01-07 01:37:56 +0000 | [diff] [blame] | 3329 | free(new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3330 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3331 | } |
Phillip Lougher | 6a78a2d | 2011-12-28 03:54:19 +0000 | [diff] [blame] | 3332 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3333 | dir->directory_count ++; |
| 3334 | } else |
| 3335 | sub_dir = NULL; |
| 3336 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3337 | add_dir_entry(dir_ent, sub_dir, lookup_inode(&buf)); |
Phillip Lougher | 205c1e0 | 2013-01-07 01:37:56 +0000 | [diff] [blame] | 3338 | free(new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | scan1_freedir(dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3342 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3343 | return dir; |
| 3344 | } |
| 3345 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3346 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3347 | /* |
| 3348 | * dir_scan2 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3349 | * This processes most actions and any pseudo files |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3350 | */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3351 | 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] | 3352 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3353 | if (dir_ent == NULL) |
| 3354 | dir_ent = dir->list; |
| 3355 | else |
| 3356 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3357 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3358 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next); |
| 3359 | |
| 3360 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | |
| 3364 | struct dir_ent *scan2_lookup(struct dir_info *dir, char *name) |
| 3365 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3366 | struct dir_ent *dir_ent = dir->list; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3367 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3368 | for(; dir_ent && strcmp(dir_ent->name, name) != 0; |
| 3369 | dir_ent = dir_ent->next); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3370 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3371 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3375 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3376 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3377 | struct dir_ent *dir_ent = NULL; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3378 | struct pseudo_entry *pseudo_ent; |
| 3379 | struct stat buf; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3380 | static int pseudo_ino = 1; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3381 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3382 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3383 | struct inode_info *inode_info = dir_ent->inode; |
| 3384 | struct stat *buf = &inode_info->buf; |
| 3385 | char *name = dir_ent->name; |
| 3386 | |
Phillip Lougher | 89d757c | 2011-09-20 00:33:19 +0100 | [diff] [blame] | 3387 | eval_actions(dir_ent); |
| 3388 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3389 | if((buf->st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3390 | dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) { |
| 3394 | dir_ent = scan2_lookup(dir, pseudo_ent->name); |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3395 | if(pseudo_ent->dev->type == 'm') { |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3396 | struct stat *buf; |
| 3397 | if(dir_ent == NULL) { |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3398 | ERROR("Pseudo modify file \"%s\" does not exist " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3399 | "in source filesystem. Ignoring.\n", |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3400 | pseudo_ent->pathname); |
| 3401 | continue; |
| 3402 | } |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3403 | if(dir_ent->inode->root_entry) { |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3404 | ERROR("Pseudo modify file \"%s\" is a pre-existing" |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3405 | " file in the filesystem being appended" |
| 3406 | " to. It cannot be modified. " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3407 | "Ignoring.\n", pseudo_ent->pathname); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3408 | continue; |
| 3409 | } |
| 3410 | buf = &dir_ent->inode->buf; |
| 3411 | buf->st_mode = (buf->st_mode & S_IFMT) | |
| 3412 | pseudo_ent->dev->mode; |
| 3413 | buf->st_uid = pseudo_ent->dev->uid; |
| 3414 | buf->st_gid = pseudo_ent->dev->gid; |
| 3415 | continue; |
| 3416 | } |
| 3417 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3418 | if(dir_ent) { |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3419 | if(dir_ent->inode->root_entry) |
| 3420 | ERROR("Pseudo file \"%s\" is a pre-existing" |
| 3421 | " file in the filesystem being appended" |
| 3422 | " to. Ignoring.\n", |
| 3423 | pseudo_ent->pathname); |
| 3424 | else |
| 3425 | ERROR("Pseudo file \"%s\" exists in source " |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3426 | "filesystem \"%s\".\nIgnoring, " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3427 | "exclude it (-e/-ef) to override.\n", |
| 3428 | pseudo_ent->pathname, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3429 | pathname(dir_ent)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3430 | continue; |
| 3431 | } |
| 3432 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3433 | memset(&buf, 0, sizeof(buf)); |
| 3434 | buf.st_mode = pseudo_ent->dev->mode; |
| 3435 | buf.st_uid = pseudo_ent->dev->uid; |
| 3436 | buf.st_gid = pseudo_ent->dev->gid; |
| 3437 | buf.st_rdev = makedev(pseudo_ent->dev->major, |
| 3438 | pseudo_ent->dev->minor); |
plougher | 7e58f4d | 2009-04-05 12:06:19 +0000 | [diff] [blame] | 3439 | buf.st_mtime = time(NULL); |
plougher | 1a3fbf2 | 2009-04-05 12:04:16 +0000 | [diff] [blame] | 3440 | buf.st_ino = pseudo_ino ++; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3441 | |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3442 | if(pseudo_ent->dev->type == 'd') { |
| 3443 | struct dir_ent *dir_ent = |
| 3444 | create_dir_entry(pseudo_ent->name, NULL, |
| 3445 | pseudo_ent->pathname, dir); |
| 3446 | char *subpath = strdup(subpathname(dir_ent)); |
| 3447 | struct dir_info *sub_dir = scan1_opendir("", subpath, |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3448 | dir->depth + 1); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3449 | if(sub_dir == NULL) { |
| 3450 | ERROR("Could not create pseudo directory \"%s\"" |
| 3451 | ", skipping...\n", |
| 3452 | pseudo_ent->pathname); |
| 3453 | free(subpath); |
| 3454 | pseudo_ino --; |
| 3455 | continue; |
| 3456 | } |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3457 | dir_scan2(sub_dir, pseudo_ent->pseudo); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3458 | dir->directory_count ++; |
| 3459 | add_dir_entry(dir_ent, sub_dir, |
| 3460 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0)); |
| 3461 | } else if(pseudo_ent->dev->type == 'f') { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3462 | #ifdef USE_TMP_FILE |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3463 | struct stat buf2; |
| 3464 | int res = stat(pseudo_ent->dev->filename, &buf2); |
| 3465 | if(res == -1) { |
| 3466 | ERROR("Stat on pseudo file \"%s\" failed, " |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3467 | "skipping...\n", pseudo_ent->pathname); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3468 | pseudo_ino --; |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3469 | continue; |
| 3470 | } |
| 3471 | buf.st_size = buf2.st_size; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3472 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3473 | pseudo_ent->dev->filename, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3474 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3475 | #else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3476 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3477 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3478 | lookup_inode2(&buf, PSEUDO_FILE_PROCESS, |
| 3479 | pseudo_ent->dev->pseudo_id), dir); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3480 | #endif |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3481 | } else { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3482 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3483 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3484 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3485 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3486 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3490 | /* |
| 3491 | * dir_scan3 routines... |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3492 | * This processes the move action |
| 3493 | */ |
| 3494 | void dir_scan3(struct dir_info *root, struct dir_info *dir) |
| 3495 | { |
| 3496 | struct dir_ent *dir_ent = NULL; |
| 3497 | |
| 3498 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
| 3499 | |
| 3500 | eval_move_actions(root, dir_ent); |
| 3501 | |
| 3502 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
| 3503 | dir_scan3(root, dir_ent->dir); |
| 3504 | } |
| 3505 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3506 | |
| 3507 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3508 | /* |
| 3509 | * dir_scan4 routines... |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3510 | * This processes the empty action. This action has to be processed after |
| 3511 | * all other actions because the previous exclude and move actions and the |
| 3512 | * pseudo actions affect whether a directory is empty |
| 3513 | */ |
| 3514 | void dir_scan4(struct dir_info *dir) |
| 3515 | { |
| 3516 | struct dir_ent *dir_ent = dir->list, *prev = NULL; |
| 3517 | |
| 3518 | while(dir_ent) { |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3519 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) { |
| 3520 | dir_scan4(dir_ent->dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3521 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3522 | if(eval_empty_actions(dir_ent)) { |
| 3523 | struct dir_ent *tmp = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3524 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3525 | /* |
| 3526 | * delete sub-directory, this is by definition |
| 3527 | * empty |
| 3528 | */ |
| 3529 | free(dir_ent->dir->pathname); |
| 3530 | free(dir_ent->dir->subpath); |
| 3531 | free(dir_ent->dir); |
| 3532 | |
| 3533 | /* remove dir_ent from list */ |
| 3534 | dir_ent = dir_ent->next; |
| 3535 | if(prev) |
| 3536 | prev->next = dir_ent; |
| 3537 | else |
| 3538 | dir->list = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3539 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3540 | /* free it */ |
| 3541 | free_dir_entry(tmp); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3542 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3543 | /* update counts */ |
| 3544 | dir->directory_count --; |
| 3545 | dir->count --; |
| 3546 | add_excluded(dir); |
| 3547 | continue; |
| 3548 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3549 | } |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3550 | |
| 3551 | prev = dir_ent; |
| 3552 | dir_ent = dir_ent->next; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | |
| 3557 | /* |
| 3558 | * dir_scan5 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3559 | * This sorts every directory and computes the inode numbers |
| 3560 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3561 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3562 | /* |
| 3563 | * Bottom up linked list merge sort. |
| 3564 | * |
| 3565 | * Qsort and other O(n log n) algorithms work well with arrays but not |
| 3566 | * linked lists. Merge sort another O(n log n) sort algorithm on the other hand |
| 3567 | * is not ideal for arrays (as it needs an additonal n storage locations |
| 3568 | * as sorting is not done in place), but it is ideal for linked lists because |
| 3569 | * it doesn't require any extra storage, |
| 3570 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3571 | void sort_directory(struct dir_info *dir) |
| 3572 | { |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3573 | struct dir_ent *cur, *l1, *l2, *next; |
| 3574 | int len1, len2, stride = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3575 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3576 | if(dir->count < 2) |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3577 | return; |
| 3578 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3579 | /* |
| 3580 | * We can consider our linked-list to be made up of stride length |
| 3581 | * sublists. Eacn iteration around this loop merges adjacent |
| 3582 | * stride length sublists into larger 2*stride sublists. We stop |
| 3583 | * when stride becomes equal to the entire list. |
| 3584 | * |
| 3585 | * Initially stride = 1 (by definition a sublist of 1 is sorted), and |
| 3586 | * these 1 element sublists are merged into 2 element sublists, which |
| 3587 | * are then merged into 4 element sublists and so on. |
| 3588 | */ |
| 3589 | do { |
| 3590 | l2 = dir->list; /* head of current linked list */ |
| 3591 | cur = NULL; /* empty output list */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3592 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3593 | /* |
| 3594 | * Iterate through the linked list, merging adjacent sublists. |
| 3595 | * On each interation l2 points to the next sublist pair to be |
| 3596 | * merged (if there's only one sublist left this is simply added |
| 3597 | * to the output list) |
| 3598 | */ |
| 3599 | while(l2) { |
| 3600 | l1 = l2; |
| 3601 | for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next); |
| 3602 | len2 = stride; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3603 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3604 | /* |
| 3605 | * l1 points to first sublist. |
| 3606 | * l2 points to second sublist. |
| 3607 | * Merge them onto the output list |
| 3608 | */ |
| 3609 | while(len1 && l2 && len2) { |
| 3610 | if(strcmp(l1->name, l2->name) <= 0) { |
| 3611 | next = l1; |
| 3612 | l1 = l1->next; |
| 3613 | len1 --; |
| 3614 | } else { |
| 3615 | next = l2; |
| 3616 | l2 = l2->next; |
| 3617 | len2 --; |
| 3618 | } |
| 3619 | |
| 3620 | if(cur) { |
| 3621 | cur->next = next; |
| 3622 | cur = next; |
| 3623 | } else |
| 3624 | dir->list = cur = next; |
| 3625 | } |
| 3626 | /* |
| 3627 | * One sublist is now empty, copy the other one onto the |
| 3628 | * output list |
| 3629 | */ |
| 3630 | for(; len1; len1 --, l1 = l1->next) { |
| 3631 | if(cur) { |
| 3632 | cur->next = l1; |
| 3633 | cur = l1; |
| 3634 | } else |
| 3635 | dir->list = cur = l1; |
| 3636 | } |
| 3637 | for(; l2 && len2; len2 --, l2 = l2->next) { |
| 3638 | if(cur) { |
| 3639 | cur->next = l2; |
| 3640 | cur = l2; |
| 3641 | } else |
| 3642 | dir->list = cur = l2; |
| 3643 | } |
| 3644 | } |
| 3645 | cur->next = NULL; |
| 3646 | stride = stride << 1; |
| 3647 | } while(stride < dir->count); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3651 | void dir_scan5(struct dir_info *dir) |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3652 | { |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3653 | struct dir_ent *dir_ent; |
| 3654 | unsigned int byte_count = 0; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3655 | |
| 3656 | sort_directory(dir); |
| 3657 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3658 | for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) { |
| 3659 | byte_count += strlen(dir_ent->name) + |
| 3660 | sizeof(struct squashfs_dir_entry); |
| 3661 | |
| 3662 | if(dir_ent->inode->root_entry) |
| 3663 | continue; |
| 3664 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3665 | alloc_inode_no(dir_ent->inode, 0); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3666 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3667 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | e69b588 | 2012-11-19 01:31:09 +0000 | [diff] [blame] | 3668 | dir_scan5(dir_ent->dir); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3669 | } |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3670 | |
| 3671 | if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE)) |
| 3672 | dir->dir_is_ldir = FALSE; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | |
| 3676 | /* |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3677 | * dir_scan6 routines... |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3678 | * This generates the filesystem metadata and writes it out to the destination |
| 3679 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3680 | void scan6_init_dir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3681 | { |
| 3682 | dir->buff = malloc(SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3683 | if(dir->buff == NULL) |
| 3684 | MEM_ERROR(); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3685 | |
| 3686 | dir->size = SQUASHFS_METADATA_SIZE; |
| 3687 | dir->p = dir->index_count_p = dir->buff; |
| 3688 | dir->entry_count = 256; |
| 3689 | dir->entry_count_p = NULL; |
| 3690 | dir->index = NULL; |
| 3691 | dir->i_count = dir->i_size = 0; |
| 3692 | } |
| 3693 | |
| 3694 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3695 | 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] | 3696 | struct dir_ent *dir_ent) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3697 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3698 | if (dir_ent == NULL) |
| 3699 | dir_ent = dir_info->list; |
| 3700 | else |
| 3701 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3702 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3703 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next) |
| 3704 | add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number, |
| 3705 | dir_ent->name, dir_ent->inode->type, dir); |
| 3706 | |
| 3707 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3711 | void scan6_freedir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3712 | { |
| 3713 | if(dir->index) |
| 3714 | free(dir->index); |
| 3715 | free(dir->buff); |
| 3716 | } |
| 3717 | |
| 3718 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3719 | void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3720 | { |
| 3721 | int squashfs_type; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3722 | int duplicate_file; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3723 | struct directory dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3724 | struct dir_ent *dir_ent = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3725 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3726 | scan6_init_dir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3727 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3728 | while((dir_ent = scan6_readdir(&dir, dir_info, dir_ent)) != NULL) { |
Phillip Lougher | 0a67088 | 2012-10-05 04:09:13 +0100 | [diff] [blame] | 3729 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3730 | |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 3731 | update_info(dir_ent); |
| 3732 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3733 | if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) { |
| 3734 | switch(buf->st_mode & S_IFMT) { |
| 3735 | case S_IFREG: |
| 3736 | squashfs_type = SQUASHFS_FILE_TYPE; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3737 | write_file(inode, dir_ent, |
| 3738 | &duplicate_file); |
| 3739 | INFO("file %s, uncompressed size %lld " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3740 | "bytes %s\n", |
| 3741 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3742 | (long long) buf->st_size, |
| 3743 | duplicate_file ? "DUPLICATE" : |
| 3744 | ""); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3745 | break; |
| 3746 | |
| 3747 | case S_IFDIR: |
| 3748 | squashfs_type = SQUASHFS_DIR_TYPE; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3749 | dir_scan6(inode, dir_ent->dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3750 | break; |
| 3751 | |
| 3752 | case S_IFLNK: |
| 3753 | squashfs_type = SQUASHFS_SYMLINK_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3754 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3755 | squashfs_type, 0, 0, 0, NULL, |
| 3756 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3757 | INFO("symbolic link %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3758 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3759 | sym_count ++; |
| 3760 | break; |
| 3761 | |
| 3762 | case S_IFCHR: |
| 3763 | squashfs_type = SQUASHFS_CHRDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3764 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3765 | squashfs_type, 0, 0, 0, NULL, |
| 3766 | NULL, NULL, 0); |
| 3767 | INFO("character device %s inode 0x%llx" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3768 | "\n", subpathname(dir_ent), |
| 3769 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3770 | dev_count ++; |
| 3771 | break; |
| 3772 | |
| 3773 | case S_IFBLK: |
| 3774 | squashfs_type = SQUASHFS_BLKDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3775 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3776 | squashfs_type, 0, 0, 0, NULL, |
| 3777 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3778 | INFO("block device %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3779 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3780 | dev_count ++; |
| 3781 | break; |
| 3782 | |
| 3783 | case S_IFIFO: |
| 3784 | squashfs_type = SQUASHFS_FIFO_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); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3788 | INFO("fifo %s inode 0x%llx\n", |
| 3789 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3790 | fifo_count ++; |
| 3791 | break; |
| 3792 | |
| 3793 | case S_IFSOCK: |
| 3794 | squashfs_type = SQUASHFS_SOCKET_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); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3798 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3799 | "0x%llx\n", |
| 3800 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3801 | sock_count ++; |
| 3802 | break; |
| 3803 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 3804 | default: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3805 | BAD_ERROR("%s unrecognised file type, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3806 | "mode is %x\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3807 | subpathname(dir_ent), |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3808 | buf->st_mode); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3809 | } |
| 3810 | dir_ent->inode->inode = *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3811 | dir_ent->inode->type = squashfs_type; |
| 3812 | } else { |
| 3813 | *inode = dir_ent->inode->inode; |
| 3814 | squashfs_type = dir_ent->inode->type; |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3815 | switch(squashfs_type) { |
| 3816 | case SQUASHFS_FILE_TYPE: |
| 3817 | if(!sorted) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3818 | INFO("file %s, uncompressed " |
| 3819 | "size %lld bytes LINK" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3820 | "\n", |
| 3821 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3822 | (long long) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3823 | buf->st_size); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3824 | break; |
| 3825 | case SQUASHFS_SYMLINK_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3826 | INFO("symbolic link %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3827 | "LINK\n", subpathname(dir_ent), |
| 3828 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3829 | break; |
| 3830 | case SQUASHFS_CHRDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3831 | INFO("character device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3832 | "LINK\n", subpathname(dir_ent), |
| 3833 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3834 | break; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3835 | case SQUASHFS_BLKDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3836 | INFO("block device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3837 | "LINK\n", subpathname(dir_ent), |
| 3838 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3839 | break; |
| 3840 | case SQUASHFS_FIFO_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3841 | INFO("fifo %s inode 0x%llx LINK\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3842 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3843 | break; |
| 3844 | case SQUASHFS_SOCKET_TYPE: |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3845 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3846 | "0x%llx LINK\n", |
| 3847 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3848 | break; |
| 3849 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Phillip Lougher | 0366aec | 2012-10-05 04:06:04 +0100 | [diff] [blame] | 3852 | add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name, |
| 3853 | squashfs_type, &dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3856 | write_dir(inode, dir_info, &dir); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3857 | INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent), |
| 3858 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3859 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3860 | scan6_freedir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | |
| 3864 | unsigned int slog(unsigned int block) |
| 3865 | { |
| 3866 | int i; |
| 3867 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 3868 | for(i = 12; i <= 20; i++) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3869 | if(block == (1 << i)) |
| 3870 | return i; |
| 3871 | return 0; |
| 3872 | } |
| 3873 | |
| 3874 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3875 | int old_excluded(char *filename, struct stat *buf) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3876 | { |
| 3877 | int i; |
| 3878 | |
| 3879 | for(i = 0; i < exclude; i++) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3880 | if((exclude_paths[i].st_dev == buf->st_dev) && |
| 3881 | (exclude_paths[i].st_ino == buf->st_ino)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3882 | return TRUE; |
| 3883 | return FALSE; |
| 3884 | } |
| 3885 | |
| 3886 | |
| 3887 | #define ADD_ENTRY(buf) \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3888 | if(exclude % EXCLUDE_SIZE == 0) { \ |
| 3889 | exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \ |
| 3890 | * sizeof(struct exclude_info)); \ |
| 3891 | if(exclude_paths == NULL) \ |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3892 | MEM_ERROR(); \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3893 | } \ |
| 3894 | exclude_paths[exclude].st_dev = buf.st_dev; \ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3895 | exclude_paths[exclude++].st_ino = buf.st_ino; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3896 | int old_add_exclude(char *path) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3897 | { |
| 3898 | int i; |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3899 | char *filename; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3900 | struct stat buf; |
| 3901 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3902 | if(path[0] == '/' || strncmp(path, "./", 2) == 0 || |
| 3903 | strncmp(path, "../", 3) == 0) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3904 | if(lstat(path, &buf) == -1) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3905 | ERROR("Cannot stat exclude dir/file %s because %s, " |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3906 | "ignoring\n", path, strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3907 | return TRUE; |
| 3908 | } |
| 3909 | ADD_ENTRY(buf); |
| 3910 | return TRUE; |
| 3911 | } |
| 3912 | |
| 3913 | for(i = 0; i < source; i++) { |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3914 | int res = asprintf(&filename, "%s/%s", source_path[i], path); |
| 3915 | if(res == -1) |
| 3916 | BAD_ERROR("asprintf failed in old_add_exclude\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3917 | if(lstat(filename, &buf) == -1) { |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 3918 | if(!(errno == ENOENT || errno == ENOTDIR)) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3919 | ERROR("Cannot stat exclude dir/file %s because " |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3920 | "%s, ignoring\n", filename, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3921 | strerror(errno)); |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3922 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3923 | continue; |
| 3924 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3925 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3926 | ADD_ENTRY(buf); |
| 3927 | } |
| 3928 | return TRUE; |
| 3929 | } |
| 3930 | |
| 3931 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3932 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 3933 | int type) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3934 | { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3935 | old_root_entry = realloc(old_root_entry, |
| 3936 | sizeof(struct old_root_entry_info) * (old_root_entries + 1)); |
| 3937 | if(old_root_entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3938 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3939 | |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3940 | old_root_entry[old_root_entries].name = strdup(name); |
| 3941 | old_root_entry[old_root_entries].inode.inode = inode; |
| 3942 | old_root_entry[old_root_entries].inode.inode_number = inode_number; |
| 3943 | old_root_entry[old_root_entries].inode.type = type; |
| 3944 | old_root_entry[old_root_entries++].inode.root_entry = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 3948 | void initialise_threads(int readb_mbytes, int writeb_mbytes, |
Phillip Lougher | c97dac5 | 2013-04-14 03:32:10 +0100 | [diff] [blame] | 3949 | int fragmentb_mbytes, int freelst) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3950 | { |
| 3951 | int i; |
| 3952 | sigset_t sigmask, old_mask; |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 3953 | int reader_buffer_size; |
| 3954 | int fragment_buffer_size; |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 3955 | /* |
| 3956 | * writer_buffer_size is global because it is needed in |
| 3957 | * write_file_blocks_dup() |
| 3958 | */ |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 3959 | |
| 3960 | /* |
| 3961 | * convert from queue size in Mbytes to queue size in |
| 3962 | * blocks. |
| 3963 | * |
| 3964 | * In doing so, check that the user supplied values do not |
| 3965 | * overflow a signed int |
| 3966 | */ |
| 3967 | if(shift_overflow(readb_mbytes, 20 - block_log)) |
| 3968 | BAD_ERROR("Read queue is too large\n"); |
| 3969 | else |
| 3970 | reader_buffer_size = readb_mbytes << (20 - block_log); |
| 3971 | |
| 3972 | if(shift_overflow(fragmentb_mbytes, 20 - block_log)) |
| 3973 | BAD_ERROR("Fragment queue is too large\n"); |
| 3974 | else |
| 3975 | fragment_buffer_size = fragmentb_mbytes << (20 - block_log); |
| 3976 | |
| 3977 | if(shift_overflow(writeb_mbytes, 20 - block_log)) |
| 3978 | BAD_ERROR("Write queue is too large\n"); |
| 3979 | else |
| 3980 | writer_buffer_size = writeb_mbytes << (20 - block_log); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3981 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 3982 | /* |
| 3983 | * setup signal handlers for the main thread, these cleanup |
| 3984 | * deleting the destination file, if appending the |
| 3985 | * handlers for SIGTERM and SIGINT will be replaced with handlers |
| 3986 | * allowing the user to press ^C twice to restore the existing |
| 3987 | * filesystem. |
| 3988 | * |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 3989 | * SIGUSR1 is an internal signal, which is used by the sub-threads |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 3990 | * to tell the main thread to terminate, deleting the destination file, |
| 3991 | * or if necessary restoring the filesystem on appending |
| 3992 | */ |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 3993 | signal(SIGTERM, sighandler); |
| 3994 | signal(SIGINT, sighandler); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 3995 | signal(SIGUSR1, sighandler); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 3996 | |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 3997 | /* block SIGQUIT and SIGHUP, these are handled by the info thread */ |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 3998 | sigemptyset(&sigmask); |
| 3999 | sigaddset(&sigmask, SIGQUIT); |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 4000 | sigaddset(&sigmask, SIGHUP); |
Phillip Lougher | b1259f7 | 2013-06-06 03:10:15 +0100 | [diff] [blame] | 4001 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4002 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4003 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4004 | /* |
| 4005 | * temporarily block these signals, so the created sub-threads |
| 4006 | * will ignore them, ensuring the main thread handles them |
| 4007 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4008 | sigemptyset(&sigmask); |
| 4009 | sigaddset(&sigmask, SIGINT); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4010 | sigaddset(&sigmask, SIGTERM); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4011 | sigaddset(&sigmask, SIGUSR1); |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4012 | if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4013 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4014 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4015 | if(processors == -1) { |
| 4016 | #ifndef linux |
| 4017 | int mib[2]; |
| 4018 | size_t len = sizeof(processors); |
| 4019 | |
| 4020 | mib[0] = CTL_HW; |
| 4021 | #ifdef HW_AVAILCPU |
| 4022 | mib[1] = HW_AVAILCPU; |
| 4023 | #else |
| 4024 | mib[1] = HW_NCPU; |
| 4025 | #endif |
| 4026 | |
| 4027 | if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4028 | ERROR("Failed to get number of available processors. " |
| 4029 | "Defaulting to 1\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4030 | processors = 1; |
| 4031 | } |
| 4032 | #else |
plougher | 9cc26b7 | 2010-08-17 01:05:55 +0000 | [diff] [blame] | 4033 | processors = sysconf(_SC_NPROCESSORS_ONLN); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4034 | #endif |
| 4035 | } |
| 4036 | |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4037 | if(multiply_overflow(processors, 2) || |
| 4038 | add_overflow(processors * 2, 2) || |
| 4039 | multiply_overflow(processors * 2 + 2, |
| 4040 | sizeof(pthread_t))) |
| 4041 | BAD_ERROR("Processors too large\n"); |
| 4042 | |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4043 | deflator_thread = malloc(processors * 2 * sizeof(pthread_t)); |
| 4044 | if(deflator_thread == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4045 | MEM_ERROR(); |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4046 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4047 | frag_deflator_thread = &deflator_thread[processors]; |
| 4048 | |
| 4049 | to_reader = queue_init(1); |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 4050 | to_deflate = queue_init(reader_buffer_size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4051 | to_writer = queue_init(writer_buffer_size); |
| 4052 | from_writer = queue_init(1); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 4053 | to_frag = queue_init(fragment_buffer_size); |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 4054 | locked_fragment = queue_init(fragment_buffer_size); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 4055 | to_main = seq_queue_init(); |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 4056 | reader_buffer = cache_init(block_size, reader_buffer_size, 0, 0); |
| 4057 | writer_buffer = cache_init(block_size, writer_buffer_size, 1, freelst); |
| 4058 | fragment_buffer = cache_init(block_size, fragment_buffer_size, 1, |
| 4059 | freelst); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4060 | pthread_create(&reader_thread, NULL, reader, NULL); |
| 4061 | pthread_create(&writer_thread, NULL, writer, NULL); |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 4062 | init_progress_bar(); |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4063 | init_info(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4064 | pthread_mutex_init(&fragment_mutex, NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4065 | |
| 4066 | for(i = 0; i < processors; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4067 | if(pthread_create(&deflator_thread[i], NULL, deflator, NULL) != |
| 4068 | 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4069 | BAD_ERROR("Failed to create thread\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4070 | if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator, |
| 4071 | NULL) != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4072 | BAD_ERROR("Failed to create thread\n"); |
| 4073 | } |
| 4074 | |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4075 | main_thread = pthread_self(); |
| 4076 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4077 | printf("Parallel mksquashfs: Using %d processor%s\n", processors, |
| 4078 | processors == 1 ? "" : "s"); |
| 4079 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4080 | /* Restore the signal mask for the main thread */ |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4081 | if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4082 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4083 | } |
| 4084 | |
| 4085 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4086 | long long write_inode_lookup_table() |
| 4087 | { |
| 4088 | int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4089 | void *it; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4090 | |
| 4091 | if(inode_count == sinode_count) |
| 4092 | goto skip_inode_hash_table; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4093 | |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4094 | it = realloc(inode_lookup_table, lookup_bytes); |
| 4095 | if(it == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4096 | MEM_ERROR(); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4097 | inode_lookup_table = it; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4098 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4099 | for(i = 0; i < INODE_HASH_SIZE; i ++) { |
| 4100 | struct inode_info *inode = inode_info[i]; |
| 4101 | |
| 4102 | for(inode = inode_info[i]; inode; inode = inode->next) { |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4103 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4104 | inode_number = get_inode_no(inode); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4105 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4106 | SQUASHFS_SWAP_LONG_LONGS(&inode->inode, |
| 4107 | &inode_lookup_table[inode_number - 1], 1); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4108 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4109 | } |
| 4110 | } |
| 4111 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4112 | skip_inode_hash_table: |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 4113 | return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL, |
| 4114 | noI); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4117 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4118 | char *get_component(char *target, char **targname) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4119 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4120 | char *start; |
| 4121 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4122 | while(*target == '/') |
rlougher | c4ebcf5 | 2007-11-08 17:52:49 +0000 | [diff] [blame] | 4123 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4124 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4125 | start = target; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4126 | while(*target != '/' && *target!= '\0') |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4127 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4128 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4129 | *targname = strndup(start, target - start); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4130 | |
| 4131 | return target; |
| 4132 | } |
| 4133 | |
| 4134 | |
| 4135 | void free_path(struct pathname *paths) |
| 4136 | { |
| 4137 | int i; |
| 4138 | |
| 4139 | for(i = 0; i < paths->names; i++) { |
| 4140 | if(paths->name[i].paths) |
| 4141 | free_path(paths->name[i].paths); |
| 4142 | free(paths->name[i].name); |
| 4143 | if(paths->name[i].preg) { |
| 4144 | regfree(paths->name[i].preg); |
| 4145 | free(paths->name[i].preg); |
| 4146 | } |
| 4147 | } |
| 4148 | |
| 4149 | free(paths); |
| 4150 | } |
| 4151 | |
| 4152 | |
| 4153 | struct pathname *add_path(struct pathname *paths, char *target, char *alltarget) |
| 4154 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4155 | char *targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4156 | int i, error; |
| 4157 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4158 | target = get_component(target, &targname); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4159 | |
| 4160 | if(paths == NULL) { |
plougher | dec6ef1 | 2010-12-18 02:47:53 +0000 | [diff] [blame] | 4161 | paths = malloc(sizeof(struct pathname)); |
| 4162 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4163 | MEM_ERROR(); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4164 | |
| 4165 | paths->names = 0; |
| 4166 | paths->name = NULL; |
| 4167 | } |
| 4168 | |
| 4169 | for(i = 0; i < paths->names; i++) |
| 4170 | if(strcmp(paths->name[i].name, targname) == 0) |
| 4171 | break; |
| 4172 | |
| 4173 | if(i == paths->names) { |
| 4174 | /* allocate new name entry */ |
| 4175 | paths->names ++; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4176 | paths->name = realloc(paths->name, (i + 1) * |
| 4177 | sizeof(struct path_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4178 | if(paths->name == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4179 | MEM_ERROR(); |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4180 | paths->name[i].name = targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4181 | paths->name[i].paths = NULL; |
| 4182 | if(use_regex) { |
| 4183 | paths->name[i].preg = malloc(sizeof(regex_t)); |
plougher | 5c60eab | 2010-07-21 01:12:19 +0000 | [diff] [blame] | 4184 | if(paths->name[i].preg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4185 | MEM_ERROR(); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4186 | error = regcomp(paths->name[i].preg, targname, |
| 4187 | REG_EXTENDED|REG_NOSUB); |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4188 | if(error) { |
Phillip Lougher | 62859d2 | 2012-11-30 05:53:56 +0000 | [diff] [blame] | 4189 | char str[1024]; /* overflow safe */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4190 | |
| 4191 | regerror(error, paths->name[i].preg, str, 1024); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4192 | BAD_ERROR("invalid regex %s in export %s, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4193 | "because %s\n", targname, alltarget, |
| 4194 | str); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4195 | } |
| 4196 | } else |
| 4197 | paths->name[i].preg = NULL; |
| 4198 | |
| 4199 | if(target[0] == '\0') |
| 4200 | /* at leaf pathname component */ |
| 4201 | paths->name[i].paths = NULL; |
| 4202 | else |
| 4203 | /* recurse adding child components */ |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4204 | paths->name[i].paths = add_path(NULL, target, |
| 4205 | alltarget); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4206 | } else { |
| 4207 | /* existing matching entry */ |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4208 | free(targname); |
| 4209 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4210 | if(paths->name[i].paths == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4211 | /* No sub-directory which means this is the leaf |
| 4212 | * component of a pre-existing exclude which subsumes |
| 4213 | * the exclude currently being added, in which case stop |
| 4214 | * adding components */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4215 | } else if(target[0] == '\0') { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4216 | /* at leaf pathname component and child components exist |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4217 | * from more specific excludes, delete as they're |
| 4218 | * subsumed by this exclude */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4219 | free_path(paths->name[i].paths); |
| 4220 | paths->name[i].paths = NULL; |
| 4221 | } else |
| 4222 | /* recurse adding child components */ |
| 4223 | add_path(paths->name[i].paths, target, alltarget); |
| 4224 | } |
| 4225 | |
| 4226 | return paths; |
| 4227 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4228 | |
| 4229 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4230 | void add_exclude(char *target) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4231 | { |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4232 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4233 | if(target[0] == '/' || strncmp(target, "./", 2) == 0 || |
| 4234 | strncmp(target, "../", 3) == 0) |
| 4235 | BAD_ERROR("/, ./ and ../ prefixed excludes not supported with " |
| 4236 | "-wildcards or -regex options\n"); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4237 | else if(strncmp(target, "... ", 4) == 0) |
| 4238 | stickypath = add_path(stickypath, target + 4, target + 4); |
| 4239 | else |
| 4240 | path = add_path(path, target, target); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | |
| 4244 | void display_path(int depth, struct pathname *paths) |
| 4245 | { |
| 4246 | int i, n; |
| 4247 | |
| 4248 | if(paths == NULL) |
| 4249 | return; |
| 4250 | |
| 4251 | for(i = 0; i < paths->names; i++) { |
| 4252 | for(n = 0; n < depth; n++) |
| 4253 | printf("\t"); |
| 4254 | printf("%d: %s\n", depth, paths->name[i].name); |
| 4255 | display_path(depth + 1, paths->name[i].paths); |
| 4256 | } |
| 4257 | } |
| 4258 | |
| 4259 | |
| 4260 | void display_path2(struct pathname *paths, char *string) |
| 4261 | { |
| 4262 | int i; |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4263 | char *path; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4264 | |
| 4265 | if(paths == NULL) { |
| 4266 | printf("%s\n", string); |
| 4267 | return; |
| 4268 | } |
| 4269 | |
| 4270 | for(i = 0; i < paths->names; i++) { |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4271 | int res = asprintf(&path, "%s/%s", string, paths->name[i].name); |
| 4272 | if(res == -1) |
| 4273 | BAD_ERROR("asprintf failed in display_path2\n"); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4274 | display_path2(paths->name[i].paths, path); |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4275 | free(path); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4276 | } |
| 4277 | } |
| 4278 | |
| 4279 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4280 | struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path) |
| 4281 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4282 | int count = paths == NULL ? 0 : paths->count; |
| 4283 | |
| 4284 | if(count % PATHS_ALLOC_SIZE == 0) { |
| 4285 | paths = realloc(paths, sizeof(struct pathnames) + |
| 4286 | (count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4287 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4288 | MEM_ERROR(); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4289 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4290 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4291 | paths->path[count] = path; |
| 4292 | paths->count = count + 1; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4293 | return paths; |
| 4294 | } |
| 4295 | |
| 4296 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4297 | int excluded_match(char *name, struct pathname *path, struct pathnames **new) |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4298 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4299 | int i; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4300 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4301 | for(i = 0; i < path->names; i++) { |
| 4302 | int match = use_regex ? |
| 4303 | regexec(path->name[i].preg, name, (size_t) 0, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4304 | NULL, 0) == 0 : |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4305 | fnmatch(path->name[i].name, name, |
| 4306 | FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4307 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4308 | if(match) { |
| 4309 | if(path->name[i].paths == NULL || new == NULL) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4310 | /* match on a leaf component, any subdirectories |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4311 | * in the filesystem should be excluded */ |
| 4312 | return TRUE; |
| 4313 | else |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4314 | /* match on a non-leaf component, add any |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4315 | * subdirectories to the new set of |
| 4316 | * subdirectories to scan for this name */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4317 | *new = add_subdir(*new, path->name[i].paths); |
| 4318 | } |
| 4319 | } |
| 4320 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4321 | return FALSE; |
| 4322 | } |
| 4323 | |
| 4324 | |
| 4325 | int excluded(char *name, struct pathnames *paths, struct pathnames **new) |
| 4326 | { |
| 4327 | int n; |
| 4328 | |
| 4329 | if(stickypath && excluded_match(name, stickypath, NULL)) |
| 4330 | return TRUE; |
| 4331 | |
| 4332 | for(n = 0; paths && n < paths->count; n++) { |
| 4333 | int res = excluded_match(name, paths->path[n], new); |
| 4334 | if(res) { |
| 4335 | free(*new); |
| 4336 | *new = NULL; |
| 4337 | return TRUE; |
| 4338 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4341 | /* |
| 4342 | * Either: |
| 4343 | * - no matching names found, return empty new search set, or |
| 4344 | * - one or more matches with sub-directories found (no leaf matches), |
| 4345 | * in which case return new search set. |
| 4346 | * |
| 4347 | * In either case return FALSE as we don't want to exclude this entry |
| 4348 | */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4349 | return FALSE; |
| 4350 | } |
| 4351 | |
| 4352 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4353 | void process_exclude_file(char *argv) |
| 4354 | { |
| 4355 | FILE *fd; |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4356 | char buffer[MAX_LINE + 1]; /* overflow safe */ |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4357 | char *filename; |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4358 | |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4359 | fd = fopen(argv, "r"); |
| 4360 | if(fd == NULL) |
| 4361 | BAD_ERROR("Failed to open exclude file \"%s\" because %s\n", |
| 4362 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4363 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4364 | while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) { |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4365 | int len = strlen(filename); |
| 4366 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4367 | if(len == MAX_LINE && filename[len - 1] != '\n') |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4368 | /* line too large */ |
| 4369 | BAD_ERROR("Line too long when reading " |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4370 | "exclude file \"%s\", larger than %d " |
Phillip Lougher | 83847c1 | 2012-12-23 07:22:36 +0000 | [diff] [blame] | 4371 | "bytes\n", argv, MAX_LINE); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4372 | |
| 4373 | /* |
| 4374 | * Remove '\n' terminator if it exists (the last line |
| 4375 | * in the file may not be '\n' terminated) |
| 4376 | */ |
| 4377 | if(len && filename[len - 1] == '\n') |
| 4378 | filename[len - 1] = '\0'; |
| 4379 | |
| 4380 | /* Skip any leading whitespace */ |
| 4381 | while(isspace(*filename)) |
| 4382 | filename ++; |
| 4383 | |
| 4384 | /* if comment line, skip */ |
| 4385 | if(*filename == '#') |
| 4386 | continue; |
| 4387 | |
| 4388 | /* |
| 4389 | * check for initial backslash, to accommodate |
| 4390 | * filenames with leading space or leading # character |
| 4391 | */ |
| 4392 | if(*filename == '\\') |
| 4393 | filename ++; |
| 4394 | |
| 4395 | /* if line is now empty after skipping characters, skip it */ |
| 4396 | if(*filename == '\0') |
| 4397 | continue; |
| 4398 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4399 | if(old_exclude) |
| 4400 | old_add_exclude(filename); |
| 4401 | else |
| 4402 | add_exclude(filename); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | if(ferror(fd)) |
| 4406 | BAD_ERROR("Reading exclude file \"%s\" failed because %s\n", |
| 4407 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4408 | |
| 4409 | fclose(fd); |
| 4410 | } |
| 4411 | |
| 4412 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4413 | #define RECOVER_ID "Squashfs recovery file v1.0\n" |
| 4414 | #define RECOVER_ID_SIZE 28 |
| 4415 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4416 | void write_recovery_data(struct squashfs_super_block *sBlk) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4417 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4418 | int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4419 | pid_t pid = getpid(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4420 | char *metadata; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4421 | char header[] = RECOVER_ID; |
| 4422 | |
| 4423 | if(recover == FALSE) { |
| 4424 | printf("No recovery data option specified.\n"); |
plougher | eac1853 | 2007-10-29 05:26:06 +0000 | [diff] [blame] | 4425 | printf("Skipping saving recovery file.\n\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4426 | return; |
| 4427 | } |
| 4428 | |
plougher | 1b879bc | 2010-12-18 02:49:42 +0000 | [diff] [blame] | 4429 | metadata = malloc(bytes); |
| 4430 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4431 | MEM_ERROR(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4432 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4433 | res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata); |
Phillip Lougher | 477f433 | 2013-03-06 03:55:08 +0000 | [diff] [blame] | 4434 | if(res == 0) { |
| 4435 | ERROR("Failed to read append filesystem metadata\n"); |
| 4436 | BAD_ERROR("Filesystem corrupted?\n"); |
| 4437 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4438 | |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4439 | res = asprintf(&recovery_file, "squashfs_recovery_%s_%d", |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4440 | getbase(destination_file), pid); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4441 | if(res == -1) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4442 | MEM_ERROR(); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4443 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4444 | recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU); |
| 4445 | if(recoverfd == -1) |
| 4446 | BAD_ERROR("Failed to create recovery file, because %s. " |
| 4447 | "Aborting\n", strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4448 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4449 | if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4450 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4451 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4452 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4453 | if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4454 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4455 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4456 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4457 | if(write_bytes(recoverfd, metadata, bytes) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4458 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4459 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4460 | |
| 4461 | close(recoverfd); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4462 | free(metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4463 | |
| 4464 | printf("Recovery file \"%s\" written\n", recovery_file); |
| 4465 | printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4466 | printf("mksquashfs dummy %s -recover %s\n", destination_file, |
| 4467 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4468 | printf("to restore filesystem\n\n"); |
| 4469 | } |
| 4470 | |
| 4471 | |
| 4472 | void read_recovery_data(char *recovery_file, char *destination_file) |
| 4473 | { |
| 4474 | int fd, recoverfd, bytes; |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4475 | struct squashfs_super_block orig_sBlk, sBlk; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4476 | char *metadata; |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4477 | int res; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4478 | struct stat buf; |
| 4479 | char header[] = RECOVER_ID; |
| 4480 | char header2[RECOVER_ID_SIZE]; |
| 4481 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4482 | recoverfd = open(recovery_file, O_RDONLY); |
| 4483 | if(recoverfd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4484 | BAD_ERROR("Failed to open recovery file because %s\n", |
| 4485 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4486 | |
| 4487 | if(stat(destination_file, &buf) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4488 | BAD_ERROR("Failed to stat destination file, because %s\n", |
| 4489 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4490 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4491 | fd = open(destination_file, O_RDWR); |
| 4492 | if(fd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4493 | BAD_ERROR("Failed to open destination file because %s\n", |
| 4494 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4495 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4496 | res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE); |
| 4497 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4498 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4499 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4500 | if(res < RECOVER_ID_SIZE) |
| 4501 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4502 | if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 ) |
| 4503 | BAD_ERROR("Not a recovery file\n"); |
| 4504 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4505 | res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4506 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4507 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4508 | strerror(errno)); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4509 | if(res < sizeof(struct squashfs_super_block)) |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4510 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4511 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4512 | 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] | 4513 | if(res == 0) { |
| 4514 | ERROR("Failed to read superblock from output filesystem\n"); |
| 4515 | BAD_ERROR("Output filesystem is empty!\n"); |
| 4516 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4517 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4518 | if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4, |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4519 | sizeof(struct squashfs_super_block) - 4) != 0) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4520 | BAD_ERROR("Recovery file and destination file do not seem to " |
| 4521 | "match\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4522 | |
| 4523 | bytes = sBlk.bytes_used - sBlk.inode_table_start; |
| 4524 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4525 | metadata = malloc(bytes); |
| 4526 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4527 | MEM_ERROR(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4528 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4529 | res = read_bytes(recoverfd, metadata, bytes); |
| 4530 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4531 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4532 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4533 | if(res < bytes) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4534 | BAD_ERROR("Recovery file appears to be truncated\n"); |
| 4535 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4536 | write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4537 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 4538 | write_destination(fd, sBlk.inode_table_start, bytes, metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4539 | |
| 4540 | close(recoverfd); |
| 4541 | close(fd); |
| 4542 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4543 | printf("Successfully wrote recovery file \"%s\". Exiting\n", |
| 4544 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4545 | |
| 4546 | exit(0); |
| 4547 | } |
| 4548 | |
| 4549 | |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4550 | int parse_number(char *start, int *res, int size) |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4551 | { |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4552 | char *end; |
| 4553 | long number = strtol(start, &end, 10); |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4554 | |
Phillip Lougher | f4f4838 | 2012-11-29 03:52:44 +0000 | [diff] [blame] | 4555 | /* |
| 4556 | * check for strtol underflow or overflow in conversion. |
| 4557 | * Note: strtol can validly return LONG_MIN and LONG_MAX |
| 4558 | * if the user entered these values, but, additional code |
| 4559 | * to distinguish this scenario is unnecessary, because for |
| 4560 | * our purposes LONG_MIN and LONG_MAX are too large anyway |
| 4561 | */ |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4562 | if(number == LONG_MIN || number == LONG_MAX) |
| 4563 | return 0; |
| 4564 | |
| 4565 | /* reject negative numbers as invalid */ |
| 4566 | if(number < 0) |
| 4567 | return 0; |
| 4568 | |
| 4569 | /* check if long result will overflow signed int */ |
| 4570 | if(number > INT_MAX) |
| 4571 | return 0; |
| 4572 | |
| 4573 | if(size) { |
Phillip Lougher | ed34569 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 4574 | /* |
| 4575 | * Check for multiplier and trailing junk. |
| 4576 | * But first check that a number exists before the |
| 4577 | * multiplier |
| 4578 | */ |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4579 | if(end == start) |
Phillip Lougher | ed34569 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 4580 | return 0; |
| 4581 | |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4582 | switch(end[0]) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4583 | case 'm': |
| 4584 | case 'M': |
| 4585 | if(multiply_overflow((int) number, 1048576)) |
| 4586 | return 0; |
| 4587 | number *= 1048576; |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4588 | |
Phillip Lougher | f968be8 | 2012-12-04 05:12:32 +0000 | [diff] [blame] | 4589 | if(end[1] != '\0') |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4590 | /* trailing junk after number */ |
| 4591 | return 0; |
| 4592 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4593 | break; |
| 4594 | case 'k': |
| 4595 | case 'K': |
| 4596 | if(multiply_overflow((int) number, 1024)) |
| 4597 | return 0; |
| 4598 | number *= 1024; |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4599 | |
Phillip Lougher | f968be8 | 2012-12-04 05:12:32 +0000 | [diff] [blame] | 4600 | if(end[1] != '\0') |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4601 | /* trailing junk after number */ |
| 4602 | return 0; |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4603 | case '\0': |
| 4604 | break; |
| 4605 | default: |
| 4606 | /* trailing junk after number */ |
| 4607 | return 0; |
| 4608 | } |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4609 | } else if(end[0] != '\0') |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4610 | /* trailing junk after number */ |
| 4611 | return 0; |
| 4612 | |
| 4613 | *res = number; |
| 4614 | return 1; |
| 4615 | } |
| 4616 | |
| 4617 | |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4618 | void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad) |
| 4619 | { |
| 4620 | int i; |
| 4621 | |
| 4622 | sBlk->fragments = fragments; |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4623 | sBlk->no_ids = id_count; |
| 4624 | sBlk->inode_table_start = write_inodes(); |
| 4625 | sBlk->directory_table_start = write_directories(); |
| 4626 | sBlk->fragment_table_start = write_fragment_table(); |
| 4627 | sBlk->lookup_table_start = exportable ? write_inode_lookup_table() : |
| 4628 | SQUASHFS_INVALID_BLK; |
| 4629 | sBlk->id_table_start = write_id_table(); |
| 4630 | sBlk->xattr_id_table_start = write_xattrs(); |
| 4631 | |
| 4632 | TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start); |
| 4633 | TRACE("sBlk->directory_table_start 0x%llx\n", |
| 4634 | sBlk->directory_table_start); |
| 4635 | TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start); |
| 4636 | if(exportable) |
| 4637 | TRACE("sBlk->lookup_table_start 0x%llx\n", |
| 4638 | sBlk->lookup_table_start); |
| 4639 | |
| 4640 | sBlk->bytes_used = bytes; |
| 4641 | |
| 4642 | sBlk->compression = comp->id; |
| 4643 | |
| 4644 | SQUASHFS_INSWAP_SUPER_BLOCK(sBlk); |
| 4645 | write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk); |
| 4646 | |
| 4647 | if(!nopad && (i = bytes & (4096 - 1))) { |
| 4648 | char temp[4096] = {0}; |
| 4649 | write_destination(fd, bytes, 4096 - i, temp); |
| 4650 | } |
| 4651 | |
| 4652 | close(fd); |
| 4653 | |
| 4654 | delete_pseudo_files(); |
| 4655 | |
| 4656 | if(recovery_file) |
| 4657 | unlink(recovery_file); |
| 4658 | |
| 4659 | total_bytes += total_inode_bytes + total_directory_bytes + |
| 4660 | sizeof(struct squashfs_super_block) + total_xattr_bytes; |
| 4661 | |
| 4662 | printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size" |
| 4663 | " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR, |
| 4664 | SQUASHFS_MINOR, comp->name, block_size); |
| 4665 | printf("\t%s data, %s metadata, %s fragments, %s xattrs\n", |
| 4666 | noD ? "uncompressed" : "compressed", noI ? "uncompressed" : |
| 4667 | "compressed", no_fragments ? "no" : noF ? "uncompressed" : |
| 4668 | "compressed", no_xattrs ? "no" : noX ? "uncompressed" : |
| 4669 | "compressed"); |
| 4670 | printf("\tduplicates are %sremoved\n", duplicate_checking ? "" : |
| 4671 | "not "); |
| 4672 | printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, |
| 4673 | bytes / (1024.0 * 1024.0)); |
| 4674 | printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n", |
| 4675 | ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0); |
| 4676 | printf("Inode table size %d bytes (%.2f Kbytes)\n", |
| 4677 | inode_bytes, inode_bytes / 1024.0); |
| 4678 | printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n", |
| 4679 | ((float) inode_bytes / total_inode_bytes) * 100.0, |
| 4680 | total_inode_bytes); |
| 4681 | printf("Directory table size %d bytes (%.2f Kbytes)\n", |
| 4682 | directory_bytes, directory_bytes / 1024.0); |
| 4683 | printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n", |
| 4684 | ((float) directory_bytes / total_directory_bytes) * 100.0, |
| 4685 | total_directory_bytes); |
| 4686 | if(total_xattr_bytes) { |
| 4687 | printf("Xattr table size %d bytes (%.2f Kbytes)\n", |
| 4688 | xattr_bytes, xattr_bytes / 1024.0); |
| 4689 | printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n", |
| 4690 | ((float) xattr_bytes / total_xattr_bytes) * 100.0, |
| 4691 | total_xattr_bytes); |
| 4692 | } |
| 4693 | if(duplicate_checking) |
| 4694 | printf("Number of duplicate files found %d\n", file_count - |
| 4695 | dup_files); |
| 4696 | else |
| 4697 | printf("No duplicate files removed\n"); |
| 4698 | printf("Number of inodes %d\n", inode_count); |
| 4699 | printf("Number of files %d\n", file_count); |
| 4700 | if(!no_fragments) |
| 4701 | printf("Number of fragments %d\n", fragments); |
| 4702 | printf("Number of symbolic links %d\n", sym_count); |
| 4703 | printf("Number of device nodes %d\n", dev_count); |
| 4704 | printf("Number of fifo nodes %d\n", fifo_count); |
| 4705 | printf("Number of socket nodes %d\n", sock_count); |
| 4706 | printf("Number of directories %d\n", dir_count); |
| 4707 | printf("Number of ids (unique uids + gids) %d\n", id_count); |
| 4708 | printf("Number of uids %d\n", uid_count); |
| 4709 | |
| 4710 | for(i = 0; i < id_count; i++) { |
| 4711 | if(id_table[i]->flags & ISA_UID) { |
| 4712 | struct passwd *user = getpwuid(id_table[i]->id); |
| 4713 | printf("\t%s (%d)\n", user == NULL ? "unknown" : |
| 4714 | user->pw_name, id_table[i]->id); |
| 4715 | } |
| 4716 | } |
| 4717 | |
| 4718 | printf("Number of gids %d\n", guid_count); |
| 4719 | |
| 4720 | for(i = 0; i < id_count; i++) { |
| 4721 | if(id_table[i]->flags & ISA_GID) { |
| 4722 | struct group *group = getgrgid(id_table[i]->id); |
| 4723 | printf("\t%s (%d)\n", group == NULL ? "unknown" : |
| 4724 | group->gr_name, id_table[i]->id); |
| 4725 | } |
| 4726 | } |
| 4727 | } |
| 4728 | |
| 4729 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4730 | int parse_num(char *arg, int *res) |
| 4731 | { |
| 4732 | return parse_number(arg, res, 0); |
| 4733 | } |
| 4734 | |
| 4735 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4736 | #define VERSION() \ |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4737 | printf("mksquashfs version 4.2-git (2014/01/25)\n");\ |
Phillip Lougher | cc06495 | 2014-01-03 04:26:34 +0000 | [diff] [blame] | 4738 | printf("copyright (C) 2014 Phillip Lougher "\ |
Phillip Lougher | 83d42a3 | 2012-10-31 23:42:22 +0000 | [diff] [blame] | 4739 | "<phillip@squashfs.org.uk>\n\n"); \ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4740 | printf("This program is free software; you can redistribute it and/or"\ |
| 4741 | "\n");\ |
| 4742 | printf("modify it under the terms of the GNU General Public License"\ |
| 4743 | "\n");\ |
| 4744 | printf("as published by the Free Software Foundation; either version "\ |
| 4745 | "2,\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4746 | printf("or (at your option) any later version.\n\n");\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4747 | printf("This program is distributed in the hope that it will be "\ |
| 4748 | "useful,\n");\ |
| 4749 | printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\ |
| 4750 | "of\n");\ |
| 4751 | printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\ |
| 4752 | "\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4753 | printf("GNU General Public License for more details.\n"); |
| 4754 | int main(int argc, char *argv[]) |
| 4755 | { |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4756 | struct stat buf, source_buf; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 4757 | int res, i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4758 | char *b, *root_name = NULL; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 4759 | int keep_as_directory = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4760 | squashfs_inode inode; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4761 | int readb_mbytes = READER_BUFFER_DEFAULT, |
| 4762 | writeb_mbytes = WRITER_BUFFER_DEFAULT, |
| 4763 | fragmentb_mbytes = FRAGMENT_BUFFER_DEFAULT; |
Phillip Lougher | 569a963 | 2013-04-22 03:30:30 +0100 | [diff] [blame] | 4764 | int progress = TRUE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 4765 | int force_progress = FALSE; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 4766 | struct file_buffer **fragment = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4767 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4768 | block_log = slog(block_size); |
| 4769 | if(argc > 1 && strcmp(argv[1], "-version") == 0) { |
| 4770 | VERSION(); |
| 4771 | exit(0); |
| 4772 | } |
| 4773 | for(i = 1; i < argc && argv[i][0] != '-'; i++); |
| 4774 | if(i < 3) |
| 4775 | goto printOptions; |
| 4776 | source_path = argv + 1; |
| 4777 | source = i - 2; |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4778 | |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 4779 | /* |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4780 | * Scan the command line for -comp xxx option, this is to ensure |
| 4781 | * any -X compressor specific options are passed to the |
| 4782 | * correct compressor |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 4783 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4784 | for(; i < argc; i++) { |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4785 | struct compressor *prev_comp = comp; |
| 4786 | |
| 4787 | if(strcmp(argv[i], "-comp") == 0) { |
| 4788 | if(++i == argc) { |
| 4789 | ERROR("%s: -comp missing compression type\n", |
| 4790 | argv[0]); |
| 4791 | exit(1); |
| 4792 | } |
| 4793 | comp = lookup_compressor(argv[i]); |
| 4794 | if(!comp->supported) { |
| 4795 | ERROR("%s: Compressor \"%s\" is not supported!" |
| 4796 | "\n", argv[0], argv[i]); |
| 4797 | ERROR("%s: Compressors available:\n", argv[0]); |
| 4798 | display_compressors("", COMP_DEFAULT); |
| 4799 | exit(1); |
| 4800 | } |
| 4801 | if(prev_comp != NULL && prev_comp != comp) { |
| 4802 | ERROR("%s: -comp multiple conflicting -comp" |
| 4803 | " options specified on command line" |
| 4804 | ", previously %s, now %s\n", argv[0], |
| 4805 | prev_comp->name, comp->name); |
| 4806 | exit(1); |
| 4807 | } |
| 4808 | compressor_opt_parsed = 1; |
| 4809 | |
| 4810 | } else if(strcmp(argv[i], "-e") == 0) |
| 4811 | break; |
| 4812 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
| 4813 | strcmp(argv[i], "-ef") == 0 || |
| 4814 | strcmp(argv[i], "-pf") == 0 || |
| 4815 | strcmp(argv[i], "-af") == 0 || |
| 4816 | strcmp(argv[i], "-comp") == 0) |
| 4817 | i++; |
| 4818 | } |
| 4819 | |
| 4820 | /* |
| 4821 | * if no -comp option specified lookup default compressor. Note the |
| 4822 | * Makefile ensures the default compressor has been built, and so we |
| 4823 | * don't need to to check for failure here |
| 4824 | */ |
| 4825 | if(comp == NULL) |
| 4826 | comp = lookup_compressor(COMP_DEFAULT); |
| 4827 | |
| 4828 | for(i = source + 2; i < argc; i++) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 4829 | if(strcmp(argv[i], "-action") == 0 || |
| 4830 | strcmp(argv[i], "-a") ==0) { |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 4831 | if(++i == argc) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 4832 | ERROR("%s: %s missing action\n", |
| 4833 | argv[0], argv[i - 1]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 4834 | exit(1); |
| 4835 | } |
| 4836 | res = parse_action(argv[i]); |
| 4837 | if(res == 0) |
| 4838 | exit(1); |
| 4839 | |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 4840 | } else if(strcmp(argv[i], "-af") == 0) { |
| 4841 | if(++i == argc) { |
| 4842 | ERROR("%s: -af missing filename\n", argv[0]); |
| 4843 | exit(1); |
| 4844 | } |
| 4845 | if(read_action_file(argv[i]) == FALSE) |
| 4846 | exit(1); |
| 4847 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4848 | } else if(strcmp(argv[i], "-comp") == 0) |
| 4849 | /* parsed previously */ |
| 4850 | i++; |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 4851 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4852 | else if(strncmp(argv[i], "-X", 2) == 0) { |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 4853 | int args; |
| 4854 | |
| 4855 | if(strcmp(argv[i] + 2, "help") == 0) |
| 4856 | goto print_compressor_options; |
| 4857 | |
| 4858 | args = compressor_options(comp, argv + i, argc - i); |
plougher | d886567 | 2010-11-27 05:05:49 +0000 | [diff] [blame] | 4859 | if(args < 0) { |
| 4860 | if(args == -1) { |
| 4861 | ERROR("%s: Unrecognised compressor" |
| 4862 | " option %s\n", argv[0], |
| 4863 | argv[i]); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4864 | if(!compressor_opt_parsed) |
| 4865 | ERROR("%s: Did you forget to" |
| 4866 | " specify -comp?\n", |
| 4867 | argv[0]); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 4868 | print_compressor_options: |
Phillip Lougher | b1c3b6a | 2014-01-06 05:30:00 +0000 | [diff] [blame] | 4869 | ERROR("%s: selected compressor \"%s\"" |
| 4870 | ". Options supported: %s\n", |
| 4871 | argv[0], comp->name, |
| 4872 | comp->usage ? "" : "none"); |
| 4873 | if(comp->usage) |
| 4874 | comp->usage(); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4875 | } |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 4876 | exit(1); |
| 4877 | } |
| 4878 | i += args; |
| 4879 | |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 4880 | } else if(strcmp(argv[i], "-pf") == 0) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4881 | if(++i == argc) { |
| 4882 | ERROR("%s: -pf missing filename\n", argv[0]); |
| 4883 | exit(1); |
| 4884 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 4885 | if(read_pseudo_file(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4886 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4887 | } else if(strcmp(argv[i], "-p") == 0) { |
| 4888 | if(++i == argc) { |
| 4889 | ERROR("%s: -p missing pseudo file definition\n", |
| 4890 | argv[0]); |
| 4891 | exit(1); |
| 4892 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 4893 | if(read_pseudo_def(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4894 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4895 | } else if(strcmp(argv[i], "-recover") == 0) { |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4896 | if(++i == argc) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4897 | ERROR("%s: -recover missing recovery file\n", |
| 4898 | argv[0]); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4899 | exit(1); |
| 4900 | } |
| 4901 | read_recovery_data(argv[i], argv[source + 1]); |
| 4902 | } else if(strcmp(argv[i], "-no-recovery") == 0) |
| 4903 | recover = FALSE; |
| 4904 | else if(strcmp(argv[i], "-wildcards") == 0) { |
plougher | 934a9ed | 2007-10-19 00:21:10 +0000 | [diff] [blame] | 4905 | old_exclude = FALSE; |
| 4906 | use_regex = FALSE; |
| 4907 | } else if(strcmp(argv[i], "-regex") == 0) { |
| 4908 | old_exclude = FALSE; |
| 4909 | use_regex = TRUE; |
| 4910 | } else if(strcmp(argv[i], "-no-sparse") == 0) |
plougher | 1f54edc | 2007-08-12 23:13:36 +0000 | [diff] [blame] | 4911 | sparse_files = FALSE; |
| 4912 | else if(strcmp(argv[i], "-no-progress") == 0) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4913 | progress = FALSE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 4914 | else if(strcmp(argv[i], "-progress") == 0) |
| 4915 | force_progress = TRUE; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4916 | else if(strcmp(argv[i], "-no-exports") == 0) |
| 4917 | exportable = FALSE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4918 | else if(strcmp(argv[i], "-processors") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4919 | if((++i == argc) || !parse_num(argv[i], &processors)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4920 | ERROR("%s: -processors missing or invalid " |
| 4921 | "processor number\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4922 | exit(1); |
| 4923 | } |
| 4924 | if(processors < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4925 | ERROR("%s: -processors should be 1 or larger\n", |
| 4926 | argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4927 | exit(1); |
| 4928 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4929 | } else if(strcmp(argv[i], "-read-queue") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4930 | if((++i == argc) || |
| 4931 | !parse_num(argv[i], &readb_mbytes)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4932 | ERROR("%s: -read-queue missing or invalid " |
| 4933 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4934 | exit(1); |
| 4935 | } |
| 4936 | if(readb_mbytes < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4937 | ERROR("%s: -read-queue should be 1 megabyte or " |
| 4938 | "larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4939 | exit(1); |
| 4940 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4941 | } else if(strcmp(argv[i], "-write-queue") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4942 | if((++i == argc) || |
| 4943 | !parse_num(argv[i], &writeb_mbytes)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4944 | ERROR("%s: -write-queue missing or invalid " |
| 4945 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4946 | exit(1); |
| 4947 | } |
| 4948 | if(writeb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4949 | ERROR("%s: -write-queue should be 1 megabyte " |
| 4950 | "or larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4951 | exit(1); |
| 4952 | } |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4953 | } else if(strcmp(argv[i], "-fragment-queue") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4954 | if((++i == argc) || |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4955 | !parse_num(argv[i], |
| 4956 | &fragmentb_mbytes)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4957 | ERROR("%s: -fragment-queue missing or invalid " |
| 4958 | "queue size\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4959 | exit(1); |
| 4960 | } |
| 4961 | if(fragmentb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4962 | ERROR("%s: -fragment-queue should be 1 " |
| 4963 | "megabyte or larger\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4964 | exit(1); |
| 4965 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4966 | } else if(strcmp(argv[i], "-b") == 0) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4967 | if(++i == argc) { |
| 4968 | ERROR("%s: -b missing block size\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4969 | exit(1); |
| 4970 | } |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4971 | if(!parse_number(argv[i], &block_size, 1)) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4972 | ERROR("%s: -b invalid block size\n", argv[0]); |
| 4973 | exit(1); |
| 4974 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4975 | if((block_log = slog(block_size)) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4976 | ERROR("%s: -b block size not power of two or " |
| 4977 | "not between 4096 and 1Mbyte\n", |
| 4978 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4979 | exit(1); |
| 4980 | } |
| 4981 | } else if(strcmp(argv[i], "-ef") == 0) { |
| 4982 | if(++i == argc) { |
| 4983 | ERROR("%s: -ef missing filename\n", argv[0]); |
| 4984 | exit(1); |
| 4985 | } |
plougher | 9b5bf8c | 2006-03-20 18:43:33 +0000 | [diff] [blame] | 4986 | } else if(strcmp(argv[i], "-no-duplicates") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4987 | duplicate_checking = FALSE; |
| 4988 | |
| 4989 | else if(strcmp(argv[i], "-no-fragments") == 0) |
| 4990 | no_fragments = TRUE; |
| 4991 | |
| 4992 | else if(strcmp(argv[i], "-always-use-fragments") == 0) |
| 4993 | always_use_fragments = TRUE; |
| 4994 | |
| 4995 | else if(strcmp(argv[i], "-sort") == 0) { |
| 4996 | if(++i == argc) { |
| 4997 | ERROR("%s: -sort missing filename\n", argv[0]); |
| 4998 | exit(1); |
| 4999 | } |
| 5000 | } else if(strcmp(argv[i], "-all-root") == 0 || |
| 5001 | strcmp(argv[i], "-root-owned") == 0) |
| 5002 | global_uid = global_gid = 0; |
| 5003 | |
| 5004 | else if(strcmp(argv[i], "-force-uid") == 0) { |
| 5005 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5006 | ERROR("%s: -force-uid missing uid or user\n", |
| 5007 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5008 | exit(1); |
| 5009 | } |
| 5010 | if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5011 | if(global_uid < 0 || global_uid > |
| 5012 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5013 | ERROR("%s: -force-uid uid out of range" |
| 5014 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5015 | exit(1); |
| 5016 | } |
| 5017 | } else { |
| 5018 | struct passwd *uid = getpwnam(argv[i]); |
| 5019 | if(uid) |
| 5020 | global_uid = uid->pw_uid; |
| 5021 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5022 | ERROR("%s: -force-uid invalid uid or " |
| 5023 | "unknown user\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5024 | exit(1); |
| 5025 | } |
| 5026 | } |
| 5027 | } else if(strcmp(argv[i], "-force-gid") == 0) { |
| 5028 | if(++i == argc) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5029 | ERROR("%s: -force-gid missing gid or group\n", |
| 5030 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5031 | exit(1); |
| 5032 | } |
| 5033 | if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5034 | if(global_gid < 0 || global_gid > |
| 5035 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5036 | ERROR("%s: -force-gid gid out of range" |
| 5037 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5038 | exit(1); |
| 5039 | } |
| 5040 | } else { |
| 5041 | struct group *gid = getgrnam(argv[i]); |
| 5042 | if(gid) |
| 5043 | global_gid = gid->gr_gid; |
| 5044 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5045 | ERROR("%s: -force-gid invalid gid or " |
| 5046 | "unknown group\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5047 | exit(1); |
| 5048 | } |
| 5049 | } |
| 5050 | } else if(strcmp(argv[i], "-noI") == 0 || |
| 5051 | strcmp(argv[i], "-noInodeCompression") == 0) |
| 5052 | noI = TRUE; |
| 5053 | |
| 5054 | else if(strcmp(argv[i], "-noD") == 0 || |
| 5055 | strcmp(argv[i], "-noDataCompression") == 0) |
| 5056 | noD = TRUE; |
| 5057 | |
| 5058 | else if(strcmp(argv[i], "-noF") == 0 || |
| 5059 | strcmp(argv[i], "-noFragmentCompression") == 0) |
| 5060 | noF = TRUE; |
| 5061 | |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5062 | else if(strcmp(argv[i], "-noX") == 0 || |
| 5063 | strcmp(argv[i], "-noXattrCompression") == 0) |
| 5064 | noX = TRUE; |
| 5065 | |
plougher | ce564c6 | 2010-05-19 03:01:49 +0000 | [diff] [blame] | 5066 | else if(strcmp(argv[i], "-no-xattrs") == 0) |
| 5067 | no_xattrs = TRUE; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 5068 | |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5069 | else if(strcmp(argv[i], "-xattrs") == 0) |
| 5070 | no_xattrs = FALSE; |
| 5071 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5072 | else if(strcmp(argv[i], "-nopad") == 0) |
| 5073 | nopad = TRUE; |
| 5074 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5075 | else if(strcmp(argv[i], "-info") == 0) |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5076 | silent = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5077 | |
| 5078 | else if(strcmp(argv[i], "-e") == 0) |
| 5079 | break; |
| 5080 | |
| 5081 | else if(strcmp(argv[i], "-noappend") == 0) |
| 5082 | delete = TRUE; |
| 5083 | |
| 5084 | else if(strcmp(argv[i], "-keep-as-directory") == 0) |
| 5085 | keep_as_directory = TRUE; |
| 5086 | |
| 5087 | else if(strcmp(argv[i], "-root-becomes") == 0) { |
| 5088 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5089 | ERROR("%s: -root-becomes: missing name\n", |
| 5090 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5091 | exit(1); |
| 5092 | } |
| 5093 | root_name = argv[i]; |
| 5094 | } else if(strcmp(argv[i], "-version") == 0) { |
| 5095 | VERSION(); |
| 5096 | } else { |
| 5097 | ERROR("%s: invalid option\n\n", argv[0]); |
| 5098 | printOptions: |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5099 | ERROR("SYNTAX:%s source1 source2 ... dest [options] " |
| 5100 | "[-e list of exclude\ndirs/files]\n", argv[0]); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5101 | ERROR("\nFilesystem build options:\n"); |
plougher | ff5ea8b | 2009-08-07 19:33:10 +0000 | [diff] [blame] | 5102 | ERROR("-comp <comp>\t\tselect <comp> compression\n"); |
plougher | 13df178 | 2009-08-29 01:05:34 +0000 | [diff] [blame] | 5103 | ERROR("\t\t\tCompressors available:\n"); |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 5104 | display_compressors("\t\t\t", COMP_DEFAULT); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5105 | ERROR("-b <block_size>\t\tset data block to " |
| 5106 | "<block_size>. Default %d bytes\n", |
| 5107 | SQUASHFS_FILE_SIZE); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5108 | ERROR("-no-exports\t\tdon't make the filesystem " |
| 5109 | "exportable via NFS\n"); |
| 5110 | ERROR("-no-sparse\t\tdon't detect sparse files\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5111 | ERROR("-no-xattrs\t\tdon't store extended attributes" |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5112 | NOXOPT_STR "\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5113 | ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5114 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5115 | ERROR("-noI\t\t\tdo not compress inode table\n"); |
| 5116 | ERROR("-noD\t\t\tdo not compress data blocks\n"); |
| 5117 | ERROR("-noF\t\t\tdo not compress fragment blocks\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5118 | ERROR("-noX\t\t\tdo not compress extended " |
| 5119 | "attributes\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5120 | ERROR("-no-fragments\t\tdo not use fragments\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5121 | ERROR("-always-use-fragments\tuse fragment blocks for " |
| 5122 | "files larger than block size\n"); |
| 5123 | ERROR("-no-duplicates\t\tdo not perform duplicate " |
| 5124 | "checking\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5125 | ERROR("-all-root\t\tmake all files owned by root\n"); |
| 5126 | ERROR("-force-uid uid\t\tset all file uids to uid\n"); |
| 5127 | ERROR("-force-gid gid\t\tset all file gids to gid\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5128 | ERROR("-nopad\t\t\tdo not pad filesystem to a multiple " |
| 5129 | "of 4K\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5130 | ERROR("-keep-as-directory\tif one source directory is " |
| 5131 | "specified, create a root\n"); |
| 5132 | ERROR("\t\t\tdirectory containing that directory, " |
| 5133 | "rather than the\n"); |
| 5134 | ERROR("\t\t\tcontents of the directory\n"); |
| 5135 | ERROR("\nFilesystem filter options:\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5136 | ERROR("-p <pseudo-definition>\tAdd pseudo file " |
| 5137 | "definition\n"); |
| 5138 | ERROR("-pf <pseudo-file>\tAdd list of pseudo file " |
| 5139 | "definitions\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5140 | ERROR("-sort <sort_file>\tsort files according to " |
| 5141 | "priorities in <sort_file>. One\n"); |
| 5142 | ERROR("\t\t\tfile or dir with priority per line. " |
| 5143 | "Priority -32768 to\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5144 | ERROR("\t\t\t32767, default priority 0\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5145 | ERROR("-ef <exclude_file>\tlist of exclude dirs/files." |
| 5146 | " One per line\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5147 | ERROR("-wildcards\t\tAllow extended shell wildcards " |
| 5148 | "(globbing) to be used in\n\t\t\texclude " |
| 5149 | "dirs/files\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5150 | ERROR("-regex\t\t\tAllow POSIX regular expressions to " |
| 5151 | "be used in exclude\n\t\t\tdirs/files\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5152 | ERROR("\nFilesystem append options:\n"); |
| 5153 | ERROR("-noappend\t\tdo not append to existing " |
| 5154 | "filesystem\n"); |
| 5155 | ERROR("-root-becomes <name>\twhen appending source " |
| 5156 | "files/directories, make the\n"); |
| 5157 | ERROR("\t\t\toriginal root become a subdirectory in " |
| 5158 | "the new root\n"); |
| 5159 | ERROR("\t\t\tcalled <name>, rather than adding the new " |
| 5160 | "source items\n"); |
| 5161 | ERROR("\t\t\tto the original root\n"); |
| 5162 | ERROR("\nMksquashfs runtime options:\n"); |
| 5163 | ERROR("-version\t\tprint version, licence and " |
| 5164 | "copyright message\n"); |
| 5165 | ERROR("-recover <name>\t\trecover filesystem data " |
| 5166 | "using recovery file <name>\n"); |
| 5167 | ERROR("-no-recovery\t\tdon't generate a recovery " |
| 5168 | "file\n"); |
| 5169 | ERROR("-info\t\t\tprint files written to filesystem\n"); |
| 5170 | ERROR("-no-progress\t\tdon't display the progress " |
| 5171 | "bar\n"); |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5172 | ERROR("-progress\t\tdisplay progress bar when using " |
| 5173 | "the -info option\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5174 | ERROR("-processors <number>\tUse <number> processors." |
| 5175 | " By default will use number of\n"); |
| 5176 | ERROR("\t\t\tprocessors available\n"); |
| 5177 | ERROR("-read-queue <size>\tSet input queue to <size> " |
| 5178 | "Mbytes. Default %d Mbytes\n", |
| 5179 | READER_BUFFER_DEFAULT); |
| 5180 | ERROR("-write-queue <size>\tSet output queue to <size> " |
| 5181 | "Mbytes. Default %d Mbytes\n", |
| 5182 | WRITER_BUFFER_DEFAULT); |
plougher | 8bc376b | 2010-11-12 04:55:20 +0000 | [diff] [blame] | 5183 | ERROR("-fragment-queue <size>\tSet fragment queue to " |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5184 | "<size> Mbytes. Default %d Mbytes\n", |
| 5185 | FRAGMENT_BUFFER_DEFAULT); |
| 5186 | ERROR("\nMiscellaneous options:\n"); |
| 5187 | ERROR("-root-owned\t\talternative name for -all-root" |
| 5188 | "\n"); |
| 5189 | ERROR("-noInodeCompression\talternative name for -noI" |
| 5190 | "\n"); |
| 5191 | ERROR("-noDataCompression\talternative name for -noD" |
| 5192 | "\n"); |
| 5193 | ERROR("-noFragmentCompression\talternative name for " |
| 5194 | "-noF\n"); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5195 | ERROR("-noXattrCompression\talternative name for " |
| 5196 | "-noX\n"); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 5197 | ERROR("\n-Xhelp\t\t\tprint compressor options for" |
| 5198 | " selected compressor\n"); |
plougher | 4fb6682 | 2010-12-08 02:49:28 +0000 | [diff] [blame] | 5199 | ERROR("\nCompressors available and compressor specific " |
| 5200 | "options:\n"); |
| 5201 | display_compressor_usage(COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5202 | exit(1); |
| 5203 | } |
| 5204 | } |
| 5205 | |
plougher | b747f4f | 2010-12-25 04:05:47 +0000 | [diff] [blame] | 5206 | /* |
| 5207 | * Some compressors may need the options to be checked for validity |
| 5208 | * once all the options have been processed |
| 5209 | */ |
| 5210 | res = compressor_options_post(comp, block_size); |
| 5211 | if(res) |
| 5212 | EXIT_MKSQUASHFS(); |
| 5213 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5214 | /* |
| 5215 | * If the -info option has been selected then disable the |
| 5216 | * progress bar unless it has been explicitly enabled with |
| 5217 | * the -progress option |
| 5218 | */ |
| 5219 | if(!silent) |
| 5220 | progress = force_progress; |
| 5221 | |
| 5222 | #ifdef SQUASHFS_TRACE |
Phillip Lougher | 989f5fe | 2013-04-22 03:16:29 +0100 | [diff] [blame] | 5223 | /* |
| 5224 | * Disable progress bar if full debug tracing is enabled. |
| 5225 | * The progress bar in this case just gets in the way of the |
| 5226 | * debug trace output |
| 5227 | */ |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5228 | progress = FALSE; |
| 5229 | #endif |
| 5230 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5231 | for(i = 0; i < source; i++) |
| 5232 | if(lstat(source_path[i], &source_buf) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5233 | fprintf(stderr, "Cannot stat source directory \"%s\" " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5234 | "because %s\n", source_path[i], |
| 5235 | strerror(errno)); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5236 | EXIT_MKSQUASHFS(); |
| 5237 | } |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5238 | |
| 5239 | destination_file = argv[source + 1]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5240 | if(stat(argv[source + 1], &buf) == -1) { |
| 5241 | if(errno == ENOENT) { /* Does not exist */ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5242 | fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, |
plougher | 59dce67 | 2010-05-19 03:56:59 +0000 | [diff] [blame] | 5243 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5244 | if(fd == -1) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5245 | perror("Could not create destination file"); |
| 5246 | exit(1); |
| 5247 | } |
| 5248 | delete = TRUE; |
| 5249 | } else { |
| 5250 | perror("Could not stat destination file"); |
| 5251 | exit(1); |
| 5252 | } |
| 5253 | |
| 5254 | } else { |
| 5255 | if(S_ISBLK(buf.st_mode)) { |
| 5256 | if((fd = open(argv[source + 1], O_RDWR)) == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5257 | perror("Could not open block device as " |
| 5258 | "destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5259 | exit(1); |
| 5260 | } |
| 5261 | block_device = 1; |
| 5262 | |
| 5263 | } else if(S_ISREG(buf.st_mode)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5264 | fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | |
| 5265 | O_RDWR); |
| 5266 | if(fd == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5267 | perror("Could not open regular file for " |
| 5268 | "writing as destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5269 | exit(1); |
| 5270 | } |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 5271 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5272 | else { |
| 5273 | ERROR("Destination not block device or regular file\n"); |
| 5274 | exit(1); |
| 5275 | } |
| 5276 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5277 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5278 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5279 | /* |
| 5280 | * process the exclude files - must be done afer destination file has |
| 5281 | * been possibly created |
| 5282 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5283 | for(i = source + 2; i < argc; i++) |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 5284 | if(strcmp(argv[i], "-ef") == 0) |
| 5285 | /* |
| 5286 | * Note presence of filename arg has already |
| 5287 | * been checked |
| 5288 | */ |
| 5289 | process_exclude_file(argv[++i]); |
| 5290 | else if(strcmp(argv[i], "-e") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5291 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5292 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5293 | strcmp(argv[i], "-sort") == 0 || |
| 5294 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5295 | strcmp(argv[i], "-af") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5296 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5297 | i++; |
| 5298 | |
| 5299 | if(i != argc) { |
| 5300 | if(++i == argc) { |
| 5301 | ERROR("%s: -e missing arguments\n", argv[0]); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5302 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5303 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 5304 | while(i < argc) |
| 5305 | if(old_exclude) |
| 5306 | old_add_exclude(argv[i++]); |
| 5307 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5308 | add_exclude(argv[i++]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | /* process the sort files - must be done afer the exclude files */ |
| 5312 | for(i = source + 2; i < argc; i++) |
| 5313 | if(strcmp(argv[i], "-sort") == 0) { |
plougher | e1c9a74 | 2010-07-21 16:59:05 +0000 | [diff] [blame] | 5314 | int res = read_sort_file(argv[++i], source, |
| 5315 | source_path); |
| 5316 | if(res == FALSE) |
| 5317 | BAD_ERROR("Failed to read sort file\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5318 | sorted ++; |
| 5319 | } else if(strcmp(argv[i], "-e") == 0) |
| 5320 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5321 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5322 | strcmp(argv[i], "-ef") == 0 || |
| 5323 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5324 | strcmp(argv[i], "-af") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5325 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5326 | i++; |
| 5327 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5328 | if(!delete) { |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 5329 | comp = read_super(fd, &sBlk, argv[source + 1]); |
| 5330 | if(comp == NULL) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5331 | ERROR("Failed to read existing filesystem - will not " |
| 5332 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5333 | ERROR("To force Mksquashfs to write to this block " |
| 5334 | "device or file use -noappend\n"); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5335 | EXIT_MKSQUASHFS(); |
| 5336 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5337 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5338 | block_log = slog(block_size = sBlk.block_size); |
| 5339 | noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags); |
| 5340 | noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags); |
| 5341 | noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5342 | noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5343 | no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags); |
| 5344 | always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags); |
| 5345 | duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5346 | exportable = SQUASHFS_EXPORTABLE(sBlk.flags); |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5347 | no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5348 | comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
Phillip Lougher | c97dac5 | 2013-04-14 03:32:10 +0100 | [diff] [blame] | 5351 | initialise_threads(readb_mbytes, writeb_mbytes, fragmentb_mbytes, |
| 5352 | delete); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5353 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 5354 | res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0); |
| 5355 | if(res) |
| 5356 | BAD_ERROR("compressor_init failed\n"); |
| 5357 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5358 | if(delete) { |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5359 | int size; |
Phillip Lougher | a45c9d2 | 2011-02-20 04:24:07 +0000 | [diff] [blame] | 5360 | void *comp_data = compressor_dump_options(comp, block_size, |
| 5361 | &size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5362 | |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 5363 | printf("Creating %d.%d filesystem on %s, block size %d.\n", |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5364 | SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5365 | |
plougher | 871c72a | 2010-12-25 04:17:27 +0000 | [diff] [blame] | 5366 | /* |
| 5367 | * store any compressor specific options after the superblock, |
| 5368 | * and set the COMP_OPT flag to show that the filesystem has |
| 5369 | * compressor specfic options |
| 5370 | */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5371 | if(comp_data) { |
| 5372 | unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT; |
| 5373 | |
| 5374 | SQUASHFS_INSWAP_SHORTS(&c_byte, 1); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5375 | write_destination(fd, sizeof(struct squashfs_super_block), |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 5376 | sizeof(c_byte), &c_byte); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5377 | write_destination(fd, sizeof(struct squashfs_super_block) + |
plougher | 8e4dad4 | 2010-12-28 05:56:22 +0000 | [diff] [blame] | 5378 | sizeof(c_byte), size, comp_data); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5379 | bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte) |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5380 | + size; |
| 5381 | comp_opts = TRUE; |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5382 | } else |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5383 | bytes = sizeof(struct squashfs_super_block); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5384 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5385 | unsigned int last_directory_block, inode_dir_offset, |
| 5386 | inode_dir_file_size, root_inode_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5387 | inode_dir_start_block, uncompressed_data, |
| 5388 | compressed_data, inode_dir_inode_number, |
| 5389 | inode_dir_parent_inode; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5390 | unsigned int root_inode_start = |
| 5391 | SQUASHFS_INODE_BLK(sBlk.root_inode), |
| 5392 | root_inode_offset = |
| 5393 | SQUASHFS_INODE_OFFSET(sBlk.root_inode); |
Phillip Lougher | b1259f7 | 2013-06-06 03:10:15 +0100 | [diff] [blame] | 5394 | sigset_t sigmask; |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5395 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5396 | if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, |
| 5397 | &data_cache, &directory_table, |
| 5398 | &directory_data_cache, &last_directory_block, |
| 5399 | &inode_dir_offset, &inode_dir_file_size, |
| 5400 | &root_inode_size, &inode_dir_start_block, |
| 5401 | &file_count, &sym_count, &dev_count, &dir_count, |
| 5402 | &fifo_count, &sock_count, &total_bytes, |
| 5403 | &total_inode_bytes, &total_directory_bytes, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5404 | &inode_dir_inode_number, |
| 5405 | &inode_dir_parent_inode, add_old_root_entry, |
| 5406 | &fragment_table, &inode_lookup_table)) == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5407 | ERROR("Failed to read existing filesystem - will not " |
| 5408 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5409 | ERROR("To force Mksquashfs to write to this block " |
| 5410 | "device or file use -noappend\n"); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5411 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5412 | } |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5413 | if((fragments = sBlk.fragments)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5414 | fragment_table = realloc((char *) fragment_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5415 | ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 5416 | * sizeof(struct squashfs_fragment_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5417 | if(fragment_table == NULL) |
| 5418 | BAD_ERROR("Out of memory in save filesystem state\n"); |
| 5419 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5420 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5421 | printf("Appending to existing %d.%d filesystem on %s, block " |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5422 | "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5423 | block_size); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5424 | printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, " |
plougher | bb98803 | 2009-08-06 08:46:34 +0000 | [diff] [blame] | 5425 | "-always-use-fragments,\n-exportable and -comp options " |
| 5426 | "ignored\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5427 | printf("\nIf appending is not wanted, please re-run with " |
| 5428 | "-noappend specified!\n\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5429 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5430 | compressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5431 | ~(SQUASHFS_METADATA_SIZE - 1); |
| 5432 | uncompressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5433 | (SQUASHFS_METADATA_SIZE - 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5434 | |
| 5435 | /* save original filesystem state for restoring ... */ |
| 5436 | sfragments = fragments; |
| 5437 | sbytes = bytes; |
| 5438 | sinode_count = sBlk.inodes; |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 5439 | scache_bytes = root_inode_offset + root_inode_size; |
| 5440 | sdirectory_cache_bytes = uncompressed_data; |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5441 | sdata_cache = malloc(scache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5442 | if(sdata_cache == NULL) |
| 5443 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5444 | sdirectory_data_cache = malloc(sdirectory_cache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5445 | if(sdirectory_data_cache == NULL) |
| 5446 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5447 | memcpy(sdata_cache, data_cache, scache_bytes); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5448 | memcpy(sdirectory_data_cache, directory_data_cache + |
| 5449 | compressed_data, sdirectory_cache_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5450 | sinode_bytes = root_inode_start; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5451 | stotal_bytes = total_bytes; |
| 5452 | stotal_inode_bytes = total_inode_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5453 | stotal_directory_bytes = total_directory_bytes + |
| 5454 | compressed_data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5455 | sfile_count = file_count; |
| 5456 | ssym_count = sym_count; |
| 5457 | sdev_count = dev_count; |
| 5458 | sdir_count = dir_count + 1; |
| 5459 | sfifo_count = fifo_count; |
| 5460 | ssock_count = sock_count; |
| 5461 | sdup_files = dup_files; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5462 | sid_count = id_count; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5463 | write_recovery_data(&sBlk); |
Phillip Lougher | 3bcf67d | 2013-02-24 10:56:38 +0000 | [diff] [blame] | 5464 | save_xattrs(); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 5465 | restore_thread = init_restore_thread(); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5466 | sigemptyset(&sigmask); |
| 5467 | sigaddset(&sigmask, SIGINT); |
| 5468 | sigaddset(&sigmask, SIGTERM); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 5469 | sigaddset(&sigmask, SIGUSR1); |
Phillip Lougher | b1259f7 | 2013-06-06 03:10:15 +0100 | [diff] [blame] | 5470 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5471 | BAD_ERROR("Failed to set signal mask\n"); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 5472 | write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5473 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5474 | /* |
| 5475 | * set the filesystem state up to be able to append to the |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5476 | * original filesystem. The filesystem state differs depending |
| 5477 | * on whether we're appending to the original root directory, or |
| 5478 | * if the original root directory becomes a sub-directory |
| 5479 | * (root-becomes specified on command line, here root_name != |
| 5480 | * NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5481 | */ |
| 5482 | inode_bytes = inode_size = root_inode_start; |
| 5483 | directory_size = last_directory_block; |
| 5484 | cache_size = root_inode_offset + root_inode_size; |
| 5485 | directory_cache_size = inode_dir_offset + inode_dir_file_size; |
| 5486 | if(root_name) { |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5487 | sdirectory_bytes = last_directory_block; |
| 5488 | sdirectory_compressed_bytes = 0; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5489 | root_inode_number = inode_dir_parent_inode; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5490 | inode_no = sBlk.inodes + 2; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5491 | directory_bytes = last_directory_block; |
| 5492 | directory_cache_bytes = uncompressed_data; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5493 | memmove(directory_data_cache, directory_data_cache + |
| 5494 | compressed_data, uncompressed_data); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5495 | cache_bytes = root_inode_offset + root_inode_size; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5496 | add_old_root_entry(root_name, sBlk.root_inode, |
| 5497 | inode_dir_inode_number, SQUASHFS_DIR_TYPE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5498 | total_directory_bytes += compressed_data; |
| 5499 | dir_count ++; |
| 5500 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5501 | sdirectory_compressed_bytes = last_directory_block - |
| 5502 | inode_dir_start_block; |
| 5503 | sdirectory_compressed = |
| 5504 | malloc(sdirectory_compressed_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5505 | if(sdirectory_compressed == NULL) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5506 | BAD_ERROR("Out of memory in save filesystem " |
| 5507 | "state\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5508 | memcpy(sdirectory_compressed, directory_table + |
| 5509 | inode_dir_start_block, |
| 5510 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5511 | sdirectory_bytes = inode_dir_start_block; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5512 | root_inode_number = inode_dir_inode_number; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5513 | inode_no = sBlk.inodes + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5514 | directory_bytes = inode_dir_start_block; |
| 5515 | directory_cache_bytes = inode_dir_offset; |
| 5516 | cache_bytes = root_inode_offset; |
| 5517 | } |
| 5518 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5519 | inode_count = file_count + dir_count + sym_count + dev_count + |
| 5520 | fifo_count + sock_count; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 5523 | if(path) |
| 5524 | paths = add_subdir(paths, path); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5525 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5526 | dump_actions(); |
| 5527 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5528 | if(delete && !keep_as_directory && source == 1 && |
| 5529 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5530 | dir_scan(&inode, source_path[0], scan1_readdir, progress); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5531 | else if(!keep_as_directory && source == 1 && |
| 5532 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5533 | dir_scan(&inode, source_path[0], scan1_single_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5534 | else |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5535 | dir_scan(&inode, "", scan1_encomp_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5536 | sBlk.root_inode = inode; |
| 5537 | sBlk.inodes = inode_count; |
| 5538 | sBlk.s_magic = SQUASHFS_MAGIC; |
| 5539 | sBlk.s_major = SQUASHFS_MAJOR; |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5540 | sBlk.s_minor = SQUASHFS_MINOR; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5541 | sBlk.block_size = block_size; |
| 5542 | sBlk.block_log = block_log; |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5543 | sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments, |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5544 | always_use_fragments, duplicate_checking, exportable, |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5545 | no_xattrs, comp_opts); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5546 | sBlk.mkfs_time = time(NULL); |
| 5547 | |
Phillip Lougher | 83b8f86 | 2013-04-09 03:28:55 +0100 | [diff] [blame] | 5548 | disable_info(); |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 5549 | |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5550 | while((fragment = get_frag_action(fragment))) |
| 5551 | write_fragment(*fragment); |
| 5552 | unlock_fragments(); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 5553 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5554 | pthread_mutex_lock(&fragment_mutex); |
| 5555 | while(fragments_outstanding) { |
| 5556 | pthread_mutex_unlock(&fragment_mutex); |
| 5557 | sched_yield(); |
| 5558 | pthread_mutex_lock(&fragment_mutex); |
| 5559 | } |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 5560 | pthread_cleanup_pop(1); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5561 | |
| 5562 | queue_put(to_writer, NULL); |
| 5563 | if(queue_get(from_writer) != 0) |
| 5564 | EXIT_MKSQUASHFS(); |
| 5565 | |
Phillip Lougher | cd1a5a6 | 2014-01-21 04:53:46 +0000 | [diff] [blame] | 5566 | set_progressbar_state(FALSE); |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 5567 | write_filesystem_tables(&sBlk, nopad); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5568 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5569 | return 0; |
| 5570 | } |