2 * capture_common.c - Mostly code to handle excluding paths from capture.
6 * Copyright (C) 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
28 #include "wimlib/assert.h"
29 #include "wimlib/capture.h"
30 #include "wimlib/dentry.h"
31 #include "wimlib/error.h"
32 #include "wimlib/lookup_table.h"
33 #include "wimlib/paths.h"
34 #include "wimlib/wildcard.h"
39 canonicalize_pattern(const tchar *pat, tchar **canonical_pat_ret)
43 if (!is_any_path_separator(pat[0]) &&
44 pat[0] != T('\0') && pat[1] == T(':'))
46 /* Pattern begins with drive letter */
47 if (!is_any_path_separator(pat[2])) {
48 /* Something like c:file, which is actually a path
49 * relative to the current working directory on the c:
50 * drive. We require paths with drive letters to be
52 ERROR("Invalid path \"%"TS"\"; paths including drive letters "
53 "must be absolute!", pat);
54 ERROR("Maybe try \"%"TC":\\%"TS"\"?",
56 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
59 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
60 "being removed.", pat);
61 /* Strip the drive letter */
64 canonical_pat = canonicalize_fs_path(pat);
66 return WIMLIB_ERR_NOMEM;
68 /* Translate all possible path separators into the operating system's
69 * preferred path separator. */
70 for (tchar *p = canonical_pat; *p; p++)
71 if (is_any_path_separator(*p))
72 *p = OS_PREFERRED_PATH_SEPARATOR;
73 *canonical_pat_ret = canonical_pat;
78 copy_and_canonicalize_pattern_list(const struct wimlib_pattern_list *list,
79 struct wimlib_pattern_list *copy)
83 copy->pats = CALLOC(list->num_pats, sizeof(list->pats[0]));
85 return WIMLIB_ERR_NOMEM;
86 copy->num_pats = list->num_pats;
87 for (size_t i = 0; i < list->num_pats; i++) {
88 ret = canonicalize_pattern(list->pats[i], ©->pats[i]);
96 copy_and_canonicalize_capture_config(const struct wimlib_capture_config *config,
97 struct wimlib_capture_config **config_copy_ret)
99 struct wimlib_capture_config *config_copy;
102 config_copy = CALLOC(1, sizeof(struct wimlib_capture_config));
104 ret = WIMLIB_ERR_NOMEM;
105 goto out_free_capture_config;
107 ret = copy_and_canonicalize_pattern_list(&config->exclusion_pats,
108 &config_copy->exclusion_pats);
110 goto out_free_capture_config;
111 ret = copy_and_canonicalize_pattern_list(&config->exclusion_exception_pats,
112 &config_copy->exclusion_exception_pats);
114 goto out_free_capture_config;
115 *config_copy_ret = config_copy;
117 out_free_capture_config:
118 free_capture_config(config_copy);
124 destroy_pattern_list(struct wimlib_pattern_list *list)
126 for (size_t i = 0; i < list->num_pats; i++)
132 free_capture_config(struct wimlib_capture_config *config)
135 destroy_pattern_list(&config->exclusion_pats);
136 destroy_pattern_list(&config->exclusion_exception_pats);
142 match_pattern(const tchar *path,
143 const tchar *path_basename,
144 const struct wimlib_pattern_list *list)
146 for (size_t i = 0; i < list->num_pats; i++) {
148 const tchar *pat = list->pats[i];
151 if (*pat == OS_PREFERRED_PATH_SEPARATOR) {
152 /* Absolute path from root of capture */
155 if (tstrchr(pat, OS_PREFERRED_PATH_SEPARATOR))
156 /* Relative path from root of capture */
159 /* A file name pattern */
160 string = path_basename;
163 /* Warning: on Windows native builds, fnmatch() calls the
164 * replacement function in win32.c. */
165 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
171 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
175 DEBUG("\"%"TS"\" does not match the pattern \"%"TS"\"",
183 do_capture_progress(struct add_image_params *params, int status,
184 const struct wim_inode *inode)
187 case WIMLIB_SCAN_DENTRY_OK:
188 if (!(params->add_flags & WIMLIB_ADD_FLAG_VERBOSE))
190 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
191 case WIMLIB_SCAN_DENTRY_EXCLUDED:
192 if (!(params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE))
195 params->progress.scan.status = status;
196 if (status == WIMLIB_SCAN_DENTRY_OK && inode->i_nlink == 1) {
197 const struct wim_lookup_table_entry *lte;
198 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
199 lte = inode_stream_lte_resolved(inode, i);
201 params->progress.scan.num_bytes_scanned += lte->size;
203 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
204 params->progress.scan.num_dirs_scanned++;
206 params->progress.scan.num_nondirs_scanned++;
208 if (params->progress_func) {
209 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY,
214 /* Return true if the image capture configuration file indicates we should
215 * exclude the filename @path from capture.
217 * If @exclude_prefix is %true, the part of the path up and including the name
218 * of the directory being captured is not included in the path for matching
219 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
220 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
224 exclude_path(const tchar *path, size_t path_len,
225 const struct wimlib_capture_config *config, bool exclude_prefix)
229 const tchar *basename = path_basename_with_len(path, path_len);
230 if (exclude_prefix) {
231 wimlib_assert(path_len >= config->_prefix_num_tchars);
232 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
233 path[config->_prefix_num_tchars] == OS_PREFERRED_PATH_SEPARATOR)
235 path += config->_prefix_num_tchars;
238 return match_pattern(path, basename, &config->exclusion_pats) &&
239 !match_pattern(path, basename, &config->exclusion_exception_pats);