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, |
Colin Cross | be75490 | 2017-12-09 22:27:51 +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> |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 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 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 54 | #ifndef FNM_EXTMATCH /* glibc extension */ |
| 55 | #define FNM_EXTMATCH 0 |
| 56 | #endif |
| 57 | |
plougher | 6885329 | 2005-12-27 02:47:04 +0000 | [diff] [blame] | 58 | #ifndef linux |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 59 | #define __BYTE_ORDER BYTE_ORDER |
| 60 | #define __BIG_ENDIAN BIG_ENDIAN |
| 61 | #define __LITTLE_ENDIAN LITTLE_ENDIAN |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 62 | #include <sys/sysctl.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 63 | #else |
| 64 | #include <endian.h> |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 65 | #include <sys/sysinfo.h> |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 68 | #include "squashfs_fs.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 69 | #include "squashfs_swap.h" |
| 70 | #include "mksquashfs.h" |
| 71 | #include "sort.h" |
| 72 | #include "pseudo.h" |
| 73 | #include "compressor.h" |
| 74 | #include "xattr.h" |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 75 | #include "action.h" |
Phillip Lougher | 2477d0d | 2012-10-24 21:49:28 +0100 | [diff] [blame] | 76 | #include "error.h" |
Phillip Lougher | 9a0e8bb | 2013-03-10 02:39:24 +0000 | [diff] [blame] | 77 | #include "progressbar.h" |
Phillip Lougher | 2e9fab1 | 2013-04-09 03:26:33 +0100 | [diff] [blame] | 78 | #include "info.h" |
Phillip Lougher | 71f3964 | 2013-04-09 04:43:21 +0100 | [diff] [blame] | 79 | #include "caches-queues-lists.h" |
Phillip Lougher | 1bbd0cc | 2013-04-19 04:03:00 +0100 | [diff] [blame] | 80 | #include "read_fs.h" |
Phillip Lougher | 0d67c9d | 2013-04-19 05:06:50 +0100 | [diff] [blame] | 81 | #include "restore.h" |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 82 | #include "process_fragments.h" |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 83 | |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 84 | /* ANDROID CHANGES START*/ |
| 85 | #ifdef ANDROID |
| 86 | #include "android.h" |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 87 | #include "private/android_filesystem_config.h" |
| 88 | #include "private/canned_fs_config.h" |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 89 | int android_config = FALSE; |
Mohamad Ayyash | e031629 | 2015-02-24 19:21:29 -0800 | [diff] [blame] | 90 | char *context_file = NULL; |
Mohamad Ayyash | 18fe5f6 | 2015-03-02 16:00:56 -0800 | [diff] [blame] | 91 | char *mount_point = NULL; |
Thierry Strudel | 1a710ff | 2015-07-09 16:34:38 -0700 | [diff] [blame] | 92 | char *target_out_path = NULL; |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 93 | fs_config_func_t fs_config_func = NULL; |
Mohamad Ayyash | 4dfe4eb | 2016-06-03 18:49:44 -0700 | [diff] [blame] | 94 | int compress_thresh_per = 0; |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 95 | int align_4k_blocks = TRUE; |
| 96 | FILE *block_map_file = NULL; |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 97 | #endif |
| 98 | /* ANDROID CHANGES END */ |
| 99 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 100 | int delete = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 101 | int fd; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 102 | struct squashfs_super_block sBlk; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 103 | |
| 104 | /* filesystem flags for building */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 105 | int comp_opts = FALSE; |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 106 | int no_xattrs = XATTR_DEF; |
| 107 | int noX = FALSE; |
| 108 | int duplicate_checking = TRUE; |
| 109 | int noF = FALSE; |
| 110 | int no_fragments = FALSE; |
| 111 | int always_use_fragments = FALSE; |
| 112 | int noI = FALSE; |
| 113 | int noD = FALSE; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 114 | int silent = TRUE; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 115 | int exportable = TRUE; |
plougher | 8dcc699 | 2007-08-17 19:32:52 +0000 | [diff] [blame] | 116 | int sparse_files = TRUE; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 117 | int old_exclude = TRUE; |
| 118 | int use_regex = FALSE; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 119 | int nopad = FALSE; |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 120 | int exit_on_error = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 121 | |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 122 | long long global_uid = -1, global_gid = -1; |
| 123 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 124 | /* superblock attributes */ |
| 125 | int block_size = SQUASHFS_FILE_SIZE, block_log; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 126 | unsigned int id_count = 0; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 127 | int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0, |
| 128 | sock_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 129 | |
Mohan Srinivasan | 58e18d7 | 2016-07-26 15:06:29 -0700 | [diff] [blame] | 130 | /* ANDROID CHANGES START*/ |
| 131 | #ifdef ANDROID |
| 132 | int whitelisted_count = 0; |
| 133 | #endif |
| 134 | /* ANDROID CHANGES END */ |
| 135 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 136 | /* write position within data section */ |
| 137 | long long bytes = 0, total_bytes = 0; |
| 138 | |
| 139 | /* in memory directory table - possibly compressed */ |
| 140 | char *directory_table = NULL; |
| 141 | unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0; |
| 142 | |
| 143 | /* cached directory table */ |
| 144 | char *directory_data_cache = NULL; |
| 145 | unsigned int directory_cache_bytes = 0, directory_cache_size = 0; |
| 146 | |
| 147 | /* in memory inode table - possibly compressed */ |
| 148 | char *inode_table = NULL; |
| 149 | unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0; |
| 150 | |
| 151 | /* cached inode table */ |
| 152 | char *data_cache = NULL; |
| 153 | unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0; |
| 154 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 155 | /* inode lookup table */ |
| 156 | squashfs_inode *inode_lookup_table = NULL; |
| 157 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 158 | /* in memory directory data */ |
| 159 | #define I_COUNT_SIZE 128 |
| 160 | #define DIR_ENTRIES 32 |
| 161 | #define INODE_HASH_SIZE 65536 |
| 162 | #define INODE_HASH_MASK (INODE_HASH_SIZE - 1) |
| 163 | #define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK) |
| 164 | |
| 165 | struct cached_dir_index { |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 166 | struct squashfs_dir_index index; |
| 167 | char *name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | struct directory { |
| 171 | unsigned int start_block; |
| 172 | unsigned int size; |
| 173 | unsigned char *buff; |
| 174 | unsigned char *p; |
| 175 | unsigned int entry_count; |
| 176 | unsigned char *entry_count_p; |
| 177 | unsigned int i_count; |
| 178 | unsigned int i_size; |
| 179 | struct cached_dir_index *index; |
| 180 | unsigned char *index_count_p; |
| 181 | unsigned int inode_number; |
| 182 | }; |
| 183 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 184 | struct inode_info *inode_info[INODE_HASH_SIZE]; |
| 185 | |
| 186 | /* hash tables used to do fast duplicate searches in duplicate check */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 187 | struct file_info *dupl[65536]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 188 | int dup_files = 0; |
| 189 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 190 | /* exclude file handling */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 191 | /* list of exclude dirs/files */ |
| 192 | struct exclude_info { |
| 193 | dev_t st_dev; |
| 194 | ino_t st_ino; |
| 195 | }; |
| 196 | |
| 197 | #define EXCLUDE_SIZE 8192 |
| 198 | int exclude = 0; |
| 199 | struct exclude_info *exclude_paths = NULL; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 200 | int old_excluded(char *filename, struct stat *buf); |
| 201 | |
| 202 | struct path_entry { |
| 203 | char *name; |
| 204 | regex_t *preg; |
| 205 | struct pathname *paths; |
| 206 | }; |
| 207 | |
| 208 | struct pathname { |
| 209 | int names; |
| 210 | struct path_entry *name; |
| 211 | }; |
| 212 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 213 | struct pathnames { |
| 214 | int count; |
| 215 | struct pathname *path[0]; |
| 216 | }; |
| 217 | #define PATHS_ALLOC_SIZE 10 |
| 218 | |
| 219 | struct pathnames *paths = NULL; |
| 220 | struct pathname *path = NULL; |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 221 | struct pathname *stickypath = NULL; |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 222 | int excluded(char *name, struct pathnames *paths, struct pathnames **new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 223 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 224 | int fragments = 0; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 225 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 226 | #define FRAG_SIZE 32768 |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 227 | |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 228 | struct squashfs_fragment_entry *fragment_table = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 229 | int fragments_outstanding = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 230 | |
Phillip Lougher | 7ffdf2a | 2013-04-14 02:53:30 +0100 | [diff] [blame] | 231 | int fragments_locked = FALSE; |
Phillip Lougher | 7ffdf2a | 2013-04-14 02:53:30 +0100 | [diff] [blame] | 232 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 233 | /* current inode number for directories and non directories */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 234 | unsigned int inode_no = 1; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 235 | unsigned int root_inode_number = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 236 | |
| 237 | /* list of source dirs/files */ |
| 238 | int source = 0; |
| 239 | char **source_path; |
| 240 | |
| 241 | /* list of root directory entries read from original filesystem */ |
| 242 | int old_root_entries = 0; |
| 243 | struct old_root_entry_info { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 244 | char *name; |
| 245 | struct inode_info inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 246 | }; |
| 247 | struct old_root_entry_info *old_root_entry; |
| 248 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 249 | /* restore orignal filesystem state if appending to existing filesystem is |
| 250 | * cancelled */ |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 251 | int appending = FALSE; |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 252 | char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 253 | |
| 254 | long long sbytes, stotal_bytes; |
| 255 | |
| 256 | unsigned int sinode_bytes, scache_bytes, sdirectory_bytes, |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 257 | sdirectory_cache_bytes, sdirectory_compressed_bytes, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 258 | stotal_inode_bytes, stotal_directory_bytes, |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 259 | sinode_count = 0, sfile_count, ssym_count, sdev_count, |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 260 | sdir_count, sfifo_count, ssock_count, sdup_files; |
| 261 | int sfragments; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 262 | int threads; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 263 | |
| 264 | /* flag whether destination file is a block device */ |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 265 | int block_device = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 266 | |
| 267 | /* flag indicating whether files are sorted using sort list(s) */ |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 268 | int sorted = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 269 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 270 | /* save destination file name for deleting on error */ |
| 271 | char *destination_file = NULL; |
| 272 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 273 | /* recovery file for abnormal exit on appending */ |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 274 | char *recovery_file = NULL; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 275 | int recover = TRUE; |
| 276 | |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 277 | /* uid/gid mapping tables */ |
| 278 | #define UGID_ENTRIES 340 |
| 279 | |
| 280 | struct ugid_map_entry { |
| 281 | unsigned int child_id; |
| 282 | unsigned int parent_id; |
| 283 | unsigned int length; |
| 284 | }; |
| 285 | struct ugid_map_entry uid_mapping[UGID_ENTRIES], gid_mapping[UGID_ENTRIES]; |
| 286 | unsigned int uid_map_count = 0, gid_map_count = 0; |
| 287 | |
| 288 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 289 | struct id *id_hash_table[ID_ENTRIES]; |
| 290 | struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS]; |
| 291 | unsigned int uid_count = 0, guid_count = 0; |
| 292 | unsigned int sid_count = 0, suid_count = 0, sguid_count = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 293 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 294 | struct cache *reader_buffer, *fragment_buffer, *reserve_cache; |
| 295 | struct cache *bwriter_buffer, *fwriter_buffer; |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 296 | struct queue *to_reader, *to_deflate, *to_writer, *from_writer, |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 297 | *to_frag, *locked_fragment, *to_process_frag; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 298 | struct seq_queue *to_main; |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 299 | pthread_t reader_thread, writer_thread, main_thread; |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 300 | pthread_t *deflator_thread, *frag_deflator_thread, *frag_thread; |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 301 | pthread_t *restore_thread = NULL; |
Phillip Lougher | 94e658f | 2014-03-13 02:59:04 +0000 | [diff] [blame] | 302 | pthread_mutex_t fragment_mutex = PTHREAD_MUTEX_INITIALIZER; |
Phillip Lougher | ea8cb65 | 2014-03-13 02:57:19 +0000 | [diff] [blame] | 303 | pthread_mutex_t pos_mutex = PTHREAD_MUTEX_INITIALIZER; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 304 | pthread_mutex_t dup_mutex = PTHREAD_MUTEX_INITIALIZER; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 305 | |
| 306 | /* user options that control parallelisation */ |
| 307 | int processors = -1; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 308 | int bwriter_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 309 | |
plougher | c5d5987 | 2010-11-22 01:36:01 +0000 | [diff] [blame] | 310 | /* compression operations */ |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 311 | struct compressor *comp = NULL; |
Phillip Lougher | 434d50c | 2014-01-21 03:05:36 +0000 | [diff] [blame] | 312 | int compressor_opt_parsed = FALSE; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 313 | void *stream = NULL; |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 314 | |
plougher | 860c1f3 | 2010-08-11 01:37:17 +0000 | [diff] [blame] | 315 | /* xattr stats */ |
| 316 | unsigned int xattr_bytes = 0, total_xattr_bytes = 0; |
| 317 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 318 | /* fragment to file mapping used when appending */ |
| 319 | int append_fragments = 0; |
| 320 | struct append_file **file_mapping; |
| 321 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 322 | /* root of the in-core directory structure */ |
| 323 | struct dir_info *root_dir; |
| 324 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 325 | static char *read_from_disk(long long start, unsigned int avail_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 326 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 327 | int type); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 328 | struct file_info *duplicate(long long file_size, long long bytes, |
| 329 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 330 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 331 | int checksum_flag); |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 332 | struct dir_info *dir_scan1(char *, char *, struct pathnames *, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 333 | struct dir_ent *(_readdir)(struct dir_info *), int); |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 334 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo); |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 335 | void dir_scan3(struct dir_info *dir); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 336 | void dir_scan4(struct dir_info *dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 337 | void dir_scan5(struct dir_info *dir); |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 338 | void dir_scan6(struct dir_info *dir); |
| 339 | void dir_scan7(squashfs_inode *inode, struct dir_info *dir_info); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 340 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 341 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 342 | unsigned short checksum, unsigned short fragment_checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 343 | int checksum_flag, int checksum_frag_flag); |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 344 | long long generic_write_table(int, void *, int, void *, int); |
plougher | ca61d1c | 2010-07-20 18:32:32 +0000 | [diff] [blame] | 345 | void restorefs(); |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 346 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 347 | void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 348 | unsigned short get_checksum_mem(char *buff, int bytes); |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 349 | void check_usable_phys_mem(int total_mem); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 350 | |
Mohan Srinivasan | 58e18d7 | 2016-07-26 15:06:29 -0700 | [diff] [blame] | 351 | /* ANDROID CHANGES START*/ |
| 352 | #ifdef ANDROID |
| 353 | static int whitelisted(struct stat *buf); |
| 354 | static void add_whitelist_entry(char *filename, struct stat *buf); |
| 355 | static int add_whitelist(char *path); |
| 356 | static void process_whitelist_file(char *argv); |
| 357 | |
| 358 | #define WHITELIST_SIZE 8192 |
| 359 | int whitelist = 0; |
| 360 | |
| 361 | struct whitelist_info { |
| 362 | dev_t st_dev; |
| 363 | ino_t st_ino; |
| 364 | }; |
| 365 | char *whitelist_filename = NULL; |
| 366 | struct whitelist_info *whitelist_paths = NULL; |
| 367 | #endif |
| 368 | /* ANDROID CHANGES END */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 369 | |
Phillip Lougher | 9b7c3c2 | 2013-03-10 02:29:42 +0000 | [diff] [blame] | 370 | void prep_exit() |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 371 | { |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 372 | if(restore_thread) { |
| 373 | if(pthread_self() == *restore_thread) { |
| 374 | /* |
| 375 | * Recursive failure when trying to restore filesystem! |
| 376 | * Nothing to do except to exit, otherwise we'll just |
| 377 | * appear to hang. The user should be able to restore |
| 378 | * from the recovery file (which is why it was added, in |
| 379 | * case of catastrophic failure in Mksquashfs) |
| 380 | */ |
| 381 | exit(1); |
| 382 | } else { |
| 383 | /* signal the restore thread to restore */ |
Phillip Lougher | 8c74081 | 2014-01-30 01:56:17 +0000 | [diff] [blame] | 384 | pthread_kill(*restore_thread, SIGUSR1); |
Phillip Lougher | cf135d3 | 2013-04-08 03:43:25 +0100 | [diff] [blame] | 385 | pthread_exit(NULL); |
| 386 | } |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 387 | } else if(delete) { |
| 388 | if(destination_file && !block_device) |
| 389 | unlink(destination_file); |
| 390 | } else if(recovery_file) |
| 391 | unlink(recovery_file); |
Phillip Lougher | 838d296 | 2012-10-23 03:29:30 +0100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 395 | int add_overflow(int a, int b) |
| 396 | { |
| 397 | return (INT_MAX - a) < b; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | int shift_overflow(int a, int shift) |
| 402 | { |
| 403 | return (INT_MAX >> shift) < a; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | int multiply_overflow(int a, int multiplier) |
| 408 | { |
| 409 | return (INT_MAX / multiplier) < a; |
| 410 | } |
| 411 | |
| 412 | |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 413 | int multiply_overflowll(long long a, int multiplier) |
| 414 | { |
| 415 | return (LLONG_MAX / multiplier) < a; |
| 416 | } |
| 417 | |
| 418 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 419 | #define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \ |
| 420 | + (((char *)A) - data_cache))) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 421 | |
| 422 | |
| 423 | void restorefs() |
| 424 | { |
| 425 | ERROR("Exiting - restoring original filesystem!\n\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 426 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 427 | bytes = sbytes; |
| 428 | memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 429 | memcpy(directory_data_cache, sdirectory_data_cache, |
| 430 | sdirectory_cache_bytes); |
| 431 | directory_cache_bytes = sdirectory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 432 | inode_bytes = sinode_bytes; |
| 433 | directory_bytes = sdirectory_bytes; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 434 | memcpy(directory_table + directory_bytes, sdirectory_compressed, |
| 435 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 436 | directory_bytes += sdirectory_compressed_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 437 | total_bytes = stotal_bytes; |
| 438 | total_inode_bytes = stotal_inode_bytes; |
| 439 | total_directory_bytes = stotal_directory_bytes; |
| 440 | inode_count = sinode_count; |
| 441 | file_count = sfile_count; |
| 442 | sym_count = ssym_count; |
| 443 | dev_count = sdev_count; |
| 444 | dir_count = sdir_count; |
| 445 | fifo_count = sfifo_count; |
| 446 | sock_count = ssock_count; |
| 447 | dup_files = sdup_files; |
| 448 | fragments = sfragments; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 449 | id_count = sid_count; |
plougher | 21f63b3 | 2010-07-18 03:59:04 +0000 | [diff] [blame] | 450 | restore_xattrs(); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 451 | write_filesystem_tables(&sBlk, nopad); |
| 452 | exit(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 456 | void sighandler() |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 457 | { |
| 458 | EXIT_MKSQUASHFS(); |
| 459 | } |
| 460 | |
| 461 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 462 | int mangle2(void *strm, char *d, char *s, int size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 463 | int block_size, int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 464 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 465 | int error, c_byte = 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 466 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 467 | if(!uncompressed) { |
plougher | bfb876c | 2010-12-31 07:48:01 +0000 | [diff] [blame] | 468 | c_byte = compressor_compress(comp, strm, d, s, size, block_size, |
| 469 | &error); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 470 | if(c_byte == -1) |
| 471 | BAD_ERROR("mangle2:: %s compress failed with error " |
| 472 | "code %d\n", comp->name, error); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 475 | if(c_byte == 0 || c_byte >= size || |
| 476 | (c_byte > (size * ((100.0 - compress_thresh_per) / 100.0)))) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 477 | memcpy(d, s, size); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 478 | return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : |
| 479 | SQUASHFS_COMPRESSED_BIT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 480 | } |
| 481 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 482 | return c_byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 486 | int mangle(char *d, char *s, int size, int block_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 487 | int uncompressed, int data_block) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 488 | { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 489 | return mangle2(stream, d, s, size, block_size, uncompressed, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 490 | data_block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 494 | void *get_inode(int req_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 495 | { |
| 496 | int data_space; |
| 497 | unsigned short c_byte; |
| 498 | |
| 499 | while(cache_bytes >= SQUASHFS_METADATA_SIZE) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 500 | if((inode_size - inode_bytes) < |
| 501 | ((SQUASHFS_METADATA_SIZE << 1)) + 2) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 502 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 503 | (SQUASHFS_METADATA_SIZE << 1) + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 504 | if(it == NULL) |
| 505 | MEM_ERROR(); |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 506 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 507 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
| 508 | } |
| 509 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 510 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 511 | data_cache, SQUASHFS_METADATA_SIZE, |
| 512 | SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 513 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 514 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 515 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 516 | total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 517 | memmove(data_cache, data_cache + SQUASHFS_METADATA_SIZE, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 518 | cache_bytes - SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 519 | cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 520 | } |
| 521 | |
| 522 | data_space = (cache_size - cache_bytes); |
| 523 | if(data_space < req_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 524 | int realloc_size = cache_size == 0 ? |
| 525 | ((req_size + SQUASHFS_METADATA_SIZE) & |
| 526 | ~(SQUASHFS_METADATA_SIZE - 1)) : req_size - |
| 527 | data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 528 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 529 | void *dc = realloc(data_cache, cache_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 530 | realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 531 | if(dc == NULL) |
| 532 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 533 | cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 534 | data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | cache_bytes += req_size; |
| 538 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 539 | return data_cache + cache_bytes - req_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 543 | int read_bytes(int fd, void *buff, int bytes) |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 544 | { |
| 545 | int res, count; |
| 546 | |
| 547 | for(count = 0; count < bytes; count += res) { |
| 548 | res = read(fd, buff + count, bytes - count); |
| 549 | if(res < 1) { |
plougher | 96f85a1 | 2009-03-29 22:39:59 +0000 | [diff] [blame] | 550 | if(res == 0) |
| 551 | goto bytes_read; |
| 552 | else if(errno != EINTR) { |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 553 | ERROR("Read failed because %s\n", |
| 554 | strerror(errno)); |
| 555 | return -1; |
| 556 | } else |
| 557 | res = 0; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | bytes_read: |
| 562 | return count; |
| 563 | } |
| 564 | |
| 565 | |
plougher | 3306cb2 | 2010-06-18 04:22:44 +0000 | [diff] [blame] | 566 | int read_fs_bytes(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 567 | { |
| 568 | off_t off = byte; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 569 | int res = 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 570 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 571 | TRACE("read_fs_bytes: reading from position 0x%llx, bytes %d\n", |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 572 | byte, bytes); |
plougher | 06a19d3 | 2009-03-29 22:00:03 +0000 | [diff] [blame] | 573 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 574 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 575 | pthread_mutex_lock(&pos_mutex); |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 576 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 577 | ERROR("read_fs_bytes: Lseek on destination failed because %s, " |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 578 | "offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 579 | res = 0; |
Phillip Lougher | 041cf6d | 2013-07-20 04:20:40 +0100 | [diff] [blame] | 580 | } else if(read_bytes(fd, buff, bytes) < bytes) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 581 | ERROR("Read on destination failed\n"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 582 | res = 0; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 585 | pthread_cleanup_pop(1); |
| 586 | return res; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 590 | int write_bytes(int fd, void *buff, int bytes) |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 591 | { |
| 592 | int res, count; |
| 593 | |
| 594 | for(count = 0; count < bytes; count += res) { |
| 595 | res = write(fd, buff + count, bytes - count); |
| 596 | if(res == -1) { |
| 597 | if(errno != EINTR) { |
| 598 | ERROR("Write failed because %s\n", |
| 599 | strerror(errno)); |
| 600 | return -1; |
| 601 | } |
| 602 | res = 0; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 610 | void write_destination(int fd, long long byte, int bytes, void *buff) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 611 | { |
| 612 | off_t off = byte; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 613 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 614 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
| 615 | pthread_mutex_lock(&pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 616 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 617 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 618 | ERROR("write_destination: Lseek on destination " |
Phillip Lougher | 1d3177a | 2013-02-06 21:48:58 +0000 | [diff] [blame] | 619 | "failed because %s, offset=0x%llx\n", strerror(errno), |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 620 | off); |
Phillip Lougher | a0c2346 | 2013-02-12 03:30:12 +0000 | [diff] [blame] | 621 | BAD_ERROR("Probably out of space on output %s\n", |
| 622 | block_device ? "block device" : "filesystem"); |
| 623 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 624 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 625 | if(write_bytes(fd, buff, bytes) == -1) |
Phillip Lougher | e8b536f | 2013-02-12 22:01:21 +0000 | [diff] [blame] | 626 | BAD_ERROR("Failed to write to output %s\n", |
| 627 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 628 | |
| 629 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | |
| 633 | long long write_inodes() |
| 634 | { |
| 635 | unsigned short c_byte; |
| 636 | int avail_bytes; |
| 637 | char *datap = data_cache; |
| 638 | long long start_bytes = bytes; |
| 639 | |
| 640 | while(cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 641 | if(inode_size - inode_bytes < |
| 642 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 643 | void *it = realloc(inode_table, inode_size + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 644 | ((SQUASHFS_METADATA_SIZE << 1) + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 645 | if(it == NULL) |
| 646 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 647 | inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 1eb2a66 | 2010-07-26 17:30:50 +0000 | [diff] [blame] | 648 | inode_table = it; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 649 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 650 | avail_bytes = cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 651 | SQUASHFS_METADATA_SIZE : cache_bytes; |
| 652 | c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET, datap, |
| 653 | avail_bytes, SQUASHFS_METADATA_SIZE, noI, 0); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 654 | TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 655 | SQUASHFS_SWAP_SHORTS(&c_byte, inode_table + inode_bytes, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 656 | inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET; |
| 657 | total_inode_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 658 | datap += avail_bytes; |
| 659 | cache_bytes -= avail_bytes; |
| 660 | } |
| 661 | |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 662 | write_destination(fd, bytes, inode_bytes, inode_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 663 | bytes += inode_bytes; |
| 664 | |
| 665 | return start_bytes; |
| 666 | } |
| 667 | |
| 668 | |
| 669 | long long write_directories() |
| 670 | { |
| 671 | unsigned short c_byte; |
| 672 | int avail_bytes; |
| 673 | char *directoryp = directory_data_cache; |
| 674 | long long start_bytes = bytes; |
| 675 | |
| 676 | while(directory_cache_bytes) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 677 | if(directory_size - directory_bytes < |
| 678 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 679 | void *dt = realloc(directory_table, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 680 | directory_size + ((SQUASHFS_METADATA_SIZE << 1) |
| 681 | + 2)); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 682 | if(dt == NULL) |
| 683 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 684 | directory_size += (SQUASHFS_METADATA_SIZE << 1) + 2; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 685 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 686 | } |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 687 | avail_bytes = directory_cache_bytes > SQUASHFS_METADATA_SIZE ? |
| 688 | SQUASHFS_METADATA_SIZE : directory_cache_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 689 | c_byte = mangle(directory_table + directory_bytes + |
| 690 | BLOCK_OFFSET, directoryp, avail_bytes, |
| 691 | SQUASHFS_METADATA_SIZE, noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 692 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 693 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 694 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 695 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 696 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 697 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 698 | total_directory_bytes += avail_bytes + BLOCK_OFFSET; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 699 | directoryp += avail_bytes; |
| 700 | directory_cache_bytes -= avail_bytes; |
| 701 | } |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 702 | write_destination(fd, bytes, directory_bytes, directory_table); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 703 | bytes += directory_bytes; |
| 704 | |
| 705 | return start_bytes; |
| 706 | } |
| 707 | |
| 708 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 709 | long long write_id_table() |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 710 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 711 | unsigned int id_bytes = SQUASHFS_ID_BYTES(id_count); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 712 | unsigned int p[id_count]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 713 | int i; |
| 714 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 715 | 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] | 716 | for(i = 0; i < id_count; i++) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 717 | 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] | 718 | SQUASHFS_SWAP_INTS(&id_table[i]->id, p + i, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 719 | } |
| 720 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 721 | return generic_write_table(id_bytes, p, 0, NULL, noI); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 725 | struct id *get_id(unsigned int id) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 726 | { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 727 | int hash = ID_HASH(id); |
| 728 | struct id *entry = id_hash_table[hash]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 729 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 730 | for(; entry; entry = entry->next) |
| 731 | if(entry->id == id) |
| 732 | break; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 733 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 734 | return entry; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 738 | struct id *create_id(unsigned int id) |
| 739 | { |
| 740 | int hash = ID_HASH(id); |
| 741 | struct id *entry = malloc(sizeof(struct id)); |
| 742 | if(entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 743 | MEM_ERROR(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 744 | entry->id = id; |
| 745 | entry->index = id_count ++; |
| 746 | entry->flags = 0; |
| 747 | entry->next = id_hash_table[hash]; |
| 748 | id_hash_table[hash] = entry; |
| 749 | id_table[entry->index] = entry; |
| 750 | return entry; |
| 751 | } |
| 752 | |
| 753 | |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 754 | int resolve_child_ugid(unsigned int *ugid, |
| 755 | const struct ugid_map_entry *ugid_mapping, |
| 756 | unsigned int ugid_map_count) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 757 | { |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 758 | unsigned int i; |
| 759 | |
| 760 | for (i = 0; i < ugid_map_count; i++) { |
| 761 | if (ugid_mapping[i].parent_id <= *ugid && |
| 762 | *ugid < |
| 763 | ugid_mapping[i].parent_id + ugid_mapping[i].length) { |
| 764 | *ugid = ugid_mapping[i].child_id + *ugid - |
| 765 | ugid_mapping[i].parent_id; |
| 766 | return 1; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | |
| 774 | unsigned int get_uid(unsigned int uid, int resolve) |
| 775 | { |
| 776 | struct id *entry; |
| 777 | |
| 778 | if (resolve && !resolve_child_ugid(&uid, uid_mapping, uid_map_count)) |
| 779 | BAD_ERROR("uid not found in mapping: %d\n", uid); |
| 780 | entry = get_id(uid); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 781 | |
| 782 | if(entry == NULL) { |
| 783 | if(id_count == SQUASHFS_IDS) |
| 784 | BAD_ERROR("Out of uids!\n"); |
| 785 | entry = create_id(uid); |
| 786 | } |
| 787 | |
| 788 | if((entry->flags & ISA_UID) == 0) { |
| 789 | entry->flags |= ISA_UID; |
| 790 | uid_count ++; |
| 791 | } |
| 792 | |
| 793 | return entry->index; |
| 794 | } |
| 795 | |
| 796 | |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 797 | unsigned int get_guid(unsigned int guid, int resolve) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 798 | { |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 799 | struct id *entry; |
| 800 | |
| 801 | if (resolve && !resolve_child_ugid(&guid, gid_mapping, gid_map_count)) |
| 802 | BAD_ERROR("gid not found in mapping: %d\n", guid); |
| 803 | entry = get_id(guid); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 804 | |
| 805 | if(entry == NULL) { |
| 806 | if(id_count == SQUASHFS_IDS) |
| 807 | BAD_ERROR("Out of gids!\n"); |
| 808 | entry = create_id(guid); |
| 809 | } |
| 810 | |
| 811 | if((entry->flags & ISA_GID) == 0) { |
| 812 | entry->flags |= ISA_GID; |
| 813 | guid_count ++; |
| 814 | } |
| 815 | |
| 816 | return entry->index; |
| 817 | } |
| 818 | |
| 819 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 820 | #define ALLOC_SIZE 128 |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 821 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 822 | char *_pathname(struct dir_ent *dir_ent, char *pathname, int *size) |
| 823 | { |
| 824 | if(pathname == NULL) { |
| 825 | pathname = malloc(ALLOC_SIZE); |
| 826 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 827 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | for(;;) { |
| 831 | int res = snprintf(pathname, *size, "%s/%s", |
| 832 | dir_ent->our_dir->pathname, |
| 833 | dir_ent->source_name ? : dir_ent->name); |
| 834 | |
| 835 | if(res < 0) |
| 836 | BAD_ERROR("snprintf failed in pathname\n"); |
| 837 | else if(res >= *size) { |
| 838 | /* |
| 839 | * pathname is too small to contain the result, so |
| 840 | * increase it and try again |
| 841 | */ |
| 842 | *size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 843 | pathname = realloc(pathname, *size); |
| 844 | if(pathname == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 845 | MEM_ERROR(); |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 846 | } else |
| 847 | break; |
| 848 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 849 | |
| 850 | return pathname; |
| 851 | } |
| 852 | |
| 853 | |
| 854 | char *pathname(struct dir_ent *dir_ent) |
| 855 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 856 | static char *pathname = NULL; |
| 857 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 858 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 859 | if (dir_ent->nonstandard_pathname) |
| 860 | return dir_ent->nonstandard_pathname; |
| 861 | |
| 862 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | |
| 866 | char *pathname_reader(struct dir_ent *dir_ent) |
| 867 | { |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 868 | static char *pathname = NULL; |
| 869 | static int size = ALLOC_SIZE; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 870 | |
Phillip Lougher | 5ef5169 | 2012-11-19 03:35:39 +0000 | [diff] [blame] | 871 | if (dir_ent->nonstandard_pathname) |
| 872 | return dir_ent->nonstandard_pathname; |
| 873 | |
| 874 | return pathname = _pathname(dir_ent, pathname, &size); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 878 | char *subpathname(struct dir_ent *dir_ent) |
| 879 | { |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 880 | static char *subpath = NULL; |
| 881 | static int size = ALLOC_SIZE; |
| 882 | int res; |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 883 | |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 884 | if(subpath == NULL) { |
| 885 | subpath = malloc(ALLOC_SIZE); |
| 886 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 887 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | for(;;) { |
| 891 | if(dir_ent->our_dir->subpath[0] != '\0') |
| 892 | res = snprintf(subpath, size, "%s/%s", |
| 893 | dir_ent->our_dir->subpath, dir_ent->name); |
| 894 | else |
| 895 | res = snprintf(subpath, size, "/%s", dir_ent->name); |
| 896 | |
| 897 | if(res < 0) |
| 898 | BAD_ERROR("snprintf failed in subpathname\n"); |
| 899 | else if(res >= size) { |
| 900 | /* |
| 901 | * subpath is too small to contain the result, so |
| 902 | * increase it and try again |
| 903 | */ |
| 904 | size = (res + ALLOC_SIZE) & ~(ALLOC_SIZE - 1); |
| 905 | subpath = realloc(subpath, size); |
| 906 | if(subpath == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 907 | MEM_ERROR(); |
Phillip Lougher | d457ed3 | 2012-11-19 01:36:56 +0000 | [diff] [blame] | 908 | } else |
| 909 | break; |
Phillip Lougher | 90e0825 | 2012-10-05 04:33:35 +0100 | [diff] [blame] | 910 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 911 | |
| 912 | return subpath; |
| 913 | } |
| 914 | |
| 915 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 916 | static inline unsigned int get_inode_no(struct inode_info *inode) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 917 | { |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 918 | return inode->inode_number; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 922 | static inline unsigned int get_parent_no(struct dir_info *dir) |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 923 | { |
Phillip Lougher | 1eae83d | 2012-09-26 02:12:45 +0100 | [diff] [blame] | 924 | return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no; |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 925 | } |
| 926 | |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 927 | |
| 928 | /* ANDROID CHANGES START*/ |
| 929 | #ifdef ANDROID |
Alex Deymo | 4445cd1 | 2016-12-08 15:19:49 -0800 | [diff] [blame] | 930 | |
| 931 | /* Round up the passed |n| value to the smallest multiple of 4096 greater or |
| 932 | * equal than |n| and return the 4K-block number for that value. */ |
| 933 | static unsigned long long round_up_block(unsigned long long n) { |
| 934 | const unsigned long long kMapBlockSize = 4096; |
| 935 | return (n + kMapBlockSize - 1) / kMapBlockSize; |
| 936 | } |
| 937 | |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 938 | static inline void write_block_map_entry(char *sub_path, unsigned long long start_block, unsigned long long total_size, |
| 939 | char * mount_point, FILE *block_map_file) { |
| 940 | if (block_map_file) { |
Alex Deymo | 4445cd1 | 2016-12-08 15:19:49 -0800 | [diff] [blame] | 941 | /* We assign each 4K block based on what file the first byte of the block |
| 942 | * belongs to. The current file consists of the chunk of bytes in the |
| 943 | * interval [start_block, start_block + total_size), (closed on the left end |
| 944 | * and open on the right end). We then compute the first block whose first |
| 945 | * byte is equal to or greater than start_block as |round_start| and then |
| 946 | * the first block whose first byte is *past* this interval, as |
| 947 | * |round_end + 1|. This means that the blocks that should be assigned to |
| 948 | * the current file are in the interval [round_start, round_end + 1), or |
| 949 | * simply [round_start, round_end]. |
| 950 | */ |
| 951 | unsigned long long round_start = round_up_block(start_block); |
| 952 | unsigned long long round_end = round_up_block(start_block + total_size) - 1; |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 953 | if (round_start && total_size && round_start <= round_end) { |
| 954 | fprintf(block_map_file, "/%s", mount_point); |
| 955 | if (sub_path[0] != '/') fprintf(block_map_file, "/"); |
| 956 | if (round_start == round_end) |
| 957 | fprintf(block_map_file, "%s %lld\n", sub_path, round_start); |
| 958 | else |
| 959 | fprintf(block_map_file, "%s %lld-%lld\n", sub_path, round_start, round_end); |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | #endif |
| 964 | /* ANDROID CHANGES END */ |
Phillip Lougher | 53cef74 | 2012-07-25 05:12:50 +0100 | [diff] [blame] | 965 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 966 | int create_inode(squashfs_inode *i_no, struct dir_info *dir_info, |
| 967 | struct dir_ent *dir_ent, int type, long long byte_size, |
| 968 | long long start_block, unsigned int offset, unsigned int *block_list, |
| 969 | struct fragment *fragment, struct directory *dir_in, long long sparse) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 970 | { |
| 971 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 8973a12 | 2010-12-31 09:52:45 +0000 | [diff] [blame] | 972 | union squashfs_inode_header inode_header; |
plougher | 857d0dd | 2010-12-31 21:03:13 +0000 | [diff] [blame] | 973 | struct squashfs_base_inode_header *base = &inode_header.base; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 974 | void *inode; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 975 | char *filename = pathname(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 976 | int nlink = dir_ent->inode->nlink; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 977 | int xattr = read_xattrs(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 978 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 979 | switch(type) { |
| 980 | case SQUASHFS_FILE_TYPE: |
| 981 | if(dir_ent->inode->nlink > 1 || |
| 982 | byte_size >= (1LL << 32) || |
| 983 | start_block >= (1LL << 32) || |
| 984 | sparse || IS_XATTR(xattr)) |
| 985 | type = SQUASHFS_LREG_TYPE; |
| 986 | break; |
| 987 | case SQUASHFS_DIR_TYPE: |
| 988 | if(dir_info->dir_is_ldir || IS_XATTR(xattr)) |
| 989 | type = SQUASHFS_LDIR_TYPE; |
| 990 | break; |
| 991 | case SQUASHFS_SYMLINK_TYPE: |
| 992 | if(IS_XATTR(xattr)) |
| 993 | type = SQUASHFS_LSYMLINK_TYPE; |
| 994 | break; |
| 995 | case SQUASHFS_BLKDEV_TYPE: |
| 996 | if(IS_XATTR(xattr)) |
| 997 | type = SQUASHFS_LBLKDEV_TYPE; |
| 998 | break; |
| 999 | case SQUASHFS_CHRDEV_TYPE: |
| 1000 | if(IS_XATTR(xattr)) |
| 1001 | type = SQUASHFS_LCHRDEV_TYPE; |
| 1002 | break; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1003 | case SQUASHFS_FIFO_TYPE: |
| 1004 | if(IS_XATTR(xattr)) |
| 1005 | type = SQUASHFS_LFIFO_TYPE; |
| 1006 | break; |
| 1007 | case SQUASHFS_SOCKET_TYPE: |
| 1008 | if(IS_XATTR(xattr)) |
| 1009 | type = SQUASHFS_LSOCKET_TYPE; |
| 1010 | break; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1013 | base->mode = SQUASHFS_MODE(buf->st_mode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1014 | base->uid = get_uid((unsigned int) global_uid == -1 ? |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 1015 | buf->st_uid : global_uid, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1016 | base->inode_type = type; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1017 | base->guid = get_guid((unsigned int) global_gid == -1 ? |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 1018 | buf->st_gid : global_gid, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1019 | base->mtime = buf->st_mtime; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1020 | base->inode_number = get_inode_no(dir_ent->inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1021 | |
| 1022 | if(type == SQUASHFS_FILE_TYPE) { |
| 1023 | int i; |
plougher | 8701ed6 | 2010-12-31 20:38:38 +0000 | [diff] [blame] | 1024 | struct squashfs_reg_inode_header *reg = &inode_header.reg; |
| 1025 | size_t off = offsetof(struct squashfs_reg_inode_header, block_list); |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 1026 | /* ANDROID CHANGES START*/ |
| 1027 | #ifdef ANDROID |
| 1028 | unsigned long long total_size = 0; |
| 1029 | char *sub_path; |
| 1030 | #endif |
| 1031 | /* ANDROID CHANGES END */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1032 | |
| 1033 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1034 | reg->file_size = byte_size; |
| 1035 | reg->start_block = start_block; |
| 1036 | reg->fragment = fragment->index; |
| 1037 | reg->offset = fragment->offset; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1038 | SQUASHFS_SWAP_REG_INODE_HEADER(reg, inode); |
| 1039 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1040 | TRACE("File inode, file_size %lld, start_block 0x%llx, blocks " |
| 1041 | "%d, fragment %d, offset %d, size %d\n", byte_size, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1042 | start_block, offset, fragment->index, fragment->offset, |
| 1043 | fragment->size); |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 1044 | for(i = 0; i < offset; i++) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1045 | TRACE("Block %d, size %d\n", i, block_list[i]); |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 1046 | total_size += SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]); |
| 1047 | } |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 1048 | /* ANDROID CHANGES START*/ |
| 1049 | #ifdef ANDROID |
| 1050 | sub_path = subpathname(dir_ent); |
| 1051 | if (block_map_file && fragment->index == -1) { |
| 1052 | write_block_map_entry(sub_path, start_block, total_size, mount_point, block_map_file); |
| 1053 | } |
| 1054 | #endif |
| 1055 | /* ANDROID CHANGES END */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1056 | } |
| 1057 | else if(type == SQUASHFS_LREG_TYPE) { |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 1058 | /* ANDROID CHANGES START*/ |
| 1059 | #ifdef ANDROID |
| 1060 | unsigned long long total_size = 0; |
| 1061 | char *sub_path; |
| 1062 | #endif |
| 1063 | /* ANDROID CHANGES END */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1064 | int i; |
plougher | 1e6ac4a | 2010-12-31 20:42:02 +0000 | [diff] [blame] | 1065 | struct squashfs_lreg_inode_header *reg = &inode_header.lreg; |
| 1066 | size_t off = offsetof(struct squashfs_lreg_inode_header, block_list); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1067 | |
| 1068 | inode = get_inode(sizeof(*reg) + offset * sizeof(unsigned int)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1069 | reg->nlink = nlink; |
| 1070 | reg->file_size = byte_size; |
| 1071 | reg->start_block = start_block; |
| 1072 | reg->fragment = fragment->index; |
| 1073 | reg->offset = fragment->offset; |
plougher | f5a674d | 2009-03-25 05:38:27 +0000 | [diff] [blame] | 1074 | if(sparse && sparse >= byte_size) |
| 1075 | sparse = byte_size - 1; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1076 | reg->sparse = sparse; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1077 | reg->xattr = xattr; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1078 | SQUASHFS_SWAP_LREG_INODE_HEADER(reg, inode); |
| 1079 | SQUASHFS_SWAP_INTS(block_list, inode + off, offset); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1080 | TRACE("Long file inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1081 | "blocks %d, fragment %d, offset %d, size %d, nlink %d" |
| 1082 | "\n", byte_size, start_block, offset, fragment->index, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1083 | fragment->offset, fragment->size, nlink); |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 1084 | for(i = 0; i < offset; i++) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1085 | TRACE("Block %d, size %d\n", i, block_list[i]); |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 1086 | total_size += SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]); |
| 1087 | } |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 1088 | /* ANDROID CHANGES START*/ |
| 1089 | #ifdef ANDROID |
| 1090 | sub_path = subpathname(dir_ent); |
| 1091 | if (block_map_file && fragment->index == -1) { |
| 1092 | write_block_map_entry(sub_path, start_block, total_size, mount_point, block_map_file); |
| 1093 | } |
| 1094 | #endif |
| 1095 | /* ANDROID CHANGES END */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1096 | } |
| 1097 | else if(type == SQUASHFS_LDIR_TYPE) { |
| 1098 | int i; |
| 1099 | unsigned char *p; |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 1100 | struct squashfs_ldir_inode_header *dir = &inode_header.ldir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1101 | struct cached_dir_index *index = dir_in->index; |
| 1102 | unsigned int i_count = dir_in->i_count; |
| 1103 | unsigned int i_size = dir_in->i_size; |
| 1104 | |
| 1105 | if(byte_size >= 1 << 27) |
| 1106 | BAD_ERROR("directory greater than 2^27-1 bytes!\n"); |
| 1107 | |
| 1108 | inode = get_inode(sizeof(*dir) + i_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1109 | dir->inode_type = SQUASHFS_LDIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1110 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 1111 | dir->file_size = byte_size; |
| 1112 | dir->offset = offset; |
| 1113 | dir->start_block = start_block; |
| 1114 | dir->i_count = i_count; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1115 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1116 | dir->xattr = xattr; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1117 | |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1118 | SQUASHFS_SWAP_LDIR_INODE_HEADER(dir, inode); |
plougher | 2611d39 | 2010-12-31 20:50:36 +0000 | [diff] [blame] | 1119 | p = inode + offsetof(struct squashfs_ldir_inode_header, index); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1120 | for(i = 0; i < i_count; i++) { |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1121 | SQUASHFS_SWAP_DIR_INDEX(&index[i].index, p); |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1122 | p += offsetof(struct squashfs_dir_index, name); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1123 | memcpy(p, index[i].name, index[i].index.size + 1); |
| 1124 | p += index[i].index.size + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1125 | } |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1126 | TRACE("Long directory inode, file_size %lld, start_block " |
| 1127 | "0x%llx, offset 0x%x, nlink %d\n", byte_size, |
| 1128 | start_block, offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1129 | } |
| 1130 | else if(type == SQUASHFS_DIR_TYPE) { |
plougher | 9d80a60 | 2010-12-31 20:47:24 +0000 | [diff] [blame] | 1131 | struct squashfs_dir_inode_header *dir = &inode_header.dir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1132 | |
| 1133 | inode = get_inode(sizeof(*dir)); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1134 | dir->nlink = dir_ent->dir->directory_count + 2; |
| 1135 | dir->file_size = byte_size; |
| 1136 | dir->offset = offset; |
| 1137 | dir->start_block = start_block; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 1138 | dir->parent_inode = get_parent_no(dir_ent->our_dir); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1139 | SQUASHFS_SWAP_DIR_INODE_HEADER(dir, inode); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1140 | TRACE("Directory inode, file_size %lld, start_block 0x%llx, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1141 | "offset 0x%x, nlink %d\n", byte_size, start_block, |
| 1142 | offset, dir_ent->dir->directory_count + 2); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1143 | } |
| 1144 | else if(type == SQUASHFS_CHRDEV_TYPE || type == SQUASHFS_BLKDEV_TYPE) { |
plougher | c70c633 | 2010-12-31 20:11:09 +0000 | [diff] [blame] | 1145 | struct squashfs_dev_inode_header *dev = &inode_header.dev; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1146 | unsigned int major = major(buf->st_rdev); |
| 1147 | unsigned int minor = minor(buf->st_rdev); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1148 | |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1149 | if(major > 0xfff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1150 | ERROR("Major %d out of range in device node %s, " |
| 1151 | "truncating to %d\n", major, filename, |
| 1152 | major & 0xfff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1153 | major &= 0xfff; |
| 1154 | } |
| 1155 | if(minor > 0xfffff) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1156 | ERROR("Minor %d out of range in device node %s, " |
| 1157 | "truncating to %d\n", minor, filename, |
| 1158 | minor & 0xfffff); |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1159 | minor &= 0xfffff; |
| 1160 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1161 | inode = get_inode(sizeof(*dev)); |
| 1162 | dev->nlink = nlink; |
plougher | 5b39850 | 2008-10-04 23:22:03 +0000 | [diff] [blame] | 1163 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1164 | ((minor & ~0xff) << 12); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1165 | SQUASHFS_SWAP_DEV_INODE_HEADER(dev, inode); |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1166 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1167 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1168 | else if(type == SQUASHFS_LCHRDEV_TYPE || type == SQUASHFS_LBLKDEV_TYPE) { |
plougher | 0b4ee5b | 2010-12-31 20:14:00 +0000 | [diff] [blame] | 1169 | struct squashfs_ldev_inode_header *dev = &inode_header.ldev; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1170 | unsigned int major = major(buf->st_rdev); |
| 1171 | unsigned int minor = minor(buf->st_rdev); |
| 1172 | |
| 1173 | if(major > 0xfff) { |
| 1174 | ERROR("Major %d out of range in device node %s, " |
| 1175 | "truncating to %d\n", major, filename, |
| 1176 | major & 0xfff); |
| 1177 | major &= 0xfff; |
| 1178 | } |
| 1179 | if(minor > 0xfffff) { |
| 1180 | ERROR("Minor %d out of range in device node %s, " |
| 1181 | "truncating to %d\n", minor, filename, |
| 1182 | minor & 0xfffff); |
| 1183 | minor &= 0xfffff; |
| 1184 | } |
| 1185 | inode = get_inode(sizeof(*dev)); |
| 1186 | dev->nlink = nlink; |
| 1187 | dev->rdev = (major << 8) | (minor & 0xff) | |
| 1188 | ((minor & ~0xff) << 12); |
| 1189 | dev->xattr = xattr; |
| 1190 | SQUASHFS_SWAP_LDEV_INODE_HEADER(dev, inode); |
| 1191 | TRACE("Device inode, rdev 0x%x, nlink %d\n", dev->rdev, nlink); |
| 1192 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1193 | else if(type == SQUASHFS_SYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1194 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 1195 | int byte = strlen(dir_ent->inode->symlink); |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1196 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1197 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1198 | inode = get_inode(sizeof(*symlink) + byte); |
| 1199 | symlink->nlink = nlink; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1200 | symlink->symlink_size = byte; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1201 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 1202 | strncpy(inode + off, dir_ent->inode->symlink, byte); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1203 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1204 | nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1205 | } |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1206 | else if(type == SQUASHFS_LSYMLINK_TYPE) { |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1207 | struct squashfs_symlink_inode_header *symlink = &inode_header.symlink; |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 1208 | int byte = strlen(dir_ent->inode->symlink); |
plougher | 5ae6e95 | 2010-12-31 20:44:34 +0000 | [diff] [blame] | 1209 | size_t off = offsetof(struct squashfs_symlink_inode_header, symlink); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1210 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1211 | inode = get_inode(sizeof(*symlink) + byte + |
| 1212 | sizeof(unsigned int)); |
| 1213 | symlink->nlink = nlink; |
| 1214 | symlink->symlink_size = byte; |
| 1215 | SQUASHFS_SWAP_SYMLINK_INODE_HEADER(symlink, inode); |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 1216 | strncpy(inode + off, dir_ent->inode->symlink, byte); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1217 | SQUASHFS_SWAP_INTS(&xattr, inode + off + byte, 1); |
| 1218 | TRACE("Symbolic link inode, symlink_size %d, nlink %d\n", byte, |
| 1219 | nlink); |
| 1220 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1221 | else if(type == SQUASHFS_FIFO_TYPE || type == SQUASHFS_SOCKET_TYPE) { |
plougher | e56b986 | 2010-12-31 10:58:35 +0000 | [diff] [blame] | 1222 | struct squashfs_ipc_inode_header *ipc = &inode_header.ipc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1223 | |
| 1224 | inode = get_inode(sizeof(*ipc)); |
| 1225 | ipc->nlink = nlink; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1226 | SQUASHFS_SWAP_IPC_INODE_HEADER(ipc, inode); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1227 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1228 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1229 | } |
| 1230 | else if(type == SQUASHFS_LFIFO_TYPE || type == SQUASHFS_LSOCKET_TYPE) { |
plougher | aa0d122 | 2010-12-31 20:06:24 +0000 | [diff] [blame] | 1231 | struct squashfs_lipc_inode_header *ipc = &inode_header.lipc; |
plougher | c5d6932 | 2010-05-12 19:28:38 +0000 | [diff] [blame] | 1232 | |
| 1233 | inode = get_inode(sizeof(*ipc)); |
| 1234 | ipc->nlink = nlink; |
| 1235 | ipc->xattr = xattr; |
| 1236 | SQUASHFS_SWAP_LIPC_INODE_HEADER(ipc, inode); |
| 1237 | TRACE("ipc inode, type %s, nlink %d\n", type == |
| 1238 | SQUASHFS_FIFO_TYPE ? "fifo" : "socket", nlink); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1239 | } else |
rlougher | 8f7d0b8 | 2007-11-08 15:33:29 +0000 | [diff] [blame] | 1240 | BAD_ERROR("Unrecognised inode %d in create_inode\n", type); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1241 | |
| 1242 | *i_no = MKINODE(inode); |
| 1243 | inode_count ++; |
| 1244 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1245 | TRACE("Created inode 0x%llx, type %d, uid %d, guid %d\n", *i_no, type, |
| 1246 | base->uid, base->guid); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1247 | |
| 1248 | return TRUE; |
| 1249 | } |
| 1250 | |
| 1251 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1252 | void add_dir(squashfs_inode inode, unsigned int inode_number, char *name, |
| 1253 | int type, struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1254 | { |
plougher | 8cb05cd | 2005-12-11 23:32:35 +0000 | [diff] [blame] | 1255 | unsigned char *buff; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1256 | struct squashfs_dir_entry idir; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1257 | unsigned int start_block = inode >> 16; |
| 1258 | unsigned int offset = inode & 0xffff; |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1259 | unsigned int size = strlen(name); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1260 | size_t name_off = offsetof(struct squashfs_dir_entry, name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1261 | |
plougher | 43d4908 | 2010-12-16 05:01:15 +0000 | [diff] [blame] | 1262 | if(size > SQUASHFS_NAME_LEN) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1263 | size = SQUASHFS_NAME_LEN; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1264 | ERROR("Filename is greater than %d characters, truncating! ..." |
| 1265 | "\n", SQUASHFS_NAME_LEN); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1268 | if(dir->p + sizeof(struct squashfs_dir_entry) + size + |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1269 | sizeof(struct squashfs_dir_header) |
| 1270 | >= dir->buff + dir->size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1271 | buff = realloc(dir->buff, dir->size += SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1272 | if(buff == NULL) |
| 1273 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1274 | |
| 1275 | dir->p = (dir->p - dir->buff) + buff; |
| 1276 | if(dir->entry_count_p) |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1277 | dir->entry_count_p = (dir->entry_count_p - dir->buff + |
| 1278 | buff); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1279 | dir->index_count_p = dir->index_count_p - dir->buff + buff; |
| 1280 | dir->buff = buff; |
| 1281 | } |
| 1282 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1283 | if(dir->entry_count == 256 || start_block != dir->start_block || |
| 1284 | ((dir->entry_count_p != NULL) && |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1285 | ((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1286 | dir->index_count_p) > SQUASHFS_METADATA_SIZE)) || |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1287 | ((long long) inode_number - dir->inode_number) > 32767 |
| 1288 | || ((long long) inode_number - dir->inode_number) |
| 1289 | < -32768) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1290 | if(dir->entry_count_p) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1291 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1292 | |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1293 | if((dir->p + sizeof(struct squashfs_dir_entry) + size - |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1294 | dir->index_count_p) > |
| 1295 | SQUASHFS_METADATA_SIZE) { |
| 1296 | if(dir->i_count % I_COUNT_SIZE == 0) { |
| 1297 | dir->index = realloc(dir->index, |
| 1298 | (dir->i_count + I_COUNT_SIZE) * |
| 1299 | sizeof(struct cached_dir_index)); |
| 1300 | if(dir->index == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1301 | MEM_ERROR(); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1302 | } |
| 1303 | dir->index[dir->i_count].index.index = |
| 1304 | dir->p - dir->buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1305 | dir->index[dir->i_count].index.size = size - 1; |
| 1306 | dir->index[dir->i_count++].name = name; |
plougher | 2bd2b72 | 2010-12-31 10:52:15 +0000 | [diff] [blame] | 1307 | dir->i_size += sizeof(struct squashfs_dir_index) |
| 1308 | + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1309 | dir->index_count_p = dir->p; |
| 1310 | } |
| 1311 | |
| 1312 | dir_header.count = dir->entry_count - 1; |
| 1313 | dir_header.start_block = dir->start_block; |
| 1314 | dir_header.inode_number = dir->inode_number; |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1315 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1316 | dir->entry_count_p); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1317 | |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | dir->entry_count_p = dir->p; |
| 1322 | dir->start_block = start_block; |
| 1323 | dir->entry_count = 0; |
| 1324 | dir->inode_number = inode_number; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1325 | dir->p += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1328 | idir.offset = offset; |
| 1329 | idir.type = type; |
| 1330 | idir.size = size - 1; |
| 1331 | idir.inode_number = ((long long) inode_number - dir->inode_number); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1332 | SQUASHFS_SWAP_DIR_ENTRY(&idir, dir->p); |
| 1333 | strncpy((char *) dir->p + name_off, name, size); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1334 | dir->p += sizeof(struct squashfs_dir_entry) + size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1335 | dir->entry_count ++; |
| 1336 | } |
| 1337 | |
| 1338 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1339 | void write_dir(squashfs_inode *inode, struct dir_info *dir_info, |
| 1340 | struct directory *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1341 | { |
| 1342 | unsigned int dir_size = dir->p - dir->buff; |
plougher | 4627ca3 | 2010-12-16 05:03:55 +0000 | [diff] [blame] | 1343 | int data_space = directory_cache_size - directory_cache_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1344 | unsigned int directory_block, directory_offset, i_count, index; |
| 1345 | unsigned short c_byte; |
| 1346 | |
| 1347 | if(data_space < dir_size) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1348 | int realloc_size = directory_cache_size == 0 ? |
| 1349 | ((dir_size + SQUASHFS_METADATA_SIZE) & |
| 1350 | ~(SQUASHFS_METADATA_SIZE - 1)) : dir_size - data_space; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1351 | |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1352 | void *dc = realloc(directory_data_cache, |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1353 | directory_cache_size + realloc_size); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1354 | if(dc == NULL) |
| 1355 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1356 | directory_cache_size += realloc_size; |
plougher | 17248ca | 2010-07-27 00:24:35 +0000 | [diff] [blame] | 1357 | directory_data_cache = dc; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | if(dir_size) { |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1361 | struct squashfs_dir_header dir_header; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1362 | |
| 1363 | dir_header.count = dir->entry_count - 1; |
| 1364 | dir_header.start_block = dir->start_block; |
| 1365 | dir_header.inode_number = dir->inode_number; |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1366 | SQUASHFS_SWAP_DIR_HEADER(&dir_header, dir->entry_count_p); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1367 | memcpy(directory_data_cache + directory_cache_bytes, dir->buff, |
| 1368 | dir_size); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1369 | } |
| 1370 | directory_offset = directory_cache_bytes; |
| 1371 | directory_block = directory_bytes; |
| 1372 | directory_cache_bytes += dir_size; |
| 1373 | i_count = 0; |
| 1374 | index = SQUASHFS_METADATA_SIZE - directory_offset; |
| 1375 | |
| 1376 | while(1) { |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1377 | while(i_count < dir->i_count && |
| 1378 | dir->index[i_count].index.index < index) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1379 | dir->index[i_count++].index.start_block = |
| 1380 | directory_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1381 | index += SQUASHFS_METADATA_SIZE; |
| 1382 | |
| 1383 | if(directory_cache_bytes < SQUASHFS_METADATA_SIZE) |
| 1384 | break; |
| 1385 | |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1386 | if((directory_size - directory_bytes) < |
| 1387 | ((SQUASHFS_METADATA_SIZE << 1) + 2)) { |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1388 | void *dt = realloc(directory_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1389 | directory_size + (SQUASHFS_METADATA_SIZE << 1) |
| 1390 | + 2); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1391 | if(dt == NULL) |
| 1392 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1393 | directory_size += SQUASHFS_METADATA_SIZE << 1; |
plougher | 79d665a | 2010-07-27 00:19:23 +0000 | [diff] [blame] | 1394 | directory_table = dt; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1397 | c_byte = mangle(directory_table + directory_bytes + |
| 1398 | BLOCK_OFFSET, directory_data_cache, |
| 1399 | SQUASHFS_METADATA_SIZE, SQUASHFS_METADATA_SIZE, |
| 1400 | noI, 0); |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1401 | TRACE("Directory block @ 0x%x, size %d\n", directory_bytes, |
| 1402 | c_byte); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1403 | SQUASHFS_SWAP_SHORTS(&c_byte, |
| 1404 | directory_table + directory_bytes, 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1405 | directory_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1406 | BLOCK_OFFSET; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1407 | total_directory_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET; |
plougher | 14dd5f3 | 2010-08-02 18:20:03 +0000 | [diff] [blame] | 1408 | memmove(directory_data_cache, directory_data_cache + |
plougher | fd57dfe | 2009-03-30 01:17:52 +0000 | [diff] [blame] | 1409 | SQUASHFS_METADATA_SIZE, directory_cache_bytes - |
| 1410 | SQUASHFS_METADATA_SIZE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1411 | directory_cache_bytes -= SQUASHFS_METADATA_SIZE; |
| 1412 | } |
| 1413 | |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 1414 | create_inode(inode, dir_info, dir_info->dir_ent, SQUASHFS_DIR_TYPE, |
| 1415 | dir_size + 3, directory_block, directory_offset, NULL, NULL, |
| 1416 | dir, 0); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1417 | |
| 1418 | #ifdef SQUASHFS_TRACE |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1419 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1420 | unsigned char *dirp; |
| 1421 | int count; |
| 1422 | |
| 1423 | TRACE("Directory contents of inode 0x%llx\n", *inode); |
| 1424 | dirp = dir->buff; |
| 1425 | while(dirp < dir->p) { |
| 1426 | char buffer[SQUASHFS_NAME_LEN + 1]; |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1427 | struct squashfs_dir_entry idir, *idirp; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1428 | struct squashfs_dir_header dirh; |
| 1429 | SQUASHFS_SWAP_DIR_HEADER((struct squashfs_dir_header *) dirp, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 1430 | &dirh); |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1431 | count = dirh.count + 1; |
plougher | 520e1a1 | 2010-12-31 10:44:38 +0000 | [diff] [blame] | 1432 | dirp += sizeof(struct squashfs_dir_header); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1433 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1434 | TRACE("\tStart block 0x%x, count %d\n", |
| 1435 | dirh.start_block, count); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1436 | |
| 1437 | while(count--) { |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1438 | idirp = (struct squashfs_dir_entry *) dirp; |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1439 | SQUASHFS_SWAP_DIR_ENTRY(idirp, &idir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1440 | strncpy(buffer, idirp->name, idir.size + 1); |
| 1441 | buffer[idir.size + 1] = '\0'; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1442 | TRACE("\t\tname %s, inode offset 0x%x, type " |
| 1443 | "%d\n", buffer, idir.offset, idir.type); |
plougher | 9b39355 | 2010-12-31 20:55:43 +0000 | [diff] [blame] | 1444 | dirp += sizeof(struct squashfs_dir_entry) + idir.size + |
plougher | e8b26f6 | 2010-12-16 05:06:00 +0000 | [diff] [blame] | 1445 | 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | #endif |
| 1450 | dir_count ++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1454 | static struct file_buffer *get_fragment(struct fragment *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1455 | { |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1456 | struct squashfs_fragment_entry *disk_fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1457 | struct file_buffer *buffer, *compressed_buffer; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1458 | long long start_block; |
| 1459 | int res, size, index = fragment->index; |
| 1460 | char locked; |
| 1461 | |
| 1462 | /* |
| 1463 | * Lookup fragment block in cache. |
| 1464 | * If the fragment block doesn't exist, then get the compressed version |
| 1465 | * from the writer cache or off disk, and decompress it. |
| 1466 | * |
| 1467 | * This routine has two things which complicate the code: |
| 1468 | * |
| 1469 | * 1. Multiple threads can simultaneously lookup/create the |
| 1470 | * same buffer. This means a buffer needs to be "locked" |
| 1471 | * when it is being filled in, to prevent other threads from |
| 1472 | * using it when it is not ready. This is because we now do |
| 1473 | * fragment duplicate checking in parallel. |
| 1474 | * 2. We have two caches which need to be checked for the |
| 1475 | * presence of fragment blocks: the normal fragment cache |
| 1476 | * and a "reserve" cache. The reserve cache is used to |
| 1477 | * prevent an unnecessary pipeline stall when the fragment cache |
| 1478 | * is full of fragments waiting to be compressed. |
| 1479 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1480 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1481 | if(fragment->index == SQUASHFS_INVALID_FRAG) |
| 1482 | return NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1483 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1484 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1485 | pthread_mutex_lock(&dup_mutex); |
| 1486 | |
| 1487 | again: |
| 1488 | buffer = cache_lookup_nowait(fragment_buffer, index, &locked); |
| 1489 | if(buffer) { |
| 1490 | pthread_mutex_unlock(&dup_mutex); |
| 1491 | if(locked) |
| 1492 | /* got a buffer being filled in. Wait for it */ |
| 1493 | cache_wait_unlock(buffer); |
| 1494 | goto finished; |
| 1495 | } |
| 1496 | |
| 1497 | /* not in fragment cache, is it in the reserve cache? */ |
| 1498 | buffer = cache_lookup_nowait(reserve_cache, index, &locked); |
| 1499 | if(buffer) { |
| 1500 | pthread_mutex_unlock(&dup_mutex); |
| 1501 | if(locked) |
| 1502 | /* got a buffer being filled in. Wait for it */ |
| 1503 | cache_wait_unlock(buffer); |
| 1504 | goto finished; |
| 1505 | } |
| 1506 | |
| 1507 | /* in neither cache, try to get it from the fragment cache */ |
| 1508 | buffer = cache_get_nowait(fragment_buffer, index); |
| 1509 | if(!buffer) { |
| 1510 | /* |
| 1511 | * no room, get it from the reserve cache, this is |
| 1512 | * dimensioned so it will always have space (no more than |
| 1513 | * processors + 1 can have an outstanding reserve buffer) |
| 1514 | */ |
| 1515 | buffer = cache_get_nowait(reserve_cache, index); |
| 1516 | if(!buffer) { |
| 1517 | /* failsafe */ |
| 1518 | ERROR("no space in reserve cache\n"); |
| 1519 | goto again; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | pthread_mutex_unlock(&dup_mutex); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1524 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1525 | compressed_buffer = cache_lookup(fwriter_buffer, index); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1526 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1527 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1528 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1529 | disk_fragment = &fragment_table[index]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1530 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(disk_fragment->size); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1531 | start_block = disk_fragment->start_block; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1532 | pthread_cleanup_pop(1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1533 | |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 1534 | if(SQUASHFS_COMPRESSED_BLOCK(disk_fragment->size)) { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1535 | int error; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1536 | char *data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1537 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1538 | if(compressed_buffer) |
| 1539 | data = compressed_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1540 | else { |
plougher | 49b57a9 | 2009-03-31 03:23:05 +0000 | [diff] [blame] | 1541 | data = read_from_disk(start_block, size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1542 | if(data == NULL) { |
| 1543 | ERROR("Failed to read fragment from output" |
| 1544 | " filesystem\n"); |
| 1545 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1546 | } |
| 1547 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1548 | |
plougher | b48442b | 2010-12-31 07:57:54 +0000 | [diff] [blame] | 1549 | res = compressor_uncompress(comp, buffer->data, data, size, |
| 1550 | block_size, &error); |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 1551 | if(res == -1) |
| 1552 | BAD_ERROR("%s uncompress failed with error code %d\n", |
| 1553 | comp->name, error); |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1554 | } else if(compressed_buffer) |
| 1555 | memcpy(buffer->data, compressed_buffer->data, size); |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1556 | else { |
| 1557 | res = read_fs_bytes(fd, start_block, size, buffer->data); |
Phillip Lougher | 9451938 | 2013-03-06 02:43:42 +0000 | [diff] [blame] | 1558 | if(res == 0) { |
| 1559 | ERROR("Failed to read fragment from output " |
| 1560 | "filesystem\n"); |
| 1561 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1562 | } |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1563 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1564 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1565 | cache_unlock(buffer); |
plougher | 03859fa | 2009-03-31 03:18:18 +0000 | [diff] [blame] | 1566 | cache_block_put(compressed_buffer); |
| 1567 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1568 | finished: |
| 1569 | pthread_cleanup_pop(0); |
| 1570 | |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 1571 | return buffer; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1574 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1575 | unsigned short get_fragment_checksum(struct file_info *file) |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1576 | { |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1577 | struct file_buffer *frag_buffer; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1578 | struct append_file *append; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1579 | int res, index = file->fragment->index; |
| 1580 | unsigned short checksum; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1581 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1582 | if(index == SQUASHFS_INVALID_FRAG) |
| 1583 | return 0; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1584 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1585 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1586 | pthread_mutex_lock(&dup_mutex); |
| 1587 | res = file->have_frag_checksum; |
| 1588 | checksum = file->fragment_checksum; |
Phillip Lougher | c4a8a79 | 2014-04-10 02:23:26 +0100 | [diff] [blame] | 1589 | pthread_cleanup_pop(1); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1590 | |
| 1591 | if(res) |
| 1592 | return checksum; |
| 1593 | |
| 1594 | frag_buffer = get_fragment(file->fragment); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1595 | |
Phillip Lougher | c4a8a79 | 2014-04-10 02:23:26 +0100 | [diff] [blame] | 1596 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 1597 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1598 | for(append = file_mapping[index]; append; append = append->next) { |
| 1599 | int offset = append->file->fragment->offset; |
| 1600 | int size = append->file->fragment->size; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1601 | unsigned short cksum = |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1602 | get_checksum_mem(frag_buffer->data + offset, size); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1603 | |
| 1604 | if(file == append->file) |
| 1605 | checksum = cksum; |
| 1606 | |
| 1607 | pthread_mutex_lock(&dup_mutex); |
| 1608 | append->file->fragment_checksum = cksum; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1609 | append->file->have_frag_checksum = TRUE; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1610 | pthread_mutex_unlock(&dup_mutex); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | cache_block_put(frag_buffer); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1614 | pthread_cleanup_pop(0); |
| 1615 | |
| 1616 | return checksum; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1620 | void lock_fragments() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1621 | { |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1622 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1623 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1624 | fragments_locked = TRUE; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1625 | pthread_cleanup_pop(1); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | void unlock_fragments() |
| 1630 | { |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1631 | int frg, size; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1632 | struct file_buffer *write_buffer; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1633 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1634 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1635 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | 71ad964 | 2013-05-25 05:03:48 +0100 | [diff] [blame] | 1636 | |
| 1637 | /* |
| 1638 | * Note queue_empty() is inherently racy with respect to concurrent |
| 1639 | * queue get and pushes. We avoid this because we're holding the |
| 1640 | * fragment_mutex which ensures no other threads can be using the |
| 1641 | * queue at this time. |
| 1642 | */ |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1643 | while(!queue_empty(locked_fragment)) { |
| 1644 | write_buffer = queue_get(locked_fragment); |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1645 | frg = write_buffer->block; |
| 1646 | size = SQUASHFS_COMPRESSED_SIZE_BLOCK(fragment_table[frg].size); |
| 1647 | fragment_table[frg].start_block = bytes; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1648 | write_buffer->block = bytes; |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1649 | bytes += size; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1650 | fragments_outstanding --; |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1651 | queue_put(to_writer, write_buffer); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1652 | TRACE("fragment_locked writing fragment %d, compressed size %d" |
Phillip Lougher | cd6cb7a | 2013-05-29 02:31:39 +0100 | [diff] [blame] | 1653 | "\n", frg, size); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1654 | } |
| 1655 | fragments_locked = FALSE; |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1656 | pthread_cleanup_pop(1); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1659 | /* Called with the fragment_mutex locked */ |
plougher | b7a6681 | 2010-07-21 01:06:59 +0000 | [diff] [blame] | 1660 | void add_pending_fragment(struct file_buffer *write_buffer, int c_byte, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1661 | int fragment) |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 1662 | { |
Phillip Lougher | 6164b5f | 2013-05-23 05:05:10 +0100 | [diff] [blame] | 1663 | fragment_table[fragment].size = c_byte; |
| 1664 | write_buffer->block = fragment; |
| 1665 | |
| 1666 | queue_put(locked_fragment, write_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1670 | void write_fragment(struct file_buffer *fragment) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1671 | { |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1672 | if(fragment == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1673 | return; |
| 1674 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1675 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1676 | pthread_mutex_lock(&fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1677 | fragment_table[fragment->block].unused = 0; |
| 1678 | fragments_outstanding ++; |
| 1679 | queue_put(to_frag, fragment); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1680 | pthread_cleanup_pop(1); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | |
| 1684 | struct file_buffer *allocate_fragment() |
| 1685 | { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 1686 | struct file_buffer *fragment = cache_get(fragment_buffer, fragments); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1687 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1688 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1689 | pthread_mutex_lock(&fragment_mutex); |
| 1690 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1691 | if(fragments % FRAG_SIZE == 0) { |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1692 | void *ft = realloc(fragment_table, (fragments + |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 1693 | FRAG_SIZE) * sizeof(struct squashfs_fragment_entry)); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 1694 | if(ft == NULL) |
| 1695 | MEM_ERROR(); |
plougher | fa89c33 | 2010-07-27 00:27:15 +0000 | [diff] [blame] | 1696 | fragment_table = ft; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1697 | } |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1698 | |
| 1699 | fragment->size = 0; |
| 1700 | fragment->block = fragments ++; |
| 1701 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 1702 | pthread_cleanup_pop(1); |
| 1703 | |
Phillip Lougher | b9508be | 2011-06-13 02:25:51 +0100 | [diff] [blame] | 1704 | return fragment; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 1707 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1708 | static struct fragment empty_fragment = {SQUASHFS_INVALID_FRAG, 0, 0}; |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 1709 | |
| 1710 | |
| 1711 | void free_fragment(struct fragment *fragment) |
| 1712 | { |
| 1713 | if(fragment != &empty_fragment) |
| 1714 | free(fragment); |
| 1715 | } |
| 1716 | |
| 1717 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1718 | struct fragment *get_and_fill_fragment(struct file_buffer *file_buffer, |
| 1719 | struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1720 | { |
| 1721 | struct fragment *ffrg; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1722 | struct file_buffer **fragment; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1723 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1724 | if(file_buffer == NULL || file_buffer->size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1725 | return &empty_fragment; |
| 1726 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 1727 | fragment = eval_frag_actions(root_dir, dir_ent); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1728 | |
| 1729 | if((*fragment) && (*fragment)->size + file_buffer->size > block_size) { |
| 1730 | write_fragment(*fragment); |
| 1731 | *fragment = NULL; |
Phillip Lougher | 04b7b53 | 2011-06-11 02:14:15 +0100 | [diff] [blame] | 1732 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1733 | |
plougher | e7e6e83 | 2010-12-16 05:08:06 +0000 | [diff] [blame] | 1734 | ffrg = malloc(sizeof(struct fragment)); |
| 1735 | if(ffrg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1736 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1737 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1738 | if(*fragment == NULL) |
| 1739 | *fragment = allocate_fragment(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1740 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1741 | ffrg->index = (*fragment)->block; |
| 1742 | ffrg->offset = (*fragment)->size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1743 | ffrg->size = file_buffer->size; |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1744 | memcpy((*fragment)->data + (*fragment)->size, file_buffer->data, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1745 | file_buffer->size); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 1746 | (*fragment)->size += file_buffer->size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1747 | |
| 1748 | return ffrg; |
| 1749 | } |
| 1750 | |
| 1751 | |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1752 | long long generic_write_table(int length, void *buffer, int length2, |
| 1753 | void *buffer2, int uncompressed) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1754 | { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1755 | int meta_blocks = (length + SQUASHFS_METADATA_SIZE - 1) / |
| 1756 | SQUASHFS_METADATA_SIZE; |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1757 | long long *list, start_bytes; |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1758 | int compressed_size, i, list_size = meta_blocks * sizeof(long long); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1759 | unsigned short c_byte; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1760 | char cbuffer[(SQUASHFS_METADATA_SIZE << 2) + 2]; |
| 1761 | |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1762 | #ifdef SQUASHFS_TRACE |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1763 | long long obytes = bytes; |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1764 | int olength = length; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 1765 | #endif |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1766 | |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1767 | list = malloc(list_size); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1768 | if(list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1769 | MEM_ERROR(); |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1770 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1771 | for(i = 0; i < meta_blocks; i++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1772 | int avail_bytes = length > SQUASHFS_METADATA_SIZE ? |
| 1773 | SQUASHFS_METADATA_SIZE : length; |
| 1774 | c_byte = mangle(cbuffer + BLOCK_OFFSET, buffer + i * |
| 1775 | SQUASHFS_METADATA_SIZE , avail_bytes, |
| 1776 | SQUASHFS_METADATA_SIZE, uncompressed, 0); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1777 | SQUASHFS_SWAP_SHORTS(&c_byte, cbuffer, 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1778 | list[i] = bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1779 | compressed_size = SQUASHFS_COMPRESSED_SIZE(c_byte) + |
| 1780 | BLOCK_OFFSET; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1781 | TRACE("block %d @ 0x%llx, compressed size %d\n", i, bytes, |
| 1782 | compressed_size); |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 1783 | write_destination(fd, bytes, compressed_size, cbuffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1784 | bytes += compressed_size; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1785 | total_bytes += avail_bytes; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1786 | length -= avail_bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1789 | start_bytes = bytes; |
| 1790 | if(length2) { |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 1791 | write_destination(fd, bytes, length2, buffer2); |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1792 | bytes += length2; |
plougher | 10f7d57 | 2010-07-20 02:14:04 +0000 | [diff] [blame] | 1793 | total_bytes += length2; |
plougher | e6e0e1b | 2010-05-12 17:17:06 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
plougher | 1f288f6 | 2009-02-21 03:05:52 +0000 | [diff] [blame] | 1796 | SQUASHFS_INSWAP_LONG_LONGS(list, meta_blocks); |
Phillip Lougher | c130532 | 2012-11-02 22:28:02 +0000 | [diff] [blame] | 1797 | write_destination(fd, bytes, list_size, list); |
| 1798 | bytes += list_size; |
| 1799 | total_bytes += list_size; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1800 | |
plougher | 40d8b95 | 2010-02-12 16:26:32 +0000 | [diff] [blame] | 1801 | TRACE("generic_write_table: total uncompressed %d compressed %lld\n", |
| 1802 | olength, bytes - obytes); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1803 | |
Phillip Lougher | d4e78ee | 2012-10-31 21:32:40 +0000 | [diff] [blame] | 1804 | free(list); |
| 1805 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1806 | return start_bytes; |
| 1807 | } |
| 1808 | |
| 1809 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1810 | long long write_fragment_table() |
| 1811 | { |
| 1812 | unsigned int frag_bytes = SQUASHFS_FRAGMENT_BYTES(fragments); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1813 | int i; |
| 1814 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1815 | TRACE("write_fragment_table: fragments %d, frag_bytes %d\n", fragments, |
| 1816 | frag_bytes); |
plougher | ac28cd1 | 2010-02-24 02:25:03 +0000 | [diff] [blame] | 1817 | for(i = 0; i < fragments; i++) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1818 | TRACE("write_fragment_table: fragment %d, start_block 0x%llx, " |
| 1819 | "size %d\n", i, fragment_table[i].start_block, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1820 | fragment_table[i].size); |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1821 | SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Phillip Lougher | 162c24c | 2012-10-30 02:54:16 +0000 | [diff] [blame] | 1824 | return generic_write_table(frag_bytes, fragment_table, 0, NULL, noF); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1828 | char read_from_file_buffer[SQUASHFS_FILE_MAX_SIZE]; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 1829 | static char *read_from_disk(long long start, unsigned int avail_bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1830 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1831 | int res; |
| 1832 | |
| 1833 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1834 | if(res == 0) |
| 1835 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1836 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1837 | return read_from_file_buffer; |
| 1838 | } |
| 1839 | |
| 1840 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1841 | char read_from_file_buffer2[SQUASHFS_FILE_MAX_SIZE]; |
| 1842 | char *read_from_disk2(long long start, unsigned int avail_bytes) |
| 1843 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1844 | int res; |
| 1845 | |
| 1846 | res = read_fs_bytes(fd, start, avail_bytes, read_from_file_buffer2); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1847 | if(res == 0) |
| 1848 | return NULL; |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 1849 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 1850 | return read_from_file_buffer2; |
| 1851 | } |
| 1852 | |
| 1853 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1854 | /* |
| 1855 | * Compute 16 bit BSD checksum over the data |
| 1856 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1857 | unsigned short get_checksum(char *buff, int bytes, unsigned short chksum) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1858 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1859 | unsigned char *b = (unsigned char *) buff; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1860 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1861 | while(bytes --) { |
| 1862 | chksum = (chksum & 1) ? (chksum >> 1) | 0x8000 : chksum >> 1; |
| 1863 | chksum += *b++; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | return chksum; |
| 1867 | } |
| 1868 | |
| 1869 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1870 | unsigned short get_checksum_disk(long long start, long long l, |
| 1871 | unsigned int *blocks) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1872 | { |
| 1873 | unsigned short chksum = 0; |
| 1874 | unsigned int bytes; |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1875 | struct file_buffer *write_buffer; |
| 1876 | int i; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1877 | |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1878 | for(i = 0; l; i++) { |
| 1879 | bytes = SQUASHFS_COMPRESSED_SIZE_BLOCK(blocks[i]); |
| 1880 | if(bytes == 0) /* sparse block */ |
| 1881 | continue; |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 1882 | write_buffer = cache_lookup(bwriter_buffer, start); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1883 | if(write_buffer) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 1884 | chksum = get_checksum(write_buffer->data, bytes, |
| 1885 | chksum); |
plougher | 57e6d18 | 2008-05-06 23:51:29 +0000 | [diff] [blame] | 1886 | cache_block_put(write_buffer); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 1887 | } else { |
| 1888 | void *data = read_from_disk(start, bytes); |
| 1889 | if(data == NULL) { |
| 1890 | ERROR("Failed to checksum data from output" |
| 1891 | " filesystem\n"); |
| 1892 | BAD_ERROR("Output filesystem corrupted?\n"); |
| 1893 | } |
| 1894 | |
| 1895 | chksum = get_checksum(data, bytes, chksum); |
| 1896 | } |
| 1897 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1898 | l -= bytes; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1899 | start += bytes; |
| 1900 | } |
| 1901 | |
| 1902 | return chksum; |
| 1903 | } |
| 1904 | |
| 1905 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1906 | unsigned short get_checksum_mem(char *buff, int bytes) |
| 1907 | { |
| 1908 | return get_checksum(buff, bytes, 0); |
| 1909 | } |
| 1910 | |
| 1911 | |
| 1912 | unsigned short get_checksum_mem_buffer(struct file_buffer *file_buffer) |
| 1913 | { |
| 1914 | if(file_buffer == NULL) |
| 1915 | return 0; |
| 1916 | else |
| 1917 | return get_checksum(file_buffer->data, file_buffer->size, 0); |
| 1918 | } |
| 1919 | |
| 1920 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1921 | #define DUP_HASH(a) (a & 0xffff) |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1922 | 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] | 1923 | unsigned int *block_listp, int blocks, unsigned int fragment, |
| 1924 | int offset, int bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1925 | { |
| 1926 | struct fragment *frg; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1927 | unsigned int *block_list = block_listp; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1928 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1929 | struct append_file *append_file; |
| 1930 | struct file_info *file; |
plougher | 058eae4 | 2006-01-30 14:27:44 +0000 | [diff] [blame] | 1931 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1932 | if(!duplicate_checking || file_size == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1933 | return; |
| 1934 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1935 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) { |
| 1936 | if(file_size != dupl_ptr->file_size) |
| 1937 | continue; |
| 1938 | if(blocks != 0 && start != dupl_ptr->start) |
| 1939 | continue; |
| 1940 | if(fragment != dupl_ptr->fragment->index) |
| 1941 | continue; |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1942 | if(fragment != SQUASHFS_INVALID_FRAG && (offset != |
| 1943 | dupl_ptr->fragment->offset || bytes != |
| 1944 | dupl_ptr->fragment->size)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1945 | continue; |
| 1946 | return; |
| 1947 | } |
| 1948 | |
plougher | 3bb279c | 2010-12-16 05:10:03 +0000 | [diff] [blame] | 1949 | frg = malloc(sizeof(struct fragment)); |
| 1950 | if(frg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1951 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1952 | |
| 1953 | frg->index = fragment; |
| 1954 | frg->offset = offset; |
| 1955 | frg->size = bytes; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1956 | |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1957 | 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] | 1958 | FALSE, FALSE); |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 1959 | |
| 1960 | if(fragment == SQUASHFS_INVALID_FRAG) |
| 1961 | return; |
| 1962 | |
| 1963 | append_file = malloc(sizeof(struct append_file)); |
| 1964 | if(append_file == NULL) |
| 1965 | MEM_ERROR(); |
| 1966 | |
| 1967 | append_file->file = file; |
| 1968 | append_file->next = file_mapping[fragment]; |
| 1969 | file_mapping[fragment] = append_file; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1970 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1971 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1972 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1973 | int pre_duplicate(long long file_size) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1974 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1975 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1976 | |
| 1977 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1978 | if(dupl_ptr->file_size == file_size) |
| 1979 | return TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 1980 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1981 | return FALSE; |
| 1982 | } |
| 1983 | |
| 1984 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 1985 | struct file_info *add_non_dup(long long file_size, long long bytes, |
| 1986 | unsigned int *block_list, long long start, struct fragment *fragment, |
| 1987 | unsigned short checksum, unsigned short fragment_checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 1988 | int checksum_flag, int checksum_frag_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1989 | { |
plougher | 51ef9ae | 2010-12-16 05:14:28 +0000 | [diff] [blame] | 1990 | struct file_info *dupl_ptr = malloc(sizeof(struct file_info)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1991 | |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 1992 | if(dupl_ptr == NULL) |
| 1993 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 1994 | |
| 1995 | dupl_ptr->file_size = file_size; |
| 1996 | dupl_ptr->bytes = bytes; |
| 1997 | dupl_ptr->block_list = block_list; |
| 1998 | dupl_ptr->start = start; |
| 1999 | dupl_ptr->fragment = fragment; |
| 2000 | dupl_ptr->checksum = checksum; |
| 2001 | dupl_ptr->fragment_checksum = fragment_checksum; |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2002 | dupl_ptr->have_frag_checksum = checksum_frag_flag; |
Phillip Lougher | 557cefa | 2014-02-05 04:36:05 +0000 | [diff] [blame] | 2003 | dupl_ptr->have_checksum = checksum_flag; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2004 | |
| 2005 | pthread_cleanup_push((void *) pthread_mutex_unlock, &dup_mutex); |
| 2006 | pthread_mutex_lock(&dup_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2007 | dupl_ptr->next = dupl[DUP_HASH(file_size)]; |
| 2008 | dupl[DUP_HASH(file_size)] = dupl_ptr; |
| 2009 | dup_files ++; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2010 | pthread_cleanup_pop(1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2011 | |
| 2012 | return dupl_ptr; |
| 2013 | } |
| 2014 | |
| 2015 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2016 | struct fragment *frag_duplicate(struct file_buffer *file_buffer, char *dont_put) |
| 2017 | { |
| 2018 | struct file_info *dupl_ptr; |
| 2019 | struct file_buffer *buffer; |
| 2020 | struct file_info *dupl_start = file_buffer->dupl_start; |
| 2021 | long long file_size = file_buffer->file_size; |
| 2022 | unsigned short checksum = file_buffer->checksum; |
| 2023 | int res; |
| 2024 | |
| 2025 | if(file_buffer->duplicate) { |
| 2026 | TRACE("Found duplicate file, fragment %d, size %d, offset %d, " |
| 2027 | "checksum 0x%x\n", dupl_start->fragment->index, |
| 2028 | file_size, dupl_start->fragment->offset, checksum); |
| 2029 | *dont_put = TRUE; |
| 2030 | return dupl_start->fragment; |
| 2031 | } else { |
| 2032 | *dont_put = FALSE; |
| 2033 | dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 2034 | } |
| 2035 | |
| 2036 | for(; dupl_ptr && dupl_ptr != dupl_start; dupl_ptr = dupl_ptr->next) { |
| 2037 | if(file_size == dupl_ptr->file_size && file_size == |
| 2038 | dupl_ptr->fragment->size) { |
| 2039 | if(get_fragment_checksum(dupl_ptr) == checksum) { |
| 2040 | buffer = get_fragment(dupl_ptr->fragment); |
| 2041 | res = memcmp(file_buffer->data, buffer->data + |
| 2042 | dupl_ptr->fragment->offset, file_size); |
| 2043 | cache_block_put(buffer); |
| 2044 | if(res == 0) |
| 2045 | break; |
| 2046 | } |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | if(!dupl_ptr || dupl_ptr == dupl_start) |
| 2051 | return NULL; |
| 2052 | |
| 2053 | TRACE("Found duplicate file, fragment %d, size %d, offset %d, " |
| 2054 | "checksum 0x%x\n", dupl_ptr->fragment->index, file_size, |
| 2055 | dupl_ptr->fragment->offset, checksum); |
| 2056 | |
| 2057 | return dupl_ptr->fragment; |
| 2058 | } |
| 2059 | |
| 2060 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2061 | struct file_info *duplicate(long long file_size, long long bytes, |
| 2062 | unsigned int **block_list, long long *start, struct fragment **fragment, |
| 2063 | struct file_buffer *file_buffer, int blocks, unsigned short checksum, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2064 | int checksum_flag) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2065 | { |
| 2066 | struct file_info *dupl_ptr = dupl[DUP_HASH(file_size)]; |
| 2067 | int frag_bytes = file_buffer ? file_buffer->size : 0; |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2068 | unsigned short fragment_checksum = file_buffer ? |
| 2069 | file_buffer->checksum : 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2070 | |
| 2071 | for(; dupl_ptr; dupl_ptr = dupl_ptr->next) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2072 | if(file_size == dupl_ptr->file_size && bytes == dupl_ptr->bytes |
| 2073 | && frag_bytes == dupl_ptr->fragment->size) { |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2074 | long long target_start, dup_start = dupl_ptr->start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2075 | int block; |
| 2076 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2077 | if(memcmp(*block_list, dupl_ptr->block_list, blocks * |
| 2078 | sizeof(unsigned int)) != 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 2079 | continue; |
| 2080 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2081 | if(checksum_flag == FALSE) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2082 | checksum = get_checksum_disk(*start, bytes, |
| 2083 | *block_list); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2084 | checksum_flag = TRUE; |
| 2085 | } |
| 2086 | |
Phillip Lougher | 557cefa | 2014-02-05 04:36:05 +0000 | [diff] [blame] | 2087 | if(!dupl_ptr->have_checksum) { |
| 2088 | dupl_ptr->checksum = |
| 2089 | get_checksum_disk(dupl_ptr->start, |
| 2090 | dupl_ptr->bytes, dupl_ptr->block_list); |
| 2091 | dupl_ptr->have_checksum = TRUE; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2094 | if(checksum != dupl_ptr->checksum || |
| 2095 | fragment_checksum != |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2096 | get_fragment_checksum(dupl_ptr)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2097 | continue; |
| 2098 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2099 | target_start = *start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2100 | for(block = 0; block < blocks; block ++) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2101 | int size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2102 | ((*block_list)[block]); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2103 | struct file_buffer *target_buffer = NULL; |
| 2104 | struct file_buffer *dup_buffer = NULL; |
| 2105 | char *target_data, *dup_data; |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2106 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2107 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2108 | if(size == 0) |
plougher | fbf9f75 | 2007-08-12 05:02:24 +0000 | [diff] [blame] | 2109 | continue; |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2110 | target_buffer = cache_lookup(bwriter_buffer, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2111 | target_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2112 | if(target_buffer) |
| 2113 | target_data = target_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 2114 | else { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2115 | target_data = |
| 2116 | read_from_disk(target_start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2117 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 2118 | if(target_data == NULL) { |
| 2119 | ERROR("Failed to read data from" |
| 2120 | " output filesystem\n"); |
| 2121 | BAD_ERROR("Output filesystem" |
| 2122 | " corrupted?\n"); |
| 2123 | } |
| 2124 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2125 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2126 | dup_buffer = cache_lookup(bwriter_buffer, |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2127 | dup_start); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2128 | if(dup_buffer) |
| 2129 | dup_data = dup_buffer->data; |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 2130 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2131 | dup_data = read_from_disk2(dup_start, |
| 2132 | size); |
Phillip Lougher | ac73138 | 2013-05-19 04:19:44 +0100 | [diff] [blame] | 2133 | if(dup_data == NULL) { |
| 2134 | ERROR("Failed to read data from" |
| 2135 | " output filesystem\n"); |
| 2136 | BAD_ERROR("Output filesystem" |
| 2137 | " corrupted?\n"); |
| 2138 | } |
| 2139 | } |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2140 | |
| 2141 | res = memcmp(target_data, dup_data, size); |
| 2142 | cache_block_put(target_buffer); |
| 2143 | cache_block_put(dup_buffer); |
plougher | 0f46444 | 2008-03-31 00:27:56 +0000 | [diff] [blame] | 2144 | if(res != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2145 | break; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2146 | target_start += size; |
| 2147 | dup_start += size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2148 | } |
| 2149 | if(block == blocks) { |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2150 | struct file_buffer *frag_buffer = |
| 2151 | get_fragment(dupl_ptr->fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2152 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2153 | if(frag_bytes == 0 || |
| 2154 | memcmp(file_buffer->data, |
| 2155 | frag_buffer->data + |
| 2156 | dupl_ptr->fragment->offset, |
| 2157 | frag_bytes) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2158 | TRACE("Found duplicate file, start " |
| 2159 | "0x%llx, size %lld, checksum " |
| 2160 | "0x%x, fragment %d, size %d, " |
| 2161 | "offset %d, checksum 0x%x\n", |
| 2162 | dupl_ptr->start, |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2163 | dupl_ptr->bytes, |
| 2164 | dupl_ptr->checksum, |
| 2165 | dupl_ptr->fragment->index, |
| 2166 | frag_bytes, |
| 2167 | dupl_ptr->fragment->offset, |
| 2168 | fragment_checksum); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2169 | *block_list = dupl_ptr->block_list; |
| 2170 | *start = dupl_ptr->start; |
| 2171 | *fragment = dupl_ptr->fragment; |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2172 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2173 | return 0; |
| 2174 | } |
plougher | 76c6408 | 2008-03-08 01:32:23 +0000 | [diff] [blame] | 2175 | cache_block_put(frag_buffer); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | |
plougher | 17b269c | 2009-03-30 01:33:07 +0000 | [diff] [blame] | 2180 | return add_non_dup(file_size, bytes, *block_list, *start, *fragment, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2181 | checksum, fragment_checksum, checksum_flag, TRUE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 2185 | static inline int is_fragment(struct inode_info *inode) |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2186 | { |
Guan, Xin | 1eda7b8 | 2014-09-13 13:15:26 +0200 | [diff] [blame] | 2187 | off_t file_size = inode->buf.st_size; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2188 | |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2189 | /* |
| 2190 | * If this block is to be compressed differently to the |
| 2191 | * fragment compression then it cannot be a fragment |
| 2192 | */ |
| 2193 | if(inode->noF != noF) |
| 2194 | return FALSE; |
| 2195 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2196 | return !inode->no_fragments && file_size && (file_size < block_size || |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2197 | (inode->always_use_fragments && file_size & (block_size - 1))); |
| 2198 | } |
| 2199 | |
| 2200 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2201 | void put_file_buffer(struct file_buffer *file_buffer) |
| 2202 | { |
| 2203 | /* |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 2204 | * Decide where to send the file buffer: |
| 2205 | * - compressible non-fragment blocks go to the deflate threads, |
| 2206 | * - fragments go to the process fragment threads, |
| 2207 | * - all others go directly to the main thread |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2208 | */ |
| 2209 | if(file_buffer->error) { |
| 2210 | file_buffer->fragment = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2211 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 2212 | } else if (file_buffer->file_size == 0) |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2213 | seq_queue_put(to_main, file_buffer); |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 2214 | else if(file_buffer->fragment) |
| 2215 | queue_put(to_process_frag, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2216 | else |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 2217 | queue_put(to_deflate, file_buffer); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2221 | static int seq = 0; |
| 2222 | void reader_read_process(struct dir_ent *dir_ent) |
| 2223 | { |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2224 | long long bytes = 0; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2225 | struct inode_info *inode = dir_ent->inode; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2226 | struct file_buffer *prev_buffer = NULL, *file_buffer; |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2227 | int status, byte, res, child; |
| 2228 | int file = pseudo_exec_file(get_pseudo_file(inode->pseudo_id), &child); |
| 2229 | |
| 2230 | if(!file) { |
| 2231 | file_buffer = cache_get_nohash(reader_buffer); |
| 2232 | file_buffer->sequence = seq ++; |
| 2233 | goto read_err; |
| 2234 | } |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2235 | |
| 2236 | while(1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2237 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2238 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2239 | file_buffer->noD = inode->noD; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2240 | |
| 2241 | byte = read_bytes(file, file_buffer->data, block_size); |
| 2242 | if(byte == -1) |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2243 | goto read_err2; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2244 | |
| 2245 | file_buffer->size = byte; |
| 2246 | file_buffer->file_size = -1; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2247 | file_buffer->error = FALSE; |
| 2248 | file_buffer->fragment = FALSE; |
| 2249 | bytes += byte; |
| 2250 | |
| 2251 | if(byte == 0) |
| 2252 | break; |
| 2253 | |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2254 | /* |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2255 | * Update progress bar size. This is done |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2256 | * on every block rather than waiting for all blocks to be |
| 2257 | * read incase write_file_process() is running in parallel |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2258 | * with this. Otherwise the current progress bar position |
| 2259 | * may get ahead of the progress bar size. |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2260 | */ |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2261 | progress_bar_size(1); |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2262 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2263 | if(prev_buffer) |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2264 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2265 | prev_buffer = file_buffer; |
| 2266 | } |
| 2267 | |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2268 | /* |
| 2269 | * Update inode file size now that the size of the dynamic pseudo file |
| 2270 | * is known. This is needed for the -info option. |
| 2271 | */ |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2272 | inode->buf.st_size = bytes; |
plougher | 54d6729 | 2009-09-19 03:26:27 +0000 | [diff] [blame] | 2273 | |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2274 | res = waitpid(child, &status, 0); |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2275 | close(file); |
| 2276 | |
plougher | d87d8d1 | 2009-09-10 04:08:16 +0000 | [diff] [blame] | 2277 | if(res == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
plougher | ba674e8 | 2009-09-10 03:50:00 +0000 | [diff] [blame] | 2278 | goto read_err; |
| 2279 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2280 | if(prev_buffer == NULL) |
| 2281 | prev_buffer = file_buffer; |
| 2282 | else { |
| 2283 | cache_block_put(file_buffer); |
| 2284 | seq --; |
| 2285 | } |
| 2286 | prev_buffer->file_size = bytes; |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2287 | prev_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2288 | put_file_buffer(prev_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2289 | |
| 2290 | return; |
| 2291 | |
Phillip Lougher | a8c9ff2 | 2014-04-29 03:53:18 +0100 | [diff] [blame] | 2292 | read_err2: |
| 2293 | close(file); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2294 | read_err: |
| 2295 | if(prev_buffer) { |
| 2296 | cache_block_put(file_buffer); |
| 2297 | seq --; |
| 2298 | file_buffer = prev_buffer; |
| 2299 | } |
| 2300 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2301 | put_file_buffer(file_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2305 | void reader_read_file(struct dir_ent *dir_ent) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 2306 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2307 | struct stat *buf = &dir_ent->inode->buf, buf2; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2308 | struct file_buffer *file_buffer; |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2309 | int blocks, file, res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2310 | long long bytes, read_size; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2311 | struct inode_info *inode = dir_ent->inode; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2312 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2313 | if(inode->read) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2314 | return; |
| 2315 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 2316 | inode->read = TRUE; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2317 | again: |
| 2318 | bytes = 0; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2319 | read_size = buf->st_size; |
| 2320 | blocks = (read_size + block_size - 1) >> block_log; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2321 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 2322 | file = open(pathname_reader(dir_ent), O_RDONLY); |
plougher | c1d258e | 2010-12-16 05:16:45 +0000 | [diff] [blame] | 2323 | if(file == -1) { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2324 | file_buffer = cache_get_nohash(reader_buffer); |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2325 | file_buffer->sequence = seq ++; |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2326 | goto read_err2; |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2327 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2328 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2329 | do { |
Phillip Lougher | 316ab63 | 2013-04-29 02:53:39 +0100 | [diff] [blame] | 2330 | file_buffer = cache_get_nohash(reader_buffer); |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2331 | file_buffer->file_size = read_size; |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2332 | file_buffer->sequence = seq ++; |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2333 | file_buffer->noD = inode->noD; |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2334 | file_buffer->error = FALSE; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2335 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2336 | /* |
| 2337 | * Always try to read block_size bytes from the file rather |
| 2338 | * than expected bytes (which will be less than the block_size |
| 2339 | * at the file tail) to check that the file hasn't grown |
| 2340 | * since being stated. If it is longer (or shorter) than |
| 2341 | * expected, then restat, and try again. Note the special |
| 2342 | * case where the file is an exact multiple of the block_size |
| 2343 | * is dealt with later. |
| 2344 | */ |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2345 | file_buffer->size = read_bytes(file, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2346 | block_size); |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2347 | if(file_buffer->size == -1) |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2348 | goto read_err; |
| 2349 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2350 | bytes += file_buffer->size; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2351 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2352 | if(blocks > 1) { |
| 2353 | /* non-tail block should be exactly block_size */ |
| 2354 | if(file_buffer->size < block_size) |
| 2355 | goto restat; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2356 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2357 | file_buffer->fragment = FALSE; |
| 2358 | put_file_buffer(file_buffer); |
| 2359 | } |
Phillip Lougher | 8678d30 | 2014-03-02 03:02:19 +0000 | [diff] [blame] | 2360 | } while(-- blocks > 0); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2361 | |
Phillip Lougher | ce119e7 | 2014-03-11 04:14:00 +0000 | [diff] [blame] | 2362 | /* Overall size including tail should match */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2363 | if(read_size != bytes) |
| 2364 | goto restat; |
| 2365 | |
Phillip Lougher | 191dfcd | 2014-03-11 02:58:52 +0000 | [diff] [blame] | 2366 | if(read_size && read_size % block_size == 0) { |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2367 | /* |
| 2368 | * Special case where we've not tried to read past the end of |
| 2369 | * the file. We expect to get EOF, i.e. the file isn't larger |
| 2370 | * than we expect. |
| 2371 | */ |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2372 | char buffer; |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2373 | int res; |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2374 | |
plougher | a4c24ed | 2010-08-11 02:08:38 +0000 | [diff] [blame] | 2375 | res = read_bytes(file, &buffer, 1); |
| 2376 | if(res == -1) |
| 2377 | goto read_err; |
| 2378 | |
| 2379 | if(res != 0) |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2380 | goto restat; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Phillip Lougher | 2504b08 | 2011-09-07 02:28:45 +0100 | [diff] [blame] | 2383 | file_buffer->fragment = is_fragment(inode); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2384 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2385 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2386 | close(file); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2387 | |
| 2388 | return; |
| 2389 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2390 | restat: |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2391 | res = fstat(file, &buf2); |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2392 | if(res == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 2393 | ERROR("Cannot stat dir/file %s because %s\n", |
Phillip Lougher | 66f7bfd | 2012-11-29 23:54:18 +0000 | [diff] [blame] | 2394 | pathname_reader(dir_ent), strerror(errno)); |
| 2395 | goto read_err; |
| 2396 | } |
| 2397 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2398 | if(read_size != buf2.st_size) { |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2399 | close(file); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2400 | memcpy(buf, &buf2, sizeof(struct stat)); |
| 2401 | file_buffer->error = 2; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2402 | put_file_buffer(file_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2403 | goto again; |
| 2404 | } |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2405 | read_err: |
Phillip Lougher | 2302f69 | 2012-12-27 04:05:02 +0000 | [diff] [blame] | 2406 | close(file); |
| 2407 | read_err2: |
plougher | 1e38070 | 2010-08-11 01:52:49 +0000 | [diff] [blame] | 2408 | file_buffer->error = TRUE; |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2409 | put_file_buffer(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | void reader_scan(struct dir_info *dir) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2414 | struct dir_ent *dir_ent = dir->list; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2415 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 2416 | for(; dir_ent; dir_ent = dir_ent->next) { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2417 | struct stat *buf = &dir_ent->inode->buf; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 2418 | if(dir_ent->inode->root_entry) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2419 | continue; |
| 2420 | |
plougher | b3977eb | 2010-05-02 02:08:48 +0000 | [diff] [blame] | 2421 | if(IS_PSEUDO_PROCESS(dir_ent->inode)) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2422 | reader_read_process(dir_ent); |
| 2423 | continue; |
| 2424 | } |
| 2425 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2426 | switch(buf->st_mode & S_IFMT) { |
| 2427 | case S_IFREG: |
| 2428 | reader_read_file(dir_ent); |
| 2429 | break; |
| 2430 | case S_IFDIR: |
| 2431 | reader_scan(dir_ent->dir); |
| 2432 | break; |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | |
| 2438 | void *reader(void *arg) |
| 2439 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2440 | if(!sorted) |
| 2441 | reader_scan(queue_get(to_reader)); |
| 2442 | else { |
| 2443 | int i; |
| 2444 | struct priority_entry *entry; |
| 2445 | |
| 2446 | queue_get(to_reader); |
| 2447 | for(i = 65535; i >= 0; i--) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 2448 | for(entry = priority_list[i]; entry; |
| 2449 | entry = entry->next) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2450 | reader_read_file(entry->dir); |
| 2451 | } |
rlougher | e4873e0 | 2007-11-08 17:50:42 +0000 | [diff] [blame] | 2452 | |
| 2453 | pthread_exit(NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | |
| 2457 | void *writer(void *arg) |
| 2458 | { |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2459 | while(1) { |
| 2460 | struct file_buffer *file_buffer = queue_get(to_writer); |
| 2461 | off_t off; |
| 2462 | |
| 2463 | if(file_buffer == NULL) { |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2464 | queue_put(from_writer, NULL); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2465 | continue; |
| 2466 | } |
| 2467 | |
| 2468 | off = file_buffer->block; |
| 2469 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2470 | pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2471 | pthread_mutex_lock(&pos_mutex); |
| 2472 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 2473 | if(lseek(fd, off, SEEK_SET) == -1) { |
Phillip Lougher | d23000c | 2013-02-06 22:05:45 +0000 | [diff] [blame] | 2474 | ERROR("writer: Lseek on destination failed because " |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 2475 | "%s, offset=0x%llx\n", strerror(errno), off); |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2476 | BAD_ERROR("Probably out of space on output " |
Phillip Lougher | 3f4ccd2 | 2013-02-11 04:40:09 +0000 | [diff] [blame] | 2477 | "%s\n", block_device ? "block device" : |
| 2478 | "filesystem"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Phillip Lougher | d2ffa3c | 2013-02-11 02:53:54 +0000 | [diff] [blame] | 2481 | if(write_bytes(fd, file_buffer->data, |
Phillip Lougher | 9bfa0b8 | 2013-04-06 05:05:15 +0100 | [diff] [blame] | 2482 | file_buffer->size) == -1) |
| 2483 | BAD_ERROR("Failed to write to output %s\n", |
Phillip Lougher | e9df455 | 2013-02-14 23:06:29 +0000 | [diff] [blame] | 2484 | block_device ? "block device" : "filesystem"); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2485 | |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 2486 | pthread_cleanup_pop(1); |
| 2487 | |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2488 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2493 | int all_zero(struct file_buffer *file_buffer) |
| 2494 | { |
| 2495 | int i; |
| 2496 | long entries = file_buffer->size / sizeof(long); |
| 2497 | long *p = (long *) file_buffer->data; |
| 2498 | |
| 2499 | for(i = 0; i < entries && p[i] == 0; i++); |
| 2500 | |
| 2501 | if(i == entries) { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2502 | for(i = file_buffer->size & ~(sizeof(long) - 1); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2503 | i < file_buffer->size && file_buffer->data[i] == 0; |
| 2504 | i++); |
plougher | 5b09fd4 | 2007-08-06 10:28:41 +0000 | [diff] [blame] | 2505 | |
| 2506 | return i == file_buffer->size; |
| 2507 | } |
| 2508 | |
| 2509 | return 0; |
| 2510 | } |
| 2511 | |
| 2512 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2513 | void *deflator(void *arg) |
| 2514 | { |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2515 | struct file_buffer *write_buffer = cache_get_nohash(bwriter_buffer); |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2516 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2517 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2518 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2519 | res = compressor_init(comp, &stream, block_size, 1); |
| 2520 | if(res) |
| 2521 | BAD_ERROR("deflator:: compressor_init failed\n"); |
| 2522 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2523 | while(1) { |
Phillip Lougher | cf478e9 | 2013-05-29 02:38:41 +0100 | [diff] [blame] | 2524 | struct file_buffer *file_buffer = queue_get(to_deflate); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2525 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2526 | if(sparse_files && all_zero(file_buffer)) { |
Phillip Lougher | 4885438 | 2011-09-09 03:36:55 +0100 | [diff] [blame] | 2527 | file_buffer->c_byte = 0; |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2528 | seq_queue_put(to_main, file_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2529 | } else { |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2530 | write_buffer->c_byte = mangle2(stream, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2531 | write_buffer->data, file_buffer->data, |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 2532 | file_buffer->size, block_size, |
| 2533 | file_buffer->noD, 1); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2534 | write_buffer->sequence = file_buffer->sequence; |
| 2535 | write_buffer->file_size = file_buffer->file_size; |
| 2536 | write_buffer->block = file_buffer->block; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2537 | write_buffer->size = SQUASHFS_COMPRESSED_SIZE_BLOCK |
| 2538 | (write_buffer->c_byte); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2539 | write_buffer->fragment = FALSE; |
| 2540 | write_buffer->error = FALSE; |
| 2541 | cache_block_put(file_buffer); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 2542 | seq_queue_put(to_main, write_buffer); |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2543 | write_buffer = cache_get_nohash(bwriter_buffer); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2544 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | |
| 2549 | void *frag_deflator(void *arg) |
| 2550 | { |
plougher | 7b8ee50 | 2009-07-29 07:54:30 +0000 | [diff] [blame] | 2551 | void *stream = NULL; |
Phillip Lougher | 3c83829 | 2013-03-26 04:24:53 +0000 | [diff] [blame] | 2552 | int res; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2553 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2554 | res = compressor_init(comp, &stream, block_size, 1); |
| 2555 | if(res) |
| 2556 | BAD_ERROR("frag_deflator:: compressor_init failed\n"); |
| 2557 | |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2558 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
| 2559 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2560 | while(1) { |
| 2561 | int c_byte, compressed_size; |
| 2562 | struct file_buffer *file_buffer = queue_get(to_frag); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2563 | struct file_buffer *write_buffer = |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2564 | cache_get(fwriter_buffer, file_buffer->block); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2565 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 2566 | c_byte = mangle2(stream, write_buffer->data, file_buffer->data, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2567 | file_buffer->size, block_size, noF, 1); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2568 | compressed_size = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte); |
plougher | d036a31 | 2008-03-08 01:38:27 +0000 | [diff] [blame] | 2569 | write_buffer->size = compressed_size; |
plougher | d1139d5 | 2008-04-28 03:07:07 +0000 | [diff] [blame] | 2570 | pthread_mutex_lock(&fragment_mutex); |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2571 | if(fragments_locked == FALSE) { |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2572 | fragment_table[file_buffer->block].size = c_byte; |
| 2573 | fragment_table[file_buffer->block].start_block = bytes; |
| 2574 | write_buffer->block = bytes; |
| 2575 | bytes += compressed_size; |
| 2576 | fragments_outstanding --; |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2577 | queue_put(to_writer, write_buffer); |
Phillip Lougher | 1ec844d | 2014-09-18 01:28:11 +0100 | [diff] [blame] | 2578 | pthread_mutex_unlock(&fragment_mutex); |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2579 | TRACE("Writing fragment %lld, uncompressed size %d, " |
| 2580 | "compressed size %d\n", file_buffer->block, |
| 2581 | file_buffer->size, compressed_size); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2582 | } else { |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2583 | add_pending_fragment(write_buffer, c_byte, |
| 2584 | file_buffer->block); |
Phillip Lougher | 39d7c4f | 2013-04-30 23:42:28 +0100 | [diff] [blame] | 2585 | pthread_mutex_unlock(&fragment_mutex); |
| 2586 | } |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2587 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2588 | } |
Phillip Lougher | 53240e6 | 2013-04-30 23:51:29 +0100 | [diff] [blame] | 2589 | |
| 2590 | pthread_cleanup_pop(0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2594 | struct file_buffer *get_file_buffer() |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2595 | { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2596 | struct file_buffer *file_buffer = seq_queue_get(to_main); |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2597 | |
Phillip Lougher | 4bb21cb | 2013-05-19 04:27:38 +0100 | [diff] [blame] | 2598 | return file_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2602 | void write_file_empty(squashfs_inode *inode, struct dir_ent *dir_ent, |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2603 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2604 | { |
| 2605 | file_count ++; |
| 2606 | *duplicate_file = FALSE; |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 2607 | cache_block_put(file_buffer); |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2608 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, 0, 0, 0, |
| 2609 | NULL, &empty_fragment, NULL, 0); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | |
Phillip Lougher | 8531f87 | 2014-03-01 03:22:41 +0000 | [diff] [blame] | 2613 | void write_file_frag(squashfs_inode *inode, struct dir_ent *dir_ent, |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2614 | struct file_buffer *file_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2615 | { |
Phillip Lougher | 8531f87 | 2014-03-01 03:22:41 +0000 | [diff] [blame] | 2616 | int size = file_buffer->file_size; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2617 | struct fragment *fragment; |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2618 | unsigned short checksum = file_buffer->checksum; |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2619 | char dont_put; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2620 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2621 | fragment = frag_duplicate(file_buffer, &dont_put); |
| 2622 | *duplicate_file = !fragment; |
| 2623 | if(!fragment) { |
| 2624 | fragment = get_and_fill_fragment(file_buffer, dir_ent); |
| 2625 | if(duplicate_checking) |
| 2626 | add_non_dup(size, 0, NULL, 0, fragment, 0, checksum, |
| 2627 | TRUE, TRUE); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2628 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2629 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2630 | if(dont_put) |
| 2631 | free(file_buffer); |
| 2632 | else |
| 2633 | cache_block_put(file_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2634 | |
| 2635 | total_bytes += size; |
| 2636 | file_count ++; |
| 2637 | |
plougher | 35a1060 | 2008-04-21 02:58:16 +0000 | [diff] [blame] | 2638 | inc_progress_bar(); |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 2639 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2640 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, size, 0, |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 2641 | 0, NULL, fragment, NULL, 0); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2642 | |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 2643 | if(!duplicate_checking) |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2644 | free_fragment(fragment); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2648 | int write_file_process(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2649 | struct file_buffer *read_buffer, int *duplicate_file) |
| 2650 | { |
| 2651 | long long read_size, file_bytes, start; |
| 2652 | struct fragment *fragment; |
| 2653 | unsigned int *block_list = NULL; |
| 2654 | int block = 0, status; |
| 2655 | long long sparse = 0; |
| 2656 | struct file_buffer *fragment_buffer = NULL; |
| 2657 | |
| 2658 | *duplicate_file = FALSE; |
| 2659 | |
| 2660 | lock_fragments(); |
| 2661 | |
| 2662 | file_bytes = 0; |
| 2663 | start = bytes; |
| 2664 | while (1) { |
| 2665 | read_size = read_buffer->file_size; |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2666 | if(read_buffer->fragment) |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2667 | fragment_buffer = read_buffer; |
| 2668 | else { |
| 2669 | block_list = realloc(block_list, (block + 1) * |
| 2670 | sizeof(unsigned int)); |
| 2671 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2672 | MEM_ERROR(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2673 | block_list[block ++] = read_buffer->c_byte; |
| 2674 | if(read_buffer->c_byte) { |
| 2675 | read_buffer->block = bytes; |
| 2676 | bytes += read_buffer->size; |
Phillip Lougher | 84e20d7 | 2014-02-23 05:23:41 +0000 | [diff] [blame] | 2677 | cache_hash(read_buffer, read_buffer->block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2678 | file_bytes += read_buffer->size; |
| 2679 | queue_put(to_writer, read_buffer); |
| 2680 | } else { |
| 2681 | sparse += read_buffer->size; |
| 2682 | cache_block_put(read_buffer); |
| 2683 | } |
| 2684 | } |
plougher | 286b6b3 | 2009-09-19 03:29:27 +0000 | [diff] [blame] | 2685 | inc_progress_bar(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2686 | |
| 2687 | if(read_size != -1) |
| 2688 | break; |
| 2689 | |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2690 | read_buffer = get_file_buffer(); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2691 | if(read_buffer->error) |
| 2692 | goto read_err; |
| 2693 | } |
| 2694 | |
| 2695 | unlock_fragments(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2696 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2697 | |
| 2698 | if(duplicate_checking) |
| 2699 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2700 | 0, fragment_buffer ? fragment_buffer->checksum : 0, |
| 2701 | FALSE, TRUE); |
| 2702 | cache_block_put(fragment_buffer); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2703 | file_count ++; |
| 2704 | total_bytes += read_size; |
| 2705 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2706 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2707 | block, block_list, fragment, NULL, sparse); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2708 | |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2709 | if(duplicate_checking == FALSE) { |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2710 | free(block_list); |
Phillip Lougher | d2f045f | 2012-12-28 03:15:45 +0000 | [diff] [blame] | 2711 | free_fragment(fragment); |
| 2712 | } |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2713 | |
| 2714 | return 0; |
| 2715 | |
| 2716 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2717 | dec_progress_bar(block); |
plougher | 00d0817 | 2009-09-03 10:17:44 +0000 | [diff] [blame] | 2718 | status = read_buffer->error; |
| 2719 | bytes = start; |
| 2720 | if(!block_device) { |
| 2721 | int res; |
| 2722 | |
| 2723 | queue_put(to_writer, NULL); |
| 2724 | if(queue_get(from_writer) != 0) |
| 2725 | EXIT_MKSQUASHFS(); |
| 2726 | res = ftruncate(fd, bytes); |
| 2727 | if(res != 0) |
| 2728 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2729 | strerror(errno)); |
| 2730 | } |
| 2731 | unlock_fragments(); |
| 2732 | free(block_list); |
| 2733 | cache_block_put(read_buffer); |
| 2734 | return status; |
| 2735 | } |
| 2736 | |
| 2737 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2738 | 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] | 2739 | struct file_buffer *read_buffer, int *duplicate_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2740 | { |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2741 | int block, thresh; |
Phillip Lougher | 1807caa | 2014-03-01 03:28:33 +0000 | [diff] [blame] | 2742 | long long read_size = read_buffer->file_size; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2743 | long long file_bytes, dup_start, start; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2744 | struct fragment *fragment; |
| 2745 | struct file_info *dupl_ptr; |
| 2746 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2747 | unsigned int *block_list, *block_listp; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2748 | struct file_buffer **buffer_list; |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2749 | int status; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2750 | long long sparse = 0; |
| 2751 | struct file_buffer *fragment_buffer = NULL; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2752 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2753 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2754 | if(block_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2755 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2756 | block_listp = block_list; |
| 2757 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 2758 | buffer_list = malloc(blocks * sizeof(struct file_buffer *)); |
| 2759 | if(buffer_list == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 2760 | MEM_ERROR(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2761 | |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 2762 | lock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2763 | |
| 2764 | file_bytes = 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2765 | start = dup_start = bytes; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 2766 | thresh = blocks > bwriter_size ? blocks - bwriter_size : 0; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2767 | |
| 2768 | for(block = 0; block < blocks;) { |
Phillip Lougher | b1ed0df | 2013-05-20 04:44:04 +0100 | [diff] [blame] | 2769 | if(read_buffer->fragment) { |
Phillip Lougher | 38cb1ab | 2013-01-13 21:20:09 +0000 | [diff] [blame] | 2770 | block_list[block] = 0; |
Phillip Lougher | 19de5b6 | 2013-01-13 21:31:51 +0000 | [diff] [blame] | 2771 | buffer_list[block] = NULL; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2772 | fragment_buffer = read_buffer; |
| 2773 | blocks = read_size >> block_log; |
| 2774 | } else { |
| 2775 | block_list[block] = read_buffer->c_byte; |
| 2776 | |
| 2777 | if(read_buffer->c_byte) { |
| 2778 | read_buffer->block = bytes; |
| 2779 | bytes += read_buffer->size; |
| 2780 | file_bytes += read_buffer->size; |
Phillip Lougher | 84e20d7 | 2014-02-23 05:23:41 +0000 | [diff] [blame] | 2781 | cache_hash(read_buffer, read_buffer->block); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2782 | if(block < thresh) { |
| 2783 | buffer_list[block] = NULL; |
| 2784 | queue_put(to_writer, read_buffer); |
| 2785 | } else |
| 2786 | buffer_list[block] = read_buffer; |
| 2787 | } else { |
| 2788 | buffer_list[block] = NULL; |
| 2789 | sparse += read_buffer->size; |
| 2790 | cache_block_put(read_buffer); |
| 2791 | } |
| 2792 | } |
| 2793 | inc_progress_bar(); |
| 2794 | |
| 2795 | if(++block < blocks) { |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2796 | read_buffer = get_file_buffer(); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2797 | if(read_buffer->error) |
| 2798 | goto read_err; |
| 2799 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 2802 | dupl_ptr = duplicate(read_size, file_bytes, &block_listp, &dup_start, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2803 | &fragment, fragment_buffer, blocks, 0, FALSE); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2804 | |
| 2805 | if(dupl_ptr) { |
| 2806 | *duplicate_file = FALSE; |
| 2807 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2808 | if(buffer_list[block]) |
| 2809 | queue_put(to_writer, buffer_list[block]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 2810 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2811 | dupl_ptr->fragment = fragment; |
| 2812 | } else { |
| 2813 | *duplicate_file = TRUE; |
| 2814 | for(block = thresh; block < blocks; block ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2815 | cache_block_put(buffer_list[block]); |
| 2816 | bytes = start; |
| 2817 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2818 | int res; |
| 2819 | |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2820 | queue_put(to_writer, NULL); |
| 2821 | if(queue_get(from_writer) != 0) |
| 2822 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2823 | res = ftruncate(fd, bytes); |
| 2824 | if(res != 0) |
| 2825 | BAD_ERROR("Failed to truncate dest file because" |
| 2826 | " %s\n", strerror(errno)); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2827 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2830 | unlock_fragments(); |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2831 | cache_block_put(fragment_buffer); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2832 | free(buffer_list); |
| 2833 | file_count ++; |
| 2834 | total_bytes += read_size; |
| 2835 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2836 | /* |
| 2837 | * sparse count is needed to ensure squashfs correctly reports a |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2838 | * a smaller block count on stat calls to sparse files. This is |
| 2839 | * to ensure intelligent applications like cp correctly handle the |
| 2840 | * file as a sparse file. If the file in the original filesystem isn't |
| 2841 | * stored as a sparse file then still store it sparsely in squashfs, but |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 2842 | * report it as non-sparse on stat calls to preserve semantics |
| 2843 | */ |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2844 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2845 | sparse = 0; |
| 2846 | |
plougher | c1ace52 | 2010-05-01 03:00:45 +0000 | [diff] [blame] | 2847 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, |
| 2848 | dup_start, blocks, block_listp, fragment, NULL, sparse); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2849 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2850 | if(*duplicate_file == TRUE) |
| 2851 | free(block_list); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 2852 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2853 | return 0; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2854 | |
| 2855 | read_err: |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 2856 | dec_progress_bar(block); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2857 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2858 | bytes = start; |
| 2859 | if(thresh && !block_device) { |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2860 | int res; |
| 2861 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2862 | queue_put(to_writer, NULL); |
| 2863 | if(queue_get(from_writer) != 0) |
| 2864 | EXIT_MKSQUASHFS(); |
plougher | 12a159a | 2009-03-03 11:06:34 +0000 | [diff] [blame] | 2865 | res = ftruncate(fd, bytes); |
| 2866 | if(res != 0) |
| 2867 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2868 | strerror(errno)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2869 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 2870 | unlock_fragments(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2871 | for(blocks = thresh; blocks < block; blocks ++) |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 2872 | cache_block_put(buffer_list[blocks]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2873 | free(buffer_list); |
| 2874 | free(block_list); |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 2875 | cache_block_put(read_buffer); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2876 | return status; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2880 | int write_file_blocks(squashfs_inode *inode, struct dir_ent *dir_ent, |
| 2881 | struct file_buffer *read_buffer, int *dup) |
| 2882 | { |
| 2883 | long long read_size = read_buffer->file_size; |
| 2884 | long long file_bytes, start; |
| 2885 | struct fragment *fragment; |
| 2886 | unsigned int *block_list; |
| 2887 | int block, status; |
| 2888 | int blocks = (read_size + block_size - 1) >> block_log; |
| 2889 | long long sparse = 0; |
| 2890 | struct file_buffer *fragment_buffer = NULL; |
| 2891 | |
| 2892 | if(pre_duplicate(read_size)) |
| 2893 | return write_file_blocks_dup(inode, dir_ent, read_buffer, dup); |
| 2894 | |
| 2895 | *dup = FALSE; |
| 2896 | |
| 2897 | block_list = malloc(blocks * sizeof(unsigned int)); |
| 2898 | if(block_list == NULL) |
| 2899 | MEM_ERROR(); |
| 2900 | |
| 2901 | lock_fragments(); |
| 2902 | |
| 2903 | file_bytes = 0; |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 2904 | /* ANDROID CHANGES START*/ |
| 2905 | #ifdef ANDROID |
| 2906 | if (align_4k_blocks && bytes % 4096) { |
| 2907 | bytes += 4096 - (bytes % 4096); |
| 2908 | } |
| 2909 | #endif |
| 2910 | /* ANDROID CHANGES END */ |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2911 | start = bytes; |
| 2912 | for(block = 0; block < blocks;) { |
| 2913 | if(read_buffer->fragment) { |
| 2914 | block_list[block] = 0; |
| 2915 | fragment_buffer = read_buffer; |
| 2916 | blocks = read_size >> block_log; |
| 2917 | } else { |
| 2918 | block_list[block] = read_buffer->c_byte; |
| 2919 | if(read_buffer->c_byte) { |
| 2920 | read_buffer->block = bytes; |
| 2921 | bytes += read_buffer->size; |
| 2922 | cache_hash(read_buffer, read_buffer->block); |
| 2923 | file_bytes += read_buffer->size; |
| 2924 | queue_put(to_writer, read_buffer); |
| 2925 | } else { |
| 2926 | sparse += read_buffer->size; |
| 2927 | cache_block_put(read_buffer); |
| 2928 | } |
| 2929 | } |
| 2930 | inc_progress_bar(); |
| 2931 | |
| 2932 | if(++block < blocks) { |
| 2933 | read_buffer = get_file_buffer(); |
| 2934 | if(read_buffer->error) |
| 2935 | goto read_err; |
| 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | unlock_fragments(); |
| 2940 | fragment = get_and_fill_fragment(fragment_buffer, dir_ent); |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2941 | |
| 2942 | if(duplicate_checking) |
| 2943 | add_non_dup(read_size, file_bytes, block_list, start, fragment, |
Phillip Lougher | c6424dc | 2014-03-13 02:01:28 +0000 | [diff] [blame] | 2944 | 0, fragment_buffer ? fragment_buffer->checksum : 0, |
| 2945 | FALSE, TRUE); |
| 2946 | cache_block_put(fragment_buffer); |
Phillip Lougher | 75bd21d | 2014-03-02 03:33:56 +0000 | [diff] [blame] | 2947 | file_count ++; |
| 2948 | total_bytes += read_size; |
| 2949 | |
| 2950 | /* |
| 2951 | * sparse count is needed to ensure squashfs correctly reports a |
| 2952 | * a smaller block count on stat calls to sparse files. This is |
| 2953 | * to ensure intelligent applications like cp correctly handle the |
| 2954 | * file as a sparse file. If the file in the original filesystem isn't |
| 2955 | * stored as a sparse file then still store it sparsely in squashfs, but |
| 2956 | * report it as non-sparse on stat calls to preserve semantics |
| 2957 | */ |
| 2958 | if(sparse && (dir_ent->inode->buf.st_blocks << 9) >= read_size) |
| 2959 | sparse = 0; |
| 2960 | |
| 2961 | create_inode(inode, NULL, dir_ent, SQUASHFS_FILE_TYPE, read_size, start, |
| 2962 | blocks, block_list, fragment, NULL, sparse); |
| 2963 | |
| 2964 | if(duplicate_checking == FALSE) { |
| 2965 | free(block_list); |
| 2966 | free_fragment(fragment); |
| 2967 | } |
| 2968 | |
| 2969 | return 0; |
| 2970 | |
| 2971 | read_err: |
| 2972 | dec_progress_bar(block); |
| 2973 | status = read_buffer->error; |
| 2974 | bytes = start; |
| 2975 | if(!block_device) { |
| 2976 | int res; |
| 2977 | |
| 2978 | queue_put(to_writer, NULL); |
| 2979 | if(queue_get(from_writer) != 0) |
| 2980 | EXIT_MKSQUASHFS(); |
| 2981 | res = ftruncate(fd, bytes); |
| 2982 | if(res != 0) |
| 2983 | BAD_ERROR("Failed to truncate dest file because %s\n", |
| 2984 | strerror(errno)); |
| 2985 | } |
| 2986 | unlock_fragments(); |
| 2987 | free(block_list); |
| 2988 | cache_block_put(read_buffer); |
| 2989 | return status; |
| 2990 | } |
| 2991 | |
| 2992 | |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 2993 | void write_file(squashfs_inode *inode, struct dir_ent *dir, int *dup) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2994 | { |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2995 | int status; |
| 2996 | struct file_buffer *read_buffer; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 2997 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 2998 | again: |
Phillip Lougher | c6a1bc5 | 2013-05-20 04:08:42 +0100 | [diff] [blame] | 2999 | read_buffer = get_file_buffer(); |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 3000 | status = read_buffer->error; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 3001 | |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 3002 | if(status) |
plougher | eb6eac9 | 2008-02-26 01:50:48 +0000 | [diff] [blame] | 3003 | cache_block_put(read_buffer); |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 3004 | else if(read_buffer->file_size == -1) |
| 3005 | status = write_file_process(inode, dir, read_buffer, dup); |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 3006 | else if(read_buffer->file_size == 0) |
| 3007 | write_file_empty(inode, dir, read_buffer, dup); |
| 3008 | else if(read_buffer->fragment && read_buffer->c_byte) |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 3009 | write_file_frag(inode, dir, read_buffer, dup); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3010 | else |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 3011 | status = write_file_blocks(inode, dir, read_buffer, dup); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3012 | |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3013 | if(status == 2) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3014 | ERROR("File %s changed size while reading filesystem, " |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 3015 | "attempting to re-read\n", pathname(dir)); |
plougher | 018d2b3 | 2007-04-23 03:01:48 +0000 | [diff] [blame] | 3016 | goto again; |
| 3017 | } else if(status == 1) { |
Phillip Lougher | c348913 | 2014-03-02 01:41:36 +0000 | [diff] [blame] | 3018 | ERROR_START("Failed to read file %s", pathname(dir)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3019 | ERROR_EXIT(", creating empty file\n"); |
Phillip Lougher | 948bf3a | 2014-03-02 02:23:54 +0000 | [diff] [blame] | 3020 | write_file_empty(inode, dir, NULL, dup); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3021 | } |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 3025 | #define BUFF_SIZE 512 |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3026 | char *name; |
| 3027 | char *basename_r(); |
| 3028 | |
| 3029 | char *getbase(char *pathname) |
| 3030 | { |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 3031 | static char *b_buffer = NULL; |
| 3032 | static int b_size = BUFF_SIZE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3033 | char *result; |
| 3034 | |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 3035 | if(b_buffer == NULL) { |
| 3036 | b_buffer = malloc(b_size); |
| 3037 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3038 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | while(1) { |
| 3042 | if(*pathname != '/') { |
| 3043 | result = getcwd(b_buffer, b_size); |
| 3044 | if(result == NULL && errno != ERANGE) |
| 3045 | BAD_ERROR("Getcwd failed in getbase\n"); |
| 3046 | |
| 3047 | /* enough room for pathname + "/" + '\0' terminator? */ |
| 3048 | if(result && strlen(pathname) + 2 <= |
| 3049 | b_size - strlen(b_buffer)) { |
| 3050 | strcat(strcat(b_buffer, "/"), pathname); |
| 3051 | break; |
| 3052 | } |
| 3053 | } else if(strlen(pathname) < b_size) { |
| 3054 | strcpy(b_buffer, pathname); |
| 3055 | break; |
| 3056 | } |
| 3057 | |
| 3058 | /* Buffer not large enough, realloc and try again */ |
| 3059 | b_buffer = realloc(b_buffer, b_size += BUFF_SIZE); |
| 3060 | if(b_buffer == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3061 | MEM_ERROR(); |
Phillip Lougher | 3f81a77 | 2012-12-04 05:07:24 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3064 | name = b_buffer; |
| 3065 | if(((result = basename_r()) == NULL) || (strcmp(result, "..") == 0)) |
| 3066 | return NULL; |
| 3067 | else |
| 3068 | return result; |
| 3069 | } |
| 3070 | |
| 3071 | |
| 3072 | char *basename_r() |
| 3073 | { |
| 3074 | char *s; |
| 3075 | char *p; |
| 3076 | int n = 1; |
| 3077 | |
| 3078 | for(;;) { |
| 3079 | s = name; |
| 3080 | if(*name == '\0') |
| 3081 | return NULL; |
| 3082 | if(*name != '/') { |
| 3083 | while(*name != '\0' && *name != '/') name++; |
| 3084 | n = name - s; |
| 3085 | } |
| 3086 | while(*name == '/') name++; |
| 3087 | if(strncmp(s, ".", n) == 0) |
| 3088 | continue; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3089 | if((*name == '\0') || (strncmp(s, "..", n) == 0) || |
| 3090 | ((p = basename_r()) == NULL)) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3091 | s[n] = '\0'; |
| 3092 | return s; |
| 3093 | } |
| 3094 | if(strcmp(p, "..") == 0) |
| 3095 | continue; |
| 3096 | return p; |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 3101 | struct inode_info *lookup_inode3(struct stat *buf, int pseudo, int id, |
| 3102 | char *symlink, int bytes) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3103 | { |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3104 | int ino_hash = INODE_HASH(buf->st_dev, buf->st_ino); |
| 3105 | struct inode_info *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3106 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3107 | /* |
| 3108 | * Look-up inode in hash table, if it already exists we have a |
| 3109 | * hard-link, so increment the nlink count and return it. |
| 3110 | * Don't do the look-up for directories because we don't hard-link |
| 3111 | * directories. |
| 3112 | */ |
| 3113 | if ((buf->st_mode & S_IFMT) != S_IFDIR) { |
| 3114 | for(inode = inode_info[ino_hash]; inode; inode = inode->next) { |
| 3115 | if(memcmp(buf, &inode->buf, sizeof(struct stat)) == 0) { |
| 3116 | inode->nlink ++; |
| 3117 | return inode; |
| 3118 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3119 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 3122 | inode = malloc(sizeof(struct inode_info) + bytes); |
plougher | 288f685 | 2010-12-16 05:22:55 +0000 | [diff] [blame] | 3123 | if(inode == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3124 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3125 | |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 3126 | if(bytes) |
| 3127 | memcpy(&inode->symlink, symlink, bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3128 | memcpy(&inode->buf, buf, sizeof(struct stat)); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 3129 | inode->read = FALSE; |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3130 | inode->root_entry = FALSE; |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3131 | inode->pseudo_file = pseudo; |
| 3132 | inode->pseudo_id = id; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3133 | inode->inode = SQUASHFS_INVALID_BLK; |
| 3134 | inode->nlink = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3135 | inode->inode_number = 0; |
plougher | dc86c3c | 2007-12-05 02:15:10 +0000 | [diff] [blame] | 3136 | |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 3137 | /* |
| 3138 | * Copy filesystem wide defaults into inode, these filesystem |
| 3139 | * wide defaults may be altered on an individual inode basis by |
| 3140 | * user specified actions |
| 3141 | * |
| 3142 | */ |
| 3143 | inode->no_fragments = no_fragments; |
| 3144 | inode->always_use_fragments = always_use_fragments; |
Mohan Srinivasan | 58e18d7 | 2016-07-26 15:06:29 -0700 | [diff] [blame] | 3145 | |
| 3146 | /* ANDROID CHANGES START*/ |
| 3147 | #ifdef ANDROID |
| 3148 | /* Check the whitelist */ |
| 3149 | inode->noD = whitelisted(buf); |
| 3150 | #else |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 3151 | inode->noD = noD; |
Mohan Srinivasan | 58e18d7 | 2016-07-26 15:06:29 -0700 | [diff] [blame] | 3152 | #endif |
| 3153 | /* ANDROID CHANGES END */ |
| 3154 | |
Phillip Lougher | 63f531f | 2011-09-10 04:03:32 +0100 | [diff] [blame] | 3155 | inode->noF = noF; |
Phillip Lougher | 9b6e341 | 2011-09-05 02:58:41 +0100 | [diff] [blame] | 3156 | |
Phillip Lougher | d657780 | 2012-10-04 19:14:33 +0100 | [diff] [blame] | 3157 | inode->next = inode_info[ino_hash]; |
| 3158 | inode_info[ino_hash] = inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3159 | |
| 3160 | return inode; |
| 3161 | } |
| 3162 | |
| 3163 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 3164 | static inline struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id) |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 3165 | { |
| 3166 | return lookup_inode3(buf, pseudo, id, NULL, 0); |
| 3167 | } |
| 3168 | |
| 3169 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 3170 | static inline struct inode_info *lookup_inode(struct stat *buf) |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3171 | { |
| 3172 | return lookup_inode2(buf, 0, 0); |
| 3173 | } |
| 3174 | |
| 3175 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 3176 | static inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this) |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3177 | { |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3178 | if (inode->inode_number == 0) { |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3179 | inode->inode_number = use_this ? : inode_no ++; |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3180 | if((inode->buf.st_mode & S_IFMT) == S_IFREG) |
| 3181 | progress_bar_size((inode->buf.st_size + block_size - 1) |
| 3182 | >> block_log); |
| 3183 | } |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 3187 | static inline struct dir_ent *create_dir_entry(char *name, char *source_name, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3188 | char *nonstandard_pathname, struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3189 | { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3190 | struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent)); |
| 3191 | if(dir_ent == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3192 | MEM_ERROR(); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3193 | |
| 3194 | dir_ent->name = name; |
| 3195 | dir_ent->source_name = source_name; |
| 3196 | dir_ent->nonstandard_pathname = nonstandard_pathname; |
| 3197 | dir_ent->our_dir = dir; |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3198 | dir_ent->inode = NULL; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3199 | dir_ent->next = NULL; |
Mohamad Ayyash | 531e36a | 2016-03-09 16:10:54 -0800 | [diff] [blame] | 3200 | /* ANDROID CHANGES START*/ |
| 3201 | #ifdef ANDROID |
| 3202 | dir_ent->capabilities = 0; |
| 3203 | #endif |
| 3204 | /* ANDROID CHANGES END */ |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3205 | |
| 3206 | return dir_ent; |
| 3207 | } |
| 3208 | |
| 3209 | |
Mohamad Ayyash | 9330263 | 2016-03-31 18:12:57 -0700 | [diff] [blame] | 3210 | static inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3211 | struct inode_info *inode_info) |
| 3212 | { |
| 3213 | struct dir_info *dir = dir_ent->our_dir; |
| 3214 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3215 | if(sub_dir) |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3216 | sub_dir->dir_ent = dir_ent; |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 3217 | |
| 3218 | /* ANDROID CHANGES START*/ |
| 3219 | #ifdef ANDROID |
Mohamad Ayyash | 18fe5f6 | 2015-03-02 16:00:56 -0800 | [diff] [blame] | 3220 | if (android_config) { |
| 3221 | if (mount_point) { |
| 3222 | char *mounted_path; |
Todd Poynor | 65ced84 | 2015-12-16 20:05:24 -0800 | [diff] [blame] | 3223 | char *rel_path; |
| 3224 | |
Mohamad Ayyash | 18fe5f6 | 2015-03-02 16:00:56 -0800 | [diff] [blame] | 3225 | alloc_mounted_path(mount_point, subpathname(dir_ent), &mounted_path); |
Todd Poynor | 65ced84 | 2015-12-16 20:05:24 -0800 | [diff] [blame] | 3226 | rel_path = mounted_path; |
| 3227 | while (rel_path && *rel_path == '/') |
| 3228 | rel_path++; |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 3229 | android_fs_config(fs_config_func, rel_path, &inode_info->buf, target_out_path, &dir_ent->capabilities); |
Mohamad Ayyash | 18fe5f6 | 2015-03-02 16:00:56 -0800 | [diff] [blame] | 3230 | free(mounted_path); |
| 3231 | } else { |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 3232 | android_fs_config(fs_config_func, pathname(dir_ent), &inode_info->buf, target_out_path, &dir_ent->capabilities); |
Mohamad Ayyash | 18fe5f6 | 2015-03-02 16:00:56 -0800 | [diff] [blame] | 3233 | } |
| 3234 | } |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 3235 | #endif |
| 3236 | /* ANDROID CHANGES END */ |
| 3237 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3238 | dir_ent->inode = inode_info; |
| 3239 | dir_ent->dir = sub_dir; |
| 3240 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3241 | dir_ent->next = dir->list; |
| 3242 | dir->list = dir_ent; |
| 3243 | dir->count++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 3246 | static inline void add_dir_entry2(char *name, char *source_name, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3247 | char *nonstandard_pathname, struct dir_info *sub_dir, |
| 3248 | struct inode_info *inode_info, struct dir_info *dir) |
| 3249 | { |
| 3250 | struct dir_ent *dir_ent = create_dir_entry(name, source_name, |
| 3251 | nonstandard_pathname, dir); |
| 3252 | |
| 3253 | |
| 3254 | add_dir_entry(dir_ent, sub_dir, inode_info); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 3258 | static inline void free_dir_entry(struct dir_ent *dir_ent) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3259 | { |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3260 | if(dir_ent->name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3261 | free(dir_ent->name); |
| 3262 | |
Phillip Lougher | eb92b19 | 2012-10-01 03:39:00 +0100 | [diff] [blame] | 3263 | if(dir_ent->source_name) |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3264 | free(dir_ent->source_name); |
| 3265 | |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3266 | if(dir_ent->nonstandard_pathname) |
| 3267 | free(dir_ent->nonstandard_pathname); |
| 3268 | |
| 3269 | /* if this entry has been associated with an inode, then we need |
| 3270 | * to update the inode nlink count. Orphaned inodes are harmless, and |
| 3271 | * is easier to leave them than go to the bother of deleting them */ |
| 3272 | if(dir_ent->inode && !dir_ent->inode->root_entry) |
| 3273 | dir_ent->inode->nlink --; |
| 3274 | |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3275 | free(dir_ent); |
| 3276 | } |
| 3277 | |
| 3278 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 3279 | static inline void add_excluded(struct dir_info *dir) |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3280 | { |
| 3281 | dir->excluded ++; |
| 3282 | } |
| 3283 | |
| 3284 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3285 | void dir_scan(squashfs_inode *inode, char *pathname, |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 3286 | struct dir_ent *(_readdir)(struct dir_info *), int progress) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3287 | { |
| 3288 | struct stat buf; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3289 | struct dir_ent *dir_ent; |
Mohamad Ayyash | 531e36a | 2016-03-09 16:10:54 -0800 | [diff] [blame] | 3290 | /* ANDROID CHANGES START*/ |
| 3291 | #ifdef ANDROID |
| 3292 | uint64_t caps = 0; |
| 3293 | #endif |
| 3294 | /* ANDROID CHANGES END */ |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3295 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3296 | root_dir = dir_scan1(pathname, "", paths, _readdir, 1); |
| 3297 | if(root_dir == NULL) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3298 | return; |
| 3299 | |
Phillip Lougher | 49bf860 | 2014-07-29 05:03:02 +0100 | [diff] [blame] | 3300 | /* Create root directory dir_ent and associated inode, and connect |
| 3301 | * it to the root directory dir_info structure */ |
| 3302 | dir_ent = create_dir_entry("", NULL, pathname, |
| 3303 | scan1_opendir("", "", 0)); |
| 3304 | |
| 3305 | if(pathname[0] == '\0') { |
| 3306 | /* |
| 3307 | * dummy top level directory, if multiple sources specified on |
| 3308 | * command line |
| 3309 | */ |
| 3310 | memset(&buf, 0, sizeof(buf)); |
| 3311 | buf.st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR; |
| 3312 | buf.st_uid = getuid(); |
| 3313 | buf.st_gid = getgid(); |
| 3314 | buf.st_mtime = time(NULL); |
| 3315 | buf.st_dev = 0; |
| 3316 | buf.st_ino = 0; |
| 3317 | dir_ent->inode = lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0); |
| 3318 | } else { |
| 3319 | if(lstat(pathname, &buf) == -1) |
| 3320 | /* source directory has disappeared? */ |
| 3321 | BAD_ERROR("Cannot stat source directory %s because %s\n", |
| 3322 | pathname, strerror(errno)); |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 3323 | /* ANDROID CHANGES START*/ |
| 3324 | #ifdef ANDROID |
Luis Hector Chavez | 1cdde05 | 2018-02-07 16:12:09 +0000 | [diff] [blame] | 3325 | buf.st_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_IFDIR; // root mode |
Mohamad Ayyash | 6595c1f | 2016-05-25 01:34:17 -0700 | [diff] [blame] | 3326 | buf.st_uid = 0; |
| 3327 | buf.st_gid = 0; |
| 3328 | buf.st_mtime = time(NULL); |
| 3329 | buf.st_dev = 0; |
| 3330 | buf.st_ino = 0; |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 3331 | #endif |
| 3332 | /* ANDROID CHANGES END */ |
Phillip Lougher | 49bf860 | 2014-07-29 05:03:02 +0100 | [diff] [blame] | 3333 | dir_ent->inode = lookup_inode(&buf); |
| 3334 | } |
| 3335 | |
Mohamad Ayyash | 531e36a | 2016-03-09 16:10:54 -0800 | [diff] [blame] | 3336 | /* ANDROID CHANGES START*/ |
| 3337 | #ifdef ANDROID |
| 3338 | dir_ent->capabilities = caps; |
Luis Hector Chavez | 1cdde05 | 2018-02-07 16:12:09 +0000 | [diff] [blame] | 3339 | if (android_config) { |
| 3340 | android_fs_config(fs_config_func, "", &dir_ent->inode->buf, target_out_path, &dir_ent->capabilities); |
| 3341 | } |
Mohamad Ayyash | 531e36a | 2016-03-09 16:10:54 -0800 | [diff] [blame] | 3342 | #endif |
| 3343 | /* ANDROID CHANGES END */ |
| 3344 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3345 | dir_ent->dir = root_dir; |
| 3346 | root_dir->dir_ent = dir_ent; |
Phillip Lougher | 49bf860 | 2014-07-29 05:03:02 +0100 | [diff] [blame] | 3347 | |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3348 | /* |
| 3349 | * Process most actions and any pseudo files |
| 3350 | */ |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 3351 | if(actions() || get_pseudo()) |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3352 | dir_scan2(root_dir, get_pseudo()); |
Phillip Lougher | f3d0f9c | 2012-11-14 05:36:58 +0000 | [diff] [blame] | 3353 | |
Phillip Lougher | e92cb51 | 2012-11-15 02:22:28 +0000 | [diff] [blame] | 3354 | /* |
| 3355 | * Process move actions |
| 3356 | */ |
| 3357 | if(move_actions()) { |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3358 | dir_scan3(root_dir); |
Phillip Lougher | e92cb51 | 2012-11-15 02:22:28 +0000 | [diff] [blame] | 3359 | do_move_actions(); |
| 3360 | } |
| 3361 | |
Phillip Lougher | 71ae14e | 2012-11-15 02:27:50 +0000 | [diff] [blame] | 3362 | /* |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3363 | * Process prune actions |
| 3364 | */ |
| 3365 | if(prune_actions()) |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3366 | dir_scan4(root_dir); |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3367 | |
| 3368 | /* |
Phillip Lougher | 71ae14e | 2012-11-15 02:27:50 +0000 | [diff] [blame] | 3369 | * Process empty actions |
| 3370 | */ |
| 3371 | if(empty_actions()) |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3372 | dir_scan5(root_dir); |
Phillip Lougher | 71ae14e | 2012-11-15 02:27:50 +0000 | [diff] [blame] | 3373 | |
Phillip Lougher | eb69dad | 2012-11-15 03:34:02 +0000 | [diff] [blame] | 3374 | /* |
| 3375 | * Sort directories and compute the inode numbers |
| 3376 | */ |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3377 | dir_scan6(root_dir); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3378 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3379 | alloc_inode_no(dir_ent->inode, root_inode_number); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3380 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3381 | eval_actions(root_dir, dir_ent); |
Andy Lutomirski | 3f31dcf | 2012-09-21 18:08:08 -0700 | [diff] [blame] | 3382 | |
Phillip Lougher | 19b877d | 2013-02-23 11:15:38 +0000 | [diff] [blame] | 3383 | if(sorted) |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3384 | generate_file_priorities(root_dir, 0, |
| 3385 | &root_dir->dir_ent->inode->buf); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 3386 | |
| 3387 | if(appending) { |
| 3388 | sigset_t sigmask; |
| 3389 | |
| 3390 | restore_thread = init_restore_thread(); |
| 3391 | sigemptyset(&sigmask); |
| 3392 | sigaddset(&sigmask, SIGINT); |
| 3393 | sigaddset(&sigmask, SIGTERM); |
| 3394 | sigaddset(&sigmask, SIGUSR1); |
| 3395 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
| 3396 | BAD_ERROR("Failed to set signal mask\n"); |
| 3397 | write_destination(fd, SQUASHFS_START, 4, "\0\0\0\0"); |
| 3398 | } |
| 3399 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3400 | queue_put(to_reader, root_dir); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 3401 | |
Phillip Lougher | 9963944 | 2014-09-12 23:28:59 +0100 | [diff] [blame] | 3402 | set_progressbar_state(progress); |
| 3403 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3404 | if(sorted) |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3405 | sort_files_and_write(root_dir); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 3406 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3407 | dir_scan7(inode, root_dir); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3408 | dir_ent->inode->inode = *inode; |
| 3409 | dir_ent->inode->type = SQUASHFS_DIR_TYPE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3413 | /* |
| 3414 | * dir_scan1 routines... |
Phillip Lougher | e8630be | 2012-11-11 02:24:24 +0000 | [diff] [blame] | 3415 | * These scan the source directories into memory for processing. |
| 3416 | * Exclude actions are processed here (in contrast to the other actions) |
| 3417 | * because they affect what is scanned. |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3418 | */ |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3419 | struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3420 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3421 | struct dir_info *dir; |
| 3422 | |
plougher | 6da792b | 2010-12-16 05:25:39 +0000 | [diff] [blame] | 3423 | dir = malloc(sizeof(struct dir_info)); |
| 3424 | if(dir == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 3425 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3426 | |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3427 | if(pathname[0] != '\0') { |
| 3428 | dir->linuxdir = opendir(pathname); |
| 3429 | if(dir->linuxdir == NULL) { |
| 3430 | free(dir); |
| 3431 | return NULL; |
| 3432 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3433 | } |
Phillip Lougher | 04ef244 | 2013-01-09 06:03:50 +0000 | [diff] [blame] | 3434 | |
Phillip Lougher | 3cb5d6c | 2013-01-09 05:50:29 +0000 | [diff] [blame] | 3435 | dir->pathname = strdup(pathname); |
| 3436 | dir->subpath = strdup(subpath); |
Phillip Lougher | ded70fa | 2011-12-25 02:14:58 +0000 | [diff] [blame] | 3437 | dir->count = 0; |
| 3438 | dir->directory_count = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3439 | dir->dir_is_ldir = TRUE; |
| 3440 | dir->list = NULL; |
Phillip Lougher | 0b5a124 | 2011-12-25 03:22:42 +0000 | [diff] [blame] | 3441 | dir->depth = depth; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3442 | dir->excluded = 0; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3443 | |
| 3444 | return dir; |
| 3445 | } |
| 3446 | |
| 3447 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3448 | struct dir_ent *scan1_encomp_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3449 | { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3450 | static int index = 0; |
| 3451 | |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3452 | if(dir->count < old_root_entries) { |
| 3453 | int i; |
| 3454 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3455 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3456 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3457 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3458 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3459 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3460 | } |
plougher | 11266ba | 2010-12-16 05:34:30 +0000 | [diff] [blame] | 3461 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3462 | |
| 3463 | while(index < source) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3464 | char *basename = NULL; |
| 3465 | char *dir_name = getbase(source_path[index]); |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3466 | int pass = 1, res; |
plougher | 89fe2c7 | 2010-12-16 05:31:34 +0000 | [diff] [blame] | 3467 | |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3468 | if(dir_name == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3469 | ERROR_START("Bad source directory %s", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3470 | source_path[index]); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3471 | ERROR_EXIT(" - skipping ...\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3472 | index ++; |
| 3473 | continue; |
| 3474 | } |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3475 | dir_name = strdup(dir_name); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3476 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3477 | struct dir_ent *dir_ent = dir->list; |
| 3478 | |
| 3479 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3480 | dir_ent = dir_ent->next); |
| 3481 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3482 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3483 | ERROR("Source directory entry %s already used! - trying" |
| 3484 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3485 | if(pass == 1) |
| 3486 | basename = dir_name; |
| 3487 | else |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3488 | free(dir_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3489 | res = asprintf(&dir_name, "%s_%d", basename, pass++); |
| 3490 | if(res == -1) |
| 3491 | BAD_ERROR("asprintf failed in " |
| 3492 | "scan1_encomp_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3493 | ERROR("%s\n", dir_name); |
| 3494 | } |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3495 | return create_dir_entry(dir_name, basename, |
Phillip Lougher | 4be6d45 | 2014-09-03 02:20:55 +0100 | [diff] [blame] | 3496 | strdup(source_path[index ++]), dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3497 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3498 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3502 | struct dir_ent *scan1_single_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3503 | { |
| 3504 | struct dirent *d_name; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3505 | int i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3506 | |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3507 | if(dir->count < old_root_entries) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3508 | for(i = 0; i < old_root_entries; i++) { |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3509 | if(old_root_entry[i].inode.type == SQUASHFS_DIR_TYPE) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3510 | dir->directory_count ++; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3511 | add_dir_entry2(old_root_entry[i].name, NULL, NULL, NULL, |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3512 | &old_root_entry[i].inode, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3513 | } |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3514 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3515 | |
| 3516 | if((d_name = readdir(dir->linuxdir)) != NULL) { |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3517 | char *basename = NULL; |
| 3518 | char *dir_name = strdup(d_name->d_name); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3519 | int pass = 1, res; |
plougher | 4925e17 | 2010-12-16 05:45:54 +0000 | [diff] [blame] | 3520 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3521 | for(;;) { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3522 | struct dir_ent *dir_ent = dir->list; |
| 3523 | |
| 3524 | for(; dir_ent && strcmp(dir_ent->name, dir_name) != 0; |
| 3525 | dir_ent = dir_ent->next); |
| 3526 | if(dir_ent == NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3527 | break; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 3528 | ERROR("Source directory entry %s already used! - trying" |
| 3529 | " ", dir_name); |
Phillip Lougher | c73ed32 | 2012-10-01 03:25:41 +0100 | [diff] [blame] | 3530 | if (pass == 1) |
| 3531 | basename = dir_name; |
| 3532 | else |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3533 | free(dir_name); |
| 3534 | res = asprintf(&dir_name, "%s_%d", d_name->d_name, pass++); |
| 3535 | if(res == -1) |
| 3536 | BAD_ERROR("asprintf failed in " |
| 3537 | "scan1_single_readdir\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3538 | ERROR("%s\n", dir_name); |
| 3539 | } |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3540 | return create_dir_entry(dir_name, basename, NULL, dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3543 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3547 | struct dir_ent *scan1_readdir(struct dir_info *dir) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3548 | { |
plougher | 480d9bf | 2010-12-18 02:28:39 +0000 | [diff] [blame] | 3549 | struct dirent *d_name = readdir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3550 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3551 | return d_name ? |
| 3552 | create_dir_entry(strdup(d_name->d_name), NULL, NULL, dir) : |
| 3553 | NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3557 | void scan1_freedir(struct dir_info *dir) |
| 3558 | { |
plougher | fbed12b | 2006-02-07 12:45:53 +0000 | [diff] [blame] | 3559 | if(dir->pathname[0] != '\0') |
| 3560 | closedir(dir->linuxdir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3564 | struct dir_info *dir_scan1(char *filename, char *subpath, |
| 3565 | struct pathnames *paths, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3566 | struct dir_ent *(_readdir)(struct dir_info *), int depth) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3567 | { |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3568 | struct dir_info *dir = scan1_opendir(filename, subpath, depth); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3569 | struct dir_ent *dir_ent; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3570 | |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3571 | if(dir == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3572 | ERROR_START("Could not open %s", filename); |
| 3573 | ERROR_EXIT(", skipping...\n"); |
Phillip Lougher | 87db567 | 2013-01-09 05:40:46 +0000 | [diff] [blame] | 3574 | return NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3577 | while((dir_ent = _readdir(dir))) { |
plougher | 360b6e4 | 2010-12-18 02:40:28 +0000 | [diff] [blame] | 3578 | struct dir_info *sub_dir; |
| 3579 | struct stat buf; |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 3580 | struct pathnames *new = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3581 | char *filename = pathname(dir_ent); |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3582 | char *subpath = NULL; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3583 | char *dir_name = dir_ent->name; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3584 | |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3585 | if(strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0) { |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3586 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3587 | continue; |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3588 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3589 | |
| 3590 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3591 | ERROR_START("Cannot stat dir/file %s because %s", |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 3592 | filename, strerror(errno)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3593 | ERROR_EXIT(", ignoring\n"); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3594 | free_dir_entry(dir_ent); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3595 | continue; |
| 3596 | } |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3597 | |
| 3598 | if((buf.st_mode & S_IFMT) != S_IFREG && |
Phillip Lougher | e4ff36a | 2012-11-19 01:34:47 +0000 | [diff] [blame] | 3599 | (buf.st_mode & S_IFMT) != S_IFDIR && |
| 3600 | (buf.st_mode & S_IFMT) != S_IFLNK && |
| 3601 | (buf.st_mode & S_IFMT) != S_IFCHR && |
| 3602 | (buf.st_mode & S_IFMT) != S_IFBLK && |
| 3603 | (buf.st_mode & S_IFMT) != S_IFIFO && |
| 3604 | (buf.st_mode & S_IFMT) != S_IFSOCK) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3605 | ERROR_START("File %s has unrecognised filetype %d", |
| 3606 | filename, buf.st_mode & S_IFMT); |
| 3607 | ERROR_EXIT(", ignoring\n"); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3608 | free_dir_entry(dir_ent); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 3609 | continue; |
| 3610 | } |
| 3611 | |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3612 | if((old_exclude && old_excluded(filename, &buf)) || |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3613 | (!old_exclude && excluded(dir_name, paths, &new))) { |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3614 | add_excluded(dir); |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3615 | free_dir_entry(dir_ent); |
Phillip Lougher | 10d8de0 | 2011-08-29 00:14:48 +0100 | [diff] [blame] | 3616 | continue; |
Phillip Lougher | ad0f921 | 2011-12-31 00:55:51 +0000 | [diff] [blame] | 3617 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3618 | |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3619 | if(exclude_actions()) { |
| 3620 | subpath = subpathname(dir_ent); |
| 3621 | |
| 3622 | if(eval_exclude_actions(dir_name, filename, subpath, |
Phillip Lougher | aa83ecb | 2014-07-12 02:47:15 +0100 | [diff] [blame] | 3623 | &buf, depth, dir_ent)) { |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3624 | add_excluded(dir); |
| 3625 | free_dir_entry(dir_ent); |
| 3626 | continue; |
| 3627 | } |
| 3628 | } |
| 3629 | |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 3630 | switch(buf.st_mode & S_IFMT) { |
| 3631 | case S_IFDIR: |
Phillip Lougher | 651d68d | 2013-01-13 05:38:13 +0000 | [diff] [blame] | 3632 | if(subpath == NULL) |
| 3633 | subpath = subpathname(dir_ent); |
| 3634 | |
Phillip Lougher | b38c172 | 2012-02-09 23:26:19 +0000 | [diff] [blame] | 3635 | sub_dir = dir_scan1(filename, subpath, new, |
| 3636 | scan1_readdir, depth + 1); |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 3637 | if(sub_dir) { |
| 3638 | dir->directory_count ++; |
| 3639 | add_dir_entry(dir_ent, sub_dir, |
| 3640 | lookup_inode(&buf)); |
| 3641 | } else |
Phillip Lougher | 10a4b57 | 2012-02-18 23:26:10 +0000 | [diff] [blame] | 3642 | free_dir_entry(dir_ent); |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 3643 | break; |
| 3644 | case S_IFLNK: { |
| 3645 | int byte; |
| 3646 | static char buff[65536]; /* overflow safe */ |
| 3647 | |
| 3648 | byte = readlink(filename, buff, 65536); |
| 3649 | if(byte == -1) { |
| 3650 | ERROR_START("Failed to read symlink %s", |
| 3651 | filename); |
| 3652 | ERROR_EXIT(", ignoring\n"); |
| 3653 | } else if(byte == 65536) { |
| 3654 | ERROR_START("Symlink %s is greater than 65536 " |
| 3655 | "bytes!", filename); |
| 3656 | ERROR_EXIT(", ignoring\n"); |
| 3657 | } else { |
| 3658 | /* readlink doesn't 0 terminate the returned |
| 3659 | * path */ |
| 3660 | buff[byte] = '\0'; |
| 3661 | add_dir_entry(dir_ent, NULL, lookup_inode3(&buf, |
| 3662 | 0, 0, buff, byte + 1)); |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3663 | } |
Phillip Lougher | e19ce45 | 2014-07-27 04:16:54 +0100 | [diff] [blame] | 3664 | break; |
| 3665 | } |
| 3666 | default: |
| 3667 | add_dir_entry(dir_ent, NULL, lookup_inode(&buf)); |
| 3668 | } |
Phillip Lougher | 6a78a2d | 2011-12-28 03:54:19 +0000 | [diff] [blame] | 3669 | |
Phillip Lougher | 205c1e0 | 2013-01-07 01:37:56 +0000 | [diff] [blame] | 3670 | free(new); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | scan1_freedir(dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3674 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 3675 | return dir; |
| 3676 | } |
| 3677 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 3678 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3679 | /* |
| 3680 | * dir_scan2 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3681 | * This processes most actions and any pseudo files |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3682 | */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3683 | 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] | 3684 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3685 | if (dir_ent == NULL) |
| 3686 | dir_ent = dir->list; |
| 3687 | else |
| 3688 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3689 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3690 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next); |
| 3691 | |
| 3692 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | |
| 3696 | struct dir_ent *scan2_lookup(struct dir_info *dir, char *name) |
| 3697 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3698 | struct dir_ent *dir_ent = dir->list; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3699 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3700 | for(; dir_ent && strcmp(dir_ent->name, name) != 0; |
| 3701 | dir_ent = dir_ent->next); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3702 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3703 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3707 | void dir_scan2(struct dir_info *dir, struct pseudo *pseudo) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3708 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3709 | struct dir_ent *dir_ent = NULL; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3710 | struct pseudo_entry *pseudo_ent; |
| 3711 | struct stat buf; |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 3712 | static int pseudo_ino = 1; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3713 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3714 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3715 | struct inode_info *inode_info = dir_ent->inode; |
| 3716 | struct stat *buf = &inode_info->buf; |
| 3717 | char *name = dir_ent->name; |
| 3718 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3719 | eval_actions(root_dir, dir_ent); |
Phillip Lougher | 89d757c | 2011-09-20 00:33:19 +0100 | [diff] [blame] | 3720 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3721 | if((buf->st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3722 | dir_scan2(dir_ent->dir, pseudo_subdir(name, pseudo)); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | while((pseudo_ent = pseudo_readdir(pseudo)) != NULL) { |
| 3726 | dir_ent = scan2_lookup(dir, pseudo_ent->name); |
plougher | dcd66c5 | 2010-09-17 03:44:17 +0000 | [diff] [blame] | 3727 | if(pseudo_ent->dev->type == 'm') { |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3728 | struct stat *buf; |
| 3729 | if(dir_ent == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3730 | ERROR_START("Pseudo modify file \"%s\" does " |
| 3731 | "not exist in source filesystem.", |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3732 | pseudo_ent->pathname); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3733 | ERROR_EXIT(" Ignoring.\n"); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3734 | continue; |
| 3735 | } |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 3736 | if(dir_ent->inode->root_entry) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3737 | ERROR_START("Pseudo modify file \"%s\" is a " |
| 3738 | "pre-existing file in the filesystem " |
| 3739 | "being appended to. It cannot be "\ |
| 3740 | "modified.", pseudo_ent->pathname); |
| 3741 | ERROR_EXIT(" Ignoring.\n"); |
plougher | b34f9f6 | 2009-04-26 02:08:42 +0000 | [diff] [blame] | 3742 | continue; |
| 3743 | } |
| 3744 | buf = &dir_ent->inode->buf; |
| 3745 | buf->st_mode = (buf->st_mode & S_IFMT) | |
| 3746 | pseudo_ent->dev->mode; |
| 3747 | buf->st_uid = pseudo_ent->dev->uid; |
| 3748 | buf->st_gid = pseudo_ent->dev->gid; |
| 3749 | continue; |
| 3750 | } |
| 3751 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3752 | if(dir_ent) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3753 | if(dir_ent->inode->root_entry) { |
| 3754 | ERROR_START("Pseudo file \"%s\" is a " |
| 3755 | "pre-existing file in the filesystem " |
| 3756 | "being appended to.", |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3757 | pseudo_ent->pathname); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3758 | ERROR_EXIT(" Ignoring.\n"); |
| 3759 | } else { |
| 3760 | ERROR_START("Pseudo file \"%s\" exists in " |
| 3761 | "source filesystem \"%s\".", |
plougher | f0dc238 | 2010-05-01 23:55:06 +0000 | [diff] [blame] | 3762 | pseudo_ent->pathname, |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3763 | pathname(dir_ent)); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3764 | ERROR_EXIT("\nIgnoring, exclude it (-e/-ef) to " |
| 3765 | "override.\n"); |
| 3766 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3767 | continue; |
| 3768 | } |
| 3769 | |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3770 | memset(&buf, 0, sizeof(buf)); |
| 3771 | buf.st_mode = pseudo_ent->dev->mode; |
| 3772 | buf.st_uid = pseudo_ent->dev->uid; |
| 3773 | buf.st_gid = pseudo_ent->dev->gid; |
| 3774 | buf.st_rdev = makedev(pseudo_ent->dev->major, |
| 3775 | pseudo_ent->dev->minor); |
plougher | 7e58f4d | 2009-04-05 12:06:19 +0000 | [diff] [blame] | 3776 | buf.st_mtime = time(NULL); |
plougher | 1a3fbf2 | 2009-04-05 12:04:16 +0000 | [diff] [blame] | 3777 | buf.st_ino = pseudo_ino ++; |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3778 | |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3779 | if(pseudo_ent->dev->type == 'd') { |
| 3780 | struct dir_ent *dir_ent = |
| 3781 | create_dir_entry(pseudo_ent->name, NULL, |
| 3782 | pseudo_ent->pathname, dir); |
| 3783 | char *subpath = strdup(subpathname(dir_ent)); |
| 3784 | struct dir_info *sub_dir = scan1_opendir("", subpath, |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3785 | dir->depth + 1); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3786 | if(sub_dir == NULL) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 3787 | ERROR_START("Could not create pseudo directory " |
| 3788 | "\"%s\"", pseudo_ent->pathname); |
| 3789 | ERROR_EXIT(", skipping...\n"); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3790 | free(subpath); |
| 3791 | pseudo_ino --; |
| 3792 | continue; |
| 3793 | } |
Phillip Lougher | 24eeb77 | 2012-10-13 01:56:24 +0100 | [diff] [blame] | 3794 | dir_scan2(sub_dir, pseudo_ent->pseudo); |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3795 | dir->directory_count ++; |
| 3796 | add_dir_entry(dir_ent, sub_dir, |
| 3797 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0)); |
| 3798 | } else if(pseudo_ent->dev->type == 'f') { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3799 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3800 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3801 | lookup_inode2(&buf, PSEUDO_FILE_PROCESS, |
| 3802 | pseudo_ent->dev->pseudo_id), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3803 | } else { |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 3804 | add_dir_entry2(pseudo_ent->name, NULL, |
Phillip Lougher | 5d57929 | 2012-10-13 01:37:16 +0100 | [diff] [blame] | 3805 | pseudo_ent->pathname, NULL, |
Phillip Lougher | 81204c2 | 2012-07-25 03:30:30 +0100 | [diff] [blame] | 3806 | lookup_inode2(&buf, PSEUDO_FILE_OTHER, 0), dir); |
plougher | b85e9ad | 2010-05-02 01:46:12 +0000 | [diff] [blame] | 3807 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3808 | } |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 3812 | /* |
| 3813 | * dir_scan3 routines... |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3814 | * This processes the move action |
| 3815 | */ |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3816 | void dir_scan3(struct dir_info *dir) |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3817 | { |
| 3818 | struct dir_ent *dir_ent = NULL; |
| 3819 | |
| 3820 | while((dir_ent = scan2_readdir(dir, dir_ent)) != NULL) { |
| 3821 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3822 | eval_move_actions(root_dir, dir_ent); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3823 | |
| 3824 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3825 | dir_scan3(dir_ent->dir); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3826 | } |
| 3827 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3828 | |
| 3829 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 3830 | /* |
| 3831 | * dir_scan4 routines... |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3832 | * This processes the prune action. This action is designed to do fine |
| 3833 | * grained tuning of the in-core directory structure after the exclude, |
| 3834 | * move and pseudo actions have been performed. This allows complex |
| 3835 | * tests to be performed which are impossible at exclude time (i.e. |
| 3836 | * tests which rely on the in-core directory structure) |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3837 | */ |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3838 | void free_dir(struct dir_info *dir) |
| 3839 | { |
| 3840 | struct dir_ent *dir_ent = dir->list; |
| 3841 | |
| 3842 | while(dir_ent) { |
| 3843 | struct dir_ent *tmp = dir_ent; |
| 3844 | |
| 3845 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
| 3846 | free_dir(dir_ent->dir); |
| 3847 | |
| 3848 | dir_ent = dir_ent->next; |
| 3849 | free_dir_entry(tmp); |
| 3850 | } |
| 3851 | |
| 3852 | free(dir->pathname); |
| 3853 | free(dir->subpath); |
| 3854 | free(dir); |
| 3855 | } |
| 3856 | |
| 3857 | |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3858 | void dir_scan4(struct dir_info *dir) |
| 3859 | { |
| 3860 | struct dir_ent *dir_ent = dir->list, *prev = NULL; |
| 3861 | |
| 3862 | while(dir_ent) { |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3863 | if(dir_ent->inode->root_entry) { |
| 3864 | prev = dir_ent; |
| 3865 | dir_ent = dir_ent->next; |
| 3866 | continue; |
| 3867 | } |
| 3868 | |
| 3869 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3870 | dir_scan4(dir_ent->dir); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3871 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3872 | if(eval_prune_actions(root_dir, dir_ent)) { |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3873 | struct dir_ent *tmp = dir_ent; |
| 3874 | |
| 3875 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) { |
| 3876 | free_dir(dir_ent->dir); |
| 3877 | dir->directory_count --; |
| 3878 | } |
| 3879 | |
| 3880 | dir->count --; |
| 3881 | |
| 3882 | /* remove dir_ent from list */ |
| 3883 | dir_ent = dir_ent->next; |
| 3884 | if(prev) |
| 3885 | prev->next = dir_ent; |
| 3886 | else |
| 3887 | dir->list = dir_ent; |
| 3888 | |
| 3889 | /* free it */ |
| 3890 | free_dir_entry(tmp); |
| 3891 | |
| 3892 | add_excluded(dir); |
| 3893 | continue; |
| 3894 | } |
| 3895 | |
| 3896 | prev = dir_ent; |
| 3897 | dir_ent = dir_ent->next; |
| 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | |
| 3902 | /* |
| 3903 | * dir_scan5 routines... |
| 3904 | * This processes the empty action. This action has to be processed after |
| 3905 | * all other actions because the previous exclude and move actions and the |
| 3906 | * pseudo actions affect whether a directory is empty |
| 3907 | */ |
| 3908 | void dir_scan5(struct dir_info *dir) |
| 3909 | { |
| 3910 | struct dir_ent *dir_ent = dir->list, *prev = NULL; |
| 3911 | |
| 3912 | while(dir_ent) { |
Phillip Lougher | 97b7c4b | 2014-08-04 04:43:15 +0100 | [diff] [blame] | 3913 | if(dir_ent->inode->root_entry) { |
| 3914 | prev = dir_ent; |
| 3915 | dir_ent = dir_ent->next; |
| 3916 | continue; |
| 3917 | } |
| 3918 | |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3919 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) { |
| 3920 | dir_scan5(dir_ent->dir); |
| 3921 | |
Phillip Lougher | 22d67da | 2014-08-08 02:55:33 +0100 | [diff] [blame] | 3922 | if(eval_empty_actions(root_dir, dir_ent)) { |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3923 | struct dir_ent *tmp = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3924 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3925 | /* |
| 3926 | * delete sub-directory, this is by definition |
| 3927 | * empty |
| 3928 | */ |
| 3929 | free(dir_ent->dir->pathname); |
| 3930 | free(dir_ent->dir->subpath); |
| 3931 | free(dir_ent->dir); |
| 3932 | |
| 3933 | /* remove dir_ent from list */ |
| 3934 | dir_ent = dir_ent->next; |
| 3935 | if(prev) |
| 3936 | prev->next = dir_ent; |
| 3937 | else |
| 3938 | dir->list = dir_ent; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3939 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3940 | /* free it */ |
| 3941 | free_dir_entry(tmp); |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3942 | |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3943 | /* update counts */ |
| 3944 | dir->directory_count --; |
| 3945 | dir->count --; |
| 3946 | add_excluded(dir); |
| 3947 | continue; |
| 3948 | } |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3949 | } |
Phillip Lougher | 15e8f04 | 2012-11-16 00:57:39 +0000 | [diff] [blame] | 3950 | |
| 3951 | prev = dir_ent; |
| 3952 | dir_ent = dir_ent->next; |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 3953 | } |
| 3954 | } |
| 3955 | |
| 3956 | |
| 3957 | /* |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 3958 | * dir_scan6 routines... |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3959 | * This sorts every directory and computes the inode numbers |
| 3960 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3961 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3962 | /* |
| 3963 | * Bottom up linked list merge sort. |
| 3964 | * |
| 3965 | * Qsort and other O(n log n) algorithms work well with arrays but not |
| 3966 | * linked lists. Merge sort another O(n log n) sort algorithm on the other hand |
| 3967 | * is not ideal for arrays (as it needs an additonal n storage locations |
| 3968 | * as sorting is not done in place), but it is ideal for linked lists because |
| 3969 | * it doesn't require any extra storage, |
| 3970 | */ |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3971 | void sort_directory(struct dir_info *dir) |
| 3972 | { |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3973 | struct dir_ent *cur, *l1, *l2, *next; |
| 3974 | int len1, len2, stride = 1; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 3975 | |
Phillip Lougher | d3459a8 | 2014-04-18 01:37:54 +0100 | [diff] [blame] | 3976 | if(dir->list == NULL || dir->count < 2) |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3977 | return; |
| 3978 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3979 | /* |
| 3980 | * We can consider our linked-list to be made up of stride length |
| 3981 | * sublists. Eacn iteration around this loop merges adjacent |
| 3982 | * stride length sublists into larger 2*stride sublists. We stop |
| 3983 | * when stride becomes equal to the entire list. |
| 3984 | * |
| 3985 | * Initially stride = 1 (by definition a sublist of 1 is sorted), and |
| 3986 | * these 1 element sublists are merged into 2 element sublists, which |
| 3987 | * are then merged into 4 element sublists and so on. |
| 3988 | */ |
| 3989 | do { |
| 3990 | l2 = dir->list; /* head of current linked list */ |
| 3991 | cur = NULL; /* empty output list */ |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 3992 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 3993 | /* |
| 3994 | * Iterate through the linked list, merging adjacent sublists. |
| 3995 | * On each interation l2 points to the next sublist pair to be |
| 3996 | * merged (if there's only one sublist left this is simply added |
| 3997 | * to the output list) |
| 3998 | */ |
| 3999 | while(l2) { |
| 4000 | l1 = l2; |
| 4001 | for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next); |
| 4002 | len2 = stride; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 4003 | |
Phillip Lougher | 242242e | 2012-08-24 04:15:36 +0100 | [diff] [blame] | 4004 | /* |
| 4005 | * l1 points to first sublist. |
| 4006 | * l2 points to second sublist. |
| 4007 | * Merge them onto the output list |
| 4008 | */ |
| 4009 | while(len1 && l2 && len2) { |
| 4010 | if(strcmp(l1->name, l2->name) <= 0) { |
| 4011 | next = l1; |
| 4012 | l1 = l1->next; |
| 4013 | len1 --; |
| 4014 | } else { |
| 4015 | next = l2; |
| 4016 | l2 = l2->next; |
| 4017 | len2 --; |
| 4018 | } |
| 4019 | |
| 4020 | if(cur) { |
| 4021 | cur->next = next; |
| 4022 | cur = next; |
| 4023 | } else |
| 4024 | dir->list = cur = next; |
| 4025 | } |
| 4026 | /* |
| 4027 | * One sublist is now empty, copy the other one onto the |
| 4028 | * output list |
| 4029 | */ |
| 4030 | for(; len1; len1 --, l1 = l1->next) { |
| 4031 | if(cur) { |
| 4032 | cur->next = l1; |
| 4033 | cur = l1; |
| 4034 | } else |
| 4035 | dir->list = cur = l1; |
| 4036 | } |
| 4037 | for(; l2 && len2; len2 --, l2 = l2->next) { |
| 4038 | if(cur) { |
| 4039 | cur->next = l2; |
| 4040 | cur = l2; |
| 4041 | } else |
| 4042 | dir->list = cur = l2; |
| 4043 | } |
| 4044 | } |
| 4045 | cur->next = NULL; |
| 4046 | stride = stride << 1; |
| 4047 | } while(stride < dir->count); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4051 | void dir_scan6(struct dir_info *dir) |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4052 | { |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 4053 | struct dir_ent *dir_ent; |
| 4054 | unsigned int byte_count = 0; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4055 | |
| 4056 | sort_directory(dir); |
| 4057 | |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 4058 | for(dir_ent = dir->list; dir_ent; dir_ent = dir_ent->next) { |
| 4059 | byte_count += strlen(dir_ent->name) + |
| 4060 | sizeof(struct squashfs_dir_entry); |
| 4061 | |
| 4062 | if(dir_ent->inode->root_entry) |
| 4063 | continue; |
| 4064 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4065 | alloc_inode_no(dir_ent->inode, 0); |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 4066 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4067 | if((dir_ent->inode->buf.st_mode & S_IFMT) == S_IFDIR) |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4068 | dir_scan6(dir_ent->dir); |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4069 | } |
Phillip Lougher | 23d8362 | 2012-10-14 02:35:22 +0100 | [diff] [blame] | 4070 | |
| 4071 | if((dir->count < 257 && byte_count < SQUASHFS_METADATA_SIZE)) |
| 4072 | dir->dir_is_ldir = FALSE; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | |
| 4076 | /* |
Phillip Lougher | 2abfcf7 | 2012-11-11 03:59:56 +0000 | [diff] [blame] | 4077 | * dir_scan6 routines... |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4078 | * This generates the filesystem metadata and writes it out to the destination |
| 4079 | */ |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4080 | void scan7_init_dir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4081 | { |
| 4082 | dir->buff = malloc(SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4083 | if(dir->buff == NULL) |
| 4084 | MEM_ERROR(); |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4085 | |
| 4086 | dir->size = SQUASHFS_METADATA_SIZE; |
| 4087 | dir->p = dir->index_count_p = dir->buff; |
| 4088 | dir->entry_count = 256; |
| 4089 | dir->entry_count_p = NULL; |
| 4090 | dir->index = NULL; |
| 4091 | dir->i_count = dir->i_size = 0; |
| 4092 | } |
| 4093 | |
| 4094 | |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4095 | struct dir_ent *scan7_readdir(struct directory *dir, struct dir_info *dir_info, |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 4096 | struct dir_ent *dir_ent) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4097 | { |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 4098 | if (dir_ent == NULL) |
| 4099 | dir_ent = dir_info->list; |
| 4100 | else |
| 4101 | dir_ent = dir_ent->next; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4102 | |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 4103 | for(; dir_ent && dir_ent->inode->root_entry; dir_ent = dir_ent->next) |
| 4104 | add_dir(dir_ent->inode->inode, dir_ent->inode->inode_number, |
| 4105 | dir_ent->name, dir_ent->inode->type, dir); |
| 4106 | |
| 4107 | return dir_ent; |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4111 | void scan7_freedir(struct directory *dir) |
Phillip Lougher | abc3b49 | 2012-07-29 02:53:35 +0100 | [diff] [blame] | 4112 | { |
| 4113 | if(dir->index) |
| 4114 | free(dir->index); |
| 4115 | free(dir->buff); |
| 4116 | } |
| 4117 | |
| 4118 | |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4119 | void dir_scan7(squashfs_inode *inode, struct dir_info *dir_info) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4120 | { |
| 4121 | int squashfs_type; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4122 | int duplicate_file; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4123 | struct directory dir; |
Phillip Lougher | bf33836 | 2012-08-22 05:24:36 +0100 | [diff] [blame] | 4124 | struct dir_ent *dir_ent = NULL; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4125 | |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4126 | scan7_init_dir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4127 | |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4128 | while((dir_ent = scan7_readdir(&dir, dir_info, dir_ent)) != NULL) { |
Phillip Lougher | 0a67088 | 2012-10-05 04:09:13 +0100 | [diff] [blame] | 4129 | struct stat *buf = &dir_ent->inode->buf; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4130 | |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4131 | update_info(dir_ent); |
| 4132 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4133 | if(dir_ent->inode->inode == SQUASHFS_INVALID_BLK) { |
| 4134 | switch(buf->st_mode & S_IFMT) { |
| 4135 | case S_IFREG: |
| 4136 | squashfs_type = SQUASHFS_FILE_TYPE; |
plougher | 110799c | 2009-03-30 01:50:40 +0000 | [diff] [blame] | 4137 | write_file(inode, dir_ent, |
| 4138 | &duplicate_file); |
| 4139 | INFO("file %s, uncompressed size %lld " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4140 | "bytes %s\n", |
| 4141 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 4142 | (long long) buf->st_size, |
| 4143 | duplicate_file ? "DUPLICATE" : |
| 4144 | ""); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4145 | break; |
| 4146 | |
| 4147 | case S_IFDIR: |
| 4148 | squashfs_type = SQUASHFS_DIR_TYPE; |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4149 | dir_scan7(inode, dir_ent->dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4150 | break; |
| 4151 | |
| 4152 | case S_IFLNK: |
| 4153 | squashfs_type = SQUASHFS_SYMLINK_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4154 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4155 | squashfs_type, 0, 0, 0, NULL, |
| 4156 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4157 | INFO("symbolic link %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4158 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4159 | sym_count ++; |
| 4160 | break; |
| 4161 | |
| 4162 | case S_IFCHR: |
| 4163 | squashfs_type = SQUASHFS_CHRDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4164 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4165 | squashfs_type, 0, 0, 0, NULL, |
| 4166 | NULL, NULL, 0); |
| 4167 | INFO("character device %s inode 0x%llx" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4168 | "\n", subpathname(dir_ent), |
| 4169 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4170 | dev_count ++; |
| 4171 | break; |
| 4172 | |
| 4173 | case S_IFBLK: |
| 4174 | squashfs_type = SQUASHFS_BLKDEV_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4175 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4176 | squashfs_type, 0, 0, 0, NULL, |
| 4177 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4178 | INFO("block device %s inode 0x%llx\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4179 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4180 | dev_count ++; |
| 4181 | break; |
| 4182 | |
| 4183 | case S_IFIFO: |
| 4184 | squashfs_type = SQUASHFS_FIFO_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4185 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4186 | squashfs_type, 0, 0, 0, NULL, |
| 4187 | NULL, NULL, 0); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4188 | INFO("fifo %s inode 0x%llx\n", |
| 4189 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4190 | fifo_count ++; |
| 4191 | break; |
| 4192 | |
| 4193 | case S_IFSOCK: |
| 4194 | squashfs_type = SQUASHFS_SOCKET_TYPE; |
plougher | 3c6bdb5 | 2010-05-01 02:30:59 +0000 | [diff] [blame] | 4195 | create_inode(inode, NULL, dir_ent, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4196 | squashfs_type, 0, 0, 0, NULL, |
| 4197 | NULL, NULL, 0); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4198 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4199 | "0x%llx\n", |
| 4200 | subpathname(dir_ent), *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4201 | sock_count ++; |
| 4202 | break; |
| 4203 | |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4204 | default: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4205 | BAD_ERROR("%s unrecognised file type, " |
Phillip Lougher | 494479f | 2012-02-03 15:45:41 +0000 | [diff] [blame] | 4206 | "mode is %x\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4207 | subpathname(dir_ent), |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4208 | buf->st_mode); |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 4209 | } |
| 4210 | dir_ent->inode->inode = *inode; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4211 | dir_ent->inode->type = squashfs_type; |
| 4212 | } else { |
| 4213 | *inode = dir_ent->inode->inode; |
| 4214 | squashfs_type = dir_ent->inode->type; |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4215 | switch(squashfs_type) { |
| 4216 | case SQUASHFS_FILE_TYPE: |
| 4217 | if(!sorted) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4218 | INFO("file %s, uncompressed " |
| 4219 | "size %lld bytes LINK" |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4220 | "\n", |
| 4221 | subpathname(dir_ent), |
plougher | 82ab233 | 2009-04-21 00:21:21 +0000 | [diff] [blame] | 4222 | (long long) |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4223 | buf->st_size); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4224 | break; |
| 4225 | case SQUASHFS_SYMLINK_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4226 | INFO("symbolic link %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4227 | "LINK\n", subpathname(dir_ent), |
| 4228 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4229 | break; |
| 4230 | case SQUASHFS_CHRDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4231 | INFO("character device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4232 | "LINK\n", subpathname(dir_ent), |
| 4233 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4234 | break; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4235 | case SQUASHFS_BLKDEV_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4236 | INFO("block device %s inode 0x%llx " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4237 | "LINK\n", subpathname(dir_ent), |
| 4238 | *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4239 | break; |
| 4240 | case SQUASHFS_FIFO_TYPE: |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4241 | INFO("fifo %s inode 0x%llx LINK\n", |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4242 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4243 | break; |
| 4244 | case SQUASHFS_SOCKET_TYPE: |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4245 | INFO("unix domain socket %s inode " |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4246 | "0x%llx LINK\n", |
| 4247 | subpathname(dir_ent), *inode); |
plougher | 04b0d5f | 2006-02-10 00:42:06 +0000 | [diff] [blame] | 4248 | break; |
| 4249 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
Phillip Lougher | 0366aec | 2012-10-05 04:06:04 +0100 | [diff] [blame] | 4252 | add_dir(*inode, get_inode_no(dir_ent->inode), dir_ent->name, |
| 4253 | squashfs_type, &dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
plougher | 29e3709 | 2007-04-15 01:24:51 +0000 | [diff] [blame] | 4256 | write_dir(inode, dir_info, &dir); |
Phillip Lougher | 4980d7f | 2012-10-05 03:47:12 +0100 | [diff] [blame] | 4257 | INFO("directory %s inode 0x%llx\n", subpathname(dir_info->dir_ent), |
| 4258 | *inode); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4259 | |
Phillip Lougher | 070c0f7 | 2014-07-27 01:03:43 +0100 | [diff] [blame] | 4260 | scan7_freedir(&dir); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
| 4263 | |
| 4264 | unsigned int slog(unsigned int block) |
| 4265 | { |
| 4266 | int i; |
| 4267 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 4268 | for(i = 12; i <= 20; i++) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4269 | if(block == (1 << i)) |
| 4270 | return i; |
| 4271 | return 0; |
| 4272 | } |
| 4273 | |
| 4274 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4275 | int old_excluded(char *filename, struct stat *buf) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4276 | { |
| 4277 | int i; |
| 4278 | |
| 4279 | for(i = 0; i < exclude; i++) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4280 | if((exclude_paths[i].st_dev == buf->st_dev) && |
| 4281 | (exclude_paths[i].st_ino == buf->st_ino)) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4282 | return TRUE; |
| 4283 | return FALSE; |
| 4284 | } |
| 4285 | |
| 4286 | |
| 4287 | #define ADD_ENTRY(buf) \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4288 | if(exclude % EXCLUDE_SIZE == 0) { \ |
| 4289 | exclude_paths = realloc(exclude_paths, (exclude + EXCLUDE_SIZE) \ |
| 4290 | * sizeof(struct exclude_info)); \ |
| 4291 | if(exclude_paths == NULL) \ |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4292 | MEM_ERROR(); \ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4293 | } \ |
| 4294 | exclude_paths[exclude].st_dev = buf.st_dev; \ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4295 | exclude_paths[exclude++].st_ino = buf.st_ino; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4296 | int old_add_exclude(char *path) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4297 | { |
| 4298 | int i; |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 4299 | char *filename; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4300 | struct stat buf; |
| 4301 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4302 | if(path[0] == '/' || strncmp(path, "./", 2) == 0 || |
| 4303 | strncmp(path, "../", 3) == 0) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4304 | if(lstat(path, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 4305 | ERROR_START("Cannot stat exclude dir/file %s because " |
| 4306 | "%s", path, strerror(errno)); |
| 4307 | ERROR_EXIT(", ignoring\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4308 | return TRUE; |
| 4309 | } |
| 4310 | ADD_ENTRY(buf); |
| 4311 | return TRUE; |
| 4312 | } |
| 4313 | |
| 4314 | for(i = 0; i < source; i++) { |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 4315 | int res = asprintf(&filename, "%s/%s", source_path[i], path); |
| 4316 | if(res == -1) |
| 4317 | BAD_ERROR("asprintf failed in old_add_exclude\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4318 | if(lstat(filename, &buf) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 4319 | if(!(errno == ENOENT || errno == ENOTDIR)) { |
| 4320 | ERROR_START("Cannot stat exclude dir/file %s " |
| 4321 | "because %s", filename, strerror(errno)); |
| 4322 | ERROR_EXIT(", ignoring\n"); |
| 4323 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 4324 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4325 | continue; |
| 4326 | } |
Phillip Lougher | dcb5af9 | 2012-11-30 03:05:31 +0000 | [diff] [blame] | 4327 | free(filename); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4328 | ADD_ENTRY(buf); |
| 4329 | } |
| 4330 | return TRUE; |
| 4331 | } |
| 4332 | |
| 4333 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4334 | void add_old_root_entry(char *name, squashfs_inode inode, int inode_number, |
| 4335 | int type) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4336 | { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4337 | old_root_entry = realloc(old_root_entry, |
| 4338 | sizeof(struct old_root_entry_info) * (old_root_entries + 1)); |
| 4339 | if(old_root_entry == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4340 | MEM_ERROR(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4341 | |
plougher | a326c18 | 2009-08-29 05:41:45 +0000 | [diff] [blame] | 4342 | old_root_entry[old_root_entries].name = strdup(name); |
| 4343 | old_root_entry[old_root_entries].inode.inode = inode; |
| 4344 | old_root_entry[old_root_entries].inode.inode_number = inode_number; |
| 4345 | old_root_entry[old_root_entries].inode.type = type; |
| 4346 | old_root_entry[old_root_entries++].inode.root_entry = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4350 | void initialise_threads(int readq, int fragq, int bwriteq, int fwriteq, |
| 4351 | int freelst, char *destination_file) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4352 | { |
| 4353 | int i; |
| 4354 | sigset_t sigmask, old_mask; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4355 | int total_mem = readq; |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4356 | int reader_size; |
| 4357 | int fragment_size; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4358 | int fwriter_size; |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4359 | /* |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4360 | * bwriter_size is global because it is needed in |
plougher | 5741d79 | 2010-09-04 03:20:50 +0000 | [diff] [blame] | 4361 | * write_file_blocks_dup() |
| 4362 | */ |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4363 | |
| 4364 | /* |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4365 | * Never allow the total size of the queues to be larger than |
| 4366 | * physical memory |
| 4367 | * |
| 4368 | * When adding together the possibly user supplied values, make |
| 4369 | * sure they've not been deliberately contrived to overflow an int |
| 4370 | */ |
| 4371 | if(add_overflow(total_mem, fragq)) |
| 4372 | BAD_ERROR("Queue sizes rediculously too large\n"); |
| 4373 | total_mem += fragq; |
| 4374 | if(add_overflow(total_mem, bwriteq)) |
| 4375 | BAD_ERROR("Queue sizes rediculously too large\n"); |
| 4376 | total_mem += bwriteq; |
| 4377 | if(add_overflow(total_mem, fwriteq)) |
| 4378 | BAD_ERROR("Queue sizes rediculously too large\n"); |
| 4379 | total_mem += fwriteq; |
| 4380 | |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 4381 | check_usable_phys_mem(total_mem); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4382 | |
| 4383 | /* |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4384 | * convert from queue size in Mbytes to queue size in |
| 4385 | * blocks. |
| 4386 | * |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4387 | * This isn't going to overflow an int unless there exists |
| 4388 | * systems with more than 8 Petabytes of RAM! |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4389 | */ |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4390 | reader_size = readq << (20 - block_log); |
| 4391 | fragment_size = fragq << (20 - block_log); |
| 4392 | bwriter_size = bwriteq << (20 - block_log); |
| 4393 | fwriter_size = fwriteq << (20 - block_log); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4394 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4395 | /* |
| 4396 | * setup signal handlers for the main thread, these cleanup |
| 4397 | * deleting the destination file, if appending the |
| 4398 | * handlers for SIGTERM and SIGINT will be replaced with handlers |
| 4399 | * allowing the user to press ^C twice to restore the existing |
| 4400 | * filesystem. |
| 4401 | * |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4402 | * SIGUSR1 is an internal signal, which is used by the sub-threads |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4403 | * to tell the main thread to terminate, deleting the destination file, |
| 4404 | * or if necessary restoring the filesystem on appending |
| 4405 | */ |
Phillip Lougher | b8ec520 | 2013-03-28 20:14:37 +0000 | [diff] [blame] | 4406 | signal(SIGTERM, sighandler); |
| 4407 | signal(SIGINT, sighandler); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4408 | signal(SIGUSR1, sighandler); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4409 | |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 4410 | /* block SIGQUIT and SIGHUP, these are handled by the info thread */ |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4411 | sigemptyset(&sigmask); |
| 4412 | sigaddset(&sigmask, SIGQUIT); |
Phillip Lougher | 7538d74 | 2013-04-22 05:33:27 +0100 | [diff] [blame] | 4413 | sigaddset(&sigmask, SIGHUP); |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 4414 | sigaddset(&sigmask, SIGALRM); |
Phillip Lougher | b1259f7 | 2013-06-06 03:10:15 +0100 | [diff] [blame] | 4415 | if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1) |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4416 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4417 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4418 | /* |
| 4419 | * temporarily block these signals, so the created sub-threads |
| 4420 | * will ignore them, ensuring the main thread handles them |
| 4421 | */ |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4422 | sigemptyset(&sigmask); |
| 4423 | sigaddset(&sigmask, SIGINT); |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4424 | sigaddset(&sigmask, SIGTERM); |
Phillip Lougher | dd34339 | 2013-03-31 23:29:03 +0100 | [diff] [blame] | 4425 | sigaddset(&sigmask, SIGUSR1); |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4426 | if(pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4427 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4428 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4429 | if(processors == -1) { |
| 4430 | #ifndef linux |
| 4431 | int mib[2]; |
| 4432 | size_t len = sizeof(processors); |
| 4433 | |
| 4434 | mib[0] = CTL_HW; |
| 4435 | #ifdef HW_AVAILCPU |
| 4436 | mib[1] = HW_AVAILCPU; |
| 4437 | #else |
| 4438 | mib[1] = HW_NCPU; |
| 4439 | #endif |
| 4440 | |
| 4441 | if(sysctl(mib, 2, &processors, &len, NULL, 0) == -1) { |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 4442 | ERROR_START("Failed to get number of available " |
| 4443 | "processors."); |
| 4444 | ERROR_EXIT(" Defaulting to 1\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4445 | processors = 1; |
| 4446 | } |
| 4447 | #else |
plougher | 9cc26b7 | 2010-08-17 01:05:55 +0000 | [diff] [blame] | 4448 | processors = sysconf(_SC_NPROCESSORS_ONLN); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4449 | #endif |
| 4450 | } |
| 4451 | |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4452 | if(multiply_overflow(processors, 3) || |
| 4453 | multiply_overflow(processors * 3, sizeof(pthread_t))) |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4454 | BAD_ERROR("Processors too large\n"); |
| 4455 | |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4456 | deflator_thread = malloc(processors * 3 * sizeof(pthread_t)); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4457 | if(deflator_thread == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4458 | MEM_ERROR(); |
Phillip Lougher | e1668fe | 2012-12-30 05:48:12 +0000 | [diff] [blame] | 4459 | |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4460 | frag_deflator_thread = &deflator_thread[processors]; |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4461 | frag_thread = &frag_deflator_thread[processors]; |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4462 | |
| 4463 | to_reader = queue_init(1); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4464 | to_deflate = queue_init(reader_size); |
| 4465 | to_process_frag = queue_init(reader_size); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4466 | to_writer = queue_init(bwriter_size + fwriter_size); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4467 | from_writer = queue_init(1); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4468 | to_frag = queue_init(fragment_size); |
| 4469 | locked_fragment = queue_init(fragment_size); |
Phillip Lougher | 0e1656d | 2013-05-20 03:47:24 +0100 | [diff] [blame] | 4470 | to_main = seq_queue_init(); |
Phillip Lougher | cf4c7bc | 2014-04-13 04:48:11 +0100 | [diff] [blame] | 4471 | reader_buffer = cache_init(block_size, reader_size, 0, 0); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 4472 | bwriter_buffer = cache_init(block_size, bwriter_size, 1, freelst); |
| 4473 | fwriter_buffer = cache_init(block_size, fwriter_size, 1, freelst); |
Phillip Lougher | 1ac3715 | 2014-05-10 03:09:28 +0100 | [diff] [blame] | 4474 | fragment_buffer = cache_init(block_size, fragment_size, 1, 0); |
| 4475 | reserve_cache = cache_init(block_size, processors + 1, 1, 0); |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4476 | pthread_create(&reader_thread, NULL, reader, NULL); |
| 4477 | pthread_create(&writer_thread, NULL, writer, NULL); |
Phillip Lougher | 3b89ee8 | 2012-10-18 23:55:37 +0100 | [diff] [blame] | 4478 | init_progress_bar(); |
Phillip Lougher | 24551a8 | 2013-03-24 02:30:50 +0000 | [diff] [blame] | 4479 | init_info(); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4480 | |
| 4481 | for(i = 0; i < processors; i++) { |
Phillip Lougher | 943acad | 2014-04-17 03:31:01 +0100 | [diff] [blame] | 4482 | if(pthread_create(&deflator_thread[i], NULL, deflator, NULL)) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4483 | BAD_ERROR("Failed to create thread\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4484 | if(pthread_create(&frag_deflator_thread[i], NULL, frag_deflator, |
| 4485 | NULL) != 0) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4486 | BAD_ERROR("Failed to create thread\n"); |
Phillip Lougher | 8bb17b0 | 2014-03-30 23:59:55 +0100 | [diff] [blame] | 4487 | if(pthread_create(&frag_thread[i], NULL, frag_thrd, |
| 4488 | (void *) destination_file) != 0) |
Phillip Lougher | 9de84ac | 2014-02-20 05:18:48 +0000 | [diff] [blame] | 4489 | BAD_ERROR("Failed to create thread\n"); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
Phillip Lougher | 0280d99 | 2014-01-27 05:54:07 +0000 | [diff] [blame] | 4492 | main_thread = pthread_self(); |
| 4493 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 4494 | printf("Parallel mksquashfs: Using %d processor%s\n", processors, |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4495 | processors == 1 ? "" : "s"); |
| 4496 | |
Phillip Lougher | 9391cb1 | 2013-03-20 05:16:10 +0000 | [diff] [blame] | 4497 | /* Restore the signal mask for the main thread */ |
Phillip Lougher | 8f8d275 | 2013-03-26 04:38:38 +0000 | [diff] [blame] | 4498 | if(pthread_sigmask(SIG_SETMASK, &old_mask, NULL) == -1) |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 4499 | BAD_ERROR("Failed to set signal mask in intialise_threads\n"); |
| 4500 | } |
| 4501 | |
| 4502 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4503 | long long write_inode_lookup_table() |
| 4504 | { |
| 4505 | int i, inode_number, lookup_bytes = SQUASHFS_LOOKUP_BYTES(inode_count); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4506 | void *it; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4507 | |
| 4508 | if(inode_count == sinode_count) |
| 4509 | goto skip_inode_hash_table; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4510 | |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4511 | it = realloc(inode_lookup_table, lookup_bytes); |
| 4512 | if(it == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4513 | MEM_ERROR(); |
plougher | 44f0328 | 2010-07-27 00:31:36 +0000 | [diff] [blame] | 4514 | inode_lookup_table = it; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4515 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4516 | for(i = 0; i < INODE_HASH_SIZE; i ++) { |
Phillip Lougher | f2f83b6 | 2014-04-18 01:30:47 +0100 | [diff] [blame] | 4517 | struct inode_info *inode; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4518 | |
| 4519 | for(inode = inode_info[i]; inode; inode = inode->next) { |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4520 | |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 4521 | inode_number = get_inode_no(inode); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4522 | |
Phillip Lougher | 9f59596 | 2014-07-07 04:37:47 +0100 | [diff] [blame] | 4523 | /* The empty action will produce orphaned inode |
| 4524 | * entries in the inode_info[] table. These |
| 4525 | * entries because they are orphaned will not be |
| 4526 | * allocated an inode number in dir_scan5(), so |
| 4527 | * skip any entries with the default dummy inode |
| 4528 | * number of 0 */ |
| 4529 | if(inode_number == 0) |
| 4530 | continue; |
| 4531 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 4532 | SQUASHFS_SWAP_LONG_LONGS(&inode->inode, |
| 4533 | &inode_lookup_table[inode_number - 1], 1); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4534 | |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4535 | } |
| 4536 | } |
| 4537 | |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 4538 | skip_inode_hash_table: |
plougher | a0a49c3 | 2010-08-11 01:47:59 +0000 | [diff] [blame] | 4539 | return generic_write_table(lookup_bytes, inode_lookup_table, 0, NULL, |
| 4540 | noI); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4543 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4544 | char *get_component(char *target, char **targname) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4545 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4546 | char *start; |
| 4547 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4548 | while(*target == '/') |
rlougher | c4ebcf5 | 2007-11-08 17:52:49 +0000 | [diff] [blame] | 4549 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4550 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4551 | start = target; |
Phillip Lougher | 66d8b8e | 2014-04-20 21:01:33 +0100 | [diff] [blame] | 4552 | while(*target != '/' && *target != '\0') |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4553 | target ++; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4554 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4555 | *targname = strndup(start, target - start); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4556 | |
Phillip Lougher | 66d8b8e | 2014-04-20 21:01:33 +0100 | [diff] [blame] | 4557 | while(*target == '/') |
| 4558 | target ++; |
| 4559 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4560 | return target; |
| 4561 | } |
| 4562 | |
| 4563 | |
| 4564 | void free_path(struct pathname *paths) |
| 4565 | { |
| 4566 | int i; |
| 4567 | |
| 4568 | for(i = 0; i < paths->names; i++) { |
| 4569 | if(paths->name[i].paths) |
| 4570 | free_path(paths->name[i].paths); |
| 4571 | free(paths->name[i].name); |
| 4572 | if(paths->name[i].preg) { |
| 4573 | regfree(paths->name[i].preg); |
| 4574 | free(paths->name[i].preg); |
| 4575 | } |
| 4576 | } |
| 4577 | |
| 4578 | free(paths); |
| 4579 | } |
| 4580 | |
| 4581 | |
| 4582 | struct pathname *add_path(struct pathname *paths, char *target, char *alltarget) |
| 4583 | { |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4584 | char *targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4585 | int i, error; |
| 4586 | |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4587 | target = get_component(target, &targname); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4588 | |
| 4589 | if(paths == NULL) { |
plougher | dec6ef1 | 2010-12-18 02:47:53 +0000 | [diff] [blame] | 4590 | paths = malloc(sizeof(struct pathname)); |
| 4591 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4592 | MEM_ERROR(); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4593 | |
| 4594 | paths->names = 0; |
| 4595 | paths->name = NULL; |
| 4596 | } |
| 4597 | |
| 4598 | for(i = 0; i < paths->names; i++) |
| 4599 | if(strcmp(paths->name[i].name, targname) == 0) |
| 4600 | break; |
| 4601 | |
| 4602 | if(i == paths->names) { |
| 4603 | /* allocate new name entry */ |
| 4604 | paths->names ++; |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4605 | paths->name = realloc(paths->name, (i + 1) * |
| 4606 | sizeof(struct path_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4607 | if(paths->name == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4608 | MEM_ERROR(); |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4609 | paths->name[i].name = targname; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4610 | paths->name[i].paths = NULL; |
| 4611 | if(use_regex) { |
| 4612 | paths->name[i].preg = malloc(sizeof(regex_t)); |
plougher | 5c60eab | 2010-07-21 01:12:19 +0000 | [diff] [blame] | 4613 | if(paths->name[i].preg == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4614 | MEM_ERROR(); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4615 | error = regcomp(paths->name[i].preg, targname, |
| 4616 | REG_EXTENDED|REG_NOSUB); |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 4617 | if(error) { |
Phillip Lougher | 62859d2 | 2012-11-30 05:53:56 +0000 | [diff] [blame] | 4618 | char str[1024]; /* overflow safe */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4619 | |
| 4620 | regerror(error, paths->name[i].preg, str, 1024); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4621 | BAD_ERROR("invalid regex %s in export %s, " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4622 | "because %s\n", targname, alltarget, |
| 4623 | str); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4624 | } |
| 4625 | } else |
| 4626 | paths->name[i].preg = NULL; |
| 4627 | |
| 4628 | if(target[0] == '\0') |
| 4629 | /* at leaf pathname component */ |
| 4630 | paths->name[i].paths = NULL; |
| 4631 | else |
| 4632 | /* recurse adding child components */ |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4633 | paths->name[i].paths = add_path(NULL, target, |
| 4634 | alltarget); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4635 | } else { |
| 4636 | /* existing matching entry */ |
Phillip Lougher | 8e44e05 | 2012-11-26 02:58:35 +0000 | [diff] [blame] | 4637 | free(targname); |
| 4638 | |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4639 | if(paths->name[i].paths == NULL) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4640 | /* No sub-directory which means this is the leaf |
| 4641 | * component of a pre-existing exclude which subsumes |
| 4642 | * the exclude currently being added, in which case stop |
| 4643 | * adding components */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4644 | } else if(target[0] == '\0') { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4645 | /* at leaf pathname component and child components exist |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4646 | * from more specific excludes, delete as they're |
| 4647 | * subsumed by this exclude */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4648 | free_path(paths->name[i].paths); |
| 4649 | paths->name[i].paths = NULL; |
| 4650 | } else |
| 4651 | /* recurse adding child components */ |
| 4652 | add_path(paths->name[i].paths, target, alltarget); |
| 4653 | } |
| 4654 | |
| 4655 | return paths; |
| 4656 | } |
plougher | 2ea8914 | 2008-03-11 01:34:19 +0000 | [diff] [blame] | 4657 | |
| 4658 | |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4659 | void add_exclude(char *target) |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4660 | { |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4661 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4662 | if(target[0] == '/' || strncmp(target, "./", 2) == 0 || |
| 4663 | strncmp(target, "../", 3) == 0) |
| 4664 | BAD_ERROR("/, ./ and ../ prefixed excludes not supported with " |
| 4665 | "-wildcards or -regex options\n"); |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 4666 | else if(strncmp(target, "... ", 4) == 0) |
| 4667 | stickypath = add_path(stickypath, target + 4, target + 4); |
| 4668 | else |
| 4669 | path = add_path(path, target, target); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4670 | } |
| 4671 | |
| 4672 | |
| 4673 | void display_path(int depth, struct pathname *paths) |
| 4674 | { |
| 4675 | int i, n; |
| 4676 | |
| 4677 | if(paths == NULL) |
| 4678 | return; |
| 4679 | |
| 4680 | for(i = 0; i < paths->names; i++) { |
| 4681 | for(n = 0; n < depth; n++) |
| 4682 | printf("\t"); |
| 4683 | printf("%d: %s\n", depth, paths->name[i].name); |
| 4684 | display_path(depth + 1, paths->name[i].paths); |
| 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | |
| 4689 | void display_path2(struct pathname *paths, char *string) |
| 4690 | { |
| 4691 | int i; |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4692 | char *path; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4693 | |
| 4694 | if(paths == NULL) { |
| 4695 | printf("%s\n", string); |
| 4696 | return; |
| 4697 | } |
| 4698 | |
| 4699 | for(i = 0; i < paths->names; i++) { |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4700 | int res = asprintf(&path, "%s/%s", string, paths->name[i].name); |
| 4701 | if(res == -1) |
| 4702 | BAD_ERROR("asprintf failed in display_path2\n"); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4703 | display_path2(paths->name[i].paths, path); |
Phillip Lougher | 91459c1 | 2012-11-30 05:24:44 +0000 | [diff] [blame] | 4704 | free(path); |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4709 | struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path) |
| 4710 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4711 | int count = paths == NULL ? 0 : paths->count; |
| 4712 | |
| 4713 | if(count % PATHS_ALLOC_SIZE == 0) { |
| 4714 | paths = realloc(paths, sizeof(struct pathnames) + |
| 4715 | (count + PATHS_ALLOC_SIZE) * sizeof(struct pathname *)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4716 | if(paths == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 4717 | MEM_ERROR(); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 4718 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4719 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4720 | paths->path[count] = path; |
| 4721 | paths->count = count + 1; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4722 | return paths; |
| 4723 | } |
| 4724 | |
| 4725 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4726 | int excluded_match(char *name, struct pathname *path, struct pathnames **new) |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4727 | { |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4728 | int i; |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4729 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4730 | for(i = 0; i < path->names; i++) { |
| 4731 | int match = use_regex ? |
| 4732 | regexec(path->name[i].preg, name, (size_t) 0, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4733 | NULL, 0) == 0 : |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4734 | fnmatch(path->name[i].name, name, |
| 4735 | FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0; |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4736 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4737 | if(match) { |
| 4738 | if(path->name[i].paths == NULL || new == NULL) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4739 | /* match on a leaf component, any subdirectories |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4740 | * in the filesystem should be excluded */ |
| 4741 | return TRUE; |
| 4742 | else |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 4743 | /* match on a non-leaf component, add any |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 4744 | * subdirectories to the new set of |
| 4745 | * subdirectories to scan for this name */ |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4746 | *new = add_subdir(*new, path->name[i].paths); |
| 4747 | } |
| 4748 | } |
| 4749 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4750 | return FALSE; |
| 4751 | } |
| 4752 | |
| 4753 | |
| 4754 | int excluded(char *name, struct pathnames *paths, struct pathnames **new) |
| 4755 | { |
| 4756 | int n; |
| 4757 | |
| 4758 | if(stickypath && excluded_match(name, stickypath, NULL)) |
| 4759 | return TRUE; |
| 4760 | |
| 4761 | for(n = 0; paths && n < paths->count; n++) { |
| 4762 | int res = excluded_match(name, paths->path[n], new); |
| 4763 | if(res) { |
| 4764 | free(*new); |
| 4765 | *new = NULL; |
| 4766 | return TRUE; |
| 4767 | } |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 4770 | /* |
| 4771 | * Either: |
| 4772 | * - no matching names found, return empty new search set, or |
| 4773 | * - one or more matches with sub-directories found (no leaf matches), |
| 4774 | * in which case return new search set. |
| 4775 | * |
| 4776 | * In either case return FALSE as we don't want to exclude this entry |
| 4777 | */ |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 4778 | return FALSE; |
| 4779 | } |
| 4780 | |
| 4781 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4782 | void process_exclude_file(char *argv) |
| 4783 | { |
| 4784 | FILE *fd; |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4785 | char buffer[MAX_LINE + 1]; /* overflow safe */ |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4786 | char *filename; |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4787 | |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4788 | fd = fopen(argv, "r"); |
| 4789 | if(fd == NULL) |
| 4790 | BAD_ERROR("Failed to open exclude file \"%s\" because %s\n", |
| 4791 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4792 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4793 | while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) { |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4794 | int len = strlen(filename); |
| 4795 | |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4796 | if(len == MAX_LINE && filename[len - 1] != '\n') |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4797 | /* line too large */ |
| 4798 | BAD_ERROR("Line too long when reading " |
Phillip Lougher | fe2c735 | 2012-12-23 06:55:41 +0000 | [diff] [blame] | 4799 | "exclude file \"%s\", larger than %d " |
Phillip Lougher | 83847c1 | 2012-12-23 07:22:36 +0000 | [diff] [blame] | 4800 | "bytes\n", argv, MAX_LINE); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4801 | |
| 4802 | /* |
| 4803 | * Remove '\n' terminator if it exists (the last line |
| 4804 | * in the file may not be '\n' terminated) |
| 4805 | */ |
| 4806 | if(len && filename[len - 1] == '\n') |
| 4807 | filename[len - 1] = '\0'; |
| 4808 | |
| 4809 | /* Skip any leading whitespace */ |
| 4810 | while(isspace(*filename)) |
| 4811 | filename ++; |
| 4812 | |
| 4813 | /* if comment line, skip */ |
| 4814 | if(*filename == '#') |
| 4815 | continue; |
| 4816 | |
| 4817 | /* |
| 4818 | * check for initial backslash, to accommodate |
| 4819 | * filenames with leading space or leading # character |
| 4820 | */ |
| 4821 | if(*filename == '\\') |
| 4822 | filename ++; |
| 4823 | |
| 4824 | /* if line is now empty after skipping characters, skip it */ |
| 4825 | if(*filename == '\0') |
| 4826 | continue; |
| 4827 | |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4828 | if(old_exclude) |
| 4829 | old_add_exclude(filename); |
| 4830 | else |
| 4831 | add_exclude(filename); |
Phillip Lougher | b22336d | 2012-12-20 18:44:22 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
| 4834 | if(ferror(fd)) |
| 4835 | BAD_ERROR("Reading exclude file \"%s\" failed because %s\n", |
| 4836 | argv, strerror(errno)); |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4837 | |
| 4838 | fclose(fd); |
| 4839 | } |
| 4840 | |
Mohan Srinivasan | 58e18d7 | 2016-07-26 15:06:29 -0700 | [diff] [blame] | 4841 | /* ANDROID CHANGES START*/ |
| 4842 | #ifdef ANDROID |
| 4843 | /* |
| 4844 | * Return TRUE (don't compress) if the (regular) file is in the |
| 4845 | * whitelist. Else return the Global noD value. |
| 4846 | * |
| 4847 | * Note : These functions are lifted 100% from the existing exclude |
| 4848 | * file code. For maintainability, I've kept this code separate from |
| 4849 | * the exclude code instead of having common code for both paths. |
| 4850 | */ |
| 4851 | static int |
| 4852 | whitelisted(struct stat *buf) |
| 4853 | { |
| 4854 | int i; |
| 4855 | |
| 4856 | /* |
| 4857 | * only regular files in the whitelist |
| 4858 | */ |
| 4859 | if (!S_ISREG(buf->st_mode)) |
| 4860 | return noD; |
| 4861 | for (i = 0; i < whitelist; i++) { |
| 4862 | if ((whitelist_paths[i].st_dev == buf->st_dev) && |
| 4863 | (whitelist_paths[i].st_ino == buf->st_ino)) { |
| 4864 | /* Don't compress */ |
| 4865 | whitelisted_count++; |
| 4866 | return TRUE; |
| 4867 | } |
| 4868 | } |
| 4869 | return noD; |
| 4870 | } |
| 4871 | |
| 4872 | static void |
| 4873 | add_whitelist_entry(char *filename, struct stat *buf) |
| 4874 | { |
| 4875 | if (!S_ISREG(buf->st_mode)) { |
| 4876 | BAD_ERROR("Cannot whitelist %s only regular files can be whitelisted", |
| 4877 | filename); |
| 4878 | } |
| 4879 | if (whitelist % WHITELIST_SIZE == 0) { |
| 4880 | whitelist_paths = realloc(whitelist_paths, |
| 4881 | (whitelist + WHITELIST_SIZE) |
| 4882 | * sizeof(struct whitelist_info)); |
| 4883 | if (whitelist_paths == NULL) |
| 4884 | MEM_ERROR(); |
| 4885 | } |
| 4886 | whitelist_paths[whitelist].st_dev = buf->st_dev; |
| 4887 | whitelist_paths[whitelist++].st_ino = buf->st_ino; |
| 4888 | } |
| 4889 | |
| 4890 | static int |
| 4891 | add_whitelist(char *path) |
| 4892 | { |
| 4893 | int i; |
| 4894 | char *filename; |
| 4895 | struct stat buf; |
| 4896 | |
| 4897 | /* Absolute of (filesystem) relative path */ |
| 4898 | if (path[0] == '/' || strncmp(path, "./", 2) == 0 || |
| 4899 | strncmp(path, "../", 3) == 0) { |
| 4900 | if(lstat(path, &buf) == -1) { |
| 4901 | BAD_ERROR("Cannot stat whitelist dir/file %s because " |
| 4902 | "%s", path, strerror(errno)); |
| 4903 | } |
| 4904 | add_whitelist_entry(path, &buf); |
| 4905 | return TRUE; |
| 4906 | } |
| 4907 | |
| 4908 | /* pathname relative to mksquashfs source dirs */ |
| 4909 | for(i = 0; i < source; i++) { |
| 4910 | int res = asprintf(&filename, "%s/%s", source_path[i], path); |
| 4911 | if(res == -1) |
| 4912 | BAD_ERROR("asprintf failed in add_whitelist\n"); |
| 4913 | if(lstat(filename, &buf) == -1) { |
| 4914 | if(!(errno == ENOENT || errno == ENOTDIR)) { |
| 4915 | BAD_ERROR("Cannot stat whitelist dir/file %s " |
| 4916 | "because %s", filename, strerror(errno)); |
| 4917 | } |
| 4918 | free(filename); |
| 4919 | continue; |
| 4920 | } |
| 4921 | add_whitelist_entry(filename, &buf); |
| 4922 | free(filename); |
| 4923 | } |
| 4924 | return TRUE; |
| 4925 | } |
| 4926 | |
| 4927 | static void |
| 4928 | process_whitelist_file(char *argv) |
| 4929 | { |
| 4930 | FILE *fd; |
| 4931 | char buffer[MAX_LINE + 1]; /* overflow safe */ |
| 4932 | char *filename; |
| 4933 | |
| 4934 | fd = fopen(argv, "r"); |
| 4935 | if(fd == NULL) |
| 4936 | BAD_ERROR("Failed to open whitelist file \"%s\" because %s\n", |
| 4937 | argv, strerror(errno)); |
| 4938 | |
| 4939 | while(fgets(filename = buffer, MAX_LINE + 1, fd) != NULL) { |
| 4940 | int len = strlen(filename); |
| 4941 | |
| 4942 | if(len == MAX_LINE && filename[len - 1] != '\n') |
| 4943 | /* line too large */ |
| 4944 | BAD_ERROR("Line too long when reading " |
| 4945 | "whitelist file \"%s\", larger than %d " |
| 4946 | "bytes\n", argv, MAX_LINE); |
| 4947 | |
| 4948 | /* |
| 4949 | * Remove '\n' terminator if it exists (the last line |
| 4950 | * in the file may not be '\n' terminated) |
| 4951 | */ |
| 4952 | if(len && filename[len - 1] == '\n') |
| 4953 | filename[len - 1] = '\0'; |
| 4954 | |
| 4955 | /* Skip any leading whitespace */ |
| 4956 | while(isspace(*filename)) |
| 4957 | filename ++; |
| 4958 | |
| 4959 | /* if comment line, skip */ |
| 4960 | if(*filename == '#') |
| 4961 | continue; |
| 4962 | |
| 4963 | /* |
| 4964 | * check for initial backslash, to accommodate |
| 4965 | * filenames with leading space or leading # character |
| 4966 | */ |
| 4967 | if(*filename == '\\') |
| 4968 | filename ++; |
| 4969 | |
| 4970 | /* if line is now empty after skipping characters, skip it */ |
| 4971 | if(*filename == '\0') |
| 4972 | continue; |
| 4973 | |
| 4974 | add_whitelist(filename); |
| 4975 | } |
| 4976 | |
| 4977 | if(ferror(fd)) |
| 4978 | BAD_ERROR("Reading whitelist file \"%s\" failed because %s\n", |
| 4979 | argv, strerror(errno)); |
| 4980 | |
| 4981 | fclose(fd); |
| 4982 | } |
| 4983 | #endif |
| 4984 | /* ANDROID CHANGES END */ |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 4985 | |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4986 | #define RECOVER_ID "Squashfs recovery file v1.0\n" |
| 4987 | #define RECOVER_ID_SIZE 28 |
| 4988 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 4989 | void write_recovery_data(struct squashfs_super_block *sBlk) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4990 | { |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 4991 | int res, recoverfd, bytes = sBlk->bytes_used - sBlk->inode_table_start; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4992 | pid_t pid = getpid(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 4993 | char *metadata; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4994 | char header[] = RECOVER_ID; |
| 4995 | |
| 4996 | if(recover == FALSE) { |
| 4997 | printf("No recovery data option specified.\n"); |
plougher | eac1853 | 2007-10-29 05:26:06 +0000 | [diff] [blame] | 4998 | printf("Skipping saving recovery file.\n\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 4999 | return; |
| 5000 | } |
| 5001 | |
plougher | 1b879bc | 2010-12-18 02:49:42 +0000 | [diff] [blame] | 5002 | metadata = malloc(bytes); |
| 5003 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 5004 | MEM_ERROR(); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 5005 | |
plougher | 1d065e9 | 2010-06-18 03:58:27 +0000 | [diff] [blame] | 5006 | res = read_fs_bytes(fd, sBlk->inode_table_start, bytes, metadata); |
Phillip Lougher | 477f433 | 2013-03-06 03:55:08 +0000 | [diff] [blame] | 5007 | if(res == 0) { |
| 5008 | ERROR("Failed to read append filesystem metadata\n"); |
| 5009 | BAD_ERROR("Filesystem corrupted?\n"); |
| 5010 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5011 | |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 5012 | res = asprintf(&recovery_file, "squashfs_recovery_%s_%d", |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 5013 | getbase(destination_file), pid); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 5014 | if(res == -1) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 5015 | MEM_ERROR(); |
Phillip Lougher | af4c923 | 2012-11-15 03:46:28 +0000 | [diff] [blame] | 5016 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5017 | recoverfd = open(recovery_file, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU); |
| 5018 | if(recoverfd == -1) |
| 5019 | BAD_ERROR("Failed to create recovery file, because %s. " |
| 5020 | "Aborting\n", strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5021 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 5022 | if(write_bytes(recoverfd, header, RECOVER_ID_SIZE) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5023 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 5024 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5025 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5026 | if(write_bytes(recoverfd, sBlk, sizeof(struct squashfs_super_block)) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5027 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 5028 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5029 | |
plougher | 628e768 | 2009-03-29 22:12:24 +0000 | [diff] [blame] | 5030 | if(write_bytes(recoverfd, metadata, bytes) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5031 | BAD_ERROR("Failed to write recovery file, because %s\n", |
| 5032 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5033 | |
| 5034 | close(recoverfd); |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 5035 | free(metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5036 | |
| 5037 | printf("Recovery file \"%s\" written\n", recovery_file); |
| 5038 | printf("If Mksquashfs aborts abnormally (i.e. power failure), run\n"); |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5039 | printf("mksquashfs dummy %s -recover %s\n", destination_file, |
| 5040 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5041 | printf("to restore filesystem\n\n"); |
| 5042 | } |
| 5043 | |
| 5044 | |
| 5045 | void read_recovery_data(char *recovery_file, char *destination_file) |
| 5046 | { |
| 5047 | int fd, recoverfd, bytes; |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5048 | struct squashfs_super_block orig_sBlk, sBlk; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5049 | char *metadata; |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 5050 | int res; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5051 | struct stat buf; |
| 5052 | char header[] = RECOVER_ID; |
| 5053 | char header2[RECOVER_ID_SIZE]; |
| 5054 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 5055 | recoverfd = open(recovery_file, O_RDONLY); |
| 5056 | if(recoverfd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5057 | BAD_ERROR("Failed to open recovery file because %s\n", |
| 5058 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5059 | |
| 5060 | if(stat(destination_file, &buf) == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5061 | BAD_ERROR("Failed to stat destination file, because %s\n", |
| 5062 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5063 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 5064 | fd = open(destination_file, O_RDWR); |
| 5065 | if(fd == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5066 | BAD_ERROR("Failed to open destination file because %s\n", |
| 5067 | strerror(errno)); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5068 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 5069 | res = read_bytes(recoverfd, header2, RECOVER_ID_SIZE); |
| 5070 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5071 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 5072 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 5073 | if(res < RECOVER_ID_SIZE) |
| 5074 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5075 | if(strncmp(header, header2, RECOVER_ID_SIZE) !=0 ) |
| 5076 | BAD_ERROR("Not a recovery file\n"); |
| 5077 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5078 | res = read_bytes(recoverfd, &sBlk, sizeof(struct squashfs_super_block)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 5079 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5080 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 5081 | strerror(errno)); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5082 | if(res < sizeof(struct squashfs_super_block)) |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 5083 | BAD_ERROR("Recovery file appears to be truncated\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5084 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5085 | 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] | 5086 | if(res == 0) { |
| 5087 | ERROR("Failed to read superblock from output filesystem\n"); |
| 5088 | BAD_ERROR("Output filesystem is empty!\n"); |
| 5089 | } |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5090 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5091 | if(memcmp(((char *) &sBlk) + 4, ((char *) &orig_sBlk) + 4, |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5092 | sizeof(struct squashfs_super_block) - 4) != 0) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5093 | BAD_ERROR("Recovery file and destination file do not seem to " |
| 5094 | "match\n"); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5095 | |
| 5096 | bytes = sBlk.bytes_used - sBlk.inode_table_start; |
| 5097 | |
plougher | 9e9d9dc | 2010-12-18 02:53:57 +0000 | [diff] [blame] | 5098 | metadata = malloc(bytes); |
| 5099 | if(metadata == NULL) |
Phillip Lougher | ec71c3c | 2013-02-21 22:25:53 +0000 | [diff] [blame] | 5100 | MEM_ERROR(); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5101 | |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 5102 | res = read_bytes(recoverfd, metadata, bytes); |
| 5103 | if(res == -1) |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5104 | BAD_ERROR("Failed to read recovery file, because %s\n", |
| 5105 | strerror(errno)); |
plougher | 8a8c410 | 2009-03-29 22:28:49 +0000 | [diff] [blame] | 5106 | if(res < bytes) |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5107 | BAD_ERROR("Recovery file appears to be truncated\n"); |
| 5108 | |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 5109 | write_destination(fd, 0, sizeof(struct squashfs_super_block), &sBlk); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5110 | |
plougher | 0dd6f12 | 2009-03-29 21:43:57 +0000 | [diff] [blame] | 5111 | write_destination(fd, sBlk.inode_table_start, bytes, metadata); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5112 | |
| 5113 | close(recoverfd); |
| 5114 | close(fd); |
| 5115 | |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5116 | printf("Successfully wrote recovery file \"%s\". Exiting\n", |
| 5117 | recovery_file); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5118 | |
| 5119 | exit(0); |
| 5120 | } |
| 5121 | |
| 5122 | |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 5123 | void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad) |
| 5124 | { |
| 5125 | int i; |
| 5126 | |
| 5127 | sBlk->fragments = fragments; |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 5128 | sBlk->no_ids = id_count; |
| 5129 | sBlk->inode_table_start = write_inodes(); |
| 5130 | sBlk->directory_table_start = write_directories(); |
| 5131 | sBlk->fragment_table_start = write_fragment_table(); |
| 5132 | sBlk->lookup_table_start = exportable ? write_inode_lookup_table() : |
| 5133 | SQUASHFS_INVALID_BLK; |
| 5134 | sBlk->id_table_start = write_id_table(); |
| 5135 | sBlk->xattr_id_table_start = write_xattrs(); |
| 5136 | |
| 5137 | TRACE("sBlk->inode_table_start 0x%llx\n", sBlk->inode_table_start); |
| 5138 | TRACE("sBlk->directory_table_start 0x%llx\n", |
| 5139 | sBlk->directory_table_start); |
| 5140 | TRACE("sBlk->fragment_table_start 0x%llx\n", sBlk->fragment_table_start); |
| 5141 | if(exportable) |
| 5142 | TRACE("sBlk->lookup_table_start 0x%llx\n", |
| 5143 | sBlk->lookup_table_start); |
| 5144 | |
| 5145 | sBlk->bytes_used = bytes; |
| 5146 | |
| 5147 | sBlk->compression = comp->id; |
| 5148 | |
| 5149 | SQUASHFS_INSWAP_SUPER_BLOCK(sBlk); |
| 5150 | write_destination(fd, SQUASHFS_START, sizeof(*sBlk), sBlk); |
| 5151 | |
| 5152 | if(!nopad && (i = bytes & (4096 - 1))) { |
| 5153 | char temp[4096] = {0}; |
| 5154 | write_destination(fd, bytes, 4096 - i, temp); |
| 5155 | } |
| 5156 | |
| 5157 | close(fd); |
| 5158 | |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 5159 | if(recovery_file) |
| 5160 | unlink(recovery_file); |
| 5161 | |
| 5162 | total_bytes += total_inode_bytes + total_directory_bytes + |
| 5163 | sizeof(struct squashfs_super_block) + total_xattr_bytes; |
| 5164 | |
| 5165 | printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size" |
| 5166 | " %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR, |
| 5167 | SQUASHFS_MINOR, comp->name, block_size); |
| 5168 | printf("\t%s data, %s metadata, %s fragments, %s xattrs\n", |
| 5169 | noD ? "uncompressed" : "compressed", noI ? "uncompressed" : |
| 5170 | "compressed", no_fragments ? "no" : noF ? "uncompressed" : |
| 5171 | "compressed", no_xattrs ? "no" : noX ? "uncompressed" : |
| 5172 | "compressed"); |
| 5173 | printf("\tduplicates are %sremoved\n", duplicate_checking ? "" : |
| 5174 | "not "); |
| 5175 | printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n", bytes / 1024.0, |
| 5176 | bytes / (1024.0 * 1024.0)); |
| 5177 | printf("\t%.2f%% of uncompressed filesystem size (%.2f Kbytes)\n", |
| 5178 | ((float) bytes / total_bytes) * 100.0, total_bytes / 1024.0); |
| 5179 | printf("Inode table size %d bytes (%.2f Kbytes)\n", |
| 5180 | inode_bytes, inode_bytes / 1024.0); |
| 5181 | printf("\t%.2f%% of uncompressed inode table size (%d bytes)\n", |
| 5182 | ((float) inode_bytes / total_inode_bytes) * 100.0, |
| 5183 | total_inode_bytes); |
| 5184 | printf("Directory table size %d bytes (%.2f Kbytes)\n", |
| 5185 | directory_bytes, directory_bytes / 1024.0); |
| 5186 | printf("\t%.2f%% of uncompressed directory table size (%d bytes)\n", |
| 5187 | ((float) directory_bytes / total_directory_bytes) * 100.0, |
| 5188 | total_directory_bytes); |
| 5189 | if(total_xattr_bytes) { |
| 5190 | printf("Xattr table size %d bytes (%.2f Kbytes)\n", |
| 5191 | xattr_bytes, xattr_bytes / 1024.0); |
| 5192 | printf("\t%.2f%% of uncompressed xattr table size (%d bytes)\n", |
| 5193 | ((float) xattr_bytes / total_xattr_bytes) * 100.0, |
| 5194 | total_xattr_bytes); |
| 5195 | } |
| 5196 | if(duplicate_checking) |
| 5197 | printf("Number of duplicate files found %d\n", file_count - |
| 5198 | dup_files); |
| 5199 | else |
| 5200 | printf("No duplicate files removed\n"); |
| 5201 | printf("Number of inodes %d\n", inode_count); |
| 5202 | printf("Number of files %d\n", file_count); |
| 5203 | if(!no_fragments) |
| 5204 | printf("Number of fragments %d\n", fragments); |
| 5205 | printf("Number of symbolic links %d\n", sym_count); |
| 5206 | printf("Number of device nodes %d\n", dev_count); |
| 5207 | printf("Number of fifo nodes %d\n", fifo_count); |
| 5208 | printf("Number of socket nodes %d\n", sock_count); |
| 5209 | printf("Number of directories %d\n", dir_count); |
| 5210 | printf("Number of ids (unique uids + gids) %d\n", id_count); |
| 5211 | printf("Number of uids %d\n", uid_count); |
| 5212 | |
| 5213 | for(i = 0; i < id_count; i++) { |
| 5214 | if(id_table[i]->flags & ISA_UID) { |
| 5215 | struct passwd *user = getpwuid(id_table[i]->id); |
| 5216 | printf("\t%s (%d)\n", user == NULL ? "unknown" : |
| 5217 | user->pw_name, id_table[i]->id); |
| 5218 | } |
| 5219 | } |
| 5220 | |
| 5221 | printf("Number of gids %d\n", guid_count); |
| 5222 | |
| 5223 | for(i = 0; i < id_count; i++) { |
| 5224 | if(id_table[i]->flags & ISA_GID) { |
| 5225 | struct group *group = getgrgid(id_table[i]->id); |
| 5226 | printf("\t%s (%d)\n", group == NULL ? "unknown" : |
| 5227 | group->gr_name, id_table[i]->id); |
| 5228 | } |
| 5229 | } |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 5230 | |
| 5231 | printf("Number of whitelisted (uncompressed) files %d\n", |
| 5232 | whitelisted_count); |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 5236 | int parse_numberll(char *start, long long *res, int size) |
| 5237 | { |
| 5238 | char *end; |
| 5239 | long long number; |
| 5240 | |
| 5241 | errno = 0; /* To distinguish success/failure after call */ |
| 5242 | |
| 5243 | number = strtoll(start, &end, 10); |
| 5244 | |
| 5245 | /* |
| 5246 | * check for strtoll underflow or overflow in conversion, and other |
| 5247 | * errors. |
| 5248 | */ |
| 5249 | if((errno == ERANGE && (number == LLONG_MIN || number == LLONG_MAX)) || |
| 5250 | (errno != 0 && number == 0)) |
| 5251 | return 0; |
| 5252 | |
| 5253 | /* reject negative numbers as invalid */ |
| 5254 | if(number < 0) |
| 5255 | return 0; |
| 5256 | |
| 5257 | if(size) { |
| 5258 | /* |
| 5259 | * Check for multiplier and trailing junk. |
| 5260 | * But first check that a number exists before the |
| 5261 | * multiplier |
| 5262 | */ |
| 5263 | if(end == start) |
| 5264 | return 0; |
| 5265 | |
| 5266 | switch(end[0]) { |
| 5267 | case 'g': |
| 5268 | case 'G': |
| 5269 | if(multiply_overflowll(number, 1073741824)) |
| 5270 | return 0; |
| 5271 | number *= 1073741824; |
| 5272 | |
| 5273 | if(end[1] != '\0') |
| 5274 | /* trailing junk after multiplier, but |
| 5275 | * allow it to be "bytes" */ |
| 5276 | if(strcmp(end + 1, "bytes")) |
| 5277 | return 0; |
| 5278 | |
| 5279 | break; |
| 5280 | case 'm': |
| 5281 | case 'M': |
| 5282 | if(multiply_overflowll(number, 1048576)) |
| 5283 | return 0; |
| 5284 | number *= 1048576; |
| 5285 | |
| 5286 | if(end[1] != '\0') |
| 5287 | /* trailing junk after multiplier, but |
| 5288 | * allow it to be "bytes" */ |
| 5289 | if(strcmp(end + 1, "bytes")) |
| 5290 | return 0; |
| 5291 | |
| 5292 | break; |
| 5293 | case 'k': |
| 5294 | case 'K': |
| 5295 | if(multiply_overflowll(number, 1024)) |
| 5296 | return 0; |
| 5297 | number *= 1024; |
| 5298 | |
| 5299 | if(end[1] != '\0') |
| 5300 | /* trailing junk after multiplier, but |
| 5301 | * allow it to be "bytes" */ |
| 5302 | if(strcmp(end + 1, "bytes")) |
| 5303 | return 0; |
| 5304 | |
| 5305 | break; |
| 5306 | case '\0': |
| 5307 | break; |
| 5308 | default: |
| 5309 | /* trailing junk after number */ |
| 5310 | return 0; |
| 5311 | } |
| 5312 | } else if(end[0] != '\0') |
| 5313 | /* trailing junk after number */ |
| 5314 | return 0; |
| 5315 | |
| 5316 | *res = number; |
| 5317 | return 1; |
| 5318 | } |
| 5319 | |
| 5320 | |
| 5321 | int parse_number(char *start, int *res, int size) |
| 5322 | { |
| 5323 | long long number; |
| 5324 | |
| 5325 | if(!parse_numberll(start, &number, size)) |
| 5326 | return 0; |
| 5327 | |
| 5328 | /* check if long result will overflow signed int */ |
| 5329 | if(number > INT_MAX) |
| 5330 | return 0; |
| 5331 | |
| 5332 | *res = (int) number; |
| 5333 | return 1; |
| 5334 | } |
| 5335 | |
| 5336 | |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5337 | int parse_num(char *arg, int *res) |
| 5338 | { |
| 5339 | return parse_number(arg, res, 0); |
| 5340 | } |
| 5341 | |
| 5342 | |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 5343 | int parse_ugid_map(char *map_str, |
| 5344 | struct ugid_map_entry ugid_mapping[UGID_ENTRIES], |
| 5345 | unsigned int *ugid_map_count) |
| 5346 | { |
| 5347 | char *line_state, *token_state; |
| 5348 | char *line, *line_str, *token, *token_str; |
| 5349 | long long numbers[3]; |
| 5350 | int i; |
| 5351 | |
| 5352 | for (*ugid_map_count = 0, line_str = map_str;; |
| 5353 | ++*ugid_map_count, line_str = NULL) { |
| 5354 | line = strtok_r(line_str, "\n", &line_state); |
| 5355 | if (line == NULL) |
| 5356 | break; |
| 5357 | ERROR("line: %s\n", line); |
| 5358 | if (*ugid_map_count >= UGID_ENTRIES) { |
| 5359 | ERROR("Too many entries for u/gid mapping\n"); |
| 5360 | return -1; |
| 5361 | } |
| 5362 | |
| 5363 | for (i = 0, token_str = line; i < 3; i++, token_str = NULL) { |
| 5364 | token = strtok_r(token_str, " ", &token_state); |
| 5365 | ERROR("token: %d, %s\n", i, token); |
| 5366 | if (token == NULL || |
| 5367 | !parse_numberll(token, &numbers[i], 0) || |
| 5368 | numbers[i] < 0 || numbers[i] > ULONG_MAX) { |
| 5369 | ERROR("Malformed u/gid mapping line1\n"); |
| 5370 | return -1; |
| 5371 | } |
| 5372 | } |
| 5373 | |
| 5374 | if (numbers[0] + numbers[2] > ULONG_MAX) { |
| 5375 | ERROR("u/gid mapping overflow\n"); |
| 5376 | return -1; |
| 5377 | } |
| 5378 | |
| 5379 | if (numbers[1] + numbers[2] > ULONG_MAX) { |
| 5380 | ERROR("u/gid mapping overflow\n"); |
| 5381 | return -1; |
| 5382 | } |
| 5383 | |
| 5384 | if (strtok_r(NULL, " ", &token_state) != NULL) { |
| 5385 | ERROR("Malformed u/gid mapping line2\n"); |
| 5386 | return -1; |
| 5387 | } |
| 5388 | |
| 5389 | ugid_mapping[*ugid_map_count].child_id = |
| 5390 | (unsigned int)numbers[0]; |
| 5391 | ugid_mapping[*ugid_map_count].parent_id = |
| 5392 | (unsigned int)numbers[1]; |
| 5393 | ugid_mapping[*ugid_map_count].length = (unsigned int)numbers[2]; |
| 5394 | } |
| 5395 | |
| 5396 | return 0; |
| 5397 | } |
| 5398 | |
| 5399 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5400 | int get_physical_memory() |
| 5401 | { |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 5402 | int phys_mem; |
| 5403 | #ifndef linux |
| 5404 | #ifdef HW_MEMSIZE |
| 5405 | #define SYSCTL_PHYSMEM HW_MEMSIZE |
| 5406 | #elif defined(HW_PHYSMEM64) |
| 5407 | #define SYSCTL_PHYSMEM HW_PHYSMEM64 |
| 5408 | #else |
| 5409 | #define SYSCTL_PHYSMEM HW_PHYSMEM |
| 5410 | #endif |
| 5411 | |
| 5412 | int mib[2]; |
| 5413 | uint64_t sysctl_physmem = 0; |
| 5414 | size_t sysctl_len = sizeof(sysctl_physmem); |
| 5415 | |
| 5416 | mib[0] = CTL_HW; |
| 5417 | mib[1] = SYSCTL_PHYSMEM; |
| 5418 | |
| 5419 | if(sysctl(mib, 2, &sysctl_physmem, &sysctl_len, NULL, 0) == 0) { |
| 5420 | /* some systems use 32-bit values, work with what we're given */ |
| 5421 | if (sysctl_len == 4) |
| 5422 | sysctl_physmem = *(uint32_t*)&sysctl_physmem; |
| 5423 | phys_mem = sysctl_physmem >> 20; |
| 5424 | } else { |
| 5425 | ERROR_START("Failed to get amount of available " |
| 5426 | "memory."); |
| 5427 | ERROR_EXIT(" Defaulting to least viable amount\n"); |
| 5428 | phys_mem = SQUASHFS_LOWMEM; |
| 5429 | } |
| 5430 | #undef SYSCTL_PHYSMEM |
| 5431 | #else |
| 5432 | /* Long longs are used here because with PAE, a 32-bit |
| 5433 | machine can have more than 4GB of physical memory */ |
| 5434 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5435 | long long num_pages = sysconf(_SC_PHYS_PAGES); |
| 5436 | long long page_size = sysconf(_SC_PAGESIZE); |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 5437 | phys_mem = num_pages * page_size >> 20; |
Phillip Lougher | 97ad5e8 | 2014-06-16 02:21:33 +0100 | [diff] [blame] | 5438 | if(num_pages == -1 || page_size == -1) |
| 5439 | return 0; |
| 5440 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 5441 | #endif |
| 5442 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5443 | if(phys_mem < SQUASHFS_LOWMEM) |
| 5444 | BAD_ERROR("Mksquashfs requires more physical memory than is " |
| 5445 | "available!\n"); |
| 5446 | |
Phillip Lougher | 78784bb | 2014-04-28 04:08:40 +0100 | [diff] [blame] | 5447 | return phys_mem; |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5448 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5449 | |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5450 | |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 5451 | void check_usable_phys_mem(int total_mem) |
| 5452 | { |
| 5453 | /* |
| 5454 | * We want to allow users to use as much of their physical |
| 5455 | * memory as they wish. However, for practical reasons there are |
| 5456 | * limits which need to be imposed, to protect users from themselves |
| 5457 | * and to prevent people from using Mksquashfs as a DOS attack by using |
| 5458 | * all physical memory. Mksquashfs uses memory to cache data from disk |
| 5459 | * to optimise performance. It is pointless to ask it to use more |
| 5460 | * than 75% of physical memory, as this causes thrashing and it is thus |
| 5461 | * self-defeating. |
| 5462 | */ |
| 5463 | int mem = get_physical_memory(); |
| 5464 | |
| 5465 | mem = (mem >> 1) + (mem >> 2); /* 75% */ |
| 5466 | |
Phillip Lougher | 97ad5e8 | 2014-06-16 02:21:33 +0100 | [diff] [blame] | 5467 | if(total_mem > mem && mem) { |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 5468 | ERROR("Total memory requested is more than 75%% of physical " |
| 5469 | "memory.\n"); |
| 5470 | ERROR("Mksquashfs uses memory to cache data from disk to " |
| 5471 | "optimise performance.\n"); |
| 5472 | ERROR("It is pointless to ask it to use more than this amount " |
| 5473 | "of memory, as this\n"); |
| 5474 | ERROR("causes thrashing and it is thus self-defeating.\n"); |
| 5475 | BAD_ERROR("Requested memory size too large\n"); |
| 5476 | } |
| 5477 | |
| 5478 | if(sizeof(void *) == 4 && total_mem > 2048) { |
| 5479 | /* |
| 5480 | * If we're running on a kernel with PAE or on a 64-bit kernel, |
| 5481 | * then the 75% physical memory limit can still easily exceed |
| 5482 | * the addressable memory by this process. |
| 5483 | * |
| 5484 | * Due to the typical kernel/user-space split (1GB/3GB, or |
| 5485 | * 2GB/2GB), we have to conservatively assume the 32-bit |
| 5486 | * processes can only address 2-3GB. So refuse if the user |
| 5487 | * tries to allocate more than 2GB. |
| 5488 | */ |
| 5489 | ERROR("Total memory requested may exceed maximum " |
| 5490 | "addressable memory by this process\n"); |
| 5491 | BAD_ERROR("Requested memory size too large\n"); |
| 5492 | } |
| 5493 | } |
| 5494 | |
| 5495 | |
| 5496 | int get_default_phys_mem() |
| 5497 | { |
Phillip Lougher | 97ad5e8 | 2014-06-16 02:21:33 +0100 | [diff] [blame] | 5498 | /* |
| 5499 | * get_physical_memory() relies on /proc being mounted. |
| 5500 | * If it fails, issue a warning, and use |
| 5501 | * SQUASHFS_LOWMEM / SQUASHFS_TAKE as default, |
| 5502 | * and allow a larger value to be set with -mem. |
| 5503 | */ |
| 5504 | int mem = get_physical_memory(); |
| 5505 | |
| 5506 | if(mem == 0) { |
| 5507 | mem = SQUASHFS_LOWMEM / SQUASHFS_TAKE; |
| 5508 | |
| 5509 | ERROR("Warning: Cannot get size of physical memory, probably " |
| 5510 | "because /proc is missing.\n"); |
| 5511 | ERROR("Warning: Defaulting to minimal use of %d Mbytes, use " |
| 5512 | "-mem to set a better value,\n", mem); |
| 5513 | ERROR("Warning: or fix /proc.\n"); |
| 5514 | } else |
| 5515 | mem /= SQUASHFS_TAKE; |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 5516 | |
| 5517 | if(sizeof(void *) == 4 && mem > 640) { |
| 5518 | /* |
| 5519 | * If we're running on a kernel with PAE or on a 64-bit kernel, |
| 5520 | * the default memory usage can exceed the addressable |
| 5521 | * memory by this process. |
| 5522 | * Due to the typical kernel/user-space split (1GB/3GB, or |
| 5523 | * 2GB/2GB), we have to conservatively assume the 32-bit |
| 5524 | * processes can only address 2-3GB. So limit the default |
| 5525 | * usage to 640M, which gives room for other data. |
| 5526 | */ |
| 5527 | mem = 640; |
| 5528 | } |
| 5529 | |
| 5530 | return mem; |
| 5531 | } |
| 5532 | |
| 5533 | |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5534 | void calculate_queue_sizes(int mem, int *readq, int *fragq, int *bwriteq, |
| 5535 | int *fwriteq) |
| 5536 | { |
| 5537 | *readq = mem / SQUASHFS_READQ_MEM; |
| 5538 | *bwriteq = mem / SQUASHFS_BWRITEQ_MEM; |
| 5539 | *fwriteq = mem / SQUASHFS_FWRITEQ_MEM; |
| 5540 | *fragq = mem - *readq - *bwriteq - *fwriteq; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5541 | } |
| 5542 | |
| 5543 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5544 | #define VERSION() \ |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 5545 | printf("mksquashfs version 4.3-git (2014/09/12)\n");\ |
| 5546 | printf("copyright (C) 2014 Phillip Lougher "\ |
Phillip Lougher | 83d42a3 | 2012-10-31 23:42:22 +0000 | [diff] [blame] | 5547 | "<phillip@squashfs.org.uk>\n\n"); \ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5548 | printf("This program is free software; you can redistribute it and/or"\ |
| 5549 | "\n");\ |
| 5550 | printf("modify it under the terms of the GNU General Public License"\ |
| 5551 | "\n");\ |
| 5552 | printf("as published by the Free Software Foundation; either version "\ |
| 5553 | "2,\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5554 | printf("or (at your option) any later version.\n\n");\ |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 5555 | printf("This program is distributed in the hope that it will be "\ |
| 5556 | "useful,\n");\ |
| 5557 | printf("but WITHOUT ANY WARRANTY; without even the implied warranty "\ |
| 5558 | "of\n");\ |
| 5559 | printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"\ |
| 5560 | "\n");\ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5561 | printf("GNU General Public License for more details.\n"); |
| 5562 | int main(int argc, char *argv[]) |
| 5563 | { |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 5564 | struct stat buf, source_buf; |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 5565 | int res, i; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5566 | char *b, *root_name = NULL; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5567 | int keep_as_directory = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5568 | squashfs_inode inode; |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5569 | int readq; |
| 5570 | int fragq; |
| 5571 | int bwriteq; |
| 5572 | int fwriteq; |
Phillip Lougher | 285a2fd | 2014-06-10 21:51:52 +0100 | [diff] [blame] | 5573 | int total_mem = get_default_phys_mem(); |
Phillip Lougher | 569a963 | 2013-04-22 03:30:30 +0100 | [diff] [blame] | 5574 | int progress = TRUE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5575 | int force_progress = FALSE; |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 5576 | struct file_buffer **fragment = NULL; |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 5577 | /* ANDROID CHANGES START*/ |
| 5578 | #ifdef ANDROID |
| 5579 | const char *fs_config_file = NULL; |
| 5580 | #endif |
| 5581 | /* ANDROID CHANGES END */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5582 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5583 | if(argc > 1 && strcmp(argv[1], "-version") == 0) { |
| 5584 | VERSION(); |
| 5585 | exit(0); |
| 5586 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5587 | |
| 5588 | block_log = slog(block_size); |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5589 | calculate_queue_sizes(total_mem, &readq, &fragq, &bwriteq, &fwriteq); |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5590 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5591 | for(i = 1; i < argc && argv[i][0] != '-'; i++); |
| 5592 | if(i < 3) |
| 5593 | goto printOptions; |
| 5594 | source_path = argv + 1; |
| 5595 | source = i - 2; |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5596 | |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 5597 | /* |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5598 | * Scan the command line for -comp xxx option, this is to ensure |
| 5599 | * any -X compressor specific options are passed to the |
| 5600 | * correct compressor |
plougher | 4da4bd4 | 2010-11-21 05:01:54 +0000 | [diff] [blame] | 5601 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5602 | for(; i < argc; i++) { |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5603 | struct compressor *prev_comp = comp; |
| 5604 | |
| 5605 | if(strcmp(argv[i], "-comp") == 0) { |
| 5606 | if(++i == argc) { |
| 5607 | ERROR("%s: -comp missing compression type\n", |
| 5608 | argv[0]); |
| 5609 | exit(1); |
| 5610 | } |
| 5611 | comp = lookup_compressor(argv[i]); |
| 5612 | if(!comp->supported) { |
| 5613 | ERROR("%s: Compressor \"%s\" is not supported!" |
| 5614 | "\n", argv[0], argv[i]); |
| 5615 | ERROR("%s: Compressors available:\n", argv[0]); |
| 5616 | display_compressors("", COMP_DEFAULT); |
| 5617 | exit(1); |
| 5618 | } |
| 5619 | if(prev_comp != NULL && prev_comp != comp) { |
| 5620 | ERROR("%s: -comp multiple conflicting -comp" |
| 5621 | " options specified on command line" |
| 5622 | ", previously %s, now %s\n", argv[0], |
| 5623 | prev_comp->name, comp->name); |
| 5624 | exit(1); |
| 5625 | } |
| 5626 | compressor_opt_parsed = 1; |
| 5627 | |
| 5628 | } else if(strcmp(argv[i], "-e") == 0) |
| 5629 | break; |
| 5630 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
| 5631 | strcmp(argv[i], "-ef") == 0 || |
| 5632 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | 089a81a | 2014-08-24 03:46:46 +0100 | [diff] [blame] | 5633 | strcmp(argv[i], "-vaf") == 0 || |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5634 | strcmp(argv[i], "-comp") == 0) |
| 5635 | i++; |
| 5636 | } |
| 5637 | |
| 5638 | /* |
| 5639 | * if no -comp option specified lookup default compressor. Note the |
| 5640 | * Makefile ensures the default compressor has been built, and so we |
| 5641 | * don't need to to check for failure here |
| 5642 | */ |
| 5643 | if(comp == NULL) |
| 5644 | comp = lookup_compressor(COMP_DEFAULT); |
| 5645 | |
| 5646 | for(i = source + 2; i < argc; i++) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 5647 | if(strcmp(argv[i], "-action") == 0 || |
| 5648 | strcmp(argv[i], "-a") ==0) { |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5649 | if(++i == argc) { |
Phillip Lougher | 9523b04 | 2012-10-27 01:48:48 +0100 | [diff] [blame] | 5650 | ERROR("%s: %s missing action\n", |
| 5651 | argv[0], argv[i - 1]); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5652 | exit(1); |
| 5653 | } |
Phillip Lougher | a228982 | 2014-08-27 05:40:39 +0100 | [diff] [blame] | 5654 | res = parse_action(argv[i], ACTION_LOG_NONE); |
Phillip Lougher | 06052c6 | 2014-08-18 03:03:14 +0100 | [diff] [blame] | 5655 | if(res == 0) |
| 5656 | exit(1); |
| 5657 | |
Phillip Lougher | b670481 | 2014-08-27 06:14:38 +0100 | [diff] [blame] | 5658 | } else if(strcmp(argv[i], "-verbose-action") == 0 || |
Phillip Lougher | 06052c6 | 2014-08-18 03:03:14 +0100 | [diff] [blame] | 5659 | strcmp(argv[i], "-va") ==0) { |
| 5660 | if(++i == argc) { |
| 5661 | ERROR("%s: %s missing action\n", |
| 5662 | argv[0], argv[i - 1]); |
| 5663 | exit(1); |
| 5664 | } |
Phillip Lougher | a228982 | 2014-08-27 05:40:39 +0100 | [diff] [blame] | 5665 | res = parse_action(argv[i], ACTION_LOG_VERBOSE); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 5666 | if(res == 0) |
| 5667 | exit(1); |
| 5668 | |
Phillip Lougher | b670481 | 2014-08-27 06:14:38 +0100 | [diff] [blame] | 5669 | } else if(strcmp(argv[i], "-true-action") == 0 || |
Phillip Lougher | 33e5d11 | 2014-08-27 05:50:21 +0100 | [diff] [blame] | 5670 | strcmp(argv[i], "-ta") ==0) { |
| 5671 | if(++i == argc) { |
| 5672 | ERROR("%s: %s missing action\n", |
| 5673 | argv[0], argv[i - 1]); |
| 5674 | exit(1); |
| 5675 | } |
| 5676 | res = parse_action(argv[i], ACTION_LOG_TRUE); |
| 5677 | if(res == 0) |
| 5678 | exit(1); |
| 5679 | |
Phillip Lougher | b670481 | 2014-08-27 06:14:38 +0100 | [diff] [blame] | 5680 | } else if(strcmp(argv[i], "-false-action") == 0 || |
Phillip Lougher | fcfaf0a | 2014-08-27 05:52:20 +0100 | [diff] [blame] | 5681 | strcmp(argv[i], "-fa") ==0) { |
| 5682 | if(++i == argc) { |
| 5683 | ERROR("%s: %s missing action\n", |
| 5684 | argv[0], argv[i - 1]); |
| 5685 | exit(1); |
| 5686 | } |
| 5687 | res = parse_action(argv[i], ACTION_LOG_FALSE); |
| 5688 | if(res == 0) |
| 5689 | exit(1); |
| 5690 | |
Phillip Lougher | b670481 | 2014-08-27 06:14:38 +0100 | [diff] [blame] | 5691 | } else if(strcmp(argv[i], "-action-file") == 0 || |
Phillip Lougher | bd43226 | 2014-08-27 05:56:13 +0100 | [diff] [blame] | 5692 | strcmp(argv[i], "-af") ==0) { |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5693 | if(++i == argc) { |
Phillip Lougher | bd43226 | 2014-08-27 05:56:13 +0100 | [diff] [blame] | 5694 | ERROR("%s: %s missing filename\n", argv[0], |
| 5695 | argv[i - 1]); |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5696 | exit(1); |
| 5697 | } |
Phillip Lougher | b7c5da7 | 2014-08-27 06:01:10 +0100 | [diff] [blame] | 5698 | if(read_action_file(argv[i], ACTION_LOG_NONE) == FALSE) |
Phillip Lougher | 089a81a | 2014-08-24 03:46:46 +0100 | [diff] [blame] | 5699 | exit(1); |
| 5700 | |
Phillip Lougher | b670481 | 2014-08-27 06:14:38 +0100 | [diff] [blame] | 5701 | } else if(strcmp(argv[i], "-verbose-action-file") == 0 || |
Phillip Lougher | dfddb9b | 2014-08-27 05:58:31 +0100 | [diff] [blame] | 5702 | strcmp(argv[i], "-vaf") ==0) { |
Phillip Lougher | 089a81a | 2014-08-24 03:46:46 +0100 | [diff] [blame] | 5703 | if(++i == argc) { |
Phillip Lougher | dfddb9b | 2014-08-27 05:58:31 +0100 | [diff] [blame] | 5704 | ERROR("%s: %s missing filename\n", argv[0], |
| 5705 | argv[i - 1]); |
Phillip Lougher | 089a81a | 2014-08-24 03:46:46 +0100 | [diff] [blame] | 5706 | exit(1); |
| 5707 | } |
Phillip Lougher | b7c5da7 | 2014-08-27 06:01:10 +0100 | [diff] [blame] | 5708 | if(read_action_file(argv[i], ACTION_LOG_VERBOSE) == FALSE) |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 5709 | exit(1); |
| 5710 | |
Phillip Lougher | 3ead06b | 2014-08-27 06:11:41 +0100 | [diff] [blame] | 5711 | } else if(strcmp(argv[i], "-true-action-file") == 0 || |
Phillip Lougher | a5541d8 | 2014-08-27 06:09:30 +0100 | [diff] [blame] | 5712 | strcmp(argv[i], "-taf") ==0) { |
| 5713 | if(++i == argc) { |
| 5714 | ERROR("%s: %s missing filename\n", argv[0], |
| 5715 | argv[i - 1]); |
| 5716 | exit(1); |
| 5717 | } |
| 5718 | if(read_action_file(argv[i], ACTION_LOG_TRUE) == FALSE) |
| 5719 | exit(1); |
| 5720 | |
Phillip Lougher | 35a00de | 2014-08-27 06:32:11 +0100 | [diff] [blame] | 5721 | } else if(strcmp(argv[i], "-false-action-file") == 0 || |
| 5722 | strcmp(argv[i], "-faf") ==0) { |
| 5723 | if(++i == argc) { |
| 5724 | ERROR("%s: %s missing filename\n", argv[0], |
| 5725 | argv[i - 1]); |
| 5726 | exit(1); |
| 5727 | } |
| 5728 | if(read_action_file(argv[i], ACTION_LOG_FALSE) == FALSE) |
| 5729 | exit(1); |
| 5730 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5731 | } else if(strcmp(argv[i], "-comp") == 0) |
| 5732 | /* parsed previously */ |
| 5733 | i++; |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 5734 | |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5735 | else if(strncmp(argv[i], "-X", 2) == 0) { |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 5736 | int args; |
| 5737 | |
| 5738 | if(strcmp(argv[i] + 2, "help") == 0) |
| 5739 | goto print_compressor_options; |
| 5740 | |
| 5741 | args = compressor_options(comp, argv + i, argc - i); |
plougher | d886567 | 2010-11-27 05:05:49 +0000 | [diff] [blame] | 5742 | if(args < 0) { |
| 5743 | if(args == -1) { |
| 5744 | ERROR("%s: Unrecognised compressor" |
| 5745 | " option %s\n", argv[0], |
| 5746 | argv[i]); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5747 | if(!compressor_opt_parsed) |
| 5748 | ERROR("%s: Did you forget to" |
| 5749 | " specify -comp?\n", |
| 5750 | argv[0]); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 5751 | print_compressor_options: |
Phillip Lougher | b1c3b6a | 2014-01-06 05:30:00 +0000 | [diff] [blame] | 5752 | ERROR("%s: selected compressor \"%s\"" |
| 5753 | ". Options supported: %s\n", |
| 5754 | argv[0], comp->name, |
| 5755 | comp->usage ? "" : "none"); |
| 5756 | if(comp->usage) |
| 5757 | comp->usage(); |
Phillip Lougher | 1f6c740 | 2014-01-06 04:16:48 +0000 | [diff] [blame] | 5758 | } |
plougher | b5576ea | 2010-11-22 01:06:53 +0000 | [diff] [blame] | 5759 | exit(1); |
| 5760 | } |
| 5761 | i += args; |
| 5762 | |
plougher | ae9dcd8 | 2009-08-01 02:59:38 +0000 | [diff] [blame] | 5763 | } else if(strcmp(argv[i], "-pf") == 0) { |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5764 | if(++i == argc) { |
| 5765 | ERROR("%s: -pf missing filename\n", argv[0]); |
| 5766 | exit(1); |
| 5767 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 5768 | if(read_pseudo_file(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5769 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5770 | } else if(strcmp(argv[i], "-p") == 0) { |
| 5771 | if(++i == argc) { |
| 5772 | ERROR("%s: -p missing pseudo file definition\n", |
| 5773 | argv[0]); |
| 5774 | exit(1); |
| 5775 | } |
Phillip Lougher | 5ef2eba | 2012-12-24 20:33:13 +0000 | [diff] [blame] | 5776 | if(read_pseudo_def(argv[i]) == FALSE) |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5777 | exit(1); |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 5778 | } else if(strcmp(argv[i], "-recover") == 0) { |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5779 | if(++i == argc) { |
plougher | b360412 | 2009-03-30 02:07:20 +0000 | [diff] [blame] | 5780 | ERROR("%s: -recover missing recovery file\n", |
| 5781 | argv[0]); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 5782 | exit(1); |
| 5783 | } |
| 5784 | read_recovery_data(argv[i], argv[source + 1]); |
| 5785 | } else if(strcmp(argv[i], "-no-recovery") == 0) |
| 5786 | recover = FALSE; |
| 5787 | else if(strcmp(argv[i], "-wildcards") == 0) { |
plougher | 934a9ed | 2007-10-19 00:21:10 +0000 | [diff] [blame] | 5788 | old_exclude = FALSE; |
| 5789 | use_regex = FALSE; |
| 5790 | } else if(strcmp(argv[i], "-regex") == 0) { |
| 5791 | old_exclude = FALSE; |
| 5792 | use_regex = TRUE; |
| 5793 | } else if(strcmp(argv[i], "-no-sparse") == 0) |
plougher | 1f54edc | 2007-08-12 23:13:36 +0000 | [diff] [blame] | 5794 | sparse_files = FALSE; |
| 5795 | else if(strcmp(argv[i], "-no-progress") == 0) |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 5796 | progress = FALSE; |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 5797 | else if(strcmp(argv[i], "-progress") == 0) |
| 5798 | force_progress = TRUE; |
plougher | 02bc3bc | 2007-02-25 12:12:01 +0000 | [diff] [blame] | 5799 | else if(strcmp(argv[i], "-no-exports") == 0) |
| 5800 | exportable = FALSE; |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5801 | else if(strcmp(argv[i], "-processors") == 0) { |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5802 | if((++i == argc) || !parse_num(argv[i], &processors)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5803 | ERROR("%s: -processors missing or invalid " |
| 5804 | "processor number\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5805 | exit(1); |
| 5806 | } |
| 5807 | if(processors < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5808 | ERROR("%s: -processors should be 1 or larger\n", |
| 5809 | argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5810 | exit(1); |
| 5811 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5812 | } else if(strcmp(argv[i], "-read-queue") == 0) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5813 | if((++i == argc) || !parse_num(argv[i], &readq)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5814 | ERROR("%s: -read-queue missing or invalid " |
| 5815 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5816 | exit(1); |
| 5817 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5818 | if(readq < 1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5819 | ERROR("%s: -read-queue should be 1 megabyte or " |
| 5820 | "larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5821 | exit(1); |
| 5822 | } |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 5823 | } else if(strcmp(argv[i], "-write-queue") == 0) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5824 | if((++i == argc) || !parse_num(argv[i], &bwriteq)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5825 | ERROR("%s: -write-queue missing or invalid " |
| 5826 | "queue size\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5827 | exit(1); |
| 5828 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5829 | if(bwriteq < 2) { |
| 5830 | ERROR("%s: -write-queue should be 2 megabytes " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5831 | "or larger\n", argv[0]); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5832 | exit(1); |
| 5833 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5834 | fwriteq = bwriteq >> 1; |
| 5835 | bwriteq -= fwriteq; |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5836 | } else if(strcmp(argv[i], "-fragment-queue") == 0) { |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5837 | if((++i == argc) || !parse_num(argv[i], &fragq)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5838 | ERROR("%s: -fragment-queue missing or invalid " |
| 5839 | "queue size\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5840 | exit(1); |
| 5841 | } |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 5842 | if(fragq < 1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5843 | ERROR("%s: -fragment-queue should be 1 " |
| 5844 | "megabyte or larger\n", argv[0]); |
plougher | 217bad8 | 2008-04-05 11:36:41 +0000 | [diff] [blame] | 5845 | exit(1); |
| 5846 | } |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5847 | } else if(strcmp(argv[i], "-mem") == 0) { |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 5848 | long long number; |
| 5849 | |
| 5850 | if((++i == argc) || |
| 5851 | !parse_numberll(argv[i], &number, 1)) { |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5852 | ERROR("%s: -mem missing or invalid mem size\n", |
| 5853 | argv[0]); |
| 5854 | exit(1); |
| 5855 | } |
Phillip Lougher | 368becc | 2014-06-11 04:51:37 +0100 | [diff] [blame] | 5856 | |
| 5857 | /* |
| 5858 | * convert from bytes to Mbytes, ensuring the value |
| 5859 | * does not overflow a signed int |
| 5860 | */ |
| 5861 | if(number >= (1LL << 51)) { |
| 5862 | ERROR("%s: -mem invalid mem size\n", argv[0]); |
| 5863 | exit(1); |
| 5864 | } |
| 5865 | |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 5866 | total_mem = number / 1048576; |
Phillip Lougher | 07def8b | 2014-05-11 19:56:11 +0100 | [diff] [blame] | 5867 | if(total_mem < (SQUASHFS_LOWMEM / SQUASHFS_TAKE)) { |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5868 | ERROR("%s: -mem should be %d Mbytes or " |
Phillip Lougher | 07def8b | 2014-05-11 19:56:11 +0100 | [diff] [blame] | 5869 | "larger\n", argv[0], |
| 5870 | SQUASHFS_LOWMEM / SQUASHFS_TAKE); |
Phillip Lougher | 48d42fc | 2014-04-24 03:04:44 +0100 | [diff] [blame] | 5871 | exit(1); |
| 5872 | } |
| 5873 | calculate_queue_sizes(total_mem, &readq, &fragq, |
| 5874 | &bwriteq, &fwriteq); |
plougher | 5507dd9 | 2006-11-06 00:43:10 +0000 | [diff] [blame] | 5875 | } else if(strcmp(argv[i], "-b") == 0) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5876 | if(++i == argc) { |
| 5877 | ERROR("%s: -b missing block size\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5878 | exit(1); |
| 5879 | } |
Phillip Lougher | 94c1fe0 | 2012-11-28 03:32:36 +0000 | [diff] [blame] | 5880 | if(!parse_number(argv[i], &block_size, 1)) { |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 5881 | ERROR("%s: -b invalid block size\n", argv[0]); |
| 5882 | exit(1); |
| 5883 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5884 | if((block_log = slog(block_size)) == 0) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5885 | ERROR("%s: -b block size not power of two or " |
| 5886 | "not between 4096 and 1Mbyte\n", |
| 5887 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5888 | exit(1); |
| 5889 | } |
| 5890 | } else if(strcmp(argv[i], "-ef") == 0) { |
| 5891 | if(++i == argc) { |
| 5892 | ERROR("%s: -ef missing filename\n", argv[0]); |
| 5893 | exit(1); |
| 5894 | } |
plougher | 9b5bf8c | 2006-03-20 18:43:33 +0000 | [diff] [blame] | 5895 | } else if(strcmp(argv[i], "-no-duplicates") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5896 | duplicate_checking = FALSE; |
| 5897 | |
| 5898 | else if(strcmp(argv[i], "-no-fragments") == 0) |
| 5899 | no_fragments = TRUE; |
| 5900 | |
| 5901 | else if(strcmp(argv[i], "-always-use-fragments") == 0) |
| 5902 | always_use_fragments = TRUE; |
| 5903 | |
| 5904 | else if(strcmp(argv[i], "-sort") == 0) { |
| 5905 | if(++i == argc) { |
| 5906 | ERROR("%s: -sort missing filename\n", argv[0]); |
| 5907 | exit(1); |
| 5908 | } |
| 5909 | } else if(strcmp(argv[i], "-all-root") == 0 || |
| 5910 | strcmp(argv[i], "-root-owned") == 0) |
| 5911 | global_uid = global_gid = 0; |
| 5912 | |
| 5913 | else if(strcmp(argv[i], "-force-uid") == 0) { |
| 5914 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5915 | ERROR("%s: -force-uid missing uid or user\n", |
| 5916 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5917 | exit(1); |
| 5918 | } |
| 5919 | if((global_uid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5920 | if(global_uid < 0 || global_uid > |
| 5921 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5922 | ERROR("%s: -force-uid uid out of range" |
| 5923 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5924 | exit(1); |
| 5925 | } |
| 5926 | } else { |
| 5927 | struct passwd *uid = getpwnam(argv[i]); |
| 5928 | if(uid) |
| 5929 | global_uid = uid->pw_uid; |
| 5930 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5931 | ERROR("%s: -force-uid invalid uid or " |
| 5932 | "unknown user\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5933 | exit(1); |
| 5934 | } |
| 5935 | } |
| 5936 | } else if(strcmp(argv[i], "-force-gid") == 0) { |
| 5937 | if(++i == argc) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5938 | ERROR("%s: -force-gid missing gid or group\n", |
| 5939 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5940 | exit(1); |
| 5941 | } |
| 5942 | if((global_gid = strtoll(argv[i], &b, 10)), *b =='\0') { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5943 | if(global_gid < 0 || global_gid > |
| 5944 | (((long long) 1 << 32) - 1)) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 5945 | ERROR("%s: -force-gid gid out of range" |
| 5946 | "\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5947 | exit(1); |
| 5948 | } |
| 5949 | } else { |
| 5950 | struct group *gid = getgrnam(argv[i]); |
| 5951 | if(gid) |
| 5952 | global_gid = gid->gr_gid; |
| 5953 | else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 5954 | ERROR("%s: -force-gid invalid gid or " |
| 5955 | "unknown group\n", argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 5956 | exit(1); |
| 5957 | } |
| 5958 | } |
| 5959 | } else if(strcmp(argv[i], "-noI") == 0 || |
| 5960 | strcmp(argv[i], "-noInodeCompression") == 0) |
| 5961 | noI = TRUE; |
| 5962 | |
| 5963 | else if(strcmp(argv[i], "-noD") == 0 || |
| 5964 | strcmp(argv[i], "-noDataCompression") == 0) |
| 5965 | noD = TRUE; |
| 5966 | |
| 5967 | else if(strcmp(argv[i], "-noF") == 0 || |
| 5968 | strcmp(argv[i], "-noFragmentCompression") == 0) |
| 5969 | noF = TRUE; |
| 5970 | |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 5971 | else if(strcmp(argv[i], "-noX") == 0 || |
| 5972 | strcmp(argv[i], "-noXattrCompression") == 0) |
| 5973 | noX = TRUE; |
| 5974 | |
plougher | ce564c6 | 2010-05-19 03:01:49 +0000 | [diff] [blame] | 5975 | else if(strcmp(argv[i], "-no-xattrs") == 0) |
| 5976 | no_xattrs = TRUE; |
plougher | 6d89ac2 | 2010-05-19 02:59:23 +0000 | [diff] [blame] | 5977 | |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 5978 | else if(strcmp(argv[i], "-xattrs") == 0) |
| 5979 | no_xattrs = FALSE; |
| 5980 | |
Mohamad Ayyash | e031629 | 2015-02-24 19:21:29 -0800 | [diff] [blame] | 5981 | /* ANDROID CHANGES START*/ |
| 5982 | #ifdef ANDROID |
| 5983 | else if(strcmp(argv[i], "-context-file") == 0) { |
| 5984 | if(++i == argc) { |
| 5985 | ERROR("%s: -context-file: missing file name\n", |
| 5986 | argv[0]); |
| 5987 | exit(1); |
| 5988 | } |
| 5989 | context_file = argv[i]; |
| 5990 | } |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 5991 | else if(strcmp(argv[i], "-fs-config-file") == 0) { |
| 5992 | if(++i == argc) { |
| 5993 | ERROR("%s: -fs-config-file: missing file name\n", |
| 5994 | argv[0]); |
| 5995 | exit(1); |
| 5996 | } |
| 5997 | fs_config_file = argv[i]; |
Mohan Srinivasan | 58e18d7 | 2016-07-26 15:06:29 -0700 | [diff] [blame] | 5998 | } else if(strcmp(argv[i], "-whitelist") == 0) { |
| 5999 | if(++i == argc) { |
| 6000 | ERROR("%s: -whitelist missing filename\n", argv[0]); |
| 6001 | exit(1); |
| 6002 | } |
| 6003 | whitelist_filename = argv[i]; |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 6004 | } |
Mohamad Ayyash | 4dfe4eb | 2016-06-03 18:49:44 -0700 | [diff] [blame] | 6005 | else if(strcmp(argv[i], "-t") == 0) { |
| 6006 | if(++i == argc) { |
| 6007 | ERROR("%s: -t missing compression threshold percentage\n", argv[0]); |
| 6008 | exit(1); |
| 6009 | } |
| 6010 | if(!parse_number(argv[i], &compress_thresh_per, 1)) { |
| 6011 | ERROR("%s: -t invalid compression threshold percentage\n", argv[0]); |
| 6012 | exit(1); |
| 6013 | } |
| 6014 | if(compress_thresh_per > 100 || compress_thresh_per < 0) { |
| 6015 | ERROR("%s: -t compression threshold percentage not between 0 and 100\n", |
| 6016 | argv[0]); |
| 6017 | exit(1); |
| 6018 | } |
| 6019 | } |
Mohamad Ayyash | e031629 | 2015-02-24 19:21:29 -0800 | [diff] [blame] | 6020 | #endif |
| 6021 | /* ANDROID CHANGES END */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6022 | else if(strcmp(argv[i], "-nopad") == 0) |
| 6023 | nopad = TRUE; |
| 6024 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 6025 | else if(strcmp(argv[i], "-info") == 0) |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 6026 | silent = FALSE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6027 | |
| 6028 | else if(strcmp(argv[i], "-e") == 0) |
| 6029 | break; |
| 6030 | |
| 6031 | else if(strcmp(argv[i], "-noappend") == 0) |
| 6032 | delete = TRUE; |
| 6033 | |
| 6034 | else if(strcmp(argv[i], "-keep-as-directory") == 0) |
| 6035 | keep_as_directory = TRUE; |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 6036 | /* ANDROID CHANGES START*/ |
| 6037 | #ifdef ANDROID |
| 6038 | else if(strcmp(argv[i], "-android-fs-config") == 0) |
| 6039 | android_config = TRUE; |
Mohamad Ayyash | 18fe5f6 | 2015-03-02 16:00:56 -0800 | [diff] [blame] | 6040 | else if(strcmp(argv[i], "-mount-point") == 0) { |
| 6041 | if(++i == argc) { |
| 6042 | ERROR("%s: -mount-point: missing mount point name\n", |
| 6043 | argv[0]); |
| 6044 | exit(1); |
| 6045 | } |
| 6046 | mount_point = argv[i]; |
| 6047 | } |
Thierry Strudel | 1a710ff | 2015-07-09 16:34:38 -0700 | [diff] [blame] | 6048 | else if(strcmp(argv[i], "-product-out") == 0) { |
| 6049 | if(++i == argc) { |
| 6050 | ERROR("%s: -product-out: missing path name\n", |
| 6051 | argv[0]); |
| 6052 | exit(1); |
| 6053 | } |
| 6054 | target_out_path = argv[i]; |
| 6055 | } |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 6056 | else if(strcmp(argv[i], "-disable-4k-align") == 0) |
| 6057 | align_4k_blocks = FALSE; |
| 6058 | else if(strcmp(argv[i], "-block-map") == 0) { |
| 6059 | if(++i == argc) { |
| 6060 | ERROR("%s: -block-map: missing path name\n", |
| 6061 | argv[0]); |
| 6062 | exit(1); |
| 6063 | } |
| 6064 | block_map_file = fopen(argv[i], "w"); |
| 6065 | if (block_map_file == NULL) { |
| 6066 | ERROR("%s: -block-map: failed to open %s\n", |
| 6067 | argv[0], argv[i]); |
| 6068 | exit(1); |
| 6069 | } |
| 6070 | if (!align_4k_blocks) { |
| 6071 | ERROR("WARNING: Using block maps with unaligned 4k blocks " |
| 6072 | "is not ideal as block map offsets are multiples of 4k, " |
| 6073 | "consider not passing -disable-4k-align\n"); |
| 6074 | } |
| 6075 | } |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 6076 | #endif |
| 6077 | /* ANDROID CHANGES END */ |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 6078 | |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 6079 | else if(strcmp(argv[i], "-exit-on-error") == 0) |
| 6080 | exit_on_error = TRUE; |
| 6081 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6082 | else if(strcmp(argv[i], "-root-becomes") == 0) { |
| 6083 | if(++i == argc) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6084 | ERROR("%s: -root-becomes: missing name\n", |
| 6085 | argv[0]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6086 | exit(1); |
| 6087 | } |
| 6088 | root_name = argv[i]; |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 6089 | } else if (strcmp(argv[i], "-uid-map") == 0) { |
| 6090 | if (++i == argc) { |
| 6091 | ERROR("%s: -uid-map: missing mapping\n", |
| 6092 | argv[0]); |
| 6093 | exit(1); |
| 6094 | } |
| 6095 | if (parse_ugid_map(argv[i], uid_mapping, |
| 6096 | &uid_map_count) != 0) { |
| 6097 | ERROR("%s: -uid-map: invalid mapping\n", |
| 6098 | argv[0]); |
| 6099 | exit(1); |
| 6100 | } |
| 6101 | } else if (strcmp(argv[i], "-gid-map") == 0) { |
| 6102 | if (++i == argc) { |
| 6103 | ERROR("%s: -gid-map: missing mapping\n", |
| 6104 | argv[0]); |
| 6105 | exit(1); |
| 6106 | } |
| 6107 | if (parse_ugid_map(argv[i], gid_mapping, |
| 6108 | &gid_map_count) != 0) { |
| 6109 | ERROR("%s: -gid-map: invalid mapping\n", |
| 6110 | argv[0]); |
| 6111 | exit(1); |
| 6112 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6113 | } else if(strcmp(argv[i], "-version") == 0) { |
| 6114 | VERSION(); |
| 6115 | } else { |
| 6116 | ERROR("%s: invalid option\n\n", argv[0]); |
| 6117 | printOptions: |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6118 | ERROR("SYNTAX:%s source1 source2 ... dest [options] " |
| 6119 | "[-e list of exclude\ndirs/files]\n", argv[0]); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 6120 | ERROR("\nFilesystem build options:\n"); |
plougher | ff5ea8b | 2009-08-07 19:33:10 +0000 | [diff] [blame] | 6121 | ERROR("-comp <comp>\t\tselect <comp> compression\n"); |
plougher | 13df178 | 2009-08-29 01:05:34 +0000 | [diff] [blame] | 6122 | ERROR("\t\t\tCompressors available:\n"); |
plougher | 764dab5 | 2009-08-24 18:28:04 +0000 | [diff] [blame] | 6123 | display_compressors("\t\t\t", COMP_DEFAULT); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6124 | ERROR("-b <block_size>\t\tset data block to " |
Phillip Lougher | b74a59c | 2014-04-30 03:10:44 +0100 | [diff] [blame] | 6125 | "<block_size>. Default 128 Kbytes\n"); |
| 6126 | ERROR("\t\t\tOptionally a suffix of K or M can be" |
| 6127 | " given to specify\n\t\t\tKbytes or Mbytes" |
| 6128 | " respectively\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 6129 | ERROR("-no-exports\t\tdon't make the filesystem " |
| 6130 | "exportable via NFS\n"); |
| 6131 | ERROR("-no-sparse\t\tdon't detect sparse files\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 6132 | ERROR("-no-xattrs\t\tdon't store extended attributes" |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 6133 | NOXOPT_STR "\n"); |
plougher | 07d25c2 | 2010-08-25 17:41:57 +0000 | [diff] [blame] | 6134 | ERROR("-xattrs\t\t\tstore extended attributes" XOPT_STR |
plougher | 30281c8 | 2010-08-25 05:06:11 +0000 | [diff] [blame] | 6135 | "\n"); |
Mohamad Ayyash | e031629 | 2015-02-24 19:21:29 -0800 | [diff] [blame] | 6136 | /* ANDROID CHANGES START*/ |
| 6137 | #ifdef ANDROID |
| 6138 | ERROR("-context-file <file>\tApply selinux security " |
| 6139 | "xattrs from context-file instead\n\t\t\t" |
| 6140 | "of reading xattrs from file system\n"); |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 6141 | ERROR("-fs-config-file <file>\tAndroid specific " |
| 6142 | "filesystem config file\n"); |
Mohamad Ayyash | 4dfe4eb | 2016-06-03 18:49:44 -0700 | [diff] [blame] | 6143 | ERROR("-t <compress_thresh>\tset minimum " |
| 6144 | "acceptable compression ratio of a block to\n\t\t\t" |
| 6145 | "<compress_thresh_per> otherwise don't compress. " |
| 6146 | "Default 0%\n"); |
Mohan Srinivasan | 58e18d7 | 2016-07-26 15:06:29 -0700 | [diff] [blame] | 6147 | ERROR("-whitelist <file>\tAndroid specific whitelist " |
| 6148 | "one entry per line (no wildcards)\n"); |
Mohamad Ayyash | e031629 | 2015-02-24 19:21:29 -0800 | [diff] [blame] | 6149 | #endif |
| 6150 | /* ANDROID CHANGES END */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6151 | ERROR("-noI\t\t\tdo not compress inode table\n"); |
| 6152 | ERROR("-noD\t\t\tdo not compress data blocks\n"); |
| 6153 | ERROR("-noF\t\t\tdo not compress fragment blocks\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 6154 | ERROR("-noX\t\t\tdo not compress extended " |
| 6155 | "attributes\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6156 | ERROR("-no-fragments\t\tdo not use fragments\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6157 | ERROR("-always-use-fragments\tuse fragment blocks for " |
| 6158 | "files larger than block size\n"); |
| 6159 | ERROR("-no-duplicates\t\tdo not perform duplicate " |
| 6160 | "checking\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6161 | ERROR("-all-root\t\tmake all files owned by root\n"); |
| 6162 | ERROR("-force-uid uid\t\tset all file uids to uid\n"); |
| 6163 | ERROR("-force-gid gid\t\tset all file gids to gid\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6164 | ERROR("-nopad\t\t\tdo not pad filesystem to a multiple " |
| 6165 | "of 4K\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 6166 | ERROR("-keep-as-directory\tif one source directory is " |
| 6167 | "specified, create a root\n"); |
| 6168 | ERROR("\t\t\tdirectory containing that directory, " |
| 6169 | "rather than the\n"); |
| 6170 | ERROR("\t\t\tcontents of the directory\n"); |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 6171 | /* ANDROID CHANGES START*/ |
| 6172 | #ifdef ANDROID |
| 6173 | ERROR("-android-fs-config\tuse android fs config " |
| 6174 | "for mode, uid, and gids of inodes\n"); |
Mohamad Ayyash | 18fe5f6 | 2015-03-02 16:00:56 -0800 | [diff] [blame] | 6175 | ERROR("-mount-point <name>\tNeed to be provided when " |
| 6176 | "android-fs-config or context-file\n\t\t\tare " |
| 6177 | "enabled and source directory is not mount point\n"); |
Thierry Strudel | 1a710ff | 2015-07-09 16:34:38 -0700 | [diff] [blame] | 6178 | ERROR("-product-out <path>\tPRODUCT_OUT directory to " |
Mohamad Ayyash | 4dfe4eb | 2016-06-03 18:49:44 -0700 | [diff] [blame] | 6179 | "read device specific FS rules files from\n"); |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 6180 | ERROR("-disable-4k-align \tDon't 4k align data blocks. Default is false\n"); |
| 6181 | ERROR("-block-map <path>\tGenerate a block map for non-fragment files\n"); |
Mohamad Ayyash | 829ffbe | 2015-02-24 13:04:49 -0800 | [diff] [blame] | 6182 | #endif |
| 6183 | /* ANDROID CHANGES END */ |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 6184 | ERROR("\nFilesystem filter options:\n"); |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 6185 | ERROR("-p <pseudo-definition>\tAdd pseudo file " |
| 6186 | "definition\n"); |
| 6187 | ERROR("-pf <pseudo-file>\tAdd list of pseudo file " |
| 6188 | "definitions\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6189 | ERROR("-sort <sort_file>\tsort files according to " |
| 6190 | "priorities in <sort_file>. One\n"); |
| 6191 | ERROR("\t\t\tfile or dir with priority per line. " |
| 6192 | "Priority -32768 to\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6193 | ERROR("\t\t\t32767, default priority 0\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6194 | ERROR("-ef <exclude_file>\tlist of exclude dirs/files." |
| 6195 | " One per line\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6196 | ERROR("-wildcards\t\tAllow extended shell wildcards " |
| 6197 | "(globbing) to be used in\n\t\t\texclude " |
| 6198 | "dirs/files\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6199 | ERROR("-regex\t\t\tAllow POSIX regular expressions to " |
| 6200 | "be used in exclude\n\t\t\tdirs/files\n"); |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 6201 | ERROR("-uid-map <mapping>\tUser ID mapping.\n"); |
| 6202 | ERROR("\t\t\tFollows the format described in " |
| 6203 | "user_namespaces(7).\n"); |
| 6204 | ERROR("-gid-map <mapping>\tGroup ID mapping.\n"); |
| 6205 | ERROR("\t\t\tFollows the format described in " |
| 6206 | "user_namespaces(7).\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 6207 | ERROR("\nFilesystem append options:\n"); |
| 6208 | ERROR("-noappend\t\tdo not append to existing " |
| 6209 | "filesystem\n"); |
| 6210 | ERROR("-root-becomes <name>\twhen appending source " |
| 6211 | "files/directories, make the\n"); |
| 6212 | ERROR("\t\t\toriginal root become a subdirectory in " |
| 6213 | "the new root\n"); |
| 6214 | ERROR("\t\t\tcalled <name>, rather than adding the new " |
| 6215 | "source items\n"); |
| 6216 | ERROR("\t\t\tto the original root\n"); |
| 6217 | ERROR("\nMksquashfs runtime options:\n"); |
| 6218 | ERROR("-version\t\tprint version, licence and " |
| 6219 | "copyright message\n"); |
Phillip Lougher | 85776df | 2014-01-31 03:10:46 +0000 | [diff] [blame] | 6220 | ERROR("-exit-on-error\t\ttreat normally ignored errors " |
| 6221 | "as fatal\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 6222 | ERROR("-recover <name>\t\trecover filesystem data " |
| 6223 | "using recovery file <name>\n"); |
| 6224 | ERROR("-no-recovery\t\tdon't generate a recovery " |
| 6225 | "file\n"); |
| 6226 | ERROR("-info\t\t\tprint files written to filesystem\n"); |
| 6227 | ERROR("-no-progress\t\tdon't display the progress " |
| 6228 | "bar\n"); |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 6229 | ERROR("-progress\t\tdisplay progress bar when using " |
| 6230 | "the -info option\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 6231 | ERROR("-processors <number>\tUse <number> processors." |
| 6232 | " By default will use number of\n"); |
| 6233 | ERROR("\t\t\tprocessors available\n"); |
Phillip Lougher | 57e2f69 | 2014-05-05 23:50:24 +0100 | [diff] [blame] | 6234 | ERROR("-mem <size>\t\tUse <size> physical memory. " |
| 6235 | "Currently set to %dM\n", total_mem); |
| 6236 | ERROR("\t\t\tOptionally a suffix of K, M or G can be" |
| 6237 | " given to specify\n\t\t\tKbytes, Mbytes or" |
| 6238 | " Gbytes respectively\n"); |
plougher | 3763256 | 2009-08-07 19:09:01 +0000 | [diff] [blame] | 6239 | ERROR("\nMiscellaneous options:\n"); |
| 6240 | ERROR("-root-owned\t\talternative name for -all-root" |
| 6241 | "\n"); |
| 6242 | ERROR("-noInodeCompression\talternative name for -noI" |
| 6243 | "\n"); |
| 6244 | ERROR("-noDataCompression\talternative name for -noD" |
| 6245 | "\n"); |
| 6246 | ERROR("-noFragmentCompression\talternative name for " |
| 6247 | "-noF\n"); |
plougher | b99d783 | 2010-05-19 01:57:34 +0000 | [diff] [blame] | 6248 | ERROR("-noXattrCompression\talternative name for " |
| 6249 | "-noX\n"); |
Phillip Lougher | 774b7b3 | 2014-01-07 04:47:13 +0000 | [diff] [blame] | 6250 | ERROR("\n-Xhelp\t\t\tprint compressor options for" |
| 6251 | " selected compressor\n"); |
plougher | 4fb6682 | 2010-12-08 02:49:28 +0000 | [diff] [blame] | 6252 | ERROR("\nCompressors available and compressor specific " |
| 6253 | "options:\n"); |
| 6254 | display_compressor_usage(COMP_DEFAULT); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6255 | exit(1); |
| 6256 | } |
| 6257 | } |
| 6258 | |
Hidehiko Abe | f240b14 | 2018-05-08 22:45:47 +0900 | [diff] [blame] | 6259 | if (!uid_map_count) { |
| 6260 | uid_mapping[0].child_id = 0; |
| 6261 | uid_mapping[0].parent_id = 0; |
| 6262 | uid_mapping[0].length = 4294967295u; |
| 6263 | uid_map_count = 1; |
| 6264 | } |
| 6265 | if (!gid_map_count) { |
| 6266 | gid_mapping[0].child_id = 0; |
| 6267 | gid_mapping[0].parent_id = 0; |
| 6268 | gid_mapping[0].length = 4294967295u; |
| 6269 | gid_map_count = 1; |
| 6270 | } |
| 6271 | |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 6272 | /* ANDROID CHANGES START*/ |
| 6273 | #ifdef ANDROID |
| 6274 | if (fs_config_file) { |
| 6275 | if (load_canned_fs_config(fs_config_file) < 0) { |
| 6276 | fprintf(stderr, "failed to load %s\n", fs_config_file); |
| 6277 | exit(1); |
| 6278 | } |
| 6279 | fs_config_func = canned_fs_config; |
| 6280 | } else if (mount_point) { |
| 6281 | fs_config_func = fs_config; |
| 6282 | } |
Mohan Srinivasan | 58e18d7 | 2016-07-26 15:06:29 -0700 | [diff] [blame] | 6283 | if (whitelist_filename) |
| 6284 | process_whitelist_file(whitelist_filename); |
Mohamad Ayyash | ec7a8b4 | 2016-04-07 22:12:10 -0700 | [diff] [blame] | 6285 | #endif |
| 6286 | /* ANDROID CHANGES END */ |
| 6287 | |
plougher | b747f4f | 2010-12-25 04:05:47 +0000 | [diff] [blame] | 6288 | /* |
| 6289 | * Some compressors may need the options to be checked for validity |
| 6290 | * once all the options have been processed |
| 6291 | */ |
| 6292 | res = compressor_options_post(comp, block_size); |
| 6293 | if(res) |
| 6294 | EXIT_MKSQUASHFS(); |
| 6295 | |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 6296 | /* |
| 6297 | * If the -info option has been selected then disable the |
| 6298 | * progress bar unless it has been explicitly enabled with |
| 6299 | * the -progress option |
| 6300 | */ |
| 6301 | if(!silent) |
| 6302 | progress = force_progress; |
| 6303 | |
| 6304 | #ifdef SQUASHFS_TRACE |
Phillip Lougher | 989f5fe | 2013-04-22 03:16:29 +0100 | [diff] [blame] | 6305 | /* |
| 6306 | * Disable progress bar if full debug tracing is enabled. |
| 6307 | * The progress bar in this case just gets in the way of the |
| 6308 | * debug trace output |
| 6309 | */ |
Phillip Lougher | c6c73b2 | 2012-10-19 03:12:08 +0100 | [diff] [blame] | 6310 | progress = FALSE; |
| 6311 | #endif |
| 6312 | |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 6313 | for(i = 0; i < source; i++) |
| 6314 | if(lstat(source_path[i], &source_buf) == -1) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6315 | fprintf(stderr, "Cannot stat source directory \"%s\" " |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6316 | "because %s\n", source_path[i], |
| 6317 | strerror(errno)); |
plougher | 91fbb30 | 2008-05-06 02:29:36 +0000 | [diff] [blame] | 6318 | EXIT_MKSQUASHFS(); |
| 6319 | } |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 6320 | |
| 6321 | destination_file = argv[source + 1]; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6322 | if(stat(argv[source + 1], &buf) == -1) { |
| 6323 | if(errno == ENOENT) { /* Does not exist */ |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6324 | fd = open(argv[source + 1], O_CREAT | O_TRUNC | O_RDWR, |
plougher | 59dce67 | 2010-05-19 03:56:59 +0000 | [diff] [blame] | 6325 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6326 | if(fd == -1) { |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6327 | perror("Could not create destination file"); |
| 6328 | exit(1); |
| 6329 | } |
| 6330 | delete = TRUE; |
| 6331 | } else { |
| 6332 | perror("Could not stat destination file"); |
| 6333 | exit(1); |
| 6334 | } |
| 6335 | |
| 6336 | } else { |
| 6337 | if(S_ISBLK(buf.st_mode)) { |
| 6338 | if((fd = open(argv[source + 1], O_RDWR)) == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6339 | perror("Could not open block device as " |
| 6340 | "destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6341 | exit(1); |
| 6342 | } |
| 6343 | block_device = 1; |
| 6344 | |
| 6345 | } else if(S_ISREG(buf.st_mode)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6346 | fd = open(argv[source + 1], (delete ? O_TRUNC : 0) | |
| 6347 | O_RDWR); |
| 6348 | if(fd == -1) { |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6349 | perror("Could not open regular file for " |
| 6350 | "writing as destination"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6351 | exit(1); |
| 6352 | } |
plougher | 44d54ef | 2010-02-08 22:13:49 +0000 | [diff] [blame] | 6353 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6354 | else { |
| 6355 | ERROR("Destination not block device or regular file\n"); |
| 6356 | exit(1); |
| 6357 | } |
| 6358 | |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 6359 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6360 | |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 6361 | /* |
| 6362 | * process the exclude files - must be done afer destination file has |
| 6363 | * been possibly created |
| 6364 | */ |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6365 | for(i = source + 2; i < argc; i++) |
Phillip Lougher | 386128f | 2012-12-16 05:23:45 +0000 | [diff] [blame] | 6366 | if(strcmp(argv[i], "-ef") == 0) |
| 6367 | /* |
| 6368 | * Note presence of filename arg has already |
| 6369 | * been checked |
| 6370 | */ |
| 6371 | process_exclude_file(argv[++i]); |
| 6372 | else if(strcmp(argv[i], "-e") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6373 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 6374 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 6375 | strcmp(argv[i], "-sort") == 0 || |
| 6376 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 6377 | strcmp(argv[i], "-af") == 0 || |
Phillip Lougher | 089a81a | 2014-08-24 03:46:46 +0100 | [diff] [blame] | 6378 | strcmp(argv[i], "-vaf") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 6379 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6380 | i++; |
| 6381 | |
| 6382 | if(i != argc) { |
| 6383 | if(++i == argc) { |
| 6384 | ERROR("%s: -e missing arguments\n", argv[0]); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 6385 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6386 | } |
plougher | 8f8e1a1 | 2007-10-18 02:50:21 +0000 | [diff] [blame] | 6387 | while(i < argc) |
| 6388 | if(old_exclude) |
| 6389 | old_add_exclude(argv[i++]); |
| 6390 | else |
plougher | 05e50ef | 2007-10-23 12:34:20 +0000 | [diff] [blame] | 6391 | add_exclude(argv[i++]); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | /* process the sort files - must be done afer the exclude files */ |
| 6395 | for(i = source + 2; i < argc; i++) |
| 6396 | if(strcmp(argv[i], "-sort") == 0) { |
plougher | e1c9a74 | 2010-07-21 16:59:05 +0000 | [diff] [blame] | 6397 | int res = read_sort_file(argv[++i], source, |
| 6398 | source_path); |
| 6399 | if(res == FALSE) |
| 6400 | BAD_ERROR("Failed to read sort file\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6401 | sorted ++; |
| 6402 | } else if(strcmp(argv[i], "-e") == 0) |
| 6403 | break; |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 6404 | else if(strcmp(argv[i], "-root-becomes") == 0 || |
plougher | 43244f2 | 2009-04-05 02:04:51 +0000 | [diff] [blame] | 6405 | strcmp(argv[i], "-ef") == 0 || |
| 6406 | strcmp(argv[i], "-pf") == 0 || |
Phillip Lougher | b73caba | 2012-12-24 21:58:41 +0000 | [diff] [blame] | 6407 | strcmp(argv[i], "-af") == 0 || |
Phillip Lougher | 089a81a | 2014-08-24 03:46:46 +0100 | [diff] [blame] | 6408 | strcmp(argv[i], "-vaf") == 0 || |
plougher | 8b9a7f6 | 2009-08-01 22:52:45 +0000 | [diff] [blame] | 6409 | strcmp(argv[i], "-comp") == 0) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6410 | i++; |
| 6411 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 6412 | if(!delete) { |
plougher | a175ce2 | 2009-07-30 04:43:27 +0000 | [diff] [blame] | 6413 | comp = read_super(fd, &sBlk, argv[source + 1]); |
| 6414 | if(comp == NULL) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6415 | ERROR("Failed to read existing filesystem - will not " |
| 6416 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6417 | ERROR("To force Mksquashfs to write to this block " |
| 6418 | "device or file use -noappend\n"); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 6419 | EXIT_MKSQUASHFS(); |
| 6420 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6421 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6422 | block_log = slog(block_size = sBlk.block_size); |
| 6423 | noI = SQUASHFS_UNCOMPRESSED_INODES(sBlk.flags); |
| 6424 | noD = SQUASHFS_UNCOMPRESSED_DATA(sBlk.flags); |
| 6425 | noF = SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk.flags); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 6426 | noX = SQUASHFS_UNCOMPRESSED_XATTRS(sBlk.flags); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6427 | no_fragments = SQUASHFS_NO_FRAGMENTS(sBlk.flags); |
| 6428 | always_use_fragments = SQUASHFS_ALWAYS_FRAGMENTS(sBlk.flags); |
| 6429 | duplicate_checking = SQUASHFS_DUPLICATES(sBlk.flags); |
plougher | 0e45365 | 2006-11-06 01:49:35 +0000 | [diff] [blame] | 6430 | exportable = SQUASHFS_EXPORTABLE(sBlk.flags); |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 6431 | no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 6432 | comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
Phillip Lougher | c3af83a | 2014-04-21 21:38:16 +0100 | [diff] [blame] | 6435 | initialise_threads(readq, fragq, bwriteq, fwriteq, delete, |
| 6436 | destination_file); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 6437 | |
plougher | 13fdddf | 2010-11-24 01:23:41 +0000 | [diff] [blame] | 6438 | res = compressor_init(comp, &stream, SQUASHFS_METADATA_SIZE, 0); |
| 6439 | if(res) |
| 6440 | BAD_ERROR("compressor_init failed\n"); |
| 6441 | |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 6442 | if(delete) { |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 6443 | int size; |
Phillip Lougher | a45c9d2 | 2011-02-20 04:24:07 +0000 | [diff] [blame] | 6444 | void *comp_data = compressor_dump_options(comp, block_size, |
| 6445 | &size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 6446 | |
Colin Cross | be75490 | 2017-12-09 22:27:51 +0000 | [diff] [blame] | 6447 | printf("Creating %d.%d filesystem on %s, block size %d.\n", |
| 6448 | SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size); |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 6449 | |
plougher | 871c72a | 2010-12-25 04:17:27 +0000 | [diff] [blame] | 6450 | /* |
| 6451 | * store any compressor specific options after the superblock, |
| 6452 | * and set the COMP_OPT flag to show that the filesystem has |
| 6453 | * compressor specfic options |
| 6454 | */ |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 6455 | if(comp_data) { |
| 6456 | unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT; |
| 6457 | |
| 6458 | SQUASHFS_INSWAP_SHORTS(&c_byte, 1); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 6459 | write_destination(fd, sizeof(struct squashfs_super_block), |
plougher | 29e2ace | 2010-12-31 08:50:00 +0000 | [diff] [blame] | 6460 | sizeof(c_byte), &c_byte); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 6461 | write_destination(fd, sizeof(struct squashfs_super_block) + |
plougher | 8e4dad4 | 2010-12-28 05:56:22 +0000 | [diff] [blame] | 6462 | sizeof(c_byte), size, comp_data); |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 6463 | bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte) |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 6464 | + size; |
| 6465 | comp_opts = TRUE; |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 6466 | } else |
plougher | 64e83fd | 2010-12-31 21:21:26 +0000 | [diff] [blame] | 6467 | bytes = sizeof(struct squashfs_super_block); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 6468 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6469 | unsigned int last_directory_block, inode_dir_offset, |
| 6470 | inode_dir_file_size, root_inode_size, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6471 | inode_dir_start_block, uncompressed_data, |
| 6472 | compressed_data, inode_dir_inode_number, |
| 6473 | inode_dir_parent_inode; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6474 | unsigned int root_inode_start = |
| 6475 | SQUASHFS_INODE_BLK(sBlk.root_inode), |
| 6476 | root_inode_offset = |
| 6477 | SQUASHFS_INODE_OFFSET(sBlk.root_inode); |
plougher | 4c99cb7 | 2007-06-14 21:46:31 +0000 | [diff] [blame] | 6478 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6479 | if((bytes = read_filesystem(root_name, fd, &sBlk, &inode_table, |
| 6480 | &data_cache, &directory_table, |
| 6481 | &directory_data_cache, &last_directory_block, |
| 6482 | &inode_dir_offset, &inode_dir_file_size, |
| 6483 | &root_inode_size, &inode_dir_start_block, |
| 6484 | &file_count, &sym_count, &dev_count, &dir_count, |
| 6485 | &fifo_count, &sock_count, &total_bytes, |
| 6486 | &total_inode_bytes, &total_directory_bytes, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6487 | &inode_dir_inode_number, |
| 6488 | &inode_dir_parent_inode, add_old_root_entry, |
| 6489 | &fragment_table, &inode_lookup_table)) == 0) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6490 | ERROR("Failed to read existing filesystem - will not " |
| 6491 | "overwrite - ABORTING!\n"); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6492 | ERROR("To force Mksquashfs to write to this block " |
| 6493 | "device or file use -noappend\n"); |
plougher | 324978d | 2006-02-27 04:53:29 +0000 | [diff] [blame] | 6494 | EXIT_MKSQUASHFS(); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6495 | } |
Phillip Lougher | b4fc3bf | 2014-02-06 01:42:06 +0000 | [diff] [blame] | 6496 | if((append_fragments = fragments = sBlk.fragments)) { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6497 | fragment_table = realloc((char *) fragment_table, |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6498 | ((fragments + FRAG_SIZE - 1) & ~(FRAG_SIZE - 1)) |
plougher | 8ed84b9 | 2010-12-31 10:37:24 +0000 | [diff] [blame] | 6499 | * sizeof(struct squashfs_fragment_entry)); |
plougher | 6f2a826 | 2010-07-27 00:37:19 +0000 | [diff] [blame] | 6500 | if(fragment_table == NULL) |
| 6501 | BAD_ERROR("Out of memory in save filesystem state\n"); |
| 6502 | } |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6503 | |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6504 | printf("Appending to existing %d.%d filesystem on %s, block " |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 6505 | "size %d\n", SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6506 | block_size); |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 6507 | printf("All -b, -noI, -noD, -noF, -noX, no-duplicates, no-fragments, " |
plougher | bb98803 | 2009-08-06 08:46:34 +0000 | [diff] [blame] | 6508 | "-always-use-fragments,\n-exportable and -comp options " |
| 6509 | "ignored\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6510 | printf("\nIf appending is not wanted, please re-run with " |
| 6511 | "-noappend specified!\n\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6512 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6513 | compressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 6514 | ~(SQUASHFS_METADATA_SIZE - 1); |
| 6515 | uncompressed_data = (inode_dir_offset + inode_dir_file_size) & |
| 6516 | (SQUASHFS_METADATA_SIZE - 1); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6517 | |
| 6518 | /* save original filesystem state for restoring ... */ |
| 6519 | sfragments = fragments; |
| 6520 | sbytes = bytes; |
| 6521 | sinode_count = sBlk.inodes; |
plougher | 2337798 | 2007-11-12 04:04:48 +0000 | [diff] [blame] | 6522 | scache_bytes = root_inode_offset + root_inode_size; |
| 6523 | sdirectory_cache_bytes = uncompressed_data; |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 6524 | sdata_cache = malloc(scache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 6525 | if(sdata_cache == NULL) |
| 6526 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | a2968ef | 2009-03-03 10:46:00 +0000 | [diff] [blame] | 6527 | sdirectory_data_cache = malloc(sdirectory_cache_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 6528 | if(sdirectory_data_cache == NULL) |
| 6529 | BAD_ERROR("Out of memory in save filesystem state\n"); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6530 | memcpy(sdata_cache, data_cache, scache_bytes); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6531 | memcpy(sdirectory_data_cache, directory_data_cache + |
| 6532 | compressed_data, sdirectory_cache_bytes); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6533 | sinode_bytes = root_inode_start; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6534 | stotal_bytes = total_bytes; |
| 6535 | stotal_inode_bytes = total_inode_bytes; |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6536 | stotal_directory_bytes = total_directory_bytes + |
| 6537 | compressed_data; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6538 | sfile_count = file_count; |
| 6539 | ssym_count = sym_count; |
| 6540 | sdev_count = dev_count; |
| 6541 | sdir_count = dir_count + 1; |
| 6542 | sfifo_count = fifo_count; |
| 6543 | ssock_count = sock_count; |
| 6544 | sdup_files = dup_files; |
plougher | 1b899fc | 2008-08-07 01:24:06 +0000 | [diff] [blame] | 6545 | sid_count = id_count; |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 6546 | write_recovery_data(&sBlk); |
Phillip Lougher | 3bcf67d | 2013-02-24 10:56:38 +0000 | [diff] [blame] | 6547 | save_xattrs(); |
Phillip Lougher | 3794e34 | 2014-04-10 02:15:45 +0100 | [diff] [blame] | 6548 | appending = TRUE; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6549 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6550 | /* |
| 6551 | * set the filesystem state up to be able to append to the |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6552 | * original filesystem. The filesystem state differs depending |
| 6553 | * on whether we're appending to the original root directory, or |
| 6554 | * if the original root directory becomes a sub-directory |
| 6555 | * (root-becomes specified on command line, here root_name != |
| 6556 | * NULL) |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6557 | */ |
| 6558 | inode_bytes = inode_size = root_inode_start; |
| 6559 | directory_size = last_directory_block; |
| 6560 | cache_size = root_inode_offset + root_inode_size; |
| 6561 | directory_cache_size = inode_dir_offset + inode_dir_file_size; |
| 6562 | if(root_name) { |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 6563 | sdirectory_bytes = last_directory_block; |
| 6564 | sdirectory_compressed_bytes = 0; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 6565 | root_inode_number = inode_dir_parent_inode; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 6566 | inode_no = sBlk.inodes + 2; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6567 | directory_bytes = last_directory_block; |
| 6568 | directory_cache_bytes = uncompressed_data; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6569 | memmove(directory_data_cache, directory_data_cache + |
| 6570 | compressed_data, uncompressed_data); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6571 | cache_bytes = root_inode_offset + root_inode_size; |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6572 | add_old_root_entry(root_name, sBlk.root_inode, |
| 6573 | inode_dir_inode_number, SQUASHFS_DIR_TYPE); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6574 | total_directory_bytes += compressed_data; |
| 6575 | dir_count ++; |
| 6576 | } else { |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6577 | sdirectory_compressed_bytes = last_directory_block - |
| 6578 | inode_dir_start_block; |
| 6579 | sdirectory_compressed = |
| 6580 | malloc(sdirectory_compressed_bytes); |
plougher | 332e43d | 2010-07-21 01:18:30 +0000 | [diff] [blame] | 6581 | if(sdirectory_compressed == NULL) |
plougher | 1611145 | 2010-07-22 05:12:18 +0000 | [diff] [blame] | 6582 | BAD_ERROR("Out of memory in save filesystem " |
| 6583 | "state\n"); |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6584 | memcpy(sdirectory_compressed, directory_table + |
| 6585 | inode_dir_start_block, |
| 6586 | sdirectory_compressed_bytes); |
plougher | ca2c93f | 2008-08-15 08:34:57 +0000 | [diff] [blame] | 6587 | sdirectory_bytes = inode_dir_start_block; |
plougher | df70c3e | 2006-01-27 09:34:13 +0000 | [diff] [blame] | 6588 | root_inode_number = inode_dir_inode_number; |
Phillip Lougher | 539c2b1 | 2012-07-30 20:14:52 +0100 | [diff] [blame] | 6589 | inode_no = sBlk.inodes + 1; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6590 | directory_bytes = inode_dir_start_block; |
| 6591 | directory_cache_bytes = inode_dir_offset; |
| 6592 | cache_bytes = root_inode_offset; |
| 6593 | } |
| 6594 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6595 | inode_count = file_count + dir_count + sym_count + dev_count + |
| 6596 | fifo_count + sock_count; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6597 | } |
| 6598 | |
Phillip Lougher | b2abb1c | 2013-01-09 04:51:21 +0000 | [diff] [blame] | 6599 | if(path) |
| 6600 | paths = add_subdir(paths, path); |
plougher | f9039c9 | 2007-10-22 03:54:16 +0000 | [diff] [blame] | 6601 | |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 6602 | dump_actions(); |
Phillip Lougher | 7a28f7a | 2014-04-20 23:47:08 +0100 | [diff] [blame] | 6603 | dump_pseudos(); |
Phillip Lougher | 4bcb7f8 | 2011-08-28 03:18:26 +0100 | [diff] [blame] | 6604 | |
plougher | 360514a | 2009-03-30 03:01:38 +0000 | [diff] [blame] | 6605 | if(delete && !keep_as_directory && source == 1 && |
| 6606 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 6607 | dir_scan(&inode, source_path[0], scan1_readdir, progress); |
plougher | 50b3176 | 2009-03-31 04:14:46 +0000 | [diff] [blame] | 6608 | else if(!keep_as_directory && source == 1 && |
| 6609 | S_ISDIR(source_buf.st_mode)) |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 6610 | dir_scan(&inode, source_path[0], scan1_single_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6611 | else |
Phillip Lougher | bae0e42 | 2014-01-19 23:42:11 +0000 | [diff] [blame] | 6612 | dir_scan(&inode, "", scan1_encomp_readdir, progress); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6613 | sBlk.root_inode = inode; |
| 6614 | sBlk.inodes = inode_count; |
| 6615 | sBlk.s_magic = SQUASHFS_MAGIC; |
| 6616 | sBlk.s_major = SQUASHFS_MAJOR; |
plougher | 978f588 | 2010-12-28 04:34:30 +0000 | [diff] [blame] | 6617 | sBlk.s_minor = SQUASHFS_MINOR; |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6618 | sBlk.block_size = block_size; |
| 6619 | sBlk.block_log = block_log; |
plougher | 89c7a51 | 2010-12-15 08:35:51 +0000 | [diff] [blame] | 6620 | sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, no_fragments, |
plougher | afae825 | 2010-12-15 09:12:58 +0000 | [diff] [blame] | 6621 | always_use_fragments, duplicate_checking, exportable, |
plougher | 3d7e518 | 2010-12-25 01:49:42 +0000 | [diff] [blame] | 6622 | no_xattrs, comp_opts); |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6623 | sBlk.mkfs_time = time(NULL); |
| 6624 | |
Phillip Lougher | 83b8f86 | 2013-04-09 03:28:55 +0100 | [diff] [blame] | 6625 | disable_info(); |
plougher | c9b11db | 2008-05-06 23:59:15 +0000 | [diff] [blame] | 6626 | |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 6627 | while((fragment = get_frag_action(fragment))) |
| 6628 | write_fragment(*fragment); |
| 6629 | unlock_fragments(); |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 6630 | pthread_cleanup_push((void *) pthread_mutex_unlock, &fragment_mutex); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 6631 | pthread_mutex_lock(&fragment_mutex); |
| 6632 | while(fragments_outstanding) { |
| 6633 | pthread_mutex_unlock(&fragment_mutex); |
| 6634 | sched_yield(); |
| 6635 | pthread_mutex_lock(&fragment_mutex); |
| 6636 | } |
Phillip Lougher | fd74d9a | 2013-03-31 22:49:50 +0100 | [diff] [blame] | 6637 | pthread_cleanup_pop(1); |
Phillip Lougher | a709bff | 2013-03-28 17:48:31 +0000 | [diff] [blame] | 6638 | |
| 6639 | queue_put(to_writer, NULL); |
| 6640 | if(queue_get(from_writer) != 0) |
| 6641 | EXIT_MKSQUASHFS(); |
| 6642 | |
Phillip Lougher | cd1a5a6 | 2014-01-21 04:53:46 +0000 | [diff] [blame] | 6643 | set_progressbar_state(FALSE); |
Phillip Lougher | d6b1f0d | 2013-03-26 03:09:27 +0000 | [diff] [blame] | 6644 | write_filesystem_tables(&sBlk, nopad); |
plougher | 99ac0cc | 2007-10-29 03:17:10 +0000 | [diff] [blame] | 6645 | |
Mohamad Ayyash | 305fca6 | 2016-06-13 15:44:16 -0700 | [diff] [blame] | 6646 | /* ANDROID CHANGES START*/ |
| 6647 | #ifdef ANDROID |
| 6648 | if (block_map_file) |
| 6649 | fclose(block_map_file); |
| 6650 | #endif |
| 6651 | /* ANDROID CHANGES END */ |
| 6652 | |
plougher | 1f413c8 | 2005-11-18 00:02:14 +0000 | [diff] [blame] | 6653 | return 0; |
| 6654 | } |