]> wimlib.net Git - wimlib/blob - src/capture_common.c
def3a13fe93501e60af9898eeac7cd5f5463f96c
[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.
48  * @inode
49  *      If @status is WIMLIB_SCAN_DENTRY_OK, this is a pointer to the WIM inode
50  *      that has been created for the scanned file.  The first time the file is
51  *      seen, inode->i_nlink will be 1.  On subsequent visits of the same inode
52  *      via additional hard links, inode->i_nlink will be greater than 1.
53  */
54 int
55 do_capture_progress(struct add_image_params *params, int status,
56                     const struct wim_inode *inode)
57 {
58         switch (status) {
59         case WIMLIB_SCAN_DENTRY_OK:
60                 if (!(params->add_flags & WIMLIB_ADD_FLAG_VERBOSE))
61                         return 0;
62                 break;
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                 break;
70         }
71         params->progress.scan.status = status;
72         if (status == WIMLIB_SCAN_DENTRY_OK && inode->i_nlink == 1) {
73
74                 /* Successful scan, and visiting inode for the first time  */
75
76                 /* Tally size of all data streams.  */
77                 const struct wim_lookup_table_entry *lte;
78                 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
79                         lte = inode_stream_lte_resolved(inode, i);
80                         if (lte)
81                                 params->progress.scan.num_bytes_scanned += lte->size;
82                 }
83
84                 /* Tally the file itself.  */
85                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
86                         params->progress.scan.num_dirs_scanned++;
87                 else
88                         params->progress.scan.num_nondirs_scanned++;
89         }
90
91         /* Call the user-provided progress function.  */
92         return call_progress(params->progfunc, WIMLIB_PROGRESS_MSG_SCAN_DENTRY,
93                              &params->progress, params->progctx);
94 }
95
96 /*
97  * Given a null-terminated pathname pattern @pat that has been read from line
98  * @line_no of the file @path, validate and canonicalize the pattern.
99  *
100  * On success, returns 0.
101  * On failure, returns WIMLIB_ERR_INVALID_CAPTURE_CONFIG.
102  * In either case, @pat may have been modified in-place (and possibly
103  * shortened).
104  */
105 int
106 mangle_pat(tchar *pat, const tchar *path, unsigned long line_no)
107 {
108         if (!is_any_path_separator(pat[0]) &&
109             pat[0] != T('\0') && pat[1] == T(':'))
110         {
111                 /* Pattern begins with drive letter.  */
112
113                 if (!is_any_path_separator(pat[2])) {
114                         /* Something like c:file, which is actually a path
115                          * relative to the current working directory on the c:
116                          * drive.  We require paths with drive letters to be
117                          * absolute.  */
118                         ERROR("%"TS":%lu: Invalid pattern \"%"TS"\":\n"
119                               "        Patterns including drive letters must be absolute!\n"
120                               "        Maybe try \"%"TC":%"TC"%"TS"\"?\n",
121                               path, line_no, pat,
122                               pat[0], OS_PREFERRED_PATH_SEPARATOR, &pat[2]);
123                         return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
124                 }
125
126                 WARNING("%"TS":%lu: Pattern \"%"TS"\" starts with a drive "
127                         "letter, which is being removed.",
128                         path, line_no, pat);
129
130                 /* Strip the drive letter.  */
131                 tmemmove(pat, pat + 2, tstrlen(pat + 2) + 1);
132         }
133
134         /* Collapse consecutive path separators, and translate both / and \ into
135          * / (UNIX) or \ (Windows).
136          *
137          * Note: we expect that this function produces patterns that can be used
138          * for both filesystem paths and WIM paths, so the desired path
139          * separators must be the same.  */
140         BUILD_BUG_ON(OS_PREFERRED_PATH_SEPARATOR != WIM_PATH_SEPARATOR);
141         do_canonicalize_path(pat, pat);
142
143         /* Relative patterns can only match file names, so they must be
144          * single-component only.  */
145         if (pat[0] != OS_PREFERRED_PATH_SEPARATOR &&
146             tstrchr(pat, OS_PREFERRED_PATH_SEPARATOR))
147         {
148                 ERROR("%"TS":%lu: Invalid pattern \"%"TS"\":\n"
149                       "        Relative patterns can only include one path component!\n"
150                       "        Maybe try \"%"TC"%"TS"\"?",
151                       path, line_no, pat, OS_PREFERRED_PATH_SEPARATOR, pat);
152                 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
153         }
154
155         return 0;
156 }
157
158 /*
159  * Read, parse, and validate a capture configuration file from either an on-disk
160  * file or an in-memory buffer.
161  *
162  * To read from a file, specify @config_file, and use NULL for @buf.
163  * To read from a buffer, specify @buf and @bufsize.
164  *
165  * @config must be initialized to all 0's.
166  *
167  * On success, 0 will be returned, and the resulting capture configuration will
168  * be stored in @config.
169  *
170  * On failure, a positive error code will be returned, and the contents of
171  * @config will be invalidated.
172  */
173 int
174 read_capture_config(const tchar *config_file, const void *buf,
175                     size_t bufsize, struct capture_config *config)
176 {
177         int ret;
178
179         /* [PrepopulateList] is used for apply, not capture.  But since we do
180          * understand it, recognize it, thereby avoiding the unrecognized
181          * section warning, but discard the resulting strings.  */
182         STRING_SET(prepopulate_pats);
183
184         struct text_file_section sections[] = {
185                 {T("ExclusionList"),
186                         &config->exclusion_pats},
187                 {T("ExclusionException"),
188                         &config->exclusion_exception_pats},
189                 {T("PrepopulateList"),
190                         &prepopulate_pats},
191         };
192         void *mem;
193
194         ret = do_load_text_file(config_file, buf, bufsize, &mem,
195                                 sections, ARRAY_LEN(sections),
196                                 LOAD_TEXT_FILE_REMOVE_QUOTES, mangle_pat);
197         if (ret)
198                 return ret;
199
200         FREE(prepopulate_pats.strings);
201
202         config->buf = mem;
203         return 0;
204 }
205
206 void
207 destroy_capture_config(struct capture_config *config)
208 {
209         FREE(config->exclusion_pats.strings);
210         FREE(config->exclusion_exception_pats.strings);
211         FREE(config->buf);
212 }
213
214 /*
215  * Determine whether a path matches any wildcard pattern in a list.
216  *
217  * Special rules apply about what form @path must be in; see match_path().
218  */
219 bool
220 match_pattern_list(const tchar *path, size_t path_nchars,
221                    const struct string_set *list)
222 {
223         for (size_t i = 0; i < list->num_strings; i++)
224                 if (match_path(path, path_nchars, list->strings[i],
225                                OS_PREFERRED_PATH_SEPARATOR, true))
226                         return true;
227         return false;
228 }
229
230 /*
231  * Determine whether the filesystem @path should be excluded from capture, based
232  * on the current capture configuration file.
233  *
234  * The @path must be given relative to the root of the capture, but with a
235  * leading path separator.  For example, if the file "in/file" is being tested
236  * and the library user ran wimlib_add_image(wim, "in", ...), then the directory
237  * "in" is the root of the capture and the path should be specified as "/file".
238  *
239  * Also, all path separators in @path must be OS_PREFERRED_PATH_SEPARATOR, there
240  * cannot be trailing slashes, and there cannot be consecutive path separators.
241  *
242  * As a special case, the empty string will be interpreted as a single path
243  * separator (which means the root of capture itself).
244  */
245 bool
246 should_exclude_path(const tchar *path, size_t path_nchars,
247                     const struct capture_config *config)
248 {
249         tchar dummy[2];
250
251         if (!config)
252                 return false;
253
254         if (!*path) {
255                 dummy[0] = OS_PREFERRED_PATH_SEPARATOR;
256                 dummy[1] = T('\0');
257                 path = dummy;
258                 path_nchars = 1;
259         }
260
261         return match_pattern_list(path, path_nchars, &config->exclusion_pats) &&
262               !match_pattern_list(path, path_nchars, &config->exclusion_exception_pats);
263
264 }