]> wimlib.net Git - wimlib/blob - src/template.c
Use LGPLv3+ for src/*.c
[wimlib] / src / template.c
1 /*
2  * template.c
3  *
4  * API to reference a template image to optimize later writing of a WIM file.
5  */
6
7 /*
8  * Copyright (C) 2013 Eric Biggers
9  *
10  * This file is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option) any
13  * later version.
14  *
15  * This file is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this file; 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.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/error.h"
31 #include "wimlib/lookup_table.h"
32 #include "wimlib/metadata.h"
33 #include "wimlib/util.h"
34
35 /* Returns %true iff the metadata of @inode and @template_inode are reasonably
36  * consistent with them being the same, unmodified file.  */
37 static bool
38 inode_metadata_consistent(const struct wim_inode *inode,
39                           const struct wim_inode *template_inode,
40                           const struct wim_lookup_table *template_lookup_table)
41 {
42         /* Must have exact same creation time and last write time.  */
43         if (inode->i_creation_time != template_inode->i_creation_time ||
44             inode->i_last_write_time != template_inode->i_last_write_time)
45                 return false;
46
47         /* Last access time may have stayed the same or increased, but certainly
48          * shouldn't have decreased.  */
49         if (inode->i_last_access_time < template_inode->i_last_access_time)
50                 return false;
51
52         /* Must have same number of alternate data stream entries.  */
53         if (inode->i_num_ads != template_inode->i_num_ads)
54                 return false;
55
56         /* If the stream entries for the inode are for some reason not resolved,
57          * then the hashes are already available and the point of this function
58          * is defeated.  */
59         if (!inode->i_resolved)
60                 return false;
61
62         /* Iterate through each stream and do some more checks.  */
63         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
64                 const struct wim_lookup_table_entry *lte, *template_lte;
65
66                 lte = inode_stream_lte_resolved(inode, i);
67                 template_lte = inode_stream_lte(template_inode, i,
68                                                 template_lookup_table);
69
70                 /* Compare stream sizes.  */
71                 if (lte && template_lte) {
72                         if (lte->size != template_lte->size)
73                                 return false;
74
75                         /* If hash happens to be available, compare with template.  */
76                         if (!lte->unhashed && !template_lte->unhashed &&
77                             !hashes_equal(lte->hash, template_lte->hash))
78                                 return false;
79
80                 } else if (lte && lte->size) {
81                         return false;
82                 } else if (template_lte && template_lte->size) {
83                         return false;
84                 }
85         }
86
87         /* All right, barring a full checksum and given that the inodes share a
88          * path and the user isn't trying to trick us, these inodes most likely
89          * refer to the same file.  */
90         return true;
91 }
92
93 /**
94  * Given an inode @inode that has been determined to be "the same" as another
95  * inode @template_inode in either the same WIM or another WIM, retrieve some
96  * useful stream information (e.g. checksums) from @template_inode.
97  *
98  * This assumes that the streams for @inode have been resolved (to point
99  * directly to the appropriate `struct wim_lookup_table_entry's)  but do not
100  * necessarily have checksum information filled in.
101  */
102 static int
103 inode_copy_checksums(struct wim_inode *inode,
104                      struct wim_inode *template_inode,
105                      WIMStruct *wim,
106                      WIMStruct *template_wim)
107 {
108         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
109                 struct wim_lookup_table_entry *lte, *template_lte;
110                 struct wim_lookup_table_entry *replace_lte;
111
112                 lte = inode_stream_lte_resolved(inode, i);
113                 template_lte = inode_stream_lte(template_inode, i,
114                                                 template_wim->lookup_table);
115
116                 /* Only take action if both entries exist, the entry for @inode
117                  * has no checksum calculated, but the entry for @template_inode
118                  * does.  */
119                 if (lte == NULL || template_lte == NULL ||
120                     !lte->unhashed || template_lte->unhashed)
121                         continue;
122
123                 wimlib_assert(lte->refcnt == inode->i_nlink);
124
125                 /* If the WIM of the template image is the same as the WIM of
126                  * the new image, then @template_lte can be used directly.
127                  *
128                  * Otherwise, look for a stream with the same hash in the WIM of
129                  * the new image.  If found, use it; otherwise re-use the entry
130                  * being discarded, filling in the hash.  */
131
132                 if (wim == template_wim)
133                         replace_lte = template_lte;
134                 else
135                         replace_lte = lookup_stream(wim->lookup_table,
136                                                     template_lte->hash);
137
138                 list_del(&lte->unhashed_list);
139                 if (replace_lte) {
140                         free_lookup_table_entry(lte);
141                 } else {
142                         copy_hash(lte->hash, template_lte->hash);
143                         lte->unhashed = 0;
144                         lookup_table_insert(wim->lookup_table, lte);
145                         lte->refcnt = 0;
146                         replace_lte = lte;
147                 }
148
149                 if (i == 0)
150                         inode->i_lte = replace_lte;
151                 else
152                         inode->i_ads_entries[i - 1].lte = replace_lte;
153
154                 replace_lte->refcnt += inode->i_nlink;
155         }
156         return 0;
157 }
158
159 struct reference_template_args {
160         WIMStruct *wim;
161         WIMStruct *template_wim;
162 };
163
164 static int
165 dentry_reference_template(struct wim_dentry *dentry, void *_args)
166 {
167         int ret;
168         struct wim_dentry *template_dentry;
169         struct wim_inode *inode, *template_inode;
170         struct reference_template_args *args = _args;
171         WIMStruct *wim = args->wim;
172         WIMStruct *template_wim = args->template_wim;
173
174         if (dentry->d_inode->i_visited)
175                 return 0;
176
177         ret = calculate_dentry_full_path(dentry);
178         if (ret)
179                 return ret;
180
181         template_dentry = get_dentry(template_wim, dentry->_full_path,
182                                      WIMLIB_CASE_SENSITIVE);
183         if (template_dentry == NULL) {
184                 DEBUG("\"%"TS"\": newly added file", dentry->_full_path);
185                 return 0;
186         }
187
188         inode = dentry->d_inode;
189         template_inode = template_dentry->d_inode;
190
191         if (inode_metadata_consistent(inode, template_inode,
192                                       template_wim->lookup_table)) {
193                 /*DEBUG("\"%"TS"\": No change detected", dentry->_full_path);*/
194                 ret = inode_copy_checksums(inode, template_inode,
195                                            wim, template_wim);
196                 inode->i_visited = 1;
197         } else {
198                 DEBUG("\"%"TS"\": change detected!", dentry->_full_path);
199                 ret = 0;
200         }
201         return ret;
202 }
203
204 /* API function documented in wimlib.h  */
205 WIMLIBAPI int
206 wimlib_reference_template_image(WIMStruct *wim, int new_image,
207                                 WIMStruct *template_wim, int template_image,
208                                 int flags)
209 {
210         int ret;
211         struct wim_image_metadata *new_imd;
212
213         if (flags != 0)
214                 return WIMLIB_ERR_INVALID_PARAM;
215
216         if (wim == NULL || template_wim == NULL)
217                 return WIMLIB_ERR_INVALID_PARAM;
218
219         if (wim == template_wim && new_image == template_image)
220                 return WIMLIB_ERR_INVALID_PARAM;
221
222         if (new_image < 1 || new_image > wim->hdr.image_count)
223                 return WIMLIB_ERR_INVALID_IMAGE;
224
225         if (!wim_has_metadata(wim))
226                 return WIMLIB_ERR_METADATA_NOT_FOUND;
227
228         new_imd = wim->image_metadata[new_image - 1];
229         if (!new_imd->modified)
230                 return WIMLIB_ERR_INVALID_PARAM;
231
232         ret = select_wim_image(template_wim, template_image);
233         if (ret)
234                 return ret;
235
236         struct reference_template_args args = {
237                 .wim = wim,
238                 .template_wim = template_wim,
239         };
240
241         ret = for_dentry_in_tree(new_imd->root_dentry,
242                                  dentry_reference_template, &args);
243         dentry_tree_clear_inode_visited(new_imd->root_dentry);
244         return ret;
245 }