Commit 8da3e652 8da3e6526878d300df0dc9574091b9bc324d302e by Sergey Poznyakoff

Fix opening remote mailbox URLs with path component.

* include/mailutils/folder.h (mu_folder_create_from_record): Change
type of the 2nd argument to mu_url_t.
* include/mailutils/url.h (mu_url_uplevel): New proto.
* mailbox/folder.c (mu_folder_create_from_record): Change
type of the 2nd argument to mu_url_t.
(mu_folder_create): Reflect changes to mu_folder_create_from_record.
* mailbox/mailbox.c (mailbox_folder_create): Use mu_url_uplevel.
* mailbox/url.c (mu_url_uplevel): New function.
1 parent a03f9a76
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2005, 2007 Free Software Foundation, Inc.
Copyright (C) 1999, 2000, 2001, 2005, 2007,
2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......@@ -39,7 +40,7 @@ typedef int (*mu_folder_enumerate_fp) (mu_folder_t, struct mu_list_response *,
/* Constructor/destructor and possible types. */
extern int mu_folder_create (mu_folder_t *, const char *);
extern int mu_folder_create_from_record (mu_folder_t *, const char *,
extern int mu_folder_create_from_record (mu_folder_t *, mu_url_t url,
mu_record_t);
extern void mu_folder_destroy (mu_folder_t *);
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2005, 2007,
2008 Free Software Foundation, Inc.
2008, 2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......@@ -28,6 +28,7 @@ extern "C" {
extern int mu_url_create (mu_url_t *, const char *name);
extern int mu_url_dup (mu_url_t old_url, mu_url_t *new_url);
extern int mu_url_uplevel (mu_url_t url, mu_url_t *upurl);
extern void mu_url_destroy (mu_url_t *);
extern int mu_url_parse (mu_url_t);
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2005, 2007 Free Software Foundation, Inc.
Copyright (C) 1999, 2000, 2005, 2007, 2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006,
2007 Free Software Foundation, Inc.
2007, 2008, 2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......@@ -60,7 +60,7 @@ mu_folder_match (const char *name, void *pattern, int flags)
there could be cases where you'll want a different folder for the same URL,
there is not easy way to do this. */
int
mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
mu_folder_create_from_record (mu_folder_t *pfolder, mu_url_t url,
mu_record_t record)
{
if (!pfolder)
......@@ -69,8 +69,8 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
if (record ||
/* Look in the registrar list(iterator), for a possible concrete mailbox
implementation that could match the URL. */
mu_registrar_lookup (name, MU_FOLDER_ATTRIBUTE_DIRECTORY, &record, NULL)
== 0)
mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_DIRECTORY, &record,
NULL) == 0)
{
int (*f_init) (mu_folder_t) = NULL;
......@@ -78,30 +78,15 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
if (f_init)
{
int status;
mu_url_t url;
mu_folder_t folder;
int (*u_init) (mu_url_t) = NULL;
/* Parse the url, it may be a bad one and we should bailout if this
failed. */
if ((status = mu_url_create (&url, name)) != 0)
return status;
status = mu_url_parse (url);
if (status)
{
mu_url_destroy (&url);
return status;
}
mu_record_get_url (record, &u_init);
if (u_init)
{
status = u_init (url);
if (status)
{
mu_url_destroy (&url);
return status;
}
return status;
}
mu_monitor_wrlock (&folder_lock);
......@@ -111,7 +96,7 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
{
folder->ref++;
*pfolder = folder;
mu_url_destroy (&url);
mu_url_destroy (&url); /* FIXME: Hmm */
mu_monitor_unlock (&folder_lock);
return 0;
}
......@@ -149,8 +134,6 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
{
if (folder->monitor)
mu_monitor_destroy (&folder->monitor, folder);
if (folder->url)
mu_url_destroy (&folder->url);
free (folder);
}
}
......@@ -164,7 +147,18 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
int
mu_folder_create (mu_folder_t *pfolder, const char *name)
{
return mu_folder_create_from_record (pfolder, name, NULL);
int rc;
mu_url_t url;
rc = mu_url_create (&url, name);
if (rc)
return rc;
rc = mu_url_parse (url);
if (rc == 0)
rc = mu_folder_create_from_record (pfolder, url, NULL);
if (rc)
mu_url_destroy (&url);
return 0;
}
/* The folder is destroy if it is the last reference. */
......@@ -213,7 +207,6 @@ mu_folder_destroy (mu_folder_t *pfolder)
if (folder->stream)
mu_stream_destroy (&(folder->stream), folder);
if (folder->url)
mu_url_destroy (&(folder->url));
free (folder);
}
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006,
2007, 2008 Free Software Foundation, Inc.
2007, 2008, 2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......@@ -49,31 +49,23 @@ mailbox_folder_create (mu_mailbox_t mbox, const char *name,
mu_record_t record)
{
int rc;
char *fname;
if ((rc = mu_url_aget_path (mbox->url, &fname)))
mu_url_t url;
if ((rc = mu_url_uplevel (mbox->url, &url)))
{
if (rc == MU_ERR_NOENT)
{
fname = strdup (mu_url_to_string (mbox->url));
if (!fname)
return ENOMEM;
rc = mu_url_dup (mbox->url, &url);
if (rc)
return rc;
}
else
return rc;
}
if (mu_url_is_scheme (mbox->url, "file")
|| mu_url_is_scheme (mbox->url, "mbox")
|| mu_url_is_scheme (mbox->url, "mh")
|| mu_url_is_scheme (mbox->url, "maildir"))
{
char *p = strrchr (fname, '/'); /* FIXME: Is this always appropriate? */
if (p)
*p = 0;
}
rc = mu_folder_create_from_record (&mbox->folder, fname, record);
free (fname);
rc = mu_folder_create_from_record (&mbox->folder, url, record);
if (rc)
mu_url_destroy (&url);
return rc;
}
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2007, 2008 Free Software Foundation, Inc.
Copyright (C) 1999, 2000, 2001, 2007, 2008,
2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......@@ -181,6 +182,42 @@ mu_url_dup (mu_url_t old_url, mu_url_t *new_url)
return rc;
}
int
mu_url_uplevel (mu_url_t url, mu_url_t *upurl)
{
int rc;
char *p;
mu_url_t new_url;
if (!url->path)
return MU_ERR_NOENT;
p = strrchr (url->path, '/');
rc = mu_url_dup (url, &new_url);
if (rc == 0)
{
if (!p || p == url->path)
{
free (new_url->path);
new_url->path = NULL;
}
else
{
size_t size = p - url->path;
new_url->path = realloc (new_url->path, size + 1);
if (!new_url->path)
{
mu_url_destroy (&new_url);
return ENOMEM;
}
memcpy (new_url->path, url->path, size);
new_url->path[size] = 0;
}
*upurl = new_url;
}
return rc;
}
void
mu_url_destroy (mu_url_t * purl)
{
......