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/assert.h"
30 #include "wimlib/dentry.h"
31 #include "wimlib/error.h"
32 #include "wimlib/lookup_table.h"
33 #include "wimlib/metadata.h"
34 #include "wimlib/util.h"
36 /* Returns %true iff the metadata of @inode and @template_inode are reasonably
37 * consistent with them being the same, unmodified file. */
39 inode_metadata_consistent(const struct wim_inode *inode,
40 const struct wim_inode *template_inode,
41 const struct wim_lookup_table *template_lookup_table)
43 /* Must have exact same creation time and last write time. */
44 if (inode->i_creation_time != template_inode->i_creation_time ||
45 inode->i_last_write_time != template_inode->i_last_write_time)
48 /* Last access time may have stayed the same or increased, but certainly
49 * shouldn't have decreased. */
50 if (inode->i_last_access_time < template_inode->i_last_access_time)
53 /* Must have same number of alternate data stream entries. */
54 if (inode->i_num_ads != template_inode->i_num_ads)
57 /* If the stream entries for the inode are for some reason not resolved,
58 * then the hashes are already available and the point of this function
60 if (!inode->i_resolved)
63 /* Iterate through each stream and do some more checks. */
64 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
65 const struct wim_lookup_table_entry *lte, *template_lte;
67 lte = inode_stream_lte_resolved(inode, i);
68 template_lte = inode_stream_lte(template_inode, i,
69 template_lookup_table);
71 /* Compare stream sizes. */
72 if (lte && template_lte) {
73 if (lte->size != template_lte->size)
76 /* If hash happens to be available, compare with template. */
77 if (!lte->unhashed && !template_lte->unhashed &&
78 !hashes_equal(lte->hash, template_lte->hash))
81 } else if (lte && lte->size) {
83 } else if (template_lte && template_lte->size) {
88 /* All right, barring a full checksum and given that the inodes share a
89 * path and the user isn't trying to trick us, these inodes most likely
90 * refer to the same file. */
95 * Given an inode @inode that has been determined to be "the same" as another
96 * inode @template_inode in either the same WIM or another WIM, retrieve some
97 * useful stream information (e.g. checksums) from @template_inode.
99 * This assumes that the streams for @inode have been resolved (to point
100 * directly to the appropriate `struct wim_lookup_table_entry's) but do not
101 * necessarily have checksum information filled in.
104 inode_copy_checksums(struct wim_inode *inode,
105 struct wim_inode *template_inode,
107 WIMStruct *template_wim)
109 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
110 struct wim_lookup_table_entry *lte, *template_lte;
111 struct wim_lookup_table_entry *replace_lte;
113 lte = inode_stream_lte_resolved(inode, i);
114 template_lte = inode_stream_lte(template_inode, i,
115 template_wim->lookup_table);
117 /* Only take action if both entries exist, the entry for @inode
118 * has no checksum calculated, but the entry for @template_inode
120 if (lte == NULL || template_lte == NULL ||
121 !lte->unhashed || template_lte->unhashed)
124 wimlib_assert(lte->refcnt == inode->i_nlink);
126 /* If the WIM of the template image is the same as the WIM of
127 * the new image, then @template_lte can be used directly.
129 * Otherwise, look for a stream with the same hash in the WIM of
130 * the new image. If found, use it; otherwise re-use the entry
131 * being discarded, filling in the hash. */
133 if (wim == template_wim)
134 replace_lte = template_lte;
136 replace_lte = lookup_stream(wim->lookup_table,
139 list_del(<e->unhashed_list);
141 free_lookup_table_entry(lte);
143 copy_hash(lte->hash, template_lte->hash);
145 lookup_table_insert(wim->lookup_table, lte);
151 inode->i_lte = replace_lte;
153 inode->i_ads_entries[i - 1].lte = replace_lte;
155 replace_lte->refcnt += inode->i_nlink;
160 struct reference_template_args {
162 WIMStruct *template_wim;
166 dentry_reference_template(struct wim_dentry *dentry, void *_args)
169 struct wim_dentry *template_dentry;
170 struct wim_inode *inode, *template_inode;
171 struct reference_template_args *args = _args;
172 WIMStruct *wim = args->wim;
173 WIMStruct *template_wim = args->template_wim;
175 if (dentry->d_inode->i_visited)
178 ret = calculate_dentry_full_path(dentry);
182 template_dentry = get_dentry(template_wim, dentry->_full_path,
183 WIMLIB_CASE_SENSITIVE);
184 if (template_dentry == NULL) {
185 DEBUG("\"%"TS"\": newly added file", dentry->_full_path);
189 inode = dentry->d_inode;
190 template_inode = template_dentry->d_inode;
192 if (inode_metadata_consistent(inode, template_inode,
193 template_wim->lookup_table)) {
194 /*DEBUG("\"%"TS"\": No change detected", dentry->_full_path);*/
195 ret = inode_copy_checksums(inode, template_inode,
197 inode->i_visited = 1;
199 DEBUG("\"%"TS"\": change detected!", dentry->_full_path);
205 /* API function documented in wimlib.h */
207 wimlib_reference_template_image(WIMStruct *wim, int new_image,
208 WIMStruct *template_wim, int template_image,
212 struct wim_image_metadata *new_imd;
215 return WIMLIB_ERR_INVALID_PARAM;
217 if (wim == NULL || template_wim == NULL)
218 return WIMLIB_ERR_INVALID_PARAM;
220 if (wim == template_wim && new_image == template_image)
221 return WIMLIB_ERR_INVALID_PARAM;
223 if (new_image < 1 || new_image > wim->hdr.image_count)
224 return WIMLIB_ERR_INVALID_IMAGE;
226 if (!wim_has_metadata(wim))
227 return WIMLIB_ERR_METADATA_NOT_FOUND;
229 new_imd = wim->image_metadata[new_image - 1];
230 if (!new_imd->modified)
231 return WIMLIB_ERR_INVALID_PARAM;
233 ret = select_wim_image(template_wim, template_image);
237 struct reference_template_args args = {
239 .template_wim = template_wim,
242 ret = for_dentry_in_tree(new_imd->root_dentry,
243 dentry_reference_template, &args);
244 dentry_tree_clear_inode_visited(new_imd->root_dentry);