+static int
+reference_resource_paths(WIMStruct *wim,
+ const tchar * const *resource_wimfiles,
+ unsigned num_resource_wimfiles,
+ int ref_flags,
+ int open_flags,
+ wimlib_progress_func_t progress_func)
+{
+ WIMStruct **resource_wims;
+ unsigned i;
+ int ret;
+
+ resource_wims = CALLOC(num_resource_wimfiles, sizeof(resource_wims[0]));
+ if (!resource_wims)
+ return WIMLIB_ERR_NOMEM;
+
+ for (i = 0; i < num_resource_wimfiles; i++) {
+ DEBUG("Referencing resources from path \"%"TS"\"",
+ resource_wimfiles[i]);
+ ret = wimlib_open_wim(resource_wimfiles[i], open_flags,
+ &resource_wims[i], progress_func);
+ if (ret)
+ goto out_free_resource_wims;
+ }
+
+ ret = wimlib_reference_resources(wim, resource_wims,
+ num_resource_wimfiles, ref_flags);
+ if (ret)
+ goto out_free_resource_wims;
+
+ for (i = 0; i < num_resource_wimfiles; i++)
+ list_add_tail(&resource_wims[i]->subwim_node, &wim->subwims);
+
+ ret = 0;
+ goto out_free_array;
+
+out_free_resource_wims:
+ for (i = 0; i < num_resource_wimfiles; i++)
+ wimlib_free(resource_wims[i]);
+out_free_array:
+ FREE(resource_wims);
+ return ret;
+}
+
+static int
+reference_resource_glob(WIMStruct *wim, const tchar *refglob,
+ int ref_flags, int open_flags,
+ wimlib_progress_func_t progress_func)
+{
+ glob_t globbuf;
+ int ret;
+
+ /* Note: glob() is replaced in Windows native builds. */
+ ret = tglob(refglob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
+ if (ret) {
+ if (ret == GLOB_NOMATCH) {
+ if (ref_flags & WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH) {
+ ERROR("Found no files for glob \"%"TS"\"", refglob);
+ return WIMLIB_ERR_GLOB_HAD_NO_MATCHES;
+ } else {
+ return reference_resource_paths(wim,
+ &refglob,
+ 1,
+ ref_flags,
+ open_flags,
+ progress_func);
+ }
+ } else {
+ ERROR_WITH_ERRNO("Failed to process glob \"%"TS"\"", refglob);
+ if (ret == GLOB_NOSPACE)
+ return WIMLIB_ERR_NOMEM;
+ else
+ return WIMLIB_ERR_READ;
+ }
+ }
+
+ ret = reference_resource_paths(wim,
+ (const tchar * const *)globbuf.gl_pathv,
+ globbuf.gl_pathc,
+ ref_flags,
+ open_flags,
+ progress_func);
+ globfree(&globbuf);
+ return ret;
+}
+
+/* API function documented in wimlib.h */
+WIMLIBAPI int
+wimlib_reference_resource_files(WIMStruct *wim,
+ const tchar * const * resource_wimfiles_or_globs,
+ unsigned count,
+ int ref_flags,
+ int open_flags,
+ wimlib_progress_func_t progress_func)
+{
+ unsigned i;
+ int ret;
+
+ if (ref_flags & WIMLIB_REF_FLAG_GLOB_ENABLE) {
+ for (i = 0; i < count; i++) {
+ ret = reference_resource_glob(wim,
+ resource_wimfiles_or_globs[i],
+ ref_flags,
+ open_flags,
+ progress_func);
+ if (ret)
+ return ret;
+ }
+ return 0;
+ } else {
+ return reference_resource_paths(wim, resource_wimfiles_or_globs,
+ count, ref_flags,
+ open_flags, progress_func);
+ }
+}