]> wimlib.net Git - wimlib/blob - src/capture_common.c
match_pattern(): No DEBUG if pattern does not match
[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 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/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"
35
36 #include <string.h>
37
38 static int
39 canonicalize_pattern(const tchar *pat, tchar **canonical_pat_ret)
40 {
41         tchar *canonical_pat;
42
43         if (!is_any_path_separator(pat[0]) &&
44             pat[0] != T('\0') && pat[1] == T(':'))
45         {
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
51                          * absolute. */
52                         ERROR("Invalid path \"%"TS"\"; paths including drive letters "
53                               "must be absolute!", pat);
54                         ERROR("Maybe try \"%"TC":\\%"TS"\"?",
55                               pat[0], pat + 2);
56                         return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
57                 }
58
59                 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
60                         "being removed.", pat);
61                 /* Strip the drive letter */
62                 pat += 2;
63         }
64         canonical_pat = canonicalize_fs_path(pat);
65         if (!canonical_pat)
66                 return WIMLIB_ERR_NOMEM;
67
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;
74         return 0;
75 }
76
77 static int
78 copy_and_canonicalize_pattern_list(const struct wimlib_pattern_list *list,
79                                    struct wimlib_pattern_list *copy)
80 {
81         int ret = 0;
82
83         copy->pats = CALLOC(list->num_pats, sizeof(list->pats[0]));
84         if (!copy->pats)
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], &copy->pats[i]);
89                 if (ret)
90                         break;
91         }
92         return ret;
93 }
94
95 int
96 copy_and_canonicalize_capture_config(const struct wimlib_capture_config *config,
97                                      struct wimlib_capture_config **config_copy_ret)
98 {
99         struct wimlib_capture_config *config_copy;
100         int ret;
101
102         config_copy = CALLOC(1, sizeof(struct wimlib_capture_config));
103         if (!config_copy) {
104                 ret = WIMLIB_ERR_NOMEM;
105                 goto out_free_capture_config;
106         }
107         ret = copy_and_canonicalize_pattern_list(&config->exclusion_pats,
108                                                  &config_copy->exclusion_pats);
109         if (ret)
110                 goto out_free_capture_config;
111         ret = copy_and_canonicalize_pattern_list(&config->exclusion_exception_pats,
112                                                  &config_copy->exclusion_exception_pats);
113         if (ret)
114                 goto out_free_capture_config;
115         *config_copy_ret = config_copy;
116         goto out;
117 out_free_capture_config:
118         free_capture_config(config_copy);
119 out:
120         return ret;
121 }
122
123 static void
124 destroy_pattern_list(struct wimlib_pattern_list *list)
125 {
126         for (size_t i = 0; i < list->num_pats; i++)
127                 FREE(list->pats[i]);
128         FREE(list->pats);
129 }
130
131 void
132 free_capture_config(struct wimlib_capture_config *config)
133 {
134         if (config) {
135                 destroy_pattern_list(&config->exclusion_pats);
136                 destroy_pattern_list(&config->exclusion_exception_pats);
137                 FREE(config);
138         }
139 }
140
141 static bool
142 match_pattern(const tchar *path,
143               const tchar *path_basename,
144               const struct wimlib_pattern_list *list)
145 {
146         for (size_t i = 0; i < list->num_pats; i++) {
147
148                 const tchar *pat = list->pats[i];
149                 const tchar *string;
150
151                 if (*pat == OS_PREFERRED_PATH_SEPARATOR) {
152                         /* Absolute path from root of capture */
153                         string = path;
154                 } else {
155                         if (tstrchr(pat, OS_PREFERRED_PATH_SEPARATOR))
156                                 /* Relative path from root of capture */
157                                 string = path + 1;
158                         else
159                                 /* A file name pattern */
160                                 string = path_basename;
161                 }
162
163                 /* Warning: on Windows native builds, fnmatch() calls the
164                  * replacement function in win32.c. */
165                 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
166                                 #ifdef FNM_CASEFOLD
167                                         | FNM_CASEFOLD
168                                 #endif
169                             ) == 0)
170                 {
171                         DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
172                               string, pat);
173                         return true;
174                 }
175         }
176         return false;
177 }
178
179 void
180 do_capture_progress(struct add_image_params *params, int status,
181                     const struct wim_inode *inode)
182 {
183         switch (status) {
184         case WIMLIB_SCAN_DENTRY_OK:
185                 if (!(params->add_flags & WIMLIB_ADD_FLAG_VERBOSE))
186                         return;
187         case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
188         case WIMLIB_SCAN_DENTRY_EXCLUDED:
189         case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
190                 if (!(params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE))
191                         return;
192         }
193         params->progress.scan.status = status;
194         if (status == WIMLIB_SCAN_DENTRY_OK && inode->i_nlink == 1) {
195                 const struct wim_lookup_table_entry *lte;
196                 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
197                         lte = inode_stream_lte_resolved(inode, i);
198                         if (lte != NULL)
199                                 params->progress.scan.num_bytes_scanned += lte->size;
200                 }
201                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
202                         params->progress.scan.num_dirs_scanned++;
203                 else
204                         params->progress.scan.num_nondirs_scanned++;
205         }
206         if (params->progress_func) {
207                 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY,
208                                       &params->progress);
209         }
210 }
211
212 /* Return true if the image capture configuration file indicates we should
213  * exclude the filename @path from capture.
214  *
215  * If @exclude_prefix is %true, the part of the path up and including the name
216  * of the directory being captured is not included in the path for matching
217  * purposes.  This allows, for example, a pattern like /hiberfil.sys to match a
218  * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
219  * directory.
220  */
221 bool
222 exclude_path(const tchar *path, size_t path_len,
223              const struct wimlib_capture_config *config, bool exclude_prefix)
224 {
225         if (!config)
226                 return false;
227         const tchar *basename = path_basename_with_len(path, path_len);
228         if (exclude_prefix) {
229                 wimlib_assert(path_len >= config->_prefix_num_tchars);
230                 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
231                     path[config->_prefix_num_tchars] == OS_PREFERRED_PATH_SEPARATOR)
232                 {
233                         path += config->_prefix_num_tchars;
234                 }
235         }
236         return match_pattern(path, basename, &config->exclusion_pats) &&
237                 !match_pattern(path, basename, &config->exclusion_exception_pats);
238
239 }