+add_stream(struct wim_inode *inode, const char *path, int stream_type,
+ const utf16lechar *stream_name, struct blob_descriptor **blob_p,
+ struct list_head *unhashed_blobs)
+{
+ struct blob_descriptor *blob = *blob_p;
+ struct wim_inode_stream *strm;
+
+ strm = inode_get_stream(inode, stream_type, stream_name);
+ if (unlikely(strm)) {
+ /* Stream already existed. */
+ if (!blob)
+ return 0;
+ if (stream_blob_resolved(strm)) {
+ WARNING("\"%s\" has multiple nonempty streams "
+ "with the same type and name! Only the first "
+ "will be saved.", path);
+ return 0;
+ }
+ inode_replace_stream_blob(inode, strm, blob, NULL);
+ } else {
+ strm = inode_add_stream(inode, stream_type, stream_name, blob);
+ if (unlikely(!strm))
+ return WIMLIB_ERR_NOMEM;
+ }
+ prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
+ *blob_p = NULL;
+ return 0;
+
+}
+
+/* Save information about an NTFS attribute (stream) to a WIM inode. */
+static int
+scan_ntfs_attr(struct wim_inode *inode,
+ ntfs_inode *ni,
+ const char *path,
+ struct list_head *unhashed_blobs,
+ struct ntfs_volume_wrapper *volume,
+ ATTR_TYPES type,
+ const ATTR_RECORD *record)
+{
+ u64 data_size = ntfs_get_attribute_value_length(record);
+ const u32 name_nchars = record->name_length;
+ struct blob_descriptor *blob = NULL;
+ utf16lechar *stream_name = (utf16lechar *)NO_STREAM_NAME;
+ int ret;
+
+ if (unlikely(name_nchars)) {
+ /* Named stream */
+ stream_name = utf16le_dupz(attr_record_name(record),
+ name_nchars * sizeof(ntfschar));
+ if (!stream_name) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_cleanup;
+ }
+ }
+
+ if (unlikely(type == AT_REPARSE_POINT)) {
+ if (data_size < REPARSE_DATA_OFFSET) {
+ ERROR("Reparse point attribute of \"%s\" "
+ "is too short!", path);
+ ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
+ goto out_cleanup;
+ }
+ data_size -= REPARSE_DATA_OFFSET;
+
+ ret = read_reparse_header(ni, inode);
+ if (ret) {
+ ERROR_WITH_ERRNO("Error reading reparse point header "
+ "of \"%s\"", path);
+ goto out_cleanup;
+ }
+ }
+
+ /* If the stream is non-empty, set up a blob descriptor for it. */
+ if (data_size != 0) {
+ blob = new_blob_descriptor();
+ if (unlikely(!blob)) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_cleanup;
+ }
+
+ blob->ntfs_loc = MALLOC(sizeof(struct ntfs_location));
+ if (unlikely(!blob->ntfs_loc)) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_cleanup;
+ }
+
+ blob->blob_location = BLOB_IN_NTFS_VOLUME;
+ blob->size = data_size;
+ blob->ntfs_loc->volume = get_ntfs_volume(volume);
+ blob->ntfs_loc->mft_no = ni->mft_no;
+ blob->ntfs_loc->attr_type = type;
+
+ if (unlikely(name_nchars)) {
+ blob->ntfs_loc->attr_name_nchars = name_nchars;
+ blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
+ if (!blob->ntfs_loc->attr_name) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_cleanup;
+ }
+ } else {
+ blob->ntfs_loc->attr_name_nchars = 0;
+ blob->ntfs_loc->attr_name = AT_UNNAMED;
+ }
+
+ ret = set_attr_sort_key(ni, blob->ntfs_loc);
+ if (ret)
+ goto out_cleanup;
+ }
+
+ ret = add_stream(inode, path, attr_type_to_wimlib_stream_type(type),
+ stream_name, &blob, unhashed_blobs);
+out_cleanup:
+ free_blob_descriptor(blob);
+ if (stream_name != NO_STREAM_NAME)
+ FREE(stream_name);
+ return ret;
+}
+
+/* Scan attributes of the specified type from a file in the NTFS volume */
+static int
+scan_ntfs_attrs_with_type(struct wim_inode *inode,