lib: Abstract the extraction of a relative path from set_filename

We'll soon be having multiple entry points that accept a filename
path, so we want common code for getting a relative path from a
potentially absolute path.
This commit is contained in:
Carl Worth 2009-12-19 12:32:11 -08:00
parent 3a9c3ec9e7
commit ba12bf1f26
3 changed files with 41 additions and 19 deletions

View file

@ -587,6 +587,40 @@ timestamp_db_key (const char *key)
return strdup (key);
}
/* Given a legal 'path' for the database, return the relative path.
*
* The return value will be a pointer to the originl path contents,
* and will be either the original string (if 'path' was relative) or
* a portion of the string (if path was absolute and begins with the
* database path).
*/
const char *
_notmuch_database_relative_path (notmuch_database_t *notmuch,
const char *path)
{
const char *db_path, *relative;
unsigned int db_path_len;
db_path = notmuch_database_get_path (notmuch);
db_path_len = strlen (db_path);
relative = path;
if (*relative == '/') {
while (*relative == '/' && *(relative+1) == '/')
relative++;
if (strncmp (relative, db_path, db_path_len) == 0)
{
relative += db_path_len;
while (*relative == '/')
relative++;
}
}
return relative;
}
notmuch_status_t
notmuch_database_set_timestamp (notmuch_database_t *notmuch,
const char *key, time_t timestamp)

View file

@ -396,9 +396,7 @@ void
_notmuch_message_set_filename (notmuch_message_t *message,
const char *filename)
{
const char *s;
const char *db_path;
unsigned int db_path_len;
const char *relative;
if (message->filename) {
talloc_free (message->filename);
@ -408,22 +406,8 @@ _notmuch_message_set_filename (notmuch_message_t *message,
if (filename == NULL)
INTERNAL_ERROR ("Message filename cannot be NULL.");
s = filename;
db_path = notmuch_database_get_path (message->notmuch);
db_path_len = strlen (db_path);
if (*s == '/' && strlen (s) > db_path_len
&& strncmp (s, db_path, db_path_len) == 0)
{
s += db_path_len;
while (*s == '/') s++;
if (!*s)
INTERNAL_ERROR ("Message filename was same as db prefix.");
}
message->doc.set_data (s);
relative = _notmuch_database_relative_path (message->notmuch, filename);
message->doc.set_data (relative);
}
const char *

View file

@ -151,6 +151,10 @@ typedef enum _notmuch_private_status {
const char *
_find_prefix (const char *name);
const char *
_notmuch_database_relative_path (notmuch_database_t *notmuch,
const char *path);
/* thread.cc */
notmuch_thread_t *