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, |
| 6 | * 2012 |
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 |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 28 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 29 | #include <pwd.h> |
| 30 | #include <grp.h> |
| 31 | #include <time.h> |
| 32 | #include <unistd.h> |
| 33 | #include <stdio.h> |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 34 | #include <stddef.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <errno.h> |
| 39 | #include <dirent.h> |
| 40 | #include <string.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <signal.h> |
| 43 | #include <setjmp.h> |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 44 | #include <sys/types.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 45 | #include <sys/mman.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 46 | #include <pthread.h> |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 47 | #include <regex.h> |
| 48 | #include <fnmatch.h> |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 49 | #include <sys/wait.h> |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 50 | #include <limits.h> |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame^] | 51 | #include <ctype.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 52 | |
plougher | 6885329 | 2005-12-27 02:47:04 +0000 | [diff] [blame] | 53 | #ifndef linux |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 54 | #define __BYTE_ORDER BYTE_ORDER |
| 55 | #define __BIG_ENDIAN BIG_ENDIAN |
| 56 | #define __LITTLE_ENDIAN LITTLE_ENDIAN |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 57 | #include <sys/sysctl.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 58 | #else |
| 59 | #include <endian.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 60 | #include <sys/sysinfo.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 63 | #include "squashfs_fs.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 64 | #include "squashfs_swap.h" |
| 65 | #include "mksquashfs.h" |
| 66 | #include "sort.h" |
| 67 | #include "pseudo.h" |
| 68 | #include "compressor.h" |
| 69 | #include "xattr.h" |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 70 | #include "action.h" |
Phillip Lougher | 2477d0d | 2012-10-24 21:49:28 +0100 | [diff] [blame] | 71 | #include "error.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 72 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 73 | int delete = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 74 | int fd; |
| 75 | |
| 76 | /* filesystem flags for building */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 77 | int comp_opts = FALSE; |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 78 | int no_xattrs = XATTR_DEF, noX = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 79 | int duplicate_checking = 1, noF = 0, no_fragments = 0, always_use_fragments = 0; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 80 | int noI = 0, noD = 0; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 81 | int silent = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 82 | long long global_uid = -1, global_gid = -1; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 83 | int exportable = TRUE; |
| 84 | int progress = TRUE; |
plougher | 8dcc699 | 2007-08-17 19:32:52 +0000 | [diff] [blame] | 85 | int sparse_files = TRUE; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 86 | int old_exclude = TRUE; |
| 87 | int use_regex = FALSE; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 88 | int first_freelist = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 89 | |
| 90 | /* superblock attributes */ |
| 91 | int block_size = SQUASHFS_FILE_SIZE, block_log; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 92 | unsigned int id_count = 0; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 93 | int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0, |
| 94 | sock_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 95 | |
| 96 | /* write position within data section */ |
| 97 | long long bytes = 0, total_bytes = 0; |
| 98 | |
| 99 | /* in memory directory table - possibly compressed */ |
| 100 | char *directory_table = NULL; |
| 101 | unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0; |
| 102 | |
| 103 | /* cached directory table */ |
| 104 | char *directory_data_cache = NULL; |
| 105 | unsigned int directory_cache_bytes = 0, directory_cache_size = 0; |
| 106 | |
| 107 | /* in memory inode table - possibly compressed */ |
| 108 | char *inode_table = NULL; |
| 109 | unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0; |
| 110 | |
| 111 | /* cached inode table */ |
| 112 | char *data_cache = NULL; |
| 113 | unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0; |
| 114 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 115 | /* inode lookup table */ |
| 116 | squashfs_inode *inode_lookup_table = NULL; |
| 117 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 118 | /* in memory directory data */ |
| 119 | #define I_COUNT_SIZE 128 |
| 120 | #define DIR_ENTRIES 32 |
| 121 | #define INODE_HASH_SIZE 65536 |
| 122 | #define INODE_HASH_MASK (INODE_HASH_SIZE - 1) |
| 123 | #define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK) |
| 124 | |
| 125 | struct cached_dir_index { |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 126 | struct squashfs_dir_index index; |
| 127 | char *name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | struct directory { |
| 131 | unsigned int start_block; |
| 132 | unsigned int size; |
| 133 | unsigned char *buff; |
| 134 | unsigned char *p; |
| 135 | unsigned int entry_count; |
| 136 | unsigned char *entry_count_p; |
| 137 | unsigned int i_count; |
| 138 | unsigned int i_size; |
| 139 | struct cached_dir_index *index; |
| 140 | unsigned char *index_count_p; |
| 141 | unsigned int inode_number; |
| 142 | }; |
| 143 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 144 | struct inode_info *inode_info[INODE_HASH_SIZE]; |
| 145 | |
| 146 | /* hash tables used to do fast duplicate searches in duplicate check */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 147 | struct file_info *dupl[65536]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 148 | int dup_files = 0; |
| 149 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 150 | /* exclude file handling */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 151 | /* list of exclude dirs/files */ |
| 152 | struct exclude_info { |
| 153 | dev_t st_dev; |
| 154 | ino_t st_ino; |
| 155 | }; |
| 156 | |
| 157 | #define EXCLUDE_SIZE 8192 |
| 158 | int exclude = 0; |
| 159 | struct exclude_info *exclude_paths = NULL; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 160 | int old_excluded(char *filename, struct stat *buf); |
| 161 | |
| 162 | struct path_entry { |
| 163 | char *name; |
| 164 | regex_t *preg; |
| 165 | struct pathname *paths; |
| 166 | }; |
| 167 | |
| 168 | struct pathname { |
| 169 | int names; |
| 170 | struct path_entry *name; |
| 171 | }; |
| 172 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 173 | struct pathnames { |
| 174 | int count; |
| 175 | struct pathname *path[0]; |
| 176 | }; |
| 177 | #define PATHS_ALLOC_SIZE 10 |
| 178 | |
| 179 | struct pathnames *paths = NULL; |
| 180 | struct pathname *path = NULL; |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 181 | struct pathname *stickypath = NULL; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 182 | int excluded(struct pathnames *paths, char *name, struct pathnames **new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 183 | |
| 184 | /* fragment block data structures */ |
| 185 | int fragments = 0; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 186 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 187 | struct fragment { |
| 188 | unsigned int index; |
| 189 | int offset; |
| 190 | int size; |
| 191 | }; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 192 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 193 | #define FRAG_SIZE 32768 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 194 | #define FRAG_INDEX (1LL << 32) |
| 195 | |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 196 | struct squashfs_fragment_entry *fragment_table = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 197 | int fragments_outstanding = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 198 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 199 | /* current inode number for directories and non directories */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 200 | unsigned int inode_no = 1; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 201 | unsigned int root_inode_number = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 202 | |
| 203 | /* list of source dirs/files */ |
| 204 | int source = 0; |
| 205 | char **source_path; |
| 206 | |
| 207 | /* list of root directory entries read from original filesystem */ |
| 208 | int old_root_entries = 0; |
| 209 | struct old_root_entry_info { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 210 | char *name; |
| 211 | struct inode_info inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 212 | }; |
| 213 | struct old_root_entry_info *old_root_entry; |
| 214 | |
| 215 | /* in memory file info */ |
| 216 | struct file_info { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 217 | long long file_size; |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 218 | long long bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 219 | unsigned short checksum; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 220 | unsigned short fragment_checksum; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 221 | long long start; |
| 222 | unsigned int *block_list; |
| 223 | struct file_info *next; |
| 224 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 225 | char checksum_flag; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | /* count of how many times SIGINT or SIGQUIT has been sent */ |
| 229 | int interrupted = 0; |
| 230 | |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 231 | /* flag if we're restoring existing filesystem */ |
| 232 | int restoring = 0; |
| 233 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 234 | /* restore orignal filesystem state if appending to existing filesystem is |
| 235 | * cancelled */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 236 | jmp_buf env; |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 237 | char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 238 | |
| 239 | long long sbytes, stotal_bytes; |
| 240 | |
| 241 | unsigned int sinode_bytes, scache_bytes, sdirectory_bytes, |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 242 | sdirectory_cache_bytes, sdirectory_compressed_bytes, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 243 | stotal_inode_bytes, stotal_directory_bytes, |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 244 | sinode_count = 0, sfile_count, ssym_count, sdev_count, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 245 | sdir_count, sfifo_count, ssock_count, sdup_files; |
| 246 | int sfragments; |
| 247 | int restore = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 248 | int threads; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 249 | |
| 250 | /* flag whether destination file is a block device */ |
| 251 | int block_device = 0; |
| 252 | |
| 253 | /* flag indicating whether files are sorted using sort list(s) */ |
| 254 | int sorted = 0; |
| 255 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 256 | /* save destination file name for deleting on error */ |
| 257 | char *destination_file = NULL; |
| 258 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 259 | /* recovery file for abnormal exit on appending */ |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 260 | char *recovery_file = NULL; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 261 | int recover = TRUE; |
| 262 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 263 | /* struct describing a cache entry passed between threads */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 264 | struct file_buffer { |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 265 | struct cache *cache; |
Phillip Lougher | fe30cc6 | 2011-08-29 01:48:08 +0100 | [diff] [blame] | 266 | struct file_buffer *hash_next; |
| 267 | struct file_buffer *hash_prev; |
| 268 | struct file_buffer *free_next; |
| 269 | struct file_buffer *free_prev; |
| 270 | struct file_buffer *next; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 271 | long long file_size; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 272 | long long index; |
| 273 | long long block; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 274 | long long sequence; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 275 | int size; |
| 276 | int c_byte; |
Phillip Lougher | fe30cc6 | 2011-08-29 01:48:08 +0100 | [diff] [blame] | 277 | char keep; |
| 278 | char used; |
| 279 | char fragment; |
| 280 | char error; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 281 | char noD; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 282 | char data[0]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 285 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 286 | /* struct describing queues used to pass data between threads */ |
| 287 | struct queue { |
| 288 | int size; |
| 289 | int readp; |
| 290 | int writep; |
| 291 | pthread_mutex_t mutex; |
| 292 | pthread_cond_t empty; |
| 293 | pthread_cond_t full; |
| 294 | void **data; |
| 295 | }; |
| 296 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 297 | |
| 298 | /* in memory uid tables */ |
| 299 | #define ID_ENTRIES 256 |
| 300 | #define ID_HASH(id) (id & (ID_ENTRIES - 1)) |
| 301 | #define ISA_UID 1 |
| 302 | #define ISA_GID 2 |
| 303 | struct id { |
| 304 | unsigned int id; |
| 305 | int index; |
| 306 | char flags; |
| 307 | struct id *next; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 308 | }; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 309 | struct id *id_hash_table[ID_ENTRIES]; |
| 310 | struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS]; |
| 311 | unsigned int uid_count = 0, guid_count = 0; |
| 312 | unsigned int sid_count = 0, suid_count = 0, sguid_count = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 313 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 314 | struct cache *reader_buffer, *writer_buffer, *fragment_buffer; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 315 | struct queue *to_reader, *from_reader, *to_writer, *from_writer, *from_deflate, |
| 316 | *to_frag; |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 317 | pthread_t *thread, *deflator_thread, *frag_deflator_thread; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 318 | pthread_mutex_t fragment_mutex; |
| 319 | pthread_cond_t fragment_waiting; |
| 320 | pthread_mutex_t pos_mutex; |
| 321 | |
| 322 | /* user options that control parallelisation */ |
| 323 | int processors = -1; |
| 324 | /* default size of output buffer in Mbytes */ |
| 325 | #define WRITER_BUFFER_DEFAULT 512 |
| 326 | /* default size of input buffer in Mbytes */ |
| 327 | #define READER_BUFFER_DEFAULT 64 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 328 | /* default size of fragment buffer in Mbytes */ |
| 329 | #define FRAGMENT_BUFFER_DEFAULT 64 |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 330 | int writer_buffer_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 331 | |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 332 | /* compression operations */ |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 333 | static struct compressor *comp; |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 334 | int compressor_opts_parsed = 0; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 335 | void *stream = NULL; |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 336 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 337 | /* xattr stats */ |
| 338 | unsigned int xattr_bytes = 0, total_xattr_bytes = 0; |
| 339 | |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 340 | char *read_from_disk(long long start, unsigned int avail_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 341 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 342 | int type); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 343 | extern struct compressor *read_super(int fd, struct squashfs_super_block *sBlk, |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 344 | char *source); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 345 | extern long long read_filesystem(char *root_name, int fd, |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 346 | struct squashfs_super_block *sBlk, char **cinode_table, char **data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 347 | char **cdirectory_table, char **directory_data_cache, |
| 348 | unsigned int *last_directory_block, unsigned int *inode_dir_offset, |
| 349 | unsigned int *inode_dir_file_size, unsigned int *root_inode_size, |
| 350 | unsigned int *inode_dir_start_block, int *file_count, int *sym_count, |
| 351 | int *dev_count, int *dir_count, int *fifo_count, int *sock_count, |
| 352 | long long *uncompressed_file, unsigned int *uncompressed_inode, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 353 | unsigned int *uncompressed_directory, |
| 354 | unsigned int *inode_dir_inode_number, |
| 355 | unsigned int *inode_dir_parent_inode, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 356 | void (push_directory_entry)(char *, squashfs_inode, int, int), |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 357 | struct squashfs_fragment_entry **fragment_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 358 | squashfs_inode **inode_lookup_table); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 359 | extern int read_sort_file(char *filename, int source, char *source_path[]); |
| 360 | extern void sort_files_and_write(struct dir_info *dir); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 361 | struct file_info *duplicate(long long file_size, long long bytes, |
| 362 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 363 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 364 | unsigned short fragment_checksum, int checksum_flag); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 365 | struct dir_info *dir_scan1(char *, char *, struct pathnames *, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 366 | struct dir_ent *(_readdir)(struct dir_info *), int); |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 367 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 368 | void dir_scan3(struct dir_info *root, struct dir_info *dir); |
| 369 | void dir_scan4(struct dir_info *dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 370 | void dir_scan5(struct dir_info *dir); |
| 371 | void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 372 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 373 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 374 | unsigned short checksum, unsigned short fragment_checksum, |
| 375 | int checksum_flag); |
plougher | 1d1c6bb | 2010-07-21 03:03:13 +0000 | [diff] [blame] | 376 | extern int generate_file_priorities(struct dir_info *dir, int priority, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 377 | struct stat *buf); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 378 | extern struct priority_entry *priority_list[65536]; |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 379 | long long generic_write_table(int, void *, int, void *, int); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 380 | void restorefs(); |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 381 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 382 | |
| 383 | |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 384 | void prep_exit_mksquashfs() |
| 385 | { |
| 386 | if(restore) |
| 387 | restorefs(); |
| 388 | if(delete && destination_file && !block_device) |
| 389 | unlink(destination_file); |
| 390 | exit(1); |
| 391 | } |
| 392 | |
| 393 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 394 | struct queue *queue_init(int size) |
| 395 | { |
| 396 | struct queue *queue = malloc(sizeof(struct queue)); |
| 397 | |
| 398 | if(queue == NULL) |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 399 | goto failed; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 400 | |
plougher | 0d55d9e | 2010-12-16 04:50:21 +0000 | [diff] [blame] | 401 | queue->data = malloc(sizeof(void *) * (size + 1)); |
| 402 | if(queue->data == NULL) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 403 | free(queue); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 404 | goto failed; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | queue->size = size + 1; |
| 408 | queue->readp = queue->writep = 0; |
| 409 | pthread_mutex_init(&queue->mutex, NULL); |
| 410 | pthread_cond_init(&queue->empty, NULL); |
| 411 | pthread_cond_init(&queue->full, NULL); |
| 412 | |
| 413 | return queue; |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 414 | |
| 415 | failed: |
| 416 | BAD_ERROR("Out of memory in queue_init\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
| 420 | void queue_put(struct queue *queue, void *data) |
| 421 | { |
| 422 | int nextp; |
| 423 | |
| 424 | pthread_mutex_lock(&queue->mutex); |
| 425 | |
| 426 | while((nextp = (queue->writep + 1) % queue->size) == queue->readp) |
| 427 | pthread_cond_wait(&queue->full, &queue->mutex); |
| 428 | |
| 429 | queue->data[queue->writep] = data; |
| 430 | queue->writep = nextp; |
| 431 | pthread_cond_signal(&queue->empty); |
| 432 | pthread_mutex_unlock(&queue->mutex); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | void *queue_get(struct queue *queue) |
| 437 | { |
| 438 | void *data; |
| 439 | pthread_mutex_lock(&queue->mutex); |
| 440 | |
| 441 | while(queue->readp == queue->writep) |
| 442 | pthread_cond_wait(&queue->empty, &queue->mutex); |
| 443 | |
| 444 | data = queue->data[queue->readp]; |
| 445 | queue->readp = (queue->readp + 1) % queue->size; |
| 446 | pthread_cond_signal(&queue->full); |
| 447 | pthread_mutex_unlock(&queue->mutex); |
| 448 | |
| 449 | return data; |
| 450 | } |
| 451 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 452 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 453 | /* Cache status struct. Caches are used to keep |
| 454 | track of memory buffers passed between different threads */ |
| 455 | struct cache { |
| 456 | int max_buffers; |
| 457 | int count; |
| 458 | int buffer_size; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 459 | pthread_mutex_t mutex; |
| 460 | pthread_cond_t wait_for_free; |
| 461 | struct file_buffer *free_list; |
| 462 | struct file_buffer *hash_table[65536]; |
| 463 | }; |
| 464 | |
| 465 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 466 | #define INSERT_LIST(NAME, TYPE) \ |
| 467 | void insert_##NAME##_list(TYPE **list, TYPE *entry) { \ |
| 468 | if(*list) { \ |
| 469 | entry->NAME##_next = *list; \ |
| 470 | entry->NAME##_prev = (*list)->NAME##_prev; \ |
| 471 | (*list)->NAME##_prev->NAME##_next = entry; \ |
| 472 | (*list)->NAME##_prev = entry; \ |
| 473 | } else { \ |
| 474 | *list = entry; \ |
| 475 | entry->NAME##_prev = entry->NAME##_next = entry; \ |
| 476 | } \ |
| 477 | } |
| 478 | |
| 479 | |
| 480 | #define REMOVE_LIST(NAME, TYPE) \ |
| 481 | void remove_##NAME##_list(TYPE **list, TYPE *entry) { \ |
| 482 | if(entry->NAME##_prev == entry && entry->NAME##_next == entry) { \ |
| 483 | /* only this entry in the list */ \ |
| 484 | *list = NULL; \ |
| 485 | } else if(entry->NAME##_prev != NULL && entry->NAME##_next != NULL) { \ |
| 486 | /* more than one entry in the list */ \ |
| 487 | entry->NAME##_next->NAME##_prev = entry->NAME##_prev; \ |
| 488 | entry->NAME##_prev->NAME##_next = entry->NAME##_next; \ |
| 489 | if(*list == entry) \ |
| 490 | *list = entry->NAME##_next; \ |
| 491 | } \ |
| 492 | entry->NAME##_prev = entry->NAME##_next = NULL; \ |
| 493 | } |
| 494 | |
| 495 | |
| 496 | #define CALCULATE_HASH(start) (start & 0xffff) \ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 497 | |
| 498 | |
| 499 | /* Called with the cache mutex held */ |
| 500 | void insert_hash_table(struct cache *cache, struct file_buffer *entry) |
| 501 | { |
| 502 | int hash = CALCULATE_HASH(entry->index); |
| 503 | |
| 504 | entry->hash_next = cache->hash_table[hash]; |
| 505 | cache->hash_table[hash] = entry; |
| 506 | entry->hash_prev = NULL; |
| 507 | if(entry->hash_next) |
| 508 | entry->hash_next->hash_prev = entry; |
| 509 | } |
| 510 | |
| 511 | |
| 512 | /* Called with the cache mutex held */ |
| 513 | void remove_hash_table(struct cache *cache, struct file_buffer *entry) |
| 514 | { |
| 515 | if(entry->hash_prev) |
| 516 | entry->hash_prev->hash_next = entry->hash_next; |
| 517 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 518 | cache->hash_table[CALCULATE_HASH(entry->index)] = |
| 519 | entry->hash_next; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 520 | if(entry->hash_next) |
| 521 | entry->hash_next->hash_prev = entry->hash_prev; |
| 522 | |
| 523 | entry->hash_prev = entry->hash_next = NULL; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | /* Called with the cache mutex held */ |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 528 | INSERT_LIST(free, struct file_buffer) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 529 | |
| 530 | /* Called with the cache mutex held */ |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 531 | REMOVE_LIST(free, struct file_buffer) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 532 | |
| 533 | |
| 534 | struct cache *cache_init(int buffer_size, int max_buffers) |
| 535 | { |
| 536 | struct cache *cache = malloc(sizeof(struct cache)); |
| 537 | |
| 538 | if(cache == NULL) |
plougher | 9ca649c | 2010-07-21 00:46:11 +0000 | [diff] [blame] | 539 | BAD_ERROR("Out of memory in cache_init\n"); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 540 | |
| 541 | cache->max_buffers = max_buffers; |
| 542 | cache->buffer_size = buffer_size; |
| 543 | cache->count = 0; |
| 544 | cache->free_list = NULL; |
| 545 | memset(cache->hash_table, 0, sizeof(struct file_buffer *) * 65536); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 546 | pthread_mutex_init(&cache->mutex, NULL); |
| 547 | pthread_cond_init(&cache->wait_for_free, NULL); |
| 548 | |
| 549 | return cache; |
| 550 | } |
| 551 | |
| 552 | |
| 553 | struct file_buffer *cache_lookup(struct cache *cache, long long index) |
| 554 | { |
| 555 | /* Lookup block in the cache, if found return with usage count |
| 556 | * incremented, if not found return NULL */ |
| 557 | int hash = CALCULATE_HASH(index); |
| 558 | struct file_buffer *entry; |
| 559 | |
| 560 | pthread_mutex_lock(&cache->mutex); |
| 561 | |
| 562 | for(entry = cache->hash_table[hash]; entry; entry = entry->hash_next) |
| 563 | if(entry->index == index) |
| 564 | break; |
| 565 | |
| 566 | if(entry) { |
| 567 | /* found the block in the cache, increment used count and |
| 568 | * if necessary remove from free list so it won't disappear |
| 569 | */ |
| 570 | entry->used ++; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 571 | remove_free_list(&cache->free_list, entry); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | pthread_mutex_unlock(&cache->mutex); |
| 575 | |
| 576 | return entry; |
| 577 | } |
| 578 | |
| 579 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 580 | struct file_buffer *cache_get(struct cache *cache, long long index, int keep) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 581 | { |
| 582 | /* Get a free block out of the cache indexed on index. */ |
| 583 | struct file_buffer *entry; |
| 584 | |
| 585 | pthread_mutex_lock(&cache->mutex); |
| 586 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 587 | while(1) { |
| 588 | /* first try to get a block from the free list */ |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 589 | if(first_freelist && cache->free_list) { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 590 | /* a block on the free_list is a "keep" block */ |
| 591 | entry = cache->free_list; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 592 | remove_free_list(&cache->free_list, entry); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 593 | remove_hash_table(cache, entry); |
| 594 | break; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 595 | } else if(cache->count < cache->max_buffers) { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 596 | /* next try to allocate new block */ |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 597 | entry = malloc(sizeof(struct file_buffer) + |
| 598 | cache->buffer_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 599 | if(entry == NULL) |
| 600 | goto failed; |
| 601 | entry->cache = cache; |
| 602 | entry->free_prev = entry->free_next = NULL; |
| 603 | cache->count ++; |
| 604 | break; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 605 | } else if(!first_freelist && cache->free_list) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 606 | /* a block on the free_list is a "keep" block */ |
| 607 | entry = cache->free_list; |
| 608 | remove_free_list(&cache->free_list, entry); |
| 609 | remove_hash_table(cache, entry); |
| 610 | break; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 611 | } else |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 612 | /* wait for a block */ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 613 | pthread_cond_wait(&cache->wait_for_free, &cache->mutex); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 614 | } |
| 615 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 616 | /* initialise block and if a keep block insert into the hash table */ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 617 | entry->used = 1; |
| 618 | entry->error = FALSE; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 619 | entry->keep = keep; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 620 | if(keep) { |
| 621 | entry->index = index; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 622 | insert_hash_table(cache, entry); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 623 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 624 | pthread_mutex_unlock(&cache->mutex); |
| 625 | |
| 626 | return entry; |
| 627 | |
| 628 | failed: |
| 629 | pthread_mutex_unlock(&cache->mutex); |
plougher | cc6896d | 2010-07-21 00:56:25 +0000 | [diff] [blame] | 630 | BAD_ERROR("Out of memory in cache_get\n"); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 631 | } |
| 632 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 633 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 634 | void cache_rehash(struct file_buffer *entry, long long index) |
| 635 | { |
| 636 | struct cache *cache = entry->cache; |
| 637 | |
| 638 | pthread_mutex_lock(&cache->mutex); |
| 639 | if(entry->keep) |
| 640 | remove_hash_table(cache, entry); |
| 641 | entry->keep = TRUE; |
| 642 | entry->index = index; |
| 643 | insert_hash_table(cache, entry); |
| 644 | pthread_mutex_unlock(&cache->mutex); |
| 645 | } |
| 646 | |
| 647 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 648 | void cache_block_put(struct file_buffer *entry) |
| 649 | { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 650 | struct cache *cache; |
| 651 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 652 | /* finished with this cache entry, once the usage count reaches zero it |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 653 | * can be reused and if a keep block put onto the free list. As keep |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 654 | * blocks remain accessible via the hash table they can be found |
| 655 | * getting a new lease of life before they are reused. */ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 656 | |
| 657 | if(entry == NULL) |
| 658 | return; |
| 659 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 660 | cache = entry->cache; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 661 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 662 | pthread_mutex_lock(&cache->mutex); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 663 | |
| 664 | entry->used --; |
| 665 | if(entry->used == 0) { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 666 | if(entry->keep) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 667 | insert_free_list(&cache->free_list, entry); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 668 | else { |
| 669 | free(entry); |
| 670 | cache->count --; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 671 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 672 | |
| 673 | /* One or more threads may be waiting on this block */ |
| 674 | pthread_cond_signal(&cache->wait_for_free); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 675 | } |
| 676 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 677 | pthread_mutex_unlock(&cache->mutex); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 681 | #define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \ |
| 682 | + (((char *)A) - data_cache))) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 683 | |
| 684 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 685 | inline void waitforthread(int i) |
| 686 | { |
| 687 | TRACE("Waiting for thread %d\n", i); |
| 688 | while(thread[i] != 0) |
| 689 | sched_yield(); |
| 690 | } |
| 691 | |
| 692 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 693 | void restorefs() |
| 694 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 695 | int i; |
| 696 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 697 | if(thread == NULL || thread[0] == 0) |
| 698 | return; |
| 699 | |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 700 | if(restoring++) |
| 701 | /* |
plougher | 5b290d2 | 2010-08-12 23:52:21 +0000 | [diff] [blame] | 702 | * Recursive failure when trying to restore filesystem! |
| 703 | * Nothing to do except to exit, otherwise we'll just appear |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 704 | * to hang. The user should be able to restore from the |
plougher | 5b290d2 | 2010-08-12 23:52:21 +0000 | [diff] [blame] | 705 | * recovery file (which is why it was added, in case of |
| 706 | * catastrophic failure in Mksquashfs) |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 707 | */ |
| 708 | exit(1); |
| 709 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 710 | ERROR("Exiting - restoring original filesystem!\n\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 711 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 712 | for(i = 0; i < 2 + processors * 2; i++) |
plougher | 5aa1816 | 2007-12-13 12:15:21 +0000 | [diff] [blame] | 713 | if(thread[i]) |
| 714 | pthread_kill(thread[i], SIGUSR1); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 715 | for(i = 0; i < 2 + processors * 2; i++) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 716 | waitforthread(i); |
| 717 | TRACE("All threads in signal handler\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 718 | bytes = sbytes; |
| 719 | memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 720 | memcpy(directory_data_cache, sdirectory_data_cache, |
| 721 | sdirectory_cache_bytes); |
| 722 | directory_cache_bytes = sdirectory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 723 | inode_bytes = sinode_bytes; |
| 724 | directory_bytes = sdirectory_bytes; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 725 | memcpy(directory_table + directory_bytes, sdirectory_compressed, |
| 726 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 727 | directory_bytes += sdirectory_compressed_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 728 | total_bytes = stotal_bytes; |
| 729 | total_inode_bytes = stotal_inode_bytes; |
| 730 | total_directory_bytes = stotal_directory_bytes; |
| 731 | inode_count = sinode_count; |
| 732 | file_count = sfile_count; |
| 733 | sym_count = ssym_count; |
| 734 | dev_count = sdev_count; |
| 735 | dir_count = sdir_count; |
| 736 | fifo_count = sfifo_count; |
| 737 | sock_count = ssock_count; |
| 738 | dup_files = sdup_files; |
| 739 | fragments = sfragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 740 | id_count = sid_count; |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 741 | restore_xattrs(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 742 | longjmp(env, 1); |
| 743 | } |
| 744 | |
| 745 | |
| 746 | void sighandler() |
| 747 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 748 | if(++interrupted > 2) |
| 749 | return; |
| 750 | if(interrupted == 2) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 751 | restorefs(); |
| 752 | else { |
| 753 | ERROR("Interrupting will restore original filesystem!\n"); |
| 754 | ERROR("Interrupt again to quit\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 759 | void sighandler2() |
| 760 | { |
| 761 | EXIT_MKSQUASHFS(); |
| 762 | } |
| 763 | |
| 764 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 765 | void sigusr1_handler() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 766 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 767 | int i; |
| 768 | sigset_t sigmask; |
| 769 | pthread_t thread_id = pthread_self(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 770 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 771 | for(i = 0; i < (2 + processors * 2) && thread[i] != thread_id; i++); |
| 772 | thread[i] = (pthread_t) 0; |
| 773 | |
plougher | 07966f7 | 2007-11-14 10:54:45 +0000 | [diff] [blame] | 774 | TRACE("Thread %d(%p) in sigusr1_handler\n", i, &thread_id); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 775 | |
| 776 | sigemptyset(&sigmask); |
| 777 | sigaddset(&sigmask, SIGINT); |
| 778 | sigaddset(&sigmask, SIGQUIT); |
| 779 | sigaddset(&sigmask, SIGUSR1); |
| 780 | while(1) { |
| 781 | sigsuspend(&sigmask); |
| 782 | TRACE("After wait in sigusr1_handler :(\n"); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 787 | int mangle2(void *strm, char *d, char *s, int size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 788 | int block_size, int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 789 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 790 | int error, c_byte = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 791 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 792 | if(!uncompressed) { |
plougher | bfb876c | 2010-12-31 07:48:01 +0000 | [diff] [blame] | 793 | c_byte = compressor_compress(comp, strm, d, s, size, block_size, |
| 794 | &error); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 795 | if(c_byte == -1) |
| 796 | BAD_ERROR("mangle2:: %s compress failed with error " |
| 797 | "code %d\n", comp->name, error); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 798 | } |
| 799 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 800 | if(c_byte == 0 || c_byte >= size) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 801 | memcpy(d, s, size); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 802 | return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : |
| 803 | SQUASHFS_COMPRESSED_BIT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 804 | } |
| 805 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 806 | return c_byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 810 | int mangle(char *d, char *s, int size, int block_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 811 | int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 812 | { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 813 | return mangle2(stream, d, s, size, block_size, uncompressed, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 814 | data_block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 818 | void *get_inode(int req_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 819 | { |
| 820 | int data_space; |
| 821 | unsigned short c_byte; |
| 822 | |
| 823 | while(cache_bytes >= SQUASHFS_METADATA_SIZE) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 824 | if((inode_size - inode_bytes) < |
| 825 | ((SQUASHFS_METADATA_SIZE << 1)) + 2) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 826 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 827 | (SQUASHFS_METADATA_SIZE << 1) + 2); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 828 | if(it == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 829 | goto failed; |
| 830 | } |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 831 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 832 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
| 833 | } |
| 834 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 835 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 836 | data_cache, SQUASHFS_METADATA_SIZE, |
| 837 | SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 838 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 839 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 840 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 841 | total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 842 | memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 843 | cache_bytes - SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 844 | cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 845 | } |
| 846 | |
| 847 | data_space = (cache_size - cache_bytes); |
| 848 | if(data_space < req_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 849 | int realloc_size = cache_size == 0 ? |
| 850 | ((req_size + SQUASHFS_METADATA_SIZE) & |
| 851 | ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - |
| 852 | data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 853 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 854 | void *dc = realloc(data_cache, cache_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 855 | realloc_size); |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 856 | if(dc == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 857 | goto failed; |
| 858 | } |
| 859 | cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 860 | data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | cache_bytes += req_size; |
| 864 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 865 | return data_cache + cache_bytes - req_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 866 | |
| 867 | failed: |
| 868 | BAD_ERROR("Out of memory in inode table reallocation!\n"); |
| 869 | } |
| 870 | |
| 871 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 872 | int read_bytes(int fd, void *buff, int bytes) |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 873 | { |
| 874 | int res, count; |
| 875 | |
| 876 | for(count = 0; count < bytes; count += res) { |
| 877 | res = read(fd, buff + count, bytes - count); |
| 878 | if(res < 1) { |
plougher | 96f85a1 | 2009-03-29 22:39:59 +0000 | [diff] [blame] | 879 | if(res == 0) |
| 880 | goto bytes_read; |
| 881 | else if(errno != EINTR) { |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 882 | ERROR("Read failed because %s\n", |
| 883 | strerror(errno)); |
| 884 | return -1; |
| 885 | } else |
| 886 | res = 0; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | bytes_read: |
| 891 | return count; |
| 892 | } |
| 893 | |
| 894 | |
plougher | 3306cb2 | 2010-06-18 04:22:44 +0000 | [diff] [blame] | 895 | int read_fs_bytes(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 896 | { |
| 897 | off_t off = byte; |
| 898 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 899 | TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 900 | byte, bytes); |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 901 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 902 | pthread_mutex_lock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 903 | if(lseek(fd, off, SEEK_SET) == -1) { |
| 904 | ERROR("Lseek on destination failed because %s\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 905 | strerror(errno)); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 906 | goto failed; |
| 907 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 908 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 909 | if(read_bytes(fd, buff, bytes) < bytes) { |
| 910 | ERROR("Read on destination failed\n"); |
| 911 | goto failed; |
| 912 | } |
| 913 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 914 | pthread_mutex_unlock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 915 | return 1; |
| 916 | |
| 917 | failed: |
| 918 | pthread_mutex_unlock(&pos_mutex); |
| 919 | return 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 923 | int write_bytes(int fd, void *buff, int bytes) |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 924 | { |
| 925 | int res, count; |
| 926 | |
| 927 | for(count = 0; count < bytes; count += res) { |
| 928 | res = write(fd, buff + count, bytes - count); |
| 929 | if(res == -1) { |
| 930 | if(errno != EINTR) { |
| 931 | ERROR("Write failed because %s\n", |
| 932 | strerror(errno)); |
| 933 | return -1; |
| 934 | } |
| 935 | res = 0; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 943 | void write_destination(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 944 | { |
| 945 | off_t off = byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 946 | |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 947 | if(!restoring) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 948 | pthread_mutex_lock(&pos_mutex); |
| 949 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 950 | if(lseek(fd, off, SEEK_SET) == -1) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 951 | BAD_ERROR("Lseek on destination failed because %s\n", |
| 952 | strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 953 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 954 | if(write_bytes(fd, buff, bytes) == -1) |
| 955 | BAD_ERROR("Write on destination failed\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 956 | |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 957 | if(!restoring) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 958 | pthread_mutex_unlock(&pos_mutex); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | |
| 962 | long long write_inodes() |
| 963 | { |
| 964 | unsigned short c_byte; |
| 965 | int avail_bytes; |
| 966 | char *datap = data_cache; |
| 967 | long long start_bytes = bytes; |
| 968 | |
| 969 | while(cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 970 | if(inode_size - inode_bytes < |
| 971 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 972 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 973 | ((SQUASHFS_METADATA_SIZE << 1) + 2)); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 974 | if(it == NULL) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 975 | BAD_ERROR("Out of memory in inode table " |
| 976 | "reallocation!\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 977 | } |
| 978 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 979 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 980 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 981 | avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 982 | SQUASHFS_METADATA_SIZE : cache_bytes; |
| 983 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap, |
| 984 | avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 985 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 986 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 987 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 988 | total_inode_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 989 | datap += avail_bytes; |
| 990 | cache_bytes -= avail_bytes; |
| 991 | } |
| 992 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 993 | write_destination(fd, bytes, inode_bytes, inode_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 994 | bytes += inode_bytes; |
| 995 | |
| 996 | return start_bytes; |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | long long write_directories() |
| 1001 | { |
| 1002 | unsigned short c_byte; |
| 1003 | int avail_bytes; |
| 1004 | char *directoryp = directory_data_cache; |
| 1005 | long long start_bytes = bytes; |
| 1006 | |
| 1007 | while(directory_cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1008 | if(directory_size - directory_bytes < |
| 1009 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1010 | void *dt = realloc(directory_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1011 | directory_size + ((SQUASHFS_METADATA_SIZE << 1) |
| 1012 | + 2)); |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1013 | if(dt == NULL) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1014 | BAD_ERROR("Out of memory in directory table " |
| 1015 | "reallocation!\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1016 | } |
| 1017 | directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1018 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1019 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1020 | avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 1021 | SQUASHFS_METADATA_SIZE : directory_cache_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1022 | c_byte = mangle(directory_table + directory_bytes + |
| 1023 | BLOCK_OFFSET, directoryp, avail_bytes, |
| 1024 | SQUASHFS_METADATA_SIZE, noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1025 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1026 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1027 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1028 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1029 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1030 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1031 | total_directory_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1032 | directoryp += avail_bytes; |
| 1033 | directory_cache_bytes -= avail_bytes; |
| 1034 | } |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 1035 | write_destination(fd, bytes, directory_bytes, directory_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1036 | bytes += directory_bytes; |
| 1037 | |
| 1038 | return start_bytes; |
| 1039 | } |
| 1040 | |
| 1041 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1042 | long long write_id_table() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1043 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1044 | unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1045 | unsigned int p[id_count]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1046 | int i; |
| 1047 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1048 | 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] | 1049 | for(i = 0; i < id_count; i++) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1050 | 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] | 1051 | SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1054 | return generic_write_table(id_bytes, p, 0, NULL, noI); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1058 | struct id *get_id(unsigned int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1059 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1060 | int hash = ID_HASH(id); |
| 1061 | struct id *entry = id_hash_table[hash]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1062 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1063 | for(; entry; entry = entry->next) |
| 1064 | if(entry->id == id) |
| 1065 | break; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1066 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1067 | return entry; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1071 | struct id *create_id(unsigned int id) |
| 1072 | { |
| 1073 | int hash = ID_HASH(id); |
| 1074 | struct id *entry = malloc(sizeof(struct id)); |
| 1075 | if(entry == NULL) |
| 1076 | BAD_ERROR("Out of memory in create_id\n"); |
| 1077 | entry->id = id; |
| 1078 | entry->index = id_count ++; |
| 1079 | entry->flags = 0; |
| 1080 | entry->next = id_hash_table[hash]; |
| 1081 | id_hash_table[hash] = entry; |
| 1082 | id_table[entry->index] = entry; |
| 1083 | return entry; |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | unsigned int get_uid(unsigned int uid) |
| 1088 | { |
| 1089 | struct id *entry = get_id(uid); |
| 1090 | |
| 1091 | if(entry == NULL) { |
| 1092 | if(id_count == SQUASHFS_IDS) |
| 1093 | BAD_ERROR("Out of uids!\n"); |
| 1094 | entry = create_id(uid); |
| 1095 | } |
| 1096 | |
| 1097 | if((entry->flags & ISA_UID) == 0) { |
| 1098 | entry->flags |= ISA_UID; |
| 1099 | uid_count ++; |
| 1100 | } |
| 1101 | |
| 1102 | return entry->index; |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | unsigned int get_guid(unsigned int guid) |
| 1107 | { |
| 1108 | struct id *entry = get_id(guid); |
| 1109 | |
| 1110 | if(entry == NULL) { |
| 1111 | if(id_count == SQUASHFS_IDS) |
| 1112 | BAD_ERROR("Out of gids!\n"); |
| 1113 | entry = create_id(guid); |
| 1114 | } |
| 1115 | |
| 1116 | if((entry->flags & ISA_GID) == 0) { |
| 1117 | entry->flags |= ISA_GID; |
| 1118 | guid_count ++; |
| 1119 | } |
| 1120 | |
| 1121 | return entry->index; |
| 1122 | } |
| 1123 | |
| 1124 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 1125 | #define ALLOC_SIZE 128 |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 1126 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 1127 | char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size) |
| 1128 | { |
| 1129 | if(pathname == NULL) { |
| 1130 | pathname = malloc(ALLOC_SIZE); |
| 1131 | if(pathname == NULL) |
| 1132 | BAD_ERROR("Out of memory in pathname\n"); |
| 1133 | } |
| 1134 | |
| 1135 | for(;;) { |
| 1136 | int res = snprintf(pathname, *size, "%s/%s", |
| 1137 | dir_ent->our_dir->pathname, |
| 1138 | dir_ent->source_name ? : dir_ent->name); |
| 1139 | |
| 1140 | if(res < 0) |
| 1141 | BAD_ERROR("snprintf failed in pathname\n"); |
| 1142 | else if(res >= *size) { |
| 1143 | /* |
| 1144 | * pathname is too small to contain the result, so |
| 1145 | * increase it and try again |
| 1146 | */ |
| 1147 | *size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 1148 | pathname = realloc(pathname, *size); |
| 1149 | if(pathname == NULL) |
| 1150 | BAD_ERROR("Out of memory in pathname\n"); |
| 1151 | } else |
| 1152 | break; |
| 1153 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 1154 | |
| 1155 | return pathname; |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | char *pathname(struct dir_ent *dir_ent) |
| 1160 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 1161 | static char *pathname = NULL; |
| 1162 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 1163 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 1164 | if (dir_ent->nonstandard_pathname) |
| 1165 | return dir_ent->nonstandard_pathname; |
| 1166 | |
| 1167 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | |
| 1171 | char *pathname_reader(struct dir_ent *dir_ent) |
| 1172 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 1173 | static char *pathname = NULL; |
| 1174 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 1175 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 1176 | if (dir_ent->nonstandard_pathname) |
| 1177 | return dir_ent->nonstandard_pathname; |
| 1178 | |
| 1179 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 1183 | char *subpathname(struct dir_ent *dir_ent) |
| 1184 | { |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 1185 | static char *subpath = NULL; |
| 1186 | static int size = ALLOC_SIZE; |
| 1187 | int res; |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 1188 | |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 1189 | if(subpath == NULL) { |
| 1190 | subpath = malloc(ALLOC_SIZE); |
| 1191 | if(subpath == NULL) |
| 1192 | BAD_ERROR("Out of memory in subpathname\n"); |
| 1193 | } |
| 1194 | |
| 1195 | for(;;) { |
| 1196 | if(dir_ent->our_dir->subpath[0] != '\0') |
| 1197 | res = snprintf(subpath, size, "%s/%s", |
| 1198 | dir_ent->our_dir->subpath, dir_ent->name); |
| 1199 | else |
| 1200 | res = snprintf(subpath, size, "/%s", dir_ent->name); |
| 1201 | |
| 1202 | if(res < 0) |
| 1203 | BAD_ERROR("snprintf failed in subpathname\n"); |
| 1204 | else if(res >= size) { |
| 1205 | /* |
| 1206 | * subpath is too small to contain the result, so |
| 1207 | * increase it and try again |
| 1208 | */ |
| 1209 | size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 1210 | subpath = realloc(subpath, size); |
| 1211 | if(subpath == NULL) |
| 1212 | BAD_ERROR("Out of memory in subpathname\n"); |
| 1213 | } else |
| 1214 | break; |
Phillip Lougher | 90e0825 | 2012-10-05 04:33:35 +0100 | [diff] [blame] | 1215 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 1216 | |
| 1217 | return subpath; |
| 1218 | } |
| 1219 | |
| 1220 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1221 | inline unsigned int get_inode_no(struct inode_info *inode) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 1222 | { |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1223 | return inode->inode_number; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1227 | inline unsigned int get_parent_no(struct dir_info *dir) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 1228 | { |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 1229 | return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1233 | int create_inode(squashfs_inode *i_no, struct dir_info *dir_info, |
| 1234 | struct dir_ent *dir_ent, int type, long long byte_size, |
| 1235 | long long start_block, unsigned int offset, unsigned int *block_list, |
| 1236 | struct fragment *fragment, struct directory *dir_in, long long sparse) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1237 | { |
| 1238 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 8973a12 | 2010-12-31 09:52:45 +0000 | [diff] [blame] | 1239 | union squashfs_inode_header inode_header; |
plougher | 857d0dd | 2010-12-31 21:03:13 +0000 | [diff] [blame] | 1240 | struct squashfs_base_inode_header *base = &inode_header.base; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1241 | void *inode; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 1242 | char *filename = pathname(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1243 | int nlink = dir_ent->inode->nlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1244 | int xattr = read_xattrs(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1245 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1246 | switch(type) { |
| 1247 | case SQUASHFS_FILE_TYPE: |
| 1248 | if(dir_ent->inode->nlink > 1 || |
| 1249 | byte_size >= (1LL << 32) || |
| 1250 | start_block >= (1LL << 32) || |
| 1251 | sparse || IS_XATTR(xattr)) |
| 1252 | type = SQUASHFS_LREG_TYPE; |
| 1253 | break; |
| 1254 | case SQUASHFS_DIR_TYPE: |
| 1255 | if(dir_info->dir_is_ldir || IS_XATTR(xattr)) |
| 1256 | type = SQUASHFS_LDIR_TYPE; |
| 1257 | break; |
| 1258 | case SQUASHFS_SYMLINK_TYPE: |
| 1259 | if(IS_XATTR(xattr)) |
| 1260 | type = SQUASHFS_LSYMLINK_TYPE; |
| 1261 | break; |
| 1262 | case SQUASHFS_BLKDEV_TYPE: |
| 1263 | if(IS_XATTR(xattr)) |
| 1264 | type = SQUASHFS_LBLKDEV_TYPE; |
| 1265 | break; |
| 1266 | case SQUASHFS_CHRDEV_TYPE: |
| 1267 | if(IS_XATTR(xattr)) |
| 1268 | type = SQUASHFS_LCHRDEV_TYPE; |
| 1269 | break; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1270 | case SQUASHFS_FIFO_TYPE: |
| 1271 | if(IS_XATTR(xattr)) |
| 1272 | type = SQUASHFS_LFIFO_TYPE; |
| 1273 | break; |
| 1274 | case SQUASHFS_SOCKET_TYPE: |
| 1275 | if(IS_XATTR(xattr)) |
| 1276 | type = SQUASHFS_LSOCKET_TYPE; |
| 1277 | break; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1280 | base->mode = SQUASHFS_MODE(buf->st_mode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1281 | base->uid = get_uid((unsigned int) global_uid == -1 ? |
| 1282 | buf->st_uid : global_uid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1283 | base->inode_type = type; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1284 | base->guid = get_guid((unsigned int) global_gid == -1 ? |
| 1285 | buf->st_gid : global_gid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1286 | base->mtime = buf->st_mtime; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1287 | base->inode_number = get_inode_no(dir_ent->inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1288 | |
| 1289 | if(type == SQUASHFS_FILE_TYPE) { |
| 1290 | int i; |
plougher | 8701ed6 | 2010-12-31 20:38:38 +0000 | [diff] [blame] | 1291 | struct squashfs_reg_inode_header *reg = &inode_header.reg; |
| 1292 | size_t off = offsetof(struct squashfs_reg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1293 | |
| 1294 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1295 | reg->file_size = byte_size; |
| 1296 | reg->start_block = start_block; |
| 1297 | reg->fragment = fragment->index; |
| 1298 | reg->offset = fragment->offset; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1299 | SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode); |
| 1300 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1301 | TRACE("File inode, file_size %lld, start_block 0x%llx, blocks " |
| 1302 | "%d, fragment %d, offset %d, size %d\n", byte_size, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1303 | start_block, offset, fragment->index, fragment->offset, |
| 1304 | fragment->size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1305 | for(i = 0; i < offset; i++) |
| 1306 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 1307 | } |
| 1308 | else if(type == SQUASHFS_LREG_TYPE) { |
| 1309 | int i; |
plougher | 1e6ac4a | 2010-12-31 20:42:02 +0000 | [diff] [blame] | 1310 | struct squashfs_lreg_inode_header *reg = &inode_header.lreg; |
| 1311 | size_t off = offsetof(struct squashfs_lreg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1312 | |
| 1313 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1314 | reg->nlink = nlink; |
| 1315 | reg->file_size = byte_size; |
| 1316 | reg->start_block = start_block; |
| 1317 | reg->fragment = fragment->index; |
| 1318 | reg->offset = fragment->offset; |
plougher | f5a674d | 2009-03-25 05:38:27 +0000 | [diff] [blame] | 1319 | if(sparse && sparse >= byte_size) |
| 1320 | sparse = byte_size - 1; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1321 | reg->sparse = sparse; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1322 | reg->xattr = xattr; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1323 | SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode); |
| 1324 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1325 | TRACE("Long file inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1326 | "blocks %d, fragment %d, offset %d, size %d, nlink %d" |
| 1327 | "\n", byte_size, start_block, offset, fragment->index, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1328 | fragment->offset, fragment->size, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1329 | for(i = 0; i < offset; i++) |
| 1330 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 1331 | } |
| 1332 | else if(type == SQUASHFS_LDIR_TYPE) { |
| 1333 | int i; |
| 1334 | unsigned char *p; |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 1335 | struct squashfs_ldir_inode_header *dir = &inode_header.ldir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1336 | struct cached_dir_index *index = dir_in->index; |
| 1337 | unsigned int i_count = dir_in->i_count; |
| 1338 | unsigned int i_size = dir_in->i_size; |
| 1339 | |
| 1340 | if(byte_size >= 1 << 27) |
| 1341 | BAD_ERROR("directory greater than 2^27-1 bytes!\n"); |
| 1342 | |
| 1343 | inode = get_inode(sizeof(*dir) + i_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1344 | dir->inode_type = SQUASHFS_LDIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1345 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 1346 | dir->file_size = byte_size; |
| 1347 | dir->offset = offset; |
| 1348 | dir->start_block = start_block; |
| 1349 | dir->i_count = i_count; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1350 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1351 | dir->xattr = xattr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1352 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1353 | SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode); |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 1354 | p = inode + offsetof(struct squashfs_ldir_inode_header, index); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1355 | for(i = 0; i < i_count; i++) { |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1356 | SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p); |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1357 | p += offsetof(struct squashfs_dir_index, name); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1358 | memcpy(p, index[i].name, index[i].index.size + 1); |
| 1359 | p += index[i].index.size + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1360 | } |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1361 | TRACE("Long directory inode, file_size %lld, start_block " |
| 1362 | "0x%llx, offset 0x%x, nlink %d\n", byte_size, |
| 1363 | start_block, offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1364 | } |
| 1365 | else if(type == SQUASHFS_DIR_TYPE) { |
plougher | 9d80a60 | 2010-12-31 20:47:24 +0000 | [diff] [blame] | 1366 | struct squashfs_dir_inode_header *dir = &inode_header.dir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1367 | |
| 1368 | inode = get_inode(sizeof(*dir)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1369 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 1370 | dir->file_size = byte_size; |
| 1371 | dir->offset = offset; |
| 1372 | dir->start_block = start_block; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1373 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1374 | SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1375 | TRACE("Directory inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1376 | "offset 0x%x, nlink %d\n", byte_size, start_block, |
| 1377 | offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1378 | } |
| 1379 | else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) { |
plougher | c70c633 | 2010-12-31 20:11:09 +0000 | [diff] [blame] | 1380 | struct squashfs_dev_inode_header *dev = &inode_header.dev; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1381 | unsigned int major = major(buf->st_rdev); |
| 1382 | unsigned int minor = minor(buf->st_rdev); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1383 | |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1384 | if(major > 0xfff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1385 | ERROR("Major %d out of range in device node %s, " |
| 1386 | "truncating to %d\n", major, filename, |
| 1387 | major & 0xfff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1388 | major &= 0xfff; |
| 1389 | } |
| 1390 | if(minor > 0xfffff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1391 | ERROR("Minor %d out of range in device node %s, " |
| 1392 | "truncating to %d\n", minor, filename, |
| 1393 | minor & 0xfffff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1394 | minor &= 0xfffff; |
| 1395 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1396 | inode = get_inode(sizeof(*dev)); |
| 1397 | dev->nlink = nlink; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1398 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1399 | ((minor & ~0xff) << 12); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1400 | SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1401 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1402 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1403 | else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) { |
plougher | 0b4ee5b | 2010-12-31 20:14:00 +0000 | [diff] [blame] | 1404 | struct squashfs_ldev_inode_header *dev = &inode_header.ldev; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1405 | unsigned int major = major(buf->st_rdev); |
| 1406 | unsigned int minor = minor(buf->st_rdev); |
| 1407 | |
| 1408 | if(major > 0xfff) { |
| 1409 | ERROR("Major %d out of range in device node %s, " |
| 1410 | "truncating to %d\n", major, filename, |
| 1411 | major & 0xfff); |
| 1412 | major &= 0xfff; |
| 1413 | } |
| 1414 | if(minor > 0xfffff) { |
| 1415 | ERROR("Minor %d out of range in device node %s, " |
| 1416 | "truncating to %d\n", minor, filename, |
| 1417 | minor & 0xfffff); |
| 1418 | minor &= 0xfffff; |
| 1419 | } |
| 1420 | inode = get_inode(sizeof(*dev)); |
| 1421 | dev->nlink = nlink; |
| 1422 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1423 | ((minor & ~0xff) << 12); |
| 1424 | dev->xattr = xattr; |
| 1425 | SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode); |
| 1426 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
| 1427 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1428 | else if(type == SQUASHFS_SYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1429 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1430 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1431 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1432 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1433 | |
plougher | 5cf38b8 | 2010-12-16 04:55:32 +0000 | [diff] [blame] | 1434 | byte = readlink(filename, buff, 65536); |
| 1435 | if(byte == -1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1436 | ERROR("Failed to read symlink %s, creating empty " |
| 1437 | "symlink\n", filename); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1438 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | if(byte == 65536) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1442 | ERROR("Symlink %s is greater than 65536 bytes! " |
| 1443 | "Creating empty symlink\n", filename); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1444 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | inode = get_inode(sizeof(*symlink) + byte); |
| 1448 | symlink->nlink = nlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1449 | symlink->symlink_size = byte; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1450 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1451 | strncpy(inode + off, buff, byte); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1452 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1453 | nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1454 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1455 | else if(type == SQUASHFS_LSYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1456 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1457 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1458 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1459 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1460 | |
plougher | df2b9aa | 2010-12-16 04:56:23 +0000 | [diff] [blame] | 1461 | byte = readlink(filename, buff, 65536); |
| 1462 | if(byte == -1) { |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1463 | ERROR("Failed to read symlink %s, creating empty " |
| 1464 | "symlink\n", filename); |
| 1465 | byte = 0; |
| 1466 | } |
| 1467 | |
| 1468 | if(byte == 65536) { |
| 1469 | ERROR("Symlink %s is greater than 65536 bytes! " |
| 1470 | "Creating empty symlink\n", filename); |
| 1471 | byte = 0; |
| 1472 | } |
| 1473 | |
| 1474 | inode = get_inode(sizeof(*symlink) + byte + |
| 1475 | sizeof(unsigned int)); |
| 1476 | symlink->nlink = nlink; |
| 1477 | symlink->symlink_size = byte; |
| 1478 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1479 | strncpy(inode + off, buff, byte); |
| 1480 | SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1); |
| 1481 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1482 | nlink); |
| 1483 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1484 | else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) { |
plougher | e56b986 | 2010-12-31 10:58:35 +0000 | [diff] [blame] | 1485 | struct squashfs_ipc_inode_header *ipc = &inode_header.ipc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1486 | |
| 1487 | inode = get_inode(sizeof(*ipc)); |
| 1488 | ipc->nlink = nlink; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1489 | SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1490 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1491 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1492 | } |
| 1493 | else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) { |
plougher | aa0d122 | 2010-12-31 20:06:24 +0000 | [diff] [blame] | 1494 | struct squashfs_lipc_inode_header *ipc = &inode_header.lipc; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1495 | |
| 1496 | inode = get_inode(sizeof(*ipc)); |
| 1497 | ipc->nlink = nlink; |
| 1498 | ipc->xattr = xattr; |
| 1499 | SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode); |
| 1500 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1501 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1502 | } else |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1503 | BAD_ERROR("Unrecognised inode %d in create_inode\n", type); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1504 | |
| 1505 | *i_no = MKINODE(inode); |
| 1506 | inode_count ++; |
| 1507 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1508 | TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type, |
| 1509 | base->uid, base->guid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1510 | |
| 1511 | return TRUE; |
| 1512 | } |
| 1513 | |
| 1514 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1515 | void add_dir(squashfs_inode inode, unsigned int inode_number, char *name, |
| 1516 | int type, struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1517 | { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 1518 | unsigned char *buff; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1519 | struct squashfs_dir_entry idir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1520 | unsigned int start_block = inode >> 16; |
| 1521 | unsigned int offset = inode & 0xffff; |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1522 | unsigned int size = strlen(name); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1523 | size_t name_off = offsetof(struct squashfs_dir_entry, name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1524 | |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1525 | if(size > SQUASHFS_NAME_LEN) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1526 | size = SQUASHFS_NAME_LEN; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1527 | ERROR("Filename is greater than %d characters, truncating! ..." |
| 1528 | "\n", SQUASHFS_NAME_LEN); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1531 | if(dir->p + sizeof(struct squashfs_dir_entry) + size + |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1532 | sizeof(struct squashfs_dir_header) |
| 1533 | >= dir->buff + dir->size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1534 | buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE); |
| 1535 | if(buff == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1536 | BAD_ERROR("Out of memory reallocating directory buffer" |
| 1537 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | dir->p = (dir->p - dir->buff) + buff; |
| 1541 | if(dir->entry_count_p) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1542 | dir->entry_count_p = (dir->entry_count_p - dir->buff + |
| 1543 | buff); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1544 | dir->index_count_p = dir->index_count_p - dir->buff + buff; |
| 1545 | dir->buff = buff; |
| 1546 | } |
| 1547 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1548 | if(dir->entry_count == 256 || start_block != dir->start_block || |
| 1549 | ((dir->entry_count_p != NULL) && |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1550 | ((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1551 | dir->index_count_p) > SQUASHFS_METADATA_SIZE)) || |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1552 | ((long long) inode_number - dir->inode_number) > 32767 |
| 1553 | || ((long long) inode_number - dir->inode_number) |
| 1554 | < -32768) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1555 | if(dir->entry_count_p) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1556 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1557 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1558 | if((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1559 | dir->index_count_p) > |
| 1560 | SQUASHFS_METADATA_SIZE) { |
| 1561 | if(dir->i_count % I_COUNT_SIZE == 0) { |
| 1562 | dir->index = realloc(dir->index, |
| 1563 | (dir->i_count + I_COUNT_SIZE) * |
| 1564 | sizeof(struct cached_dir_index)); |
| 1565 | if(dir->index == NULL) |
| 1566 | BAD_ERROR("Out of memory in " |
| 1567 | "directory index table " |
| 1568 | "reallocation!\n"); |
| 1569 | } |
| 1570 | dir->index[dir->i_count].index.index = |
| 1571 | dir->p - dir->buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1572 | dir->index[dir->i_count].index.size = size - 1; |
| 1573 | dir->index[dir->i_count++].name = name; |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1574 | dir->i_size += sizeof(struct squashfs_dir_index) |
| 1575 | + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1576 | dir->index_count_p = dir->p; |
| 1577 | } |
| 1578 | |
| 1579 | dir_header.count = dir->entry_count - 1; |
| 1580 | dir_header.start_block = dir->start_block; |
| 1581 | dir_header.inode_number = dir->inode_number; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1582 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1583 | dir->entry_count_p); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1584 | |
| 1585 | } |
| 1586 | |
| 1587 | |
| 1588 | dir->entry_count_p = dir->p; |
| 1589 | dir->start_block = start_block; |
| 1590 | dir->entry_count = 0; |
| 1591 | dir->inode_number = inode_number; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1592 | dir->p += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1595 | idir.offset = offset; |
| 1596 | idir.type = type; |
| 1597 | idir.size = size - 1; |
| 1598 | idir.inode_number = ((long long) inode_number - dir->inode_number); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1599 | SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p); |
| 1600 | strncpy((char *) dir->p + name_off, name, size); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1601 | dir->p += sizeof(struct squashfs_dir_entry) + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1602 | dir->entry_count ++; |
| 1603 | } |
| 1604 | |
| 1605 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1606 | void write_dir(squashfs_inode *inode, struct dir_info *dir_info, |
| 1607 | struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1608 | { |
| 1609 | unsigned int dir_size = dir->p - dir->buff; |
plougher | 4627ca3 | 2010-12-16 05:03:55 +0000 | [diff] [blame] | 1610 | int data_space = directory_cache_size - directory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1611 | unsigned int directory_block, directory_offset, i_count, index; |
| 1612 | unsigned short c_byte; |
| 1613 | |
| 1614 | if(data_space < dir_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1615 | int realloc_size = directory_cache_size == 0 ? |
| 1616 | ((dir_size + SQUASHFS_METADATA_SIZE) & |
| 1617 | ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1618 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1619 | void *dc = realloc(directory_data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1620 | directory_cache_size + realloc_size); |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1621 | if(dc == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1622 | goto failed; |
| 1623 | } |
| 1624 | directory_cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1625 | directory_data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | if(dir_size) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1629 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1630 | |
| 1631 | dir_header.count = dir->entry_count - 1; |
| 1632 | dir_header.start_block = dir->start_block; |
| 1633 | dir_header.inode_number = dir->inode_number; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1634 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1635 | memcpy(directory_data_cache + directory_cache_bytes, dir->buff, |
| 1636 | dir_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1637 | } |
| 1638 | directory_offset = directory_cache_bytes; |
| 1639 | directory_block = directory_bytes; |
| 1640 | directory_cache_bytes += dir_size; |
| 1641 | i_count = 0; |
| 1642 | index = SQUASHFS_METADATA_SIZE - directory_offset; |
| 1643 | |
| 1644 | while(1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1645 | while(i_count < dir->i_count && |
| 1646 | dir->index[i_count].index.index < index) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1647 | dir->index[i_count++].index.start_block = |
| 1648 | directory_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1649 | index += SQUASHFS_METADATA_SIZE; |
| 1650 | |
| 1651 | if(directory_cache_bytes < SQUASHFS_METADATA_SIZE) |
| 1652 | break; |
| 1653 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1654 | if((directory_size - directory_bytes) < |
| 1655 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1656 | void *dt = realloc(directory_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1657 | directory_size + (SQUASHFS_METADATA_SIZE << 1) |
| 1658 | + 2); |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1659 | if(dt == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1660 | goto failed; |
| 1661 | } |
| 1662 | directory_size += SQUASHFS_METADATA_SIZE << 1; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1663 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1666 | c_byte = mangle(directory_table + directory_bytes + |
| 1667 | BLOCK_OFFSET, directory_data_cache, |
| 1668 | SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, |
| 1669 | noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1670 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1671 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1672 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1673 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1674 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1675 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1676 | total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 1677 | memmove(directory_data_cache, directory_data_cache + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1678 | SQUASHFS_METADATA_SIZE, directory_cache_bytes - |
| 1679 | SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1680 | directory_cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 1681 | } |
| 1682 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1683 | create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE, |
| 1684 | dir_size + 3, directory_block, directory_offset, NULL, NULL, |
| 1685 | dir, 0); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1686 | |
| 1687 | #ifdef SQUASHFS_TRACE |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1688 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1689 | unsigned char *dirp; |
| 1690 | int count; |
| 1691 | |
| 1692 | TRACE("Directory contents of inode 0x%llx\n", *inode); |
| 1693 | dirp = dir->buff; |
| 1694 | while(dirp < dir->p) { |
| 1695 | char buffer[SQUASHFS_NAME_LEN + 1]; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1696 | struct squashfs_dir_entry idir, *idirp; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1697 | struct squashfs_dir_header dirh; |
| 1698 | SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1699 | &dirh); |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1700 | count = dirh.count + 1; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1701 | dirp += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1702 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1703 | TRACE("\tStart block 0x%x, count %d\n", |
| 1704 | dirh.start_block, count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1705 | |
| 1706 | while(count--) { |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1707 | idirp = (struct squashfs_dir_entry *) dirp; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1708 | SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1709 | strncpy(buffer, idirp->name, idir.size + 1); |
| 1710 | buffer[idir.size + 1] = '\0'; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1711 | TRACE("\t\tname %s, inode offset 0x%x, type " |
| 1712 | "%d\n", buffer, idir.offset, idir.type); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1713 | dirp += sizeof(struct squashfs_dir_entry) + idir.size + |
plougher | e8b26f6 | 2010-12-16 05:06:00 +0000 | [diff] [blame] | 1714 | 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | #endif |
| 1719 | dir_count ++; |
| 1720 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1721 | return; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1722 | |
| 1723 | failed: |
| 1724 | BAD_ERROR("Out of memory in directory table reallocation!\n"); |
| 1725 | } |
| 1726 | |
| 1727 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1728 | struct file_buffer *get_fragment(struct fragment *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1729 | { |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1730 | struct squashfs_fragment_entry *disk_fragment; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1731 | int res, size; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1732 | long long start_block; |
| 1733 | struct file_buffer *buffer, *compressed_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1734 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1735 | if(fragment->index == SQUASHFS_INVALID_FRAG) |
| 1736 | return NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1737 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1738 | buffer = cache_lookup(fragment_buffer, fragment->index); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1739 | if(buffer) |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1740 | return buffer; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1741 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1742 | compressed_buffer = cache_lookup(writer_buffer, fragment->index + |
| 1743 | FRAG_INDEX); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1744 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1745 | buffer = cache_get(fragment_buffer, fragment->index, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1746 | |
| 1747 | pthread_mutex_lock(&fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1748 | disk_fragment = &fragment_table[fragment->index]; |
| 1749 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1750 | start_block = disk_fragment->start_block; |
| 1751 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1752 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1753 | if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1754 | int error; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1755 | char *data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1756 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1757 | if(compressed_buffer) |
| 1758 | data = compressed_buffer->data; |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 1759 | else |
| 1760 | data = read_from_disk(start_block, size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1761 | |
plougher | b48442b | 2010-12-31 07:57:54 +0000 | [diff] [blame] | 1762 | res = compressor_uncompress(comp, buffer->data, data, size, |
| 1763 | block_size, &error); |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 1764 | if(res == -1) |
| 1765 | BAD_ERROR("%s uncompress failed with error code %d\n", |
| 1766 | comp->name, error); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1767 | } else if(compressed_buffer) |
| 1768 | memcpy(buffer->data, compressed_buffer->data, size); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1769 | else { |
| 1770 | res = read_fs_bytes(fd, start_block, size, buffer->data); |
| 1771 | if(res == 0) |
| 1772 | EXIT_MKSQUASHFS(); |
| 1773 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1774 | |
plougher | 03859fa | 2009-03-31 03:18:18 +0000 | [diff] [blame] | 1775 | cache_block_put(compressed_buffer); |
| 1776 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1777 | return buffer; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1780 | |
| 1781 | struct frag_locked { |
| 1782 | struct file_buffer *buffer; |
| 1783 | int c_byte; |
| 1784 | int fragment; |
| 1785 | struct frag_locked *fragment_prev; |
| 1786 | struct frag_locked *fragment_next; |
| 1787 | }; |
| 1788 | |
| 1789 | int fragments_locked = FALSE; |
| 1790 | struct frag_locked *frag_locked_list = NULL; |
| 1791 | |
| 1792 | INSERT_LIST(fragment, struct frag_locked) |
| 1793 | REMOVE_LIST(fragment, struct frag_locked) |
| 1794 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1795 | int lock_fragments() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1796 | { |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1797 | int count; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1798 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1799 | fragments_locked = TRUE; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1800 | count = fragments_outstanding; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1801 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1802 | return count; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | void unlock_fragments() |
| 1807 | { |
| 1808 | struct frag_locked *entry; |
| 1809 | int compressed_size; |
| 1810 | |
| 1811 | pthread_mutex_lock(&fragment_mutex); |
| 1812 | while(frag_locked_list) { |
| 1813 | entry = frag_locked_list; |
| 1814 | remove_fragment_list(&frag_locked_list, entry); |
| 1815 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(entry->c_byte); |
| 1816 | fragment_table[entry->fragment].size = entry->c_byte; |
| 1817 | fragment_table[entry->fragment].start_block = bytes; |
| 1818 | entry->buffer->block = bytes; |
| 1819 | bytes += compressed_size; |
| 1820 | fragments_outstanding --; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1821 | queue_put(to_writer, entry->buffer); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1822 | TRACE("fragment_locked writing fragment %d, compressed size %d" |
| 1823 | "\n", entry->fragment, compressed_size); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1824 | free(entry); |
| 1825 | } |
| 1826 | fragments_locked = FALSE; |
| 1827 | pthread_mutex_unlock(&fragment_mutex); |
| 1828 | } |
| 1829 | |
| 1830 | |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1831 | void add_pending_fragment(struct file_buffer *write_buffer, int c_byte, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1832 | int fragment) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1833 | { |
| 1834 | struct frag_locked *entry = malloc(sizeof(struct frag_locked)); |
| 1835 | if(entry == NULL) |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1836 | BAD_ERROR("Out of memory in add_pending fragment\n"); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1837 | entry->buffer = write_buffer; |
| 1838 | entry->c_byte = c_byte; |
| 1839 | entry->fragment = fragment; |
| 1840 | entry->fragment_prev = entry->fragment_next = NULL; |
| 1841 | pthread_mutex_lock(&fragment_mutex); |
| 1842 | insert_fragment_list(&frag_locked_list, entry); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1843 | pthread_mutex_unlock(&fragment_mutex); |
| 1844 | } |
| 1845 | |
| 1846 | |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1847 | void write_fragment(struct file_buffer *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1848 | { |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1849 | if(fragment == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1850 | return; |
| 1851 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1852 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1853 | fragment_table[fragment->block].unused = 0; |
| 1854 | fragments_outstanding ++; |
| 1855 | queue_put(to_frag, fragment); |
| 1856 | pthread_mutex_unlock(&fragment_mutex); |
| 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | struct file_buffer *allocate_fragment() |
| 1861 | { |
| 1862 | struct file_buffer *fragment = cache_get(fragment_buffer, fragments, 1); |
| 1863 | |
| 1864 | pthread_mutex_lock(&fragment_mutex); |
| 1865 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1866 | if(fragments % FRAG_SIZE == 0) { |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1867 | void *ft = realloc(fragment_table, (fragments + |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1868 | FRAG_SIZE) * sizeof(struct squashfs_fragment_entry)); |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1869 | if(ft == NULL) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1870 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1871 | BAD_ERROR("Out of memory in fragment table\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1872 | } |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1873 | fragment_table = ft; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1874 | } |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1875 | |
| 1876 | fragment->size = 0; |
| 1877 | fragment->block = fragments ++; |
| 1878 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1879 | pthread_mutex_unlock(&fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1880 | |
| 1881 | return fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 1884 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1885 | static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0}; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1886 | struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer, |
| 1887 | struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1888 | { |
| 1889 | struct fragment *ffrg; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1890 | struct file_buffer **fragment; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1891 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1892 | if(file_buffer == NULL || file_buffer->size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1893 | return &empty_fragment; |
| 1894 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1895 | fragment = eval_frag_actions(dir_ent); |
| 1896 | |
| 1897 | if((*fragment) && (*fragment)->size + file_buffer->size > block_size) { |
| 1898 | write_fragment(*fragment); |
| 1899 | *fragment = NULL; |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1900 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1901 | |
plougher | e7e6e83 | 2010-12-16 05:08:06 +0000 | [diff] [blame] | 1902 | ffrg = malloc(sizeof(struct fragment)); |
| 1903 | if(ffrg == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1904 | BAD_ERROR("Out of memory in fragment block allocation!\n"); |
| 1905 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1906 | if(*fragment == NULL) |
| 1907 | *fragment = allocate_fragment(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1908 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1909 | ffrg->index = (*fragment)->block; |
| 1910 | ffrg->offset = (*fragment)->size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1911 | ffrg->size = file_buffer->size; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1912 | memcpy((*fragment)->data + (*fragment)->size, file_buffer->data, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1913 | file_buffer->size); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1914 | (*fragment)->size += file_buffer->size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1915 | |
| 1916 | return ffrg; |
| 1917 | } |
| 1918 | |
| 1919 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1920 | long long generic_write_table(int length, void *buffer, int length2, |
| 1921 | void *buffer2, int uncompressed) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1922 | { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1923 | int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) / |
| 1924 | SQUASHFS_METADATA_SIZE; |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1925 | long long *list, start_bytes; |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1926 | int compressed_size, i, list_size = meta_blocks * sizeof(long long); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1927 | unsigned short c_byte; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1928 | char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2]; |
| 1929 | |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1930 | #ifdef SQUASHFS_TRACE |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1931 | long long obytes = bytes; |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1932 | int olength = length; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1933 | #endif |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1934 | |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1935 | list = malloc(list_size); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1936 | if(list == NULL) |
| 1937 | BAD_ERROR("Out of memory in generic_write_table\n"); |
| 1938 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1939 | for(i = 0; i < meta_blocks; i++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1940 | int avail_bytes = length > SQUASHFS_METADATA_SIZE ? |
| 1941 | SQUASHFS_METADATA_SIZE : length; |
| 1942 | c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i * |
| 1943 | SQUASHFS_METADATA_SIZE , avail_bytes, |
| 1944 | SQUASHFS_METADATA_SIZE, uncompressed, 0); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1945 | SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1946 | list[i] = bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1947 | compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1948 | BLOCK_OFFSET; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1949 | TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes, |
| 1950 | compressed_size); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1951 | write_destination(fd, bytes, compressed_size, cbuffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1952 | bytes += compressed_size; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1953 | total_bytes += avail_bytes; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1954 | length -= avail_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1957 | start_bytes = bytes; |
| 1958 | if(length2) { |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1959 | write_destination(fd, bytes, length2, buffer2); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1960 | bytes += length2; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1961 | total_bytes += length2; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1964 | SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks); |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1965 | write_destination(fd, bytes, list_size, list); |
| 1966 | bytes += list_size; |
| 1967 | total_bytes += list_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1968 | |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1969 | TRACE("generic_write_table: total uncompressed %d compressed %lld\n", |
| 1970 | olength, bytes - obytes); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1971 | |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1972 | free(list); |
| 1973 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1974 | return start_bytes; |
| 1975 | } |
| 1976 | |
| 1977 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1978 | long long write_fragment_table() |
| 1979 | { |
| 1980 | unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1981 | int i; |
| 1982 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1983 | TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments, |
| 1984 | frag_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1985 | for(i = 0; i < fragments; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1986 | TRACE("write_fragment_table: fragment %d, start_block 0x%llx, " |
| 1987 | "size %d\n", i, fragment_table[i].start_block, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1988 | fragment_table[i].size); |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1989 | SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1992 | return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1996 | char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1997 | char *read_from_disk(long long start, unsigned int avail_bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1998 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1999 | int res; |
| 2000 | |
| 2001 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer); |
| 2002 | if(res == 0) |
| 2003 | EXIT_MKSQUASHFS(); |
| 2004 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2005 | return read_from_file_buffer; |
| 2006 | } |
| 2007 | |
| 2008 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2009 | char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE]; |
| 2010 | char *read_from_disk2(long long start, unsigned int avail_bytes) |
| 2011 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 2012 | int res; |
| 2013 | |
| 2014 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2); |
| 2015 | if(res == 0) |
| 2016 | EXIT_MKSQUASHFS(); |
| 2017 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2018 | return read_from_file_buffer2; |
| 2019 | } |
| 2020 | |
| 2021 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2022 | /* |
| 2023 | * Compute 16 bit BSD checksum over the data |
| 2024 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2025 | unsigned short get_checksum(char *buff, int bytes, unsigned short chksum) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2026 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2027 | unsigned char *b = (unsigned char *) buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2028 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2029 | while(bytes --) { |
| 2030 | chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1; |
| 2031 | chksum += *b++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | return chksum; |
| 2035 | } |
| 2036 | |
| 2037 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2038 | unsigned short get_checksum_disk(long long start, long long l, |
| 2039 | unsigned int *blocks) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2040 | { |
| 2041 | unsigned short chksum = 0; |
| 2042 | unsigned int bytes; |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 2043 | struct file_buffer *write_buffer; |
| 2044 | int i; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2045 | |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 2046 | for(i = 0; l; i++) { |
| 2047 | bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]); |
| 2048 | if(bytes == 0) /* sparse block */ |
| 2049 | continue; |
| 2050 | write_buffer = cache_lookup(writer_buffer, start); |
| 2051 | if(write_buffer) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2052 | chksum = get_checksum(write_buffer->data, bytes, |
| 2053 | chksum); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 2054 | cache_block_put(write_buffer); |
| 2055 | } else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2056 | chksum = get_checksum(read_from_disk(start, bytes), |
| 2057 | bytes, chksum); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2058 | l -= bytes; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2059 | start += bytes; |
| 2060 | } |
| 2061 | |
| 2062 | return chksum; |
| 2063 | } |
| 2064 | |
| 2065 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2066 | unsigned short get_checksum_mem(char *buff, int bytes) |
| 2067 | { |
| 2068 | return get_checksum(buff, bytes, 0); |
| 2069 | } |
| 2070 | |
| 2071 | |
| 2072 | unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer) |
| 2073 | { |
| 2074 | if(file_buffer == NULL) |
| 2075 | return 0; |
| 2076 | else |
| 2077 | return get_checksum(file_buffer->data, file_buffer->size, 0); |
| 2078 | } |
| 2079 | |
| 2080 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2081 | #define DUP_HASH(a) (a & 0xffff) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2082 | 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] | 2083 | unsigned int *block_listp, int blocks, unsigned int fragment, |
| 2084 | int offset, int bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2085 | { |
| 2086 | struct fragment *frg; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 2087 | unsigned int *block_list = block_listp; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2088 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 2089 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2090 | if(!duplicate_checking || file_size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2091 | return; |
| 2092 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2093 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) { |
| 2094 | if(file_size != dupl_ptr->file_size) |
| 2095 | continue; |
| 2096 | if(blocks != 0 && start != dupl_ptr->start) |
| 2097 | continue; |
| 2098 | if(fragment != dupl_ptr->fragment->index) |
| 2099 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2100 | if(fragment != SQUASHFS_INVALID_FRAG && (offset != |
| 2101 | dupl_ptr->fragment->offset || bytes != |
| 2102 | dupl_ptr->fragment->size)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2103 | continue; |
| 2104 | return; |
| 2105 | } |
| 2106 | |
plougher | 3bb279c | 2010-12-16 05:10:03 +0000 | [diff] [blame] | 2107 | frg = malloc(sizeof(struct fragment)); |
| 2108 | if(frg == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2109 | BAD_ERROR("Out of memory in fragment block allocation!\n"); |
| 2110 | |
| 2111 | frg->index = fragment; |
| 2112 | frg->offset = offset; |
| 2113 | frg->size = bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2114 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2115 | add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0, FALSE); |
| 2116 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2117 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2118 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2119 | int pre_duplicate(long long file_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2120 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2121 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2122 | |
| 2123 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2124 | if(dupl_ptr->file_size == file_size) |
| 2125 | return TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2126 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2127 | return FALSE; |
| 2128 | } |
| 2129 | |
| 2130 | |
| 2131 | int pre_duplicate_frag(long long file_size, unsigned short checksum) |
| 2132 | { |
| 2133 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 2134 | |
| 2135 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2136 | if(file_size == dupl_ptr->file_size && file_size == |
| 2137 | dupl_ptr->fragment->size) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2138 | if(dupl_ptr->checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2139 | struct file_buffer *frag_buffer = |
| 2140 | get_fragment(dupl_ptr->fragment); |
| 2141 | dupl_ptr->checksum = |
| 2142 | get_checksum_disk(dupl_ptr->start, |
| 2143 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 2144 | dupl_ptr->fragment_checksum = |
| 2145 | get_checksum_mem(frag_buffer->data + |
| 2146 | dupl_ptr->fragment->offset, file_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2147 | cache_block_put(frag_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2148 | dupl_ptr->checksum_flag = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2149 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2150 | if(dupl_ptr->fragment_checksum == checksum) |
| 2151 | return TRUE; |
| 2152 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2153 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2154 | return FALSE; |
| 2155 | } |
| 2156 | |
| 2157 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2158 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 2159 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 2160 | unsigned short checksum, unsigned short fragment_checksum, |
| 2161 | int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2162 | { |
plougher | 51ef9ae | 2010-12-16 05:14:28 +0000 | [diff] [blame] | 2163 | struct file_info *dupl_ptr = malloc(sizeof(struct file_info)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2164 | |
plougher | 51ef9ae | 2010-12-16 05:14:28 +0000 | [diff] [blame] | 2165 | if(dupl_ptr == NULL) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2166 | BAD_ERROR("Out of memory in dup_files allocation!\n"); |
| 2167 | } |
| 2168 | |
| 2169 | dupl_ptr->file_size = file_size; |
| 2170 | dupl_ptr->bytes = bytes; |
| 2171 | dupl_ptr->block_list = block_list; |
| 2172 | dupl_ptr->start = start; |
| 2173 | dupl_ptr->fragment = fragment; |
| 2174 | dupl_ptr->checksum = checksum; |
| 2175 | dupl_ptr->fragment_checksum = fragment_checksum; |
| 2176 | dupl_ptr->checksum_flag = checksum_flag; |
| 2177 | dupl_ptr->next = dupl[DUP_HASH(file_size)]; |
| 2178 | dupl[DUP_HASH(file_size)] = dupl_ptr; |
| 2179 | dup_files ++; |
| 2180 | |
| 2181 | return dupl_ptr; |
| 2182 | } |
| 2183 | |
| 2184 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2185 | struct file_info *duplicate(long long file_size, long long bytes, |
| 2186 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 2187 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 2188 | unsigned short fragment_checksum, int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2189 | { |
| 2190 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 2191 | int frag_bytes = file_buffer ? file_buffer->size : 0; |
| 2192 | |
| 2193 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2194 | if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes |
| 2195 | && frag_bytes == dupl_ptr->fragment->size) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2196 | long long target_start, dup_start = dupl_ptr->start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2197 | int block; |
| 2198 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2199 | if(memcmp(*block_list, dupl_ptr->block_list, blocks * |
| 2200 | sizeof(unsigned int)) != 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 2201 | continue; |
| 2202 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2203 | if(checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2204 | checksum = get_checksum_disk(*start, bytes, |
| 2205 | *block_list); |
| 2206 | fragment_checksum = |
| 2207 | get_checksum_mem_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2208 | checksum_flag = TRUE; |
| 2209 | } |
| 2210 | |
| 2211 | if(dupl_ptr->checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2212 | struct file_buffer *frag_buffer = |
| 2213 | get_fragment(dupl_ptr->fragment); |
| 2214 | dupl_ptr->checksum = |
| 2215 | get_checksum_disk(dupl_ptr->start, |
| 2216 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 2217 | dupl_ptr->fragment_checksum = |
| 2218 | get_checksum_mem(frag_buffer->data + |
| 2219 | dupl_ptr->fragment->offset, frag_bytes); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2220 | cache_block_put(frag_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2221 | dupl_ptr->checksum_flag = TRUE; |
| 2222 | } |
| 2223 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2224 | if(checksum != dupl_ptr->checksum || |
| 2225 | fragment_checksum != |
| 2226 | dupl_ptr->fragment_checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2227 | continue; |
| 2228 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2229 | target_start = *start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2230 | for(block = 0; block < blocks; block ++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2231 | int size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2232 | ((*block_list)[block]); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2233 | struct file_buffer *target_buffer = NULL; |
| 2234 | struct file_buffer *dup_buffer = NULL; |
| 2235 | char *target_data, *dup_data; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2236 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2237 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2238 | if(size == 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 2239 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2240 | target_buffer = cache_lookup(writer_buffer, |
| 2241 | target_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2242 | if(target_buffer) |
| 2243 | target_data = target_buffer->data; |
| 2244 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2245 | target_data = |
| 2246 | read_from_disk(target_start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2247 | size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2248 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2249 | dup_buffer = cache_lookup(writer_buffer, |
| 2250 | dup_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2251 | if(dup_buffer) |
| 2252 | dup_data = dup_buffer->data; |
| 2253 | else |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2254 | dup_data = read_from_disk2(dup_start, |
| 2255 | size); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2256 | |
| 2257 | res = memcmp(target_data, dup_data, size); |
| 2258 | cache_block_put(target_buffer); |
| 2259 | cache_block_put(dup_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2260 | if(res != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2261 | break; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2262 | target_start += size; |
| 2263 | dup_start += size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2264 | } |
| 2265 | if(block == blocks) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2266 | struct file_buffer *frag_buffer = |
| 2267 | get_fragment(dupl_ptr->fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2268 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2269 | if(frag_bytes == 0 || |
| 2270 | memcmp(file_buffer->data, |
| 2271 | frag_buffer->data + |
| 2272 | dupl_ptr->fragment->offset, |
| 2273 | frag_bytes) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2274 | TRACE("Found duplicate file, start " |
| 2275 | "0x%llx, size %lld, checksum " |
| 2276 | "0x%x, fragment %d, size %d, " |
| 2277 | "offset %d, checksum 0x%x\n", |
| 2278 | dupl_ptr->start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2279 | dupl_ptr->bytes, |
| 2280 | dupl_ptr->checksum, |
| 2281 | dupl_ptr->fragment->index, |
| 2282 | frag_bytes, |
| 2283 | dupl_ptr->fragment->offset, |
| 2284 | fragment_checksum); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2285 | *block_list = dupl_ptr->block_list; |
| 2286 | *start = dupl_ptr->start; |
| 2287 | *fragment = dupl_ptr->fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2288 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2289 | return 0; |
| 2290 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2291 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2296 | return add_non_dup(file_size, bytes, *block_list, *start, *fragment, |
| 2297 | checksum, fragment_checksum, checksum_flag); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2301 | inline int is_fragment(struct inode_info *inode) |
| 2302 | { |
| 2303 | int file_size = inode->buf.st_size; |
| 2304 | |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2305 | /* |
| 2306 | * If this block is to be compressed differently to the |
| 2307 | * fragment compression then it cannot be a fragment |
| 2308 | */ |
| 2309 | if(inode->noF != noF) |
| 2310 | return FALSE; |
| 2311 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2312 | return !inode->no_fragments && (file_size < block_size || |
| 2313 | (inode->always_use_fragments && file_size & (block_size - 1))); |
| 2314 | } |
| 2315 | |
| 2316 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2317 | static int seq = 0; |
| 2318 | void reader_read_process(struct dir_ent *dir_ent) |
| 2319 | { |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2320 | struct inode_info *inode = dir_ent->inode; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2321 | struct file_buffer *prev_buffer = NULL, *file_buffer; |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2322 | int status, res, byte, count = 0; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2323 | int file = get_pseudo_file(inode->pseudo_id)->fd; |
| 2324 | int child = get_pseudo_file(inode->pseudo_id)->child; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2325 | long long bytes = 0; |
| 2326 | |
| 2327 | while(1) { |
| 2328 | file_buffer = cache_get(reader_buffer, 0, 0); |
| 2329 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2330 | file_buffer->noD = inode->noD; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2331 | |
| 2332 | byte = read_bytes(file, file_buffer->data, block_size); |
| 2333 | if(byte == -1) |
| 2334 | goto read_err; |
| 2335 | |
| 2336 | file_buffer->size = byte; |
| 2337 | file_buffer->file_size = -1; |
| 2338 | file_buffer->block = count ++; |
| 2339 | file_buffer->error = FALSE; |
| 2340 | file_buffer->fragment = FALSE; |
| 2341 | bytes += byte; |
| 2342 | |
| 2343 | if(byte == 0) |
| 2344 | break; |
| 2345 | |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2346 | /* |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2347 | * Update progress bar size. This is done |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2348 | * on every block rather than waiting for all blocks to be |
| 2349 | * read incase write_file_process() is running in parallel |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2350 | * with this. Otherwise the current progress bar position |
| 2351 | * may get ahead of the progress bar size. |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2352 | */ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2353 | progress_bar_size(1); |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2354 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2355 | if(prev_buffer) |
| 2356 | queue_put(from_reader, prev_buffer); |
| 2357 | prev_buffer = file_buffer; |
| 2358 | } |
| 2359 | |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2360 | /* |
| 2361 | * Update inode file size now that the size of the dynamic pseudo file |
| 2362 | * is known. This is needed for the -info option. |
| 2363 | */ |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2364 | inode->buf.st_size = bytes; |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2365 | |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2366 | res = waitpid(child, &status, 0); |
plougher | d87d8d1 | 2009-09-10 04:08:16 +0000 | [diff] [blame] | 2367 | if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2368 | goto read_err; |
| 2369 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2370 | if(prev_buffer == NULL) |
| 2371 | prev_buffer = file_buffer; |
| 2372 | else { |
| 2373 | cache_block_put(file_buffer); |
| 2374 | seq --; |
| 2375 | } |
| 2376 | prev_buffer->file_size = bytes; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2377 | prev_buffer->fragment = is_fragment(inode); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2378 | queue_put(from_reader, prev_buffer); |
| 2379 | |
| 2380 | return; |
| 2381 | |
| 2382 | read_err: |
| 2383 | if(prev_buffer) { |
| 2384 | cache_block_put(file_buffer); |
| 2385 | seq --; |
| 2386 | file_buffer = prev_buffer; |
| 2387 | } |
| 2388 | file_buffer->error = TRUE; |
| 2389 | queue_put(from_deflate, file_buffer); |
| 2390 | } |
| 2391 | |
| 2392 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2393 | void reader_read_file(struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2394 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2395 | struct stat *buf = &dir_ent->inode->buf, buf2; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2396 | struct file_buffer *file_buffer; |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2397 | int blocks, byte, count, expected, file, res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2398 | long long bytes, read_size; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2399 | struct inode_info *inode = dir_ent->inode; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2400 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2401 | if(inode->read) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2402 | return; |
| 2403 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2404 | inode->read = TRUE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2405 | again: |
| 2406 | bytes = 0; |
| 2407 | count = 0; |
| 2408 | file_buffer = NULL; |
| 2409 | read_size = buf->st_size; |
| 2410 | blocks = (read_size + block_size - 1) >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2411 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2412 | file = open(pathname_reader(dir_ent), O_RDONLY); |
plougher | c1d258e | 2010-12-16 05:16:45 +0000 | [diff] [blame] | 2413 | if(file == -1) { |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2414 | file_buffer = cache_get(reader_buffer, 0, 0); |
| 2415 | file_buffer->sequence = seq ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2416 | goto read_err; |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2417 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2418 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2419 | do { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2420 | expected = read_size - ((long long) count * block_size) > |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2421 | block_size ? block_size : |
| 2422 | read_size - ((long long) count * block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2423 | |
| 2424 | if(file_buffer) |
| 2425 | queue_put(from_reader, file_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2426 | file_buffer = cache_get(reader_buffer, 0, 0); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2427 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2428 | file_buffer->noD = inode->noD; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2429 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2430 | /* |
| 2431 | * Always try to read block_size bytes from the file rather |
| 2432 | * than expected bytes (which will be less than the block_size |
| 2433 | * at the file tail) to check that the file hasn't grown |
| 2434 | * since being stated. If it is longer (or shorter) than |
| 2435 | * expected, then restat, and try again. Note the special |
| 2436 | * case where the file is an exact multiple of the block_size |
| 2437 | * is dealt with later. |
| 2438 | */ |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2439 | byte = file_buffer->size = read_bytes(file, file_buffer->data, |
| 2440 | block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2441 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2442 | file_buffer->file_size = read_size; |
| 2443 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2444 | if(byte == -1) |
| 2445 | goto read_err; |
| 2446 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2447 | if(byte != expected) |
| 2448 | goto restat; |
| 2449 | |
| 2450 | file_buffer->block = count; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2451 | file_buffer->error = FALSE; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2452 | file_buffer->fragment = FALSE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2453 | |
| 2454 | bytes += byte; |
| 2455 | count ++; |
| 2456 | } while(count < blocks); |
| 2457 | |
| 2458 | if(read_size != bytes) |
| 2459 | goto restat; |
| 2460 | |
| 2461 | if(expected == block_size) { |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2462 | /* |
| 2463 | * Special case where we've not tried to read past the end of |
| 2464 | * the file. We expect to get EOF, i.e. the file isn't larger |
| 2465 | * than we expect. |
| 2466 | */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2467 | char buffer; |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2468 | int res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2469 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2470 | res = read_bytes(file, &buffer, 1); |
| 2471 | if(res == -1) |
| 2472 | goto read_err; |
| 2473 | |
| 2474 | if(res != 0) |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2475 | goto restat; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2478 | file_buffer->fragment = is_fragment(inode); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2479 | queue_put(from_reader, file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2480 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2481 | close(file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2482 | |
| 2483 | return; |
| 2484 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2485 | restat: |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2486 | res = fstat(file, &buf2); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2487 | close(file); |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2488 | if(res == -1) { |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 2489 | ERROR("Cannot stat dir/file %s because %s, ignoring\n", |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2490 | pathname_reader(dir_ent), strerror(errno)); |
| 2491 | goto read_err; |
| 2492 | } |
| 2493 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2494 | if(read_size != buf2.st_size) { |
| 2495 | memcpy(buf, &buf2, sizeof(struct stat)); |
| 2496 | file_buffer->error = 2; |
| 2497 | queue_put(from_deflate, file_buffer); |
| 2498 | goto again; |
| 2499 | } |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2500 | read_err: |
| 2501 | file_buffer->error = TRUE; |
| 2502 | queue_put(from_deflate, file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | |
| 2506 | void reader_scan(struct dir_info *dir) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2507 | struct dir_ent *dir_ent = dir->list; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2508 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2509 | for(; dir_ent; dir_ent = dir_ent->next) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2510 | struct stat *buf = &dir_ent->inode->buf; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2511 | if(dir_ent->inode->root_entry) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2512 | continue; |
| 2513 | |
plougher | b3977eb | 2010-05-02 02:08:48 +0000 | [diff] [blame] | 2514 | if(IS_PSEUDO_PROCESS(dir_ent->inode)) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2515 | reader_read_process(dir_ent); |
| 2516 | continue; |
| 2517 | } |
| 2518 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2519 | switch(buf->st_mode & S_IFMT) { |
| 2520 | case S_IFREG: |
| 2521 | reader_read_file(dir_ent); |
| 2522 | break; |
| 2523 | case S_IFDIR: |
| 2524 | reader_scan(dir_ent->dir); |
| 2525 | break; |
| 2526 | } |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | |
| 2531 | void *reader(void *arg) |
| 2532 | { |
| 2533 | int oldstate; |
| 2534 | |
| 2535 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2536 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2537 | |
| 2538 | if(!sorted) |
| 2539 | reader_scan(queue_get(to_reader)); |
| 2540 | else { |
| 2541 | int i; |
| 2542 | struct priority_entry *entry; |
| 2543 | |
| 2544 | queue_get(to_reader); |
| 2545 | for(i = 65535; i >= 0; i--) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2546 | for(entry = priority_list[i]; entry; |
| 2547 | entry = entry->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2548 | reader_read_file(entry->dir); |
| 2549 | } |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2550 | |
plougher | 5aa1816 | 2007-12-13 12:15:21 +0000 | [diff] [blame] | 2551 | thread[0] = 0; |
| 2552 | |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2553 | pthread_exit(NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | |
| 2557 | void *writer(void *arg) |
| 2558 | { |
| 2559 | int write_error = FALSE; |
| 2560 | int oldstate; |
| 2561 | |
| 2562 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2563 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2564 | |
| 2565 | while(1) { |
| 2566 | struct file_buffer *file_buffer = queue_get(to_writer); |
| 2567 | off_t off; |
| 2568 | |
| 2569 | if(file_buffer == NULL) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2570 | queue_put(from_writer, |
plougher | c2a5ae1 | 2010-12-31 08:35:28 +0000 | [diff] [blame] | 2571 | write_error ? &write_error : NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2572 | continue; |
| 2573 | } |
| 2574 | |
| 2575 | off = file_buffer->block; |
| 2576 | |
| 2577 | pthread_mutex_lock(&pos_mutex); |
| 2578 | |
| 2579 | if(!write_error && lseek(fd, off, SEEK_SET) == -1) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2580 | ERROR("Lseek on destination failed because %s\n", |
| 2581 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2582 | write_error = TRUE; |
| 2583 | } |
| 2584 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2585 | if(!write_error && write_bytes(fd, file_buffer->data, |
| 2586 | file_buffer->size) == -1) { |
| 2587 | ERROR("Write on destination failed because %s\n", |
| 2588 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2589 | write_error = TRUE; |
| 2590 | } |
| 2591 | pthread_mutex_unlock(&pos_mutex); |
| 2592 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2593 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2598 | int all_zero(struct file_buffer *file_buffer) |
| 2599 | { |
| 2600 | int i; |
| 2601 | long entries = file_buffer->size / sizeof(long); |
| 2602 | long *p = (long *) file_buffer->data; |
| 2603 | |
| 2604 | for(i = 0; i < entries && p[i] == 0; i++); |
| 2605 | |
| 2606 | if(i == entries) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2607 | for(i = file_buffer->size & ~(sizeof(long) - 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2608 | i < file_buffer->size && file_buffer->data[i] == 0; |
| 2609 | i++); |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2610 | |
| 2611 | return i == file_buffer->size; |
| 2612 | } |
| 2613 | |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
| 2617 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2618 | void *deflator(void *arg) |
| 2619 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2620 | void *stream = NULL; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2621 | int res, oldstate; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2622 | |
| 2623 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2624 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2625 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2626 | res = compressor_init(comp, &stream, block_size, 1); |
| 2627 | if(res) |
| 2628 | BAD_ERROR("deflator:: compressor_init failed\n"); |
| 2629 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2630 | while(1) { |
| 2631 | struct file_buffer *file_buffer = queue_get(from_reader); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2632 | struct file_buffer *write_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2633 | |
Phillip Lougher | 4885438 | 2011-09-09 03:36:55 +0100 | [diff] [blame] | 2634 | if(file_buffer->file_size == 0) { |
| 2635 | file_buffer->c_byte = 0; |
| 2636 | queue_put(from_deflate, file_buffer); |
| 2637 | } else if(sparse_files && all_zero(file_buffer)) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2638 | file_buffer->c_byte = 0; |
| 2639 | queue_put(from_deflate, file_buffer); |
| 2640 | } else if(file_buffer->fragment) { |
| 2641 | file_buffer->c_byte = file_buffer->size; |
| 2642 | queue_put(from_deflate, file_buffer); |
| 2643 | } else { |
| 2644 | write_buffer = cache_get(writer_buffer, 0, 0); |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2645 | write_buffer->c_byte = mangle2(stream, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2646 | write_buffer->data, file_buffer->data, |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2647 | file_buffer->size, block_size, |
| 2648 | file_buffer->noD, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2649 | write_buffer->sequence = file_buffer->sequence; |
| 2650 | write_buffer->file_size = file_buffer->file_size; |
| 2651 | write_buffer->block = file_buffer->block; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2652 | write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2653 | (write_buffer->c_byte); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2654 | write_buffer->fragment = FALSE; |
| 2655 | write_buffer->error = FALSE; |
| 2656 | cache_block_put(file_buffer); |
| 2657 | queue_put(from_deflate, write_buffer); |
| 2658 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | |
| 2663 | void *frag_deflator(void *arg) |
| 2664 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2665 | void *stream = NULL; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2666 | int res, oldstate; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2667 | |
| 2668 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2669 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2670 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2671 | res = compressor_init(comp, &stream, block_size, 1); |
| 2672 | if(res) |
| 2673 | BAD_ERROR("frag_deflator:: compressor_init failed\n"); |
| 2674 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2675 | while(1) { |
| 2676 | int c_byte, compressed_size; |
| 2677 | struct file_buffer *file_buffer = queue_get(to_frag); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2678 | struct file_buffer *write_buffer = |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2679 | cache_get(writer_buffer, file_buffer->block + |
| 2680 | FRAG_INDEX, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2681 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2682 | c_byte = mangle2(stream, write_buffer->data, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2683 | file_buffer->size, block_size, noF, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2684 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte); |
plougher | d036a31 | 2008-03-08 01:38:27 +0000 | [diff] [blame] | 2685 | write_buffer->size = compressed_size; |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2686 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2687 | if(fragments_locked == FALSE) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2688 | fragment_table[file_buffer->block].size = c_byte; |
| 2689 | fragment_table[file_buffer->block].start_block = bytes; |
| 2690 | write_buffer->block = bytes; |
| 2691 | bytes += compressed_size; |
| 2692 | fragments_outstanding --; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2693 | queue_put(to_writer, write_buffer); |
plougher | 5750191 | 2009-09-20 02:20:42 +0000 | [diff] [blame] | 2694 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2695 | TRACE("Writing fragment %lld, uncompressed size %d, " |
| 2696 | "compressed size %d\n", file_buffer->block, |
| 2697 | file_buffer->size, compressed_size); |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2698 | } else { |
| 2699 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2700 | add_pending_fragment(write_buffer, c_byte, |
| 2701 | file_buffer->block); |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2702 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2703 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | |
| 2708 | #define HASH_ENTRIES 256 |
| 2709 | #define BLOCK_HASH(a) (a % HASH_ENTRIES) |
| 2710 | struct file_buffer *block_hash[HASH_ENTRIES]; |
| 2711 | |
| 2712 | void push_buffer(struct file_buffer *file_buffer) |
| 2713 | { |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2714 | int hash = BLOCK_HASH(file_buffer->sequence); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2715 | |
| 2716 | file_buffer->next = block_hash[hash]; |
| 2717 | block_hash[hash] = file_buffer; |
| 2718 | } |
| 2719 | |
| 2720 | |
| 2721 | struct file_buffer *get_file_buffer(struct queue *queue) |
| 2722 | { |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2723 | static unsigned int sequence = 0; |
| 2724 | int hash = BLOCK_HASH(sequence); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2725 | struct file_buffer *file_buffer = block_hash[hash], *prev = NULL; |
| 2726 | |
| 2727 | for(;file_buffer; prev = file_buffer, file_buffer = file_buffer->next) |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2728 | if(file_buffer->sequence == sequence) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2729 | break; |
| 2730 | |
| 2731 | if(file_buffer) { |
| 2732 | if(prev) |
| 2733 | prev->next = file_buffer->next; |
| 2734 | else |
| 2735 | block_hash[hash] = file_buffer->next; |
| 2736 | } else { |
| 2737 | while(1) { |
| 2738 | file_buffer = queue_get(queue); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2739 | if(file_buffer->sequence == sequence) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2740 | break; |
| 2741 | push_buffer(file_buffer); |
| 2742 | } |
| 2743 | } |
| 2744 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2745 | sequence ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2746 | |
| 2747 | return file_buffer; |
| 2748 | } |
| 2749 | |
| 2750 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2751 | void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2752 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2753 | { |
| 2754 | file_count ++; |
| 2755 | *duplicate_file = FALSE; |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2756 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0, |
| 2757 | NULL, &empty_fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2761 | void write_file_frag_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2762 | int size, int *duplicate_file, struct file_buffer *file_buffer, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2763 | unsigned short checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2764 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2765 | struct file_info *dupl_ptr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2766 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2767 | unsigned int *block_listp = NULL; |
| 2768 | long long start = 0; |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2769 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2770 | dupl_ptr = duplicate(size, 0, &block_listp, &start, &fragment, |
| 2771 | file_buffer, 0, 0, checksum, TRUE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2772 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2773 | if(dupl_ptr) { |
| 2774 | *duplicate_file = FALSE; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2775 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2776 | dupl_ptr->fragment = fragment; |
| 2777 | } else |
| 2778 | *duplicate_file = TRUE; |
| 2779 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2780 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2781 | |
| 2782 | total_bytes += size; |
| 2783 | file_count ++; |
| 2784 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2785 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2786 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2787 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2788 | 0, NULL, fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2792 | void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, int size, |
| 2793 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2794 | { |
| 2795 | struct fragment *fragment; |
| 2796 | unsigned short checksum; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2797 | |
| 2798 | checksum = get_checksum_mem_buffer(file_buffer); |
| 2799 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2800 | if(pre_duplicate_frag(size, checksum)) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2801 | write_file_frag_dup(inode, dir_ent, size, duplicate_file, |
| 2802 | file_buffer, checksum); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2803 | return; |
| 2804 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2805 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2806 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2807 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2808 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2809 | |
| 2810 | if(duplicate_checking) |
| 2811 | add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, TRUE); |
| 2812 | |
| 2813 | total_bytes += size; |
| 2814 | file_count ++; |
| 2815 | |
| 2816 | *duplicate_file = FALSE; |
| 2817 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2818 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2819 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2820 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2821 | 0, NULL, fragment, NULL, 0); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2822 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2823 | return; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2827 | int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2828 | struct file_buffer *read_buffer, int *duplicate_file) |
| 2829 | { |
| 2830 | long long read_size, file_bytes, start; |
| 2831 | struct fragment *fragment; |
| 2832 | unsigned int *block_list = NULL; |
| 2833 | int block = 0, status; |
| 2834 | long long sparse = 0; |
| 2835 | struct file_buffer *fragment_buffer = NULL; |
| 2836 | |
| 2837 | *duplicate_file = FALSE; |
| 2838 | |
| 2839 | lock_fragments(); |
| 2840 | |
| 2841 | file_bytes = 0; |
| 2842 | start = bytes; |
| 2843 | while (1) { |
| 2844 | read_size = read_buffer->file_size; |
| 2845 | if(read_buffer->fragment && read_buffer->c_byte) |
| 2846 | fragment_buffer = read_buffer; |
| 2847 | else { |
| 2848 | block_list = realloc(block_list, (block + 1) * |
| 2849 | sizeof(unsigned int)); |
| 2850 | if(block_list == NULL) |
| 2851 | BAD_ERROR("Out of memory allocating block_list" |
| 2852 | "\n"); |
| 2853 | block_list[block ++] = read_buffer->c_byte; |
| 2854 | if(read_buffer->c_byte) { |
| 2855 | read_buffer->block = bytes; |
| 2856 | bytes += read_buffer->size; |
| 2857 | cache_rehash(read_buffer, read_buffer->block); |
| 2858 | file_bytes += read_buffer->size; |
| 2859 | queue_put(to_writer, read_buffer); |
| 2860 | } else { |
| 2861 | sparse += read_buffer->size; |
| 2862 | cache_block_put(read_buffer); |
| 2863 | } |
| 2864 | } |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2865 | inc_progress_bar(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2866 | |
| 2867 | if(read_size != -1) |
| 2868 | break; |
| 2869 | |
| 2870 | read_buffer = get_file_buffer(from_deflate); |
| 2871 | if(read_buffer->error) |
| 2872 | goto read_err; |
| 2873 | } |
| 2874 | |
| 2875 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2876 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2877 | cache_block_put(fragment_buffer); |
| 2878 | |
| 2879 | if(duplicate_checking) |
| 2880 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2881 | 0, 0, FALSE); |
| 2882 | file_count ++; |
| 2883 | total_bytes += read_size; |
| 2884 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2885 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2886 | block, block_list, fragment, NULL, sparse); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2887 | |
| 2888 | if(duplicate_checking == FALSE) |
| 2889 | free(block_list); |
| 2890 | |
| 2891 | return 0; |
| 2892 | |
| 2893 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2894 | dec_progress_bar(block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2895 | status = read_buffer->error; |
| 2896 | bytes = start; |
| 2897 | if(!block_device) { |
| 2898 | int res; |
| 2899 | |
| 2900 | queue_put(to_writer, NULL); |
| 2901 | if(queue_get(from_writer) != 0) |
| 2902 | EXIT_MKSQUASHFS(); |
| 2903 | res = ftruncate(fd, bytes); |
| 2904 | if(res != 0) |
| 2905 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2906 | strerror(errno)); |
| 2907 | } |
| 2908 | unlock_fragments(); |
| 2909 | free(block_list); |
| 2910 | cache_block_put(read_buffer); |
| 2911 | return status; |
| 2912 | } |
| 2913 | |
| 2914 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2915 | int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2916 | long long read_size, struct file_buffer *read_buffer, |
| 2917 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2918 | { |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 2919 | long long file_bytes, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2920 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2921 | unsigned int *block_list; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2922 | int block, status; |
| 2923 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2924 | long long sparse = 0; |
| 2925 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2926 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2927 | *duplicate_file = FALSE; |
| 2928 | |
plougher | 8713962 | 2010-12-16 05:19:30 +0000 | [diff] [blame] | 2929 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2930 | if(block_list == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2931 | BAD_ERROR("Out of memory allocating block_list\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2932 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2933 | lock_fragments(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2934 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2935 | file_bytes = 0; |
| 2936 | start = bytes; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2937 | for(block = 0; block < blocks;) { |
| 2938 | if(read_buffer->fragment && read_buffer->c_byte) { |
| 2939 | fragment_buffer = read_buffer; |
| 2940 | blocks = read_size >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2941 | } else { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2942 | block_list[block] = read_buffer->c_byte; |
| 2943 | if(read_buffer->c_byte) { |
| 2944 | read_buffer->block = bytes; |
| 2945 | bytes += read_buffer->size; |
| 2946 | cache_rehash(read_buffer, read_buffer->block); |
| 2947 | file_bytes += read_buffer->size; |
| 2948 | queue_put(to_writer, read_buffer); |
| 2949 | } else { |
| 2950 | sparse += read_buffer->size; |
| 2951 | cache_block_put(read_buffer); |
| 2952 | } |
| 2953 | } |
| 2954 | inc_progress_bar(); |
| 2955 | |
| 2956 | if(++block < blocks) { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2957 | read_buffer = get_file_buffer(from_deflate); |
| 2958 | if(read_buffer->error) |
| 2959 | goto read_err; |
| 2960 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2963 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2964 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2965 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2966 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2967 | if(duplicate_checking) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2968 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2969 | 0, 0, FALSE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2970 | file_count ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2971 | total_bytes += read_size; |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2972 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2973 | /* |
| 2974 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2975 | * a smaller block count on stat calls to sparse files. This is |
| 2976 | * to ensure intelligent applications like cp correctly handle the |
| 2977 | * file as a sparse file. If the file in the original filesystem isn't |
| 2978 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2979 | * report it as non-sparse on stat calls to preserve semantics |
| 2980 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2981 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2982 | sparse = 0; |
| 2983 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2984 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2985 | blocks, block_list, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2986 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2987 | if(duplicate_checking == FALSE) |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2988 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2989 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2990 | return 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2991 | |
| 2992 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2993 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2994 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2995 | bytes = start; |
| 2996 | if(!block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2997 | int res; |
| 2998 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2999 | queue_put(to_writer, NULL); |
| 3000 | if(queue_get(from_writer) != 0) |
| 3001 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3002 | res = ftruncate(fd, bytes); |
| 3003 | if(res != 0) |
| 3004 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 3005 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3006 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3007 | unlock_fragments(); |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 3008 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3009 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3010 | return status; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3014 | int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3015 | long long read_size, struct file_buffer *read_buffer, |
| 3016 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3017 | { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3018 | int block, thresh; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3019 | long long file_bytes, dup_start, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3020 | struct fragment *fragment; |
| 3021 | struct file_info *dupl_ptr; |
| 3022 | int blocks = (read_size + block_size - 1) >> block_log; |
| 3023 | unsigned int *block_list, *block_listp; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3024 | struct file_buffer **buffer_list; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 3025 | int status, num_locked_fragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3026 | long long sparse = 0; |
| 3027 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3028 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3029 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 3030 | if(block_list == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3031 | BAD_ERROR("Out of memory allocating block_list\n"); |
| 3032 | block_listp = block_list; |
| 3033 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3034 | buffer_list = malloc(blocks * sizeof(struct file_buffer *)); |
| 3035 | if(buffer_list == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3036 | BAD_ERROR("Out of memory allocating file block list\n"); |
| 3037 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 3038 | num_locked_fragments = lock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3039 | |
| 3040 | file_bytes = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3041 | start = dup_start = bytes; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3042 | thresh = blocks > (writer_buffer_size - num_locked_fragments) ? |
| 3043 | blocks - (writer_buffer_size - num_locked_fragments): 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3044 | |
| 3045 | for(block = 0; block < blocks;) { |
| 3046 | if(read_buffer->fragment && read_buffer->c_byte) { |
| 3047 | fragment_buffer = read_buffer; |
| 3048 | blocks = read_size >> block_log; |
| 3049 | } else { |
| 3050 | block_list[block] = read_buffer->c_byte; |
| 3051 | |
| 3052 | if(read_buffer->c_byte) { |
| 3053 | read_buffer->block = bytes; |
| 3054 | bytes += read_buffer->size; |
| 3055 | file_bytes += read_buffer->size; |
| 3056 | cache_rehash(read_buffer, read_buffer->block); |
| 3057 | if(block < thresh) { |
| 3058 | buffer_list[block] = NULL; |
| 3059 | queue_put(to_writer, read_buffer); |
| 3060 | } else |
| 3061 | buffer_list[block] = read_buffer; |
| 3062 | } else { |
| 3063 | buffer_list[block] = NULL; |
| 3064 | sparse += read_buffer->size; |
| 3065 | cache_block_put(read_buffer); |
| 3066 | } |
| 3067 | } |
| 3068 | inc_progress_bar(); |
| 3069 | |
| 3070 | if(++block < blocks) { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3071 | read_buffer = get_file_buffer(from_deflate); |
| 3072 | if(read_buffer->error) |
| 3073 | goto read_err; |
| 3074 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3077 | dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start, |
| 3078 | &fragment, fragment_buffer, blocks, 0, 0, FALSE); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3079 | |
| 3080 | if(dupl_ptr) { |
| 3081 | *duplicate_file = FALSE; |
| 3082 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3083 | if(buffer_list[block]) |
| 3084 | queue_put(to_writer, buffer_list[block]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 3085 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3086 | dupl_ptr->fragment = fragment; |
| 3087 | } else { |
| 3088 | *duplicate_file = TRUE; |
| 3089 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3090 | cache_block_put(buffer_list[block]); |
| 3091 | bytes = start; |
| 3092 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3093 | int res; |
| 3094 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3095 | queue_put(to_writer, NULL); |
| 3096 | if(queue_get(from_writer) != 0) |
| 3097 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3098 | res = ftruncate(fd, bytes); |
| 3099 | if(res != 0) |
| 3100 | BAD_ERROR("Failed to truncate dest file because" |
| 3101 | " %s\n", strerror(errno)); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3102 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3105 | unlock_fragments(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3106 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3107 | free(buffer_list); |
| 3108 | file_count ++; |
| 3109 | total_bytes += read_size; |
| 3110 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3111 | /* |
| 3112 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3113 | * a smaller block count on stat calls to sparse files. This is |
| 3114 | * to ensure intelligent applications like cp correctly handle the |
| 3115 | * file as a sparse file. If the file in the original filesystem isn't |
| 3116 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3117 | * report it as non-sparse on stat calls to preserve semantics |
| 3118 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3119 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 3120 | sparse = 0; |
| 3121 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 3122 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, |
| 3123 | dup_start, blocks, block_listp, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3124 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3125 | if(*duplicate_file == TRUE) |
| 3126 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3127 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3128 | return 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3129 | |
| 3130 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 3131 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3132 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3133 | bytes = start; |
| 3134 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3135 | int res; |
| 3136 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3137 | queue_put(to_writer, NULL); |
| 3138 | if(queue_get(from_writer) != 0) |
| 3139 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3140 | res = ftruncate(fd, bytes); |
| 3141 | if(res != 0) |
| 3142 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 3143 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3144 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3145 | unlock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3146 | for(blocks = thresh; blocks < block; blocks ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3147 | cache_block_put(buffer_list[blocks]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3148 | free(buffer_list); |
| 3149 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3150 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3151 | return status; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3155 | void write_file(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 3156 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3157 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3158 | int status; |
| 3159 | struct file_buffer *read_buffer; |
| 3160 | long long read_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3161 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3162 | again: |
| 3163 | read_buffer = get_file_buffer(from_deflate); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3164 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 3165 | status = read_buffer->error; |
| 3166 | if(status) { |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3167 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3168 | goto file_err; |
| 3169 | } |
| 3170 | |
| 3171 | read_size = read_buffer->file_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3172 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3173 | if(read_size == -1) |
| 3174 | status = write_file_process(inode, dir_ent, read_buffer, |
| 3175 | duplicate_file); |
| 3176 | else if(read_size == 0) { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3177 | write_file_empty(inode, dir_ent, duplicate_file); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3178 | cache_block_put(read_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3179 | } else if(read_buffer->fragment && read_buffer->c_byte) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3180 | write_file_frag(inode, dir_ent, read_size, read_buffer, |
| 3181 | duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3182 | else if(pre_duplicate(read_size)) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3183 | status = write_file_blocks_dup(inode, dir_ent, read_size, |
| 3184 | read_buffer, duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3185 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3186 | status = write_file_blocks(inode, dir_ent, read_size, |
| 3187 | read_buffer, duplicate_file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3188 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3189 | file_err: |
| 3190 | if(status == 2) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3191 | ERROR("File %s changed size while reading filesystem, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3192 | "attempting to re-read\n", pathname(dir_ent)); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3193 | goto again; |
| 3194 | } else if(status == 1) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3195 | ERROR("Failed to read file %s, creating empty file\n", |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3196 | pathname(dir_ent)); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3197 | write_file_empty(inode, dir_ent, duplicate_file); |
| 3198 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 3202 | #define BUFF_SIZE 512 |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3203 | char *name; |
| 3204 | char *basename_r(); |
| 3205 | |
| 3206 | char *getbase(char *pathname) |
| 3207 | { |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 3208 | static char *b_buffer = NULL; |
| 3209 | static int b_size = BUFF_SIZE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3210 | char *result; |
| 3211 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 3212 | if(b_buffer == NULL) { |
| 3213 | b_buffer = malloc(b_size); |
| 3214 | if(b_buffer == NULL) |
| 3215 | BAD_ERROR("Malloc failed in getbase\n"); |
| 3216 | } |
| 3217 | |
| 3218 | while(1) { |
| 3219 | if(*pathname != '/') { |
| 3220 | result = getcwd(b_buffer, b_size); |
| 3221 | if(result == NULL && errno != ERANGE) |
| 3222 | BAD_ERROR("Getcwd failed in getbase\n"); |
| 3223 | |
| 3224 | /* enough room for pathname + "/" + '\0' terminator? */ |
| 3225 | if(result && strlen(pathname) + 2 <= |
| 3226 | b_size - strlen(b_buffer)) { |
| 3227 | strcat(strcat(b_buffer, "/"), pathname); |
| 3228 | break; |
| 3229 | } |
| 3230 | } else if(strlen(pathname) < b_size) { |
| 3231 | strcpy(b_buffer, pathname); |
| 3232 | break; |
| 3233 | } |
| 3234 | |
| 3235 | /* Buffer not large enough, realloc and try again */ |
| 3236 | b_buffer = realloc(b_buffer, b_size += BUFF_SIZE); |
| 3237 | if(b_buffer == NULL) |
| 3238 | BAD_ERROR("Realloc failed in getbase\n"); |
| 3239 | } |
| 3240 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3241 | name = b_buffer; |
| 3242 | if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0)) |
| 3243 | return NULL; |
| 3244 | else |
| 3245 | return result; |
| 3246 | } |
| 3247 | |
| 3248 | |
| 3249 | char *basename_r() |
| 3250 | { |
| 3251 | char *s; |
| 3252 | char *p; |
| 3253 | int n = 1; |
| 3254 | |
| 3255 | for(;;) { |
| 3256 | s = name; |
| 3257 | if(*name == '\0') |
| 3258 | return NULL; |
| 3259 | if(*name != '/') { |
| 3260 | while(*name != '\0' && *name != '/') name++; |
| 3261 | n = name - s; |
| 3262 | } |
| 3263 | while(*name == '/') name++; |
| 3264 | if(strncmp(s, ".", n) == 0) |
| 3265 | continue; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3266 | if((*name == '\0') || (strncmp(s, "..", n) == 0) || |
| 3267 | ((p = basename_r()) == NULL)) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3268 | s[n] = '\0'; |
| 3269 | return s; |
| 3270 | } |
| 3271 | if(strcmp(p, "..") == 0) |
| 3272 | continue; |
| 3273 | return p; |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3278 | struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3279 | { |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3280 | int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino); |
| 3281 | struct inode_info *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3282 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3283 | /* |
| 3284 | * Look-up inode in hash table, if it already exists we have a |
| 3285 | * hard-link, so increment the nlink count and return it. |
| 3286 | * Don't do the look-up for directories because we don't hard-link |
| 3287 | * directories. |
| 3288 | */ |
| 3289 | if ((buf->st_mode & S_IFMT) != S_IFDIR) { |
| 3290 | for(inode = inode_info[ino_hash]; inode; inode = inode->next) { |
| 3291 | if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) { |
| 3292 | inode->nlink ++; |
| 3293 | return inode; |
| 3294 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3295 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
plougher | 288f685 | 2010-12-16 05:22:55 +0000 | [diff] [blame] | 3298 | inode = malloc(sizeof(struct inode_info)); |
| 3299 | if(inode == NULL) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3300 | BAD_ERROR("Out of memory in inode hash table entry allocation" |
| 3301 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3302 | |
| 3303 | memcpy(&inode->buf, buf, sizeof(struct stat)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3304 | inode->read = FALSE; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3305 | inode->root_entry = FALSE; |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3306 | inode->pseudo_file = pseudo; |
| 3307 | inode->pseudo_id = id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3308 | inode->inode = SQUASHFS_INVALID_BLK; |
| 3309 | inode->nlink = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3310 | inode->inode_number = 0; |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3311 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 3312 | /* |
| 3313 | * Copy filesystem wide defaults into inode, these filesystem |
| 3314 | * wide defaults may be altered on an individual inode basis by |
| 3315 | * user specified actions |
| 3316 | * |
| 3317 | */ |
| 3318 | inode->no_fragments = no_fragments; |
| 3319 | inode->always_use_fragments = always_use_fragments; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 3320 | inode->noD = noD; |
| 3321 | inode->noF = noF; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 3322 | |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3323 | if((buf->st_mode & S_IFMT) == S_IFREG) |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 3324 | progress_bar_size((buf->st_size + block_size - 1) >> block_log); |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3325 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3326 | inode->next = inode_info[ino_hash]; |
| 3327 | inode_info[ino_hash] = inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3328 | |
| 3329 | return inode; |
| 3330 | } |
| 3331 | |
| 3332 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3333 | inline struct inode_info *lookup_inode(struct stat *buf) |
| 3334 | { |
| 3335 | return lookup_inode2(buf, 0, 0); |
| 3336 | } |
| 3337 | |
| 3338 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3339 | inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this) |
| 3340 | { |
| 3341 | if (inode->inode_number == 0) |
| 3342 | inode->inode_number = use_this ? : inode_no ++; |
| 3343 | } |
| 3344 | |
| 3345 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3346 | inline struct dir_ent *create_dir_entry(char *name, char *source_name, |
| 3347 | char *nonstandard_pathname, struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3348 | { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3349 | struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent)); |
| 3350 | if(dir_ent == NULL) |
| 3351 | BAD_ERROR("Out of memory in linux_opendir\n"); |
| 3352 | |
| 3353 | dir_ent->name = name; |
| 3354 | dir_ent->source_name = source_name; |
| 3355 | dir_ent->nonstandard_pathname = nonstandard_pathname; |
| 3356 | dir_ent->our_dir = dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3357 | dir_ent->next = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3358 | |
| 3359 | return dir_ent; |
| 3360 | } |
| 3361 | |
| 3362 | |
| 3363 | inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir, |
| 3364 | struct inode_info *inode_info) |
| 3365 | { |
| 3366 | struct dir_info *dir = dir_ent->our_dir; |
| 3367 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3368 | if(sub_dir) |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3369 | sub_dir->dir_ent = dir_ent; |
| 3370 | dir_ent->inode = inode_info; |
| 3371 | dir_ent->dir = sub_dir; |
| 3372 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3373 | dir_ent->next = dir->list; |
| 3374 | dir->list = dir_ent; |
| 3375 | dir->count++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
| 3378 | |
| 3379 | inline void add_dir_entry2(char *name, char *source_name, |
| 3380 | char *nonstandard_pathname, struct dir_info *sub_dir, |
| 3381 | struct inode_info *inode_info, struct dir_info *dir) |
| 3382 | { |
| 3383 | struct dir_ent *dir_ent = create_dir_entry(name, source_name, |
| 3384 | nonstandard_pathname, dir); |
| 3385 | |
| 3386 | |
| 3387 | add_dir_entry(dir_ent, sub_dir, inode_info); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3391 | inline void free_dir_entry(struct dir_ent *dir_ent) |
| 3392 | { |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3393 | if(dir_ent->name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3394 | free(dir_ent->name); |
| 3395 | |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3396 | if(dir_ent->source_name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3397 | free(dir_ent->source_name); |
| 3398 | |
| 3399 | free(dir_ent); |
| 3400 | } |
| 3401 | |
| 3402 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3403 | inline void add_excluded(struct dir_info *dir) |
| 3404 | { |
| 3405 | dir->excluded ++; |
| 3406 | } |
| 3407 | |
| 3408 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3409 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3410 | void dir_scan(squashfs_inode *inode, char *pathname, |
Phillip Lougher | dc587a0 | 2012-11-14 05:30:27 +0000 | [diff] [blame] | 3411 | struct dir_ent *(_readdir)(struct dir_info *), struct pseudo *pseudo) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3412 | { |
| 3413 | struct stat buf; |
| 3414 | struct dir_info *dir_info = dir_scan1(pathname, "", paths, _readdir, 1); |
| 3415 | struct dir_ent *dir_ent; |
| 3416 | |
| 3417 | if(dir_info == NULL) |
| 3418 | return; |
| 3419 | |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3420 | /* |
| 3421 | * Process most actions and any pseudo files |
| 3422 | */ |
| 3423 | if(actions() || pseudo) |
| 3424 | dir_scan2(dir_info, pseudo); |
| 3425 | |
Phillip Lougher | e92cb51 | 2012-11-15 02:22:28 +0000 | [diff] [blame] | 3426 | /* |
| 3427 | * Process move actions |
| 3428 | */ |
| 3429 | if(move_actions()) { |
| 3430 | dir_scan3(dir_info, dir_info); |
| 3431 | do_move_actions(); |
| 3432 | } |
| 3433 | |
Phillip Lougher | 71ae14e | 2012-11-15 02:27:50 +0000 | [diff] [blame] | 3434 | /* |
| 3435 | * Process empty actions |
| 3436 | */ |
| 3437 | if(empty_actions()) |
| 3438 | dir_scan4(dir_info); |
| 3439 | |
Phillip Lougher | eb69dad | 2012-11-15 03:34:02 +0000 | [diff] [blame] | 3440 | /* |
| 3441 | * Sort directories and compute the inode numbers |
| 3442 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3443 | dir_scan5(dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3444 | |
Phillip Lougher | d0fe1d5 | 2012-10-09 03:34:34 +0100 | [diff] [blame] | 3445 | dir_ent = create_dir_entry("", NULL, pathname, |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 3446 | scan1_opendir("", "", 0)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3447 | |
| 3448 | if(pathname[0] == '\0') { |
| 3449 | /* |
| 3450 | * dummy top level directory, if multiple sources specified on |
| 3451 | * command line |
| 3452 | */ |
| 3453 | memset(&buf, 0, sizeof(buf)); |
| 3454 | buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR; |
| 3455 | buf.st_uid = getuid(); |
| 3456 | buf.st_gid = getgid(); |
| 3457 | buf.st_mtime = time(NULL); |
| 3458 | buf.st_dev = 0; |
| 3459 | buf.st_ino = 0; |
| 3460 | dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0); |
| 3461 | } else { |
Phillip Lougher | 38a35ec | 2012-11-30 04:31:35 +0000 | [diff] [blame] | 3462 | if(lstat(pathname, &buf) == -1) |
| 3463 | /* source directory has disappeared? */ |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3464 | BAD_ERROR("Cannot stat source directory %s because %s\n", |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3465 | pathname, strerror(errno)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3466 | dir_ent->inode = lookup_inode(&buf); |
| 3467 | } |
| 3468 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3469 | alloc_inode_no(dir_ent->inode, root_inode_number); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3470 | dir_ent->dir = dir_info; |
| 3471 | dir_info->dir_ent = dir_ent; |
| 3472 | |
Andy Lutomirski | 3f31dcf | 2012-09-21 18:08:08 -0700 | [diff] [blame] | 3473 | eval_actions(dir_ent); |
| 3474 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3475 | if(sorted) { |
| 3476 | int res = generate_file_priorities(dir_info, 0, |
| 3477 | &dir_info->dir_ent->inode->buf); |
| 3478 | |
| 3479 | if(res == FALSE) |
| 3480 | BAD_ERROR("generate_file_priorities failed\n"); |
| 3481 | } |
| 3482 | queue_put(to_reader, dir_info); |
| 3483 | if(sorted) |
| 3484 | sort_files_and_write(dir_info); |
| 3485 | if(progress) |
| 3486 | enable_progress_bar(); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3487 | dir_scan6(inode, dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3488 | dir_ent->inode->inode = *inode; |
| 3489 | dir_ent->inode->type = SQUASHFS_DIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3493 | /* |
| 3494 | * dir_scan1 routines... |
Phillip Lougher | e8630be | 2012-11-11 02:24:24 +0000 | [diff] [blame] | 3495 | * These scan the source directories into memory for processing. |
| 3496 | * Exclude actions are processed here (in contrast to the other actions) |
| 3497 | * because they affect what is scanned. |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3498 | */ |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3499 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3500 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3501 | struct dir_info *dir; |
| 3502 | |
plougher | 6da792b | 2010-12-16 05:25:39 +0000 | [diff] [blame] | 3503 | dir = malloc(sizeof(struct dir_info)); |
| 3504 | if(dir == NULL) |
plougher | fbfdda7 | 2010-07-21 01:09:14 +0000 | [diff] [blame] | 3505 | BAD_ERROR("Out of memory in scan1_opendir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3506 | |
| 3507 | if(pathname[0] != '\0' && (dir->linuxdir = opendir(pathname)) == NULL) { |
| 3508 | free(dir); |
| 3509 | return NULL; |
| 3510 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3511 | dir->pathname = pathname; |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3512 | dir->subpath = subpath; |
Phillip Lougher | ded70fa | 2011-12-25 02:14:58 +0000 | [diff] [blame] | 3513 | dir->count = 0; |
| 3514 | dir->directory_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3515 | dir->dir_is_ldir = TRUE; |
| 3516 | dir->list = NULL; |
Phillip Lougher | 0b5a124 | 2011-12-25 03:22:42 +0000 | [diff] [blame] | 3517 | dir->depth = depth; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3518 | dir->excluded = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3519 | |
| 3520 | return dir; |
| 3521 | } |
| 3522 | |
| 3523 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3524 | struct dir_ent *scan1_encomp_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3525 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3526 | static int index = 0; |
| 3527 | |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3528 | if(dir->count < old_root_entries) { |
| 3529 | int i; |
| 3530 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3531 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3532 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3533 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3534 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3535 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3536 | } |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3537 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3538 | |
| 3539 | while(index < source) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3540 | char *basename = NULL; |
| 3541 | char *dir_name = getbase(source_path[index]); |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3542 | int pass = 1, res; |
plougher | 89fe2c7 | 2010-12-16 05:31:34 +0000 | [diff] [blame] | 3543 | |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3544 | if(dir_name == NULL) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3545 | ERROR("Bad source directory %s - skipping ...\n", |
| 3546 | source_path[index]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3547 | index ++; |
| 3548 | continue; |
| 3549 | } |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3550 | dir_name = strdup(dir_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3551 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3552 | struct dir_ent *dir_ent = dir->list; |
| 3553 | |
| 3554 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3555 | dir_ent = dir_ent->next); |
| 3556 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3557 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3558 | ERROR("Source directory entry %s already used! - trying" |
| 3559 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3560 | if(pass == 1) |
| 3561 | basename = dir_name; |
| 3562 | else |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3563 | free(dir_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3564 | res = asprintf(&dir_name, "%s_%d", basename, pass++); |
| 3565 | if(res == -1) |
| 3566 | BAD_ERROR("asprintf failed in " |
| 3567 | "scan1_encomp_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3568 | ERROR("%s\n", dir_name); |
| 3569 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3570 | return create_dir_entry(dir_name, basename, |
| 3571 | source_path[index ++], dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3572 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3573 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3577 | struct dir_ent *scan1_single_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3578 | { |
| 3579 | struct dirent *d_name; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3580 | int i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3581 | |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3582 | if(dir->count < old_root_entries) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3583 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3584 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3585 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3586 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3587 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3588 | } |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3589 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3590 | |
| 3591 | if((d_name = readdir(dir->linuxdir)) != NULL) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3592 | char *basename = NULL; |
| 3593 | char *dir_name = strdup(d_name->d_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3594 | int pass = 1, res; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3595 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3596 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3597 | struct dir_ent *dir_ent = dir->list; |
| 3598 | |
| 3599 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3600 | dir_ent = dir_ent->next); |
| 3601 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3602 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3603 | ERROR("Source directory entry %s already used! - trying" |
| 3604 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3605 | if (pass == 1) |
| 3606 | basename = dir_name; |
| 3607 | else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3608 | free(dir_name); |
| 3609 | res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++); |
| 3610 | if(res == -1) |
| 3611 | BAD_ERROR("asprintf failed in " |
| 3612 | "scan1_single_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3613 | ERROR("%s\n", dir_name); |
| 3614 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3615 | return create_dir_entry(dir_name, basename, NULL, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3618 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3622 | struct dir_ent *scan1_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3623 | { |
plougher | 480d9bf | 2010-12-18 02:28:39 +0000 | [diff] [blame] | 3624 | struct dirent *d_name = readdir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3625 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3626 | return d_name ? |
| 3627 | create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) : |
| 3628 | NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3632 | void scan1_freedir(struct dir_info *dir) |
| 3633 | { |
plougher | fbed12b | 2006-02-07 12:45:53 +0000 | [diff] [blame] | 3634 | if(dir->pathname[0] != '\0') |
| 3635 | closedir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3639 | struct dir_info *dir_scan1(char *filename, char *subpath, |
| 3640 | struct pathnames *paths, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3641 | struct dir_ent *(_readdir)(struct dir_info *), int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3642 | { |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3643 | struct dir_info *dir = scan1_opendir(filename, subpath, depth); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3644 | struct dir_ent *dir_ent; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3645 | |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3646 | if(dir == NULL) { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3647 | ERROR("Could not open %s, skipping...\n", filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3648 | goto error; |
| 3649 | } |
| 3650 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3651 | while((dir_ent = _readdir(dir))) { |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3652 | struct dir_info *sub_dir; |
| 3653 | struct stat buf; |
| 3654 | struct pathnames *new; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3655 | char *filename = pathname(dir_ent); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3656 | char *subpath = subpathname(dir_ent); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3657 | char *dir_name = dir_ent->name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3658 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3659 | if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3660 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3661 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3662 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3663 | |
| 3664 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3665 | ERROR("Cannot stat dir/file %s because %s, ignoring\n", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3666 | filename, strerror(errno)); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3667 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3668 | continue; |
| 3669 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3670 | |
| 3671 | if((buf.st_mode & S_IFMT) != S_IFREG && |
Phillip Lougher | e4ff36a | 2012-11-19 01:34:47 +0000 | [diff] [blame] | 3672 | (buf.st_mode & S_IFMT) != S_IFDIR && |
| 3673 | (buf.st_mode & S_IFMT) != S_IFLNK && |
| 3674 | (buf.st_mode & S_IFMT) != S_IFCHR && |
| 3675 | (buf.st_mode & S_IFMT) != S_IFBLK && |
| 3676 | (buf.st_mode & S_IFMT) != S_IFIFO && |
| 3677 | (buf.st_mode & S_IFMT) != S_IFSOCK) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3678 | ERROR("File %s has unrecognised filetype %d, ignoring" |
| 3679 | "\n", filename, buf.st_mode & S_IFMT); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3680 | free_dir_entry(dir_ent); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3681 | continue; |
| 3682 | } |
| 3683 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3684 | if((old_exclude && old_excluded(filename, &buf)) || |
| 3685 | excluded(paths, dir_name, &new) || |
Phillip Lougher | b4f4b0d | 2012-02-20 02:30:26 +0000 | [diff] [blame] | 3686 | eval_exclude_actions(dir_name, filename, subpath, |
| 3687 | &buf, depth)) { |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3688 | add_excluded(dir); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3689 | free_dir_entry(dir_ent); |
Phillip Lougher | 10d8de0 | 2011-08-29 00:14:48 +0100 | [diff] [blame] | 3690 | continue; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3691 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3692 | |
| 3693 | if((buf.st_mode & S_IFMT) == S_IFDIR) { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3694 | filename = strdup(filename); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3695 | subpath = strdup(subpath); |
| 3696 | sub_dir = dir_scan1(filename, subpath, new, |
| 3697 | scan1_readdir, depth + 1); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3698 | if(sub_dir == NULL) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3699 | free_dir_entry(dir_ent); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3700 | free(filename); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3701 | free(subpath); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3702 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3703 | } |
Phillip Lougher | 6a78a2d | 2011-12-28 03:54:19 +0000 | [diff] [blame] | 3704 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3705 | dir->directory_count ++; |
| 3706 | } else |
| 3707 | sub_dir = NULL; |
| 3708 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3709 | add_dir_entry(dir_ent, sub_dir, lookup_inode(&buf)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | scan1_freedir(dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3713 | |
| 3714 | error: |
| 3715 | return dir; |
| 3716 | } |
| 3717 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3718 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3719 | /* |
| 3720 | * dir_scan2 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3721 | * This processes most actions and any pseudo files |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3722 | */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3723 | 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] | 3724 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3725 | if (dir_ent == NULL) |
| 3726 | dir_ent = dir->list; |
| 3727 | else |
| 3728 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3729 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3730 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next); |
| 3731 | |
| 3732 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | |
| 3736 | struct dir_ent *scan2_lookup(struct dir_info *dir, char *name) |
| 3737 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3738 | struct dir_ent *dir_ent = dir->list; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3739 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3740 | for(; dir_ent && strcmp(dir_ent->name, name) != 0; |
| 3741 | dir_ent = dir_ent->next); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3742 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3743 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3747 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3748 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3749 | struct dir_ent *dir_ent = NULL; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3750 | struct pseudo_entry *pseudo_ent; |
| 3751 | struct stat buf; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3752 | static int pseudo_ino = 1; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3753 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3754 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3755 | struct inode_info *inode_info = dir_ent->inode; |
| 3756 | struct stat *buf = &inode_info->buf; |
| 3757 | char *name = dir_ent->name; |
| 3758 | |
Phillip Lougher | 89d757c | 2011-09-20 00:33:19 +0100 | [diff] [blame] | 3759 | eval_actions(dir_ent); |
| 3760 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3761 | if((buf->st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3762 | dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) { |
| 3766 | dir_ent = scan2_lookup(dir, pseudo_ent->name); |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3767 | if(pseudo_ent->dev->type == 'm') { |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3768 | struct stat *buf; |
| 3769 | if(dir_ent == NULL) { |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3770 | ERROR("Pseudo modify file \"%s\" does not exist " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3771 | "in source filesystem. Ignoring.\n", |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3772 | pseudo_ent->pathname); |
| 3773 | continue; |
| 3774 | } |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3775 | if(dir_ent->inode->root_entry) { |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3776 | ERROR("Pseudo modify file \"%s\" is a pre-existing" |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3777 | " file in the filesystem being appended" |
| 3778 | " to. It cannot be modified. " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3779 | "Ignoring.\n", pseudo_ent->pathname); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3780 | continue; |
| 3781 | } |
| 3782 | buf = &dir_ent->inode->buf; |
| 3783 | buf->st_mode = (buf->st_mode & S_IFMT) | |
| 3784 | pseudo_ent->dev->mode; |
| 3785 | buf->st_uid = pseudo_ent->dev->uid; |
| 3786 | buf->st_gid = pseudo_ent->dev->gid; |
| 3787 | continue; |
| 3788 | } |
| 3789 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3790 | if(dir_ent) { |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3791 | if(dir_ent->inode->root_entry) |
| 3792 | ERROR("Pseudo file \"%s\" is a pre-existing" |
| 3793 | " file in the filesystem being appended" |
| 3794 | " to. Ignoring.\n", |
| 3795 | pseudo_ent->pathname); |
| 3796 | else |
| 3797 | ERROR("Pseudo file \"%s\" exists in source " |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3798 | "filesystem \"%s\".\nIgnoring, " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3799 | "exclude it (-e/-ef) to override.\n", |
| 3800 | pseudo_ent->pathname, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3801 | pathname(dir_ent)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3802 | continue; |
| 3803 | } |
| 3804 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3805 | memset(&buf, 0, sizeof(buf)); |
| 3806 | buf.st_mode = pseudo_ent->dev->mode; |
| 3807 | buf.st_uid = pseudo_ent->dev->uid; |
| 3808 | buf.st_gid = pseudo_ent->dev->gid; |
| 3809 | buf.st_rdev = makedev(pseudo_ent->dev->major, |
| 3810 | pseudo_ent->dev->minor); |
plougher | 7e58f4d | 2009-04-05 12:06:19 +0000 | [diff] [blame] | 3811 | buf.st_mtime = time(NULL); |
plougher | 1a3fbf2 | 2009-04-05 12:04:16 +0000 | [diff] [blame] | 3812 | buf.st_ino = pseudo_ino ++; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3813 | |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3814 | if(pseudo_ent->dev->type == 'd') { |
| 3815 | struct dir_ent *dir_ent = |
| 3816 | create_dir_entry(pseudo_ent->name, NULL, |
| 3817 | pseudo_ent->pathname, dir); |
| 3818 | char *subpath = strdup(subpathname(dir_ent)); |
| 3819 | struct dir_info *sub_dir = scan1_opendir("", subpath, |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3820 | dir->depth + 1); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3821 | if(sub_dir == NULL) { |
| 3822 | ERROR("Could not create pseudo directory \"%s\"" |
| 3823 | ", skipping...\n", |
| 3824 | pseudo_ent->pathname); |
| 3825 | free(subpath); |
| 3826 | pseudo_ino --; |
| 3827 | continue; |
| 3828 | } |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3829 | dir_scan2(sub_dir, pseudo_ent->pseudo); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3830 | dir->directory_count ++; |
| 3831 | add_dir_entry(dir_ent, sub_dir, |
| 3832 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0)); |
| 3833 | } else if(pseudo_ent->dev->type == 'f') { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3834 | #ifdef USE_TMP_FILE |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3835 | struct stat buf2; |
| 3836 | int res = stat(pseudo_ent->dev->filename, &buf2); |
| 3837 | if(res == -1) { |
| 3838 | ERROR("Stat on pseudo file \"%s\" failed, " |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3839 | "skipping...\n", pseudo_ent->pathname); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3840 | pseudo_ino --; |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3841 | continue; |
| 3842 | } |
| 3843 | buf.st_size = buf2.st_size; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3844 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3845 | pseudo_ent->dev->filename, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3846 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3847 | #else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3848 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3849 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3850 | lookup_inode2(&buf, PSEUDO_FILE_PROCESS, |
| 3851 | pseudo_ent->dev->pseudo_id), dir); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3852 | #endif |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3853 | } else { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3854 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3855 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3856 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3857 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3858 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
| 3861 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3862 | /* |
| 3863 | * dir_scan3 routines... |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3864 | * This processes the move action |
| 3865 | */ |
| 3866 | void dir_scan3(struct dir_info *root, struct dir_info *dir) |
| 3867 | { |
| 3868 | struct dir_ent *dir_ent = NULL; |
| 3869 | |
| 3870 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
| 3871 | |
| 3872 | eval_move_actions(root, dir_ent); |
| 3873 | |
| 3874 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
| 3875 | dir_scan3(root, dir_ent->dir); |
| 3876 | } |
| 3877 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3878 | |
| 3879 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3880 | /* |
| 3881 | * dir_scan4 routines... |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3882 | * This processes the empty action. This action has to be processed after |
| 3883 | * all other actions because the previous exclude and move actions and the |
| 3884 | * pseudo actions affect whether a directory is empty |
| 3885 | */ |
| 3886 | void dir_scan4(struct dir_info *dir) |
| 3887 | { |
| 3888 | struct dir_ent *dir_ent = dir->list, *prev = NULL; |
| 3889 | |
| 3890 | while(dir_ent) { |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3891 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) { |
| 3892 | dir_scan4(dir_ent->dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3893 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3894 | if(eval_empty_actions(dir_ent)) { |
| 3895 | struct dir_ent *tmp = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3896 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3897 | /* |
| 3898 | * delete sub-directory, this is by definition |
| 3899 | * empty |
| 3900 | */ |
| 3901 | free(dir_ent->dir->pathname); |
| 3902 | free(dir_ent->dir->subpath); |
| 3903 | free(dir_ent->dir); |
| 3904 | |
| 3905 | /* remove dir_ent from list */ |
| 3906 | dir_ent = dir_ent->next; |
| 3907 | if(prev) |
| 3908 | prev->next = dir_ent; |
| 3909 | else |
| 3910 | dir->list = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3911 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3912 | /* free it */ |
| 3913 | free_dir_entry(tmp); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3914 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3915 | /* update counts */ |
| 3916 | dir->directory_count --; |
| 3917 | dir->count --; |
| 3918 | add_excluded(dir); |
| 3919 | continue; |
| 3920 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3921 | } |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3922 | |
| 3923 | prev = dir_ent; |
| 3924 | dir_ent = dir_ent->next; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | |
| 3929 | /* |
| 3930 | * dir_scan5 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3931 | * This sorts every directory and computes the inode numbers |
| 3932 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3933 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3934 | /* |
| 3935 | * Bottom up linked list merge sort. |
| 3936 | * |
| 3937 | * Qsort and other O(n log n) algorithms work well with arrays but not |
| 3938 | * linked lists. Merge sort another O(n log n) sort algorithm on the other hand |
| 3939 | * is not ideal for arrays (as it needs an additonal n storage locations |
| 3940 | * as sorting is not done in place), but it is ideal for linked lists because |
| 3941 | * it doesn't require any extra storage, |
| 3942 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3943 | void sort_directory(struct dir_info *dir) |
| 3944 | { |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3945 | struct dir_ent *cur, *l1, *l2, *next; |
| 3946 | int len1, len2, stride = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3947 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3948 | if(dir->count < 2) |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3949 | return; |
| 3950 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3951 | /* |
| 3952 | * We can consider our linked-list to be made up of stride length |
| 3953 | * sublists. Eacn iteration around this loop merges adjacent |
| 3954 | * stride length sublists into larger 2*stride sublists. We stop |
| 3955 | * when stride becomes equal to the entire list. |
| 3956 | * |
| 3957 | * Initially stride = 1 (by definition a sublist of 1 is sorted), and |
| 3958 | * these 1 element sublists are merged into 2 element sublists, which |
| 3959 | * are then merged into 4 element sublists and so on. |
| 3960 | */ |
| 3961 | do { |
| 3962 | l2 = dir->list; /* head of current linked list */ |
| 3963 | cur = NULL; /* empty output list */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3964 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3965 | /* |
| 3966 | * Iterate through the linked list, merging adjacent sublists. |
| 3967 | * On each interation l2 points to the next sublist pair to be |
| 3968 | * merged (if there's only one sublist left this is simply added |
| 3969 | * to the output list) |
| 3970 | */ |
| 3971 | while(l2) { |
| 3972 | l1 = l2; |
| 3973 | for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next); |
| 3974 | len2 = stride; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3975 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3976 | /* |
| 3977 | * l1 points to first sublist. |
| 3978 | * l2 points to second sublist. |
| 3979 | * Merge them onto the output list |
| 3980 | */ |
| 3981 | while(len1 && l2 && len2) { |
| 3982 | if(strcmp(l1->name, l2->name) <= 0) { |
| 3983 | next = l1; |
| 3984 | l1 = l1->next; |
| 3985 | len1 --; |
| 3986 | } else { |
| 3987 | next = l2; |
| 3988 | l2 = l2->next; |
| 3989 | len2 --; |
| 3990 | } |
| 3991 | |
| 3992 | if(cur) { |
| 3993 | cur->next = next; |
| 3994 | cur = next; |
| 3995 | } else |
| 3996 | dir->list = cur = next; |
| 3997 | } |
| 3998 | /* |
| 3999 | * One sublist is now empty, copy the other one onto the |
| 4000 | * output list |
| 4001 | */ |
| 4002 | for(; len1; len1 --, l1 = l1->next) { |
| 4003 | if(cur) { |
| 4004 | cur->next = l1; |
| 4005 | cur = l1; |
| 4006 | } else |
| 4007 | dir->list = cur = l1; |
| 4008 | } |
| 4009 | for(; l2 && len2; len2 --, l2 = l2->next) { |
| 4010 | if(cur) { |
| 4011 | cur->next = l2; |
| 4012 | cur = l2; |
| 4013 | } else |
| 4014 | dir->list = cur = l2; |
| 4015 | } |
| 4016 | } |
| 4017 | cur->next = NULL; |
| 4018 | stride = stride << 1; |
| 4019 | } while(stride < dir->count); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4020 | } |
| 4021 | |
| 4022 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4023 | void dir_scan5(struct dir_info *dir) |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4024 | { |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 4025 | struct dir_ent *dir_ent; |
| 4026 | unsigned int byte_count = 0; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4027 | |
| 4028 | sort_directory(dir); |
| 4029 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 4030 | for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) { |
| 4031 | byte_count += strlen(dir_ent->name) + |
| 4032 | sizeof(struct squashfs_dir_entry); |
| 4033 | |
| 4034 | if(dir_ent->inode->root_entry) |
| 4035 | continue; |
| 4036 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4037 | alloc_inode_no(dir_ent->inode, 0); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 4038 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4039 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | e69b588 | 2012-11-19 01:31:09 +0000 | [diff] [blame] | 4040 | dir_scan5(dir_ent->dir); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4041 | } |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 4042 | |
| 4043 | if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE)) |
| 4044 | dir->dir_is_ldir = FALSE; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | |
| 4048 | /* |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4049 | * dir_scan6 routines... |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4050 | * This generates the filesystem metadata and writes it out to the destination |
| 4051 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4052 | void scan6_init_dir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4053 | { |
| 4054 | dir->buff = malloc(SQUASHFS_METADATA_SIZE); |
| 4055 | if(dir->buff == NULL) { |
| 4056 | BAD_ERROR("Out of memory allocating directory buffer\n"); |
| 4057 | } |
| 4058 | |
| 4059 | dir->size = SQUASHFS_METADATA_SIZE; |
| 4060 | dir->p = dir->index_count_p = dir->buff; |
| 4061 | dir->entry_count = 256; |
| 4062 | dir->entry_count_p = NULL; |
| 4063 | dir->index = NULL; |
| 4064 | dir->i_count = dir->i_size = 0; |
| 4065 | } |
| 4066 | |
| 4067 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4068 | 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] | 4069 | struct dir_ent *dir_ent) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4070 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 4071 | if (dir_ent == NULL) |
| 4072 | dir_ent = dir_info->list; |
| 4073 | else |
| 4074 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4075 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 4076 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next) |
| 4077 | add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number, |
| 4078 | dir_ent->name, dir_ent->inode->type, dir); |
| 4079 | |
| 4080 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4084 | void scan6_freedir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4085 | { |
| 4086 | if(dir->index) |
| 4087 | free(dir->index); |
| 4088 | free(dir->buff); |
| 4089 | } |
| 4090 | |
| 4091 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4092 | void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4093 | { |
| 4094 | int squashfs_type; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4095 | int duplicate_file; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4096 | struct directory dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 4097 | struct dir_ent *dir_ent = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4098 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4099 | scan6_init_dir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4100 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4101 | while((dir_ent = scan6_readdir(&dir, dir_info, dir_ent)) != NULL) { |
Phillip Lougher | 0a67088 | 2012-10-05 04:09:13 +0100 | [diff] [blame] | 4102 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4103 | |
| 4104 | if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) { |
| 4105 | switch(buf->st_mode & S_IFMT) { |
| 4106 | case S_IFREG: |
| 4107 | squashfs_type = SQUASHFS_FILE_TYPE; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 4108 | write_file(inode, dir_ent, |
| 4109 | &duplicate_file); |
| 4110 | INFO("file %s, uncompressed size %lld " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4111 | "bytes %s\n", |
| 4112 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 4113 | (long long) buf->st_size, |
| 4114 | duplicate_file ? "DUPLICATE" : |
| 4115 | ""); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4116 | break; |
| 4117 | |
| 4118 | case S_IFDIR: |
| 4119 | squashfs_type = SQUASHFS_DIR_TYPE; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4120 | dir_scan6(inode, dir_ent->dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4121 | break; |
| 4122 | |
| 4123 | case S_IFLNK: |
| 4124 | squashfs_type = SQUASHFS_SYMLINK_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4125 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4126 | squashfs_type, 0, 0, 0, NULL, |
| 4127 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4128 | INFO("symbolic link %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4129 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4130 | sym_count ++; |
| 4131 | break; |
| 4132 | |
| 4133 | case S_IFCHR: |
| 4134 | squashfs_type = SQUASHFS_CHRDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4135 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4136 | squashfs_type, 0, 0, 0, NULL, |
| 4137 | NULL, NULL, 0); |
| 4138 | INFO("character device %s inode 0x%llx" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4139 | "\n", subpathname(dir_ent), |
| 4140 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4141 | dev_count ++; |
| 4142 | break; |
| 4143 | |
| 4144 | case S_IFBLK: |
| 4145 | squashfs_type = SQUASHFS_BLKDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4146 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4147 | squashfs_type, 0, 0, 0, NULL, |
| 4148 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4149 | INFO("block device %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4150 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4151 | dev_count ++; |
| 4152 | break; |
| 4153 | |
| 4154 | case S_IFIFO: |
| 4155 | squashfs_type = SQUASHFS_FIFO_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4156 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4157 | squashfs_type, 0, 0, 0, NULL, |
| 4158 | NULL, NULL, 0); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4159 | INFO("fifo %s inode 0x%llx\n", |
| 4160 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4161 | fifo_count ++; |
| 4162 | break; |
| 4163 | |
| 4164 | case S_IFSOCK: |
| 4165 | squashfs_type = SQUASHFS_SOCKET_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4166 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4167 | squashfs_type, 0, 0, 0, NULL, |
| 4168 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4169 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4170 | "0x%llx\n", |
| 4171 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4172 | sock_count ++; |
| 4173 | break; |
| 4174 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4175 | default: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4176 | BAD_ERROR("%s unrecognised file type, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 4177 | "mode is %x\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4178 | subpathname(dir_ent), |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4179 | buf->st_mode); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 4180 | } |
| 4181 | dir_ent->inode->inode = *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4182 | dir_ent->inode->type = squashfs_type; |
| 4183 | } else { |
| 4184 | *inode = dir_ent->inode->inode; |
| 4185 | squashfs_type = dir_ent->inode->type; |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4186 | switch(squashfs_type) { |
| 4187 | case SQUASHFS_FILE_TYPE: |
| 4188 | if(!sorted) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4189 | INFO("file %s, uncompressed " |
| 4190 | "size %lld bytes LINK" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4191 | "\n", |
| 4192 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 4193 | (long long) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4194 | buf->st_size); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4195 | break; |
| 4196 | case SQUASHFS_SYMLINK_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4197 | INFO("symbolic link %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4198 | "LINK\n", subpathname(dir_ent), |
| 4199 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4200 | break; |
| 4201 | case SQUASHFS_CHRDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4202 | INFO("character device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4203 | "LINK\n", subpathname(dir_ent), |
| 4204 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4205 | break; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4206 | case SQUASHFS_BLKDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4207 | INFO("block device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4208 | "LINK\n", subpathname(dir_ent), |
| 4209 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4210 | break; |
| 4211 | case SQUASHFS_FIFO_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4212 | INFO("fifo %s inode 0x%llx LINK\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4213 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4214 | break; |
| 4215 | case SQUASHFS_SOCKET_TYPE: |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4216 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4217 | "0x%llx LINK\n", |
| 4218 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4219 | break; |
| 4220 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
Phillip Lougher | 0366aec | 2012-10-05 04:06:04 +0100 | [diff] [blame] | 4223 | add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name, |
| 4224 | squashfs_type, &dir); |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 4225 | update_progress_bar(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4226 | } |
| 4227 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 4228 | write_dir(inode, dir_info, &dir); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4229 | INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent), |
| 4230 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4231 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4232 | scan6_freedir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | |
| 4236 | unsigned int slog(unsigned int block) |
| 4237 | { |
| 4238 | int i; |
| 4239 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4240 | for(i = 12; i <= 20; i++) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4241 | if(block == (1 << i)) |
| 4242 | return i; |
| 4243 | return 0; |
| 4244 | } |
| 4245 | |
| 4246 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4247 | int old_excluded(char *filename, struct stat *buf) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4248 | { |
| 4249 | int i; |
| 4250 | |
| 4251 | for(i = 0; i < exclude; i++) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4252 | if((exclude_paths[i].st_dev == buf->st_dev) && |
| 4253 | (exclude_paths[i].st_ino == buf->st_ino)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4254 | return TRUE; |
| 4255 | return FALSE; |
| 4256 | } |
| 4257 | |
| 4258 | |
| 4259 | #define ADD_ENTRY(buf) \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4260 | if(exclude % EXCLUDE_SIZE == 0) { \ |
| 4261 | exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \ |
| 4262 | * sizeof(struct exclude_info)); \ |
| 4263 | if(exclude_paths == NULL) \ |
| 4264 | BAD_ERROR("Out of memory in exclude dir/file table\n"); \ |
| 4265 | } \ |
| 4266 | exclude_paths[exclude].st_dev = buf.st_dev; \ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4267 | exclude_paths[exclude++].st_ino = buf.st_ino; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4268 | int old_add_exclude(char *path) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4269 | { |
| 4270 | int i; |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 4271 | char *filename; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4272 | struct stat buf; |
| 4273 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4274 | if(path[0] == '/' || strncmp(path, "./", 2) == 0 || |
| 4275 | strncmp(path, "../", 3) == 0) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4276 | if(lstat(path, &buf) == -1) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4277 | ERROR("Cannot stat exclude dir/file %s because %s, " |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 4278 | "ignoring\n", path, strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4279 | return TRUE; |
| 4280 | } |
| 4281 | ADD_ENTRY(buf); |
| 4282 | return TRUE; |
| 4283 | } |
| 4284 | |
| 4285 | for(i = 0; i < source; i++) { |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 4286 | int res = asprintf(&filename, "%s/%s", source_path[i], path); |
| 4287 | if(res == -1) |
| 4288 | BAD_ERROR("asprintf failed in old_add_exclude\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4289 | if(lstat(filename, &buf) == -1) { |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4290 | if(!(errno == ENOENT || errno == ENOTDIR)) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4291 | ERROR("Cannot stat exclude dir/file %s because " |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 4292 | "%s, ignoring\n", filename, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4293 | strerror(errno)); |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 4294 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4295 | continue; |
| 4296 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 4297 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4298 | ADD_ENTRY(buf); |
| 4299 | } |
| 4300 | return TRUE; |
| 4301 | } |
| 4302 | |
| 4303 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4304 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 4305 | int type) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4306 | { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4307 | old_root_entry = realloc(old_root_entry, |
| 4308 | sizeof(struct old_root_entry_info) * (old_root_entries + 1)); |
| 4309 | if(old_root_entry == NULL) |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4310 | BAD_ERROR("Out of memory in old root directory entries " |
| 4311 | "reallocation\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4312 | |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 4313 | old_root_entry[old_root_entries].name = strdup(name); |
| 4314 | old_root_entry[old_root_entries].inode.inode = inode; |
| 4315 | old_root_entry[old_root_entries].inode.inode_number = inode_number; |
| 4316 | old_root_entry[old_root_entries].inode.type = type; |
| 4317 | old_root_entry[old_root_entries++].inode.root_entry = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4321 | void initialise_threads(int readb_mbytes, int writeb_mbytes, |
| 4322 | int fragmentb_mbytes) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4323 | { |
| 4324 | int i; |
| 4325 | sigset_t sigmask, old_mask; |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4326 | int reader_buffer_size = readb_mbytes << (20 - block_log); |
| 4327 | int fragment_buffer_size = fragmentb_mbytes << (20 - block_log); |
| 4328 | |
| 4329 | /* |
| 4330 | * writer_buffer_size is global because it is needed in |
| 4331 | * write_file_blocks_dup() |
| 4332 | */ |
| 4333 | writer_buffer_size = writeb_mbytes << (20 - block_log); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4334 | |
| 4335 | sigemptyset(&sigmask); |
| 4336 | sigaddset(&sigmask, SIGINT); |
| 4337 | sigaddset(&sigmask, SIGQUIT); |
| 4338 | if(sigprocmask(SIG_BLOCK, &sigmask, &old_mask) == -1) |
| 4339 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4340 | |
| 4341 | signal(SIGUSR1, sigusr1_handler); |
| 4342 | |
| 4343 | if(processors == -1) { |
| 4344 | #ifndef linux |
| 4345 | int mib[2]; |
| 4346 | size_t len = sizeof(processors); |
| 4347 | |
| 4348 | mib[0] = CTL_HW; |
| 4349 | #ifdef HW_AVAILCPU |
| 4350 | mib[1] = HW_AVAILCPU; |
| 4351 | #else |
| 4352 | mib[1] = HW_NCPU; |
| 4353 | #endif |
| 4354 | |
| 4355 | if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4356 | ERROR("Failed to get number of available processors. " |
| 4357 | "Defaulting to 1\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4358 | processors = 1; |
| 4359 | } |
| 4360 | #else |
plougher | 9cc26b7 | 2010-08-17 01:05:55 +0000 | [diff] [blame] | 4361 | processors = sysconf(_SC_NPROCESSORS_ONLN); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4362 | #endif |
| 4363 | } |
| 4364 | |
plougher | f2d2a84 | 2010-12-18 02:43:48 +0000 | [diff] [blame] | 4365 | thread = malloc((2 + processors * 2) * sizeof(pthread_t)); |
| 4366 | if(thread == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4367 | BAD_ERROR("Out of memory allocating thread descriptors\n"); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4368 | deflator_thread = &thread[2]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4369 | frag_deflator_thread = &deflator_thread[processors]; |
| 4370 | |
| 4371 | to_reader = queue_init(1); |
| 4372 | from_reader = queue_init(reader_buffer_size); |
| 4373 | to_writer = queue_init(writer_buffer_size); |
| 4374 | from_writer = queue_init(1); |
| 4375 | from_deflate = queue_init(reader_buffer_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 4376 | to_frag = queue_init(fragment_buffer_size); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 4377 | reader_buffer = cache_init(block_size, reader_buffer_size); |
| 4378 | writer_buffer = cache_init(block_size, writer_buffer_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 4379 | fragment_buffer = cache_init(block_size, fragment_buffer_size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4380 | pthread_create(&thread[0], NULL, reader, NULL); |
| 4381 | pthread_create(&thread[1], NULL, writer, NULL); |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 4382 | init_progress_bar(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4383 | pthread_mutex_init(&fragment_mutex, NULL); |
| 4384 | pthread_cond_init(&fragment_waiting, NULL); |
| 4385 | |
| 4386 | for(i = 0; i < processors; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4387 | if(pthread_create(&deflator_thread[i], NULL, deflator, NULL) != |
| 4388 | 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4389 | BAD_ERROR("Failed to create thread\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4390 | if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator, |
| 4391 | NULL) != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4392 | BAD_ERROR("Failed to create thread\n"); |
| 4393 | } |
| 4394 | |
| 4395 | printf("Parallel mksquashfs: Using %d processor%s\n", processors, |
| 4396 | processors == 1 ? "" : "s"); |
| 4397 | |
| 4398 | if(sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) |
| 4399 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4400 | } |
| 4401 | |
| 4402 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4403 | long long write_inode_lookup_table() |
| 4404 | { |
| 4405 | int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4406 | void *it; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4407 | |
| 4408 | if(inode_count == sinode_count) |
| 4409 | goto skip_inode_hash_table; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4410 | |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4411 | it = realloc(inode_lookup_table, lookup_bytes); |
| 4412 | if(it == NULL) |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4413 | BAD_ERROR("Out of memory in write_inode_table\n"); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4414 | inode_lookup_table = it; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4415 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4416 | for(i = 0; i < INODE_HASH_SIZE; i ++) { |
| 4417 | struct inode_info *inode = inode_info[i]; |
| 4418 | |
| 4419 | for(inode = inode_info[i]; inode; inode = inode->next) { |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4420 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4421 | inode_number = get_inode_no(inode); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4422 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4423 | SQUASHFS_SWAP_LONG_LONGS(&inode->inode, |
| 4424 | &inode_lookup_table[inode_number - 1], 1); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4425 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4426 | } |
| 4427 | } |
| 4428 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4429 | skip_inode_hash_table: |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 4430 | return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL, |
| 4431 | noI); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4434 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4435 | char *get_component(char *target, char **targname) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4436 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4437 | char *start; |
| 4438 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4439 | while(*target == '/') |
rlougher | c4ebcf5 | 2007-11-08 17:52:49 +0000 | [diff] [blame] | 4440 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4441 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4442 | start = target; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4443 | while(*target != '/' && *target!= '\0') |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4444 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4445 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4446 | *targname = strndup(start, target - start); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4447 | |
| 4448 | return target; |
| 4449 | } |
| 4450 | |
| 4451 | |
| 4452 | void free_path(struct pathname *paths) |
| 4453 | { |
| 4454 | int i; |
| 4455 | |
| 4456 | for(i = 0; i < paths->names; i++) { |
| 4457 | if(paths->name[i].paths) |
| 4458 | free_path(paths->name[i].paths); |
| 4459 | free(paths->name[i].name); |
| 4460 | if(paths->name[i].preg) { |
| 4461 | regfree(paths->name[i].preg); |
| 4462 | free(paths->name[i].preg); |
| 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | free(paths); |
| 4467 | } |
| 4468 | |
| 4469 | |
| 4470 | struct pathname *add_path(struct pathname *paths, char *target, char *alltarget) |
| 4471 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4472 | char *targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4473 | int i, error; |
| 4474 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4475 | target = get_component(target, &targname); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4476 | |
| 4477 | if(paths == NULL) { |
plougher | dec6ef1 | 2010-12-18 02:47:53 +0000 | [diff] [blame] | 4478 | paths = malloc(sizeof(struct pathname)); |
| 4479 | if(paths == NULL) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4480 | BAD_ERROR("failed to allocate paths\n"); |
| 4481 | |
| 4482 | paths->names = 0; |
| 4483 | paths->name = NULL; |
| 4484 | } |
| 4485 | |
| 4486 | for(i = 0; i < paths->names; i++) |
| 4487 | if(strcmp(paths->name[i].name, targname) == 0) |
| 4488 | break; |
| 4489 | |
| 4490 | if(i == paths->names) { |
| 4491 | /* allocate new name entry */ |
| 4492 | paths->names ++; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4493 | paths->name = realloc(paths->name, (i + 1) * |
| 4494 | sizeof(struct path_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4495 | if(paths->name == NULL) |
| 4496 | BAD_ERROR("Out of memory in add path\n"); |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4497 | paths->name[i].name = targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4498 | paths->name[i].paths = NULL; |
| 4499 | if(use_regex) { |
| 4500 | paths->name[i].preg = malloc(sizeof(regex_t)); |
plougher | 5c60eab | 2010-07-21 01:12:19 +0000 | [diff] [blame] | 4501 | if(paths->name[i].preg == NULL) |
| 4502 | BAD_ERROR("Out of memory in add_path\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4503 | error = regcomp(paths->name[i].preg, targname, |
| 4504 | REG_EXTENDED|REG_NOSUB); |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4505 | if(error) { |
Phillip Lougher | 62859d2 | 2012-11-30 05:53:56 +0000 | [diff] [blame] | 4506 | char str[1024]; /* overflow safe */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4507 | |
| 4508 | regerror(error, paths->name[i].preg, str, 1024); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4509 | BAD_ERROR("invalid regex %s in export %s, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4510 | "because %s\n", targname, alltarget, |
| 4511 | str); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4512 | } |
| 4513 | } else |
| 4514 | paths->name[i].preg = NULL; |
| 4515 | |
| 4516 | if(target[0] == '\0') |
| 4517 | /* at leaf pathname component */ |
| 4518 | paths->name[i].paths = NULL; |
| 4519 | else |
| 4520 | /* recurse adding child components */ |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4521 | paths->name[i].paths = add_path(NULL, target, |
| 4522 | alltarget); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4523 | } else { |
| 4524 | /* existing matching entry */ |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4525 | free(targname); |
| 4526 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4527 | if(paths->name[i].paths == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4528 | /* No sub-directory which means this is the leaf |
| 4529 | * component of a pre-existing exclude which subsumes |
| 4530 | * the exclude currently being added, in which case stop |
| 4531 | * adding components */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4532 | } else if(target[0] == '\0') { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4533 | /* at leaf pathname component and child components exist |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4534 | * from more specific excludes, delete as they're |
| 4535 | * subsumed by this exclude */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4536 | free_path(paths->name[i].paths); |
| 4537 | paths->name[i].paths = NULL; |
| 4538 | } else |
| 4539 | /* recurse adding child components */ |
| 4540 | add_path(paths->name[i].paths, target, alltarget); |
| 4541 | } |
| 4542 | |
| 4543 | return paths; |
| 4544 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4545 | |
| 4546 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4547 | void add_exclude(char *target) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4548 | { |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4549 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4550 | if(target[0] == '/' || strncmp(target, "./", 2) == 0 || |
| 4551 | strncmp(target, "../", 3) == 0) |
| 4552 | BAD_ERROR("/, ./ and ../ prefixed excludes not supported with " |
| 4553 | "-wildcards or -regex options\n"); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4554 | else if(strncmp(target, "... ", 4) == 0) |
| 4555 | stickypath = add_path(stickypath, target + 4, target + 4); |
| 4556 | else |
| 4557 | path = add_path(path, target, target); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | |
| 4561 | void display_path(int depth, struct pathname *paths) |
| 4562 | { |
| 4563 | int i, n; |
| 4564 | |
| 4565 | if(paths == NULL) |
| 4566 | return; |
| 4567 | |
| 4568 | for(i = 0; i < paths->names; i++) { |
| 4569 | for(n = 0; n < depth; n++) |
| 4570 | printf("\t"); |
| 4571 | printf("%d: %s\n", depth, paths->name[i].name); |
| 4572 | display_path(depth + 1, paths->name[i].paths); |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | |
| 4577 | void display_path2(struct pathname *paths, char *string) |
| 4578 | { |
| 4579 | int i; |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4580 | char *path; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4581 | |
| 4582 | if(paths == NULL) { |
| 4583 | printf("%s\n", string); |
| 4584 | return; |
| 4585 | } |
| 4586 | |
| 4587 | for(i = 0; i < paths->names; i++) { |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4588 | int res = asprintf(&path, "%s/%s", string, paths->name[i].name); |
| 4589 | if(res == -1) |
| 4590 | BAD_ERROR("asprintf failed in display_path2\n"); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4591 | display_path2(paths->name[i].paths, path); |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4592 | free(path); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4597 | struct pathnames *init_subdir() |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4598 | { |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 4599 | struct pathnames *new = malloc(sizeof(struct pathnames)); |
plougher | d86ee82 | 2010-07-21 01:14:26 +0000 | [diff] [blame] | 4600 | if(new == NULL) |
| 4601 | BAD_ERROR("Out of memory in init_subdir\n"); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4602 | new->count = 0; |
| 4603 | return new; |
| 4604 | } |
| 4605 | |
| 4606 | |
| 4607 | struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path) |
| 4608 | { |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4609 | if(paths->count % PATHS_ALLOC_SIZE == 0) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4610 | paths = realloc(paths, sizeof(struct pathnames *) + |
| 4611 | (paths->count + PATHS_ALLOC_SIZE) * |
| 4612 | sizeof(struct pathname *)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4613 | if(paths == NULL) |
| 4614 | BAD_ERROR("Out of memory in add_subdir\n"); |
| 4615 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4616 | |
| 4617 | paths->path[paths->count++] = path; |
| 4618 | return paths; |
| 4619 | } |
| 4620 | |
| 4621 | |
| 4622 | void free_subdir(struct pathnames *paths) |
| 4623 | { |
| 4624 | free(paths); |
| 4625 | } |
| 4626 | |
| 4627 | |
| 4628 | int excluded(struct pathnames *paths, char *name, struct pathnames **new) |
| 4629 | { |
| 4630 | int i, n, res; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4631 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4632 | if(paths == NULL) { |
| 4633 | *new = NULL; |
| 4634 | return FALSE; |
| 4635 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4636 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4637 | |
| 4638 | *new = init_subdir(); |
plougher | 806581a | 2007-10-23 15:41:30 +0000 | [diff] [blame] | 4639 | if(stickypath) |
| 4640 | *new = add_subdir(*new, stickypath); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4641 | |
| 4642 | for(n = 0; n < paths->count; n++) { |
| 4643 | struct pathname *path = paths->path[n]; |
| 4644 | |
| 4645 | for(i = 0; i < path->names; i++) { |
| 4646 | int match = use_regex ? |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4647 | regexec(path->name[i].preg, name, (size_t) 0, |
| 4648 | NULL, 0) == 0 : |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4649 | fnmatch(path->name[i].name, name, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4650 | FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == |
| 4651 | 0; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4652 | |
| 4653 | if(match && path->name[i].paths == NULL) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4654 | /* match on a leaf component, any subdirectories |
| 4655 | * in the filesystem should be excluded */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4656 | res = TRUE; |
| 4657 | goto empty_set; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4660 | if(match) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4661 | /* match on a non-leaf component, add any |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4662 | * subdirectories to the new set of |
| 4663 | * subdirectories to scan for this name */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4664 | *new = add_subdir(*new, path->name[i].paths); |
| 4665 | } |
| 4666 | } |
| 4667 | |
| 4668 | if((*new)->count == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4669 | /* no matching names found, return empty new search set |
| 4670 | */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4671 | res = FALSE; |
| 4672 | goto empty_set; |
| 4673 | } |
| 4674 | |
| 4675 | /* one or more matches with sub-directories found (no leaf matches). |
| 4676 | * Return new set */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4677 | return FALSE; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4678 | |
| 4679 | empty_set: |
| 4680 | free_subdir(*new); |
| 4681 | *new = NULL; |
| 4682 | return res; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4686 | void process_exclude_file(char *argv) |
| 4687 | { |
| 4688 | FILE *fd; |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame^] | 4689 | char buffer[16385]; /* overflow safe */ |
| 4690 | char *filename; |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4691 | |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame^] | 4692 | fd = fopen(argv, "r"); |
| 4693 | if(fd == NULL) |
| 4694 | BAD_ERROR("Failed to open exclude file \"%s\" because %s\n", |
| 4695 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4696 | |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame^] | 4697 | while(fgets(filename = buffer, 16384, fd) != NULL) { |
| 4698 | int len = strlen(filename); |
| 4699 | |
| 4700 | if(len == 16384 && filename[len] != '\n') |
| 4701 | /* line too large */ |
| 4702 | BAD_ERROR("Line too long when reading " |
| 4703 | "exclude file \"%s\", larger than 16384 " |
| 4704 | "bytes\n", argv); |
| 4705 | |
| 4706 | /* |
| 4707 | * Remove '\n' terminator if it exists (the last line |
| 4708 | * in the file may not be '\n' terminated) |
| 4709 | */ |
| 4710 | if(len && filename[len - 1] == '\n') |
| 4711 | filename[len - 1] = '\0'; |
| 4712 | |
| 4713 | /* Skip any leading whitespace */ |
| 4714 | while(isspace(*filename)) |
| 4715 | filename ++; |
| 4716 | |
| 4717 | /* if comment line, skip */ |
| 4718 | if(*filename == '#') |
| 4719 | continue; |
| 4720 | |
| 4721 | /* |
| 4722 | * check for initial backslash, to accommodate |
| 4723 | * filenames with leading space or leading # character |
| 4724 | */ |
| 4725 | if(*filename == '\\') |
| 4726 | filename ++; |
| 4727 | |
| 4728 | /* if line is now empty after skipping characters, skip it */ |
| 4729 | if(*filename == '\0') |
| 4730 | continue; |
| 4731 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4732 | if(old_exclude) |
| 4733 | old_add_exclude(filename); |
| 4734 | else |
| 4735 | add_exclude(filename); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame^] | 4736 | } |
| 4737 | |
| 4738 | if(ferror(fd)) |
| 4739 | BAD_ERROR("Reading exclude file \"%s\" failed because %s\n", |
| 4740 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4741 | |
| 4742 | fclose(fd); |
| 4743 | } |
| 4744 | |
| 4745 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4746 | #define RECOVER_ID "Squashfs recovery file v1.0\n" |
| 4747 | #define RECOVER_ID_SIZE 28 |
| 4748 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4749 | void write_recovery_data(struct squashfs_super_block *sBlk) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4750 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4751 | int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4752 | pid_t pid = getpid(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4753 | char *metadata; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4754 | char header[] = RECOVER_ID; |
| 4755 | |
| 4756 | if(recover == FALSE) { |
| 4757 | printf("No recovery data option specified.\n"); |
plougher | eac1853 | 2007-10-29 05:26:06 +0000 | [diff] [blame] | 4758 | printf("Skipping saving recovery file.\n\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4759 | return; |
| 4760 | } |
| 4761 | |
plougher | 1b879bc | 2010-12-18 02:49:42 +0000 | [diff] [blame] | 4762 | metadata = malloc(bytes); |
| 4763 | if(metadata == NULL) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4764 | BAD_ERROR("Failed to alloc metadata buffer in " |
| 4765 | "write_recovery_data\n"); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4766 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4767 | res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata); |
| 4768 | if(res == 0) |
| 4769 | EXIT_MKSQUASHFS(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4770 | |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4771 | res = asprintf(&recovery_file, "squashfs_recovery_%s_%d", |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4772 | getbase(destination_file), pid); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4773 | if(res == -1) |
| 4774 | BAD_ERROR("Out of memory allocating recovery_file\n"); |
| 4775 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4776 | recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU); |
| 4777 | if(recoverfd == -1) |
| 4778 | BAD_ERROR("Failed to create recovery file, because %s. " |
| 4779 | "Aborting\n", strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4780 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4781 | if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4782 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4783 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4784 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4785 | if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4786 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4787 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4788 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4789 | if(write_bytes(recoverfd, metadata, bytes) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4790 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4791 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4792 | |
| 4793 | close(recoverfd); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4794 | free(metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4795 | |
| 4796 | printf("Recovery file \"%s\" written\n", recovery_file); |
| 4797 | printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4798 | printf("mksquashfs dummy %s -recover %s\n", destination_file, |
| 4799 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4800 | printf("to restore filesystem\n\n"); |
| 4801 | } |
| 4802 | |
| 4803 | |
| 4804 | void read_recovery_data(char *recovery_file, char *destination_file) |
| 4805 | { |
| 4806 | int fd, recoverfd, bytes; |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4807 | struct squashfs_super_block orig_sBlk, sBlk; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4808 | char *metadata; |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4809 | int res; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4810 | struct stat buf; |
| 4811 | char header[] = RECOVER_ID; |
| 4812 | char header2[RECOVER_ID_SIZE]; |
| 4813 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4814 | recoverfd = open(recovery_file, O_RDONLY); |
| 4815 | if(recoverfd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4816 | BAD_ERROR("Failed to open recovery file because %s\n", |
| 4817 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4818 | |
| 4819 | if(stat(destination_file, &buf) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4820 | BAD_ERROR("Failed to stat destination file, because %s\n", |
| 4821 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4822 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4823 | fd = open(destination_file, O_RDWR); |
| 4824 | if(fd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4825 | BAD_ERROR("Failed to open destination file because %s\n", |
| 4826 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4827 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4828 | res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE); |
| 4829 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4830 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4831 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4832 | if(res < RECOVER_ID_SIZE) |
| 4833 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4834 | if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 ) |
| 4835 | BAD_ERROR("Not a recovery file\n"); |
| 4836 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4837 | res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4838 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4839 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4840 | strerror(errno)); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4841 | if(res < sizeof(struct squashfs_super_block)) |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4842 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4843 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4844 | res = read_fs_bytes(fd, 0, sizeof(struct squashfs_super_block), &orig_sBlk); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4845 | if(res == 0) |
| 4846 | EXIT_MKSQUASHFS(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4847 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4848 | if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4, |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4849 | sizeof(struct squashfs_super_block) - 4) != 0) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4850 | BAD_ERROR("Recovery file and destination file do not seem to " |
| 4851 | "match\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4852 | |
| 4853 | bytes = sBlk.bytes_used - sBlk.inode_table_start; |
| 4854 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4855 | metadata = malloc(bytes); |
| 4856 | if(metadata == NULL) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4857 | BAD_ERROR("Failed to alloc metadata buffer in " |
| 4858 | "read_recovery_data\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4859 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4860 | res = read_bytes(recoverfd, metadata, bytes); |
| 4861 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4862 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4863 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4864 | if(res < bytes) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4865 | BAD_ERROR("Recovery file appears to be truncated\n"); |
| 4866 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4867 | write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4868 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 4869 | write_destination(fd, sBlk.inode_table_start, bytes, metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4870 | |
| 4871 | close(recoverfd); |
| 4872 | close(fd); |
| 4873 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4874 | printf("Successfully wrote recovery file \"%s\". Exiting\n", |
| 4875 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4876 | |
| 4877 | exit(0); |
| 4878 | } |
| 4879 | |
| 4880 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4881 | int multiply_overflow(int a, int multiplier) |
| 4882 | { |
| 4883 | return (INT_MAX / multiplier) < a; |
| 4884 | } |
| 4885 | |
| 4886 | |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4887 | int parse_number(char *start, int *res, int size) |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4888 | { |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4889 | char *end; |
| 4890 | long number = strtol(start, &end, 10); |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4891 | |
Phillip Lougher | f4f4838 | 2012-11-29 03:52:44 +0000 | [diff] [blame] | 4892 | /* |
| 4893 | * check for strtol underflow or overflow in conversion. |
| 4894 | * Note: strtol can validly return LONG_MIN and LONG_MAX |
| 4895 | * if the user entered these values, but, additional code |
| 4896 | * to distinguish this scenario is unnecessary, because for |
| 4897 | * our purposes LONG_MIN and LONG_MAX are too large anyway |
| 4898 | */ |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4899 | if(number == LONG_MIN || number == LONG_MAX) |
| 4900 | return 0; |
| 4901 | |
| 4902 | /* reject negative numbers as invalid */ |
| 4903 | if(number < 0) |
| 4904 | return 0; |
| 4905 | |
| 4906 | /* check if long result will overflow signed int */ |
| 4907 | if(number > INT_MAX) |
| 4908 | return 0; |
| 4909 | |
| 4910 | if(size) { |
Phillip Lougher | ed34569 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 4911 | /* |
| 4912 | * Check for multiplier and trailing junk. |
| 4913 | * But first check that a number exists before the |
| 4914 | * multiplier |
| 4915 | */ |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4916 | if(end == start) |
Phillip Lougher | ed34569 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 4917 | return 0; |
| 4918 | |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4919 | switch(end[0]) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4920 | case 'm': |
| 4921 | case 'M': |
| 4922 | if(multiply_overflow((int) number, 1048576)) |
| 4923 | return 0; |
| 4924 | number *= 1048576; |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4925 | |
Phillip Lougher | f968be8 | 2012-12-04 05:12:32 +0000 | [diff] [blame] | 4926 | if(end[1] != '\0') |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4927 | /* trailing junk after number */ |
| 4928 | return 0; |
| 4929 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4930 | break; |
| 4931 | case 'k': |
| 4932 | case 'K': |
| 4933 | if(multiply_overflow((int) number, 1024)) |
| 4934 | return 0; |
| 4935 | number *= 1024; |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4936 | |
Phillip Lougher | f968be8 | 2012-12-04 05:12:32 +0000 | [diff] [blame] | 4937 | if(end[1] != '\0') |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4938 | /* trailing junk after number */ |
| 4939 | return 0; |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4940 | case '\0': |
| 4941 | break; |
| 4942 | default: |
| 4943 | /* trailing junk after number */ |
| 4944 | return 0; |
| 4945 | } |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4946 | } else if(end[0] != '\0') |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4947 | /* trailing junk after number */ |
| 4948 | return 0; |
| 4949 | |
| 4950 | *res = number; |
| 4951 | return 1; |
| 4952 | } |
| 4953 | |
| 4954 | |
| 4955 | int parse_num(char *arg, int *res) |
| 4956 | { |
| 4957 | return parse_number(arg, res, 0); |
| 4958 | } |
| 4959 | |
| 4960 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4961 | #define VERSION() \ |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame^] | 4962 | printf("mksquashfs version 4.2-git (2012/12/20)\n");\ |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 4963 | printf("copyright (C) 2012 Phillip Lougher "\ |
Phillip Lougher | 83d42a3 | 2012-10-31 23:42:22 +0000 | [diff] [blame] | 4964 | "<phillip@squashfs.org.uk>\n\n"); \ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4965 | printf("This program is free software; you can redistribute it and/or"\ |
| 4966 | "\n");\ |
| 4967 | printf("modify it under the terms of the GNU General Public License"\ |
| 4968 | "\n");\ |
| 4969 | printf("as published by the Free Software Foundation; either version "\ |
| 4970 | "2,\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4971 | printf("or (at your option) any later version.\n\n");\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4972 | printf("This program is distributed in the hope that it will be "\ |
| 4973 | "useful,\n");\ |
| 4974 | printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\ |
| 4975 | "of\n");\ |
| 4976 | printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\ |
| 4977 | "\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4978 | printf("GNU General Public License for more details.\n"); |
| 4979 | int main(int argc, char *argv[]) |
| 4980 | { |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4981 | struct stat buf, source_buf; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 4982 | int res, i; |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4983 | struct squashfs_super_block sBlk; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4984 | char *b, *root_name = NULL; |
plougher | 6c65b03 | 2008-08-07 09:13:24 +0000 | [diff] [blame] | 4985 | int nopad = FALSE, keep_as_directory = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4986 | squashfs_inode inode; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4987 | int readb_mbytes = READER_BUFFER_DEFAULT, |
| 4988 | writeb_mbytes = WRITER_BUFFER_DEFAULT, |
| 4989 | fragmentb_mbytes = FRAGMENT_BUFFER_DEFAULT; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 4990 | int force_progress = FALSE; |
Phillip Lougher | dc587a0 | 2012-11-14 05:30:27 +0000 | [diff] [blame] | 4991 | struct pseudo *pseudo = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4992 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4993 | block_log = slog(block_size); |
| 4994 | if(argc > 1 && strcmp(argv[1], "-version") == 0) { |
| 4995 | VERSION(); |
| 4996 | exit(0); |
| 4997 | } |
| 4998 | for(i = 1; i < argc && argv[i][0] != '-'; i++); |
| 4999 | if(i < 3) |
| 5000 | goto printOptions; |
| 5001 | source_path = argv + 1; |
| 5002 | source = i - 2; |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 5003 | /* |
| 5004 | * lookup default compressor. Note the Makefile ensures the default |
| 5005 | * compressor has been built, and so we don't need to to check |
| 5006 | * for failure here |
| 5007 | */ |
plougher | 5c2adaf | 2010-11-22 01:00:15 +0000 | [diff] [blame] | 5008 | comp = lookup_compressor(COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5009 | for(; i < argc; i++) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 5010 | if(strcmp(argv[i], "-action") == 0 || |
| 5011 | strcmp(argv[i], "-a") ==0) { |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5012 | if(++i == argc) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 5013 | ERROR("%s: %s missing action\n", |
| 5014 | argv[0], argv[i - 1]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5015 | exit(1); |
| 5016 | } |
| 5017 | res = parse_action(argv[i]); |
| 5018 | if(res == 0) |
| 5019 | exit(1); |
| 5020 | |
| 5021 | } else if(strcmp(argv[i], "-comp") == 0) { |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 5022 | if(compressor_opts_parsed) { |
| 5023 | ERROR("%s: -comp must appear before -X options" |
| 5024 | "\n", argv[0]); |
| 5025 | exit(1); |
| 5026 | } |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 5027 | if(++i == argc) { |
| 5028 | ERROR("%s: -comp missing compression type\n", |
| 5029 | argv[0]); |
| 5030 | exit(1); |
| 5031 | } |
plougher | 5c2adaf | 2010-11-22 01:00:15 +0000 | [diff] [blame] | 5032 | comp = lookup_compressor(argv[i]); |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 5033 | if(!comp->supported) { |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 5034 | ERROR("%s: Compressor \"%s\" is not supported!" |
| 5035 | "\n", argv[0], argv[i]); |
| 5036 | ERROR("%s: Compressors available:\n", argv[0]); |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 5037 | display_compressors("", COMP_DEFAULT); |
| 5038 | exit(1); |
| 5039 | } |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 5040 | |
| 5041 | } else if(strncmp(argv[i], "-X", 2) == 0) { |
| 5042 | int args = compressor_options(comp, argv + i, argc - i); |
plougher | d886567 | 2010-11-27 05:05:49 +0000 | [diff] [blame] | 5043 | if(args < 0) { |
| 5044 | if(args == -1) { |
| 5045 | ERROR("%s: Unrecognised compressor" |
| 5046 | " option %s\n", argv[0], |
| 5047 | argv[i]); |
| 5048 | ERROR("%s: Did you forget to specify" |
| 5049 | " -comp, or specify it after" |
| 5050 | " the compressor specific" |
| 5051 | " option?\n", argv[0]); |
| 5052 | } |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 5053 | exit(1); |
| 5054 | } |
| 5055 | i += args; |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 5056 | compressor_opts_parsed = 1; |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 5057 | |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 5058 | } else if(strcmp(argv[i], "-pf") == 0) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5059 | if(++i == argc) { |
| 5060 | ERROR("%s: -pf missing filename\n", argv[0]); |
| 5061 | exit(1); |
| 5062 | } |
plougher | 620b717 | 2009-09-10 04:17:48 +0000 | [diff] [blame] | 5063 | if(read_pseudo_file(&pseudo, argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5064 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5065 | } else if(strcmp(argv[i], "-p") == 0) { |
| 5066 | if(++i == argc) { |
| 5067 | ERROR("%s: -p missing pseudo file definition\n", |
| 5068 | argv[0]); |
| 5069 | exit(1); |
| 5070 | } |
plougher | 620b717 | 2009-09-10 04:17:48 +0000 | [diff] [blame] | 5071 | if(read_pseudo_def(&pseudo, argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5072 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5073 | } else if(strcmp(argv[i], "-recover") == 0) { |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5074 | if(++i == argc) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5075 | ERROR("%s: -recover missing recovery file\n", |
| 5076 | argv[0]); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5077 | exit(1); |
| 5078 | } |
| 5079 | read_recovery_data(argv[i], argv[source + 1]); |
| 5080 | } else if(strcmp(argv[i], "-no-recovery") == 0) |
| 5081 | recover = FALSE; |
| 5082 | else if(strcmp(argv[i], "-wildcards") == 0) { |
plougher | 934a9ed | 2007-10-19 00:21:10 +0000 | [diff] [blame] | 5083 | old_exclude = FALSE; |
| 5084 | use_regex = FALSE; |
| 5085 | } else if(strcmp(argv[i], "-regex") == 0) { |
| 5086 | old_exclude = FALSE; |
| 5087 | use_regex = TRUE; |
| 5088 | } else if(strcmp(argv[i], "-no-sparse") == 0) |
plougher | 1f54edc | 2007-08-12 23:13:36 +0000 | [diff] [blame] | 5089 | sparse_files = FALSE; |
| 5090 | else if(strcmp(argv[i], "-no-progress") == 0) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 5091 | progress = FALSE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5092 | else if(strcmp(argv[i], "-progress") == 0) |
| 5093 | force_progress = TRUE; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 5094 | else if(strcmp(argv[i], "-no-exports") == 0) |
| 5095 | exportable = FALSE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5096 | else if(strcmp(argv[i], "-processors") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5097 | if((++i == argc) || !parse_num(argv[i], &processors)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5098 | ERROR("%s: -processors missing or invalid " |
| 5099 | "processor number\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5100 | exit(1); |
| 5101 | } |
| 5102 | if(processors < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5103 | ERROR("%s: -processors should be 1 or larger\n", |
| 5104 | argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5105 | exit(1); |
| 5106 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5107 | } else if(strcmp(argv[i], "-read-queue") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5108 | if((++i == argc) || |
| 5109 | !parse_num(argv[i], &readb_mbytes)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5110 | ERROR("%s: -read-queue missing or invalid " |
| 5111 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5112 | exit(1); |
| 5113 | } |
| 5114 | if(readb_mbytes < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5115 | ERROR("%s: -read-queue should be 1 megabyte or " |
| 5116 | "larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5117 | exit(1); |
| 5118 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5119 | } else if(strcmp(argv[i], "-write-queue") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5120 | if((++i == argc) || |
| 5121 | !parse_num(argv[i], &writeb_mbytes)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5122 | ERROR("%s: -write-queue missing or invalid " |
| 5123 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5124 | exit(1); |
| 5125 | } |
| 5126 | if(writeb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5127 | ERROR("%s: -write-queue should be 1 megabyte " |
| 5128 | "or larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5129 | exit(1); |
| 5130 | } |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5131 | } else if(strcmp(argv[i], "-fragment-queue") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5132 | if((++i == argc) || |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5133 | !parse_num(argv[i], |
| 5134 | &fragmentb_mbytes)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5135 | ERROR("%s: -fragment-queue missing or invalid " |
| 5136 | "queue size\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5137 | exit(1); |
| 5138 | } |
| 5139 | if(fragmentb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5140 | ERROR("%s: -fragment-queue should be 1 " |
| 5141 | "megabyte or larger\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5142 | exit(1); |
| 5143 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5144 | } else if(strcmp(argv[i], "-b") == 0) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5145 | if(++i == argc) { |
| 5146 | ERROR("%s: -b missing block size\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5147 | exit(1); |
| 5148 | } |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5149 | if(!parse_number(argv[i], &block_size, 1)) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5150 | ERROR("%s: -b invalid block size\n", argv[0]); |
| 5151 | exit(1); |
| 5152 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5153 | if((block_log = slog(block_size)) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5154 | ERROR("%s: -b block size not power of two or " |
| 5155 | "not between 4096 and 1Mbyte\n", |
| 5156 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5157 | exit(1); |
| 5158 | } |
| 5159 | } else if(strcmp(argv[i], "-ef") == 0) { |
| 5160 | if(++i == argc) { |
| 5161 | ERROR("%s: -ef missing filename\n", argv[0]); |
| 5162 | exit(1); |
| 5163 | } |
plougher | 9b5bf8c | 2006-03-20 18:43:33 +0000 | [diff] [blame] | 5164 | } else if(strcmp(argv[i], "-no-duplicates") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5165 | duplicate_checking = FALSE; |
| 5166 | |
| 5167 | else if(strcmp(argv[i], "-no-fragments") == 0) |
| 5168 | no_fragments = TRUE; |
| 5169 | |
| 5170 | else if(strcmp(argv[i], "-always-use-fragments") == 0) |
| 5171 | always_use_fragments = TRUE; |
| 5172 | |
| 5173 | else if(strcmp(argv[i], "-sort") == 0) { |
| 5174 | if(++i == argc) { |
| 5175 | ERROR("%s: -sort missing filename\n", argv[0]); |
| 5176 | exit(1); |
| 5177 | } |
| 5178 | } else if(strcmp(argv[i], "-all-root") == 0 || |
| 5179 | strcmp(argv[i], "-root-owned") == 0) |
| 5180 | global_uid = global_gid = 0; |
| 5181 | |
| 5182 | else if(strcmp(argv[i], "-force-uid") == 0) { |
| 5183 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5184 | ERROR("%s: -force-uid missing uid or user\n", |
| 5185 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5186 | exit(1); |
| 5187 | } |
| 5188 | if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5189 | if(global_uid < 0 || global_uid > |
| 5190 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5191 | ERROR("%s: -force-uid uid out of range" |
| 5192 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5193 | exit(1); |
| 5194 | } |
| 5195 | } else { |
| 5196 | struct passwd *uid = getpwnam(argv[i]); |
| 5197 | if(uid) |
| 5198 | global_uid = uid->pw_uid; |
| 5199 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5200 | ERROR("%s: -force-uid invalid uid or " |
| 5201 | "unknown user\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5202 | exit(1); |
| 5203 | } |
| 5204 | } |
| 5205 | } else if(strcmp(argv[i], "-force-gid") == 0) { |
| 5206 | if(++i == argc) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5207 | ERROR("%s: -force-gid missing gid or group\n", |
| 5208 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5209 | exit(1); |
| 5210 | } |
| 5211 | if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5212 | if(global_gid < 0 || global_gid > |
| 5213 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5214 | ERROR("%s: -force-gid gid out of range" |
| 5215 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5216 | exit(1); |
| 5217 | } |
| 5218 | } else { |
| 5219 | struct group *gid = getgrnam(argv[i]); |
| 5220 | if(gid) |
| 5221 | global_gid = gid->gr_gid; |
| 5222 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5223 | ERROR("%s: -force-gid invalid gid or " |
| 5224 | "unknown group\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5225 | exit(1); |
| 5226 | } |
| 5227 | } |
| 5228 | } else if(strcmp(argv[i], "-noI") == 0 || |
| 5229 | strcmp(argv[i], "-noInodeCompression") == 0) |
| 5230 | noI = TRUE; |
| 5231 | |
| 5232 | else if(strcmp(argv[i], "-noD") == 0 || |
| 5233 | strcmp(argv[i], "-noDataCompression") == 0) |
| 5234 | noD = TRUE; |
| 5235 | |
| 5236 | else if(strcmp(argv[i], "-noF") == 0 || |
| 5237 | strcmp(argv[i], "-noFragmentCompression") == 0) |
| 5238 | noF = TRUE; |
| 5239 | |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5240 | else if(strcmp(argv[i], "-noX") == 0 || |
| 5241 | strcmp(argv[i], "-noXattrCompression") == 0) |
| 5242 | noX = TRUE; |
| 5243 | |
plougher | ce564c6 | 2010-05-19 03:01:49 +0000 | [diff] [blame] | 5244 | else if(strcmp(argv[i], "-no-xattrs") == 0) |
| 5245 | no_xattrs = TRUE; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 5246 | |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5247 | else if(strcmp(argv[i], "-xattrs") == 0) |
| 5248 | no_xattrs = FALSE; |
| 5249 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5250 | else if(strcmp(argv[i], "-nopad") == 0) |
| 5251 | nopad = TRUE; |
| 5252 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5253 | else if(strcmp(argv[i], "-info") == 0) |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5254 | silent = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5255 | |
| 5256 | else if(strcmp(argv[i], "-e") == 0) |
| 5257 | break; |
| 5258 | |
| 5259 | else if(strcmp(argv[i], "-noappend") == 0) |
| 5260 | delete = TRUE; |
| 5261 | |
| 5262 | else if(strcmp(argv[i], "-keep-as-directory") == 0) |
| 5263 | keep_as_directory = TRUE; |
| 5264 | |
| 5265 | else if(strcmp(argv[i], "-root-becomes") == 0) { |
| 5266 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5267 | ERROR("%s: -root-becomes: missing name\n", |
| 5268 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5269 | exit(1); |
| 5270 | } |
| 5271 | root_name = argv[i]; |
| 5272 | } else if(strcmp(argv[i], "-version") == 0) { |
| 5273 | VERSION(); |
| 5274 | } else { |
| 5275 | ERROR("%s: invalid option\n\n", argv[0]); |
| 5276 | printOptions: |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5277 | ERROR("SYNTAX:%s source1 source2 ... dest [options] " |
| 5278 | "[-e list of exclude\ndirs/files]\n", argv[0]); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5279 | ERROR("\nFilesystem build options:\n"); |
plougher | ff5ea8b | 2009-08-07 19:33:10 +0000 | [diff] [blame] | 5280 | ERROR("-comp <comp>\t\tselect <comp> compression\n"); |
plougher | 13df178 | 2009-08-29 01:05:34 +0000 | [diff] [blame] | 5281 | ERROR("\t\t\tCompressors available:\n"); |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 5282 | display_compressors("\t\t\t", COMP_DEFAULT); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5283 | ERROR("-b <block_size>\t\tset data block to " |
| 5284 | "<block_size>. Default %d bytes\n", |
| 5285 | SQUASHFS_FILE_SIZE); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5286 | ERROR("-no-exports\t\tdon't make the filesystem " |
| 5287 | "exportable via NFS\n"); |
| 5288 | ERROR("-no-sparse\t\tdon't detect sparse files\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5289 | ERROR("-no-xattrs\t\tdon't store extended attributes" |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5290 | NOXOPT_STR "\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5291 | ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5292 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5293 | ERROR("-noI\t\t\tdo not compress inode table\n"); |
| 5294 | ERROR("-noD\t\t\tdo not compress data blocks\n"); |
| 5295 | ERROR("-noF\t\t\tdo not compress fragment blocks\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5296 | ERROR("-noX\t\t\tdo not compress extended " |
| 5297 | "attributes\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5298 | ERROR("-no-fragments\t\tdo not use fragments\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5299 | ERROR("-always-use-fragments\tuse fragment blocks for " |
| 5300 | "files larger than block size\n"); |
| 5301 | ERROR("-no-duplicates\t\tdo not perform duplicate " |
| 5302 | "checking\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5303 | ERROR("-all-root\t\tmake all files owned by root\n"); |
| 5304 | ERROR("-force-uid uid\t\tset all file uids to uid\n"); |
| 5305 | ERROR("-force-gid gid\t\tset all file gids to gid\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5306 | ERROR("-nopad\t\t\tdo not pad filesystem to a multiple " |
| 5307 | "of 4K\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5308 | ERROR("-keep-as-directory\tif one source directory is " |
| 5309 | "specified, create a root\n"); |
| 5310 | ERROR("\t\t\tdirectory containing that directory, " |
| 5311 | "rather than the\n"); |
| 5312 | ERROR("\t\t\tcontents of the directory\n"); |
| 5313 | ERROR("\nFilesystem filter options:\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5314 | ERROR("-p <pseudo-definition>\tAdd pseudo file " |
| 5315 | "definition\n"); |
| 5316 | ERROR("-pf <pseudo-file>\tAdd list of pseudo file " |
| 5317 | "definitions\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5318 | ERROR("-sort <sort_file>\tsort files according to " |
| 5319 | "priorities in <sort_file>. One\n"); |
| 5320 | ERROR("\t\t\tfile or dir with priority per line. " |
| 5321 | "Priority -32768 to\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5322 | ERROR("\t\t\t32767, default priority 0\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5323 | ERROR("-ef <exclude_file>\tlist of exclude dirs/files." |
| 5324 | " One per line\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5325 | ERROR("-wildcards\t\tAllow extended shell wildcards " |
| 5326 | "(globbing) to be used in\n\t\t\texclude " |
| 5327 | "dirs/files\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5328 | ERROR("-regex\t\t\tAllow POSIX regular expressions to " |
| 5329 | "be used in exclude\n\t\t\tdirs/files\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5330 | ERROR("\nFilesystem append options:\n"); |
| 5331 | ERROR("-noappend\t\tdo not append to existing " |
| 5332 | "filesystem\n"); |
| 5333 | ERROR("-root-becomes <name>\twhen appending source " |
| 5334 | "files/directories, make the\n"); |
| 5335 | ERROR("\t\t\toriginal root become a subdirectory in " |
| 5336 | "the new root\n"); |
| 5337 | ERROR("\t\t\tcalled <name>, rather than adding the new " |
| 5338 | "source items\n"); |
| 5339 | ERROR("\t\t\tto the original root\n"); |
| 5340 | ERROR("\nMksquashfs runtime options:\n"); |
| 5341 | ERROR("-version\t\tprint version, licence and " |
| 5342 | "copyright message\n"); |
| 5343 | ERROR("-recover <name>\t\trecover filesystem data " |
| 5344 | "using recovery file <name>\n"); |
| 5345 | ERROR("-no-recovery\t\tdon't generate a recovery " |
| 5346 | "file\n"); |
| 5347 | ERROR("-info\t\t\tprint files written to filesystem\n"); |
| 5348 | ERROR("-no-progress\t\tdon't display the progress " |
| 5349 | "bar\n"); |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5350 | ERROR("-progress\t\tdisplay progress bar when using " |
| 5351 | "the -info option\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5352 | ERROR("-processors <number>\tUse <number> processors." |
| 5353 | " By default will use number of\n"); |
| 5354 | ERROR("\t\t\tprocessors available\n"); |
| 5355 | ERROR("-read-queue <size>\tSet input queue to <size> " |
| 5356 | "Mbytes. Default %d Mbytes\n", |
| 5357 | READER_BUFFER_DEFAULT); |
| 5358 | ERROR("-write-queue <size>\tSet output queue to <size> " |
| 5359 | "Mbytes. Default %d Mbytes\n", |
| 5360 | WRITER_BUFFER_DEFAULT); |
plougher | 8bc376b | 2010-11-12 04:55:20 +0000 | [diff] [blame] | 5361 | ERROR("-fragment-queue <size>\tSet fragment queue to " |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5362 | "<size> Mbytes. Default %d Mbytes\n", |
| 5363 | FRAGMENT_BUFFER_DEFAULT); |
| 5364 | ERROR("\nMiscellaneous options:\n"); |
| 5365 | ERROR("-root-owned\t\talternative name for -all-root" |
| 5366 | "\n"); |
| 5367 | ERROR("-noInodeCompression\talternative name for -noI" |
| 5368 | "\n"); |
| 5369 | ERROR("-noDataCompression\talternative name for -noD" |
| 5370 | "\n"); |
| 5371 | ERROR("-noFragmentCompression\talternative name for " |
| 5372 | "-noF\n"); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5373 | ERROR("-noXattrCompression\talternative name for " |
| 5374 | "-noX\n"); |
plougher | 4fb6682 | 2010-12-08 02:49:28 +0000 | [diff] [blame] | 5375 | ERROR("\nCompressors available and compressor specific " |
| 5376 | "options:\n"); |
| 5377 | display_compressor_usage(COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5378 | exit(1); |
| 5379 | } |
| 5380 | } |
| 5381 | |
plougher | b747f4f | 2010-12-25 04:05:47 +0000 | [diff] [blame] | 5382 | /* |
| 5383 | * Some compressors may need the options to be checked for validity |
| 5384 | * once all the options have been processed |
| 5385 | */ |
| 5386 | res = compressor_options_post(comp, block_size); |
| 5387 | if(res) |
| 5388 | EXIT_MKSQUASHFS(); |
| 5389 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5390 | /* |
| 5391 | * If the -info option has been selected then disable the |
| 5392 | * progress bar unless it has been explicitly enabled with |
| 5393 | * the -progress option |
| 5394 | */ |
| 5395 | if(!silent) |
| 5396 | progress = force_progress; |
| 5397 | |
| 5398 | #ifdef SQUASHFS_TRACE |
| 5399 | progress = FALSE; |
| 5400 | #endif |
| 5401 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5402 | for(i = 0; i < source; i++) |
| 5403 | if(lstat(source_path[i], &source_buf) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5404 | fprintf(stderr, "Cannot stat source directory \"%s\" " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5405 | "because %s\n", source_path[i], |
| 5406 | strerror(errno)); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5407 | EXIT_MKSQUASHFS(); |
| 5408 | } |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5409 | |
| 5410 | destination_file = argv[source + 1]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5411 | if(stat(argv[source + 1], &buf) == -1) { |
| 5412 | if(errno == ENOENT) { /* Does not exist */ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5413 | fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, |
plougher | 59dce67 | 2010-05-19 03:56:59 +0000 | [diff] [blame] | 5414 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5415 | if(fd == -1) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5416 | perror("Could not create destination file"); |
| 5417 | exit(1); |
| 5418 | } |
| 5419 | delete = TRUE; |
| 5420 | } else { |
| 5421 | perror("Could not stat destination file"); |
| 5422 | exit(1); |
| 5423 | } |
| 5424 | |
| 5425 | } else { |
| 5426 | if(S_ISBLK(buf.st_mode)) { |
| 5427 | if((fd = open(argv[source + 1], O_RDWR)) == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5428 | perror("Could not open block device as " |
| 5429 | "destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5430 | exit(1); |
| 5431 | } |
| 5432 | block_device = 1; |
| 5433 | |
| 5434 | } else if(S_ISREG(buf.st_mode)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5435 | fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | |
| 5436 | O_RDWR); |
| 5437 | if(fd == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5438 | perror("Could not open regular file for " |
| 5439 | "writing as destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5440 | exit(1); |
| 5441 | } |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 5442 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5443 | else { |
| 5444 | ERROR("Destination not block device or regular file\n"); |
| 5445 | exit(1); |
| 5446 | } |
| 5447 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5448 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5449 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5450 | signal(SIGTERM, sighandler2); |
| 5451 | signal(SIGINT, sighandler2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5452 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5453 | /* |
| 5454 | * process the exclude files - must be done afer destination file has |
| 5455 | * been possibly created |
| 5456 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5457 | for(i = source + 2; i < argc; i++) |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 5458 | if(strcmp(argv[i], "-ef") == 0) |
| 5459 | /* |
| 5460 | * Note presence of filename arg has already |
| 5461 | * been checked |
| 5462 | */ |
| 5463 | process_exclude_file(argv[++i]); |
| 5464 | else if(strcmp(argv[i], "-e") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5465 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5466 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5467 | strcmp(argv[i], "-sort") == 0 || |
| 5468 | strcmp(argv[i], "-pf") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5469 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5470 | i++; |
| 5471 | |
| 5472 | if(i != argc) { |
| 5473 | if(++i == argc) { |
| 5474 | ERROR("%s: -e missing arguments\n", argv[0]); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5475 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5476 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 5477 | while(i < argc) |
| 5478 | if(old_exclude) |
| 5479 | old_add_exclude(argv[i++]); |
| 5480 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5481 | add_exclude(argv[i++]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | /* process the sort files - must be done afer the exclude files */ |
| 5485 | for(i = source + 2; i < argc; i++) |
| 5486 | if(strcmp(argv[i], "-sort") == 0) { |
plougher | e1c9a74 | 2010-07-21 16:59:05 +0000 | [diff] [blame] | 5487 | int res = read_sort_file(argv[++i], source, |
| 5488 | source_path); |
| 5489 | if(res == FALSE) |
| 5490 | BAD_ERROR("Failed to read sort file\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5491 | sorted ++; |
| 5492 | } else if(strcmp(argv[i], "-e") == 0) |
| 5493 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5494 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5495 | strcmp(argv[i], "-ef") == 0 || |
| 5496 | strcmp(argv[i], "-pf") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5497 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5498 | i++; |
| 5499 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5500 | if(!delete) { |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 5501 | comp = read_super(fd, &sBlk, argv[source + 1]); |
| 5502 | if(comp == NULL) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5503 | ERROR("Failed to read existing filesystem - will not " |
| 5504 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5505 | ERROR("To force Mksquashfs to write to this block " |
| 5506 | "device or file use -noappend\n"); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5507 | EXIT_MKSQUASHFS(); |
| 5508 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5509 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5510 | block_log = slog(block_size = sBlk.block_size); |
| 5511 | noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags); |
| 5512 | noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags); |
| 5513 | noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5514 | noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5515 | no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags); |
| 5516 | always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags); |
| 5517 | duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5518 | exportable = SQUASHFS_EXPORTABLE(sBlk.flags); |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5519 | no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5520 | comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 5523 | initialise_threads(readb_mbytes, writeb_mbytes, fragmentb_mbytes); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5524 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 5525 | res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0); |
| 5526 | if(res) |
| 5527 | BAD_ERROR("compressor_init failed\n"); |
| 5528 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5529 | if(delete) { |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5530 | int size; |
Phillip Lougher | a45c9d2 | 2011-02-20 04:24:07 +0000 | [diff] [blame] | 5531 | void *comp_data = compressor_dump_options(comp, block_size, |
| 5532 | &size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5533 | |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 5534 | printf("Creating %d.%d filesystem on %s, block size %d.\n", |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5535 | SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5536 | |
plougher | 871c72a | 2010-12-25 04:17:27 +0000 | [diff] [blame] | 5537 | /* |
| 5538 | * store any compressor specific options after the superblock, |
| 5539 | * and set the COMP_OPT flag to show that the filesystem has |
| 5540 | * compressor specfic options |
| 5541 | */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5542 | if(comp_data) { |
| 5543 | unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT; |
| 5544 | |
| 5545 | SQUASHFS_INSWAP_SHORTS(&c_byte, 1); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5546 | write_destination(fd, sizeof(struct squashfs_super_block), |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 5547 | sizeof(c_byte), &c_byte); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5548 | write_destination(fd, sizeof(struct squashfs_super_block) + |
plougher | 8e4dad4 | 2010-12-28 05:56:22 +0000 | [diff] [blame] | 5549 | sizeof(c_byte), size, comp_data); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5550 | bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte) |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5551 | + size; |
| 5552 | comp_opts = TRUE; |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5553 | } else |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5554 | bytes = sizeof(struct squashfs_super_block); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5555 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5556 | unsigned int last_directory_block, inode_dir_offset, |
| 5557 | inode_dir_file_size, root_inode_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5558 | inode_dir_start_block, uncompressed_data, |
| 5559 | compressed_data, inode_dir_inode_number, |
| 5560 | inode_dir_parent_inode; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5561 | unsigned int root_inode_start = |
| 5562 | SQUASHFS_INODE_BLK(sBlk.root_inode), |
| 5563 | root_inode_offset = |
| 5564 | SQUASHFS_INODE_OFFSET(sBlk.root_inode); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5565 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5566 | if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, |
| 5567 | &data_cache, &directory_table, |
| 5568 | &directory_data_cache, &last_directory_block, |
| 5569 | &inode_dir_offset, &inode_dir_file_size, |
| 5570 | &root_inode_size, &inode_dir_start_block, |
| 5571 | &file_count, &sym_count, &dev_count, &dir_count, |
| 5572 | &fifo_count, &sock_count, &total_bytes, |
| 5573 | &total_inode_bytes, &total_directory_bytes, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5574 | &inode_dir_inode_number, |
| 5575 | &inode_dir_parent_inode, add_old_root_entry, |
| 5576 | &fragment_table, &inode_lookup_table)) == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5577 | ERROR("Failed to read existing filesystem - will not " |
| 5578 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5579 | ERROR("To force Mksquashfs to write to this block " |
| 5580 | "device or file use -noappend\n"); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5581 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5582 | } |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5583 | if((fragments = sBlk.fragments)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5584 | fragment_table = realloc((char *) fragment_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5585 | ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 5586 | * sizeof(struct squashfs_fragment_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5587 | if(fragment_table == NULL) |
| 5588 | BAD_ERROR("Out of memory in save filesystem state\n"); |
| 5589 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5590 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5591 | printf("Appending to existing %d.%d filesystem on %s, block " |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5592 | "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5593 | block_size); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5594 | printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, " |
plougher | bb98803 | 2009-08-06 08:46:34 +0000 | [diff] [blame] | 5595 | "-always-use-fragments,\n-exportable and -comp options " |
| 5596 | "ignored\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5597 | printf("\nIf appending is not wanted, please re-run with " |
| 5598 | "-noappend specified!\n\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5599 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5600 | compressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5601 | ~(SQUASHFS_METADATA_SIZE - 1); |
| 5602 | uncompressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5603 | (SQUASHFS_METADATA_SIZE - 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5604 | |
| 5605 | /* save original filesystem state for restoring ... */ |
| 5606 | sfragments = fragments; |
| 5607 | sbytes = bytes; |
| 5608 | sinode_count = sBlk.inodes; |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 5609 | scache_bytes = root_inode_offset + root_inode_size; |
| 5610 | sdirectory_cache_bytes = uncompressed_data; |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5611 | sdata_cache = malloc(scache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5612 | if(sdata_cache == NULL) |
| 5613 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5614 | sdirectory_data_cache = malloc(sdirectory_cache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5615 | if(sdirectory_data_cache == NULL) |
| 5616 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5617 | memcpy(sdata_cache, data_cache, scache_bytes); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5618 | memcpy(sdirectory_data_cache, directory_data_cache + |
| 5619 | compressed_data, sdirectory_cache_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5620 | sinode_bytes = root_inode_start; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5621 | stotal_bytes = total_bytes; |
| 5622 | stotal_inode_bytes = total_inode_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5623 | stotal_directory_bytes = total_directory_bytes + |
| 5624 | compressed_data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5625 | sfile_count = file_count; |
| 5626 | ssym_count = sym_count; |
| 5627 | sdev_count = dev_count; |
| 5628 | sdir_count = dir_count + 1; |
| 5629 | sfifo_count = fifo_count; |
| 5630 | ssock_count = sock_count; |
| 5631 | sdup_files = dup_files; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5632 | sid_count = id_count; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5633 | write_recovery_data(&sBlk); |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 5634 | if(save_xattrs() == FALSE) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5635 | BAD_ERROR("Failed to save xattrs from existing " |
| 5636 | "filesystem\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5637 | restore = TRUE; |
| 5638 | if(setjmp(env)) |
| 5639 | goto restore_filesystem; |
| 5640 | signal(SIGTERM, sighandler); |
| 5641 | signal(SIGINT, sighandler); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 5642 | write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5643 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5644 | /* |
| 5645 | * set the filesystem state up to be able to append to the |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5646 | * original filesystem. The filesystem state differs depending |
| 5647 | * on whether we're appending to the original root directory, or |
| 5648 | * if the original root directory becomes a sub-directory |
| 5649 | * (root-becomes specified on command line, here root_name != |
| 5650 | * NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5651 | */ |
| 5652 | inode_bytes = inode_size = root_inode_start; |
| 5653 | directory_size = last_directory_block; |
| 5654 | cache_size = root_inode_offset + root_inode_size; |
| 5655 | directory_cache_size = inode_dir_offset + inode_dir_file_size; |
| 5656 | if(root_name) { |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5657 | sdirectory_bytes = last_directory_block; |
| 5658 | sdirectory_compressed_bytes = 0; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5659 | root_inode_number = inode_dir_parent_inode; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5660 | inode_no = sBlk.inodes + 2; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5661 | directory_bytes = last_directory_block; |
| 5662 | directory_cache_bytes = uncompressed_data; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5663 | memmove(directory_data_cache, directory_data_cache + |
| 5664 | compressed_data, uncompressed_data); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5665 | cache_bytes = root_inode_offset + root_inode_size; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5666 | add_old_root_entry(root_name, sBlk.root_inode, |
| 5667 | inode_dir_inode_number, SQUASHFS_DIR_TYPE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5668 | total_directory_bytes += compressed_data; |
| 5669 | dir_count ++; |
| 5670 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5671 | sdirectory_compressed_bytes = last_directory_block - |
| 5672 | inode_dir_start_block; |
| 5673 | sdirectory_compressed = |
| 5674 | malloc(sdirectory_compressed_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5675 | if(sdirectory_compressed == NULL) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5676 | BAD_ERROR("Out of memory in save filesystem " |
| 5677 | "state\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5678 | memcpy(sdirectory_compressed, directory_table + |
| 5679 | inode_dir_start_block, |
| 5680 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5681 | sdirectory_bytes = inode_dir_start_block; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5682 | root_inode_number = inode_dir_inode_number; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5683 | inode_no = sBlk.inodes + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5684 | directory_bytes = inode_dir_start_block; |
| 5685 | directory_cache_bytes = inode_dir_offset; |
| 5686 | cache_bytes = root_inode_offset; |
| 5687 | } |
| 5688 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5689 | inode_count = file_count + dir_count + sym_count + dev_count + |
| 5690 | fifo_count + sock_count; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 5691 | |
| 5692 | /* |
| 5693 | * The default use freelist before growing cache policy behaves |
| 5694 | * poorly with appending - with many deplicates the caches |
| 5695 | * do not grow due to the fact that large queues of outstanding |
| 5696 | * fragments/writer blocks do not occur, leading to small caches |
| 5697 | * and un-uncessary performance loss to frequent cache |
| 5698 | * replacement in the small caches. Therefore with appending |
| 5699 | * change the policy to grow the caches before reusing blocks |
| 5700 | * from the freelist |
| 5701 | */ |
| 5702 | first_freelist = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5705 | if(path || stickypath) { |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5706 | paths = init_subdir(); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5707 | if(path) |
| 5708 | paths = add_subdir(paths, path); |
| 5709 | if(stickypath) |
| 5710 | paths = add_subdir(paths, stickypath); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5713 | dump_actions(); |
| 5714 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5715 | if(delete && !keep_as_directory && source == 1 && |
| 5716 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | dc587a0 | 2012-11-14 05:30:27 +0000 | [diff] [blame] | 5717 | dir_scan(&inode, source_path[0], scan1_readdir, pseudo); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5718 | else if(!keep_as_directory && source == 1 && |
| 5719 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | dc587a0 | 2012-11-14 05:30:27 +0000 | [diff] [blame] | 5720 | dir_scan(&inode, source_path[0], scan1_single_readdir, pseudo); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5721 | else |
Phillip Lougher | dc587a0 | 2012-11-14 05:30:27 +0000 | [diff] [blame] | 5722 | dir_scan(&inode, "", scan1_encomp_readdir, pseudo); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5723 | sBlk.root_inode = inode; |
| 5724 | sBlk.inodes = inode_count; |
| 5725 | sBlk.s_magic = SQUASHFS_MAGIC; |
| 5726 | sBlk.s_major = SQUASHFS_MAJOR; |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5727 | sBlk.s_minor = SQUASHFS_MINOR; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5728 | sBlk.block_size = block_size; |
| 5729 | sBlk.block_log = block_log; |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5730 | sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments, |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5731 | always_use_fragments, duplicate_checking, exportable, |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5732 | no_xattrs, comp_opts); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5733 | sBlk.mkfs_time = time(NULL); |
| 5734 | |
| 5735 | restore_filesystem: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 5736 | if(progress) |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 5737 | disable_progress_bar(); |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 5738 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5739 | sBlk.fragments = fragments; |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 5740 | if(!restoring) { |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5741 | struct file_buffer **fragment = NULL; |
| 5742 | while((fragment = get_frag_action(fragment))) |
| 5743 | write_fragment(*fragment); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 5744 | unlock_fragments(); |
| 5745 | pthread_mutex_lock(&fragment_mutex); |
| 5746 | while(fragments_outstanding) { |
| 5747 | pthread_mutex_unlock(&fragment_mutex); |
| 5748 | sched_yield(); |
| 5749 | pthread_mutex_lock(&fragment_mutex); |
| 5750 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5751 | queue_put(to_writer, NULL); |
| 5752 | if(queue_get(from_writer) != 0) |
| 5753 | EXIT_MKSQUASHFS(); |
| 5754 | } |
| 5755 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 5756 | sBlk.no_ids = id_count; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5757 | sBlk.inode_table_start = write_inodes(); |
| 5758 | sBlk.directory_table_start = write_directories(); |
| 5759 | sBlk.fragment_table_start = write_fragment_table(); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5760 | sBlk.lookup_table_start = exportable ? write_inode_lookup_table() : |
| 5761 | SQUASHFS_INVALID_BLK; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 5762 | sBlk.id_table_start = write_id_table(); |
| 5763 | sBlk.xattr_id_table_start = write_xattrs(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5764 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5765 | TRACE("sBlk->inode_table_start 0x%llx\n", sBlk.inode_table_start); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5766 | TRACE("sBlk->directory_table_start 0x%llx\n", |
| 5767 | sBlk.directory_table_start); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5768 | TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk.fragment_table_start); |
| 5769 | if(exportable) |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5770 | TRACE("sBlk->lookup_table_start 0x%llx\n", |
| 5771 | sBlk.lookup_table_start); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5772 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5773 | sBlk.bytes_used = bytes; |
plougher | 9fca346 | 2008-10-27 00:34:35 +0000 | [diff] [blame] | 5774 | |
plougher | 8c4b7b9 | 2009-07-30 04:47:52 +0000 | [diff] [blame] | 5775 | sBlk.compression = comp->id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5776 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 5777 | SQUASHFS_INSWAP_SUPER_BLOCK(&sBlk); |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 5778 | write_destination(fd, SQUASHFS_START, sizeof(sBlk), &sBlk); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5779 | |
| 5780 | if(!nopad && (i = bytes & (4096 - 1))) { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 5781 | char temp[4096] = {0}; |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 5782 | write_destination(fd, bytes, 4096 - i, temp); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5785 | close(fd); |
| 5786 | |
plougher | 11e7b1b | 2009-09-11 12:10:58 +0000 | [diff] [blame] | 5787 | delete_pseudo_files(); |
| 5788 | |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 5789 | if(recovery_file) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5790 | unlink(recovery_file); |
| 5791 | |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 5792 | total_bytes += total_inode_bytes + total_directory_bytes + |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5793 | sizeof(struct squashfs_super_block) + total_xattr_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5794 | |
plougher | 62542fb | 2009-08-06 08:43:08 +0000 | [diff] [blame] | 5795 | printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size" |
| 5796 | " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR, |
| 5797 | SQUASHFS_MINOR, comp->name, block_size); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5798 | printf("\t%s data, %s metadata, %s fragments, %s xattrs\n", |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 5799 | noD ? "uncompressed" : "compressed", noI ? "uncompressed" : |
| 5800 | "compressed", no_fragments ? "no" : noF ? "uncompressed" : |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5801 | "compressed", no_xattrs ? "no" : noX ? "uncompressed" : |
| 5802 | "compressed"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5803 | printf("\tduplicates are %sremoved\n", duplicate_checking ? "" : |
| 5804 | "not "); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5805 | printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, |
| 5806 | bytes / (1024.0 * 1024.0)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5807 | printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n", |
| 5808 | ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0); |
| 5809 | printf("Inode table size %d bytes (%.2f Kbytes)\n", |
| 5810 | inode_bytes, inode_bytes / 1024.0); |
| 5811 | printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n", |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5812 | ((float) inode_bytes / total_inode_bytes) * 100.0, |
| 5813 | total_inode_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5814 | printf("Directory table size %d bytes (%.2f Kbytes)\n", |
| 5815 | directory_bytes, directory_bytes / 1024.0); |
| 5816 | printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n", |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5817 | ((float) directory_bytes / total_directory_bytes) * 100.0, |
| 5818 | total_directory_bytes); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 5819 | if(total_xattr_bytes) { |
| 5820 | printf("Xattr table size %d bytes (%.2f Kbytes)\n", |
| 5821 | xattr_bytes, xattr_bytes / 1024.0); |
| 5822 | printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n", |
| 5823 | ((float) xattr_bytes / total_xattr_bytes) * 100.0, |
| 5824 | total_xattr_bytes); |
| 5825 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5826 | if(duplicate_checking) |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5827 | printf("Number of duplicate files found %d\n", file_count - |
| 5828 | dup_files); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5829 | else |
| 5830 | printf("No duplicate files removed\n"); |
| 5831 | printf("Number of inodes %d\n", inode_count); |
| 5832 | printf("Number of files %d\n", file_count); |
| 5833 | if(!no_fragments) |
| 5834 | printf("Number of fragments %d\n", fragments); |
| 5835 | printf("Number of symbolic links %d\n", sym_count); |
| 5836 | printf("Number of device nodes %d\n", dev_count); |
| 5837 | printf("Number of fifo nodes %d\n", fifo_count); |
| 5838 | printf("Number of socket nodes %d\n", sock_count); |
| 5839 | printf("Number of directories %d\n", dir_count); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5840 | printf("Number of ids (unique uids + gids) %d\n", id_count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5841 | printf("Number of uids %d\n", uid_count); |
| 5842 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5843 | for(i = 0; i < id_count; i++) { |
| 5844 | if(id_table[i]->flags & ISA_UID) { |
| 5845 | struct passwd *user = getpwuid(id_table[i]->id); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5846 | printf("\t%s (%d)\n", user == NULL ? "unknown" : |
| 5847 | user->pw_name, id_table[i]->id); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5848 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5849 | } |
| 5850 | |
| 5851 | printf("Number of gids %d\n", guid_count); |
| 5852 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5853 | for(i = 0; i < id_count; i++) { |
| 5854 | if(id_table[i]->flags & ISA_GID) { |
| 5855 | struct group *group = getgrgid(id_table[i]->id); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5856 | printf("\t%s (%d)\n", group == NULL ? "unknown" : |
| 5857 | group->gr_name, id_table[i]->id); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5858 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5859 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5860 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5861 | return 0; |
| 5862 | } |