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 | * |
plougher | 8d4404d | 2010-07-21 03:16:55 +0000 | [diff] [blame] | 5 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
plougher | f6cd337 | 2007-08-19 03:33:23 +0000 | [diff] [blame] | 6 | * Phillip Lougher <phillip@lougher.demon.co.uk> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2, |
| 11 | * or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | * |
| 22 | * mksquashfs.c |
| 23 | */ |
| 24 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 25 | #define FALSE 0 |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 26 | #define TRUE 1 |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 27 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 28 | #include <pwd.h> |
| 29 | #include <grp.h> |
| 30 | #include <time.h> |
| 31 | #include <unistd.h> |
| 32 | #include <stdio.h> |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 33 | #include <stddef.h> |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 34 | #include <sys/time.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/ioctl.h> |
| 45 | #include <sys/types.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 46 | #include <sys/mman.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 47 | #include <pthread.h> |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 48 | #include <math.h> |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 49 | #include <regex.h> |
| 50 | #include <fnmatch.h> |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 51 | #include <sys/wait.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 52 | |
plougher | 6885329 | 2005-12-27 02:47:04 +0000 | [diff] [blame] | 53 | #ifndef linux |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 54 | #define __BYTE_ORDER BYTE_ORDER |
| 55 | #define __BIG_ENDIAN BIG_ENDIAN |
| 56 | #define __LITTLE_ENDIAN LITTLE_ENDIAN |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 57 | #include <sys/sysctl.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 58 | #else |
| 59 | #include <endian.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 60 | #include <sys/sysinfo.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 63 | #include "squashfs_fs.h" |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 64 | #include "global.h" |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 65 | #include "squashfs_swap.h" |
plougher | f5456bd | 2010-05-01 01:11:32 +0000 | [diff] [blame] | 66 | #include "mksquashfs.h" |
plougher | 117b2ea | 2006-02-09 09:23:56 +0000 | [diff] [blame] | 67 | #include "sort.h" |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 68 | #include "pseudo.h" |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 69 | #include "compressor.h" |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 70 | #include "xattr.h" |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 71 | |
| 72 | #ifdef SQUASHFS_TRACE |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 73 | #define TRACE(s, args...) \ |
| 74 | do { \ |
| 75 | if(progress_enabled) \ |
| 76 | printf("\n"); \ |
| 77 | printf("mksquashfs: "s, ## args); \ |
| 78 | } while(0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 79 | #else |
| 80 | #define TRACE(s, args...) |
| 81 | #endif |
| 82 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 83 | #define INFO(s, args...) \ |
| 84 | do {\ |
| 85 | if(!silent)\ |
| 86 | printf("mksquashfs: "s, ## args);\ |
| 87 | } while(0) |
| 88 | |
| 89 | #define ERROR(s, args...) \ |
| 90 | do {\ |
| 91 | pthread_mutex_lock(&progress_mutex); \ |
| 92 | if(progress_enabled) \ |
| 93 | fprintf(stderr, "\n"); \ |
| 94 | fprintf(stderr, s, ## args);\ |
| 95 | pthread_mutex_unlock(&progress_mutex); \ |
| 96 | } while(0) |
| 97 | |
| 98 | #define EXIT_MKSQUASHFS() \ |
| 99 | do {\ |
| 100 | if(restore)\ |
| 101 | restorefs();\ |
| 102 | if(delete && destination_file && !block_device)\ |
| 103 | unlink(destination_file);\ |
| 104 | exit(1);\ |
| 105 | } while(0) |
| 106 | |
| 107 | #define BAD_ERROR(s, args...) \ |
| 108 | do {\ |
| 109 | pthread_mutex_lock(&progress_mutex); \ |
| 110 | if(progress_enabled) \ |
| 111 | fprintf(stderr, "\n"); \ |
| 112 | fprintf(stderr, "FATAL ERROR:" s, ##args);\ |
| 113 | pthread_mutex_unlock(&progress_mutex); \ |
| 114 | EXIT_MKSQUASHFS();\ |
| 115 | } while(0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 116 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 117 | /* offset of data in compressed metadata blocks (allowing room for |
| 118 | * compressed size */ |
| 119 | #define BLOCK_OFFSET 2 |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 120 | int delete = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 121 | int fd; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 122 | int cur_uncompressed = 0, estimated_uncompressed = 0; |
| 123 | int columns; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 124 | |
| 125 | /* filesystem flags for building */ |
plougher | ce564c6 | 2010-05-19 03:01:49 +0000 | [diff] [blame] | 126 | int no_xattrs = 0, noX = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 127 | int duplicate_checking = 1, noF = 0, no_fragments = 0, always_use_fragments = 0; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 128 | int noI = 0, noD = 0; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 129 | int silent = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 130 | long long global_uid = -1, global_gid = -1; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 131 | int exportable = TRUE; |
| 132 | int progress = TRUE; |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 133 | int progress_enabled = FALSE; |
plougher | 8dcc699 | 2007-08-17 19:32:52 +0000 | [diff] [blame] | 134 | int sparse_files = TRUE; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 135 | int old_exclude = TRUE; |
| 136 | int use_regex = FALSE; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 137 | int first_freelist = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 138 | |
| 139 | /* superblock attributes */ |
| 140 | int block_size = SQUASHFS_FILE_SIZE, block_log; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 141 | unsigned int id_count = 0; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 142 | int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0, |
| 143 | sock_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 144 | |
| 145 | /* write position within data section */ |
| 146 | long long bytes = 0, total_bytes = 0; |
| 147 | |
| 148 | /* in memory directory table - possibly compressed */ |
| 149 | char *directory_table = NULL; |
| 150 | unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0; |
| 151 | |
| 152 | /* cached directory table */ |
| 153 | char *directory_data_cache = NULL; |
| 154 | unsigned int directory_cache_bytes = 0, directory_cache_size = 0; |
| 155 | |
| 156 | /* in memory inode table - possibly compressed */ |
| 157 | char *inode_table = NULL; |
| 158 | unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0; |
| 159 | |
| 160 | /* cached inode table */ |
| 161 | char *data_cache = NULL; |
| 162 | unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0; |
| 163 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 164 | /* inode lookup table */ |
| 165 | squashfs_inode *inode_lookup_table = NULL; |
| 166 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 167 | /* in memory directory data */ |
| 168 | #define I_COUNT_SIZE 128 |
| 169 | #define DIR_ENTRIES 32 |
| 170 | #define INODE_HASH_SIZE 65536 |
| 171 | #define INODE_HASH_MASK (INODE_HASH_SIZE - 1) |
| 172 | #define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK) |
| 173 | |
| 174 | struct cached_dir_index { |
| 175 | squashfs_dir_index index; |
| 176 | char *name; |
| 177 | }; |
| 178 | |
| 179 | struct directory { |
| 180 | unsigned int start_block; |
| 181 | unsigned int size; |
| 182 | unsigned char *buff; |
| 183 | unsigned char *p; |
| 184 | unsigned int entry_count; |
| 185 | unsigned char *entry_count_p; |
| 186 | unsigned int i_count; |
| 187 | unsigned int i_size; |
| 188 | struct cached_dir_index *index; |
| 189 | unsigned char *index_count_p; |
| 190 | unsigned int inode_number; |
| 191 | }; |
| 192 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 193 | struct inode_info *inode_info[INODE_HASH_SIZE]; |
| 194 | |
| 195 | /* hash tables used to do fast duplicate searches in duplicate check */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 196 | struct file_info *dupl[65536]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 197 | int dup_files = 0; |
| 198 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 199 | /* exclude file handling */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 200 | /* list of exclude dirs/files */ |
| 201 | struct exclude_info { |
| 202 | dev_t st_dev; |
| 203 | ino_t st_ino; |
| 204 | }; |
| 205 | |
| 206 | #define EXCLUDE_SIZE 8192 |
| 207 | int exclude = 0; |
| 208 | struct exclude_info *exclude_paths = NULL; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 209 | int old_excluded(char *filename, struct stat *buf); |
| 210 | |
| 211 | struct path_entry { |
| 212 | char *name; |
| 213 | regex_t *preg; |
| 214 | struct pathname *paths; |
| 215 | }; |
| 216 | |
| 217 | struct pathname { |
| 218 | int names; |
| 219 | struct path_entry *name; |
| 220 | }; |
| 221 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 222 | struct pathnames { |
| 223 | int count; |
| 224 | struct pathname *path[0]; |
| 225 | }; |
| 226 | #define PATHS_ALLOC_SIZE 10 |
| 227 | |
| 228 | struct pathnames *paths = NULL; |
| 229 | struct pathname *path = NULL; |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 230 | struct pathname *stickypath = NULL; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 231 | int excluded(struct pathnames *paths, char *name, struct pathnames **new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 232 | |
| 233 | /* fragment block data structures */ |
| 234 | int fragments = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 235 | struct file_buffer *fragment_data = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 236 | int fragment_size = 0; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 237 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 238 | struct fragment { |
| 239 | unsigned int index; |
| 240 | int offset; |
| 241 | int size; |
| 242 | }; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 243 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 244 | #define FRAG_SIZE 32768 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 245 | #define FRAG_INDEX (1LL << 32) |
| 246 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 247 | squashfs_fragment_entry *fragment_table = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 248 | int fragments_outstanding = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 249 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 250 | /* current inode number for directories and non directories */ |
| 251 | unsigned int dir_inode_no = 1; |
| 252 | unsigned int inode_no = 0; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 253 | unsigned int root_inode_number = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 254 | |
| 255 | /* list of source dirs/files */ |
| 256 | int source = 0; |
| 257 | char **source_path; |
| 258 | |
| 259 | /* list of root directory entries read from original filesystem */ |
| 260 | int old_root_entries = 0; |
| 261 | struct old_root_entry_info { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 262 | char *name; |
| 263 | struct inode_info inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 264 | }; |
| 265 | struct old_root_entry_info *old_root_entry; |
| 266 | |
| 267 | /* in memory file info */ |
| 268 | struct file_info { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 269 | long long file_size; |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 270 | long long bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 271 | unsigned short checksum; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 272 | unsigned short fragment_checksum; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 273 | long long start; |
| 274 | unsigned int *block_list; |
| 275 | struct file_info *next; |
| 276 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 277 | char checksum_flag; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | /* count of how many times SIGINT or SIGQUIT has been sent */ |
| 281 | int interrupted = 0; |
| 282 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 283 | /* restore orignal filesystem state if appending to existing filesystem is |
| 284 | * cancelled */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 285 | jmp_buf env; |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 286 | char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 287 | |
| 288 | long long sbytes, stotal_bytes; |
| 289 | |
| 290 | unsigned int sinode_bytes, scache_bytes, sdirectory_bytes, |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 291 | sdirectory_cache_bytes, sdirectory_compressed_bytes, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 292 | stotal_inode_bytes, stotal_directory_bytes, |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 293 | sinode_count = 0, sfile_count, ssym_count, sdev_count, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 294 | sdir_count, sfifo_count, ssock_count, sdup_files; |
| 295 | int sfragments; |
| 296 | int restore = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 297 | int threads; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 298 | |
| 299 | /* flag whether destination file is a block device */ |
| 300 | int block_device = 0; |
| 301 | |
| 302 | /* flag indicating whether files are sorted using sort list(s) */ |
| 303 | int sorted = 0; |
| 304 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 305 | /* save destination file name for deleting on error */ |
| 306 | char *destination_file = NULL; |
| 307 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 308 | /* recovery file for abnormal exit on appending */ |
| 309 | char recovery_file[1024] = ""; |
| 310 | int recover = TRUE; |
| 311 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 312 | /* struct describing a cache entry passed between threads */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 313 | struct file_buffer { |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 314 | struct cache *cache; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 315 | int keep; |
| 316 | long long file_size; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 317 | long long index; |
| 318 | long long block; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 319 | long long sequence; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 320 | int size; |
| 321 | int c_byte; |
| 322 | int used; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 323 | int fragment; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 324 | int error; |
| 325 | struct file_buffer *hash_next; |
| 326 | struct file_buffer *hash_prev; |
| 327 | struct file_buffer *free_next; |
| 328 | struct file_buffer *free_prev; |
| 329 | struct file_buffer *next; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 330 | char data[0]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 333 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 334 | /* struct describing queues used to pass data between threads */ |
| 335 | struct queue { |
| 336 | int size; |
| 337 | int readp; |
| 338 | int writep; |
| 339 | pthread_mutex_t mutex; |
| 340 | pthread_cond_t empty; |
| 341 | pthread_cond_t full; |
| 342 | void **data; |
| 343 | }; |
| 344 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 345 | |
| 346 | /* in memory uid tables */ |
| 347 | #define ID_ENTRIES 256 |
| 348 | #define ID_HASH(id) (id & (ID_ENTRIES - 1)) |
| 349 | #define ISA_UID 1 |
| 350 | #define ISA_GID 2 |
| 351 | struct id { |
| 352 | unsigned int id; |
| 353 | int index; |
| 354 | char flags; |
| 355 | struct id *next; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 356 | }; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 357 | struct id *id_hash_table[ID_ENTRIES]; |
| 358 | struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS]; |
| 359 | unsigned int uid_count = 0, guid_count = 0; |
| 360 | unsigned int sid_count = 0, suid_count = 0, sguid_count = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 361 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 362 | struct cache *reader_buffer, *writer_buffer, *fragment_buffer; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 363 | struct queue *to_reader, *from_reader, *to_writer, *from_writer, *from_deflate, |
| 364 | *to_frag; |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 365 | pthread_t *thread, *deflator_thread, *frag_deflator_thread, progress_thread; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 366 | pthread_mutex_t fragment_mutex; |
| 367 | pthread_cond_t fragment_waiting; |
| 368 | pthread_mutex_t pos_mutex; |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 369 | pthread_mutex_t progress_mutex; |
| 370 | pthread_cond_t progress_wait; |
| 371 | int rotate = 0; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 372 | struct pseudo *pseudo = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 373 | |
| 374 | /* user options that control parallelisation */ |
| 375 | int processors = -1; |
| 376 | /* default size of output buffer in Mbytes */ |
| 377 | #define WRITER_BUFFER_DEFAULT 512 |
| 378 | /* default size of input buffer in Mbytes */ |
| 379 | #define READER_BUFFER_DEFAULT 64 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 380 | /* default size of fragment buffer in Mbytes */ |
| 381 | #define FRAGMENT_BUFFER_DEFAULT 64 |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 382 | int writer_buffer_size; |
| 383 | int reader_buffer_size; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 384 | int fragment_buffer_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 385 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 386 | /* compression operations structure */ |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 387 | static struct compressor *comp; |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 388 | char *comp_name = COMP_DEFAULT; |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 389 | |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 390 | char *read_from_disk(long long start, unsigned int avail_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 391 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 392 | int type); |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 393 | extern struct compressor *read_super(int fd, squashfs_super_block *sBlk, |
| 394 | char *source); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 395 | extern long long read_filesystem(char *root_name, int fd, |
| 396 | squashfs_super_block *sBlk, char **cinode_table, char **data_cache, |
| 397 | char **cdirectory_table, char **directory_data_cache, |
| 398 | unsigned int *last_directory_block, unsigned int *inode_dir_offset, |
| 399 | unsigned int *inode_dir_file_size, unsigned int *root_inode_size, |
| 400 | unsigned int *inode_dir_start_block, int *file_count, int *sym_count, |
| 401 | int *dev_count, int *dir_count, int *fifo_count, int *sock_count, |
| 402 | long long *uncompressed_file, unsigned int *uncompressed_inode, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 403 | unsigned int *uncompressed_directory, |
| 404 | unsigned int *inode_dir_inode_number, |
| 405 | unsigned int *inode_dir_parent_inode, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 406 | void (push_directory_entry)(char *, squashfs_inode, int, int), |
| 407 | squashfs_fragment_entry **fragment_table, |
| 408 | squashfs_inode **inode_lookup_table); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 409 | extern int read_sort_file(char *filename, int source, char *source_path[]); |
| 410 | extern void sort_files_and_write(struct dir_info *dir); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 411 | struct file_info *duplicate(long long file_size, long long bytes, |
| 412 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 413 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 414 | unsigned short fragment_checksum, int checksum_flag); |
| 415 | struct dir_info *dir_scan1(char *, struct pathnames *, int (_readdir)(char *, |
| 416 | char *, struct dir_info *)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 417 | struct dir_info *dir_scan2(struct dir_info *dir, struct pseudo *pseudo); |
| 418 | void dir_scan3(squashfs_inode *inode, struct dir_info *dir_info); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 419 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 420 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 421 | unsigned short checksum, unsigned short fragment_checksum, |
| 422 | int checksum_flag); |
plougher | 1d1c6bb | 2010-07-21 03:03:13 +0000 | [diff] [blame] | 423 | extern int generate_file_priorities(struct dir_info *dir, int priority, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 424 | struct stat *buf); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 425 | extern struct priority_entry *priority_list[65536]; |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 426 | void progress_bar(long long current, long long max, int columns); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 427 | long long generic_write_table(int, char *, int, char *, int); |
| 428 | extern long long write_xattrs(); |
| 429 | extern unsigned int xattr_bytes, total_xattr_bytes; |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 430 | extern int save_xattrs(); |
| 431 | extern void restore_xattrs(); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 432 | void restorefs(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 433 | |
| 434 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 435 | struct queue *queue_init(int size) |
| 436 | { |
| 437 | struct queue *queue = malloc(sizeof(struct queue)); |
| 438 | |
| 439 | if(queue == NULL) |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 440 | goto failed; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 441 | |
| 442 | if((queue->data = malloc(sizeof(void *) * (size + 1))) == NULL) { |
| 443 | free(queue); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 444 | goto failed; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | queue->size = size + 1; |
| 448 | queue->readp = queue->writep = 0; |
| 449 | pthread_mutex_init(&queue->mutex, NULL); |
| 450 | pthread_cond_init(&queue->empty, NULL); |
| 451 | pthread_cond_init(&queue->full, NULL); |
| 452 | |
| 453 | return queue; |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 454 | |
| 455 | failed: |
| 456 | BAD_ERROR("Out of memory in queue_init\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
| 460 | void queue_put(struct queue *queue, void *data) |
| 461 | { |
| 462 | int nextp; |
| 463 | |
| 464 | pthread_mutex_lock(&queue->mutex); |
| 465 | |
| 466 | while((nextp = (queue->writep + 1) % queue->size) == queue->readp) |
| 467 | pthread_cond_wait(&queue->full, &queue->mutex); |
| 468 | |
| 469 | queue->data[queue->writep] = data; |
| 470 | queue->writep = nextp; |
| 471 | pthread_cond_signal(&queue->empty); |
| 472 | pthread_mutex_unlock(&queue->mutex); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | void *queue_get(struct queue *queue) |
| 477 | { |
| 478 | void *data; |
| 479 | pthread_mutex_lock(&queue->mutex); |
| 480 | |
| 481 | while(queue->readp == queue->writep) |
| 482 | pthread_cond_wait(&queue->empty, &queue->mutex); |
| 483 | |
| 484 | data = queue->data[queue->readp]; |
| 485 | queue->readp = (queue->readp + 1) % queue->size; |
| 486 | pthread_cond_signal(&queue->full); |
| 487 | pthread_mutex_unlock(&queue->mutex); |
| 488 | |
| 489 | return data; |
| 490 | } |
| 491 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 492 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 493 | /* Cache status struct. Caches are used to keep |
| 494 | track of memory buffers passed between different threads */ |
| 495 | struct cache { |
| 496 | int max_buffers; |
| 497 | int count; |
| 498 | int buffer_size; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 499 | pthread_mutex_t mutex; |
| 500 | pthread_cond_t wait_for_free; |
| 501 | struct file_buffer *free_list; |
| 502 | struct file_buffer *hash_table[65536]; |
| 503 | }; |
| 504 | |
| 505 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 506 | #define INSERT_LIST(NAME, TYPE) \ |
| 507 | void insert_##NAME##_list(TYPE **list, TYPE *entry) { \ |
| 508 | if(*list) { \ |
| 509 | entry->NAME##_next = *list; \ |
| 510 | entry->NAME##_prev = (*list)->NAME##_prev; \ |
| 511 | (*list)->NAME##_prev->NAME##_next = entry; \ |
| 512 | (*list)->NAME##_prev = entry; \ |
| 513 | } else { \ |
| 514 | *list = entry; \ |
| 515 | entry->NAME##_prev = entry->NAME##_next = entry; \ |
| 516 | } \ |
| 517 | } |
| 518 | |
| 519 | |
| 520 | #define REMOVE_LIST(NAME, TYPE) \ |
| 521 | void remove_##NAME##_list(TYPE **list, TYPE *entry) { \ |
| 522 | if(entry->NAME##_prev == entry && entry->NAME##_next == entry) { \ |
| 523 | /* only this entry in the list */ \ |
| 524 | *list = NULL; \ |
| 525 | } else if(entry->NAME##_prev != NULL && entry->NAME##_next != NULL) { \ |
| 526 | /* more than one entry in the list */ \ |
| 527 | entry->NAME##_next->NAME##_prev = entry->NAME##_prev; \ |
| 528 | entry->NAME##_prev->NAME##_next = entry->NAME##_next; \ |
| 529 | if(*list == entry) \ |
| 530 | *list = entry->NAME##_next; \ |
| 531 | } \ |
| 532 | entry->NAME##_prev = entry->NAME##_next = NULL; \ |
| 533 | } |
| 534 | |
| 535 | |
| 536 | #define CALCULATE_HASH(start) (start & 0xffff) \ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 537 | |
| 538 | |
| 539 | /* Called with the cache mutex held */ |
| 540 | void insert_hash_table(struct cache *cache, struct file_buffer *entry) |
| 541 | { |
| 542 | int hash = CALCULATE_HASH(entry->index); |
| 543 | |
| 544 | entry->hash_next = cache->hash_table[hash]; |
| 545 | cache->hash_table[hash] = entry; |
| 546 | entry->hash_prev = NULL; |
| 547 | if(entry->hash_next) |
| 548 | entry->hash_next->hash_prev = entry; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | /* Called with the cache mutex held */ |
| 553 | void remove_hash_table(struct cache *cache, struct file_buffer *entry) |
| 554 | { |
| 555 | if(entry->hash_prev) |
| 556 | entry->hash_prev->hash_next = entry->hash_next; |
| 557 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 558 | cache->hash_table[CALCULATE_HASH(entry->index)] = |
| 559 | entry->hash_next; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 560 | if(entry->hash_next) |
| 561 | entry->hash_next->hash_prev = entry->hash_prev; |
| 562 | |
| 563 | entry->hash_prev = entry->hash_next = NULL; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /* Called with the cache mutex held */ |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 568 | INSERT_LIST(free, struct file_buffer) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 569 | |
| 570 | /* Called with the cache mutex held */ |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 571 | REMOVE_LIST(free, struct file_buffer) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 572 | |
| 573 | |
| 574 | struct cache *cache_init(int buffer_size, int max_buffers) |
| 575 | { |
| 576 | struct cache *cache = malloc(sizeof(struct cache)); |
| 577 | |
| 578 | if(cache == NULL) |
plougher | 9ca649c | 2010-07-21 00:46:11 +0000 | [diff] [blame] | 579 | BAD_ERROR("Out of memory in cache_init\n"); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 580 | |
| 581 | cache->max_buffers = max_buffers; |
| 582 | cache->buffer_size = buffer_size; |
| 583 | cache->count = 0; |
| 584 | cache->free_list = NULL; |
| 585 | memset(cache->hash_table, 0, sizeof(struct file_buffer *) * 65536); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 586 | pthread_mutex_init(&cache->mutex, NULL); |
| 587 | pthread_cond_init(&cache->wait_for_free, NULL); |
| 588 | |
| 589 | return cache; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | struct file_buffer *cache_lookup(struct cache *cache, long long index) |
| 594 | { |
| 595 | /* Lookup block in the cache, if found return with usage count |
| 596 | * incremented, if not found return NULL */ |
| 597 | int hash = CALCULATE_HASH(index); |
| 598 | struct file_buffer *entry; |
| 599 | |
| 600 | pthread_mutex_lock(&cache->mutex); |
| 601 | |
| 602 | for(entry = cache->hash_table[hash]; entry; entry = entry->hash_next) |
| 603 | if(entry->index == index) |
| 604 | break; |
| 605 | |
| 606 | if(entry) { |
| 607 | /* found the block in the cache, increment used count and |
| 608 | * if necessary remove from free list so it won't disappear |
| 609 | */ |
| 610 | entry->used ++; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 611 | remove_free_list(&cache->free_list, entry); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | pthread_mutex_unlock(&cache->mutex); |
| 615 | |
| 616 | return entry; |
| 617 | } |
| 618 | |
| 619 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 620 | struct file_buffer *cache_get(struct cache *cache, long long index, int keep) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 621 | { |
| 622 | /* Get a free block out of the cache indexed on index. */ |
| 623 | struct file_buffer *entry; |
| 624 | |
| 625 | pthread_mutex_lock(&cache->mutex); |
| 626 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 627 | while(1) { |
| 628 | /* first try to get a block from the free list */ |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 629 | if(first_freelist && cache->free_list) { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 630 | /* a block on the free_list is a "keep" block */ |
| 631 | entry = cache->free_list; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 632 | remove_free_list(&cache->free_list, entry); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 633 | remove_hash_table(cache, entry); |
| 634 | break; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 635 | } else if(cache->count < cache->max_buffers) { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 636 | /* next try to allocate new block */ |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 637 | entry = malloc(sizeof(struct file_buffer) + |
| 638 | cache->buffer_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 639 | if(entry == NULL) |
| 640 | goto failed; |
| 641 | entry->cache = cache; |
| 642 | entry->free_prev = entry->free_next = NULL; |
| 643 | cache->count ++; |
| 644 | break; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 645 | } else if(!first_freelist && cache->free_list) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 646 | /* a block on the free_list is a "keep" block */ |
| 647 | entry = cache->free_list; |
| 648 | remove_free_list(&cache->free_list, entry); |
| 649 | remove_hash_table(cache, entry); |
| 650 | break; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 651 | } else |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 652 | /* wait for a block */ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 653 | pthread_cond_wait(&cache->wait_for_free, &cache->mutex); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 654 | } |
| 655 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 656 | /* initialise block and if a keep block insert into the hash table */ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 657 | entry->used = 1; |
| 658 | entry->error = FALSE; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 659 | entry->keep = keep; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 660 | if(keep) { |
| 661 | entry->index = index; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 662 | insert_hash_table(cache, entry); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 663 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 664 | pthread_mutex_unlock(&cache->mutex); |
| 665 | |
| 666 | return entry; |
| 667 | |
| 668 | failed: |
| 669 | pthread_mutex_unlock(&cache->mutex); |
plougher | cc6896d | 2010-07-21 00:56:25 +0000 | [diff] [blame] | 670 | BAD_ERROR("Out of memory in cache_get\n"); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 671 | } |
| 672 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 673 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 674 | void cache_rehash(struct file_buffer *entry, long long index) |
| 675 | { |
| 676 | struct cache *cache = entry->cache; |
| 677 | |
| 678 | pthread_mutex_lock(&cache->mutex); |
| 679 | if(entry->keep) |
| 680 | remove_hash_table(cache, entry); |
| 681 | entry->keep = TRUE; |
| 682 | entry->index = index; |
| 683 | insert_hash_table(cache, entry); |
| 684 | pthread_mutex_unlock(&cache->mutex); |
| 685 | } |
| 686 | |
| 687 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 688 | void cache_block_put(struct file_buffer *entry) |
| 689 | { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 690 | struct cache *cache; |
| 691 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 692 | /* finished with this cache entry, once the usage count reaches zero it |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 693 | * 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] | 694 | * blocks remain accessible via the hash table they can be found |
| 695 | * getting a new lease of life before they are reused. */ |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 696 | |
| 697 | if(entry == NULL) |
| 698 | return; |
| 699 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 700 | cache = entry->cache; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 701 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 702 | pthread_mutex_lock(&cache->mutex); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 703 | |
| 704 | entry->used --; |
| 705 | if(entry->used == 0) { |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 706 | if(entry->keep) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 707 | insert_free_list(&cache->free_list, entry); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 708 | else { |
| 709 | free(entry); |
| 710 | cache->count --; |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 711 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 712 | |
| 713 | /* One or more threads may be waiting on this block */ |
| 714 | pthread_cond_signal(&cache->wait_for_free); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 715 | } |
| 716 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 717 | pthread_mutex_unlock(&cache->mutex); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 721 | #define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \ |
| 722 | + (((char *)A) - data_cache))) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 723 | |
| 724 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 725 | inline void inc_progress_bar() |
| 726 | { |
| 727 | cur_uncompressed ++; |
| 728 | } |
| 729 | |
| 730 | |
| 731 | inline void update_progress_bar() |
| 732 | { |
| 733 | pthread_mutex_lock(&progress_mutex); |
| 734 | pthread_cond_signal(&progress_wait); |
| 735 | pthread_mutex_unlock(&progress_mutex); |
| 736 | } |
| 737 | |
| 738 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 739 | inline void waitforthread(int i) |
| 740 | { |
| 741 | TRACE("Waiting for thread %d\n", i); |
| 742 | while(thread[i] != 0) |
| 743 | sched_yield(); |
| 744 | } |
| 745 | |
| 746 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 747 | void restorefs() |
| 748 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 749 | int i; |
| 750 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 751 | if(thread == NULL || thread[0] == 0) |
| 752 | return; |
| 753 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 754 | ERROR("Exiting - restoring original filesystem!\n\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 755 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 756 | for(i = 0; i < 2 + processors * 2; i++) |
plougher | 5aa1816 | 2007-12-13 12:15:21 +0000 | [diff] [blame] | 757 | if(thread[i]) |
| 758 | pthread_kill(thread[i], SIGUSR1); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 759 | for(i = 0; i < 2 + processors * 2; i++) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 760 | waitforthread(i); |
| 761 | TRACE("All threads in signal handler\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 762 | bytes = sbytes; |
| 763 | memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 764 | memcpy(directory_data_cache, sdirectory_data_cache, |
| 765 | sdirectory_cache_bytes); |
| 766 | directory_cache_bytes = sdirectory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 767 | inode_bytes = sinode_bytes; |
| 768 | directory_bytes = sdirectory_bytes; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 769 | memcpy(directory_table + directory_bytes, sdirectory_compressed, |
| 770 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 771 | directory_bytes += sdirectory_compressed_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 772 | total_bytes = stotal_bytes; |
| 773 | total_inode_bytes = stotal_inode_bytes; |
| 774 | total_directory_bytes = stotal_directory_bytes; |
| 775 | inode_count = sinode_count; |
| 776 | file_count = sfile_count; |
| 777 | sym_count = ssym_count; |
| 778 | dev_count = sdev_count; |
| 779 | dir_count = sdir_count; |
| 780 | fifo_count = sfifo_count; |
| 781 | sock_count = ssock_count; |
| 782 | dup_files = sdup_files; |
| 783 | fragments = sfragments; |
| 784 | fragment_size = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 785 | id_count = sid_count; |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 786 | restore_xattrs(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 787 | longjmp(env, 1); |
| 788 | } |
| 789 | |
| 790 | |
| 791 | void sighandler() |
| 792 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 793 | if(++interrupted > 2) |
| 794 | return; |
| 795 | if(interrupted == 2) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 796 | restorefs(); |
| 797 | else { |
| 798 | ERROR("Interrupting will restore original filesystem!\n"); |
| 799 | ERROR("Interrupt again to quit\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
| 803 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 804 | void sighandler2() |
| 805 | { |
| 806 | EXIT_MKSQUASHFS(); |
| 807 | } |
| 808 | |
| 809 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 810 | void sigusr1_handler() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 811 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 812 | int i; |
| 813 | sigset_t sigmask; |
| 814 | pthread_t thread_id = pthread_self(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 815 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 816 | for(i = 0; i < (2 + processors * 2) && thread[i] != thread_id; i++); |
| 817 | thread[i] = (pthread_t) 0; |
| 818 | |
plougher | 07966f7 | 2007-11-14 10:54:45 +0000 | [diff] [blame] | 819 | TRACE("Thread %d(%p) in sigusr1_handler\n", i, &thread_id); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 820 | |
| 821 | sigemptyset(&sigmask); |
| 822 | sigaddset(&sigmask, SIGINT); |
| 823 | sigaddset(&sigmask, SIGQUIT); |
| 824 | sigaddset(&sigmask, SIGUSR1); |
| 825 | while(1) { |
| 826 | sigsuspend(&sigmask); |
| 827 | TRACE("After wait in sigusr1_handler :(\n"); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 832 | void sigwinch_handler() |
| 833 | { |
| 834 | struct winsize winsize; |
| 835 | |
| 836 | if(ioctl(1, TIOCGWINSZ, &winsize) == -1) { |
plougher | 1d87866 | 2009-03-29 23:02:00 +0000 | [diff] [blame] | 837 | if(isatty(STDOUT_FILENO)) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 838 | printf("TIOCGWINSZ ioctl failed, defaulting to 80 " |
| 839 | "columns\n"); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 840 | columns = 80; |
| 841 | } else |
| 842 | columns = winsize.ws_col; |
| 843 | } |
| 844 | |
| 845 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 846 | void sigalrm_handler() |
| 847 | { |
| 848 | rotate = (rotate + 1) % 4; |
| 849 | } |
| 850 | |
| 851 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 852 | int mangle2(void **strm, char *d, char *s, int size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 853 | int block_size, int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 854 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 855 | int error, c_byte = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 856 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 857 | if(!uncompressed) { |
| 858 | c_byte = comp->compress(strm, d, s, size, block_size, &error); |
| 859 | if(c_byte == -1) |
| 860 | BAD_ERROR("mangle2:: %s compress failed with error " |
| 861 | "code %d\n", comp->name, error); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 862 | } |
| 863 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 864 | if(c_byte == 0 || c_byte >= size) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 865 | memcpy(d, s, size); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 866 | return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : |
| 867 | SQUASHFS_COMPRESSED_BIT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 868 | } |
| 869 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 870 | return c_byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 874 | int mangle(char *d, char *s, int size, int block_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 875 | int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 876 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 877 | static void *stream = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 878 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 879 | return mangle2(&stream, d, s, size, block_size, uncompressed, |
| 880 | data_block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 884 | void *get_inode(int req_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 885 | { |
| 886 | int data_space; |
| 887 | unsigned short c_byte; |
| 888 | |
| 889 | while(cache_bytes >= SQUASHFS_METADATA_SIZE) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 890 | if((inode_size - inode_bytes) < |
| 891 | ((SQUASHFS_METADATA_SIZE << 1)) + 2) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 892 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 893 | (SQUASHFS_METADATA_SIZE << 1) + 2); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 894 | if(it == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 895 | goto failed; |
| 896 | } |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 897 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 898 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
| 899 | } |
| 900 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 901 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 902 | data_cache, SQUASHFS_METADATA_SIZE, |
| 903 | SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 904 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 905 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 906 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 907 | total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 908 | memcpy(data_cache, data_cache + SQUASHFS_METADATA_SIZE, |
| 909 | cache_bytes - SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 910 | cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 911 | } |
| 912 | |
| 913 | data_space = (cache_size - cache_bytes); |
| 914 | if(data_space < req_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 915 | int realloc_size = cache_size == 0 ? |
| 916 | ((req_size + SQUASHFS_METADATA_SIZE) & |
| 917 | ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - |
| 918 | data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 919 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 920 | void *dc = realloc(data_cache, cache_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 921 | realloc_size); |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 922 | if(dc == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 923 | goto failed; |
| 924 | } |
| 925 | cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 926 | data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | cache_bytes += req_size; |
| 930 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 931 | return data_cache + cache_bytes - req_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 932 | |
| 933 | failed: |
| 934 | BAD_ERROR("Out of memory in inode table reallocation!\n"); |
| 935 | } |
| 936 | |
| 937 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 938 | int read_bytes(int fd, void *buff, int bytes) |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 939 | { |
| 940 | int res, count; |
| 941 | |
| 942 | for(count = 0; count < bytes; count += res) { |
| 943 | res = read(fd, buff + count, bytes - count); |
| 944 | if(res < 1) { |
plougher | 96f85a1 | 2009-03-29 22:39:59 +0000 | [diff] [blame] | 945 | if(res == 0) |
| 946 | goto bytes_read; |
| 947 | else if(errno != EINTR) { |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 948 | ERROR("Read failed because %s\n", |
| 949 | strerror(errno)); |
| 950 | return -1; |
| 951 | } else |
| 952 | res = 0; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | bytes_read: |
| 957 | return count; |
| 958 | } |
| 959 | |
| 960 | |
plougher | 3306cb2 | 2010-06-18 04:22:44 +0000 | [diff] [blame] | 961 | int read_fs_bytes(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 962 | { |
| 963 | off_t off = byte; |
| 964 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 965 | TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 966 | byte, bytes); |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 967 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 968 | pthread_mutex_lock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 969 | if(lseek(fd, off, SEEK_SET) == -1) { |
| 970 | ERROR("Lseek on destination failed because %s\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 971 | strerror(errno)); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 972 | goto failed; |
| 973 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 974 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 975 | if(read_bytes(fd, buff, bytes) < bytes) { |
| 976 | ERROR("Read on destination failed\n"); |
| 977 | goto failed; |
| 978 | } |
| 979 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 980 | pthread_mutex_unlock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 981 | return 1; |
| 982 | |
| 983 | failed: |
| 984 | pthread_mutex_unlock(&pos_mutex); |
| 985 | return 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 989 | int write_bytes(int fd, void *buff, int bytes) |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 990 | { |
| 991 | int res, count; |
| 992 | |
| 993 | for(count = 0; count < bytes; count += res) { |
| 994 | res = write(fd, buff + count, bytes - count); |
| 995 | if(res == -1) { |
| 996 | if(errno != EINTR) { |
| 997 | ERROR("Write failed because %s\n", |
| 998 | strerror(errno)); |
| 999 | return -1; |
| 1000 | } |
| 1001 | res = 0; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | void write_destination(int fd, long long byte, int bytes, char *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1010 | { |
| 1011 | off_t off = byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1012 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1013 | if(interrupted < 2) |
| 1014 | pthread_mutex_lock(&pos_mutex); |
| 1015 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 1016 | if(lseek(fd, off, SEEK_SET) == -1) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1017 | BAD_ERROR("Lseek on destination failed because %s\n", |
| 1018 | strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1019 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1020 | if(write_bytes(fd, buff, bytes) == -1) |
| 1021 | BAD_ERROR("Write on destination failed\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1022 | |
| 1023 | if(interrupted < 2) |
| 1024 | pthread_mutex_unlock(&pos_mutex); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | long long write_inodes() |
| 1029 | { |
| 1030 | unsigned short c_byte; |
| 1031 | int avail_bytes; |
| 1032 | char *datap = data_cache; |
| 1033 | long long start_bytes = bytes; |
| 1034 | |
| 1035 | while(cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1036 | if(inode_size - inode_bytes < |
| 1037 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 1038 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1039 | ((SQUASHFS_METADATA_SIZE << 1) + 2)); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 1040 | if(it == NULL) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1041 | BAD_ERROR("Out of memory in inode table " |
| 1042 | "reallocation!\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1043 | } |
| 1044 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 1045 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1046 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1047 | avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 1048 | SQUASHFS_METADATA_SIZE : cache_bytes; |
| 1049 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap, |
| 1050 | avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1051 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1052 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1053 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 1054 | total_inode_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1055 | datap += avail_bytes; |
| 1056 | cache_bytes -= avail_bytes; |
| 1057 | } |
| 1058 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1059 | write_destination(fd, bytes, inode_bytes, (char *) inode_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1060 | bytes += inode_bytes; |
| 1061 | |
| 1062 | return start_bytes; |
| 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | long long write_directories() |
| 1067 | { |
| 1068 | unsigned short c_byte; |
| 1069 | int avail_bytes; |
| 1070 | char *directoryp = directory_data_cache; |
| 1071 | long long start_bytes = bytes; |
| 1072 | |
| 1073 | while(directory_cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1074 | if(directory_size - directory_bytes < |
| 1075 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1076 | void *dt = realloc(directory_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1077 | directory_size + ((SQUASHFS_METADATA_SIZE << 1) |
| 1078 | + 2)); |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1079 | if(dt == NULL) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1080 | BAD_ERROR("Out of memory in directory table " |
| 1081 | "reallocation!\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1082 | } |
| 1083 | directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1084 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1085 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1086 | avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 1087 | SQUASHFS_METADATA_SIZE : directory_cache_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1088 | c_byte = mangle(directory_table + directory_bytes + |
| 1089 | BLOCK_OFFSET, directoryp, avail_bytes, |
| 1090 | SQUASHFS_METADATA_SIZE, noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1091 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1092 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1093 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1094 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1095 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1096 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1097 | total_directory_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1098 | directoryp += avail_bytes; |
| 1099 | directory_cache_bytes -= avail_bytes; |
| 1100 | } |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1101 | write_destination(fd, bytes, directory_bytes, (char *) directory_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1102 | bytes += directory_bytes; |
| 1103 | |
| 1104 | return start_bytes; |
| 1105 | } |
| 1106 | |
| 1107 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1108 | long long write_id_table() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1109 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1110 | unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1111 | unsigned int p[id_count]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1112 | int i; |
| 1113 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1114 | 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] | 1115 | for(i = 0; i < id_count; i++) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1116 | 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] | 1117 | SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
plougher | 3a4a1b3 | 2010-07-20 00:58:11 +0000 | [diff] [blame] | 1120 | return generic_write_table(id_bytes, (char *) p, 0, NULL, noI); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1124 | struct id *get_id(unsigned int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1125 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1126 | int hash = ID_HASH(id); |
| 1127 | struct id *entry = id_hash_table[hash]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1128 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1129 | for(; entry; entry = entry->next) |
| 1130 | if(entry->id == id) |
| 1131 | break; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1132 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1133 | return entry; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1137 | struct id *create_id(unsigned int id) |
| 1138 | { |
| 1139 | int hash = ID_HASH(id); |
| 1140 | struct id *entry = malloc(sizeof(struct id)); |
| 1141 | if(entry == NULL) |
| 1142 | BAD_ERROR("Out of memory in create_id\n"); |
| 1143 | entry->id = id; |
| 1144 | entry->index = id_count ++; |
| 1145 | entry->flags = 0; |
| 1146 | entry->next = id_hash_table[hash]; |
| 1147 | id_hash_table[hash] = entry; |
| 1148 | id_table[entry->index] = entry; |
| 1149 | return entry; |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | unsigned int get_uid(unsigned int uid) |
| 1154 | { |
| 1155 | struct id *entry = get_id(uid); |
| 1156 | |
| 1157 | if(entry == NULL) { |
| 1158 | if(id_count == SQUASHFS_IDS) |
| 1159 | BAD_ERROR("Out of uids!\n"); |
| 1160 | entry = create_id(uid); |
| 1161 | } |
| 1162 | |
| 1163 | if((entry->flags & ISA_UID) == 0) { |
| 1164 | entry->flags |= ISA_UID; |
| 1165 | uid_count ++; |
| 1166 | } |
| 1167 | |
| 1168 | return entry->index; |
| 1169 | } |
| 1170 | |
| 1171 | |
| 1172 | unsigned int get_guid(unsigned int guid) |
| 1173 | { |
| 1174 | struct id *entry = get_id(guid); |
| 1175 | |
| 1176 | if(entry == NULL) { |
| 1177 | if(id_count == SQUASHFS_IDS) |
| 1178 | BAD_ERROR("Out of gids!\n"); |
| 1179 | entry = create_id(guid); |
| 1180 | } |
| 1181 | |
| 1182 | if((entry->flags & ISA_GID) == 0) { |
| 1183 | entry->flags |= ISA_GID; |
| 1184 | guid_count ++; |
| 1185 | } |
| 1186 | |
| 1187 | return entry->index; |
| 1188 | } |
| 1189 | |
| 1190 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1191 | int create_inode(squashfs_inode *i_no, struct dir_info *dir_info, |
| 1192 | struct dir_ent *dir_ent, int type, long long byte_size, |
| 1193 | long long start_block, unsigned int offset, unsigned int *block_list, |
| 1194 | struct fragment *fragment, struct directory *dir_in, long long sparse) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1195 | { |
| 1196 | struct stat *buf = &dir_ent->inode->buf; |
| 1197 | squashfs_inode_header inode_header; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1198 | squashfs_base_inode_header *base = &inode_header.base; |
| 1199 | void *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1200 | char *filename = dir_ent->pathname; |
| 1201 | int nlink = dir_ent->inode->nlink; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1202 | int inode_number = type == SQUASHFS_DIR_TYPE ? |
| 1203 | dir_ent->inode->inode_number : |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1204 | dir_ent->inode->inode_number + dir_inode_no; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1205 | int xattr = read_xattrs(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1206 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1207 | switch(type) { |
| 1208 | case SQUASHFS_FILE_TYPE: |
| 1209 | if(dir_ent->inode->nlink > 1 || |
| 1210 | byte_size >= (1LL << 32) || |
| 1211 | start_block >= (1LL << 32) || |
| 1212 | sparse || IS_XATTR(xattr)) |
| 1213 | type = SQUASHFS_LREG_TYPE; |
| 1214 | break; |
| 1215 | case SQUASHFS_DIR_TYPE: |
| 1216 | if(dir_info->dir_is_ldir || IS_XATTR(xattr)) |
| 1217 | type = SQUASHFS_LDIR_TYPE; |
| 1218 | break; |
| 1219 | case SQUASHFS_SYMLINK_TYPE: |
| 1220 | if(IS_XATTR(xattr)) |
| 1221 | type = SQUASHFS_LSYMLINK_TYPE; |
| 1222 | break; |
| 1223 | case SQUASHFS_BLKDEV_TYPE: |
| 1224 | if(IS_XATTR(xattr)) |
| 1225 | type = SQUASHFS_LBLKDEV_TYPE; |
| 1226 | break; |
| 1227 | case SQUASHFS_CHRDEV_TYPE: |
| 1228 | if(IS_XATTR(xattr)) |
| 1229 | type = SQUASHFS_LCHRDEV_TYPE; |
| 1230 | break; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1231 | case SQUASHFS_FIFO_TYPE: |
| 1232 | if(IS_XATTR(xattr)) |
| 1233 | type = SQUASHFS_LFIFO_TYPE; |
| 1234 | break; |
| 1235 | case SQUASHFS_SOCKET_TYPE: |
| 1236 | if(IS_XATTR(xattr)) |
| 1237 | type = SQUASHFS_LSOCKET_TYPE; |
| 1238 | break; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1241 | base->mode = SQUASHFS_MODE(buf->st_mode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1242 | base->uid = get_uid((unsigned int) global_uid == -1 ? |
| 1243 | buf->st_uid : global_uid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1244 | base->inode_type = type; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1245 | base->guid = get_guid((unsigned int) global_gid == -1 ? |
| 1246 | buf->st_gid : global_gid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1247 | base->mtime = buf->st_mtime; |
| 1248 | base->inode_number = inode_number; |
| 1249 | |
| 1250 | if(type == SQUASHFS_FILE_TYPE) { |
| 1251 | int i; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1252 | squashfs_reg_inode_header *reg = &inode_header.reg; |
| 1253 | size_t off = offsetof(squashfs_reg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1254 | |
| 1255 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1256 | reg->file_size = byte_size; |
| 1257 | reg->start_block = start_block; |
| 1258 | reg->fragment = fragment->index; |
| 1259 | reg->offset = fragment->offset; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1260 | SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode); |
| 1261 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1262 | TRACE("File inode, file_size %lld, start_block 0x%llx, blocks " |
| 1263 | "%d, fragment %d, offset %d, size %d\n", byte_size, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1264 | start_block, offset, fragment->index, fragment->offset, |
| 1265 | fragment->size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1266 | for(i = 0; i < offset; i++) |
| 1267 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 1268 | } |
| 1269 | else if(type == SQUASHFS_LREG_TYPE) { |
| 1270 | int i; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1271 | squashfs_lreg_inode_header *reg = &inode_header.lreg; |
| 1272 | size_t off = offsetof(squashfs_lreg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1273 | |
| 1274 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1275 | reg->nlink = nlink; |
| 1276 | reg->file_size = byte_size; |
| 1277 | reg->start_block = start_block; |
| 1278 | reg->fragment = fragment->index; |
| 1279 | reg->offset = fragment->offset; |
plougher | f5a674d | 2009-03-25 05:38:27 +0000 | [diff] [blame] | 1280 | if(sparse && sparse >= byte_size) |
| 1281 | sparse = byte_size - 1; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1282 | reg->sparse = sparse; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1283 | reg->xattr = xattr; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1284 | SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode); |
| 1285 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1286 | TRACE("Long file inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1287 | "blocks %d, fragment %d, offset %d, size %d, nlink %d" |
| 1288 | "\n", byte_size, start_block, offset, fragment->index, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1289 | fragment->offset, fragment->size, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1290 | for(i = 0; i < offset; i++) |
| 1291 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 1292 | } |
| 1293 | else if(type == SQUASHFS_LDIR_TYPE) { |
| 1294 | int i; |
| 1295 | unsigned char *p; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1296 | squashfs_ldir_inode_header *dir = &inode_header.ldir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1297 | struct cached_dir_index *index = dir_in->index; |
| 1298 | unsigned int i_count = dir_in->i_count; |
| 1299 | unsigned int i_size = dir_in->i_size; |
| 1300 | |
| 1301 | if(byte_size >= 1 << 27) |
| 1302 | BAD_ERROR("directory greater than 2^27-1 bytes!\n"); |
| 1303 | |
| 1304 | inode = get_inode(sizeof(*dir) + i_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1305 | dir->inode_type = SQUASHFS_LDIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1306 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 1307 | dir->file_size = byte_size; |
| 1308 | dir->offset = offset; |
| 1309 | dir->start_block = start_block; |
| 1310 | dir->i_count = i_count; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1311 | dir->parent_inode = dir_ent->our_dir ? |
| 1312 | dir_ent->our_dir->dir_ent->inode->inode_number : |
| 1313 | dir_inode_no + inode_no; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1314 | dir->xattr = xattr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1315 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1316 | SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode); |
| 1317 | p = inode + offsetof(squashfs_ldir_inode_header, index); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1318 | for(i = 0; i < i_count; i++) { |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1319 | SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p); |
| 1320 | p += offsetof(squashfs_dir_index, name); |
| 1321 | memcpy(p, index[i].name, index[i].index.size + 1); |
| 1322 | p += index[i].index.size + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1323 | } |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1324 | TRACE("Long directory inode, file_size %lld, start_block " |
| 1325 | "0x%llx, offset 0x%x, nlink %d\n", byte_size, |
| 1326 | start_block, offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1327 | } |
| 1328 | else if(type == SQUASHFS_DIR_TYPE) { |
| 1329 | squashfs_dir_inode_header *dir = &inode_header.dir; |
| 1330 | |
| 1331 | inode = get_inode(sizeof(*dir)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1332 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 1333 | dir->file_size = byte_size; |
| 1334 | dir->offset = offset; |
| 1335 | dir->start_block = start_block; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1336 | dir->parent_inode = dir_ent->our_dir ? |
| 1337 | dir_ent->our_dir->dir_ent->inode->inode_number : |
| 1338 | dir_inode_no + inode_no; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1339 | SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1340 | TRACE("Directory inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1341 | "offset 0x%x, nlink %d\n", byte_size, start_block, |
| 1342 | offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1343 | } |
| 1344 | else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) { |
| 1345 | squashfs_dev_inode_header *dev = &inode_header.dev; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1346 | unsigned int major = major(buf->st_rdev); |
| 1347 | unsigned int minor = minor(buf->st_rdev); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1348 | |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1349 | if(major > 0xfff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1350 | ERROR("Major %d out of range in device node %s, " |
| 1351 | "truncating to %d\n", major, filename, |
| 1352 | major & 0xfff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1353 | major &= 0xfff; |
| 1354 | } |
| 1355 | if(minor > 0xfffff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1356 | ERROR("Minor %d out of range in device node %s, " |
| 1357 | "truncating to %d\n", minor, filename, |
| 1358 | minor & 0xfffff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1359 | minor &= 0xfffff; |
| 1360 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1361 | inode = get_inode(sizeof(*dev)); |
| 1362 | dev->nlink = nlink; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1363 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1364 | ((minor & ~0xff) << 12); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1365 | SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1366 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1367 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1368 | else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) { |
| 1369 | squashfs_ldev_inode_header *dev = &inode_header.ldev; |
| 1370 | unsigned int major = major(buf->st_rdev); |
| 1371 | unsigned int minor = minor(buf->st_rdev); |
| 1372 | |
| 1373 | if(major > 0xfff) { |
| 1374 | ERROR("Major %d out of range in device node %s, " |
| 1375 | "truncating to %d\n", major, filename, |
| 1376 | major & 0xfff); |
| 1377 | major &= 0xfff; |
| 1378 | } |
| 1379 | if(minor > 0xfffff) { |
| 1380 | ERROR("Minor %d out of range in device node %s, " |
| 1381 | "truncating to %d\n", minor, filename, |
| 1382 | minor & 0xfffff); |
| 1383 | minor &= 0xfffff; |
| 1384 | } |
| 1385 | inode = get_inode(sizeof(*dev)); |
| 1386 | dev->nlink = nlink; |
| 1387 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1388 | ((minor & ~0xff) << 12); |
| 1389 | dev->xattr = xattr; |
| 1390 | SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode); |
| 1391 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
| 1392 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1393 | else if(type == SQUASHFS_SYMLINK_TYPE) { |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1394 | squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1395 | int byte; |
| 1396 | char buff[65536]; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1397 | size_t off = offsetof(squashfs_symlink_inode_header, symlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1398 | |
| 1399 | if((byte = readlink(filename, buff, 65536)) == -1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1400 | ERROR("Failed to read symlink %s, creating empty " |
| 1401 | "symlink\n", filename); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1402 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | if(byte == 65536) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1406 | ERROR("Symlink %s is greater than 65536 bytes! " |
| 1407 | "Creating empty symlink\n", filename); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1408 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | inode = get_inode(sizeof(*symlink) + byte); |
| 1412 | symlink->nlink = nlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1413 | symlink->symlink_size = byte; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1414 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1415 | strncpy(inode + off, buff, byte); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1416 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1417 | nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1418 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1419 | else if(type == SQUASHFS_LSYMLINK_TYPE) { |
| 1420 | squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
| 1421 | int byte; |
| 1422 | char buff[65536]; |
| 1423 | size_t off = offsetof(squashfs_symlink_inode_header, symlink); |
| 1424 | |
| 1425 | if((byte = readlink(filename, buff, 65536)) == -1) { |
| 1426 | ERROR("Failed to read symlink %s, creating empty " |
| 1427 | "symlink\n", filename); |
| 1428 | byte = 0; |
| 1429 | } |
| 1430 | |
| 1431 | if(byte == 65536) { |
| 1432 | ERROR("Symlink %s is greater than 65536 bytes! " |
| 1433 | "Creating empty symlink\n", filename); |
| 1434 | byte = 0; |
| 1435 | } |
| 1436 | |
| 1437 | inode = get_inode(sizeof(*symlink) + byte + |
| 1438 | sizeof(unsigned int)); |
| 1439 | symlink->nlink = nlink; |
| 1440 | symlink->symlink_size = byte; |
| 1441 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1442 | strncpy(inode + off, buff, byte); |
| 1443 | SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1); |
| 1444 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1445 | nlink); |
| 1446 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1447 | else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) { |
| 1448 | squashfs_ipc_inode_header *ipc = &inode_header.ipc; |
| 1449 | |
| 1450 | inode = get_inode(sizeof(*ipc)); |
| 1451 | ipc->nlink = nlink; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1452 | SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1453 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1454 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1455 | } |
| 1456 | else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) { |
| 1457 | squashfs_lipc_inode_header *ipc = &inode_header.lipc; |
| 1458 | |
| 1459 | inode = get_inode(sizeof(*ipc)); |
| 1460 | ipc->nlink = nlink; |
| 1461 | ipc->xattr = xattr; |
| 1462 | SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode); |
| 1463 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1464 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1465 | } else |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1466 | BAD_ERROR("Unrecognised inode %d in create_inode\n", type); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1467 | |
| 1468 | *i_no = MKINODE(inode); |
| 1469 | inode_count ++; |
| 1470 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1471 | TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type, |
| 1472 | base->uid, base->guid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1473 | |
| 1474 | return TRUE; |
| 1475 | } |
| 1476 | |
| 1477 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 1478 | void scan3_init_dir(struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1479 | { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 1480 | if((dir->buff = malloc(SQUASHFS_METADATA_SIZE)) == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1481 | BAD_ERROR("Out of memory allocating directory buffer\n"); |
| 1482 | } |
| 1483 | |
| 1484 | dir->size = SQUASHFS_METADATA_SIZE; |
| 1485 | dir->p = dir->index_count_p = dir->buff; |
| 1486 | dir->entry_count = 256; |
| 1487 | dir->entry_count_p = NULL; |
| 1488 | dir->index = NULL; |
| 1489 | dir->i_count = dir->i_size = 0; |
| 1490 | } |
| 1491 | |
| 1492 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1493 | void add_dir(squashfs_inode inode, unsigned int inode_number, char *name, |
| 1494 | int type, struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1495 | { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 1496 | unsigned char *buff; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1497 | squashfs_dir_entry idir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1498 | unsigned int start_block = inode >> 16; |
| 1499 | unsigned int offset = inode & 0xffff; |
| 1500 | unsigned int size; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1501 | size_t name_off = offsetof(squashfs_dir_entry, name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1502 | |
| 1503 | if((size = strlen(name)) > SQUASHFS_NAME_LEN) { |
| 1504 | size = SQUASHFS_NAME_LEN; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1505 | ERROR("Filename is greater than %d characters, truncating! ..." |
| 1506 | "\n", SQUASHFS_NAME_LEN); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1509 | if(dir->p + sizeof(squashfs_dir_entry) + size + |
| 1510 | sizeof(squashfs_dir_header) >= dir->buff + dir->size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1511 | buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE); |
| 1512 | if(buff == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1513 | BAD_ERROR("Out of memory reallocating directory buffer" |
| 1514 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | dir->p = (dir->p - dir->buff) + buff; |
| 1518 | if(dir->entry_count_p) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1519 | dir->entry_count_p = (dir->entry_count_p - dir->buff + |
| 1520 | buff); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1521 | dir->index_count_p = dir->index_count_p - dir->buff + buff; |
| 1522 | dir->buff = buff; |
| 1523 | } |
| 1524 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1525 | if(dir->entry_count == 256 || start_block != dir->start_block || |
| 1526 | ((dir->entry_count_p != NULL) && |
| 1527 | ((dir->p + sizeof(squashfs_dir_entry) + size - |
| 1528 | dir->index_count_p) > SQUASHFS_METADATA_SIZE)) || |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1529 | ((long long) inode_number - dir->inode_number) > 32767 |
| 1530 | || ((long long) inode_number - dir->inode_number) |
| 1531 | < -32768) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1532 | if(dir->entry_count_p) { |
| 1533 | squashfs_dir_header dir_header; |
| 1534 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1535 | if((dir->p + sizeof(squashfs_dir_entry) + size - |
| 1536 | dir->index_count_p) > |
| 1537 | SQUASHFS_METADATA_SIZE) { |
| 1538 | if(dir->i_count % I_COUNT_SIZE == 0) { |
| 1539 | dir->index = realloc(dir->index, |
| 1540 | (dir->i_count + I_COUNT_SIZE) * |
| 1541 | sizeof(struct cached_dir_index)); |
| 1542 | if(dir->index == NULL) |
| 1543 | BAD_ERROR("Out of memory in " |
| 1544 | "directory index table " |
| 1545 | "reallocation!\n"); |
| 1546 | } |
| 1547 | dir->index[dir->i_count].index.index = |
| 1548 | dir->p - dir->buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1549 | dir->index[dir->i_count].index.size = size - 1; |
| 1550 | dir->index[dir->i_count++].name = name; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1551 | dir->i_size += sizeof(squashfs_dir_index) + |
| 1552 | size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1553 | dir->index_count_p = dir->p; |
| 1554 | } |
| 1555 | |
| 1556 | dir_header.count = dir->entry_count - 1; |
| 1557 | dir_header.start_block = dir->start_block; |
| 1558 | dir_header.inode_number = dir->inode_number; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1559 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1560 | dir->entry_count_p); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1561 | |
| 1562 | } |
| 1563 | |
| 1564 | |
| 1565 | dir->entry_count_p = dir->p; |
| 1566 | dir->start_block = start_block; |
| 1567 | dir->entry_count = 0; |
| 1568 | dir->inode_number = inode_number; |
| 1569 | dir->p += sizeof(squashfs_dir_header); |
| 1570 | } |
| 1571 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1572 | idir.offset = offset; |
| 1573 | idir.type = type; |
| 1574 | idir.size = size - 1; |
| 1575 | idir.inode_number = ((long long) inode_number - dir->inode_number); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1576 | SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p); |
| 1577 | strncpy((char *) dir->p + name_off, name, size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1578 | dir->p += sizeof(squashfs_dir_entry) + size; |
| 1579 | dir->entry_count ++; |
| 1580 | } |
| 1581 | |
| 1582 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1583 | void write_dir(squashfs_inode *inode, struct dir_info *dir_info, |
| 1584 | struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1585 | { |
| 1586 | unsigned int dir_size = dir->p - dir->buff; |
| 1587 | int data_space = (directory_cache_size - directory_cache_bytes); |
| 1588 | unsigned int directory_block, directory_offset, i_count, index; |
| 1589 | unsigned short c_byte; |
| 1590 | |
| 1591 | if(data_space < dir_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1592 | int realloc_size = directory_cache_size == 0 ? |
| 1593 | ((dir_size + SQUASHFS_METADATA_SIZE) & |
| 1594 | ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1595 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1596 | void *dc = realloc(directory_data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1597 | directory_cache_size + realloc_size); |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1598 | if(dc == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1599 | goto failed; |
| 1600 | } |
| 1601 | directory_cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1602 | directory_data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | if(dir_size) { |
| 1606 | squashfs_dir_header dir_header; |
| 1607 | |
| 1608 | dir_header.count = dir->entry_count - 1; |
| 1609 | dir_header.start_block = dir->start_block; |
| 1610 | dir_header.inode_number = dir->inode_number; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1611 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1612 | memcpy(directory_data_cache + directory_cache_bytes, dir->buff, |
| 1613 | dir_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1614 | } |
| 1615 | directory_offset = directory_cache_bytes; |
| 1616 | directory_block = directory_bytes; |
| 1617 | directory_cache_bytes += dir_size; |
| 1618 | i_count = 0; |
| 1619 | index = SQUASHFS_METADATA_SIZE - directory_offset; |
| 1620 | |
| 1621 | while(1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1622 | while(i_count < dir->i_count && |
| 1623 | dir->index[i_count].index.index < index) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1624 | dir->index[i_count++].index.start_block = |
| 1625 | directory_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1626 | index += SQUASHFS_METADATA_SIZE; |
| 1627 | |
| 1628 | if(directory_cache_bytes < SQUASHFS_METADATA_SIZE) |
| 1629 | break; |
| 1630 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1631 | if((directory_size - directory_bytes) < |
| 1632 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1633 | void *dt = realloc(directory_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1634 | directory_size + (SQUASHFS_METADATA_SIZE << 1) |
| 1635 | + 2); |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1636 | if(dt == NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1637 | goto failed; |
| 1638 | } |
| 1639 | directory_size += SQUASHFS_METADATA_SIZE << 1; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1640 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1643 | c_byte = mangle(directory_table + directory_bytes + |
| 1644 | BLOCK_OFFSET, directory_data_cache, |
| 1645 | SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, |
| 1646 | noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1647 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1648 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1649 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1650 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1651 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1652 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1653 | total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1654 | memcpy(directory_data_cache, directory_data_cache + |
| 1655 | SQUASHFS_METADATA_SIZE, directory_cache_bytes - |
| 1656 | SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1657 | directory_cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 1658 | } |
| 1659 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1660 | create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE, |
| 1661 | dir_size + 3, directory_block, directory_offset, NULL, NULL, |
| 1662 | dir, 0); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1663 | |
| 1664 | #ifdef SQUASHFS_TRACE |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1665 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1666 | unsigned char *dirp; |
| 1667 | int count; |
| 1668 | |
| 1669 | TRACE("Directory contents of inode 0x%llx\n", *inode); |
| 1670 | dirp = dir->buff; |
| 1671 | while(dirp < dir->p) { |
| 1672 | char buffer[SQUASHFS_NAME_LEN + 1]; |
| 1673 | squashfs_dir_entry idir, *idirp; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1674 | squashfs_dir_header dirh; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1675 | SQUASHFS_SWAP_DIR_HEADER((squashfs_dir_header *) dirp, |
| 1676 | &dirh); |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1677 | count = dirh.count + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1678 | dirp += sizeof(squashfs_dir_header); |
| 1679 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1680 | TRACE("\tStart block 0x%x, count %d\n", |
| 1681 | dirh.start_block, count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1682 | |
| 1683 | while(count--) { |
| 1684 | idirp = (squashfs_dir_entry *) dirp; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1685 | SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1686 | strncpy(buffer, idirp->name, idir.size + 1); |
| 1687 | buffer[idir.size + 1] = '\0'; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1688 | TRACE("\t\tname %s, inode offset 0x%x, type " |
| 1689 | "%d\n", buffer, idir.offset, idir.type); |
| 1690 | dirp += sizeof(squashfs_dir_entry) + idir.size + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | #endif |
| 1695 | dir_count ++; |
| 1696 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1697 | return; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1698 | |
| 1699 | failed: |
| 1700 | BAD_ERROR("Out of memory in directory table reallocation!\n"); |
| 1701 | } |
| 1702 | |
| 1703 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1704 | struct file_buffer *get_fragment(struct fragment *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1705 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1706 | squashfs_fragment_entry *disk_fragment; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1707 | int res, size; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1708 | long long start_block; |
| 1709 | struct file_buffer *buffer, *compressed_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1710 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1711 | if(fragment->index == SQUASHFS_INVALID_FRAG) |
| 1712 | return NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1713 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1714 | buffer = cache_lookup(fragment_buffer, fragment->index); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1715 | if(buffer) |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1716 | return buffer; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1717 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1718 | compressed_buffer = cache_lookup(writer_buffer, fragment->index + |
| 1719 | FRAG_INDEX); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1720 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1721 | buffer = cache_get(fragment_buffer, fragment->index, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1722 | |
| 1723 | pthread_mutex_lock(&fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1724 | disk_fragment = &fragment_table[fragment->index]; |
| 1725 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1726 | start_block = disk_fragment->start_block; |
| 1727 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1728 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1729 | if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1730 | int error; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1731 | char *data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1732 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1733 | if(compressed_buffer) |
| 1734 | data = compressed_buffer->data; |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 1735 | else |
| 1736 | data = read_from_disk(start_block, size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1737 | |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 1738 | res = comp->uncompress(buffer->data, data, size, block_size, |
| 1739 | &error); |
| 1740 | if(res == -1) |
| 1741 | BAD_ERROR("%s uncompress failed with error code %d\n", |
| 1742 | comp->name, error); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1743 | } else if(compressed_buffer) |
| 1744 | memcpy(buffer->data, compressed_buffer->data, size); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1745 | else { |
| 1746 | res = read_fs_bytes(fd, start_block, size, buffer->data); |
| 1747 | if(res == 0) |
| 1748 | EXIT_MKSQUASHFS(); |
| 1749 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1750 | |
plougher | 03859fa | 2009-03-31 03:18:18 +0000 | [diff] [blame] | 1751 | cache_block_put(compressed_buffer); |
| 1752 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1753 | return buffer; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1756 | |
| 1757 | struct frag_locked { |
| 1758 | struct file_buffer *buffer; |
| 1759 | int c_byte; |
| 1760 | int fragment; |
| 1761 | struct frag_locked *fragment_prev; |
| 1762 | struct frag_locked *fragment_next; |
| 1763 | }; |
| 1764 | |
| 1765 | int fragments_locked = FALSE; |
| 1766 | struct frag_locked *frag_locked_list = NULL; |
| 1767 | |
| 1768 | INSERT_LIST(fragment, struct frag_locked) |
| 1769 | REMOVE_LIST(fragment, struct frag_locked) |
| 1770 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1771 | int lock_fragments() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1772 | { |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1773 | int count; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1774 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1775 | fragments_locked = TRUE; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1776 | count = fragments_outstanding; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1777 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 1778 | return count; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | |
| 1782 | void unlock_fragments() |
| 1783 | { |
| 1784 | struct frag_locked *entry; |
| 1785 | int compressed_size; |
| 1786 | |
| 1787 | pthread_mutex_lock(&fragment_mutex); |
| 1788 | while(frag_locked_list) { |
| 1789 | entry = frag_locked_list; |
| 1790 | remove_fragment_list(&frag_locked_list, entry); |
| 1791 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(entry->c_byte); |
| 1792 | fragment_table[entry->fragment].size = entry->c_byte; |
| 1793 | fragment_table[entry->fragment].start_block = bytes; |
| 1794 | entry->buffer->block = bytes; |
| 1795 | bytes += compressed_size; |
| 1796 | fragments_outstanding --; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1797 | queue_put(to_writer, entry->buffer); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1798 | TRACE("fragment_locked writing fragment %d, compressed size %d" |
| 1799 | "\n", entry->fragment, compressed_size); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1800 | free(entry); |
| 1801 | } |
| 1802 | fragments_locked = FALSE; |
| 1803 | pthread_mutex_unlock(&fragment_mutex); |
| 1804 | } |
| 1805 | |
| 1806 | |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1807 | void add_pending_fragment(struct file_buffer *write_buffer, int c_byte, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1808 | int fragment) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1809 | { |
| 1810 | struct frag_locked *entry = malloc(sizeof(struct frag_locked)); |
| 1811 | if(entry == NULL) |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1812 | BAD_ERROR("Out of memory in add_pending fragment\n"); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1813 | entry->buffer = write_buffer; |
| 1814 | entry->c_byte = c_byte; |
| 1815 | entry->fragment = fragment; |
| 1816 | entry->fragment_prev = entry->fragment_next = NULL; |
| 1817 | pthread_mutex_lock(&fragment_mutex); |
| 1818 | insert_fragment_list(&frag_locked_list, entry); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1819 | pthread_mutex_unlock(&fragment_mutex); |
| 1820 | } |
| 1821 | |
| 1822 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1823 | void write_fragment() |
| 1824 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1825 | if(fragment_size == 0) |
| 1826 | return; |
| 1827 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1828 | pthread_mutex_lock(&fragment_mutex); |
| 1829 | if(fragments % FRAG_SIZE == 0) { |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1830 | void *ft = realloc(fragment_table, (fragments + |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1831 | FRAG_SIZE) * sizeof(squashfs_fragment_entry)); |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1832 | if(ft == NULL) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1833 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1834 | BAD_ERROR("Out of memory in fragment table\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1835 | } |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1836 | fragment_table = ft; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1837 | } |
| 1838 | fragment_data->size = fragment_size; |
| 1839 | fragment_data->block = fragments; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1840 | fragment_table[fragments].unused = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1841 | fragments_outstanding ++; |
| 1842 | queue_put(to_frag, fragment_data); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1843 | fragments ++; |
| 1844 | fragment_size = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1845 | pthread_mutex_unlock(&fragment_mutex); |
| 1846 | } |
| 1847 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 1848 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1849 | static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0}; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1850 | struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1851 | { |
| 1852 | struct fragment *ffrg; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1853 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1854 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1855 | if(file_buffer == NULL || file_buffer->size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1856 | return &empty_fragment; |
| 1857 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1858 | if(fragment_size + file_buffer->size > block_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1859 | write_fragment(); |
| 1860 | |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 1861 | if((ffrg = malloc(sizeof(struct fragment))) == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1862 | BAD_ERROR("Out of memory in fragment block allocation!\n"); |
| 1863 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1864 | if(fragment_size == 0) |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1865 | fragment_data = cache_get(fragment_buffer, fragments, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1866 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1867 | ffrg->index = fragments; |
| 1868 | ffrg->offset = fragment_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1869 | ffrg->size = file_buffer->size; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1870 | memcpy(fragment_data->data + fragment_size, file_buffer->data, |
| 1871 | file_buffer->size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1872 | fragment_size += file_buffer->size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1873 | |
| 1874 | return ffrg; |
| 1875 | } |
| 1876 | |
| 1877 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1878 | long long generic_write_table(int length, char *buffer, int length2, |
| 1879 | char *buffer2, int uncompressed) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1880 | { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1881 | int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) / |
| 1882 | SQUASHFS_METADATA_SIZE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1883 | long long list[meta_blocks], start_bytes; |
plougher | badfac6 | 2007-11-12 03:29:41 +0000 | [diff] [blame] | 1884 | int compressed_size, i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1885 | unsigned short c_byte; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1886 | char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2]; |
| 1887 | |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1888 | #ifdef SQUASHFS_TRACE |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1889 | long long obytes = bytes; |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1890 | int olength = length; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1891 | #endif |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1892 | |
| 1893 | for(i = 0; i < meta_blocks; i++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1894 | int avail_bytes = length > SQUASHFS_METADATA_SIZE ? |
| 1895 | SQUASHFS_METADATA_SIZE : length; |
| 1896 | c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i * |
| 1897 | SQUASHFS_METADATA_SIZE , avail_bytes, |
| 1898 | SQUASHFS_METADATA_SIZE, uncompressed, 0); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1899 | SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1900 | list[i] = bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1901 | compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1902 | BLOCK_OFFSET; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1903 | TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes, |
| 1904 | compressed_size); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1905 | write_destination(fd, bytes, compressed_size, cbuffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1906 | bytes += compressed_size; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1907 | total_bytes += avail_bytes; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1908 | length -= avail_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1911 | start_bytes = bytes; |
| 1912 | if(length2) { |
| 1913 | write_destination(fd, bytes, length2, (char *) buffer2); |
| 1914 | bytes += length2; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1915 | total_bytes += length2; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1918 | SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1919 | write_destination(fd, bytes, sizeof(list), (char *) list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1920 | bytes += sizeof(list); |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1921 | total_bytes += sizeof(list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1922 | |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1923 | TRACE("generic_write_table: total uncompressed %d compressed %lld\n", |
| 1924 | olength, bytes - obytes); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1925 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1926 | return start_bytes; |
| 1927 | } |
| 1928 | |
| 1929 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1930 | long long write_fragment_table() |
| 1931 | { |
| 1932 | unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1933 | squashfs_fragment_entry p[fragments]; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1934 | int i; |
| 1935 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1936 | TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments, |
| 1937 | frag_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1938 | for(i = 0; i < fragments; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1939 | TRACE("write_fragment_table: fragment %d, start_block 0x%llx, " |
| 1940 | "size %d\n", i, fragment_table[i].start_block, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1941 | fragment_table[i].size); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1942 | SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_table[i], p + i); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1945 | return generic_write_table(frag_bytes, (char *) p, 0, NULL, noF); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1949 | char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1950 | char *read_from_disk(long long start, unsigned int avail_bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1951 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1952 | int res; |
| 1953 | |
| 1954 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer); |
| 1955 | if(res == 0) |
| 1956 | EXIT_MKSQUASHFS(); |
| 1957 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1958 | return read_from_file_buffer; |
| 1959 | } |
| 1960 | |
| 1961 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1962 | char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE]; |
| 1963 | char *read_from_disk2(long long start, unsigned int avail_bytes) |
| 1964 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1965 | int res; |
| 1966 | |
| 1967 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2); |
| 1968 | if(res == 0) |
| 1969 | EXIT_MKSQUASHFS(); |
| 1970 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1971 | return read_from_file_buffer2; |
| 1972 | } |
| 1973 | |
| 1974 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1975 | /* |
| 1976 | * Compute 16 bit BSD checksum over the data |
| 1977 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1978 | unsigned short get_checksum(char *buff, int bytes, unsigned short chksum) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1979 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1980 | unsigned char *b = (unsigned char *) buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1981 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1982 | while(bytes --) { |
| 1983 | chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1; |
| 1984 | chksum += *b++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | return chksum; |
| 1988 | } |
| 1989 | |
| 1990 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1991 | unsigned short get_checksum_disk(long long start, long long l, |
| 1992 | unsigned int *blocks) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1993 | { |
| 1994 | unsigned short chksum = 0; |
| 1995 | unsigned int bytes; |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1996 | struct file_buffer *write_buffer; |
| 1997 | int i; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1998 | |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1999 | for(i = 0; l; i++) { |
| 2000 | bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]); |
| 2001 | if(bytes == 0) /* sparse block */ |
| 2002 | continue; |
| 2003 | write_buffer = cache_lookup(writer_buffer, start); |
| 2004 | if(write_buffer) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2005 | chksum = get_checksum(write_buffer->data, bytes, |
| 2006 | chksum); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 2007 | cache_block_put(write_buffer); |
| 2008 | } else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2009 | chksum = get_checksum(read_from_disk(start, bytes), |
| 2010 | bytes, chksum); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2011 | l -= bytes; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2012 | start += bytes; |
| 2013 | } |
| 2014 | |
| 2015 | return chksum; |
| 2016 | } |
| 2017 | |
| 2018 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2019 | unsigned short get_checksum_mem(char *buff, int bytes) |
| 2020 | { |
| 2021 | return get_checksum(buff, bytes, 0); |
| 2022 | } |
| 2023 | |
| 2024 | |
| 2025 | unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer) |
| 2026 | { |
| 2027 | if(file_buffer == NULL) |
| 2028 | return 0; |
| 2029 | else |
| 2030 | return get_checksum(file_buffer->data, file_buffer->size, 0); |
| 2031 | } |
| 2032 | |
| 2033 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2034 | #define DUP_HASH(a) (a & 0xffff) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2035 | 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] | 2036 | unsigned int *block_listp, int blocks, unsigned int fragment, |
| 2037 | int offset, int bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2038 | { |
| 2039 | struct fragment *frg; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 2040 | unsigned int *block_list = block_listp; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2041 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 2042 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2043 | if(!duplicate_checking || file_size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2044 | return; |
| 2045 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2046 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) { |
| 2047 | if(file_size != dupl_ptr->file_size) |
| 2048 | continue; |
| 2049 | if(blocks != 0 && start != dupl_ptr->start) |
| 2050 | continue; |
| 2051 | if(fragment != dupl_ptr->fragment->index) |
| 2052 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2053 | if(fragment != SQUASHFS_INVALID_FRAG && (offset != |
| 2054 | dupl_ptr->fragment->offset || bytes != |
| 2055 | dupl_ptr->fragment->size)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2056 | continue; |
| 2057 | return; |
| 2058 | } |
| 2059 | |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 2060 | if((frg = malloc(sizeof(struct fragment))) == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2061 | BAD_ERROR("Out of memory in fragment block allocation!\n"); |
| 2062 | |
| 2063 | frg->index = fragment; |
| 2064 | frg->offset = offset; |
| 2065 | frg->size = bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2066 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2067 | add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0, FALSE); |
| 2068 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2069 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2070 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2071 | int pre_duplicate(long long file_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2072 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2073 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2074 | |
| 2075 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2076 | if(dupl_ptr->file_size == file_size) |
| 2077 | return TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2078 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2079 | return FALSE; |
| 2080 | } |
| 2081 | |
| 2082 | |
| 2083 | int pre_duplicate_frag(long long file_size, unsigned short checksum) |
| 2084 | { |
| 2085 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 2086 | |
| 2087 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2088 | if(file_size == dupl_ptr->file_size && file_size == |
| 2089 | dupl_ptr->fragment->size) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2090 | if(dupl_ptr->checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2091 | struct file_buffer *frag_buffer = |
| 2092 | get_fragment(dupl_ptr->fragment); |
| 2093 | dupl_ptr->checksum = |
| 2094 | get_checksum_disk(dupl_ptr->start, |
| 2095 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 2096 | dupl_ptr->fragment_checksum = |
| 2097 | get_checksum_mem(frag_buffer->data + |
| 2098 | dupl_ptr->fragment->offset, file_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2099 | cache_block_put(frag_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2100 | dupl_ptr->checksum_flag = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2101 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2102 | if(dupl_ptr->fragment_checksum == checksum) |
| 2103 | return TRUE; |
| 2104 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2105 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2106 | return FALSE; |
| 2107 | } |
| 2108 | |
| 2109 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2110 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 2111 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 2112 | unsigned short checksum, unsigned short fragment_checksum, |
| 2113 | int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2114 | { |
| 2115 | struct file_info *dupl_ptr; |
| 2116 | |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 2117 | if((dupl_ptr = malloc(sizeof(struct file_info))) == NULL) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2118 | BAD_ERROR("Out of memory in dup_files allocation!\n"); |
| 2119 | } |
| 2120 | |
| 2121 | dupl_ptr->file_size = file_size; |
| 2122 | dupl_ptr->bytes = bytes; |
| 2123 | dupl_ptr->block_list = block_list; |
| 2124 | dupl_ptr->start = start; |
| 2125 | dupl_ptr->fragment = fragment; |
| 2126 | dupl_ptr->checksum = checksum; |
| 2127 | dupl_ptr->fragment_checksum = fragment_checksum; |
| 2128 | dupl_ptr->checksum_flag = checksum_flag; |
| 2129 | dupl_ptr->next = dupl[DUP_HASH(file_size)]; |
| 2130 | dupl[DUP_HASH(file_size)] = dupl_ptr; |
| 2131 | dup_files ++; |
| 2132 | |
| 2133 | return dupl_ptr; |
| 2134 | } |
| 2135 | |
| 2136 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2137 | struct file_info *duplicate(long long file_size, long long bytes, |
| 2138 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 2139 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
| 2140 | unsigned short fragment_checksum, int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2141 | { |
| 2142 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 2143 | int frag_bytes = file_buffer ? file_buffer->size : 0; |
| 2144 | |
| 2145 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2146 | if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes |
| 2147 | && frag_bytes == dupl_ptr->fragment->size) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2148 | long long target_start, dup_start = dupl_ptr->start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2149 | int block; |
| 2150 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2151 | if(memcmp(*block_list, dupl_ptr->block_list, blocks * |
| 2152 | sizeof(unsigned int)) != 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 2153 | continue; |
| 2154 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2155 | if(checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2156 | checksum = get_checksum_disk(*start, bytes, |
| 2157 | *block_list); |
| 2158 | fragment_checksum = |
| 2159 | get_checksum_mem_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2160 | checksum_flag = TRUE; |
| 2161 | } |
| 2162 | |
| 2163 | if(dupl_ptr->checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2164 | struct file_buffer *frag_buffer = |
| 2165 | get_fragment(dupl_ptr->fragment); |
| 2166 | dupl_ptr->checksum = |
| 2167 | get_checksum_disk(dupl_ptr->start, |
| 2168 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 2169 | dupl_ptr->fragment_checksum = |
| 2170 | get_checksum_mem(frag_buffer->data + |
| 2171 | dupl_ptr->fragment->offset, frag_bytes); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2172 | cache_block_put(frag_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2173 | dupl_ptr->checksum_flag = TRUE; |
| 2174 | } |
| 2175 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2176 | if(checksum != dupl_ptr->checksum || |
| 2177 | fragment_checksum != |
| 2178 | dupl_ptr->fragment_checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2179 | continue; |
| 2180 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2181 | target_start = *start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2182 | for(block = 0; block < blocks; block ++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2183 | int size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2184 | ((*block_list)[block]); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2185 | struct file_buffer *target_buffer = NULL; |
| 2186 | struct file_buffer *dup_buffer = NULL; |
| 2187 | char *target_data, *dup_data; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2188 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2189 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2190 | if(size == 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 2191 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2192 | target_buffer = cache_lookup(writer_buffer, |
| 2193 | target_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2194 | if(target_buffer) |
| 2195 | target_data = target_buffer->data; |
| 2196 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2197 | target_data = |
| 2198 | read_from_disk(target_start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2199 | size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2200 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2201 | dup_buffer = cache_lookup(writer_buffer, |
| 2202 | dup_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2203 | if(dup_buffer) |
| 2204 | dup_data = dup_buffer->data; |
| 2205 | else |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2206 | dup_data = read_from_disk2(dup_start, |
| 2207 | size); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2208 | |
| 2209 | res = memcmp(target_data, dup_data, size); |
| 2210 | cache_block_put(target_buffer); |
| 2211 | cache_block_put(dup_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2212 | if(res != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2213 | break; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2214 | target_start += size; |
| 2215 | dup_start += size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2216 | } |
| 2217 | if(block == blocks) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2218 | struct file_buffer *frag_buffer = |
| 2219 | get_fragment(dupl_ptr->fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2220 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2221 | if(frag_bytes == 0 || |
| 2222 | memcmp(file_buffer->data, |
| 2223 | frag_buffer->data + |
| 2224 | dupl_ptr->fragment->offset, |
| 2225 | frag_bytes) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2226 | TRACE("Found duplicate file, start " |
| 2227 | "0x%llx, size %lld, checksum " |
| 2228 | "0x%x, fragment %d, size %d, " |
| 2229 | "offset %d, checksum 0x%x\n", |
| 2230 | dupl_ptr->start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2231 | dupl_ptr->bytes, |
| 2232 | dupl_ptr->checksum, |
| 2233 | dupl_ptr->fragment->index, |
| 2234 | frag_bytes, |
| 2235 | dupl_ptr->fragment->offset, |
| 2236 | fragment_checksum); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2237 | *block_list = dupl_ptr->block_list; |
| 2238 | *start = dupl_ptr->start; |
| 2239 | *fragment = dupl_ptr->fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2240 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2241 | return 0; |
| 2242 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2243 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2248 | return add_non_dup(file_size, bytes, *block_list, *start, *fragment, |
| 2249 | checksum, fragment_checksum, checksum_flag); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2253 | static int seq = 0; |
| 2254 | void reader_read_process(struct dir_ent *dir_ent) |
| 2255 | { |
| 2256 | struct file_buffer *prev_buffer = NULL, *file_buffer; |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2257 | int status, res, byte, count = 0; |
| 2258 | int file = get_pseudo_file(dir_ent->inode->pseudo_id)->fd; |
| 2259 | int child = get_pseudo_file(dir_ent->inode->pseudo_id)->child; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2260 | long long bytes = 0; |
| 2261 | |
| 2262 | while(1) { |
| 2263 | file_buffer = cache_get(reader_buffer, 0, 0); |
| 2264 | file_buffer->sequence = seq ++; |
| 2265 | |
| 2266 | byte = read_bytes(file, file_buffer->data, block_size); |
| 2267 | if(byte == -1) |
| 2268 | goto read_err; |
| 2269 | |
| 2270 | file_buffer->size = byte; |
| 2271 | file_buffer->file_size = -1; |
| 2272 | file_buffer->block = count ++; |
| 2273 | file_buffer->error = FALSE; |
| 2274 | file_buffer->fragment = FALSE; |
| 2275 | bytes += byte; |
| 2276 | |
| 2277 | if(byte == 0) |
| 2278 | break; |
| 2279 | |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2280 | /* |
| 2281 | * Update estimated_uncompressed block count. This is done |
| 2282 | * on every block rather than waiting for all blocks to be |
| 2283 | * read incase write_file_process() is running in parallel |
| 2284 | * with this. Otherwise cur uncompressed block count may |
| 2285 | * get ahead of the total uncompressed block count. |
| 2286 | */ |
| 2287 | estimated_uncompressed ++; |
| 2288 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2289 | if(prev_buffer) |
| 2290 | queue_put(from_reader, prev_buffer); |
| 2291 | prev_buffer = file_buffer; |
| 2292 | } |
| 2293 | |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2294 | /* |
| 2295 | * Update inode file size now that the size of the dynamic pseudo file |
| 2296 | * is known. This is needed for the -info option. |
| 2297 | */ |
| 2298 | dir_ent->inode->buf.st_size = bytes; |
| 2299 | |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2300 | res = waitpid(child, &status, 0); |
plougher | d87d8d1 | 2009-09-10 04:08:16 +0000 | [diff] [blame] | 2301 | if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2302 | goto read_err; |
| 2303 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2304 | if(prev_buffer == NULL) |
| 2305 | prev_buffer = file_buffer; |
| 2306 | else { |
| 2307 | cache_block_put(file_buffer); |
| 2308 | seq --; |
| 2309 | } |
| 2310 | prev_buffer->file_size = bytes; |
| 2311 | prev_buffer->fragment = !no_fragments && |
| 2312 | (count == 2 || always_use_fragments) && (byte < block_size); |
| 2313 | queue_put(from_reader, prev_buffer); |
| 2314 | |
| 2315 | return; |
| 2316 | |
| 2317 | read_err: |
| 2318 | if(prev_buffer) { |
| 2319 | cache_block_put(file_buffer); |
| 2320 | seq --; |
| 2321 | file_buffer = prev_buffer; |
| 2322 | } |
| 2323 | file_buffer->error = TRUE; |
| 2324 | queue_put(from_deflate, file_buffer); |
| 2325 | } |
| 2326 | |
| 2327 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2328 | void reader_read_file(struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2329 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2330 | struct stat *buf = &dir_ent->inode->buf, buf2; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2331 | struct file_buffer *file_buffer; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2332 | int blocks, byte, count, expected, file, frag_block; |
| 2333 | long long bytes, read_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2334 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2335 | if(dir_ent->inode->read) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2336 | return; |
| 2337 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2338 | dir_ent->inode->read = TRUE; |
| 2339 | again: |
| 2340 | bytes = 0; |
| 2341 | count = 0; |
| 2342 | file_buffer = NULL; |
| 2343 | read_size = buf->st_size; |
| 2344 | blocks = (read_size + block_size - 1) >> block_log; |
| 2345 | frag_block = !no_fragments && (always_use_fragments || |
| 2346 | (read_size < block_size)) ? read_size >> block_log : -1; |
| 2347 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2348 | if((file = open(dir_ent->pathname, O_RDONLY)) == -1) |
| 2349 | goto read_err; |
| 2350 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2351 | do { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2352 | expected = read_size - ((long long) count * block_size) > |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2353 | block_size ? block_size : |
| 2354 | read_size - ((long long) count * block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2355 | |
| 2356 | if(file_buffer) |
| 2357 | queue_put(from_reader, file_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2358 | file_buffer = cache_get(reader_buffer, 0, 0); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2359 | file_buffer->sequence = seq ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2360 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2361 | byte = file_buffer->size = read_bytes(file, file_buffer->data, |
| 2362 | block_size); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2363 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2364 | file_buffer->file_size = read_size; |
| 2365 | |
| 2366 | if(byte != expected) |
| 2367 | goto restat; |
| 2368 | |
| 2369 | file_buffer->block = count; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2370 | file_buffer->error = FALSE; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2371 | file_buffer->fragment = (file_buffer->block == frag_block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2372 | |
| 2373 | bytes += byte; |
| 2374 | count ++; |
| 2375 | } while(count < blocks); |
| 2376 | |
| 2377 | if(read_size != bytes) |
| 2378 | goto restat; |
| 2379 | |
| 2380 | if(expected == block_size) { |
| 2381 | char buffer; |
| 2382 | |
plougher | 1179b5f | 2009-03-30 00:33:19 +0000 | [diff] [blame] | 2383 | if(read_bytes(file, &buffer, 1) == 1) |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2384 | goto restat; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2387 | queue_put(from_reader, file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2388 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2389 | close(file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2390 | |
| 2391 | return; |
| 2392 | |
| 2393 | read_err: |
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 ++; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2396 | read_err2: |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2397 | file_buffer->error = TRUE; |
| 2398 | queue_put(from_deflate, file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2399 | return; |
| 2400 | restat: |
| 2401 | fstat(file, &buf2); |
| 2402 | close(file); |
| 2403 | if(read_size != buf2.st_size) { |
| 2404 | memcpy(buf, &buf2, sizeof(struct stat)); |
| 2405 | file_buffer->error = 2; |
| 2406 | queue_put(from_deflate, file_buffer); |
| 2407 | goto again; |
| 2408 | } |
| 2409 | goto read_err2; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | void reader_scan(struct dir_info *dir) { |
| 2414 | int i; |
| 2415 | |
| 2416 | for(i = 0; i < dir->count; i++) { |
| 2417 | struct dir_ent *dir_ent = dir->list[i]; |
| 2418 | struct stat *buf = &dir_ent->inode->buf; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2419 | if(dir_ent->inode->root_entry) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2420 | continue; |
| 2421 | |
plougher | b3977eb | 2010-05-02 02:08:48 +0000 | [diff] [blame] | 2422 | if(IS_PSEUDO_PROCESS(dir_ent->inode)) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2423 | reader_read_process(dir_ent); |
| 2424 | continue; |
| 2425 | } |
| 2426 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2427 | switch(buf->st_mode & S_IFMT) { |
| 2428 | case S_IFREG: |
| 2429 | reader_read_file(dir_ent); |
| 2430 | break; |
| 2431 | case S_IFDIR: |
| 2432 | reader_scan(dir_ent->dir); |
| 2433 | break; |
| 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | |
| 2439 | void *reader(void *arg) |
| 2440 | { |
| 2441 | int oldstate; |
| 2442 | |
| 2443 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2444 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2445 | |
| 2446 | if(!sorted) |
| 2447 | reader_scan(queue_get(to_reader)); |
| 2448 | else { |
| 2449 | int i; |
| 2450 | struct priority_entry *entry; |
| 2451 | |
| 2452 | queue_get(to_reader); |
| 2453 | for(i = 65535; i >= 0; i--) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2454 | for(entry = priority_list[i]; entry; |
| 2455 | entry = entry->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2456 | reader_read_file(entry->dir); |
| 2457 | } |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2458 | |
plougher | 5aa1816 | 2007-12-13 12:15:21 +0000 | [diff] [blame] | 2459 | thread[0] = 0; |
| 2460 | |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2461 | pthread_exit(NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | |
| 2465 | void *writer(void *arg) |
| 2466 | { |
| 2467 | int write_error = FALSE; |
| 2468 | int oldstate; |
| 2469 | |
| 2470 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2471 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2472 | |
| 2473 | while(1) { |
| 2474 | struct file_buffer *file_buffer = queue_get(to_writer); |
| 2475 | off_t off; |
| 2476 | |
| 2477 | if(file_buffer == NULL) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2478 | queue_put(from_writer, |
| 2479 | write_error ? (void *) &write_error : NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2480 | continue; |
| 2481 | } |
| 2482 | |
| 2483 | off = file_buffer->block; |
| 2484 | |
| 2485 | pthread_mutex_lock(&pos_mutex); |
| 2486 | |
| 2487 | if(!write_error && lseek(fd, off, SEEK_SET) == -1) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2488 | ERROR("Lseek on destination failed because %s\n", |
| 2489 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2490 | write_error = TRUE; |
| 2491 | } |
| 2492 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2493 | if(!write_error && write_bytes(fd, file_buffer->data, |
| 2494 | file_buffer->size) == -1) { |
| 2495 | ERROR("Write on destination failed because %s\n", |
| 2496 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2497 | write_error = TRUE; |
| 2498 | } |
| 2499 | pthread_mutex_unlock(&pos_mutex); |
| 2500 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2501 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2506 | int all_zero(struct file_buffer *file_buffer) |
| 2507 | { |
| 2508 | int i; |
| 2509 | long entries = file_buffer->size / sizeof(long); |
| 2510 | long *p = (long *) file_buffer->data; |
| 2511 | |
| 2512 | for(i = 0; i < entries && p[i] == 0; i++); |
| 2513 | |
| 2514 | if(i == entries) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2515 | for(i = file_buffer->size & ~(sizeof(long) - 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2516 | i < file_buffer->size && file_buffer->data[i] == 0; |
| 2517 | i++); |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2518 | |
| 2519 | return i == file_buffer->size; |
| 2520 | } |
| 2521 | |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2526 | void *deflator(void *arg) |
| 2527 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2528 | void *stream = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2529 | int oldstate; |
| 2530 | |
| 2531 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2532 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2533 | |
| 2534 | while(1) { |
| 2535 | struct file_buffer *file_buffer = queue_get(from_reader); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2536 | struct file_buffer *write_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2537 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2538 | if(sparse_files && all_zero(file_buffer)) { |
| 2539 | file_buffer->c_byte = 0; |
| 2540 | queue_put(from_deflate, file_buffer); |
| 2541 | } else if(file_buffer->fragment) { |
| 2542 | file_buffer->c_byte = file_buffer->size; |
| 2543 | queue_put(from_deflate, file_buffer); |
| 2544 | } else { |
| 2545 | write_buffer = cache_get(writer_buffer, 0, 0); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2546 | write_buffer->c_byte = mangle2(&stream, |
| 2547 | write_buffer->data, file_buffer->data, |
| 2548 | file_buffer->size, block_size, noD, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2549 | write_buffer->sequence = file_buffer->sequence; |
| 2550 | write_buffer->file_size = file_buffer->file_size; |
| 2551 | write_buffer->block = file_buffer->block; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2552 | write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2553 | (write_buffer->c_byte); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2554 | write_buffer->fragment = FALSE; |
| 2555 | write_buffer->error = FALSE; |
| 2556 | cache_block_put(file_buffer); |
| 2557 | queue_put(from_deflate, write_buffer); |
| 2558 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | void *frag_deflator(void *arg) |
| 2564 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2565 | void *stream = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2566 | int oldstate; |
| 2567 | |
| 2568 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); |
| 2569 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); |
| 2570 | |
| 2571 | while(1) { |
| 2572 | int c_byte, compressed_size; |
| 2573 | struct file_buffer *file_buffer = queue_get(to_frag); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2574 | struct file_buffer *write_buffer = |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2575 | cache_get(writer_buffer, file_buffer->block + |
| 2576 | FRAG_INDEX, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2577 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2578 | c_byte = mangle2(&stream, write_buffer->data, file_buffer->data, |
| 2579 | file_buffer->size, block_size, noF, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2580 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte); |
plougher | d036a31 | 2008-03-08 01:38:27 +0000 | [diff] [blame] | 2581 | write_buffer->size = compressed_size; |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2582 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2583 | if(fragments_locked == FALSE) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2584 | fragment_table[file_buffer->block].size = c_byte; |
| 2585 | fragment_table[file_buffer->block].start_block = bytes; |
| 2586 | write_buffer->block = bytes; |
| 2587 | bytes += compressed_size; |
| 2588 | fragments_outstanding --; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2589 | queue_put(to_writer, write_buffer); |
plougher | 5750191 | 2009-09-20 02:20:42 +0000 | [diff] [blame] | 2590 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2591 | TRACE("Writing fragment %lld, uncompressed size %d, " |
| 2592 | "compressed size %d\n", file_buffer->block, |
| 2593 | file_buffer->size, compressed_size); |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2594 | } else { |
| 2595 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2596 | add_pending_fragment(write_buffer, c_byte, |
| 2597 | file_buffer->block); |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2598 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2599 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | |
| 2604 | #define HASH_ENTRIES 256 |
| 2605 | #define BLOCK_HASH(a) (a % HASH_ENTRIES) |
| 2606 | struct file_buffer *block_hash[HASH_ENTRIES]; |
| 2607 | |
| 2608 | void push_buffer(struct file_buffer *file_buffer) |
| 2609 | { |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2610 | int hash = BLOCK_HASH(file_buffer->sequence); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2611 | |
| 2612 | file_buffer->next = block_hash[hash]; |
| 2613 | block_hash[hash] = file_buffer; |
| 2614 | } |
| 2615 | |
| 2616 | |
| 2617 | struct file_buffer *get_file_buffer(struct queue *queue) |
| 2618 | { |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2619 | static unsigned int sequence = 0; |
| 2620 | int hash = BLOCK_HASH(sequence); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2621 | struct file_buffer *file_buffer = block_hash[hash], *prev = NULL; |
| 2622 | |
| 2623 | for(;file_buffer; prev = file_buffer, file_buffer = file_buffer->next) |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2624 | if(file_buffer->sequence == sequence) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2625 | break; |
| 2626 | |
| 2627 | if(file_buffer) { |
| 2628 | if(prev) |
| 2629 | prev->next = file_buffer->next; |
| 2630 | else |
| 2631 | block_hash[hash] = file_buffer->next; |
| 2632 | } else { |
| 2633 | while(1) { |
| 2634 | file_buffer = queue_get(queue); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2635 | if(file_buffer->sequence == sequence) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2636 | break; |
| 2637 | push_buffer(file_buffer); |
| 2638 | } |
| 2639 | } |
| 2640 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2641 | sequence ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2642 | |
| 2643 | return file_buffer; |
| 2644 | } |
| 2645 | |
| 2646 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 2647 | void *progress_thrd(void *arg) |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2648 | { |
| 2649 | struct timeval timeval; |
| 2650 | struct timespec timespec; |
| 2651 | struct itimerval itimerval; |
| 2652 | struct winsize winsize; |
| 2653 | |
| 2654 | if(ioctl(1, TIOCGWINSZ, &winsize) == -1) { |
plougher | 01b4328 | 2009-03-30 18:21:37 +0000 | [diff] [blame] | 2655 | if(isatty(STDOUT_FILENO)) |
| 2656 | printf("TIOCGWINSZ ioctl failed, defaulting to 80 " |
| 2657 | "columns\n"); |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2658 | columns = 80; |
| 2659 | } else |
| 2660 | columns = winsize.ws_col; |
| 2661 | signal(SIGWINCH, sigwinch_handler); |
| 2662 | signal(SIGALRM, sigalrm_handler); |
| 2663 | |
| 2664 | itimerval.it_value.tv_sec = 0; |
| 2665 | itimerval.it_value.tv_usec = 250000; |
| 2666 | itimerval.it_interval.tv_sec = 0; |
| 2667 | itimerval.it_interval.tv_usec = 250000; |
| 2668 | setitimer(ITIMER_REAL, &itimerval, NULL); |
| 2669 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2670 | pthread_cond_init(&progress_wait, NULL); |
| 2671 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 2672 | pthread_mutex_lock(&progress_mutex); |
| 2673 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2674 | while(1) { |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2675 | gettimeofday(&timeval, NULL); |
| 2676 | timespec.tv_sec = timeval.tv_sec; |
| 2677 | if(timeval.tv_usec + 250000 > 999999) |
| 2678 | timespec.tv_sec++; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2679 | timespec.tv_nsec = ((timeval.tv_usec + 250000) % 1000000) * |
| 2680 | 1000; |
| 2681 | pthread_cond_timedwait(&progress_wait, &progress_mutex, |
| 2682 | ×pec); |
plougher | 43bb7e9 | 2008-08-17 17:16:09 +0000 | [diff] [blame] | 2683 | if(progress_enabled && estimated_uncompressed) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2684 | progress_bar(cur_uncompressed, estimated_uncompressed, |
| 2685 | columns); |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 2690 | void enable_progress_bar() |
| 2691 | { |
| 2692 | pthread_mutex_lock(&progress_mutex); |
| 2693 | progress_enabled = TRUE; |
| 2694 | pthread_mutex_unlock(&progress_mutex); |
| 2695 | } |
| 2696 | |
| 2697 | |
| 2698 | void disable_progress_bar() |
| 2699 | { |
| 2700 | pthread_mutex_lock(&progress_mutex); |
| 2701 | progress_enabled = FALSE; |
| 2702 | pthread_mutex_unlock(&progress_mutex); |
| 2703 | } |
| 2704 | |
| 2705 | |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2706 | void progress_bar(long long current, long long max, int columns) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2707 | { |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2708 | char rotate_list[] = { '|', '/', '-', '\\' }; |
plougher | f3f95e4 | 2010-03-08 01:32:20 +0000 | [diff] [blame] | 2709 | int max_digits, used, hashes, spaces; |
plougher | 898763d | 2009-03-29 22:54:55 +0000 | [diff] [blame] | 2710 | static int tty = -1; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2711 | |
plougher | f3f95e4 | 2010-03-08 01:32:20 +0000 | [diff] [blame] | 2712 | if(max == 0) |
| 2713 | return; |
| 2714 | |
| 2715 | max_digits = floor(log10(max)) + 1; |
| 2716 | used = max_digits * 2 + 11; |
| 2717 | hashes = (current * (columns - used)) / max; |
| 2718 | spaces = columns - used - hashes; |
| 2719 | |
plougher | e9e0139 | 2009-03-29 22:48:42 +0000 | [diff] [blame] | 2720 | if((current > max) || (columns - used < 0)) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | |
plougher | 898763d | 2009-03-29 22:54:55 +0000 | [diff] [blame] | 2723 | if(tty == -1) |
| 2724 | tty = isatty(STDOUT_FILENO); |
| 2725 | if(!tty) { |
| 2726 | static long long previous = -1; |
| 2727 | |
| 2728 | /* Updating much more frequently than this results in huge |
| 2729 | * log files. */ |
| 2730 | if((current % 100) != 0 && current != max) |
| 2731 | return; |
| 2732 | /* Don't update just to rotate the spinner. */ |
| 2733 | if(current == previous) |
| 2734 | return; |
| 2735 | previous = current; |
| 2736 | } |
| 2737 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2738 | printf("\r["); |
| 2739 | |
| 2740 | while (hashes --) |
| 2741 | putchar('='); |
| 2742 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2743 | putchar(rotate_list[rotate]); |
| 2744 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2745 | while(spaces --) |
| 2746 | putchar(' '); |
| 2747 | |
| 2748 | printf("] %*lld/%*lld", max_digits, current, max_digits, max); |
| 2749 | printf(" %3lld%%", current * 100 / max); |
| 2750 | fflush(stdout); |
| 2751 | } |
| 2752 | |
| 2753 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2754 | void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2755 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2756 | { |
| 2757 | file_count ++; |
| 2758 | *duplicate_file = FALSE; |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2759 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0, |
| 2760 | NULL, &empty_fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2764 | void write_file_frag_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2765 | int size, int *duplicate_file, struct file_buffer *file_buffer, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2766 | unsigned short checksum) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2767 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2768 | struct file_info *dupl_ptr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2769 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2770 | unsigned int *block_listp = NULL; |
| 2771 | long long start = 0; |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2772 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2773 | dupl_ptr = duplicate(size, 0, &block_listp, &start, &fragment, |
| 2774 | file_buffer, 0, 0, checksum, TRUE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2775 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2776 | if(dupl_ptr) { |
| 2777 | *duplicate_file = FALSE; |
| 2778 | fragment = get_and_fill_fragment(file_buffer); |
| 2779 | dupl_ptr->fragment = fragment; |
| 2780 | } else |
| 2781 | *duplicate_file = TRUE; |
| 2782 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2783 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2784 | |
| 2785 | total_bytes += size; |
| 2786 | file_count ++; |
| 2787 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2788 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2789 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2790 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2791 | 0, NULL, fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2795 | void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, int size, |
| 2796 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2797 | { |
| 2798 | struct fragment *fragment; |
| 2799 | unsigned short checksum; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2800 | |
| 2801 | checksum = get_checksum_mem_buffer(file_buffer); |
| 2802 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2803 | if(pre_duplicate_frag(size, checksum)) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2804 | write_file_frag_dup(inode, dir_ent, size, duplicate_file, |
| 2805 | file_buffer, checksum); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2806 | return; |
| 2807 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2808 | |
| 2809 | fragment = get_and_fill_fragment(file_buffer); |
| 2810 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2811 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2812 | |
| 2813 | if(duplicate_checking) |
| 2814 | add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, TRUE); |
| 2815 | |
| 2816 | total_bytes += size; |
| 2817 | file_count ++; |
| 2818 | |
| 2819 | *duplicate_file = FALSE; |
| 2820 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2821 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2822 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2823 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2824 | 0, NULL, fragment, NULL, 0); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2825 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2826 | return; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2830 | int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2831 | struct file_buffer *read_buffer, int *duplicate_file) |
| 2832 | { |
| 2833 | long long read_size, file_bytes, start; |
| 2834 | struct fragment *fragment; |
| 2835 | unsigned int *block_list = NULL; |
| 2836 | int block = 0, status; |
| 2837 | long long sparse = 0; |
| 2838 | struct file_buffer *fragment_buffer = NULL; |
| 2839 | |
| 2840 | *duplicate_file = FALSE; |
| 2841 | |
| 2842 | lock_fragments(); |
| 2843 | |
| 2844 | file_bytes = 0; |
| 2845 | start = bytes; |
| 2846 | while (1) { |
| 2847 | read_size = read_buffer->file_size; |
| 2848 | if(read_buffer->fragment && read_buffer->c_byte) |
| 2849 | fragment_buffer = read_buffer; |
| 2850 | else { |
| 2851 | block_list = realloc(block_list, (block + 1) * |
| 2852 | sizeof(unsigned int)); |
| 2853 | if(block_list == NULL) |
| 2854 | BAD_ERROR("Out of memory allocating block_list" |
| 2855 | "\n"); |
| 2856 | block_list[block ++] = read_buffer->c_byte; |
| 2857 | if(read_buffer->c_byte) { |
| 2858 | read_buffer->block = bytes; |
| 2859 | bytes += read_buffer->size; |
| 2860 | cache_rehash(read_buffer, read_buffer->block); |
| 2861 | file_bytes += read_buffer->size; |
| 2862 | queue_put(to_writer, read_buffer); |
| 2863 | } else { |
| 2864 | sparse += read_buffer->size; |
| 2865 | cache_block_put(read_buffer); |
| 2866 | } |
| 2867 | } |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2868 | inc_progress_bar(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2869 | |
| 2870 | if(read_size != -1) |
| 2871 | break; |
| 2872 | |
| 2873 | read_buffer = get_file_buffer(from_deflate); |
| 2874 | if(read_buffer->error) |
| 2875 | goto read_err; |
| 2876 | } |
| 2877 | |
| 2878 | unlock_fragments(); |
| 2879 | fragment = get_and_fill_fragment(fragment_buffer); |
| 2880 | cache_block_put(fragment_buffer); |
| 2881 | |
| 2882 | if(duplicate_checking) |
| 2883 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2884 | 0, 0, FALSE); |
| 2885 | file_count ++; |
| 2886 | total_bytes += read_size; |
| 2887 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2888 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2889 | block, block_list, fragment, NULL, sparse); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2890 | |
| 2891 | if(duplicate_checking == FALSE) |
| 2892 | free(block_list); |
| 2893 | |
| 2894 | return 0; |
| 2895 | |
| 2896 | read_err: |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2897 | cur_uncompressed -= block; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2898 | status = read_buffer->error; |
| 2899 | bytes = start; |
| 2900 | if(!block_device) { |
| 2901 | int res; |
| 2902 | |
| 2903 | queue_put(to_writer, NULL); |
| 2904 | if(queue_get(from_writer) != 0) |
| 2905 | EXIT_MKSQUASHFS(); |
| 2906 | res = ftruncate(fd, bytes); |
| 2907 | if(res != 0) |
| 2908 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2909 | strerror(errno)); |
| 2910 | } |
| 2911 | unlock_fragments(); |
| 2912 | free(block_list); |
| 2913 | cache_block_put(read_buffer); |
| 2914 | return status; |
| 2915 | } |
| 2916 | |
| 2917 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2918 | int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2919 | long long read_size, struct file_buffer *read_buffer, |
| 2920 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2921 | { |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 2922 | long long file_bytes, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2923 | struct fragment *fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2924 | unsigned int *block_list; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2925 | int block, status; |
| 2926 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2927 | long long sparse = 0; |
| 2928 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2929 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2930 | *duplicate_file = FALSE; |
| 2931 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2932 | if((block_list = malloc(blocks * sizeof(unsigned int))) == NULL) |
| 2933 | BAD_ERROR("Out of memory allocating block_list\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2934 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2935 | lock_fragments(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2936 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2937 | file_bytes = 0; |
| 2938 | start = bytes; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2939 | for(block = 0; block < blocks;) { |
| 2940 | if(read_buffer->fragment && read_buffer->c_byte) { |
| 2941 | fragment_buffer = read_buffer; |
| 2942 | blocks = read_size >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2943 | } else { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2944 | block_list[block] = read_buffer->c_byte; |
| 2945 | if(read_buffer->c_byte) { |
| 2946 | read_buffer->block = bytes; |
| 2947 | bytes += read_buffer->size; |
| 2948 | cache_rehash(read_buffer, read_buffer->block); |
| 2949 | file_bytes += read_buffer->size; |
| 2950 | queue_put(to_writer, read_buffer); |
| 2951 | } else { |
| 2952 | sparse += read_buffer->size; |
| 2953 | cache_block_put(read_buffer); |
| 2954 | } |
| 2955 | } |
| 2956 | inc_progress_bar(); |
| 2957 | |
| 2958 | if(++block < blocks) { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2959 | read_buffer = get_file_buffer(from_deflate); |
| 2960 | if(read_buffer->error) |
| 2961 | goto read_err; |
| 2962 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2965 | unlock_fragments(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2966 | fragment = get_and_fill_fragment(fragment_buffer); |
| 2967 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2968 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2969 | if(duplicate_checking) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2970 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
| 2971 | 0, 0, FALSE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2972 | file_count ++; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2973 | total_bytes += read_size; |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2974 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2975 | /* |
| 2976 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2977 | * a smaller block count on stat calls to sparse files. This is |
| 2978 | * to ensure intelligent applications like cp correctly handle the |
| 2979 | * file as a sparse file. If the file in the original filesystem isn't |
| 2980 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2981 | * report it as non-sparse on stat calls to preserve semantics |
| 2982 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2983 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2984 | sparse = 0; |
| 2985 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2986 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2987 | blocks, block_list, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2988 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2989 | if(duplicate_checking == FALSE) |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 2990 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2991 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2992 | return 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2993 | |
| 2994 | read_err: |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2995 | cur_uncompressed -= block; |
| 2996 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2997 | bytes = start; |
| 2998 | if(!block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2999 | int res; |
| 3000 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3001 | queue_put(to_writer, NULL); |
| 3002 | if(queue_get(from_writer) != 0) |
| 3003 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3004 | res = ftruncate(fd, bytes); |
| 3005 | if(res != 0) |
| 3006 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 3007 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3008 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3009 | unlock_fragments(); |
plougher | f9c72b1 | 2006-01-23 13:52:40 +0000 | [diff] [blame] | 3010 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3011 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3012 | return status; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3016 | int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3017 | long long read_size, struct file_buffer *read_buffer, |
| 3018 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3019 | { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3020 | int block, thresh; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3021 | long long file_bytes, dup_start, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3022 | struct fragment *fragment; |
| 3023 | struct file_info *dupl_ptr; |
| 3024 | int blocks = (read_size + block_size - 1) >> block_log; |
| 3025 | unsigned int *block_list, *block_listp; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3026 | struct file_buffer **buffer_list; |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 3027 | int status, num_locked_fragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3028 | long long sparse = 0; |
| 3029 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3030 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3031 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 3032 | if(block_list == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3033 | BAD_ERROR("Out of memory allocating block_list\n"); |
| 3034 | block_listp = block_list; |
| 3035 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3036 | buffer_list = malloc(blocks * sizeof(struct file_buffer *)); |
| 3037 | if(buffer_list == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3038 | BAD_ERROR("Out of memory allocating file block list\n"); |
| 3039 | |
plougher | 4e8484a | 2008-03-15 23:30:16 +0000 | [diff] [blame] | 3040 | num_locked_fragments = lock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3041 | |
| 3042 | file_bytes = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3043 | start = dup_start = bytes; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3044 | thresh = blocks > (writer_buffer_size - num_locked_fragments) ? |
| 3045 | blocks - (writer_buffer_size - num_locked_fragments): 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3046 | |
| 3047 | for(block = 0; block < blocks;) { |
| 3048 | if(read_buffer->fragment && read_buffer->c_byte) { |
| 3049 | fragment_buffer = read_buffer; |
| 3050 | blocks = read_size >> block_log; |
| 3051 | } else { |
| 3052 | block_list[block] = read_buffer->c_byte; |
| 3053 | |
| 3054 | if(read_buffer->c_byte) { |
| 3055 | read_buffer->block = bytes; |
| 3056 | bytes += read_buffer->size; |
| 3057 | file_bytes += read_buffer->size; |
| 3058 | cache_rehash(read_buffer, read_buffer->block); |
| 3059 | if(block < thresh) { |
| 3060 | buffer_list[block] = NULL; |
| 3061 | queue_put(to_writer, read_buffer); |
| 3062 | } else |
| 3063 | buffer_list[block] = read_buffer; |
| 3064 | } else { |
| 3065 | buffer_list[block] = NULL; |
| 3066 | sparse += read_buffer->size; |
| 3067 | cache_block_put(read_buffer); |
| 3068 | } |
| 3069 | } |
| 3070 | inc_progress_bar(); |
| 3071 | |
| 3072 | if(++block < blocks) { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3073 | read_buffer = get_file_buffer(from_deflate); |
| 3074 | if(read_buffer->error) |
| 3075 | goto read_err; |
| 3076 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3079 | dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start, |
| 3080 | &fragment, fragment_buffer, blocks, 0, 0, FALSE); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3081 | |
| 3082 | if(dupl_ptr) { |
| 3083 | *duplicate_file = FALSE; |
| 3084 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3085 | if(buffer_list[block]) |
| 3086 | queue_put(to_writer, buffer_list[block]); |
| 3087 | fragment = get_and_fill_fragment(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3088 | dupl_ptr->fragment = fragment; |
| 3089 | } else { |
| 3090 | *duplicate_file = TRUE; |
| 3091 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3092 | cache_block_put(buffer_list[block]); |
| 3093 | bytes = start; |
| 3094 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3095 | int res; |
| 3096 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3097 | queue_put(to_writer, NULL); |
| 3098 | if(queue_get(from_writer) != 0) |
| 3099 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3100 | res = ftruncate(fd, bytes); |
| 3101 | if(res != 0) |
| 3102 | BAD_ERROR("Failed to truncate dest file because" |
| 3103 | " %s\n", strerror(errno)); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3104 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3107 | unlock_fragments(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3108 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3109 | free(buffer_list); |
| 3110 | file_count ++; |
| 3111 | total_bytes += read_size; |
| 3112 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3113 | /* |
| 3114 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3115 | * a smaller block count on stat calls to sparse files. This is |
| 3116 | * to ensure intelligent applications like cp correctly handle the |
| 3117 | * file as a sparse file. If the file in the original filesystem isn't |
| 3118 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3119 | * report it as non-sparse on stat calls to preserve semantics |
| 3120 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3121 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 3122 | sparse = 0; |
| 3123 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 3124 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, |
| 3125 | dup_start, blocks, block_listp, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3126 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3127 | if(*duplicate_file == TRUE) |
| 3128 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3129 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3130 | return 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3131 | |
| 3132 | read_err: |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3133 | cur_uncompressed -= block; |
| 3134 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3135 | bytes = start; |
| 3136 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3137 | int res; |
| 3138 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3139 | queue_put(to_writer, NULL); |
| 3140 | if(queue_get(from_writer) != 0) |
| 3141 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 3142 | res = ftruncate(fd, bytes); |
| 3143 | if(res != 0) |
| 3144 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 3145 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3146 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3147 | unlock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3148 | for(blocks = thresh; blocks < block; blocks ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3149 | cache_block_put(buffer_list[blocks]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3150 | free(buffer_list); |
| 3151 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3152 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3153 | return status; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3157 | void write_file(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 3158 | int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3159 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3160 | int status; |
| 3161 | struct file_buffer *read_buffer; |
| 3162 | long long read_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3163 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3164 | again: |
| 3165 | read_buffer = get_file_buffer(from_deflate); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3166 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 3167 | status = read_buffer->error; |
| 3168 | if(status) { |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3169 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3170 | goto file_err; |
| 3171 | } |
| 3172 | |
| 3173 | read_size = read_buffer->file_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3174 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3175 | if(read_size == -1) |
| 3176 | status = write_file_process(inode, dir_ent, read_buffer, |
| 3177 | duplicate_file); |
| 3178 | else if(read_size == 0) { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3179 | write_file_empty(inode, dir_ent, duplicate_file); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3180 | cache_block_put(read_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3181 | } else if(read_buffer->fragment && read_buffer->c_byte) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3182 | write_file_frag(inode, dir_ent, read_size, read_buffer, |
| 3183 | duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3184 | else if(pre_duplicate(read_size)) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3185 | status = write_file_blocks_dup(inode, dir_ent, read_size, |
| 3186 | read_buffer, duplicate_file); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3187 | else |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3188 | status = write_file_blocks(inode, dir_ent, read_size, |
| 3189 | read_buffer, duplicate_file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3190 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3191 | file_err: |
| 3192 | if(status == 2) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3193 | ERROR("File %s changed size while reading filesystem, " |
| 3194 | "attempting to re-read\n", dir_ent->pathname); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3195 | goto again; |
| 3196 | } else if(status == 1) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3197 | ERROR("Failed to read file %s, creating empty file\n", |
| 3198 | dir_ent->pathname); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3199 | write_file_empty(inode, dir_ent, duplicate_file); |
| 3200 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 3204 | #define BUFF_SIZE 8192 |
| 3205 | char b_buffer[BUFF_SIZE]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3206 | char *name; |
| 3207 | char *basename_r(); |
| 3208 | |
| 3209 | char *getbase(char *pathname) |
| 3210 | { |
| 3211 | char *result; |
| 3212 | |
| 3213 | if(*pathname != '/') { |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 3214 | result = getcwd(b_buffer, BUFF_SIZE); |
| 3215 | if(result == NULL) |
| 3216 | return NULL; |
| 3217 | strcat(strcat(b_buffer, "/"), pathname); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3218 | } else |
| 3219 | strcpy(b_buffer, pathname); |
| 3220 | name = b_buffer; |
| 3221 | if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0)) |
| 3222 | return NULL; |
| 3223 | else |
| 3224 | return result; |
| 3225 | } |
| 3226 | |
| 3227 | |
| 3228 | char *basename_r() |
| 3229 | { |
| 3230 | char *s; |
| 3231 | char *p; |
| 3232 | int n = 1; |
| 3233 | |
| 3234 | for(;;) { |
| 3235 | s = name; |
| 3236 | if(*name == '\0') |
| 3237 | return NULL; |
| 3238 | if(*name != '/') { |
| 3239 | while(*name != '\0' && *name != '/') name++; |
| 3240 | n = name - s; |
| 3241 | } |
| 3242 | while(*name == '/') name++; |
| 3243 | if(strncmp(s, ".", n) == 0) |
| 3244 | continue; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3245 | if((*name == '\0') || (strncmp(s, "..", n) == 0) || |
| 3246 | ((p = basename_r()) == NULL)) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3247 | s[n] = '\0'; |
| 3248 | return s; |
| 3249 | } |
| 3250 | if(strcmp(p, "..") == 0) |
| 3251 | continue; |
| 3252 | return p; |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | |
| 3257 | struct inode_info *lookup_inode(struct stat *buf) |
| 3258 | { |
| 3259 | int inode_hash = INODE_HASH(buf->st_dev, buf->st_ino); |
| 3260 | struct inode_info *inode = inode_info[inode_hash]; |
| 3261 | |
| 3262 | while(inode != NULL) { |
| 3263 | if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) { |
| 3264 | inode->nlink ++; |
| 3265 | return inode; |
| 3266 | } |
| 3267 | inode = inode->next; |
| 3268 | } |
| 3269 | |
| 3270 | if((inode = malloc(sizeof(struct inode_info))) == NULL) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3271 | BAD_ERROR("Out of memory in inode hash table entry allocation" |
| 3272 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3273 | |
| 3274 | memcpy(&inode->buf, buf, sizeof(struct stat)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3275 | inode->read = FALSE; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3276 | inode->root_entry = FALSE; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3277 | inode->pseudo_file = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3278 | inode->inode = SQUASHFS_INVALID_BLK; |
| 3279 | inode->nlink = 1; |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3280 | |
| 3281 | if((buf->st_mode & S_IFMT) == S_IFREG) |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3282 | estimated_uncompressed += (buf->st_size + block_size - 1) >> |
| 3283 | block_log; |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3284 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3285 | if((buf->st_mode & S_IFMT) == S_IFDIR) |
| 3286 | inode->inode_number = dir_inode_no ++; |
| 3287 | else |
| 3288 | inode->inode_number = inode_no ++; |
| 3289 | |
| 3290 | inode->next = inode_info[inode_hash]; |
| 3291 | inode_info[inode_hash] = inode; |
| 3292 | |
| 3293 | return inode; |
| 3294 | } |
| 3295 | |
| 3296 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3297 | inline void add_dir_entry(char *name, char *pathname, struct dir_info *sub_dir, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3298 | struct inode_info *inode_info, struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3299 | { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3300 | if((dir->count % DIR_ENTRIES) == 0) { |
| 3301 | dir->list = realloc(dir->list, (dir->count + DIR_ENTRIES) * |
| 3302 | sizeof(struct dir_ent *)); |
| 3303 | if(dir->list == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3304 | BAD_ERROR("Out of memory in add_dir_entry\n"); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3305 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3306 | |
| 3307 | if((dir->list[dir->count] = malloc(sizeof(struct dir_ent))) == NULL) |
| 3308 | BAD_ERROR("Out of memory in linux_opendir\n"); |
| 3309 | |
| 3310 | if(sub_dir) |
| 3311 | sub_dir->dir_ent = dir->list[dir->count]; |
| 3312 | dir->list[dir->count]->name = strdup(name); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3313 | dir->list[dir->count]->pathname = pathname != NULL ? strdup(pathname) : |
| 3314 | NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3315 | dir->list[dir->count]->inode = inode_info; |
| 3316 | dir->list[dir->count]->dir = sub_dir; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3317 | dir->list[dir->count++]->our_dir = dir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3318 | dir->byte_count += strlen(name) + sizeof(squashfs_dir_entry); |
| 3319 | } |
| 3320 | |
| 3321 | |
| 3322 | int compare_name(const void *ent1_ptr, const void *ent2_ptr) |
| 3323 | { |
| 3324 | struct dir_ent *ent1 = *((struct dir_ent **) ent1_ptr); |
| 3325 | struct dir_ent *ent2 = *((struct dir_ent **) ent2_ptr); |
| 3326 | |
| 3327 | return strcmp(ent1->name, ent2->name); |
| 3328 | } |
| 3329 | |
| 3330 | |
| 3331 | void sort_directory(struct dir_info *dir) |
| 3332 | { |
| 3333 | qsort(dir->list, dir->count, sizeof(struct dir_ent *), compare_name); |
| 3334 | |
| 3335 | if((dir->count < 257 && dir->byte_count < SQUASHFS_METADATA_SIZE)) |
| 3336 | dir->dir_is_ldir = FALSE; |
| 3337 | } |
| 3338 | |
| 3339 | |
| 3340 | struct dir_info *scan1_opendir(char *pathname) |
| 3341 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3342 | struct dir_info *dir; |
| 3343 | |
| 3344 | if((dir = malloc(sizeof(struct dir_info))) == NULL) |
plougher | fbfdda7 | 2010-07-21 01:09:14 +0000 | [diff] [blame] | 3345 | BAD_ERROR("Out of memory in scan1_opendir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3346 | |
| 3347 | if(pathname[0] != '\0' && (dir->linuxdir = opendir(pathname)) == NULL) { |
| 3348 | free(dir); |
| 3349 | return NULL; |
| 3350 | } |
| 3351 | dir->pathname = strdup(pathname); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3352 | dir->count = dir->directory_count = dir->current_count = dir->byte_count |
| 3353 | = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3354 | dir->dir_is_ldir = TRUE; |
| 3355 | dir->list = NULL; |
| 3356 | |
| 3357 | return dir; |
| 3358 | } |
| 3359 | |
| 3360 | |
| 3361 | int scan1_encomp_readdir(char *pathname, char *dir_name, struct dir_info *dir) |
| 3362 | { |
| 3363 | int i, n, pass; |
| 3364 | char *basename; |
| 3365 | static int index = 0; |
| 3366 | |
| 3367 | if(dir->count < old_root_entries) |
| 3368 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3369 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3370 | dir->directory_count ++; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3371 | add_dir_entry(old_root_entry[i].name, "", NULL, |
| 3372 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | while(index < source) { |
| 3376 | if((basename = getbase(source_path[index])) == NULL) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3377 | ERROR("Bad source directory %s - skipping ...\n", |
| 3378 | source_path[index]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3379 | index ++; |
| 3380 | continue; |
| 3381 | } |
| 3382 | strcpy(dir_name, basename); |
| 3383 | pass = 1; |
| 3384 | for(;;) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3385 | for(n = 0; n < dir->count && |
| 3386 | strcmp(dir->list[n]->name, dir_name) != 0; n++); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3387 | if(n == dir->count) |
| 3388 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3389 | ERROR("Source directory entry %s already used! - trying" |
| 3390 | " ", dir_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3391 | sprintf(dir_name, "%s_%d", basename, pass++); |
| 3392 | ERROR("%s\n", dir_name); |
| 3393 | } |
| 3394 | strcpy(pathname, source_path[index ++]); |
| 3395 | return 1; |
| 3396 | } |
| 3397 | return 0; |
| 3398 | } |
| 3399 | |
| 3400 | |
| 3401 | int scan1_single_readdir(char *pathname, char *dir_name, struct dir_info *dir) |
| 3402 | { |
| 3403 | struct dirent *d_name; |
| 3404 | int i, pass; |
| 3405 | |
| 3406 | if(dir->count < old_root_entries) |
| 3407 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3408 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3409 | dir->directory_count ++; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3410 | add_dir_entry(old_root_entry[i].name, "", NULL, |
| 3411 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | if((d_name = readdir(dir->linuxdir)) != NULL) { |
| 3415 | strcpy(dir_name, d_name->d_name); |
| 3416 | pass = 1; |
| 3417 | for(;;) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3418 | for(i = 0; i < dir->count && |
| 3419 | strcmp(dir->list[i]->name, dir_name) != 0; i++); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3420 | if(i == dir->count) |
| 3421 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3422 | ERROR("Source directory entry %s already used! - trying" |
| 3423 | " ", dir_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3424 | sprintf(dir_name, "%s_%d", d_name->d_name, pass++); |
| 3425 | ERROR("%s\n", dir_name); |
| 3426 | } |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3427 | strcat(strcat(strcpy(pathname, dir->pathname), "/"), |
| 3428 | d_name->d_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3429 | return 1; |
| 3430 | } |
| 3431 | |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | |
| 3436 | int scan1_readdir(char *pathname, char *dir_name, struct dir_info *dir) |
| 3437 | { |
| 3438 | struct dirent *d_name; |
| 3439 | |
| 3440 | if((d_name = readdir(dir->linuxdir)) != NULL) { |
| 3441 | strcpy(dir_name, d_name->d_name); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3442 | strcat(strcat(strcpy(pathname, dir->pathname), "/"), |
| 3443 | d_name->d_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3444 | return 1; |
| 3445 | } |
| 3446 | |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
| 3450 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3451 | struct dir_ent *scan2_readdir(struct dir_info *dir_info) |
| 3452 | { |
| 3453 | int current_count; |
| 3454 | |
| 3455 | while((current_count = dir_info->current_count++) < dir_info->count) |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3456 | if(dir_info->list[current_count]->inode->root_entry) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3457 | continue; |
| 3458 | else |
| 3459 | return dir_info->list[current_count]; |
| 3460 | return NULL; |
| 3461 | } |
| 3462 | |
| 3463 | |
| 3464 | struct dir_ent *scan2_lookup(struct dir_info *dir, char *name) |
| 3465 | { |
| 3466 | int i; |
| 3467 | |
| 3468 | for(i = 0; i < dir->count; i++) |
| 3469 | if(strcmp(dir->list[i]->name, name) == 0) |
| 3470 | return dir->list[i]; |
| 3471 | |
| 3472 | return NULL; |
| 3473 | } |
| 3474 | |
| 3475 | |
| 3476 | struct dir_ent *scan3_readdir(struct directory *dir, struct dir_info *dir_info) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3477 | { |
| 3478 | int current_count; |
| 3479 | |
| 3480 | while((current_count = dir_info->current_count++) < dir_info->count) |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3481 | if(dir_info->list[current_count]->inode->root_entry) |
| 3482 | add_dir(dir_info->list[current_count]->inode->inode, |
| 3483 | dir_info->list[current_count]->inode->inode_number, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3484 | dir_info->list[current_count]->name, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3485 | dir_info->list[current_count]->inode->type, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3486 | else |
| 3487 | return dir_info->list[current_count]; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3488 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | |
| 3492 | void scan1_freedir(struct dir_info *dir) |
| 3493 | { |
plougher | fbed12b | 2006-02-07 12:45:53 +0000 | [diff] [blame] | 3494 | if(dir->pathname[0] != '\0') |
| 3495 | closedir(dir->linuxdir); |
plougher | b087fc1 | 2010-05-01 04:53:28 +0000 | [diff] [blame] | 3496 | free(dir->pathname); |
| 3497 | dir->pathname = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3501 | void scan2_freedir(struct dir_info *dir) |
| 3502 | { |
| 3503 | dir->current_count = 0; |
plougher | b087fc1 | 2010-05-01 04:53:28 +0000 | [diff] [blame] | 3504 | if(dir->pathname) { |
| 3505 | free(dir->pathname); |
| 3506 | dir->pathname = NULL; |
| 3507 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | |
| 3511 | void scan3_freedir(struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3512 | { |
| 3513 | if(dir->index) |
| 3514 | free(dir->index); |
| 3515 | free(dir->buff); |
| 3516 | } |
| 3517 | |
| 3518 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3519 | void dir_scan(squashfs_inode *inode, char *pathname, |
| 3520 | int (_readdir)(char *, char *, struct dir_info *)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3521 | { |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 3522 | struct stat buf; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3523 | struct dir_info *dir_info = dir_scan1(pathname, paths, _readdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3524 | struct dir_ent *dir_ent; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3525 | |
| 3526 | if(dir_info == NULL) |
| 3527 | return; |
| 3528 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3529 | dir_scan2(dir_info, pseudo); |
| 3530 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3531 | if((dir_ent = malloc(sizeof(struct dir_ent))) == NULL) |
| 3532 | BAD_ERROR("Out of memory in dir_scan\n"); |
| 3533 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3534 | if(pathname[0] == '\0') { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3535 | /* |
plougher | 1e4dbce | 2010-07-31 03:01:34 +0000 | [diff] [blame^] | 3536 | * dummy top level directory, if multiple sources specified on |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3537 | * command line |
| 3538 | */ |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3539 | memset(&buf, 0, sizeof(buf)); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 3540 | buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR; |
| 3541 | buf.st_uid = getuid(); |
| 3542 | buf.st_gid = getgid(); |
| 3543 | buf.st_mtime = time(NULL); |
| 3544 | buf.st_dev = 0; |
| 3545 | buf.st_ino = 0; |
plougher | 1e4dbce | 2010-07-31 03:01:34 +0000 | [diff] [blame^] | 3546 | dir_ent->inode = lookup_inode(&buf); |
| 3547 | dir_ent->inode->pseudo_file = PSEUDO_FILE_OTHER; |
| 3548 | } else { |
| 3549 | if(lstat(pathname, &buf) == -1) { |
| 3550 | ERROR("Cannot stat dir/file %s because %s, ignoring", |
| 3551 | pathname, strerror(errno)); |
| 3552 | return; |
| 3553 | } |
| 3554 | dir_ent->inode = lookup_inode(&buf); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3555 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 3556 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 3557 | if(root_inode_number) { |
| 3558 | dir_ent->inode->inode_number = root_inode_number; |
| 3559 | dir_inode_no --; |
| 3560 | } |
| 3561 | dir_ent->name = dir_ent->pathname = strdup(pathname); |
| 3562 | dir_ent->dir = dir_info; |
| 3563 | dir_ent->our_dir = NULL; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 3564 | dir_info->dir_ent = dir_ent; |
| 3565 | |
plougher | 1d1c6bb | 2010-07-21 03:03:13 +0000 | [diff] [blame] | 3566 | if(sorted) { |
| 3567 | int res = generate_file_priorities(dir_info, 0, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3568 | &dir_info->dir_ent->inode->buf); |
plougher | 1d1c6bb | 2010-07-21 03:03:13 +0000 | [diff] [blame] | 3569 | |
| 3570 | if(res == FALSE) |
| 3571 | BAD_ERROR("generate_file_priorities failed\n"); |
| 3572 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3573 | queue_put(to_reader, dir_info); |
| 3574 | if(sorted) |
plougher | 117b2ea | 2006-02-09 09:23:56 +0000 | [diff] [blame] | 3575 | sort_files_and_write(dir_info); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 3576 | if(progress) |
| 3577 | enable_progress_bar(); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3578 | dir_scan3(inode, dir_info); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 3579 | dir_ent->inode->inode = *inode; |
| 3580 | dir_ent->inode->type = SQUASHFS_DIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3584 | struct dir_info *dir_scan1(char *pathname, struct pathnames *paths, |
| 3585 | int (_readdir)(char *, char *, struct dir_info *)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3586 | { |
| 3587 | struct dir_info *dir, *sub_dir; |
| 3588 | struct stat buf; |
| 3589 | char filename[8192], dir_name[8192]; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 3590 | struct pathnames *new; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3591 | |
| 3592 | if((dir = scan1_opendir(pathname)) == NULL) { |
| 3593 | ERROR("Could not open %s, skipping...\n", pathname); |
| 3594 | goto error; |
| 3595 | } |
| 3596 | |
| 3597 | while(_readdir(filename, dir_name, dir) != FALSE) { |
| 3598 | |
| 3599 | if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) |
| 3600 | continue; |
| 3601 | |
| 3602 | if(lstat(filename, &buf) == -1) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3603 | ERROR("Cannot stat dir/file %s because %s, ignoring", |
| 3604 | filename, strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3605 | continue; |
| 3606 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3607 | |
| 3608 | if((buf.st_mode & S_IFMT) != S_IFREG && |
| 3609 | (buf.st_mode & S_IFMT) != S_IFDIR && |
| 3610 | (buf.st_mode & S_IFMT) != S_IFLNK && |
| 3611 | (buf.st_mode & S_IFMT) != S_IFCHR && |
| 3612 | (buf.st_mode & S_IFMT) != S_IFBLK && |
| 3613 | (buf.st_mode & S_IFMT) != S_IFIFO && |
| 3614 | (buf.st_mode & S_IFMT) != S_IFSOCK) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3615 | ERROR("File %s has unrecognised filetype %d, ignoring" |
| 3616 | "\n", filename, buf.st_mode & S_IFMT); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3617 | continue; |
| 3618 | } |
| 3619 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3620 | if(old_exclude) { |
| 3621 | if(old_excluded(filename, &buf)) |
| 3622 | continue; |
| 3623 | } else { |
| 3624 | if(excluded(paths, dir_name, &new)) |
| 3625 | continue; |
| 3626 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3627 | |
| 3628 | if((buf.st_mode & S_IFMT) == S_IFDIR) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3629 | sub_dir = dir_scan1(filename, new, scan1_readdir); |
| 3630 | if(sub_dir == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3631 | continue; |
| 3632 | dir->directory_count ++; |
| 3633 | } else |
| 3634 | sub_dir = NULL; |
| 3635 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3636 | add_dir_entry(dir_name, filename, sub_dir, lookup_inode(&buf), |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3637 | dir); |
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 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3647 | struct dir_info *dir_scan2(struct dir_info *dir, struct pseudo *pseudo) |
| 3648 | { |
| 3649 | struct dir_info *sub_dir; |
| 3650 | struct dir_ent *dir_ent; |
| 3651 | struct pseudo_entry *pseudo_ent; |
| 3652 | struct stat buf; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3653 | static int pseudo_ino = 1; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3654 | |
| 3655 | if(dir == NULL && (dir = scan1_opendir("")) == NULL) |
| 3656 | return NULL; |
| 3657 | |
| 3658 | while((dir_ent = scan2_readdir(dir)) != NULL) { |
| 3659 | struct inode_info *inode_info = dir_ent->inode; |
| 3660 | struct stat *buf = &inode_info->buf; |
| 3661 | char *name = dir_ent->name; |
| 3662 | |
| 3663 | if((buf->st_mode & S_IFMT) == S_IFDIR) |
| 3664 | dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo)); |
| 3665 | } |
| 3666 | |
| 3667 | while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) { |
| 3668 | dir_ent = scan2_lookup(dir, pseudo_ent->name); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3669 | if(pseudo_ent->dev->type == 's') { |
| 3670 | struct stat *buf; |
| 3671 | if(dir_ent == NULL) { |
| 3672 | ERROR("Pseudo set file \"%s\" does not exist " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3673 | "in source filesystem. Ignoring.\n", |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3674 | pseudo_ent->pathname); |
| 3675 | continue; |
| 3676 | } |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3677 | if(dir_ent->inode->root_entry) { |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3678 | ERROR("Pseudo set file \"%s\" is a pre-existing" |
| 3679 | " file in the filesystem being appended" |
| 3680 | " to. It cannot be modified. " |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3681 | "Ignoring.\n", pseudo_ent->pathname); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3682 | continue; |
| 3683 | } |
| 3684 | buf = &dir_ent->inode->buf; |
| 3685 | buf->st_mode = (buf->st_mode & S_IFMT) | |
| 3686 | pseudo_ent->dev->mode; |
| 3687 | buf->st_uid = pseudo_ent->dev->uid; |
| 3688 | buf->st_gid = pseudo_ent->dev->gid; |
| 3689 | continue; |
| 3690 | } |
| 3691 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3692 | if(dir_ent) { |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3693 | if(dir_ent->inode->root_entry) |
| 3694 | ERROR("Pseudo file \"%s\" is a pre-existing" |
| 3695 | " file in the filesystem being appended" |
| 3696 | " to. Ignoring.\n", |
| 3697 | pseudo_ent->pathname); |
| 3698 | else |
| 3699 | ERROR("Pseudo file \"%s\" exists in source " |
| 3700 | "filesystem \"%s\"\n. Ignoring, " |
| 3701 | "exclude it (-e/-ef) to override.\n", |
| 3702 | pseudo_ent->pathname, |
| 3703 | dir_ent->pathname); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3704 | continue; |
| 3705 | } |
| 3706 | |
| 3707 | if(pseudo_ent->dev->type == 'd') { |
| 3708 | sub_dir = dir_scan2(NULL, pseudo_ent->pseudo); |
| 3709 | if(sub_dir == NULL) { |
| 3710 | ERROR("Could not create pseudo directory \"%s\"" |
| 3711 | ", skipping...\n", |
| 3712 | pseudo_ent->pathname); |
| 3713 | continue; |
| 3714 | } |
| 3715 | dir->directory_count ++; |
| 3716 | } else |
| 3717 | sub_dir = NULL; |
| 3718 | |
| 3719 | memset(&buf, 0, sizeof(buf)); |
| 3720 | buf.st_mode = pseudo_ent->dev->mode; |
| 3721 | buf.st_uid = pseudo_ent->dev->uid; |
| 3722 | buf.st_gid = pseudo_ent->dev->gid; |
| 3723 | buf.st_rdev = makedev(pseudo_ent->dev->major, |
| 3724 | pseudo_ent->dev->minor); |
plougher | 7e58f4d | 2009-04-05 12:06:19 +0000 | [diff] [blame] | 3725 | buf.st_mtime = time(NULL); |
plougher | 1a3fbf2 | 2009-04-05 12:04:16 +0000 | [diff] [blame] | 3726 | buf.st_ino = pseudo_ino ++; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3727 | |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3728 | if(pseudo_ent->dev->type == 'f') { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3729 | #ifdef USE_TMP_FILE |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3730 | struct stat buf2; |
| 3731 | int res = stat(pseudo_ent->dev->filename, &buf2); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3732 | struct inode_info *inode; |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3733 | if(res == -1) { |
| 3734 | ERROR("Stat on pseudo file \"%s\" failed, " |
| 3735 | "skipping...", pseudo_ent->pathname); |
| 3736 | continue; |
| 3737 | } |
| 3738 | buf.st_size = buf2.st_size; |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3739 | inode = lookup_inode(&buf); |
| 3740 | inode->pseudo_file = PSEUDO_FILE_OTHER; |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3741 | add_dir_entry(pseudo_ent->name, |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3742 | pseudo_ent->dev->filename, sub_dir, inode, |
| 3743 | dir); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3744 | #else |
| 3745 | struct inode_info *inode = lookup_inode(&buf); |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 3746 | inode->pseudo_id = pseudo_ent->dev->pseudo_id; |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3747 | inode->pseudo_file = PSEUDO_FILE_PROCESS; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 3748 | add_dir_entry(pseudo_ent->name, pseudo_ent->pathname, |
| 3749 | sub_dir, inode, dir); |
| 3750 | #endif |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3751 | } else { |
| 3752 | struct inode_info *inode = lookup_inode(&buf); |
| 3753 | inode->pseudo_file = PSEUDO_FILE_OTHER; |
plougher | 4ab7e51 | 2009-05-05 02:35:58 +0000 | [diff] [blame] | 3754 | add_dir_entry(pseudo_ent->name, pseudo_ent->pathname, |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3755 | sub_dir, inode, dir); |
| 3756 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | scan2_freedir(dir); |
| 3760 | sort_directory(dir); |
| 3761 | |
| 3762 | return dir; |
| 3763 | } |
| 3764 | |
| 3765 | |
| 3766 | void dir_scan3(squashfs_inode *inode, struct dir_info *dir_info) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3767 | { |
| 3768 | int squashfs_type; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3769 | int duplicate_file; |
plougher | e4e9209 | 2010-05-01 04:51:50 +0000 | [diff] [blame] | 3770 | char *pathname = dir_info->dir_ent->pathname; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3771 | struct directory dir; |
| 3772 | struct dir_ent *dir_ent; |
| 3773 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3774 | scan3_init_dir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3775 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3776 | while((dir_ent = scan3_readdir(&dir, dir_info)) != NULL) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3777 | struct inode_info *inode_info = dir_ent->inode; |
| 3778 | struct stat *buf = &inode_info->buf; |
| 3779 | char *filename = dir_ent->pathname; |
| 3780 | char *dir_name = dir_ent->name; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3781 | unsigned int inode_number = ((buf->st_mode & S_IFMT) == S_IFDIR) |
| 3782 | ? dir_ent->inode->inode_number : |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3783 | dir_ent->inode->inode_number + dir_inode_no; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3784 | |
| 3785 | if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) { |
| 3786 | switch(buf->st_mode & S_IFMT) { |
| 3787 | case S_IFREG: |
| 3788 | squashfs_type = SQUASHFS_FILE_TYPE; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3789 | write_file(inode, dir_ent, |
| 3790 | &duplicate_file); |
| 3791 | INFO("file %s, uncompressed size %lld " |
| 3792 | "bytes %s\n", filename, |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3793 | (long long) buf->st_size, |
| 3794 | duplicate_file ? "DUPLICATE" : |
| 3795 | ""); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3796 | break; |
| 3797 | |
| 3798 | case S_IFDIR: |
| 3799 | squashfs_type = SQUASHFS_DIR_TYPE; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3800 | dir_scan3(inode, dir_ent->dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3801 | break; |
| 3802 | |
| 3803 | case S_IFLNK: |
| 3804 | squashfs_type = SQUASHFS_SYMLINK_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3805 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3806 | squashfs_type, 0, 0, 0, NULL, |
| 3807 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3808 | INFO("symbolic link %s inode 0x%llx\n", |
| 3809 | dir_name, *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3810 | sym_count ++; |
| 3811 | break; |
| 3812 | |
| 3813 | case S_IFCHR: |
| 3814 | squashfs_type = SQUASHFS_CHRDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3815 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3816 | squashfs_type, 0, 0, 0, NULL, |
| 3817 | NULL, NULL, 0); |
| 3818 | INFO("character device %s inode 0x%llx" |
| 3819 | "\n", dir_name, *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3820 | dev_count ++; |
| 3821 | break; |
| 3822 | |
| 3823 | case S_IFBLK: |
| 3824 | squashfs_type = SQUASHFS_BLKDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3825 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3826 | squashfs_type, 0, 0, 0, NULL, |
| 3827 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3828 | INFO("block device %s inode 0x%llx\n", |
| 3829 | dir_name, *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3830 | dev_count ++; |
| 3831 | break; |
| 3832 | |
| 3833 | case S_IFIFO: |
| 3834 | squashfs_type = SQUASHFS_FIFO_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3835 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3836 | squashfs_type, 0, 0, 0, NULL, |
| 3837 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3838 | INFO("fifo %s inode 0x%llx\n",dir_name, |
| 3839 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3840 | fifo_count ++; |
| 3841 | break; |
| 3842 | |
| 3843 | case S_IFSOCK: |
| 3844 | squashfs_type = SQUASHFS_SOCKET_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3845 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3846 | squashfs_type, 0, 0, 0, NULL, |
| 3847 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3848 | INFO("unix domain socket %s inode " |
| 3849 | "0x%llx\n", dir_name, *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3850 | sock_count ++; |
| 3851 | break; |
| 3852 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 3853 | default: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3854 | BAD_ERROR("%s unrecognised file type, " |
| 3855 | "mode is %x\n", filename, |
| 3856 | buf->st_mode); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3857 | } |
| 3858 | dir_ent->inode->inode = *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3859 | dir_ent->inode->type = squashfs_type; |
| 3860 | } else { |
| 3861 | *inode = dir_ent->inode->inode; |
| 3862 | squashfs_type = dir_ent->inode->type; |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3863 | switch(squashfs_type) { |
| 3864 | case SQUASHFS_FILE_TYPE: |
| 3865 | if(!sorted) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3866 | INFO("file %s, uncompressed " |
| 3867 | "size %lld bytes LINK" |
| 3868 | "\n", filename, |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3869 | (long long) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3870 | buf->st_size); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3871 | break; |
| 3872 | case SQUASHFS_SYMLINK_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3873 | INFO("symbolic link %s inode 0x%llx " |
| 3874 | "LINK\n", dir_name, *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3875 | break; |
| 3876 | case SQUASHFS_CHRDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3877 | INFO("character device %s inode 0x%llx " |
| 3878 | "LINK\n", dir_name, *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3879 | break; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3880 | case SQUASHFS_BLKDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3881 | INFO("block device %s inode 0x%llx " |
| 3882 | "LINK\n", dir_name, *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3883 | break; |
| 3884 | case SQUASHFS_FIFO_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3885 | INFO("fifo %s inode 0x%llx LINK\n", |
| 3886 | dir_name, *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3887 | break; |
| 3888 | case SQUASHFS_SOCKET_TYPE: |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3889 | INFO("unix domain socket %s inode " |
| 3890 | "0x%llx LINK\n", dir_name, |
| 3891 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3892 | break; |
| 3893 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3896 | add_dir(*inode, inode_number, dir_name, squashfs_type, &dir); |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 3897 | update_progress_bar(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3900 | write_dir(inode, dir_info, &dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3901 | INFO("directory %s inode 0x%llx\n", pathname, *inode); |
| 3902 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3903 | scan3_freedir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | |
| 3907 | unsigned int slog(unsigned int block) |
| 3908 | { |
| 3909 | int i; |
| 3910 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 3911 | for(i = 12; i <= 20; i++) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3912 | if(block == (1 << i)) |
| 3913 | return i; |
| 3914 | return 0; |
| 3915 | } |
| 3916 | |
| 3917 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3918 | int old_excluded(char *filename, struct stat *buf) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3919 | { |
| 3920 | int i; |
| 3921 | |
| 3922 | for(i = 0; i < exclude; i++) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3923 | if((exclude_paths[i].st_dev == buf->st_dev) && |
| 3924 | (exclude_paths[i].st_ino == buf->st_ino)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3925 | return TRUE; |
| 3926 | return FALSE; |
| 3927 | } |
| 3928 | |
| 3929 | |
| 3930 | #define ADD_ENTRY(buf) \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3931 | if(exclude % EXCLUDE_SIZE == 0) { \ |
| 3932 | exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \ |
| 3933 | * sizeof(struct exclude_info)); \ |
| 3934 | if(exclude_paths == NULL) \ |
| 3935 | BAD_ERROR("Out of memory in exclude dir/file table\n"); \ |
| 3936 | } \ |
| 3937 | exclude_paths[exclude].st_dev = buf.st_dev; \ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3938 | exclude_paths[exclude++].st_ino = buf.st_ino; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3939 | int old_add_exclude(char *path) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3940 | { |
| 3941 | int i; |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 3942 | char filename[4096]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3943 | struct stat buf; |
| 3944 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3945 | if(path[0] == '/' || strncmp(path, "./", 2) == 0 || |
| 3946 | strncmp(path, "../", 3) == 0) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3947 | if(lstat(path, &buf) == -1) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3948 | ERROR("Cannot stat exclude dir/file %s because %s, " |
| 3949 | "ignoring", path, strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3950 | return TRUE; |
| 3951 | } |
| 3952 | ADD_ENTRY(buf); |
| 3953 | return TRUE; |
| 3954 | } |
| 3955 | |
| 3956 | for(i = 0; i < source; i++) { |
| 3957 | strcat(strcat(strcpy(filename, source_path[i]), "/"), path); |
| 3958 | if(lstat(filename, &buf) == -1) { |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 3959 | if(!(errno == ENOENT || errno == ENOTDIR)) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3960 | ERROR("Cannot stat exclude dir/file %s because " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3961 | "%s, ignoring", filename, |
| 3962 | strerror(errno)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3963 | continue; |
| 3964 | } |
| 3965 | ADD_ENTRY(buf); |
| 3966 | } |
| 3967 | return TRUE; |
| 3968 | } |
| 3969 | |
| 3970 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3971 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 3972 | int type) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3973 | { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3974 | old_root_entry = realloc(old_root_entry, |
| 3975 | sizeof(struct old_root_entry_info) * (old_root_entries + 1)); |
| 3976 | if(old_root_entry == NULL) |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3977 | BAD_ERROR("Out of memory in old root directory entries " |
| 3978 | "reallocation\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3979 | |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3980 | old_root_entry[old_root_entries].name = strdup(name); |
| 3981 | old_root_entry[old_root_entries].inode.inode = inode; |
| 3982 | old_root_entry[old_root_entries].inode.inode_number = inode_number; |
| 3983 | old_root_entry[old_root_entries].inode.type = type; |
| 3984 | old_root_entry[old_root_entries++].inode.root_entry = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
| 3987 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3988 | void initialise_threads() |
| 3989 | { |
| 3990 | int i; |
| 3991 | sigset_t sigmask, old_mask; |
| 3992 | |
| 3993 | sigemptyset(&sigmask); |
| 3994 | sigaddset(&sigmask, SIGINT); |
| 3995 | sigaddset(&sigmask, SIGQUIT); |
| 3996 | if(sigprocmask(SIG_BLOCK, &sigmask, &old_mask) == -1) |
| 3997 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 3998 | |
| 3999 | signal(SIGUSR1, sigusr1_handler); |
| 4000 | |
| 4001 | if(processors == -1) { |
| 4002 | #ifndef linux |
| 4003 | int mib[2]; |
| 4004 | size_t len = sizeof(processors); |
| 4005 | |
| 4006 | mib[0] = CTL_HW; |
| 4007 | #ifdef HW_AVAILCPU |
| 4008 | mib[1] = HW_AVAILCPU; |
| 4009 | #else |
| 4010 | mib[1] = HW_NCPU; |
| 4011 | #endif |
| 4012 | |
| 4013 | if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4014 | ERROR("Failed to get number of available processors. " |
| 4015 | "Defaulting to 1\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4016 | processors = 1; |
| 4017 | } |
| 4018 | #else |
| 4019 | processors = get_nprocs(); |
| 4020 | #endif |
| 4021 | } |
| 4022 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4023 | if((thread = malloc((2 + processors * 2) * sizeof(pthread_t))) == NULL) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4024 | BAD_ERROR("Out of memory allocating thread descriptors\n"); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4025 | deflator_thread = &thread[2]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4026 | frag_deflator_thread = &deflator_thread[processors]; |
| 4027 | |
| 4028 | to_reader = queue_init(1); |
| 4029 | from_reader = queue_init(reader_buffer_size); |
| 4030 | to_writer = queue_init(writer_buffer_size); |
| 4031 | from_writer = queue_init(1); |
| 4032 | from_deflate = queue_init(reader_buffer_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 4033 | to_frag = queue_init(fragment_buffer_size); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 4034 | reader_buffer = cache_init(block_size, reader_buffer_size); |
| 4035 | writer_buffer = cache_init(block_size, writer_buffer_size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 4036 | fragment_buffer = cache_init(block_size, fragment_buffer_size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4037 | pthread_create(&thread[0], NULL, reader, NULL); |
| 4038 | pthread_create(&thread[1], NULL, writer, NULL); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4039 | pthread_create(&progress_thread, NULL, progress_thrd, NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4040 | pthread_mutex_init(&fragment_mutex, NULL); |
| 4041 | pthread_cond_init(&fragment_waiting, NULL); |
| 4042 | |
| 4043 | for(i = 0; i < processors; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4044 | if(pthread_create(&deflator_thread[i], NULL, deflator, NULL) != |
| 4045 | 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4046 | BAD_ERROR("Failed to create thread\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4047 | if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator, |
| 4048 | NULL) != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4049 | BAD_ERROR("Failed to create thread\n"); |
| 4050 | } |
| 4051 | |
| 4052 | printf("Parallel mksquashfs: Using %d processor%s\n", processors, |
| 4053 | processors == 1 ? "" : "s"); |
| 4054 | |
| 4055 | if(sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) |
| 4056 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4057 | } |
| 4058 | |
| 4059 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4060 | long long write_inode_lookup_table() |
| 4061 | { |
| 4062 | int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4063 | void *it; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4064 | |
| 4065 | if(inode_count == sinode_count) |
| 4066 | goto skip_inode_hash_table; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4067 | |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4068 | it = realloc(inode_lookup_table, lookup_bytes); |
| 4069 | if(it == NULL) |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4070 | BAD_ERROR("Out of memory in write_inode_table\n"); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4071 | inode_lookup_table = it; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4072 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4073 | for(i = 0; i < INODE_HASH_SIZE; i ++) { |
| 4074 | struct inode_info *inode = inode_info[i]; |
| 4075 | |
| 4076 | for(inode = inode_info[i]; inode; inode = inode->next) { |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4077 | |
| 4078 | inode_number = inode->type == SQUASHFS_DIR_TYPE ? |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4079 | inode->inode_number : inode->inode_number + |
| 4080 | dir_inode_no; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4081 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4082 | SQUASHFS_SWAP_LONG_LONGS(&inode->inode, |
| 4083 | &inode_lookup_table[inode_number - 1], 1); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4084 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4085 | } |
| 4086 | } |
| 4087 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4088 | skip_inode_hash_table: |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4089 | return generic_write_table(lookup_bytes, (char *) inode_lookup_table, |
plougher | 85c5626 | 2010-07-20 01:00:57 +0000 | [diff] [blame] | 4090 | 0, NULL, noI); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4093 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4094 | char *get_component(char *target, char *targname) |
| 4095 | { |
| 4096 | while(*target == '/') |
rlougher | c4ebcf5 | 2007-11-08 17:52:49 +0000 | [diff] [blame] | 4097 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4098 | |
| 4099 | while(*target != '/' && *target!= '\0') |
| 4100 | *targname ++ = *target ++; |
| 4101 | |
| 4102 | *targname = '\0'; |
| 4103 | |
| 4104 | return target; |
| 4105 | } |
| 4106 | |
| 4107 | |
| 4108 | void free_path(struct pathname *paths) |
| 4109 | { |
| 4110 | int i; |
| 4111 | |
| 4112 | for(i = 0; i < paths->names; i++) { |
| 4113 | if(paths->name[i].paths) |
| 4114 | free_path(paths->name[i].paths); |
| 4115 | free(paths->name[i].name); |
| 4116 | if(paths->name[i].preg) { |
| 4117 | regfree(paths->name[i].preg); |
| 4118 | free(paths->name[i].preg); |
| 4119 | } |
| 4120 | } |
| 4121 | |
| 4122 | free(paths); |
| 4123 | } |
| 4124 | |
| 4125 | |
| 4126 | struct pathname *add_path(struct pathname *paths, char *target, char *alltarget) |
| 4127 | { |
| 4128 | char targname[1024]; |
| 4129 | int i, error; |
| 4130 | |
| 4131 | target = get_component(target, targname); |
| 4132 | |
| 4133 | if(paths == NULL) { |
| 4134 | if((paths = malloc(sizeof(struct pathname))) == NULL) |
| 4135 | BAD_ERROR("failed to allocate paths\n"); |
| 4136 | |
| 4137 | paths->names = 0; |
| 4138 | paths->name = NULL; |
| 4139 | } |
| 4140 | |
| 4141 | for(i = 0; i < paths->names; i++) |
| 4142 | if(strcmp(paths->name[i].name, targname) == 0) |
| 4143 | break; |
| 4144 | |
| 4145 | if(i == paths->names) { |
| 4146 | /* allocate new name entry */ |
| 4147 | paths->names ++; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4148 | paths->name = realloc(paths->name, (i + 1) * |
| 4149 | sizeof(struct path_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4150 | if(paths->name == NULL) |
| 4151 | BAD_ERROR("Out of memory in add path\n"); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4152 | paths->name[i].name = strdup(targname); |
| 4153 | paths->name[i].paths = NULL; |
| 4154 | if(use_regex) { |
| 4155 | paths->name[i].preg = malloc(sizeof(regex_t)); |
plougher | 5c60eab | 2010-07-21 01:12:19 +0000 | [diff] [blame] | 4156 | if(paths->name[i].preg == NULL) |
| 4157 | BAD_ERROR("Out of memory in add_path\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4158 | error = regcomp(paths->name[i].preg, targname, |
| 4159 | REG_EXTENDED|REG_NOSUB); |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4160 | if(error) { |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4161 | char str[1024]; |
| 4162 | |
| 4163 | regerror(error, paths->name[i].preg, str, 1024); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4164 | BAD_ERROR("invalid regex %s in export %s, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4165 | "because %s\n", targname, alltarget, |
| 4166 | str); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4167 | } |
| 4168 | } else |
| 4169 | paths->name[i].preg = NULL; |
| 4170 | |
| 4171 | if(target[0] == '\0') |
| 4172 | /* at leaf pathname component */ |
| 4173 | paths->name[i].paths = NULL; |
| 4174 | else |
| 4175 | /* recurse adding child components */ |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4176 | paths->name[i].paths = add_path(NULL, target, |
| 4177 | alltarget); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4178 | } else { |
| 4179 | /* existing matching entry */ |
| 4180 | if(paths->name[i].paths == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4181 | /* No sub-directory which means this is the leaf |
| 4182 | * component of a pre-existing exclude which subsumes |
| 4183 | * the exclude currently being added, in which case stop |
| 4184 | * adding components */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4185 | } else if(target[0] == '\0') { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4186 | /* at leaf pathname component and child components exist |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4187 | * from more specific excludes, delete as they're |
| 4188 | * subsumed by this exclude */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4189 | free_path(paths->name[i].paths); |
| 4190 | paths->name[i].paths = NULL; |
| 4191 | } else |
| 4192 | /* recurse adding child components */ |
| 4193 | add_path(paths->name[i].paths, target, alltarget); |
| 4194 | } |
| 4195 | |
| 4196 | return paths; |
| 4197 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4198 | |
| 4199 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4200 | void add_exclude(char *target) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4201 | { |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4202 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4203 | if(target[0] == '/' || strncmp(target, "./", 2) == 0 || |
| 4204 | strncmp(target, "../", 3) == 0) |
| 4205 | BAD_ERROR("/, ./ and ../ prefixed excludes not supported with " |
| 4206 | "-wildcards or -regex options\n"); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4207 | else if(strncmp(target, "... ", 4) == 0) |
| 4208 | stickypath = add_path(stickypath, target + 4, target + 4); |
| 4209 | else |
| 4210 | path = add_path(path, target, target); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | |
| 4214 | void display_path(int depth, struct pathname *paths) |
| 4215 | { |
| 4216 | int i, n; |
| 4217 | |
| 4218 | if(paths == NULL) |
| 4219 | return; |
| 4220 | |
| 4221 | for(i = 0; i < paths->names; i++) { |
| 4222 | for(n = 0; n < depth; n++) |
| 4223 | printf("\t"); |
| 4224 | printf("%d: %s\n", depth, paths->name[i].name); |
| 4225 | display_path(depth + 1, paths->name[i].paths); |
| 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | |
| 4230 | void display_path2(struct pathname *paths, char *string) |
| 4231 | { |
| 4232 | int i; |
| 4233 | char path[1024]; |
| 4234 | |
| 4235 | if(paths == NULL) { |
| 4236 | printf("%s\n", string); |
| 4237 | return; |
| 4238 | } |
| 4239 | |
| 4240 | for(i = 0; i < paths->names; i++) { |
| 4241 | strcat(strcat(strcpy(path, string), "/"), paths->name[i].name); |
| 4242 | display_path2(paths->name[i].paths, path); |
| 4243 | } |
| 4244 | } |
| 4245 | |
| 4246 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4247 | struct pathnames *init_subdir() |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4248 | { |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 4249 | struct pathnames *new = malloc(sizeof(struct pathnames)); |
plougher | d86ee82 | 2010-07-21 01:14:26 +0000 | [diff] [blame] | 4250 | if(new == NULL) |
| 4251 | BAD_ERROR("Out of memory in init_subdir\n"); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4252 | new->count = 0; |
| 4253 | return new; |
| 4254 | } |
| 4255 | |
| 4256 | |
| 4257 | struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path) |
| 4258 | { |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4259 | if(paths->count % PATHS_ALLOC_SIZE == 0) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4260 | paths = realloc(paths, sizeof(struct pathnames *) + |
| 4261 | (paths->count + PATHS_ALLOC_SIZE) * |
| 4262 | sizeof(struct pathname *)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4263 | if(paths == NULL) |
| 4264 | BAD_ERROR("Out of memory in add_subdir\n"); |
| 4265 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4266 | |
| 4267 | paths->path[paths->count++] = path; |
| 4268 | return paths; |
| 4269 | } |
| 4270 | |
| 4271 | |
| 4272 | void free_subdir(struct pathnames *paths) |
| 4273 | { |
| 4274 | free(paths); |
| 4275 | } |
| 4276 | |
| 4277 | |
| 4278 | int excluded(struct pathnames *paths, char *name, struct pathnames **new) |
| 4279 | { |
| 4280 | int i, n, res; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4281 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4282 | if(paths == NULL) { |
| 4283 | *new = NULL; |
| 4284 | return FALSE; |
| 4285 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4286 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4287 | |
| 4288 | *new = init_subdir(); |
plougher | 806581a | 2007-10-23 15:41:30 +0000 | [diff] [blame] | 4289 | if(stickypath) |
| 4290 | *new = add_subdir(*new, stickypath); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4291 | |
| 4292 | for(n = 0; n < paths->count; n++) { |
| 4293 | struct pathname *path = paths->path[n]; |
| 4294 | |
| 4295 | for(i = 0; i < path->names; i++) { |
| 4296 | int match = use_regex ? |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4297 | regexec(path->name[i].preg, name, (size_t) 0, |
| 4298 | NULL, 0) == 0 : |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4299 | fnmatch(path->name[i].name, name, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4300 | FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == |
| 4301 | 0; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4302 | |
| 4303 | if(match && path->name[i].paths == NULL) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4304 | /* match on a leaf component, any subdirectories |
| 4305 | * in the filesystem should be excluded */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4306 | res = TRUE; |
| 4307 | goto empty_set; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4310 | if(match) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4311 | /* match on a non-leaf component, add any |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4312 | * subdirectories to the new set of |
| 4313 | * subdirectories to scan for this name */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4314 | *new = add_subdir(*new, path->name[i].paths); |
| 4315 | } |
| 4316 | } |
| 4317 | |
| 4318 | if((*new)->count == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4319 | /* no matching names found, return empty new search set |
| 4320 | */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4321 | res = FALSE; |
| 4322 | goto empty_set; |
| 4323 | } |
| 4324 | |
| 4325 | /* one or more matches with sub-directories found (no leaf matches). |
| 4326 | * Return new set */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4327 | return FALSE; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4328 | |
| 4329 | empty_set: |
| 4330 | free_subdir(*new); |
| 4331 | *new = NULL; |
| 4332 | return res; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4336 | #define RECOVER_ID "Squashfs recovery file v1.0\n" |
| 4337 | #define RECOVER_ID_SIZE 28 |
| 4338 | |
| 4339 | void write_recovery_data(squashfs_super_block *sBlk) |
| 4340 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4341 | int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4342 | pid_t pid = getpid(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4343 | char *metadata; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4344 | char header[] = RECOVER_ID; |
| 4345 | |
| 4346 | if(recover == FALSE) { |
| 4347 | printf("No recovery data option specified.\n"); |
plougher | eac1853 | 2007-10-29 05:26:06 +0000 | [diff] [blame] | 4348 | printf("Skipping saving recovery file.\n\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4349 | return; |
| 4350 | } |
| 4351 | |
| 4352 | if((metadata = malloc(bytes)) == NULL) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4353 | BAD_ERROR("Failed to alloc metadata buffer in " |
| 4354 | "write_recovery_data\n"); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4355 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4356 | res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata); |
| 4357 | if(res == 0) |
| 4358 | EXIT_MKSQUASHFS(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4359 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4360 | sprintf(recovery_file, "squashfs_recovery_%s_%d", |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4361 | getbase(destination_file), pid); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4362 | recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU); |
| 4363 | if(recoverfd == -1) |
| 4364 | BAD_ERROR("Failed to create recovery file, because %s. " |
| 4365 | "Aborting\n", strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4366 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4367 | if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4368 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4369 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4370 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4371 | if(write_bytes(recoverfd, sBlk, sizeof(squashfs_super_block)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4372 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4373 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4374 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4375 | if(write_bytes(recoverfd, metadata, bytes) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4376 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4377 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4378 | |
| 4379 | close(recoverfd); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4380 | free(metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4381 | |
| 4382 | printf("Recovery file \"%s\" written\n", recovery_file); |
| 4383 | printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4384 | printf("mksquashfs dummy %s -recover %s\n", destination_file, |
| 4385 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4386 | printf("to restore filesystem\n\n"); |
| 4387 | } |
| 4388 | |
| 4389 | |
| 4390 | void read_recovery_data(char *recovery_file, char *destination_file) |
| 4391 | { |
| 4392 | int fd, recoverfd, bytes; |
| 4393 | squashfs_super_block orig_sBlk, sBlk; |
| 4394 | char *metadata; |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4395 | int res; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4396 | struct stat buf; |
| 4397 | char header[] = RECOVER_ID; |
| 4398 | char header2[RECOVER_ID_SIZE]; |
| 4399 | |
| 4400 | if((recoverfd = open(recovery_file, O_RDONLY)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4401 | BAD_ERROR("Failed to open recovery file because %s\n", |
| 4402 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4403 | |
| 4404 | if(stat(destination_file, &buf) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4405 | BAD_ERROR("Failed to stat destination file, because %s\n", |
| 4406 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4407 | |
| 4408 | if((fd = open(destination_file, O_RDWR)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4409 | BAD_ERROR("Failed to open destination file because %s\n", |
| 4410 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4411 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4412 | res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE); |
| 4413 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4414 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4415 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4416 | if(res < RECOVER_ID_SIZE) |
| 4417 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4418 | if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 ) |
| 4419 | BAD_ERROR("Not a recovery file\n"); |
| 4420 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4421 | res = read_bytes(recoverfd, &sBlk, sizeof(squashfs_super_block)); |
| 4422 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4423 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4424 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4425 | if(res < sizeof(squashfs_super_block)) |
| 4426 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4427 | |
plougher | 3306cb2 | 2010-06-18 04:22:44 +0000 | [diff] [blame] | 4428 | res = read_fs_bytes(fd, 0, sizeof(squashfs_super_block), &orig_sBlk); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4429 | if(res == 0) |
| 4430 | EXIT_MKSQUASHFS(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4431 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4432 | if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4, |
| 4433 | sizeof(squashfs_super_block) - 4) != 0) |
| 4434 | BAD_ERROR("Recovery file and destination file do not seem to " |
| 4435 | "match\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4436 | |
| 4437 | bytes = sBlk.bytes_used - sBlk.inode_table_start; |
| 4438 | |
| 4439 | if((metadata = malloc(bytes)) == NULL) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4440 | BAD_ERROR("Failed to alloc metadata buffer in " |
| 4441 | "read_recovery_data\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4442 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4443 | res = read_bytes(recoverfd, metadata, bytes); |
| 4444 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4445 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4446 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4447 | if(res < bytes) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4448 | BAD_ERROR("Recovery file appears to be truncated\n"); |
| 4449 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 4450 | write_destination(fd, 0, sizeof(squashfs_super_block), (char *) &sBlk); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4451 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 4452 | write_destination(fd, sBlk.inode_table_start, bytes, metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4453 | |
| 4454 | close(recoverfd); |
| 4455 | close(fd); |
| 4456 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4457 | printf("Successfully wrote recovery file \"%s\". Exiting\n", |
| 4458 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4459 | |
| 4460 | exit(0); |
| 4461 | } |
| 4462 | |
| 4463 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4464 | #define VERSION() \ |
plougher | fdee12a | 2010-07-19 17:50:28 +0000 | [diff] [blame] | 4465 | printf("mksquashfs version 4.1-CVS (2010/07/19)\n");\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4466 | printf("copyright (C) 2010 Phillip Lougher "\ |
| 4467 | "<phillip@lougher.demon.co.uk>\n\n"); \ |
| 4468 | printf("This program is free software; you can redistribute it and/or"\ |
| 4469 | "\n");\ |
| 4470 | printf("modify it under the terms of the GNU General Public License"\ |
| 4471 | "\n");\ |
| 4472 | printf("as published by the Free Software Foundation; either version "\ |
| 4473 | "2,\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4474 | printf("or (at your option) any later version.\n\n");\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4475 | printf("This program is distributed in the hope that it will be "\ |
| 4476 | "useful,\n");\ |
| 4477 | printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\ |
| 4478 | "of\n");\ |
| 4479 | printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\ |
| 4480 | "\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4481 | printf("GNU General Public License for more details.\n"); |
| 4482 | int main(int argc, char *argv[]) |
| 4483 | { |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4484 | struct stat buf, source_buf; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4485 | int i; |
| 4486 | squashfs_super_block sBlk; |
| 4487 | char *b, *root_name = NULL; |
plougher | 6c65b03 | 2008-08-07 09:13:24 +0000 | [diff] [blame] | 4488 | int nopad = FALSE, keep_as_directory = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4489 | squashfs_inode inode; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4490 | int readb_mbytes = READER_BUFFER_DEFAULT, |
| 4491 | writeb_mbytes = WRITER_BUFFER_DEFAULT, |
| 4492 | fragmentb_mbytes = FRAGMENT_BUFFER_DEFAULT; |
plougher | f11dfd6 | 2009-01-26 03:56:07 +0000 | [diff] [blame] | 4493 | int s_minor = SQUASHFS_MINOR; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4494 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4495 | pthread_mutex_init(&progress_mutex, NULL); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4496 | block_log = slog(block_size); |
| 4497 | if(argc > 1 && strcmp(argv[1], "-version") == 0) { |
| 4498 | VERSION(); |
| 4499 | exit(0); |
| 4500 | } |
| 4501 | for(i = 1; i < argc && argv[i][0] != '-'; i++); |
| 4502 | if(i < 3) |
| 4503 | goto printOptions; |
| 4504 | source_path = argv + 1; |
| 4505 | source = i - 2; |
| 4506 | for(; i < argc; i++) { |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 4507 | if(strcmp(argv[i], "-comp") == 0) { |
| 4508 | if(++i == argc) { |
| 4509 | ERROR("%s: -comp missing compression type\n", |
| 4510 | argv[0]); |
| 4511 | exit(1); |
| 4512 | } |
| 4513 | comp_name = argv[i]; |
| 4514 | } else if(strcmp(argv[i], "-pf") == 0) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4515 | if(++i == argc) { |
| 4516 | ERROR("%s: -pf missing filename\n", argv[0]); |
| 4517 | exit(1); |
| 4518 | } |
plougher | 620b717 | 2009-09-10 04:17:48 +0000 | [diff] [blame] | 4519 | if(read_pseudo_file(&pseudo, argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4520 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4521 | } else if(strcmp(argv[i], "-p") == 0) { |
| 4522 | if(++i == argc) { |
| 4523 | ERROR("%s: -p missing pseudo file definition\n", |
| 4524 | argv[0]); |
| 4525 | exit(1); |
| 4526 | } |
plougher | 620b717 | 2009-09-10 04:17:48 +0000 | [diff] [blame] | 4527 | if(read_pseudo_def(&pseudo, argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4528 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4529 | } else if(strcmp(argv[i], "-recover") == 0) { |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4530 | if(++i == argc) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4531 | ERROR("%s: -recover missing recovery file\n", |
| 4532 | argv[0]); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4533 | exit(1); |
| 4534 | } |
| 4535 | read_recovery_data(argv[i], argv[source + 1]); |
| 4536 | } else if(strcmp(argv[i], "-no-recovery") == 0) |
| 4537 | recover = FALSE; |
| 4538 | else if(strcmp(argv[i], "-wildcards") == 0) { |
plougher | 934a9ed | 2007-10-19 00:21:10 +0000 | [diff] [blame] | 4539 | old_exclude = FALSE; |
| 4540 | use_regex = FALSE; |
| 4541 | } else if(strcmp(argv[i], "-regex") == 0) { |
| 4542 | old_exclude = FALSE; |
| 4543 | use_regex = TRUE; |
| 4544 | } else if(strcmp(argv[i], "-no-sparse") == 0) |
plougher | 1f54edc | 2007-08-12 23:13:36 +0000 | [diff] [blame] | 4545 | sparse_files = FALSE; |
| 4546 | else if(strcmp(argv[i], "-no-progress") == 0) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4547 | progress = FALSE; |
| 4548 | else if(strcmp(argv[i], "-no-exports") == 0) |
| 4549 | exportable = FALSE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4550 | else if(strcmp(argv[i], "-processors") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4551 | if((++i == argc) || (processors = |
| 4552 | strtol(argv[i], &b, 10), *b != '\0')) { |
| 4553 | ERROR("%s: -processors missing or invalid " |
| 4554 | "processor number\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4555 | exit(1); |
| 4556 | } |
| 4557 | if(processors < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4558 | ERROR("%s: -processors should be 1 or larger\n", |
| 4559 | argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4560 | exit(1); |
| 4561 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4562 | } else if(strcmp(argv[i], "-read-queue") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4563 | if((++i == argc) || (readb_mbytes = |
| 4564 | strtol(argv[i], &b, 10), *b != '\0')) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4565 | ERROR("%s: -read-queue missing or invalid " |
| 4566 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4567 | exit(1); |
| 4568 | } |
| 4569 | if(readb_mbytes < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4570 | ERROR("%s: -read-queue should be 1 megabyte or " |
| 4571 | "larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4572 | exit(1); |
| 4573 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4574 | } else if(strcmp(argv[i], "-write-queue") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4575 | if((++i == argc) || (writeb_mbytes = |
| 4576 | strtol(argv[i], &b, 10), *b != '\0')) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4577 | ERROR("%s: -write-queue missing or invalid " |
| 4578 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4579 | exit(1); |
| 4580 | } |
| 4581 | if(writeb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4582 | ERROR("%s: -write-queue should be 1 megabyte " |
| 4583 | "or larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4584 | exit(1); |
| 4585 | } |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4586 | } else if(strcmp(argv[i], "-fragment-queue") == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4587 | if((++i == argc) || |
| 4588 | (fragmentb_mbytes = |
| 4589 | strtol(argv[i], &b, 10), *b != '\0')) { |
| 4590 | ERROR("%s: -fragment-queue missing or invalid " |
| 4591 | "queue size\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4592 | exit(1); |
| 4593 | } |
| 4594 | if(fragmentb_mbytes < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4595 | ERROR("%s: -fragment-queue should be 1 " |
| 4596 | "megabyte or larger\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 4597 | exit(1); |
| 4598 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4599 | } else if(strcmp(argv[i], "-b") == 0) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4600 | if(++i == argc) { |
| 4601 | ERROR("%s: -b missing block size\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4602 | exit(1); |
| 4603 | } |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4604 | block_size = strtol(argv[i], &b, 10); |
| 4605 | if(*b == 'm' || *b == 'M') |
| 4606 | block_size *= 1048576; |
| 4607 | else if(*b == 'k' || *b == 'K') |
| 4608 | block_size *= 1024; |
| 4609 | else if(*b != '\0') { |
| 4610 | ERROR("%s: -b invalid block size\n", argv[0]); |
| 4611 | exit(1); |
| 4612 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4613 | if((block_log = slog(block_size)) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4614 | ERROR("%s: -b block size not power of two or " |
| 4615 | "not between 4096 and 1Mbyte\n", |
| 4616 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4617 | exit(1); |
| 4618 | } |
| 4619 | } else if(strcmp(argv[i], "-ef") == 0) { |
| 4620 | if(++i == argc) { |
| 4621 | ERROR("%s: -ef missing filename\n", argv[0]); |
| 4622 | exit(1); |
| 4623 | } |
plougher | 9b5bf8c | 2006-03-20 18:43:33 +0000 | [diff] [blame] | 4624 | } else if(strcmp(argv[i], "-no-duplicates") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4625 | duplicate_checking = FALSE; |
| 4626 | |
| 4627 | else if(strcmp(argv[i], "-no-fragments") == 0) |
| 4628 | no_fragments = TRUE; |
| 4629 | |
| 4630 | else if(strcmp(argv[i], "-always-use-fragments") == 0) |
| 4631 | always_use_fragments = TRUE; |
| 4632 | |
| 4633 | else if(strcmp(argv[i], "-sort") == 0) { |
| 4634 | if(++i == argc) { |
| 4635 | ERROR("%s: -sort missing filename\n", argv[0]); |
| 4636 | exit(1); |
| 4637 | } |
| 4638 | } else if(strcmp(argv[i], "-all-root") == 0 || |
| 4639 | strcmp(argv[i], "-root-owned") == 0) |
| 4640 | global_uid = global_gid = 0; |
| 4641 | |
| 4642 | else if(strcmp(argv[i], "-force-uid") == 0) { |
| 4643 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4644 | ERROR("%s: -force-uid missing uid or user\n", |
| 4645 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4646 | exit(1); |
| 4647 | } |
| 4648 | if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4649 | if(global_uid < 0 || global_uid > |
| 4650 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4651 | ERROR("%s: -force-uid uid out of range" |
| 4652 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4653 | exit(1); |
| 4654 | } |
| 4655 | } else { |
| 4656 | struct passwd *uid = getpwnam(argv[i]); |
| 4657 | if(uid) |
| 4658 | global_uid = uid->pw_uid; |
| 4659 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4660 | ERROR("%s: -force-uid invalid uid or " |
| 4661 | "unknown user\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4662 | exit(1); |
| 4663 | } |
| 4664 | } |
| 4665 | } else if(strcmp(argv[i], "-force-gid") == 0) { |
| 4666 | if(++i == argc) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4667 | ERROR("%s: -force-gid missing gid or group\n", |
| 4668 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4669 | exit(1); |
| 4670 | } |
| 4671 | if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4672 | if(global_gid < 0 || global_gid > |
| 4673 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4674 | ERROR("%s: -force-gid gid out of range" |
| 4675 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4676 | exit(1); |
| 4677 | } |
| 4678 | } else { |
| 4679 | struct group *gid = getgrnam(argv[i]); |
| 4680 | if(gid) |
| 4681 | global_gid = gid->gr_gid; |
| 4682 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4683 | ERROR("%s: -force-gid invalid gid or " |
| 4684 | "unknown group\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4685 | exit(1); |
| 4686 | } |
| 4687 | } |
| 4688 | } else if(strcmp(argv[i], "-noI") == 0 || |
| 4689 | strcmp(argv[i], "-noInodeCompression") == 0) |
| 4690 | noI = TRUE; |
| 4691 | |
| 4692 | else if(strcmp(argv[i], "-noD") == 0 || |
| 4693 | strcmp(argv[i], "-noDataCompression") == 0) |
| 4694 | noD = TRUE; |
| 4695 | |
| 4696 | else if(strcmp(argv[i], "-noF") == 0 || |
| 4697 | strcmp(argv[i], "-noFragmentCompression") == 0) |
| 4698 | noF = TRUE; |
| 4699 | |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 4700 | else if(strcmp(argv[i], "-noX") == 0 || |
| 4701 | strcmp(argv[i], "-noXattrCompression") == 0) |
| 4702 | noX = TRUE; |
| 4703 | |
plougher | ce564c6 | 2010-05-19 03:01:49 +0000 | [diff] [blame] | 4704 | else if(strcmp(argv[i], "-no-xattrs") == 0) |
| 4705 | no_xattrs = TRUE; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 4706 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4707 | else if(strcmp(argv[i], "-nopad") == 0) |
| 4708 | nopad = TRUE; |
| 4709 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4710 | else if(strcmp(argv[i], "-info") == 0) { |
| 4711 | silent = FALSE; |
| 4712 | progress = FALSE; |
| 4713 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4714 | |
| 4715 | else if(strcmp(argv[i], "-e") == 0) |
| 4716 | break; |
| 4717 | |
| 4718 | else if(strcmp(argv[i], "-noappend") == 0) |
| 4719 | delete = TRUE; |
| 4720 | |
| 4721 | else if(strcmp(argv[i], "-keep-as-directory") == 0) |
| 4722 | keep_as_directory = TRUE; |
| 4723 | |
| 4724 | else if(strcmp(argv[i], "-root-becomes") == 0) { |
| 4725 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4726 | ERROR("%s: -root-becomes: missing name\n", |
| 4727 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4728 | exit(1); |
| 4729 | } |
| 4730 | root_name = argv[i]; |
| 4731 | } else if(strcmp(argv[i], "-version") == 0) { |
| 4732 | VERSION(); |
| 4733 | } else { |
| 4734 | ERROR("%s: invalid option\n\n", argv[0]); |
| 4735 | printOptions: |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4736 | ERROR("SYNTAX:%s source1 source2 ... dest [options] " |
| 4737 | "[-e list of exclude\ndirs/files]\n", argv[0]); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 4738 | ERROR("\nFilesystem build options:\n"); |
plougher | ff5ea8b | 2009-08-07 19:33:10 +0000 | [diff] [blame] | 4739 | ERROR("-comp <comp>\t\tselect <comp> compression\n"); |
plougher | 13df178 | 2009-08-29 01:05:34 +0000 | [diff] [blame] | 4740 | ERROR("\t\t\tCompressors available:\n"); |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 4741 | display_compressors("\t\t\t", COMP_DEFAULT); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4742 | ERROR("-b <block_size>\t\tset data block to " |
| 4743 | "<block_size>. Default %d bytes\n", |
| 4744 | SQUASHFS_FILE_SIZE); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 4745 | ERROR("-no-exports\t\tdon't make the filesystem " |
| 4746 | "exportable via NFS\n"); |
| 4747 | ERROR("-no-sparse\t\tdon't detect sparse files\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4748 | ERROR("-no-xattrs\t\tdon't detect extended " |
| 4749 | "attributes\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4750 | ERROR("-noI\t\t\tdo not compress inode table\n"); |
| 4751 | ERROR("-noD\t\t\tdo not compress data blocks\n"); |
| 4752 | ERROR("-noF\t\t\tdo not compress fragment blocks\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4753 | ERROR("-noX\t\t\tdo not compress extended " |
| 4754 | "attributes\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4755 | ERROR("-no-fragments\t\tdo not use fragments\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4756 | ERROR("-always-use-fragments\tuse fragment blocks for " |
| 4757 | "files larger than block size\n"); |
| 4758 | ERROR("-no-duplicates\t\tdo not perform duplicate " |
| 4759 | "checking\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4760 | ERROR("-all-root\t\tmake all files owned by root\n"); |
| 4761 | ERROR("-force-uid uid\t\tset all file uids to uid\n"); |
| 4762 | ERROR("-force-gid gid\t\tset all file gids to gid\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4763 | ERROR("-nopad\t\t\tdo not pad filesystem to a multiple " |
| 4764 | "of 4K\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 4765 | ERROR("-keep-as-directory\tif one source directory is " |
| 4766 | "specified, create a root\n"); |
| 4767 | ERROR("\t\t\tdirectory containing that directory, " |
| 4768 | "rather than the\n"); |
| 4769 | ERROR("\t\t\tcontents of the directory\n"); |
| 4770 | ERROR("\nFilesystem filter options:\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4771 | ERROR("-p <pseudo-definition>\tAdd pseudo file " |
| 4772 | "definition\n"); |
| 4773 | ERROR("-pf <pseudo-file>\tAdd list of pseudo file " |
| 4774 | "definitions\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4775 | ERROR("-sort <sort_file>\tsort files according to " |
| 4776 | "priorities in <sort_file>. One\n"); |
| 4777 | ERROR("\t\t\tfile or dir with priority per line. " |
| 4778 | "Priority -32768 to\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4779 | ERROR("\t\t\t32767, default priority 0\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4780 | ERROR("-ef <exclude_file>\tlist of exclude dirs/files." |
| 4781 | " One per line\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4782 | ERROR("-wildcards\t\tAllow extended shell wildcards " |
| 4783 | "(globbing) to be used in\n\t\t\texclude " |
| 4784 | "dirs/files\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4785 | ERROR("-regex\t\t\tAllow POSIX regular expressions to " |
| 4786 | "be used in exclude\n\t\t\tdirs/files\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 4787 | ERROR("\nFilesystem append options:\n"); |
| 4788 | ERROR("-noappend\t\tdo not append to existing " |
| 4789 | "filesystem\n"); |
| 4790 | ERROR("-root-becomes <name>\twhen appending source " |
| 4791 | "files/directories, make the\n"); |
| 4792 | ERROR("\t\t\toriginal root become a subdirectory in " |
| 4793 | "the new root\n"); |
| 4794 | ERROR("\t\t\tcalled <name>, rather than adding the new " |
| 4795 | "source items\n"); |
| 4796 | ERROR("\t\t\tto the original root\n"); |
| 4797 | ERROR("\nMksquashfs runtime options:\n"); |
| 4798 | ERROR("-version\t\tprint version, licence and " |
| 4799 | "copyright message\n"); |
| 4800 | ERROR("-recover <name>\t\trecover filesystem data " |
| 4801 | "using recovery file <name>\n"); |
| 4802 | ERROR("-no-recovery\t\tdon't generate a recovery " |
| 4803 | "file\n"); |
| 4804 | ERROR("-info\t\t\tprint files written to filesystem\n"); |
| 4805 | ERROR("-no-progress\t\tdon't display the progress " |
| 4806 | "bar\n"); |
| 4807 | ERROR("-processors <number>\tUse <number> processors." |
| 4808 | " By default will use number of\n"); |
| 4809 | ERROR("\t\t\tprocessors available\n"); |
| 4810 | ERROR("-read-queue <size>\tSet input queue to <size> " |
| 4811 | "Mbytes. Default %d Mbytes\n", |
| 4812 | READER_BUFFER_DEFAULT); |
| 4813 | ERROR("-write-queue <size>\tSet output queue to <size> " |
| 4814 | "Mbytes. Default %d Mbytes\n", |
| 4815 | WRITER_BUFFER_DEFAULT); |
| 4816 | ERROR("-fragment-queue <size>\tSet fagment queue to " |
| 4817 | "<size> Mbytes. Default %d Mbytes\n", |
| 4818 | FRAGMENT_BUFFER_DEFAULT); |
| 4819 | ERROR("\nMiscellaneous options:\n"); |
| 4820 | ERROR("-root-owned\t\talternative name for -all-root" |
| 4821 | "\n"); |
| 4822 | ERROR("-noInodeCompression\talternative name for -noI" |
| 4823 | "\n"); |
| 4824 | ERROR("-noDataCompression\talternative name for -noD" |
| 4825 | "\n"); |
| 4826 | ERROR("-noFragmentCompression\talternative name for " |
| 4827 | "-noF\n"); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 4828 | ERROR("-noXattrCompression\talternative name for " |
| 4829 | "-noX\n"); |
plougher | 4366487 | 2009-12-08 18:02:54 +0000 | [diff] [blame] | 4830 | ERROR("\nCompressors available:\n"); |
| 4831 | display_compressors("", COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4832 | exit(1); |
| 4833 | } |
| 4834 | } |
| 4835 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4836 | reader_buffer_size = readb_mbytes << (20 - block_log); |
| 4837 | writer_buffer_size = writeb_mbytes << (20 - block_log); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 4838 | fragment_buffer_size = fragmentb_mbytes << (20 - block_log); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4839 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4840 | for(i = 0; i < source; i++) |
| 4841 | if(lstat(source_path[i], &source_buf) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4842 | fprintf(stderr, "Cannot stat source directory \"%s\" " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4843 | "because %s\n", source_path[i], |
| 4844 | strerror(errno)); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 4845 | EXIT_MKSQUASHFS(); |
| 4846 | } |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4847 | |
| 4848 | destination_file = argv[source + 1]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4849 | if(stat(argv[source + 1], &buf) == -1) { |
| 4850 | if(errno == ENOENT) { /* Does not exist */ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4851 | fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, |
plougher | 59dce67 | 2010-05-19 03:56:59 +0000 | [diff] [blame] | 4852 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4853 | if(fd == -1) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4854 | perror("Could not create destination file"); |
| 4855 | exit(1); |
| 4856 | } |
| 4857 | delete = TRUE; |
| 4858 | } else { |
| 4859 | perror("Could not stat destination file"); |
| 4860 | exit(1); |
| 4861 | } |
| 4862 | |
| 4863 | } else { |
| 4864 | if(S_ISBLK(buf.st_mode)) { |
| 4865 | if((fd = open(argv[source + 1], O_RDWR)) == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4866 | perror("Could not open block device as " |
| 4867 | "destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4868 | exit(1); |
| 4869 | } |
| 4870 | block_device = 1; |
| 4871 | |
| 4872 | } else if(S_ISREG(buf.st_mode)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4873 | fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | |
| 4874 | O_RDWR); |
| 4875 | if(fd == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4876 | perror("Could not open regular file for " |
| 4877 | "writing as destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4878 | exit(1); |
| 4879 | } |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4880 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4881 | else { |
| 4882 | ERROR("Destination not block device or regular file\n"); |
| 4883 | exit(1); |
| 4884 | } |
| 4885 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4886 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4887 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4888 | signal(SIGTERM, sighandler2); |
| 4889 | signal(SIGINT, sighandler2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4890 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4891 | /* |
| 4892 | * process the exclude files - must be done afer destination file has |
| 4893 | * been possibly created |
| 4894 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4895 | for(i = source + 2; i < argc; i++) |
| 4896 | if(strcmp(argv[i], "-ef") == 0) { |
| 4897 | FILE *fd; |
| 4898 | char filename[16385]; |
| 4899 | if((fd = fopen(argv[++i], "r")) == NULL) { |
| 4900 | perror("Could not open exclude file..."); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4901 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4902 | } |
| 4903 | while(fscanf(fd, "%16384[^\n]\n", filename) != EOF) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4904 | if(old_exclude) |
| 4905 | old_add_exclude(filename); |
| 4906 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4907 | add_exclude(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4908 | fclose(fd); |
| 4909 | } else if(strcmp(argv[i], "-e") == 0) |
| 4910 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 4911 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4912 | strcmp(argv[i], "-sort") == 0 || |
| 4913 | strcmp(argv[i], "-pf") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 4914 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4915 | i++; |
| 4916 | |
| 4917 | if(i != argc) { |
| 4918 | if(++i == argc) { |
| 4919 | ERROR("%s: -e missing arguments\n", argv[0]); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4920 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4921 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4922 | while(i < argc) |
| 4923 | if(old_exclude) |
| 4924 | old_add_exclude(argv[i++]); |
| 4925 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4926 | add_exclude(argv[i++]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | /* process the sort files - must be done afer the exclude files */ |
| 4930 | for(i = source + 2; i < argc; i++) |
| 4931 | if(strcmp(argv[i], "-sort") == 0) { |
plougher | e1c9a74 | 2010-07-21 16:59:05 +0000 | [diff] [blame] | 4932 | int res = read_sort_file(argv[++i], source, |
| 4933 | source_path); |
| 4934 | if(res == FALSE) |
| 4935 | BAD_ERROR("Failed to read sort file\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4936 | sorted ++; |
| 4937 | } else if(strcmp(argv[i], "-e") == 0) |
| 4938 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 4939 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 4940 | strcmp(argv[i], "-ef") == 0 || |
| 4941 | strcmp(argv[i], "-pf") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 4942 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4943 | i++; |
| 4944 | |
plougher | 62d5b5c | 2008-08-16 01:49:42 +0000 | [diff] [blame] | 4945 | #ifdef SQUASHFS_TRACE |
| 4946 | progress = FALSE; |
| 4947 | #endif |
| 4948 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4949 | if(!delete) { |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 4950 | comp = read_super(fd, &sBlk, argv[source + 1]); |
| 4951 | if(comp == NULL) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4952 | ERROR("Failed to read existing filesystem - will not " |
| 4953 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4954 | ERROR("To force Mksquashfs to write to this block " |
| 4955 | "device or file use -noappend\n"); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4956 | EXIT_MKSQUASHFS(); |
| 4957 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4958 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4959 | block_log = slog(block_size = sBlk.block_size); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4960 | s_minor = sBlk.s_minor; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4961 | noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags); |
| 4962 | noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags); |
| 4963 | noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4964 | no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags); |
| 4965 | always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags); |
| 4966 | duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4967 | exportable = SQUASHFS_EXPORTABLE(sBlk.flags); |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 4968 | } else { |
| 4969 | comp = lookup_compressor(comp_name); |
plougher | 394fe0d | 2009-08-04 04:27:12 +0000 | [diff] [blame] | 4970 | if(!comp->supported) { |
plougher | fbfb2f4 | 2009-08-03 02:33:25 +0000 | [diff] [blame] | 4971 | ERROR("FATAL_ERROR: Compressor \"%s\" is not " |
| 4972 | "supported!\n", comp_name); |
plougher | 13df178 | 2009-08-29 01:05:34 +0000 | [diff] [blame] | 4973 | ERROR("Compressors available:\n"); |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 4974 | display_compressors("", COMP_DEFAULT); |
plougher | fbfb2f4 | 2009-08-03 02:33:25 +0000 | [diff] [blame] | 4975 | EXIT_MKSQUASHFS(); |
plougher | 394fe0d | 2009-08-04 04:27:12 +0000 | [diff] [blame] | 4976 | } |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
| 4979 | initialise_threads(); |
| 4980 | |
| 4981 | if(delete) { |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 4982 | printf("Creating %d.%d filesystem on %s, block size %d.\n", |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4983 | SQUASHFS_MAJOR, s_minor, argv[source + 1], block_size); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4984 | bytes = sizeof(squashfs_super_block); |
| 4985 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4986 | unsigned int last_directory_block, inode_dir_offset, |
| 4987 | inode_dir_file_size, root_inode_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4988 | inode_dir_start_block, uncompressed_data, |
| 4989 | compressed_data, inode_dir_inode_number, |
| 4990 | inode_dir_parent_inode; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4991 | unsigned int root_inode_start = |
| 4992 | SQUASHFS_INODE_BLK(sBlk.root_inode), |
| 4993 | root_inode_offset = |
| 4994 | SQUASHFS_INODE_OFFSET(sBlk.root_inode); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4995 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4996 | if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, |
| 4997 | &data_cache, &directory_table, |
| 4998 | &directory_data_cache, &last_directory_block, |
| 4999 | &inode_dir_offset, &inode_dir_file_size, |
| 5000 | &root_inode_size, &inode_dir_start_block, |
| 5001 | &file_count, &sym_count, &dev_count, &dir_count, |
| 5002 | &fifo_count, &sock_count, &total_bytes, |
| 5003 | &total_inode_bytes, &total_directory_bytes, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5004 | &inode_dir_inode_number, |
| 5005 | &inode_dir_parent_inode, add_old_root_entry, |
| 5006 | &fragment_table, &inode_lookup_table)) == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5007 | ERROR("Failed to read existing filesystem - will not " |
| 5008 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5009 | ERROR("To force Mksquashfs to write to this block " |
| 5010 | "device or file use -noappend\n"); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5011 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5012 | } |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5013 | if((fragments = sBlk.fragments)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5014 | fragment_table = realloc((char *) fragment_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5015 | ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) |
| 5016 | * sizeof(squashfs_fragment_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5017 | if(fragment_table == NULL) |
| 5018 | BAD_ERROR("Out of memory in save filesystem state\n"); |
| 5019 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5020 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5021 | printf("Appending to existing %d.%d filesystem on %s, block " |
| 5022 | "size %d\n", SQUASHFS_MAJOR, s_minor, argv[source + 1], |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5023 | block_size); |
| 5024 | printf("All -b, -noI, -noD, -noF, no-duplicates, no-fragments, " |
plougher | bb98803 | 2009-08-06 08:46:34 +0000 | [diff] [blame] | 5025 | "-always-use-fragments,\n-exportable and -comp options " |
| 5026 | "ignored\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5027 | printf("\nIf appending is not wanted, please re-run with " |
| 5028 | "-noappend specified!\n\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5029 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5030 | compressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5031 | ~(SQUASHFS_METADATA_SIZE - 1); |
| 5032 | uncompressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5033 | (SQUASHFS_METADATA_SIZE - 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5034 | |
| 5035 | /* save original filesystem state for restoring ... */ |
| 5036 | sfragments = fragments; |
| 5037 | sbytes = bytes; |
| 5038 | sinode_count = sBlk.inodes; |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 5039 | scache_bytes = root_inode_offset + root_inode_size; |
| 5040 | sdirectory_cache_bytes = uncompressed_data; |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5041 | sdata_cache = malloc(scache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5042 | if(sdata_cache == NULL) |
| 5043 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5044 | sdirectory_data_cache = malloc(sdirectory_cache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5045 | if(sdirectory_data_cache == NULL) |
| 5046 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5047 | memcpy(sdata_cache, data_cache, scache_bytes); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5048 | memcpy(sdirectory_data_cache, directory_data_cache + |
| 5049 | compressed_data, sdirectory_cache_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5050 | sinode_bytes = root_inode_start; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5051 | stotal_bytes = total_bytes; |
| 5052 | stotal_inode_bytes = total_inode_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5053 | stotal_directory_bytes = total_directory_bytes + |
| 5054 | compressed_data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5055 | sfile_count = file_count; |
| 5056 | ssym_count = sym_count; |
| 5057 | sdev_count = dev_count; |
| 5058 | sdir_count = dir_count + 1; |
| 5059 | sfifo_count = fifo_count; |
| 5060 | ssock_count = sock_count; |
| 5061 | sdup_files = dup_files; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5062 | sid_count = id_count; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5063 | write_recovery_data(&sBlk); |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 5064 | if(save_xattrs() == FALSE) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5065 | BAD_ERROR("Failed to save xattrs from existing " |
| 5066 | "filesystem\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5067 | restore = TRUE; |
| 5068 | if(setjmp(env)) |
| 5069 | goto restore_filesystem; |
| 5070 | signal(SIGTERM, sighandler); |
| 5071 | signal(SIGINT, sighandler); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 5072 | write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5073 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5074 | /* |
| 5075 | * set the filesystem state up to be able to append to the |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5076 | * original filesystem. The filesystem state differs depending |
| 5077 | * on whether we're appending to the original root directory, or |
| 5078 | * if the original root directory becomes a sub-directory |
| 5079 | * (root-becomes specified on command line, here root_name != |
| 5080 | * NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5081 | */ |
| 5082 | inode_bytes = inode_size = root_inode_start; |
| 5083 | directory_size = last_directory_block; |
| 5084 | cache_size = root_inode_offset + root_inode_size; |
| 5085 | directory_cache_size = inode_dir_offset + inode_dir_file_size; |
| 5086 | if(root_name) { |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5087 | sdirectory_bytes = last_directory_block; |
| 5088 | sdirectory_compressed_bytes = 0; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5089 | root_inode_number = inode_dir_parent_inode; |
| 5090 | dir_inode_no = sBlk.inodes + 2; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5091 | directory_bytes = last_directory_block; |
| 5092 | directory_cache_bytes = uncompressed_data; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5093 | memmove(directory_data_cache, directory_data_cache + |
| 5094 | compressed_data, uncompressed_data); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5095 | cache_bytes = root_inode_offset + root_inode_size; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5096 | add_old_root_entry(root_name, sBlk.root_inode, |
| 5097 | inode_dir_inode_number, SQUASHFS_DIR_TYPE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5098 | total_directory_bytes += compressed_data; |
| 5099 | dir_count ++; |
| 5100 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5101 | sdirectory_compressed_bytes = last_directory_block - |
| 5102 | inode_dir_start_block; |
| 5103 | sdirectory_compressed = |
| 5104 | malloc(sdirectory_compressed_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5105 | if(sdirectory_compressed == NULL) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5106 | BAD_ERROR("Out of memory in save filesystem " |
| 5107 | "state\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5108 | memcpy(sdirectory_compressed, directory_table + |
| 5109 | inode_dir_start_block, |
| 5110 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5111 | sdirectory_bytes = inode_dir_start_block; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5112 | root_inode_number = inode_dir_inode_number; |
plougher | 778e936 | 2006-02-01 09:32:31 +0000 | [diff] [blame] | 5113 | dir_inode_no = sBlk.inodes + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5114 | directory_bytes = inode_dir_start_block; |
| 5115 | directory_cache_bytes = inode_dir_offset; |
| 5116 | cache_bytes = root_inode_offset; |
| 5117 | } |
| 5118 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5119 | inode_count = file_count + dir_count + sym_count + dev_count + |
| 5120 | fifo_count + sock_count; |
plougher | 801ba6a | 2010-02-01 03:12:59 +0000 | [diff] [blame] | 5121 | |
| 5122 | /* |
| 5123 | * The default use freelist before growing cache policy behaves |
| 5124 | * poorly with appending - with many deplicates the caches |
| 5125 | * do not grow due to the fact that large queues of outstanding |
| 5126 | * fragments/writer blocks do not occur, leading to small caches |
| 5127 | * and un-uncessary performance loss to frequent cache |
| 5128 | * replacement in the small caches. Therefore with appending |
| 5129 | * change the policy to grow the caches before reusing blocks |
| 5130 | * from the freelist |
| 5131 | */ |
| 5132 | first_freelist = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5135 | if(path || stickypath) { |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5136 | paths = init_subdir(); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5137 | if(path) |
| 5138 | paths = add_subdir(paths, path); |
| 5139 | if(stickypath) |
| 5140 | paths = add_subdir(paths, stickypath); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5143 | if(delete && !keep_as_directory && source == 1 && |
| 5144 | S_ISDIR(source_buf.st_mode)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5145 | dir_scan(&inode, source_path[0], scan1_readdir); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5146 | else if(!keep_as_directory && source == 1 && |
| 5147 | S_ISDIR(source_buf.st_mode)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5148 | dir_scan(&inode, source_path[0], scan1_single_readdir); |
| 5149 | else |
| 5150 | dir_scan(&inode, "", scan1_encomp_readdir); |
| 5151 | sBlk.root_inode = inode; |
| 5152 | sBlk.inodes = inode_count; |
| 5153 | sBlk.s_magic = SQUASHFS_MAGIC; |
| 5154 | sBlk.s_major = SQUASHFS_MAJOR; |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5155 | sBlk.s_minor = s_minor; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5156 | sBlk.block_size = block_size; |
| 5157 | sBlk.block_log = block_log; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5158 | sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, no_fragments, |
| 5159 | always_use_fragments, duplicate_checking, exportable); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5160 | sBlk.mkfs_time = time(NULL); |
| 5161 | |
| 5162 | restore_filesystem: |
plougher | 43bb7e9 | 2008-08-17 17:16:09 +0000 | [diff] [blame] | 5163 | if(progress && estimated_uncompressed) { |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 5164 | disable_progress_bar(); |
| 5165 | progress_bar(cur_uncompressed, estimated_uncompressed, columns); |
| 5166 | } |
| 5167 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5168 | write_fragment(); |
| 5169 | sBlk.fragments = fragments; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5170 | if(interrupted < 2) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 5171 | unlock_fragments(); |
| 5172 | pthread_mutex_lock(&fragment_mutex); |
| 5173 | while(fragments_outstanding) { |
| 5174 | pthread_mutex_unlock(&fragment_mutex); |
| 5175 | sched_yield(); |
| 5176 | pthread_mutex_lock(&fragment_mutex); |
| 5177 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5178 | queue_put(to_writer, NULL); |
| 5179 | if(queue_get(from_writer) != 0) |
| 5180 | EXIT_MKSQUASHFS(); |
| 5181 | } |
| 5182 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 5183 | sBlk.no_ids = id_count; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5184 | sBlk.inode_table_start = write_inodes(); |
| 5185 | sBlk.directory_table_start = write_directories(); |
| 5186 | sBlk.fragment_table_start = write_fragment_table(); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5187 | sBlk.lookup_table_start = exportable ? write_inode_lookup_table() : |
| 5188 | SQUASHFS_INVALID_BLK; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 5189 | sBlk.id_table_start = write_id_table(); |
| 5190 | sBlk.xattr_id_table_start = write_xattrs(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5191 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5192 | TRACE("sBlk->inode_table_start 0x%llx\n", sBlk.inode_table_start); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5193 | TRACE("sBlk->directory_table_start 0x%llx\n", |
| 5194 | sBlk.directory_table_start); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5195 | TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk.fragment_table_start); |
| 5196 | if(exportable) |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5197 | TRACE("sBlk->lookup_table_start 0x%llx\n", |
| 5198 | sBlk.lookup_table_start); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5199 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5200 | sBlk.bytes_used = bytes; |
plougher | 9fca346 | 2008-10-27 00:34:35 +0000 | [diff] [blame] | 5201 | |
plougher | 8c4b7b9 | 2009-07-30 04:47:52 +0000 | [diff] [blame] | 5202 | sBlk.compression = comp->id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5203 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 5204 | SQUASHFS_INSWAP_SUPER_BLOCK(&sBlk); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5205 | write_destination(fd, SQUASHFS_START, sizeof(squashfs_super_block), |
| 5206 | (char *) &sBlk); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5207 | |
| 5208 | if(!nopad && (i = bytes & (4096 - 1))) { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 5209 | char temp[4096] = {0}; |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 5210 | write_destination(fd, bytes, 4096 - i, temp); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5213 | close(fd); |
| 5214 | |
plougher | 11e7b1b | 2009-09-11 12:10:58 +0000 | [diff] [blame] | 5215 | delete_pseudo_files(); |
| 5216 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5217 | if(recovery_file[0] != '\0') |
| 5218 | unlink(recovery_file); |
| 5219 | |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 5220 | total_bytes += total_inode_bytes + total_directory_bytes + |
plougher | 747e726 | 2010-07-19 18:10:54 +0000 | [diff] [blame] | 5221 | sizeof(squashfs_super_block) + total_xattr_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5222 | |
plougher | 62542fb | 2009-08-06 08:43:08 +0000 | [diff] [blame] | 5223 | printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size" |
| 5224 | " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR, |
| 5225 | SQUASHFS_MINOR, comp->name, block_size); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5226 | printf("\t%s data, %s metadata, %s fragments, %s xattrs\n", |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 5227 | noD ? "uncompressed" : "compressed", noI ? "uncompressed" : |
| 5228 | "compressed", no_fragments ? "no" : noF ? "uncompressed" : |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5229 | "compressed", no_xattrs ? "no" : noX ? "uncompressed" : |
| 5230 | "compressed"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5231 | printf("\tduplicates are %sremoved\n", duplicate_checking ? "" : |
| 5232 | "not "); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5233 | printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, |
| 5234 | bytes / (1024.0 * 1024.0)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5235 | printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n", |
| 5236 | ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0); |
| 5237 | printf("Inode table size %d bytes (%.2f Kbytes)\n", |
| 5238 | inode_bytes, inode_bytes / 1024.0); |
| 5239 | printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n", |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5240 | ((float) inode_bytes / total_inode_bytes) * 100.0, |
| 5241 | total_inode_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5242 | printf("Directory table size %d bytes (%.2f Kbytes)\n", |
| 5243 | directory_bytes, directory_bytes / 1024.0); |
| 5244 | printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n", |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5245 | ((float) directory_bytes / total_directory_bytes) * 100.0, |
| 5246 | total_directory_bytes); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 5247 | if(total_xattr_bytes) { |
| 5248 | printf("Xattr table size %d bytes (%.2f Kbytes)\n", |
| 5249 | xattr_bytes, xattr_bytes / 1024.0); |
| 5250 | printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n", |
| 5251 | ((float) xattr_bytes / total_xattr_bytes) * 100.0, |
| 5252 | total_xattr_bytes); |
| 5253 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5254 | if(duplicate_checking) |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5255 | printf("Number of duplicate files found %d\n", file_count - |
| 5256 | dup_files); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5257 | else |
| 5258 | printf("No duplicate files removed\n"); |
| 5259 | printf("Number of inodes %d\n", inode_count); |
| 5260 | printf("Number of files %d\n", file_count); |
| 5261 | if(!no_fragments) |
| 5262 | printf("Number of fragments %d\n", fragments); |
| 5263 | printf("Number of symbolic links %d\n", sym_count); |
| 5264 | printf("Number of device nodes %d\n", dev_count); |
| 5265 | printf("Number of fifo nodes %d\n", fifo_count); |
| 5266 | printf("Number of socket nodes %d\n", sock_count); |
| 5267 | printf("Number of directories %d\n", dir_count); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5268 | printf("Number of ids (unique uids + gids) %d\n", id_count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5269 | printf("Number of uids %d\n", uid_count); |
| 5270 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5271 | for(i = 0; i < id_count; i++) { |
| 5272 | if(id_table[i]->flags & ISA_UID) { |
| 5273 | struct passwd *user = getpwuid(id_table[i]->id); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5274 | printf("\t%s (%d)\n", user == NULL ? "unknown" : |
| 5275 | user->pw_name, id_table[i]->id); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5276 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5277 | } |
| 5278 | |
| 5279 | printf("Number of gids %d\n", guid_count); |
| 5280 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5281 | for(i = 0; i < id_count; i++) { |
| 5282 | if(id_table[i]->flags & ISA_GID) { |
| 5283 | struct group *group = getgrgid(id_table[i]->id); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5284 | printf("\t%s (%d)\n", group == NULL ? "unknown" : |
| 5285 | group->gr_name, id_table[i]->id); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5286 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5287 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5288 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5289 | return 0; |
| 5290 | } |