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 | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 307 | void check_usable_phys_mem(int total_mem); |
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 | |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 353 | int multiply_overflowll(long long a, int multiplier) |
| 354 | { |
| 355 | return (LLONG_MAX / multiplier) < a; |
| 356 | } |
| 357 | |
| 358 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 359 | #define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \ |
| 360 | + (((char *)A) - data_cache))) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 361 | |
| 362 | |
| 363 | void restorefs() |
| 364 | { |
| 365 | ERROR("Exiting - restoring original filesystem!\n\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 366 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 367 | bytes = sbytes; |
| 368 | memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 369 | memcpy(directory_data_cache, sdirectory_data_cache, |
| 370 | sdirectory_cache_bytes); |
| 371 | directory_cache_bytes = sdirectory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 372 | inode_bytes = sinode_bytes; |
| 373 | directory_bytes = sdirectory_bytes; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 374 | memcpy(directory_table + directory_bytes, sdirectory_compressed, |
| 375 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 376 | directory_bytes += sdirectory_compressed_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 377 | total_bytes = stotal_bytes; |
| 378 | total_inode_bytes = stotal_inode_bytes; |
| 379 | total_directory_bytes = stotal_directory_bytes; |
| 380 | inode_count = sinode_count; |
| 381 | file_count = sfile_count; |
| 382 | sym_count = ssym_count; |
| 383 | dev_count = sdev_count; |
| 384 | dir_count = sdir_count; |
| 385 | fifo_count = sfifo_count; |
| 386 | sock_count = ssock_count; |
| 387 | dup_files = sdup_files; |
| 388 | fragments = sfragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 389 | id_count = sid_count; |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 390 | restore_xattrs(); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 391 | write_filesystem_tables(&sBlk, nopad); |
| 392 | exit(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 396 | void sighandler() |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 397 | { |
| 398 | EXIT_MKSQUASHFS(); |
| 399 | } |
| 400 | |
| 401 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 402 | int mangle2(void *strm, char *d, char *s, int size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 403 | int block_size, int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 404 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 405 | int error, c_byte = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 406 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 407 | if(!uncompressed) { |
plougher | bfb876c | 2010-12-31 07:48:01 +0000 | [diff] [blame] | 408 | c_byte = compressor_compress(comp, strm, d, s, size, block_size, |
| 409 | &error); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 410 | if(c_byte == -1) |
| 411 | BAD_ERROR("mangle2:: %s compress failed with error " |
| 412 | "code %d\n", comp->name, error); |
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 | if(c_byte == 0 || c_byte >= size) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 416 | memcpy(d, s, size); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 417 | return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : |
| 418 | SQUASHFS_COMPRESSED_BIT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 419 | } |
| 420 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 421 | return c_byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 425 | int mangle(char *d, char *s, int size, int block_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 426 | int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 427 | { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 428 | return mangle2(stream, d, s, size, block_size, uncompressed, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 429 | data_block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 433 | void *get_inode(int req_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 434 | { |
| 435 | int data_space; |
| 436 | unsigned short c_byte; |
| 437 | |
| 438 | while(cache_bytes >= SQUASHFS_METADATA_SIZE) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 439 | if((inode_size - inode_bytes) < |
| 440 | ((SQUASHFS_METADATA_SIZE << 1)) + 2) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 441 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 442 | (SQUASHFS_METADATA_SIZE << 1) + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 443 | if(it == NULL) |
| 444 | MEM_ERROR(); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 445 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 446 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
| 447 | } |
| 448 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 449 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 450 | data_cache, SQUASHFS_METADATA_SIZE, |
| 451 | SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 452 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 453 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 454 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 455 | total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 456 | memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 457 | cache_bytes - SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 458 | cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 459 | } |
| 460 | |
| 461 | data_space = (cache_size - cache_bytes); |
| 462 | if(data_space < req_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 463 | int realloc_size = cache_size == 0 ? |
| 464 | ((req_size + SQUASHFS_METADATA_SIZE) & |
| 465 | ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - |
| 466 | data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 467 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 468 | void *dc = realloc(data_cache, cache_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 469 | realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 470 | if(dc == NULL) |
| 471 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 472 | cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 473 | data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | cache_bytes += req_size; |
| 477 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 478 | return data_cache + cache_bytes - req_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 482 | int read_bytes(int fd, void *buff, int bytes) |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 483 | { |
| 484 | int res, count; |
| 485 | |
| 486 | for(count = 0; count < bytes; count += res) { |
| 487 | res = read(fd, buff + count, bytes - count); |
| 488 | if(res < 1) { |
plougher | 96f85a1 | 2009-03-29 22:39:59 +0000 | [diff] [blame] | 489 | if(res == 0) |
| 490 | goto bytes_read; |
| 491 | else if(errno != EINTR) { |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 492 | ERROR("Read failed because %s\n", |
| 493 | strerror(errno)); |
| 494 | return -1; |
| 495 | } else |
| 496 | res = 0; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | bytes_read: |
| 501 | return count; |
| 502 | } |
| 503 | |
| 504 | |
plougher | 3306cb2 | 2010-06-18 04:22:44 +0000 | [diff] [blame] | 505 | int read_fs_bytes(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 506 | { |
| 507 | off_t off = byte; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 508 | int res = 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 509 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 510 | TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 511 | byte, bytes); |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 512 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 513 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 514 | pthread_mutex_lock(&pos_mutex); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 515 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 516 | ERROR("read_fs_bytes: Lseek on destination failed because %s, " |
| 517 | "offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 518 | res = 0; |
Phillip Lougher | 041cf6d | 2013-07-20 04:20:40 +0100 | [diff] [blame] | 519 | } else if(read_bytes(fd, buff, bytes) < bytes) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 520 | ERROR("Read on destination failed\n"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 521 | res = 0; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 524 | pthread_cleanup_pop(1); |
| 525 | return res; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 529 | int write_bytes(int fd, void *buff, int bytes) |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 530 | { |
| 531 | int res, count; |
| 532 | |
| 533 | for(count = 0; count < bytes; count += res) { |
| 534 | res = write(fd, buff + count, bytes - count); |
| 535 | if(res == -1) { |
| 536 | if(errno != EINTR) { |
| 537 | ERROR("Write failed because %s\n", |
| 538 | strerror(errno)); |
| 539 | return -1; |
| 540 | } |
| 541 | res = 0; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 549 | void write_destination(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 550 | { |
| 551 | off_t off = byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 552 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 553 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
| 554 | pthread_mutex_lock(&pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 555 | |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 556 | if(lseek(fd, off, SEEK_SET) == -1) { |
| 557 | ERROR("write_destination: Lseek on destination " |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 558 | "failed because %s, offset=0x%llx\n", strerror(errno), |
| 559 | off); |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 560 | BAD_ERROR("Probably out of space on output %s\n", |
| 561 | block_device ? "block device" : "filesystem"); |
| 562 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 563 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 564 | if(write_bytes(fd, buff, bytes) == -1) |
Phillip Lougher | e8b536f | 2013-02-12 22:01:21 +0000 | [diff] [blame] | 565 | BAD_ERROR("Failed to write to output %s\n", |
| 566 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 567 | |
| 568 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
| 572 | long long write_inodes() |
| 573 | { |
| 574 | unsigned short c_byte; |
| 575 | int avail_bytes; |
| 576 | char *datap = data_cache; |
| 577 | long long start_bytes = bytes; |
| 578 | |
| 579 | while(cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 580 | if(inode_size - inode_bytes < |
| 581 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 582 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 583 | ((SQUASHFS_METADATA_SIZE << 1) + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 584 | if(it == NULL) |
| 585 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 586 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 587 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 588 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 589 | avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 590 | SQUASHFS_METADATA_SIZE : cache_bytes; |
| 591 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap, |
| 592 | avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 593 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 594 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 595 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 596 | total_inode_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 597 | datap += avail_bytes; |
| 598 | cache_bytes -= avail_bytes; |
| 599 | } |
| 600 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 601 | write_destination(fd, bytes, inode_bytes, inode_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 602 | bytes += inode_bytes; |
| 603 | |
| 604 | return start_bytes; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | long long write_directories() |
| 609 | { |
| 610 | unsigned short c_byte; |
| 611 | int avail_bytes; |
| 612 | char *directoryp = directory_data_cache; |
| 613 | long long start_bytes = bytes; |
| 614 | |
| 615 | while(directory_cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 616 | if(directory_size - directory_bytes < |
| 617 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 618 | void *dt = realloc(directory_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 619 | directory_size + ((SQUASHFS_METADATA_SIZE << 1) |
| 620 | + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 621 | if(dt == NULL) |
| 622 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 623 | directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 624 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 625 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 626 | avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 627 | SQUASHFS_METADATA_SIZE : directory_cache_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 628 | c_byte = mangle(directory_table + directory_bytes + |
| 629 | BLOCK_OFFSET, directoryp, avail_bytes, |
| 630 | SQUASHFS_METADATA_SIZE, noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 631 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 632 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 633 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 634 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 635 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 636 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 637 | total_directory_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 638 | directoryp += avail_bytes; |
| 639 | directory_cache_bytes -= avail_bytes; |
| 640 | } |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 641 | write_destination(fd, bytes, directory_bytes, directory_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 642 | bytes += directory_bytes; |
| 643 | |
| 644 | return start_bytes; |
| 645 | } |
| 646 | |
| 647 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 648 | long long write_id_table() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 649 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 650 | unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 651 | unsigned int p[id_count]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 652 | int i; |
| 653 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 654 | 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] | 655 | for(i = 0; i < id_count; i++) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 656 | 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] | 657 | SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 658 | } |
| 659 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 660 | return generic_write_table(id_bytes, p, 0, NULL, noI); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 664 | struct id *get_id(unsigned int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 665 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 666 | int hash = ID_HASH(id); |
| 667 | struct id *entry = id_hash_table[hash]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 668 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 669 | for(; entry; entry = entry->next) |
| 670 | if(entry->id == id) |
| 671 | break; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 672 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 673 | return entry; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 677 | struct id *create_id(unsigned int id) |
| 678 | { |
| 679 | int hash = ID_HASH(id); |
| 680 | struct id *entry = malloc(sizeof(struct id)); |
| 681 | if(entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 682 | MEM_ERROR(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 683 | entry->id = id; |
| 684 | entry->index = id_count ++; |
| 685 | entry->flags = 0; |
| 686 | entry->next = id_hash_table[hash]; |
| 687 | id_hash_table[hash] = entry; |
| 688 | id_table[entry->index] = entry; |
| 689 | return entry; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | unsigned int get_uid(unsigned int uid) |
| 694 | { |
| 695 | struct id *entry = get_id(uid); |
| 696 | |
| 697 | if(entry == NULL) { |
| 698 | if(id_count == SQUASHFS_IDS) |
| 699 | BAD_ERROR("Out of uids!\n"); |
| 700 | entry = create_id(uid); |
| 701 | } |
| 702 | |
| 703 | if((entry->flags & ISA_UID) == 0) { |
| 704 | entry->flags |= ISA_UID; |
| 705 | uid_count ++; |
| 706 | } |
| 707 | |
| 708 | return entry->index; |
| 709 | } |
| 710 | |
| 711 | |
| 712 | unsigned int get_guid(unsigned int guid) |
| 713 | { |
| 714 | struct id *entry = get_id(guid); |
| 715 | |
| 716 | if(entry == NULL) { |
| 717 | if(id_count == SQUASHFS_IDS) |
| 718 | BAD_ERROR("Out of gids!\n"); |
| 719 | entry = create_id(guid); |
| 720 | } |
| 721 | |
| 722 | if((entry->flags & ISA_GID) == 0) { |
| 723 | entry->flags |= ISA_GID; |
| 724 | guid_count ++; |
| 725 | } |
| 726 | |
| 727 | return entry->index; |
| 728 | } |
| 729 | |
| 730 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 731 | #define ALLOC_SIZE 128 |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 732 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 733 | char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size) |
| 734 | { |
| 735 | if(pathname == NULL) { |
| 736 | pathname = malloc(ALLOC_SIZE); |
| 737 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 738 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | for(;;) { |
| 742 | int res = snprintf(pathname, *size, "%s/%s", |
| 743 | dir_ent->our_dir->pathname, |
| 744 | dir_ent->source_name ? : dir_ent->name); |
| 745 | |
| 746 | if(res < 0) |
| 747 | BAD_ERROR("snprintf failed in pathname\n"); |
| 748 | else if(res >= *size) { |
| 749 | /* |
| 750 | * pathname is too small to contain the result, so |
| 751 | * increase it and try again |
| 752 | */ |
| 753 | *size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 754 | pathname = realloc(pathname, *size); |
| 755 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 756 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 757 | } else |
| 758 | break; |
| 759 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 760 | |
| 761 | return pathname; |
| 762 | } |
| 763 | |
| 764 | |
| 765 | char *pathname(struct dir_ent *dir_ent) |
| 766 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 767 | static char *pathname = NULL; |
| 768 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 769 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 770 | if (dir_ent->nonstandard_pathname) |
| 771 | return dir_ent->nonstandard_pathname; |
| 772 | |
| 773 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | |
| 777 | char *pathname_reader(struct dir_ent *dir_ent) |
| 778 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 779 | static char *pathname = NULL; |
| 780 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 781 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 782 | if (dir_ent->nonstandard_pathname) |
| 783 | return dir_ent->nonstandard_pathname; |
| 784 | |
| 785 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 789 | char *subpathname(struct dir_ent *dir_ent) |
| 790 | { |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 791 | static char *subpath = NULL; |
| 792 | static int size = ALLOC_SIZE; |
| 793 | int res; |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 794 | |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 795 | if(subpath == NULL) { |
| 796 | subpath = malloc(ALLOC_SIZE); |
| 797 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 798 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | for(;;) { |
| 802 | if(dir_ent->our_dir->subpath[0] != '\0') |
| 803 | res = snprintf(subpath, size, "%s/%s", |
| 804 | dir_ent->our_dir->subpath, dir_ent->name); |
| 805 | else |
| 806 | res = snprintf(subpath, size, "/%s", dir_ent->name); |
| 807 | |
| 808 | if(res < 0) |
| 809 | BAD_ERROR("snprintf failed in subpathname\n"); |
| 810 | else if(res >= size) { |
| 811 | /* |
| 812 | * subpath is too small to contain the result, so |
| 813 | * increase it and try again |
| 814 | */ |
| 815 | size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 816 | subpath = realloc(subpath, size); |
| 817 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 818 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 819 | } else |
| 820 | break; |
Phillip Lougher | 90e0825 | 2012-10-05 04:33:35 +0100 | [diff] [blame] | 821 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 822 | |
| 823 | return subpath; |
| 824 | } |
| 825 | |
| 826 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 827 | inline unsigned int get_inode_no(struct inode_info *inode) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 828 | { |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 829 | return inode->inode_number; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 833 | inline unsigned int get_parent_no(struct dir_info *dir) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 834 | { |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 835 | return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 839 | int create_inode(squashfs_inode *i_no, struct dir_info *dir_info, |
| 840 | struct dir_ent *dir_ent, int type, long long byte_size, |
| 841 | long long start_block, unsigned int offset, unsigned int *block_list, |
| 842 | struct fragment *fragment, struct directory *dir_in, long long sparse) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 843 | { |
| 844 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 8973a12 | 2010-12-31 09:52:45 +0000 | [diff] [blame] | 845 | union squashfs_inode_header inode_header; |
plougher | 857d0dd | 2010-12-31 21:03:13 +0000 | [diff] [blame] | 846 | struct squashfs_base_inode_header *base = &inode_header.base; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 847 | void *inode; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 848 | char *filename = pathname(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 849 | int nlink = dir_ent->inode->nlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 850 | int xattr = read_xattrs(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 851 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 852 | switch(type) { |
| 853 | case SQUASHFS_FILE_TYPE: |
| 854 | if(dir_ent->inode->nlink > 1 || |
| 855 | byte_size >= (1LL << 32) || |
| 856 | start_block >= (1LL << 32) || |
| 857 | sparse || IS_XATTR(xattr)) |
| 858 | type = SQUASHFS_LREG_TYPE; |
| 859 | break; |
| 860 | case SQUASHFS_DIR_TYPE: |
| 861 | if(dir_info->dir_is_ldir || IS_XATTR(xattr)) |
| 862 | type = SQUASHFS_LDIR_TYPE; |
| 863 | break; |
| 864 | case SQUASHFS_SYMLINK_TYPE: |
| 865 | if(IS_XATTR(xattr)) |
| 866 | type = SQUASHFS_LSYMLINK_TYPE; |
| 867 | break; |
| 868 | case SQUASHFS_BLKDEV_TYPE: |
| 869 | if(IS_XATTR(xattr)) |
| 870 | type = SQUASHFS_LBLKDEV_TYPE; |
| 871 | break; |
| 872 | case SQUASHFS_CHRDEV_TYPE: |
| 873 | if(IS_XATTR(xattr)) |
| 874 | type = SQUASHFS_LCHRDEV_TYPE; |
| 875 | break; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 876 | case SQUASHFS_FIFO_TYPE: |
| 877 | if(IS_XATTR(xattr)) |
| 878 | type = SQUASHFS_LFIFO_TYPE; |
| 879 | break; |
| 880 | case SQUASHFS_SOCKET_TYPE: |
| 881 | if(IS_XATTR(xattr)) |
| 882 | type = SQUASHFS_LSOCKET_TYPE; |
| 883 | break; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 884 | } |
| 885 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 886 | base->mode = SQUASHFS_MODE(buf->st_mode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 887 | base->uid = get_uid((unsigned int) global_uid == -1 ? |
| 888 | buf->st_uid : global_uid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 889 | base->inode_type = type; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 890 | base->guid = get_guid((unsigned int) global_gid == -1 ? |
| 891 | buf->st_gid : global_gid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 892 | base->mtime = buf->st_mtime; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 893 | base->inode_number = get_inode_no(dir_ent->inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 894 | |
| 895 | if(type == SQUASHFS_FILE_TYPE) { |
| 896 | int i; |
plougher | 8701ed6 | 2010-12-31 20:38:38 +0000 | [diff] [blame] | 897 | struct squashfs_reg_inode_header *reg = &inode_header.reg; |
| 898 | size_t off = offsetof(struct squashfs_reg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 899 | |
| 900 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 901 | reg->file_size = byte_size; |
| 902 | reg->start_block = start_block; |
| 903 | reg->fragment = fragment->index; |
| 904 | reg->offset = fragment->offset; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 905 | SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode); |
| 906 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 907 | TRACE("File inode, file_size %lld, start_block 0x%llx, blocks " |
| 908 | "%d, fragment %d, offset %d, size %d\n", byte_size, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 909 | start_block, offset, fragment->index, fragment->offset, |
| 910 | fragment->size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 911 | for(i = 0; i < offset; i++) |
| 912 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 913 | } |
| 914 | else if(type == SQUASHFS_LREG_TYPE) { |
| 915 | int i; |
plougher | 1e6ac4a | 2010-12-31 20:42:02 +0000 | [diff] [blame] | 916 | struct squashfs_lreg_inode_header *reg = &inode_header.lreg; |
| 917 | size_t off = offsetof(struct squashfs_lreg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 918 | |
| 919 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 920 | reg->nlink = nlink; |
| 921 | reg->file_size = byte_size; |
| 922 | reg->start_block = start_block; |
| 923 | reg->fragment = fragment->index; |
| 924 | reg->offset = fragment->offset; |
plougher | f5a674d | 2009-03-25 05:38:27 +0000 | [diff] [blame] | 925 | if(sparse && sparse >= byte_size) |
| 926 | sparse = byte_size - 1; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 927 | reg->sparse = sparse; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 928 | reg->xattr = xattr; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 929 | SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode); |
| 930 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 931 | TRACE("Long file inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 932 | "blocks %d, fragment %d, offset %d, size %d, nlink %d" |
| 933 | "\n", byte_size, start_block, offset, fragment->index, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 934 | fragment->offset, fragment->size, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 935 | for(i = 0; i < offset; i++) |
| 936 | TRACE("Block %d, size %d\n", i, block_list[i]); |
| 937 | } |
| 938 | else if(type == SQUASHFS_LDIR_TYPE) { |
| 939 | int i; |
| 940 | unsigned char *p; |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 941 | struct squashfs_ldir_inode_header *dir = &inode_header.ldir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 942 | struct cached_dir_index *index = dir_in->index; |
| 943 | unsigned int i_count = dir_in->i_count; |
| 944 | unsigned int i_size = dir_in->i_size; |
| 945 | |
| 946 | if(byte_size >= 1 << 27) |
| 947 | BAD_ERROR("directory greater than 2^27-1 bytes!\n"); |
| 948 | |
| 949 | inode = get_inode(sizeof(*dir) + i_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 950 | dir->inode_type = SQUASHFS_LDIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 951 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 952 | dir->file_size = byte_size; |
| 953 | dir->offset = offset; |
| 954 | dir->start_block = start_block; |
| 955 | dir->i_count = i_count; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 956 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 957 | dir->xattr = xattr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 958 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 959 | SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode); |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 960 | p = inode + offsetof(struct squashfs_ldir_inode_header, index); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 961 | for(i = 0; i < i_count; i++) { |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 962 | SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p); |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 963 | p += offsetof(struct squashfs_dir_index, name); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 964 | memcpy(p, index[i].name, index[i].index.size + 1); |
| 965 | p += index[i].index.size + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 966 | } |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 967 | TRACE("Long directory inode, file_size %lld, start_block " |
| 968 | "0x%llx, offset 0x%x, nlink %d\n", byte_size, |
| 969 | start_block, offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 970 | } |
| 971 | else if(type == SQUASHFS_DIR_TYPE) { |
plougher | 9d80a60 | 2010-12-31 20:47:24 +0000 | [diff] [blame] | 972 | struct squashfs_dir_inode_header *dir = &inode_header.dir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 973 | |
| 974 | inode = get_inode(sizeof(*dir)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 975 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 976 | dir->file_size = byte_size; |
| 977 | dir->offset = offset; |
| 978 | dir->start_block = start_block; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 979 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 980 | SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 981 | TRACE("Directory inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 982 | "offset 0x%x, nlink %d\n", byte_size, start_block, |
| 983 | offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 984 | } |
| 985 | else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) { |
plougher | c70c633 | 2010-12-31 20:11:09 +0000 | [diff] [blame] | 986 | struct squashfs_dev_inode_header *dev = &inode_header.dev; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 987 | unsigned int major = major(buf->st_rdev); |
| 988 | unsigned int minor = minor(buf->st_rdev); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 989 | |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 990 | if(major > 0xfff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 991 | ERROR("Major %d out of range in device node %s, " |
| 992 | "truncating to %d\n", major, filename, |
| 993 | major & 0xfff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 994 | major &= 0xfff; |
| 995 | } |
| 996 | if(minor > 0xfffff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 997 | ERROR("Minor %d out of range in device node %s, " |
| 998 | "truncating to %d\n", minor, filename, |
| 999 | minor & 0xfffff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1000 | minor &= 0xfffff; |
| 1001 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1002 | inode = get_inode(sizeof(*dev)); |
| 1003 | dev->nlink = nlink; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1004 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1005 | ((minor & ~0xff) << 12); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1006 | SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1007 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1008 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1009 | else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) { |
plougher | 0b4ee5b | 2010-12-31 20:14:00 +0000 | [diff] [blame] | 1010 | struct squashfs_ldev_inode_header *dev = &inode_header.ldev; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1011 | unsigned int major = major(buf->st_rdev); |
| 1012 | unsigned int minor = minor(buf->st_rdev); |
| 1013 | |
| 1014 | if(major > 0xfff) { |
| 1015 | ERROR("Major %d out of range in device node %s, " |
| 1016 | "truncating to %d\n", major, filename, |
| 1017 | major & 0xfff); |
| 1018 | major &= 0xfff; |
| 1019 | } |
| 1020 | if(minor > 0xfffff) { |
| 1021 | ERROR("Minor %d out of range in device node %s, " |
| 1022 | "truncating to %d\n", minor, filename, |
| 1023 | minor & 0xfffff); |
| 1024 | minor &= 0xfffff; |
| 1025 | } |
| 1026 | inode = get_inode(sizeof(*dev)); |
| 1027 | dev->nlink = nlink; |
| 1028 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1029 | ((minor & ~0xff) << 12); |
| 1030 | dev->xattr = xattr; |
| 1031 | SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode); |
| 1032 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
| 1033 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1034 | else if(type == SQUASHFS_SYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1035 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1036 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1037 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1038 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1039 | |
plougher | 5cf38b8 | 2010-12-16 04:55:32 +0000 | [diff] [blame] | 1040 | byte = readlink(filename, buff, 65536); |
| 1041 | if(byte == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1042 | ERROR_START("Failed to read symlink %s", filename); |
| 1043 | ERROR_EXIT(", creating empty symlink\n"); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1044 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | if(byte == 65536) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1048 | ERROR_START("Symlink %s is greater than 65536 bytes!", |
| 1049 | filename); |
| 1050 | ERROR_EXIT(" Creating empty symlink\n"); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 1051 | byte = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | inode = get_inode(sizeof(*symlink) + byte); |
| 1055 | symlink->nlink = nlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1056 | symlink->symlink_size = byte; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1057 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1058 | strncpy(inode + off, buff, byte); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1059 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1060 | nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1061 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1062 | else if(type == SQUASHFS_LSYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1063 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1064 | int byte; |
Phillip Lougher | 1df6c11 | 2012-12-12 05:21:42 +0000 | [diff] [blame] | 1065 | char buff[65536]; /* overflow safe */ |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1066 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1067 | |
plougher | df2b9aa | 2010-12-16 04:56:23 +0000 | [diff] [blame] | 1068 | byte = readlink(filename, buff, 65536); |
| 1069 | if(byte == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1070 | ERROR_START("Failed to read symlink %s", filename); |
| 1071 | ERROR_EXIT(", creating empty symlink\n"); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1072 | byte = 0; |
| 1073 | } |
| 1074 | |
| 1075 | if(byte == 65536) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 1076 | ERROR_START("Symlink %s is greater than 65536 bytes!", |
| 1077 | filename); |
| 1078 | ERROR_EXIT(" Creating empty symlink\n"); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1079 | byte = 0; |
| 1080 | } |
| 1081 | |
| 1082 | inode = get_inode(sizeof(*symlink) + byte + |
| 1083 | sizeof(unsigned int)); |
| 1084 | symlink->nlink = nlink; |
| 1085 | symlink->symlink_size = byte; |
| 1086 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
| 1087 | strncpy(inode + off, buff, byte); |
| 1088 | SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1); |
| 1089 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1090 | nlink); |
| 1091 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1092 | else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) { |
plougher | e56b986 | 2010-12-31 10:58:35 +0000 | [diff] [blame] | 1093 | struct squashfs_ipc_inode_header *ipc = &inode_header.ipc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1094 | |
| 1095 | inode = get_inode(sizeof(*ipc)); |
| 1096 | ipc->nlink = nlink; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1097 | SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1098 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1099 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1100 | } |
| 1101 | else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) { |
plougher | aa0d122 | 2010-12-31 20:06:24 +0000 | [diff] [blame] | 1102 | struct squashfs_lipc_inode_header *ipc = &inode_header.lipc; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1103 | |
| 1104 | inode = get_inode(sizeof(*ipc)); |
| 1105 | ipc->nlink = nlink; |
| 1106 | ipc->xattr = xattr; |
| 1107 | SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode); |
| 1108 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1109 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1110 | } else |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1111 | BAD_ERROR("Unrecognised inode %d in create_inode\n", type); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1112 | |
| 1113 | *i_no = MKINODE(inode); |
| 1114 | inode_count ++; |
| 1115 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1116 | TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type, |
| 1117 | base->uid, base->guid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1118 | |
| 1119 | return TRUE; |
| 1120 | } |
| 1121 | |
| 1122 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1123 | void add_dir(squashfs_inode inode, unsigned int inode_number, char *name, |
| 1124 | int type, struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1125 | { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 1126 | unsigned char *buff; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1127 | struct squashfs_dir_entry idir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1128 | unsigned int start_block = inode >> 16; |
| 1129 | unsigned int offset = inode & 0xffff; |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1130 | unsigned int size = strlen(name); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1131 | size_t name_off = offsetof(struct squashfs_dir_entry, name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1132 | |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1133 | if(size > SQUASHFS_NAME_LEN) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1134 | size = SQUASHFS_NAME_LEN; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1135 | ERROR("Filename is greater than %d characters, truncating! ..." |
| 1136 | "\n", SQUASHFS_NAME_LEN); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1139 | if(dir->p + sizeof(struct squashfs_dir_entry) + size + |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1140 | sizeof(struct squashfs_dir_header) |
| 1141 | >= dir->buff + dir->size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1142 | buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1143 | if(buff == NULL) |
| 1144 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1145 | |
| 1146 | dir->p = (dir->p - dir->buff) + buff; |
| 1147 | if(dir->entry_count_p) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1148 | dir->entry_count_p = (dir->entry_count_p - dir->buff + |
| 1149 | buff); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1150 | dir->index_count_p = dir->index_count_p - dir->buff + buff; |
| 1151 | dir->buff = buff; |
| 1152 | } |
| 1153 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1154 | if(dir->entry_count == 256 || start_block != dir->start_block || |
| 1155 | ((dir->entry_count_p != NULL) && |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1156 | ((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1157 | dir->index_count_p) > SQUASHFS_METADATA_SIZE)) || |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1158 | ((long long) inode_number - dir->inode_number) > 32767 |
| 1159 | || ((long long) inode_number - dir->inode_number) |
| 1160 | < -32768) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1161 | if(dir->entry_count_p) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1162 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1163 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1164 | if((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1165 | dir->index_count_p) > |
| 1166 | SQUASHFS_METADATA_SIZE) { |
| 1167 | if(dir->i_count % I_COUNT_SIZE == 0) { |
| 1168 | dir->index = realloc(dir->index, |
| 1169 | (dir->i_count + I_COUNT_SIZE) * |
| 1170 | sizeof(struct cached_dir_index)); |
| 1171 | if(dir->index == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1172 | MEM_ERROR(); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1173 | } |
| 1174 | dir->index[dir->i_count].index.index = |
| 1175 | dir->p - dir->buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1176 | dir->index[dir->i_count].index.size = size - 1; |
| 1177 | dir->index[dir->i_count++].name = name; |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1178 | dir->i_size += sizeof(struct squashfs_dir_index) |
| 1179 | + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1180 | dir->index_count_p = dir->p; |
| 1181 | } |
| 1182 | |
| 1183 | dir_header.count = dir->entry_count - 1; |
| 1184 | dir_header.start_block = dir->start_block; |
| 1185 | dir_header.inode_number = dir->inode_number; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1186 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1187 | dir->entry_count_p); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1188 | |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | dir->entry_count_p = dir->p; |
| 1193 | dir->start_block = start_block; |
| 1194 | dir->entry_count = 0; |
| 1195 | dir->inode_number = inode_number; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1196 | dir->p += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1199 | idir.offset = offset; |
| 1200 | idir.type = type; |
| 1201 | idir.size = size - 1; |
| 1202 | idir.inode_number = ((long long) inode_number - dir->inode_number); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1203 | SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p); |
| 1204 | strncpy((char *) dir->p + name_off, name, size); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1205 | dir->p += sizeof(struct squashfs_dir_entry) + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1206 | dir->entry_count ++; |
| 1207 | } |
| 1208 | |
| 1209 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1210 | void write_dir(squashfs_inode *inode, struct dir_info *dir_info, |
| 1211 | struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1212 | { |
| 1213 | unsigned int dir_size = dir->p - dir->buff; |
plougher | 4627ca3 | 2010-12-16 05:03:55 +0000 | [diff] [blame] | 1214 | int data_space = directory_cache_size - directory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1215 | unsigned int directory_block, directory_offset, i_count, index; |
| 1216 | unsigned short c_byte; |
| 1217 | |
| 1218 | if(data_space < dir_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1219 | int realloc_size = directory_cache_size == 0 ? |
| 1220 | ((dir_size + SQUASHFS_METADATA_SIZE) & |
| 1221 | ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1222 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1223 | void *dc = realloc(directory_data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1224 | directory_cache_size + realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1225 | if(dc == NULL) |
| 1226 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1227 | directory_cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1228 | directory_data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | if(dir_size) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1232 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1233 | |
| 1234 | dir_header.count = dir->entry_count - 1; |
| 1235 | dir_header.start_block = dir->start_block; |
| 1236 | dir_header.inode_number = dir->inode_number; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1237 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1238 | memcpy(directory_data_cache + directory_cache_bytes, dir->buff, |
| 1239 | dir_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1240 | } |
| 1241 | directory_offset = directory_cache_bytes; |
| 1242 | directory_block = directory_bytes; |
| 1243 | directory_cache_bytes += dir_size; |
| 1244 | i_count = 0; |
| 1245 | index = SQUASHFS_METADATA_SIZE - directory_offset; |
| 1246 | |
| 1247 | while(1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1248 | while(i_count < dir->i_count && |
| 1249 | dir->index[i_count].index.index < index) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1250 | dir->index[i_count++].index.start_block = |
| 1251 | directory_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1252 | index += SQUASHFS_METADATA_SIZE; |
| 1253 | |
| 1254 | if(directory_cache_bytes < SQUASHFS_METADATA_SIZE) |
| 1255 | break; |
| 1256 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1257 | if((directory_size - directory_bytes) < |
| 1258 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1259 | void *dt = realloc(directory_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1260 | directory_size + (SQUASHFS_METADATA_SIZE << 1) |
| 1261 | + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1262 | if(dt == NULL) |
| 1263 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1264 | directory_size += SQUASHFS_METADATA_SIZE << 1; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1265 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1268 | c_byte = mangle(directory_table + directory_bytes + |
| 1269 | BLOCK_OFFSET, directory_data_cache, |
| 1270 | SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, |
| 1271 | noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1272 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1273 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1274 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1275 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1276 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1277 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1278 | total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 1279 | memmove(directory_data_cache, directory_data_cache + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1280 | SQUASHFS_METADATA_SIZE, directory_cache_bytes - |
| 1281 | SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1282 | directory_cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 1283 | } |
| 1284 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1285 | create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE, |
| 1286 | dir_size + 3, directory_block, directory_offset, NULL, NULL, |
| 1287 | dir, 0); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1288 | |
| 1289 | #ifdef SQUASHFS_TRACE |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1290 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1291 | unsigned char *dirp; |
| 1292 | int count; |
| 1293 | |
| 1294 | TRACE("Directory contents of inode 0x%llx\n", *inode); |
| 1295 | dirp = dir->buff; |
| 1296 | while(dirp < dir->p) { |
| 1297 | char buffer[SQUASHFS_NAME_LEN + 1]; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1298 | struct squashfs_dir_entry idir, *idirp; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1299 | struct squashfs_dir_header dirh; |
| 1300 | SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1301 | &dirh); |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1302 | count = dirh.count + 1; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1303 | dirp += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1304 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1305 | TRACE("\tStart block 0x%x, count %d\n", |
| 1306 | dirh.start_block, count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1307 | |
| 1308 | while(count--) { |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1309 | idirp = (struct squashfs_dir_entry *) dirp; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1310 | SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1311 | strncpy(buffer, idirp->name, idir.size + 1); |
| 1312 | buffer[idir.size + 1] = '\0'; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1313 | TRACE("\t\tname %s, inode offset 0x%x, type " |
| 1314 | "%d\n", buffer, idir.offset, idir.type); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1315 | dirp += sizeof(struct squashfs_dir_entry) + idir.size + |
plougher | e8b26f6 | 2010-12-16 05:06:00 +0000 | [diff] [blame] | 1316 | 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | #endif |
| 1321 | dir_count ++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1325 | static struct file_buffer *get_fragment(struct fragment *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1326 | { |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1327 | struct squashfs_fragment_entry *disk_fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1328 | struct file_buffer *buffer, *compressed_buffer; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1329 | long long start_block; |
| 1330 | int res, size, index = fragment->index; |
| 1331 | char locked; |
| 1332 | |
| 1333 | /* |
| 1334 | * Lookup fragment block in cache. |
| 1335 | * If the fragment block doesn't exist, then get the compressed version |
| 1336 | * from the writer cache or off disk, and decompress it. |
| 1337 | * |
| 1338 | * This routine has two things which complicate the code: |
| 1339 | * |
| 1340 | * 1. Multiple threads can simultaneously lookup/create the |
| 1341 | * same buffer. This means a buffer needs to be "locked" |
| 1342 | * when it is being filled in, to prevent other threads from |
| 1343 | * using it when it is not ready. This is because we now do |
| 1344 | * fragment duplicate checking in parallel. |
| 1345 | * 2. We have two caches which need to be checked for the |
| 1346 | * presence of fragment blocks: the normal fragment cache |
| 1347 | * and a "reserve" cache. The reserve cache is used to |
| 1348 | * prevent an unnecessary pipeline stall when the fragment cache |
| 1349 | * is full of fragments waiting to be compressed. |
| 1350 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1351 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1352 | if(fragment->index == SQUASHFS_INVALID_FRAG) |
| 1353 | return NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1354 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1355 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1356 | pthread_mutex_lock(&dup_mutex); |
| 1357 | |
| 1358 | again: |
| 1359 | buffer = cache_lookup_nowait(fragment_buffer, index, &locked); |
| 1360 | if(buffer) { |
| 1361 | pthread_mutex_unlock(&dup_mutex); |
| 1362 | if(locked) |
| 1363 | /* got a buffer being filled in. Wait for it */ |
| 1364 | cache_wait_unlock(buffer); |
| 1365 | goto finished; |
| 1366 | } |
| 1367 | |
| 1368 | /* not in fragment cache, is it in the reserve cache? */ |
| 1369 | buffer = cache_lookup_nowait(reserve_cache, index, &locked); |
| 1370 | if(buffer) { |
| 1371 | pthread_mutex_unlock(&dup_mutex); |
| 1372 | if(locked) |
| 1373 | /* got a buffer being filled in. Wait for it */ |
| 1374 | cache_wait_unlock(buffer); |
| 1375 | goto finished; |
| 1376 | } |
| 1377 | |
| 1378 | /* in neither cache, try to get it from the fragment cache */ |
| 1379 | buffer = cache_get_nowait(fragment_buffer, index); |
| 1380 | if(!buffer) { |
| 1381 | /* |
| 1382 | * no room, get it from the reserve cache, this is |
| 1383 | * dimensioned so it will always have space (no more than |
| 1384 | * processors + 1 can have an outstanding reserve buffer) |
| 1385 | */ |
| 1386 | buffer = cache_get_nowait(reserve_cache, index); |
| 1387 | if(!buffer) { |
| 1388 | /* failsafe */ |
| 1389 | ERROR("no space in reserve cache\n"); |
| 1390 | goto again; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | pthread_mutex_unlock(&dup_mutex); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1395 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1396 | compressed_buffer = cache_lookup(fwriter_buffer, index); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1397 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1398 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1399 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1400 | disk_fragment = &fragment_table[index]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1401 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1402 | start_block = disk_fragment->start_block; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1403 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1404 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1405 | if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1406 | int error; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1407 | char *data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1408 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1409 | if(compressed_buffer) |
| 1410 | data = compressed_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1411 | else { |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 1412 | data = read_from_disk(start_block, size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1413 | if(data == NULL) { |
| 1414 | ERROR("Failed to read fragment from output" |
| 1415 | " filesystem\n"); |
| 1416 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1417 | } |
| 1418 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1419 | |
plougher | b48442b | 2010-12-31 07:57:54 +0000 | [diff] [blame] | 1420 | res = compressor_uncompress(comp, buffer->data, data, size, |
| 1421 | block_size, &error); |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 1422 | if(res == -1) |
| 1423 | BAD_ERROR("%s uncompress failed with error code %d\n", |
| 1424 | comp->name, error); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1425 | } else if(compressed_buffer) |
| 1426 | memcpy(buffer->data, compressed_buffer->data, size); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1427 | else { |
| 1428 | res = read_fs_bytes(fd, start_block, size, buffer->data); |
Phillip Lougher | 9451938 | 2013-03-06 02:43:42 +0000 | [diff] [blame] | 1429 | if(res == 0) { |
| 1430 | ERROR("Failed to read fragment from output " |
| 1431 | "filesystem\n"); |
| 1432 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1433 | } |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1434 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1435 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1436 | cache_unlock(buffer); |
plougher | 03859fa | 2009-03-31 03:18:18 +0000 | [diff] [blame] | 1437 | cache_block_put(compressed_buffer); |
| 1438 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1439 | finished: |
| 1440 | pthread_cleanup_pop(0); |
| 1441 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1442 | return buffer; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1445 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1446 | unsigned short get_fragment_checksum(struct file_info *file) |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1447 | { |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1448 | struct file_buffer *frag_buffer; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1449 | struct append_file *append; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1450 | int res, index = file->fragment->index; |
| 1451 | unsigned short checksum; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1452 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1453 | if(index == SQUASHFS_INVALID_FRAG) |
| 1454 | return 0; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1455 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1456 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1457 | pthread_mutex_lock(&dup_mutex); |
| 1458 | res = file->have_frag_checksum; |
| 1459 | checksum = file->fragment_checksum; |
Phillip Lougher | c4a8a79 | 2014-04-10 02:23:26 +0100 | [diff] [blame] | 1460 | pthread_cleanup_pop(1); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1461 | |
| 1462 | if(res) |
| 1463 | return checksum; |
| 1464 | |
| 1465 | frag_buffer = get_fragment(file->fragment); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1466 | |
Phillip Lougher | c4a8a79 | 2014-04-10 02:23:26 +0100 | [diff] [blame] | 1467 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1468 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1469 | for(append = file_mapping[index]; append; append = append->next) { |
| 1470 | int offset = append->file->fragment->offset; |
| 1471 | int size = append->file->fragment->size; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1472 | unsigned short cksum = |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1473 | get_checksum_mem(frag_buffer->data + offset, size); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1474 | |
| 1475 | if(file == append->file) |
| 1476 | checksum = cksum; |
| 1477 | |
| 1478 | pthread_mutex_lock(&dup_mutex); |
| 1479 | append->file->fragment_checksum = cksum; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1480 | append->file->have_frag_checksum = TRUE; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1481 | pthread_mutex_unlock(&dup_mutex); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | cache_block_put(frag_buffer); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1485 | pthread_cleanup_pop(0); |
| 1486 | |
| 1487 | return checksum; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1491 | void lock_fragments() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1492 | { |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1493 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1494 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1495 | fragments_locked = TRUE; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1496 | pthread_cleanup_pop(1); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | void unlock_fragments() |
| 1501 | { |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1502 | int frg, size; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1503 | struct file_buffer *write_buffer; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1504 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1505 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1506 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | 71ad964 | 2013-05-25 05:03:48 +0100 | [diff] [blame] | 1507 | |
| 1508 | /* |
| 1509 | * Note queue_empty() is inherently racy with respect to concurrent |
| 1510 | * queue get and pushes. We avoid this because we're holding the |
| 1511 | * fragment_mutex which ensures no other threads can be using the |
| 1512 | * queue at this time. |
| 1513 | */ |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1514 | while(!queue_empty(locked_fragment)) { |
| 1515 | write_buffer = queue_get(locked_fragment); |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1516 | frg = write_buffer->block; |
| 1517 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size); |
| 1518 | fragment_table[frg].start_block = bytes; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1519 | write_buffer->block = bytes; |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1520 | bytes += size; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1521 | fragments_outstanding --; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1522 | queue_put(to_writer, write_buffer); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1523 | TRACE("fragment_locked writing fragment %d, compressed size %d" |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1524 | "\n", frg, size); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1525 | } |
| 1526 | fragments_locked = FALSE; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1527 | pthread_cleanup_pop(1); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1530 | /* Called with the fragment_mutex locked */ |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1531 | void add_pending_fragment(struct file_buffer *write_buffer, int c_byte, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1532 | int fragment) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1533 | { |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1534 | fragment_table[fragment].size = c_byte; |
| 1535 | write_buffer->block = fragment; |
| 1536 | |
| 1537 | queue_put(locked_fragment, write_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1541 | void write_fragment(struct file_buffer *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1542 | { |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1543 | if(fragment == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1544 | return; |
| 1545 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1546 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1547 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1548 | fragment_table[fragment->block].unused = 0; |
| 1549 | fragments_outstanding ++; |
| 1550 | queue_put(to_frag, fragment); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1551 | pthread_cleanup_pop(1); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | |
| 1555 | struct file_buffer *allocate_fragment() |
| 1556 | { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 1557 | struct file_buffer *fragment = cache_get(fragment_buffer, fragments); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1558 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1559 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1560 | pthread_mutex_lock(&fragment_mutex); |
| 1561 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1562 | if(fragments % FRAG_SIZE == 0) { |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1563 | void *ft = realloc(fragment_table, (fragments + |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1564 | FRAG_SIZE) * sizeof(struct squashfs_fragment_entry)); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 1565 | if(ft == NULL) |
| 1566 | MEM_ERROR(); |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1567 | fragment_table = ft; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1568 | } |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1569 | |
| 1570 | fragment->size = 0; |
| 1571 | fragment->block = fragments ++; |
| 1572 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1573 | pthread_cleanup_pop(1); |
| 1574 | |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1575 | return fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 1578 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1579 | static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0}; |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 1580 | |
| 1581 | |
| 1582 | void free_fragment(struct fragment *fragment) |
| 1583 | { |
| 1584 | if(fragment != &empty_fragment) |
| 1585 | free(fragment); |
| 1586 | } |
| 1587 | |
| 1588 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1589 | struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer, |
| 1590 | struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1591 | { |
| 1592 | struct fragment *ffrg; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1593 | struct file_buffer **fragment; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1594 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1595 | if(file_buffer == NULL || file_buffer->size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1596 | return &empty_fragment; |
| 1597 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1598 | fragment = eval_frag_actions(dir_ent); |
| 1599 | |
| 1600 | if((*fragment) && (*fragment)->size + file_buffer->size > block_size) { |
| 1601 | write_fragment(*fragment); |
| 1602 | *fragment = NULL; |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1603 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1604 | |
plougher | e7e6e83 | 2010-12-16 05:08:06 +0000 | [diff] [blame] | 1605 | ffrg = malloc(sizeof(struct fragment)); |
| 1606 | if(ffrg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1607 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1608 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1609 | if(*fragment == NULL) |
| 1610 | *fragment = allocate_fragment(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1611 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1612 | ffrg->index = (*fragment)->block; |
| 1613 | ffrg->offset = (*fragment)->size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1614 | ffrg->size = file_buffer->size; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1615 | memcpy((*fragment)->data + (*fragment)->size, file_buffer->data, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1616 | file_buffer->size); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1617 | (*fragment)->size += file_buffer->size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1618 | |
| 1619 | return ffrg; |
| 1620 | } |
| 1621 | |
| 1622 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1623 | long long generic_write_table(int length, void *buffer, int length2, |
| 1624 | void *buffer2, int uncompressed) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1625 | { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1626 | int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) / |
| 1627 | SQUASHFS_METADATA_SIZE; |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1628 | long long *list, start_bytes; |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1629 | int compressed_size, i, list_size = meta_blocks * sizeof(long long); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1630 | unsigned short c_byte; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1631 | char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2]; |
| 1632 | |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1633 | #ifdef SQUASHFS_TRACE |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1634 | long long obytes = bytes; |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1635 | int olength = length; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1636 | #endif |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1637 | |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1638 | list = malloc(list_size); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1639 | if(list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1640 | MEM_ERROR(); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1641 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1642 | for(i = 0; i < meta_blocks; i++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1643 | int avail_bytes = length > SQUASHFS_METADATA_SIZE ? |
| 1644 | SQUASHFS_METADATA_SIZE : length; |
| 1645 | c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i * |
| 1646 | SQUASHFS_METADATA_SIZE , avail_bytes, |
| 1647 | SQUASHFS_METADATA_SIZE, uncompressed, 0); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1648 | SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1649 | list[i] = bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1650 | compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1651 | BLOCK_OFFSET; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1652 | TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes, |
| 1653 | compressed_size); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1654 | write_destination(fd, bytes, compressed_size, cbuffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1655 | bytes += compressed_size; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1656 | total_bytes += avail_bytes; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1657 | length -= avail_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1660 | start_bytes = bytes; |
| 1661 | if(length2) { |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1662 | write_destination(fd, bytes, length2, buffer2); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1663 | bytes += length2; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1664 | total_bytes += length2; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1667 | SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks); |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1668 | write_destination(fd, bytes, list_size, list); |
| 1669 | bytes += list_size; |
| 1670 | total_bytes += list_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1671 | |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1672 | TRACE("generic_write_table: total uncompressed %d compressed %lld\n", |
| 1673 | olength, bytes - obytes); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1674 | |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1675 | free(list); |
| 1676 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1677 | return start_bytes; |
| 1678 | } |
| 1679 | |
| 1680 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1681 | long long write_fragment_table() |
| 1682 | { |
| 1683 | unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1684 | int i; |
| 1685 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1686 | TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments, |
| 1687 | frag_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1688 | for(i = 0; i < fragments; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1689 | TRACE("write_fragment_table: fragment %d, start_block 0x%llx, " |
| 1690 | "size %d\n", i, fragment_table[i].start_block, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1691 | fragment_table[i].size); |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1692 | SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1695 | return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1699 | char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE]; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1700 | static char *read_from_disk(long long start, unsigned int avail_bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1701 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1702 | int res; |
| 1703 | |
| 1704 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1705 | if(res == 0) |
| 1706 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1707 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1708 | return read_from_file_buffer; |
| 1709 | } |
| 1710 | |
| 1711 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1712 | char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE]; |
| 1713 | char *read_from_disk2(long long start, unsigned int avail_bytes) |
| 1714 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1715 | int res; |
| 1716 | |
| 1717 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1718 | if(res == 0) |
| 1719 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1720 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1721 | return read_from_file_buffer2; |
| 1722 | } |
| 1723 | |
| 1724 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1725 | /* |
| 1726 | * Compute 16 bit BSD checksum over the data |
| 1727 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1728 | unsigned short get_checksum(char *buff, int bytes, unsigned short chksum) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1729 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1730 | unsigned char *b = (unsigned char *) buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1731 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1732 | while(bytes --) { |
| 1733 | chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1; |
| 1734 | chksum += *b++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | return chksum; |
| 1738 | } |
| 1739 | |
| 1740 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1741 | unsigned short get_checksum_disk(long long start, long long l, |
| 1742 | unsigned int *blocks) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1743 | { |
| 1744 | unsigned short chksum = 0; |
| 1745 | unsigned int bytes; |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1746 | struct file_buffer *write_buffer; |
| 1747 | int i; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1748 | |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1749 | for(i = 0; l; i++) { |
| 1750 | bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]); |
| 1751 | if(bytes == 0) /* sparse block */ |
| 1752 | continue; |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1753 | write_buffer = cache_lookup(bwriter_buffer, start); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1754 | if(write_buffer) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1755 | chksum = get_checksum(write_buffer->data, bytes, |
| 1756 | chksum); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1757 | cache_block_put(write_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1758 | } else { |
| 1759 | void *data = read_from_disk(start, bytes); |
| 1760 | if(data == NULL) { |
| 1761 | ERROR("Failed to checksum data from output" |
| 1762 | " filesystem\n"); |
| 1763 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1764 | } |
| 1765 | |
| 1766 | chksum = get_checksum(data, bytes, chksum); |
| 1767 | } |
| 1768 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1769 | l -= bytes; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1770 | start += bytes; |
| 1771 | } |
| 1772 | |
| 1773 | return chksum; |
| 1774 | } |
| 1775 | |
| 1776 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1777 | unsigned short get_checksum_mem(char *buff, int bytes) |
| 1778 | { |
| 1779 | return get_checksum(buff, bytes, 0); |
| 1780 | } |
| 1781 | |
| 1782 | |
| 1783 | unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer) |
| 1784 | { |
| 1785 | if(file_buffer == NULL) |
| 1786 | return 0; |
| 1787 | else |
| 1788 | return get_checksum(file_buffer->data, file_buffer->size, 0); |
| 1789 | } |
| 1790 | |
| 1791 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1792 | #define DUP_HASH(a) (a & 0xffff) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1793 | 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] | 1794 | unsigned int *block_listp, int blocks, unsigned int fragment, |
| 1795 | int offset, int bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1796 | { |
| 1797 | struct fragment *frg; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1798 | unsigned int *block_list = block_listp; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1799 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1800 | struct append_file *append_file; |
| 1801 | struct file_info *file; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1802 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1803 | if(!duplicate_checking || file_size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1804 | return; |
| 1805 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1806 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) { |
| 1807 | if(file_size != dupl_ptr->file_size) |
| 1808 | continue; |
| 1809 | if(blocks != 0 && start != dupl_ptr->start) |
| 1810 | continue; |
| 1811 | if(fragment != dupl_ptr->fragment->index) |
| 1812 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1813 | if(fragment != SQUASHFS_INVALID_FRAG && (offset != |
| 1814 | dupl_ptr->fragment->offset || bytes != |
| 1815 | dupl_ptr->fragment->size)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1816 | continue; |
| 1817 | return; |
| 1818 | } |
| 1819 | |
plougher | 3bb279c | 2010-12-16 05:10:03 +0000 | [diff] [blame] | 1820 | frg = malloc(sizeof(struct fragment)); |
| 1821 | if(frg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1822 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1823 | |
| 1824 | frg->index = fragment; |
| 1825 | frg->offset = offset; |
| 1826 | frg->size = bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1827 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1828 | 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] | 1829 | FALSE, FALSE); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1830 | |
| 1831 | if(fragment == SQUASHFS_INVALID_FRAG) |
| 1832 | return; |
| 1833 | |
| 1834 | append_file = malloc(sizeof(struct append_file)); |
| 1835 | if(append_file == NULL) |
| 1836 | MEM_ERROR(); |
| 1837 | |
| 1838 | append_file->file = file; |
| 1839 | append_file->next = file_mapping[fragment]; |
| 1840 | file_mapping[fragment] = append_file; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1841 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1842 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1843 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1844 | int pre_duplicate(long long file_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1845 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1846 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1847 | |
| 1848 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1849 | if(dupl_ptr->file_size == file_size) |
| 1850 | return TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1851 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1852 | return FALSE; |
| 1853 | } |
| 1854 | |
| 1855 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1856 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 1857 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 1858 | unsigned short checksum, unsigned short fragment_checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1859 | int checksum_flag, int checksum_frag_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1860 | { |
plougher | 51ef9ae | 2010-12-16 05:14:28 +0000 | [diff] [blame] | 1861 | struct file_info *dupl_ptr = malloc(sizeof(struct file_info)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1862 | |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1863 | if(dupl_ptr == NULL) |
| 1864 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1865 | |
| 1866 | dupl_ptr->file_size = file_size; |
| 1867 | dupl_ptr->bytes = bytes; |
| 1868 | dupl_ptr->block_list = block_list; |
| 1869 | dupl_ptr->start = start; |
| 1870 | dupl_ptr->fragment = fragment; |
| 1871 | dupl_ptr->checksum = checksum; |
| 1872 | dupl_ptr->fragment_checksum = fragment_checksum; |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1873 | dupl_ptr->have_frag_checksum = checksum_frag_flag; |
Phillip Lougher | 557cefa | 2014-02-05 04:36:05 +0000 | [diff] [blame] | 1874 | dupl_ptr->have_checksum = checksum_flag; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1875 | |
| 1876 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1877 | pthread_mutex_lock(&dup_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1878 | dupl_ptr->next = dupl[DUP_HASH(file_size)]; |
| 1879 | dupl[DUP_HASH(file_size)] = dupl_ptr; |
| 1880 | dup_files ++; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1881 | pthread_cleanup_pop(1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1882 | |
| 1883 | return dupl_ptr; |
| 1884 | } |
| 1885 | |
| 1886 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1887 | struct fragment *frag_duplicate(struct file_buffer *file_buffer, char *dont_put) |
| 1888 | { |
| 1889 | struct file_info *dupl_ptr; |
| 1890 | struct file_buffer *buffer; |
| 1891 | struct file_info *dupl_start = file_buffer->dupl_start; |
| 1892 | long long file_size = file_buffer->file_size; |
| 1893 | unsigned short checksum = file_buffer->checksum; |
| 1894 | int res; |
| 1895 | |
| 1896 | if(file_buffer->duplicate) { |
| 1897 | TRACE("Found duplicate file, fragment %d, size %d, offset %d, " |
| 1898 | "checksum 0x%x\n", dupl_start->fragment->index, |
| 1899 | file_size, dupl_start->fragment->offset, checksum); |
| 1900 | *dont_put = TRUE; |
| 1901 | return dupl_start->fragment; |
| 1902 | } else { |
| 1903 | *dont_put = FALSE; |
| 1904 | dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 1905 | } |
| 1906 | |
| 1907 | for(; dupl_ptr && dupl_ptr != dupl_start; dupl_ptr = dupl_ptr->next) { |
| 1908 | if(file_size == dupl_ptr->file_size && file_size == |
| 1909 | dupl_ptr->fragment->size) { |
| 1910 | if(get_fragment_checksum(dupl_ptr) == checksum) { |
| 1911 | buffer = get_fragment(dupl_ptr->fragment); |
| 1912 | res = memcmp(file_buffer->data, buffer->data + |
| 1913 | dupl_ptr->fragment->offset, file_size); |
| 1914 | cache_block_put(buffer); |
| 1915 | if(res == 0) |
| 1916 | break; |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | if(!dupl_ptr || dupl_ptr == dupl_start) |
| 1922 | return NULL; |
| 1923 | |
| 1924 | TRACE("Found duplicate file, fragment %d, size %d, offset %d, " |
| 1925 | "checksum 0x%x\n", dupl_ptr->fragment->index, file_size, |
| 1926 | dupl_ptr->fragment->offset, checksum); |
| 1927 | |
| 1928 | return dupl_ptr->fragment; |
| 1929 | } |
| 1930 | |
| 1931 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1932 | struct file_info *duplicate(long long file_size, long long bytes, |
| 1933 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 1934 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1935 | int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1936 | { |
| 1937 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 1938 | int frag_bytes = file_buffer ? file_buffer->size : 0; |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1939 | unsigned short fragment_checksum = file_buffer ? |
| 1940 | file_buffer->checksum : 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1941 | |
| 1942 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 1943 | if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes |
| 1944 | && frag_bytes == dupl_ptr->fragment->size) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1945 | long long target_start, dup_start = dupl_ptr->start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1946 | int block; |
| 1947 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1948 | if(memcmp(*block_list, dupl_ptr->block_list, blocks * |
| 1949 | sizeof(unsigned int)) != 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 1950 | continue; |
| 1951 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1952 | if(checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1953 | checksum = get_checksum_disk(*start, bytes, |
| 1954 | *block_list); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1955 | checksum_flag = TRUE; |
| 1956 | } |
| 1957 | |
Phillip Lougher | 557cefa | 2014-02-05 04:36:05 +0000 | [diff] [blame] | 1958 | if(!dupl_ptr->have_checksum) { |
| 1959 | dupl_ptr->checksum = |
| 1960 | get_checksum_disk(dupl_ptr->start, |
| 1961 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 1962 | dupl_ptr->have_checksum = TRUE; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1965 | if(checksum != dupl_ptr->checksum || |
| 1966 | fragment_checksum != |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1967 | get_fragment_checksum(dupl_ptr)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1968 | continue; |
| 1969 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1970 | target_start = *start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1971 | for(block = 0; block < blocks; block ++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1972 | int size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 1973 | ((*block_list)[block]); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1974 | struct file_buffer *target_buffer = NULL; |
| 1975 | struct file_buffer *dup_buffer = NULL; |
| 1976 | char *target_data, *dup_data; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1977 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1978 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1979 | if(size == 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 1980 | continue; |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1981 | target_buffer = cache_lookup(bwriter_buffer, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1982 | target_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1983 | if(target_buffer) |
| 1984 | target_data = target_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1985 | else { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1986 | target_data = |
| 1987 | read_from_disk(target_start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1988 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1989 | if(target_data == NULL) { |
| 1990 | ERROR("Failed to read data from" |
| 1991 | " output filesystem\n"); |
| 1992 | BAD_ERROR("Output filesystem" |
| 1993 | " corrupted?\n"); |
| 1994 | } |
| 1995 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1996 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1997 | dup_buffer = cache_lookup(bwriter_buffer, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1998 | dup_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1999 | if(dup_buffer) |
| 2000 | dup_data = dup_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 2001 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2002 | dup_data = read_from_disk2(dup_start, |
| 2003 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 2004 | if(dup_data == NULL) { |
| 2005 | ERROR("Failed to read data from" |
| 2006 | " output filesystem\n"); |
| 2007 | BAD_ERROR("Output filesystem" |
| 2008 | " corrupted?\n"); |
| 2009 | } |
| 2010 | } |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2011 | |
| 2012 | res = memcmp(target_data, dup_data, size); |
| 2013 | cache_block_put(target_buffer); |
| 2014 | cache_block_put(dup_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2015 | if(res != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2016 | break; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2017 | target_start += size; |
| 2018 | dup_start += size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2019 | } |
| 2020 | if(block == blocks) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2021 | struct file_buffer *frag_buffer = |
| 2022 | get_fragment(dupl_ptr->fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2023 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2024 | if(frag_bytes == 0 || |
| 2025 | memcmp(file_buffer->data, |
| 2026 | frag_buffer->data + |
| 2027 | dupl_ptr->fragment->offset, |
| 2028 | frag_bytes) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2029 | TRACE("Found duplicate file, start " |
| 2030 | "0x%llx, size %lld, checksum " |
| 2031 | "0x%x, fragment %d, size %d, " |
| 2032 | "offset %d, checksum 0x%x\n", |
| 2033 | dupl_ptr->start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2034 | dupl_ptr->bytes, |
| 2035 | dupl_ptr->checksum, |
| 2036 | dupl_ptr->fragment->index, |
| 2037 | frag_bytes, |
| 2038 | dupl_ptr->fragment->offset, |
| 2039 | fragment_checksum); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2040 | *block_list = dupl_ptr->block_list; |
| 2041 | *start = dupl_ptr->start; |
| 2042 | *fragment = dupl_ptr->fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2043 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2044 | return 0; |
| 2045 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2046 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2051 | return add_non_dup(file_size, bytes, *block_list, *start, *fragment, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2052 | checksum, fragment_checksum, checksum_flag, TRUE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2056 | inline int is_fragment(struct inode_info *inode) |
| 2057 | { |
| 2058 | int file_size = inode->buf.st_size; |
| 2059 | |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2060 | /* |
| 2061 | * If this block is to be compressed differently to the |
| 2062 | * fragment compression then it cannot be a fragment |
| 2063 | */ |
| 2064 | if(inode->noF != noF) |
| 2065 | return FALSE; |
| 2066 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2067 | return !inode->no_fragments && file_size && (file_size < block_size || |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2068 | (inode->always_use_fragments && file_size & (block_size - 1))); |
| 2069 | } |
| 2070 | |
| 2071 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2072 | void put_file_buffer(struct file_buffer *file_buffer) |
| 2073 | { |
| 2074 | /* |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 2075 | * Decide where to send the file buffer: |
| 2076 | * - compressible non-fragment blocks go to the deflate threads, |
| 2077 | * - fragments go to the process fragment threads, |
| 2078 | * - all others go directly to the main thread |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2079 | */ |
| 2080 | if(file_buffer->error) { |
| 2081 | file_buffer->fragment = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2082 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 2083 | } else if (file_buffer->file_size == 0) |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2084 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 2085 | else if(file_buffer->fragment) |
| 2086 | queue_put(to_process_frag, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2087 | else |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 2088 | queue_put(to_deflate, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2092 | static int seq = 0; |
| 2093 | void reader_read_process(struct dir_ent *dir_ent) |
| 2094 | { |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2095 | long long bytes = 0; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2096 | struct inode_info *inode = dir_ent->inode; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2097 | struct file_buffer *prev_buffer = NULL, *file_buffer; |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2098 | int status, byte, res, child; |
| 2099 | int file = pseudo_exec_file(get_pseudo_file(inode->pseudo_id), &child); |
| 2100 | |
| 2101 | if(!file) { |
| 2102 | file_buffer = cache_get_nohash(reader_buffer); |
| 2103 | file_buffer->sequence = seq ++; |
| 2104 | goto read_err; |
| 2105 | } |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2106 | |
| 2107 | while(1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2108 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2109 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2110 | file_buffer->noD = inode->noD; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2111 | |
| 2112 | byte = read_bytes(file, file_buffer->data, block_size); |
| 2113 | if(byte == -1) |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2114 | goto read_err2; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2115 | |
| 2116 | file_buffer->size = byte; |
| 2117 | file_buffer->file_size = -1; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2118 | file_buffer->error = FALSE; |
| 2119 | file_buffer->fragment = FALSE; |
| 2120 | bytes += byte; |
| 2121 | |
| 2122 | if(byte == 0) |
| 2123 | break; |
| 2124 | |
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 | * Update progress bar size. This is done |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2127 | * on every block rather than waiting for all blocks to be |
| 2128 | * read incase write_file_process() is running in parallel |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2129 | * with this. Otherwise the current progress bar position |
| 2130 | * may get ahead of the progress bar size. |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2131 | */ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2132 | progress_bar_size(1); |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2133 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2134 | if(prev_buffer) |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2135 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2136 | prev_buffer = file_buffer; |
| 2137 | } |
| 2138 | |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2139 | /* |
| 2140 | * Update inode file size now that the size of the dynamic pseudo file |
| 2141 | * is known. This is needed for the -info option. |
| 2142 | */ |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2143 | inode->buf.st_size = bytes; |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2144 | |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2145 | res = waitpid(child, &status, 0); |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2146 | close(file); |
| 2147 | |
plougher | d87d8d1 | 2009-09-10 04:08:16 +0000 | [diff] [blame] | 2148 | if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2149 | goto read_err; |
| 2150 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2151 | if(prev_buffer == NULL) |
| 2152 | prev_buffer = file_buffer; |
| 2153 | else { |
| 2154 | cache_block_put(file_buffer); |
| 2155 | seq --; |
| 2156 | } |
| 2157 | prev_buffer->file_size = bytes; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2158 | prev_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2159 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2160 | |
| 2161 | return; |
| 2162 | |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2163 | read_err2: |
| 2164 | close(file); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2165 | read_err: |
| 2166 | if(prev_buffer) { |
| 2167 | cache_block_put(file_buffer); |
| 2168 | seq --; |
| 2169 | file_buffer = prev_buffer; |
| 2170 | } |
| 2171 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2172 | put_file_buffer(file_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2176 | void reader_read_file(struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2177 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2178 | struct stat *buf = &dir_ent->inode->buf, buf2; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2179 | struct file_buffer *file_buffer; |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2180 | int blocks, file, res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2181 | long long bytes, read_size; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2182 | struct inode_info *inode = dir_ent->inode; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2183 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2184 | if(inode->read) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2185 | return; |
| 2186 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2187 | inode->read = TRUE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2188 | again: |
| 2189 | bytes = 0; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2190 | read_size = buf->st_size; |
| 2191 | blocks = (read_size + block_size - 1) >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2192 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2193 | file = open(pathname_reader(dir_ent), O_RDONLY); |
plougher | c1d258e | 2010-12-16 05:16:45 +0000 | [diff] [blame] | 2194 | if(file == -1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2195 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2196 | file_buffer->sequence = seq ++; |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2197 | goto read_err2; |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2198 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2199 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2200 | do { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2201 | file_buffer = cache_get_nohash(reader_buffer); |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2202 | file_buffer->file_size = read_size; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2203 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2204 | file_buffer->noD = inode->noD; |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2205 | file_buffer->error = FALSE; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2206 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2207 | /* |
| 2208 | * Always try to read block_size bytes from the file rather |
| 2209 | * than expected bytes (which will be less than the block_size |
| 2210 | * at the file tail) to check that the file hasn't grown |
| 2211 | * since being stated. If it is longer (or shorter) than |
| 2212 | * expected, then restat, and try again. Note the special |
| 2213 | * case where the file is an exact multiple of the block_size |
| 2214 | * is dealt with later. |
| 2215 | */ |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2216 | file_buffer->size = read_bytes(file, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2217 | block_size); |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2218 | if(file_buffer->size == -1) |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2219 | goto read_err; |
| 2220 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2221 | bytes += file_buffer->size; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2222 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2223 | if(blocks > 1) { |
| 2224 | /* non-tail block should be exactly block_size */ |
| 2225 | if(file_buffer->size < block_size) |
| 2226 | goto restat; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2227 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2228 | file_buffer->fragment = FALSE; |
| 2229 | put_file_buffer(file_buffer); |
| 2230 | } |
Phillip Lougher | 8678d30 | 2014-03-02 03:02:19 +0000 | [diff] [blame] | 2231 | } while(-- blocks > 0); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2232 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2233 | /* Overall size including tail should match */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2234 | if(read_size != bytes) |
| 2235 | goto restat; |
| 2236 | |
Phillip Lougher | 191dfcd | 2014-03-11 02:58:52 +0000 | [diff] [blame] | 2237 | if(read_size && read_size % block_size == 0) { |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2238 | /* |
| 2239 | * Special case where we've not tried to read past the end of |
| 2240 | * the file. We expect to get EOF, i.e. the file isn't larger |
| 2241 | * than we expect. |
| 2242 | */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2243 | char buffer; |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2244 | int res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2245 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2246 | res = read_bytes(file, &buffer, 1); |
| 2247 | if(res == -1) |
| 2248 | goto read_err; |
| 2249 | |
| 2250 | if(res != 0) |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2251 | goto restat; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2254 | file_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2255 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2256 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2257 | close(file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2258 | |
| 2259 | return; |
| 2260 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2261 | restat: |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2262 | res = fstat(file, &buf2); |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2263 | if(res == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 2264 | ERROR("Cannot stat dir/file %s because %s\n", |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2265 | pathname_reader(dir_ent), strerror(errno)); |
| 2266 | goto read_err; |
| 2267 | } |
| 2268 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2269 | if(read_size != buf2.st_size) { |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2270 | close(file); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2271 | memcpy(buf, &buf2, sizeof(struct stat)); |
| 2272 | file_buffer->error = 2; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2273 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2274 | goto again; |
| 2275 | } |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2276 | read_err: |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2277 | close(file); |
| 2278 | read_err2: |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2279 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2280 | put_file_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | |
| 2284 | void reader_scan(struct dir_info *dir) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2285 | struct dir_ent *dir_ent = dir->list; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2286 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2287 | for(; dir_ent; dir_ent = dir_ent->next) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2288 | struct stat *buf = &dir_ent->inode->buf; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2289 | if(dir_ent->inode->root_entry) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2290 | continue; |
| 2291 | |
plougher | b3977eb | 2010-05-02 02:08:48 +0000 | [diff] [blame] | 2292 | if(IS_PSEUDO_PROCESS(dir_ent->inode)) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2293 | reader_read_process(dir_ent); |
| 2294 | continue; |
| 2295 | } |
| 2296 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2297 | switch(buf->st_mode & S_IFMT) { |
| 2298 | case S_IFREG: |
| 2299 | reader_read_file(dir_ent); |
| 2300 | break; |
| 2301 | case S_IFDIR: |
| 2302 | reader_scan(dir_ent->dir); |
| 2303 | break; |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | |
| 2309 | void *reader(void *arg) |
| 2310 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2311 | if(!sorted) |
| 2312 | reader_scan(queue_get(to_reader)); |
| 2313 | else { |
| 2314 | int i; |
| 2315 | struct priority_entry *entry; |
| 2316 | |
| 2317 | queue_get(to_reader); |
| 2318 | for(i = 65535; i >= 0; i--) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2319 | for(entry = priority_list[i]; entry; |
| 2320 | entry = entry->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2321 | reader_read_file(entry->dir); |
| 2322 | } |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2323 | |
| 2324 | pthread_exit(NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | |
| 2328 | void *writer(void *arg) |
| 2329 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2330 | while(1) { |
| 2331 | struct file_buffer *file_buffer = queue_get(to_writer); |
| 2332 | off_t off; |
| 2333 | |
| 2334 | if(file_buffer == NULL) { |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2335 | queue_put(from_writer, NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2336 | continue; |
| 2337 | } |
| 2338 | |
| 2339 | off = file_buffer->block; |
| 2340 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2341 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2342 | pthread_mutex_lock(&pos_mutex); |
| 2343 | |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2344 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | d23000c | 2013-02-06 22:05:45 +0000 | [diff] [blame] | 2345 | ERROR("writer: Lseek on destination failed because " |
| 2346 | "%s, offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2347 | BAD_ERROR("Probably out of space on output " |
Phillip Lougher | 3f4ccd2 | 2013-02-11 04:40:09 +0000 | [diff] [blame] | 2348 | "%s\n", block_device ? "block device" : |
| 2349 | "filesystem"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2352 | if(write_bytes(fd, file_buffer->data, |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2353 | file_buffer->size) == -1) |
| 2354 | BAD_ERROR("Failed to write to output %s\n", |
Phillip Lougher | e9df455 | 2013-02-14 23:06:29 +0000 | [diff] [blame] | 2355 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2356 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2357 | pthread_cleanup_pop(1); |
| 2358 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2359 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2364 | int all_zero(struct file_buffer *file_buffer) |
| 2365 | { |
| 2366 | int i; |
| 2367 | long entries = file_buffer->size / sizeof(long); |
| 2368 | long *p = (long *) file_buffer->data; |
| 2369 | |
| 2370 | for(i = 0; i < entries && p[i] == 0; i++); |
| 2371 | |
| 2372 | if(i == entries) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2373 | for(i = file_buffer->size & ~(sizeof(long) - 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2374 | i < file_buffer->size && file_buffer->data[i] == 0; |
| 2375 | i++); |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2376 | |
| 2377 | return i == file_buffer->size; |
| 2378 | } |
| 2379 | |
| 2380 | return 0; |
| 2381 | } |
| 2382 | |
| 2383 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2384 | void *deflator(void *arg) |
| 2385 | { |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2386 | struct file_buffer *write_buffer = cache_get_nohash(bwriter_buffer); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2387 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2388 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2389 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2390 | res = compressor_init(comp, &stream, block_size, 1); |
| 2391 | if(res) |
| 2392 | BAD_ERROR("deflator:: compressor_init failed\n"); |
| 2393 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2394 | while(1) { |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 2395 | struct file_buffer *file_buffer = queue_get(to_deflate); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2396 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2397 | if(sparse_files && all_zero(file_buffer)) { |
Phillip Lougher | 4885438 | 2011-09-09 03:36:55 +0100 | [diff] [blame] | 2398 | file_buffer->c_byte = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2399 | seq_queue_put(to_main, file_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2400 | } else { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2401 | write_buffer->c_byte = mangle2(stream, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2402 | write_buffer->data, file_buffer->data, |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2403 | file_buffer->size, block_size, |
| 2404 | file_buffer->noD, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2405 | write_buffer->sequence = file_buffer->sequence; |
| 2406 | write_buffer->file_size = file_buffer->file_size; |
| 2407 | write_buffer->block = file_buffer->block; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2408 | write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2409 | (write_buffer->c_byte); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2410 | write_buffer->fragment = FALSE; |
| 2411 | write_buffer->error = FALSE; |
| 2412 | cache_block_put(file_buffer); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2413 | seq_queue_put(to_main, write_buffer); |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2414 | write_buffer = cache_get_nohash(bwriter_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2415 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | |
| 2420 | void *frag_deflator(void *arg) |
| 2421 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2422 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2423 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2424 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2425 | res = compressor_init(comp, &stream, block_size, 1); |
| 2426 | if(res) |
| 2427 | BAD_ERROR("frag_deflator:: compressor_init failed\n"); |
| 2428 | |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2429 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
| 2430 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2431 | while(1) { |
| 2432 | int c_byte, compressed_size; |
| 2433 | struct file_buffer *file_buffer = queue_get(to_frag); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2434 | struct file_buffer *write_buffer = |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2435 | cache_get(fwriter_buffer, file_buffer->block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2436 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2437 | c_byte = mangle2(stream, write_buffer->data, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2438 | file_buffer->size, block_size, noF, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2439 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte); |
plougher | d036a31 | 2008-03-08 01:38:27 +0000 | [diff] [blame] | 2440 | write_buffer->size = compressed_size; |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2441 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2442 | if(fragments_locked == FALSE) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2443 | fragment_table[file_buffer->block].size = c_byte; |
| 2444 | fragment_table[file_buffer->block].start_block = bytes; |
| 2445 | write_buffer->block = bytes; |
| 2446 | bytes += compressed_size; |
| 2447 | fragments_outstanding --; |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2448 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2449 | queue_put(to_writer, write_buffer); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2450 | TRACE("Writing fragment %lld, uncompressed size %d, " |
| 2451 | "compressed size %d\n", file_buffer->block, |
| 2452 | file_buffer->size, compressed_size); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2453 | } else { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2454 | add_pending_fragment(write_buffer, c_byte, |
| 2455 | file_buffer->block); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2456 | pthread_mutex_unlock(&fragment_mutex); |
| 2457 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2458 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2459 | } |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2460 | |
| 2461 | pthread_cleanup_pop(0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2465 | struct file_buffer *get_file_buffer() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2466 | { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2467 | struct file_buffer *file_buffer = seq_queue_get(to_main); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2468 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2469 | return file_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2473 | void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent, |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2474 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2475 | { |
| 2476 | file_count ++; |
| 2477 | *duplicate_file = FALSE; |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2478 | cache_block_put(file_buffer); |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2479 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0, |
| 2480 | NULL, &empty_fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | |
Phillip Lougher | 8531f87 | 2014-03-01 03:22:41 +0000 | [diff] [blame] | 2484 | void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2485 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2486 | { |
Phillip Lougher | 8531f87 | 2014-03-01 03:22:41 +0000 | [diff] [blame] | 2487 | int size = file_buffer->file_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2488 | struct fragment *fragment; |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2489 | unsigned short checksum = file_buffer->checksum; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2490 | char dont_put; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2491 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2492 | fragment = frag_duplicate(file_buffer, &dont_put); |
| 2493 | *duplicate_file = !fragment; |
| 2494 | if(!fragment) { |
| 2495 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
| 2496 | if(duplicate_checking) |
| 2497 | add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, |
| 2498 | TRUE, TRUE); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2499 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2500 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2501 | if(dont_put) |
| 2502 | free(file_buffer); |
| 2503 | else |
| 2504 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2505 | |
| 2506 | total_bytes += size; |
| 2507 | file_count ++; |
| 2508 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2509 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2510 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2511 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2512 | 0, NULL, fragment, NULL, 0); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2513 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2514 | if(!duplicate_checking) |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2515 | free_fragment(fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2519 | int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2520 | struct file_buffer *read_buffer, int *duplicate_file) |
| 2521 | { |
| 2522 | long long read_size, file_bytes, start; |
| 2523 | struct fragment *fragment; |
| 2524 | unsigned int *block_list = NULL; |
| 2525 | int block = 0, status; |
| 2526 | long long sparse = 0; |
| 2527 | struct file_buffer *fragment_buffer = NULL; |
| 2528 | |
| 2529 | *duplicate_file = FALSE; |
| 2530 | |
| 2531 | lock_fragments(); |
| 2532 | |
| 2533 | file_bytes = 0; |
| 2534 | start = bytes; |
| 2535 | while (1) { |
| 2536 | read_size = read_buffer->file_size; |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2537 | if(read_buffer->fragment) |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2538 | fragment_buffer = read_buffer; |
| 2539 | else { |
| 2540 | block_list = realloc(block_list, (block + 1) * |
| 2541 | sizeof(unsigned int)); |
| 2542 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2543 | MEM_ERROR(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2544 | block_list[block ++] = read_buffer->c_byte; |
| 2545 | if(read_buffer->c_byte) { |
| 2546 | read_buffer->block = bytes; |
| 2547 | bytes += read_buffer->size; |
Phillip Lougher | 84e20d7 | 2014-02-23 05:23:41 +0000 | [diff] [blame] | 2548 | cache_hash(read_buffer, read_buffer->block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2549 | file_bytes += read_buffer->size; |
| 2550 | queue_put(to_writer, read_buffer); |
| 2551 | } else { |
| 2552 | sparse += read_buffer->size; |
| 2553 | cache_block_put(read_buffer); |
| 2554 | } |
| 2555 | } |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2556 | inc_progress_bar(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2557 | |
| 2558 | if(read_size != -1) |
| 2559 | break; |
| 2560 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2561 | read_buffer = get_file_buffer(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2562 | if(read_buffer->error) |
| 2563 | goto read_err; |
| 2564 | } |
| 2565 | |
| 2566 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2567 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2568 | |
| 2569 | if(duplicate_checking) |
| 2570 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2571 | 0, fragment_buffer ? fragment_buffer->checksum : 0, |
| 2572 | FALSE, TRUE); |
| 2573 | cache_block_put(fragment_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2574 | file_count ++; |
| 2575 | total_bytes += read_size; |
| 2576 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2577 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2578 | block, block_list, fragment, NULL, sparse); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2579 | |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2580 | if(duplicate_checking == FALSE) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2581 | free(block_list); |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2582 | free_fragment(fragment); |
| 2583 | } |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2584 | |
| 2585 | return 0; |
| 2586 | |
| 2587 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2588 | dec_progress_bar(block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2589 | status = read_buffer->error; |
| 2590 | bytes = start; |
| 2591 | if(!block_device) { |
| 2592 | int res; |
| 2593 | |
| 2594 | queue_put(to_writer, NULL); |
| 2595 | if(queue_get(from_writer) != 0) |
| 2596 | EXIT_MKSQUASHFS(); |
| 2597 | res = ftruncate(fd, bytes); |
| 2598 | if(res != 0) |
| 2599 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2600 | strerror(errno)); |
| 2601 | } |
| 2602 | unlock_fragments(); |
| 2603 | free(block_list); |
| 2604 | cache_block_put(read_buffer); |
| 2605 | return status; |
| 2606 | } |
| 2607 | |
| 2608 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2609 | 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] | 2610 | struct file_buffer *read_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2611 | { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2612 | int block, thresh; |
Phillip Lougher | 1807caa | 2014-03-01 03:28:33 +0000 | [diff] [blame] | 2613 | long long read_size = read_buffer->file_size; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2614 | long long file_bytes, dup_start, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2615 | struct fragment *fragment; |
| 2616 | struct file_info *dupl_ptr; |
| 2617 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2618 | unsigned int *block_list, *block_listp; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2619 | struct file_buffer **buffer_list; |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2620 | int status; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2621 | long long sparse = 0; |
| 2622 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2623 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2624 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2625 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2626 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2627 | block_listp = block_list; |
| 2628 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2629 | buffer_list = malloc(blocks * sizeof(struct file_buffer *)); |
| 2630 | if(buffer_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2631 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2632 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2633 | lock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2634 | |
| 2635 | file_bytes = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2636 | start = dup_start = bytes; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 2637 | thresh = blocks > bwriter_size ? blocks - bwriter_size : 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2638 | |
| 2639 | for(block = 0; block < blocks;) { |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2640 | if(read_buffer->fragment) { |
Phillip Lougher | 38cb1ab | 2013-01-13 21:20:09 +0000 | [diff] [blame] | 2641 | block_list[block] = 0; |
Phillip Lougher | 19de5b6 | 2013-01-13 21:31:51 +0000 | [diff] [blame] | 2642 | buffer_list[block] = NULL; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2643 | fragment_buffer = read_buffer; |
| 2644 | blocks = read_size >> block_log; |
| 2645 | } else { |
| 2646 | block_list[block] = read_buffer->c_byte; |
| 2647 | |
| 2648 | if(read_buffer->c_byte) { |
| 2649 | read_buffer->block = bytes; |
| 2650 | bytes += read_buffer->size; |
| 2651 | file_bytes += read_buffer->size; |
Phillip Lougher | 84e20d7 | 2014-02-23 05:23:41 +0000 | [diff] [blame] | 2652 | cache_hash(read_buffer, read_buffer->block); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2653 | if(block < thresh) { |
| 2654 | buffer_list[block] = NULL; |
| 2655 | queue_put(to_writer, read_buffer); |
| 2656 | } else |
| 2657 | buffer_list[block] = read_buffer; |
| 2658 | } else { |
| 2659 | buffer_list[block] = NULL; |
| 2660 | sparse += read_buffer->size; |
| 2661 | cache_block_put(read_buffer); |
| 2662 | } |
| 2663 | } |
| 2664 | inc_progress_bar(); |
| 2665 | |
| 2666 | if(++block < blocks) { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2667 | read_buffer = get_file_buffer(); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2668 | if(read_buffer->error) |
| 2669 | goto read_err; |
| 2670 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2673 | dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2674 | &fragment, fragment_buffer, blocks, 0, FALSE); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2675 | |
| 2676 | if(dupl_ptr) { |
| 2677 | *duplicate_file = FALSE; |
| 2678 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2679 | if(buffer_list[block]) |
| 2680 | queue_put(to_writer, buffer_list[block]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2681 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2682 | dupl_ptr->fragment = fragment; |
| 2683 | } else { |
| 2684 | *duplicate_file = TRUE; |
| 2685 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2686 | cache_block_put(buffer_list[block]); |
| 2687 | bytes = start; |
| 2688 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2689 | int res; |
| 2690 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2691 | queue_put(to_writer, NULL); |
| 2692 | if(queue_get(from_writer) != 0) |
| 2693 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2694 | res = ftruncate(fd, bytes); |
| 2695 | if(res != 0) |
| 2696 | BAD_ERROR("Failed to truncate dest file because" |
| 2697 | " %s\n", strerror(errno)); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2698 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2701 | unlock_fragments(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2702 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2703 | free(buffer_list); |
| 2704 | file_count ++; |
| 2705 | total_bytes += read_size; |
| 2706 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2707 | /* |
| 2708 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2709 | * a smaller block count on stat calls to sparse files. This is |
| 2710 | * to ensure intelligent applications like cp correctly handle the |
| 2711 | * file as a sparse file. If the file in the original filesystem isn't |
| 2712 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2713 | * report it as non-sparse on stat calls to preserve semantics |
| 2714 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2715 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2716 | sparse = 0; |
| 2717 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2718 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, |
| 2719 | dup_start, blocks, block_listp, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2720 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2721 | if(*duplicate_file == TRUE) |
| 2722 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2723 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2724 | return 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2725 | |
| 2726 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2727 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2728 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2729 | bytes = start; |
| 2730 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2731 | int res; |
| 2732 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2733 | queue_put(to_writer, NULL); |
| 2734 | if(queue_get(from_writer) != 0) |
| 2735 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2736 | res = ftruncate(fd, bytes); |
| 2737 | if(res != 0) |
| 2738 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2739 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2740 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2741 | unlock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2742 | for(blocks = thresh; blocks < block; blocks ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2743 | cache_block_put(buffer_list[blocks]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2744 | free(buffer_list); |
| 2745 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2746 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2747 | return status; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2751 | int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2752 | struct file_buffer *read_buffer, int *dup) |
| 2753 | { |
| 2754 | long long read_size = read_buffer->file_size; |
| 2755 | long long file_bytes, start; |
| 2756 | struct fragment *fragment; |
| 2757 | unsigned int *block_list; |
| 2758 | int block, status; |
| 2759 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2760 | long long sparse = 0; |
| 2761 | struct file_buffer *fragment_buffer = NULL; |
| 2762 | |
| 2763 | if(pre_duplicate(read_size)) |
| 2764 | return write_file_blocks_dup(inode, dir_ent, read_buffer, dup); |
| 2765 | |
| 2766 | *dup = FALSE; |
| 2767 | |
| 2768 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2769 | if(block_list == NULL) |
| 2770 | MEM_ERROR(); |
| 2771 | |
| 2772 | lock_fragments(); |
| 2773 | |
| 2774 | file_bytes = 0; |
| 2775 | start = bytes; |
| 2776 | for(block = 0; block < blocks;) { |
| 2777 | if(read_buffer->fragment) { |
| 2778 | block_list[block] = 0; |
| 2779 | fragment_buffer = read_buffer; |
| 2780 | blocks = read_size >> block_log; |
| 2781 | } else { |
| 2782 | block_list[block] = read_buffer->c_byte; |
| 2783 | if(read_buffer->c_byte) { |
| 2784 | read_buffer->block = bytes; |
| 2785 | bytes += read_buffer->size; |
| 2786 | cache_hash(read_buffer, read_buffer->block); |
| 2787 | file_bytes += read_buffer->size; |
| 2788 | queue_put(to_writer, read_buffer); |
| 2789 | } else { |
| 2790 | sparse += read_buffer->size; |
| 2791 | cache_block_put(read_buffer); |
| 2792 | } |
| 2793 | } |
| 2794 | inc_progress_bar(); |
| 2795 | |
| 2796 | if(++block < blocks) { |
| 2797 | read_buffer = get_file_buffer(); |
| 2798 | if(read_buffer->error) |
| 2799 | goto read_err; |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | unlock_fragments(); |
| 2804 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2805 | |
| 2806 | if(duplicate_checking) |
| 2807 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2808 | 0, fragment_buffer ? fragment_buffer->checksum : 0, |
| 2809 | FALSE, TRUE); |
| 2810 | cache_block_put(fragment_buffer); |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2811 | file_count ++; |
| 2812 | total_bytes += read_size; |
| 2813 | |
| 2814 | /* |
| 2815 | * sparse count is needed to ensure squashfs correctly reports a |
| 2816 | * a smaller block count on stat calls to sparse files. This is |
| 2817 | * to ensure intelligent applications like cp correctly handle the |
| 2818 | * file as a sparse file. If the file in the original filesystem isn't |
| 2819 | * stored as a sparse file then still store it sparsely in squashfs, but |
| 2820 | * report it as non-sparse on stat calls to preserve semantics |
| 2821 | */ |
| 2822 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2823 | sparse = 0; |
| 2824 | |
| 2825 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2826 | blocks, block_list, fragment, NULL, sparse); |
| 2827 | |
| 2828 | if(duplicate_checking == FALSE) { |
| 2829 | free(block_list); |
| 2830 | free_fragment(fragment); |
| 2831 | } |
| 2832 | |
| 2833 | return 0; |
| 2834 | |
| 2835 | read_err: |
| 2836 | dec_progress_bar(block); |
| 2837 | status = read_buffer->error; |
| 2838 | bytes = start; |
| 2839 | if(!block_device) { |
| 2840 | int res; |
| 2841 | |
| 2842 | queue_put(to_writer, NULL); |
| 2843 | if(queue_get(from_writer) != 0) |
| 2844 | EXIT_MKSQUASHFS(); |
| 2845 | res = ftruncate(fd, bytes); |
| 2846 | if(res != 0) |
| 2847 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2848 | strerror(errno)); |
| 2849 | } |
| 2850 | unlock_fragments(); |
| 2851 | free(block_list); |
| 2852 | cache_block_put(read_buffer); |
| 2853 | return status; |
| 2854 | } |
| 2855 | |
| 2856 | |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2857 | void write_file(squashfs_inode *inode, struct dir_ent *dir, int *dup) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2858 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2859 | int status; |
| 2860 | struct file_buffer *read_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2861 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2862 | again: |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2863 | read_buffer = get_file_buffer(); |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2864 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2865 | |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2866 | if(status) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2867 | cache_block_put(read_buffer); |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2868 | else if(read_buffer->file_size == -1) |
| 2869 | status = write_file_process(inode, dir, read_buffer, dup); |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2870 | else if(read_buffer->file_size == 0) |
| 2871 | write_file_empty(inode, dir, read_buffer, dup); |
| 2872 | else if(read_buffer->fragment && read_buffer->c_byte) |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2873 | write_file_frag(inode, dir, read_buffer, dup); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2874 | else |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2875 | status = write_file_blocks(inode, dir, read_buffer, dup); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2876 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2877 | if(status == 2) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2878 | ERROR("File %s changed size while reading filesystem, " |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2879 | "attempting to re-read\n", pathname(dir)); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2880 | goto again; |
| 2881 | } else if(status == 1) { |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2882 | ERROR_START("Failed to read file %s", pathname(dir)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 2883 | ERROR_EXIT(", creating empty file\n"); |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2884 | write_file_empty(inode, dir, NULL, dup); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2885 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2889 | #define BUFF_SIZE 512 |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2890 | char *name; |
| 2891 | char *basename_r(); |
| 2892 | |
| 2893 | char *getbase(char *pathname) |
| 2894 | { |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2895 | static char *b_buffer = NULL; |
| 2896 | static int b_size = BUFF_SIZE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2897 | char *result; |
| 2898 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2899 | if(b_buffer == NULL) { |
| 2900 | b_buffer = malloc(b_size); |
| 2901 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2902 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | while(1) { |
| 2906 | if(*pathname != '/') { |
| 2907 | result = getcwd(b_buffer, b_size); |
| 2908 | if(result == NULL && errno != ERANGE) |
| 2909 | BAD_ERROR("Getcwd failed in getbase\n"); |
| 2910 | |
| 2911 | /* enough room for pathname + "/" + '\0' terminator? */ |
| 2912 | if(result && strlen(pathname) + 2 <= |
| 2913 | b_size - strlen(b_buffer)) { |
| 2914 | strcat(strcat(b_buffer, "/"), pathname); |
| 2915 | break; |
| 2916 | } |
| 2917 | } else if(strlen(pathname) < b_size) { |
| 2918 | strcpy(b_buffer, pathname); |
| 2919 | break; |
| 2920 | } |
| 2921 | |
| 2922 | /* Buffer not large enough, realloc and try again */ |
| 2923 | b_buffer = realloc(b_buffer, b_size += BUFF_SIZE); |
| 2924 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2925 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2928 | name = b_buffer; |
| 2929 | if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0)) |
| 2930 | return NULL; |
| 2931 | else |
| 2932 | return result; |
| 2933 | } |
| 2934 | |
| 2935 | |
| 2936 | char *basename_r() |
| 2937 | { |
| 2938 | char *s; |
| 2939 | char *p; |
| 2940 | int n = 1; |
| 2941 | |
| 2942 | for(;;) { |
| 2943 | s = name; |
| 2944 | if(*name == '\0') |
| 2945 | return NULL; |
| 2946 | if(*name != '/') { |
| 2947 | while(*name != '\0' && *name != '/') name++; |
| 2948 | n = name - s; |
| 2949 | } |
| 2950 | while(*name == '/') name++; |
| 2951 | if(strncmp(s, ".", n) == 0) |
| 2952 | continue; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2953 | if((*name == '\0') || (strncmp(s, "..", n) == 0) || |
| 2954 | ((p = basename_r()) == NULL)) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2955 | s[n] = '\0'; |
| 2956 | return s; |
| 2957 | } |
| 2958 | if(strcmp(p, "..") == 0) |
| 2959 | continue; |
| 2960 | return p; |
| 2961 | } |
| 2962 | } |
| 2963 | |
| 2964 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2965 | struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2966 | { |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2967 | int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino); |
| 2968 | struct inode_info *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2969 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 2970 | /* |
| 2971 | * Look-up inode in hash table, if it already exists we have a |
| 2972 | * hard-link, so increment the nlink count and return it. |
| 2973 | * Don't do the look-up for directories because we don't hard-link |
| 2974 | * directories. |
| 2975 | */ |
| 2976 | if ((buf->st_mode & S_IFMT) != S_IFDIR) { |
| 2977 | for(inode = inode_info[ino_hash]; inode; inode = inode->next) { |
| 2978 | if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) { |
| 2979 | inode->nlink ++; |
| 2980 | return inode; |
| 2981 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2982 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
plougher | 288f685 | 2010-12-16 05:22:55 +0000 | [diff] [blame] | 2985 | inode = malloc(sizeof(struct inode_info)); |
| 2986 | if(inode == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2987 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2988 | |
| 2989 | memcpy(&inode->buf, buf, sizeof(struct stat)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2990 | inode->read = FALSE; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2991 | inode->root_entry = FALSE; |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 2992 | inode->pseudo_file = pseudo; |
| 2993 | inode->pseudo_id = id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2994 | inode->inode = SQUASHFS_INVALID_BLK; |
| 2995 | inode->nlink = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 2996 | inode->inode_number = 0; |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 2997 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2998 | /* |
| 2999 | * Copy filesystem wide defaults into inode, these filesystem |
| 3000 | * wide defaults may be altered on an individual inode basis by |
| 3001 | * user specified actions |
| 3002 | * |
| 3003 | */ |
| 3004 | inode->no_fragments = no_fragments; |
| 3005 | inode->always_use_fragments = always_use_fragments; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 3006 | inode->noD = noD; |
| 3007 | inode->noF = noF; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 3008 | |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3009 | if((buf->st_mode & S_IFMT) == S_IFREG) |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 3010 | progress_bar_size((buf->st_size + block_size - 1) >> block_log); |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3011 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3012 | inode->next = inode_info[ino_hash]; |
| 3013 | inode_info[ino_hash] = inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3014 | |
| 3015 | return inode; |
| 3016 | } |
| 3017 | |
| 3018 | |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3019 | inline struct inode_info *lookup_inode(struct stat *buf) |
| 3020 | { |
| 3021 | return lookup_inode2(buf, 0, 0); |
| 3022 | } |
| 3023 | |
| 3024 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3025 | inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this) |
| 3026 | { |
| 3027 | if (inode->inode_number == 0) |
| 3028 | inode->inode_number = use_this ? : inode_no ++; |
| 3029 | } |
| 3030 | |
| 3031 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3032 | inline struct dir_ent *create_dir_entry(char *name, char *source_name, |
| 3033 | char *nonstandard_pathname, struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3034 | { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3035 | struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent)); |
| 3036 | if(dir_ent == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3037 | MEM_ERROR(); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3038 | |
| 3039 | dir_ent->name = name; |
| 3040 | dir_ent->source_name = source_name; |
| 3041 | dir_ent->nonstandard_pathname = nonstandard_pathname; |
| 3042 | dir_ent->our_dir = dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3043 | dir_ent->next = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3044 | |
| 3045 | return dir_ent; |
| 3046 | } |
| 3047 | |
| 3048 | |
| 3049 | inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir, |
| 3050 | struct inode_info *inode_info) |
| 3051 | { |
| 3052 | struct dir_info *dir = dir_ent->our_dir; |
| 3053 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3054 | if(sub_dir) |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3055 | sub_dir->dir_ent = dir_ent; |
| 3056 | dir_ent->inode = inode_info; |
| 3057 | dir_ent->dir = sub_dir; |
| 3058 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3059 | dir_ent->next = dir->list; |
| 3060 | dir->list = dir_ent; |
| 3061 | dir->count++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | |
| 3065 | inline void add_dir_entry2(char *name, char *source_name, |
| 3066 | char *nonstandard_pathname, struct dir_info *sub_dir, |
| 3067 | struct inode_info *inode_info, struct dir_info *dir) |
| 3068 | { |
| 3069 | struct dir_ent *dir_ent = create_dir_entry(name, source_name, |
| 3070 | nonstandard_pathname, dir); |
| 3071 | |
| 3072 | |
| 3073 | add_dir_entry(dir_ent, sub_dir, inode_info); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3077 | inline void free_dir_entry(struct dir_ent *dir_ent) |
| 3078 | { |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3079 | if(dir_ent->name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3080 | free(dir_ent->name); |
| 3081 | |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3082 | if(dir_ent->source_name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3083 | free(dir_ent->source_name); |
| 3084 | |
| 3085 | free(dir_ent); |
| 3086 | } |
| 3087 | |
| 3088 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3089 | inline void add_excluded(struct dir_info *dir) |
| 3090 | { |
| 3091 | dir->excluded ++; |
| 3092 | } |
| 3093 | |
| 3094 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3095 | void dir_scan(squashfs_inode *inode, char *pathname, |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 3096 | struct dir_ent *(_readdir)(struct dir_info *), int progress) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3097 | { |
| 3098 | struct stat buf; |
| 3099 | struct dir_info *dir_info = dir_scan1(pathname, "", paths, _readdir, 1); |
| 3100 | struct dir_ent *dir_ent; |
| 3101 | |
| 3102 | if(dir_info == NULL) |
| 3103 | return; |
| 3104 | |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3105 | /* |
| 3106 | * Process most actions and any pseudo files |
| 3107 | */ |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 3108 | if(actions() || get_pseudo()) |
| 3109 | dir_scan2(dir_info, get_pseudo()); |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3110 | |
Phillip Lougher | e92cb51 | 2012-11-15 02:22:28 +0000 | [diff] [blame] | 3111 | /* |
| 3112 | * Process move actions |
| 3113 | */ |
| 3114 | if(move_actions()) { |
| 3115 | dir_scan3(dir_info, dir_info); |
| 3116 | do_move_actions(); |
| 3117 | } |
| 3118 | |
Phillip Lougher | 71ae14e | 2012-11-15 02:27:50 +0000 | [diff] [blame] | 3119 | /* |
| 3120 | * Process empty actions |
| 3121 | */ |
| 3122 | if(empty_actions()) |
| 3123 | dir_scan4(dir_info); |
| 3124 | |
Phillip Lougher | eb69dad | 2012-11-15 03:34:02 +0000 | [diff] [blame] | 3125 | /* |
| 3126 | * Sort directories and compute the inode numbers |
| 3127 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3128 | dir_scan5(dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3129 | |
Phillip Lougher | d0fe1d5 | 2012-10-09 03:34:34 +0100 | [diff] [blame] | 3130 | dir_ent = create_dir_entry("", NULL, pathname, |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 3131 | scan1_opendir("", "", 0)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3132 | |
| 3133 | if(pathname[0] == '\0') { |
| 3134 | /* |
| 3135 | * dummy top level directory, if multiple sources specified on |
| 3136 | * command line |
| 3137 | */ |
| 3138 | memset(&buf, 0, sizeof(buf)); |
| 3139 | buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR; |
| 3140 | buf.st_uid = getuid(); |
| 3141 | buf.st_gid = getgid(); |
| 3142 | buf.st_mtime = time(NULL); |
| 3143 | buf.st_dev = 0; |
| 3144 | buf.st_ino = 0; |
| 3145 | dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0); |
| 3146 | } else { |
Phillip Lougher | 38a35ec | 2012-11-30 04:31:35 +0000 | [diff] [blame] | 3147 | if(lstat(pathname, &buf) == -1) |
| 3148 | /* source directory has disappeared? */ |
Phillip Lougher | fe58b49 | 2012-12-15 01:27:07 +0000 | [diff] [blame] | 3149 | BAD_ERROR("Cannot stat source directory %s because %s\n", |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3150 | pathname, strerror(errno)); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3151 | dir_ent->inode = lookup_inode(&buf); |
| 3152 | } |
| 3153 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3154 | alloc_inode_no(dir_ent->inode, root_inode_number); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3155 | dir_ent->dir = dir_info; |
| 3156 | dir_info->dir_ent = dir_ent; |
| 3157 | |
Andy Lutomirski | 3f31dcf | 2012-09-21 18:08:08 -0700 | [diff] [blame] | 3158 | eval_actions(dir_ent); |
| 3159 | |
Phillip Lougher | 19b877d | 2013-02-23 11:15:38 +0000 | [diff] [blame] | 3160 | if(sorted) |
| 3161 | generate_file_priorities(dir_info, 0, |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3162 | &dir_info->dir_ent->inode->buf); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 3163 | |
| 3164 | if(appending) { |
| 3165 | sigset_t sigmask; |
| 3166 | |
| 3167 | restore_thread = init_restore_thread(); |
| 3168 | sigemptyset(&sigmask); |
| 3169 | sigaddset(&sigmask, SIGINT); |
| 3170 | sigaddset(&sigmask, SIGTERM); |
| 3171 | sigaddset(&sigmask, SIGUSR1); |
| 3172 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
| 3173 | BAD_ERROR("Failed to set signal mask\n"); |
| 3174 | write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0"); |
| 3175 | } |
| 3176 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3177 | queue_put(to_reader, dir_info); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 3178 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3179 | if(sorted) |
| 3180 | sort_files_and_write(dir_info); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 3181 | |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 3182 | set_progressbar_state(progress); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3183 | dir_scan6(inode, dir_info); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3184 | dir_ent->inode->inode = *inode; |
| 3185 | dir_ent->inode->type = SQUASHFS_DIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3189 | /* |
| 3190 | * dir_scan1 routines... |
Phillip Lougher | e8630be | 2012-11-11 02:24:24 +0000 | [diff] [blame] | 3191 | * These scan the source directories into memory for processing. |
| 3192 | * Exclude actions are processed here (in contrast to the other actions) |
| 3193 | * because they affect what is scanned. |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3194 | */ |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3195 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3196 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3197 | struct dir_info *dir; |
| 3198 | |
plougher | 6da792b | 2010-12-16 05:25:39 +0000 | [diff] [blame] | 3199 | dir = malloc(sizeof(struct dir_info)); |
| 3200 | if(dir == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3201 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3202 | |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3203 | if(pathname[0] != '\0') { |
| 3204 | dir->linuxdir = opendir(pathname); |
| 3205 | if(dir->linuxdir == NULL) { |
| 3206 | free(dir); |
| 3207 | return NULL; |
| 3208 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3209 | } |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3210 | |
Phillip Lougher | 3cb5d6c | 2013-01-09 05:50:29 +0000 | [diff] [blame] | 3211 | dir->pathname = strdup(pathname); |
| 3212 | dir->subpath = strdup(subpath); |
Phillip Lougher | ded70fa | 2011-12-25 02:14:58 +0000 | [diff] [blame] | 3213 | dir->count = 0; |
| 3214 | dir->directory_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3215 | dir->dir_is_ldir = TRUE; |
| 3216 | dir->list = NULL; |
Phillip Lougher | 0b5a124 | 2011-12-25 03:22:42 +0000 | [diff] [blame] | 3217 | dir->depth = depth; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3218 | dir->excluded = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3219 | |
| 3220 | return dir; |
| 3221 | } |
| 3222 | |
| 3223 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3224 | struct dir_ent *scan1_encomp_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3225 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3226 | static int index = 0; |
| 3227 | |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3228 | if(dir->count < old_root_entries) { |
| 3229 | int i; |
| 3230 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3231 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3232 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3233 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3234 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3235 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3236 | } |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3237 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3238 | |
| 3239 | while(index < source) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3240 | char *basename = NULL; |
| 3241 | char *dir_name = getbase(source_path[index]); |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3242 | int pass = 1, res; |
plougher | 89fe2c7 | 2010-12-16 05:31:34 +0000 | [diff] [blame] | 3243 | |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3244 | if(dir_name == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3245 | ERROR_START("Bad source directory %s", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3246 | source_path[index]); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3247 | ERROR_EXIT(" - skipping ...\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3248 | index ++; |
| 3249 | continue; |
| 3250 | } |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3251 | dir_name = strdup(dir_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3252 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3253 | struct dir_ent *dir_ent = dir->list; |
| 3254 | |
| 3255 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3256 | dir_ent = dir_ent->next); |
| 3257 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3258 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3259 | ERROR("Source directory entry %s already used! - trying" |
| 3260 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3261 | if(pass == 1) |
| 3262 | basename = dir_name; |
| 3263 | else |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3264 | free(dir_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3265 | res = asprintf(&dir_name, "%s_%d", basename, pass++); |
| 3266 | if(res == -1) |
| 3267 | BAD_ERROR("asprintf failed in " |
| 3268 | "scan1_encomp_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3269 | ERROR("%s\n", dir_name); |
| 3270 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3271 | return create_dir_entry(dir_name, basename, |
| 3272 | source_path[index ++], dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3273 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3274 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3278 | struct dir_ent *scan1_single_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3279 | { |
| 3280 | struct dirent *d_name; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3281 | int i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3282 | |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3283 | if(dir->count < old_root_entries) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3284 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3285 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3286 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3287 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3288 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3289 | } |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3290 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3291 | |
| 3292 | if((d_name = readdir(dir->linuxdir)) != NULL) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3293 | char *basename = NULL; |
| 3294 | char *dir_name = strdup(d_name->d_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3295 | int pass = 1, res; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3296 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3297 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3298 | struct dir_ent *dir_ent = dir->list; |
| 3299 | |
| 3300 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3301 | dir_ent = dir_ent->next); |
| 3302 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3303 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3304 | ERROR("Source directory entry %s already used! - trying" |
| 3305 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3306 | if (pass == 1) |
| 3307 | basename = dir_name; |
| 3308 | else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3309 | free(dir_name); |
| 3310 | res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++); |
| 3311 | if(res == -1) |
| 3312 | BAD_ERROR("asprintf failed in " |
| 3313 | "scan1_single_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3314 | ERROR("%s\n", dir_name); |
| 3315 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3316 | return create_dir_entry(dir_name, basename, NULL, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3319 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3323 | struct dir_ent *scan1_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3324 | { |
plougher | 480d9bf | 2010-12-18 02:28:39 +0000 | [diff] [blame] | 3325 | struct dirent *d_name = readdir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3326 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3327 | return d_name ? |
| 3328 | create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) : |
| 3329 | NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3333 | void scan1_freedir(struct dir_info *dir) |
| 3334 | { |
plougher | fbed12b | 2006-02-07 12:45:53 +0000 | [diff] [blame] | 3335 | if(dir->pathname[0] != '\0') |
| 3336 | closedir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3340 | struct dir_info *dir_scan1(char *filename, char *subpath, |
| 3341 | struct pathnames *paths, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3342 | struct dir_ent *(_readdir)(struct dir_info *), int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3343 | { |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3344 | struct dir_info *dir = scan1_opendir(filename, subpath, depth); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3345 | struct dir_ent *dir_ent; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3346 | |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3347 | if(dir == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3348 | ERROR_START("Could not open %s", filename); |
| 3349 | ERROR_EXIT(", skipping...\n"); |
Phillip Lougher | 87db567 | 2013-01-09 05:40:46 +0000 | [diff] [blame] | 3350 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3353 | while((dir_ent = _readdir(dir))) { |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3354 | struct dir_info *sub_dir; |
| 3355 | struct stat buf; |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 3356 | struct pathnames *new = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3357 | char *filename = pathname(dir_ent); |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3358 | char *subpath = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3359 | char *dir_name = dir_ent->name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3360 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3361 | if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3362 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3363 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3364 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3365 | |
| 3366 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3367 | ERROR_START("Cannot stat dir/file %s because %s", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3368 | filename, strerror(errno)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3369 | ERROR_EXIT(", ignoring\n"); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3370 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3371 | continue; |
| 3372 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3373 | |
| 3374 | if((buf.st_mode & S_IFMT) != S_IFREG && |
Phillip Lougher | e4ff36a | 2012-11-19 01:34:47 +0000 | [diff] [blame] | 3375 | (buf.st_mode & S_IFMT) != S_IFDIR && |
| 3376 | (buf.st_mode & S_IFMT) != S_IFLNK && |
| 3377 | (buf.st_mode & S_IFMT) != S_IFCHR && |
| 3378 | (buf.st_mode & S_IFMT) != S_IFBLK && |
| 3379 | (buf.st_mode & S_IFMT) != S_IFIFO && |
| 3380 | (buf.st_mode & S_IFMT) != S_IFSOCK) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3381 | ERROR_START("File %s has unrecognised filetype %d", |
| 3382 | filename, buf.st_mode & S_IFMT); |
| 3383 | ERROR_EXIT(", ignoring\n"); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3384 | free_dir_entry(dir_ent); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3385 | continue; |
| 3386 | } |
| 3387 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3388 | if((old_exclude && old_excluded(filename, &buf)) || |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3389 | (!old_exclude && excluded(dir_name, paths, &new))) { |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3390 | add_excluded(dir); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3391 | free_dir_entry(dir_ent); |
Phillip Lougher | 10d8de0 | 2011-08-29 00:14:48 +0100 | [diff] [blame] | 3392 | continue; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3393 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3394 | |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3395 | if(exclude_actions()) { |
| 3396 | subpath = subpathname(dir_ent); |
| 3397 | |
| 3398 | if(eval_exclude_actions(dir_name, filename, subpath, |
Phillip Lougher | aa83ecb | 2014-07-12 02:47:15 +0100 | [diff] [blame^] | 3399 | &buf, depth, dir_ent)) { |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3400 | add_excluded(dir); |
| 3401 | free_dir_entry(dir_ent); |
| 3402 | continue; |
| 3403 | } |
| 3404 | } |
| 3405 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3406 | if((buf.st_mode & S_IFMT) == S_IFDIR) { |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3407 | if(subpath == NULL) |
| 3408 | subpath = subpathname(dir_ent); |
| 3409 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3410 | sub_dir = dir_scan1(filename, subpath, new, |
| 3411 | scan1_readdir, depth + 1); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3412 | if(sub_dir == NULL) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3413 | free_dir_entry(dir_ent); |
Phillip Lougher | 205c1e0 | 2013-01-07 01:37:56 +0000 | [diff] [blame] | 3414 | free(new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3415 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3416 | } |
Phillip Lougher | 6a78a2d | 2011-12-28 03:54:19 +0000 | [diff] [blame] | 3417 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3418 | dir->directory_count ++; |
| 3419 | } else |
| 3420 | sub_dir = NULL; |
| 3421 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3422 | add_dir_entry(dir_ent, sub_dir, lookup_inode(&buf)); |
Phillip Lougher | 205c1e0 | 2013-01-07 01:37:56 +0000 | [diff] [blame] | 3423 | free(new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | scan1_freedir(dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3427 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3428 | return dir; |
| 3429 | } |
| 3430 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3431 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3432 | /* |
| 3433 | * dir_scan2 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3434 | * This processes most actions and any pseudo files |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3435 | */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3436 | 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] | 3437 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3438 | if (dir_ent == NULL) |
| 3439 | dir_ent = dir->list; |
| 3440 | else |
| 3441 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3442 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3443 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next); |
| 3444 | |
| 3445 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3446 | } |
| 3447 | |
| 3448 | |
| 3449 | struct dir_ent *scan2_lookup(struct dir_info *dir, char *name) |
| 3450 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3451 | struct dir_ent *dir_ent = dir->list; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3452 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3453 | for(; dir_ent && strcmp(dir_ent->name, name) != 0; |
| 3454 | dir_ent = dir_ent->next); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3455 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3456 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3460 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3461 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3462 | struct dir_ent *dir_ent = NULL; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3463 | struct pseudo_entry *pseudo_ent; |
| 3464 | struct stat buf; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3465 | static int pseudo_ino = 1; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3466 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3467 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3468 | struct inode_info *inode_info = dir_ent->inode; |
| 3469 | struct stat *buf = &inode_info->buf; |
| 3470 | char *name = dir_ent->name; |
| 3471 | |
Phillip Lougher | 89d757c | 2011-09-20 00:33:19 +0100 | [diff] [blame] | 3472 | eval_actions(dir_ent); |
| 3473 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3474 | if((buf->st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3475 | dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) { |
| 3479 | dir_ent = scan2_lookup(dir, pseudo_ent->name); |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3480 | if(pseudo_ent->dev->type == 'm') { |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3481 | struct stat *buf; |
| 3482 | if(dir_ent == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3483 | ERROR_START("Pseudo modify file \"%s\" does " |
| 3484 | "not exist in source filesystem.", |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3485 | pseudo_ent->pathname); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3486 | ERROR_EXIT(" Ignoring.\n"); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3487 | continue; |
| 3488 | } |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3489 | if(dir_ent->inode->root_entry) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3490 | ERROR_START("Pseudo modify file \"%s\" is a " |
| 3491 | "pre-existing file in the filesystem " |
| 3492 | "being appended to. It cannot be "\ |
| 3493 | "modified.", pseudo_ent->pathname); |
| 3494 | ERROR_EXIT(" Ignoring.\n"); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3495 | continue; |
| 3496 | } |
| 3497 | buf = &dir_ent->inode->buf; |
| 3498 | buf->st_mode = (buf->st_mode & S_IFMT) | |
| 3499 | pseudo_ent->dev->mode; |
| 3500 | buf->st_uid = pseudo_ent->dev->uid; |
| 3501 | buf->st_gid = pseudo_ent->dev->gid; |
| 3502 | continue; |
| 3503 | } |
| 3504 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3505 | if(dir_ent) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3506 | if(dir_ent->inode->root_entry) { |
| 3507 | ERROR_START("Pseudo file \"%s\" is a " |
| 3508 | "pre-existing file in the filesystem " |
| 3509 | "being appended to.", |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3510 | pseudo_ent->pathname); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3511 | ERROR_EXIT(" Ignoring.\n"); |
| 3512 | } else { |
| 3513 | ERROR_START("Pseudo file \"%s\" exists in " |
| 3514 | "source filesystem \"%s\".", |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3515 | pseudo_ent->pathname, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3516 | pathname(dir_ent)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3517 | ERROR_EXIT("\nIgnoring, exclude it (-e/-ef) to " |
| 3518 | "override.\n"); |
| 3519 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3520 | continue; |
| 3521 | } |
| 3522 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3523 | memset(&buf, 0, sizeof(buf)); |
| 3524 | buf.st_mode = pseudo_ent->dev->mode; |
| 3525 | buf.st_uid = pseudo_ent->dev->uid; |
| 3526 | buf.st_gid = pseudo_ent->dev->gid; |
| 3527 | buf.st_rdev = makedev(pseudo_ent->dev->major, |
| 3528 | pseudo_ent->dev->minor); |
plougher | 7e58f4d | 2009-04-05 12:06:19 +0000 | [diff] [blame] | 3529 | buf.st_mtime = time(NULL); |
plougher | 1a3fbf2 | 2009-04-05 12:04:16 +0000 | [diff] [blame] | 3530 | buf.st_ino = pseudo_ino ++; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3531 | |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3532 | if(pseudo_ent->dev->type == 'd') { |
| 3533 | struct dir_ent *dir_ent = |
| 3534 | create_dir_entry(pseudo_ent->name, NULL, |
| 3535 | pseudo_ent->pathname, dir); |
| 3536 | char *subpath = strdup(subpathname(dir_ent)); |
| 3537 | struct dir_info *sub_dir = scan1_opendir("", subpath, |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3538 | dir->depth + 1); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3539 | if(sub_dir == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3540 | ERROR_START("Could not create pseudo directory " |
| 3541 | "\"%s\"", pseudo_ent->pathname); |
| 3542 | ERROR_EXIT(", skipping...\n"); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3543 | free(subpath); |
| 3544 | pseudo_ino --; |
| 3545 | continue; |
| 3546 | } |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3547 | dir_scan2(sub_dir, pseudo_ent->pseudo); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3548 | dir->directory_count ++; |
| 3549 | add_dir_entry(dir_ent, sub_dir, |
| 3550 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0)); |
| 3551 | } else if(pseudo_ent->dev->type == 'f') { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3552 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3553 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3554 | lookup_inode2(&buf, PSEUDO_FILE_PROCESS, |
| 3555 | pseudo_ent->dev->pseudo_id), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3556 | } else { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3557 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3558 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3559 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3560 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3561 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3565 | /* |
| 3566 | * dir_scan3 routines... |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3567 | * This processes the move action |
| 3568 | */ |
| 3569 | void dir_scan3(struct dir_info *root, struct dir_info *dir) |
| 3570 | { |
| 3571 | struct dir_ent *dir_ent = NULL; |
| 3572 | |
| 3573 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
| 3574 | |
| 3575 | eval_move_actions(root, dir_ent); |
| 3576 | |
| 3577 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
| 3578 | dir_scan3(root, dir_ent->dir); |
| 3579 | } |
| 3580 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3581 | |
| 3582 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3583 | /* |
| 3584 | * dir_scan4 routines... |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3585 | * This processes the empty action. This action has to be processed after |
| 3586 | * all other actions because the previous exclude and move actions and the |
| 3587 | * pseudo actions affect whether a directory is empty |
| 3588 | */ |
| 3589 | void dir_scan4(struct dir_info *dir) |
| 3590 | { |
| 3591 | struct dir_ent *dir_ent = dir->list, *prev = NULL; |
| 3592 | |
| 3593 | while(dir_ent) { |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3594 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) { |
| 3595 | dir_scan4(dir_ent->dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3596 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3597 | if(eval_empty_actions(dir_ent)) { |
| 3598 | struct dir_ent *tmp = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3599 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3600 | /* |
| 3601 | * delete sub-directory, this is by definition |
| 3602 | * empty |
| 3603 | */ |
| 3604 | free(dir_ent->dir->pathname); |
| 3605 | free(dir_ent->dir->subpath); |
| 3606 | free(dir_ent->dir); |
| 3607 | |
| 3608 | /* remove dir_ent from list */ |
| 3609 | dir_ent = dir_ent->next; |
| 3610 | if(prev) |
| 3611 | prev->next = dir_ent; |
| 3612 | else |
| 3613 | dir->list = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3614 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3615 | /* free it */ |
| 3616 | free_dir_entry(tmp); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3617 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3618 | /* update counts */ |
| 3619 | dir->directory_count --; |
| 3620 | dir->count --; |
| 3621 | add_excluded(dir); |
| 3622 | continue; |
| 3623 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3624 | } |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3625 | |
| 3626 | prev = dir_ent; |
| 3627 | dir_ent = dir_ent->next; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | |
| 3632 | /* |
| 3633 | * dir_scan5 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3634 | * This sorts every directory and computes the inode numbers |
| 3635 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3636 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3637 | /* |
| 3638 | * Bottom up linked list merge sort. |
| 3639 | * |
| 3640 | * Qsort and other O(n log n) algorithms work well with arrays but not |
| 3641 | * linked lists. Merge sort another O(n log n) sort algorithm on the other hand |
| 3642 | * is not ideal for arrays (as it needs an additonal n storage locations |
| 3643 | * as sorting is not done in place), but it is ideal for linked lists because |
| 3644 | * it doesn't require any extra storage, |
| 3645 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3646 | void sort_directory(struct dir_info *dir) |
| 3647 | { |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3648 | struct dir_ent *cur, *l1, *l2, *next; |
| 3649 | int len1, len2, stride = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3650 | |
Phillip Lougher | d3459a8 | 2014-04-18 01:37:54 +0100 | [diff] [blame] | 3651 | if(dir->list == NULL || dir->count < 2) |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3652 | return; |
| 3653 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3654 | /* |
| 3655 | * We can consider our linked-list to be made up of stride length |
| 3656 | * sublists. Eacn iteration around this loop merges adjacent |
| 3657 | * stride length sublists into larger 2*stride sublists. We stop |
| 3658 | * when stride becomes equal to the entire list. |
| 3659 | * |
| 3660 | * Initially stride = 1 (by definition a sublist of 1 is sorted), and |
| 3661 | * these 1 element sublists are merged into 2 element sublists, which |
| 3662 | * are then merged into 4 element sublists and so on. |
| 3663 | */ |
| 3664 | do { |
| 3665 | l2 = dir->list; /* head of current linked list */ |
| 3666 | cur = NULL; /* empty output list */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3667 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3668 | /* |
| 3669 | * Iterate through the linked list, merging adjacent sublists. |
| 3670 | * On each interation l2 points to the next sublist pair to be |
| 3671 | * merged (if there's only one sublist left this is simply added |
| 3672 | * to the output list) |
| 3673 | */ |
| 3674 | while(l2) { |
| 3675 | l1 = l2; |
| 3676 | for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next); |
| 3677 | len2 = stride; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3678 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3679 | /* |
| 3680 | * l1 points to first sublist. |
| 3681 | * l2 points to second sublist. |
| 3682 | * Merge them onto the output list |
| 3683 | */ |
| 3684 | while(len1 && l2 && len2) { |
| 3685 | if(strcmp(l1->name, l2->name) <= 0) { |
| 3686 | next = l1; |
| 3687 | l1 = l1->next; |
| 3688 | len1 --; |
| 3689 | } else { |
| 3690 | next = l2; |
| 3691 | l2 = l2->next; |
| 3692 | len2 --; |
| 3693 | } |
| 3694 | |
| 3695 | if(cur) { |
| 3696 | cur->next = next; |
| 3697 | cur = next; |
| 3698 | } else |
| 3699 | dir->list = cur = next; |
| 3700 | } |
| 3701 | /* |
| 3702 | * One sublist is now empty, copy the other one onto the |
| 3703 | * output list |
| 3704 | */ |
| 3705 | for(; len1; len1 --, l1 = l1->next) { |
| 3706 | if(cur) { |
| 3707 | cur->next = l1; |
| 3708 | cur = l1; |
| 3709 | } else |
| 3710 | dir->list = cur = l1; |
| 3711 | } |
| 3712 | for(; l2 && len2; len2 --, l2 = l2->next) { |
| 3713 | if(cur) { |
| 3714 | cur->next = l2; |
| 3715 | cur = l2; |
| 3716 | } else |
| 3717 | dir->list = cur = l2; |
| 3718 | } |
| 3719 | } |
| 3720 | cur->next = NULL; |
| 3721 | stride = stride << 1; |
| 3722 | } while(stride < dir->count); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3726 | void dir_scan5(struct dir_info *dir) |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3727 | { |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3728 | struct dir_ent *dir_ent; |
| 3729 | unsigned int byte_count = 0; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3730 | |
| 3731 | sort_directory(dir); |
| 3732 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3733 | for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) { |
| 3734 | byte_count += strlen(dir_ent->name) + |
| 3735 | sizeof(struct squashfs_dir_entry); |
| 3736 | |
| 3737 | if(dir_ent->inode->root_entry) |
| 3738 | continue; |
| 3739 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3740 | alloc_inode_no(dir_ent->inode, 0); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3741 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3742 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | e69b588 | 2012-11-19 01:31:09 +0000 | [diff] [blame] | 3743 | dir_scan5(dir_ent->dir); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3744 | } |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3745 | |
| 3746 | if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE)) |
| 3747 | dir->dir_is_ldir = FALSE; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | |
| 3751 | /* |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3752 | * dir_scan6 routines... |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3753 | * This generates the filesystem metadata and writes it out to the destination |
| 3754 | */ |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3755 | void scan6_init_dir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3756 | { |
| 3757 | dir->buff = malloc(SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3758 | if(dir->buff == NULL) |
| 3759 | MEM_ERROR(); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3760 | |
| 3761 | dir->size = SQUASHFS_METADATA_SIZE; |
| 3762 | dir->p = dir->index_count_p = dir->buff; |
| 3763 | dir->entry_count = 256; |
| 3764 | dir->entry_count_p = NULL; |
| 3765 | dir->index = NULL; |
| 3766 | dir->i_count = dir->i_size = 0; |
| 3767 | } |
| 3768 | |
| 3769 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3770 | 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] | 3771 | struct dir_ent *dir_ent) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3772 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3773 | if (dir_ent == NULL) |
| 3774 | dir_ent = dir_info->list; |
| 3775 | else |
| 3776 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3777 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3778 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next) |
| 3779 | add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number, |
| 3780 | dir_ent->name, dir_ent->inode->type, dir); |
| 3781 | |
| 3782 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3786 | void scan6_freedir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3787 | { |
| 3788 | if(dir->index) |
| 3789 | free(dir->index); |
| 3790 | free(dir->buff); |
| 3791 | } |
| 3792 | |
| 3793 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3794 | void dir_scan6(squashfs_inode *inode, struct dir_info *dir_info) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3795 | { |
| 3796 | int squashfs_type; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3797 | int duplicate_file; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3798 | struct directory dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3799 | struct dir_ent *dir_ent = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3800 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3801 | scan6_init_dir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3802 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3803 | while((dir_ent = scan6_readdir(&dir, dir_info, dir_ent)) != NULL) { |
Phillip Lougher | 0a67088 | 2012-10-05 04:09:13 +0100 | [diff] [blame] | 3804 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3805 | |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 3806 | update_info(dir_ent); |
| 3807 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3808 | if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) { |
| 3809 | switch(buf->st_mode & S_IFMT) { |
| 3810 | case S_IFREG: |
| 3811 | squashfs_type = SQUASHFS_FILE_TYPE; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3812 | write_file(inode, dir_ent, |
| 3813 | &duplicate_file); |
| 3814 | INFO("file %s, uncompressed size %lld " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3815 | "bytes %s\n", |
| 3816 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3817 | (long long) buf->st_size, |
| 3818 | duplicate_file ? "DUPLICATE" : |
| 3819 | ""); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3820 | break; |
| 3821 | |
| 3822 | case S_IFDIR: |
| 3823 | squashfs_type = SQUASHFS_DIR_TYPE; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3824 | dir_scan6(inode, dir_ent->dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3825 | break; |
| 3826 | |
| 3827 | case S_IFLNK: |
| 3828 | squashfs_type = SQUASHFS_SYMLINK_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3829 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3830 | squashfs_type, 0, 0, 0, NULL, |
| 3831 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3832 | INFO("symbolic link %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3833 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3834 | sym_count ++; |
| 3835 | break; |
| 3836 | |
| 3837 | case S_IFCHR: |
| 3838 | squashfs_type = SQUASHFS_CHRDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3839 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3840 | squashfs_type, 0, 0, 0, NULL, |
| 3841 | NULL, NULL, 0); |
| 3842 | INFO("character device %s inode 0x%llx" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3843 | "\n", subpathname(dir_ent), |
| 3844 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3845 | dev_count ++; |
| 3846 | break; |
| 3847 | |
| 3848 | case S_IFBLK: |
| 3849 | squashfs_type = SQUASHFS_BLKDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3850 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3851 | squashfs_type, 0, 0, 0, NULL, |
| 3852 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3853 | INFO("block device %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3854 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3855 | dev_count ++; |
| 3856 | break; |
| 3857 | |
| 3858 | case S_IFIFO: |
| 3859 | squashfs_type = SQUASHFS_FIFO_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3860 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3861 | squashfs_type, 0, 0, 0, NULL, |
| 3862 | NULL, NULL, 0); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3863 | INFO("fifo %s inode 0x%llx\n", |
| 3864 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3865 | fifo_count ++; |
| 3866 | break; |
| 3867 | |
| 3868 | case S_IFSOCK: |
| 3869 | squashfs_type = SQUASHFS_SOCKET_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 3870 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3871 | squashfs_type, 0, 0, 0, NULL, |
| 3872 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3873 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3874 | "0x%llx\n", |
| 3875 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3876 | sock_count ++; |
| 3877 | break; |
| 3878 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 3879 | default: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3880 | BAD_ERROR("%s unrecognised file type, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3881 | "mode is %x\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3882 | subpathname(dir_ent), |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3883 | buf->st_mode); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3884 | } |
| 3885 | dir_ent->inode->inode = *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3886 | dir_ent->inode->type = squashfs_type; |
| 3887 | } else { |
| 3888 | *inode = dir_ent->inode->inode; |
| 3889 | squashfs_type = dir_ent->inode->type; |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3890 | switch(squashfs_type) { |
| 3891 | case SQUASHFS_FILE_TYPE: |
| 3892 | if(!sorted) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3893 | INFO("file %s, uncompressed " |
| 3894 | "size %lld bytes LINK" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3895 | "\n", |
| 3896 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3897 | (long long) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3898 | buf->st_size); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3899 | break; |
| 3900 | case SQUASHFS_SYMLINK_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3901 | INFO("symbolic link %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3902 | "LINK\n", subpathname(dir_ent), |
| 3903 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3904 | break; |
| 3905 | case SQUASHFS_CHRDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3906 | INFO("character device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3907 | "LINK\n", subpathname(dir_ent), |
| 3908 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3909 | break; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3910 | case SQUASHFS_BLKDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3911 | INFO("block device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3912 | "LINK\n", subpathname(dir_ent), |
| 3913 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3914 | break; |
| 3915 | case SQUASHFS_FIFO_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3916 | INFO("fifo %s inode 0x%llx LINK\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3917 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3918 | break; |
| 3919 | case SQUASHFS_SOCKET_TYPE: |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3920 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3921 | "0x%llx LINK\n", |
| 3922 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 3923 | break; |
| 3924 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
Phillip Lougher | 0366aec | 2012-10-05 04:06:04 +0100 | [diff] [blame] | 3927 | add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name, |
| 3928 | squashfs_type, &dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3931 | write_dir(inode, dir_info, &dir); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 3932 | INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent), |
| 3933 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3934 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3935 | scan6_freedir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | |
| 3939 | unsigned int slog(unsigned int block) |
| 3940 | { |
| 3941 | int i; |
| 3942 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 3943 | for(i = 12; i <= 20; i++) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3944 | if(block == (1 << i)) |
| 3945 | return i; |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
| 3949 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3950 | int old_excluded(char *filename, struct stat *buf) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3951 | { |
| 3952 | int i; |
| 3953 | |
| 3954 | for(i = 0; i < exclude; i++) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3955 | if((exclude_paths[i].st_dev == buf->st_dev) && |
| 3956 | (exclude_paths[i].st_ino == buf->st_ino)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3957 | return TRUE; |
| 3958 | return FALSE; |
| 3959 | } |
| 3960 | |
| 3961 | |
| 3962 | #define ADD_ENTRY(buf) \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3963 | if(exclude % EXCLUDE_SIZE == 0) { \ |
| 3964 | exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \ |
| 3965 | * sizeof(struct exclude_info)); \ |
| 3966 | if(exclude_paths == NULL) \ |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3967 | MEM_ERROR(); \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 3968 | } \ |
| 3969 | exclude_paths[exclude].st_dev = buf.st_dev; \ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3970 | exclude_paths[exclude++].st_ino = buf.st_ino; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 3971 | int old_add_exclude(char *path) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3972 | { |
| 3973 | int i; |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3974 | char *filename; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3975 | struct stat buf; |
| 3976 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 3977 | if(path[0] == '/' || strncmp(path, "./", 2) == 0 || |
| 3978 | strncmp(path, "../", 3) == 0) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3979 | if(lstat(path, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3980 | ERROR_START("Cannot stat exclude dir/file %s because " |
| 3981 | "%s", path, strerror(errno)); |
| 3982 | ERROR_EXIT(", ignoring\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3983 | return TRUE; |
| 3984 | } |
| 3985 | ADD_ENTRY(buf); |
| 3986 | return TRUE; |
| 3987 | } |
| 3988 | |
| 3989 | for(i = 0; i < source; i++) { |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3990 | int res = asprintf(&filename, "%s/%s", source_path[i], path); |
| 3991 | if(res == -1) |
| 3992 | BAD_ERROR("asprintf failed in old_add_exclude\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3993 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3994 | if(!(errno == ENOENT || errno == ENOTDIR)) { |
| 3995 | ERROR_START("Cannot stat exclude dir/file %s " |
| 3996 | "because %s", filename, strerror(errno)); |
| 3997 | ERROR_EXIT(", ignoring\n"); |
| 3998 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 3999 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4000 | continue; |
| 4001 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 4002 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4003 | ADD_ENTRY(buf); |
| 4004 | } |
| 4005 | return TRUE; |
| 4006 | } |
| 4007 | |
| 4008 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4009 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 4010 | int type) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4011 | { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4012 | old_root_entry = realloc(old_root_entry, |
| 4013 | sizeof(struct old_root_entry_info) * (old_root_entries + 1)); |
| 4014 | if(old_root_entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4015 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4016 | |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 4017 | old_root_entry[old_root_entries].name = strdup(name); |
| 4018 | old_root_entry[old_root_entries].inode.inode = inode; |
| 4019 | old_root_entry[old_root_entries].inode.inode_number = inode_number; |
| 4020 | old_root_entry[old_root_entries].inode.type = type; |
| 4021 | old_root_entry[old_root_entries++].inode.root_entry = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
| 4024 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4025 | void initialise_threads(int readq, int fragq, int bwriteq, int fwriteq, |
| 4026 | int freelst, char *destination_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4027 | { |
| 4028 | int i; |
| 4029 | sigset_t sigmask, old_mask; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4030 | int total_mem = readq; |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4031 | int reader_size; |
| 4032 | int fragment_size; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4033 | int fwriter_size; |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4034 | /* |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4035 | * bwriter_size is global because it is needed in |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4036 | * write_file_blocks_dup() |
| 4037 | */ |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4038 | |
| 4039 | /* |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4040 | * Never allow the total size of the queues to be larger than |
| 4041 | * physical memory |
| 4042 | * |
| 4043 | * When adding together the possibly user supplied values, make |
| 4044 | * sure they've not been deliberately contrived to overflow an int |
| 4045 | */ |
| 4046 | if(add_overflow(total_mem, fragq)) |
| 4047 | BAD_ERROR("Queue sizes rediculously too large\n"); |
| 4048 | total_mem += fragq; |
| 4049 | if(add_overflow(total_mem, bwriteq)) |
| 4050 | BAD_ERROR("Queue sizes rediculously too large\n"); |
| 4051 | total_mem += bwriteq; |
| 4052 | if(add_overflow(total_mem, fwriteq)) |
| 4053 | BAD_ERROR("Queue sizes rediculously too large\n"); |
| 4054 | total_mem += fwriteq; |
| 4055 | |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 4056 | check_usable_phys_mem(total_mem); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4057 | |
| 4058 | /* |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4059 | * convert from queue size in Mbytes to queue size in |
| 4060 | * blocks. |
| 4061 | * |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4062 | * This isn't going to overflow an int unless there exists |
| 4063 | * systems with more than 8 Petabytes of RAM! |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4064 | */ |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4065 | reader_size = readq << (20 - block_log); |
| 4066 | fragment_size = fragq << (20 - block_log); |
| 4067 | bwriter_size = bwriteq << (20 - block_log); |
| 4068 | fwriter_size = fwriteq << (20 - block_log); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4069 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4070 | /* |
| 4071 | * setup signal handlers for the main thread, these cleanup |
| 4072 | * deleting the destination file, if appending the |
| 4073 | * handlers for SIGTERM and SIGINT will be replaced with handlers |
| 4074 | * allowing the user to press ^C twice to restore the existing |
| 4075 | * filesystem. |
| 4076 | * |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4077 | * SIGUSR1 is an internal signal, which is used by the sub-threads |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4078 | * to tell the main thread to terminate, deleting the destination file, |
| 4079 | * or if necessary restoring the filesystem on appending |
| 4080 | */ |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 4081 | signal(SIGTERM, sighandler); |
| 4082 | signal(SIGINT, sighandler); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4083 | signal(SIGUSR1, sighandler); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4084 | |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 4085 | /* block SIGQUIT and SIGHUP, these are handled by the info thread */ |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4086 | sigemptyset(&sigmask); |
| 4087 | sigaddset(&sigmask, SIGQUIT); |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 4088 | sigaddset(&sigmask, SIGHUP); |
Phillip Lougher | b1259f7 | 2013-06-06 03:10:15 +0100 | [diff] [blame] | 4089 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4090 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4091 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4092 | /* |
| 4093 | * temporarily block these signals, so the created sub-threads |
| 4094 | * will ignore them, ensuring the main thread handles them |
| 4095 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4096 | sigemptyset(&sigmask); |
| 4097 | sigaddset(&sigmask, SIGINT); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4098 | sigaddset(&sigmask, SIGTERM); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4099 | sigaddset(&sigmask, SIGUSR1); |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4100 | if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4101 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4102 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4103 | if(processors == -1) { |
| 4104 | #ifndef linux |
| 4105 | int mib[2]; |
| 4106 | size_t len = sizeof(processors); |
| 4107 | |
| 4108 | mib[0] = CTL_HW; |
| 4109 | #ifdef HW_AVAILCPU |
| 4110 | mib[1] = HW_AVAILCPU; |
| 4111 | #else |
| 4112 | mib[1] = HW_NCPU; |
| 4113 | #endif |
| 4114 | |
| 4115 | if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 4116 | ERROR_START("Failed to get number of available " |
| 4117 | "processors."); |
| 4118 | ERROR_EXIT(" Defaulting to 1\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4119 | processors = 1; |
| 4120 | } |
| 4121 | #else |
plougher | 9cc26b7 | 2010-08-17 01:05:55 +0000 | [diff] [blame] | 4122 | processors = sysconf(_SC_NPROCESSORS_ONLN); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4123 | #endif |
| 4124 | } |
| 4125 | |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4126 | if(multiply_overflow(processors, 3) || |
| 4127 | multiply_overflow(processors * 3, sizeof(pthread_t))) |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4128 | BAD_ERROR("Processors too large\n"); |
| 4129 | |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4130 | deflator_thread = malloc(processors * 3 * sizeof(pthread_t)); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4131 | if(deflator_thread == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4132 | MEM_ERROR(); |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4133 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4134 | frag_deflator_thread = &deflator_thread[processors]; |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4135 | frag_thread = &frag_deflator_thread[processors]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4136 | |
| 4137 | to_reader = queue_init(1); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4138 | to_deflate = queue_init(reader_size); |
| 4139 | to_process_frag = queue_init(reader_size); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4140 | to_writer = queue_init(bwriter_size + fwriter_size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4141 | from_writer = queue_init(1); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4142 | to_frag = queue_init(fragment_size); |
| 4143 | locked_fragment = queue_init(fragment_size); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 4144 | to_main = seq_queue_init(); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4145 | reader_buffer = cache_init(block_size, reader_size, 0, 0); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4146 | bwriter_buffer = cache_init(block_size, bwriter_size, 1, freelst); |
| 4147 | fwriter_buffer = cache_init(block_size, fwriter_size, 1, freelst); |
Phillip Lougher | 1ac3715 | 2014-05-10 03:09:28 +0100 | [diff] [blame] | 4148 | fragment_buffer = cache_init(block_size, fragment_size, 1, 0); |
| 4149 | reserve_cache = cache_init(block_size, processors + 1, 1, 0); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4150 | pthread_create(&reader_thread, NULL, reader, NULL); |
| 4151 | pthread_create(&writer_thread, NULL, writer, NULL); |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 4152 | init_progress_bar(); |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4153 | init_info(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4154 | |
| 4155 | for(i = 0; i < processors; i++) { |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 4156 | if(pthread_create(&deflator_thread[i], NULL, deflator, NULL)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4157 | BAD_ERROR("Failed to create thread\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4158 | if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator, |
| 4159 | NULL) != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4160 | BAD_ERROR("Failed to create thread\n"); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 4161 | if(pthread_create(&frag_thread[i], NULL, frag_thrd, |
| 4162 | (void *) destination_file) != 0) |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4163 | BAD_ERROR("Failed to create thread\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4166 | main_thread = pthread_self(); |
| 4167 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4168 | printf("Parallel mksquashfs: Using %d processor%s\n", processors, |
| 4169 | processors == 1 ? "" : "s"); |
| 4170 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4171 | /* Restore the signal mask for the main thread */ |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4172 | if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4173 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4174 | } |
| 4175 | |
| 4176 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4177 | long long write_inode_lookup_table() |
| 4178 | { |
| 4179 | int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4180 | void *it; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4181 | |
| 4182 | if(inode_count == sinode_count) |
| 4183 | goto skip_inode_hash_table; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4184 | |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4185 | it = realloc(inode_lookup_table, lookup_bytes); |
| 4186 | if(it == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4187 | MEM_ERROR(); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4188 | inode_lookup_table = it; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4189 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4190 | for(i = 0; i < INODE_HASH_SIZE; i ++) { |
Phillip Lougher | f2f83b6 | 2014-04-18 01:30:47 +0100 | [diff] [blame] | 4191 | struct inode_info *inode; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4192 | |
| 4193 | for(inode = inode_info[i]; inode; inode = inode->next) { |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4194 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4195 | inode_number = get_inode_no(inode); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4196 | |
Phillip Lougher | 9f59596 | 2014-07-07 04:37:47 +0100 | [diff] [blame] | 4197 | /* The empty action will produce orphaned inode |
| 4198 | * entries in the inode_info[] table. These |
| 4199 | * entries because they are orphaned will not be |
| 4200 | * allocated an inode number in dir_scan5(), so |
| 4201 | * skip any entries with the default dummy inode |
| 4202 | * number of 0 */ |
| 4203 | if(inode_number == 0) |
| 4204 | continue; |
| 4205 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4206 | SQUASHFS_SWAP_LONG_LONGS(&inode->inode, |
| 4207 | &inode_lookup_table[inode_number - 1], 1); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4208 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4209 | } |
| 4210 | } |
| 4211 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4212 | skip_inode_hash_table: |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 4213 | return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL, |
| 4214 | noI); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4217 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4218 | char *get_component(char *target, char **targname) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4219 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4220 | char *start; |
| 4221 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4222 | while(*target == '/') |
rlougher | c4ebcf5 | 2007-11-08 17:52:49 +0000 | [diff] [blame] | 4223 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4224 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4225 | start = target; |
Phillip Lougher | 66d8b8e | 2014-04-20 21:01:33 +0100 | [diff] [blame] | 4226 | while(*target != '/' && *target != '\0') |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4227 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4228 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4229 | *targname = strndup(start, target - start); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4230 | |
Phillip Lougher | 66d8b8e | 2014-04-20 21:01:33 +0100 | [diff] [blame] | 4231 | while(*target == '/') |
| 4232 | target ++; |
| 4233 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4234 | return target; |
| 4235 | } |
| 4236 | |
| 4237 | |
| 4238 | void free_path(struct pathname *paths) |
| 4239 | { |
| 4240 | int i; |
| 4241 | |
| 4242 | for(i = 0; i < paths->names; i++) { |
| 4243 | if(paths->name[i].paths) |
| 4244 | free_path(paths->name[i].paths); |
| 4245 | free(paths->name[i].name); |
| 4246 | if(paths->name[i].preg) { |
| 4247 | regfree(paths->name[i].preg); |
| 4248 | free(paths->name[i].preg); |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | free(paths); |
| 4253 | } |
| 4254 | |
| 4255 | |
| 4256 | struct pathname *add_path(struct pathname *paths, char *target, char *alltarget) |
| 4257 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4258 | char *targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4259 | int i, error; |
| 4260 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4261 | target = get_component(target, &targname); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4262 | |
| 4263 | if(paths == NULL) { |
plougher | dec6ef1 | 2010-12-18 02:47:53 +0000 | [diff] [blame] | 4264 | paths = malloc(sizeof(struct pathname)); |
| 4265 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4266 | MEM_ERROR(); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4267 | |
| 4268 | paths->names = 0; |
| 4269 | paths->name = NULL; |
| 4270 | } |
| 4271 | |
| 4272 | for(i = 0; i < paths->names; i++) |
| 4273 | if(strcmp(paths->name[i].name, targname) == 0) |
| 4274 | break; |
| 4275 | |
| 4276 | if(i == paths->names) { |
| 4277 | /* allocate new name entry */ |
| 4278 | paths->names ++; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4279 | paths->name = realloc(paths->name, (i + 1) * |
| 4280 | sizeof(struct path_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4281 | if(paths->name == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4282 | MEM_ERROR(); |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4283 | paths->name[i].name = targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4284 | paths->name[i].paths = NULL; |
| 4285 | if(use_regex) { |
| 4286 | paths->name[i].preg = malloc(sizeof(regex_t)); |
plougher | 5c60eab | 2010-07-21 01:12:19 +0000 | [diff] [blame] | 4287 | if(paths->name[i].preg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4288 | MEM_ERROR(); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4289 | error = regcomp(paths->name[i].preg, targname, |
| 4290 | REG_EXTENDED|REG_NOSUB); |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4291 | if(error) { |
Phillip Lougher | 62859d2 | 2012-11-30 05:53:56 +0000 | [diff] [blame] | 4292 | char str[1024]; /* overflow safe */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4293 | |
| 4294 | regerror(error, paths->name[i].preg, str, 1024); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4295 | BAD_ERROR("invalid regex %s in export %s, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4296 | "because %s\n", targname, alltarget, |
| 4297 | str); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4298 | } |
| 4299 | } else |
| 4300 | paths->name[i].preg = NULL; |
| 4301 | |
| 4302 | if(target[0] == '\0') |
| 4303 | /* at leaf pathname component */ |
| 4304 | paths->name[i].paths = NULL; |
| 4305 | else |
| 4306 | /* recurse adding child components */ |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4307 | paths->name[i].paths = add_path(NULL, target, |
| 4308 | alltarget); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4309 | } else { |
| 4310 | /* existing matching entry */ |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4311 | free(targname); |
| 4312 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4313 | if(paths->name[i].paths == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4314 | /* No sub-directory which means this is the leaf |
| 4315 | * component of a pre-existing exclude which subsumes |
| 4316 | * the exclude currently being added, in which case stop |
| 4317 | * adding components */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4318 | } else if(target[0] == '\0') { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4319 | /* at leaf pathname component and child components exist |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4320 | * from more specific excludes, delete as they're |
| 4321 | * subsumed by this exclude */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4322 | free_path(paths->name[i].paths); |
| 4323 | paths->name[i].paths = NULL; |
| 4324 | } else |
| 4325 | /* recurse adding child components */ |
| 4326 | add_path(paths->name[i].paths, target, alltarget); |
| 4327 | } |
| 4328 | |
| 4329 | return paths; |
| 4330 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4331 | |
| 4332 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4333 | void add_exclude(char *target) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4334 | { |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4335 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4336 | if(target[0] == '/' || strncmp(target, "./", 2) == 0 || |
| 4337 | strncmp(target, "../", 3) == 0) |
| 4338 | BAD_ERROR("/, ./ and ../ prefixed excludes not supported with " |
| 4339 | "-wildcards or -regex options\n"); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4340 | else if(strncmp(target, "... ", 4) == 0) |
| 4341 | stickypath = add_path(stickypath, target + 4, target + 4); |
| 4342 | else |
| 4343 | path = add_path(path, target, target); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | |
| 4347 | void display_path(int depth, struct pathname *paths) |
| 4348 | { |
| 4349 | int i, n; |
| 4350 | |
| 4351 | if(paths == NULL) |
| 4352 | return; |
| 4353 | |
| 4354 | for(i = 0; i < paths->names; i++) { |
| 4355 | for(n = 0; n < depth; n++) |
| 4356 | printf("\t"); |
| 4357 | printf("%d: %s\n", depth, paths->name[i].name); |
| 4358 | display_path(depth + 1, paths->name[i].paths); |
| 4359 | } |
| 4360 | } |
| 4361 | |
| 4362 | |
| 4363 | void display_path2(struct pathname *paths, char *string) |
| 4364 | { |
| 4365 | int i; |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4366 | char *path; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4367 | |
| 4368 | if(paths == NULL) { |
| 4369 | printf("%s\n", string); |
| 4370 | return; |
| 4371 | } |
| 4372 | |
| 4373 | for(i = 0; i < paths->names; i++) { |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4374 | int res = asprintf(&path, "%s/%s", string, paths->name[i].name); |
| 4375 | if(res == -1) |
| 4376 | BAD_ERROR("asprintf failed in display_path2\n"); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4377 | display_path2(paths->name[i].paths, path); |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4378 | free(path); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4379 | } |
| 4380 | } |
| 4381 | |
| 4382 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4383 | struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path) |
| 4384 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4385 | int count = paths == NULL ? 0 : paths->count; |
| 4386 | |
| 4387 | if(count % PATHS_ALLOC_SIZE == 0) { |
| 4388 | paths = realloc(paths, sizeof(struct pathnames) + |
| 4389 | (count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4390 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4391 | MEM_ERROR(); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4392 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4393 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4394 | paths->path[count] = path; |
| 4395 | paths->count = count + 1; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4396 | return paths; |
| 4397 | } |
| 4398 | |
| 4399 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4400 | int excluded_match(char *name, struct pathname *path, struct pathnames **new) |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4401 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4402 | int i; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4403 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4404 | for(i = 0; i < path->names; i++) { |
| 4405 | int match = use_regex ? |
| 4406 | regexec(path->name[i].preg, name, (size_t) 0, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4407 | NULL, 0) == 0 : |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4408 | fnmatch(path->name[i].name, name, |
| 4409 | FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4410 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4411 | if(match) { |
| 4412 | if(path->name[i].paths == NULL || new == NULL) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4413 | /* match on a leaf component, any subdirectories |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4414 | * in the filesystem should be excluded */ |
| 4415 | return TRUE; |
| 4416 | else |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4417 | /* match on a non-leaf component, add any |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4418 | * subdirectories to the new set of |
| 4419 | * subdirectories to scan for this name */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4420 | *new = add_subdir(*new, path->name[i].paths); |
| 4421 | } |
| 4422 | } |
| 4423 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4424 | return FALSE; |
| 4425 | } |
| 4426 | |
| 4427 | |
| 4428 | int excluded(char *name, struct pathnames *paths, struct pathnames **new) |
| 4429 | { |
| 4430 | int n; |
| 4431 | |
| 4432 | if(stickypath && excluded_match(name, stickypath, NULL)) |
| 4433 | return TRUE; |
| 4434 | |
| 4435 | for(n = 0; paths && n < paths->count; n++) { |
| 4436 | int res = excluded_match(name, paths->path[n], new); |
| 4437 | if(res) { |
| 4438 | free(*new); |
| 4439 | *new = NULL; |
| 4440 | return TRUE; |
| 4441 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4444 | /* |
| 4445 | * Either: |
| 4446 | * - no matching names found, return empty new search set, or |
| 4447 | * - one or more matches with sub-directories found (no leaf matches), |
| 4448 | * in which case return new search set. |
| 4449 | * |
| 4450 | * In either case return FALSE as we don't want to exclude this entry |
| 4451 | */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4452 | return FALSE; |
| 4453 | } |
| 4454 | |
| 4455 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4456 | void process_exclude_file(char *argv) |
| 4457 | { |
| 4458 | FILE *fd; |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4459 | char buffer[MAX_LINE + 1]; /* overflow safe */ |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4460 | char *filename; |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4461 | |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4462 | fd = fopen(argv, "r"); |
| 4463 | if(fd == NULL) |
| 4464 | BAD_ERROR("Failed to open exclude file \"%s\" because %s\n", |
| 4465 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4466 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4467 | while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) { |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4468 | int len = strlen(filename); |
| 4469 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4470 | if(len == MAX_LINE && filename[len - 1] != '\n') |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4471 | /* line too large */ |
| 4472 | BAD_ERROR("Line too long when reading " |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4473 | "exclude file \"%s\", larger than %d " |
Phillip Lougher | 83847c1 | 2012-12-23 07:22:36 +0000 | [diff] [blame] | 4474 | "bytes\n", argv, MAX_LINE); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4475 | |
| 4476 | /* |
| 4477 | * Remove '\n' terminator if it exists (the last line |
| 4478 | * in the file may not be '\n' terminated) |
| 4479 | */ |
| 4480 | if(len && filename[len - 1] == '\n') |
| 4481 | filename[len - 1] = '\0'; |
| 4482 | |
| 4483 | /* Skip any leading whitespace */ |
| 4484 | while(isspace(*filename)) |
| 4485 | filename ++; |
| 4486 | |
| 4487 | /* if comment line, skip */ |
| 4488 | if(*filename == '#') |
| 4489 | continue; |
| 4490 | |
| 4491 | /* |
| 4492 | * check for initial backslash, to accommodate |
| 4493 | * filenames with leading space or leading # character |
| 4494 | */ |
| 4495 | if(*filename == '\\') |
| 4496 | filename ++; |
| 4497 | |
| 4498 | /* if line is now empty after skipping characters, skip it */ |
| 4499 | if(*filename == '\0') |
| 4500 | continue; |
| 4501 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4502 | if(old_exclude) |
| 4503 | old_add_exclude(filename); |
| 4504 | else |
| 4505 | add_exclude(filename); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | if(ferror(fd)) |
| 4509 | BAD_ERROR("Reading exclude file \"%s\" failed because %s\n", |
| 4510 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4511 | |
| 4512 | fclose(fd); |
| 4513 | } |
| 4514 | |
| 4515 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4516 | #define RECOVER_ID "Squashfs recovery file v1.0\n" |
| 4517 | #define RECOVER_ID_SIZE 28 |
| 4518 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4519 | void write_recovery_data(struct squashfs_super_block *sBlk) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4520 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4521 | int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4522 | pid_t pid = getpid(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4523 | char *metadata; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4524 | char header[] = RECOVER_ID; |
| 4525 | |
| 4526 | if(recover == FALSE) { |
| 4527 | printf("No recovery data option specified.\n"); |
plougher | eac1853 | 2007-10-29 05:26:06 +0000 | [diff] [blame] | 4528 | printf("Skipping saving recovery file.\n\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4529 | return; |
| 4530 | } |
| 4531 | |
plougher | 1b879bc | 2010-12-18 02:49:42 +0000 | [diff] [blame] | 4532 | metadata = malloc(bytes); |
| 4533 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4534 | MEM_ERROR(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4535 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4536 | res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata); |
Phillip Lougher | 477f433 | 2013-03-06 03:55:08 +0000 | [diff] [blame] | 4537 | if(res == 0) { |
| 4538 | ERROR("Failed to read append filesystem metadata\n"); |
| 4539 | BAD_ERROR("Filesystem corrupted?\n"); |
| 4540 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4541 | |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4542 | res = asprintf(&recovery_file, "squashfs_recovery_%s_%d", |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4543 | getbase(destination_file), pid); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4544 | if(res == -1) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4545 | MEM_ERROR(); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 4546 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4547 | recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU); |
| 4548 | if(recoverfd == -1) |
| 4549 | BAD_ERROR("Failed to create recovery file, because %s. " |
| 4550 | "Aborting\n", strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4551 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4552 | if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4553 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4554 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4555 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4556 | if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4557 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4558 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4559 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 4560 | if(write_bytes(recoverfd, metadata, bytes) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4561 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 4562 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4563 | |
| 4564 | close(recoverfd); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4565 | free(metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4566 | |
| 4567 | printf("Recovery file \"%s\" written\n", recovery_file); |
| 4568 | printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4569 | printf("mksquashfs dummy %s -recover %s\n", destination_file, |
| 4570 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4571 | printf("to restore filesystem\n\n"); |
| 4572 | } |
| 4573 | |
| 4574 | |
| 4575 | void read_recovery_data(char *recovery_file, char *destination_file) |
| 4576 | { |
| 4577 | int fd, recoverfd, bytes; |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4578 | struct squashfs_super_block orig_sBlk, sBlk; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4579 | char *metadata; |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4580 | int res; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4581 | struct stat buf; |
| 4582 | char header[] = RECOVER_ID; |
| 4583 | char header2[RECOVER_ID_SIZE]; |
| 4584 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4585 | recoverfd = open(recovery_file, O_RDONLY); |
| 4586 | if(recoverfd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4587 | BAD_ERROR("Failed to open recovery file because %s\n", |
| 4588 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4589 | |
| 4590 | if(stat(destination_file, &buf) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4591 | BAD_ERROR("Failed to stat destination file, because %s\n", |
| 4592 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4593 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4594 | fd = open(destination_file, O_RDWR); |
| 4595 | if(fd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4596 | BAD_ERROR("Failed to open destination file because %s\n", |
| 4597 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4598 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4599 | res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE); |
| 4600 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4601 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4602 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4603 | if(res < RECOVER_ID_SIZE) |
| 4604 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4605 | if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 ) |
| 4606 | BAD_ERROR("Not a recovery file\n"); |
| 4607 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4608 | res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4609 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4610 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4611 | strerror(errno)); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4612 | if(res < sizeof(struct squashfs_super_block)) |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4613 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4614 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4615 | 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] | 4616 | if(res == 0) { |
| 4617 | ERROR("Failed to read superblock from output filesystem\n"); |
| 4618 | BAD_ERROR("Output filesystem is empty!\n"); |
| 4619 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4620 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4621 | if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4, |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4622 | sizeof(struct squashfs_super_block) - 4) != 0) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4623 | BAD_ERROR("Recovery file and destination file do not seem to " |
| 4624 | "match\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4625 | |
| 4626 | bytes = sBlk.bytes_used - sBlk.inode_table_start; |
| 4627 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 4628 | metadata = malloc(bytes); |
| 4629 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4630 | MEM_ERROR(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4631 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4632 | res = read_bytes(recoverfd, metadata, bytes); |
| 4633 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4634 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 4635 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 4636 | if(res < bytes) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4637 | BAD_ERROR("Recovery file appears to be truncated\n"); |
| 4638 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4639 | write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4640 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 4641 | write_destination(fd, sBlk.inode_table_start, bytes, metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4642 | |
| 4643 | close(recoverfd); |
| 4644 | close(fd); |
| 4645 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4646 | printf("Successfully wrote recovery file \"%s\". Exiting\n", |
| 4647 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4648 | |
| 4649 | exit(0); |
| 4650 | } |
| 4651 | |
| 4652 | |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4653 | void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad) |
| 4654 | { |
| 4655 | int i; |
| 4656 | |
| 4657 | sBlk->fragments = fragments; |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4658 | sBlk->no_ids = id_count; |
| 4659 | sBlk->inode_table_start = write_inodes(); |
| 4660 | sBlk->directory_table_start = write_directories(); |
| 4661 | sBlk->fragment_table_start = write_fragment_table(); |
| 4662 | sBlk->lookup_table_start = exportable ? write_inode_lookup_table() : |
| 4663 | SQUASHFS_INVALID_BLK; |
| 4664 | sBlk->id_table_start = write_id_table(); |
| 4665 | sBlk->xattr_id_table_start = write_xattrs(); |
| 4666 | |
| 4667 | TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start); |
| 4668 | TRACE("sBlk->directory_table_start 0x%llx\n", |
| 4669 | sBlk->directory_table_start); |
| 4670 | TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start); |
| 4671 | if(exportable) |
| 4672 | TRACE("sBlk->lookup_table_start 0x%llx\n", |
| 4673 | sBlk->lookup_table_start); |
| 4674 | |
| 4675 | sBlk->bytes_used = bytes; |
| 4676 | |
| 4677 | sBlk->compression = comp->id; |
| 4678 | |
| 4679 | SQUASHFS_INSWAP_SUPER_BLOCK(sBlk); |
| 4680 | write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk); |
| 4681 | |
| 4682 | if(!nopad && (i = bytes & (4096 - 1))) { |
| 4683 | char temp[4096] = {0}; |
| 4684 | write_destination(fd, bytes, 4096 - i, temp); |
| 4685 | } |
| 4686 | |
| 4687 | close(fd); |
| 4688 | |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 4689 | if(recovery_file) |
| 4690 | unlink(recovery_file); |
| 4691 | |
| 4692 | total_bytes += total_inode_bytes + total_directory_bytes + |
| 4693 | sizeof(struct squashfs_super_block) + total_xattr_bytes; |
| 4694 | |
| 4695 | printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size" |
| 4696 | " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR, |
| 4697 | SQUASHFS_MINOR, comp->name, block_size); |
| 4698 | printf("\t%s data, %s metadata, %s fragments, %s xattrs\n", |
| 4699 | noD ? "uncompressed" : "compressed", noI ? "uncompressed" : |
| 4700 | "compressed", no_fragments ? "no" : noF ? "uncompressed" : |
| 4701 | "compressed", no_xattrs ? "no" : noX ? "uncompressed" : |
| 4702 | "compressed"); |
| 4703 | printf("\tduplicates are %sremoved\n", duplicate_checking ? "" : |
| 4704 | "not "); |
| 4705 | printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, |
| 4706 | bytes / (1024.0 * 1024.0)); |
| 4707 | printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n", |
| 4708 | ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0); |
| 4709 | printf("Inode table size %d bytes (%.2f Kbytes)\n", |
| 4710 | inode_bytes, inode_bytes / 1024.0); |
| 4711 | printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n", |
| 4712 | ((float) inode_bytes / total_inode_bytes) * 100.0, |
| 4713 | total_inode_bytes); |
| 4714 | printf("Directory table size %d bytes (%.2f Kbytes)\n", |
| 4715 | directory_bytes, directory_bytes / 1024.0); |
| 4716 | printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n", |
| 4717 | ((float) directory_bytes / total_directory_bytes) * 100.0, |
| 4718 | total_directory_bytes); |
| 4719 | if(total_xattr_bytes) { |
| 4720 | printf("Xattr table size %d bytes (%.2f Kbytes)\n", |
| 4721 | xattr_bytes, xattr_bytes / 1024.0); |
| 4722 | printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n", |
| 4723 | ((float) xattr_bytes / total_xattr_bytes) * 100.0, |
| 4724 | total_xattr_bytes); |
| 4725 | } |
| 4726 | if(duplicate_checking) |
| 4727 | printf("Number of duplicate files found %d\n", file_count - |
| 4728 | dup_files); |
| 4729 | else |
| 4730 | printf("No duplicate files removed\n"); |
| 4731 | printf("Number of inodes %d\n", inode_count); |
| 4732 | printf("Number of files %d\n", file_count); |
| 4733 | if(!no_fragments) |
| 4734 | printf("Number of fragments %d\n", fragments); |
| 4735 | printf("Number of symbolic links %d\n", sym_count); |
| 4736 | printf("Number of device nodes %d\n", dev_count); |
| 4737 | printf("Number of fifo nodes %d\n", fifo_count); |
| 4738 | printf("Number of socket nodes %d\n", sock_count); |
| 4739 | printf("Number of directories %d\n", dir_count); |
| 4740 | printf("Number of ids (unique uids + gids) %d\n", id_count); |
| 4741 | printf("Number of uids %d\n", uid_count); |
| 4742 | |
| 4743 | for(i = 0; i < id_count; i++) { |
| 4744 | if(id_table[i]->flags & ISA_UID) { |
| 4745 | struct passwd *user = getpwuid(id_table[i]->id); |
| 4746 | printf("\t%s (%d)\n", user == NULL ? "unknown" : |
| 4747 | user->pw_name, id_table[i]->id); |
| 4748 | } |
| 4749 | } |
| 4750 | |
| 4751 | printf("Number of gids %d\n", guid_count); |
| 4752 | |
| 4753 | for(i = 0; i < id_count; i++) { |
| 4754 | if(id_table[i]->flags & ISA_GID) { |
| 4755 | struct group *group = getgrgid(id_table[i]->id); |
| 4756 | printf("\t%s (%d)\n", group == NULL ? "unknown" : |
| 4757 | group->gr_name, id_table[i]->id); |
| 4758 | } |
| 4759 | } |
| 4760 | } |
| 4761 | |
| 4762 | |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 4763 | int parse_numberll(char *start, long long *res, int size) |
| 4764 | { |
| 4765 | char *end; |
| 4766 | long long number; |
| 4767 | |
| 4768 | errno = 0; /* To distinguish success/failure after call */ |
| 4769 | |
| 4770 | number = strtoll(start, &end, 10); |
| 4771 | |
| 4772 | /* |
| 4773 | * check for strtoll underflow or overflow in conversion, and other |
| 4774 | * errors. |
| 4775 | */ |
| 4776 | if((errno == ERANGE && (number == LLONG_MIN || number == LLONG_MAX)) || |
| 4777 | (errno != 0 && number == 0)) |
| 4778 | return 0; |
| 4779 | |
| 4780 | /* reject negative numbers as invalid */ |
| 4781 | if(number < 0) |
| 4782 | return 0; |
| 4783 | |
| 4784 | if(size) { |
| 4785 | /* |
| 4786 | * Check for multiplier and trailing junk. |
| 4787 | * But first check that a number exists before the |
| 4788 | * multiplier |
| 4789 | */ |
| 4790 | if(end == start) |
| 4791 | return 0; |
| 4792 | |
| 4793 | switch(end[0]) { |
| 4794 | case 'g': |
| 4795 | case 'G': |
| 4796 | if(multiply_overflowll(number, 1073741824)) |
| 4797 | return 0; |
| 4798 | number *= 1073741824; |
| 4799 | |
| 4800 | if(end[1] != '\0') |
| 4801 | /* trailing junk after multiplier, but |
| 4802 | * allow it to be "bytes" */ |
| 4803 | if(strcmp(end + 1, "bytes")) |
| 4804 | return 0; |
| 4805 | |
| 4806 | break; |
| 4807 | case 'm': |
| 4808 | case 'M': |
| 4809 | if(multiply_overflowll(number, 1048576)) |
| 4810 | return 0; |
| 4811 | number *= 1048576; |
| 4812 | |
| 4813 | if(end[1] != '\0') |
| 4814 | /* trailing junk after multiplier, but |
| 4815 | * allow it to be "bytes" */ |
| 4816 | if(strcmp(end + 1, "bytes")) |
| 4817 | return 0; |
| 4818 | |
| 4819 | break; |
| 4820 | case 'k': |
| 4821 | case 'K': |
| 4822 | if(multiply_overflowll(number, 1024)) |
| 4823 | return 0; |
| 4824 | number *= 1024; |
| 4825 | |
| 4826 | if(end[1] != '\0') |
| 4827 | /* trailing junk after multiplier, but |
| 4828 | * allow it to be "bytes" */ |
| 4829 | if(strcmp(end + 1, "bytes")) |
| 4830 | return 0; |
| 4831 | |
| 4832 | break; |
| 4833 | case '\0': |
| 4834 | break; |
| 4835 | default: |
| 4836 | /* trailing junk after number */ |
| 4837 | return 0; |
| 4838 | } |
| 4839 | } else if(end[0] != '\0') |
| 4840 | /* trailing junk after number */ |
| 4841 | return 0; |
| 4842 | |
| 4843 | *res = number; |
| 4844 | return 1; |
| 4845 | } |
| 4846 | |
| 4847 | |
| 4848 | int parse_number(char *start, int *res, int size) |
| 4849 | { |
| 4850 | long long number; |
| 4851 | |
| 4852 | if(!parse_numberll(start, &number, size)) |
| 4853 | return 0; |
| 4854 | |
| 4855 | /* check if long result will overflow signed int */ |
| 4856 | if(number > INT_MAX) |
| 4857 | return 0; |
| 4858 | |
| 4859 | *res = (int) number; |
| 4860 | return 1; |
| 4861 | } |
| 4862 | |
| 4863 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 4864 | int parse_num(char *arg, int *res) |
| 4865 | { |
| 4866 | return parse_number(arg, res, 0); |
| 4867 | } |
| 4868 | |
| 4869 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4870 | int get_physical_memory() |
| 4871 | { |
Phillip Lougher | 97ad5e8 | 2014-06-16 02:21:33 +0100 | [diff] [blame] | 4872 | /* |
| 4873 | * Long longs are used here because with PAE, a 32-bit |
| 4874 | * machine can have more than 4GB of physical memory |
| 4875 | * |
| 4876 | * sysconf(_SC_PHYS_PAGES) relies on /proc being mounted. |
| 4877 | * If it isn't fail. |
| 4878 | */ |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4879 | long long num_pages = sysconf(_SC_PHYS_PAGES); |
| 4880 | long long page_size = sysconf(_SC_PAGESIZE); |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 4881 | int phys_mem = num_pages * page_size >> 20; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4882 | |
Phillip Lougher | 97ad5e8 | 2014-06-16 02:21:33 +0100 | [diff] [blame] | 4883 | if(num_pages == -1 || page_size == -1) |
| 4884 | return 0; |
| 4885 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4886 | if(phys_mem < SQUASHFS_LOWMEM) |
| 4887 | BAD_ERROR("Mksquashfs requires more physical memory than is " |
| 4888 | "available!\n"); |
| 4889 | |
Phillip Lougher | 78784bb | 2014-04-28 04:08:40 +0100 | [diff] [blame] | 4890 | return phys_mem; |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 4891 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4892 | |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 4893 | |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 4894 | void check_usable_phys_mem(int total_mem) |
| 4895 | { |
| 4896 | /* |
| 4897 | * We want to allow users to use as much of their physical |
| 4898 | * memory as they wish. However, for practical reasons there are |
| 4899 | * limits which need to be imposed, to protect users from themselves |
| 4900 | * and to prevent people from using Mksquashfs as a DOS attack by using |
| 4901 | * all physical memory. Mksquashfs uses memory to cache data from disk |
| 4902 | * to optimise performance. It is pointless to ask it to use more |
| 4903 | * than 75% of physical memory, as this causes thrashing and it is thus |
| 4904 | * self-defeating. |
| 4905 | */ |
| 4906 | int mem = get_physical_memory(); |
| 4907 | |
| 4908 | mem = (mem >> 1) + (mem >> 2); /* 75% */ |
| 4909 | |
Phillip Lougher | 97ad5e8 | 2014-06-16 02:21:33 +0100 | [diff] [blame] | 4910 | if(total_mem > mem && mem) { |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 4911 | ERROR("Total memory requested is more than 75%% of physical " |
| 4912 | "memory.\n"); |
| 4913 | ERROR("Mksquashfs uses memory to cache data from disk to " |
| 4914 | "optimise performance.\n"); |
| 4915 | ERROR("It is pointless to ask it to use more than this amount " |
| 4916 | "of memory, as this\n"); |
| 4917 | ERROR("causes thrashing and it is thus self-defeating.\n"); |
| 4918 | BAD_ERROR("Requested memory size too large\n"); |
| 4919 | } |
| 4920 | |
| 4921 | if(sizeof(void *) == 4 && total_mem > 2048) { |
| 4922 | /* |
| 4923 | * If we're running on a kernel with PAE or on a 64-bit kernel, |
| 4924 | * then the 75% physical memory limit can still easily exceed |
| 4925 | * the addressable memory by this process. |
| 4926 | * |
| 4927 | * Due to the typical kernel/user-space split (1GB/3GB, or |
| 4928 | * 2GB/2GB), we have to conservatively assume the 32-bit |
| 4929 | * processes can only address 2-3GB. So refuse if the user |
| 4930 | * tries to allocate more than 2GB. |
| 4931 | */ |
| 4932 | ERROR("Total memory requested may exceed maximum " |
| 4933 | "addressable memory by this process\n"); |
| 4934 | BAD_ERROR("Requested memory size too large\n"); |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | |
| 4939 | int get_default_phys_mem() |
| 4940 | { |
Phillip Lougher | 97ad5e8 | 2014-06-16 02:21:33 +0100 | [diff] [blame] | 4941 | /* |
| 4942 | * get_physical_memory() relies on /proc being mounted. |
| 4943 | * If it fails, issue a warning, and use |
| 4944 | * SQUASHFS_LOWMEM / SQUASHFS_TAKE as default, |
| 4945 | * and allow a larger value to be set with -mem. |
| 4946 | */ |
| 4947 | int mem = get_physical_memory(); |
| 4948 | |
| 4949 | if(mem == 0) { |
| 4950 | mem = SQUASHFS_LOWMEM / SQUASHFS_TAKE; |
| 4951 | |
| 4952 | ERROR("Warning: Cannot get size of physical memory, probably " |
| 4953 | "because /proc is missing.\n"); |
| 4954 | ERROR("Warning: Defaulting to minimal use of %d Mbytes, use " |
| 4955 | "-mem to set a better value,\n", mem); |
| 4956 | ERROR("Warning: or fix /proc.\n"); |
| 4957 | } else |
| 4958 | mem /= SQUASHFS_TAKE; |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 4959 | |
| 4960 | if(sizeof(void *) == 4 && mem > 640) { |
| 4961 | /* |
| 4962 | * If we're running on a kernel with PAE or on a 64-bit kernel, |
| 4963 | * the default memory usage can exceed the addressable |
| 4964 | * memory by this process. |
| 4965 | * Due to the typical kernel/user-space split (1GB/3GB, or |
| 4966 | * 2GB/2GB), we have to conservatively assume the 32-bit |
| 4967 | * processes can only address 2-3GB. So limit the default |
| 4968 | * usage to 640M, which gives room for other data. |
| 4969 | */ |
| 4970 | mem = 640; |
| 4971 | } |
| 4972 | |
| 4973 | return mem; |
| 4974 | } |
| 4975 | |
| 4976 | |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 4977 | void calculate_queue_sizes(int mem, int *readq, int *fragq, int *bwriteq, |
| 4978 | int *fwriteq) |
| 4979 | { |
| 4980 | *readq = mem / SQUASHFS_READQ_MEM; |
| 4981 | *bwriteq = mem / SQUASHFS_BWRITEQ_MEM; |
| 4982 | *fwriteq = mem / SQUASHFS_FWRITEQ_MEM; |
| 4983 | *fragq = mem - *readq - *bwriteq - *fwriteq; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4987 | #define VERSION() \ |
Phillip Lougher | b29dd5e | 2014-06-23 04:46:50 +0100 | [diff] [blame] | 4988 | printf("mksquashfs version 4.3-git (2014/06/22)\n");\ |
Phillip Lougher | cc06495 | 2014-01-03 04:26:34 +0000 | [diff] [blame] | 4989 | printf("copyright (C) 2014 Phillip Lougher "\ |
Phillip Lougher | 83d42a3 | 2012-10-31 23:42:22 +0000 | [diff] [blame] | 4990 | "<phillip@squashfs.org.uk>\n\n"); \ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4991 | printf("This program is free software; you can redistribute it and/or"\ |
| 4992 | "\n");\ |
| 4993 | printf("modify it under the terms of the GNU General Public License"\ |
| 4994 | "\n");\ |
| 4995 | printf("as published by the Free Software Foundation; either version "\ |
| 4996 | "2,\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4997 | printf("or (at your option) any later version.\n\n");\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 4998 | printf("This program is distributed in the hope that it will be "\ |
| 4999 | "useful,\n");\ |
| 5000 | printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\ |
| 5001 | "of\n");\ |
| 5002 | printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\ |
| 5003 | "\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5004 | printf("GNU General Public License for more details.\n"); |
| 5005 | int main(int argc, char *argv[]) |
| 5006 | { |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5007 | struct stat buf, source_buf; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 5008 | int res, i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5009 | char *b, *root_name = NULL; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5010 | int keep_as_directory = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5011 | squashfs_inode inode; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5012 | int readq; |
| 5013 | int fragq; |
| 5014 | int bwriteq; |
| 5015 | int fwriteq; |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 5016 | int total_mem = get_default_phys_mem(); |
Phillip Lougher | 569a963 | 2013-04-22 03:30:30 +0100 | [diff] [blame] | 5017 | int progress = TRUE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5018 | int force_progress = FALSE; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5019 | struct file_buffer **fragment = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5020 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5021 | if(argc > 1 && strcmp(argv[1], "-version") == 0) { |
| 5022 | VERSION(); |
| 5023 | exit(0); |
| 5024 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5025 | |
| 5026 | block_log = slog(block_size); |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5027 | calculate_queue_sizes(total_mem, &readq, &fragq, &bwriteq, &fwriteq); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5028 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5029 | for(i = 1; i < argc && argv[i][0] != '-'; i++); |
| 5030 | if(i < 3) |
| 5031 | goto printOptions; |
| 5032 | source_path = argv + 1; |
| 5033 | source = i - 2; |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5034 | |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 5035 | /* |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5036 | * Scan the command line for -comp xxx option, this is to ensure |
| 5037 | * any -X compressor specific options are passed to the |
| 5038 | * correct compressor |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 5039 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5040 | for(; i < argc; i++) { |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5041 | struct compressor *prev_comp = comp; |
| 5042 | |
| 5043 | if(strcmp(argv[i], "-comp") == 0) { |
| 5044 | if(++i == argc) { |
| 5045 | ERROR("%s: -comp missing compression type\n", |
| 5046 | argv[0]); |
| 5047 | exit(1); |
| 5048 | } |
| 5049 | comp = lookup_compressor(argv[i]); |
| 5050 | if(!comp->supported) { |
| 5051 | ERROR("%s: Compressor \"%s\" is not supported!" |
| 5052 | "\n", argv[0], argv[i]); |
| 5053 | ERROR("%s: Compressors available:\n", argv[0]); |
| 5054 | display_compressors("", COMP_DEFAULT); |
| 5055 | exit(1); |
| 5056 | } |
| 5057 | if(prev_comp != NULL && prev_comp != comp) { |
| 5058 | ERROR("%s: -comp multiple conflicting -comp" |
| 5059 | " options specified on command line" |
| 5060 | ", previously %s, now %s\n", argv[0], |
| 5061 | prev_comp->name, comp->name); |
| 5062 | exit(1); |
| 5063 | } |
| 5064 | compressor_opt_parsed = 1; |
| 5065 | |
| 5066 | } else if(strcmp(argv[i], "-e") == 0) |
| 5067 | break; |
| 5068 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
| 5069 | strcmp(argv[i], "-ef") == 0 || |
| 5070 | strcmp(argv[i], "-pf") == 0 || |
| 5071 | strcmp(argv[i], "-af") == 0 || |
| 5072 | strcmp(argv[i], "-comp") == 0) |
| 5073 | i++; |
| 5074 | } |
| 5075 | |
| 5076 | /* |
| 5077 | * if no -comp option specified lookup default compressor. Note the |
| 5078 | * Makefile ensures the default compressor has been built, and so we |
| 5079 | * don't need to to check for failure here |
| 5080 | */ |
| 5081 | if(comp == NULL) |
| 5082 | comp = lookup_compressor(COMP_DEFAULT); |
| 5083 | |
| 5084 | for(i = source + 2; i < argc; i++) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 5085 | if(strcmp(argv[i], "-action") == 0 || |
| 5086 | strcmp(argv[i], "-a") ==0) { |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5087 | if(++i == argc) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 5088 | ERROR("%s: %s missing action\n", |
| 5089 | argv[0], argv[i - 1]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5090 | exit(1); |
| 5091 | } |
| 5092 | res = parse_action(argv[i]); |
| 5093 | if(res == 0) |
| 5094 | exit(1); |
| 5095 | |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5096 | } else if(strcmp(argv[i], "-af") == 0) { |
| 5097 | if(++i == argc) { |
| 5098 | ERROR("%s: -af missing filename\n", argv[0]); |
| 5099 | exit(1); |
| 5100 | } |
| 5101 | if(read_action_file(argv[i]) == FALSE) |
| 5102 | exit(1); |
| 5103 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5104 | } else if(strcmp(argv[i], "-comp") == 0) |
| 5105 | /* parsed previously */ |
| 5106 | i++; |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 5107 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5108 | else if(strncmp(argv[i], "-X", 2) == 0) { |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 5109 | int args; |
| 5110 | |
| 5111 | if(strcmp(argv[i] + 2, "help") == 0) |
| 5112 | goto print_compressor_options; |
| 5113 | |
| 5114 | args = compressor_options(comp, argv + i, argc - i); |
plougher | d886567 | 2010-11-27 05:05:49 +0000 | [diff] [blame] | 5115 | if(args < 0) { |
| 5116 | if(args == -1) { |
| 5117 | ERROR("%s: Unrecognised compressor" |
| 5118 | " option %s\n", argv[0], |
| 5119 | argv[i]); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5120 | if(!compressor_opt_parsed) |
| 5121 | ERROR("%s: Did you forget to" |
| 5122 | " specify -comp?\n", |
| 5123 | argv[0]); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 5124 | print_compressor_options: |
Phillip Lougher | b1c3b6a | 2014-01-06 05:30:00 +0000 | [diff] [blame] | 5125 | ERROR("%s: selected compressor \"%s\"" |
| 5126 | ". Options supported: %s\n", |
| 5127 | argv[0], comp->name, |
| 5128 | comp->usage ? "" : "none"); |
| 5129 | if(comp->usage) |
| 5130 | comp->usage(); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5131 | } |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 5132 | exit(1); |
| 5133 | } |
| 5134 | i += args; |
| 5135 | |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 5136 | } else if(strcmp(argv[i], "-pf") == 0) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5137 | if(++i == argc) { |
| 5138 | ERROR("%s: -pf missing filename\n", argv[0]); |
| 5139 | exit(1); |
| 5140 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 5141 | if(read_pseudo_file(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5142 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5143 | } else if(strcmp(argv[i], "-p") == 0) { |
| 5144 | if(++i == argc) { |
| 5145 | ERROR("%s: -p missing pseudo file definition\n", |
| 5146 | argv[0]); |
| 5147 | exit(1); |
| 5148 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 5149 | if(read_pseudo_def(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5150 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5151 | } else if(strcmp(argv[i], "-recover") == 0) { |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5152 | if(++i == argc) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5153 | ERROR("%s: -recover missing recovery file\n", |
| 5154 | argv[0]); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5155 | exit(1); |
| 5156 | } |
| 5157 | read_recovery_data(argv[i], argv[source + 1]); |
| 5158 | } else if(strcmp(argv[i], "-no-recovery") == 0) |
| 5159 | recover = FALSE; |
| 5160 | else if(strcmp(argv[i], "-wildcards") == 0) { |
plougher | 934a9ed | 2007-10-19 00:21:10 +0000 | [diff] [blame] | 5161 | old_exclude = FALSE; |
| 5162 | use_regex = FALSE; |
| 5163 | } else if(strcmp(argv[i], "-regex") == 0) { |
| 5164 | old_exclude = FALSE; |
| 5165 | use_regex = TRUE; |
| 5166 | } else if(strcmp(argv[i], "-no-sparse") == 0) |
plougher | 1f54edc | 2007-08-12 23:13:36 +0000 | [diff] [blame] | 5167 | sparse_files = FALSE; |
| 5168 | else if(strcmp(argv[i], "-no-progress") == 0) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 5169 | progress = FALSE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5170 | else if(strcmp(argv[i], "-progress") == 0) |
| 5171 | force_progress = TRUE; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 5172 | else if(strcmp(argv[i], "-no-exports") == 0) |
| 5173 | exportable = FALSE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5174 | else if(strcmp(argv[i], "-processors") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5175 | if((++i == argc) || !parse_num(argv[i], &processors)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5176 | ERROR("%s: -processors missing or invalid " |
| 5177 | "processor number\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5178 | exit(1); |
| 5179 | } |
| 5180 | if(processors < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5181 | ERROR("%s: -processors should be 1 or larger\n", |
| 5182 | argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5183 | exit(1); |
| 5184 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5185 | } else if(strcmp(argv[i], "-read-queue") == 0) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5186 | if((++i == argc) || !parse_num(argv[i], &readq)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5187 | ERROR("%s: -read-queue missing or invalid " |
| 5188 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5189 | exit(1); |
| 5190 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5191 | if(readq < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5192 | ERROR("%s: -read-queue should be 1 megabyte or " |
| 5193 | "larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5194 | exit(1); |
| 5195 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5196 | } else if(strcmp(argv[i], "-write-queue") == 0) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5197 | if((++i == argc) || !parse_num(argv[i], &bwriteq)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5198 | ERROR("%s: -write-queue missing or invalid " |
| 5199 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5200 | exit(1); |
| 5201 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5202 | if(bwriteq < 2) { |
| 5203 | ERROR("%s: -write-queue should be 2 megabytes " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5204 | "or larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5205 | exit(1); |
| 5206 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5207 | fwriteq = bwriteq >> 1; |
| 5208 | bwriteq -= fwriteq; |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5209 | } else if(strcmp(argv[i], "-fragment-queue") == 0) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5210 | if((++i == argc) || !parse_num(argv[i], &fragq)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5211 | ERROR("%s: -fragment-queue missing or invalid " |
| 5212 | "queue size\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5213 | exit(1); |
| 5214 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5215 | if(fragq < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5216 | ERROR("%s: -fragment-queue should be 1 " |
| 5217 | "megabyte or larger\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5218 | exit(1); |
| 5219 | } |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5220 | } else if(strcmp(argv[i], "-mem") == 0) { |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 5221 | long long number; |
| 5222 | |
| 5223 | if((++i == argc) || |
| 5224 | !parse_numberll(argv[i], &number, 1)) { |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5225 | ERROR("%s: -mem missing or invalid mem size\n", |
| 5226 | argv[0]); |
| 5227 | exit(1); |
| 5228 | } |
Phillip Lougher | 368becc | 2014-06-11 04:51:37 +0100 | [diff] [blame] | 5229 | |
| 5230 | /* |
| 5231 | * convert from bytes to Mbytes, ensuring the value |
| 5232 | * does not overflow a signed int |
| 5233 | */ |
| 5234 | if(number >= (1LL << 51)) { |
| 5235 | ERROR("%s: -mem invalid mem size\n", argv[0]); |
| 5236 | exit(1); |
| 5237 | } |
| 5238 | |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 5239 | total_mem = number / 1048576; |
Phillip Lougher | 07def8b | 2014-05-11 19:56:11 +0100 | [diff] [blame] | 5240 | if(total_mem < (SQUASHFS_LOWMEM / SQUASHFS_TAKE)) { |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5241 | ERROR("%s: -mem should be %d Mbytes or " |
Phillip Lougher | 07def8b | 2014-05-11 19:56:11 +0100 | [diff] [blame] | 5242 | "larger\n", argv[0], |
| 5243 | SQUASHFS_LOWMEM / SQUASHFS_TAKE); |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5244 | exit(1); |
| 5245 | } |
| 5246 | calculate_queue_sizes(total_mem, &readq, &fragq, |
| 5247 | &bwriteq, &fwriteq); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5248 | } else if(strcmp(argv[i], "-b") == 0) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5249 | if(++i == argc) { |
| 5250 | ERROR("%s: -b missing block size\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5251 | exit(1); |
| 5252 | } |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5253 | if(!parse_number(argv[i], &block_size, 1)) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5254 | ERROR("%s: -b invalid block size\n", argv[0]); |
| 5255 | exit(1); |
| 5256 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5257 | if((block_log = slog(block_size)) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5258 | ERROR("%s: -b block size not power of two or " |
| 5259 | "not between 4096 and 1Mbyte\n", |
| 5260 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5261 | exit(1); |
| 5262 | } |
| 5263 | } else if(strcmp(argv[i], "-ef") == 0) { |
| 5264 | if(++i == argc) { |
| 5265 | ERROR("%s: -ef missing filename\n", argv[0]); |
| 5266 | exit(1); |
| 5267 | } |
plougher | 9b5bf8c | 2006-03-20 18:43:33 +0000 | [diff] [blame] | 5268 | } else if(strcmp(argv[i], "-no-duplicates") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5269 | duplicate_checking = FALSE; |
| 5270 | |
| 5271 | else if(strcmp(argv[i], "-no-fragments") == 0) |
| 5272 | no_fragments = TRUE; |
| 5273 | |
| 5274 | else if(strcmp(argv[i], "-always-use-fragments") == 0) |
| 5275 | always_use_fragments = TRUE; |
| 5276 | |
| 5277 | else if(strcmp(argv[i], "-sort") == 0) { |
| 5278 | if(++i == argc) { |
| 5279 | ERROR("%s: -sort missing filename\n", argv[0]); |
| 5280 | exit(1); |
| 5281 | } |
| 5282 | } else if(strcmp(argv[i], "-all-root") == 0 || |
| 5283 | strcmp(argv[i], "-root-owned") == 0) |
| 5284 | global_uid = global_gid = 0; |
| 5285 | |
| 5286 | else if(strcmp(argv[i], "-force-uid") == 0) { |
| 5287 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5288 | ERROR("%s: -force-uid missing uid or user\n", |
| 5289 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5290 | exit(1); |
| 5291 | } |
| 5292 | if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5293 | if(global_uid < 0 || global_uid > |
| 5294 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5295 | ERROR("%s: -force-uid uid out of range" |
| 5296 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5297 | exit(1); |
| 5298 | } |
| 5299 | } else { |
| 5300 | struct passwd *uid = getpwnam(argv[i]); |
| 5301 | if(uid) |
| 5302 | global_uid = uid->pw_uid; |
| 5303 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5304 | ERROR("%s: -force-uid invalid uid or " |
| 5305 | "unknown user\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5306 | exit(1); |
| 5307 | } |
| 5308 | } |
| 5309 | } else if(strcmp(argv[i], "-force-gid") == 0) { |
| 5310 | if(++i == argc) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5311 | ERROR("%s: -force-gid missing gid or group\n", |
| 5312 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5313 | exit(1); |
| 5314 | } |
| 5315 | if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5316 | if(global_gid < 0 || global_gid > |
| 5317 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5318 | ERROR("%s: -force-gid gid out of range" |
| 5319 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5320 | exit(1); |
| 5321 | } |
| 5322 | } else { |
| 5323 | struct group *gid = getgrnam(argv[i]); |
| 5324 | if(gid) |
| 5325 | global_gid = gid->gr_gid; |
| 5326 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5327 | ERROR("%s: -force-gid invalid gid or " |
| 5328 | "unknown group\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5329 | exit(1); |
| 5330 | } |
| 5331 | } |
| 5332 | } else if(strcmp(argv[i], "-noI") == 0 || |
| 5333 | strcmp(argv[i], "-noInodeCompression") == 0) |
| 5334 | noI = TRUE; |
| 5335 | |
| 5336 | else if(strcmp(argv[i], "-noD") == 0 || |
| 5337 | strcmp(argv[i], "-noDataCompression") == 0) |
| 5338 | noD = TRUE; |
| 5339 | |
| 5340 | else if(strcmp(argv[i], "-noF") == 0 || |
| 5341 | strcmp(argv[i], "-noFragmentCompression") == 0) |
| 5342 | noF = TRUE; |
| 5343 | |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5344 | else if(strcmp(argv[i], "-noX") == 0 || |
| 5345 | strcmp(argv[i], "-noXattrCompression") == 0) |
| 5346 | noX = TRUE; |
| 5347 | |
plougher | ce564c6 | 2010-05-19 03:01:49 +0000 | [diff] [blame] | 5348 | else if(strcmp(argv[i], "-no-xattrs") == 0) |
| 5349 | no_xattrs = TRUE; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 5350 | |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5351 | else if(strcmp(argv[i], "-xattrs") == 0) |
| 5352 | no_xattrs = FALSE; |
| 5353 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5354 | else if(strcmp(argv[i], "-nopad") == 0) |
| 5355 | nopad = TRUE; |
| 5356 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5357 | else if(strcmp(argv[i], "-info") == 0) |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5358 | silent = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5359 | |
| 5360 | else if(strcmp(argv[i], "-e") == 0) |
| 5361 | break; |
| 5362 | |
| 5363 | else if(strcmp(argv[i], "-noappend") == 0) |
| 5364 | delete = TRUE; |
| 5365 | |
| 5366 | else if(strcmp(argv[i], "-keep-as-directory") == 0) |
| 5367 | keep_as_directory = TRUE; |
| 5368 | |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 5369 | else if(strcmp(argv[i], "-exit-on-error") == 0) |
| 5370 | exit_on_error = TRUE; |
| 5371 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5372 | else if(strcmp(argv[i], "-root-becomes") == 0) { |
| 5373 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5374 | ERROR("%s: -root-becomes: missing name\n", |
| 5375 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5376 | exit(1); |
| 5377 | } |
| 5378 | root_name = argv[i]; |
| 5379 | } else if(strcmp(argv[i], "-version") == 0) { |
| 5380 | VERSION(); |
| 5381 | } else { |
| 5382 | ERROR("%s: invalid option\n\n", argv[0]); |
| 5383 | printOptions: |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5384 | ERROR("SYNTAX:%s source1 source2 ... dest [options] " |
| 5385 | "[-e list of exclude\ndirs/files]\n", argv[0]); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5386 | ERROR("\nFilesystem build options:\n"); |
plougher | ff5ea8b | 2009-08-07 19:33:10 +0000 | [diff] [blame] | 5387 | ERROR("-comp <comp>\t\tselect <comp> compression\n"); |
plougher | 13df178 | 2009-08-29 01:05:34 +0000 | [diff] [blame] | 5388 | ERROR("\t\t\tCompressors available:\n"); |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 5389 | display_compressors("\t\t\t", COMP_DEFAULT); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5390 | ERROR("-b <block_size>\t\tset data block to " |
Phillip Lougher | b74a59c | 2014-04-30 03:10:44 +0100 | [diff] [blame] | 5391 | "<block_size>. Default 128 Kbytes\n"); |
| 5392 | ERROR("\t\t\tOptionally a suffix of K or M can be" |
| 5393 | " given to specify\n\t\t\tKbytes or Mbytes" |
| 5394 | " respectively\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5395 | ERROR("-no-exports\t\tdon't make the filesystem " |
| 5396 | "exportable via NFS\n"); |
| 5397 | ERROR("-no-sparse\t\tdon't detect sparse files\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5398 | ERROR("-no-xattrs\t\tdon't store extended attributes" |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5399 | NOXOPT_STR "\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 5400 | ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5401 | "\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5402 | ERROR("-noI\t\t\tdo not compress inode table\n"); |
| 5403 | ERROR("-noD\t\t\tdo not compress data blocks\n"); |
| 5404 | ERROR("-noF\t\t\tdo not compress fragment blocks\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5405 | ERROR("-noX\t\t\tdo not compress extended " |
| 5406 | "attributes\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5407 | ERROR("-no-fragments\t\tdo not use fragments\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5408 | ERROR("-always-use-fragments\tuse fragment blocks for " |
| 5409 | "files larger than block size\n"); |
| 5410 | ERROR("-no-duplicates\t\tdo not perform duplicate " |
| 5411 | "checking\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5412 | ERROR("-all-root\t\tmake all files owned by root\n"); |
| 5413 | ERROR("-force-uid uid\t\tset all file uids to uid\n"); |
| 5414 | ERROR("-force-gid gid\t\tset all file gids to gid\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5415 | ERROR("-nopad\t\t\tdo not pad filesystem to a multiple " |
| 5416 | "of 4K\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5417 | ERROR("-keep-as-directory\tif one source directory is " |
| 5418 | "specified, create a root\n"); |
| 5419 | ERROR("\t\t\tdirectory containing that directory, " |
| 5420 | "rather than the\n"); |
| 5421 | ERROR("\t\t\tcontents of the directory\n"); |
| 5422 | ERROR("\nFilesystem filter options:\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5423 | ERROR("-p <pseudo-definition>\tAdd pseudo file " |
| 5424 | "definition\n"); |
| 5425 | ERROR("-pf <pseudo-file>\tAdd list of pseudo file " |
| 5426 | "definitions\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5427 | ERROR("-sort <sort_file>\tsort files according to " |
| 5428 | "priorities in <sort_file>. One\n"); |
| 5429 | ERROR("\t\t\tfile or dir with priority per line. " |
| 5430 | "Priority -32768 to\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5431 | ERROR("\t\t\t32767, default priority 0\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5432 | ERROR("-ef <exclude_file>\tlist of exclude dirs/files." |
| 5433 | " One per line\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5434 | ERROR("-wildcards\t\tAllow extended shell wildcards " |
| 5435 | "(globbing) to be used in\n\t\t\texclude " |
| 5436 | "dirs/files\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5437 | ERROR("-regex\t\t\tAllow POSIX regular expressions to " |
| 5438 | "be used in exclude\n\t\t\tdirs/files\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5439 | ERROR("\nFilesystem append options:\n"); |
| 5440 | ERROR("-noappend\t\tdo not append to existing " |
| 5441 | "filesystem\n"); |
| 5442 | ERROR("-root-becomes <name>\twhen appending source " |
| 5443 | "files/directories, make the\n"); |
| 5444 | ERROR("\t\t\toriginal root become a subdirectory in " |
| 5445 | "the new root\n"); |
| 5446 | ERROR("\t\t\tcalled <name>, rather than adding the new " |
| 5447 | "source items\n"); |
| 5448 | ERROR("\t\t\tto the original root\n"); |
| 5449 | ERROR("\nMksquashfs runtime options:\n"); |
| 5450 | ERROR("-version\t\tprint version, licence and " |
| 5451 | "copyright message\n"); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 5452 | ERROR("-exit-on-error\t\ttreat normally ignored errors " |
| 5453 | "as fatal\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5454 | ERROR("-recover <name>\t\trecover filesystem data " |
| 5455 | "using recovery file <name>\n"); |
| 5456 | ERROR("-no-recovery\t\tdon't generate a recovery " |
| 5457 | "file\n"); |
| 5458 | ERROR("-info\t\t\tprint files written to filesystem\n"); |
| 5459 | ERROR("-no-progress\t\tdon't display the progress " |
| 5460 | "bar\n"); |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5461 | ERROR("-progress\t\tdisplay progress bar when using " |
| 5462 | "the -info option\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5463 | ERROR("-processors <number>\tUse <number> processors." |
| 5464 | " By default will use number of\n"); |
| 5465 | ERROR("\t\t\tprocessors available\n"); |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 5466 | ERROR("-mem <size>\t\tUse <size> physical memory. " |
| 5467 | "Currently set to %dM\n", total_mem); |
| 5468 | ERROR("\t\t\tOptionally a suffix of K, M or G can be" |
| 5469 | " given to specify\n\t\t\tKbytes, Mbytes or" |
| 5470 | " Gbytes respectively\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 5471 | ERROR("\nMiscellaneous options:\n"); |
| 5472 | ERROR("-root-owned\t\talternative name for -all-root" |
| 5473 | "\n"); |
| 5474 | ERROR("-noInodeCompression\talternative name for -noI" |
| 5475 | "\n"); |
| 5476 | ERROR("-noDataCompression\talternative name for -noD" |
| 5477 | "\n"); |
| 5478 | ERROR("-noFragmentCompression\talternative name for " |
| 5479 | "-noF\n"); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5480 | ERROR("-noXattrCompression\talternative name for " |
| 5481 | "-noX\n"); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 5482 | ERROR("\n-Xhelp\t\t\tprint compressor options for" |
| 5483 | " selected compressor\n"); |
plougher | 4fb6682 | 2010-12-08 02:49:28 +0000 | [diff] [blame] | 5484 | ERROR("\nCompressors available and compressor specific " |
| 5485 | "options:\n"); |
| 5486 | display_compressor_usage(COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5487 | exit(1); |
| 5488 | } |
| 5489 | } |
| 5490 | |
plougher | b747f4f | 2010-12-25 04:05:47 +0000 | [diff] [blame] | 5491 | /* |
| 5492 | * Some compressors may need the options to be checked for validity |
| 5493 | * once all the options have been processed |
| 5494 | */ |
| 5495 | res = compressor_options_post(comp, block_size); |
| 5496 | if(res) |
| 5497 | EXIT_MKSQUASHFS(); |
| 5498 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5499 | /* |
| 5500 | * If the -info option has been selected then disable the |
| 5501 | * progress bar unless it has been explicitly enabled with |
| 5502 | * the -progress option |
| 5503 | */ |
| 5504 | if(!silent) |
| 5505 | progress = force_progress; |
| 5506 | |
| 5507 | #ifdef SQUASHFS_TRACE |
Phillip Lougher | 989f5fe | 2013-04-22 03:16:29 +0100 | [diff] [blame] | 5508 | /* |
| 5509 | * Disable progress bar if full debug tracing is enabled. |
| 5510 | * The progress bar in this case just gets in the way of the |
| 5511 | * debug trace output |
| 5512 | */ |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5513 | progress = FALSE; |
| 5514 | #endif |
| 5515 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5516 | for(i = 0; i < source; i++) |
| 5517 | if(lstat(source_path[i], &source_buf) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5518 | fprintf(stderr, "Cannot stat source directory \"%s\" " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5519 | "because %s\n", source_path[i], |
| 5520 | strerror(errno)); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 5521 | EXIT_MKSQUASHFS(); |
| 5522 | } |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5523 | |
| 5524 | destination_file = argv[source + 1]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5525 | if(stat(argv[source + 1], &buf) == -1) { |
| 5526 | if(errno == ENOENT) { /* Does not exist */ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5527 | fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, |
plougher | 59dce67 | 2010-05-19 03:56:59 +0000 | [diff] [blame] | 5528 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5529 | if(fd == -1) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5530 | perror("Could not create destination file"); |
| 5531 | exit(1); |
| 5532 | } |
| 5533 | delete = TRUE; |
| 5534 | } else { |
| 5535 | perror("Could not stat destination file"); |
| 5536 | exit(1); |
| 5537 | } |
| 5538 | |
| 5539 | } else { |
| 5540 | if(S_ISBLK(buf.st_mode)) { |
| 5541 | if((fd = open(argv[source + 1], O_RDWR)) == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5542 | perror("Could not open block device as " |
| 5543 | "destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5544 | exit(1); |
| 5545 | } |
| 5546 | block_device = 1; |
| 5547 | |
| 5548 | } else if(S_ISREG(buf.st_mode)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5549 | fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | |
| 5550 | O_RDWR); |
| 5551 | if(fd == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5552 | perror("Could not open regular file for " |
| 5553 | "writing as destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5554 | exit(1); |
| 5555 | } |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 5556 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5557 | else { |
| 5558 | ERROR("Destination not block device or regular file\n"); |
| 5559 | exit(1); |
| 5560 | } |
| 5561 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5562 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5563 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5564 | /* |
| 5565 | * process the exclude files - must be done afer destination file has |
| 5566 | * been possibly created |
| 5567 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5568 | for(i = source + 2; i < argc; i++) |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 5569 | if(strcmp(argv[i], "-ef") == 0) |
| 5570 | /* |
| 5571 | * Note presence of filename arg has already |
| 5572 | * been checked |
| 5573 | */ |
| 5574 | process_exclude_file(argv[++i]); |
| 5575 | else if(strcmp(argv[i], "-e") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5576 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5577 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5578 | strcmp(argv[i], "-sort") == 0 || |
| 5579 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5580 | strcmp(argv[i], "-af") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5581 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5582 | i++; |
| 5583 | |
| 5584 | if(i != argc) { |
| 5585 | if(++i == argc) { |
| 5586 | ERROR("%s: -e missing arguments\n", argv[0]); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5587 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5588 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 5589 | while(i < argc) |
| 5590 | if(old_exclude) |
| 5591 | old_add_exclude(argv[i++]); |
| 5592 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 5593 | add_exclude(argv[i++]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | /* process the sort files - must be done afer the exclude files */ |
| 5597 | for(i = source + 2; i < argc; i++) |
| 5598 | if(strcmp(argv[i], "-sort") == 0) { |
plougher | e1c9a74 | 2010-07-21 16:59:05 +0000 | [diff] [blame] | 5599 | int res = read_sort_file(argv[++i], source, |
| 5600 | source_path); |
| 5601 | if(res == FALSE) |
| 5602 | BAD_ERROR("Failed to read sort file\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5603 | sorted ++; |
| 5604 | } else if(strcmp(argv[i], "-e") == 0) |
| 5605 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5606 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5607 | strcmp(argv[i], "-ef") == 0 || |
| 5608 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5609 | strcmp(argv[i], "-af") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 5610 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5611 | i++; |
| 5612 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5613 | if(!delete) { |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 5614 | comp = read_super(fd, &sBlk, argv[source + 1]); |
| 5615 | if(comp == NULL) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5616 | ERROR("Failed to read existing filesystem - will not " |
| 5617 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5618 | ERROR("To force Mksquashfs to write to this block " |
| 5619 | "device or file use -noappend\n"); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5620 | EXIT_MKSQUASHFS(); |
| 5621 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5622 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5623 | block_log = slog(block_size = sBlk.block_size); |
| 5624 | noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags); |
| 5625 | noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags); |
| 5626 | noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5627 | noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5628 | no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags); |
| 5629 | always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags); |
| 5630 | duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5631 | exportable = SQUASHFS_EXPORTABLE(sBlk.flags); |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5632 | no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5633 | comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5634 | } |
| 5635 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5636 | initialise_threads(readq, fragq, bwriteq, fwriteq, delete, |
| 5637 | destination_file); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5638 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 5639 | res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0); |
| 5640 | if(res) |
| 5641 | BAD_ERROR("compressor_init failed\n"); |
| 5642 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5643 | if(delete) { |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5644 | int size; |
Phillip Lougher | a45c9d2 | 2011-02-20 04:24:07 +0000 | [diff] [blame] | 5645 | void *comp_data = compressor_dump_options(comp, block_size, |
| 5646 | &size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5647 | |
plougher | df6d8f0 | 2009-03-20 03:10:00 +0000 | [diff] [blame] | 5648 | printf("Creating %d.%d filesystem on %s, block size %d.\n", |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5649 | SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5650 | |
plougher | 871c72a | 2010-12-25 04:17:27 +0000 | [diff] [blame] | 5651 | /* |
| 5652 | * store any compressor specific options after the superblock, |
| 5653 | * and set the COMP_OPT flag to show that the filesystem has |
| 5654 | * compressor specfic options |
| 5655 | */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5656 | if(comp_data) { |
| 5657 | unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT; |
| 5658 | |
| 5659 | SQUASHFS_INSWAP_SHORTS(&c_byte, 1); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5660 | write_destination(fd, sizeof(struct squashfs_super_block), |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 5661 | sizeof(c_byte), &c_byte); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5662 | write_destination(fd, sizeof(struct squashfs_super_block) + |
plougher | 8e4dad4 | 2010-12-28 05:56:22 +0000 | [diff] [blame] | 5663 | sizeof(c_byte), size, comp_data); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5664 | bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte) |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5665 | + size; |
| 5666 | comp_opts = TRUE; |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5667 | } else |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5668 | bytes = sizeof(struct squashfs_super_block); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5669 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5670 | unsigned int last_directory_block, inode_dir_offset, |
| 5671 | inode_dir_file_size, root_inode_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5672 | inode_dir_start_block, uncompressed_data, |
| 5673 | compressed_data, inode_dir_inode_number, |
| 5674 | inode_dir_parent_inode; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5675 | unsigned int root_inode_start = |
| 5676 | SQUASHFS_INODE_BLK(sBlk.root_inode), |
| 5677 | root_inode_offset = |
| 5678 | SQUASHFS_INODE_OFFSET(sBlk.root_inode); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5679 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5680 | if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, |
| 5681 | &data_cache, &directory_table, |
| 5682 | &directory_data_cache, &last_directory_block, |
| 5683 | &inode_dir_offset, &inode_dir_file_size, |
| 5684 | &root_inode_size, &inode_dir_start_block, |
| 5685 | &file_count, &sym_count, &dev_count, &dir_count, |
| 5686 | &fifo_count, &sock_count, &total_bytes, |
| 5687 | &total_inode_bytes, &total_directory_bytes, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5688 | &inode_dir_inode_number, |
| 5689 | &inode_dir_parent_inode, add_old_root_entry, |
| 5690 | &fragment_table, &inode_lookup_table)) == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5691 | ERROR("Failed to read existing filesystem - will not " |
| 5692 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5693 | ERROR("To force Mksquashfs to write to this block " |
| 5694 | "device or file use -noappend\n"); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5695 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5696 | } |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 5697 | if((append_fragments = fragments = sBlk.fragments)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5698 | fragment_table = realloc((char *) fragment_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5699 | ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 5700 | * sizeof(struct squashfs_fragment_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 5701 | if(fragment_table == NULL) |
| 5702 | BAD_ERROR("Out of memory in save filesystem state\n"); |
| 5703 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5704 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5705 | printf("Appending to existing %d.%d filesystem on %s, block " |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5706 | "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5707 | block_size); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5708 | printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, " |
plougher | bb98803 | 2009-08-06 08:46:34 +0000 | [diff] [blame] | 5709 | "-always-use-fragments,\n-exportable and -comp options " |
| 5710 | "ignored\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5711 | printf("\nIf appending is not wanted, please re-run with " |
| 5712 | "-noappend specified!\n\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5713 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5714 | compressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5715 | ~(SQUASHFS_METADATA_SIZE - 1); |
| 5716 | uncompressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 5717 | (SQUASHFS_METADATA_SIZE - 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5718 | |
| 5719 | /* save original filesystem state for restoring ... */ |
| 5720 | sfragments = fragments; |
| 5721 | sbytes = bytes; |
| 5722 | sinode_count = sBlk.inodes; |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 5723 | scache_bytes = root_inode_offset + root_inode_size; |
| 5724 | sdirectory_cache_bytes = uncompressed_data; |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5725 | sdata_cache = malloc(scache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5726 | if(sdata_cache == NULL) |
| 5727 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 5728 | sdirectory_data_cache = malloc(sdirectory_cache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5729 | if(sdirectory_data_cache == NULL) |
| 5730 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5731 | memcpy(sdata_cache, data_cache, scache_bytes); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5732 | memcpy(sdirectory_data_cache, directory_data_cache + |
| 5733 | compressed_data, sdirectory_cache_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5734 | sinode_bytes = root_inode_start; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5735 | stotal_bytes = total_bytes; |
| 5736 | stotal_inode_bytes = total_inode_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5737 | stotal_directory_bytes = total_directory_bytes + |
| 5738 | compressed_data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5739 | sfile_count = file_count; |
| 5740 | ssym_count = sym_count; |
| 5741 | sdev_count = dev_count; |
| 5742 | sdir_count = dir_count + 1; |
| 5743 | sfifo_count = fifo_count; |
| 5744 | ssock_count = sock_count; |
| 5745 | sdup_files = dup_files; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 5746 | sid_count = id_count; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5747 | write_recovery_data(&sBlk); |
Phillip Lougher | 3bcf67d | 2013-02-24 10:56:38 +0000 | [diff] [blame] | 5748 | save_xattrs(); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 5749 | appending = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5750 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5751 | /* |
| 5752 | * set the filesystem state up to be able to append to the |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5753 | * original filesystem. The filesystem state differs depending |
| 5754 | * on whether we're appending to the original root directory, or |
| 5755 | * if the original root directory becomes a sub-directory |
| 5756 | * (root-becomes specified on command line, here root_name != |
| 5757 | * NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5758 | */ |
| 5759 | inode_bytes = inode_size = root_inode_start; |
| 5760 | directory_size = last_directory_block; |
| 5761 | cache_size = root_inode_offset + root_inode_size; |
| 5762 | directory_cache_size = inode_dir_offset + inode_dir_file_size; |
| 5763 | if(root_name) { |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5764 | sdirectory_bytes = last_directory_block; |
| 5765 | sdirectory_compressed_bytes = 0; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5766 | root_inode_number = inode_dir_parent_inode; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5767 | inode_no = sBlk.inodes + 2; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5768 | directory_bytes = last_directory_block; |
| 5769 | directory_cache_bytes = uncompressed_data; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5770 | memmove(directory_data_cache, directory_data_cache + |
| 5771 | compressed_data, uncompressed_data); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5772 | cache_bytes = root_inode_offset + root_inode_size; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5773 | add_old_root_entry(root_name, sBlk.root_inode, |
| 5774 | inode_dir_inode_number, SQUASHFS_DIR_TYPE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5775 | total_directory_bytes += compressed_data; |
| 5776 | dir_count ++; |
| 5777 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5778 | sdirectory_compressed_bytes = last_directory_block - |
| 5779 | inode_dir_start_block; |
| 5780 | sdirectory_compressed = |
| 5781 | malloc(sdirectory_compressed_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 5782 | if(sdirectory_compressed == NULL) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5783 | BAD_ERROR("Out of memory in save filesystem " |
| 5784 | "state\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5785 | memcpy(sdirectory_compressed, directory_table + |
| 5786 | inode_dir_start_block, |
| 5787 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 5788 | sdirectory_bytes = inode_dir_start_block; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 5789 | root_inode_number = inode_dir_inode_number; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 5790 | inode_no = sBlk.inodes + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5791 | directory_bytes = inode_dir_start_block; |
| 5792 | directory_cache_bytes = inode_dir_offset; |
| 5793 | cache_bytes = root_inode_offset; |
| 5794 | } |
| 5795 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5796 | inode_count = file_count + dir_count + sym_count + dev_count + |
| 5797 | fifo_count + sock_count; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5798 | } |
| 5799 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 5800 | if(path) |
| 5801 | paths = add_subdir(paths, path); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 5802 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5803 | dump_actions(); |
Phillip Lougher | 7a28f7a | 2014-04-20 23:47:08 +0100 | [diff] [blame] | 5804 | dump_pseudos(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5805 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5806 | if(delete && !keep_as_directory && source == 1 && |
| 5807 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5808 | dir_scan(&inode, source_path[0], scan1_readdir, progress); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5809 | else if(!keep_as_directory && source == 1 && |
| 5810 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5811 | dir_scan(&inode, source_path[0], scan1_single_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5812 | else |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 5813 | dir_scan(&inode, "", scan1_encomp_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5814 | sBlk.root_inode = inode; |
| 5815 | sBlk.inodes = inode_count; |
| 5816 | sBlk.s_magic = SQUASHFS_MAGIC; |
| 5817 | sBlk.s_major = SQUASHFS_MAJOR; |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 5818 | sBlk.s_minor = SQUASHFS_MINOR; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5819 | sBlk.block_size = block_size; |
| 5820 | sBlk.block_log = block_log; |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 5821 | sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments, |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 5822 | always_use_fragments, duplicate_checking, exportable, |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 5823 | no_xattrs, comp_opts); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5824 | sBlk.mkfs_time = time(NULL); |
| 5825 | |
Phillip Lougher | 83b8f86 | 2013-04-09 03:28:55 +0100 | [diff] [blame] | 5826 | disable_info(); |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 5827 | |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5828 | while((fragment = get_frag_action(fragment))) |
| 5829 | write_fragment(*fragment); |
| 5830 | unlock_fragments(); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 5831 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5832 | pthread_mutex_lock(&fragment_mutex); |
| 5833 | while(fragments_outstanding) { |
| 5834 | pthread_mutex_unlock(&fragment_mutex); |
| 5835 | sched_yield(); |
| 5836 | pthread_mutex_lock(&fragment_mutex); |
| 5837 | } |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 5838 | pthread_cleanup_pop(1); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5839 | |
| 5840 | queue_put(to_writer, NULL); |
| 5841 | if(queue_get(from_writer) != 0) |
| 5842 | EXIT_MKSQUASHFS(); |
| 5843 | |
Phillip Lougher | cd1a5a6 | 2014-01-21 04:53:46 +0000 | [diff] [blame] | 5844 | set_progressbar_state(FALSE); |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 5845 | write_filesystem_tables(&sBlk, nopad); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5846 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5847 | return 0; |
| 5848 | } |