+ ntfs_attr_search_ctx *actx;
+ struct ntfs_location *ntfs_loc;
+ int ret;
+ 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))
+ {
+ u64 data_size = ntfs_get_attribute_value_length(actx->attr);
+ u64 name_length = actx->attr->name_length;
+ u32 stream_id;
+
+ if (data_size == 0) {
+ /* Empty stream. No lookup table entry is needed. */
+ lte = NULL;
+ ntfs_loc = NULL;
+ } else {
+ ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
+ if (!ntfs_loc) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_put_actx;
+ }
+ ntfs_loc->ntfs_vol = vol;
+ ntfs_loc->path = MALLOC(path_len + 1);
+ if (!ntfs_loc->path) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_free_ntfs_loc;
+ }
+ memcpy(ntfs_loc->path, path, path_len + 1);
+ if (name_length) {
+ ntfs_loc->stream_name = MALLOC(name_length * 2);
+ if (!ntfs_loc->stream_name) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_free_ntfs_loc;
+ }
+ memcpy(ntfs_loc->stream_name,
+ attr_record_name(actx->attr),
+ actx->attr->name_length * 2);
+ ntfs_loc->stream_name_nchars = name_length;
+ }
+
+ lte = new_lookup_table_entry();
+ if (!lte) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_free_ntfs_loc;
+ }
+ lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
+ lte->ntfs_loc = ntfs_loc;
+ ntfs_loc = NULL;
+ if (type == AT_REPARSE_POINT) {
+ if (data_size < 8) {
+ ERROR("Invalid reparse data (only %u bytes)!",
+ (unsigned)data_size);
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out_free_lte;
+ }
+ lte->ntfs_loc->is_reparse_point = true;
+ lte->resource_entry.original_size = data_size - 8;
+ ret = read_reparse_tag(ni, lte->ntfs_loc,
+ &inode->i_reparse_tag);
+ if (ret)
+ goto out_free_lte;
+ } else {
+ lte->ntfs_loc->is_reparse_point = false;
+ lte->resource_entry.original_size = data_size;
+ }
+ }
+ if (name_length == 0) {
+ /* Unnamed data stream. Put the reference to it in the
+ * dentry's inode. */
+ if (inode->i_lte) {
+ ERROR("Found two un-named data streams for `%s'",
+ path);
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out_free_lte;
+ }
+ stream_id = 0;
+ 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;
+
+ new_ads_entry = inode_add_ads_utf16le(inode,
+ attr_record_name(actx->attr),
+ name_length * 2);
+ if (!new_ads_entry) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_free_lte;
+ }
+ wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
+ stream_id = new_ads_entry->stream_id;
+ new_ads_entry->lte = lte;
+ }
+ if (lte) {
+ lookup_table_insert_unhashed(lookup_table, lte,
+ inode, stream_id);
+ }
+ }
+ if (errno == ENOENT) {
+ ret = 0;
+ } else {
+ ERROR_WITH_ERRNO("Error listing NTFS attributes from `%s'",
+ path);
+ ret = WIMLIB_ERR_NTFS_3G;
+ }
+ goto out_put_actx;
+out_free_lte:
+ free_lookup_table_entry(lte);
+out_free_ntfs_loc:
+ if (ntfs_loc) {
+ FREE(ntfs_loc->path);
+ FREE(ntfs_loc->stream_name);
+ FREE(ntfs_loc);
+ }
+out_put_actx:
+ ntfs_attr_put_search_ctx(actx);
+ if (ret == 0)
+ DEBUG2("Successfully captured NTFS streams from `%s'", path);