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/error.h"
31 #include "wimlib/paths.h"
34 # include "wimlib/win32.h" /* for fnmatch() equivalent */
42 canonicalize_pattern(const tchar *pat, tchar **canonical_pat_ret)
46 if (pat[0] != T('/') && pat[0] != T('\\') &&
47 pat[0] != T('\0') && pat[1] == T(':'))
49 /* Pattern begins with drive letter */
50 if (pat[2] != T('/') && pat[2] != T('\\')) {
51 /* Something like c:file, which is actually a path
52 * relative to the current working directory on the c:
53 * drive. We require paths with drive letters to be
55 ERROR("Invalid path \"%"TS"\"; paths including drive letters "
56 "must be absolute!", pat);
57 ERROR("Maybe try \"%"TC":/%"TS"\"?",
59 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
62 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
63 "being removed.", pat);
64 /* Strip the drive letter */
67 canonical_pat = canonicalize_fs_path(pat);
69 return WIMLIB_ERR_NOMEM;
70 *canonical_pat_ret = canonical_pat;
75 copy_and_canonicalize_pattern_list(const struct wimlib_pattern_list *list,
76 struct wimlib_pattern_list *copy)
80 copy->pats = CALLOC(list->num_pats, sizeof(list->pats[0]));
82 return WIMLIB_ERR_NOMEM;
83 copy->num_pats = list->num_pats;
84 for (size_t i = 0; i < list->num_pats; i++) {
85 ret = canonicalize_pattern(list->pats[i], ©->pats[i]);
93 copy_and_canonicalize_capture_config(const struct wimlib_capture_config *config,
94 struct wimlib_capture_config **config_copy_ret)
96 struct wimlib_capture_config *config_copy;
99 config_copy = CALLOC(1, sizeof(struct wimlib_capture_config));
101 ret = WIMLIB_ERR_NOMEM;
102 goto out_free_capture_config;
104 ret = copy_and_canonicalize_pattern_list(&config->exclusion_pats,
105 &config_copy->exclusion_pats);
107 goto out_free_capture_config;
108 ret = copy_and_canonicalize_pattern_list(&config->exclusion_exception_pats,
109 &config_copy->exclusion_exception_pats);
111 goto out_free_capture_config;
112 *config_copy_ret = config_copy;
114 out_free_capture_config:
115 free_capture_config(config_copy);
121 destroy_pattern_list(struct wimlib_pattern_list *list)
123 for (size_t i = 0; i < list->num_pats; i++)
129 free_capture_config(struct wimlib_capture_config *config)
132 destroy_pattern_list(&config->exclusion_pats);
133 destroy_pattern_list(&config->exclusion_exception_pats);
139 match_pattern(const tchar *path,
140 const tchar *path_basename,
141 const struct wimlib_pattern_list *list)
143 for (size_t i = 0; i < list->num_pats; i++) {
145 const tchar *pat = list->pats[i];
148 if (*pat == T('/')) {
149 /* Absolute path from root of capture */
152 if (tstrchr(pat, T('/')))
153 /* Relative path from root of capture */
156 /* A file name pattern */
157 string = path_basename;
160 /* Warning: on Windows native builds, fnmatch() calls the
161 * replacement function in win32.c. */
162 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
168 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
172 DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
179 /* Return true if the image capture configuration file indicates we should
180 * exclude the filename @path from capture.
182 * If @exclude_prefix is %true, the part of the path up and including the name
183 * of the directory being captured is not included in the path for matching
184 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
185 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
189 exclude_path(const tchar *path, size_t path_len,
190 const struct wimlib_capture_config *config, bool exclude_prefix)
194 const tchar *basename = path_basename_with_len(path, path_len);
195 if (exclude_prefix) {
196 wimlib_assert(path_len >= config->_prefix_num_tchars);
197 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
198 path[config->_prefix_num_tchars] == T('/'))
200 path += config->_prefix_num_tchars;
203 return match_pattern(path, basename, &config->exclusion_pats) &&
204 !match_pattern(path, basename, &config->exclusion_exception_pats);