4 * Reference resources from external WIM file(s).
8 * Copyright (C) 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/error.h"
32 #include "wimlib/glob.h"
33 #include "wimlib/lookup_table.h"
34 #include "wimlib/wim.h"
36 #define WIMLIB_REF_MASK_PUBLIC (WIMLIB_REF_FLAG_GLOB_ENABLE | \
37 WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH)
40 lte_clone_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
42 struct wim_lookup_table *lookup_table = _lookup_table;
44 if (lookup_stream(lookup_table, lte->hash))
45 return 0; /* Resource already present. */
47 lte = clone_lookup_table_entry(lte);
49 return WIMLIB_ERR_NOMEM;
51 lookup_table_insert(lookup_table, lte);
56 lte_delete_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
58 struct wim_lookup_table *lookup_table = _lookup_table;
60 if (lte->out_refcnt) {
61 lookup_table_unlink(lookup_table, lte);
62 free_lookup_table_entry(lte);
67 /* API function documented in wimlib.h */
69 wimlib_reference_resources(WIMStruct *wim,
70 WIMStruct **resource_wims, unsigned num_resource_wims,
77 return WIMLIB_ERR_INVALID_PARAM;
79 if (num_resource_wims != 0 && resource_wims == NULL)
80 return WIMLIB_ERR_INVALID_PARAM;
82 if (ref_flags & ~WIMLIB_REF_MASK_PUBLIC)
83 return WIMLIB_ERR_INVALID_PARAM;
85 for (i = 0; i < num_resource_wims; i++)
86 if (resource_wims[i] == NULL)
87 return WIMLIB_ERR_INVALID_PARAM;
89 for_lookup_table_entry(wim->lookup_table, lte_zero_out_refcnt, NULL);
91 for (i = 0; i < num_resource_wims; i++) {
92 ret = for_lookup_table_entry(resource_wims[i]->lookup_table,
101 for_lookup_table_entry(wim->lookup_table, lte_delete_if_new,
107 reference_resource_paths(WIMStruct *wim,
108 const tchar * const *resource_wimfiles,
109 unsigned num_resource_wimfiles,
112 wimlib_progress_func_t progress_func)
114 WIMStruct **resource_wims;
118 resource_wims = CALLOC(num_resource_wimfiles, sizeof(resource_wims[0]));
120 return WIMLIB_ERR_NOMEM;
122 for (i = 0; i < num_resource_wimfiles; i++) {
123 DEBUG("Referencing resources from path \"%"TS"\"",
124 resource_wimfiles[i]);
125 ret = wimlib_open_wim(resource_wimfiles[i], open_flags,
126 &resource_wims[i], progress_func);
128 goto out_free_resource_wims;
131 ret = wimlib_reference_resources(wim, resource_wims,
132 num_resource_wimfiles, ref_flags);
134 goto out_free_resource_wims;
136 for (i = 0; i < num_resource_wimfiles; i++)
137 list_add_tail(&resource_wims[i]->subwim_node, &wim->subwims);
142 out_free_resource_wims:
143 for (i = 0; i < num_resource_wimfiles; i++)
144 wimlib_free(resource_wims[i]);
151 reference_resource_glob(WIMStruct *wim, const tchar *refglob,
152 int ref_flags, int open_flags,
153 wimlib_progress_func_t progress_func)
158 /* Note: glob() is replaced in Windows native builds. */
159 ret = tglob(refglob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
161 if (ret == GLOB_NOMATCH) {
162 if (ref_flags & WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH) {
163 ERROR("Found no files for glob \"%"TS"\"", refglob);
164 return WIMLIB_ERR_GLOB_HAD_NO_MATCHES;
166 return reference_resource_paths(wim,
174 ERROR_WITH_ERRNO("Failed to process glob \"%"TS"\"", refglob);
175 if (ret == GLOB_NOSPACE)
176 return WIMLIB_ERR_NOMEM;
178 return WIMLIB_ERR_READ;
182 ret = reference_resource_paths(wim,
183 (const tchar * const *)globbuf.gl_pathv,
192 /* API function documented in wimlib.h */
194 wimlib_reference_resource_files(WIMStruct *wim,
195 const tchar * const * resource_wimfiles_or_globs,
199 wimlib_progress_func_t progress_func)
204 if (ref_flags & ~WIMLIB_REF_MASK_PUBLIC)
205 return WIMLIB_ERR_INVALID_PARAM;
207 if (ref_flags & WIMLIB_REF_FLAG_GLOB_ENABLE) {
208 for (i = 0; i < count; i++) {
209 ret = reference_resource_glob(wim,
210 resource_wimfiles_or_globs[i],
219 return reference_resource_paths(wim, resource_wimfiles_or_globs,
221 open_flags, progress_func);