+out_error:
+ ERROR_WITH_ERRNO("Error reading NTFS attribute");
+ return WIMLIB_ERR_NTFS_3G;
+}
+
+/* Load the streams from a file or reparse point in the NTFS volume into the WIM
+ * lookup table */
+static int capture_ntfs_streams(struct wim_dentry *dentry, ntfs_inode *ni,
+ char path[], size_t path_len,
+ struct wim_lookup_table *lookup_table,
+ ntfs_volume **ntfs_vol_p,
+ ATTR_TYPES type)
+{
+ ntfs_attr_search_ctx *actx;
+ u8 attr_hash[SHA1_HASH_SIZE];
+ struct ntfs_location *ntfs_loc = NULL;
+ int ret = 0;
+ struct wim_lookup_table_entry *lte;
+
+ DEBUG2("Capturing NTFS data streams from `%s'", path);
+
+ /* Get context to search the streams of the NTFS file. */
+ actx = ntfs_attr_get_search_ctx(ni, NULL);
+ if (!actx) {
+ ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
+ "context");
+ return WIMLIB_ERR_NTFS_3G;
+ }
+
+ /* Capture each data stream or reparse data stream. */
+ while (!ntfs_attr_lookup(type, NULL, 0,
+ CASE_SENSITIVE, 0, NULL, 0, actx))
+ {
+ char *stream_name_utf8;
+ u32 reparse_tag;
+ u64 data_size = ntfs_get_attribute_value_length(actx->attr);
+ u64 name_length = actx->attr->name_length;
+
+ if (data_size == 0) {
+ if (errno != 0) {
+ ERROR_WITH_ERRNO("Failed to get size of attribute of "
+ "`%s'", path);
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out_put_actx;
+ }
+ /* Empty stream. No lookup table entry is needed. */
+ lte = NULL;
+ } else {
+ if (type == AT_REPARSE_POINT && data_size < 8) {
+ ERROR("`%s': reparse point buffer too small",
+ path);
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out_put_actx;
+ }
+ /* Checksum the stream. */
+ ret = ntfs_attr_sha1sum(ni, actx->attr, attr_hash,
+ type == AT_REPARSE_POINT, &reparse_tag);
+ if (ret != 0)
+ goto out_put_actx;
+
+ /* Make a lookup table entry for the stream, or use an existing
+ * one if there's already an identical stream. */
+ lte = __lookup_resource(lookup_table, attr_hash);
+ ret = WIMLIB_ERR_NOMEM;
+ if (lte) {
+ lte->refcnt++;
+ } else {
+ ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
+ if (!ntfs_loc)
+ goto out_put_actx;
+ ntfs_loc->ntfs_vol_p = ntfs_vol_p;
+ ntfs_loc->path_utf8 = MALLOC(path_len + 1);
+ if (!ntfs_loc->path_utf8)
+ goto out_free_ntfs_loc;
+ memcpy(ntfs_loc->path_utf8, path, path_len + 1);
+ if (name_length) {
+ ntfs_loc->stream_name_utf16 = MALLOC(name_length * 2);
+ if (!ntfs_loc->stream_name_utf16)
+ goto out_free_ntfs_loc;
+ memcpy(ntfs_loc->stream_name_utf16,
+ attr_record_name(actx->attr),
+ actx->attr->name_length * 2);
+ ntfs_loc->stream_name_utf16_num_chars = name_length;
+ }
+
+ lte = new_lookup_table_entry();
+ if (!lte)
+ goto out_free_ntfs_loc;
+ lte->ntfs_loc = ntfs_loc;
+ lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
+ if (type == AT_REPARSE_POINT) {
+ dentry->d_inode->i_reparse_tag = reparse_tag;
+ ntfs_loc->is_reparse_point = true;
+ lte->resource_entry.original_size = data_size - 8;
+ lte->resource_entry.size = data_size - 8;
+ } else {
+ ntfs_loc->is_reparse_point = false;
+ lte->resource_entry.original_size = data_size;
+ lte->resource_entry.size = data_size;
+ }
+ ntfs_loc = NULL;
+ DEBUG("Add resource for `%s' (size = %"PRIu64")",
+ dentry->file_name_utf8,
+ lte->resource_entry.original_size);
+ copy_hash(lte->hash, attr_hash);
+ lookup_table_insert(lookup_table, lte);
+ }
+ }
+ if (name_length == 0) {
+ /* Unnamed data stream. Put the reference to it in the
+ * dentry's inode. */
+ if (dentry->d_inode->i_lte) {
+ ERROR("Found two un-named data streams for "
+ "`%s'", path);
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out_free_lte;
+ }
+ dentry->d_inode->i_lte = lte;
+ } else {
+ /* Named data stream. Put the reference to it in the
+ * alternate data stream entries */
+ struct wim_ads_entry *new_ads_entry;
+ size_t stream_name_utf8_len;
+
+ ret = utf16_to_utf8((const char*)attr_record_name(actx->attr),
+ name_length * 2,
+ &stream_name_utf8,
+ &stream_name_utf8_len);
+ if (ret != 0)
+ goto out_free_lte;
+ new_ads_entry = inode_add_ads(dentry->d_inode, stream_name_utf8);
+ FREE(stream_name_utf8);
+ if (!new_ads_entry)
+ goto out_free_lte;
+
+ wimlib_assert(new_ads_entry->stream_name_len == name_length * 2);
+
+ new_ads_entry->lte = lte;
+ }
+ }
+ ret = 0;
+ goto out_put_actx;
+out_free_lte:
+ free_lookup_table_entry(lte);
+out_free_ntfs_loc:
+ if (ntfs_loc) {
+ FREE(ntfs_loc->path_utf8);
+ FREE(ntfs_loc->stream_name_utf16);
+ FREE(ntfs_loc);
+ }
+out_put_actx:
+ ntfs_attr_put_search_ctx(actx);
+ if (ret == 0)
+ DEBUG2("Successfully captured NTFS streams from `%s'", path);
+ else
+ ERROR("Failed to capture NTFS streams from `%s", path);