+ return 0;
+}
+
+static int
+cmp_object_ids(const struct wim_inode *inode1,
+ const struct wim_inode *inode2, int cmp_flags)
+{
+ const void *objid1, *objid2;
+ u32 len1, len2;
+
+ objid1 = inode_get_object_id(inode1, &len1);
+ objid2 = inode_get_object_id(inode2, &len2);
+
+ if (!objid1 && !objid2)
+ return 0;
+
+ if (objid1 && !objid2) {
+ if (cmp_flags & WIMLIB_CMP_FLAG_UNIX_MODE)
+ return 0;
+ ERROR("%"TS" unexpectedly lost its object ID",
+ inode_any_full_path(inode1));
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+
+ if (!objid1 && objid2) {
+ ERROR("%"TS" unexpectedly gained an object ID",
+ inode_any_full_path(inode1));
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+
+ if (len1 != len2 || memcmp(objid1, objid2, len1) != 0) {
+ ERROR("Object ID of %"TS" differs",
+ inode_any_full_path(inode1));
+ fprintf(stderr, "objid1=");
+ print_byte_field(objid1, len1, stderr);
+ fprintf(stderr, "\nobjid2=");
+ print_byte_field(objid2, len2, stderr);
+ fprintf(stderr, "\n");
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+
+ return 0;
+}
+
+static int
+cmp_unix_metadata(const struct wim_inode *inode1,
+ const struct wim_inode *inode2, int cmp_flags)
+{
+ struct wimlib_unix_data dat1, dat2;
+ bool present1, present2;
+
+ present1 = inode_get_unix_data(inode1, &dat1);
+ present2 = inode_get_unix_data(inode2, &dat2);
+
+ if (!present1 && !present2)
+ return 0;
+
+ if (present1 && !present2) {
+ if (cmp_flags & (WIMLIB_CMP_FLAG_NTFS_3G_MODE |
+ WIMLIB_CMP_FLAG_WINDOWS_MODE))
+ return 0;
+ ERROR("%"TS" unexpectedly lost its UNIX metadata",
+ inode_any_full_path(inode1));
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+
+ if (!present1 && present2) {
+ if (cmp_flags & WIMLIB_CMP_FLAG_UNIX_MODE)
+ return 0;
+ ERROR("%"TS" unexpectedly gained UNIX metadata",
+ inode_any_full_path(inode1));
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+
+ if (memcmp(&dat1, &dat2, sizeof(dat1)) != 0) {
+ ERROR("UNIX metadata of %"TS" differs: "
+ "[uid=%u, gid=%u, mode=0%o, rdev=%u] vs. "
+ "[uid=%u, gid=%u, mode=0%o, rdev=%u]",
+ inode_any_full_path(inode1),
+ dat1.uid, dat1.gid, dat1.mode, dat1.rdev,
+ dat2.uid, dat2.gid, dat2.mode, dat2.rdev);
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+
+ return 0;
+}
+
+static int
+cmp_xattr_names(const void *p1, const void *p2)
+{
+ const struct wim_xattr_entry *entry1 = *(const struct wim_xattr_entry **)p1;
+ const struct wim_xattr_entry *entry2 = *(const struct wim_xattr_entry **)p2;
+ int res;
+
+ res = entry1->name_len - entry2->name_len;
+ if (res)
+ return res;
+
+ return memcmp(entry1->name, entry2->name, entry1->name_len);
+}
+
+/* Validate and sort by name a list of extended attributes */
+static int
+parse_xattrs(const void *xattrs, u32 len,
+ const struct wim_xattr_entry *entries[],
+ u32 *num_entries_p)
+{
+ u32 limit = *num_entries_p;
+ u32 num_entries = 0;
+ const struct wim_xattr_entry *entry = xattrs;
+
+ while ((void *)entry < xattrs + len) {
+ if (!valid_xattr_entry(entry, xattrs + len - (void *)entry)) {
+ ERROR("Invalid xattr entry");
+ return WIMLIB_ERR_INVALID_XATTR;
+ }
+ if (num_entries >= limit) {
+ ERROR("Too many xattr entries");
+ return WIMLIB_ERR_INVALID_XATTR;
+ }
+ entries[num_entries++] = entry;
+ entry = xattr_entry_next(entry);
+ }
+
+ if (num_entries == 0) {
+ ERROR("No xattr entries");
+ return WIMLIB_ERR_INVALID_XATTR;
+ }
+
+ qsort(entries, num_entries, sizeof(entries[0]), cmp_xattr_names);
+
+ for (u32 i = 1; i < num_entries; i++) {
+ if (cmp_xattr_names(&entries[i - 1], &entries[i]) == 0) {
+ ERROR("Duplicate xattr names");
+ return WIMLIB_ERR_INVALID_XATTR;
+ }
+ }
+
+ *num_entries_p = num_entries;
+ return 0;
+}
+
+static int
+cmp_xattrs(const struct wim_inode *inode1, const struct wim_inode *inode2,
+ int cmp_flags)
+{
+ const void *xattrs1, *xattrs2;
+ u32 len1, len2;
+
+ xattrs1 = inode_get_xattrs(inode1, &len1);
+ xattrs2 = inode_get_xattrs(inode2, &len2);
+
+ if (!xattrs1 && !xattrs2) {
+ return 0;
+ } else if (xattrs1 && !xattrs2) {
+ if (cmp_flags & WIMLIB_CMP_FLAG_NTFS_3G_MODE)
+ return 0;
+ ERROR("%"TS" unexpectedly lost its xattrs",
+ inode_any_full_path(inode1));
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ } else if (!xattrs1 && xattrs2) {
+ ERROR("%"TS" unexpectedly gained xattrs",
+ inode_any_full_path(inode1));
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ } else {
+ const int max_entries = 64;
+ const struct wim_xattr_entry *entries1[max_entries];
+ const struct wim_xattr_entry *entries2[max_entries];
+ u32 xattr_count1 = max_entries;
+ u32 xattr_count2 = max_entries;
+ int ret;
+
+ ret = parse_xattrs(xattrs1, len1, entries1, &xattr_count1);
+ if (ret) {
+ ERROR("%"TS": invalid xattrs",
+ inode_any_full_path(inode1));
+ return ret;
+ }
+ ret = parse_xattrs(xattrs2, len2, entries2, &xattr_count2);
+ if (ret) {
+ ERROR("%"TS": invalid xattrs",
+ inode_any_full_path(inode2));
+ return ret;
+ }
+ if (xattr_count1 != xattr_count2) {
+ ERROR("%"TS": number of xattrs changed. had %u "
+ "before, now has %u", inode_any_full_path(inode1),
+ xattr_count1, xattr_count2);
+ }
+ for (u32 i = 0; i < xattr_count1; i++) {
+ const struct wim_xattr_entry *entry1 = entries1[i];
+ const struct wim_xattr_entry *entry2 = entries2[i];
+
+ if (entry1->value_len != entry2->value_len ||
+ entry1->name_len != entry2->name_len ||
+ entry1->flags != entry2->flags ||
+ memcmp(entry1->name, entry2->name,
+ entry1->name_len) ||
+ memcmp(entry1->name + entry1->name_len + 1,
+ entry2->name + entry2->name_len + 1,
+ le16_to_cpu(entry1->value_len)))
+ {
+ ERROR("xattr %.*s of %"TS" differs",
+ entry1->name_len, entry1->name,
+ inode_any_full_path(inode1));
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+ }
+ return 0;
+ }
+}
+
+/*
+ * ext4 only supports timestamps from years 1901 to 2446, more specifically the
+ * range [-0x80000000, 0x380000000) seconds relative to the start of UNIX epoch.
+ */
+static bool
+in_ext4_range(u64 ts)
+{
+ return ts >= time_t_to_wim_timestamp(-0x80000000LL) &&
+ ts < time_t_to_wim_timestamp(0x380000000LL);
+}
+
+static bool
+timestamps_differ(u64 ts1, u64 ts2, int cmp_flags)
+{
+ if (ts1 == ts2)
+ return false;
+ if ((cmp_flags & WIMLIB_CMP_FLAG_EXT4) &&
+ (!in_ext4_range(ts1) || !in_ext4_range(ts2)))
+ return false;
+ return true;
+}
+
+static int
+cmp_timestamps(const struct wim_inode *inode1, const struct wim_inode *inode2,
+ int cmp_flags)
+{
+ if (timestamps_differ(inode1->i_creation_time,
+ inode2->i_creation_time, cmp_flags) &&
+ !(cmp_flags & WIMLIB_CMP_FLAG_UNIX_MODE)) {
+ ERROR("Creation time of %"TS" differs; %"PRIu64" != %"PRIu64,
+ inode_any_full_path(inode1),
+ inode1->i_creation_time, inode2->i_creation_time);
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+
+ if (timestamps_differ(inode1->i_last_write_time,
+ inode2->i_last_write_time, cmp_flags)) {
+ ERROR("Last write time of %"TS" differs; %"PRIu64" != %"PRIu64,
+ inode_any_full_path(inode1),
+ inode1->i_last_write_time, inode2->i_last_write_time);
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+
+ if (timestamps_differ(inode1->i_last_access_time,
+ inode2->i_last_access_time, cmp_flags) &&
+ /*
+ * On Windows, sometimes a file's last access time will end up as
+ * the current time rather than the expected time. Maybe caused by
+ * some OS process scanning the files?
+ */
+ !(cmp_flags & WIMLIB_CMP_FLAG_WINDOWS_MODE)) {
+ ERROR("Last access time of %"TS" differs; %"PRIu64" != %"PRIu64,
+ inode_any_full_path(inode1),
+ inode1->i_last_access_time, inode2->i_last_access_time);
+ return WIMLIB_ERR_IMAGES_ARE_DIFFERENT;
+ }
+
+ return 0;
+}
+
+static int
+cmp_inodes(const struct wim_inode *inode1, const struct wim_inode *inode2,
+ const struct wim_image_metadata *imd1,
+ const struct wim_image_metadata *imd2, int cmp_flags)
+{
+ int ret;
+
+ /* Compare attributes */
+ ret = cmp_attributes(inode1, inode2, cmp_flags);
+ if (ret)
+ return ret;
+
+ /* Compare security descriptors */
+ ret = cmp_security(inode1, inode2, imd1, imd2, cmp_flags);
+ if (ret)
+ return ret;