+ i = message->doc.termlist_begin ();
+ i.skip_to (prefix);
+
+ if (i != message->doc.termlist_end ())
+ direntry = talloc_strdup (local, (*i).c_str ());
+
+ if (i == message->doc.termlist_end () ||
+ strncmp (direntry, prefix, prefix_len))
+ {
+ /* A message document created by an old version of notmuch
+ * (prior to rename support) will have the filename in the
+ * data of the document rather than as a file-direntry term.
+ *
+ * It would be nice to do the upgrade of the document directly
+ * here, but the database is likely open in read-only mode. */
+ const char *data;
+
+ data = message->doc.get_data ().c_str ();
+
+ if (data == NULL)
+ INTERNAL_ERROR ("message with no filename");
+
+ message->filename = talloc_strdup (message, data);
+
+ return message->filename;
+ }
+
+ direntry += prefix_len;
+
+ directory_id = strtol (direntry, &colon, 10);
+
+ if (colon == NULL || *colon != ':')
+ INTERNAL_ERROR ("malformed direntry");
+
+ basename = colon + 1;
+
+ *colon = '\0';
+