]> rtime.felk.cvut.cz Git - notmuch.git/blob - contrib/notmuch-deliver/maildrop/maildir/maildirmkdir.c
28a3ac21dae326089c9f8c3acfc8a0bf2d57cfdb
[notmuch.git] / contrib / notmuch-deliver / maildrop / maildir / maildirmkdir.c
1 /*
2 ** Copyright 2000 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5
6 #if HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include        <sys/types.h>
11 #include        <sys/stat.h>
12 #include        <string.h>
13 #include        <stdlib.h>
14 #if     HAVE_UNISTD_H
15 #include        <unistd.h>
16 #endif
17 #include        <errno.h>
18
19 #include        "maildirmisc.h"
20
21
22 int maildir_mkdir(const char *dir)
23 {
24 char    *buf, *p;
25 size_t  l;
26
27         if (dir == 0 || dir[0] == 0)
28         {
29                 errno = EINVAL;
30                 return (-1);
31         }
32         l = strlen(dir);
33         if ((buf = malloc(l + sizeof("/tmp"))) == 0)
34         {
35                 errno = ENOMEM;
36                 return (-1);
37         }
38         strcpy(buf, dir);
39         strcpy(buf+l, "/cur");
40
41         /* We do mkdir -p here */
42
43         p = buf+1;
44         while ((p = strchr(p, '/')) != 0)
45         {
46                 *p = '\0';
47                 if (mkdir(buf, 0700) < 0 && errno != EEXIST)
48                 {
49                         free(buf);
50                         return (-1);
51                 }
52                 *p++ = '/';
53         }
54
55         if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
56                 free(buf);
57                 return (-1);
58         }
59         strcpy(buf+l, "/new");
60         if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
61                 free(buf);
62                 return (-1);
63         }
64         /*
65          *  make /tmp last because this is the one we open first -
66          *  the existence of this directory implies the whole
67          *  Maildir structure is complete
68          */
69         strcpy(buf+l, "/tmp");
70         if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
71                 free(buf);
72                 return (-1);
73         }
74         free(buf);
75         return (0);
76 }
77