NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
path.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <dirent.h>
31#include <limits.h>
32#include <stdbool.h>
33#include <stdio.h>
34#include <sys/stat.h>
35#include "mutt/lib.h"
36#include "path.h"
37#include "globals.h"
38
39// Mailbox API -----------------------------------------------------------------
40
44int maildir_path_canon(struct Buffer *path)
45{
46 mutt_path_canon(path, HomeDir, true);
47 return 0;
48}
49
58{
59 DIR *dir = NULL;
60 struct dirent *de = NULL;
61 int rc = 1; /* assume empty until we find a message */
62 char realpath[PATH_MAX] = { 0 };
63 int iter = 0;
64
65 /* Strategy here is to look for any file not beginning with a period */
66
67 do
68 {
69 /* we do "cur" on the first iteration since it's more likely that we'll
70 * find old messages without having to scan both subdirs */
71 snprintf(realpath, sizeof(realpath), "%s/%s", buf_string(path),
72 (iter == 0) ? "cur" : "new");
74 if (!dir)
75 return -1;
76 while ((de = readdir(dir)))
77 {
78 if (*de->d_name != '.')
79 {
80 rc = 0;
81 break;
82 }
83 }
84 closedir(dir);
85 iter++;
86 } while (rc && iter < 2);
87
88 return rc;
89}
90
94enum MailboxType maildir_path_probe(const char *path, const struct stat *st)
95{
96 if (!st || !S_ISDIR(st->st_mode))
97 return MUTT_UNKNOWN;
98
99 char sub[PATH_MAX] = { 0 };
100 struct stat stsub = { 0 };
101 char *subs[] = { "cur", "new" };
102 for (size_t i = 0; i < mutt_array_size(subs); ++i)
103 {
104 snprintf(sub, sizeof(sub), "%s/%s", path, subs[i]);
105 if ((stat(sub, &stsub) == 0) && S_ISDIR(stsub.st_mode))
106 return MUTT_MAILDIR;
107 }
108
109 return MUTT_UNKNOWN;
110}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
char * HomeDir
User's home directory.
Definition: globals.c:37
MailboxType
Supported mailbox formats.
Definition: mailbox.h:41
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition: mailbox.h:44
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition: mailbox.h:48
DIR * mutt_file_opendir(const char *path, enum MuttOpenDirMode mode)
Open a directory.
Definition: file.c:642
@ MUTT_OPENDIR_CREATE
Create the directory if it doesn't exist.
Definition: file.h:64
int maildir_path_canon(struct Buffer *path)
Canonicalise a Mailbox path - Implements MxOps::path_canon() -.
Definition: path.c:44
enum MailboxType maildir_path_probe(const char *path, const struct stat *st)
Is this a Maildir Mailbox? - Implements MxOps::path_probe() -.
Definition: path.c:94
int maildir_path_is_empty(struct Buffer *path)
Is the mailbox empty.
Definition: path.c:57
#define mutt_array_size(x)
Definition: memory.h:38
Convenience wrapper for the library headers.
bool mutt_path_canon(struct Buffer *path, const char *homedir, bool is_dir)
Create the canonical version of a path.
Definition: path.c:248
Path manipulation functions.
#define PATH_MAX
Definition: mutt.h:42
String manipulation buffer.
Definition: buffer.h:36