4 * Reference blobs from external WIM file(s).
8 * Copyright (C) 2013, 2014 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/blob_table.h"
30 #include "wimlib/error.h"
31 #include "wimlib/glob.h"
32 #include "wimlib/wim.h"
34 #define WIMLIB_REF_MASK_PUBLIC (WIMLIB_REF_FLAG_GLOB_ENABLE | \
35 WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH)
37 struct reference_info {
39 struct list_head new_blobs;
40 struct list_head new_subwims;
42 struct blob_table *src_table;
46 init_reference_info(struct reference_info *info, WIMStruct *dest_wim,
49 info->dest_wim = dest_wim;
50 INIT_LIST_HEAD(&info->new_blobs);
51 INIT_LIST_HEAD(&info->new_subwims);
52 info->ref_flags = ref_flags;
56 commit_reference_info(struct reference_info *info)
58 list_splice(&info->new_subwims, &info->dest_wim->subwims);
62 rollback_reference_info(struct reference_info *info)
65 struct blob_descriptor *blob;
67 while (!list_empty(&info->new_subwims)) {
68 subwim = list_first_entry(&info->new_subwims,
69 WIMStruct, subwim_node);
70 list_del(&subwim->subwim_node);
74 while (!list_empty(&info->new_blobs)) {
75 blob = list_first_entry(&info->new_blobs,
76 struct blob_descriptor, blob_table_list);
77 list_del(&blob->blob_table_list);
78 blob_table_unlink(info->dest_wim->blob_table, blob);
79 free_blob_descriptor(blob);
84 commit_or_rollback_reference_info(struct reference_info *info, int ret)
87 rollback_reference_info(info);
89 commit_reference_info(info);
94 need_blob(const struct reference_info *info, const struct blob_descriptor *blob)
96 return !lookup_blob(info->dest_wim->blob_table, blob->hash);
100 reference_blob(struct reference_info *info, struct blob_descriptor *blob)
102 blob_table_insert(info->dest_wim->blob_table, blob);
103 list_add(&blob->blob_table_list, &info->new_blobs);
107 reference_subwim(struct reference_info *info, WIMStruct *subwim)
109 list_add(&subwim->subwim_node, &info->new_subwims);
113 blob_clone_if_new(struct blob_descriptor *blob, void *_info)
115 struct reference_info *info = _info;
117 if (need_blob(info, blob)) {
118 blob = clone_blob_descriptor(blob);
120 return WIMLIB_ERR_NOMEM;
121 reference_blob(info, blob);
126 /* API function documented in wimlib.h */
128 wimlib_reference_resources(WIMStruct *wim, WIMStruct **resource_wims,
129 unsigned num_resource_wims, int ref_flags)
132 struct reference_info info;
136 return WIMLIB_ERR_INVALID_PARAM;
138 if (num_resource_wims != 0 && resource_wims == NULL)
139 return WIMLIB_ERR_INVALID_PARAM;
141 if (ref_flags & ~WIMLIB_REF_MASK_PUBLIC)
142 return WIMLIB_ERR_INVALID_PARAM;
144 for (i = 0; i < num_resource_wims; i++)
145 if (resource_wims[i] == NULL)
146 return WIMLIB_ERR_INVALID_PARAM;
148 init_reference_info(&info, wim, ref_flags);
150 for (i = 0; i < num_resource_wims; i++) {
151 ret = for_blob_in_table(resource_wims[i]->blob_table,
152 blob_clone_if_new, &info);
157 return commit_or_rollback_reference_info(&info, ret);
161 blob_gift(struct blob_descriptor *blob, void *_info)
163 struct reference_info *info = _info;
165 blob_table_unlink(info->src_table, blob);
166 if (need_blob(info, blob))
167 reference_blob(info, blob);
169 free_blob_descriptor(blob);
174 reference_resource_path(struct reference_info *info, const tchar *path,
180 ret = wimlib_open_wim_with_progress(path, open_flags, &src_wim,
181 info->dest_wim->progfunc,
182 info->dest_wim->progctx);
186 info->src_table = src_wim->blob_table;
187 for_blob_in_table(src_wim->blob_table, blob_gift, info);
188 reference_subwim(info, src_wim);
193 reference_resource_paths(struct reference_info *info,
194 const tchar * const *paths, unsigned num_paths,
197 for (unsigned i = 0; i < num_paths; i++) {
198 int ret = reference_resource_path(info, paths[i], open_flags);
206 reference_resource_glob(struct reference_info *info,
207 const tchar *refglob, int open_flags)
212 /* Note: glob() is replaced in Windows native builds. */
213 ret = tglob(refglob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
215 if (ret == GLOB_NOMATCH) {
216 if (info->ref_flags &
217 WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH)
219 ERROR("Found no files for glob \"%"TS"\"", refglob);
220 return WIMLIB_ERR_GLOB_HAD_NO_MATCHES;
222 return reference_resource_path(info,
226 ERROR_WITH_ERRNO("Failed to process glob \"%"TS"\"", refglob);
227 if (ret == GLOB_NOSPACE)
228 return WIMLIB_ERR_NOMEM;
229 return WIMLIB_ERR_READ;
232 ret = reference_resource_paths(info,
233 (const tchar * const *)globbuf.gl_pathv,
241 reference_resource_globs(struct reference_info *info,
242 const tchar * const *globs, unsigned num_globs,
245 for (unsigned i = 0; i < num_globs; i++) {
246 int ret = reference_resource_glob(info, globs[i], open_flags);
253 /* API function documented in wimlib.h */
255 wimlib_reference_resource_files(WIMStruct *wim,
256 const tchar * const *paths_or_globs,
257 unsigned count, int ref_flags, int open_flags)
259 struct reference_info info;
262 if (ref_flags & ~WIMLIB_REF_MASK_PUBLIC)
263 return WIMLIB_ERR_INVALID_PARAM;
265 init_reference_info(&info, wim, ref_flags);
267 if (ref_flags & WIMLIB_REF_FLAG_GLOB_ENABLE)
268 ret = reference_resource_globs(&info, paths_or_globs, count, open_flags);
270 ret = reference_resource_paths(&info, paths_or_globs, count, open_flags);
272 return commit_or_rollback_reference_info(&info, ret);