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
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2005, 2007 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2000, 2001, 2005, 2007,
3 2009 Free Software Foundation, Inc.
3 4
4 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public 6 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 *, ...@@ -39,7 +40,7 @@ typedef int (*mu_folder_enumerate_fp) (mu_folder_t, struct mu_list_response *,
39 40
40 /* Constructor/destructor and possible types. */ 41 /* Constructor/destructor and possible types. */
41 extern int mu_folder_create (mu_folder_t *, const char *); 42 extern int mu_folder_create (mu_folder_t *, const char *);
42 extern int mu_folder_create_from_record (mu_folder_t *, const char *, 43 extern int mu_folder_create_from_record (mu_folder_t *, mu_url_t url,
43 mu_record_t); 44 mu_record_t);
44 45
45 extern void mu_folder_destroy (mu_folder_t *); 46 extern void mu_folder_destroy (mu_folder_t *);
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2005, 2007, 2 Copyright (C) 1999, 2000, 2001, 2005, 2007,
3 2008 Free Software Foundation, Inc. 3 2008, 2009 Free Software Foundation, Inc.
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 6 modify it under the terms of the GNU Lesser General Public
...@@ -28,6 +28,7 @@ extern "C" { ...@@ -28,6 +28,7 @@ extern "C" {
28 28
29 extern int mu_url_create (mu_url_t *, const char *name); 29 extern int mu_url_create (mu_url_t *, const char *name);
30 extern int mu_url_dup (mu_url_t old_url, mu_url_t *new_url); 30 extern int mu_url_dup (mu_url_t old_url, mu_url_t *new_url);
31 extern int mu_url_uplevel (mu_url_t url, mu_url_t *upurl);
31 32
32 extern void mu_url_destroy (mu_url_t *); 33 extern void mu_url_destroy (mu_url_t *);
33 extern int mu_url_parse (mu_url_t); 34 extern int mu_url_parse (mu_url_t);
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2005, 2007 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2000, 2005, 2007, 2009 Free Software Foundation, Inc.
3 3
4 This library is free software; you can redistribute it and/or 4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public 5 modify it under the terms of the GNU Lesser General Public
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006,
3 2007 Free Software Foundation, Inc. 3 2007, 2008, 2009 Free Software Foundation, Inc.
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 6 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) ...@@ -60,7 +60,7 @@ mu_folder_match (const char *name, void *pattern, int flags)
60 there could be cases where you'll want a different folder for the same URL, 60 there could be cases where you'll want a different folder for the same URL,
61 there is not easy way to do this. */ 61 there is not easy way to do this. */
62 int 62 int
63 mu_folder_create_from_record (mu_folder_t *pfolder, const char *name, 63 mu_folder_create_from_record (mu_folder_t *pfolder, mu_url_t url,
64 mu_record_t record) 64 mu_record_t record)
65 { 65 {
66 if (!pfolder) 66 if (!pfolder)
...@@ -69,8 +69,8 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name, ...@@ -69,8 +69,8 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
69 if (record || 69 if (record ||
70 /* Look in the registrar list(iterator), for a possible concrete mailbox 70 /* Look in the registrar list(iterator), for a possible concrete mailbox
71 implementation that could match the URL. */ 71 implementation that could match the URL. */
72 mu_registrar_lookup (name, MU_FOLDER_ATTRIBUTE_DIRECTORY, &record, NULL) 72 mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_DIRECTORY, &record,
73 == 0) 73 NULL) == 0)
74 { 74 {
75 int (*f_init) (mu_folder_t) = NULL; 75 int (*f_init) (mu_folder_t) = NULL;
76 76
...@@ -78,30 +78,15 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name, ...@@ -78,30 +78,15 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
78 if (f_init) 78 if (f_init)
79 { 79 {
80 int status; 80 int status;
81 mu_url_t url;
82 mu_folder_t folder; 81 mu_folder_t folder;
83 int (*u_init) (mu_url_t) = NULL; 82 int (*u_init) (mu_url_t) = NULL;
84 83
85 /* Parse the url, it may be a bad one and we should bailout if this
86 failed. */
87 if ((status = mu_url_create (&url, name)) != 0)
88 return status;
89
90 status = mu_url_parse (url);
91 if (status)
92 {
93 mu_url_destroy (&url);
94 return status;
95 }
96 mu_record_get_url (record, &u_init); 84 mu_record_get_url (record, &u_init);
97 if (u_init) 85 if (u_init)
98 { 86 {
99 status = u_init (url); 87 status = u_init (url);
100 if (status) 88 if (status)
101 { 89 return status;
102 mu_url_destroy (&url);
103 return status;
104 }
105 } 90 }
106 91
107 mu_monitor_wrlock (&folder_lock); 92 mu_monitor_wrlock (&folder_lock);
...@@ -111,7 +96,7 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name, ...@@ -111,7 +96,7 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
111 { 96 {
112 folder->ref++; 97 folder->ref++;
113 *pfolder = folder; 98 *pfolder = folder;
114 mu_url_destroy (&url); 99 mu_url_destroy (&url); /* FIXME: Hmm */
115 mu_monitor_unlock (&folder_lock); 100 mu_monitor_unlock (&folder_lock);
116 return 0; 101 return 0;
117 } 102 }
...@@ -149,8 +134,6 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name, ...@@ -149,8 +134,6 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
149 { 134 {
150 if (folder->monitor) 135 if (folder->monitor)
151 mu_monitor_destroy (&folder->monitor, folder); 136 mu_monitor_destroy (&folder->monitor, folder);
152 if (folder->url)
153 mu_url_destroy (&folder->url);
154 free (folder); 137 free (folder);
155 } 138 }
156 } 139 }
...@@ -164,7 +147,18 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name, ...@@ -164,7 +147,18 @@ mu_folder_create_from_record (mu_folder_t *pfolder, const char *name,
164 int 147 int
165 mu_folder_create (mu_folder_t *pfolder, const char *name) 148 mu_folder_create (mu_folder_t *pfolder, const char *name)
166 { 149 {
167 return mu_folder_create_from_record (pfolder, name, NULL); 150 int rc;
151 mu_url_t url;
152
153 rc = mu_url_create (&url, name);
154 if (rc)
155 return rc;
156 rc = mu_url_parse (url);
157 if (rc == 0)
158 rc = mu_folder_create_from_record (pfolder, url, NULL);
159 if (rc)
160 mu_url_destroy (&url);
161 return 0;
168 } 162 }
169 163
170 /* The folder is destroy if it is the last reference. */ 164 /* The folder is destroy if it is the last reference. */
...@@ -213,7 +207,6 @@ mu_folder_destroy (mu_folder_t *pfolder) ...@@ -213,7 +207,6 @@ mu_folder_destroy (mu_folder_t *pfolder)
213 if (folder->stream) 207 if (folder->stream)
214 mu_stream_destroy (&(folder->stream), folder); 208 mu_stream_destroy (&(folder->stream), folder);
215 if (folder->url) 209 if (folder->url)
216
217 mu_url_destroy (&(folder->url)); 210 mu_url_destroy (&(folder->url));
218 free (folder); 211 free (folder);
219 } 212 }
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006, 2 Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006,
3 2007, 2008 Free Software Foundation, Inc. 3 2007, 2008, 2009 Free Software Foundation, Inc.
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 6 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, ...@@ -49,31 +49,23 @@ mailbox_folder_create (mu_mailbox_t mbox, const char *name,
49 mu_record_t record) 49 mu_record_t record)
50 { 50 {
51 int rc; 51 int rc;
52 char *fname; 52 mu_url_t url;
53 53
54 if ((rc = mu_url_aget_path (mbox->url, &fname))) 54 if ((rc = mu_url_uplevel (mbox->url, &url)))
55 { 55 {
56 if (rc == MU_ERR_NOENT) 56 if (rc == MU_ERR_NOENT)
57 { 57 {
58 fname = strdup (mu_url_to_string (mbox->url)); 58 rc = mu_url_dup (mbox->url, &url);
59 if (!fname) 59 if (rc)
60 return ENOMEM; 60 return rc;
61 } 61 }
62 else 62 else
63 return rc; 63 return rc;
64 } 64 }
65 65
66 if (mu_url_is_scheme (mbox->url, "file") 66 rc = mu_folder_create_from_record (&mbox->folder, url, record);
67 || mu_url_is_scheme (mbox->url, "mbox") 67 if (rc)
68 || mu_url_is_scheme (mbox->url, "mh") 68 mu_url_destroy (&url);
69 || mu_url_is_scheme (mbox->url, "maildir"))
70 {
71 char *p = strrchr (fname, '/'); /* FIXME: Is this always appropriate? */
72 if (p)
73 *p = 0;
74 }
75 rc = mu_folder_create_from_record (&mbox->folder, fname, record);
76 free (fname);
77 return rc; 69 return rc;
78 } 70 }
79 71
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2007, 2008 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2000, 2001, 2007, 2008,
3 2009 Free Software Foundation, Inc.
3 4
4 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public 6 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) ...@@ -181,6 +182,42 @@ mu_url_dup (mu_url_t old_url, mu_url_t *new_url)
181 return rc; 182 return rc;
182 } 183 }
183 184
185 int
186 mu_url_uplevel (mu_url_t url, mu_url_t *upurl)
187 {
188 int rc;
189 char *p;
190 mu_url_t new_url;
191
192 if (!url->path)
193 return MU_ERR_NOENT;
194 p = strrchr (url->path, '/');
195
196 rc = mu_url_dup (url, &new_url);
197 if (rc == 0)
198 {
199 if (!p || p == url->path)
200 {
201 free (new_url->path);
202 new_url->path = NULL;
203 }
204 else
205 {
206 size_t size = p - url->path;
207 new_url->path = realloc (new_url->path, size + 1);
208 if (!new_url->path)
209 {
210 mu_url_destroy (&new_url);
211 return ENOMEM;
212 }
213 memcpy (new_url->path, url->path, size);
214 new_url->path[size] = 0;
215 }
216 *upurl = new_url;
217 }
218 return rc;
219 }
220
184 void 221 void
185 mu_url_destroy (mu_url_t * purl) 222 mu_url_destroy (mu_url_t * purl)
186 { 223 {
......