+ int ret;
+ for (int i = 0; i < wim->hdr.image_count; i++) {
+ struct wim_lookup_table_entry *lte, *tmp;
+ struct wim_image_metadata *imd = wim->image_metadata[i];
+ image_for_each_unhashed_stream_safe(lte, tmp, imd) {
+ ret = hash_unhashed_stream(lte, wim->lookup_table, NULL);
+ if (ret)
+ return ret;
+ }
+ }
+ return 0;
+}
+
+/*
+ * can_modify_wim - Check if a given WIM is writeable. This is only the case if
+ * it meets the following three conditions:
+ *
+ * 1. Write access is allowed to the underlying file (if any) at the filesystem level.
+ * 2. The WIM is not part of a spanned set.
+ * 3. The WIM_HDR_FLAG_READONLY flag is not set in the WIM header.
+ *
+ * Return value is 0 if writable; WIMLIB_ERR_WIM_IS_READONLY otherwise.
+ */
+int
+can_modify_wim(WIMStruct *wim)
+{
+ if (wim->filename) {
+ if (taccess(wim->filename, W_OK)) {
+ ERROR_WITH_ERRNO("Can't modify \"%"TS"\"", wim->filename);
+ return WIMLIB_ERR_WIM_IS_READONLY;
+ }
+ }
+ if (wim->hdr.total_parts != 1) {
+ ERROR("Cannot modify \"%"TS"\": is part of a spanned set",
+ wim->filename);
+ return WIMLIB_ERR_WIM_IS_READONLY;
+ }
+ if (wim->hdr.flags & WIM_HDR_FLAG_READONLY) {
+ ERROR("Cannot modify \"%"TS"\": is marked read-only",
+ wim->filename);
+ return WIMLIB_ERR_WIM_IS_READONLY;
+ }
+ return 0;
+}
+
+/*
+ * can_delete_from_wim - Check if files or images can be deleted from a given
+ * WIM file.
+ *
+ * This theoretically should be exactly the same as can_modify_wim(), but
+ * unfortunately, due to bugs in Microsoft's software that generate incorrect
+ * reference counts for some WIM resources, we need to run expensive
+ * verifications to make sure the reference counts are correct on all WIM
+ * resources. Otherwise we might delete a WIM resource whose reference count
+ * has fallen to 0, but is actually still referenced somewhere.
+ */
+int
+can_delete_from_wim(WIMStruct *wim)
+{
+ int ret;
+
+ ret = can_modify_wim(wim);
+ if (ret)
+ return ret;
+ if (!wim->refcnts_ok)
+ wim_recalculate_refcnts(wim);
+ return 0;
+}