summaryrefslogtreecommitdiff
path: root/libmailutils
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2021-05-22 18:27:56 +0300
committerSergey Poznyakoff <gray@gnu.org>2021-05-22 18:49:49 +0300
commit3f74f2ada1d077751b3b73bbc828c1918dde9ebf (patch)
treea37f2d21de2bb58c61f3e59e6d790bdc9114af6e /libmailutils
parent3eb73b1f9fe2ec48f05cc6d38202886bf6fd4072 (diff)
downloadmailutils-3f74f2ada1d077751b3b73bbc828c1918dde9ebf.tar.gz
mailutils-3f74f2ada1d077751b3b73bbc828c1918dde9ebf.tar.bz2
folder scan returns mailbox names without the leading directory prefix.
For example, suppose folder /foo/bar contains mailboxes a, b, and c. Then scanning the mu_folder_t object for '/foo' will return 'bar/a', 'bar/b', and 'bar/c'. Scanning the mu_folder_t for '/foo/bar' will return 'a', 'b', and 'c', and so on. * libmailutils/string/mkfilename.c (mu_make_file_name_suf): Reasonable handling of corner cases. Document its behavior. * libmailutils/mailbox/fsfolder.c (struct folder_scan_data): New member: prefix_len (list_helper): Return the portion of the file name after the base directory prefix. (_fsfolder_list): Initialize sdata.prefix_len. * libmailutils/tests/fsfolder.c: Remove prefix handling code. * libmailutils/tests/fsfolder00.at: Change expectations. * libmailutils/tests/fsfolder02.at: Likewise. * libmailutils/tests/fsfolder03.at: Likewise. * libmailutils/tests/fsfolder04.at: Likewise. * include/mailutils/sys/imap.h (struct _mu_imap): New members: separator and prefix_len. * libproto/imap/folder.c (_mu_imap_folder_separator): New function. (_mu_imap_folder_pathname): New function. (_mu_imap_folder_list): Prefix the original refname with the base directory of the folder. (_mu_imap_folder_lsub,_mu_imap_folder_subscribe) (_mu_imap_folder_unsubscribe,_mu_imap_folder_delete) (_mu_imap_folder_rename): Adjust input mailbox names. * libproto/imap/genlist.c (list_untagged_handler): Return the portion of the file name after the base directory prefix. * libproto/imap/tests/imapfolder.c: Minor change. Update for changed folder behavior * imap4d/list.c (struct refinfo): Replace dirlen with delim - a character to be inserted between the original reference and mailbox name. (list_fun): Insert delimiter, if required. (list_ref): Set refinfo.delim, unless the reference prefix already ends with a delimiter or is the same as the namespace prefix. Mail 'folders' command: add support for remote folders. * mail/folders.c: Handle remote folders correctly. * mail/mail.h (util_get_folder): New proto. * mail/mailline.c (struct filegen): Remove path_len. (new_folder): Rename to util_get_folder and move to util.c (filegen_init): Initialize prefix_len from the argument. * mail/util.c (util_get_folder): New function.
Diffstat (limited to 'libmailutils')
-rw-r--r--libmailutils/cli/stdcapa.c2
-rw-r--r--libmailutils/mailbox/fsfolder.c8
-rw-r--r--libmailutils/mailbox/mbx_default.c2
-rw-r--r--libmailutils/string/mkfilename.c63
-rw-r--r--libmailutils/tests/fsfolder.c8
-rw-r--r--libmailutils/tests/fsfolder00.at22
-rw-r--r--libmailutils/tests/fsfolder02.at2
-rw-r--r--libmailutils/tests/fsfolder03.at14
-rw-r--r--libmailutils/tests/fsfolder04.at12
9 files changed, 84 insertions, 49 deletions
diff --git a/libmailutils/cli/stdcapa.c b/libmailutils/cli/stdcapa.c
index 1d496161c..12a088546 100644
--- a/libmailutils/cli/stdcapa.c
+++ b/libmailutils/cli/stdcapa.c
@@ -466,8 +466,6 @@ cb_locker_type (void *data, mu_config_value_t *val)
static int
cb_locker_external (void *data, mu_config_value_t *val)
{
- int t;
-
if (mu_cfg_assert_value_type (val, MU_CFG_STRING))
return 1;
diff --git a/libmailutils/mailbox/fsfolder.c b/libmailutils/mailbox/fsfolder.c
index 805aea626..0b3e59a4a 100644
--- a/libmailutils/mailbox/fsfolder.c
+++ b/libmailutils/mailbox/fsfolder.c
@@ -146,7 +146,7 @@ _fsfolder_close (mu_folder_t folder MU_ARG_UNUSED)
static int
_fsfolder_rename (mu_folder_t folder, const char *oldpath,
- const char *newpath)
+ const char *newpath)
{
struct _mu_fsfolder *fsfolder = folder->data;
if (oldpath && newpath)
@@ -188,6 +188,7 @@ struct folder_scan_data
mu_folder_t folder;
char *dirname;
size_t dirlen;
+ size_t prefix_len;
size_t errcnt;
};
@@ -376,7 +377,7 @@ list_helper (struct mu_folder_scanner *scn,
}
else
{
- resp->name = fname;
+ resp->name = strdup (fname + data->prefix_len + 1);
resp->depth = depth;
resp->separator = '/';
resp->type = type;
@@ -458,6 +459,9 @@ _fsfolder_list (mu_folder_t folder, struct mu_folder_scanner *scn)
sdata.folder = folder;
sdata.dirname = get_pathname (fsfolder->dirname, scn->refname);
sdata.dirlen = strlen (sdata.dirname);
+ sdata.prefix_len = strlen (fsfolder->dirname);
+ if (sdata.prefix_len > 0 && fsfolder->dirname[sdata.prefix_len-1] == '/')
+ sdata.prefix_len--;
sdata.errcnt = 0;
list_helper (scn, &sdata, &iroot, sdata.dirname, 0);
free (sdata.dirname);
diff --git a/libmailutils/mailbox/mbx_default.c b/libmailutils/mailbox/mbx_default.c
index 389eb6962..2571d21b7 100644
--- a/libmailutils/mailbox/mbx_default.c
+++ b/libmailutils/mailbox/mbx_default.c
@@ -299,7 +299,7 @@ plus_expand (const char *file, char **buf)
else
{
file++;
-
+
if (folder_dir[0] == '/' || mu_is_proto (folder_dir))
{
char *p = mu_make_file_name (folder_dir, file);
diff --git a/libmailutils/string/mkfilename.c b/libmailutils/string/mkfilename.c
index 653ac7a2a..d24d2f48a 100644
--- a/libmailutils/string/mkfilename.c
+++ b/libmailutils/string/mkfilename.c
@@ -21,30 +21,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
#include <mailutils/alloc.h>
#include <mailutils/util.h>
+/*
+ * Given directory name DIR, file name FILE and optional suffix SUF,
+ * return full pathname composed from these three. In the resulting
+ * string, DIR and FILE are separated by '/'. If SUF is supplied, it
+ * is concatenated to the resulting string without additional
+ * separators.
+ *
+ * Corner cases:
+ * all three arguments are NULL or empty
+ * Return NULL and set errno to EINVAL.
+ * dir is NULL
+ * Return FILE and SUF concatenated.
+ * file is NULL, suf is not NULL
+ * Same as mu_make_file_name_suf(dir, suf, NULL);
+ * file is NULL, suf is NULL
+ * Return allocated copy of DIR.
+ */
char *
mu_make_file_name_suf (const char *dir, const char *file, const char *suf)
{
char *tmp;
- size_t dirlen = strlen (dir);
- size_t suflen = suf ? strlen (suf) : 0;
- size_t fillen = strlen (file);
+ size_t dirlen, suflen, fillen;
size_t len;
- while (dirlen > 0 && dir[dirlen-1] == '/')
- dirlen--;
-
- len = dirlen + (dir[0] ? 1 : 0) + fillen + suflen;
- tmp = mu_alloc (len + 1);
+ dirlen = dir ? strlen (dir) : 0;
+ fillen = file ? strlen (file) : 0;
+ suflen = suf ? strlen (suf) : 0;
+
+ len = suflen + fillen;
+ if (dirlen == 0)
+ {
+ if (len == 0)
+ {
+ errno = EINVAL;
+ return NULL;
+ }
+ }
+ else
+ {
+ if (len) len++; // account for the '/' separator
+ while (dirlen > 0 && dir[dirlen-1] == '/')
+ dirlen--;
+ }
+ len += dirlen;
+
+ tmp = malloc (len + 1);
if (tmp)
{
- memcpy (tmp, dir, dirlen);
- if (dir[0])
- tmp[dirlen++] = '/';
- memcpy (tmp + dirlen, file, fillen);
- if (suf)
+ if (dirlen)
+ {
+ memcpy (tmp, dir, dirlen);
+ if (fillen || suflen)
+ tmp[dirlen++] = '/';
+ }
+ if (fillen)
+ memcpy (tmp + dirlen, file, fillen);
+ if (suflen)
memcpy (tmp + dirlen + fillen, suf, suflen);
tmp[len] = 0;
}
diff --git a/libmailutils/tests/fsfolder.c b/libmailutils/tests/fsfolder.c
index 6a4a8ec39..3f8333406 100644
--- a/libmailutils/tests/fsfolder.c
+++ b/libmailutils/tests/fsfolder.c
@@ -37,7 +37,6 @@
#include "tesh.h"
int sort_option;
-int prefix_len;
static int
compare_response (void const *a, void const *b)
@@ -81,7 +80,7 @@ com_list (int argc, char **argv, mu_assoc_t options, void *env)
{
if (sort_option)
mu_list_sort (list, compare_response);
- mu_list_foreach (list, _print_list_entry, &prefix_len);
+ mu_list_foreach (list, _print_list_entry, NULL);
mu_list_destroy (&list);
}
return 0;
@@ -211,7 +210,7 @@ com_scan (int argc, char **argv, mu_assoc_t options, void *env)
{
if (sort_option)
mu_list_sort (scn.result, compare_response);
- mu_list_foreach (scn.result, _print_list_entry, &prefix_len);
+ mu_list_foreach (scn.result, _print_list_entry, NULL);
mu_list_destroy (&scn.result);
}
mu_list_destroy (&scn.records);
@@ -347,9 +346,6 @@ main (int argc, char **argv)
if (fname[0] != '/')
{
char *cwd = mu_getcwd ();
- prefix_len = strlen (cwd);
- if (cwd[prefix_len-1] != '/')
- prefix_len++;
fname = mu_make_file_name (cwd, fname);
free (cwd);
}
diff --git a/libmailutils/tests/fsfolder00.at b/libmailutils/tests/fsfolder00.at
index e82ef28b2..574e74068 100644
--- a/libmailutils/tests/fsfolder00.at
+++ b/libmailutils/tests/fsfolder00.at
@@ -34,20 +34,20 @@ fsfolder -name=dir -sort dnl
],
[0],
[listing '' '*'
--f / 0 dir/bar
--f / 0 dir/foo
--f / 0 dir/foo.mbox
-d- / 0 dir/subdir
--f / 1 dir/subdir/baz.mbox
--f / 1 dir/subdir/file
+-f / 0 bar
+-f / 0 foo
+-f / 0 foo.mbox
+d- / 0 subdir
+-f / 1 subdir/baz.mbox
+-f / 1 subdir/file
listing 'subdir' '*'
--f / 0 dir/subdir/baz.mbox
--f / 0 dir/subdir/file
+-f / 0 subdir/baz.mbox
+-f / 0 subdir/file
listing '' '*.mbox'
--f / 0 dir/foo.mbox
--f / 1 dir/subdir/baz.mbox
+-f / 0 foo.mbox
+-f / 1 subdir/baz.mbox
listing 'subdir' '*.mbox'
--f / 0 dir/subdir/baz.mbox
+-f / 0 subdir/baz.mbox
])
AT_CLEANUP
diff --git a/libmailutils/tests/fsfolder02.at b/libmailutils/tests/fsfolder02.at
index f8d3fdfb9..562e7e3e2 100644
--- a/libmailutils/tests/fsfolder02.at
+++ b/libmailutils/tests/fsfolder02.at
@@ -29,7 +29,7 @@ fsfolder -name=dir rename bar baz
[renaming foo to bar
rename successful
listing '' '*'
--f / 0 dir/bar
+-f / 0 bar
renaming bar to baz
],
[fsfolder: mu_folder_rename(bar) failed: File exists
diff --git a/libmailutils/tests/fsfolder03.at b/libmailutils/tests/fsfolder03.at
index 0bee92adb..0b9fd9578 100644
--- a/libmailutils/tests/fsfolder03.at
+++ b/libmailutils/tests/fsfolder03.at
@@ -32,13 +32,13 @@ fsfolder -name=dir -sort scan -maxdepth=2
],
[0],
[maxdepth=1
-d- / 0 dir/a
-d- / 0 dir/b
+d- / 0 a
+d- / 0 b
maxdepth=2
-d- / 0 dir/a
-d- / 0 dir/b
-d- / 1 dir/a/b
-d- / 1 dir/b/b
-d- / 1 dir/b/c
+d- / 0 a
+d- / 0 b
+d- / 1 a/b
+d- / 1 b/b
+d- / 1 b/c
])
AT_CLEANUP
diff --git a/libmailutils/tests/fsfolder04.at b/libmailutils/tests/fsfolder04.at
index 5a6929958..4e08e7db9 100644
--- a/libmailutils/tests/fsfolder04.at
+++ b/libmailutils/tests/fsfolder04.at
@@ -29,13 +29,13 @@ fsfolder -name=dir -sort scan -type=reg
],
[0],
[type=any
--f / 0 dir/1
-d- / 0 dir/a
-d- / 0 dir/b
--f / 1 dir/a/2
+-f / 0 1
+d- / 0 a
+d- / 0 b
+-f / 1 a/2
type=reg
--f / 0 dir/1
--f / 1 dir/a/2
+-f / 0 1
+-f / 1 a/2
])
AT_CLEANUP

Return to:

Send suggestions and report system problems to the System administrator.