]> wimlib.net Git - wimlib/blob - src/capture_common.c
Don't exclude out-of-tree absolute symlinks in reparse point fix mode
[wimlib] / src / capture_common.c
1 /*
2  * capture_common.c - Mostly code to handle excluding paths from capture.
3  */
4
5 /*
6  * Copyright (C) 2013, 2014 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/capture.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/error.h"
31 #include "wimlib/lookup_table.h"
32 #include "wimlib/paths.h"
33 #include "wimlib/progress.h"
34 #include "wimlib/textfile.h"
35 #include "wimlib/wildcard.h"
36
37 #include <string.h>
38
39 /*
40  * Tally a file (or directory) that has been scanned for a capture operation,
41  * and possibly call the progress function provided by the library user.
42  *
43  * @params
44  *      Flags, optional progress function, and progress data for the capture
45  *      operation.
46  * @status
47  *      Status of the scanned file (ok, unsupported, excluded, or excluded
48  *      symlink).
49  * @inode
50  *      If @status is WIMLIB_SCAN_DENTRY_OK, this is a pointer to the WIM inode
51  *      that has been created for the scanned file.  The first time the file is
52  *      seen, inode->i_nlink will be 1.  On subsequent visits of the same inode
53  *      via additional hard links, inode->i_nlink will be greater than 1.
54  */
55 int
56 do_capture_progress(struct add_image_params *params, int status,
57                     const struct wim_inode *inode)
58 {
59         switch (status) {
60         case WIMLIB_SCAN_DENTRY_OK:
61                 if (!(params->add_flags & WIMLIB_ADD_FLAG_VERBOSE))
62                         return 0;
63         case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
64         case WIMLIB_SCAN_DENTRY_EXCLUDED:
65         case WIMLIB_SCAN_DENTRY_FIXED_SYMLINK:
66         case WIMLIB_SCAN_DENTRY_NOT_FIXED_SYMLINK:
67                 if (!(params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE))
68                         return 0;
69         }
70         params->progress.scan.status = status;
71         if (status == WIMLIB_SCAN_DENTRY_OK && inode->i_nlink == 1) {
72
73                 /* Successful scan, and visiting inode for the first time  */
74
75                 /* Tally size of all data streams.  */
76                 const struct wim_lookup_table_entry *lte;
77                 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
78                         lte = inode_stream_lte_resolved(inode, i);
79                         if (lte)
80                                 params->progress.scan.num_bytes_scanned += lte->size;
81                 }
82
83                 /* Tally the file itself.  */
84                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
85                         params->progress.scan.num_dirs_scanned++;
86                 else
87                         params->progress.scan.num_nondirs_scanned++;
88         }
89
90         /* Call the user-provided progress function.  */
91         return call_progress(params->progfunc, WIMLIB_PROGRESS_MSG_SCAN_DENTRY,
92                              &params->progress, params->progctx);
93 }
94
95 /*
96  * Given a null-terminated pathname pattern @pat that has been read from line
97  * @line_no of the file @path, validate and canonicalize the pattern.
98  *
99  * On success, returns 0.
100  * On failure, returns WIMLIB_ERR_INVALID_CAPTURE_CONFIG.
101  * In either case, @pat may have been modified in-place (and possibly
102  * shortened).
103  */
104 int
105 mangle_pat(tchar *pat, const tchar *path, unsigned long line_no)
106 {
107         if (!is_any_path_separator(pat[0]) &&
108             pat[0] != T('\0') && pat[1] == T(':'))
109         {
110                 /* Pattern begins with drive letter.  */
111
112                 if (!is_any_path_separator(pat[2])) {
113                         /* Something like c:file, which is actually a path
114                          * relative to the current working directory on the c:
115                          * drive.  We require paths with drive letters to be
116                          * absolute.  */
117                         ERROR("%"TS":%lu: Invalid pattern \"%"TS"\":\n"
118                               "        Patterns including drive letters must be absolute!\n"
119                               "        Maybe try \"%"TC":%"TC"%"TS"\"?\n",
120                               path, line_no, pat,
121                               pat[0], OS_PREFERRED_PATH_SEPARATOR, &pat[2]);
122                         return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
123                 }
124
125                 WARNING("%"TS":%lu: Pattern \"%"TS"\" starts with a drive "
126                         "letter, which is being removed.",
127                         path, line_no, pat);
128
129                 /* Strip the drive letter.  */
130                 tmemmove(pat, pat + 2, tstrlen(pat + 2) + 1);
131         }
132
133         /* Collapse consecutive path separators, and translate both / and \ into
134          * / (UNIX) or \ (Windows).
135          *
136          * Note: we expect that this function produces patterns that can be used
137          * for both filesystem paths and WIM paths, so the desired path
138          * separators must be the same.  */
139         BUILD_BUG_ON(OS_PREFERRED_PATH_SEPARATOR != WIM_PATH_SEPARATOR);
140         do_canonicalize_path(pat, pat);
141
142         /* Relative patterns can only match file names, so they must be
143          * single-component only.  */
144         if (pat[0] != OS_PREFERRED_PATH_SEPARATOR &&
145             tstrchr(pat, OS_PREFERRED_PATH_SEPARATOR))
146         {
147                 ERROR("%"TS":%lu: Invalid pattern \"%"TS"\":\n"
148                       "        Relative patterns can only include one path component!\n"
149                       "        Maybe try \"%"TC"%"TS"\"?",
150                       path, line_no, pat, OS_PREFERRED_PATH_SEPARATOR, pat);
151                 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
152         }
153
154         return 0;
155 }
156
157 /*
158  * Read, parse, and validate a capture configuration file from either an on-disk
159  * file or an in-memory buffer.
160  *
161  * To read from a file, specify @config_file, and use NULL for @buf.
162  * To read from a buffer, specify @buf and @bufsize.
163  *
164  * @config must be initialized to all 0's.
165  *
166  * On success, 0 will be returned, and the resulting capture configuration will
167  * be stored in @config.
168  *
169  * On failure, a positive error code will be returned, and the contents of
170  * @config will be invalidated.
171  */
172 int
173 read_capture_config(const tchar *config_file, const void *buf,
174                     size_t bufsize, struct capture_config *config)
175 {
176         int ret;
177
178         /* [PrepopulateList] is used for apply, not capture.  But since we do
179          * understand it, recognize it, thereby avoiding the unrecognized
180          * section warning, but discard the resulting strings.  */
181         STRING_SET(prepopulate_pats);
182
183         struct text_file_section sections[] = {
184                 {T("ExclusionList"),
185                         &config->exclusion_pats},
186                 {T("ExclusionException"),
187                         &config->exclusion_exception_pats},
188                 {T("PrepopulateList"),
189                         &prepopulate_pats},
190         };
191         void *mem;
192
193         ret = do_load_text_file(config_file, buf, bufsize, &mem,
194                                 sections, ARRAY_LEN(sections),
195                                 LOAD_TEXT_FILE_REMOVE_QUOTES, mangle_pat);
196         if (ret)
197                 return ret;
198
199         FREE(prepopulate_pats.strings);
200
201         config->buf = mem;
202         return 0;
203 }
204
205 void
206 destroy_capture_config(struct capture_config *config)
207 {
208         FREE(config->exclusion_pats.strings);
209         FREE(config->exclusion_exception_pats.strings);
210         FREE(config->buf);
211 }
212
213 /*
214  * Determine whether a path matches any wildcard pattern in a list.
215  *
216  * Special rules apply about what form @path must be in; see match_path().
217  */
218 bool
219 match_pattern_list(const tchar *path, size_t path_nchars,
220                    const struct string_set *list)
221 {
222         for (size_t i = 0; i < list->num_strings; i++)
223                 if (match_path(path, path_nchars, list->strings[i],
224                                OS_PREFERRED_PATH_SEPARATOR, true))
225                         return true;
226         return false;
227 }
228
229 /*
230  * Determine whether the filesystem @path should be excluded from capture, based
231  * on the current capture configuration file.
232  *
233  * The @path must be given relative to the root of the capture, but with a
234  * leading path separator.  For example, if the file "in/file" is being tested
235  * and the library user ran wimlib_add_image(wim, "in", ...), then the directory
236  * "in" is the root of the capture and the path should be specified as "/file".
237  *
238  * Also, all path separators in @path must be OS_PREFERRED_PATH_SEPARATOR, there
239  * cannot be trailing slashes, and there cannot be consecutive path separators.
240  *
241  * As a special case, the empty string will be interpreted as a single path
242  * separator (which means the root of capture itself).
243  */
244 bool
245 should_exclude_path(const tchar *path, size_t path_nchars,
246                     const struct capture_config *config)
247 {
248         tchar dummy[2];
249
250         if (!config)
251                 return false;
252
253         if (!*path) {
254                 dummy[0] = OS_PREFERRED_PATH_SEPARATOR;
255                 dummy[1] = T('\0');
256                 path = dummy;
257                 path_nchars = 1;
258         }
259
260         return match_pattern_list(path, path_nchars, &config->exclusion_pats) &&
261               !match_pattern_list(path, path_nchars, &config->exclusion_exception_pats);
262
263 }