Don't warn about [PrepopulateList] when reading capture config
[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/textfile.h"
35 #include "wimlib/wildcard.h"
36
37 #include <string.h>
38
39 void
40 do_capture_progress(struct add_image_params *params, int status,
41                     const struct wim_inode *inode)
42 {
43         switch (status) {
44         case WIMLIB_SCAN_DENTRY_OK:
45                 if (!(params->add_flags & WIMLIB_ADD_FLAG_VERBOSE))
46                         return;
47         case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
48         case WIMLIB_SCAN_DENTRY_EXCLUDED:
49         case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
50                 if (!(params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE))
51                         return;
52         }
53         params->progress.scan.status = status;
54         if (status == WIMLIB_SCAN_DENTRY_OK && inode->i_nlink == 1) {
55                 const struct wim_lookup_table_entry *lte;
56                 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
57                         lte = inode_stream_lte_resolved(inode, i);
58                         if (lte != NULL)
59                                 params->progress.scan.num_bytes_scanned += lte->size;
60                 }
61                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
62                         params->progress.scan.num_dirs_scanned++;
63                 else
64                         params->progress.scan.num_nondirs_scanned++;
65         }
66         if (params->progress_func) {
67                 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY,
68                                       &params->progress);
69         }
70 }
71
72 int
73 mangle_pat(tchar *pat, const tchar *path, unsigned long line_no)
74 {
75         if (!is_any_path_separator(pat[0]) &&
76             pat[0] != T('\0') && pat[1] == T(':'))
77         {
78                 /* Pattern begins with drive letter */
79                 if (!is_any_path_separator(pat[2])) {
80                         /* Something like c:file, which is actually a path
81                          * relative to the current working directory on the c:
82                          * drive.  We require paths with drive letters to be
83                          * absolute. */
84                         ERROR("%"TS":%lu: Invalid path \"%"TS"\"; paths including "
85                               "drive letters must be absolute!\n"
86                               "        Maybe try \"%"TC":\\%"TS"\"?",
87                               path, line_no, pat, pat[0], &pat[2]);
88                         return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
89                 }
90
91                 WARNING("%"TS":%lu: Pattern \"%"TS"\" starts with a drive "
92                         "letter, which is being removed.",
93                         path, line_no, pat);
94
95                 /* Strip the drive letter.  */
96                 tmemmove(pat, pat + 2, tstrlen(pat + 2) + 1);
97         }
98
99         /* Translate all possible path separators into the operating system's
100          * preferred path separator.  */
101         for (tchar *p = pat; *p; p++)
102                 if (is_any_path_separator(*p))
103                         *p = OS_PREFERRED_PATH_SEPARATOR;
104         return 0;
105 }
106
107 int
108 do_read_capture_config_file(const tchar *config_file, const void *buf,
109                             size_t bufsize, struct capture_config *config)
110 {
111         int ret;
112         STRING_SET(prepopulate_pats);
113         struct text_file_section sections[] = {
114                 {T("ExclusionList"),
115                         &config->exclusion_pats},
116                 {T("ExclusionException"),
117                         &config->exclusion_exception_pats},
118                 {T("PrepopulateList"),
119                         &prepopulate_pats},
120         };
121         void *mem;
122
123         ret = do_load_text_file(config_file, buf, bufsize, &mem,
124                                 sections, ARRAY_LEN(sections),
125                                 LOAD_TEXT_FILE_REMOVE_QUOTES, mangle_pat);
126         if (ret)
127                 return ret;
128
129         FREE(prepopulate_pats.strings);
130
131         config->buf = mem;
132         return 0;
133 }
134
135 void
136 destroy_capture_config(struct capture_config *config)
137 {
138         FREE(config->exclusion_pats.strings);
139         FREE(config->exclusion_exception_pats.strings);
140         FREE(config->buf);
141 }
142
143 bool
144 match_pattern(const tchar *path,
145               const tchar *path_basename,
146               const struct string_set *list)
147 {
148         for (size_t i = 0; i < list->num_strings; i++) {
149
150                 const tchar *pat = list->strings[i];
151                 const tchar *string;
152
153                 if (*pat == OS_PREFERRED_PATH_SEPARATOR) {
154                         /* Absolute path from root of capture */
155                         string = path;
156                 } else {
157                         if (tstrchr(pat, OS_PREFERRED_PATH_SEPARATOR))
158                                 /* Relative path from root of capture */
159                                 string = path + 1;
160                         else
161                                 /* A file name pattern */
162                                 string = path_basename;
163                 }
164
165                 /* Warning: on Windows native builds, fnmatch() calls the
166                  * replacement function in win32.c. */
167                 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
168                                 #ifdef FNM_CASEFOLD
169                                         | FNM_CASEFOLD
170                                 #endif
171                             ) == 0)
172                 {
173                         DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
174                               string, pat);
175                         return true;
176                 }
177         }
178         return false;
179 }
180
181 /* Return true if the image capture configuration file indicates we should
182  * exclude the filename @path from capture.
183  *
184  * If @exclude_prefix is %true, the part of the path up and including the name
185  * of the directory being captured is not included in the path for matching
186  * purposes.  This allows, for example, a pattern like /hiberfil.sys to match a
187  * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
188  * directory.
189  */
190 bool
191 exclude_path(const tchar *path, size_t path_len,
192              const struct capture_config *config, bool exclude_prefix)
193 {
194         if (!config)
195                 return false;
196         const tchar *basename = path_basename_with_len(path, path_len);
197         if (exclude_prefix) {
198                 wimlib_assert(path_len >= config->prefix_num_tchars);
199                 if (!tmemcmp(config->prefix, path, config->prefix_num_tchars) &&
200                     path[config->prefix_num_tchars] == OS_PREFERRED_PATH_SEPARATOR)
201                 {
202                         path += config->prefix_num_tchars;
203                 }
204         }
205         return match_pattern(path, basename, &config->exclusion_pats) &&
206                 !match_pattern(path, basename, &config->exclusion_exception_pats);
207
208 }