]> wimlib.net Git - wimlib/blob - src/capture_common.c
canonicalize_fs_path(): Retain backslashes
[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/error.h"
31 #include "wimlib/paths.h"
32
33 #ifdef __WIN32__
34 #  include "wimlib/win32.h" /* for fnmatch() equivalent */
35 #else
36 #  include <fnmatch.h>
37 #endif
38 #include <string.h>
39
40
41 static int
42 canonicalize_pattern(const tchar *pat, tchar **canonical_pat_ret)
43 {
44         tchar *canonical_pat;
45
46         if (pat[0] != T('/') && pat[0] != T('\\') &&
47             pat[0] != T('\0') && pat[1] == T(':'))
48         {
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
54                          * absolute. */
55                         ERROR("Invalid path \"%"TS"\"; paths including drive letters "
56                               "must be absolute!", pat);
57                         ERROR("Maybe try \"%"TC":/%"TS"\"?",
58                               pat[0], pat + 2);
59                         return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
60                 }
61
62                 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
63                         "being removed.", pat);
64                 /* Strip the drive letter */
65                 pat += 2;
66         }
67         canonical_pat = canonicalize_fs_path(pat);
68         if (!canonical_pat)
69                 return WIMLIB_ERR_NOMEM;
70         zap_backslashes(canonical_pat);
71         *canonical_pat_ret = canonical_pat;
72         return 0;
73 }
74
75 static int
76 copy_and_canonicalize_pattern_list(const struct wimlib_pattern_list *list,
77                                    struct wimlib_pattern_list *copy)
78 {
79         int ret = 0;
80
81         copy->pats = CALLOC(list->num_pats, sizeof(list->pats[0]));
82         if (!copy->pats)
83                 return WIMLIB_ERR_NOMEM;
84         copy->num_pats = list->num_pats;
85         for (size_t i = 0; i < list->num_pats; i++) {
86                 ret = canonicalize_pattern(list->pats[i], &copy->pats[i]);
87                 if (ret)
88                         break;
89         }
90         return ret;
91 }
92
93 int
94 copy_and_canonicalize_capture_config(const struct wimlib_capture_config *config,
95                                      struct wimlib_capture_config **config_copy_ret)
96 {
97         struct wimlib_capture_config *config_copy;
98         int ret;
99
100         config_copy = CALLOC(1, sizeof(struct wimlib_capture_config));
101         if (!config_copy) {
102                 ret = WIMLIB_ERR_NOMEM;
103                 goto out_free_capture_config;
104         }
105         ret = copy_and_canonicalize_pattern_list(&config->exclusion_pats,
106                                                  &config_copy->exclusion_pats);
107         if (ret)
108                 goto out_free_capture_config;
109         ret = copy_and_canonicalize_pattern_list(&config->exclusion_exception_pats,
110                                                  &config_copy->exclusion_exception_pats);
111         if (ret)
112                 goto out_free_capture_config;
113         *config_copy_ret = config_copy;
114         goto out;
115 out_free_capture_config:
116         free_capture_config(config_copy);
117 out:
118         return ret;
119 }
120
121 static void
122 destroy_pattern_list(struct wimlib_pattern_list *list)
123 {
124         for (size_t i = 0; i < list->num_pats; i++)
125                 FREE(list->pats[i]);
126         FREE(list->pats);
127 }
128
129 void
130 free_capture_config(struct wimlib_capture_config *config)
131 {
132         if (config) {
133                 destroy_pattern_list(&config->exclusion_pats);
134                 destroy_pattern_list(&config->exclusion_exception_pats);
135                 FREE(config);
136         }
137 }
138
139 static bool
140 match_pattern(const tchar *path,
141               const tchar *path_basename,
142               const struct wimlib_pattern_list *list)
143 {
144         for (size_t i = 0; i < list->num_pats; i++) {
145
146                 const tchar *pat = list->pats[i];
147                 const tchar *string;
148
149                 if (*pat == T('/')) {
150                         /* Absolute path from root of capture */
151                         string = path;
152                 } else {
153                         if (tstrchr(pat, T('/')))
154                                 /* Relative path from root of capture */
155                                 string = path + 1;
156                         else
157                                 /* A file name pattern */
158                                 string = path_basename;
159                 }
160
161                 /* Warning: on Windows native builds, fnmatch() calls the
162                  * replacement function in win32.c. */
163                 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
164                                 #ifdef FNM_CASEFOLD
165                                         | FNM_CASEFOLD
166                                 #endif
167                             ) == 0)
168                 {
169                         DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
170                               string, pat);
171                         return true;
172                 } else {
173                         DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
174                                string, pat);
175                 }
176         }
177         return false;
178 }
179
180 /* Return true if the image capture configuration file indicates we should
181  * exclude the filename @path from capture.
182  *
183  * If @exclude_prefix is %true, the part of the path up and including the name
184  * of the directory being captured is not included in the path for matching
185  * purposes.  This allows, for example, a pattern like /hiberfil.sys to match a
186  * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
187  * directory.
188  */
189 bool
190 exclude_path(const tchar *path, size_t path_len,
191              const struct wimlib_capture_config *config, bool exclude_prefix)
192 {
193         if (!config)
194                 return false;
195         const tchar *basename = path_basename_with_len(path, path_len);
196         if (exclude_prefix) {
197                 wimlib_assert(path_len >= config->_prefix_num_tchars);
198                 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
199                     path[config->_prefix_num_tchars] == T('/'))
200                 {
201                         path += config->_prefix_num_tchars;
202                 }
203         }
204         return match_pattern(path, basename, &config->exclusion_pats) &&
205                 !match_pattern(path, basename, &config->exclusion_exception_pats);
206
207 }