+/* Can look up named data stream with colon syntax */
+#define LOOKUP_FLAG_ADS_OK 0x01
+
+/* Can look up directory (otherwise get -ENOTDIR) */
+#define LOOKUP_FLAG_DIRECTORY_OK 0x02
+
+/*
+ * Translate a path into the corresponding dentry, lookup table entry, and
+ * stream index in the mounted WIM image.
+ *
+ * Returns 0 or a -errno code. All of @dentry_ret, @lte_ret, and
+ * @stream_idx_ret are optional.
+ */
+static int
+wim_pathname_to_stream(const struct wimfs_context *ctx, const char *path,
+ int lookup_flags,
+ struct wim_dentry **dentry_ret,
+ struct wim_lookup_table_entry **lte_ret,
+ u16 *stream_idx_ret)
+{
+ WIMStruct *wim = ctx->wim;
+ struct wim_dentry *dentry;
+ struct wim_lookup_table_entry *lte;
+ u16 stream_idx;
+ const tchar *stream_name = NULL;
+ struct wim_inode *inode;
+ tchar *p = NULL;
+
+ lookup_flags |= ctx->default_lookup_flags;
+
+ if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
+ stream_name = path_stream_name(path);
+ if (stream_name) {
+ p = (tchar*)stream_name - 1;
+ *p = T('\0');
+ }
+ }
+
+ dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
+ if (p)
+ *p = T(':');
+ if (!dentry)
+ return -errno;
+
+ inode = dentry->d_inode;
+
+ if (!inode->i_resolved)
+ if (inode_resolve_streams(inode, wim->lookup_table, false))
+ return -EIO;
+
+ if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
+ && inode_is_directory(inode))
+ return -EISDIR;
+
+ if (stream_name) {
+ struct wim_ads_entry *ads_entry;
+
+ ads_entry = inode_get_ads_entry(inode, stream_name);
+ if (!ads_entry)
+ return -errno;
+
+ stream_idx = ads_entry - inode->i_ads_entries + 1;
+ lte = ads_entry->lte;
+ } else {
+ lte = inode_unnamed_stream_resolved(inode, &stream_idx);
+ }
+ if (dentry_ret)
+ *dentry_ret = dentry;
+ if (lte_ret)
+ *lte_ret = lte;
+ if (stream_idx_ret)
+ *stream_idx_ret = stream_idx;
+ return 0;
+}
+