plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1 | /* |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2 | * Create a squashfs filesystem. This is a highly compressed read only |
| 3 | * filesystem. |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4 | * |
Phillip Lougher | 43cc428 | 2012-01-25 00:25:56 +0000 | [diff] [blame] | 5 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, |
Phillip Lougher | cc06495 | 2014-01-03 04:26:34 +0000 | [diff] [blame] | 6 | * 2012, 2013, 2014 |
Phillip Lougher | 83d42a3 | 2012-10-31 23:42:22 +0000 | [diff] [blame] | 7 | * Phillip Lougher <phillip@squashfs.org.uk> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version 2, |
| 12 | * or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | * |
| 23 | * mksquashfs.c |
| 24 | */ |
| 25 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 26 | #define FALSE 0 |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 27 | #define TRUE 1 |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 28 | #define MAX_LINE 16384 |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 29 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 30 | #include <pwd.h> |
| 31 | #include <grp.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stdio.h> |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 35 | #include <stddef.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <errno.h> |
| 40 | #include <dirent.h> |
| 41 | #include <string.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 42 | #include <stdlib.h> |
| 43 | #include <signal.h> |
| 44 | #include <setjmp.h> |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 45 | #include <sys/types.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 46 | #include <sys/mman.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 47 | #include <pthread.h> |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 48 | #include <regex.h> |
| 49 | #include <fnmatch.h> |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 50 | #include <sys/wait.h> |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 51 | #include <limits.h> |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 52 | #include <ctype.h> |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 53 | |
plougher | 6885329 | 2005-12-27 02:47:04 +0000 | [diff] [blame] | 54 | #ifndef linux |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 55 | #define __BYTE_ORDER BYTE_ORDER |
| 56 | #define __BIG_ENDIAN BIG_ENDIAN |
| 57 | #define __LITTLE_ENDIAN LITTLE_ENDIAN |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 58 | #include <sys/sysctl.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 59 | #else |
| 60 | #include <endian.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 61 | #include <sys/sysinfo.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 64 | #include "squashfs_fs.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 65 | #include "squashfs_swap.h" |
| 66 | #include "mksquashfs.h" |
| 67 | #include "sort.h" |
| 68 | #include "pseudo.h" |
| 69 | #include "compressor.h" |
| 70 | #include "xattr.h" |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 71 | #include "action.h" |
Phillip Lougher | 2477d0d | 2012-10-24 21:49:28 +0100 | [diff] [blame] | 72 | #include "error.h" |
Phillip Lougher | 9a0e8bb | 2013-03-10 02:39:24 +0000 | [diff] [blame] | 73 | #include "progressbar.h" |
Phillip Lougher | 2e9fab1 | 2013-04-09 03:26:33 +0100 | [diff] [blame] | 74 | #include "info.h" |
Phillip Lougher | 71f3964 | 2013-04-09 04:43:21 +0100 | [diff] [blame] | 75 | #include "caches-queues-lists.h" |
Phillip Lougher | 1bbd0cc | 2013-04-19 04:03:00 +0100 | [diff] [blame] | 76 | #include "read_fs.h" |
Phillip Lougher | 0d67c9d | 2013-04-19 05:06:50 +0100 | [diff] [blame] | 77 | #include "restore.h" |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 78 | #include "process_fragments.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 79 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 80 | int delete = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 81 | int fd; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 82 | struct squashfs_super_block sBlk; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 83 | |
| 84 | /* filesystem flags for building */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 85 | int comp_opts = FALSE; |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 86 | int no_xattrs = XATTR_DEF; |
| 87 | int noX = FALSE; |
| 88 | int duplicate_checking = TRUE; |
| 89 | int noF = FALSE; |
| 90 | int no_fragments = FALSE; |
| 91 | int always_use_fragments = FALSE; |
| 92 | int noI = FALSE; |
| 93 | int noD = FALSE; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 94 | int silent = TRUE; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 95 | int exportable = TRUE; |
plougher | 8dcc699 | 2007-08-17 19:32:52 +0000 | [diff] [blame] | 96 | int sparse_files = TRUE; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 97 | int old_exclude = TRUE; |
| 98 | int use_regex = FALSE; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 99 | int nopad = FALSE; |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 100 | int exit_on_error = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 101 | |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 102 | long long global_uid = -1, global_gid = -1; |
| 103 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 104 | /* superblock attributes */ |
| 105 | int block_size = SQUASHFS_FILE_SIZE, block_log; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 106 | unsigned int id_count = 0; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 107 | int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0, |
| 108 | sock_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 109 | |
| 110 | /* write position within data section */ |
| 111 | long long bytes = 0, total_bytes = 0; |
| 112 | |
| 113 | /* in memory directory table - possibly compressed */ |
| 114 | char *directory_table = NULL; |
| 115 | unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0; |
| 116 | |
| 117 | /* cached directory table */ |
| 118 | char *directory_data_cache = NULL; |
| 119 | unsigned int directory_cache_bytes = 0, directory_cache_size = 0; |
| 120 | |
| 121 | /* in memory inode table - possibly compressed */ |
| 122 | char *inode_table = NULL; |
| 123 | unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0; |
| 124 | |
| 125 | /* cached inode table */ |
| 126 | char *data_cache = NULL; |
| 127 | unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0; |
| 128 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 129 | /* inode lookup table */ |
| 130 | squashfs_inode *inode_lookup_table = NULL; |
| 131 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 132 | /* in memory directory data */ |
| 133 | #define I_COUNT_SIZE 128 |
| 134 | #define DIR_ENTRIES 32 |
| 135 | #define INODE_HASH_SIZE 65536 |
| 136 | #define INODE_HASH_MASK (INODE_HASH_SIZE - 1) |
| 137 | #define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK) |
| 138 | |
| 139 | struct cached_dir_index { |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 140 | struct squashfs_dir_index index; |
| 141 | char *name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | struct directory { |
| 145 | unsigned int start_block; |
| 146 | unsigned int size; |
| 147 | unsigned char *buff; |
| 148 | unsigned char *p; |
| 149 | unsigned int entry_count; |
| 150 | unsigned char *entry_count_p; |
| 151 | unsigned int i_count; |
| 152 | unsigned int i_size; |
| 153 | struct cached_dir_index *index; |
| 154 | unsigned char *index_count_p; |
| 155 | unsigned int inode_number; |
| 156 | }; |
| 157 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 158 | struct inode_info *inode_info[INODE_HASH_SIZE]; |
| 159 | |
| 160 | /* hash tables used to do fast duplicate searches in duplicate check */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 161 | struct file_info *dupl[65536]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 162 | int dup_files = 0; |
| 163 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 164 | /* exclude file handling */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 165 | /* list of exclude dirs/files */ |
| 166 | struct exclude_info { |
| 167 | dev_t st_dev; |
| 168 | ino_t st_ino; |
| 169 | }; |
| 170 | |
| 171 | #define EXCLUDE_SIZE 8192 |
| 172 | int exclude = 0; |
| 173 | struct exclude_info *exclude_paths = NULL; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 174 | int old_excluded(char *filename, struct stat *buf); |
| 175 | |
| 176 | struct path_entry { |
| 177 | char *name; |
| 178 | regex_t *preg; |
| 179 | struct pathname *paths; |
| 180 | }; |
| 181 | |
| 182 | struct pathname { |
| 183 | int names; |
| 184 | struct path_entry *name; |
| 185 | }; |
| 186 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 187 | struct pathnames { |
| 188 | int count; |
| 189 | struct pathname *path[0]; |
| 190 | }; |
| 191 | #define PATHS_ALLOC_SIZE 10 |
| 192 | |
| 193 | struct pathnames *paths = NULL; |
| 194 | struct pathname *path = NULL; |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 195 | struct pathname *stickypath = NULL; |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 196 | int excluded(char *name, struct pathnames *paths, struct pathnames **new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 197 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 198 | int fragments = 0; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 199 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 200 | #define FRAG_SIZE 32768 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 201 | |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 202 | struct squashfs_fragment_entry *fragment_table = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 203 | int fragments_outstanding = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 204 | |
Phillip Lougher | 7ffdf2a | 2013-04-14 02:53:30 +0100 | [diff] [blame] | 205 | int fragments_locked = FALSE; |
Phillip Lougher | 7ffdf2a | 2013-04-14 02:53:30 +0100 | [diff] [blame] | 206 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 207 | /* current inode number for directories and non directories */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 208 | unsigned int inode_no = 1; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 209 | unsigned int root_inode_number = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 210 | |
| 211 | /* list of source dirs/files */ |
| 212 | int source = 0; |
| 213 | char **source_path; |
| 214 | |
| 215 | /* list of root directory entries read from original filesystem */ |
| 216 | int old_root_entries = 0; |
| 217 | struct old_root_entry_info { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 218 | char *name; |
| 219 | struct inode_info inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 220 | }; |
| 221 | struct old_root_entry_info *old_root_entry; |
| 222 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 223 | /* restore orignal filesystem state if appending to existing filesystem is |
| 224 | * cancelled */ |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 225 | int appending = FALSE; |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 226 | char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 227 | |
| 228 | long long sbytes, stotal_bytes; |
| 229 | |
| 230 | unsigned int sinode_bytes, scache_bytes, sdirectory_bytes, |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 231 | sdirectory_cache_bytes, sdirectory_compressed_bytes, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 232 | stotal_inode_bytes, stotal_directory_bytes, |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 233 | sinode_count = 0, sfile_count, ssym_count, sdev_count, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 234 | sdir_count, sfifo_count, ssock_count, sdup_files; |
| 235 | int sfragments; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 236 | int threads; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 237 | |
| 238 | /* flag whether destination file is a block device */ |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 239 | int block_device = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 240 | |
| 241 | /* flag indicating whether files are sorted using sort list(s) */ |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 242 | int sorted = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 243 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 244 | /* save destination file name for deleting on error */ |
| 245 | char *destination_file = NULL; |
| 246 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 247 | /* recovery file for abnormal exit on appending */ |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 248 | char *recovery_file = NULL; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 249 | int recover = TRUE; |
| 250 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 251 | struct id *id_hash_table[ID_ENTRIES]; |
| 252 | struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS]; |
| 253 | unsigned int uid_count = 0, guid_count = 0; |
| 254 | unsigned int sid_count = 0, suid_count = 0, sguid_count = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 255 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 256 | struct cache *reader_buffer, *fragment_buffer, *reserve_cache; |
| 257 | struct cache *bwriter_buffer, *fwriter_buffer; |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 258 | struct queue *to_reader, *to_deflate, *to_writer, *from_writer, |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 259 | *to_frag, *locked_fragment, *to_process_frag; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 260 | struct seq_queue *to_main; |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 261 | pthread_t reader_thread, writer_thread, main_thread; |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 262 | pthread_t *deflator_thread, *frag_deflator_thread, *frag_thread; |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 263 | pthread_t *restore_thread = NULL; |
Phillip Lougher | 94e658f | 2014-03-13 02:59:04 +0000 | [diff] [blame] | 264 | pthread_mutex_t fragment_mutex = PTHREAD_MUTEX_INITIALIZER; |
Phillip Lougher | ea8cb65 | 2014-03-13 02:57:19 +0000 | [diff] [blame] | 265 | pthread_mutex_t pos_mutex = PTHREAD_MUTEX_INITIALIZER; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 266 | pthread_mutex_t dup_mutex = PTHREAD_MUTEX_INITIALIZER; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 267 | |
| 268 | /* user options that control parallelisation */ |
| 269 | int processors = -1; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 270 | int bwriter_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 271 | |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 272 | /* compression operations */ |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 273 | struct compressor *comp = NULL; |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 274 | int compressor_opt_parsed = FALSE; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 275 | void *stream = NULL; |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 276 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 277 | /* xattr stats */ |
| 278 | unsigned int xattr_bytes = 0, total_xattr_bytes = 0; |
| 279 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 280 | /* fragment to file mapping used when appending */ |
| 281 | int append_fragments = 0; |
| 282 | struct append_file **file_mapping; |
| 283 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 284 | static char *read_from_disk(long long start, unsigned int avail_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 285 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 286 | int type); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 287 | struct file_info *duplicate(long long file_size, long long bytes, |
| 288 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 289 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 290 | int checksum_flag); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 291 | struct dir_info *dir_scan1(char *, char *, struct pathnames *, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 292 | struct dir_ent *(_readdir)(struct dir_info *), int); |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 293 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 294 | void dir_scan3(struct dir_info *root, struct dir_info *dir); |
| 295 | void dir_scan4(struct dir_info *dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 296 | void dir_scan5(struct dir_info *dir); |
| 297 | void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 298 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 299 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 300 | unsigned short checksum, unsigned short fragment_checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 301 | int checksum_flag, int checksum_frag_flag); |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 302 | long long generic_write_table(int, void *, int, void *, int); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 303 | void restorefs(); |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 304 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 305 | void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 306 | unsigned short get_checksum_mem(char *buff, int bytes); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 307 | int get_physical_memory(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 308 | |
| 309 | |
Phillip Lougher | 9b7c3c2 | 2013-03-10 02:29:42 +0000 | [diff] [blame] | 310 | void prep_exit() |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 311 | { |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 312 | if(restore_thread) { |
| 313 | if(pthread_self() == *restore_thread) { |
| 314 | /* |
| 315 | * Recursive failure when trying to restore filesystem! |
| 316 | * Nothing to do except to exit, otherwise we'll just |
| 317 | * appear to hang. The user should be able to restore |
| 318 | * from the recovery file (which is why it was added, in |
| 319 | * case of catastrophic failure in Mksquashfs) |
| 320 | */ |
| 321 | exit(1); |
| 322 | } else { |
| 323 | /* signal the restore thread to restore */ |
Phillip Lougher | 8c74081 | 2014-01-30 01:56:17 +0000 | [diff] [blame] | 324 | pthread_kill(*restore_thread, SIGUSR1); |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 325 | pthread_exit(NULL); |
| 326 | } |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 327 | } else if(delete) { |
| 328 | if(destination_file && !block_device) |
| 329 | unlink(destination_file); |
| 330 | } else if(recovery_file) |
| 331 | unlink(recovery_file); |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 335 | int add_overflow(int a, int b) |
| 336 | { |
| 337 | return (INT_MAX - a) < b; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | int shift_overflow(int a, int shift) |
| 342 | { |
| 343 | return (INT_MAX >> shift) < a; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | int multiply_overflow(int a, int multiplier) |
| 348 | { |
| 349 | return (INT_MAX / multiplier) < a; |
| 350 | } |
| 351 | |
| 352 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 353 | #define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \ |
| 354 | + (((char *)A) - data_cache))) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 355 | |
| 356 | |
| 357 | void restorefs() |
| 358 | { |
| 359 | ERROR("Exiting - restoring original filesystem!\n\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 360 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 361 | bytes = sbytes; |
| 362 | memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 363 | memcpy(directory_data_cache, sdirectory_data_cache, |
| 364 | sdirectory_cache_bytes); |
| 365 | directory_cache_bytes = sdirectory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 366 | inode_bytes = sinode_bytes; |
| 367 | directory_bytes = sdirectory_bytes; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 368 | memcpy(directory_table + directory_bytes, sdirectory_compressed, |
| 369 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 370 | directory_bytes += sdirectory_compressed_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 371 | total_bytes = stotal_bytes; |
| 372 | total_inode_bytes = stotal_inode_bytes; |
| 373 | total_directory_bytes = stotal_directory_bytes; |
| 374 | inode_count = sinode_count; |
| 375 | file_count = sfile_count; |
| 376 | sym_count = ssym_count; |
| 377 | dev_count = sdev_count; |
| 378 | dir_count = sdir_count; |
| 379 | fifo_count = sfifo_count; |
| 380 | sock_count = ssock_count; |
| 381 | dup_files = sdup_files; |
| 382 | fragments = sfragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 383 | id_count = sid_count; |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 384 | restore_xattrs(); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 385 | write_filesystem_tables(&sBlk, nopad); |
| 386 | exit(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 390 | void sighandler() |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 391 | { |
| 392 | EXIT_MKSQUASHFS(); |
| 393 | } |
| 394 | |
| 395 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 396 | int mangle2(void *strm, char *d, char *s, int size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 397 | int block_size, int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 398 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 399 | int error, c_byte = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 400 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 401 | if(!uncompressed) { |
plougher | bfb876c | 2010-12-31 07:48:01 +0000 | [diff] [blame] | 402 | c_byte = compressor_compress(comp, strm, d, s, size, block_size, |
| 403 | &error); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 404 | if(c_byte == -1) |
| 405 | BAD_ERROR("mangle2:: %s compress failed with error " |
| 406 | "code %d\n", comp->name, error); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 407 | } |
| 408 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 409 | if(c_byte == 0 || c_byte >= size) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 410 | memcpy(d, s, size); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 411 | return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : |
| 412 | SQUASHFS_COMPRESSED_BIT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 413 | } |
| 414 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 415 | return c_byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 419 | int mangle(char *d, char *s, int size, int block_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 420 | int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 421 | { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 422 | return mangle2(stream, d, s, size, block_size, uncompressed, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 423 | data_block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 427 | void *get_inode(int req_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 428 | { |
| 429 | int data_space; |
| 430 | unsigned short c_byte; |
| 431 | |
| 432 | while(cache_bytes >= SQUASHFS_METADATA_SIZE) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 433 | if((inode_size - inode_bytes) < |
| 434 | ((SQUASHFS_METADATA_SIZE << 1)) + 2) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 435 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 436 | (SQUASHFS_METADATA_SIZE << 1) + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 437 | if(it == NULL) |
| 438 | MEM_ERROR(); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 439 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 440 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
| 441 | } |
| 442 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 443 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 444 | data_cache, SQUASHFS_METADATA_SIZE, |
| 445 | SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 446 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 447 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 448 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 449 | total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 450 | memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 451 | cache_bytes - SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 452 | cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 453 | } |
| 454 | |
| 455 | data_space = (cache_size - cache_bytes); |
| 456 | if(data_space < req_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 457 | int realloc_size = cache_size == 0 ? |
| 458 | ((req_size + SQUASHFS_METADATA_SIZE) & |
| 459 | ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - |
| 460 | data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 461 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 462 | void *dc = realloc(data_cache, cache_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 463 | realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 464 | if(dc == NULL) |
| 465 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 466 | cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 467 | data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | cache_bytes += req_size; |
| 471 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 472 | return data_cache + cache_bytes - req_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 476 | int read_bytes(int fd, void *buff, int bytes) |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 477 | { |
| 478 | int res, count; |
| 479 | |
| 480 | for(count = 0; count < bytes; count += res) { |
| 481 | res = read(fd, buff + count, bytes - count); |
| 482 | if(res < 1) { |
plougher | 96f85a1 | 2009-03-29 22:39:59 +0000 | [diff] [blame] | 483 | if(res == 0) |
| 484 | goto bytes_read; |
| 485 | else if(errno != EINTR) { |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 486 | ERROR("Read failed because %s\n", |
| 487 | strerror(errno)); |
| 488 | return -1; |
| 489 | } else |
| 490 | res = 0; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | bytes_read: |
| 495 | return count; |
| 496 | } |
| 497 | |
| 498 | |
plougher | 3306cb2 | 2010-06-18 04:22:44 +0000 | [diff] [blame] | 499 | int read_fs_bytes(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 500 | { |
| 501 | off_t off = byte; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 502 | int res = 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 503 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 504 | TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 505 | byte, bytes); |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 506 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 507 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 508 | pthread_mutex_lock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 509 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 510 | ERROR("read_fs_bytes: Lseek on destination failed because %s, " |
| 511 | "offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 512 | res = 0; |
Phillip Lougher | 041cf6d | 2013-07-20 04:20:40 +0100 | [diff] [blame] | 513 | } else if(read_bytes(fd, buff, bytes) < bytes) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 514 | ERROR("Read on destination failed\n"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 515 | res = 0; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 518 | pthread_cleanup_pop(1); |
| 519 | return res; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 523 | int write_bytes(int fd, void *buff, int bytes) |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 524 | { |
| 525 | int res, count; |
| 526 | |
| 527 | for(count = 0; count < bytes; count += res) { |
| 528 | res = write(fd, buff + count, bytes - count); |
| 529 | if(res == -1) { |
| 530 | if(errno != EINTR) { |
| 531 | ERROR("Write failed because %s\n", |
| 532 | strerror(errno)); |
| 533 | return -1; |
| 534 | } |
| 535 | res = 0; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 543 | void write_destination(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 544 | { |
| 545 | off_t off = byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 546 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 547 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
| 548 | pthread_mutex_lock(&pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 549 | |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 550 | if(lseek(fd, off, SEEK_SET) == -1) { |
| 551 | ERROR("write_destination: Lseek on destination " |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 552 | "failed because %s, offset=0x%llx\n", strerror(errno), |
| 553 | off); |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 554 | BAD_ERROR("Probably out of space on output %s\n", |
| 555 | block_device ? "block device" : "filesystem"); |
| 556 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 557 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 558 | if(write_bytes(fd, buff, bytes) == -1) |
Phillip Lougher | e8b536f | 2013-02-12 22:01:21 +0000 | [diff] [blame] | 559 | BAD_ERROR("Failed to write to output %s\n", |
| 560 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 561 | |
| 562 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
| 566 | long long write_inodes() |
| 567 | { |
| 568 | unsigned short c_byte; |
| 569 | int avail_bytes; |
| 570 | char *datap = data_cache; |
| 571 | long long start_bytes = bytes; |
| 572 | |
| 573 | while(cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 574 | if(inode_size - inode_bytes < |
| 575 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 576 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 577 | ((SQUASHFS_METADATA_SIZE << 1) + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 578 | if(it == NULL) |
| 579 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 580 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 581 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 582 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 583 | avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 584 | SQUASHFS_METADATA_SIZE : cache_bytes; |
| 585 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap, |
| 586 | avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 587 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 588 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 589 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 590 | total_inode_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 591 | datap += avail_bytes; |
| 592 | cache_bytes -= avail_bytes; |
| 593 | } |
| 594 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 595 | write_destination(fd, bytes, inode_bytes, inode_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 596 | bytes += inode_bytes; |
| 597 | |
| 598 | return start_bytes; |
| 599 | } |
| 600 | |
| 601 | |
| 602 | long long write_directories() |
| 603 | { |
| 604 | unsigned short c_byte; |
| 605 | int avail_bytes; |
| 606 | char *directoryp = directory_data_cache; |
| 607 | long long start_bytes = bytes; |
| 608 | |
| 609 | while(directory_cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 610 | if(directory_size - directory_bytes < |
| 611 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 612 | void *dt = realloc(directory_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 613 | directory_size + ((SQUASHFS_METADATA_SIZE << 1) |
| 614 | + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 615 | if(dt == NULL) |
| 616 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 617 | directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 618 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 619 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 620 | avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 621 | SQUASHFS_METADATA_SIZE : directory_cache_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 622 | c_byte = mangle(directory_table + directory_bytes + |
| 623 | BLOCK_OFFSET, directoryp, avail_bytes, |
| 624 | SQUASHFS_METADATA_SIZE, noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 625 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 626 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 627 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 628 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 629 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 630 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 631 | total_directory_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 632 | directoryp += avail_bytes; |
| 633 | directory_cache_bytes -= avail_bytes; |
| 634 | } |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 635 | write_destination(fd, bytes, directory_bytes, directory_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 636 | bytes += directory_bytes; |
| 637 | |
| 638 | return start_bytes; |
| 639 | } |
| 640 | |
| 641 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 642 | long long write_id_table() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 643 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 644 | unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 645 | unsigned int p[id_count]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 646 | int i; |
| 647 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 648 | TRACE("write_id_table: ids %d, id_bytes %d\n", id_count, id_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 649 | for(i = 0; i < id_count; i++) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 650 | TRACE("write_id_table: id index %d, id %d", i, id_table[i]->id); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 651 | SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 652 | } |
| 653 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 654 | return generic_write_table(id_bytes, p, 0, NULL, noI); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 658 | struct id *get_id(unsigned int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 659 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 660 | int hash = ID_HASH(id); |
| 661 | struct id *entry = id_hash_table[hash]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 662 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 663 | for(; entry; entry = entry->next) |
| 664 | if(entry->id == id) |
| 665 | break; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 666 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 667 | return entry; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 671 | struct id *create_id(unsigned int id) |
| 672 | { |
| 673 | int hash = ID_HASH(id); |
| 674 | struct id *entry = malloc(sizeof(struct id)); |
| 675 | if(entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 676 | MEM_ERROR(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 677 | entry->id = id; |
| 678 | entry->index = id_count ++; |
| 679 | entry->flags = 0; |
| 680 | entry->next = id_hash_table[hash]; |
| 681 | id_hash_table[hash] = entry; |
| 682 | id_table[entry->index] = entry; |
| 683 | return entry; |
| 684 | } |
| 685 | |
| 686 | |
| 687 | unsigned int get_uid(unsigned int uid) |
| 688 | { |
| 689 | struct id *entry = get_id(uid); |
| 690 | |
| 691 | if(entry == NULL) { |
| 692 | if(id_count == SQUASHFS_IDS) |
| 693 | BAD_ERROR("Out of uids!\n"); |
| 694 | entry = create_id(uid); |
| 695 | } |
| 696 | |
| 697 | if((entry->flags & ISA_UID) == 0) { |
| 698 | entry->flags |= ISA_UID; |
| 699 | uid_count ++; |
| 700 | } |
| 701 | |
| 702 | return entry->index; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | unsigned int get_guid(unsigned int guid) |
| 707 | { |
| 708 | struct id *entry = get_id(guid); |
| 709 | |
| 710 | if(entry == NULL) { |
| 711 | if(id_count == SQUASHFS_IDS) |
| 712 | BAD_ERROR("Out of gids!\n"); |
| 713 | entry = create_id(guid); |
| 714 | } |
| 715 | |
| 716 | if((entry->flags & ISA_GID) == 0) { |
| 717 | entry->flags |= ISA_GID; |
| 718 | guid_count ++; |
| 719 | } |
| 720 | |
| 721 | return entry->index; |
| 722 | } |
| 723 | |
| 724 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 725 | #define ALLOC_SIZE 128 |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 726 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 727 | char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size) |
| 728 | { |
| 729 | if(pathname == NULL) { |
| 730 | pathname = malloc(ALLOC_SIZE); |
| 731 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 732 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | for(;;) { |
| 736 | int res = snprintf(pathname, *size, "%s/%s", |
| 737 | dir_ent->our_dir->pathname, |
| 738 | dir_ent->source_name ? : dir_ent->name); |
| 739 | |
| 740 | if(res < 0) |
| 741 | BAD_ERROR("snprintf failed in pathname\n"); |
| 742 | else if(res >= *size) { |
| 743 | /* |
| 744 | * pathname is too small to contain the result, so |
| 745 | * increase it and try again |
| 746 | */ |
| 747 | *size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 748 | pathname = realloc(pathname, *size); |
| 749 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 750 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 751 | } else |
| 752 | break; |
| 753 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 754 | |
| 755 | return pathname; |
| 756 | } |
| 757 | |
| 758 | |
| 759 | char *pathname(struct dir_ent *dir_ent) |
| 760 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 761 | static char *pathname = NULL; |
| 762 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 763 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 764 | if (dir_ent->nonstandard_pathname) |
| 765 | return dir_ent->nonstandard_pathname; |
| 766 | |
| 767 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | |
| 771 | char *pathname_reader(struct dir_ent *dir_ent) |
| 772 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 773 | static char *pathname = NULL; |
| 774 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 775 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 776 | if (dir_ent->nonstandard_pathname) |
| 777 | return dir_ent->nonstandard_pathname; |
| 778 | |
| 779 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 783 | char *subpathname(struct dir_ent *dir_ent) |
| 784 | { |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 785 | static char *subpath = NULL; |
| 786 | static int size = ALLOC_SIZE; |
| 787 | int res; |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 788 | |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 789 | if(subpath == NULL) { |
| 790 | subpath = malloc(ALLOC_SIZE); |
| 791 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 792 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | for(;;) { |
| 796 | if(dir_ent->our_dir->subpath[0] != '\0') |
| 797 | res = snprintf(subpath, size, "%s/%s", |
| 798 | dir_ent->our_dir->subpath, dir_ent->name); |
| 799 | else |
| 800 | res = snprintf(subpath, size, "/%s", dir_ent->name); |
| 801 | |
| 802 | if(res < 0) |
| 803 | BAD_ERROR("snprintf failed in subpathname\n"); |
| 804 | else if(res >= size) { |
| 805 | /* |
| 806 | * subpath is too small to contain the result, so |
| 807 | * increase it and try again |
| 808 | */ |
| 809 | size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 810 | subpath = realloc(subpath, size); |
| 811 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 812 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 813 | } else |
| 814 | break; |
Phillip Lougher | 90e0825 | 2012-10-05 04:33:35 +0100 | [diff] [blame] | 815 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 816 | |
| 817 | return subpath; |
| 818 | } |
| 819 | |
| 820 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 821 | inline unsigned int get_inode_no(struct inode_info *inode) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 822 | { |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 823 | return inode->inode_number; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 827 | inline unsigned int get_parent_no(struct dir_info *dir) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 828 | { |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 829 | return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 833 | int create_inode(squashfs_inode *i_no, struct dir_info *dir_info, |
| 834 | struct dir_ent *dir_ent, int type, long long byte_size, |
| 835 | long long start_block, unsigned int offset, unsigned int *block_list, |
| 836 | struct fragment *fragment, struct directory *dir_in, long long sparse) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 837 | { |
| 838 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 8973a12 | 2010-12-31 09:52:45 +0000 | [diff] [blame] | 839 | union squashfs_inode_header inode_header; |
plougher | 857d0dd | 2010-12-31 21:03:13 +0000 | [diff] [blame] | 840 | struct squashfs_base_inode_header *base = &inode_header.base; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 841 | void *inode; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 842 | char *filename = pathname(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 843 | int nlink = dir_ent->inode->nlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 844 | int xattr = read_xattrs(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 845 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 846 | switch(type) { |
| 847 | case SQUASHFS_FILE_TYPE: |
| 848 | if(dir_ent->inode->nlink > 1 || |
| 849 | byte_size >= (1LL << 32) || |
| 850 | start_block >= (1LL << 32) || |
| 851 | sparse || IS_XATTR(xattr)) |
| 852 | type = SQUASHFS_LREG_TYPE; |
| 853 | break; |
| 854 | case SQUASHFS_DIR_TYPE: |
| 855 | if(dir_info->dir_is_ldir || IS_XATTR(xattr)) |
| 856 | type = SQUASHFS_LDIR_TYPE; |
| 857 | break; |
| 858 | case SQUASHFS_SYMLINK_TYPE: |
| 859 | if(IS_XATTR(xattr)) |
| 860 | type = SQUASHFS_LSYMLINK_TYPE; |
| 861 | break; |
| 862 | case SQUASHFS_BLKDEV_TYPE: |
| 863 | if(IS_XATTR(xattr)) |
| 864 | type = SQUASHFS_LBLKDEV_TYPE; |
| 865 | break; |
| 866 | case SQUASHFS_CHRDEV_TYPE: |
| 867 | if(IS_XATTR(xattr)) |
| 868 | type = SQUASHFS_LCHRDEV_TYPE; |
| 869 | break; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 870 | case SQUASHFS_FIFO_TYPE: |
| 871 | if(IS_XATTR(xattr)) |
| 872 | type = SQUASHFS_LFIFO_TYPE; |
| 873 | break; |
| 874 | case SQUASHFS_SOCKET_TYPE: |
| 875 | if(IS_XATTR(xattr)) |
| 876 | type = SQUASHFS_LSOCKET_TYPE; |
| 877 | break; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 878 | } |
| 879 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 880 | base->mode = SQUASHFS_MODE(buf->st_mode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 881 | base->uid = get_uid((unsigned int) global_uid == -1 ? |
| 882 | buf->st_uid : global_uid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 883 | base->inode_type = type; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 884 | base->guid = get_guid((unsigned int) global_gid == -1 ? |
| 885 | buf->st_gid : global_gid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 886 | base->mtime = buf->st_mtime; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 887 | base->inode_number = get_inode_no(dir_ent->inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 888 | |
| 889 | if(type == SQUASHFS_FILE_TYPE) { |
| 890 | int i; |
plougher | 8701ed6 | 2010-12-31 20:38:38 +0000 | [diff] [blame] | 891 | struct squashfs_reg_inode_header *reg = &inode_header.reg; |
| 892 | size_t off = offsetof(struct squashfs_reg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 893 | |
| 894 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 895 | reg->file_size = byte_size; |
| 896 | reg->start_block = start_block; |
| 897 | reg->fragment = fragment->index; |
| 898 | reg->offset = fragment->offset; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 899 | SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode); |
| 900 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 901 | TRACE("File inode, file_size %lld, start_block 0x%llx, blocks " |
| 902 | "%d, fragment %d, offset %d, size %d\n", byte_size, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 903 | start_block, offset, fragment->index, fragment->offset, |
| 904 | fragment->size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 905 | for(i = 0; i < offset; i++) |
| 906 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 907 | } |
| 908 | else if(type == SQUASHFS_LREG_TYPE) { |
| 909 | int i; |
plougher | 1e6ac4a | 2010-12-31 20:42:02 +0000 | [diff] [blame] | 910 | struct squashfs_lreg_inode_header *reg = &inode_header.lreg; |
| 911 | size_t off = offsetof(struct squashfs_lreg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 912 | |
| 913 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 914 | reg->nlink = nlink; |
| 915 | reg->file_size = byte_size; |
| 916 | reg->start_block = start_block; |
| 917 | reg->fragment = fragment->index; |
| 918 | reg->offset = fragment->offset; |
plougher | f5a674d | 2009-03-25 05:38:27 +0000 | [diff] [blame] | 919 | if(sparse && sparse >= byte_size) |
| 920 | sparse = byte_size - 1; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 921 | reg->sparse = sparse; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 922 | reg->xattr = xattr; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 923 | SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode); |
| 924 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 925 | TRACE("Long file inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 926 | "blocks %d, fragment %d, offset %d, size %d, nlink %d" |
| 927 | "\n", byte_size, start_block, offset, fragment->index, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 928 | fragment->offset, fragment->size, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 929 | for(i = 0; i < offset; i++) |
| 930 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 931 | } |
| 932 | else if(type == SQUASHFS_LDIR_TYPE) { |
| 933 | int i; |
| 934 | unsigned char *p; |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 935 | struct squashfs_ldir_inode_header *dir = &inode_header.ldir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 936 | struct cached_dir_index *index = dir_in->index; |
| 937 | unsigned int i_count = dir_in->i_count; |
| 938 | unsigned int i_size = dir_in->i_size; |
| 939 | |
| 940 | if(byte_size >= 1 << 27) |
| 941 | BAD_ERROR("directory greater than 2^27-1 bytes!\n"); |
| 942 | |
| 943 | inode = get_inode(sizeof(*dir) + i_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 944 | dir->inode_type = SQUASHFS_LDIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 945 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 946 | dir->file_size = byte_size; |
| 947 | dir->offset = offset; |
| 948 | dir->start_block = start_block; |
| 949 | dir->i_count = i_count; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 950 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 951 | dir->xattr = xattr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 952 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 953 | SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode); |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 954 | p = inode + offsetof(struct squashfs_ldir_inode_header, index); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 955 | for(i = 0; i < i_count; i++) { |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 956 | SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p); |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 957 | p += offsetof(struct squashfs_dir_index, name); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 958 | memcpy(p, index[i].name, index[i].index.size + 1); |
| 959 | p += index[i].index.size + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 960 | } |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 961 | TRACE("Long directory inode, file_size %lld, start_block " |
| 962 | "0x%llx, offset 0x%x, nlink %d\n", byte_size, |
| 963 | start_block, offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 964 | } |
| 965 | else if(type == SQUASHFS_DIR_TYPE) { |
plougher | 9d80a60 | 2010-12-31 20:47:24 +0000 | [diff] [blame] | 966 | struct squashfs_dir_inode_header *dir = &inode_header.dir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 967 | |
| 968 | inode = get_inode(sizeof(*dir)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 969 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 970 | dir->file_size = byte_size; |
| 971 | dir->offset = offset; |
| 972 | dir->start_block = start_block; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 973 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 974 | SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 975 | TRACE("Directory inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 976 | "offset 0x%x, nlink %d\n", byte_size, start_block, |
| 977 | offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 978 | } |
| 979 | else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) { |
plougher | c70c633 | 2010-12-31 20:11:09 +0000 | [diff] [blame] | 980 | struct squashfs_dev_inode_header *dev = &inode_header.dev; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 981 | unsigned int major = major(buf->st_rdev); |
| 982 | unsigned int minor = minor(buf->st_rdev); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 983 | |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 984 | if(major > 0xfff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 985 | ERROR("Major %d out of range in device node %s, " |
| 986 | "truncating to %d\n", major, filename, |
| 987 | major & 0xfff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 988 | major &= 0xfff; |
| 989 | } |
| 990 | if(minor > 0xfffff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 991 | ERROR("Minor %d out of range in device node %s, " |
| 992 | "truncating to %d\n", minor, filename, |
| 993 | minor & 0xfffff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 994 | minor &= 0xfffff; |
| 995 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 996 | inode = get_inode(sizeof(*dev)); |
| 997 | dev->nlink = nlink; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 998 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 999 | ((minor & ~0xff) << 12); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1000 | SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1001 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1002 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1003 | else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) { |
plougher | 0b4ee5b | 2010-12-31 20:14:00 +0000 | [diff] [blame] | 1004 | struct squashfs_ldev_inode_header *dev = &inode_header.ldev; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1005 | unsigned int major = major(buf->st_rdev); |
| 1006 | unsigned int minor = minor(buf->st_rdev); |
| 1007 | |
| 1008 | if(major > 0xfff) { |
| 1009 | ERROR("Major %d out of range in device node %s, " |
| 1010 | "truncating to %d\n", major, filename, |
| 1011 | major & 0xfff); |
| 1012 | major &= 0xfff; |
| 1013 | } |
| 1014 | if(minor > 0xfffff) { |
| 1015 | ERROR("Minor %d out of range in device node %s, " |
| 1016 | "truncating to %d\n", minor, filename, |
| 1017 | minor & 0xfffff); |
| 1018 | minor &= 0xfffff; |
| 1019 | } |
| 1020 | inode = get_inode(sizeof(*dev)); |
| 1021 | dev->nlink = nlink; |
| 1022 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1023 | ((minor & ~0xff) << 12); |
| 1024 | dev->xattr = xattr; |
| 1025 | SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode); |
| 1026 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
| 1027 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1028 | else if(type == SQUASHFS_SYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1029 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1030 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1031 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1032 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1033 | |
plougher | 5cf38b8 | 2010-12-16 04:55:32 +0000 | [diff] [blame] | 1034 | byte = readlink(filename, buff, 65536); |
| 1035 | if(byte == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1036 | ERROR_START("Failed to read symlink %s", filename); |
| 1037 | ERROR_EXIT(", creating empty symlink\n"); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1038 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | if(byte == 65536) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1042 | ERROR_START("Symlink %s is greater than 65536 bytes!", |
| 1043 | filename); |
| 1044 | ERROR_EXIT(" Creating empty symlink\n"); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1045 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | inode = get_inode(sizeof(*symlink) + byte); |
| 1049 | symlink->nlink = nlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1050 | symlink->symlink_size = byte; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1051 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1052 | strncpy(inode + off, buff, byte); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1053 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1054 | nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1055 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1056 | else if(type == SQUASHFS_LSYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1057 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1058 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1059 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1060 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1061 | |
plougher | df2b9aa | 2010-12-16 04:56:23 +0000 | [diff] [blame] | 1062 | byte = readlink(filename, buff, 65536); |
| 1063 | if(byte == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1064 | ERROR_START("Failed to read symlink %s", filename); |
| 1065 | ERROR_EXIT(", creating empty symlink\n"); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1066 | byte = 0; |
| 1067 | } |
| 1068 | |
| 1069 | if(byte == 65536) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1070 | ERROR_START("Symlink %s is greater than 65536 bytes!", |
| 1071 | filename); |
| 1072 | ERROR_EXIT(" Creating empty symlink\n"); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1073 | byte = 0; |
| 1074 | } |
| 1075 | |
| 1076 | inode = get_inode(sizeof(*symlink) + byte + |
| 1077 | sizeof(unsigned int)); |
| 1078 | symlink->nlink = nlink; |
| 1079 | symlink->symlink_size = byte; |
| 1080 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1081 | strncpy(inode + off, buff, byte); |
| 1082 | SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1); |
| 1083 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1084 | nlink); |
| 1085 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1086 | else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) { |
plougher | e56b986 | 2010-12-31 10:58:35 +0000 | [diff] [blame] | 1087 | struct squashfs_ipc_inode_header *ipc = &inode_header.ipc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1088 | |
| 1089 | inode = get_inode(sizeof(*ipc)); |
| 1090 | ipc->nlink = nlink; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1091 | SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1092 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1093 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1094 | } |
| 1095 | else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) { |
plougher | aa0d122 | 2010-12-31 20:06:24 +0000 | [diff] [blame] | 1096 | struct squashfs_lipc_inode_header *ipc = &inode_header.lipc; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1097 | |
| 1098 | inode = get_inode(sizeof(*ipc)); |
| 1099 | ipc->nlink = nlink; |
| 1100 | ipc->xattr = xattr; |
| 1101 | SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode); |
| 1102 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1103 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1104 | } else |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1105 | BAD_ERROR("Unrecognised inode %d in create_inode\n", type); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1106 | |
| 1107 | *i_no = MKINODE(inode); |
| 1108 | inode_count ++; |
| 1109 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1110 | TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type, |
| 1111 | base->uid, base->guid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1112 | |
| 1113 | return TRUE; |
| 1114 | } |
| 1115 | |
| 1116 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1117 | void add_dir(squashfs_inode inode, unsigned int inode_number, char *name, |
| 1118 | int type, struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1119 | { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 1120 | unsigned char *buff; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1121 | struct squashfs_dir_entry idir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1122 | unsigned int start_block = inode >> 16; |
| 1123 | unsigned int offset = inode & 0xffff; |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1124 | unsigned int size = strlen(name); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1125 | size_t name_off = offsetof(struct squashfs_dir_entry, name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1126 | |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1127 | if(size > SQUASHFS_NAME_LEN) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1128 | size = SQUASHFS_NAME_LEN; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1129 | ERROR("Filename is greater than %d characters, truncating! ..." |
| 1130 | "\n", SQUASHFS_NAME_LEN); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1133 | if(dir->p + sizeof(struct squashfs_dir_entry) + size + |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1134 | sizeof(struct squashfs_dir_header) |
| 1135 | >= dir->buff + dir->size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1136 | buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1137 | if(buff == NULL) |
| 1138 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1139 | |
| 1140 | dir->p = (dir->p - dir->buff) + buff; |
| 1141 | if(dir->entry_count_p) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1142 | dir->entry_count_p = (dir->entry_count_p - dir->buff + |
| 1143 | buff); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1144 | dir->index_count_p = dir->index_count_p - dir->buff + buff; |
| 1145 | dir->buff = buff; |
| 1146 | } |
| 1147 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1148 | if(dir->entry_count == 256 || start_block != dir->start_block || |
| 1149 | ((dir->entry_count_p != NULL) && |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1150 | ((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1151 | dir->index_count_p) > SQUASHFS_METADATA_SIZE)) || |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1152 | ((long long) inode_number - dir->inode_number) > 32767 |
| 1153 | || ((long long) inode_number - dir->inode_number) |
| 1154 | < -32768) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1155 | if(dir->entry_count_p) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1156 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1157 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1158 | if((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1159 | dir->index_count_p) > |
| 1160 | SQUASHFS_METADATA_SIZE) { |
| 1161 | if(dir->i_count % I_COUNT_SIZE == 0) { |
| 1162 | dir->index = realloc(dir->index, |
| 1163 | (dir->i_count + I_COUNT_SIZE) * |
| 1164 | sizeof(struct cached_dir_index)); |
| 1165 | if(dir->index == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1166 | MEM_ERROR(); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1167 | } |
| 1168 | dir->index[dir->i_count].index.index = |
| 1169 | dir->p - dir->buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1170 | dir->index[dir->i_count].index.size = size - 1; |
| 1171 | dir->index[dir->i_count++].name = name; |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1172 | dir->i_size += sizeof(struct squashfs_dir_index) |
| 1173 | + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1174 | dir->index_count_p = dir->p; |
| 1175 | } |
| 1176 | |
| 1177 | dir_header.count = dir->entry_count - 1; |
| 1178 | dir_header.start_block = dir->start_block; |
| 1179 | dir_header.inode_number = dir->inode_number; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1180 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1181 | dir->entry_count_p); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1182 | |
| 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | dir->entry_count_p = dir->p; |
| 1187 | dir->start_block = start_block; |
| 1188 | dir->entry_count = 0; |
| 1189 | dir->inode_number = inode_number; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1190 | dir->p += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1193 | idir.offset = offset; |
| 1194 | idir.type = type; |
| 1195 | idir.size = size - 1; |
| 1196 | idir.inode_number = ((long long) inode_number - dir->inode_number); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1197 | SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p); |
| 1198 | strncpy((char *) dir->p + name_off, name, size); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1199 | dir->p += sizeof(struct squashfs_dir_entry) + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1200 | dir->entry_count ++; |
| 1201 | } |
| 1202 | |
| 1203 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1204 | void write_dir(squashfs_inode *inode, struct dir_info *dir_info, |
| 1205 | struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1206 | { |
| 1207 | unsigned int dir_size = dir->p - dir->buff; |
plougher | 4627ca3 | 2010-12-16 05:03:55 +0000 | [diff] [blame] | 1208 | int data_space = directory_cache_size - directory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1209 | unsigned int directory_block, directory_offset, i_count, index; |
| 1210 | unsigned short c_byte; |
| 1211 | |
| 1212 | if(data_space < dir_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1213 | int realloc_size = directory_cache_size == 0 ? |
| 1214 | ((dir_size + SQUASHFS_METADATA_SIZE) & |
| 1215 | ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1216 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1217 | void *dc = realloc(directory_data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1218 | directory_cache_size + realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1219 | if(dc == NULL) |
| 1220 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1221 | directory_cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1222 | directory_data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | if(dir_size) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1226 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1227 | |
| 1228 | dir_header.count = dir->entry_count - 1; |
| 1229 | dir_header.start_block = dir->start_block; |
| 1230 | dir_header.inode_number = dir->inode_number; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1231 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1232 | memcpy(directory_data_cache + directory_cache_bytes, dir->buff, |
| 1233 | dir_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1234 | } |
| 1235 | directory_offset = directory_cache_bytes; |
| 1236 | directory_block = directory_bytes; |
| 1237 | directory_cache_bytes += dir_size; |
| 1238 | i_count = 0; |
| 1239 | index = SQUASHFS_METADATA_SIZE - directory_offset; |
| 1240 | |
| 1241 | while(1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1242 | while(i_count < dir->i_count && |
| 1243 | dir->index[i_count].index.index < index) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1244 | dir->index[i_count++].index.start_block = |
| 1245 | directory_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1246 | index += SQUASHFS_METADATA_SIZE; |
| 1247 | |
| 1248 | if(directory_cache_bytes < SQUASHFS_METADATA_SIZE) |
| 1249 | break; |
| 1250 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1251 | if((directory_size - directory_bytes) < |
| 1252 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1253 | void *dt = realloc(directory_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1254 | directory_size + (SQUASHFS_METADATA_SIZE << 1) |
| 1255 | + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1256 | if(dt == NULL) |
| 1257 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1258 | directory_size += SQUASHFS_METADATA_SIZE << 1; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1259 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1262 | c_byte = mangle(directory_table + directory_bytes + |
| 1263 | BLOCK_OFFSET, directory_data_cache, |
| 1264 | SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, |
| 1265 | noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1266 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1267 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1268 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1269 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1270 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1271 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1272 | total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 1273 | memmove(directory_data_cache, directory_data_cache + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1274 | SQUASHFS_METADATA_SIZE, directory_cache_bytes - |
| 1275 | SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1276 | directory_cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 1277 | } |
| 1278 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1279 | create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE, |
| 1280 | dir_size + 3, directory_block, directory_offset, NULL, NULL, |
| 1281 | dir, 0); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1282 | |
| 1283 | #ifdef SQUASHFS_TRACE |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1284 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1285 | unsigned char *dirp; |
| 1286 | int count; |
| 1287 | |
| 1288 | TRACE("Directory contents of inode 0x%llx\n", *inode); |
| 1289 | dirp = dir->buff; |
| 1290 | while(dirp < dir->p) { |
| 1291 | char buffer[SQUASHFS_NAME_LEN + 1]; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1292 | struct squashfs_dir_entry idir, *idirp; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1293 | struct squashfs_dir_header dirh; |
| 1294 | SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1295 | &dirh); |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1296 | count = dirh.count + 1; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1297 | dirp += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1298 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1299 | TRACE("\tStart block 0x%x, count %d\n", |
| 1300 | dirh.start_block, count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1301 | |
| 1302 | while(count--) { |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1303 | idirp = (struct squashfs_dir_entry *) dirp; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1304 | SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1305 | strncpy(buffer, idirp->name, idir.size + 1); |
| 1306 | buffer[idir.size + 1] = '\0'; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1307 | TRACE("\t\tname %s, inode offset 0x%x, type " |
| 1308 | "%d\n", buffer, idir.offset, idir.type); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1309 | dirp += sizeof(struct squashfs_dir_entry) + idir.size + |
plougher | e8b26f6 | 2010-12-16 05:06:00 +0000 | [diff] [blame] | 1310 | 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | #endif |
| 1315 | dir_count ++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1319 | static struct file_buffer *get_fragment(struct fragment *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1320 | { |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1321 | struct squashfs_fragment_entry *disk_fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1322 | struct file_buffer *buffer, *compressed_buffer; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1323 | long long start_block; |
| 1324 | int res, size, index = fragment->index; |
| 1325 | char locked; |
| 1326 | |
| 1327 | /* |
| 1328 | * Lookup fragment block in cache. |
| 1329 | * If the fragment block doesn't exist, then get the compressed version |
| 1330 | * from the writer cache or off disk, and decompress it. |
| 1331 | * |
| 1332 | * This routine has two things which complicate the code: |
| 1333 | * |
| 1334 | * 1. Multiple threads can simultaneously lookup/create the |
| 1335 | * same buffer. This means a buffer needs to be "locked" |
| 1336 | * when it is being filled in, to prevent other threads from |
| 1337 | * using it when it is not ready. This is because we now do |
| 1338 | * fragment duplicate checking in parallel. |
| 1339 | * 2. We have two caches which need to be checked for the |
| 1340 | * presence of fragment blocks: the normal fragment cache |
| 1341 | * and a "reserve" cache. The reserve cache is used to |
| 1342 | * prevent an unnecessary pipeline stall when the fragment cache |
| 1343 | * is full of fragments waiting to be compressed. |
| 1344 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1345 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1346 | if(fragment->index == SQUASHFS_INVALID_FRAG) |
| 1347 | return NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1348 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1349 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1350 | pthread_mutex_lock(&dup_mutex); |
| 1351 | |
| 1352 | again: |
| 1353 | buffer = cache_lookup_nowait(fragment_buffer, index, &locked); |
| 1354 | if(buffer) { |
| 1355 | pthread_mutex_unlock(&dup_mutex); |
| 1356 | if(locked) |
| 1357 | /* got a buffer being filled in. Wait for it */ |
| 1358 | cache_wait_unlock(buffer); |
| 1359 | goto finished; |
| 1360 | } |
| 1361 | |
| 1362 | /* not in fragment cache, is it in the reserve cache? */ |
| 1363 | buffer = cache_lookup_nowait(reserve_cache, index, &locked); |
| 1364 | if(buffer) { |
| 1365 | pthread_mutex_unlock(&dup_mutex); |
| 1366 | if(locked) |
| 1367 | /* got a buffer being filled in. Wait for it */ |
| 1368 | cache_wait_unlock(buffer); |
| 1369 | goto finished; |
| 1370 | } |
| 1371 | |
| 1372 | /* in neither cache, try to get it from the fragment cache */ |
| 1373 | buffer = cache_get_nowait(fragment_buffer, index); |
| 1374 | if(!buffer) { |
| 1375 | /* |
| 1376 | * no room, get it from the reserve cache, this is |
| 1377 | * dimensioned so it will always have space (no more than |
| 1378 | * processors + 1 can have an outstanding reserve buffer) |
| 1379 | */ |
| 1380 | buffer = cache_get_nowait(reserve_cache, index); |
| 1381 | if(!buffer) { |
| 1382 | /* failsafe */ |
| 1383 | ERROR("no space in reserve cache\n"); |
| 1384 | goto again; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | pthread_mutex_unlock(&dup_mutex); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1389 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1390 | compressed_buffer = cache_lookup(fwriter_buffer, index); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1391 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1392 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1393 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1394 | disk_fragment = &fragment_table[index]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1395 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1396 | start_block = disk_fragment->start_block; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1397 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1398 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1399 | if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1400 | int error; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1401 | char *data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1402 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1403 | if(compressed_buffer) |
| 1404 | data = compressed_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1405 | else { |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 1406 | data = read_from_disk(start_block, size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1407 | if(data == NULL) { |
| 1408 | ERROR("Failed to read fragment from output" |
| 1409 | " filesystem\n"); |
| 1410 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1411 | } |
| 1412 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1413 | |
plougher | b48442b | 2010-12-31 07:57:54 +0000 | [diff] [blame] | 1414 | res = compressor_uncompress(comp, buffer->data, data, size, |
| 1415 | block_size, &error); |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 1416 | if(res == -1) |
| 1417 | BAD_ERROR("%s uncompress failed with error code %d\n", |
| 1418 | comp->name, error); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1419 | } else if(compressed_buffer) |
| 1420 | memcpy(buffer->data, compressed_buffer->data, size); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1421 | else { |
| 1422 | res = read_fs_bytes(fd, start_block, size, buffer->data); |
Phillip Lougher | 9451938 | 2013-03-06 02:43:42 +0000 | [diff] [blame] | 1423 | if(res == 0) { |
| 1424 | ERROR("Failed to read fragment from output " |
| 1425 | "filesystem\n"); |
| 1426 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1427 | } |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1428 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1429 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1430 | cache_unlock(buffer); |
plougher | 03859fa | 2009-03-31 03:18:18 +0000 | [diff] [blame] | 1431 | cache_block_put(compressed_buffer); |
| 1432 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1433 | finished: |
| 1434 | pthread_cleanup_pop(0); |
| 1435 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1436 | return buffer; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1439 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1440 | unsigned short get_fragment_checksum(struct file_info *file) |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1441 | { |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1442 | struct file_buffer *frag_buffer; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1443 | struct append_file *append; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1444 | int res, index = file->fragment->index; |
| 1445 | unsigned short checksum; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1446 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1447 | if(index == SQUASHFS_INVALID_FRAG) |
| 1448 | return 0; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1449 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1450 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1451 | pthread_mutex_lock(&dup_mutex); |
| 1452 | res = file->have_frag_checksum; |
| 1453 | checksum = file->fragment_checksum; |
Phillip Lougher | c4a8a79 | 2014-04-10 02:23:26 +0100 | [diff] [blame] | 1454 | pthread_cleanup_pop(1); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1455 | |
| 1456 | if(res) |
| 1457 | return checksum; |
| 1458 | |
| 1459 | frag_buffer = get_fragment(file->fragment); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1460 | |
Phillip Lougher | c4a8a79 | 2014-04-10 02:23:26 +0100 | [diff] [blame] | 1461 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1462 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1463 | for(append = file_mapping[index]; append; append = append->next) { |
| 1464 | int offset = append->file->fragment->offset; |
| 1465 | int size = append->file->fragment->size; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1466 | unsigned short cksum = |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1467 | get_checksum_mem(frag_buffer->data + offset, size); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1468 | |
| 1469 | if(file == append->file) |
| 1470 | checksum = cksum; |
| 1471 | |
| 1472 | pthread_mutex_lock(&dup_mutex); |
| 1473 | append->file->fragment_checksum = cksum; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1474 | append->file->have_frag_checksum = TRUE; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1475 | pthread_mutex_unlock(&dup_mutex); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | cache_block_put(frag_buffer); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1479 | pthread_cleanup_pop(0); |
| 1480 | |
| 1481 | return checksum; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1485 | void lock_fragments() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1486 | { |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1487 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1488 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1489 | fragments_locked = TRUE; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1490 | pthread_cleanup_pop(1); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | void unlock_fragments() |
| 1495 | { |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1496 | int frg, size; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1497 | struct file_buffer *write_buffer; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1498 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1499 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1500 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | 71ad964 | 2013-05-25 05:03:48 +0100 | [diff] [blame] | 1501 | |
| 1502 | /* |
| 1503 | * Note queue_empty() is inherently racy with respect to concurrent |
| 1504 | * queue get and pushes. We avoid this because we're holding the |
| 1505 | * fragment_mutex which ensures no other threads can be using the |
| 1506 | * queue at this time. |
| 1507 | */ |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1508 | while(!queue_empty(locked_fragment)) { |
| 1509 | write_buffer = queue_get(locked_fragment); |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1510 | frg = write_buffer->block; |
| 1511 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size); |
| 1512 | fragment_table[frg].start_block = bytes; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1513 | write_buffer->block = bytes; |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1514 | bytes += size; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1515 | fragments_outstanding --; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1516 | queue_put(to_writer, write_buffer); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1517 | TRACE("fragment_locked writing fragment %d, compressed size %d" |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1518 | "\n", frg, size); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1519 | } |
| 1520 | fragments_locked = FALSE; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1521 | pthread_cleanup_pop(1); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1524 | /* Called with the fragment_mutex locked */ |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1525 | void add_pending_fragment(struct file_buffer *write_buffer, int c_byte, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1526 | int fragment) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1527 | { |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1528 | fragment_table[fragment].size = c_byte; |
| 1529 | write_buffer->block = fragment; |
| 1530 | |
| 1531 | queue_put(locked_fragment, write_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1535 | void write_fragment(struct file_buffer *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1536 | { |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1537 | if(fragment == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1538 | return; |
| 1539 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1540 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1541 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1542 | fragment_table[fragment->block].unused = 0; |
| 1543 | fragments_outstanding ++; |
| 1544 | queue_put(to_frag, fragment); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1545 | pthread_cleanup_pop(1); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | |
| 1549 | struct file_buffer *allocate_fragment() |
| 1550 | { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 1551 | struct file_buffer *fragment = cache_get(fragment_buffer, fragments); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1552 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1553 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1554 | pthread_mutex_lock(&fragment_mutex); |
| 1555 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1556 | if(fragments % FRAG_SIZE == 0) { |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1557 | void *ft = realloc(fragment_table, (fragments + |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1558 | FRAG_SIZE) * sizeof(struct squashfs_fragment_entry)); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 1559 | if(ft == NULL) |
| 1560 | MEM_ERROR(); |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1561 | fragment_table = ft; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1562 | } |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1563 | |
| 1564 | fragment->size = 0; |
| 1565 | fragment->block = fragments ++; |
| 1566 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1567 | pthread_cleanup_pop(1); |
| 1568 | |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1569 | return fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 1572 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1573 | static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0}; |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 1574 | |
| 1575 | |
| 1576 | void free_fragment(struct fragment *fragment) |
| 1577 | { |
| 1578 | if(fragment != &empty_fragment) |
| 1579 | free(fragment); |
| 1580 | } |
| 1581 | |
| 1582 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1583 | struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer, |
| 1584 | struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1585 | { |
| 1586 | struct fragment *ffrg; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1587 | struct file_buffer **fragment; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1588 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1589 | if(file_buffer == NULL || file_buffer->size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1590 | return &empty_fragment; |
| 1591 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1592 | fragment = eval_frag_actions(dir_ent); |
| 1593 | |
| 1594 | if((*fragment) && (*fragment)->size + file_buffer->size > block_size) { |
| 1595 | write_fragment(*fragment); |
| 1596 | *fragment = NULL; |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1597 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1598 | |
plougher | e7e6e83 | 2010-12-16 05:08:06 +0000 | [diff] [blame] | 1599 | ffrg = malloc(sizeof(struct fragment)); |
| 1600 | if(ffrg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1601 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1602 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1603 | if(*fragment == NULL) |
| 1604 | *fragment = allocate_fragment(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1605 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1606 | ffrg->index = (*fragment)->block; |
| 1607 | ffrg->offset = (*fragment)->size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1608 | ffrg->size = file_buffer->size; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1609 | memcpy((*fragment)->data + (*fragment)->size, file_buffer->data, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1610 | file_buffer->size); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1611 | (*fragment)->size += file_buffer->size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1612 | |
| 1613 | return ffrg; |
| 1614 | } |
| 1615 | |
| 1616 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1617 | long long generic_write_table(int length, void *buffer, int length2, |
| 1618 | void *buffer2, int uncompressed) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1619 | { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1620 | int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) / |
| 1621 | SQUASHFS_METADATA_SIZE; |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1622 | long long *list, start_bytes; |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1623 | int compressed_size, i, list_size = meta_blocks * sizeof(long long); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1624 | unsigned short c_byte; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1625 | char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2]; |
| 1626 | |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1627 | #ifdef SQUASHFS_TRACE |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1628 | long long obytes = bytes; |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1629 | int olength = length; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1630 | #endif |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1631 | |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1632 | list = malloc(list_size); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1633 | if(list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1634 | MEM_ERROR(); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1635 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1636 | for(i = 0; i < meta_blocks; i++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1637 | int avail_bytes = length > SQUASHFS_METADATA_SIZE ? |
| 1638 | SQUASHFS_METADATA_SIZE : length; |
| 1639 | c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i * |
| 1640 | SQUASHFS_METADATA_SIZE , avail_bytes, |
| 1641 | SQUASHFS_METADATA_SIZE, uncompressed, 0); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1642 | SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1643 | list[i] = bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1644 | compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1645 | BLOCK_OFFSET; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1646 | TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes, |
| 1647 | compressed_size); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1648 | write_destination(fd, bytes, compressed_size, cbuffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1649 | bytes += compressed_size; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1650 | total_bytes += avail_bytes; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1651 | length -= avail_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1654 | start_bytes = bytes; |
| 1655 | if(length2) { |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1656 | write_destination(fd, bytes, length2, buffer2); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1657 | bytes += length2; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1658 | total_bytes += length2; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1661 | SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks); |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1662 | write_destination(fd, bytes, list_size, list); |
| 1663 | bytes += list_size; |
| 1664 | total_bytes += list_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1665 | |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1666 | TRACE("generic_write_table: total uncompressed %d compressed %lld\n", |
| 1667 | olength, bytes - obytes); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1668 | |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1669 | free(list); |
| 1670 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1671 | return start_bytes; |
| 1672 | } |
| 1673 | |
| 1674 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1675 | long long write_fragment_table() |
| 1676 | { |
| 1677 | unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1678 | int i; |
| 1679 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1680 | TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments, |
| 1681 | frag_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1682 | for(i = 0; i < fragments; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1683 | TRACE("write_fragment_table: fragment %d, start_block 0x%llx, " |
| 1684 | "size %d\n", i, fragment_table[i].start_block, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1685 | fragment_table[i].size); |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1686 | SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1689 | return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1693 | char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE]; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1694 | static char *read_from_disk(long long start, unsigned int avail_bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1695 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1696 | int res; |
| 1697 | |
| 1698 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1699 | if(res == 0) |
| 1700 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1701 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1702 | return read_from_file_buffer; |
| 1703 | } |
| 1704 | |
| 1705 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1706 | char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE]; |
| 1707 | char *read_from_disk2(long long start, unsigned int avail_bytes) |
| 1708 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1709 | int res; |
| 1710 | |
| 1711 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1712 | if(res == 0) |
| 1713 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1714 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1715 | return read_from_file_buffer2; |
| 1716 | } |
| 1717 | |
| 1718 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1719 | /* |
| 1720 | * Compute 16 bit BSD checksum over the data |
| 1721 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1722 | unsigned short get_checksum(char *buff, int bytes, unsigned short chksum) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1723 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1724 | unsigned char *b = (unsigned char *) buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1725 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1726 | while(bytes --) { |
| 1727 | chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1; |
| 1728 | chksum += *b++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | return chksum; |
| 1732 | } |
| 1733 | |
| 1734 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1735 | unsigned short get_checksum_disk(long long start, long long l, |
| 1736 | unsigned int *blocks) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1737 | { |
| 1738 | unsigned short chksum = 0; |
| 1739 | unsigned int bytes; |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1740 | struct file_buffer *write_buffer; |
| 1741 | int i; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1742 | |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1743 | for(i = 0; l; i++) { |
| 1744 | bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]); |
| 1745 | if(bytes == 0) /* sparse block */ |
| 1746 | continue; |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1747 | write_buffer = cache_lookup(bwriter_buffer, start); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1748 | if(write_buffer) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1749 | chksum = get_checksum(write_buffer->data, bytes, |
| 1750 | chksum); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1751 | cache_block_put(write_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1752 | } else { |
| 1753 | void *data = read_from_disk(start, bytes); |
| 1754 | if(data == NULL) { |
| 1755 | ERROR("Failed to checksum data from output" |
| 1756 | " filesystem\n"); |
| 1757 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1758 | } |
| 1759 | |
| 1760 | chksum = get_checksum(data, bytes, chksum); |
| 1761 | } |
| 1762 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1763 | l -= bytes; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1764 | start += bytes; |
| 1765 | } |
| 1766 | |
| 1767 | return chksum; |
| 1768 | } |
| 1769 | |
| 1770 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1771 | unsigned short get_checksum_mem(char *buff, int bytes) |
| 1772 | { |
| 1773 | return get_checksum(buff, bytes, 0); |
| 1774 | } |
| 1775 | |
| 1776 | |
| 1777 | unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer) |
| 1778 | { |
| 1779 | if(file_buffer == NULL) |
| 1780 | return 0; |
| 1781 | else |
| 1782 | return get_checksum(file_buffer->data, file_buffer->size, 0); |
| 1783 | } |
| 1784 | |
| 1785 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1786 | #define DUP_HASH(a) (a & 0xffff) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1787 | 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] | 1788 | unsigned int *block_listp, int blocks, unsigned int fragment, |
| 1789 | int offset, int bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1790 | { |
| 1791 | struct fragment *frg; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1792 | unsigned int *block_list = block_listp; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1793 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1794 | struct append_file *append_file; |
| 1795 | struct file_info *file; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1796 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1797 | if(!duplicate_checking || file_size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1798 | return; |
| 1799 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1800 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) { |
| 1801 | if(file_size != dupl_ptr->file_size) |
| 1802 | continue; |
| 1803 | if(blocks != 0 && start != dupl_ptr->start) |
| 1804 | continue; |
| 1805 | if(fragment != dupl_ptr->fragment->index) |
| 1806 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1807 | if(fragment != SQUASHFS_INVALID_FRAG && (offset != |
| 1808 | dupl_ptr->fragment->offset || bytes != |
| 1809 | dupl_ptr->fragment->size)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1810 | continue; |
| 1811 | return; |
| 1812 | } |
| 1813 | |
plougher | 3bb279c | 2010-12-16 05:10:03 +0000 | [diff] [blame] | 1814 | frg = malloc(sizeof(struct fragment)); |
| 1815 | if(frg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1816 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1817 | |
| 1818 | frg->index = fragment; |
| 1819 | frg->offset = offset; |
| 1820 | frg->size = bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1821 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1822 | file = add_non_dup(file_size, file_bytes, block_list, start, frg, 0, 0, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1823 | FALSE, FALSE); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1824 | |
| 1825 | if(fragment == SQUASHFS_INVALID_FRAG) |
| 1826 | return; |
| 1827 | |
| 1828 | append_file = malloc(sizeof(struct append_file)); |
| 1829 | if(append_file == NULL) |
| 1830 | MEM_ERROR(); |
| 1831 | |
| 1832 | append_file->file = file; |
| 1833 | append_file->next = file_mapping[fragment]; |
| 1834 | file_mapping[fragment] = append_file; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1835 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1836 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1837 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1838 | int pre_duplicate(long long file_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1839 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1840 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1841 | |
| 1842 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1843 | if(dupl_ptr->file_size == file_size) |
| 1844 | return TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1845 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1846 | return FALSE; |
| 1847 | } |
| 1848 | |
| 1849 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1850 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 1851 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 1852 | unsigned short checksum, unsigned short fragment_checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1853 | int checksum_flag, int checksum_frag_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1854 | { |
plougher | 51ef9ae | 2010-12-16 05:14:28 +0000 | [diff] [blame] | 1855 | struct file_info *dupl_ptr = malloc(sizeof(struct file_info)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1856 | |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1857 | if(dupl_ptr == NULL) |
| 1858 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1859 | |
| 1860 | dupl_ptr->file_size = file_size; |
| 1861 | dupl_ptr->bytes = bytes; |
| 1862 | dupl_ptr->block_list = block_list; |
| 1863 | dupl_ptr->start = start; |
| 1864 | dupl_ptr->fragment = fragment; |
| 1865 | dupl_ptr->checksum = checksum; |
| 1866 | dupl_ptr->fragment_checksum = fragment_checksum; |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1867 | dupl_ptr->have_frag_checksum = checksum_frag_flag; |
Phillip Lougher | 557cefa | 2014-02-05 04:36:05 +0000 | [diff] [blame] | 1868 | dupl_ptr->have_checksum = checksum_flag; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1869 | |
| 1870 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1871 | pthread_mutex_lock(&dup_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1872 | dupl_ptr->next = dupl[DUP_HASH(file_size)]; |
| 1873 | dupl[DUP_HASH(file_size)] = dupl_ptr; |
| 1874 | dup_files ++; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1875 | pthread_cleanup_pop(1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1876 | |
| 1877 | return dupl_ptr; |
| 1878 | } |
| 1879 | |
| 1880 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1881 | struct fragment *frag_duplicate(struct file_buffer *file_buffer, char *dont_put) |
| 1882 | { |
| 1883 | struct file_info *dupl_ptr; |
| 1884 | struct file_buffer *buffer; |
| 1885 | struct file_info *dupl_start = file_buffer->dupl_start; |
| 1886 | long long file_size = file_buffer->file_size; |
| 1887 | unsigned short checksum = file_buffer->checksum; |
| 1888 | int res; |
| 1889 | |
| 1890 | if(file_buffer->duplicate) { |
| 1891 | TRACE("Found duplicate file, fragment %d, size %d, offset %d, " |
| 1892 | "checksum 0x%x\n", dupl_start->fragment->index, |
| 1893 | file_size, dupl_start->fragment->offset, checksum); |
| 1894 | *dont_put = TRUE; |
| 1895 | return dupl_start->fragment; |
| 1896 | } else { |
| 1897 | *dont_put = FALSE; |
| 1898 | dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 1899 | } |
| 1900 | |
| 1901 | for(; dupl_ptr && dupl_ptr != dupl_start; dupl_ptr = dupl_ptr->next) { |
| 1902 | if(file_size == dupl_ptr->file_size && file_size == |
| 1903 | dupl_ptr->fragment->size) { |
| 1904 | if(get_fragment_checksum(dupl_ptr) == checksum) { |
| 1905 | buffer = get_fragment(dupl_ptr->fragment); |
| 1906 | res = memcmp(file_buffer->data, buffer->data + |
| 1907 | dupl_ptr->fragment->offset, file_size); |
| 1908 | cache_block_put(buffer); |
| 1909 | if(res == 0) |
| 1910 | break; |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | if(!dupl_ptr || dupl_ptr == dupl_start) |
| 1916 | return NULL; |
| 1917 | |
| 1918 | TRACE("Found duplicate file, fragment %d, size %d, offset %d, " |
| 1919 | "checksum 0x%x\n", dupl_ptr->fragment->index, file_size, |
| 1920 | dupl_ptr->fragment->offset, checksum); |
| 1921 | |
| 1922 | return dupl_ptr->fragment; |
| 1923 | } |
| 1924 | |
| 1925 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1926 | struct file_info *duplicate(long long file_size, long long bytes, |
| 1927 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 1928 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1929 | int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1930 | { |
| 1931 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 1932 | int frag_bytes = file_buffer ? file_buffer->size : 0; |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1933 | unsigned short fragment_checksum = file_buffer ? |
| 1934 | file_buffer->checksum : 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1935 | |
| 1936 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 1937 | if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes |
| 1938 | && frag_bytes == dupl_ptr->fragment->size) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1939 | long long target_start, dup_start = dupl_ptr->start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1940 | int block; |
| 1941 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1942 | if(memcmp(*block_list, dupl_ptr->block_list, blocks * |
| 1943 | sizeof(unsigned int)) != 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 1944 | continue; |
| 1945 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1946 | if(checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1947 | checksum = get_checksum_disk(*start, bytes, |
| 1948 | *block_list); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1949 | checksum_flag = TRUE; |
| 1950 | } |
| 1951 | |
Phillip Lougher | 557cefa | 2014-02-05 04:36:05 +0000 | [diff] [blame] | 1952 | if(!dupl_ptr->have_checksum) { |
| 1953 | dupl_ptr->checksum = |
| 1954 | get_checksum_disk(dupl_ptr->start, |
| 1955 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 1956 | dupl_ptr->have_checksum = TRUE; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1959 | if(checksum != dupl_ptr->checksum || |
| 1960 | fragment_checksum != |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1961 | get_fragment_checksum(dupl_ptr)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1962 | continue; |
| 1963 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1964 | target_start = *start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1965 | for(block = 0; block < blocks; block ++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1966 | int size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 1967 | ((*block_list)[block]); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1968 | struct file_buffer *target_buffer = NULL; |
| 1969 | struct file_buffer *dup_buffer = NULL; |
| 1970 | char *target_data, *dup_data; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1971 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1972 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1973 | if(size == 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 1974 | continue; |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1975 | target_buffer = cache_lookup(bwriter_buffer, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1976 | target_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1977 | if(target_buffer) |
| 1978 | target_data = target_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1979 | else { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1980 | target_data = |
| 1981 | read_from_disk(target_start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1982 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1983 | if(target_data == NULL) { |
| 1984 | ERROR("Failed to read data from" |
| 1985 | " output filesystem\n"); |
| 1986 | BAD_ERROR("Output filesystem" |
| 1987 | " corrupted?\n"); |
| 1988 | } |
| 1989 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1990 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1991 | dup_buffer = cache_lookup(bwriter_buffer, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1992 | dup_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1993 | if(dup_buffer) |
| 1994 | dup_data = dup_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1995 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1996 | dup_data = read_from_disk2(dup_start, |
| 1997 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1998 | if(dup_data == NULL) { |
| 1999 | ERROR("Failed to read data from" |
| 2000 | " output filesystem\n"); |
| 2001 | BAD_ERROR("Output filesystem" |
| 2002 | " corrupted?\n"); |
| 2003 | } |
| 2004 | } |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2005 | |
| 2006 | res = memcmp(target_data, dup_data, size); |
| 2007 | cache_block_put(target_buffer); |
| 2008 | cache_block_put(dup_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2009 | if(res != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2010 | break; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2011 | target_start += size; |
| 2012 | dup_start += size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2013 | } |
| 2014 | if(block == blocks) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2015 | struct file_buffer *frag_buffer = |
| 2016 | get_fragment(dupl_ptr->fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2017 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2018 | if(frag_bytes == 0 || |
| 2019 | memcmp(file_buffer->data, |
| 2020 | frag_buffer->data + |
| 2021 | dupl_ptr->fragment->offset, |
| 2022 | frag_bytes) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2023 | TRACE("Found duplicate file, start " |
| 2024 | "0x%llx, size %lld, checksum " |
| 2025 | "0x%x, fragment %d, size %d, " |
| 2026 | "offset %d, checksum 0x%x\n", |
| 2027 | dupl_ptr->start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2028 | dupl_ptr->bytes, |
| 2029 | dupl_ptr->checksum, |
| 2030 | dupl_ptr->fragment->index, |
| 2031 | frag_bytes, |
| 2032 | dupl_ptr->fragment->offset, |
| 2033 | fragment_checksum); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2034 | *block_list = dupl_ptr->block_list; |
| 2035 | *start = dupl_ptr->start; |
| 2036 | *fragment = dupl_ptr->fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2037 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2038 | return 0; |
| 2039 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2040 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2045 | return add_non_dup(file_size, bytes, *block_list, *start, *fragment, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2046 | checksum, fragment_checksum, checksum_flag, TRUE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2050 | inline int is_fragment(struct inode_info *inode) |
| 2051 | { |
| 2052 | int file_size = inode->buf.st_size; |
| 2053 | |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2054 | /* |
| 2055 | * If this block is to be compressed differently to the |
| 2056 | * fragment compression then it cannot be a fragment |
| 2057 | */ |
| 2058 | if(inode->noF != noF) |
| 2059 | return FALSE; |
| 2060 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2061 | return !inode->no_fragments && file_size && (file_size < block_size || |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2062 | (inode->always_use_fragments && file_size & (block_size - 1))); |
| 2063 | } |
| 2064 | |
| 2065 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2066 | void put_file_buffer(struct file_buffer *file_buffer) |
| 2067 | { |
| 2068 | /* |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 2069 | * Decide where to send the file buffer: |
| 2070 | * - compressible non-fragment blocks go to the deflate threads, |
| 2071 | * - fragments go to the process fragment threads, |
| 2072 | * - all others go directly to the main thread |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2073 | */ |
| 2074 | if(file_buffer->error) { |
| 2075 | file_buffer->fragment = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2076 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 2077 | } else if (file_buffer->file_size == 0) |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2078 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 2079 | else if(file_buffer->fragment) |
| 2080 | queue_put(to_process_frag, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2081 | else |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 2082 | queue_put(to_deflate, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2086 | static int seq = 0; |
| 2087 | void reader_read_process(struct dir_ent *dir_ent) |
| 2088 | { |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2089 | long long bytes = 0; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2090 | struct inode_info *inode = dir_ent->inode; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2091 | struct file_buffer *prev_buffer = NULL, *file_buffer; |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2092 | int status, byte, res, child; |
| 2093 | int file = pseudo_exec_file(get_pseudo_file(inode->pseudo_id), &child); |
| 2094 | |
| 2095 | if(!file) { |
| 2096 | file_buffer = cache_get_nohash(reader_buffer); |
| 2097 | file_buffer->sequence = seq ++; |
| 2098 | goto read_err; |
| 2099 | } |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2100 | |
| 2101 | while(1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2102 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2103 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2104 | file_buffer->noD = inode->noD; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2105 | |
| 2106 | byte = read_bytes(file, file_buffer->data, block_size); |
| 2107 | if(byte == -1) |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2108 | goto read_err2; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2109 | |
| 2110 | file_buffer->size = byte; |
| 2111 | file_buffer->file_size = -1; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2112 | file_buffer->error = FALSE; |
| 2113 | file_buffer->fragment = FALSE; |
| 2114 | bytes += byte; |
| 2115 | |
| 2116 | if(byte == 0) |
| 2117 | break; |
| 2118 | |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2119 | /* |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2120 | * Update progress bar size. This is done |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2121 | * on every block rather than waiting for all blocks to be |
| 2122 | * read incase write_file_process() is running in parallel |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2123 | * with this. Otherwise the current progress bar position |
| 2124 | * may get ahead of the progress bar size. |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2125 | */ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2126 | progress_bar_size(1); |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2127 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2128 | if(prev_buffer) |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2129 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2130 | prev_buffer = file_buffer; |
| 2131 | } |
| 2132 | |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2133 | /* |
| 2134 | * Update inode file size now that the size of the dynamic pseudo file |
| 2135 | * is known. This is needed for the -info option. |
| 2136 | */ |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2137 | inode->buf.st_size = bytes; |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2138 | |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2139 | res = waitpid(child, &status, 0); |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2140 | close(file); |
| 2141 | |
plougher | d87d8d1 | 2009-09-10 04:08:16 +0000 | [diff] [blame] | 2142 | if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2143 | goto read_err; |
| 2144 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2145 | if(prev_buffer == NULL) |
| 2146 | prev_buffer = file_buffer; |
| 2147 | else { |
| 2148 | cache_block_put(file_buffer); |
| 2149 | seq --; |
| 2150 | } |
| 2151 | prev_buffer->file_size = bytes; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2152 | prev_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2153 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2154 | |
| 2155 | return; |
| 2156 | |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2157 | read_err2: |
| 2158 | close(file); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2159 | read_err: |
| 2160 | if(prev_buffer) { |
| 2161 | cache_block_put(file_buffer); |
| 2162 | seq --; |
| 2163 | file_buffer = prev_buffer; |
| 2164 | } |
| 2165 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2166 | put_file_buffer(file_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2170 | void reader_read_file(struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2171 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2172 | struct stat *buf = &dir_ent->inode->buf, buf2; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2173 | struct file_buffer *file_buffer; |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2174 | int blocks, file, res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2175 | long long bytes, read_size; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2176 | struct inode_info *inode = dir_ent->inode; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2177 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2178 | if(inode->read) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2179 | return; |
| 2180 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2181 | inode->read = TRUE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2182 | again: |
| 2183 | bytes = 0; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2184 | read_size = buf->st_size; |
| 2185 | blocks = (read_size + block_size - 1) >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2186 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2187 | file = open(pathname_reader(dir_ent), O_RDONLY); |
plougher | c1d258e | 2010-12-16 05:16:45 +0000 | [diff] [blame] | 2188 | if(file == -1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2189 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2190 | file_buffer->sequence = seq ++; |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2191 | goto read_err2; |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2192 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2193 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2194 | do { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2195 | file_buffer = cache_get_nohash(reader_buffer); |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2196 | file_buffer->file_size = read_size; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2197 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2198 | file_buffer->noD = inode->noD; |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2199 | file_buffer->error = FALSE; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2200 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2201 | /* |
| 2202 | * Always try to read block_size bytes from the file rather |
| 2203 | * than expected bytes (which will be less than the block_size |
| 2204 | * at the file tail) to check that the file hasn't grown |
| 2205 | * since being stated. If it is longer (or shorter) than |
| 2206 | * expected, then restat, and try again. Note the special |
| 2207 | * case where the file is an exact multiple of the block_size |
| 2208 | * is dealt with later. |
| 2209 | */ |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2210 | file_buffer->size = read_bytes(file, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2211 | block_size); |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2212 | if(file_buffer->size == -1) |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2213 | goto read_err; |
| 2214 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2215 | bytes += file_buffer->size; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2216 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2217 | if(blocks > 1) { |
| 2218 | /* non-tail block should be exactly block_size */ |
| 2219 | if(file_buffer->size < block_size) |
| 2220 | goto restat; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2221 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2222 | file_buffer->fragment = FALSE; |
| 2223 | put_file_buffer(file_buffer); |
| 2224 | } |
Phillip Lougher | 8678d30 | 2014-03-02 03:02:19 +0000 | [diff] [blame] | 2225 | } while(-- blocks > 0); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2226 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2227 | /* Overall size including tail should match */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2228 | if(read_size != bytes) |
| 2229 | goto restat; |
| 2230 | |
Phillip Lougher | 191dfcd | 2014-03-11 02:58:52 +0000 | [diff] [blame] | 2231 | if(read_size && read_size % block_size == 0) { |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2232 | /* |
| 2233 | * Special case where we've not tried to read past the end of |
| 2234 | * the file. We expect to get EOF, i.e. the file isn't larger |
| 2235 | * than we expect. |
| 2236 | */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2237 | char buffer; |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2238 | int res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2239 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2240 | res = read_bytes(file, &buffer, 1); |
| 2241 | if(res == -1) |
| 2242 | goto read_err; |
| 2243 | |
| 2244 | if(res != 0) |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2245 | goto restat; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2248 | file_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2249 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2250 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2251 | close(file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2252 | |
| 2253 | return; |
| 2254 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2255 | restat: |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2256 | res = fstat(file, &buf2); |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2257 | if(res == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 2258 | ERROR("Cannot stat dir/file %s because %s\n", |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2259 | pathname_reader(dir_ent), strerror(errno)); |
| 2260 | goto read_err; |
| 2261 | } |
| 2262 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2263 | if(read_size != buf2.st_size) { |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2264 | close(file); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2265 | memcpy(buf, &buf2, sizeof(struct stat)); |
| 2266 | file_buffer->error = 2; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2267 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2268 | goto again; |
| 2269 | } |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2270 | read_err: |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2271 | close(file); |
| 2272 | read_err2: |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2273 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2274 | put_file_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | |
| 2278 | void reader_scan(struct dir_info *dir) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2279 | struct dir_ent *dir_ent = dir->list; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2280 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2281 | for(; dir_ent; dir_ent = dir_ent->next) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2282 | struct stat *buf = &dir_ent->inode->buf; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2283 | if(dir_ent->inode->root_entry) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2284 | continue; |
| 2285 | |
plougher | b3977eb | 2010-05-02 02:08:48 +0000 | [diff] [blame] | 2286 | if(IS_PSEUDO_PROCESS(dir_ent->inode)) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2287 | reader_read_process(dir_ent); |
| 2288 | continue; |
| 2289 | } |
| 2290 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2291 | switch(buf->st_mode & S_IFMT) { |
| 2292 | case S_IFREG: |
| 2293 | reader_read_file(dir_ent); |
| 2294 | break; |
| 2295 | case S_IFDIR: |
| 2296 | reader_scan(dir_ent->dir); |
| 2297 | break; |
| 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | |
| 2303 | void *reader(void *arg) |
| 2304 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2305 | if(!sorted) |
| 2306 | reader_scan(queue_get(to_reader)); |
| 2307 | else { |
| 2308 | int i; |
| 2309 | struct priority_entry *entry; |
| 2310 | |
| 2311 | queue_get(to_reader); |
| 2312 | for(i = 65535; i >= 0; i--) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2313 | for(entry = priority_list[i]; entry; |
| 2314 | entry = entry->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2315 | reader_read_file(entry->dir); |
| 2316 | } |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2317 | |
| 2318 | pthread_exit(NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | |
| 2322 | void *writer(void *arg) |
| 2323 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2324 | while(1) { |
| 2325 | struct file_buffer *file_buffer = queue_get(to_writer); |
| 2326 | off_t off; |
| 2327 | |
| 2328 | if(file_buffer == NULL) { |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2329 | queue_put(from_writer, NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2330 | continue; |
| 2331 | } |
| 2332 | |
| 2333 | off = file_buffer->block; |
| 2334 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2335 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2336 | pthread_mutex_lock(&pos_mutex); |
| 2337 | |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2338 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | d23000c | 2013-02-06 22:05:45 +0000 | [diff] [blame] | 2339 | ERROR("writer: Lseek on destination failed because " |
| 2340 | "%s, offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2341 | BAD_ERROR("Probably out of space on output " |
Phillip Lougher | 3f4ccd2 | 2013-02-11 04:40:09 +0000 | [diff] [blame] | 2342 | "%s\n", block_device ? "block device" : |
| 2343 | "filesystem"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2346 | if(write_bytes(fd, file_buffer->data, |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2347 | file_buffer->size) == -1) |
| 2348 | BAD_ERROR("Failed to write to output %s\n", |
Phillip Lougher | e9df455 | 2013-02-14 23:06:29 +0000 | [diff] [blame] | 2349 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2350 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2351 | pthread_cleanup_pop(1); |
| 2352 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2353 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2358 | int all_zero(struct file_buffer *file_buffer) |
| 2359 | { |
| 2360 | int i; |
| 2361 | long entries = file_buffer->size / sizeof(long); |
| 2362 | long *p = (long *) file_buffer->data; |
| 2363 | |
| 2364 | for(i = 0; i < entries && p[i] == 0; i++); |
| 2365 | |
| 2366 | if(i == entries) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2367 | for(i = file_buffer->size & ~(sizeof(long) - 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2368 | i < file_buffer->size && file_buffer->data[i] == 0; |
| 2369 | i++); |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2370 | |
| 2371 | return i == file_buffer->size; |
| 2372 | } |
| 2373 | |
| 2374 | return 0; |
| 2375 | } |
| 2376 | |
| 2377 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2378 | void *deflator(void *arg) |
| 2379 | { |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2380 | struct file_buffer *write_buffer = cache_get_nohash(bwriter_buffer); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2381 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2382 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2383 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2384 | res = compressor_init(comp, &stream, block_size, 1); |
| 2385 | if(res) |
| 2386 | BAD_ERROR("deflator:: compressor_init failed\n"); |
| 2387 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2388 | while(1) { |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 2389 | struct file_buffer *file_buffer = queue_get(to_deflate); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2390 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2391 | if(sparse_files && all_zero(file_buffer)) { |
Phillip Lougher | 4885438 | 2011-09-09 03:36:55 +0100 | [diff] [blame] | 2392 | file_buffer->c_byte = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2393 | seq_queue_put(to_main, file_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2394 | } else { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2395 | write_buffer->c_byte = mangle2(stream, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2396 | write_buffer->data, file_buffer->data, |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2397 | file_buffer->size, block_size, |
| 2398 | file_buffer->noD, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2399 | write_buffer->sequence = file_buffer->sequence; |
| 2400 | write_buffer->file_size = file_buffer->file_size; |
| 2401 | write_buffer->block = file_buffer->block; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2402 | write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2403 | (write_buffer->c_byte); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2404 | write_buffer->fragment = FALSE; |
| 2405 | write_buffer->error = FALSE; |
| 2406 | cache_block_put(file_buffer); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2407 | seq_queue_put(to_main, write_buffer); |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2408 | write_buffer = cache_get_nohash(bwriter_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2409 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | |
| 2414 | void *frag_deflator(void *arg) |
| 2415 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2416 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2417 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2418 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2419 | res = compressor_init(comp, &stream, block_size, 1); |
| 2420 | if(res) |
| 2421 | BAD_ERROR("frag_deflator:: compressor_init failed\n"); |
| 2422 | |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2423 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
| 2424 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2425 | while(1) { |
| 2426 | int c_byte, compressed_size; |
| 2427 | struct file_buffer *file_buffer = queue_get(to_frag); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2428 | struct file_buffer *write_buffer = |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2429 | cache_get(fwriter_buffer, file_buffer->block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2430 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2431 | c_byte = mangle2(stream, write_buffer->data, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2432 | file_buffer->size, block_size, noF, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2433 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte); |
plougher | d036a31 | 2008-03-08 01:38:27 +0000 | [diff] [blame] | 2434 | write_buffer->size = compressed_size; |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2435 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2436 | if(fragments_locked == FALSE) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2437 | fragment_table[file_buffer->block].size = c_byte; |
| 2438 | fragment_table[file_buffer->block].start_block = bytes; |
| 2439 | write_buffer->block = bytes; |
| 2440 | bytes += compressed_size; |
| 2441 | fragments_outstanding --; |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2442 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2443 | queue_put(to_writer, write_buffer); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2444 | TRACE("Writing fragment %lld, uncompressed size %d, " |
| 2445 | "compressed size %d\n", file_buffer->block, |
| 2446 | file_buffer->size, compressed_size); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2447 | } else { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2448 | add_pending_fragment(write_buffer, c_byte, |
| 2449 | file_buffer->block); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2450 | pthread_mutex_unlock(&fragment_mutex); |
| 2451 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2452 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2453 | } |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2454 | |
| 2455 | pthread_cleanup_pop(0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2459 | struct file_buffer *get_file_buffer() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2460 | { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2461 | struct file_buffer *file_buffer = seq_queue_get(to_main); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2462 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2463 | return file_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2467 | void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent, |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2468 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2469 | { |
| 2470 | file_count ++; |
| 2471 | *duplicate_file = FALSE; |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2472 | cache_block_put(file_buffer); |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2473 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0, |
| 2474 | NULL, &empty_fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | |
Phillip Lougher | 8531f87 | 2014-03-01 03:22:41 +0000 | [diff] [blame] | 2478 | void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2479 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2480 | { |
Phillip Lougher | 8531f87 | 2014-03-01 03:22:41 +0000 | [diff] [blame] | 2481 | int size = file_buffer->file_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2482 | struct fragment *fragment; |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2483 | unsigned short checksum = file_buffer->checksum; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2484 | char dont_put; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2485 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2486 | fragment = frag_duplicate(file_buffer, &dont_put); |
| 2487 | *duplicate_file = !fragment; |
| 2488 | if(!fragment) { |
| 2489 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
| 2490 | if(duplicate_checking) |
| 2491 | add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, |
| 2492 | TRUE, TRUE); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2493 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2494 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2495 | if(dont_put) |
| 2496 | free(file_buffer); |
| 2497 | else |
| 2498 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2499 | |
| 2500 | total_bytes += size; |
| 2501 | file_count ++; |
| 2502 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2503 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2504 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2505 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2506 | 0, NULL, fragment, NULL, 0); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2507 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2508 | if(!duplicate_checking) |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2509 | free_fragment(fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2513 | int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2514 | struct file_buffer *read_buffer, int *duplicate_file) |
| 2515 | { |
| 2516 | long long read_size, file_bytes, start; |
| 2517 | struct fragment *fragment; |
| 2518 | unsigned int *block_list = NULL; |
| 2519 | int block = 0, status; |
| 2520 | long long sparse = 0; |
| 2521 | struct file_buffer *fragment_buffer = NULL; |
| 2522 | |
| 2523 | *duplicate_file = FALSE; |
| 2524 | |
| 2525 | lock_fragments(); |
| 2526 | |
| 2527 | file_bytes = 0; |
| 2528 | start = bytes; |
| 2529 | while (1) { |
| 2530 | read_size = read_buffer->file_size; |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2531 | if(read_buffer->fragment) |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2532 | fragment_buffer = read_buffer; |
| 2533 | else { |
| 2534 | block_list = realloc(block_list, (block + 1) * |
| 2535 | sizeof(unsigned int)); |
| 2536 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2537 | MEM_ERROR(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2538 | block_list[block ++] = read_buffer->c_byte; |
| 2539 | if(read_buffer->c_byte) { |
| 2540 | read_buffer->block = bytes; |
| 2541 | bytes += read_buffer->size; |
Phillip Lougher | 84e20d7 | 2014-02-23 05:23:41 +0000 | [diff] [blame] | 2542 | cache_hash(read_buffer, read_buffer->block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2543 | file_bytes += read_buffer->size; |
| 2544 | queue_put(to_writer, read_buffer); |
| 2545 | } else { |
| 2546 | sparse += read_buffer->size; |
| 2547 | cache_block_put(read_buffer); |
| 2548 | } |
| 2549 | } |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2550 | inc_progress_bar(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2551 | |
| 2552 | if(read_size != -1) |
| 2553 | break; |
| 2554 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2555 | read_buffer = get_file_buffer(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2556 | if(read_buffer->error) |
| 2557 | goto read_err; |
| 2558 | } |
| 2559 | |
| 2560 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2561 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2562 | |
| 2563 | if(duplicate_checking) |
| 2564 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2565 | 0, fragment_buffer ? fragment_buffer->checksum : 0, |
| 2566 | FALSE, TRUE); |
| 2567 | cache_block_put(fragment_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2568 | file_count ++; |
| 2569 | total_bytes += read_size; |
| 2570 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2571 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2572 | block, block_list, fragment, NULL, sparse); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2573 | |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2574 | if(duplicate_checking == FALSE) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2575 | free(block_list); |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2576 | free_fragment(fragment); |
| 2577 | } |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2578 | |
| 2579 | return 0; |
| 2580 | |
| 2581 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2582 | dec_progress_bar(block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2583 | status = read_buffer->error; |
| 2584 | bytes = start; |
| 2585 | if(!block_device) { |
| 2586 | int res; |
| 2587 | |
| 2588 | queue_put(to_writer, NULL); |
| 2589 | if(queue_get(from_writer) != 0) |
| 2590 | EXIT_MKSQUASHFS(); |
| 2591 | res = ftruncate(fd, bytes); |
| 2592 | if(res != 0) |
| 2593 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2594 | strerror(errno)); |
| 2595 | } |
| 2596 | unlock_fragments(); |
| 2597 | free(block_list); |
| 2598 | cache_block_put(read_buffer); |
| 2599 | return status; |
| 2600 | } |
| 2601 | |
| 2602 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2603 | int write_file_blocks_dup(squashfs_inode *inode, struct dir_ent *dir_ent, |
Phillip Lougher | 1807caa | 2014-03-01 03:28:33 +0000 | [diff] [blame] | 2604 | struct file_buffer *read_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2605 | { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2606 | int block, thresh; |
Phillip Lougher | 1807caa | 2014-03-01 03:28:33 +0000 | [diff] [blame] | 2607 | long long read_size = read_buffer->file_size; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2608 | long long file_bytes, dup_start, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2609 | struct fragment *fragment; |
| 2610 | struct file_info *dupl_ptr; |
| 2611 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2612 | unsigned int *block_list, *block_listp; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2613 | struct file_buffer **buffer_list; |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2614 | int status; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2615 | long long sparse = 0; |
| 2616 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2617 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2618 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2619 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2620 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2621 | block_listp = block_list; |
| 2622 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2623 | buffer_list = malloc(blocks * sizeof(struct file_buffer *)); |
| 2624 | if(buffer_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2625 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2626 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2627 | lock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2628 | |
| 2629 | file_bytes = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2630 | start = dup_start = bytes; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 2631 | thresh = blocks > bwriter_size ? blocks - bwriter_size : 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2632 | |
| 2633 | for(block = 0; block < blocks;) { |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2634 | if(read_buffer->fragment) { |
Phillip Lougher | 38cb1ab | 2013-01-13 21:20:09 +0000 | [diff] [blame] | 2635 | block_list[block] = 0; |
Phillip Lougher | 19de5b6 | 2013-01-13 21:31:51 +0000 | [diff] [blame] | 2636 | buffer_list[block] = NULL; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2637 | fragment_buffer = read_buffer; |
| 2638 | blocks = read_size >> block_log; |
| 2639 | } else { |
| 2640 | block_list[block] = read_buffer->c_byte; |
| 2641 | |
| 2642 | if(read_buffer->c_byte) { |
| 2643 | read_buffer->block = bytes; |
| 2644 | bytes += read_buffer->size; |
| 2645 | file_bytes += read_buffer->size; |
Phillip Lougher | 84e20d7 | 2014-02-23 05:23:41 +0000 | [diff] [blame] | 2646 | cache_hash(read_buffer, read_buffer->block); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2647 | if(block < thresh) { |
| 2648 | buffer_list[block] = NULL; |
| 2649 | queue_put(to_writer, read_buffer); |
| 2650 | } else |
| 2651 | buffer_list[block] = read_buffer; |
| 2652 | } else { |
| 2653 | buffer_list[block] = NULL; |
| 2654 | sparse += read_buffer->size; |
| 2655 | cache_block_put(read_buffer); |
| 2656 | } |
| 2657 | } |
| 2658 | inc_progress_bar(); |
| 2659 | |
| 2660 | if(++block < blocks) { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2661 | read_buffer = get_file_buffer(); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2662 | if(read_buffer->error) |
| 2663 | goto read_err; |
| 2664 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2667 | dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2668 | &fragment, fragment_buffer, blocks, 0, FALSE); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2669 | |
| 2670 | if(dupl_ptr) { |
| 2671 | *duplicate_file = FALSE; |
| 2672 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2673 | if(buffer_list[block]) |
| 2674 | queue_put(to_writer, buffer_list[block]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2675 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2676 | dupl_ptr->fragment = fragment; |
| 2677 | } else { |
| 2678 | *duplicate_file = TRUE; |
| 2679 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2680 | cache_block_put(buffer_list[block]); |
| 2681 | bytes = start; |
| 2682 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2683 | int res; |
| 2684 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2685 | queue_put(to_writer, NULL); |
| 2686 | if(queue_get(from_writer) != 0) |
| 2687 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2688 | res = ftruncate(fd, bytes); |
| 2689 | if(res != 0) |
| 2690 | BAD_ERROR("Failed to truncate dest file because" |
| 2691 | " %s\n", strerror(errno)); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2692 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2695 | unlock_fragments(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2696 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2697 | free(buffer_list); |
| 2698 | file_count ++; |
| 2699 | total_bytes += read_size; |
| 2700 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2701 | /* |
| 2702 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2703 | * a smaller block count on stat calls to sparse files. This is |
| 2704 | * to ensure intelligent applications like cp correctly handle the |
| 2705 | * file as a sparse file. If the file in the original filesystem isn't |
| 2706 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2707 | * report it as non-sparse on stat calls to preserve semantics |
| 2708 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2709 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2710 | sparse = 0; |
| 2711 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2712 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, |
| 2713 | dup_start, blocks, block_listp, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2714 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2715 | if(*duplicate_file == TRUE) |
| 2716 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2717 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2718 | return 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2719 | |
| 2720 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2721 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2722 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2723 | bytes = start; |
| 2724 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2725 | int res; |
| 2726 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2727 | queue_put(to_writer, NULL); |
| 2728 | if(queue_get(from_writer) != 0) |
| 2729 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2730 | res = ftruncate(fd, bytes); |
| 2731 | if(res != 0) |
| 2732 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2733 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2734 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2735 | unlock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2736 | for(blocks = thresh; blocks < block; blocks ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2737 | cache_block_put(buffer_list[blocks]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2738 | free(buffer_list); |
| 2739 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2740 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2741 | return status; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2745 | int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2746 | struct file_buffer *read_buffer, int *dup) |
| 2747 | { |
| 2748 | long long read_size = read_buffer->file_size; |
| 2749 | long long file_bytes, start; |
| 2750 | struct fragment *fragment; |
| 2751 | unsigned int *block_list; |
| 2752 | int block, status; |
| 2753 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2754 | long long sparse = 0; |
| 2755 | struct file_buffer *fragment_buffer = NULL; |
| 2756 | |
| 2757 | if(pre_duplicate(read_size)) |
| 2758 | return write_file_blocks_dup(inode, dir_ent, read_buffer, dup); |
| 2759 | |
| 2760 | *dup = FALSE; |
| 2761 | |
| 2762 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2763 | if(block_list == NULL) |
| 2764 | MEM_ERROR(); |
| 2765 | |
| 2766 | lock_fragments(); |
| 2767 | |
| 2768 | file_bytes = 0; |
| 2769 | start = bytes; |
| 2770 | for(block = 0; block < blocks;) { |
| 2771 | if(read_buffer->fragment) { |
| 2772 | block_list[block] = 0; |
| 2773 | fragment_buffer = read_buffer; |
| 2774 | blocks = read_size >> block_log; |
| 2775 | } else { |
| 2776 | block_list[block] = read_buffer->c_byte; |
| 2777 | if(read_buffer->c_byte) { |
| 2778 | read_buffer->block = bytes; |
| 2779 | bytes += read_buffer->size; |
| 2780 | cache_hash(read_buffer, read_buffer->block); |
| 2781 | file_bytes += read_buffer->size; |
| 2782 | queue_put(to_writer, read_buffer); |
| 2783 | } else { |
| 2784 | sparse += read_buffer->size; |
| 2785 | cache_block_put(read_buffer); |
| 2786 | } |
| 2787 | } |
| 2788 | inc_progress_bar(); |
| 2789 | |
| 2790 | if(++block < blocks) { |
| 2791 | read_buffer = get_file_buffer(); |
| 2792 | if(read_buffer->error) |
| 2793 | goto read_err; |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | unlock_fragments(); |
| 2798 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2799 | |
| 2800 | if(duplicate_checking) |
| 2801 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2802 | 0, fragment_buffer ? fragment_buffer->checksum : 0, |
| 2803 | FALSE, TRUE); |
| 2804 | cache_block_put(fragment_buffer); |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2805 | file_count ++; |
| 2806 | total_bytes += read_size; |
| 2807 | |
| 2808 | /* |
| 2809 | * sparse count is needed to ensure squashfs correctly reports a |
| 2810 | * a smaller block count on stat calls to sparse files. This is |
| 2811 | * to ensure intelligent applications like cp correctly handle the |
| 2812 | * file as a sparse file. If the file in the original filesystem isn't |
| 2813 | * stored as a sparse file then still store it sparsely in squashfs, but |
| 2814 | * report it as non-sparse on stat calls to preserve semantics |
| 2815 | */ |
| 2816 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2817 | sparse = 0; |
| 2818 | |
| 2819 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2820 | blocks, block_list, fragment, NULL, sparse); |
| 2821 | |
| 2822 | if(duplicate_checking == FALSE) { |
| 2823 | free(block_list); |
| 2824 | free_fragment(fragment); |
| 2825 | } |
| 2826 | |
| 2827 | return 0; |
| 2828 | |
| 2829 | read_err: |
| 2830 | dec_progress_bar(block); |
| 2831 | status = read_buffer->error; |
| 2832 | bytes = start; |
| 2833 | if(!block_device) { |
| 2834 | int res; |
| 2835 | |
| 2836 | queue_put(to_writer, NULL); |
| 2837 | if(queue_get(from_writer) != 0) |
| 2838 | EXIT_MKSQUASHFS(); |
| 2839 | res = ftruncate(fd, bytes); |
| 2840 | if(res != 0) |
| 2841 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2842 | strerror(errno)); |
| 2843 | } |
| 2844 | unlock_fragments(); |
| 2845 | free(block_list); |
| 2846 | cache_block_put(read_buffer); |
| 2847 | return status; |
| 2848 | } |
| 2849 | |
| 2850 | |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2851 | void write_file(squashfs_inode *inode, struct dir_ent *dir, int *dup) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2852 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2853 | int status; |
| 2854 | struct file_buffer *read_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2855 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2856 | again: |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2857 | read_buffer = get_file_buffer(); |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2858 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2859 | |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2860 | if(status) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2861 | cache_block_put(read_buffer); |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2862 | else if(read_buffer->file_size == -1) |
| 2863 | status = write_file_process(inode, dir, read_buffer, dup); |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2864 | else if(read_buffer->file_size == 0) |
| 2865 | write_file_empty(inode, dir, read_buffer, dup); |
| 2866 | else if(read_buffer->fragment && read_buffer->c_byte) |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2867 | write_file_frag(inode, dir, read_buffer, dup); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2868 | else |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2869 | status = write_file_blocks(inode, dir, read_buffer, dup); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2870 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2871 | if(status == 2) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2872 | ERROR("File %s changed size while reading filesystem, " |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2873 | "attempting to re-read\n", pathname(dir)); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2874 | goto again; |
| 2875 | } else if(status == 1) { |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2876 | ERROR_START("Failed to read file %s", pathname(dir)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 2877 | ERROR_EXIT(", creating empty file\n"); |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2878 | write_file_empty(inode, dir, NULL, dup); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2879 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2883 | #define BUFF_SIZE 512 |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2884 | char *name; |
| 2885 | char *basename_r(); |
| 2886 | |
| 2887 | char *getbase(char *pathname) |
| 2888 | { |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2889 | static char *b_buffer = NULL; |
| 2890 | static int b_size = BUFF_SIZE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2891 | char *result; |
| 2892 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2893 | if(b_buffer == NULL) { |
| 2894 | b_buffer = malloc(b_size); |
| 2895 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2896 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | while(1) { |
| 2900 | if(*pathname != '/') { |
| 2901 | result = getcwd(b_buffer, b_size); |
| 2902 | if(result == NULL && errno != ERANGE) |
| 2903 | BAD_ERROR("Getcwd failed in getbase\n"); |
| 2904 | |
| 2905 | /* enough room for pathname + "/" + '\0' terminator? */ |
| 2906 | if(result && strlen(pathname) + 2 <= |
| 2907 | b_size - strlen(b_buffer)) { |
| 2908 | strcat(strcat(b_buffer, "/"), pathname); |
| 2909 | break; |
| 2910 | } |
| 2911 | } else if(strlen(pathname) < b_size) { |
| 2912 | strcpy(b_buffer, pathname); |
| 2913 | break; |
| 2914 | } |
| 2915 | |
| 2916 | /* Buffer not large enough, realloc and try again */ |
| 2917 | b_buffer = realloc(b_buffer, b_size += BUFF_SIZE); |
| 2918 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2919 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2922 | name = b_buffer; |
| 2923 | if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0)) |
| 2924 | return NULL; |
| 2925 | else |
| 2926 | return result; |
| 2927 | } |
| 2928 | |
| 2929 | |
| 2930 | char *basename_r() |
| 2931 | { |
| 2932 | char *s; |
| 2933 | char *p; |
| 2934 | int n = 1; |
| 2935 | |
| 2936 | for(;;) { |
| 2937 | s = name; |
| 2938 | if(*name == '\0') |
| 2939 | return NULL; |
| 2940 | if(*name != '/') { |
| 2941 | while(*name != '\0' && *name != '/') name++; |
| 2942 | n = name - s; |
| 2943 | } |
| 2944 | while(*name == '/') name++; |
| 2945 | if(strncmp(s, ".", n) == 0) |
| 2946 | continue; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2947 | if((*name == '\0') || (strncmp(s, "..", n) == 0) || |
| 2948 | ((p = basename_r()) == NULL)) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2949 | s[n] = '\0'; |
| 2950 | return s; |
| 2951 | } |
| 2952 | if(strcmp(p, "..") == 0) |
| 2953 | continue; |
| 2954 | return p; |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2959 | struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2960 | { |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2961 | int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino); |
| 2962 | struct inode_info *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2963 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2964 | /* |
| 2965 | * Look-up inode in hash table, if it already exists we have a |
| 2966 | * hard-link, so increment the nlink count and return it. |
| 2967 | * Don't do the look-up for directories because we don't hard-link |
| 2968 | * directories. |
| 2969 | */ |
| 2970 | if ((buf->st_mode & S_IFMT) != S_IFDIR) { |
| 2971 | for(inode = inode_info[ino_hash]; inode; inode = inode->next) { |
| 2972 | if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) { |
| 2973 | inode->nlink ++; |
| 2974 | return inode; |
| 2975 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2976 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
plougher | 288f685 | 2010-12-16 05:22:55 +0000 | [diff] [blame] | 2979 | inode = malloc(sizeof(struct inode_info)); |
| 2980 | if(inode == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2981 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2982 | |
| 2983 | memcpy(&inode->buf, buf, sizeof(struct stat)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2984 | inode->read = FALSE; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2985 | inode->root_entry = FALSE; |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2986 | inode->pseudo_file = pseudo; |
| 2987 | inode->pseudo_id = id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2988 | inode->inode = SQUASHFS_INVALID_BLK; |
| 2989 | inode->nlink = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 2990 | inode->inode_number = 0; |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 2991 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2992 | /* |
| 2993 | * Copy filesystem wide defaults into inode, these filesystem |
| 2994 | * wide defaults may be altered on an individual inode basis by |
| 2995 | * user specified actions |
| 2996 | * |
| 2997 | */ |
| 2998 | inode->no_fragments = no_fragments; |
| 2999 | inode->always_use_fragments = always_use_fragments; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 3000 | inode->noD = noD; |
| 3001 | inode->noF = noF; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 3002 | |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3003 | if((buf->st_mode & S_IFMT) == S_IFREG) |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 3004 | progress_bar_size((buf->st_size + block_size - 1) >> block_log); |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3005 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3006 | inode->next = inode_info[ino_hash]; |
| 3007 | inode_info[ino_hash] = inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3008 | |
| 3009 | return inode; |
| 3010 | } |
| 3011 | |
| 3012 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3013 | inline struct inode_info *lookup_inode(struct stat *buf) |
| 3014 | { |
| 3015 | return lookup_inode2(buf, 0, 0); |
| 3016 | } |
| 3017 | |
| 3018 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3019 | inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this) |
| 3020 | { |
| 3021 | if (inode->inode_number == 0) |
| 3022 | inode->inode_number = use_this ? : inode_no ++; |
| 3023 | } |
| 3024 | |
| 3025 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3026 | inline struct dir_ent *create_dir_entry(char *name, char *source_name, |
| 3027 | char *nonstandard_pathname, struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3028 | { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3029 | struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent)); |
| 3030 | if(dir_ent == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3031 | MEM_ERROR(); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3032 | |
| 3033 | dir_ent->name = name; |
| 3034 | dir_ent->source_name = source_name; |
| 3035 | dir_ent->nonstandard_pathname = nonstandard_pathname; |
| 3036 | dir_ent->our_dir = dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3037 | dir_ent->next = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3038 | |
| 3039 | return dir_ent; |
| 3040 | } |
| 3041 | |
| 3042 | |
| 3043 | inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir, |
| 3044 | struct inode_info *inode_info) |
| 3045 | { |
| 3046 | struct dir_info *dir = dir_ent->our_dir; |
| 3047 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3048 | if(sub_dir) |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3049 | sub_dir->dir_ent = dir_ent; |
| 3050 | dir_ent->inode = inode_info; |
| 3051 | dir_ent->dir = sub_dir; |
| 3052 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3053 | dir_ent->next = dir->list; |
| 3054 | dir->list = dir_ent; |
| 3055 | dir->count++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | |
| 3059 | inline void add_dir_entry2(char *name, char *source_name, |
| 3060 | char *nonstandard_pathname, struct dir_info *sub_dir, |
| 3061 | struct inode_info *inode_info, struct dir_info *dir) |
| 3062 | { |
| 3063 | struct dir_ent *dir_ent = create_dir_entry(name, source_name, |
| 3064 | nonstandard_pathname, dir); |
| 3065 | |
| 3066 | |
| 3067 | add_dir_entry(dir_ent, sub_dir, inode_info); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3071 | inline void free_dir_entry(struct dir_ent *dir_ent) |
| 3072 | { |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3073 | if(dir_ent->name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3074 | free(dir_ent->name); |
| 3075 | |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3076 | if(dir_ent->source_name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3077 | free(dir_ent->source_name); |
| 3078 | |
| 3079 | free(dir_ent); |
| 3080 | } |
| 3081 | |
| 3082 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3083 | inline void add_excluded(struct dir_info *dir) |
| 3084 | { |
| 3085 | dir->excluded ++; |
| 3086 | } |
| 3087 | |
| 3088 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3089 | void dir_scan(squashfs_inode *inode, char *pathname, |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 3090 | struct dir_ent *(_readdir)(struct dir_info *), int progress) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3091 | { |
| 3092 | struct stat buf; |
| 3093 | struct dir_info *dir_info = dir_scan1(pathname, "", paths, _readdir, 1); |
| 3094 | struct dir_ent *dir_ent; |
| 3095 | |
| 3096 | if(dir_info == NULL) |
| 3097 | return; |
| 3098 | |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3099 | /* |
| 3100 | * Process most actions and any pseudo files |
| 3101 | */ |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 3102 | if(actions() || get_pseudo()) |
| 3103 | dir_scan2(dir_info, get_pseudo()); |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3104 | |
Phillip Lougher | e92cb51 | 2012-11-15 02:22:28 +0000 | [diff] [blame] | 3105 | /* |
| 3106 | * Process move actions |
| 3107 | */ |
| 3108 | if(move_actions()) { |
| 3109 | dir_scan3(dir_info, dir_info); |
| 3110 | do_move_actions(); |
| 3111 | } |
| 3112 | |
Phillip Lougher | 71ae14e | 2012-11-15 02:27:50 +0000 | [diff] [blame] | 3113 | /* |
| 3114 | * Process empty actions |
| 3115 | */ |
| 3116 | if(empty_actions()) |
| 3117 | dir_scan4(dir_info); |
| 3118 | |
Phillip Lougher | eb69dad | 2012-11-15 03:34:02 +0000 | [diff] [blame] | 3119 | /* |
| 3120 | * Sort directories and compute the inode numbers |
| 3121 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3122 | dir_scan5(dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3123 | |
Phillip Lougher | d0fe1d5 | 2012-10-09 03:34:34 +0100 | [diff] [blame] | 3124 | dir_ent = create_dir_entry("", NULL, pathname, |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 3125 | scan1_opendir("", "", 0)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3126 | |
| 3127 | if(pathname[0] == '\0') { |
| 3128 | /* |
| 3129 | * dummy top level directory, if multiple sources specified on |
| 3130 | * command line |
| 3131 | */ |
| 3132 | memset(&buf, 0, sizeof(buf)); |
| 3133 | buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR; |
| 3134 | buf.st_uid = getuid(); |
| 3135 | buf.st_gid = getgid(); |
| 3136 | buf.st_mtime = time(NULL); |
| 3137 | buf.st_dev = 0; |
| 3138 | buf.st_ino = 0; |
| 3139 | dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0); |
| 3140 | } else { |
Phillip Lougher | 38a35ec | 2012-11-30 04:31:35 +0000 | [diff] [blame] | 3141 | if(lstat(pathname, &buf) == -1) |
| 3142 | /* source directory has disappeared? */ |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3143 | BAD_ERROR("Cannot stat source directory %s because %s\n", |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3144 | pathname, strerror(errno)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3145 | dir_ent->inode = lookup_inode(&buf); |
| 3146 | } |
| 3147 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3148 | alloc_inode_no(dir_ent->inode, root_inode_number); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3149 | dir_ent->dir = dir_info; |
| 3150 | dir_info->dir_ent = dir_ent; |
| 3151 | |
Andy Lutomirski | 3f31dcf | 2012-09-21 18:08:08 -0700 | [diff] [blame] | 3152 | eval_actions(dir_ent); |
| 3153 | |
Phillip Lougher | 19b877d | 2013-02-23 11:15:38 +0000 | [diff] [blame] | 3154 | if(sorted) |
| 3155 | generate_file_priorities(dir_info, 0, |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3156 | &dir_info->dir_ent->inode->buf); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 3157 | |
| 3158 | if(appending) { |
| 3159 | sigset_t sigmask; |
| 3160 | |
| 3161 | restore_thread = init_restore_thread(); |
| 3162 | sigemptyset(&sigmask); |
| 3163 | sigaddset(&sigmask, SIGINT); |
| 3164 | sigaddset(&sigmask, SIGTERM); |
| 3165 | sigaddset(&sigmask, SIGUSR1); |
| 3166 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
| 3167 | BAD_ERROR("Failed to set signal mask\n"); |
| 3168 | write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0"); |
| 3169 | } |
| 3170 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3171 | queue_put(to_reader, dir_info); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 3172 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3173 | if(sorted) |
| 3174 | sort_files_and_write(dir_info); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 3175 | |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 3176 | set_progressbar_state(progress); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3177 | dir_scan6(inode, dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3178 | dir_ent->inode->inode = *inode; |
| 3179 | dir_ent->inode->type = SQUASHFS_DIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3183 | /* |
| 3184 | * dir_scan1 routines... |
Phillip Lougher | e8630be | 2012-11-11 02:24:24 +0000 | [diff] [blame] | 3185 | * These scan the source directories into memory for processing. |
| 3186 | * Exclude actions are processed here (in contrast to the other actions) |
| 3187 | * because they affect what is scanned. |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3188 | */ |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3189 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3190 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3191 | struct dir_info *dir; |
| 3192 | |
plougher | 6da792b | 2010-12-16 05:25:39 +0000 | [diff] [blame] | 3193 | dir = malloc(sizeof(struct dir_info)); |
| 3194 | if(dir == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3195 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3196 | |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3197 | if(pathname[0] != '\0') { |
| 3198 | dir->linuxdir = opendir(pathname); |
| 3199 | if(dir->linuxdir == NULL) { |
| 3200 | free(dir); |
| 3201 | return NULL; |
| 3202 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3203 | } |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3204 | |
Phillip Lougher | 3cb5d6c | 2013-01-09 05:50:29 +0000 | [diff] [blame] | 3205 | dir->pathname = strdup(pathname); |
| 3206 | dir->subpath = strdup(subpath); |
Phillip Lougher | ded70fa | 2011-12-25 02:14:58 +0000 | [diff] [blame] | 3207 | dir->count = 0; |
| 3208 | dir->directory_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3209 | dir->dir_is_ldir = TRUE; |
| 3210 | dir->list = NULL; |
Phillip Lougher | 0b5a124 | 2011-12-25 03:22:42 +0000 | [diff] [blame] | 3211 | dir->depth = depth; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3212 | dir->excluded = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3213 | |
| 3214 | return dir; |
| 3215 | } |
| 3216 | |
| 3217 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3218 | struct dir_ent *scan1_encomp_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3219 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3220 | static int index = 0; |
| 3221 | |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3222 | if(dir->count < old_root_entries) { |
| 3223 | int i; |
| 3224 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3225 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3226 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3227 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3228 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3229 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3230 | } |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3231 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3232 | |
| 3233 | while(index < source) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3234 | char *basename = NULL; |
| 3235 | char *dir_name = getbase(source_path[index]); |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3236 | int pass = 1, res; |
plougher | 89fe2c7 | 2010-12-16 05:31:34 +0000 | [diff] [blame] | 3237 | |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3238 | if(dir_name == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3239 | ERROR_START("Bad source directory %s", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3240 | source_path[index]); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3241 | ERROR_EXIT(" - skipping ...\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3242 | index ++; |
| 3243 | continue; |
| 3244 | } |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3245 | dir_name = strdup(dir_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3246 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3247 | struct dir_ent *dir_ent = dir->list; |
| 3248 | |
| 3249 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3250 | dir_ent = dir_ent->next); |
| 3251 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3252 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3253 | ERROR("Source directory entry %s already used! - trying" |
| 3254 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3255 | if(pass == 1) |
| 3256 | basename = dir_name; |
| 3257 | else |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3258 | free(dir_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3259 | res = asprintf(&dir_name, "%s_%d", basename, pass++); |
| 3260 | if(res == -1) |
| 3261 | BAD_ERROR("asprintf failed in " |
| 3262 | "scan1_encomp_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3263 | ERROR("%s\n", dir_name); |
| 3264 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3265 | return create_dir_entry(dir_name, basename, |
| 3266 | source_path[index ++], dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3267 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3268 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3272 | struct dir_ent *scan1_single_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3273 | { |
| 3274 | struct dirent *d_name; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3275 | int i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3276 | |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3277 | if(dir->count < old_root_entries) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3278 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3279 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3280 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3281 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3282 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3283 | } |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3284 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3285 | |
| 3286 | if((d_name = readdir(dir->linuxdir)) != NULL) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3287 | char *basename = NULL; |
| 3288 | char *dir_name = strdup(d_name->d_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3289 | int pass = 1, res; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3290 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3291 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3292 | struct dir_ent *dir_ent = dir->list; |
| 3293 | |
| 3294 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3295 | dir_ent = dir_ent->next); |
| 3296 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3297 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3298 | ERROR("Source directory entry %s already used! - trying" |
| 3299 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3300 | if (pass == 1) |
| 3301 | basename = dir_name; |
| 3302 | else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3303 | free(dir_name); |
| 3304 | res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++); |
| 3305 | if(res == -1) |
| 3306 | BAD_ERROR("asprintf failed in " |
| 3307 | "scan1_single_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3308 | ERROR("%s\n", dir_name); |
| 3309 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3310 | return create_dir_entry(dir_name, basename, NULL, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3313 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3317 | struct dir_ent *scan1_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3318 | { |
plougher | 480d9bf | 2010-12-18 02:28:39 +0000 | [diff] [blame] | 3319 | struct dirent *d_name = readdir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3320 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3321 | return d_name ? |
| 3322 | create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) : |
| 3323 | NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3327 | void scan1_freedir(struct dir_info *dir) |
| 3328 | { |
plougher | fbed12b | 2006-02-07 12:45:53 +0000 | [diff] [blame] | 3329 | if(dir->pathname[0] != '\0') |
| 3330 | closedir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3334 | struct dir_info *dir_scan1(char *filename, char *subpath, |
| 3335 | struct pathnames *paths, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3336 | struct dir_ent *(_readdir)(struct dir_info *), int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3337 | { |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3338 | struct dir_info *dir = scan1_opendir(filename, subpath, depth); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3339 | struct dir_ent *dir_ent; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3340 | |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3341 | if(dir == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3342 | ERROR_START("Could not open %s", filename); |
| 3343 | ERROR_EXIT(", skipping...\n"); |
Phillip Lougher | 87db567 | 2013-01-09 05:40:46 +0000 | [diff] [blame] | 3344 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3347 | while((dir_ent = _readdir(dir))) { |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3348 | struct dir_info *sub_dir; |
| 3349 | struct stat buf; |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 3350 | struct pathnames *new = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3351 | char *filename = pathname(dir_ent); |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3352 | char *subpath = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3353 | char *dir_name = dir_ent->name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3354 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3355 | if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3356 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3357 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3358 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3359 | |
| 3360 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3361 | ERROR_START("Cannot stat dir/file %s because %s", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3362 | filename, strerror(errno)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3363 | ERROR_EXIT(", ignoring\n"); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3364 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3365 | continue; |
| 3366 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3367 | |
| 3368 | if((buf.st_mode & S_IFMT) != S_IFREG && |
Phillip Lougher | e4ff36a | 2012-11-19 01:34:47 +0000 | [diff] [blame] | 3369 | (buf.st_mode & S_IFMT) != S_IFDIR && |
| 3370 | (buf.st_mode & S_IFMT) != S_IFLNK && |
| 3371 | (buf.st_mode & S_IFMT) != S_IFCHR && |
| 3372 | (buf.st_mode & S_IFMT) != S_IFBLK && |
| 3373 | (buf.st_mode & S_IFMT) != S_IFIFO && |
| 3374 | (buf.st_mode & S_IFMT) != S_IFSOCK) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3375 | ERROR_START("File %s has unrecognised filetype %d", |
| 3376 | filename, buf.st_mode & S_IFMT); |
| 3377 | ERROR_EXIT(", ignoring\n"); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3378 | free_dir_entry(dir_ent); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3379 | continue; |
| 3380 | } |
| 3381 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3382 | if((old_exclude && old_excluded(filename, &buf)) || |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3383 | (!old_exclude && excluded(dir_name, paths, &new))) { |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3384 | add_excluded(dir); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3385 | free_dir_entry(dir_ent); |
Phillip Lougher | 10d8de0 | 2011-08-29 00:14:48 +0100 | [diff] [blame] | 3386 | continue; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3387 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3388 | |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3389 | if(exclude_actions()) { |
| 3390 | subpath = subpathname(dir_ent); |
| 3391 | |
| 3392 | if(eval_exclude_actions(dir_name, filename, subpath, |
| 3393 | &buf, depth)) { |
| 3394 | add_excluded(dir); |
| 3395 | free_dir_entry(dir_ent); |
| 3396 | continue; |
| 3397 | } |
| 3398 | } |
| 3399 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3400 | if((buf.st_mode & S_IFMT) == S_IFDIR) { |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3401 | if(subpath == NULL) |
| 3402 | subpath = subpathname(dir_ent); |
| 3403 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3404 | sub_dir = dir_scan1(filename, subpath, new, |
| 3405 | scan1_readdir, depth + 1); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3406 | if(sub_dir == NULL) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3407 | free_dir_entry(dir_ent); |
Phillip Lougher | 205c1e0 | 2013-01-07 01:37:56 +0000 | [diff] [blame] | 3408 | free(new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3409 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3410 | } |
Phillip Lougher | 6a78a2d | 2011-12-28 03:54:19 +0000 | [diff] [blame] | 3411 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3412 | dir->directory_count ++; |
| 3413 | } else |
| 3414 | sub_dir = NULL; |
| 3415 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3416 | add_dir_entry(dir_ent, sub_dir, lookup_inode(&buf)); |
Phillip Lougher | 205c1e0 | 2013-01-07 01:37:56 +0000 | [diff] [blame] | 3417 | free(new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | scan1_freedir(dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3421 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3422 | return dir; |
| 3423 | } |
| 3424 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3425 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3426 | /* |
| 3427 | * dir_scan2 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3428 | * This processes most actions and any pseudo files |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3429 | */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3430 | struct dir_ent *scan2_readdir(struct dir_info *dir, struct dir_ent *dir_ent) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3431 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3432 | if (dir_ent == NULL) |
| 3433 | dir_ent = dir->list; |
| 3434 | else |
| 3435 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3436 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3437 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next); |
| 3438 | |
| 3439 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | |
| 3443 | struct dir_ent *scan2_lookup(struct dir_info *dir, char *name) |
| 3444 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3445 | struct dir_ent *dir_ent = dir->list; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3446 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3447 | for(; dir_ent && strcmp(dir_ent->name, name) != 0; |
| 3448 | dir_ent = dir_ent->next); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3449 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3450 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3451 | } |
| 3452 | |
| 3453 | |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3454 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3455 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3456 | struct dir_ent *dir_ent = NULL; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3457 | struct pseudo_entry *pseudo_ent; |
| 3458 | struct stat buf; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3459 | static int pseudo_ino = 1; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3460 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3461 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3462 | struct inode_info *inode_info = dir_ent->inode; |
| 3463 | struct stat *buf = &inode_info->buf; |
| 3464 | char *name = dir_ent->name; |
| 3465 | |
Phillip Lougher | 89d757c | 2011-09-20 00:33:19 +0100 | [diff] [blame] | 3466 | eval_actions(dir_ent); |
| 3467 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3468 | if((buf->st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3469 | dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) { |
| 3473 | dir_ent = scan2_lookup(dir, pseudo_ent->name); |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3474 | if(pseudo_ent->dev->type == 'm') { |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3475 | struct stat *buf; |
| 3476 | if(dir_ent == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3477 | ERROR_START("Pseudo modify file \"%s\" does " |
| 3478 | "not exist in source filesystem.", |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3479 | pseudo_ent->pathname); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3480 | ERROR_EXIT(" Ignoring.\n"); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3481 | continue; |
| 3482 | } |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3483 | if(dir_ent->inode->root_entry) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3484 | ERROR_START("Pseudo modify file \"%s\" is a " |
| 3485 | "pre-existing file in the filesystem " |
| 3486 | "being appended to. It cannot be "\ |
| 3487 | "modified.", pseudo_ent->pathname); |
| 3488 | ERROR_EXIT(" Ignoring.\n"); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3489 | continue; |
| 3490 | } |
| 3491 | buf = &dir_ent->inode->buf; |
| 3492 | buf->st_mode = (buf->st_mode & S_IFMT) | |
| 3493 | pseudo_ent->dev->mode; |
| 3494 | buf->st_uid = pseudo_ent->dev->uid; |
| 3495 | buf->st_gid = pseudo_ent->dev->gid; |
| 3496 | continue; |
| 3497 | } |
| 3498 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3499 | if(dir_ent) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3500 | if(dir_ent->inode->root_entry) { |
| 3501 | ERROR_START("Pseudo file \"%s\" is a " |
| 3502 | "pre-existing file in the filesystem " |
| 3503 | "being appended to.", |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3504 | pseudo_ent->pathname); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3505 | ERROR_EXIT(" Ignoring.\n"); |
| 3506 | } else { |
| 3507 | ERROR_START("Pseudo file \"%s\" exists in " |
| 3508 | "source filesystem \"%s\".", |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3509 | pseudo_ent->pathname, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3510 | pathname(dir_ent)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3511 | ERROR_EXIT("\nIgnoring, exclude it (-e/-ef) to " |
| 3512 | "override.\n"); |
| 3513 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3514 | continue; |
| 3515 | } |
| 3516 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3517 | memset(&buf, 0, sizeof(buf)); |
| 3518 | buf.st_mode = pseudo_ent->dev->mode; |
| 3519 | buf.st_uid = pseudo_ent->dev->uid; |
| 3520 | buf.st_gid = pseudo_ent->dev->gid; |
| 3521 | buf.st_rdev = makedev(pseudo_ent->dev->major, |
| 3522 | pseudo_ent->dev->minor); |
plougher | 7e58f4d | 2009-04-05 12:06:19 +0000 | [diff] [blame] | 3523 | buf.st_mtime = time(NULL); |
plougher | 1a3fbf2 | 2009-04-05 12:04:16 +0000 | [diff] [blame] | 3524 | buf.st_ino = pseudo_ino ++; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3525 | |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3526 | if(pseudo_ent->dev->type == 'd') { |
| 3527 | struct dir_ent *dir_ent = |
| 3528 | create_dir_entry(pseudo_ent->name, NULL, |
| 3529 | pseudo_ent->pathname, dir); |
| 3530 | char *subpath = strdup(subpathname(dir_ent)); |
| 3531 | struct dir_info *sub_dir = scan1_opendir("", subpath, |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3532 | dir->depth + 1); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3533 | if(sub_dir == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3534 | ERROR_START("Could not create pseudo directory " |
| 3535 | "\"%s\"", pseudo_ent->pathname); |
| 3536 | ERROR_EXIT(", skipping...\n"); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3537 | free(subpath); |
| 3538 | pseudo_ino --; |
| 3539 | continue; |
| 3540 | } |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3541 | dir_scan2(sub_dir, pseudo_ent->pseudo); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3542 | dir->directory_count ++; |
| 3543 | add_dir_entry(dir_ent, sub_dir, |
| 3544 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0)); |
| 3545 | } else if(pseudo_ent->dev->type == 'f') { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3546 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3547 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3548 | lookup_inode2(&buf, PSEUDO_FILE_PROCESS, |
| 3549 | pseudo_ent->dev->pseudo_id), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3550 | } else { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3551 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3552 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3553 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3554 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3555 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3559 | /* |
| 3560 | * dir_scan3 routines... |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3561 | * This processes the move action |
| 3562 | */ |
| 3563 | void dir_scan3(struct dir_info *root, struct dir_info *dir) |
| 3564 | { |
| 3565 | struct dir_ent *dir_ent = NULL; |
| 3566 | |
| 3567 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
| 3568 | |
| 3569 | eval_move_actions(root, dir_ent); |
| 3570 | |
| 3571 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
| 3572 | dir_scan3(root, dir_ent->dir); |
| 3573 | } |
| 3574 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3575 | |
| 3576 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3577 | /* |
| 3578 | * dir_scan4 routines... |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3579 | * This processes the empty action. This action has to be processed after |
| 3580 | * all other actions because the previous exclude and move actions and the |
| 3581 | * pseudo actions affect whether a directory is empty |
| 3582 | */ |
| 3583 | void dir_scan4(struct dir_info *dir) |
| 3584 | { |
| 3585 | struct dir_ent *dir_ent = dir->list, *prev = NULL; |
| 3586 | |
| 3587 | while(dir_ent) { |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3588 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) { |
| 3589 | dir_scan4(dir_ent->dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3590 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3591 | if(eval_empty_actions(dir_ent)) { |
| 3592 | struct dir_ent *tmp = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3593 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3594 | /* |
| 3595 | * delete sub-directory, this is by definition |
| 3596 | * empty |
| 3597 | */ |
| 3598 | free(dir_ent->dir->pathname); |
| 3599 | free(dir_ent->dir->subpath); |
| 3600 | free(dir_ent->dir); |
| 3601 | |
| 3602 | /* remove dir_ent from list */ |
| 3603 | dir_ent = dir_ent->next; |
| 3604 | if(prev) |
| 3605 | prev->next = dir_ent; |
| 3606 | else |
| 3607 | dir->list = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3608 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3609 | /* free it */ |
| 3610 | free_dir_entry(tmp); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3611 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3612 | /* update counts */ |
| 3613 | dir->directory_count --; |
| 3614 | dir->count --; |
| 3615 | add_excluded(dir); |
| 3616 | continue; |
| 3617 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3618 | } |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3619 | |
| 3620 | prev = dir_ent; |
| 3621 | dir_ent = dir_ent->next; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3622 | } |
| 3623 | } |
| 3624 | |
| 3625 | |
| 3626 | /* |
| 3627 | * dir_scan5 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3628 | * This sorts every directory and computes the inode numbers |
| 3629 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3630 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3631 | /* |
| 3632 | * Bottom up linked list merge sort. |
| 3633 | * |
| 3634 | * Qsort and other O(n log n) algorithms work well with arrays but not |
| 3635 | * linked lists. Merge sort another O(n log n) sort algorithm on the other hand |
| 3636 | * is not ideal for arrays (as it needs an additonal n storage locations |
| 3637 | * as sorting is not done in place), but it is ideal for linked lists because |
| 3638 | * it doesn't require any extra storage, |
| 3639 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3640 | void sort_directory(struct dir_info *dir) |
| 3641 | { |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3642 | struct dir_ent *cur, *l1, *l2, *next; |
| 3643 | int len1, len2, stride = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3644 | |
Phillip Lougher | d3459a8 | 2014-04-18 01:37:54 +0100 | [diff] [blame] | 3645 | if(dir->list == NULL || dir->count < 2) |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3646 | return; |
| 3647 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3648 | /* |
| 3649 | * We can consider our linked-list to be made up of stride length |
| 3650 | * sublists. Eacn iteration around this loop merges adjacent |
| 3651 | * stride length sublists into larger 2*stride sublists. We stop |
| 3652 | * when stride becomes equal to the entire list. |
| 3653 | * |
| 3654 | * Initially stride = 1 (by definition a sublist of 1 is sorted), and |
| 3655 | * these 1 element sublists are merged into 2 element sublists, which |
| 3656 | * are then merged into 4 element sublists and so on. |
| 3657 | */ |
| 3658 | do { |
| 3659 | l2 = dir->list; /* head of current linked list */ |
| 3660 | cur = NULL; /* empty output list */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3661 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3662 | /* |
| 3663 | * Iterate through the linked list, merging adjacent sublists. |
| 3664 | * On each interation l2 points to the next sublist pair to be |
| 3665 | * merged (if there's only one sublist left this is simply added |
| 3666 | * to the output list) |
| 3667 | */ |
| 3668 | while(l2) { |
| 3669 | l1 = l2; |
| 3670 | for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next); |
| 3671 | len2 = stride; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3672 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3673 | /* |
| 3674 | * l1 points to first sublist. |
| 3675 | * l2 points to second sublist. |
| 3676 | * Merge them onto the output list |
| 3677 | */ |
| 3678 | while(len1 && l2 && len2) { |
| 3679 | if(strcmp(l1->name, l2->name) <= 0) { |
| 3680 | next = l1; |
| 3681 | l1 = l1->next; |
| 3682 | len1 --; |
| 3683 | } else { |
| 3684 | next = l2; |
| 3685 | l2 = l2->next; |
| 3686 | len2 --; |
| 3687 | } |
| 3688 | |
| 3689 | if(cur) { |
| 3690 | cur->next = next; |
| 3691 | cur = next; |
| 3692 | } else |
| 3693 | dir->list = cur = next; |
| 3694 | } |
| 3695 | /* |
| 3696 | * One sublist is now empty, copy the other one onto the |
| 3697 | * output list |
| 3698 | */ |
| 3699 | for(; len1; len1 --, l1 = l1->next) { |
| 3700 | if(cur) { |
| 3701 | cur->next = l1; |
| 3702 | cur = l1; |
| 3703 | } else |
| 3704 | dir->list = cur = l1; |
| 3705 | } |
| 3706 | for(; l2 && len2; len2 --, l2 = l2->next) { |
| 3707 | if(cur) { |
| 3708 | cur->next = l2; |
| 3709 | cur = l2; |
| 3710 | } else |
| 3711 | dir->list = cur = l2; |
| 3712 | } |
| 3713 | } |
| 3714 | cur->next = NULL; |
| 3715 | stride = stride << 1; |
| 3716 | } while(stride < dir->count); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3720 | void dir_scan5(struct dir_info *dir) |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3721 | { |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3722 | struct dir_ent *dir_ent; |
| 3723 | unsigned int byte_count = 0; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3724 | |
| 3725 | sort_directory(dir); |
| 3726 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3727 | for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) { |
| 3728 | byte_count += strlen(dir_ent->name) + |
| 3729 | sizeof(struct squashfs_dir_entry); |
| 3730 | |
| 3731 | if(dir_ent->inode->root_entry) |
| 3732 | continue; |
| 3733 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3734 | alloc_inode_no(dir_ent->inode, 0); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3735 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3736 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | e69b588 | 2012-11-19 01:31:09 +0000 | [diff] [blame] | 3737 | dir_scan5(dir_ent->dir); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3738 | } |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3739 | |
| 3740 | if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE)) |
| 3741 | dir->dir_is_ldir = FALSE; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | |
| 3745 | /* |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3746 | * dir_scan6 routines... |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3747 | * This generates the filesystem metadata and writes it out to the destination |
| 3748 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3749 | void scan6_init_dir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3750 | { |
| 3751 | dir->buff = malloc(SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3752 | if(dir->buff == NULL) |
| 3753 | MEM_ERROR(); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3754 | |
| 3755 | dir->size = SQUASHFS_METADATA_SIZE; |
| 3756 | dir->p = dir->index_count_p = dir->buff; |
| 3757 | dir->entry_count = 256; |
| 3758 | dir->entry_count_p = NULL; |
| 3759 | dir->index = NULL; |
| 3760 | dir->i_count = dir->i_size = 0; |
| 3761 | } |
| 3762 | |
| 3763 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3764 | struct dir_ent *scan6_readdir(struct directory *dir, struct dir_info *dir_info, |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3765 | struct dir_ent *dir_ent) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3766 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3767 | if (dir_ent == NULL) |
| 3768 | dir_ent = dir_info->list; |
| 3769 | else |
| 3770 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3771 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3772 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next) |
| 3773 | add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number, |
| 3774 | dir_ent->name, dir_ent->inode->type, dir); |
| 3775 | |
| 3776 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3780 | void scan6_freedir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3781 | { |
| 3782 | if(dir->index) |
| 3783 | free(dir->index); |
| 3784 | free(dir->buff); |
| 3785 | } |
| 3786 | |
| 3787 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3788 | void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3789 | { |
| 3790 | int squashfs_type; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3791 | int duplicate_file; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3792 | struct directory dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3793 | struct dir_ent *dir_ent = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3794 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3795 | scan6_init_dir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3796 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3797 | while((dir_ent = scan6_readdir(&dir, dir_info, dir_ent)) != NULL) { |
Phillip Lougher | 0a67088 | 2012-10-05 04:09:13 +0100 | [diff] [blame] | 3798 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3799 | |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 3800 | update_info(dir_ent); |
| 3801 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3802 | if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) { |
| 3803 | switch(buf->st_mode & S_IFMT) { |
| 3804 | case S_IFREG: |
| 3805 | squashfs_type = SQUASHFS_FILE_TYPE; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3806 | write_file(inode, dir_ent, |
| 3807 | &duplicate_file); |
| 3808 | INFO("file %s, uncompressed size %lld " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3809 | "bytes %s\n", |
| 3810 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3811 | (long long) buf->st_size, |
| 3812 | duplicate_file ? "DUPLICATE" : |
| 3813 | ""); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3814 | break; |
| 3815 | |
| 3816 | case S_IFDIR: |
| 3817 | squashfs_type = SQUASHFS_DIR_TYPE; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3818 | dir_scan6(inode, dir_ent->dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3819 | break; |
| 3820 | |
| 3821 | case S_IFLNK: |
| 3822 | squashfs_type = SQUASHFS_SYMLINK_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3823 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3824 | squashfs_type, 0, 0, 0, NULL, |
| 3825 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3826 | INFO("symbolic link %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3827 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3828 | sym_count ++; |
| 3829 | break; |
| 3830 | |
| 3831 | case S_IFCHR: |
| 3832 | squashfs_type = SQUASHFS_CHRDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3833 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3834 | squashfs_type, 0, 0, 0, NULL, |
| 3835 | NULL, NULL, 0); |
| 3836 | INFO("character device %s inode 0x%llx" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3837 | "\n", subpathname(dir_ent), |
| 3838 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3839 | dev_count ++; |
| 3840 | break; |
| 3841 | |
| 3842 | case S_IFBLK: |
| 3843 | squashfs_type = SQUASHFS_BLKDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3844 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3845 | squashfs_type, 0, 0, 0, NULL, |
| 3846 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3847 | INFO("block device %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3848 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3849 | dev_count ++; |
| 3850 | break; |
| 3851 | |
| 3852 | case S_IFIFO: |
| 3853 | squashfs_type = SQUASHFS_FIFO_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3854 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3855 | squashfs_type, 0, 0, 0, NULL, |
| 3856 | NULL, NULL, 0); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3857 | INFO("fifo %s inode 0x%llx\n", |
| 3858 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3859 | fifo_count ++; |
| 3860 | break; |
| 3861 | |
| 3862 | case S_IFSOCK: |
| 3863 | squashfs_type = SQUASHFS_SOCKET_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3864 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3865 | squashfs_type, 0, 0, 0, NULL, |
| 3866 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3867 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3868 | "0x%llx\n", |
| 3869 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3870 | sock_count ++; |
| 3871 | break; |
| 3872 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 3873 | default: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3874 | BAD_ERROR("%s unrecognised file type, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3875 | "mode is %x\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3876 | subpathname(dir_ent), |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3877 | buf->st_mode); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3878 | } |
| 3879 | dir_ent->inode->inode = *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3880 | dir_ent->inode->type = squashfs_type; |
| 3881 | } else { |
| 3882 | *inode = dir_ent->inode->inode; |
| 3883 | squashfs_type = dir_ent->inode->type; |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3884 | switch(squashfs_type) { |
| 3885 | case SQUASHFS_FILE_TYPE: |
| 3886 | if(!sorted) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3887 | INFO("file %s, uncompressed " |
| 3888 | "size %lld bytes LINK" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3889 | "\n", |
| 3890 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3891 | (long long) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3892 | buf->st_size); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3893 | break; |
| 3894 | case SQUASHFS_SYMLINK_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3895 | INFO("symbolic link %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3896 | "LINK\n", subpathname(dir_ent), |
| 3897 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3898 | break; |
| 3899 | case SQUASHFS_CHRDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3900 | INFO("character device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3901 | "LINK\n", subpathname(dir_ent), |
| 3902 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3903 | break; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3904 | case SQUASHFS_BLKDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3905 | INFO("block device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3906 | "LINK\n", subpathname(dir_ent), |
| 3907 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3908 | break; |
| 3909 | case SQUASHFS_FIFO_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3910 | INFO("fifo %s inode 0x%llx LINK\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3911 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3912 | break; |
| 3913 | case SQUASHFS_SOCKET_TYPE: |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3914 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3915 | "0x%llx LINK\n", |
| 3916 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3917 | break; |
| 3918 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Phillip Lougher | 0366aec | 2012-10-05 04:06:04 +0100 | [diff] [blame] | 3921 | add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name, |
| 3922 | squashfs_type, &dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3925 | write_dir(inode, dir_info, &dir); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3926 | INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent), |
| 3927 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3928 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3929 | scan6_freedir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | |
| 3933 | unsigned int slog(unsigned int block) |
| 3934 | { |
| 3935 | int i; |
| 3936 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 3937 | for(i = 12; i <= 20; i++) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3938 | if(block == (1 << i)) |
| 3939 | return i; |
| 3940 | return 0; |
| 3941 | } |
| 3942 | |
| 3943 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3944 | int old_excluded(char *filename, struct stat *buf) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3945 | { |
| 3946 | int i; |
| 3947 | |
| 3948 | for(i = 0; i < exclude; i++) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3949 | if((exclude_paths[i].st_dev == buf->st_dev) && |
| 3950 | (exclude_paths[i].st_ino == buf->st_ino)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3951 | return TRUE; |
| 3952 | return FALSE; |
| 3953 | } |
| 3954 | |
| 3955 | |
| 3956 | #define ADD_ENTRY(buf) \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3957 | if(exclude % EXCLUDE_SIZE == 0) { \ |
| 3958 | exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \ |
| 3959 | * sizeof(struct exclude_info)); \ |
| 3960 | if(exclude_paths == NULL) \ |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3961 | MEM_ERROR(); \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3962 | } \ |
| 3963 | exclude_paths[exclude].st_dev = buf.st_dev; \ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3964 | exclude_paths[exclude++].st_ino = buf.st_ino; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3965 | int old_add_exclude(char *path) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3966 | { |
| 3967 | int i; |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3968 | char *filename; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3969 | struct stat buf; |
| 3970 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3971 | if(path[0] == '/' || strncmp(path, "./", 2) == 0 || |
| 3972 | strncmp(path, "../", 3) == 0) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3973 | if(lstat(path, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3974 | ERROR_START("Cannot stat exclude dir/file %s because " |
| 3975 | "%s", path, strerror(errno)); |
| 3976 | ERROR_EXIT(", ignoring\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3977 | return TRUE; |
| 3978 | } |
| 3979 | ADD_ENTRY(buf); |
| 3980 | return TRUE; |
| 3981 | } |
| 3982 | |
| 3983 | for(i = 0; i < source; i++) { |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3984 | int res = asprintf(&filename, "%s/%s", source_path[i], path); |
| 3985 | if(res == -1) |
| 3986 | BAD_ERROR("asprintf failed in old_add_exclude\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3987 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3988 | if(!(errno == ENOENT || errno == ENOTDIR)) { |
| 3989 | ERROR_START("Cannot stat exclude dir/file %s " |
| 3990 | "because %s", filename, strerror(errno)); |
| 3991 | ERROR_EXIT(", ignoring\n"); |
| 3992 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3993 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3994 | continue; |
| 3995 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3996 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3997 | ADD_ENTRY(buf); |
| 3998 | } |
| 3999 | return TRUE; |
| 4000 | } |
| 4001 | |
| 4002 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4003 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 4004 | int type) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4005 | { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4006 | old_root_entry = realloc(old_root_entry, |
| 4007 | sizeof(struct old_root_entry_info) * (old_root_entries + 1)); |
| 4008 | if(old_root_entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4009 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4010 | |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 4011 | old_root_entry[old_root_entries].name = strdup(name); |
| 4012 | old_root_entry[old_root_entries].inode.inode = inode; |
| 4013 | old_root_entry[old_root_entries].inode.inode_number = inode_number; |
| 4014 | old_root_entry[old_root_entries].inode.type = type; |
| 4015 | old_root_entry[old_root_entries++].inode.root_entry = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4019 | void initialise_threads(int readq, int fragq, int bwriteq, int fwriteq, |
| 4020 | int freelst, char *destination_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4021 | { |
| 4022 | int i; |
| 4023 | sigset_t sigmask, old_mask; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4024 | int total_mem = readq; |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4025 | int reader_size; |
| 4026 | int fragment_size; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4027 | int fwriter_size; |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4028 | /* |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4029 | * bwriter_size is global because it is needed in |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4030 | * write_file_blocks_dup() |
| 4031 | */ |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4032 | |
| 4033 | /* |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4034 | * Never allow the total size of the queues to be larger than |
| 4035 | * physical memory |
| 4036 | * |
| 4037 | * When adding together the possibly user supplied values, make |
| 4038 | * sure they've not been deliberately contrived to overflow an int |
| 4039 | */ |
| 4040 | if(add_overflow(total_mem, fragq)) |
| 4041 | BAD_ERROR("Queue sizes rediculously too large\n"); |
| 4042 | total_mem += fragq; |
| 4043 | if(add_overflow(total_mem, bwriteq)) |
| 4044 | BAD_ERROR("Queue sizes rediculously too large\n"); |
| 4045 | total_mem += bwriteq; |
| 4046 | if(add_overflow(total_mem, fwriteq)) |
| 4047 | BAD_ERROR("Queue sizes rediculously too large\n"); |
| 4048 | total_mem += fwriteq; |
| 4049 | |
Phillip Lougher | 78784bb | 2014-04-28 04:08:40 +0100 | [diff] [blame] | 4050 | if(total_mem > get_physical_memory()) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4051 | ERROR("Total queue sizes larger than physical memory.\n"); |
| 4052 | ERROR("Mksquashfs will exhaust physical memory and thrash.\n"); |
| 4053 | BAD_ERROR("Queues too large\n"); |
| 4054 | } |
| 4055 | |
| 4056 | /* |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4057 | * convert from queue size in Mbytes to queue size in |
| 4058 | * blocks. |
| 4059 | * |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4060 | * This isn't going to overflow an int unless there exists |
| 4061 | * systems with more than 8 Petabytes of RAM! |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4062 | */ |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4063 | reader_size = readq << (20 - block_log); |
| 4064 | fragment_size = fragq << (20 - block_log); |
| 4065 | bwriter_size = bwriteq << (20 - block_log); |
| 4066 | fwriter_size = fwriteq << (20 - block_log); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4067 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4068 | /* |
| 4069 | * setup signal handlers for the main thread, these cleanup |
| 4070 | * deleting the destination file, if appending the |
| 4071 | * handlers for SIGTERM and SIGINT will be replaced with handlers |
| 4072 | * allowing the user to press ^C twice to restore the existing |
| 4073 | * filesystem. |
| 4074 | * |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4075 | * SIGUSR1 is an internal signal, which is used by the sub-threads |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4076 | * to tell the main thread to terminate, deleting the destination file, |
| 4077 | * or if necessary restoring the filesystem on appending |
| 4078 | */ |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 4079 | signal(SIGTERM, sighandler); |
| 4080 | signal(SIGINT, sighandler); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4081 | signal(SIGUSR1, sighandler); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4082 | |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 4083 | /* block SIGQUIT and SIGHUP, these are handled by the info thread */ |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4084 | sigemptyset(&sigmask); |
| 4085 | sigaddset(&sigmask, SIGQUIT); |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 4086 | sigaddset(&sigmask, SIGHUP); |
Phillip Lougher | b1259f7 | 2013-06-06 03:10:15 +0100 | [diff] [blame] | 4087 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4088 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4089 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4090 | /* |
| 4091 | * temporarily block these signals, so the created sub-threads |
| 4092 | * will ignore them, ensuring the main thread handles them |
| 4093 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4094 | sigemptyset(&sigmask); |
| 4095 | sigaddset(&sigmask, SIGINT); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4096 | sigaddset(&sigmask, SIGTERM); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4097 | sigaddset(&sigmask, SIGUSR1); |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4098 | if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4099 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4100 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4101 | if(processors == -1) { |
| 4102 | #ifndef linux |
| 4103 | int mib[2]; |
| 4104 | size_t len = sizeof(processors); |
| 4105 | |
| 4106 | mib[0] = CTL_HW; |
| 4107 | #ifdef HW_AVAILCPU |
| 4108 | mib[1] = HW_AVAILCPU; |
| 4109 | #else |
| 4110 | mib[1] = HW_NCPU; |
| 4111 | #endif |
| 4112 | |
| 4113 | if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 4114 | ERROR_START("Failed to get number of available " |
| 4115 | "processors."); |
| 4116 | ERROR_EXIT(" Defaulting to 1\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4117 | processors = 1; |
| 4118 | } |
| 4119 | #else |
plougher | 9cc26b7 | 2010-08-17 01:05:55 +0000 | [diff] [blame] | 4120 | processors = sysconf(_SC_NPROCESSORS_ONLN); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4121 | #endif |
| 4122 | } |
| 4123 | |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4124 | if(multiply_overflow(processors, 3) || |
| 4125 | multiply_overflow(processors * 3, sizeof(pthread_t))) |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4126 | BAD_ERROR("Processors too large\n"); |
| 4127 | |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4128 | deflator_thread = malloc(processors * 3 * sizeof(pthread_t)); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4129 | if(deflator_thread == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4130 | MEM_ERROR(); |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4131 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4132 | frag_deflator_thread = &deflator_thread[processors]; |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4133 | frag_thread = &frag_deflator_thread[processors]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4134 | |
| 4135 | to_reader = queue_init(1); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4136 | to_deflate = queue_init(reader_size); |
| 4137 | to_process_frag = queue_init(reader_size); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4138 | to_writer = queue_init(bwriter_size + fwriter_size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4139 | from_writer = queue_init(1); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4140 | to_frag = queue_init(fragment_size); |
| 4141 | locked_fragment = queue_init(fragment_size); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 4142 | to_main = seq_queue_init(); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4143 | reader_buffer = cache_init(block_size, reader_size, 0, 0); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4144 | bwriter_buffer = cache_init(block_size, bwriter_size, 1, freelst); |
| 4145 | fwriter_buffer = cache_init(block_size, fwriter_size, 1, freelst); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4146 | fragment_buffer = cache_init(block_size, fragment_size, 1, freelst); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 4147 | reserve_cache = cache_init(block_size, processors + 1, 1, freelst); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4148 | pthread_create(&reader_thread, NULL, reader, NULL); |
| 4149 | pthread_create(&writer_thread, NULL, writer, NULL); |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 4150 | init_progress_bar(); |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4151 | init_info(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4152 | |
| 4153 | for(i = 0; i < processors; i++) { |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 4154 | if(pthread_create(&deflator_thread[i], NULL, deflator, NULL)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4155 | BAD_ERROR("Failed to create thread\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4156 | if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator, |
| 4157 | NULL) != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4158 | BAD_ERROR("Failed to create thread\n"); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 4159 | if(pthread_create(&frag_thread[i], NULL, frag_thrd, |
| 4160 | (void *) destination_file) != 0) |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4161 | BAD_ERROR("Failed to create thread\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4164 | main_thread = pthread_self(); |
| 4165 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4166 | printf("Parallel mksquashfs: Using %d processor%s\n", processors, |
| 4167 | processors == 1 ? "" : "s"); |
| 4168 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4169 | /* Restore the signal mask for the main thread */ |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4170 | if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4171 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4172 | } |
| 4173 | |
| 4174 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4175 | long long write_inode_lookup_table() |
| 4176 | { |
| 4177 | int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4178 | void *it; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4179 | |
| 4180 | if(inode_count == sinode_count) |
| 4181 | goto skip_inode_hash_table; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4182 | |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4183 | it = realloc(inode_lookup_table, lookup_bytes); |
| 4184 | if(it == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4185 | MEM_ERROR(); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4186 | inode_lookup_table = it; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4187 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4188 | for(i = 0; i < INODE_HASH_SIZE; i ++) { |
Phillip Lougher | f2f83b6 | 2014-04-18 01:30:47 +0100 | [diff] [blame] | 4189 | struct inode_info *inode; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4190 | |
| 4191 | for(inode = inode_info[i]; inode; inode = inode->next) { |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4192 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4193 | inode_number = get_inode_no(inode); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4194 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4195 | SQUASHFS_SWAP_LONG_LONGS(&inode->inode, |
| 4196 | &inode_lookup_table[inode_number - 1], 1); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4197 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4198 | } |
| 4199 | } |
| 4200 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4201 | skip_inode_hash_table: |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 4202 | return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL, |
| 4203 | noI); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4206 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4207 | char *get_component(char *target, char **targname) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4208 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4209 | char *start; |
| 4210 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4211 | while(*target == '/') |
rlougher | c4ebcf5 | 2007-11-08 17:52:49 +0000 | [diff] [blame] | 4212 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4213 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4214 | start = target; |
Phillip Lougher | 66d8b8e | 2014-04-20 21:01:33 +0100 | [diff] [blame] | 4215 | while(*target != '/' && *target != '\0') |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4216 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4217 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4218 | *targname = strndup(start, target - start); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4219 | |
Phillip Lougher | 66d8b8e | 2014-04-20 21:01:33 +0100 | [diff] [blame] | 4220 | while(*target == '/') |
| 4221 | target ++; |
| 4222 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4223 | return target; |
| 4224 | } |
| 4225 | |
| 4226 | |
| 4227 | void free_path(struct pathname *paths) |
| 4228 | { |
| 4229 | int i; |
| 4230 | |
| 4231 | for(i = 0; i < paths->names; i++) { |
| 4232 | if(paths->name[i].paths) |
| 4233 | free_path(paths->name[i].paths); |
| 4234 | free(paths->name[i].name); |
| 4235 | if(paths->name[i].preg) { |
| 4236 | regfree(paths->name[i].preg); |
| 4237 | free(paths->name[i].preg); |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | free(paths); |
| 4242 | } |
| 4243 | |
| 4244 | |
| 4245 | struct pathname *add_path(struct pathname *paths, char *target, char *alltarget) |
| 4246 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4247 | char *targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4248 | int i, error; |
| 4249 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4250 | target = get_component(target, &targname); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4251 | |
| 4252 | if(paths == NULL) { |
plougher | dec6ef1 | 2010-12-18 02:47:53 +0000 | [diff] [blame] | 4253 | paths = malloc(sizeof(struct pathname)); |
| 4254 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4255 | MEM_ERROR(); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4256 | |
| 4257 | paths->names = 0; |
| 4258 | paths->name = NULL; |
| 4259 | } |
| 4260 | |
| 4261 | for(i = 0; i < paths->names; i++) |
| 4262 | if(strcmp(paths->name[i].name, targname) == 0) |
| 4263 | break; |
| 4264 | |
| 4265 | if(i == paths->names) { |
| 4266 | /* allocate new name entry */ |
| 4267 | paths->names ++; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4268 | paths->name = realloc(paths->name, (i + 1) * |
| 4269 | sizeof(struct path_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4270 | if(paths->name == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4271 | MEM_ERROR(); |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4272 | paths->name[i].name = targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4273 | paths->name[i].paths = NULL; |
| 4274 | if(use_regex) { |
| 4275 | paths->name[i].preg = malloc(sizeof(regex_t)); |
plougher | 5c60eab | 2010-07-21 01:12:19 +0000 | [diff] [blame] | 4276 | if(paths->name[i].preg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4277 | MEM_ERROR(); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4278 | error = regcomp(paths->name[i].preg, targname, |
| 4279 | REG_EXTENDED|REG_NOSUB); |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4280 | if(error) { |
Phillip Lougher | 62859d2 | 2012-11-30 05:53:56 +0000 | [diff] [blame] | 4281 | char str[1024]; /* overflow safe */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4282 | |
| 4283 | regerror(error, paths->name[i].preg, str, 1024); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4284 | BAD_ERROR("invalid regex %s in export %s, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4285 | "because %s\n", targname, alltarget, |
| 4286 | str); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4287 | } |
| 4288 | } else |
| 4289 | paths->name[i].preg = NULL; |
| 4290 | |
| 4291 | if(target[0] == '\0') |
| 4292 | /* at leaf pathname component */ |
| 4293 | paths->name[i].paths = NULL; |
| 4294 | else |
| 4295 | /* recurse adding child components */ |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4296 | paths->name[i].paths = add_path(NULL, target, |
| 4297 | alltarget); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4298 | } else { |
| 4299 | /* existing matching entry */ |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4300 | free(targname); |
| 4301 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4302 | if(paths->name[i].paths == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4303 | /* No sub-directory which means this is the leaf |
| 4304 | * component of a pre-existing exclude which subsumes |
| 4305 | * the exclude currently being added, in which case stop |
| 4306 | * adding components */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4307 | } else if(target[0] == '\0') { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4308 | /* at leaf pathname component and child components exist |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4309 | * from more specific excludes, delete as they're |
| 4310 | * subsumed by this exclude */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4311 | free_path(paths->name[i].paths); |
| 4312 | paths->name[i].paths = NULL; |
| 4313 | } else |
| 4314 | /* recurse adding child components */ |
| 4315 | add_path(paths->name[i].paths, target, alltarget); |
| 4316 | } |
| 4317 | |
| 4318 | return paths; |
| 4319 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4320 | |
| 4321 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4322 | void add_exclude(char *target) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4323 | { |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4324 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4325 | if(target[0] == '/' || strncmp(target, "./", 2) == 0 || |
| 4326 | strncmp(target, "../", 3) == 0) |
| 4327 | BAD_ERROR("/, ./ and ../ prefixed excludes not supported with " |
| 4328 | "-wildcards or -regex options\n"); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4329 | else if(strncmp(target, "... ", 4) == 0) |
| 4330 | stickypath = add_path(stickypath, target + 4, target + 4); |
| 4331 | else |
| 4332 | path = add_path(path, target, target); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | |
| 4336 | void display_path(int depth, struct pathname *paths) |
| 4337 | { |
| 4338 | int i, n; |
| 4339 | |
| 4340 | if(paths == NULL) |
| 4341 | return; |
| 4342 | |
| 4343 | for(i = 0; i < paths->names; i++) { |
| 4344 | for(n = 0; n < depth; n++) |
| 4345 | printf("\t"); |
| 4346 | printf("%d: %s\n", depth, paths->name[i].name); |
| 4347 | display_path(depth + 1, paths->name[i].paths); |
| 4348 | } |
| 4349 | } |
| 4350 | |
| 4351 | |
| 4352 | void display_path2(struct pathname *paths, char *string) |
| 4353 | { |
| 4354 | int i; |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4355 | char *path; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4356 | |
| 4357 | if(paths == NULL) { |
| 4358 | printf("%s\n", string); |
| 4359 | return; |
| 4360 | } |
| 4361 | |
| 4362 | for(i = 0; i < paths->names; i++) { |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4363 | int res = asprintf(&path, "%s/%s", string, paths->name[i].name); |
| 4364 | if(res == -1) |
| 4365 | BAD_ERROR("asprintf failed in display_path2\n"); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4366 | display_path2(paths->name[i].paths, path); |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4367 | free(path); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4368 | } |
| 4369 | } |
| 4370 | |
| 4371 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4372 | struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path) |
| 4373 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4374 | int count = paths == NULL ? 0 : paths->count; |
| 4375 | |
| 4376 | if(count % PATHS_ALLOC_SIZE == 0) { |
| 4377 | paths = realloc(paths, sizeof(struct pathnames) + |
| 4378 | (count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4379 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4380 | MEM_ERROR(); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4381 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4382 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4383 | paths->path[count] = path; |
| 4384 | paths->count = count + 1; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4385 | return paths; |
| 4386 | } |
| 4387 | |
| 4388 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4389 | int excluded_match(char *name, struct pathname *path, struct pathnames **new) |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4390 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4391 | int i; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4392 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4393 | for(i = 0; i < path->names; i++) { |
| 4394 | int match = use_regex ? |
| 4395 | regexec(path->name[i].preg, name, (size_t) 0, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4396 | NULL, 0) == 0 : |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4397 | fnmatch(path->name[i].name, name, |
| 4398 | FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4399 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4400 | if(match) { |
| 4401 | if(path->name[i].paths == NULL || new == NULL) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4402 | /* match on a leaf component, any subdirectories |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4403 | * in the filesystem should be excluded */ |
| 4404 | return TRUE; |
| 4405 | else |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4406 | /* match on a non-leaf component, add any |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4407 | * subdirectories to the new set of |
| 4408 | * subdirectories to scan for this name */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4409 | *new = add_subdir(*new, path->name[i].paths); |
| 4410 | } |
| 4411 | } |
| 4412 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4413 | return FALSE; |
| 4414 | } |
| 4415 | |
| 4416 | |
| 4417 | int excluded(char *name, struct pathnames *paths, struct pathnames **new) |
| 4418 | { |
| 4419 | int n; |
| 4420 | |
| 4421 | if(stickypath && excluded_match(name, stickypath, NULL)) |
| 4422 | return TRUE; |
| 4423 | |
| 4424 | for(n = 0; paths && n < paths->count; n++) { |
| 4425 | int res = excluded_match(name, paths->path[n], new); |
| 4426 | if(res) { |
| 4427 | free(*new); |
| 4428 | *new = NULL; |
| 4429 | return TRUE; |
| 4430 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4431 | } |
| 4432 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4433 | /* |
| 4434 | * Either: |
| 4435 | * - no matching names found, return empty new search set, or |
| 4436 | * - one or more matches with sub-directories found (no leaf matches), |
| 4437 | * in which case return new search set. |
| 4438 | * |
| 4439 | * In either case return FALSE as we don't want to exclude this entry |
| 4440 | */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4441 | return FALSE; |
| 4442 | } |
| 4443 | |
| 4444 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4445 | void process_exclude_file(char *argv) |
| 4446 | { |
| 4447 | FILE *fd; |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4448 | char buffer[MAX_LINE + 1]; /* overflow safe */ |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4449 | char *filename; |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4450 | |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4451 | fd = fopen(argv, "r"); |
| 4452 | if(fd == NULL) |
| 4453 | BAD_ERROR("Failed to open exclude file \"%s\" because %s\n", |
| 4454 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4455 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4456 | while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) { |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4457 | int len = strlen(filename); |
| 4458 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4459 | if(len == MAX_LINE && filename[len - 1] != '\n') |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4460 | /* line too large */ |
| 4461 | BAD_ERROR("Line too long when reading " |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4462 | "exclude file \"%s\", larger than %d " |
Phillip Lougher | 83847c1 | 2012-12-23 07:22:36 +0000 | [diff] [blame] | 4463 | "bytes\n", argv, MAX_LINE); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4464 | |
| 4465 | /* |
| 4466 | * Remove '\n' terminator if it exists (the last line |
| 4467 | * in the file may not be '\n' terminated) |
| 4468 | */ |
| 4469 | if(len && filename[len - 1] == '\n') |
| 4470 | filename[len - 1] = '\0'; |
| 4471 | |
| 4472 | /* Skip any leading whitespace */ |
| 4473 | while(isspace(*filename)) |
| 4474 | filename ++; |
| 4475 | |
| 4476 | /* if comment line, skip */ |
| 4477 | if(*filename == '#') |
| 4478 | continue; |
| 4479 | |
| 4480 | /* |
| 4481 | * check for initial backslash, to accommodate |
| 4482 | * filenames with leading space or leading # character |
| 4483 | */ |
| 4484 | if(*filename == '\\') |
| 4485 | filename ++; |
| 4486 | |
| 4487 | /* if line is now empty after skipping characters, skip it */ |
| 4488 | if(*filename == '\0') |
| 4489 | continue; |
| 4490 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4491 | if(old_exclude) |
| 4492 | old_add_exclude(filename); |
| 4493 | else |
| 4494 | add_exclude(filename); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | if(ferror(fd)) |
| 4498 | BAD_ERROR("Reading exclude file \"%s\" failed because %s\n", |
| 4499 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4500 | |
| 4501 | fclose(fd); |
| 4502 | } |
| 4503 | |
| 4504 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4505 | #define RECOVER_ID "Squashfs recovery file v1.0\n" |
| 4506 | #define RECOVER_ID_SIZE 28 |
| 4507 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4508 | void write_recovery_data(struct squashfs_super_block *sBlk) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4509 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4510 | int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4511 | pid_t pid = getpid(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4512 | char *metadata; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4513 | char header[] = RECOVER_ID; |
| 4514 | |
| 4515 | if(recover == FALSE) { |
| 4516 | printf("No recovery data option specified.\n"); |
plougher | eac1853 | 2007-10-29 05:26:06 +0000 | [diff] [blame] | 4517 | printf("Skipping saving recovery file.\n\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4518 | return; |
| 4519 | } |
| 4520 | |
plougher | 1b879bc | 2010-12-18 02:49:42 +0000 | [diff] [blame] | 4521 | metadata = malloc(bytes); |
| 4522 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4523 | MEM_ERROR(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4524 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4525 | res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata); |
Phillip Lougher | 477f433 | 2013-03-06 03:55:08 +0000 | [diff] [blame] | 4526 | if(res == 0) { |
| 4527 | ERROR("Failed to read append filesystem metadata\n"); |
| 4528 | BAD_ERROR("Filesystem corrupted?\n"); |
| 4529 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4530 | |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4531 | res = asprintf(&recovery_file, "squashfs_recovery_%s_%d", |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4532 | getbase(destination_file), pid); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4533 | if(res == -1) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4534 | MEM_ERROR(); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4535 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4536 | recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU); |
| 4537 | if(recoverfd == -1) |
| 4538 | BAD_ERROR("Failed to create recovery file, because %s. " |
| 4539 | "Aborting\n", strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4540 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4541 | if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4542 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4543 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4544 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4545 | if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4546 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4547 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4548 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4549 | if(write_bytes(recoverfd, metadata, bytes) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4550 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4551 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4552 | |
| 4553 | close(recoverfd); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4554 | free(metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4555 | |
| 4556 | printf("Recovery file \"%s\" written\n", recovery_file); |
| 4557 | printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4558 | printf("mksquashfs dummy %s -recover %s\n", destination_file, |
| 4559 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4560 | printf("to restore filesystem\n\n"); |
| 4561 | } |
| 4562 | |
| 4563 | |
| 4564 | void read_recovery_data(char *recovery_file, char *destination_file) |
| 4565 | { |
| 4566 | int fd, recoverfd, bytes; |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4567 | struct squashfs_super_block orig_sBlk, sBlk; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4568 | char *metadata; |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4569 | int res; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4570 | struct stat buf; |
| 4571 | char header[] = RECOVER_ID; |
| 4572 | char header2[RECOVER_ID_SIZE]; |
| 4573 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4574 | recoverfd = open(recovery_file, O_RDONLY); |
| 4575 | if(recoverfd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4576 | BAD_ERROR("Failed to open recovery file because %s\n", |
| 4577 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4578 | |
| 4579 | if(stat(destination_file, &buf) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4580 | BAD_ERROR("Failed to stat destination file, because %s\n", |
| 4581 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4582 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4583 | fd = open(destination_file, O_RDWR); |
| 4584 | if(fd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4585 | BAD_ERROR("Failed to open destination file because %s\n", |
| 4586 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4587 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4588 | res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE); |
| 4589 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4590 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4591 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4592 | if(res < RECOVER_ID_SIZE) |
| 4593 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4594 | if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 ) |
| 4595 | BAD_ERROR("Not a recovery file\n"); |
| 4596 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4597 | res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4598 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4599 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4600 | strerror(errno)); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4601 | if(res < sizeof(struct squashfs_super_block)) |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4602 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4603 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4604 | res = read_fs_bytes(fd, 0, sizeof(struct squashfs_super_block), &orig_sBlk); |
Phillip Lougher | 477f433 | 2013-03-06 03:55:08 +0000 | [diff] [blame] | 4605 | if(res == 0) { |
| 4606 | ERROR("Failed to read superblock from output filesystem\n"); |
| 4607 | BAD_ERROR("Output filesystem is empty!\n"); |
| 4608 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4609 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4610 | if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4, |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4611 | sizeof(struct squashfs_super_block) - 4) != 0) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4612 | BAD_ERROR("Recovery file and destination file do not seem to " |
| 4613 | "match\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4614 | |
| 4615 | bytes = sBlk.bytes_used - sBlk.inode_table_start; |
| 4616 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4617 | metadata = malloc(bytes); |
| 4618 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4619 | MEM_ERROR(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4620 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4621 | res = read_bytes(recoverfd, metadata, bytes); |
| 4622 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4623 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4624 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4625 | if(res < bytes) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4626 | BAD_ERROR("Recovery file appears to be truncated\n"); |
| 4627 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4628 | write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4629 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 4630 | write_destination(fd, sBlk.inode_table_start, bytes, metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4631 | |
| 4632 | close(recoverfd); |
| 4633 | close(fd); |
| 4634 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4635 | printf("Successfully wrote recovery file \"%s\". Exiting\n", |
| 4636 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4637 | |
| 4638 | exit(0); |
| 4639 | } |
| 4640 | |
| 4641 | |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4642 | int parse_number(char *start, int *res, int size) |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4643 | { |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4644 | char *end; |
| 4645 | long number = strtol(start, &end, 10); |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4646 | |
Phillip Lougher | f4f4838 | 2012-11-29 03:52:44 +0000 | [diff] [blame] | 4647 | /* |
| 4648 | * check for strtol underflow or overflow in conversion. |
| 4649 | * Note: strtol can validly return LONG_MIN and LONG_MAX |
| 4650 | * if the user entered these values, but, additional code |
| 4651 | * to distinguish this scenario is unnecessary, because for |
| 4652 | * our purposes LONG_MIN and LONG_MAX are too large anyway |
| 4653 | */ |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4654 | if(number == LONG_MIN || number == LONG_MAX) |
| 4655 | return 0; |
| 4656 | |
| 4657 | /* reject negative numbers as invalid */ |
| 4658 | if(number < 0) |
| 4659 | return 0; |
| 4660 | |
| 4661 | /* check if long result will overflow signed int */ |
| 4662 | if(number > INT_MAX) |
| 4663 | return 0; |
| 4664 | |
| 4665 | if(size) { |
Phillip Lougher | ed34569 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 4666 | /* |
| 4667 | * Check for multiplier and trailing junk. |
| 4668 | * But first check that a number exists before the |
| 4669 | * multiplier |
| 4670 | */ |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4671 | if(end == start) |
Phillip Lougher | ed34569 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 4672 | return 0; |
| 4673 | |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4674 | switch(end[0]) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4675 | case 'm': |
| 4676 | case 'M': |
| 4677 | if(multiply_overflow((int) number, 1048576)) |
| 4678 | return 0; |
| 4679 | number *= 1048576; |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4680 | |
Phillip Lougher | f968be8 | 2012-12-04 05:12:32 +0000 | [diff] [blame] | 4681 | if(end[1] != '\0') |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4682 | /* trailing junk after number */ |
| 4683 | return 0; |
| 4684 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4685 | break; |
| 4686 | case 'k': |
| 4687 | case 'K': |
| 4688 | if(multiply_overflow((int) number, 1024)) |
| 4689 | return 0; |
| 4690 | number *= 1024; |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4691 | |
Phillip Lougher | f968be8 | 2012-12-04 05:12:32 +0000 | [diff] [blame] | 4692 | if(end[1] != '\0') |
Phillip Lougher | a9e6563 | 2012-11-28 04:17:27 +0000 | [diff] [blame] | 4693 | /* trailing junk after number */ |
| 4694 | return 0; |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4695 | case '\0': |
| 4696 | break; |
| 4697 | default: |
| 4698 | /* trailing junk after number */ |
| 4699 | return 0; |
| 4700 | } |
Phillip Lougher | da96f5b | 2012-11-28 04:32:13 +0000 | [diff] [blame] | 4701 | } else if(end[0] != '\0') |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4702 | /* trailing junk after number */ |
| 4703 | return 0; |
| 4704 | |
| 4705 | *res = number; |
| 4706 | return 1; |
| 4707 | } |
| 4708 | |
| 4709 | |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4710 | void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad) |
| 4711 | { |
| 4712 | int i; |
| 4713 | |
| 4714 | sBlk->fragments = fragments; |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4715 | sBlk->no_ids = id_count; |
| 4716 | sBlk->inode_table_start = write_inodes(); |
| 4717 | sBlk->directory_table_start = write_directories(); |
| 4718 | sBlk->fragment_table_start = write_fragment_table(); |
| 4719 | sBlk->lookup_table_start = exportable ? write_inode_lookup_table() : |
| 4720 | SQUASHFS_INVALID_BLK; |
| 4721 | sBlk->id_table_start = write_id_table(); |
| 4722 | sBlk->xattr_id_table_start = write_xattrs(); |
| 4723 | |
| 4724 | TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start); |
| 4725 | TRACE("sBlk->directory_table_start 0x%llx\n", |
| 4726 | sBlk->directory_table_start); |
| 4727 | TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start); |
| 4728 | if(exportable) |
| 4729 | TRACE("sBlk->lookup_table_start 0x%llx\n", |
| 4730 | sBlk->lookup_table_start); |
| 4731 | |
| 4732 | sBlk->bytes_used = bytes; |
| 4733 | |
| 4734 | sBlk->compression = comp->id; |
| 4735 | |
| 4736 | SQUASHFS_INSWAP_SUPER_BLOCK(sBlk); |
| 4737 | write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk); |
| 4738 | |
| 4739 | if(!nopad && (i = bytes & (4096 - 1))) { |
| 4740 | char temp[4096] = {0}; |
| 4741 | write_destination(fd, bytes, 4096 - i, temp); |
| 4742 | } |
| 4743 | |
| 4744 | close(fd); |
| 4745 | |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4746 | if(recovery_file) |
| 4747 | unlink(recovery_file); |
| 4748 | |
| 4749 | total_bytes += total_inode_bytes + total_directory_bytes + |
| 4750 | sizeof(struct squashfs_super_block) + total_xattr_bytes; |
| 4751 | |
| 4752 | printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size" |
| 4753 | " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR, |
| 4754 | SQUASHFS_MINOR, comp->name, block_size); |
| 4755 | printf("\t%s data, %s metadata, %s fragments, %s xattrs\n", |
| 4756 | noD ? "uncompressed" : "compressed", noI ? "uncompressed" : |
| 4757 | "compressed", no_fragments ? "no" : noF ? "uncompressed" : |
| 4758 | "compressed", no_xattrs ? "no" : noX ? "uncompressed" : |
| 4759 | "compressed"); |
| 4760 | printf("\tduplicates are %sremoved\n", duplicate_checking ? "" : |
| 4761 | "not "); |
| 4762 | printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, |
| 4763 | bytes / (1024.0 * 1024.0)); |
| 4764 | printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n", |
| 4765 | ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0); |
| 4766 | printf("Inode table size %d bytes (%.2f Kbytes)\n", |
| 4767 | inode_bytes, inode_bytes / 1024.0); |
| 4768 | printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n", |
| 4769 | ((float) inode_bytes / total_inode_bytes) * 100.0, |
| 4770 | total_inode_bytes); |
| 4771 | printf("Directory table size %d bytes (%.2f Kbytes)\n", |
| 4772 | directory_bytes, directory_bytes / 1024.0); |
| 4773 | printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n", |
| 4774 | ((float) directory_bytes / total_directory_bytes) * 100.0, |
| 4775 | total_directory_bytes); |
| 4776 | if(total_xattr_bytes) { |
| 4777 | printf("Xattr table size %d bytes (%.2f Kbytes)\n", |
| 4778 | xattr_bytes, xattr_bytes / 1024.0); |
| 4779 | printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n", |
| 4780 | ((float) xattr_bytes / total_xattr_bytes) * 100.0, |
| 4781 | total_xattr_bytes); |
| 4782 | } |
| 4783 | if(duplicate_checking) |
| 4784 | printf("Number of duplicate files found %d\n", file_count - |
| 4785 | dup_files); |
| 4786 | else |
| 4787 | printf("No duplicate files removed\n"); |
| 4788 | printf("Number of inodes %d\n", inode_count); |
| 4789 | printf("Number of files %d\n", file_count); |
| 4790 | if(!no_fragments) |
| 4791 | printf("Number of fragments %d\n", fragments); |
| 4792 | printf("Number of symbolic links %d\n", sym_count); |
| 4793 | printf("Number of device nodes %d\n", dev_count); |
| 4794 | printf("Number of fifo nodes %d\n", fifo_count); |
| 4795 | printf("Number of socket nodes %d\n", sock_count); |
| 4796 | printf("Number of directories %d\n", dir_count); |
| 4797 | printf("Number of ids (unique uids + gids) %d\n", id_count); |
| 4798 | printf("Number of uids %d\n", uid_count); |
| 4799 | |
| 4800 | for(i = 0; i < id_count; i++) { |
| 4801 | if(id_table[i]->flags & ISA_UID) { |
| 4802 | struct passwd *user = getpwuid(id_table[i]->id); |
| 4803 | printf("\t%s (%d)\n", user == NULL ? "unknown" : |
| 4804 | user->pw_name, id_table[i]->id); |
| 4805 | } |
| 4806 | } |
| 4807 | |
| 4808 | printf("Number of gids %d\n", guid_count); |
| 4809 | |
| 4810 | for(i = 0; i < id_count; i++) { |
| 4811 | if(id_table[i]->flags & ISA_GID) { |
| 4812 | struct group *group = getgrgid(id_table[i]->id); |
| 4813 | printf("\t%s (%d)\n", group == NULL ? "unknown" : |
| 4814 | group->gr_name, id_table[i]->id); |
| 4815 | } |
| 4816 | } |
| 4817 | } |
| 4818 | |
| 4819 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4820 | int parse_num(char *arg, int *res) |
| 4821 | { |
| 4822 | return parse_number(arg, res, 0); |
| 4823 | } |
| 4824 | |
| 4825 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4826 | int get_physical_memory() |
| 4827 | { |
| 4828 | /* Long longs are used here because with PAE, a 32-bit |
| 4829 | machine can have more than 4GB of physical memory */ |
| 4830 | |
| 4831 | long long num_pages = sysconf(_SC_PHYS_PAGES); |
| 4832 | long long page_size = sysconf(_SC_PAGESIZE); |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 4833 | int phys_mem = num_pages * page_size >> 20; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4834 | |
| 4835 | if(phys_mem < SQUASHFS_LOWMEM) |
| 4836 | BAD_ERROR("Mksquashfs requires more physical memory than is " |
| 4837 | "available!\n"); |
| 4838 | |
Phillip Lougher | 78784bb | 2014-04-28 04:08:40 +0100 | [diff] [blame] | 4839 | return phys_mem; |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 4840 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4841 | |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 4842 | |
| 4843 | void calculate_queue_sizes(int mem, int *readq, int *fragq, int *bwriteq, |
| 4844 | int *fwriteq) |
| 4845 | { |
| 4846 | *readq = mem / SQUASHFS_READQ_MEM; |
| 4847 | *bwriteq = mem / SQUASHFS_BWRITEQ_MEM; |
| 4848 | *fwriteq = mem / SQUASHFS_FWRITEQ_MEM; |
| 4849 | *fragq = mem - *readq - *bwriteq - *fwriteq; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4853 | #define VERSION() \ |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 4854 | printf("mksquashfs version 4.2-git (2014/04/27)\n");\ |
Phillip Lougher | cc06495 | 2014-01-03 04:26:34 +0000 | [diff] [blame] | 4855 | printf("copyright (C) 2014 Phillip Lougher "\ |
Phillip Lougher | 83d42a3 | 2012-10-31 23:42:22 +0000 | [diff] [blame] | 4856 | "<phillip@squashfs.org.uk>\n\n"); \ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4857 | printf("This program is free software; you can redistribute it and/or"\ |
| 4858 | "\n");\ |
| 4859 | printf("modify it under the terms of the GNU General Public License"\ |
| 4860 | "\n");\ |
| 4861 | printf("as published by the Free Software Foundation; either version "\ |
| 4862 | "2,\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4863 | printf("or (at your option) any later version.\n\n");\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4864 | printf("This program is distributed in the hope that it will be "\ |
| 4865 | "useful,\n");\ |
| 4866 | printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\ |
| 4867 | "of\n");\ |
| 4868 | printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\ |
| 4869 | "\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4870 | printf("GNU General Public License for more details.\n"); |
| 4871 | int main(int argc, char *argv[]) |
| 4872 | { |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 4873 | struct stat buf, source_buf; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 4874 | int res, i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4875 | char *b, *root_name = NULL; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 4876 | int keep_as_directory = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4877 | squashfs_inode inode; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4878 | int readq; |
| 4879 | int fragq; |
| 4880 | int bwriteq; |
| 4881 | int fwriteq; |
Phillip Lougher | 78784bb | 2014-04-28 04:08:40 +0100 | [diff] [blame] | 4882 | int total_mem = get_physical_memory() / SQUASHFS_TAKE; |
Phillip Lougher | 569a963 | 2013-04-22 03:30:30 +0100 | [diff] [blame] | 4883 | int progress = TRUE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 4884 | int force_progress = FALSE; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 4885 | struct file_buffer **fragment = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4886 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4887 | if(argc > 1 && strcmp(argv[1], "-version") == 0) { |
| 4888 | VERSION(); |
| 4889 | exit(0); |
| 4890 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4891 | |
| 4892 | block_log = slog(block_size); |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 4893 | calculate_queue_sizes(total_mem, &readq, &fragq, &bwriteq, &fwriteq); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4894 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4895 | for(i = 1; i < argc && argv[i][0] != '-'; i++); |
| 4896 | if(i < 3) |
| 4897 | goto printOptions; |
| 4898 | source_path = argv + 1; |
| 4899 | source = i - 2; |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4900 | |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 4901 | /* |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4902 | * Scan the command line for -comp xxx option, this is to ensure |
| 4903 | * any -X compressor specific options are passed to the |
| 4904 | * correct compressor |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 4905 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4906 | for(; i < argc; i++) { |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4907 | struct compressor *prev_comp = comp; |
| 4908 | |
| 4909 | if(strcmp(argv[i], "-comp") == 0) { |
| 4910 | if(++i == argc) { |
| 4911 | ERROR("%s: -comp missing compression type\n", |
| 4912 | argv[0]); |
| 4913 | exit(1); |
| 4914 | } |
| 4915 | comp = lookup_compressor(argv[i]); |
| 4916 | if(!comp->supported) { |
| 4917 | ERROR("%s: Compressor \"%s\" is not supported!" |
| 4918 | "\n", argv[0], argv[i]); |
| 4919 | ERROR("%s: Compressors available:\n", argv[0]); |
| 4920 | display_compressors("", COMP_DEFAULT); |
| 4921 | exit(1); |
| 4922 | } |
| 4923 | if(prev_comp != NULL && prev_comp != comp) { |
| 4924 | ERROR("%s: -comp multiple conflicting -comp" |
| 4925 | " options specified on command line" |
| 4926 | ", previously %s, now %s\n", argv[0], |
| 4927 | prev_comp->name, comp->name); |
| 4928 | exit(1); |
| 4929 | } |
| 4930 | compressor_opt_parsed = 1; |
| 4931 | |
| 4932 | } else if(strcmp(argv[i], "-e") == 0) |
| 4933 | break; |
| 4934 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
| 4935 | strcmp(argv[i], "-ef") == 0 || |
| 4936 | strcmp(argv[i], "-pf") == 0 || |
| 4937 | strcmp(argv[i], "-af") == 0 || |
| 4938 | strcmp(argv[i], "-comp") == 0) |
| 4939 | i++; |
| 4940 | } |
| 4941 | |
| 4942 | /* |
| 4943 | * if no -comp option specified lookup default compressor. Note the |
| 4944 | * Makefile ensures the default compressor has been built, and so we |
| 4945 | * don't need to to check for failure here |
| 4946 | */ |
| 4947 | if(comp == NULL) |
| 4948 | comp = lookup_compressor(COMP_DEFAULT); |
| 4949 | |
| 4950 | for(i = source + 2; i < argc; i++) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 4951 | if(strcmp(argv[i], "-action") == 0 || |
| 4952 | strcmp(argv[i], "-a") ==0) { |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 4953 | if(++i == argc) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 4954 | ERROR("%s: %s missing action\n", |
| 4955 | argv[0], argv[i - 1]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 4956 | exit(1); |
| 4957 | } |
| 4958 | res = parse_action(argv[i]); |
| 4959 | if(res == 0) |
| 4960 | exit(1); |
| 4961 | |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 4962 | } else if(strcmp(argv[i], "-af") == 0) { |
| 4963 | if(++i == argc) { |
| 4964 | ERROR("%s: -af missing filename\n", argv[0]); |
| 4965 | exit(1); |
| 4966 | } |
| 4967 | if(read_action_file(argv[i]) == FALSE) |
| 4968 | exit(1); |
| 4969 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4970 | } else if(strcmp(argv[i], "-comp") == 0) |
| 4971 | /* parsed previously */ |
| 4972 | i++; |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 4973 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4974 | else if(strncmp(argv[i], "-X", 2) == 0) { |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 4975 | int args; |
| 4976 | |
| 4977 | if(strcmp(argv[i] + 2, "help") == 0) |
| 4978 | goto print_compressor_options; |
| 4979 | |
| 4980 | args = compressor_options(comp, argv + i, argc - i); |
plougher | d886567 | 2010-11-27 05:05:49 +0000 | [diff] [blame] | 4981 | if(args < 0) { |
| 4982 | if(args == -1) { |
| 4983 | ERROR("%s: Unrecognised compressor" |
| 4984 | " option %s\n", argv[0], |
| 4985 | argv[i]); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4986 | if(!compressor_opt_parsed) |
| 4987 | ERROR("%s: Did you forget to" |
| 4988 | " specify -comp?\n", |
| 4989 | argv[0]); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 4990 | print_compressor_options: |
Phillip Lougher | b1c3b6a | 2014-01-06 05:30:00 +0000 | [diff] [blame] | 4991 | ERROR("%s: selected compressor \"%s\"" |
| 4992 | ". Options supported: %s\n", |
| 4993 | argv[0], comp->name, |
| 4994 | comp->usage ? "" : "none"); |
| 4995 | if(comp->usage) |
| 4996 | comp->usage(); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 4997 | } |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 4998 | exit(1); |
| 4999 | } |
| 5000 | i += args; |
| 5001 | |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 5002 | } else if(strcmp(argv[i], "-pf") == 0) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5003 | if(++i == argc) { |
| 5004 | ERROR("%s: -pf missing filename\n", argv[0]); |
| 5005 | exit(1); |
| 5006 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 5007 | if(read_pseudo_file(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5008 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5009 | } else if(strcmp(argv[i], "-p") == 0) { |
| 5010 | if(++i == argc) { |
| 5011 | ERROR("%s: -p missing pseudo file definition\n", |
| 5012 | argv[0]); |
| 5013 | exit(1); |
| 5014 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 5015 | if(read_pseudo_def(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5016 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5017 | } else if(strcmp(argv[i], "-recover") == 0) { |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5018 | if(++i == argc) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5019 | ERROR("%s: -recover missing recovery file\n", |
| 5020 | argv[0]); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5021 | exit(1); |
| 5022 | } |
| 5023 | read_recovery_data(argv[i], argv[source + 1]); |
| 5024 | } else if(strcmp(argv[i], "-no-recovery") == 0) |
| 5025 | recover = FALSE; |
| 5026 | else if(strcmp(argv[i], "-wildcards") == 0) { |
plougher | 934a9ed | 2007-10-19 00:21:10 +0000 | [diff] [blame] | 5027 | old_exclude = FALSE; |
| 5028 | use_regex = FALSE; |
| 5029 | } else if(strcmp(argv[i], "-regex") == 0) { |
| 5030 | old_exclude = FALSE; |
| 5031 | use_regex = TRUE; |
| 5032 | } else if(strcmp(argv[i], "-no-sparse") == 0) |
plougher | 1f54edc | 2007-08-12 23:13:36 +0000 | [diff] [blame] | 5033 | sparse_files = FALSE; |
| 5034 | else if(strcmp(argv[i], "-no-progress") == 0) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 5035 | progress = FALSE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5036 | else if(strcmp(argv[i], "-progress") == 0) |
| 5037 | force_progress = TRUE; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 5038 | else if(strcmp(argv[i], "-no-exports") == 0) |
| 5039 | exportable = FALSE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5040 | else if(strcmp(argv[i], "-processors") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5041 | if((++i == argc) || !parse_num(argv[i], &processors)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5042 | ERROR("%s: -processors missing or invalid " |
| 5043 | "processor number\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5044 | exit(1); |
| 5045 | } |
| 5046 | if(processors < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5047 | ERROR("%s: -processors should be 1 or larger\n", |
| 5048 | argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5049 | exit(1); |
| 5050 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5051 | } else if(strcmp(argv[i], "-read-queue") == 0) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5052 | if((++i == argc) || !parse_num(argv[i], &readq)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5053 | ERROR("%s: -read-queue missing or invalid " |
| 5054 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5055 | exit(1); |
| 5056 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5057 | if(readq < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5058 | ERROR("%s: -read-queue should be 1 megabyte or " |
| 5059 | "larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5060 | exit(1); |
| 5061 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5062 | } else if(strcmp(argv[i], "-write-queue") == 0) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5063 | if((++i == argc) || !parse_num(argv[i], &bwriteq)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5064 | ERROR("%s: -write-queue missing or invalid " |
| 5065 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5066 | exit(1); |
| 5067 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5068 | if(bwriteq < 2) { |
| 5069 | ERROR("%s: -write-queue should be 2 megabytes " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5070 | "or larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5071 | exit(1); |
| 5072 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5073 | fwriteq = bwriteq >> 1; |
| 5074 | bwriteq -= fwriteq; |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5075 | } else if(strcmp(argv[i], "-fragment-queue") == 0) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5076 | if((++i == argc) || !parse_num(argv[i], &fragq)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5077 | ERROR("%s: -fragment-queue missing or invalid " |
| 5078 | "queue size\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5079 | exit(1); |
| 5080 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5081 | if(fragq < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5082 | ERROR("%s: -fragment-queue should be 1 " |
| 5083 | "megabyte or larger\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5084 | exit(1); |
| 5085 | } |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5086 | } else if(strcmp(argv[i], "-mem") == 0) { |
| 5087 | if((++i == argc) || !parse_num(argv[i], &total_mem)) { |
| 5088 | ERROR("%s: -mem missing or invalid mem size\n", |
| 5089 | argv[0]); |
| 5090 | exit(1); |
| 5091 | } |
| 5092 | if(total_mem < SQUASHFS_LOWMEM) { |
| 5093 | ERROR("%s: -mem should be %d Mbytes or " |
| 5094 | "larger\n", SQUASHFS_LOWMEM, argv[0]); |
| 5095 | exit(1); |
| 5096 | } |
| 5097 | calculate_queue_sizes(total_mem, &readq, &fragq, |
| 5098 | &bwriteq, &fwriteq); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5099 | } else if(strcmp(argv[i], "-b") == 0) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5100 | if(++i == argc) { |
| 5101 | ERROR("%s: -b missing block size\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5102 | exit(1); |
| 5103 | } |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5104 | if(!parse_number(argv[i], &block_size, 1)) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5105 | ERROR("%s: -b invalid block size\n", argv[0]); |
| 5106 | exit(1); |
| 5107 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5108 | if((block_log = slog(block_size)) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5109 | ERROR("%s: -b block size not power of two or " |
| 5110 | "not between 4096 and 1Mbyte\n", |
| 5111 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5112 | exit(1); |
| 5113 | } |
| 5114 | } else if(strcmp(argv[i], "-ef") == 0) { |
| 5115 | if(++i == argc) { |
| 5116 | ERROR("%s: -ef missing filename\n", argv[0]); |
| 5117 | exit(1); |
| 5118 | } |
plougher | 9b5bf8c | 2006-03-20 18:43:33 +0000 | [diff] [blame] | 5119 | } else if(strcmp(argv[i], "-no-duplicates") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5120 | duplicate_checking = FALSE; |
| 5121 | |
| 5122 | else if(strcmp(argv[i], "-no-fragments") == 0) |
| 5123 | no_fragments = TRUE; |
| 5124 | |
| 5125 | else if(strcmp(argv[i], "-always-use-fragments") == 0) |
| 5126 | always_use_fragments = TRUE; |
| 5127 | |
| 5128 | else if(strcmp(argv[i], "-sort") == 0) { |
| 5129 | if(++i == argc) { |
| 5130 | ERROR("%s: -sort missing filename\n", argv[0]); |
| 5131 | exit(1); |
| 5132 | } |
| 5133 | } else if(strcmp(argv[i], "-all-root") == 0 || |
| 5134 | strcmp(argv[i], "-root-owned") == 0) |
| 5135 | global_uid = global_gid = 0; |
| 5136 | |
| 5137 | else if(strcmp(argv[i], "-force-uid") == 0) { |
| 5138 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5139 | ERROR("%s: -force-uid missing uid or user\n", |
| 5140 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5141 | exit(1); |
| 5142 | } |
| 5143 | if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5144 | if(global_uid < 0 || global_uid > |
| 5145 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5146 | ERROR("%s: -force-uid uid out of range" |
| 5147 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5148 | exit(1); |
| 5149 | } |
| 5150 | } else { |
| 5151 | struct passwd *uid = getpwnam(argv[i]); |
| 5152 | if(uid) |
| 5153 | global_uid = uid->pw_uid; |
| 5154 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5155 | ERROR("%s: -force-uid invalid uid or " |
| 5156 | "unknown user\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5157 | exit(1); |
| 5158 | } |
| 5159 | } |
| 5160 | } else if(strcmp(argv[i], "-force-gid") == 0) { |
| 5161 | if(++i == argc) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5162 | ERROR("%s: -force-gid missing gid or group\n", |
| 5163 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5164 | exit(1); |
| 5165 | } |
| 5166 | if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5167 | if(global_gid < 0 || global_gid > |
| 5168 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5169 | ERROR("%s: -force-gid gid out of range" |
| 5170 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5171 | exit(1); |
| 5172 | } |
| 5173 | } else { |
| 5174 | struct group *gid = getgrnam(argv[i]); |
| 5175 | if(gid) |
| 5176 | global_gid = gid->gr_gid; |
| 5177 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5178 | ERROR("%s: -force-gid invalid gid or " |
| 5179 | "unknown group\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5180 | exit(1); |
| 5181 | } |
| 5182 | } |
| 5183 | } else if(strcmp(argv[i], "-noI") == 0 || |
| 5184 | strcmp(argv[i], "-noInodeCompression") == 0) |
| 5185 | noI = TRUE; |
| 5186 | |
| 5187 | else if(strcmp(argv[i], "-noD") == 0 || |
| 5188 | strcmp(argv[i], "-noDataCompression") == 0) |
| 5189 | noD = TRUE; |
| 5190 | |
| 5191 | else if(strcmp(argv[i], "-noF") == 0 || |
| 5192 | strcmp(argv[i], "-noFragmentCompression") == 0) |
| 5193 | noF = TRUE; |
| 5194 | |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5195 | else if(strcmp(argv[i], "-noX") == 0 || |
| 5196 | strcmp(argv[i], "-noXattrCompression") == 0) |
| 5197 | noX = TRUE; |
| 5198 | |
plougher | ce564c6 | 2010-05-19 03:01:49 +0000 | [diff] [blame] | 5199 | else if(strcmp(argv[i], "-no-xattrs") == 0) |
| 5200 | no_xattrs = TRUE; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 5201 | |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5202 | else if(strcmp(argv[i], "-xattrs") == 0) |
| 5203 | no_xattrs = FALSE; |
| 5204 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5205 | else if(strcmp(argv[i], "-nopad") == 0) |
| 5206 | nopad = TRUE; |
| 5207 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5208 | else if(strcmp(argv[i], "-info") == 0) |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5209 | silent = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5210 | |
| 5211 | else if(strcmp(argv[i], "-e") == 0) |
| 5212 | break; |
| 5213 | |
| 5214 | else if(strcmp(argv[i], "-noappend") == 0) |
| 5215 | delete = TRUE; |
| 5216 | |
| 5217 | else if(strcmp(argv[i], "-keep-as-directory") == 0) |
| 5218 | keep_as_directory = TRUE; |
| 5219 | |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 5220 | else if(strcmp(argv[i], "-exit-on-error") == 0) |
| 5221 | exit_on_error = TRUE; |
| 5222 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5223 | else if(strcmp(argv[i], "-root-becomes") == 0) { |
| 5224 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5225 | ERROR("%s: -root-becomes: missing name\n", |
| 5226 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5227 | exit(1); |
| 5228 | } |
| 5229 | root_name = argv[i]; |
| 5230 | } else if(strcmp(argv[i], "-version") == 0) { |
| 5231 | VERSION(); |
| 5232 | } else { |
| 5233 | ERROR("%s: invalid option\n\n", argv[0]); |
| 5234 | printOptions: |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5235 | ERROR("SYNTAX:%s source1 source2 ... dest [options] " |
| 5236 | "[-e list of exclude\ndirs/files]\n", argv[0]); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5237 | ERROR("\nFilesystem build options:\n"); |
plougher | ff5ea8b | 2009-08-07 19:33:10 +0000 | [diff] [blame] | 5238 | ERROR("-comp <comp>\t\tselect <comp> compression\n"); |
plougher | 13df178 | 2009-08-29 01:05:34 +0000 | [diff] [blame] | 5239 | ERROR("\t\t\tCompressors available:\n"); |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 5240 | display_compressors("\t\t\t", COMP_DEFAULT); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5241 | ERROR("-b <block_size>\t\tset data block to " |
Phillip Lougher | b74a59c | 2014-04-30 03:10:44 +0100 | [diff] [blame^] | 5242 | "<block_size>. Default 128 Kbytes\n"); |
| 5243 | ERROR("\t\t\tOptionally a suffix of K or M can be" |
| 5244 | " given to specify\n\t\t\tKbytes or Mbytes" |
| 5245 | " respectively\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5246 | ERROR("-no-exports\t\tdon't make the filesystem " |
| 5247 | "exportable via NFS\n"); |
| 5248 | ERROR("-no-sparse\t\tdon't detect sparse files\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5249 | ERROR("-no-xattrs\t\tdon't store extended attributes" |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5250 | NOXOPT_STR "\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5251 | ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5252 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5253 | ERROR("-noI\t\t\tdo not compress inode table\n"); |
| 5254 | ERROR("-noD\t\t\tdo not compress data blocks\n"); |
| 5255 | ERROR("-noF\t\t\tdo not compress fragment blocks\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5256 | ERROR("-noX\t\t\tdo not compress extended " |
| 5257 | "attributes\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5258 | ERROR("-no-fragments\t\tdo not use fragments\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5259 | ERROR("-always-use-fragments\tuse fragment blocks for " |
| 5260 | "files larger than block size\n"); |
| 5261 | ERROR("-no-duplicates\t\tdo not perform duplicate " |
| 5262 | "checking\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5263 | ERROR("-all-root\t\tmake all files owned by root\n"); |
| 5264 | ERROR("-force-uid uid\t\tset all file uids to uid\n"); |
| 5265 | ERROR("-force-gid gid\t\tset all file gids to gid\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5266 | ERROR("-nopad\t\t\tdo not pad filesystem to a multiple " |
| 5267 | "of 4K\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5268 | ERROR("-keep-as-directory\tif one source directory is " |
| 5269 | "specified, create a root\n"); |
| 5270 | ERROR("\t\t\tdirectory containing that directory, " |
| 5271 | "rather than the\n"); |
| 5272 | ERROR("\t\t\tcontents of the directory\n"); |
| 5273 | ERROR("\nFilesystem filter options:\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5274 | ERROR("-p <pseudo-definition>\tAdd pseudo file " |
| 5275 | "definition\n"); |
| 5276 | ERROR("-pf <pseudo-file>\tAdd list of pseudo file " |
| 5277 | "definitions\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5278 | ERROR("-sort <sort_file>\tsort files according to " |
| 5279 | "priorities in <sort_file>. One\n"); |
| 5280 | ERROR("\t\t\tfile or dir with priority per line. " |
| 5281 | "Priority -32768 to\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5282 | ERROR("\t\t\t32767, default priority 0\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5283 | ERROR("-ef <exclude_file>\tlist of exclude dirs/files." |
| 5284 | " One per line\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5285 | ERROR("-wildcards\t\tAllow extended shell wildcards " |
| 5286 | "(globbing) to be used in\n\t\t\texclude " |
| 5287 | "dirs/files\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5288 | ERROR("-regex\t\t\tAllow POSIX regular expressions to " |
| 5289 | "be used in exclude\n\t\t\tdirs/files\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5290 | ERROR("\nFilesystem append options:\n"); |
| 5291 | ERROR("-noappend\t\tdo not append to existing " |
| 5292 | "filesystem\n"); |
| 5293 | ERROR("-root-becomes <name>\twhen appending source " |
| 5294 | "files/directories, make the\n"); |
| 5295 | ERROR("\t\t\toriginal root become a subdirectory in " |
| 5296 | "the new root\n"); |
| 5297 | ERROR("\t\t\tcalled <name>, rather than adding the new " |
| 5298 | "source items\n"); |
| 5299 | ERROR("\t\t\tto the original root\n"); |
| 5300 | ERROR("\nMksquashfs runtime options:\n"); |
| 5301 | ERROR("-version\t\tprint version, licence and " |
| 5302 | "copyright message\n"); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 5303 | ERROR("-exit-on-error\t\ttreat normally ignored errors " |
| 5304 | "as fatal\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5305 | ERROR("-recover <name>\t\trecover filesystem data " |
| 5306 | "using recovery file <name>\n"); |
| 5307 | ERROR("-no-recovery\t\tdon't generate a recovery " |
| 5308 | "file\n"); |
| 5309 | ERROR("-info\t\t\tprint files written to filesystem\n"); |
| 5310 | ERROR("-no-progress\t\tdon't display the progress " |
| 5311 | "bar\n"); |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5312 | ERROR("-progress\t\tdisplay progress bar when using " |
| 5313 | "the -info option\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5314 | ERROR("-processors <number>\tUse <number> processors." |
| 5315 | " By default will use number of\n"); |
| 5316 | ERROR("\t\t\tprocessors available\n"); |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5317 | ERROR("-mem <size>\t\tUse <size> Mbytes. Currently " |
| 5318 | "set to %d Mbytes\n", total_mem); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5319 | ERROR("\nMiscellaneous options:\n"); |
| 5320 | ERROR("-root-owned\t\talternative name for -all-root" |
| 5321 | "\n"); |
| 5322 | ERROR("-noInodeCompression\talternative name for -noI" |
| 5323 | "\n"); |
| 5324 | ERROR("-noDataCompression\talternative name for -noD" |
| 5325 | "\n"); |
| 5326 | ERROR("-noFragmentCompression\talternative name for " |
| 5327 | "-noF\n"); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5328 | ERROR("-noXattrCompression\talternative name for " |
| 5329 | "-noX\n"); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 5330 | ERROR("\n-Xhelp\t\t\tprint compressor options for" |
| 5331 | " selected compressor\n"); |
plougher | 4fb6682 | 2010-12-08 02:49:28 +0000 | [diff] [blame] | 5332 | ERROR("\nCompressors available and compressor specific " |
| 5333 | "options:\n"); |
| 5334 | display_compressor_usage(COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5335 | exit(1); |
| 5336 | } |
| 5337 | } |
| 5338 | |
plougher | b747f4f | 2010-12-25 04:05:47 +0000 | [diff] [blame] | 5339 | /* |
| 5340 | * Some compressors may need the options to be checked for validity |
| 5341 | * once all the options have been processed |
| 5342 | */ |
| 5343 | res = compressor_options_post(comp, block_size); |
| 5344 | if(res) |
| 5345 | EXIT_MKSQUASHFS(); |
| 5346 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5347 | /* |
| 5348 | * If the -info option has been selected then disable the |
| 5349 | * progress bar unless it has been explicitly enabled with |
| 5350 | * the -progress option |
| 5351 | */ |
| 5352 | if(!silent) |
| 5353 | progress = force_progress; |
| 5354 | |
| 5355 | #ifdef SQUASHFS_TRACE |
Phillip Lougher | 989f5fe | 2013-04-22 03:16:29 +0100 | [diff] [blame] | 5356 | /* |
| 5357 | * Disable progress bar if full debug tracing is enabled. |
| 5358 | * The progress bar in this case just gets in the way of the |
| 5359 | * debug trace output |
| 5360 | */ |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5361 | progress = FALSE; |
| 5362 | #endif |
| 5363 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5364 | for(i = 0; i < source; i++) |
| 5365 | if(lstat(source_path[i], &source_buf) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5366 | fprintf(stderr, "Cannot stat source directory \"%s\" " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5367 | "because %s\n", source_path[i], |
| 5368 | strerror(errno)); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5369 | EXIT_MKSQUASHFS(); |
| 5370 | } |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5371 | |
| 5372 | destination_file = argv[source + 1]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5373 | if(stat(argv[source + 1], &buf) == -1) { |
| 5374 | if(errno == ENOENT) { /* Does not exist */ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5375 | fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, |
plougher | 59dce67 | 2010-05-19 03:56:59 +0000 | [diff] [blame] | 5376 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5377 | if(fd == -1) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5378 | perror("Could not create destination file"); |
| 5379 | exit(1); |
| 5380 | } |
| 5381 | delete = TRUE; |
| 5382 | } else { |
| 5383 | perror("Could not stat destination file"); |
| 5384 | exit(1); |
| 5385 | } |
| 5386 | |
| 5387 | } else { |
| 5388 | if(S_ISBLK(buf.st_mode)) { |
| 5389 | if((fd = open(argv[source + 1], O_RDWR)) == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5390 | perror("Could not open block device as " |
| 5391 | "destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5392 | exit(1); |
| 5393 | } |
| 5394 | block_device = 1; |
| 5395 | |
| 5396 | } else if(S_ISREG(buf.st_mode)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5397 | fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | |
| 5398 | O_RDWR); |
| 5399 | if(fd == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5400 | perror("Could not open regular file for " |
| 5401 | "writing as destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5402 | exit(1); |
| 5403 | } |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 5404 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5405 | else { |
| 5406 | ERROR("Destination not block device or regular file\n"); |
| 5407 | exit(1); |
| 5408 | } |
| 5409 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5410 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5411 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5412 | /* |
| 5413 | * process the exclude files - must be done afer destination file has |
| 5414 | * been possibly created |
| 5415 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5416 | for(i = source + 2; i < argc; i++) |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 5417 | if(strcmp(argv[i], "-ef") == 0) |
| 5418 | /* |
| 5419 | * Note presence of filename arg has already |
| 5420 | * been checked |
| 5421 | */ |
| 5422 | process_exclude_file(argv[++i]); |
| 5423 | else if(strcmp(argv[i], "-e") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5424 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5425 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5426 | strcmp(argv[i], "-sort") == 0 || |
| 5427 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5428 | strcmp(argv[i], "-af") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5429 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5430 | i++; |
| 5431 | |
| 5432 | if(i != argc) { |
| 5433 | if(++i == argc) { |
| 5434 | ERROR("%s: -e missing arguments\n", argv[0]); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5435 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5436 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 5437 | while(i < argc) |
| 5438 | if(old_exclude) |
| 5439 | old_add_exclude(argv[i++]); |
| 5440 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5441 | add_exclude(argv[i++]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | /* process the sort files - must be done afer the exclude files */ |
| 5445 | for(i = source + 2; i < argc; i++) |
| 5446 | if(strcmp(argv[i], "-sort") == 0) { |
plougher | e1c9a74 | 2010-07-21 16:59:05 +0000 | [diff] [blame] | 5447 | int res = read_sort_file(argv[++i], source, |
| 5448 | source_path); |
| 5449 | if(res == FALSE) |
| 5450 | BAD_ERROR("Failed to read sort file\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5451 | sorted ++; |
| 5452 | } else if(strcmp(argv[i], "-e") == 0) |
| 5453 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5454 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5455 | strcmp(argv[i], "-ef") == 0 || |
| 5456 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5457 | strcmp(argv[i], "-af") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5458 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5459 | i++; |
| 5460 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5461 | if(!delete) { |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 5462 | comp = read_super(fd, &sBlk, argv[source + 1]); |
| 5463 | if(comp == NULL) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5464 | ERROR("Failed to read existing filesystem - will not " |
| 5465 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5466 | ERROR("To force Mksquashfs to write to this block " |
| 5467 | "device or file use -noappend\n"); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5468 | EXIT_MKSQUASHFS(); |
| 5469 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5470 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5471 | block_log = slog(block_size = sBlk.block_size); |
| 5472 | noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags); |
| 5473 | noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags); |
| 5474 | noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5475 | noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5476 | no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags); |
| 5477 | always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags); |
| 5478 | duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5479 | exportable = SQUASHFS_EXPORTABLE(sBlk.flags); |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5480 | no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5481 | comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5484 | initialise_threads(readq, fragq, bwriteq, fwriteq, delete, |
| 5485 | destination_file); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5486 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 5487 | res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0); |
| 5488 | if(res) |
| 5489 | BAD_ERROR("compressor_init failed\n"); |
| 5490 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5491 | if(delete) { |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5492 | int size; |
Phillip Lougher | a45c9d2 | 2011-02-20 04:24:07 +0000 | [diff] [blame] | 5493 | void *comp_data = compressor_dump_options(comp, block_size, |
| 5494 | &size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5495 | |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 5496 | printf("Creating %d.%d filesystem on %s, block size %d.\n", |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5497 | SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5498 | |
plougher | 871c72a | 2010-12-25 04:17:27 +0000 | [diff] [blame] | 5499 | /* |
| 5500 | * store any compressor specific options after the superblock, |
| 5501 | * and set the COMP_OPT flag to show that the filesystem has |
| 5502 | * compressor specfic options |
| 5503 | */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5504 | if(comp_data) { |
| 5505 | unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT; |
| 5506 | |
| 5507 | SQUASHFS_INSWAP_SHORTS(&c_byte, 1); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5508 | write_destination(fd, sizeof(struct squashfs_super_block), |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 5509 | sizeof(c_byte), &c_byte); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5510 | write_destination(fd, sizeof(struct squashfs_super_block) + |
plougher | 8e4dad4 | 2010-12-28 05:56:22 +0000 | [diff] [blame] | 5511 | sizeof(c_byte), size, comp_data); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5512 | bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte) |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5513 | + size; |
| 5514 | comp_opts = TRUE; |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5515 | } else |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5516 | bytes = sizeof(struct squashfs_super_block); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5517 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5518 | unsigned int last_directory_block, inode_dir_offset, |
| 5519 | inode_dir_file_size, root_inode_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5520 | inode_dir_start_block, uncompressed_data, |
| 5521 | compressed_data, inode_dir_inode_number, |
| 5522 | inode_dir_parent_inode; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5523 | unsigned int root_inode_start = |
| 5524 | SQUASHFS_INODE_BLK(sBlk.root_inode), |
| 5525 | root_inode_offset = |
| 5526 | SQUASHFS_INODE_OFFSET(sBlk.root_inode); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5527 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5528 | if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, |
| 5529 | &data_cache, &directory_table, |
| 5530 | &directory_data_cache, &last_directory_block, |
| 5531 | &inode_dir_offset, &inode_dir_file_size, |
| 5532 | &root_inode_size, &inode_dir_start_block, |
| 5533 | &file_count, &sym_count, &dev_count, &dir_count, |
| 5534 | &fifo_count, &sock_count, &total_bytes, |
| 5535 | &total_inode_bytes, &total_directory_bytes, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5536 | &inode_dir_inode_number, |
| 5537 | &inode_dir_parent_inode, add_old_root_entry, |
| 5538 | &fragment_table, &inode_lookup_table)) == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5539 | ERROR("Failed to read existing filesystem - will not " |
| 5540 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5541 | ERROR("To force Mksquashfs to write to this block " |
| 5542 | "device or file use -noappend\n"); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5543 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5544 | } |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 5545 | if((append_fragments = fragments = sBlk.fragments)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5546 | fragment_table = realloc((char *) fragment_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5547 | ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 5548 | * sizeof(struct squashfs_fragment_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5549 | if(fragment_table == NULL) |
| 5550 | BAD_ERROR("Out of memory in save filesystem state\n"); |
| 5551 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5552 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5553 | printf("Appending to existing %d.%d filesystem on %s, block " |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5554 | "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5555 | block_size); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5556 | printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, " |
plougher | bb98803 | 2009-08-06 08:46:34 +0000 | [diff] [blame] | 5557 | "-always-use-fragments,\n-exportable and -comp options " |
| 5558 | "ignored\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5559 | printf("\nIf appending is not wanted, please re-run with " |
| 5560 | "-noappend specified!\n\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5561 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5562 | compressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5563 | ~(SQUASHFS_METADATA_SIZE - 1); |
| 5564 | uncompressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5565 | (SQUASHFS_METADATA_SIZE - 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5566 | |
| 5567 | /* save original filesystem state for restoring ... */ |
| 5568 | sfragments = fragments; |
| 5569 | sbytes = bytes; |
| 5570 | sinode_count = sBlk.inodes; |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 5571 | scache_bytes = root_inode_offset + root_inode_size; |
| 5572 | sdirectory_cache_bytes = uncompressed_data; |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5573 | sdata_cache = malloc(scache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5574 | if(sdata_cache == NULL) |
| 5575 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5576 | sdirectory_data_cache = malloc(sdirectory_cache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5577 | if(sdirectory_data_cache == NULL) |
| 5578 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5579 | memcpy(sdata_cache, data_cache, scache_bytes); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5580 | memcpy(sdirectory_data_cache, directory_data_cache + |
| 5581 | compressed_data, sdirectory_cache_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5582 | sinode_bytes = root_inode_start; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5583 | stotal_bytes = total_bytes; |
| 5584 | stotal_inode_bytes = total_inode_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5585 | stotal_directory_bytes = total_directory_bytes + |
| 5586 | compressed_data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5587 | sfile_count = file_count; |
| 5588 | ssym_count = sym_count; |
| 5589 | sdev_count = dev_count; |
| 5590 | sdir_count = dir_count + 1; |
| 5591 | sfifo_count = fifo_count; |
| 5592 | ssock_count = sock_count; |
| 5593 | sdup_files = dup_files; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5594 | sid_count = id_count; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5595 | write_recovery_data(&sBlk); |
Phillip Lougher | 3bcf67d | 2013-02-24 10:56:38 +0000 | [diff] [blame] | 5596 | save_xattrs(); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 5597 | appending = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5598 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5599 | /* |
| 5600 | * set the filesystem state up to be able to append to the |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5601 | * original filesystem. The filesystem state differs depending |
| 5602 | * on whether we're appending to the original root directory, or |
| 5603 | * if the original root directory becomes a sub-directory |
| 5604 | * (root-becomes specified on command line, here root_name != |
| 5605 | * NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5606 | */ |
| 5607 | inode_bytes = inode_size = root_inode_start; |
| 5608 | directory_size = last_directory_block; |
| 5609 | cache_size = root_inode_offset + root_inode_size; |
| 5610 | directory_cache_size = inode_dir_offset + inode_dir_file_size; |
| 5611 | if(root_name) { |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5612 | sdirectory_bytes = last_directory_block; |
| 5613 | sdirectory_compressed_bytes = 0; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5614 | root_inode_number = inode_dir_parent_inode; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5615 | inode_no = sBlk.inodes + 2; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5616 | directory_bytes = last_directory_block; |
| 5617 | directory_cache_bytes = uncompressed_data; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5618 | memmove(directory_data_cache, directory_data_cache + |
| 5619 | compressed_data, uncompressed_data); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5620 | cache_bytes = root_inode_offset + root_inode_size; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5621 | add_old_root_entry(root_name, sBlk.root_inode, |
| 5622 | inode_dir_inode_number, SQUASHFS_DIR_TYPE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5623 | total_directory_bytes += compressed_data; |
| 5624 | dir_count ++; |
| 5625 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5626 | sdirectory_compressed_bytes = last_directory_block - |
| 5627 | inode_dir_start_block; |
| 5628 | sdirectory_compressed = |
| 5629 | malloc(sdirectory_compressed_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5630 | if(sdirectory_compressed == NULL) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5631 | BAD_ERROR("Out of memory in save filesystem " |
| 5632 | "state\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5633 | memcpy(sdirectory_compressed, directory_table + |
| 5634 | inode_dir_start_block, |
| 5635 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5636 | sdirectory_bytes = inode_dir_start_block; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5637 | root_inode_number = inode_dir_inode_number; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5638 | inode_no = sBlk.inodes + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5639 | directory_bytes = inode_dir_start_block; |
| 5640 | directory_cache_bytes = inode_dir_offset; |
| 5641 | cache_bytes = root_inode_offset; |
| 5642 | } |
| 5643 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5644 | inode_count = file_count + dir_count + sym_count + dev_count + |
| 5645 | fifo_count + sock_count; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 5648 | if(path) |
| 5649 | paths = add_subdir(paths, path); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5650 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5651 | dump_actions(); |
Phillip Lougher | 7a28f7a | 2014-04-20 23:47:08 +0100 | [diff] [blame] | 5652 | dump_pseudos(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5653 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5654 | if(delete && !keep_as_directory && source == 1 && |
| 5655 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5656 | dir_scan(&inode, source_path[0], scan1_readdir, progress); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5657 | else if(!keep_as_directory && source == 1 && |
| 5658 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5659 | dir_scan(&inode, source_path[0], scan1_single_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5660 | else |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5661 | dir_scan(&inode, "", scan1_encomp_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5662 | sBlk.root_inode = inode; |
| 5663 | sBlk.inodes = inode_count; |
| 5664 | sBlk.s_magic = SQUASHFS_MAGIC; |
| 5665 | sBlk.s_major = SQUASHFS_MAJOR; |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5666 | sBlk.s_minor = SQUASHFS_MINOR; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5667 | sBlk.block_size = block_size; |
| 5668 | sBlk.block_log = block_log; |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5669 | sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments, |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5670 | always_use_fragments, duplicate_checking, exportable, |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5671 | no_xattrs, comp_opts); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5672 | sBlk.mkfs_time = time(NULL); |
| 5673 | |
Phillip Lougher | 83b8f86 | 2013-04-09 03:28:55 +0100 | [diff] [blame] | 5674 | disable_info(); |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 5675 | |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5676 | while((fragment = get_frag_action(fragment))) |
| 5677 | write_fragment(*fragment); |
| 5678 | unlock_fragments(); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 5679 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5680 | pthread_mutex_lock(&fragment_mutex); |
| 5681 | while(fragments_outstanding) { |
| 5682 | pthread_mutex_unlock(&fragment_mutex); |
| 5683 | sched_yield(); |
| 5684 | pthread_mutex_lock(&fragment_mutex); |
| 5685 | } |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 5686 | pthread_cleanup_pop(1); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5687 | |
| 5688 | queue_put(to_writer, NULL); |
| 5689 | if(queue_get(from_writer) != 0) |
| 5690 | EXIT_MKSQUASHFS(); |
| 5691 | |
Phillip Lougher | cd1a5a6 | 2014-01-21 04:53:46 +0000 | [diff] [blame] | 5692 | set_progressbar_state(FALSE); |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 5693 | write_filesystem_tables(&sBlk, nopad); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5694 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5695 | return 0; |
| 5696 | } |