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 |
plougher | f6cd337 | 2007-08-19 03:33:23 +0000 | [diff] [blame] | 7 | * Phillip Lougher <phillip@lougher.demon.co.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> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 50 | |
plougher | 6885329 | 2005-12-27 02:47:04 +0000 | [diff] [blame] | 51 | #ifndef linux |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 52 | #define __BYTE_ORDER BYTE_ORDER |
| 53 | #define __BIG_ENDIAN BIG_ENDIAN |
| 54 | #define __LITTLE_ENDIAN LITTLE_ENDIAN |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 55 | #include <sys/sysctl.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 56 | #else |
| 57 | #include <endian.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 58 | #include <sys/sysinfo.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 61 | #ifdef SQUASHFS_TRACE |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 62 | #define TRACE(s, args...) \ |
| 63 | do { \ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 64 | printf("mksquashfs: "s, ## args); \ |
| 65 | } while(0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 66 | #else |
| 67 | #define TRACE(s, args...) |
| 68 | #endif |
| 69 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 70 | #define INFO(s, args...) \ |
| 71 | do {\ |
| 72 | if(!silent)\ |
| 73 | printf("mksquashfs: "s, ## args);\ |
| 74 | } while(0) |
| 75 | |
| 76 | #define ERROR(s, args...) \ |
| 77 | do {\ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 78 | progressbar_error(s, ## args); \ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 79 | } while(0) |
| 80 | |
| 81 | #define EXIT_MKSQUASHFS() \ |
| 82 | do {\ |
| 83 | if(restore)\ |
| 84 | restorefs();\ |
| 85 | if(delete && destination_file && !block_device)\ |
| 86 | unlink(destination_file);\ |
| 87 | exit(1);\ |
| 88 | } while(0) |
| 89 | |
| 90 | #define BAD_ERROR(s, args...) \ |
| 91 | do {\ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 92 | progressbar_error("FATAL ERROR:" s, ##args); \ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 93 | EXIT_MKSQUASHFS();\ |
| 94 | } while(0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 95 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 96 | #include "squashfs_fs.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 97 | #include "squashfs_swap.h" |
| 98 | #include "mksquashfs.h" |
| 99 | #include "sort.h" |
| 100 | #include "pseudo.h" |
| 101 | #include "compressor.h" |
| 102 | #include "xattr.h" |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 103 | #include "action.h" |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 104 | #include "progressbar.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 105 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 106 | int delete = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 107 | int fd; |
| 108 | |
| 109 | /* filesystem flags for building */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 110 | int comp_opts = FALSE; |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 111 | int no_xattrs = XATTR_DEF, noX = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 112 | int duplicate_checking = 1, noF = 0, no_fragments = 0, always_use_fragments = 0; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 113 | int noI = 0, noD = 0; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 114 | int silent = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 115 | long long global_uid = -1, global_gid = -1; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 116 | int exportable = TRUE; |
| 117 | int progress = TRUE; |
plougher | 8dcc699 | 2007-08-17 19:32:52 +0000 | [diff] [blame] | 118 | int sparse_files = TRUE; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 119 | int old_exclude = TRUE; |
| 120 | int use_regex = FALSE; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 121 | int first_freelist = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 122 | |
| 123 | /* superblock attributes */ |
| 124 | int block_size = SQUASHFS_FILE_SIZE, block_log; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 125 | unsigned int id_count = 0; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 126 | int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0, |
| 127 | sock_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 128 | |
| 129 | /* write position within data section */ |
| 130 | long long bytes = 0, total_bytes = 0; |
| 131 | |
| 132 | /* in memory directory table - possibly compressed */ |
| 133 | char *directory_table = NULL; |
| 134 | unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0; |
| 135 | |
| 136 | /* cached directory table */ |
| 137 | char *directory_data_cache = NULL; |
| 138 | unsigned int directory_cache_bytes = 0, directory_cache_size = 0; |
| 139 | |
| 140 | /* in memory inode table - possibly compressed */ |
| 141 | char *inode_table = NULL; |
| 142 | unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0; |
| 143 | |
| 144 | /* cached inode table */ |
| 145 | char *data_cache = NULL; |
| 146 | unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0; |
| 147 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 148 | /* inode lookup table */ |
| 149 | squashfs_inode *inode_lookup_table = NULL; |
| 150 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 151 | /* in memory directory data */ |
| 152 | #define I_COUNT_SIZE 128 |
| 153 | #define DIR_ENTRIES 32 |
| 154 | #define INODE_HASH_SIZE 65536 |
| 155 | #define INODE_HASH_MASK (INODE_HASH_SIZE - 1) |
| 156 | #define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK) |
| 157 | |
| 158 | struct cached_dir_index { |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 159 | struct squashfs_dir_index index; |
| 160 | char *name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | struct directory { |
| 164 | unsigned int start_block; |
| 165 | unsigned int size; |
| 166 | unsigned char *buff; |
| 167 | unsigned char *p; |
| 168 | unsigned int entry_count; |
| 169 | unsigned char *entry_count_p; |
| 170 | unsigned int i_count; |
| 171 | unsigned int i_size; |
| 172 | struct cached_dir_index *index; |
| 173 | unsigned char *index_count_p; |
| 174 | unsigned int inode_number; |
| 175 | }; |
| 176 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 177 | struct inode_info *inode_info[INODE_HASH_SIZE]; |
| 178 | |
| 179 | /* hash tables used to do fast duplicate searches in duplicate check */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 180 | struct file_info *dupl[65536]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 181 | int dup_files = 0; |
| 182 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 183 | /* exclude file handling */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 184 | /* list of exclude dirs/files */ |
| 185 | struct exclude_info { |
| 186 | dev_t st_dev; |
| 187 | ino_t st_ino; |
| 188 | }; |
| 189 | |
| 190 | #define EXCLUDE_SIZE 8192 |
| 191 | int exclude = 0; |
| 192 | struct exclude_info *exclude_paths = NULL; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 193 | int old_excluded(char *filename, struct stat *buf); |
| 194 | |
| 195 | struct path_entry { |
| 196 | char *name; |
| 197 | regex_t *preg; |
| 198 | struct pathname *paths; |
| 199 | }; |
| 200 | |
| 201 | struct pathname { |
| 202 | int names; |
| 203 | struct path_entry *name; |
| 204 | }; |
| 205 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 206 | struct pathnames { |
| 207 | int count; |
| 208 | struct pathname *path[0]; |
| 209 | }; |
| 210 | #define PATHS_ALLOC_SIZE 10 |
| 211 | |
| 212 | struct pathnames *paths = NULL; |
| 213 | struct pathname *path = NULL; |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 214 | struct pathname *stickypath = NULL; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 215 | int excluded(struct pathnames *paths, char *name, struct pathnames **new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 216 | |
| 217 | /* fragment block data structures */ |
| 218 | int fragments = 0; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 219 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 220 | struct fragment { |
| 221 | unsigned int index; |
| 222 | int offset; |
| 223 | int size; |
| 224 | }; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 225 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 226 | #define FRAG_SIZE 32768 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 227 | #define FRAG_INDEX (1LL << 32) |
| 228 | |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 229 | struct squashfs_fragment_entry *fragment_table = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 230 | int fragments_outstanding = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 231 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 232 | /* current inode number for directories and non directories */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 233 | unsigned int inode_no = 1; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 234 | unsigned int root_inode_number = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 235 | |
| 236 | /* list of source dirs/files */ |
| 237 | int source = 0; |
| 238 | char **source_path; |
| 239 | |
| 240 | /* list of root directory entries read from original filesystem */ |
| 241 | int old_root_entries = 0; |
| 242 | struct old_root_entry_info { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 243 | char *name; |
| 244 | struct inode_info inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 245 | }; |
| 246 | struct old_root_entry_info *old_root_entry; |
| 247 | |
| 248 | /* in memory file info */ |
| 249 | struct file_info { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 250 | long long file_size; |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 251 | long long bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 252 | unsigned short checksum; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 253 | unsigned short fragment_checksum; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 254 | long long start; |
| 255 | unsigned int *block_list; |
| 256 | struct file_info *next; |
| 257 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 258 | char checksum_flag; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | /* count of how many times SIGINT or SIGQUIT has been sent */ |
| 262 | int interrupted = 0; |
| 263 | |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 264 | /* flag if we're restoring existing filesystem */ |
| 265 | int restoring = 0; |
| 266 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 267 | /* restore orignal filesystem state if appending to existing filesystem is |
| 268 | * cancelled */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 269 | jmp_buf env; |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 270 | char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 271 | |
| 272 | long long sbytes, stotal_bytes; |
| 273 | |
| 274 | unsigned int sinode_bytes, scache_bytes, sdirectory_bytes, |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 275 | sdirectory_cache_bytes, sdirectory_compressed_bytes, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 276 | stotal_inode_bytes, stotal_directory_bytes, |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 277 | sinode_count = 0, sfile_count, ssym_count, sdev_count, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 278 | sdir_count, sfifo_count, ssock_count, sdup_files; |
| 279 | int sfragments; |
| 280 | int restore = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 281 | int threads; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 282 | |
| 283 | /* flag whether destination file is a block device */ |
| 284 | int block_device = 0; |
| 285 | |
| 286 | /* flag indicating whether files are sorted using sort list(s) */ |
| 287 | int sorted = 0; |
| 288 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 289 | /* save destination file name for deleting on error */ |
| 290 | char *destination_file = NULL; |
| 291 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 292 | /* recovery file for abnormal exit on appending */ |
| 293 | char recovery_file[1024] = ""; |
| 294 | int recover = TRUE; |
| 295 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 296 | /* struct describing a cache entry passed between threads */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 297 | struct file_buffer { |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 298 | struct cache *cache; |
Phillip Lougher | fe30cc6 | 2011-08-29 01:48:08 +0100 | [diff] [blame] | 299 | struct file_buffer *hash_next; |
| 300 | struct file_buffer *hash_prev; |
| 301 | struct file_buffer *free_next; |
| 302 | struct file_buffer *free_prev; |
| 303 | struct file_buffer *next; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 304 | long long file_size; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 305 | long long index; |
| 306 | long long block; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 307 | long long sequence; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 308 | int size; |
| 309 | int c_byte; |
Phillip Lougher | fe30cc6 | 2011-08-29 01:48:08 +0100 | [diff] [blame] | 310 | char keep; |
| 311 | char used; |
| 312 | char fragment; |
| 313 | char error; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 314 | char noD; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 315 | char data[0]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 316 | }; |
| 317 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 318 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 319 | /* struct describing queues used to pass data between threads */ |
| 320 | struct queue { |
| 321 | int size; |
| 322 | int readp; |
| 323 | int writep; |
| 324 | pthread_mutex_t mutex; |
| 325 | pthread_cond_t empty; |
| 326 | pthread_cond_t full; |
| 327 | void **data; |
| 328 | }; |
| 329 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 330 | |
| 331 | /* in memory uid tables */ |
| 332 | #define ID_ENTRIES 256 |
| 333 | #define ID_HASH(id) (id & (ID_ENTRIES - 1)) |
| 334 | #define ISA_UID 1 |
| 335 | #define ISA_GID 2 |
| 336 | struct id { |
| 337 | unsigned int id; |
| 338 | int index; |
| 339 | char flags; |
| 340 | struct id *next; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 341 | }; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 342 | struct id *id_hash_table[ID_ENTRIES]; |
| 343 | struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS]; |
| 344 | unsigned int uid_count = 0, guid_count = 0; |
| 345 | unsigned int sid_count = 0, suid_count = 0, sguid_count = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 346 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 347 | struct cache *reader_buffer, *writer_buffer, *fragment_buffer; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 348 | struct queue *to_reader, *from_reader, *to_writer, *from_writer, *from_deflate, |
| 349 | *to_frag; |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 350 | pthread_t *thread, *deflator_thread, *frag_deflator_thread; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 351 | pthread_mutex_t fragment_mutex; |
| 352 | pthread_cond_t fragment_waiting; |
| 353 | pthread_mutex_t pos_mutex; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 354 | struct pseudo *pseudo = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 355 | |
| 356 | /* user options that control parallelisation */ |
| 357 | int processors = -1; |
| 358 | /* default size of output buffer in Mbytes */ |
| 359 | #define WRITER_BUFFER_DEFAULT 512 |
| 360 | /* default size of input buffer in Mbytes */ |
| 361 | #define READER_BUFFER_DEFAULT 64 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 362 | /* default size of fragment buffer in Mbytes */ |
| 363 | #define FRAGMENT_BUFFER_DEFAULT 64 |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 364 | int writer_buffer_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 365 | |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 366 | /* compression operations */ |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 367 | static struct compressor *comp; |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 368 | int compressor_opts_parsed = 0; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 369 | void *stream = NULL; |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 370 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 371 | /* xattr stats */ |
| 372 | unsigned int xattr_bytes = 0, total_xattr_bytes = 0; |
| 373 | |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 374 | char *read_from_disk(long long start, unsigned int avail_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 375 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 376 | int type); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 377 | extern struct compressor *read_super(int fd, struct squashfs_super_block *sBlk, |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 378 | char *source); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 379 | extern long long read_filesystem(char *root_name, int fd, |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 380 | struct squashfs_super_block *sBlk, char **cinode_table, char **data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 381 | char **cdirectory_table, char **directory_data_cache, |
| 382 | unsigned int *last_directory_block, unsigned int *inode_dir_offset, |
| 383 | unsigned int *inode_dir_file_size, unsigned int *root_inode_size, |
| 384 | unsigned int *inode_dir_start_block, int *file_count, int *sym_count, |
| 385 | int *dev_count, int *dir_count, int *fifo_count, int *sock_count, |
| 386 | long long *uncompressed_file, unsigned int *uncompressed_inode, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 387 | unsigned int *uncompressed_directory, |
| 388 | unsigned int *inode_dir_inode_number, |
| 389 | unsigned int *inode_dir_parent_inode, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 390 | void (push_directory_entry)(char *, squashfs_inode, int, int), |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 391 | struct squashfs_fragment_entry **fragment_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 392 | squashfs_inode **inode_lookup_table); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 393 | extern int read_sort_file(char *filename, int source, char *source_path[]); |
| 394 | extern void sort_files_and_write(struct dir_info *dir); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 395 | struct file_info *duplicate(long long file_size, long long bytes, |
| 396 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 397 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 398 | unsigned short fragment_checksum, int checksum_flag); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 399 | struct dir_info *dir_scan1(char *, char *, struct pathnames *, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 400 | struct dir_ent *(_readdir)(struct dir_info *), int); |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 401 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 402 | void dir_scan3(struct dir_info *root, struct dir_info *dir); |
| 403 | void dir_scan4(struct dir_info *dir); |
| 404 | void dir_scan5(squashfs_inode *inode, struct dir_info *dir_info); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 405 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 406 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 407 | unsigned short checksum, unsigned short fragment_checksum, |
| 408 | int checksum_flag); |
plougher | 1d1c6bb | 2010-07-21 03:03:13 +0000 | [diff] [blame] | 409 | extern int generate_file_priorities(struct dir_info *dir, int priority, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 410 | struct stat *buf); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 411 | extern struct priority_entry *priority_list[65536]; |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 412 | long long generic_write_table(int, void *, int, void *, int); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 413 | void restorefs(); |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 414 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 415 | |
| 416 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 417 | struct queue *queue_init(int size) |
| 418 | { |
| 419 | struct queue *queue = malloc(sizeof(struct queue)); |
| 420 | |
| 421 | if(queue == NULL) |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 422 | goto failed; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 423 | |
plougher | 0d55d9e | 2010-12-16 04:50:21 +0000 | [diff] [blame] | 424 | queue->data = malloc(sizeof(void *) * (size + 1)); |
| 425 | if(queue->data == NULL) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 426 | free(queue); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 427 | goto failed; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | queue->size = size + 1; |
| 431 | queue->readp = queue->writep = 0; |
| 432 | pthread_mutex_init(&queue->mutex, NULL); |
| 433 | pthread_cond_init(&queue->empty, NULL); |
| 434 | pthread_cond_init(&queue->full, NULL); |
| 435 | |
| 436 | return queue; |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 437 | |
| 438 | failed: |
| 439 | BAD_ERROR("Out of memory in queue_init\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
| 443 | void queue_put(struct queue *queue, void *data) |
| 444 | { |
| 445 | int nextp; |
| 446 | |
| 447 | pthread_mutex_lock(&queue->mutex); |
| 448 | |
| 449 | while((nextp = (queue->writep + 1) % queue->size) == queue->readp) |
| 450 | pthread_cond_wait(&queue->full, &queue->mutex); |
| 451 | |
| 452 | queue->data[queue->writep] = data; |
| 453 | queue->writep = nextp; |
| 454 | pthread_cond_signal(&queue->empty); |
| 455 | pthread_mutex_unlock(&queue->mutex); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | void *queue_get(struct queue *queue) |
| 460 | { |
| 461 | void *data; |
| 462 | pthread_mutex_lock(&queue->mutex); |
| 463 | |
| 464 | while(queue->readp == queue->writep) |
| 465 | pthread_cond_wait(&queue->empty, &queue->mutex); |
| 466 | |
| 467 | data = queue->data[queue->readp]; |
| 468 | queue->readp = (queue->readp + 1) % queue->size; |
| 469 | pthread_cond_signal(&queue->full); |
| 470 | pthread_mutex_unlock(&queue->mutex); |
| 471 | |
| 472 | return data; |
| 473 | } |
| 474 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 475 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 476 | /* Cache status struct. Caches are used to keep |
| 477 | track of memory buffers passed between different threads */ |
| 478 | struct cache { |
| 479 | int max_buffers; |
| 480 | int count; |
| 481 | int buffer_size; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 482 | pthread_mutex_t mutex; |
| 483 | pthread_cond_t wait_for_free; |
| 484 | struct file_buffer *free_list; |
| 485 | struct file_buffer *hash_table[65536]; |
| 486 | }; |
| 487 | |
| 488 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 489 | #define INSERT_LIST(NAME, TYPE) \ |
| 490 | void insert_##NAME##_list(TYPE **list, TYPE *entry) { \ |
| 491 | if(*list) { \ |
| 492 | entry->NAME##_next = *list; \ |
| 493 | entry->NAME##_prev = (*list)->NAME##_prev; \ |
| 494 | (*list)->NAME##_prev->NAME##_next = entry; \ |
| 495 | (*list)->NAME##_prev = entry; \ |
| 496 | } else { \ |
| 497 | *list = entry; \ |
| 498 | entry->NAME##_prev = entry->NAME##_next = entry; \ |
| 499 | } \ |
| 500 | } |
| 501 | |
| 502 | |
| 503 | #define REMOVE_LIST(NAME, TYPE) \ |
| 504 | void remove_##NAME##_list(TYPE **list, TYPE *entry) { \ |
| 505 | if(entry->NAME##_prev == entry && entry->NAME##_next == entry) { \ |
| 506 | /* only this entry in the list */ \ |
| 507 | *list = NULL; \ |
| 508 | } else if(entry->NAME##_prev != NULL && entry->NAME##_next != NULL) { \ |
| 509 | /* more than one entry in the list */ \ |
| 510 | entry->NAME##_next->NAME##_prev = entry->NAME##_prev; \ |
| 511 | entry->NAME##_prev->NAME##_next = entry->NAME##_next; \ |
| 512 | if(*list == entry) \ |
| 513 | *list = entry->NAME##_next; \ |
| 514 | } \ |
| 515 | entry->NAME##_prev = entry->NAME##_next = NULL; \ |
| 516 | } |
| 517 | |
| 518 | |
| 519 | #define CALCULATE_HASH(start) (start & 0xffff) \ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 520 | |
| 521 | |
| 522 | /* Called with the cache mutex held */ |
| 523 | void insert_hash_table(struct cache *cache, struct file_buffer *entry) |
| 524 | { |
| 525 | int hash = CALCULATE_HASH(entry->index); |
| 526 | |
| 527 | entry->hash_next = cache->hash_table[hash]; |
| 528 | cache->hash_table[hash] = entry; |
| 529 | entry->hash_prev = NULL; |
| 530 | if(entry->hash_next) |
| 531 | entry->hash_next->hash_prev = entry; |
| 532 | } |
| 533 | |
| 534 | |
| 535 | /* Called with the cache mutex held */ |
| 536 | void remove_hash_table(struct cache *cache, struct file_buffer *entry) |
| 537 | { |
| 538 | if(entry->hash_prev) |
| 539 | entry->hash_prev->hash_next = entry->hash_next; |
| 540 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 541 | cache->hash_table[CALCULATE_HASH(entry->index)] = |
| 542 | entry->hash_next; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 543 | if(entry->hash_next) |
| 544 | entry->hash_next->hash_prev = entry->hash_prev; |
| 545 | |
| 546 | entry->hash_prev = entry->hash_next = NULL; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | /* Called with the cache mutex held */ |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 551 | INSERT_LIST(free, struct file_buffer) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 552 | |
| 553 | /* Called with the cache mutex held */ |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 554 | REMOVE_LIST(free, struct file_buffer) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 555 | |
| 556 | |
| 557 | struct cache *cache_init(int buffer_size, int max_buffers) |
| 558 | { |
| 559 | struct cache *cache = malloc(sizeof(struct cache)); |
| 560 | |
| 561 | if(cache == NULL) |
plougher | 9ca649c | 2010-07-21 00:46:11 +0000 | [diff] [blame] | 562 | BAD_ERROR("Out of memory in cache_init\n"); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 563 | |
| 564 | cache->max_buffers = max_buffers; |
| 565 | cache->buffer_size = buffer_size; |
| 566 | cache->count = 0; |
| 567 | cache->free_list = NULL; |
| 568 | memset(cache->hash_table, 0, sizeof(struct file_buffer *) * 65536); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 569 | pthread_mutex_init(&cache->mutex, NULL); |
| 570 | pthread_cond_init(&cache->wait_for_free, NULL); |
| 571 | |
| 572 | return cache; |
| 573 | } |
| 574 | |
| 575 | |
| 576 | struct file_buffer *cache_lookup(struct cache *cache, long long index) |
| 577 | { |
| 578 | /* Lookup block in the cache, if found return with usage count |
| 579 | * incremented, if not found return NULL */ |
| 580 | int hash = CALCULATE_HASH(index); |
| 581 | struct file_buffer *entry; |
| 582 | |
| 583 | pthread_mutex_lock(&cache->mutex); |
| 584 | |
| 585 | for(entry = cache->hash_table[hash]; entry; entry = entry->hash_next) |
| 586 | if(entry->index == index) |
| 587 | break; |
| 588 | |
| 589 | if(entry) { |
| 590 | /* found the block in the cache, increment used count and |
| 591 | * if necessary remove from free list so it won't disappear |
| 592 | */ |
| 593 | entry->used ++; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 594 | remove_free_list(&cache->free_list, entry); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | pthread_mutex_unlock(&cache->mutex); |
| 598 | |
| 599 | return entry; |
| 600 | } |
| 601 | |
| 602 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 603 | struct file_buffer *cache_get(struct cache *cache, long long index, int keep) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 604 | { |
| 605 | /* Get a free block out of the cache indexed on index. */ |
| 606 | struct file_buffer *entry; |
| 607 | |
| 608 | pthread_mutex_lock(&cache->mutex); |
| 609 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 610 | while(1) { |
| 611 | /* first try to get a block from the free list */ |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 612 | if(first_freelist && cache->free_list) { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 613 | /* a block on the free_list is a "keep" block */ |
| 614 | entry = cache->free_list; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 615 | remove_free_list(&cache->free_list, entry); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 616 | remove_hash_table(cache, entry); |
| 617 | break; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 618 | } else if(cache->count < cache->max_buffers) { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 619 | /* next try to allocate new block */ |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 620 | entry = malloc(sizeof(struct file_buffer) + |
| 621 | cache->buffer_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 622 | if(entry == NULL) |
| 623 | goto failed; |
| 624 | entry->cache = cache; |
| 625 | entry->free_prev = entry->free_next = NULL; |
| 626 | cache->count ++; |
| 627 | break; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 628 | } else if(!first_freelist && cache->free_list) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 629 | /* a block on the free_list is a "keep" block */ |
| 630 | entry = cache->free_list; |
| 631 | remove_free_list(&cache->free_list, entry); |
| 632 | remove_hash_table(cache, entry); |
| 633 | break; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 634 | } else |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 635 | /* wait for a block */ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 636 | pthread_cond_wait(&cache->wait_for_free, &cache->mutex); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 637 | } |
| 638 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 639 | /* initialise block and if a keep block insert into the hash table */ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 640 | entry->used = 1; |
| 641 | entry->error = FALSE; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 642 | entry->keep = keep; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 643 | if(keep) { |
| 644 | entry->index = index; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 645 | insert_hash_table(cache, entry); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 646 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 647 | pthread_mutex_unlock(&cache->mutex); |
| 648 | |
| 649 | return entry; |
| 650 | |
| 651 | failed: |
| 652 | pthread_mutex_unlock(&cache->mutex); |
plougher | cc6896d | 2010-07-21 00:56:25 +0000 | [diff] [blame] | 653 | BAD_ERROR("Out of memory in cache_get\n"); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 654 | } |
| 655 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 656 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 657 | void cache_rehash(struct file_buffer *entry, long long index) |
| 658 | { |
| 659 | struct cache *cache = entry->cache; |
| 660 | |
| 661 | pthread_mutex_lock(&cache->mutex); |
| 662 | if(entry->keep) |
| 663 | remove_hash_table(cache, entry); |
| 664 | entry->keep = TRUE; |
| 665 | entry->index = index; |
| 666 | insert_hash_table(cache, entry); |
| 667 | pthread_mutex_unlock(&cache->mutex); |
| 668 | } |
| 669 | |
| 670 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 671 | void cache_block_put(struct file_buffer *entry) |
| 672 | { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 673 | struct cache *cache; |
| 674 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 675 | /* finished with this cache entry, once the usage count reaches zero it |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 676 | * 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] | 677 | * blocks remain accessible via the hash table they can be found |
| 678 | * getting a new lease of life before they are reused. */ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 679 | |
| 680 | if(entry == NULL) |
| 681 | return; |
| 682 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 683 | cache = entry->cache; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 684 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 685 | pthread_mutex_lock(&cache->mutex); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 686 | |
| 687 | entry->used --; |
| 688 | if(entry->used == 0) { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 689 | if(entry->keep) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 690 | insert_free_list(&cache->free_list, entry); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 691 | else { |
| 692 | free(entry); |
| 693 | cache->count --; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 694 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 695 | |
| 696 | /* One or more threads may be waiting on this block */ |
| 697 | pthread_cond_signal(&cache->wait_for_free); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 698 | } |
| 699 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 700 | pthread_mutex_unlock(&cache->mutex); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 704 | #define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \ |
| 705 | + (((char *)A) - data_cache))) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 706 | |
| 707 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 708 | inline void waitforthread(int i) |
| 709 | { |
| 710 | TRACE("Waiting for thread %d\n", i); |
| 711 | while(thread[i] != 0) |
| 712 | sched_yield(); |
| 713 | } |
| 714 | |
| 715 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 716 | void restorefs() |
| 717 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 718 | int i; |
| 719 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 720 | if(thread == NULL || thread[0] == 0) |
| 721 | return; |
| 722 | |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 723 | if(restoring++) |
| 724 | /* |
plougher | 5b290d2 | 2010-08-12 23:52:21 +0000 | [diff] [blame] | 725 | * Recursive failure when trying to restore filesystem! |
| 726 | * Nothing to do except to exit, otherwise we'll just appear |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 727 | * to hang. The user should be able to restore from the |
plougher | 5b290d2 | 2010-08-12 23:52:21 +0000 | [diff] [blame] | 728 | * recovery file (which is why it was added, in case of |
| 729 | * catastrophic failure in Mksquashfs) |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 730 | */ |
| 731 | exit(1); |
| 732 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 733 | ERROR("Exiting - restoring original filesystem!\n\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 734 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 735 | for(i = 0; i < 2 + processors * 2; i++) |
plougher | 5aa1816 | 2007-12-13 12:15:21 +0000 | [diff] [blame] | 736 | if(thread[i]) |
| 737 | pthread_kill(thread[i], SIGUSR1); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 738 | for(i = 0; i < 2 + processors * 2; i++) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 739 | waitforthread(i); |
| 740 | TRACE("All threads in signal handler\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 741 | bytes = sbytes; |
| 742 | memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 743 | memcpy(directory_data_cache, sdirectory_data_cache, |
| 744 | sdirectory_cache_bytes); |
| 745 | directory_cache_bytes = sdirectory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 746 | inode_bytes = sinode_bytes; |
| 747 | directory_bytes = sdirectory_bytes; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 748 | memcpy(directory_table + directory_bytes, sdirectory_compressed, |
| 749 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 750 | directory_bytes += sdirectory_compressed_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 751 | total_bytes = stotal_bytes; |
| 752 | total_inode_bytes = stotal_inode_bytes; |
| 753 | total_directory_bytes = stotal_directory_bytes; |
| 754 | inode_count = sinode_count; |
| 755 | file_count = sfile_count; |
| 756 | sym_count = ssym_count; |
| 757 | dev_count = sdev_count; |
| 758 | dir_count = sdir_count; |
| 759 | fifo_count = sfifo_count; |
| 760 | sock_count = ssock_count; |
| 761 | dup_files = sdup_files; |
| 762 | fragments = sfragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 763 | id_count = sid_count; |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 764 | restore_xattrs(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 765 | longjmp(env, 1); |
| 766 | } |
| 767 | |
| 768 | |
| 769 | void sighandler() |
| 770 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 771 | if(++interrupted > 2) |
| 772 | return; |
| 773 | if(interrupted == 2) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 774 | restorefs(); |
| 775 | else { |
| 776 | ERROR("Interrupting will restore original filesystem!\n"); |
| 777 | ERROR("Interrupt again to quit\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
| 781 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 782 | void sighandler2() |
| 783 | { |
| 784 | EXIT_MKSQUASHFS(); |
| 785 | } |
| 786 | |
| 787 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 788 | void sigusr1_handler() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 789 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 790 | int i; |
| 791 | sigset_t sigmask; |
| 792 | pthread_t thread_id = pthread_self(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 793 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 794 | for(i = 0; i < (2 + processors * 2) && thread[i] != thread_id; i++); |
| 795 | thread[i] = (pthread_t) 0; |
| 796 | |
plougher | 07966f7 | 2007-11-14 10:54:45 +0000 | [diff] [blame] | 797 | TRACE("Thread %d(%p) in sigusr1_handler\n", i, &thread_id); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 798 | |
| 799 | sigemptyset(&sigmask); |
| 800 | sigaddset(&sigmask, SIGINT); |
| 801 | sigaddset(&sigmask, SIGQUIT); |
| 802 | sigaddset(&sigmask, SIGUSR1); |
| 803 | while(1) { |
| 804 | sigsuspend(&sigmask); |
| 805 | TRACE("After wait in sigusr1_handler :(\n"); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 810 | int mangle2(void *strm, char *d, char *s, int size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 811 | int block_size, int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 812 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 813 | int error, c_byte = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 814 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 815 | if(!uncompressed) { |
plougher | bfb876c | 2010-12-31 07:48:01 +0000 | [diff] [blame] | 816 | c_byte = compressor_compress(comp, strm, d, s, size, block_size, |
| 817 | &error); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 818 | if(c_byte == -1) |
| 819 | BAD_ERROR("mangle2:: %s compress failed with error " |
| 820 | "code %d\n", comp->name, error); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 821 | } |
| 822 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 823 | if(c_byte == 0 || c_byte >= size) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 824 | memcpy(d, s, size); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 825 | return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : |
| 826 | SQUASHFS_COMPRESSED_BIT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 827 | } |
| 828 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 829 | return c_byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 833 | int mangle(char *d, char *s, int size, int block_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 834 | int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 835 | { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 836 | return mangle2(stream, d, s, size, block_size, uncompressed, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 837 | data_block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 841 | void *get_inode(int req_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 842 | { |
| 843 | int data_space; |
| 844 | unsigned short c_byte; |
| 845 | |
| 846 | while(cache_bytes >= SQUASHFS_METADATA_SIZE) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 847 | if((inode_size - inode_bytes) < |
| 848 | ((SQUASHFS_METADATA_SIZE << 1)) + 2) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 849 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 850 | (SQUASHFS_METADATA_SIZE << 1) + 2); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 851 | if(it == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 852 | goto failed; |
| 853 | } |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 854 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 855 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
| 856 | } |
| 857 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 858 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 859 | data_cache, SQUASHFS_METADATA_SIZE, |
| 860 | SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 861 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 862 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 863 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 864 | total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 865 | memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 866 | cache_bytes - SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 867 | cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 868 | } |
| 869 | |
| 870 | data_space = (cache_size - cache_bytes); |
| 871 | if(data_space < req_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 872 | int realloc_size = cache_size == 0 ? |
| 873 | ((req_size + SQUASHFS_METADATA_SIZE) & |
| 874 | ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - |
| 875 | data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 876 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 877 | void *dc = realloc(data_cache, cache_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 878 | realloc_size); |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 879 | if(dc == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 880 | goto failed; |
| 881 | } |
| 882 | cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 883 | data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | cache_bytes += req_size; |
| 887 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 888 | return data_cache + cache_bytes - req_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 889 | |
| 890 | failed: |
| 891 | BAD_ERROR("Out of memory in inode table reallocation!\n"); |
| 892 | } |
| 893 | |
| 894 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 895 | int read_bytes(int fd, void *buff, int bytes) |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 896 | { |
| 897 | int res, count; |
| 898 | |
| 899 | for(count = 0; count < bytes; count += res) { |
| 900 | res = read(fd, buff + count, bytes - count); |
| 901 | if(res < 1) { |
plougher | 96f85a1 | 2009-03-29 22:39:59 +0000 | [diff] [blame] | 902 | if(res == 0) |
| 903 | goto bytes_read; |
| 904 | else if(errno != EINTR) { |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 905 | ERROR("Read failed because %s\n", |
| 906 | strerror(errno)); |
| 907 | return -1; |
| 908 | } else |
| 909 | res = 0; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | bytes_read: |
| 914 | return count; |
| 915 | } |
| 916 | |
| 917 | |
plougher | 3306cb2 | 2010-06-18 04:22:44 +0000 | [diff] [blame] | 918 | int read_fs_bytes(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 919 | { |
| 920 | off_t off = byte; |
| 921 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 922 | TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 923 | byte, bytes); |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 924 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 925 | pthread_mutex_lock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 926 | if(lseek(fd, off, SEEK_SET) == -1) { |
| 927 | ERROR("Lseek on destination failed because %s\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 928 | strerror(errno)); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 929 | goto failed; |
| 930 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 931 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 932 | if(read_bytes(fd, buff, bytes) < bytes) { |
| 933 | ERROR("Read on destination failed\n"); |
| 934 | goto failed; |
| 935 | } |
| 936 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 937 | pthread_mutex_unlock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 938 | return 1; |
| 939 | |
| 940 | failed: |
| 941 | pthread_mutex_unlock(&pos_mutex); |
| 942 | return 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 946 | int write_bytes(int fd, void *buff, int bytes) |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 947 | { |
| 948 | int res, count; |
| 949 | |
| 950 | for(count = 0; count < bytes; count += res) { |
| 951 | res = write(fd, buff + count, bytes - count); |
| 952 | if(res == -1) { |
| 953 | if(errno != EINTR) { |
| 954 | ERROR("Write failed because %s\n", |
| 955 | strerror(errno)); |
| 956 | return -1; |
| 957 | } |
| 958 | res = 0; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 966 | void write_destination(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 967 | { |
| 968 | off_t off = byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 969 | |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 970 | if(!restoring) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 971 | pthread_mutex_lock(&pos_mutex); |
| 972 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 973 | if(lseek(fd, off, SEEK_SET) == -1) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 974 | BAD_ERROR("Lseek on destination failed because %s\n", |
| 975 | strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 976 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 977 | if(write_bytes(fd, buff, bytes) == -1) |
| 978 | BAD_ERROR("Write on destination failed\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 979 | |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 980 | if(!restoring) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 981 | pthread_mutex_unlock(&pos_mutex); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | |
| 985 | long long write_inodes() |
| 986 | { |
| 987 | unsigned short c_byte; |
| 988 | int avail_bytes; |
| 989 | char *datap = data_cache; |
| 990 | long long start_bytes = bytes; |
| 991 | |
| 992 | while(cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 993 | if(inode_size - inode_bytes < |
| 994 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 995 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 996 | ((SQUASHFS_METADATA_SIZE << 1) + 2)); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 997 | if(it == NULL) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 998 | BAD_ERROR("Out of memory in inode table " |
| 999 | "reallocation!\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1000 | } |
| 1001 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 1002 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1003 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1004 | avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 1005 | SQUASHFS_METADATA_SIZE : cache_bytes; |
| 1006 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap, |
| 1007 | avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1008 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1009 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1010 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 1011 | total_inode_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1012 | datap += avail_bytes; |
| 1013 | cache_bytes -= avail_bytes; |
| 1014 | } |
| 1015 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 1016 | write_destination(fd, bytes, inode_bytes, inode_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1017 | bytes += inode_bytes; |
| 1018 | |
| 1019 | return start_bytes; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | long long write_directories() |
| 1024 | { |
| 1025 | unsigned short c_byte; |
| 1026 | int avail_bytes; |
| 1027 | char *directoryp = directory_data_cache; |
| 1028 | long long start_bytes = bytes; |
| 1029 | |
| 1030 | while(directory_cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1031 | if(directory_size - directory_bytes < |
| 1032 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1033 | void *dt = realloc(directory_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1034 | directory_size + ((SQUASHFS_METADATA_SIZE << 1) |
| 1035 | + 2)); |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1036 | if(dt == NULL) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1037 | BAD_ERROR("Out of memory in directory table " |
| 1038 | "reallocation!\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1039 | } |
| 1040 | directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1041 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1042 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1043 | avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 1044 | SQUASHFS_METADATA_SIZE : directory_cache_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1045 | c_byte = mangle(directory_table + directory_bytes + |
| 1046 | BLOCK_OFFSET, directoryp, avail_bytes, |
| 1047 | SQUASHFS_METADATA_SIZE, noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1048 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1049 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1050 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1051 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1052 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1053 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1054 | total_directory_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1055 | directoryp += avail_bytes; |
| 1056 | directory_cache_bytes -= avail_bytes; |
| 1057 | } |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 1058 | write_destination(fd, bytes, directory_bytes, directory_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1059 | bytes += directory_bytes; |
| 1060 | |
| 1061 | return start_bytes; |
| 1062 | } |
| 1063 | |
| 1064 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1065 | long long write_id_table() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1066 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1067 | unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1068 | unsigned int p[id_count]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1069 | int i; |
| 1070 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1071 | 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] | 1072 | for(i = 0; i < id_count; i++) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1073 | 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] | 1074 | SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1077 | return generic_write_table(id_bytes, p, 0, NULL, noI); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1081 | struct id *get_id(unsigned int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1082 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1083 | int hash = ID_HASH(id); |
| 1084 | struct id *entry = id_hash_table[hash]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1085 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1086 | for(; entry; entry = entry->next) |
| 1087 | if(entry->id == id) |
| 1088 | break; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1089 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1090 | return entry; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1094 | struct id *create_id(unsigned int id) |
| 1095 | { |
| 1096 | int hash = ID_HASH(id); |
| 1097 | struct id *entry = malloc(sizeof(struct id)); |
| 1098 | if(entry == NULL) |
| 1099 | BAD_ERROR("Out of memory in create_id\n"); |
| 1100 | entry->id = id; |
| 1101 | entry->index = id_count ++; |
| 1102 | entry->flags = 0; |
| 1103 | entry->next = id_hash_table[hash]; |
| 1104 | id_hash_table[hash] = entry; |
| 1105 | id_table[entry->index] = entry; |
| 1106 | return entry; |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | unsigned int get_uid(unsigned int uid) |
| 1111 | { |
| 1112 | struct id *entry = get_id(uid); |
| 1113 | |
| 1114 | if(entry == NULL) { |
| 1115 | if(id_count == SQUASHFS_IDS) |
| 1116 | BAD_ERROR("Out of uids!\n"); |
| 1117 | entry = create_id(uid); |
| 1118 | } |
| 1119 | |
| 1120 | if((entry->flags & ISA_UID) == 0) { |
| 1121 | entry->flags |= ISA_UID; |
| 1122 | uid_count ++; |
| 1123 | } |
| 1124 | |
| 1125 | return entry->index; |
| 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | unsigned int get_guid(unsigned int guid) |
| 1130 | { |
| 1131 | struct id *entry = get_id(guid); |
| 1132 | |
| 1133 | if(entry == NULL) { |
| 1134 | if(id_count == SQUASHFS_IDS) |
| 1135 | BAD_ERROR("Out of gids!\n"); |
| 1136 | entry = create_id(guid); |
| 1137 | } |
| 1138 | |
| 1139 | if((entry->flags & ISA_GID) == 0) { |
| 1140 | entry->flags |= ISA_GID; |
| 1141 | guid_count ++; |
| 1142 | } |
| 1143 | |
| 1144 | return entry->index; |
| 1145 | } |
| 1146 | |
| 1147 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 1148 | char *_pathname(struct dir_ent *dir_ent, char *pathname) |
| 1149 | { |
| 1150 | if (dir_ent->nonstandard_pathname) |
| 1151 | return dir_ent->nonstandard_pathname; |
| 1152 | |
| 1153 | strcpy(pathname, dir_ent->our_dir->pathname); |
| 1154 | strcat(pathname, "/"); |
| 1155 | strcat(pathname, dir_ent->source_name ? : dir_ent->name); |
| 1156 | |
| 1157 | return pathname; |
| 1158 | } |
| 1159 | |
| 1160 | |
| 1161 | char *pathname(struct dir_ent *dir_ent) |
| 1162 | { |
| 1163 | static char pathname[1024]; |
| 1164 | |
| 1165 | return _pathname(dir_ent, pathname); |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | char *pathname_reader(struct dir_ent *dir_ent) |
| 1170 | { |
| 1171 | static char pathname[1024]; |
| 1172 | |
| 1173 | return _pathname(dir_ent, pathname); |
| 1174 | } |
| 1175 | |
| 1176 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 1177 | char *subpathname(struct dir_ent *dir_ent) |
| 1178 | { |
| 1179 | static char subpath[1024]; |
| 1180 | |
| 1181 | if(dir_ent->our_dir->subpath[0] != '\0') { |
| 1182 | strcpy(subpath, dir_ent->our_dir->subpath); |
| 1183 | strcat(subpath, "/"); |
Phillip Lougher | 9e2f123 | 2012-10-01 02:26:09 +0100 | [diff] [blame] | 1184 | strcat(subpath, dir_ent->name); |
Phillip Lougher | 90e0825 | 2012-10-05 04:33:35 +0100 | [diff] [blame] | 1185 | } else { |
| 1186 | strcpy(subpath, "/"); |
| 1187 | strcat(subpath, dir_ent->name); |
| 1188 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 1189 | |
| 1190 | return subpath; |
| 1191 | } |
| 1192 | |
| 1193 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1194 | inline unsigned int get_inode_no(struct inode_info *inode) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 1195 | { |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1196 | return inode->inode_number; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1200 | inline unsigned int get_parent_no(struct dir_info *dir) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 1201 | { |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 1202 | return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1206 | int create_inode(squashfs_inode *i_no, struct dir_info *dir_info, |
| 1207 | struct dir_ent *dir_ent, int type, long long byte_size, |
| 1208 | long long start_block, unsigned int offset, unsigned int *block_list, |
| 1209 | struct fragment *fragment, struct directory *dir_in, long long sparse) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1210 | { |
| 1211 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 8973a12 | 2010-12-31 09:52:45 +0000 | [diff] [blame] | 1212 | union squashfs_inode_header inode_header; |
plougher | 857d0dd | 2010-12-31 21:03:13 +0000 | [diff] [blame] | 1213 | struct squashfs_base_inode_header *base = &inode_header.base; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1214 | void *inode; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 1215 | char *filename = pathname(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1216 | int nlink = dir_ent->inode->nlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1217 | int xattr = read_xattrs(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1218 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1219 | switch(type) { |
| 1220 | case SQUASHFS_FILE_TYPE: |
| 1221 | if(dir_ent->inode->nlink > 1 || |
| 1222 | byte_size >= (1LL << 32) || |
| 1223 | start_block >= (1LL << 32) || |
| 1224 | sparse || IS_XATTR(xattr)) |
| 1225 | type = SQUASHFS_LREG_TYPE; |
| 1226 | break; |
| 1227 | case SQUASHFS_DIR_TYPE: |
| 1228 | if(dir_info->dir_is_ldir || IS_XATTR(xattr)) |
| 1229 | type = SQUASHFS_LDIR_TYPE; |
| 1230 | break; |
| 1231 | case SQUASHFS_SYMLINK_TYPE: |
| 1232 | if(IS_XATTR(xattr)) |
| 1233 | type = SQUASHFS_LSYMLINK_TYPE; |
| 1234 | break; |
| 1235 | case SQUASHFS_BLKDEV_TYPE: |
| 1236 | if(IS_XATTR(xattr)) |
| 1237 | type = SQUASHFS_LBLKDEV_TYPE; |
| 1238 | break; |
| 1239 | case SQUASHFS_CHRDEV_TYPE: |
| 1240 | if(IS_XATTR(xattr)) |
| 1241 | type = SQUASHFS_LCHRDEV_TYPE; |
| 1242 | break; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1243 | case SQUASHFS_FIFO_TYPE: |
| 1244 | if(IS_XATTR(xattr)) |
| 1245 | type = SQUASHFS_LFIFO_TYPE; |
| 1246 | break; |
| 1247 | case SQUASHFS_SOCKET_TYPE: |
| 1248 | if(IS_XATTR(xattr)) |
| 1249 | type = SQUASHFS_LSOCKET_TYPE; |
| 1250 | break; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1253 | base->mode = SQUASHFS_MODE(buf->st_mode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1254 | base->uid = get_uid((unsigned int) global_uid == -1 ? |
| 1255 | buf->st_uid : global_uid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1256 | base->inode_type = type; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1257 | base->guid = get_guid((unsigned int) global_gid == -1 ? |
| 1258 | buf->st_gid : global_gid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1259 | base->mtime = buf->st_mtime; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1260 | base->inode_number = get_inode_no(dir_ent->inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1261 | |
| 1262 | if(type == SQUASHFS_FILE_TYPE) { |
| 1263 | int i; |
plougher | 8701ed6 | 2010-12-31 20:38:38 +0000 | [diff] [blame] | 1264 | struct squashfs_reg_inode_header *reg = &inode_header.reg; |
| 1265 | size_t off = offsetof(struct squashfs_reg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1266 | |
| 1267 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1268 | reg->file_size = byte_size; |
| 1269 | reg->start_block = start_block; |
| 1270 | reg->fragment = fragment->index; |
| 1271 | reg->offset = fragment->offset; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1272 | SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode); |
| 1273 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1274 | TRACE("File inode, file_size %lld, start_block 0x%llx, blocks " |
| 1275 | "%d, fragment %d, offset %d, size %d\n", byte_size, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1276 | start_block, offset, fragment->index, fragment->offset, |
| 1277 | fragment->size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1278 | for(i = 0; i < offset; i++) |
| 1279 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 1280 | } |
| 1281 | else if(type == SQUASHFS_LREG_TYPE) { |
| 1282 | int i; |
plougher | 1e6ac4a | 2010-12-31 20:42:02 +0000 | [diff] [blame] | 1283 | struct squashfs_lreg_inode_header *reg = &inode_header.lreg; |
| 1284 | size_t off = offsetof(struct squashfs_lreg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1285 | |
| 1286 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1287 | reg->nlink = nlink; |
| 1288 | reg->file_size = byte_size; |
| 1289 | reg->start_block = start_block; |
| 1290 | reg->fragment = fragment->index; |
| 1291 | reg->offset = fragment->offset; |
plougher | f5a674d | 2009-03-25 05:38:27 +0000 | [diff] [blame] | 1292 | if(sparse && sparse >= byte_size) |
| 1293 | sparse = byte_size - 1; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1294 | reg->sparse = sparse; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1295 | reg->xattr = xattr; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1296 | SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode); |
| 1297 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1298 | TRACE("Long file inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1299 | "blocks %d, fragment %d, offset %d, size %d, nlink %d" |
| 1300 | "\n", byte_size, start_block, offset, fragment->index, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1301 | fragment->offset, fragment->size, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1302 | for(i = 0; i < offset; i++) |
| 1303 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 1304 | } |
| 1305 | else if(type == SQUASHFS_LDIR_TYPE) { |
| 1306 | int i; |
| 1307 | unsigned char *p; |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 1308 | struct squashfs_ldir_inode_header *dir = &inode_header.ldir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1309 | struct cached_dir_index *index = dir_in->index; |
| 1310 | unsigned int i_count = dir_in->i_count; |
| 1311 | unsigned int i_size = dir_in->i_size; |
| 1312 | |
| 1313 | if(byte_size >= 1 << 27) |
| 1314 | BAD_ERROR("directory greater than 2^27-1 bytes!\n"); |
| 1315 | |
| 1316 | inode = get_inode(sizeof(*dir) + i_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1317 | dir->inode_type = SQUASHFS_LDIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1318 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 1319 | dir->file_size = byte_size; |
| 1320 | dir->offset = offset; |
| 1321 | dir->start_block = start_block; |
| 1322 | dir->i_count = i_count; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1323 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1324 | dir->xattr = xattr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1325 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1326 | SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode); |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 1327 | p = inode + offsetof(struct squashfs_ldir_inode_header, index); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1328 | for(i = 0; i < i_count; i++) { |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1329 | SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p); |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1330 | p += offsetof(struct squashfs_dir_index, name); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1331 | memcpy(p, index[i].name, index[i].index.size + 1); |
| 1332 | p += index[i].index.size + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1333 | } |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1334 | TRACE("Long directory inode, file_size %lld, start_block " |
| 1335 | "0x%llx, offset 0x%x, nlink %d\n", byte_size, |
| 1336 | start_block, offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1337 | } |
| 1338 | else if(type == SQUASHFS_DIR_TYPE) { |
plougher | 9d80a60 | 2010-12-31 20:47:24 +0000 | [diff] [blame] | 1339 | struct squashfs_dir_inode_header *dir = &inode_header.dir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1340 | |
| 1341 | inode = get_inode(sizeof(*dir)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1342 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 1343 | dir->file_size = byte_size; |
| 1344 | dir->offset = offset; |
| 1345 | dir->start_block = start_block; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1346 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1347 | SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1348 | TRACE("Directory inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1349 | "offset 0x%x, nlink %d\n", byte_size, start_block, |
| 1350 | offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1351 | } |
| 1352 | else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) { |
plougher | c70c633 | 2010-12-31 20:11:09 +0000 | [diff] [blame] | 1353 | struct squashfs_dev_inode_header *dev = &inode_header.dev; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1354 | unsigned int major = major(buf->st_rdev); |
| 1355 | unsigned int minor = minor(buf->st_rdev); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1356 | |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1357 | if(major > 0xfff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1358 | ERROR("Major %d out of range in device node %s, " |
| 1359 | "truncating to %d\n", major, filename, |
| 1360 | major & 0xfff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1361 | major &= 0xfff; |
| 1362 | } |
| 1363 | if(minor > 0xfffff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1364 | ERROR("Minor %d out of range in device node %s, " |
| 1365 | "truncating to %d\n", minor, filename, |
| 1366 | minor & 0xfffff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1367 | minor &= 0xfffff; |
| 1368 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1369 | inode = get_inode(sizeof(*dev)); |
| 1370 | dev->nlink = nlink; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1371 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1372 | ((minor & ~0xff) << 12); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1373 | SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1374 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1375 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1376 | else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) { |
plougher | 0b4ee5b | 2010-12-31 20:14:00 +0000 | [diff] [blame] | 1377 | struct squashfs_ldev_inode_header *dev = &inode_header.ldev; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1378 | unsigned int major = major(buf->st_rdev); |
| 1379 | unsigned int minor = minor(buf->st_rdev); |
| 1380 | |
| 1381 | if(major > 0xfff) { |
| 1382 | ERROR("Major %d out of range in device node %s, " |
| 1383 | "truncating to %d\n", major, filename, |
| 1384 | major & 0xfff); |
| 1385 | major &= 0xfff; |
| 1386 | } |
| 1387 | if(minor > 0xfffff) { |
| 1388 | ERROR("Minor %d out of range in device node %s, " |
| 1389 | "truncating to %d\n", minor, filename, |
| 1390 | minor & 0xfffff); |
| 1391 | minor &= 0xfffff; |
| 1392 | } |
| 1393 | inode = get_inode(sizeof(*dev)); |
| 1394 | dev->nlink = nlink; |
| 1395 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1396 | ((minor & ~0xff) << 12); |
| 1397 | dev->xattr = xattr; |
| 1398 | SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode); |
| 1399 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
| 1400 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1401 | else if(type == SQUASHFS_SYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1402 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1403 | int byte; |
| 1404 | char buff[65536]; |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1405 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1406 | |
plougher | 5cf38b8 | 2010-12-16 04:55:32 +0000 | [diff] [blame] | 1407 | byte = readlink(filename, buff, 65536); |
| 1408 | if(byte == -1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1409 | ERROR("Failed to read symlink %s, creating empty " |
| 1410 | "symlink\n", filename); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1411 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | if(byte == 65536) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1415 | ERROR("Symlink %s is greater than 65536 bytes! " |
| 1416 | "Creating empty symlink\n", filename); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1417 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | inode = get_inode(sizeof(*symlink) + byte); |
| 1421 | symlink->nlink = nlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1422 | symlink->symlink_size = byte; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1423 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1424 | strncpy(inode + off, buff, byte); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1425 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1426 | nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1427 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1428 | else if(type == SQUASHFS_LSYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1429 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1430 | int byte; |
| 1431 | char buff[65536]; |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1432 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1433 | |
plougher | df2b9aa | 2010-12-16 04:56:23 +0000 | [diff] [blame] | 1434 | byte = readlink(filename, buff, 65536); |
| 1435 | if(byte == -1) { |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1436 | ERROR("Failed to read symlink %s, creating empty " |
| 1437 | "symlink\n", filename); |
| 1438 | byte = 0; |
| 1439 | } |
| 1440 | |
| 1441 | if(byte == 65536) { |
| 1442 | ERROR("Symlink %s is greater than 65536 bytes! " |
| 1443 | "Creating empty symlink\n", filename); |
| 1444 | byte = 0; |
| 1445 | } |
| 1446 | |
| 1447 | inode = get_inode(sizeof(*symlink) + byte + |
| 1448 | sizeof(unsigned int)); |
| 1449 | symlink->nlink = nlink; |
| 1450 | symlink->symlink_size = byte; |
| 1451 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1452 | strncpy(inode + off, buff, byte); |
| 1453 | SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1); |
| 1454 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1455 | nlink); |
| 1456 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1457 | else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) { |
plougher | e56b986 | 2010-12-31 10:58:35 +0000 | [diff] [blame] | 1458 | struct squashfs_ipc_inode_header *ipc = &inode_header.ipc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1459 | |
| 1460 | inode = get_inode(sizeof(*ipc)); |
| 1461 | ipc->nlink = nlink; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1462 | SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1463 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1464 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1465 | } |
| 1466 | else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) { |
plougher | aa0d122 | 2010-12-31 20:06:24 +0000 | [diff] [blame] | 1467 | struct squashfs_lipc_inode_header *ipc = &inode_header.lipc; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1468 | |
| 1469 | inode = get_inode(sizeof(*ipc)); |
| 1470 | ipc->nlink = nlink; |
| 1471 | ipc->xattr = xattr; |
| 1472 | SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode); |
| 1473 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1474 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1475 | } else |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1476 | BAD_ERROR("Unrecognised inode %d in create_inode\n", type); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1477 | |
| 1478 | *i_no = MKINODE(inode); |
| 1479 | inode_count ++; |
| 1480 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1481 | TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type, |
| 1482 | base->uid, base->guid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1483 | |
| 1484 | return TRUE; |
| 1485 | } |
| 1486 | |
| 1487 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1488 | void add_dir(squashfs_inode inode, unsigned int inode_number, char *name, |
| 1489 | int type, struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1490 | { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 1491 | unsigned char *buff; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1492 | struct squashfs_dir_entry idir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1493 | unsigned int start_block = inode >> 16; |
| 1494 | unsigned int offset = inode & 0xffff; |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1495 | unsigned int size = strlen(name); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1496 | size_t name_off = offsetof(struct squashfs_dir_entry, name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1497 | |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1498 | if(size > SQUASHFS_NAME_LEN) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1499 | size = SQUASHFS_NAME_LEN; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1500 | ERROR("Filename is greater than %d characters, truncating! ..." |
| 1501 | "\n", SQUASHFS_NAME_LEN); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1504 | if(dir->p + sizeof(struct squashfs_dir_entry) + size + |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1505 | sizeof(struct squashfs_dir_header) |
| 1506 | >= dir->buff + dir->size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1507 | buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE); |
| 1508 | if(buff == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1509 | BAD_ERROR("Out of memory reallocating directory buffer" |
| 1510 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | dir->p = (dir->p - dir->buff) + buff; |
| 1514 | if(dir->entry_count_p) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1515 | dir->entry_count_p = (dir->entry_count_p - dir->buff + |
| 1516 | buff); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1517 | dir->index_count_p = dir->index_count_p - dir->buff + buff; |
| 1518 | dir->buff = buff; |
| 1519 | } |
| 1520 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1521 | if(dir->entry_count == 256 || start_block != dir->start_block || |
| 1522 | ((dir->entry_count_p != NULL) && |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1523 | ((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1524 | dir->index_count_p) > SQUASHFS_METADATA_SIZE)) || |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1525 | ((long long) inode_number - dir->inode_number) > 32767 |
| 1526 | || ((long long) inode_number - dir->inode_number) |
| 1527 | < -32768) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1528 | if(dir->entry_count_p) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1529 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1530 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1531 | if((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1532 | dir->index_count_p) > |
| 1533 | SQUASHFS_METADATA_SIZE) { |
| 1534 | if(dir->i_count % I_COUNT_SIZE == 0) { |
| 1535 | dir->index = realloc(dir->index, |
| 1536 | (dir->i_count + I_COUNT_SIZE) * |
| 1537 | sizeof(struct cached_dir_index)); |
| 1538 | if(dir->index == NULL) |
| 1539 | BAD_ERROR("Out of memory in " |
| 1540 | "directory index table " |
| 1541 | "reallocation!\n"); |
| 1542 | } |
| 1543 | dir->index[dir->i_count].index.index = |
| 1544 | dir->p - dir->buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1545 | dir->index[dir->i_count].index.size = size - 1; |
| 1546 | dir->index[dir->i_count++].name = name; |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1547 | dir->i_size += sizeof(struct squashfs_dir_index) |
| 1548 | + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1549 | dir->index_count_p = dir->p; |
| 1550 | } |
| 1551 | |
| 1552 | dir_header.count = dir->entry_count - 1; |
| 1553 | dir_header.start_block = dir->start_block; |
| 1554 | dir_header.inode_number = dir->inode_number; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1555 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1556 | dir->entry_count_p); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1557 | |
| 1558 | } |
| 1559 | |
| 1560 | |
| 1561 | dir->entry_count_p = dir->p; |
| 1562 | dir->start_block = start_block; |
| 1563 | dir->entry_count = 0; |
| 1564 | dir->inode_number = inode_number; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1565 | dir->p += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1568 | idir.offset = offset; |
| 1569 | idir.type = type; |
| 1570 | idir.size = size - 1; |
| 1571 | idir.inode_number = ((long long) inode_number - dir->inode_number); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1572 | SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p); |
| 1573 | strncpy((char *) dir->p + name_off, name, size); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1574 | dir->p += sizeof(struct squashfs_dir_entry) + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1575 | dir->entry_count ++; |
| 1576 | } |
| 1577 | |
| 1578 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1579 | void write_dir(squashfs_inode *inode, struct dir_info *dir_info, |
| 1580 | struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1581 | { |
| 1582 | unsigned int dir_size = dir->p - dir->buff; |
plougher | 4627ca3 | 2010-12-16 05:03:55 +0000 | [diff] [blame] | 1583 | int data_space = directory_cache_size - directory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1584 | unsigned int directory_block, directory_offset, i_count, index; |
| 1585 | unsigned short c_byte; |
| 1586 | |
| 1587 | if(data_space < dir_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1588 | int realloc_size = directory_cache_size == 0 ? |
| 1589 | ((dir_size + SQUASHFS_METADATA_SIZE) & |
| 1590 | ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1591 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1592 | void *dc = realloc(directory_data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1593 | directory_cache_size + realloc_size); |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1594 | if(dc == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1595 | goto failed; |
| 1596 | } |
| 1597 | directory_cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1598 | directory_data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | if(dir_size) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1602 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1603 | |
| 1604 | dir_header.count = dir->entry_count - 1; |
| 1605 | dir_header.start_block = dir->start_block; |
| 1606 | dir_header.inode_number = dir->inode_number; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1607 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1608 | memcpy(directory_data_cache + directory_cache_bytes, dir->buff, |
| 1609 | dir_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1610 | } |
| 1611 | directory_offset = directory_cache_bytes; |
| 1612 | directory_block = directory_bytes; |
| 1613 | directory_cache_bytes += dir_size; |
| 1614 | i_count = 0; |
| 1615 | index = SQUASHFS_METADATA_SIZE - directory_offset; |
| 1616 | |
| 1617 | while(1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1618 | while(i_count < dir->i_count && |
| 1619 | dir->index[i_count].index.index < index) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1620 | dir->index[i_count++].index.start_block = |
| 1621 | directory_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1622 | index += SQUASHFS_METADATA_SIZE; |
| 1623 | |
| 1624 | if(directory_cache_bytes < SQUASHFS_METADATA_SIZE) |
| 1625 | break; |
| 1626 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1627 | if((directory_size - directory_bytes) < |
| 1628 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1629 | void *dt = realloc(directory_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1630 | directory_size + (SQUASHFS_METADATA_SIZE << 1) |
| 1631 | + 2); |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1632 | if(dt == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1633 | goto failed; |
| 1634 | } |
| 1635 | directory_size += SQUASHFS_METADATA_SIZE << 1; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1636 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1639 | c_byte = mangle(directory_table + directory_bytes + |
| 1640 | BLOCK_OFFSET, directory_data_cache, |
| 1641 | SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, |
| 1642 | noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1643 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1644 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1645 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1646 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1647 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1648 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1649 | total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 1650 | memmove(directory_data_cache, directory_data_cache + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1651 | SQUASHFS_METADATA_SIZE, directory_cache_bytes - |
| 1652 | SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1653 | directory_cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 1654 | } |
| 1655 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1656 | create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE, |
| 1657 | dir_size + 3, directory_block, directory_offset, NULL, NULL, |
| 1658 | dir, 0); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1659 | |
| 1660 | #ifdef SQUASHFS_TRACE |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1661 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1662 | unsigned char *dirp; |
| 1663 | int count; |
| 1664 | |
| 1665 | TRACE("Directory contents of inode 0x%llx\n", *inode); |
| 1666 | dirp = dir->buff; |
| 1667 | while(dirp < dir->p) { |
| 1668 | char buffer[SQUASHFS_NAME_LEN + 1]; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1669 | struct squashfs_dir_entry idir, *idirp; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1670 | struct squashfs_dir_header dirh; |
| 1671 | SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1672 | &dirh); |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1673 | count = dirh.count + 1; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1674 | dirp += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1675 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1676 | TRACE("\tStart block 0x%x, count %d\n", |
| 1677 | dirh.start_block, count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1678 | |
| 1679 | while(count--) { |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1680 | idirp = (struct squashfs_dir_entry *) dirp; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1681 | SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1682 | strncpy(buffer, idirp->name, idir.size + 1); |
| 1683 | buffer[idir.size + 1] = '\0'; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1684 | TRACE("\t\tname %s, inode offset 0x%x, type " |
| 1685 | "%d\n", buffer, idir.offset, idir.type); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1686 | dirp += sizeof(struct squashfs_dir_entry) + idir.size + |
plougher | e8b26f6 | 2010-12-16 05:06:00 +0000 | [diff] [blame] | 1687 | 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | #endif |
| 1692 | dir_count ++; |
| 1693 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1694 | return; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1695 | |
| 1696 | failed: |
| 1697 | BAD_ERROR("Out of memory in directory table reallocation!\n"); |
| 1698 | } |
| 1699 | |
| 1700 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1701 | struct file_buffer *get_fragment(struct fragment *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1702 | { |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1703 | struct squashfs_fragment_entry *disk_fragment; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1704 | int res, size; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1705 | long long start_block; |
| 1706 | struct file_buffer *buffer, *compressed_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1707 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1708 | if(fragment->index == SQUASHFS_INVALID_FRAG) |
| 1709 | return NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1710 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1711 | buffer = cache_lookup(fragment_buffer, fragment->index); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1712 | if(buffer) |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1713 | return buffer; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1714 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1715 | compressed_buffer = cache_lookup(writer_buffer, fragment->index + |
| 1716 | FRAG_INDEX); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1717 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1718 | buffer = cache_get(fragment_buffer, fragment->index, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1719 | |
| 1720 | pthread_mutex_lock(&fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1721 | disk_fragment = &fragment_table[fragment->index]; |
| 1722 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1723 | start_block = disk_fragment->start_block; |
| 1724 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1725 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1726 | if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1727 | int error; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1728 | char *data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1729 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1730 | if(compressed_buffer) |
| 1731 | data = compressed_buffer->data; |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 1732 | else |
| 1733 | data = read_from_disk(start_block, size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1734 | |
plougher | b48442b | 2010-12-31 07:57:54 +0000 | [diff] [blame] | 1735 | res = compressor_uncompress(comp, buffer->data, data, size, |
| 1736 | block_size, &error); |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 1737 | if(res == -1) |
| 1738 | BAD_ERROR("%s uncompress failed with error code %d\n", |
| 1739 | comp->name, error); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1740 | } else if(compressed_buffer) |
| 1741 | memcpy(buffer->data, compressed_buffer->data, size); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1742 | else { |
| 1743 | res = read_fs_bytes(fd, start_block, size, buffer->data); |
| 1744 | if(res == 0) |
| 1745 | EXIT_MKSQUASHFS(); |
| 1746 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1747 | |
plougher | 03859fa | 2009-03-31 03:18:18 +0000 | [diff] [blame] | 1748 | cache_block_put(compressed_buffer); |
| 1749 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1750 | return buffer; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1753 | |
| 1754 | struct frag_locked { |
| 1755 | struct file_buffer *buffer; |
| 1756 | int c_byte; |
| 1757 | int fragment; |
| 1758 | struct frag_locked *fragment_prev; |
| 1759 | struct frag_locked *fragment_next; |
| 1760 | }; |
| 1761 | |
| 1762 | int fragments_locked = FALSE; |
| 1763 | struct frag_locked *frag_locked_list = NULL; |
| 1764 | |
| 1765 | INSERT_LIST(fragment, struct frag_locked) |
| 1766 | REMOVE_LIST(fragment, struct frag_locked) |
| 1767 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1768 | int lock_fragments() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1769 | { |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1770 | int count; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1771 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1772 | fragments_locked = TRUE; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1773 | count = fragments_outstanding; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1774 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1775 | return count; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | |
| 1779 | void unlock_fragments() |
| 1780 | { |
| 1781 | struct frag_locked *entry; |
| 1782 | int compressed_size; |
| 1783 | |
| 1784 | pthread_mutex_lock(&fragment_mutex); |
| 1785 | while(frag_locked_list) { |
| 1786 | entry = frag_locked_list; |
| 1787 | remove_fragment_list(&frag_locked_list, entry); |
| 1788 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(entry->c_byte); |
| 1789 | fragment_table[entry->fragment].size = entry->c_byte; |
| 1790 | fragment_table[entry->fragment].start_block = bytes; |
| 1791 | entry->buffer->block = bytes; |
| 1792 | bytes += compressed_size; |
| 1793 | fragments_outstanding --; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1794 | queue_put(to_writer, entry->buffer); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1795 | TRACE("fragment_locked writing fragment %d, compressed size %d" |
| 1796 | "\n", entry->fragment, compressed_size); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1797 | free(entry); |
| 1798 | } |
| 1799 | fragments_locked = FALSE; |
| 1800 | pthread_mutex_unlock(&fragment_mutex); |
| 1801 | } |
| 1802 | |
| 1803 | |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1804 | void add_pending_fragment(struct file_buffer *write_buffer, int c_byte, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1805 | int fragment) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1806 | { |
| 1807 | struct frag_locked *entry = malloc(sizeof(struct frag_locked)); |
| 1808 | if(entry == NULL) |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1809 | BAD_ERROR("Out of memory in add_pending fragment\n"); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1810 | entry->buffer = write_buffer; |
| 1811 | entry->c_byte = c_byte; |
| 1812 | entry->fragment = fragment; |
| 1813 | entry->fragment_prev = entry->fragment_next = NULL; |
| 1814 | pthread_mutex_lock(&fragment_mutex); |
| 1815 | insert_fragment_list(&frag_locked_list, entry); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1816 | pthread_mutex_unlock(&fragment_mutex); |
| 1817 | } |
| 1818 | |
| 1819 | |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1820 | void write_fragment(struct file_buffer *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1821 | { |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1822 | if(fragment == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1823 | return; |
| 1824 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1825 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1826 | fragment_table[fragment->block].unused = 0; |
| 1827 | fragments_outstanding ++; |
| 1828 | queue_put(to_frag, fragment); |
| 1829 | pthread_mutex_unlock(&fragment_mutex); |
| 1830 | } |
| 1831 | |
| 1832 | |
| 1833 | struct file_buffer *allocate_fragment() |
| 1834 | { |
| 1835 | struct file_buffer *fragment = cache_get(fragment_buffer, fragments, 1); |
| 1836 | |
| 1837 | pthread_mutex_lock(&fragment_mutex); |
| 1838 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1839 | if(fragments % FRAG_SIZE == 0) { |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1840 | void *ft = realloc(fragment_table, (fragments + |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1841 | FRAG_SIZE) * sizeof(struct squashfs_fragment_entry)); |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1842 | if(ft == NULL) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1843 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1844 | BAD_ERROR("Out of memory in fragment table\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1845 | } |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1846 | fragment_table = ft; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1847 | } |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1848 | |
| 1849 | fragment->size = 0; |
| 1850 | fragment->block = fragments ++; |
| 1851 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1852 | pthread_mutex_unlock(&fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1853 | |
| 1854 | return fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 1857 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1858 | static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0}; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1859 | struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer, |
| 1860 | struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1861 | { |
| 1862 | struct fragment *ffrg; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1863 | struct file_buffer **fragment; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1864 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1865 | if(file_buffer == NULL || file_buffer->size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1866 | return &empty_fragment; |
| 1867 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1868 | fragment = eval_frag_actions(dir_ent); |
| 1869 | |
| 1870 | if((*fragment) && (*fragment)->size + file_buffer->size > block_size) { |
| 1871 | write_fragment(*fragment); |
| 1872 | *fragment = NULL; |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1873 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1874 | |
plougher | e7e6e83 | 2010-12-16 05:08:06 +0000 | [diff] [blame] | 1875 | ffrg = malloc(sizeof(struct fragment)); |
| 1876 | if(ffrg == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1877 | BAD_ERROR("Out of memory in fragment block allocation!\n"); |
| 1878 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1879 | if(*fragment == NULL) |
| 1880 | *fragment = allocate_fragment(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1881 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1882 | ffrg->index = (*fragment)->block; |
| 1883 | ffrg->offset = (*fragment)->size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1884 | ffrg->size = file_buffer->size; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1885 | memcpy((*fragment)->data + (*fragment)->size, file_buffer->data, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1886 | file_buffer->size); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1887 | (*fragment)->size += file_buffer->size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1888 | |
| 1889 | return ffrg; |
| 1890 | } |
| 1891 | |
| 1892 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1893 | long long generic_write_table(int length, void *buffer, int length2, |
| 1894 | void *buffer2, int uncompressed) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1895 | { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1896 | int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) / |
| 1897 | SQUASHFS_METADATA_SIZE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1898 | long long list[meta_blocks], start_bytes; |
plougher | badfac6 | 2007-11-12 03:29:41 +0000 | [diff] [blame] | 1899 | int compressed_size, i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1900 | unsigned short c_byte; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1901 | char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2]; |
| 1902 | |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1903 | #ifdef SQUASHFS_TRACE |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1904 | long long obytes = bytes; |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1905 | int olength = length; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1906 | #endif |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1907 | |
| 1908 | for(i = 0; i < meta_blocks; i++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1909 | int avail_bytes = length > SQUASHFS_METADATA_SIZE ? |
| 1910 | SQUASHFS_METADATA_SIZE : length; |
| 1911 | c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i * |
| 1912 | SQUASHFS_METADATA_SIZE , avail_bytes, |
| 1913 | SQUASHFS_METADATA_SIZE, uncompressed, 0); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1914 | SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1915 | list[i] = bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1916 | compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1917 | BLOCK_OFFSET; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1918 | TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes, |
| 1919 | compressed_size); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1920 | write_destination(fd, bytes, compressed_size, cbuffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1921 | bytes += compressed_size; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1922 | total_bytes += avail_bytes; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1923 | length -= avail_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1926 | start_bytes = bytes; |
| 1927 | if(length2) { |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1928 | write_destination(fd, bytes, length2, buffer2); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1929 | bytes += length2; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1930 | total_bytes += length2; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1933 | SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks); |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 1934 | write_destination(fd, bytes, sizeof(list), list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1935 | bytes += sizeof(list); |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1936 | total_bytes += sizeof(list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1937 | |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1938 | TRACE("generic_write_table: total uncompressed %d compressed %lld\n", |
| 1939 | olength, bytes - obytes); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1940 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1941 | return start_bytes; |
| 1942 | } |
| 1943 | |
| 1944 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1945 | long long write_fragment_table() |
| 1946 | { |
| 1947 | unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments); |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1948 | struct squashfs_fragment_entry p[fragments]; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1949 | int i; |
| 1950 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1951 | TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments, |
| 1952 | frag_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1953 | for(i = 0; i < fragments; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1954 | TRACE("write_fragment_table: fragment %d, start_block 0x%llx, " |
| 1955 | "size %d\n", i, fragment_table[i].start_block, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1956 | fragment_table[i].size); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1957 | SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_table[i], p + i); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1960 | return generic_write_table(frag_bytes, p, 0, NULL, noF); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1964 | char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1965 | char *read_from_disk(long long start, unsigned int avail_bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1966 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1967 | int res; |
| 1968 | |
| 1969 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer); |
| 1970 | if(res == 0) |
| 1971 | EXIT_MKSQUASHFS(); |
| 1972 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1973 | return read_from_file_buffer; |
| 1974 | } |
| 1975 | |
| 1976 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1977 | char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE]; |
| 1978 | char *read_from_disk2(long long start, unsigned int avail_bytes) |
| 1979 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1980 | int res; |
| 1981 | |
| 1982 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2); |
| 1983 | if(res == 0) |
| 1984 | EXIT_MKSQUASHFS(); |
| 1985 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1986 | return read_from_file_buffer2; |
| 1987 | } |
| 1988 | |
| 1989 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1990 | /* |
| 1991 | * Compute 16 bit BSD checksum over the data |
| 1992 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1993 | unsigned short get_checksum(char *buff, int bytes, unsigned short chksum) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1994 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1995 | unsigned char *b = (unsigned char *) buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1996 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1997 | while(bytes --) { |
| 1998 | chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1; |
| 1999 | chksum += *b++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | return chksum; |
| 2003 | } |
| 2004 | |
| 2005 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2006 | unsigned short get_checksum_disk(long long start, long long l, |
| 2007 | unsigned int *blocks) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2008 | { |
| 2009 | unsigned short chksum = 0; |
| 2010 | unsigned int bytes; |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 2011 | struct file_buffer *write_buffer; |
| 2012 | int i; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2013 | |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 2014 | for(i = 0; l; i++) { |
| 2015 | bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]); |
| 2016 | if(bytes == 0) /* sparse block */ |
| 2017 | continue; |
| 2018 | write_buffer = cache_lookup(writer_buffer, start); |
| 2019 | if(write_buffer) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2020 | chksum = get_checksum(write_buffer->data, bytes, |
| 2021 | chksum); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 2022 | cache_block_put(write_buffer); |
| 2023 | } else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2024 | chksum = get_checksum(read_from_disk(start, bytes), |
| 2025 | bytes, chksum); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2026 | l -= bytes; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2027 | start += bytes; |
| 2028 | } |
| 2029 | |
| 2030 | return chksum; |
| 2031 | } |
| 2032 | |
| 2033 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2034 | unsigned short get_checksum_mem(char *buff, int bytes) |
| 2035 | { |
| 2036 | return get_checksum(buff, bytes, 0); |
| 2037 | } |
| 2038 | |
| 2039 | |
| 2040 | unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer) |
| 2041 | { |
| 2042 | if(file_buffer == NULL) |
| 2043 | return 0; |
| 2044 | else |
| 2045 | return get_checksum(file_buffer->data, file_buffer->size, 0); |
| 2046 | } |
| 2047 | |
| 2048 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2049 | #define DUP_HASH(a) (a & 0xffff) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2050 | 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] | 2051 | unsigned int *block_listp, int blocks, unsigned int fragment, |
| 2052 | int offset, int bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2053 | { |
| 2054 | struct fragment *frg; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 2055 | unsigned int *block_list = block_listp; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2056 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 2057 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2058 | if(!duplicate_checking || file_size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2059 | return; |
| 2060 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2061 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) { |
| 2062 | if(file_size != dupl_ptr->file_size) |
| 2063 | continue; |
| 2064 | if(blocks != 0 && start != dupl_ptr->start) |
| 2065 | continue; |
| 2066 | if(fragment != dupl_ptr->fragment->index) |
| 2067 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2068 | if(fragment != SQUASHFS_INVALID_FRAG && (offset != |
| 2069 | dupl_ptr->fragment->offset || bytes != |
| 2070 | dupl_ptr->fragment->size)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2071 | continue; |
| 2072 | return; |
| 2073 | } |
| 2074 | |
plougher | 3bb279c | 2010-12-16 05:10:03 +0000 | [diff] [blame] | 2075 | frg = malloc(sizeof(struct fragment)); |
| 2076 | if(frg == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2077 | BAD_ERROR("Out of memory in fragment block allocation!\n"); |
| 2078 | |
| 2079 | frg->index = fragment; |
| 2080 | frg->offset = offset; |
| 2081 | frg->size = bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2082 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2083 | add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0, FALSE); |
| 2084 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2085 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2086 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2087 | int pre_duplicate(long long file_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2088 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2089 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2090 | |
| 2091 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2092 | if(dupl_ptr->file_size == file_size) |
| 2093 | return TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2094 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2095 | return FALSE; |
| 2096 | } |
| 2097 | |
| 2098 | |
| 2099 | int pre_duplicate_frag(long long file_size, unsigned short checksum) |
| 2100 | { |
| 2101 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 2102 | |
| 2103 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2104 | if(file_size == dupl_ptr->file_size && file_size == |
| 2105 | dupl_ptr->fragment->size) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2106 | if(dupl_ptr->checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2107 | struct file_buffer *frag_buffer = |
| 2108 | get_fragment(dupl_ptr->fragment); |
| 2109 | dupl_ptr->checksum = |
| 2110 | get_checksum_disk(dupl_ptr->start, |
| 2111 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 2112 | dupl_ptr->fragment_checksum = |
| 2113 | get_checksum_mem(frag_buffer->data + |
| 2114 | dupl_ptr->fragment->offset, file_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2115 | cache_block_put(frag_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2116 | dupl_ptr->checksum_flag = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2117 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2118 | if(dupl_ptr->fragment_checksum == checksum) |
| 2119 | return TRUE; |
| 2120 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2121 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2122 | return FALSE; |
| 2123 | } |
| 2124 | |
| 2125 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2126 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 2127 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 2128 | unsigned short checksum, unsigned short fragment_checksum, |
| 2129 | int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2130 | { |
plougher | 51ef9ae | 2010-12-16 05:14:28 +0000 | [diff] [blame] | 2131 | struct file_info *dupl_ptr = malloc(sizeof(struct file_info)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2132 | |
plougher | 51ef9ae | 2010-12-16 05:14:28 +0000 | [diff] [blame] | 2133 | if(dupl_ptr == NULL) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2134 | BAD_ERROR("Out of memory in dup_files allocation!\n"); |
| 2135 | } |
| 2136 | |
| 2137 | dupl_ptr->file_size = file_size; |
| 2138 | dupl_ptr->bytes = bytes; |
| 2139 | dupl_ptr->block_list = block_list; |
| 2140 | dupl_ptr->start = start; |
| 2141 | dupl_ptr->fragment = fragment; |
| 2142 | dupl_ptr->checksum = checksum; |
| 2143 | dupl_ptr->fragment_checksum = fragment_checksum; |
| 2144 | dupl_ptr->checksum_flag = checksum_flag; |
| 2145 | dupl_ptr->next = dupl[DUP_HASH(file_size)]; |
| 2146 | dupl[DUP_HASH(file_size)] = dupl_ptr; |
| 2147 | dup_files ++; |
| 2148 | |
| 2149 | return dupl_ptr; |
| 2150 | } |
| 2151 | |
| 2152 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2153 | struct file_info *duplicate(long long file_size, long long bytes, |
| 2154 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 2155 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 2156 | unsigned short fragment_checksum, int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2157 | { |
| 2158 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 2159 | int frag_bytes = file_buffer ? file_buffer->size : 0; |
| 2160 | |
| 2161 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2162 | if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes |
| 2163 | && frag_bytes == dupl_ptr->fragment->size) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2164 | long long target_start, dup_start = dupl_ptr->start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2165 | int block; |
| 2166 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2167 | if(memcmp(*block_list, dupl_ptr->block_list, blocks * |
| 2168 | sizeof(unsigned int)) != 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 2169 | continue; |
| 2170 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2171 | if(checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2172 | checksum = get_checksum_disk(*start, bytes, |
| 2173 | *block_list); |
| 2174 | fragment_checksum = |
| 2175 | get_checksum_mem_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2176 | checksum_flag = TRUE; |
| 2177 | } |
| 2178 | |
| 2179 | if(dupl_ptr->checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2180 | struct file_buffer *frag_buffer = |
| 2181 | get_fragment(dupl_ptr->fragment); |
| 2182 | dupl_ptr->checksum = |
| 2183 | get_checksum_disk(dupl_ptr->start, |
| 2184 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 2185 | dupl_ptr->fragment_checksum = |
| 2186 | get_checksum_mem(frag_buffer->data + |
| 2187 | dupl_ptr->fragment->offset, frag_bytes); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2188 | cache_block_put(frag_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2189 | dupl_ptr->checksum_flag = TRUE; |
| 2190 | } |
| 2191 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2192 | if(checksum != dupl_ptr->checksum || |
| 2193 | fragment_checksum != |
| 2194 | dupl_ptr->fragment_checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2195 | continue; |
| 2196 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2197 | target_start = *start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2198 | for(block = 0; block < blocks; block ++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2199 | int size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2200 | ((*block_list)[block]); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2201 | struct file_buffer *target_buffer = NULL; |
| 2202 | struct file_buffer *dup_buffer = NULL; |
| 2203 | char *target_data, *dup_data; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2204 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2205 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2206 | if(size == 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 2207 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2208 | target_buffer = cache_lookup(writer_buffer, |
| 2209 | target_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2210 | if(target_buffer) |
| 2211 | target_data = target_buffer->data; |
| 2212 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2213 | target_data = |
| 2214 | read_from_disk(target_start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2215 | size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2216 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2217 | dup_buffer = cache_lookup(writer_buffer, |
| 2218 | dup_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2219 | if(dup_buffer) |
| 2220 | dup_data = dup_buffer->data; |
| 2221 | else |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2222 | dup_data = read_from_disk2(dup_start, |
| 2223 | size); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2224 | |
| 2225 | res = memcmp(target_data, dup_data, size); |
| 2226 | cache_block_put(target_buffer); |
| 2227 | cache_block_put(dup_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2228 | if(res != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2229 | break; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2230 | target_start += size; |
| 2231 | dup_start += size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2232 | } |
| 2233 | if(block == blocks) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2234 | struct file_buffer *frag_buffer = |
| 2235 | get_fragment(dupl_ptr->fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2236 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2237 | if(frag_bytes == 0 || |
| 2238 | memcmp(file_buffer->data, |
| 2239 | frag_buffer->data + |
| 2240 | dupl_ptr->fragment->offset, |
| 2241 | frag_bytes) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2242 | TRACE("Found duplicate file, start " |
| 2243 | "0x%llx, size %lld, checksum " |
| 2244 | "0x%x, fragment %d, size %d, " |
| 2245 | "offset %d, checksum 0x%x\n", |
| 2246 | dupl_ptr->start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2247 | dupl_ptr->bytes, |
| 2248 | dupl_ptr->checksum, |
| 2249 | dupl_ptr->fragment->index, |
| 2250 | frag_bytes, |
| 2251 | dupl_ptr->fragment->offset, |
| 2252 | fragment_checksum); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2253 | *block_list = dupl_ptr->block_list; |
| 2254 | *start = dupl_ptr->start; |
| 2255 | *fragment = dupl_ptr->fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2256 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2257 | return 0; |
| 2258 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2259 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2264 | return add_non_dup(file_size, bytes, *block_list, *start, *fragment, |
| 2265 | checksum, fragment_checksum, checksum_flag); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2269 | inline int is_fragment(struct inode_info *inode) |
| 2270 | { |
| 2271 | int file_size = inode->buf.st_size; |
| 2272 | |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2273 | /* |
| 2274 | * If this block is to be compressed differently to the |
| 2275 | * fragment compression then it cannot be a fragment |
| 2276 | */ |
| 2277 | if(inode->noF != noF) |
| 2278 | return FALSE; |
| 2279 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2280 | return !inode->no_fragments && (file_size < block_size || |
| 2281 | (inode->always_use_fragments && file_size & (block_size - 1))); |
| 2282 | } |
| 2283 | |
| 2284 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2285 | static int seq = 0; |
| 2286 | void reader_read_process(struct dir_ent *dir_ent) |
| 2287 | { |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2288 | struct inode_info *inode = dir_ent->inode; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2289 | struct file_buffer *prev_buffer = NULL, *file_buffer; |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2290 | int status, res, byte, count = 0; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2291 | int file = get_pseudo_file(inode->pseudo_id)->fd; |
| 2292 | int child = get_pseudo_file(inode->pseudo_id)->child; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2293 | long long bytes = 0; |
| 2294 | |
| 2295 | while(1) { |
| 2296 | file_buffer = cache_get(reader_buffer, 0, 0); |
| 2297 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2298 | file_buffer->noD = inode->noD; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2299 | |
| 2300 | byte = read_bytes(file, file_buffer->data, block_size); |
| 2301 | if(byte == -1) |
| 2302 | goto read_err; |
| 2303 | |
| 2304 | file_buffer->size = byte; |
| 2305 | file_buffer->file_size = -1; |
| 2306 | file_buffer->block = count ++; |
| 2307 | file_buffer->error = FALSE; |
| 2308 | file_buffer->fragment = FALSE; |
| 2309 | bytes += byte; |
| 2310 | |
| 2311 | if(byte == 0) |
| 2312 | break; |
| 2313 | |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2314 | /* |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 2315 | * Update progress bar size. This is done |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2316 | * on every block rather than waiting for all blocks to be |
| 2317 | * read incase write_file_process() is running in parallel |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 2318 | * with this. Otherwise the current progress bar position |
| 2319 | * may get ahead of the progress bar size. |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2320 | */ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 2321 | progress_bar_size(1); |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2322 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2323 | if(prev_buffer) |
| 2324 | queue_put(from_reader, prev_buffer); |
| 2325 | prev_buffer = file_buffer; |
| 2326 | } |
| 2327 | |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2328 | /* |
| 2329 | * Update inode file size now that the size of the dynamic pseudo file |
| 2330 | * is known. This is needed for the -info option. |
| 2331 | */ |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2332 | inode->buf.st_size = bytes; |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2333 | |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2334 | res = waitpid(child, &status, 0); |
plougher | d87d8d1 | 2009-09-10 04:08:16 +0000 | [diff] [blame] | 2335 | if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2336 | goto read_err; |
| 2337 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2338 | if(prev_buffer == NULL) |
| 2339 | prev_buffer = file_buffer; |
| 2340 | else { |
| 2341 | cache_block_put(file_buffer); |
| 2342 | seq --; |
| 2343 | } |
| 2344 | prev_buffer->file_size = bytes; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2345 | prev_buffer->fragment = is_fragment(inode); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2346 | queue_put(from_reader, prev_buffer); |
| 2347 | |
| 2348 | return; |
| 2349 | |
| 2350 | read_err: |
| 2351 | if(prev_buffer) { |
| 2352 | cache_block_put(file_buffer); |
| 2353 | seq --; |
| 2354 | file_buffer = prev_buffer; |
| 2355 | } |
| 2356 | file_buffer->error = TRUE; |
| 2357 | queue_put(from_deflate, file_buffer); |
| 2358 | } |
| 2359 | |
| 2360 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2361 | void reader_read_file(struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2362 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2363 | struct stat *buf = &dir_ent->inode->buf, buf2; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2364 | struct file_buffer *file_buffer; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2365 | int blocks, byte, count, expected, file; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2366 | long long bytes, read_size; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2367 | struct inode_info *inode = dir_ent->inode; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2368 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2369 | if(inode->read) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2370 | return; |
| 2371 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2372 | inode->read = TRUE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2373 | again: |
| 2374 | bytes = 0; |
| 2375 | count = 0; |
| 2376 | file_buffer = NULL; |
| 2377 | read_size = buf->st_size; |
| 2378 | blocks = (read_size + block_size - 1) >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2379 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2380 | file = open(pathname_reader(dir_ent), O_RDONLY); |
plougher | c1d258e | 2010-12-16 05:16:45 +0000 | [diff] [blame] | 2381 | if(file == -1) { |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2382 | file_buffer = cache_get(reader_buffer, 0, 0); |
| 2383 | file_buffer->sequence = seq ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2384 | goto read_err; |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2385 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2386 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2387 | do { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2388 | expected = read_size - ((long long) count * block_size) > |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2389 | block_size ? block_size : |
| 2390 | read_size - ((long long) count * block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2391 | |
| 2392 | if(file_buffer) |
| 2393 | queue_put(from_reader, file_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2394 | file_buffer = cache_get(reader_buffer, 0, 0); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2395 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2396 | file_buffer->noD = inode->noD; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2397 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2398 | /* |
| 2399 | * Always try to read block_size bytes from the file rather |
| 2400 | * than expected bytes (which will be less than the block_size |
| 2401 | * at the file tail) to check that the file hasn't grown |
| 2402 | * since being stated. If it is longer (or shorter) than |
| 2403 | * expected, then restat, and try again. Note the special |
| 2404 | * case where the file is an exact multiple of the block_size |
| 2405 | * is dealt with later. |
| 2406 | */ |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2407 | byte = file_buffer->size = read_bytes(file, file_buffer->data, |
| 2408 | block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2409 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2410 | file_buffer->file_size = read_size; |
| 2411 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2412 | if(byte == -1) |
| 2413 | goto read_err; |
| 2414 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2415 | if(byte != expected) |
| 2416 | goto restat; |
| 2417 | |
| 2418 | file_buffer->block = count; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2419 | file_buffer->error = FALSE; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2420 | file_buffer->fragment = FALSE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2421 | |
| 2422 | bytes += byte; |
| 2423 | count ++; |
| 2424 | } while(count < blocks); |
| 2425 | |
| 2426 | if(read_size != bytes) |
| 2427 | goto restat; |
| 2428 | |
| 2429 | if(expected == block_size) { |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2430 | /* |
| 2431 | * Special case where we've not tried to read past the end of |
| 2432 | * the file. We expect to get EOF, i.e. the file isn't larger |
| 2433 | * than we expect. |
| 2434 | */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2435 | char buffer; |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2436 | int res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2437 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2438 | res = read_bytes(file, &buffer, 1); |
| 2439 | if(res == -1) |
| 2440 | goto read_err; |
| 2441 | |
| 2442 | if(res != 0) |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2443 | goto restat; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2446 | file_buffer->fragment = is_fragment(inode); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2447 | queue_put(from_reader, file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2448 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2449 | close(file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2450 | |
| 2451 | return; |
| 2452 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2453 | restat: |
| 2454 | fstat(file, &buf2); |
| 2455 | close(file); |
| 2456 | if(read_size != buf2.st_size) { |
| 2457 | memcpy(buf, &buf2, sizeof(struct stat)); |
| 2458 | file_buffer->error = 2; |
| 2459 | queue_put(from_deflate, file_buffer); |
| 2460 | goto again; |
| 2461 | } |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2462 | read_err: |
| 2463 | file_buffer->error = TRUE; |
| 2464 | queue_put(from_deflate, file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | |
| 2468 | void reader_scan(struct dir_info *dir) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2469 | struct dir_ent *dir_ent = dir->list; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2470 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2471 | for(; dir_ent; dir_ent = dir_ent->next) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2472 | struct stat *buf = &dir_ent->inode->buf; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2473 | if(dir_ent->inode->root_entry) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2474 | continue; |
| 2475 | |
plougher | b3977eb | 2010-05-02 02:08:48 +0000 | [diff] [blame] | 2476 | if(IS_PSEUDO_PROCESS(dir_ent->inode)) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2477 | reader_read_process(dir_ent); |
| 2478 | continue; |
| 2479 | } |
| 2480 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2481 | switch(buf->st_mode & S_IFMT) { |
| 2482 | case S_IFREG: |
| 2483 | reader_read_file(dir_ent); |
| 2484 | break; |
| 2485 | case S_IFDIR: |
| 2486 | reader_scan(dir_ent->dir); |
| 2487 | break; |
| 2488 | } |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | |
| 2493 | void *reader(void *arg) |
| 2494 | { |
| 2495 | int oldstate; |
| 2496 | |
| 2497 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2498 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2499 | |
| 2500 | if(!sorted) |
| 2501 | reader_scan(queue_get(to_reader)); |
| 2502 | else { |
| 2503 | int i; |
| 2504 | struct priority_entry *entry; |
| 2505 | |
| 2506 | queue_get(to_reader); |
| 2507 | for(i = 65535; i >= 0; i--) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2508 | for(entry = priority_list[i]; entry; |
| 2509 | entry = entry->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2510 | reader_read_file(entry->dir); |
| 2511 | } |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2512 | |
plougher | 5aa1816 | 2007-12-13 12:15:21 +0000 | [diff] [blame] | 2513 | thread[0] = 0; |
| 2514 | |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2515 | pthread_exit(NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | |
| 2519 | void *writer(void *arg) |
| 2520 | { |
| 2521 | int write_error = FALSE; |
| 2522 | int oldstate; |
| 2523 | |
| 2524 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2525 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2526 | |
| 2527 | while(1) { |
| 2528 | struct file_buffer *file_buffer = queue_get(to_writer); |
| 2529 | off_t off; |
| 2530 | |
| 2531 | if(file_buffer == NULL) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2532 | queue_put(from_writer, |
plougher | c2a5ae1 | 2010-12-31 08:35:28 +0000 | [diff] [blame] | 2533 | write_error ? &write_error : NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2534 | continue; |
| 2535 | } |
| 2536 | |
| 2537 | off = file_buffer->block; |
| 2538 | |
| 2539 | pthread_mutex_lock(&pos_mutex); |
| 2540 | |
| 2541 | if(!write_error && lseek(fd, off, SEEK_SET) == -1) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2542 | ERROR("Lseek on destination failed because %s\n", |
| 2543 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2544 | write_error = TRUE; |
| 2545 | } |
| 2546 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2547 | if(!write_error && write_bytes(fd, file_buffer->data, |
| 2548 | file_buffer->size) == -1) { |
| 2549 | ERROR("Write on destination failed because %s\n", |
| 2550 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2551 | write_error = TRUE; |
| 2552 | } |
| 2553 | pthread_mutex_unlock(&pos_mutex); |
| 2554 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2555 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2560 | int all_zero(struct file_buffer *file_buffer) |
| 2561 | { |
| 2562 | int i; |
| 2563 | long entries = file_buffer->size / sizeof(long); |
| 2564 | long *p = (long *) file_buffer->data; |
| 2565 | |
| 2566 | for(i = 0; i < entries && p[i] == 0; i++); |
| 2567 | |
| 2568 | if(i == entries) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2569 | for(i = file_buffer->size & ~(sizeof(long) - 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2570 | i < file_buffer->size && file_buffer->data[i] == 0; |
| 2571 | i++); |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2572 | |
| 2573 | return i == file_buffer->size; |
| 2574 | } |
| 2575 | |
| 2576 | return 0; |
| 2577 | } |
| 2578 | |
| 2579 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2580 | void *deflator(void *arg) |
| 2581 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2582 | void *stream = NULL; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2583 | int res, oldstate; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2584 | |
| 2585 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2586 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2587 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2588 | res = compressor_init(comp, &stream, block_size, 1); |
| 2589 | if(res) |
| 2590 | BAD_ERROR("deflator:: compressor_init failed\n"); |
| 2591 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2592 | while(1) { |
| 2593 | struct file_buffer *file_buffer = queue_get(from_reader); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2594 | struct file_buffer *write_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2595 | |
Phillip Lougher | 4885438 | 2011-09-09 03:36:55 +0100 | [diff] [blame] | 2596 | if(file_buffer->file_size == 0) { |
| 2597 | file_buffer->c_byte = 0; |
| 2598 | queue_put(from_deflate, file_buffer); |
| 2599 | } else if(sparse_files && all_zero(file_buffer)) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2600 | file_buffer->c_byte = 0; |
| 2601 | queue_put(from_deflate, file_buffer); |
| 2602 | } else if(file_buffer->fragment) { |
| 2603 | file_buffer->c_byte = file_buffer->size; |
| 2604 | queue_put(from_deflate, file_buffer); |
| 2605 | } else { |
| 2606 | write_buffer = cache_get(writer_buffer, 0, 0); |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2607 | write_buffer->c_byte = mangle2(stream, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2608 | write_buffer->data, file_buffer->data, |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2609 | file_buffer->size, block_size, |
| 2610 | file_buffer->noD, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2611 | write_buffer->sequence = file_buffer->sequence; |
| 2612 | write_buffer->file_size = file_buffer->file_size; |
| 2613 | write_buffer->block = file_buffer->block; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2614 | write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2615 | (write_buffer->c_byte); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2616 | write_buffer->fragment = FALSE; |
| 2617 | write_buffer->error = FALSE; |
| 2618 | cache_block_put(file_buffer); |
| 2619 | queue_put(from_deflate, write_buffer); |
| 2620 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | |
| 2625 | void *frag_deflator(void *arg) |
| 2626 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2627 | void *stream = NULL; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2628 | int res, oldstate; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2629 | |
| 2630 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2631 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2632 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2633 | res = compressor_init(comp, &stream, block_size, 1); |
| 2634 | if(res) |
| 2635 | BAD_ERROR("frag_deflator:: compressor_init failed\n"); |
| 2636 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2637 | while(1) { |
| 2638 | int c_byte, compressed_size; |
| 2639 | struct file_buffer *file_buffer = queue_get(to_frag); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2640 | struct file_buffer *write_buffer = |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2641 | cache_get(writer_buffer, file_buffer->block + |
| 2642 | FRAG_INDEX, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2643 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2644 | c_byte = mangle2(stream, write_buffer->data, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2645 | file_buffer->size, block_size, noF, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2646 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte); |
plougher | d036a31 | 2008-03-08 01:38:27 +0000 | [diff] [blame] | 2647 | write_buffer->size = compressed_size; |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2648 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2649 | if(fragments_locked == FALSE) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2650 | fragment_table[file_buffer->block].size = c_byte; |
| 2651 | fragment_table[file_buffer->block].start_block = bytes; |
| 2652 | write_buffer->block = bytes; |
| 2653 | bytes += compressed_size; |
| 2654 | fragments_outstanding --; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2655 | queue_put(to_writer, write_buffer); |
plougher | 5750191 | 2009-09-20 02:20:42 +0000 | [diff] [blame] | 2656 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2657 | TRACE("Writing fragment %lld, uncompressed size %d, " |
| 2658 | "compressed size %d\n", file_buffer->block, |
| 2659 | file_buffer->size, compressed_size); |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2660 | } else { |
| 2661 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2662 | add_pending_fragment(write_buffer, c_byte, |
| 2663 | file_buffer->block); |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2664 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2665 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | |
| 2670 | #define HASH_ENTRIES 256 |
| 2671 | #define BLOCK_HASH(a) (a % HASH_ENTRIES) |
| 2672 | struct file_buffer *block_hash[HASH_ENTRIES]; |
| 2673 | |
| 2674 | void push_buffer(struct file_buffer *file_buffer) |
| 2675 | { |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2676 | int hash = BLOCK_HASH(file_buffer->sequence); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2677 | |
| 2678 | file_buffer->next = block_hash[hash]; |
| 2679 | block_hash[hash] = file_buffer; |
| 2680 | } |
| 2681 | |
| 2682 | |
| 2683 | struct file_buffer *get_file_buffer(struct queue *queue) |
| 2684 | { |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2685 | static unsigned int sequence = 0; |
| 2686 | int hash = BLOCK_HASH(sequence); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2687 | struct file_buffer *file_buffer = block_hash[hash], *prev = NULL; |
| 2688 | |
| 2689 | for(;file_buffer; prev = file_buffer, file_buffer = file_buffer->next) |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2690 | if(file_buffer->sequence == sequence) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2691 | break; |
| 2692 | |
| 2693 | if(file_buffer) { |
| 2694 | if(prev) |
| 2695 | prev->next = file_buffer->next; |
| 2696 | else |
| 2697 | block_hash[hash] = file_buffer->next; |
| 2698 | } else { |
| 2699 | while(1) { |
| 2700 | file_buffer = queue_get(queue); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2701 | if(file_buffer->sequence == sequence) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2702 | break; |
| 2703 | push_buffer(file_buffer); |
| 2704 | } |
| 2705 | } |
| 2706 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2707 | sequence ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2708 | |
| 2709 | return file_buffer; |
| 2710 | } |
| 2711 | |
| 2712 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2713 | void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2714 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2715 | { |
| 2716 | file_count ++; |
| 2717 | *duplicate_file = FALSE; |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2718 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0, |
| 2719 | NULL, &empty_fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2723 | void write_file_frag_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2724 | int size, int *duplicate_file, struct file_buffer *file_buffer, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2725 | unsigned short checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2726 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2727 | struct file_info *dupl_ptr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2728 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2729 | unsigned int *block_listp = NULL; |
| 2730 | long long start = 0; |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2731 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2732 | dupl_ptr = duplicate(size, 0, &block_listp, &start, &fragment, |
| 2733 | file_buffer, 0, 0, checksum, TRUE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2734 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2735 | if(dupl_ptr) { |
| 2736 | *duplicate_file = FALSE; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2737 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2738 | dupl_ptr->fragment = fragment; |
| 2739 | } else |
| 2740 | *duplicate_file = TRUE; |
| 2741 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2742 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2743 | |
| 2744 | total_bytes += size; |
| 2745 | file_count ++; |
| 2746 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2747 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2748 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2749 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2750 | 0, NULL, fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2754 | void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, int size, |
| 2755 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2756 | { |
| 2757 | struct fragment *fragment; |
| 2758 | unsigned short checksum; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2759 | |
| 2760 | checksum = get_checksum_mem_buffer(file_buffer); |
| 2761 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2762 | if(pre_duplicate_frag(size, checksum)) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2763 | write_file_frag_dup(inode, dir_ent, size, duplicate_file, |
| 2764 | file_buffer, checksum); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2765 | return; |
| 2766 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2767 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2768 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2769 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2770 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2771 | |
| 2772 | if(duplicate_checking) |
| 2773 | add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, TRUE); |
| 2774 | |
| 2775 | total_bytes += size; |
| 2776 | file_count ++; |
| 2777 | |
| 2778 | *duplicate_file = FALSE; |
| 2779 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2780 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2781 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2782 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2783 | 0, NULL, fragment, NULL, 0); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2784 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2785 | return; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2789 | int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2790 | struct file_buffer *read_buffer, int *duplicate_file) |
| 2791 | { |
| 2792 | long long read_size, file_bytes, start; |
| 2793 | struct fragment *fragment; |
| 2794 | unsigned int *block_list = NULL; |
| 2795 | int block = 0, status; |
| 2796 | long long sparse = 0; |
| 2797 | struct file_buffer *fragment_buffer = NULL; |
| 2798 | |
| 2799 | *duplicate_file = FALSE; |
| 2800 | |
| 2801 | lock_fragments(); |
| 2802 | |
| 2803 | file_bytes = 0; |
| 2804 | start = bytes; |
| 2805 | while (1) { |
| 2806 | read_size = read_buffer->file_size; |
| 2807 | if(read_buffer->fragment && read_buffer->c_byte) |
| 2808 | fragment_buffer = read_buffer; |
| 2809 | else { |
| 2810 | block_list = realloc(block_list, (block + 1) * |
| 2811 | sizeof(unsigned int)); |
| 2812 | if(block_list == NULL) |
| 2813 | BAD_ERROR("Out of memory allocating block_list" |
| 2814 | "\n"); |
| 2815 | block_list[block ++] = read_buffer->c_byte; |
| 2816 | if(read_buffer->c_byte) { |
| 2817 | read_buffer->block = bytes; |
| 2818 | bytes += read_buffer->size; |
| 2819 | cache_rehash(read_buffer, read_buffer->block); |
| 2820 | file_bytes += read_buffer->size; |
| 2821 | queue_put(to_writer, read_buffer); |
| 2822 | } else { |
| 2823 | sparse += read_buffer->size; |
| 2824 | cache_block_put(read_buffer); |
| 2825 | } |
| 2826 | } |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2827 | inc_progress_bar(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2828 | |
| 2829 | if(read_size != -1) |
| 2830 | break; |
| 2831 | |
| 2832 | read_buffer = get_file_buffer(from_deflate); |
| 2833 | if(read_buffer->error) |
| 2834 | goto read_err; |
| 2835 | } |
| 2836 | |
| 2837 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2838 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2839 | cache_block_put(fragment_buffer); |
| 2840 | |
| 2841 | if(duplicate_checking) |
| 2842 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2843 | 0, 0, FALSE); |
| 2844 | file_count ++; |
| 2845 | total_bytes += read_size; |
| 2846 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2847 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2848 | block, block_list, fragment, NULL, sparse); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2849 | |
| 2850 | if(duplicate_checking == FALSE) |
| 2851 | free(block_list); |
| 2852 | |
| 2853 | return 0; |
| 2854 | |
| 2855 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 2856 | dec_progress_bar(block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2857 | status = read_buffer->error; |
| 2858 | bytes = start; |
| 2859 | if(!block_device) { |
| 2860 | int res; |
| 2861 | |
| 2862 | queue_put(to_writer, NULL); |
| 2863 | if(queue_get(from_writer) != 0) |
| 2864 | EXIT_MKSQUASHFS(); |
| 2865 | res = ftruncate(fd, bytes); |
| 2866 | if(res != 0) |
| 2867 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2868 | strerror(errno)); |
| 2869 | } |
| 2870 | unlock_fragments(); |
| 2871 | free(block_list); |
| 2872 | cache_block_put(read_buffer); |
| 2873 | return status; |
| 2874 | } |
| 2875 | |
| 2876 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2877 | int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2878 | long long read_size, struct file_buffer *read_buffer, |
| 2879 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2880 | { |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 2881 | long long file_bytes, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2882 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2883 | unsigned int *block_list; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2884 | int block, status; |
| 2885 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2886 | long long sparse = 0; |
| 2887 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2888 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2889 | *duplicate_file = FALSE; |
| 2890 | |
plougher | 8713962 | 2010-12-16 05:19:30 +0000 | [diff] [blame] | 2891 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2892 | if(block_list == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2893 | BAD_ERROR("Out of memory allocating block_list\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2894 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2895 | lock_fragments(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2896 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2897 | file_bytes = 0; |
| 2898 | start = bytes; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2899 | for(block = 0; block < blocks;) { |
| 2900 | if(read_buffer->fragment && read_buffer->c_byte) { |
| 2901 | fragment_buffer = read_buffer; |
| 2902 | blocks = read_size >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2903 | } else { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2904 | block_list[block] = read_buffer->c_byte; |
| 2905 | if(read_buffer->c_byte) { |
| 2906 | read_buffer->block = bytes; |
| 2907 | bytes += read_buffer->size; |
| 2908 | cache_rehash(read_buffer, read_buffer->block); |
| 2909 | file_bytes += read_buffer->size; |
| 2910 | queue_put(to_writer, read_buffer); |
| 2911 | } else { |
| 2912 | sparse += read_buffer->size; |
| 2913 | cache_block_put(read_buffer); |
| 2914 | } |
| 2915 | } |
| 2916 | inc_progress_bar(); |
| 2917 | |
| 2918 | if(++block < blocks) { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2919 | read_buffer = get_file_buffer(from_deflate); |
| 2920 | if(read_buffer->error) |
| 2921 | goto read_err; |
| 2922 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2925 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2926 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2927 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2928 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2929 | if(duplicate_checking) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2930 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2931 | 0, 0, FALSE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2932 | file_count ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2933 | total_bytes += read_size; |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2934 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2935 | /* |
| 2936 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2937 | * a smaller block count on stat calls to sparse files. This is |
| 2938 | * to ensure intelligent applications like cp correctly handle the |
| 2939 | * file as a sparse file. If the file in the original filesystem isn't |
| 2940 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2941 | * report it as non-sparse on stat calls to preserve semantics |
| 2942 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2943 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2944 | sparse = 0; |
| 2945 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2946 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2947 | blocks, block_list, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2948 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2949 | if(duplicate_checking == FALSE) |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2950 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2951 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2952 | return 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2953 | |
| 2954 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 2955 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2956 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2957 | bytes = start; |
| 2958 | if(!block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2959 | int res; |
| 2960 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2961 | queue_put(to_writer, NULL); |
| 2962 | if(queue_get(from_writer) != 0) |
| 2963 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2964 | res = ftruncate(fd, bytes); |
| 2965 | if(res != 0) |
| 2966 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2967 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2968 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2969 | unlock_fragments(); |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2970 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2971 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2972 | return status; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2976 | int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2977 | long long read_size, struct file_buffer *read_buffer, |
| 2978 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2979 | { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2980 | int block, thresh; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2981 | long long file_bytes, dup_start, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2982 | struct fragment *fragment; |
| 2983 | struct file_info *dupl_ptr; |
| 2984 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2985 | unsigned int *block_list, *block_listp; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2986 | struct file_buffer **buffer_list; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 2987 | int status, num_locked_fragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2988 | long long sparse = 0; |
| 2989 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2990 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2991 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2992 | if(block_list == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2993 | BAD_ERROR("Out of memory allocating block_list\n"); |
| 2994 | block_listp = block_list; |
| 2995 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2996 | buffer_list = malloc(blocks * sizeof(struct file_buffer *)); |
| 2997 | if(buffer_list == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2998 | BAD_ERROR("Out of memory allocating file block list\n"); |
| 2999 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 3000 | num_locked_fragments = lock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3001 | |
| 3002 | file_bytes = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3003 | start = dup_start = bytes; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3004 | thresh = blocks > (writer_buffer_size - num_locked_fragments) ? |
| 3005 | blocks - (writer_buffer_size - num_locked_fragments): 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3006 | |
| 3007 | for(block = 0; block < blocks;) { |
| 3008 | if(read_buffer->fragment && read_buffer->c_byte) { |
| 3009 | fragment_buffer = read_buffer; |
| 3010 | blocks = read_size >> block_log; |
| 3011 | } else { |
| 3012 | block_list[block] = read_buffer->c_byte; |
| 3013 | |
| 3014 | if(read_buffer->c_byte) { |
| 3015 | read_buffer->block = bytes; |
| 3016 | bytes += read_buffer->size; |
| 3017 | file_bytes += read_buffer->size; |
| 3018 | cache_rehash(read_buffer, read_buffer->block); |
| 3019 | if(block < thresh) { |
| 3020 | buffer_list[block] = NULL; |
| 3021 | queue_put(to_writer, read_buffer); |
| 3022 | } else |
| 3023 | buffer_list[block] = read_buffer; |
| 3024 | } else { |
| 3025 | buffer_list[block] = NULL; |
| 3026 | sparse += read_buffer->size; |
| 3027 | cache_block_put(read_buffer); |
| 3028 | } |
| 3029 | } |
| 3030 | inc_progress_bar(); |
| 3031 | |
| 3032 | if(++block < blocks) { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3033 | read_buffer = get_file_buffer(from_deflate); |
| 3034 | if(read_buffer->error) |
| 3035 | goto read_err; |
| 3036 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3039 | dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start, |
| 3040 | &fragment, fragment_buffer, blocks, 0, 0, FALSE); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3041 | |
| 3042 | if(dupl_ptr) { |
| 3043 | *duplicate_file = FALSE; |
| 3044 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3045 | if(buffer_list[block]) |
| 3046 | queue_put(to_writer, buffer_list[block]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 3047 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3048 | dupl_ptr->fragment = fragment; |
| 3049 | } else { |
| 3050 | *duplicate_file = TRUE; |
| 3051 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3052 | cache_block_put(buffer_list[block]); |
| 3053 | bytes = start; |
| 3054 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3055 | int res; |
| 3056 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3057 | queue_put(to_writer, NULL); |
| 3058 | if(queue_get(from_writer) != 0) |
| 3059 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3060 | res = ftruncate(fd, bytes); |
| 3061 | if(res != 0) |
| 3062 | BAD_ERROR("Failed to truncate dest file because" |
| 3063 | " %s\n", strerror(errno)); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3064 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3067 | unlock_fragments(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3068 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3069 | free(buffer_list); |
| 3070 | file_count ++; |
| 3071 | total_bytes += read_size; |
| 3072 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3073 | /* |
| 3074 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3075 | * a smaller block count on stat calls to sparse files. This is |
| 3076 | * to ensure intelligent applications like cp correctly handle the |
| 3077 | * file as a sparse file. If the file in the original filesystem isn't |
| 3078 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3079 | * report it as non-sparse on stat calls to preserve semantics |
| 3080 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3081 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 3082 | sparse = 0; |
| 3083 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 3084 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, |
| 3085 | dup_start, blocks, block_listp, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3086 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3087 | if(*duplicate_file == TRUE) |
| 3088 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3089 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3090 | return 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3091 | |
| 3092 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 3093 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3094 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3095 | bytes = start; |
| 3096 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3097 | int res; |
| 3098 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3099 | queue_put(to_writer, NULL); |
| 3100 | if(queue_get(from_writer) != 0) |
| 3101 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3102 | res = ftruncate(fd, bytes); |
| 3103 | if(res != 0) |
| 3104 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 3105 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3106 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3107 | unlock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3108 | for(blocks = thresh; blocks < block; blocks ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3109 | cache_block_put(buffer_list[blocks]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3110 | free(buffer_list); |
| 3111 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3112 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3113 | return status; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3117 | void write_file(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 3118 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3119 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3120 | int status; |
| 3121 | struct file_buffer *read_buffer; |
| 3122 | long long read_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3123 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3124 | again: |
| 3125 | read_buffer = get_file_buffer(from_deflate); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3126 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 3127 | status = read_buffer->error; |
| 3128 | if(status) { |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3129 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3130 | goto file_err; |
| 3131 | } |
| 3132 | |
| 3133 | read_size = read_buffer->file_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3134 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3135 | if(read_size == -1) |
| 3136 | status = write_file_process(inode, dir_ent, read_buffer, |
| 3137 | duplicate_file); |
| 3138 | else if(read_size == 0) { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3139 | write_file_empty(inode, dir_ent, duplicate_file); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3140 | cache_block_put(read_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3141 | } else if(read_buffer->fragment && read_buffer->c_byte) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3142 | write_file_frag(inode, dir_ent, read_size, read_buffer, |
| 3143 | duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3144 | else if(pre_duplicate(read_size)) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3145 | status = write_file_blocks_dup(inode, dir_ent, read_size, |
| 3146 | read_buffer, duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3147 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3148 | status = write_file_blocks(inode, dir_ent, read_size, |
| 3149 | read_buffer, duplicate_file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3150 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3151 | file_err: |
| 3152 | if(status == 2) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3153 | ERROR("File %s changed size while reading filesystem, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3154 | "attempting to re-read\n", pathname(dir_ent)); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3155 | goto again; |
| 3156 | } else if(status == 1) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3157 | ERROR("Failed to read file %s, creating empty file\n", |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3158 | pathname(dir_ent)); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3159 | write_file_empty(inode, dir_ent, duplicate_file); |
| 3160 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 3164 | #define BUFF_SIZE 8192 |
| 3165 | char b_buffer[BUFF_SIZE]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3166 | char *name; |
| 3167 | char *basename_r(); |
| 3168 | |
| 3169 | char *getbase(char *pathname) |
| 3170 | { |
| 3171 | char *result; |
| 3172 | |
| 3173 | if(*pathname != '/') { |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 3174 | result = getcwd(b_buffer, BUFF_SIZE); |
| 3175 | if(result == NULL) |
| 3176 | return NULL; |
| 3177 | strcat(strcat(b_buffer, "/"), pathname); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3178 | } else |
| 3179 | strcpy(b_buffer, pathname); |
| 3180 | name = b_buffer; |
| 3181 | if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0)) |
| 3182 | return NULL; |
| 3183 | else |
| 3184 | return result; |
| 3185 | } |
| 3186 | |
| 3187 | |
| 3188 | char *basename_r() |
| 3189 | { |
| 3190 | char *s; |
| 3191 | char *p; |
| 3192 | int n = 1; |
| 3193 | |
| 3194 | for(;;) { |
| 3195 | s = name; |
| 3196 | if(*name == '\0') |
| 3197 | return NULL; |
| 3198 | if(*name != '/') { |
| 3199 | while(*name != '\0' && *name != '/') name++; |
| 3200 | n = name - s; |
| 3201 | } |
| 3202 | while(*name == '/') name++; |
| 3203 | if(strncmp(s, ".", n) == 0) |
| 3204 | continue; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3205 | if((*name == '\0') || (strncmp(s, "..", n) == 0) || |
| 3206 | ((p = basename_r()) == NULL)) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3207 | s[n] = '\0'; |
| 3208 | return s; |
| 3209 | } |
| 3210 | if(strcmp(p, "..") == 0) |
| 3211 | continue; |
| 3212 | return p; |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3217 | struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3218 | { |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3219 | int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino); |
| 3220 | struct inode_info *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3221 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3222 | /* |
| 3223 | * Look-up inode in hash table, if it already exists we have a |
| 3224 | * hard-link, so increment the nlink count and return it. |
| 3225 | * Don't do the look-up for directories because we don't hard-link |
| 3226 | * directories. |
| 3227 | */ |
| 3228 | if ((buf->st_mode & S_IFMT) != S_IFDIR) { |
| 3229 | for(inode = inode_info[ino_hash]; inode; inode = inode->next) { |
| 3230 | if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) { |
| 3231 | inode->nlink ++; |
| 3232 | return inode; |
| 3233 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3234 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
plougher | 288f685 | 2010-12-16 05:22:55 +0000 | [diff] [blame] | 3237 | inode = malloc(sizeof(struct inode_info)); |
| 3238 | if(inode == NULL) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3239 | BAD_ERROR("Out of memory in inode hash table entry allocation" |
| 3240 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3241 | |
| 3242 | memcpy(&inode->buf, buf, sizeof(struct stat)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3243 | inode->read = FALSE; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3244 | inode->root_entry = FALSE; |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3245 | inode->pseudo_file = pseudo; |
| 3246 | inode->pseudo_id = id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3247 | inode->inode = SQUASHFS_INVALID_BLK; |
| 3248 | inode->nlink = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3249 | inode->inode_number = 0; |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3250 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 3251 | /* |
| 3252 | * Copy filesystem wide defaults into inode, these filesystem |
| 3253 | * wide defaults may be altered on an individual inode basis by |
| 3254 | * user specified actions |
| 3255 | * |
| 3256 | */ |
| 3257 | inode->no_fragments = no_fragments; |
| 3258 | inode->always_use_fragments = always_use_fragments; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 3259 | inode->noD = noD; |
| 3260 | inode->noF = noF; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 3261 | |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3262 | if((buf->st_mode & S_IFMT) == S_IFREG) |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 3263 | progress_bar_size((buf->st_size + block_size - 1) >> block_log); |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3264 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3265 | inode->next = inode_info[ino_hash]; |
| 3266 | inode_info[ino_hash] = inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3267 | |
| 3268 | return inode; |
| 3269 | } |
| 3270 | |
| 3271 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3272 | inline struct inode_info *lookup_inode(struct stat *buf) |
| 3273 | { |
| 3274 | return lookup_inode2(buf, 0, 0); |
| 3275 | } |
| 3276 | |
| 3277 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3278 | inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this) |
| 3279 | { |
| 3280 | if (inode->inode_number == 0) |
| 3281 | inode->inode_number = use_this ? : inode_no ++; |
| 3282 | } |
| 3283 | |
| 3284 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3285 | inline struct dir_ent *create_dir_entry(char *name, char *source_name, |
| 3286 | char *nonstandard_pathname, struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3287 | { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3288 | struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent)); |
| 3289 | if(dir_ent == NULL) |
| 3290 | BAD_ERROR("Out of memory in linux_opendir\n"); |
| 3291 | |
| 3292 | dir_ent->name = name; |
| 3293 | dir_ent->source_name = source_name; |
| 3294 | dir_ent->nonstandard_pathname = nonstandard_pathname; |
| 3295 | dir_ent->our_dir = dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3296 | dir_ent->next = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3297 | |
| 3298 | return dir_ent; |
| 3299 | } |
| 3300 | |
| 3301 | |
| 3302 | inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir, |
| 3303 | struct inode_info *inode_info) |
| 3304 | { |
| 3305 | struct dir_info *dir = dir_ent->our_dir; |
| 3306 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3307 | if(sub_dir) |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3308 | sub_dir->dir_ent = dir_ent; |
| 3309 | dir_ent->inode = inode_info; |
| 3310 | dir_ent->dir = sub_dir; |
| 3311 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3312 | dir_ent->next = dir->list; |
| 3313 | dir->list = dir_ent; |
| 3314 | dir->count++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | |
| 3318 | inline void add_dir_entry2(char *name, char *source_name, |
| 3319 | char *nonstandard_pathname, struct dir_info *sub_dir, |
| 3320 | struct inode_info *inode_info, struct dir_info *dir) |
| 3321 | { |
| 3322 | struct dir_ent *dir_ent = create_dir_entry(name, source_name, |
| 3323 | nonstandard_pathname, dir); |
| 3324 | |
| 3325 | |
| 3326 | add_dir_entry(dir_ent, sub_dir, inode_info); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3330 | inline void free_dir_entry(struct dir_ent *dir_ent) |
| 3331 | { |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3332 | if(dir_ent->name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3333 | free(dir_ent->name); |
| 3334 | |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3335 | if(dir_ent->source_name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3336 | free(dir_ent->source_name); |
| 3337 | |
| 3338 | free(dir_ent); |
| 3339 | } |
| 3340 | |
| 3341 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3342 | inline void add_excluded(struct dir_info *dir) |
| 3343 | { |
| 3344 | dir->excluded ++; |
| 3345 | } |
| 3346 | |
| 3347 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3348 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3349 | void dir_scan(squashfs_inode *inode, char *pathname, |
| 3350 | struct dir_ent *(_readdir)(struct dir_info *)) |
| 3351 | { |
| 3352 | struct stat buf; |
| 3353 | struct dir_info *dir_info = dir_scan1(pathname, "", paths, _readdir, 1); |
| 3354 | struct dir_ent *dir_ent; |
| 3355 | |
| 3356 | if(dir_info == NULL) |
| 3357 | return; |
| 3358 | |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3359 | dir_scan2(dir_info, pseudo); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3360 | dir_scan3(dir_info, dir_info); |
| 3361 | do_move_actions(); |
| 3362 | dir_scan4(dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3363 | |
Phillip Lougher | d0fe1d5 | 2012-10-09 03:34:34 +0100 | [diff] [blame] | 3364 | dir_ent = create_dir_entry("", NULL, pathname, |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 3365 | scan1_opendir("", "", 0)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3366 | |
| 3367 | if(pathname[0] == '\0') { |
| 3368 | /* |
| 3369 | * dummy top level directory, if multiple sources specified on |
| 3370 | * command line |
| 3371 | */ |
| 3372 | memset(&buf, 0, sizeof(buf)); |
| 3373 | buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR; |
| 3374 | buf.st_uid = getuid(); |
| 3375 | buf.st_gid = getgid(); |
| 3376 | buf.st_mtime = time(NULL); |
| 3377 | buf.st_dev = 0; |
| 3378 | buf.st_ino = 0; |
| 3379 | dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0); |
| 3380 | } else { |
| 3381 | if(lstat(pathname, &buf) == -1) { |
| 3382 | ERROR("Cannot stat dir/file %s because %s, ignoring", |
| 3383 | pathname, strerror(errno)); |
| 3384 | return; |
| 3385 | } |
| 3386 | dir_ent->inode = lookup_inode(&buf); |
| 3387 | } |
| 3388 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3389 | alloc_inode_no(dir_ent->inode, root_inode_number); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3390 | dir_ent->dir = dir_info; |
| 3391 | dir_info->dir_ent = dir_ent; |
| 3392 | |
Andy Lutomirski | 3f31dcf | 2012-09-21 18:08:08 -0700 | [diff] [blame] | 3393 | eval_actions(dir_ent); |
| 3394 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3395 | if(sorted) { |
| 3396 | int res = generate_file_priorities(dir_info, 0, |
| 3397 | &dir_info->dir_ent->inode->buf); |
| 3398 | |
| 3399 | if(res == FALSE) |
| 3400 | BAD_ERROR("generate_file_priorities failed\n"); |
| 3401 | } |
| 3402 | queue_put(to_reader, dir_info); |
| 3403 | if(sorted) |
| 3404 | sort_files_and_write(dir_info); |
| 3405 | if(progress) |
| 3406 | enable_progress_bar(); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3407 | dir_scan5(inode, dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3408 | dir_ent->inode->inode = *inode; |
| 3409 | dir_ent->inode->type = SQUASHFS_DIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3413 | /* |
| 3414 | * dir_scan1 routines... |
| 3415 | * This scans the source directories into memory for processing |
| 3416 | */ |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3417 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3418 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3419 | struct dir_info *dir; |
| 3420 | |
plougher | 6da792b | 2010-12-16 05:25:39 +0000 | [diff] [blame] | 3421 | dir = malloc(sizeof(struct dir_info)); |
| 3422 | if(dir == NULL) |
plougher | fbfdda7 | 2010-07-21 01:09:14 +0000 | [diff] [blame] | 3423 | BAD_ERROR("Out of memory in scan1_opendir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3424 | |
| 3425 | if(pathname[0] != '\0' && (dir->linuxdir = opendir(pathname)) == NULL) { |
| 3426 | free(dir); |
| 3427 | return NULL; |
| 3428 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3429 | dir->pathname = pathname; |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3430 | dir->subpath = subpath; |
Phillip Lougher | ded70fa | 2011-12-25 02:14:58 +0000 | [diff] [blame] | 3431 | dir->count = 0; |
| 3432 | dir->directory_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3433 | dir->dir_is_ldir = TRUE; |
| 3434 | dir->list = NULL; |
Phillip Lougher | 0b5a124 | 2011-12-25 03:22:42 +0000 | [diff] [blame] | 3435 | dir->depth = depth; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3436 | dir->excluded = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3437 | |
| 3438 | return dir; |
| 3439 | } |
| 3440 | |
| 3441 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3442 | struct dir_ent *scan1_encomp_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3443 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3444 | static int index = 0; |
| 3445 | |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3446 | if(dir->count < old_root_entries) { |
| 3447 | int i; |
| 3448 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3449 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3450 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3451 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3452 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3453 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3454 | } |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3455 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3456 | |
| 3457 | while(index < source) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3458 | char *basename = NULL; |
| 3459 | char *dir_name = getbase(source_path[index]); |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3460 | int pass = 1, res; |
plougher | 89fe2c7 | 2010-12-16 05:31:34 +0000 | [diff] [blame] | 3461 | |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3462 | if(dir_name == NULL) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3463 | ERROR("Bad source directory %s - skipping ...\n", |
| 3464 | source_path[index]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3465 | index ++; |
| 3466 | continue; |
| 3467 | } |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3468 | dir_name = strdup(dir_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3469 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3470 | struct dir_ent *dir_ent = dir->list; |
| 3471 | |
| 3472 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3473 | dir_ent = dir_ent->next); |
| 3474 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3475 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3476 | ERROR("Source directory entry %s already used! - trying" |
| 3477 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3478 | if(pass == 1) |
| 3479 | basename = dir_name; |
| 3480 | else |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3481 | free(dir_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3482 | res = asprintf(&dir_name, "%s_%d", basename, pass++); |
| 3483 | if(res == -1) |
| 3484 | BAD_ERROR("asprintf failed in " |
| 3485 | "scan1_encomp_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3486 | ERROR("%s\n", dir_name); |
| 3487 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3488 | return create_dir_entry(dir_name, basename, |
| 3489 | source_path[index ++], dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3490 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3491 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3495 | struct dir_ent *scan1_single_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3496 | { |
| 3497 | struct dirent *d_name; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3498 | int i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3499 | |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3500 | if(dir->count < old_root_entries) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3501 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3502 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3503 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3504 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3505 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3506 | } |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3507 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3508 | |
| 3509 | if((d_name = readdir(dir->linuxdir)) != NULL) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3510 | char *basename = NULL; |
| 3511 | char *dir_name = strdup(d_name->d_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3512 | int pass = 1, res; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3513 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3514 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3515 | struct dir_ent *dir_ent = dir->list; |
| 3516 | |
| 3517 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3518 | dir_ent = dir_ent->next); |
| 3519 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3520 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3521 | ERROR("Source directory entry %s already used! - trying" |
| 3522 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3523 | if (pass == 1) |
| 3524 | basename = dir_name; |
| 3525 | else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3526 | free(dir_name); |
| 3527 | res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++); |
| 3528 | if(res == -1) |
| 3529 | BAD_ERROR("asprintf failed in " |
| 3530 | "scan1_single_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3531 | ERROR("%s\n", dir_name); |
| 3532 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3533 | return create_dir_entry(dir_name, basename, NULL, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3536 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3540 | struct dir_ent *scan1_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3541 | { |
plougher | 480d9bf | 2010-12-18 02:28:39 +0000 | [diff] [blame] | 3542 | struct dirent *d_name = readdir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3543 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3544 | return d_name ? |
| 3545 | create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) : |
| 3546 | NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3550 | void scan1_freedir(struct dir_info *dir) |
| 3551 | { |
plougher | fbed12b | 2006-02-07 12:45:53 +0000 | [diff] [blame] | 3552 | if(dir->pathname[0] != '\0') |
| 3553 | closedir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3557 | struct dir_info *dir_scan1(char *filename, char *subpath, |
| 3558 | struct pathnames *paths, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3559 | struct dir_ent *(_readdir)(struct dir_info *), int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3560 | { |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3561 | struct dir_info *dir = scan1_opendir(filename, subpath, depth); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3562 | struct dir_ent *dir_ent; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3563 | |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3564 | if(dir == NULL) { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3565 | ERROR("Could not open %s, skipping...\n", filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3566 | goto error; |
| 3567 | } |
| 3568 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3569 | while((dir_ent = _readdir(dir))) { |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3570 | struct dir_info *sub_dir; |
| 3571 | struct stat buf; |
| 3572 | struct pathnames *new; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3573 | char *filename = pathname(dir_ent); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3574 | char *subpath = subpathname(dir_ent); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3575 | char *dir_name = dir_ent->name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3576 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3577 | if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3578 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3579 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3580 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3581 | |
| 3582 | if(lstat(filename, &buf) == -1) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3583 | ERROR("Cannot stat dir/file %s because %s, ignoring", |
| 3584 | filename, strerror(errno)); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3585 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3586 | continue; |
| 3587 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3588 | |
| 3589 | if((buf.st_mode & S_IFMT) != S_IFREG && |
| 3590 | (buf.st_mode & S_IFMT) != S_IFDIR && |
| 3591 | (buf.st_mode & S_IFMT) != S_IFLNK && |
| 3592 | (buf.st_mode & S_IFMT) != S_IFCHR && |
| 3593 | (buf.st_mode & S_IFMT) != S_IFBLK && |
| 3594 | (buf.st_mode & S_IFMT) != S_IFIFO && |
| 3595 | (buf.st_mode & S_IFMT) != S_IFSOCK) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3596 | ERROR("File %s has unrecognised filetype %d, ignoring" |
| 3597 | "\n", filename, buf.st_mode & S_IFMT); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3598 | free_dir_entry(dir_ent); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3599 | continue; |
| 3600 | } |
| 3601 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3602 | if((old_exclude && old_excluded(filename, &buf)) || |
| 3603 | excluded(paths, dir_name, &new) || |
Phillip Lougher | b4f4b0d | 2012-02-20 02:30:26 +0000 | [diff] [blame] | 3604 | eval_exclude_actions(dir_name, filename, subpath, |
| 3605 | &buf, depth)) { |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3606 | add_excluded(dir); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3607 | free_dir_entry(dir_ent); |
Phillip Lougher | 10d8de0 | 2011-08-29 00:14:48 +0100 | [diff] [blame] | 3608 | continue; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3609 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3610 | |
| 3611 | if((buf.st_mode & S_IFMT) == S_IFDIR) { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3612 | filename = strdup(filename); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3613 | subpath = strdup(subpath); |
| 3614 | sub_dir = dir_scan1(filename, subpath, new, |
| 3615 | scan1_readdir, depth + 1); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3616 | if(sub_dir == NULL) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3617 | free_dir_entry(dir_ent); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3618 | free(filename); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3619 | free(subpath); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3620 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3621 | } |
Phillip Lougher | 6a78a2d | 2011-12-28 03:54:19 +0000 | [diff] [blame] | 3622 | |
Phillip Lougher | b4f4b0d | 2012-02-20 02:30:26 +0000 | [diff] [blame] | 3623 | if(eval_empty_actions(dir_name, filename, subpath, &buf, |
| 3624 | depth, sub_dir)) { |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3625 | add_excluded(dir); |
Phillip Lougher | b2e9a40 | 2012-02-19 00:20:22 +0000 | [diff] [blame] | 3626 | free(sub_dir); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3627 | free_dir_entry(dir_ent); |
Phillip Lougher | b2e9a40 | 2012-02-19 00:20:22 +0000 | [diff] [blame] | 3628 | free(filename); |
| 3629 | free(subpath); |
Phillip Lougher | 6a78a2d | 2011-12-28 03:54:19 +0000 | [diff] [blame] | 3630 | continue; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3633 | dir->directory_count ++; |
| 3634 | } else |
| 3635 | sub_dir = NULL; |
| 3636 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3637 | add_dir_entry(dir_ent, sub_dir, lookup_inode(&buf)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | scan1_freedir(dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3641 | |
| 3642 | error: |
| 3643 | return dir; |
| 3644 | } |
| 3645 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3646 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3647 | /* |
| 3648 | * dir_scan2 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3649 | * This processes most actions and any pseudo files |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3650 | */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3651 | 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] | 3652 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3653 | if (dir_ent == NULL) |
| 3654 | dir_ent = dir->list; |
| 3655 | else |
| 3656 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3657 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3658 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next); |
| 3659 | |
| 3660 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3661 | } |
| 3662 | |
| 3663 | |
| 3664 | struct dir_ent *scan2_lookup(struct dir_info *dir, char *name) |
| 3665 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3666 | struct dir_ent *dir_ent = dir->list; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3667 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3668 | for(; dir_ent && strcmp(dir_ent->name, name) != 0; |
| 3669 | dir_ent = dir_ent->next); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3670 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3671 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3675 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3676 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3677 | struct dir_ent *dir_ent = NULL; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3678 | struct pseudo_entry *pseudo_ent; |
| 3679 | struct stat buf; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3680 | static int pseudo_ino = 1; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3681 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3682 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3683 | struct inode_info *inode_info = dir_ent->inode; |
| 3684 | struct stat *buf = &inode_info->buf; |
| 3685 | char *name = dir_ent->name; |
| 3686 | |
Phillip Lougher | 89d757c | 2011-09-20 00:33:19 +0100 | [diff] [blame] | 3687 | eval_actions(dir_ent); |
| 3688 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3689 | if((buf->st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3690 | dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) { |
| 3694 | dir_ent = scan2_lookup(dir, pseudo_ent->name); |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3695 | if(pseudo_ent->dev->type == 'm') { |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3696 | struct stat *buf; |
| 3697 | if(dir_ent == NULL) { |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3698 | ERROR("Pseudo modify file \"%s\" does not exist " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3699 | "in source filesystem. Ignoring.\n", |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3700 | pseudo_ent->pathname); |
| 3701 | continue; |
| 3702 | } |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3703 | if(dir_ent->inode->root_entry) { |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3704 | ERROR("Pseudo modify file \"%s\" is a pre-existing" |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3705 | " file in the filesystem being appended" |
| 3706 | " to. It cannot be modified. " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3707 | "Ignoring.\n", pseudo_ent->pathname); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3708 | continue; |
| 3709 | } |
| 3710 | buf = &dir_ent->inode->buf; |
| 3711 | buf->st_mode = (buf->st_mode & S_IFMT) | |
| 3712 | pseudo_ent->dev->mode; |
| 3713 | buf->st_uid = pseudo_ent->dev->uid; |
| 3714 | buf->st_gid = pseudo_ent->dev->gid; |
| 3715 | continue; |
| 3716 | } |
| 3717 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3718 | if(dir_ent) { |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3719 | if(dir_ent->inode->root_entry) |
| 3720 | ERROR("Pseudo file \"%s\" is a pre-existing" |
| 3721 | " file in the filesystem being appended" |
| 3722 | " to. Ignoring.\n", |
| 3723 | pseudo_ent->pathname); |
| 3724 | else |
| 3725 | ERROR("Pseudo file \"%s\" exists in source " |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3726 | "filesystem \"%s\".\nIgnoring, " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3727 | "exclude it (-e/-ef) to override.\n", |
| 3728 | pseudo_ent->pathname, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3729 | pathname(dir_ent)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3730 | continue; |
| 3731 | } |
| 3732 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3733 | memset(&buf, 0, sizeof(buf)); |
| 3734 | buf.st_mode = pseudo_ent->dev->mode; |
| 3735 | buf.st_uid = pseudo_ent->dev->uid; |
| 3736 | buf.st_gid = pseudo_ent->dev->gid; |
| 3737 | buf.st_rdev = makedev(pseudo_ent->dev->major, |
| 3738 | pseudo_ent->dev->minor); |
plougher | 7e58f4d | 2009-04-05 12:06:19 +0000 | [diff] [blame] | 3739 | buf.st_mtime = time(NULL); |
plougher | 1a3fbf2 | 2009-04-05 12:04:16 +0000 | [diff] [blame] | 3740 | buf.st_ino = pseudo_ino ++; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3741 | |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3742 | if(pseudo_ent->dev->type == 'd') { |
| 3743 | struct dir_ent *dir_ent = |
| 3744 | create_dir_entry(pseudo_ent->name, NULL, |
| 3745 | pseudo_ent->pathname, dir); |
| 3746 | char *subpath = strdup(subpathname(dir_ent)); |
| 3747 | struct dir_info *sub_dir = scan1_opendir("", subpath, |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3748 | dir->depth + 1); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3749 | if(sub_dir == NULL) { |
| 3750 | ERROR("Could not create pseudo directory \"%s\"" |
| 3751 | ", skipping...\n", |
| 3752 | pseudo_ent->pathname); |
| 3753 | free(subpath); |
| 3754 | pseudo_ino --; |
| 3755 | continue; |
| 3756 | } |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3757 | dir_scan2(sub_dir, pseudo_ent->pseudo); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3758 | dir->directory_count ++; |
| 3759 | add_dir_entry(dir_ent, sub_dir, |
| 3760 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0)); |
| 3761 | } else if(pseudo_ent->dev->type == 'f') { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3762 | #ifdef USE_TMP_FILE |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3763 | struct stat buf2; |
| 3764 | int res = stat(pseudo_ent->dev->filename, &buf2); |
| 3765 | if(res == -1) { |
| 3766 | ERROR("Stat on pseudo file \"%s\" failed, " |
| 3767 | "skipping...", pseudo_ent->pathname); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3768 | pseudo_ino --; |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3769 | continue; |
| 3770 | } |
| 3771 | buf.st_size = buf2.st_size; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3772 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3773 | pseudo_ent->dev->filename, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3774 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3775 | #else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3776 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3777 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3778 | lookup_inode2(&buf, PSEUDO_FILE_PROCESS, |
| 3779 | pseudo_ent->dev->pseudo_id), dir); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3780 | #endif |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3781 | } else { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3782 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3783 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3784 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3785 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3786 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3790 | /* |
| 3791 | * dir_scan3 routines... |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3792 | * This processes the move action |
| 3793 | */ |
| 3794 | void dir_scan3(struct dir_info *root, struct dir_info *dir) |
| 3795 | { |
| 3796 | struct dir_ent *dir_ent = NULL; |
| 3797 | |
| 3798 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
| 3799 | |
| 3800 | eval_move_actions(root, dir_ent); |
| 3801 | |
| 3802 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
| 3803 | dir_scan3(root, dir_ent->dir); |
| 3804 | } |
| 3805 | } |
| 3806 | /* |
| 3807 | * dir_scan4 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3808 | * This sorts every directory and computes the inode numbers |
| 3809 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3810 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3811 | /* |
| 3812 | * Bottom up linked list merge sort. |
| 3813 | * |
| 3814 | * Qsort and other O(n log n) algorithms work well with arrays but not |
| 3815 | * linked lists. Merge sort another O(n log n) sort algorithm on the other hand |
| 3816 | * is not ideal for arrays (as it needs an additonal n storage locations |
| 3817 | * as sorting is not done in place), but it is ideal for linked lists because |
| 3818 | * it doesn't require any extra storage, |
| 3819 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3820 | void sort_directory(struct dir_info *dir) |
| 3821 | { |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3822 | struct dir_ent *cur, *l1, *l2, *next; |
| 3823 | int len1, len2, stride = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3824 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3825 | if(dir->count < 2) |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3826 | return; |
| 3827 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3828 | /* |
| 3829 | * We can consider our linked-list to be made up of stride length |
| 3830 | * sublists. Eacn iteration around this loop merges adjacent |
| 3831 | * stride length sublists into larger 2*stride sublists. We stop |
| 3832 | * when stride becomes equal to the entire list. |
| 3833 | * |
| 3834 | * Initially stride = 1 (by definition a sublist of 1 is sorted), and |
| 3835 | * these 1 element sublists are merged into 2 element sublists, which |
| 3836 | * are then merged into 4 element sublists and so on. |
| 3837 | */ |
| 3838 | do { |
| 3839 | l2 = dir->list; /* head of current linked list */ |
| 3840 | cur = NULL; /* empty output list */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3841 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3842 | /* |
| 3843 | * Iterate through the linked list, merging adjacent sublists. |
| 3844 | * On each interation l2 points to the next sublist pair to be |
| 3845 | * merged (if there's only one sublist left this is simply added |
| 3846 | * to the output list) |
| 3847 | */ |
| 3848 | while(l2) { |
| 3849 | l1 = l2; |
| 3850 | for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next); |
| 3851 | len2 = stride; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3852 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3853 | /* |
| 3854 | * l1 points to first sublist. |
| 3855 | * l2 points to second sublist. |
| 3856 | * Merge them onto the output list |
| 3857 | */ |
| 3858 | while(len1 && l2 && len2) { |
| 3859 | if(strcmp(l1->name, l2->name) <= 0) { |
| 3860 | next = l1; |
| 3861 | l1 = l1->next; |
| 3862 | len1 --; |
| 3863 | } else { |
| 3864 | next = l2; |
| 3865 | l2 = l2->next; |
| 3866 | len2 --; |
| 3867 | } |
| 3868 | |
| 3869 | if(cur) { |
| 3870 | cur->next = next; |
| 3871 | cur = next; |
| 3872 | } else |
| 3873 | dir->list = cur = next; |
| 3874 | } |
| 3875 | /* |
| 3876 | * One sublist is now empty, copy the other one onto the |
| 3877 | * output list |
| 3878 | */ |
| 3879 | for(; len1; len1 --, l1 = l1->next) { |
| 3880 | if(cur) { |
| 3881 | cur->next = l1; |
| 3882 | cur = l1; |
| 3883 | } else |
| 3884 | dir->list = cur = l1; |
| 3885 | } |
| 3886 | for(; l2 && len2; len2 --, l2 = l2->next) { |
| 3887 | if(cur) { |
| 3888 | cur->next = l2; |
| 3889 | cur = l2; |
| 3890 | } else |
| 3891 | dir->list = cur = l2; |
| 3892 | } |
| 3893 | } |
| 3894 | cur->next = NULL; |
| 3895 | stride = stride << 1; |
| 3896 | } while(stride < dir->count); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3900 | void dir_scan4(struct dir_info *dir) |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3901 | { |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3902 | struct dir_ent *dir_ent; |
| 3903 | unsigned int byte_count = 0; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3904 | |
| 3905 | sort_directory(dir); |
| 3906 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3907 | for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) { |
| 3908 | byte_count += strlen(dir_ent->name) + |
| 3909 | sizeof(struct squashfs_dir_entry); |
| 3910 | |
| 3911 | if(dir_ent->inode->root_entry) |
| 3912 | continue; |
| 3913 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3914 | alloc_inode_no(dir_ent->inode, 0); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3915 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3916 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3917 | dir_scan4(dir_ent->dir); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3918 | } |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3919 | |
| 3920 | if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE)) |
| 3921 | dir->dir_is_ldir = FALSE; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | |
| 3925 | /* |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3926 | * dir_scan5 routines... |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3927 | * This generates the filesystem metadata and writes it out to the destination |
| 3928 | */ |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3929 | void scan5_init_dir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3930 | { |
| 3931 | dir->buff = malloc(SQUASHFS_METADATA_SIZE); |
| 3932 | if(dir->buff == NULL) { |
| 3933 | BAD_ERROR("Out of memory allocating directory buffer\n"); |
| 3934 | } |
| 3935 | |
| 3936 | dir->size = SQUASHFS_METADATA_SIZE; |
| 3937 | dir->p = dir->index_count_p = dir->buff; |
| 3938 | dir->entry_count = 256; |
| 3939 | dir->entry_count_p = NULL; |
| 3940 | dir->index = NULL; |
| 3941 | dir->i_count = dir->i_size = 0; |
| 3942 | } |
| 3943 | |
| 3944 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3945 | struct dir_ent *scan5_readdir(struct directory *dir, struct dir_info *dir_info, |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3946 | struct dir_ent *dir_ent) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3947 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3948 | if (dir_ent == NULL) |
| 3949 | dir_ent = dir_info->list; |
| 3950 | else |
| 3951 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3952 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3953 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next) |
| 3954 | add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number, |
| 3955 | dir_ent->name, dir_ent->inode->type, dir); |
| 3956 | |
| 3957 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3961 | void scan5_freedir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3962 | { |
| 3963 | if(dir->index) |
| 3964 | free(dir->index); |
| 3965 | free(dir->buff); |
| 3966 | } |
| 3967 | |
| 3968 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3969 | void dir_scan5(squashfs_inode *inode, struct dir_info *dir_info) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3970 | { |
| 3971 | int squashfs_type; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3972 | int duplicate_file; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3973 | struct directory dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3974 | struct dir_ent *dir_ent = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3975 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3976 | scan5_init_dir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3977 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3978 | while((dir_ent = scan5_readdir(&dir, dir_info, dir_ent)) != NULL) { |
Phillip Lougher | 0a67088 | 2012-10-05 04:09:13 +0100 | [diff] [blame] | 3979 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3980 | |
| 3981 | if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) { |
| 3982 | switch(buf->st_mode & S_IFMT) { |
| 3983 | case S_IFREG: |
| 3984 | squashfs_type = SQUASHFS_FILE_TYPE; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3985 | write_file(inode, dir_ent, |
| 3986 | &duplicate_file); |
| 3987 | INFO("file %s, uncompressed size %lld " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3988 | "bytes %s\n", |
| 3989 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3990 | (long long) buf->st_size, |
| 3991 | duplicate_file ? "DUPLICATE" : |
| 3992 | ""); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3993 | break; |
| 3994 | |
| 3995 | case S_IFDIR: |
| 3996 | squashfs_type = SQUASHFS_DIR_TYPE; |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3997 | dir_scan5(inode, dir_ent->dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3998 | break; |
| 3999 | |
| 4000 | case S_IFLNK: |
| 4001 | squashfs_type = SQUASHFS_SYMLINK_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4002 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4003 | squashfs_type, 0, 0, 0, NULL, |
| 4004 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4005 | INFO("symbolic link %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4006 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4007 | sym_count ++; |
| 4008 | break; |
| 4009 | |
| 4010 | case S_IFCHR: |
| 4011 | squashfs_type = SQUASHFS_CHRDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4012 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4013 | squashfs_type, 0, 0, 0, NULL, |
| 4014 | NULL, NULL, 0); |
| 4015 | INFO("character device %s inode 0x%llx" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4016 | "\n", subpathname(dir_ent), |
| 4017 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4018 | dev_count ++; |
| 4019 | break; |
| 4020 | |
| 4021 | case S_IFBLK: |
| 4022 | squashfs_type = SQUASHFS_BLKDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4023 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4024 | squashfs_type, 0, 0, 0, NULL, |
| 4025 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4026 | INFO("block device %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4027 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4028 | dev_count ++; |
| 4029 | break; |
| 4030 | |
| 4031 | case S_IFIFO: |
| 4032 | squashfs_type = SQUASHFS_FIFO_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4033 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4034 | squashfs_type, 0, 0, 0, NULL, |
| 4035 | NULL, NULL, 0); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4036 | INFO("fifo %s inode 0x%llx\n", |
| 4037 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4038 | fifo_count ++; |
| 4039 | break; |
| 4040 | |
| 4041 | case S_IFSOCK: |
| 4042 | squashfs_type = SQUASHFS_SOCKET_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4043 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4044 | squashfs_type, 0, 0, 0, NULL, |
| 4045 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4046 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4047 | "0x%llx\n", |
| 4048 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4049 | sock_count ++; |
| 4050 | break; |
| 4051 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4052 | default: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4053 | BAD_ERROR("%s unrecognised file type, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 4054 | "mode is %x\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4055 | subpathname(dir_ent), |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4056 | buf->st_mode); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 4057 | } |
| 4058 | dir_ent->inode->inode = *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4059 | dir_ent->inode->type = squashfs_type; |
| 4060 | } else { |
| 4061 | *inode = dir_ent->inode->inode; |
| 4062 | squashfs_type = dir_ent->inode->type; |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4063 | switch(squashfs_type) { |
| 4064 | case SQUASHFS_FILE_TYPE: |
| 4065 | if(!sorted) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4066 | INFO("file %s, uncompressed " |
| 4067 | "size %lld bytes LINK" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4068 | "\n", |
| 4069 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 4070 | (long long) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4071 | buf->st_size); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4072 | break; |
| 4073 | case SQUASHFS_SYMLINK_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4074 | INFO("symbolic link %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4075 | "LINK\n", subpathname(dir_ent), |
| 4076 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4077 | break; |
| 4078 | case SQUASHFS_CHRDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4079 | INFO("character device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4080 | "LINK\n", subpathname(dir_ent), |
| 4081 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4082 | break; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4083 | case SQUASHFS_BLKDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4084 | INFO("block device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4085 | "LINK\n", subpathname(dir_ent), |
| 4086 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4087 | break; |
| 4088 | case SQUASHFS_FIFO_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4089 | INFO("fifo %s inode 0x%llx LINK\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4090 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4091 | break; |
| 4092 | case SQUASHFS_SOCKET_TYPE: |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4093 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4094 | "0x%llx LINK\n", |
| 4095 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4096 | break; |
| 4097 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
Phillip Lougher | 0366aec | 2012-10-05 04:06:04 +0100 | [diff] [blame] | 4100 | add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name, |
| 4101 | squashfs_type, &dir); |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 4102 | update_progress_bar(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 4105 | write_dir(inode, dir_info, &dir); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4106 | INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent), |
| 4107 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4108 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 4109 | scan5_freedir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | |
| 4113 | unsigned int slog(unsigned int block) |
| 4114 | { |
| 4115 | int i; |
| 4116 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4117 | for(i = 12; i <= 20; i++) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4118 | if(block == (1 << i)) |
| 4119 | return i; |
| 4120 | return 0; |
| 4121 | } |
| 4122 | |
| 4123 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4124 | int old_excluded(char *filename, struct stat *buf) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4125 | { |
| 4126 | int i; |
| 4127 | |
| 4128 | for(i = 0; i < exclude; i++) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4129 | if((exclude_paths[i].st_dev == buf->st_dev) && |
| 4130 | (exclude_paths[i].st_ino == buf->st_ino)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4131 | return TRUE; |
| 4132 | return FALSE; |
| 4133 | } |
| 4134 | |
| 4135 | |
| 4136 | #define ADD_ENTRY(buf) \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4137 | if(exclude % EXCLUDE_SIZE == 0) { \ |
| 4138 | exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \ |
| 4139 | * sizeof(struct exclude_info)); \ |
| 4140 | if(exclude_paths == NULL) \ |
| 4141 | BAD_ERROR("Out of memory in exclude dir/file table\n"); \ |
| 4142 | } \ |
| 4143 | exclude_paths[exclude].st_dev = buf.st_dev; \ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4144 | exclude_paths[exclude++].st_ino = buf.st_ino; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4145 | int old_add_exclude(char *path) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4146 | { |
| 4147 | int i; |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4148 | char filename[4096]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4149 | struct stat buf; |
| 4150 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4151 | if(path[0] == '/' || strncmp(path, "./", 2) == 0 || |
| 4152 | strncmp(path, "../", 3) == 0) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4153 | if(lstat(path, &buf) == -1) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4154 | ERROR("Cannot stat exclude dir/file %s because %s, " |
| 4155 | "ignoring", path, strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4156 | return TRUE; |
| 4157 | } |
| 4158 | ADD_ENTRY(buf); |
| 4159 | return TRUE; |
| 4160 | } |
| 4161 | |
| 4162 | for(i = 0; i < source; i++) { |
| 4163 | strcat(strcat(strcpy(filename, source_path[i]), "/"), path); |
| 4164 | if(lstat(filename, &buf) == -1) { |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4165 | if(!(errno == ENOENT || errno == ENOTDIR)) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4166 | ERROR("Cannot stat exclude dir/file %s because " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4167 | "%s, ignoring", filename, |
| 4168 | strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4169 | continue; |
| 4170 | } |
| 4171 | ADD_ENTRY(buf); |
| 4172 | } |
| 4173 | return TRUE; |
| 4174 | } |
| 4175 | |
| 4176 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4177 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 4178 | int type) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4179 | { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4180 | old_root_entry = realloc(old_root_entry, |
| 4181 | sizeof(struct old_root_entry_info) * (old_root_entries + 1)); |
| 4182 | if(old_root_entry == NULL) |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4183 | BAD_ERROR("Out of memory in old root directory entries " |
| 4184 | "reallocation\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4185 | |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 4186 | old_root_entry[old_root_entries].name = strdup(name); |
| 4187 | old_root_entry[old_root_entries].inode.inode = inode; |
| 4188 | old_root_entry[old_root_entries].inode.inode_number = inode_number; |
| 4189 | old_root_entry[old_root_entries].inode.type = type; |
| 4190 | old_root_entry[old_root_entries++].inode.root_entry = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4194 | void initialise_threads(int readb_mbytes, int writeb_mbytes, |
| 4195 | int fragmentb_mbytes) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4196 | { |
| 4197 | int i; |
| 4198 | sigset_t sigmask, old_mask; |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4199 | int reader_buffer_size = readb_mbytes << (20 - block_log); |
| 4200 | int fragment_buffer_size = fragmentb_mbytes << (20 - block_log); |
| 4201 | |
| 4202 | /* |
| 4203 | * writer_buffer_size is global because it is needed in |
| 4204 | * write_file_blocks_dup() |
| 4205 | */ |
| 4206 | writer_buffer_size = writeb_mbytes << (20 - block_log); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4207 | |
| 4208 | sigemptyset(&sigmask); |
| 4209 | sigaddset(&sigmask, SIGINT); |
| 4210 | sigaddset(&sigmask, SIGQUIT); |
| 4211 | if(sigprocmask(SIG_BLOCK, &sigmask, &old_mask) == -1) |
| 4212 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4213 | |
| 4214 | signal(SIGUSR1, sigusr1_handler); |
| 4215 | |
| 4216 | if(processors == -1) { |
| 4217 | #ifndef linux |
| 4218 | int mib[2]; |
| 4219 | size_t len = sizeof(processors); |
| 4220 | |
| 4221 | mib[0] = CTL_HW; |
| 4222 | #ifdef HW_AVAILCPU |
| 4223 | mib[1] = HW_AVAILCPU; |
| 4224 | #else |
| 4225 | mib[1] = HW_NCPU; |
| 4226 | #endif |
| 4227 | |
| 4228 | if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4229 | ERROR("Failed to get number of available processors. " |
| 4230 | "Defaulting to 1\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4231 | processors = 1; |
| 4232 | } |
| 4233 | #else |
plougher | 9cc26b7 | 2010-08-17 01:05:55 +0000 | [diff] [blame] | 4234 | processors = sysconf(_SC_NPROCESSORS_ONLN); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4235 | #endif |
| 4236 | } |
| 4237 | |
plougher | f2d2a84 | 2010-12-18 02:43:48 +0000 | [diff] [blame] | 4238 | thread = malloc((2 + processors * 2) * sizeof(pthread_t)); |
| 4239 | if(thread == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4240 | BAD_ERROR("Out of memory allocating thread descriptors\n"); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4241 | deflator_thread = &thread[2]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4242 | frag_deflator_thread = &deflator_thread[processors]; |
| 4243 | |
| 4244 | to_reader = queue_init(1); |
| 4245 | from_reader = queue_init(reader_buffer_size); |
| 4246 | to_writer = queue_init(writer_buffer_size); |
| 4247 | from_writer = queue_init(1); |
| 4248 | from_deflate = queue_init(reader_buffer_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 4249 | to_frag = queue_init(fragment_buffer_size); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 4250 | reader_buffer = cache_init(block_size, reader_buffer_size); |
| 4251 | writer_buffer = cache_init(block_size, writer_buffer_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 4252 | fragment_buffer = cache_init(block_size, fragment_buffer_size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4253 | pthread_create(&thread[0], NULL, reader, NULL); |
| 4254 | pthread_create(&thread[1], NULL, writer, NULL); |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 4255 | init_progress_bar(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4256 | pthread_mutex_init(&fragment_mutex, NULL); |
| 4257 | pthread_cond_init(&fragment_waiting, NULL); |
| 4258 | |
| 4259 | for(i = 0; i < processors; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4260 | if(pthread_create(&deflator_thread[i], NULL, deflator, NULL) != |
| 4261 | 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4262 | BAD_ERROR("Failed to create thread\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4263 | if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator, |
| 4264 | NULL) != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4265 | BAD_ERROR("Failed to create thread\n"); |
| 4266 | } |
| 4267 | |
| 4268 | printf("Parallel mksquashfs: Using %d processor%s\n", processors, |
| 4269 | processors == 1 ? "" : "s"); |
| 4270 | |
| 4271 | if(sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) |
| 4272 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4273 | } |
| 4274 | |
| 4275 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4276 | long long write_inode_lookup_table() |
| 4277 | { |
| 4278 | int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4279 | void *it; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4280 | |
| 4281 | if(inode_count == sinode_count) |
| 4282 | goto skip_inode_hash_table; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4283 | |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4284 | it = realloc(inode_lookup_table, lookup_bytes); |
| 4285 | if(it == NULL) |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4286 | BAD_ERROR("Out of memory in write_inode_table\n"); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4287 | inode_lookup_table = it; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4288 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4289 | for(i = 0; i < INODE_HASH_SIZE; i ++) { |
| 4290 | struct inode_info *inode = inode_info[i]; |
| 4291 | |
| 4292 | for(inode = inode_info[i]; inode; inode = inode->next) { |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4293 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4294 | inode_number = get_inode_no(inode); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4295 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4296 | SQUASHFS_SWAP_LONG_LONGS(&inode->inode, |
| 4297 | &inode_lookup_table[inode_number - 1], 1); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4298 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4299 | } |
| 4300 | } |
| 4301 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4302 | skip_inode_hash_table: |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 4303 | return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL, |
| 4304 | noI); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4307 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4308 | char *get_component(char *target, char *targname) |
| 4309 | { |
| 4310 | while(*target == '/') |
rlougher | c4ebcf5 | 2007-11-08 17:52:49 +0000 | [diff] [blame] | 4311 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4312 | |
| 4313 | while(*target != '/' && *target!= '\0') |
| 4314 | *targname ++ = *target ++; |
| 4315 | |
| 4316 | *targname = '\0'; |
| 4317 | |
| 4318 | return target; |
| 4319 | } |
| 4320 | |
| 4321 | |
| 4322 | void free_path(struct pathname *paths) |
| 4323 | { |
| 4324 | int i; |
| 4325 | |
| 4326 | for(i = 0; i < paths->names; i++) { |
| 4327 | if(paths->name[i].paths) |
| 4328 | free_path(paths->name[i].paths); |
| 4329 | free(paths->name[i].name); |
| 4330 | if(paths->name[i].preg) { |
| 4331 | regfree(paths->name[i].preg); |
| 4332 | free(paths->name[i].preg); |
| 4333 | } |
| 4334 | } |
| 4335 | |
| 4336 | free(paths); |
| 4337 | } |
| 4338 | |
| 4339 | |
| 4340 | struct pathname *add_path(struct pathname *paths, char *target, char *alltarget) |
| 4341 | { |
| 4342 | char targname[1024]; |
| 4343 | int i, error; |
| 4344 | |
| 4345 | target = get_component(target, targname); |
| 4346 | |
| 4347 | if(paths == NULL) { |
plougher | dec6ef1 | 2010-12-18 02:47:53 +0000 | [diff] [blame] | 4348 | paths = malloc(sizeof(struct pathname)); |
| 4349 | if(paths == NULL) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4350 | BAD_ERROR("failed to allocate paths\n"); |
| 4351 | |
| 4352 | paths->names = 0; |
| 4353 | paths->name = NULL; |
| 4354 | } |
| 4355 | |
| 4356 | for(i = 0; i < paths->names; i++) |
| 4357 | if(strcmp(paths->name[i].name, targname) == 0) |
| 4358 | break; |
| 4359 | |
| 4360 | if(i == paths->names) { |
| 4361 | /* allocate new name entry */ |
| 4362 | paths->names ++; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4363 | paths->name = realloc(paths->name, (i + 1) * |
| 4364 | sizeof(struct path_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4365 | if(paths->name == NULL) |
| 4366 | BAD_ERROR("Out of memory in add path\n"); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4367 | paths->name[i].name = strdup(targname); |
| 4368 | paths->name[i].paths = NULL; |
| 4369 | if(use_regex) { |
| 4370 | paths->name[i].preg = malloc(sizeof(regex_t)); |
plougher | 5c60eab | 2010-07-21 01:12:19 +0000 | [diff] [blame] | 4371 | if(paths->name[i].preg == NULL) |
| 4372 | BAD_ERROR("Out of memory in add_path\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4373 | error = regcomp(paths->name[i].preg, targname, |
| 4374 | REG_EXTENDED|REG_NOSUB); |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4375 | if(error) { |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4376 | char str[1024]; |
| 4377 | |
| 4378 | regerror(error, paths->name[i].preg, str, 1024); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4379 | BAD_ERROR("invalid regex %s in export %s, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4380 | "because %s\n", targname, alltarget, |
| 4381 | str); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4382 | } |
| 4383 | } else |
| 4384 | paths->name[i].preg = NULL; |
| 4385 | |
| 4386 | if(target[0] == '\0') |
| 4387 | /* at leaf pathname component */ |
| 4388 | paths->name[i].paths = NULL; |
| 4389 | else |
| 4390 | /* recurse adding child components */ |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4391 | paths->name[i].paths = add_path(NULL, target, |
| 4392 | alltarget); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4393 | } else { |
| 4394 | /* existing matching entry */ |
| 4395 | if(paths->name[i].paths == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4396 | /* No sub-directory which means this is the leaf |
| 4397 | * component of a pre-existing exclude which subsumes |
| 4398 | * the exclude currently being added, in which case stop |
| 4399 | * adding components */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4400 | } else if(target[0] == '\0') { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4401 | /* at leaf pathname component and child components exist |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4402 | * from more specific excludes, delete as they're |
| 4403 | * subsumed by this exclude */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4404 | free_path(paths->name[i].paths); |
| 4405 | paths->name[i].paths = NULL; |
| 4406 | } else |
| 4407 | /* recurse adding child components */ |
| 4408 | add_path(paths->name[i].paths, target, alltarget); |
| 4409 | } |
| 4410 | |
| 4411 | return paths; |
| 4412 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4413 | |
| 4414 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4415 | void add_exclude(char *target) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4416 | { |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4417 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4418 | if(target[0] == '/' || strncmp(target, "./", 2) == 0 || |
| 4419 | strncmp(target, "../", 3) == 0) |
| 4420 | BAD_ERROR("/, ./ and ../ prefixed excludes not supported with " |
| 4421 | "-wildcards or -regex options\n"); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4422 | else if(strncmp(target, "... ", 4) == 0) |
| 4423 | stickypath = add_path(stickypath, target + 4, target + 4); |
| 4424 | else |
| 4425 | path = add_path(path, target, target); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | |
| 4429 | void display_path(int depth, struct pathname *paths) |
| 4430 | { |
| 4431 | int i, n; |
| 4432 | |
| 4433 | if(paths == NULL) |
| 4434 | return; |
| 4435 | |
| 4436 | for(i = 0; i < paths->names; i++) { |
| 4437 | for(n = 0; n < depth; n++) |
| 4438 | printf("\t"); |
| 4439 | printf("%d: %s\n", depth, paths->name[i].name); |
| 4440 | display_path(depth + 1, paths->name[i].paths); |
| 4441 | } |
| 4442 | } |
| 4443 | |
| 4444 | |
| 4445 | void display_path2(struct pathname *paths, char *string) |
| 4446 | { |
| 4447 | int i; |
| 4448 | char path[1024]; |
| 4449 | |
| 4450 | if(paths == NULL) { |
| 4451 | printf("%s\n", string); |
| 4452 | return; |
| 4453 | } |
| 4454 | |
| 4455 | for(i = 0; i < paths->names; i++) { |
| 4456 | strcat(strcat(strcpy(path, string), "/"), paths->name[i].name); |
| 4457 | display_path2(paths->name[i].paths, path); |
| 4458 | } |
| 4459 | } |
| 4460 | |
| 4461 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4462 | struct pathnames *init_subdir() |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4463 | { |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 4464 | struct pathnames *new = malloc(sizeof(struct pathnames)); |
plougher | d86ee82 | 2010-07-21 01:14:26 +0000 | [diff] [blame] | 4465 | if(new == NULL) |
| 4466 | BAD_ERROR("Out of memory in init_subdir\n"); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4467 | new->count = 0; |
| 4468 | return new; |
| 4469 | } |
| 4470 | |
| 4471 | |
| 4472 | struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path) |
| 4473 | { |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4474 | if(paths->count % PATHS_ALLOC_SIZE == 0) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4475 | paths = realloc(paths, sizeof(struct pathnames *) + |
| 4476 | (paths->count + PATHS_ALLOC_SIZE) * |
| 4477 | sizeof(struct pathname *)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4478 | if(paths == NULL) |
| 4479 | BAD_ERROR("Out of memory in add_subdir\n"); |
| 4480 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4481 | |
| 4482 | paths->path[paths->count++] = path; |
| 4483 | return paths; |
| 4484 | } |
| 4485 | |
| 4486 | |
| 4487 | void free_subdir(struct pathnames *paths) |
| 4488 | { |
| 4489 | free(paths); |
| 4490 | } |
| 4491 | |
| 4492 | |
| 4493 | int excluded(struct pathnames *paths, char *name, struct pathnames **new) |
| 4494 | { |
| 4495 | int i, n, res; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4496 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4497 | if(paths == NULL) { |
| 4498 | *new = NULL; |
| 4499 | return FALSE; |
| 4500 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4501 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4502 | |
| 4503 | *new = init_subdir(); |
plougher | 806581a | 2007-10-23 15:41:30 +0000 | [diff] [blame] | 4504 | if(stickypath) |
| 4505 | *new = add_subdir(*new, stickypath); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4506 | |
| 4507 | for(n = 0; n < paths->count; n++) { |
| 4508 | struct pathname *path = paths->path[n]; |
| 4509 | |
| 4510 | for(i = 0; i < path->names; i++) { |
| 4511 | int match = use_regex ? |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4512 | regexec(path->name[i].preg, name, (size_t) 0, |
| 4513 | NULL, 0) == 0 : |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4514 | fnmatch(path->name[i].name, name, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4515 | FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == |
| 4516 | 0; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4517 | |
| 4518 | if(match && path->name[i].paths == NULL) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4519 | /* match on a leaf component, any subdirectories |
| 4520 | * in the filesystem should be excluded */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4521 | res = TRUE; |
| 4522 | goto empty_set; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4525 | if(match) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4526 | /* match on a non-leaf component, add any |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4527 | * subdirectories to the new set of |
| 4528 | * subdirectories to scan for this name */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4529 | *new = add_subdir(*new, path->name[i].paths); |
| 4530 | } |
| 4531 | } |
| 4532 | |
| 4533 | if((*new)->count == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4534 | /* no matching names found, return empty new search set |
| 4535 | */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4536 | res = FALSE; |
| 4537 | goto empty_set; |
| 4538 | } |
| 4539 | |
| 4540 | /* one or more matches with sub-directories found (no leaf matches). |
| 4541 | * Return new set */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4542 | return FALSE; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4543 | |
| 4544 | empty_set: |
| 4545 | free_subdir(*new); |
| 4546 | *new = NULL; |
| 4547 | return res; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4551 | #define RECOVER_ID "Squashfs recovery file v1.0\n" |
| 4552 | #define RECOVER_ID_SIZE 28 |
| 4553 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4554 | void write_recovery_data(struct squashfs_super_block *sBlk) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4555 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4556 | int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4557 | pid_t pid = getpid(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4558 | char *metadata; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4559 | char header[] = RECOVER_ID; |
| 4560 | |
| 4561 | if(recover == FALSE) { |
| 4562 | printf("No recovery data option specified.\n"); |
plougher | eac1853 | 2007-10-29 05:26:06 +0000 | [diff] [blame] | 4563 | printf("Skipping saving recovery file.\n\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4564 | return; |
| 4565 | } |
| 4566 | |
plougher | 1b879bc | 2010-12-18 02:49:42 +0000 | [diff] [blame] | 4567 | metadata = malloc(bytes); |
| 4568 | if(metadata == NULL) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4569 | BAD_ERROR("Failed to alloc metadata buffer in " |
| 4570 | "write_recovery_data\n"); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4571 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4572 | res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata); |
| 4573 | if(res == 0) |
| 4574 | EXIT_MKSQUASHFS(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4575 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4576 | sprintf(recovery_file, "squashfs_recovery_%s_%d", |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4577 | getbase(destination_file), pid); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4578 | recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU); |
| 4579 | if(recoverfd == -1) |
| 4580 | BAD_ERROR("Failed to create recovery file, because %s. " |
| 4581 | "Aborting\n", strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4582 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4583 | if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4584 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4585 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4586 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4587 | if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4588 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4589 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4590 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4591 | if(write_bytes(recoverfd, metadata, bytes) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4592 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4593 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4594 | |
| 4595 | close(recoverfd); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4596 | free(metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4597 | |
| 4598 | printf("Recovery file \"%s\" written\n", recovery_file); |
| 4599 | printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4600 | printf("mksquashfs dummy %s -recover %s\n", destination_file, |
| 4601 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4602 | printf("to restore filesystem\n\n"); |
| 4603 | } |
| 4604 | |
| 4605 | |
| 4606 | void read_recovery_data(char *recovery_file, char *destination_file) |
| 4607 | { |
| 4608 | int fd, recoverfd, bytes; |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4609 | struct squashfs_super_block orig_sBlk, sBlk; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4610 | char *metadata; |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4611 | int res; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4612 | struct stat buf; |
| 4613 | char header[] = RECOVER_ID; |
| 4614 | char header2[RECOVER_ID_SIZE]; |
| 4615 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4616 | recoverfd = open(recovery_file, O_RDONLY); |
| 4617 | if(recoverfd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4618 | BAD_ERROR("Failed to open recovery file because %s\n", |
| 4619 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4620 | |
| 4621 | if(stat(destination_file, &buf) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4622 | BAD_ERROR("Failed to stat destination file, because %s\n", |
| 4623 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4624 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4625 | fd = open(destination_file, O_RDWR); |
| 4626 | if(fd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4627 | BAD_ERROR("Failed to open destination file because %s\n", |
| 4628 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4629 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4630 | res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE); |
| 4631 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4632 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4633 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4634 | if(res < RECOVER_ID_SIZE) |
| 4635 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4636 | if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 ) |
| 4637 | BAD_ERROR("Not a recovery file\n"); |
| 4638 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4639 | res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4640 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4641 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4642 | strerror(errno)); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4643 | if(res < sizeof(struct squashfs_super_block)) |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4644 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4645 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4646 | res = read_fs_bytes(fd, 0, sizeof(struct squashfs_super_block), &orig_sBlk); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4647 | if(res == 0) |
| 4648 | EXIT_MKSQUASHFS(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4649 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4650 | if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4, |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4651 | sizeof(struct squashfs_super_block) - 4) != 0) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4652 | BAD_ERROR("Recovery file and destination file do not seem to " |
| 4653 | "match\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4654 | |
| 4655 | bytes = sBlk.bytes_used - sBlk.inode_table_start; |
| 4656 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4657 | metadata = malloc(bytes); |
| 4658 | if(metadata == NULL) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4659 | BAD_ERROR("Failed to alloc metadata buffer in " |
| 4660 | "read_recovery_data\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4661 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4662 | res = read_bytes(recoverfd, metadata, bytes); |
| 4663 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4664 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4665 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4666 | if(res < bytes) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4667 | BAD_ERROR("Recovery file appears to be truncated\n"); |
| 4668 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4669 | write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4670 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 4671 | write_destination(fd, sBlk.inode_table_start, bytes, metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4672 | |
| 4673 | close(recoverfd); |
| 4674 | close(fd); |
| 4675 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4676 | printf("Successfully wrote recovery file \"%s\". Exiting\n", |
| 4677 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4678 | |
| 4679 | exit(0); |
| 4680 | } |
| 4681 | |
| 4682 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4683 | #define VERSION() \ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 4684 | printf("mksquashfs version 4.2-CVS (2012/10/18)\n");\ |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 4685 | printf("copyright (C) 2012 Phillip Lougher "\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4686 | "<phillip@lougher.demon.co.uk>\n\n"); \ |
| 4687 | printf("This program is free software; you can redistribute it and/or"\ |
| 4688 | "\n");\ |
| 4689 | printf("modify it under the terms of the GNU General Public License"\ |
| 4690 | "\n");\ |
| 4691 | printf("as published by the Free Software Foundation; either version "\ |
| 4692 | "2,\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4693 | printf("or (at your option) any later version.\n\n");\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4694 | printf("This program is distributed in the hope that it will be "\ |
| 4695 | "useful,\n");\ |
| 4696 | printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\ |
| 4697 | "of\n");\ |
| 4698 | printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\ |
| 4699 | "\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4700 | printf("GNU General Public License for more details.\n"); |
| 4701 | int main(int argc, char *argv[]) |
| 4702 | { |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4703 | struct stat buf, source_buf; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 4704 | int res, i; |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4705 | struct squashfs_super_block sBlk; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4706 | char *b, *root_name = NULL; |
plougher | 6c65b03 | 2008-08-07 09:13:24 +0000 | [diff] [blame] | 4707 | int nopad = FALSE, keep_as_directory = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4708 | squashfs_inode inode; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4709 | int readb_mbytes = READER_BUFFER_DEFAULT, |
| 4710 | writeb_mbytes = WRITER_BUFFER_DEFAULT, |
| 4711 | fragmentb_mbytes = FRAGMENT_BUFFER_DEFAULT; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4712 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4713 | block_log = slog(block_size); |
| 4714 | if(argc > 1 && strcmp(argv[1], "-version") == 0) { |
| 4715 | VERSION(); |
| 4716 | exit(0); |
| 4717 | } |
| 4718 | for(i = 1; i < argc && argv[i][0] != '-'; i++); |
| 4719 | if(i < 3) |
| 4720 | goto printOptions; |
| 4721 | source_path = argv + 1; |
| 4722 | source = i - 2; |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 4723 | /* |
| 4724 | * lookup default compressor. Note the Makefile ensures the default |
| 4725 | * compressor has been built, and so we don't need to to check |
| 4726 | * for failure here |
| 4727 | */ |
plougher | 5c2adaf | 2010-11-22 01:00:15 +0000 | [diff] [blame] | 4728 | comp = lookup_compressor(COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4729 | for(; i < argc; i++) { |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 4730 | if(strcmp(argv[i], "-action") == 0) { |
| 4731 | if(++i == argc) { |
| 4732 | ERROR("%s: -action missing action\n", |
| 4733 | argv[0]); |
| 4734 | exit(1); |
| 4735 | } |
| 4736 | res = parse_action(argv[i]); |
| 4737 | if(res == 0) |
| 4738 | exit(1); |
| 4739 | |
| 4740 | } else if(strcmp(argv[i], "-comp") == 0) { |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 4741 | if(compressor_opts_parsed) { |
| 4742 | ERROR("%s: -comp must appear before -X options" |
| 4743 | "\n", argv[0]); |
| 4744 | exit(1); |
| 4745 | } |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 4746 | if(++i == argc) { |
| 4747 | ERROR("%s: -comp missing compression type\n", |
| 4748 | argv[0]); |
| 4749 | exit(1); |
| 4750 | } |
plougher | 5c2adaf | 2010-11-22 01:00:15 +0000 | [diff] [blame] | 4751 | comp = lookup_compressor(argv[i]); |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 4752 | if(!comp->supported) { |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 4753 | ERROR("%s: Compressor \"%s\" is not supported!" |
| 4754 | "\n", argv[0], argv[i]); |
| 4755 | ERROR("%s: Compressors available:\n", argv[0]); |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 4756 | display_compressors("", COMP_DEFAULT); |
| 4757 | exit(1); |
| 4758 | } |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 4759 | |
| 4760 | } else if(strncmp(argv[i], "-X", 2) == 0) { |
| 4761 | int args = compressor_options(comp, argv + i, argc - i); |
plougher | d886567 | 2010-11-27 05:05:49 +0000 | [diff] [blame] | 4762 | if(args < 0) { |
| 4763 | if(args == -1) { |
| 4764 | ERROR("%s: Unrecognised compressor" |
| 4765 | " option %s\n", argv[0], |
| 4766 | argv[i]); |
| 4767 | ERROR("%s: Did you forget to specify" |
| 4768 | " -comp, or specify it after" |
| 4769 | " the compressor specific" |
| 4770 | " option?\n", argv[0]); |
| 4771 | } |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 4772 | exit(1); |
| 4773 | } |
| 4774 | i += args; |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 4775 | compressor_opts_parsed = 1; |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 4776 | |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 4777 | } else if(strcmp(argv[i], "-pf") == 0) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4778 | if(++i == argc) { |
| 4779 | ERROR("%s: -pf missing filename\n", argv[0]); |
| 4780 | exit(1); |
| 4781 | } |
plougher | 620b717 | 2009-09-10 04:17:48 +0000 | [diff] [blame] | 4782 | if(read_pseudo_file(&pseudo, argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4783 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4784 | } else if(strcmp(argv[i], "-p") == 0) { |
| 4785 | if(++i == argc) { |
| 4786 | ERROR("%s: -p missing pseudo file definition\n", |
| 4787 | argv[0]); |
| 4788 | exit(1); |
| 4789 | } |
plougher | 620b717 | 2009-09-10 04:17:48 +0000 | [diff] [blame] | 4790 | if(read_pseudo_def(&pseudo, argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4791 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4792 | } else if(strcmp(argv[i], "-recover") == 0) { |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4793 | if(++i == argc) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4794 | ERROR("%s: -recover missing recovery file\n", |
| 4795 | argv[0]); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4796 | exit(1); |
| 4797 | } |
| 4798 | read_recovery_data(argv[i], argv[source + 1]); |
| 4799 | } else if(strcmp(argv[i], "-no-recovery") == 0) |
| 4800 | recover = FALSE; |
| 4801 | else if(strcmp(argv[i], "-wildcards") == 0) { |
plougher | 934a9ed | 2007-10-19 00:21:10 +0000 | [diff] [blame] | 4802 | old_exclude = FALSE; |
| 4803 | use_regex = FALSE; |
| 4804 | } else if(strcmp(argv[i], "-regex") == 0) { |
| 4805 | old_exclude = FALSE; |
| 4806 | use_regex = TRUE; |
| 4807 | } else if(strcmp(argv[i], "-no-sparse") == 0) |
plougher | 1f54edc | 2007-08-12 23:13:36 +0000 | [diff] [blame] | 4808 | sparse_files = FALSE; |
| 4809 | else if(strcmp(argv[i], "-no-progress") == 0) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4810 | progress = FALSE; |
| 4811 | else if(strcmp(argv[i], "-no-exports") == 0) |
| 4812 | exportable = FALSE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4813 | else if(strcmp(argv[i], "-processors") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4814 | if((++i == argc) || (processors = |
| 4815 | strtol(argv[i], &b, 10), *b != '\0')) { |
| 4816 | ERROR("%s: -processors missing or invalid " |
| 4817 | "processor number\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4818 | exit(1); |
| 4819 | } |
| 4820 | if(processors < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4821 | ERROR("%s: -processors should be 1 or larger\n", |
| 4822 | argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4823 | exit(1); |
| 4824 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4825 | } else if(strcmp(argv[i], "-read-queue") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4826 | if((++i == argc) || (readb_mbytes = |
| 4827 | strtol(argv[i], &b, 10), *b != '\0')) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4828 | ERROR("%s: -read-queue missing or invalid " |
| 4829 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4830 | exit(1); |
| 4831 | } |
| 4832 | if(readb_mbytes < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4833 | ERROR("%s: -read-queue should be 1 megabyte or " |
| 4834 | "larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4835 | exit(1); |
| 4836 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4837 | } else if(strcmp(argv[i], "-write-queue") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4838 | if((++i == argc) || (writeb_mbytes = |
| 4839 | strtol(argv[i], &b, 10), *b != '\0')) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4840 | ERROR("%s: -write-queue missing or invalid " |
| 4841 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4842 | exit(1); |
| 4843 | } |
| 4844 | if(writeb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4845 | ERROR("%s: -write-queue should be 1 megabyte " |
| 4846 | "or larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4847 | exit(1); |
| 4848 | } |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4849 | } else if(strcmp(argv[i], "-fragment-queue") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4850 | if((++i == argc) || |
| 4851 | (fragmentb_mbytes = |
| 4852 | strtol(argv[i], &b, 10), *b != '\0')) { |
| 4853 | ERROR("%s: -fragment-queue missing or invalid " |
| 4854 | "queue size\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4855 | exit(1); |
| 4856 | } |
| 4857 | if(fragmentb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4858 | ERROR("%s: -fragment-queue should be 1 " |
| 4859 | "megabyte or larger\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4860 | exit(1); |
| 4861 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4862 | } else if(strcmp(argv[i], "-b") == 0) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4863 | if(++i == argc) { |
| 4864 | ERROR("%s: -b missing block size\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4865 | exit(1); |
| 4866 | } |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4867 | block_size = strtol(argv[i], &b, 10); |
| 4868 | if(*b == 'm' || *b == 'M') |
| 4869 | block_size *= 1048576; |
| 4870 | else if(*b == 'k' || *b == 'K') |
| 4871 | block_size *= 1024; |
| 4872 | else if(*b != '\0') { |
| 4873 | ERROR("%s: -b invalid block size\n", argv[0]); |
| 4874 | exit(1); |
| 4875 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4876 | if((block_log = slog(block_size)) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4877 | ERROR("%s: -b block size not power of two or " |
| 4878 | "not between 4096 and 1Mbyte\n", |
| 4879 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4880 | exit(1); |
| 4881 | } |
| 4882 | } else if(strcmp(argv[i], "-ef") == 0) { |
| 4883 | if(++i == argc) { |
| 4884 | ERROR("%s: -ef missing filename\n", argv[0]); |
| 4885 | exit(1); |
| 4886 | } |
plougher | 9b5bf8c | 2006-03-20 18:43:33 +0000 | [diff] [blame] | 4887 | } else if(strcmp(argv[i], "-no-duplicates") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4888 | duplicate_checking = FALSE; |
| 4889 | |
| 4890 | else if(strcmp(argv[i], "-no-fragments") == 0) |
| 4891 | no_fragments = TRUE; |
| 4892 | |
| 4893 | else if(strcmp(argv[i], "-always-use-fragments") == 0) |
| 4894 | always_use_fragments = TRUE; |
| 4895 | |
| 4896 | else if(strcmp(argv[i], "-sort") == 0) { |
| 4897 | if(++i == argc) { |
| 4898 | ERROR("%s: -sort missing filename\n", argv[0]); |
| 4899 | exit(1); |
| 4900 | } |
| 4901 | } else if(strcmp(argv[i], "-all-root") == 0 || |
| 4902 | strcmp(argv[i], "-root-owned") == 0) |
| 4903 | global_uid = global_gid = 0; |
| 4904 | |
| 4905 | else if(strcmp(argv[i], "-force-uid") == 0) { |
| 4906 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4907 | ERROR("%s: -force-uid missing uid or user\n", |
| 4908 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4909 | exit(1); |
| 4910 | } |
| 4911 | if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4912 | if(global_uid < 0 || global_uid > |
| 4913 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4914 | ERROR("%s: -force-uid uid out of range" |
| 4915 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4916 | exit(1); |
| 4917 | } |
| 4918 | } else { |
| 4919 | struct passwd *uid = getpwnam(argv[i]); |
| 4920 | if(uid) |
| 4921 | global_uid = uid->pw_uid; |
| 4922 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4923 | ERROR("%s: -force-uid invalid uid or " |
| 4924 | "unknown user\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4925 | exit(1); |
| 4926 | } |
| 4927 | } |
| 4928 | } else if(strcmp(argv[i], "-force-gid") == 0) { |
| 4929 | if(++i == argc) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4930 | ERROR("%s: -force-gid missing gid or group\n", |
| 4931 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4932 | exit(1); |
| 4933 | } |
| 4934 | if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4935 | if(global_gid < 0 || global_gid > |
| 4936 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4937 | ERROR("%s: -force-gid gid out of range" |
| 4938 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4939 | exit(1); |
| 4940 | } |
| 4941 | } else { |
| 4942 | struct group *gid = getgrnam(argv[i]); |
| 4943 | if(gid) |
| 4944 | global_gid = gid->gr_gid; |
| 4945 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4946 | ERROR("%s: -force-gid invalid gid or " |
| 4947 | "unknown group\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4948 | exit(1); |
| 4949 | } |
| 4950 | } |
| 4951 | } else if(strcmp(argv[i], "-noI") == 0 || |
| 4952 | strcmp(argv[i], "-noInodeCompression") == 0) |
| 4953 | noI = TRUE; |
| 4954 | |
| 4955 | else if(strcmp(argv[i], "-noD") == 0 || |
| 4956 | strcmp(argv[i], "-noDataCompression") == 0) |
| 4957 | noD = TRUE; |
| 4958 | |
| 4959 | else if(strcmp(argv[i], "-noF") == 0 || |
| 4960 | strcmp(argv[i], "-noFragmentCompression") == 0) |
| 4961 | noF = TRUE; |
| 4962 | |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 4963 | else if(strcmp(argv[i], "-noX") == 0 || |
| 4964 | strcmp(argv[i], "-noXattrCompression") == 0) |
| 4965 | noX = TRUE; |
| 4966 | |
plougher | ce564c6 | 2010-05-19 03:01:49 +0000 | [diff] [blame] | 4967 | else if(strcmp(argv[i], "-no-xattrs") == 0) |
| 4968 | no_xattrs = TRUE; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 4969 | |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 4970 | else if(strcmp(argv[i], "-xattrs") == 0) |
| 4971 | no_xattrs = FALSE; |
| 4972 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4973 | else if(strcmp(argv[i], "-nopad") == 0) |
| 4974 | nopad = TRUE; |
| 4975 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4976 | else if(strcmp(argv[i], "-info") == 0) { |
| 4977 | silent = FALSE; |
| 4978 | progress = FALSE; |
| 4979 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4980 | |
| 4981 | else if(strcmp(argv[i], "-e") == 0) |
| 4982 | break; |
| 4983 | |
| 4984 | else if(strcmp(argv[i], "-noappend") == 0) |
| 4985 | delete = TRUE; |
| 4986 | |
| 4987 | else if(strcmp(argv[i], "-keep-as-directory") == 0) |
| 4988 | keep_as_directory = TRUE; |
| 4989 | |
| 4990 | else if(strcmp(argv[i], "-root-becomes") == 0) { |
| 4991 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4992 | ERROR("%s: -root-becomes: missing name\n", |
| 4993 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4994 | exit(1); |
| 4995 | } |
| 4996 | root_name = argv[i]; |
| 4997 | } else if(strcmp(argv[i], "-version") == 0) { |
| 4998 | VERSION(); |
| 4999 | } else { |
| 5000 | ERROR("%s: invalid option\n\n", argv[0]); |
| 5001 | printOptions: |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5002 | ERROR("SYNTAX:%s source1 source2 ... dest [options] " |
| 5003 | "[-e list of exclude\ndirs/files]\n", argv[0]); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5004 | ERROR("\nFilesystem build options:\n"); |
plougher | ff5ea8b | 2009-08-07 19:33:10 +0000 | [diff] [blame] | 5005 | ERROR("-comp <comp>\t\tselect <comp> compression\n"); |
plougher | 13df178 | 2009-08-29 01:05:34 +0000 | [diff] [blame] | 5006 | ERROR("\t\t\tCompressors available:\n"); |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 5007 | display_compressors("\t\t\t", COMP_DEFAULT); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5008 | ERROR("-b <block_size>\t\tset data block to " |
| 5009 | "<block_size>. Default %d bytes\n", |
| 5010 | SQUASHFS_FILE_SIZE); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5011 | ERROR("-no-exports\t\tdon't make the filesystem " |
| 5012 | "exportable via NFS\n"); |
| 5013 | ERROR("-no-sparse\t\tdon't detect sparse files\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5014 | ERROR("-no-xattrs\t\tdon't store extended attributes" |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5015 | NOXOPT_STR "\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5016 | ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5017 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5018 | ERROR("-noI\t\t\tdo not compress inode table\n"); |
| 5019 | ERROR("-noD\t\t\tdo not compress data blocks\n"); |
| 5020 | ERROR("-noF\t\t\tdo not compress fragment blocks\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5021 | ERROR("-noX\t\t\tdo not compress extended " |
| 5022 | "attributes\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5023 | ERROR("-no-fragments\t\tdo not use fragments\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5024 | ERROR("-always-use-fragments\tuse fragment blocks for " |
| 5025 | "files larger than block size\n"); |
| 5026 | ERROR("-no-duplicates\t\tdo not perform duplicate " |
| 5027 | "checking\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5028 | ERROR("-all-root\t\tmake all files owned by root\n"); |
| 5029 | ERROR("-force-uid uid\t\tset all file uids to uid\n"); |
| 5030 | ERROR("-force-gid gid\t\tset all file gids to gid\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5031 | ERROR("-nopad\t\t\tdo not pad filesystem to a multiple " |
| 5032 | "of 4K\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5033 | ERROR("-keep-as-directory\tif one source directory is " |
| 5034 | "specified, create a root\n"); |
| 5035 | ERROR("\t\t\tdirectory containing that directory, " |
| 5036 | "rather than the\n"); |
| 5037 | ERROR("\t\t\tcontents of the directory\n"); |
| 5038 | ERROR("\nFilesystem filter options:\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5039 | ERROR("-p <pseudo-definition>\tAdd pseudo file " |
| 5040 | "definition\n"); |
| 5041 | ERROR("-pf <pseudo-file>\tAdd list of pseudo file " |
| 5042 | "definitions\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5043 | ERROR("-sort <sort_file>\tsort files according to " |
| 5044 | "priorities in <sort_file>. One\n"); |
| 5045 | ERROR("\t\t\tfile or dir with priority per line. " |
| 5046 | "Priority -32768 to\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5047 | ERROR("\t\t\t32767, default priority 0\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5048 | ERROR("-ef <exclude_file>\tlist of exclude dirs/files." |
| 5049 | " One per line\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5050 | ERROR("-wildcards\t\tAllow extended shell wildcards " |
| 5051 | "(globbing) to be used in\n\t\t\texclude " |
| 5052 | "dirs/files\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5053 | ERROR("-regex\t\t\tAllow POSIX regular expressions to " |
| 5054 | "be used in exclude\n\t\t\tdirs/files\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5055 | ERROR("\nFilesystem append options:\n"); |
| 5056 | ERROR("-noappend\t\tdo not append to existing " |
| 5057 | "filesystem\n"); |
| 5058 | ERROR("-root-becomes <name>\twhen appending source " |
| 5059 | "files/directories, make the\n"); |
| 5060 | ERROR("\t\t\toriginal root become a subdirectory in " |
| 5061 | "the new root\n"); |
| 5062 | ERROR("\t\t\tcalled <name>, rather than adding the new " |
| 5063 | "source items\n"); |
| 5064 | ERROR("\t\t\tto the original root\n"); |
| 5065 | ERROR("\nMksquashfs runtime options:\n"); |
| 5066 | ERROR("-version\t\tprint version, licence and " |
| 5067 | "copyright message\n"); |
| 5068 | ERROR("-recover <name>\t\trecover filesystem data " |
| 5069 | "using recovery file <name>\n"); |
| 5070 | ERROR("-no-recovery\t\tdon't generate a recovery " |
| 5071 | "file\n"); |
| 5072 | ERROR("-info\t\t\tprint files written to filesystem\n"); |
| 5073 | ERROR("-no-progress\t\tdon't display the progress " |
| 5074 | "bar\n"); |
| 5075 | ERROR("-processors <number>\tUse <number> processors." |
| 5076 | " By default will use number of\n"); |
| 5077 | ERROR("\t\t\tprocessors available\n"); |
| 5078 | ERROR("-read-queue <size>\tSet input queue to <size> " |
| 5079 | "Mbytes. Default %d Mbytes\n", |
| 5080 | READER_BUFFER_DEFAULT); |
| 5081 | ERROR("-write-queue <size>\tSet output queue to <size> " |
| 5082 | "Mbytes. Default %d Mbytes\n", |
| 5083 | WRITER_BUFFER_DEFAULT); |
plougher | 8bc376b | 2010-11-12 04:55:20 +0000 | [diff] [blame] | 5084 | ERROR("-fragment-queue <size>\tSet fragment queue to " |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5085 | "<size> Mbytes. Default %d Mbytes\n", |
| 5086 | FRAGMENT_BUFFER_DEFAULT); |
| 5087 | ERROR("\nMiscellaneous options:\n"); |
| 5088 | ERROR("-root-owned\t\talternative name for -all-root" |
| 5089 | "\n"); |
| 5090 | ERROR("-noInodeCompression\talternative name for -noI" |
| 5091 | "\n"); |
| 5092 | ERROR("-noDataCompression\talternative name for -noD" |
| 5093 | "\n"); |
| 5094 | ERROR("-noFragmentCompression\talternative name for " |
| 5095 | "-noF\n"); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5096 | ERROR("-noXattrCompression\talternative name for " |
| 5097 | "-noX\n"); |
plougher | 4fb6682 | 2010-12-08 02:49:28 +0000 | [diff] [blame] | 5098 | ERROR("\nCompressors available and compressor specific " |
| 5099 | "options:\n"); |
| 5100 | display_compressor_usage(COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5101 | exit(1); |
| 5102 | } |
| 5103 | } |
| 5104 | |
plougher | b747f4f | 2010-12-25 04:05:47 +0000 | [diff] [blame] | 5105 | /* |
| 5106 | * Some compressors may need the options to be checked for validity |
| 5107 | * once all the options have been processed |
| 5108 | */ |
| 5109 | res = compressor_options_post(comp, block_size); |
| 5110 | if(res) |
| 5111 | EXIT_MKSQUASHFS(); |
| 5112 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5113 | for(i = 0; i < source; i++) |
| 5114 | if(lstat(source_path[i], &source_buf) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5115 | fprintf(stderr, "Cannot stat source directory \"%s\" " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5116 | "because %s\n", source_path[i], |
| 5117 | strerror(errno)); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5118 | EXIT_MKSQUASHFS(); |
| 5119 | } |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5120 | |
| 5121 | destination_file = argv[source + 1]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5122 | if(stat(argv[source + 1], &buf) == -1) { |
| 5123 | if(errno == ENOENT) { /* Does not exist */ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5124 | fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, |
plougher | 59dce67 | 2010-05-19 03:56:59 +0000 | [diff] [blame] | 5125 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5126 | if(fd == -1) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5127 | perror("Could not create destination file"); |
| 5128 | exit(1); |
| 5129 | } |
| 5130 | delete = TRUE; |
| 5131 | } else { |
| 5132 | perror("Could not stat destination file"); |
| 5133 | exit(1); |
| 5134 | } |
| 5135 | |
| 5136 | } else { |
| 5137 | if(S_ISBLK(buf.st_mode)) { |
| 5138 | if((fd = open(argv[source + 1], O_RDWR)) == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5139 | perror("Could not open block device as " |
| 5140 | "destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5141 | exit(1); |
| 5142 | } |
| 5143 | block_device = 1; |
| 5144 | |
| 5145 | } else if(S_ISREG(buf.st_mode)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5146 | fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | |
| 5147 | O_RDWR); |
| 5148 | if(fd == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5149 | perror("Could not open regular file for " |
| 5150 | "writing as destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5151 | exit(1); |
| 5152 | } |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 5153 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5154 | else { |
| 5155 | ERROR("Destination not block device or regular file\n"); |
| 5156 | exit(1); |
| 5157 | } |
| 5158 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5159 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5160 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5161 | signal(SIGTERM, sighandler2); |
| 5162 | signal(SIGINT, sighandler2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5163 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5164 | /* |
| 5165 | * process the exclude files - must be done afer destination file has |
| 5166 | * been possibly created |
| 5167 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5168 | for(i = source + 2; i < argc; i++) |
| 5169 | if(strcmp(argv[i], "-ef") == 0) { |
| 5170 | FILE *fd; |
| 5171 | char filename[16385]; |
| 5172 | if((fd = fopen(argv[++i], "r")) == NULL) { |
| 5173 | perror("Could not open exclude file..."); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5174 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5175 | } |
| 5176 | while(fscanf(fd, "%16384[^\n]\n", filename) != EOF) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 5177 | if(old_exclude) |
| 5178 | old_add_exclude(filename); |
| 5179 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5180 | add_exclude(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5181 | fclose(fd); |
| 5182 | } else if(strcmp(argv[i], "-e") == 0) |
| 5183 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5184 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5185 | strcmp(argv[i], "-sort") == 0 || |
| 5186 | strcmp(argv[i], "-pf") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5187 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5188 | i++; |
| 5189 | |
| 5190 | if(i != argc) { |
| 5191 | if(++i == argc) { |
| 5192 | ERROR("%s: -e missing arguments\n", argv[0]); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5193 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5194 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 5195 | while(i < argc) |
| 5196 | if(old_exclude) |
| 5197 | old_add_exclude(argv[i++]); |
| 5198 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5199 | add_exclude(argv[i++]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | /* process the sort files - must be done afer the exclude files */ |
| 5203 | for(i = source + 2; i < argc; i++) |
| 5204 | if(strcmp(argv[i], "-sort") == 0) { |
plougher | e1c9a74 | 2010-07-21 16:59:05 +0000 | [diff] [blame] | 5205 | int res = read_sort_file(argv[++i], source, |
| 5206 | source_path); |
| 5207 | if(res == FALSE) |
| 5208 | BAD_ERROR("Failed to read sort file\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5209 | sorted ++; |
| 5210 | } else if(strcmp(argv[i], "-e") == 0) |
| 5211 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5212 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5213 | strcmp(argv[i], "-ef") == 0 || |
| 5214 | strcmp(argv[i], "-pf") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5215 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5216 | i++; |
| 5217 | |
plougher | 62d5b5c | 2008-08-16 01:49:42 +0000 | [diff] [blame] | 5218 | #ifdef SQUASHFS_TRACE |
| 5219 | progress = FALSE; |
| 5220 | #endif |
| 5221 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5222 | if(!delete) { |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 5223 | comp = read_super(fd, &sBlk, argv[source + 1]); |
| 5224 | if(comp == NULL) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5225 | ERROR("Failed to read existing filesystem - will not " |
| 5226 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5227 | ERROR("To force Mksquashfs to write to this block " |
| 5228 | "device or file use -noappend\n"); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5229 | EXIT_MKSQUASHFS(); |
| 5230 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5231 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5232 | block_log = slog(block_size = sBlk.block_size); |
| 5233 | noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags); |
| 5234 | noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags); |
| 5235 | noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5236 | noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5237 | no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags); |
| 5238 | always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags); |
| 5239 | duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5240 | exportable = SQUASHFS_EXPORTABLE(sBlk.flags); |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5241 | no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5242 | comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 5245 | initialise_threads(readb_mbytes, writeb_mbytes, fragmentb_mbytes); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5246 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 5247 | res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0); |
| 5248 | if(res) |
| 5249 | BAD_ERROR("compressor_init failed\n"); |
| 5250 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5251 | if(delete) { |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5252 | int size; |
Phillip Lougher | a45c9d2 | 2011-02-20 04:24:07 +0000 | [diff] [blame] | 5253 | void *comp_data = compressor_dump_options(comp, block_size, |
| 5254 | &size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5255 | |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 5256 | printf("Creating %d.%d filesystem on %s, block size %d.\n", |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5257 | SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5258 | |
plougher | 871c72a | 2010-12-25 04:17:27 +0000 | [diff] [blame] | 5259 | /* |
| 5260 | * store any compressor specific options after the superblock, |
| 5261 | * and set the COMP_OPT flag to show that the filesystem has |
| 5262 | * compressor specfic options |
| 5263 | */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5264 | if(comp_data) { |
| 5265 | unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT; |
| 5266 | |
| 5267 | SQUASHFS_INSWAP_SHORTS(&c_byte, 1); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5268 | write_destination(fd, sizeof(struct squashfs_super_block), |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 5269 | sizeof(c_byte), &c_byte); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5270 | write_destination(fd, sizeof(struct squashfs_super_block) + |
plougher | 8e4dad4 | 2010-12-28 05:56:22 +0000 | [diff] [blame] | 5271 | sizeof(c_byte), size, comp_data); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5272 | bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte) |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5273 | + size; |
| 5274 | comp_opts = TRUE; |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5275 | } else |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5276 | bytes = sizeof(struct squashfs_super_block); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5277 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5278 | unsigned int last_directory_block, inode_dir_offset, |
| 5279 | inode_dir_file_size, root_inode_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5280 | inode_dir_start_block, uncompressed_data, |
| 5281 | compressed_data, inode_dir_inode_number, |
| 5282 | inode_dir_parent_inode; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5283 | unsigned int root_inode_start = |
| 5284 | SQUASHFS_INODE_BLK(sBlk.root_inode), |
| 5285 | root_inode_offset = |
| 5286 | SQUASHFS_INODE_OFFSET(sBlk.root_inode); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5287 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5288 | if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, |
| 5289 | &data_cache, &directory_table, |
| 5290 | &directory_data_cache, &last_directory_block, |
| 5291 | &inode_dir_offset, &inode_dir_file_size, |
| 5292 | &root_inode_size, &inode_dir_start_block, |
| 5293 | &file_count, &sym_count, &dev_count, &dir_count, |
| 5294 | &fifo_count, &sock_count, &total_bytes, |
| 5295 | &total_inode_bytes, &total_directory_bytes, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5296 | &inode_dir_inode_number, |
| 5297 | &inode_dir_parent_inode, add_old_root_entry, |
| 5298 | &fragment_table, &inode_lookup_table)) == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5299 | ERROR("Failed to read existing filesystem - will not " |
| 5300 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5301 | ERROR("To force Mksquashfs to write to this block " |
| 5302 | "device or file use -noappend\n"); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5303 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5304 | } |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5305 | if((fragments = sBlk.fragments)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5306 | fragment_table = realloc((char *) fragment_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5307 | ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 5308 | * sizeof(struct squashfs_fragment_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5309 | if(fragment_table == NULL) |
| 5310 | BAD_ERROR("Out of memory in save filesystem state\n"); |
| 5311 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5312 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5313 | printf("Appending to existing %d.%d filesystem on %s, block " |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5314 | "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5315 | block_size); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5316 | printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, " |
plougher | bb98803 | 2009-08-06 08:46:34 +0000 | [diff] [blame] | 5317 | "-always-use-fragments,\n-exportable and -comp options " |
| 5318 | "ignored\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5319 | printf("\nIf appending is not wanted, please re-run with " |
| 5320 | "-noappend specified!\n\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5321 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5322 | compressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5323 | ~(SQUASHFS_METADATA_SIZE - 1); |
| 5324 | uncompressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5325 | (SQUASHFS_METADATA_SIZE - 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5326 | |
| 5327 | /* save original filesystem state for restoring ... */ |
| 5328 | sfragments = fragments; |
| 5329 | sbytes = bytes; |
| 5330 | sinode_count = sBlk.inodes; |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 5331 | scache_bytes = root_inode_offset + root_inode_size; |
| 5332 | sdirectory_cache_bytes = uncompressed_data; |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5333 | sdata_cache = malloc(scache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5334 | if(sdata_cache == NULL) |
| 5335 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5336 | sdirectory_data_cache = malloc(sdirectory_cache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5337 | if(sdirectory_data_cache == NULL) |
| 5338 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5339 | memcpy(sdata_cache, data_cache, scache_bytes); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5340 | memcpy(sdirectory_data_cache, directory_data_cache + |
| 5341 | compressed_data, sdirectory_cache_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5342 | sinode_bytes = root_inode_start; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5343 | stotal_bytes = total_bytes; |
| 5344 | stotal_inode_bytes = total_inode_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5345 | stotal_directory_bytes = total_directory_bytes + |
| 5346 | compressed_data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5347 | sfile_count = file_count; |
| 5348 | ssym_count = sym_count; |
| 5349 | sdev_count = dev_count; |
| 5350 | sdir_count = dir_count + 1; |
| 5351 | sfifo_count = fifo_count; |
| 5352 | ssock_count = sock_count; |
| 5353 | sdup_files = dup_files; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5354 | sid_count = id_count; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5355 | write_recovery_data(&sBlk); |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 5356 | if(save_xattrs() == FALSE) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5357 | BAD_ERROR("Failed to save xattrs from existing " |
| 5358 | "filesystem\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5359 | restore = TRUE; |
| 5360 | if(setjmp(env)) |
| 5361 | goto restore_filesystem; |
| 5362 | signal(SIGTERM, sighandler); |
| 5363 | signal(SIGINT, sighandler); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 5364 | write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5365 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5366 | /* |
| 5367 | * set the filesystem state up to be able to append to the |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5368 | * original filesystem. The filesystem state differs depending |
| 5369 | * on whether we're appending to the original root directory, or |
| 5370 | * if the original root directory becomes a sub-directory |
| 5371 | * (root-becomes specified on command line, here root_name != |
| 5372 | * NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5373 | */ |
| 5374 | inode_bytes = inode_size = root_inode_start; |
| 5375 | directory_size = last_directory_block; |
| 5376 | cache_size = root_inode_offset + root_inode_size; |
| 5377 | directory_cache_size = inode_dir_offset + inode_dir_file_size; |
| 5378 | if(root_name) { |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5379 | sdirectory_bytes = last_directory_block; |
| 5380 | sdirectory_compressed_bytes = 0; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5381 | root_inode_number = inode_dir_parent_inode; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5382 | inode_no = sBlk.inodes + 2; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5383 | directory_bytes = last_directory_block; |
| 5384 | directory_cache_bytes = uncompressed_data; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5385 | memmove(directory_data_cache, directory_data_cache + |
| 5386 | compressed_data, uncompressed_data); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5387 | cache_bytes = root_inode_offset + root_inode_size; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5388 | add_old_root_entry(root_name, sBlk.root_inode, |
| 5389 | inode_dir_inode_number, SQUASHFS_DIR_TYPE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5390 | total_directory_bytes += compressed_data; |
| 5391 | dir_count ++; |
| 5392 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5393 | sdirectory_compressed_bytes = last_directory_block - |
| 5394 | inode_dir_start_block; |
| 5395 | sdirectory_compressed = |
| 5396 | malloc(sdirectory_compressed_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5397 | if(sdirectory_compressed == NULL) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5398 | BAD_ERROR("Out of memory in save filesystem " |
| 5399 | "state\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5400 | memcpy(sdirectory_compressed, directory_table + |
| 5401 | inode_dir_start_block, |
| 5402 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5403 | sdirectory_bytes = inode_dir_start_block; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5404 | root_inode_number = inode_dir_inode_number; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5405 | inode_no = sBlk.inodes + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5406 | directory_bytes = inode_dir_start_block; |
| 5407 | directory_cache_bytes = inode_dir_offset; |
| 5408 | cache_bytes = root_inode_offset; |
| 5409 | } |
| 5410 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5411 | inode_count = file_count + dir_count + sym_count + dev_count + |
| 5412 | fifo_count + sock_count; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 5413 | |
| 5414 | /* |
| 5415 | * The default use freelist before growing cache policy behaves |
| 5416 | * poorly with appending - with many deplicates the caches |
| 5417 | * do not grow due to the fact that large queues of outstanding |
| 5418 | * fragments/writer blocks do not occur, leading to small caches |
| 5419 | * and un-uncessary performance loss to frequent cache |
| 5420 | * replacement in the small caches. Therefore with appending |
| 5421 | * change the policy to grow the caches before reusing blocks |
| 5422 | * from the freelist |
| 5423 | */ |
| 5424 | first_freelist = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5427 | if(path || stickypath) { |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5428 | paths = init_subdir(); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5429 | if(path) |
| 5430 | paths = add_subdir(paths, path); |
| 5431 | if(stickypath) |
| 5432 | paths = add_subdir(paths, stickypath); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5435 | dump_actions(); |
| 5436 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5437 | if(delete && !keep_as_directory && source == 1 && |
| 5438 | S_ISDIR(source_buf.st_mode)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5439 | dir_scan(&inode, source_path[0], scan1_readdir); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5440 | else if(!keep_as_directory && source == 1 && |
| 5441 | S_ISDIR(source_buf.st_mode)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5442 | dir_scan(&inode, source_path[0], scan1_single_readdir); |
| 5443 | else |
| 5444 | dir_scan(&inode, "", scan1_encomp_readdir); |
| 5445 | sBlk.root_inode = inode; |
| 5446 | sBlk.inodes = inode_count; |
| 5447 | sBlk.s_magic = SQUASHFS_MAGIC; |
| 5448 | sBlk.s_major = SQUASHFS_MAJOR; |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5449 | sBlk.s_minor = SQUASHFS_MINOR; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5450 | sBlk.block_size = block_size; |
| 5451 | sBlk.block_log = block_log; |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5452 | sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments, |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5453 | always_use_fragments, duplicate_checking, exportable, |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5454 | no_xattrs, comp_opts); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5455 | sBlk.mkfs_time = time(NULL); |
| 5456 | |
| 5457 | restore_filesystem: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame^] | 5458 | if(progress) |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 5459 | disable_progress_bar(); |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 5460 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5461 | sBlk.fragments = fragments; |
plougher | 875bfef | 2010-08-12 23:48:41 +0000 | [diff] [blame] | 5462 | if(!restoring) { |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5463 | struct file_buffer **fragment = NULL; |
| 5464 | while((fragment = get_frag_action(fragment))) |
| 5465 | write_fragment(*fragment); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 5466 | unlock_fragments(); |
| 5467 | pthread_mutex_lock(&fragment_mutex); |
| 5468 | while(fragments_outstanding) { |
| 5469 | pthread_mutex_unlock(&fragment_mutex); |
| 5470 | sched_yield(); |
| 5471 | pthread_mutex_lock(&fragment_mutex); |
| 5472 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5473 | queue_put(to_writer, NULL); |
| 5474 | if(queue_get(from_writer) != 0) |
| 5475 | EXIT_MKSQUASHFS(); |
| 5476 | } |
| 5477 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 5478 | sBlk.no_ids = id_count; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5479 | sBlk.inode_table_start = write_inodes(); |
| 5480 | sBlk.directory_table_start = write_directories(); |
| 5481 | sBlk.fragment_table_start = write_fragment_table(); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5482 | sBlk.lookup_table_start = exportable ? write_inode_lookup_table() : |
| 5483 | SQUASHFS_INVALID_BLK; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 5484 | sBlk.id_table_start = write_id_table(); |
| 5485 | sBlk.xattr_id_table_start = write_xattrs(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5486 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5487 | TRACE("sBlk->inode_table_start 0x%llx\n", sBlk.inode_table_start); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5488 | TRACE("sBlk->directory_table_start 0x%llx\n", |
| 5489 | sBlk.directory_table_start); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5490 | TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk.fragment_table_start); |
| 5491 | if(exportable) |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5492 | TRACE("sBlk->lookup_table_start 0x%llx\n", |
| 5493 | sBlk.lookup_table_start); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5494 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5495 | sBlk.bytes_used = bytes; |
plougher | 9fca346 | 2008-10-27 00:34:35 +0000 | [diff] [blame] | 5496 | |
plougher | 8c4b7b9 | 2009-07-30 04:47:52 +0000 | [diff] [blame] | 5497 | sBlk.compression = comp->id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5498 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 5499 | SQUASHFS_INSWAP_SUPER_BLOCK(&sBlk); |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 5500 | write_destination(fd, SQUASHFS_START, sizeof(sBlk), &sBlk); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5501 | |
| 5502 | if(!nopad && (i = bytes & (4096 - 1))) { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 5503 | char temp[4096] = {0}; |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 5504 | write_destination(fd, bytes, 4096 - i, temp); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5507 | close(fd); |
| 5508 | |
plougher | 11e7b1b | 2009-09-11 12:10:58 +0000 | [diff] [blame] | 5509 | delete_pseudo_files(); |
| 5510 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5511 | if(recovery_file[0] != '\0') |
| 5512 | unlink(recovery_file); |
| 5513 | |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 5514 | total_bytes += total_inode_bytes + total_directory_bytes + |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5515 | sizeof(struct squashfs_super_block) + total_xattr_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5516 | |
plougher | 62542fb | 2009-08-06 08:43:08 +0000 | [diff] [blame] | 5517 | printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size" |
| 5518 | " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR, |
| 5519 | SQUASHFS_MINOR, comp->name, block_size); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5520 | printf("\t%s data, %s metadata, %s fragments, %s xattrs\n", |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 5521 | noD ? "uncompressed" : "compressed", noI ? "uncompressed" : |
| 5522 | "compressed", no_fragments ? "no" : noF ? "uncompressed" : |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5523 | "compressed", no_xattrs ? "no" : noX ? "uncompressed" : |
| 5524 | "compressed"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5525 | printf("\tduplicates are %sremoved\n", duplicate_checking ? "" : |
| 5526 | "not "); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5527 | printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, |
| 5528 | bytes / (1024.0 * 1024.0)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5529 | printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n", |
| 5530 | ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0); |
| 5531 | printf("Inode table size %d bytes (%.2f Kbytes)\n", |
| 5532 | inode_bytes, inode_bytes / 1024.0); |
| 5533 | printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n", |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5534 | ((float) inode_bytes / total_inode_bytes) * 100.0, |
| 5535 | total_inode_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5536 | printf("Directory table size %d bytes (%.2f Kbytes)\n", |
| 5537 | directory_bytes, directory_bytes / 1024.0); |
| 5538 | printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n", |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5539 | ((float) directory_bytes / total_directory_bytes) * 100.0, |
| 5540 | total_directory_bytes); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 5541 | if(total_xattr_bytes) { |
| 5542 | printf("Xattr table size %d bytes (%.2f Kbytes)\n", |
| 5543 | xattr_bytes, xattr_bytes / 1024.0); |
| 5544 | printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n", |
| 5545 | ((float) xattr_bytes / total_xattr_bytes) * 100.0, |
| 5546 | total_xattr_bytes); |
| 5547 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5548 | if(duplicate_checking) |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5549 | printf("Number of duplicate files found %d\n", file_count - |
| 5550 | dup_files); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5551 | else |
| 5552 | printf("No duplicate files removed\n"); |
| 5553 | printf("Number of inodes %d\n", inode_count); |
| 5554 | printf("Number of files %d\n", file_count); |
| 5555 | if(!no_fragments) |
| 5556 | printf("Number of fragments %d\n", fragments); |
| 5557 | printf("Number of symbolic links %d\n", sym_count); |
| 5558 | printf("Number of device nodes %d\n", dev_count); |
| 5559 | printf("Number of fifo nodes %d\n", fifo_count); |
| 5560 | printf("Number of socket nodes %d\n", sock_count); |
| 5561 | printf("Number of directories %d\n", dir_count); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5562 | printf("Number of ids (unique uids + gids) %d\n", id_count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5563 | printf("Number of uids %d\n", uid_count); |
| 5564 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5565 | for(i = 0; i < id_count; i++) { |
| 5566 | if(id_table[i]->flags & ISA_UID) { |
| 5567 | struct passwd *user = getpwuid(id_table[i]->id); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5568 | printf("\t%s (%d)\n", user == NULL ? "unknown" : |
| 5569 | user->pw_name, id_table[i]->id); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5570 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | printf("Number of gids %d\n", guid_count); |
| 5574 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5575 | for(i = 0; i < id_count; i++) { |
| 5576 | if(id_table[i]->flags & ISA_GID) { |
| 5577 | struct group *group = getgrgid(id_table[i]->id); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5578 | printf("\t%s (%d)\n", group == NULL ? "unknown" : |
| 5579 | group->gr_name, id_table[i]->id); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5580 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5581 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5582 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5583 | return 0; |
| 5584 | } |