4 * API to reference a template image to optimize later writing of a WIM file.
8 * Copyright (C) 2013 Eric Biggers
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
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
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/.
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"
35 /* Returns %true iff the metadata of @inode and @template_inode are reasonably
36 * consistent with them being the same, unmodified file. */
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)
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)
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)
52 /* Must have same number of alternate data stream entries. */
53 if (inode->i_num_ads != template_inode->i_num_ads)
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
59 if (!inode->i_resolved)
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;
66 lte = inode_stream_lte_resolved(inode, i);
67 template_lte = inode_stream_lte(template_inode, i,
68 template_lookup_table);
70 /* Compare stream sizes. */
71 if (lte && template_lte) {
72 if (lte->size != template_lte->size)
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))
80 } else if (lte && lte->size) {
82 } else if (template_lte && template_lte->size) {
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. */
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.
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.
103 inode_copy_checksums(struct wim_inode *inode,
104 struct wim_inode *template_inode,
106 WIMStruct *template_wim)
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;
112 lte = inode_stream_lte_resolved(inode, i);
113 template_lte = inode_stream_lte(template_inode, i,
114 template_wim->lookup_table);
116 /* Only take action if both entries exist, the entry for @inode
117 * has no checksum calculated, but the entry for @template_inode
119 if (lte == NULL || template_lte == NULL ||
120 !lte->unhashed || template_lte->unhashed)
123 wimlib_assert(lte->refcnt == inode->i_nlink);
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.
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. */
132 if (wim == template_wim)
133 replace_lte = template_lte;
135 replace_lte = lookup_stream(wim->lookup_table,
138 list_del(<e->unhashed_list);
140 free_lookup_table_entry(lte);
142 copy_hash(lte->hash, template_lte->hash);
144 lookup_table_insert(wim->lookup_table, lte);
150 inode->i_lte = replace_lte;
152 inode->i_ads_entries[i - 1].lte = replace_lte;
154 replace_lte->refcnt += inode->i_nlink;
159 struct reference_template_args {
161 WIMStruct *template_wim;
165 dentry_reference_template(struct wim_dentry *dentry, void *_args)
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;
174 if (dentry->d_inode->i_visited)
177 ret = calculate_dentry_full_path(dentry);
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);
188 inode = dentry->d_inode;
189 template_inode = template_dentry->d_inode;
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,
196 inode->i_visited = 1;
198 DEBUG("\"%"TS"\": change detected!", dentry->_full_path);
204 /* API function documented in wimlib.h */
206 wimlib_reference_template_image(WIMStruct *wim, int new_image,
207 WIMStruct *template_wim, int template_image,
211 struct wim_image_metadata *new_imd;
214 return WIMLIB_ERR_INVALID_PARAM;
216 if (wim == NULL || template_wim == NULL)
217 return WIMLIB_ERR_INVALID_PARAM;
219 if (wim == template_wim && new_image == template_image)
220 return WIMLIB_ERR_INVALID_PARAM;
222 if (new_image < 1 || new_image > wim->hdr.image_count)
223 return WIMLIB_ERR_INVALID_IMAGE;
225 if (!wim_has_metadata(wim))
226 return WIMLIB_ERR_METADATA_NOT_FOUND;
228 new_imd = wim->image_metadata[new_image - 1];
229 if (!new_imd->modified)
230 return WIMLIB_ERR_INVALID_PARAM;
232 ret = select_wim_image(template_wim, template_image);
236 struct reference_template_args args = {
238 .template_wim = template_wim,
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);