]> git.notmuchmail.org Git - notmuch/blob - contrib/notmuch-deliver/maildrop/maildir/maildirmkdir.c
Move files copied from maildrop to a separate hierarchy.
[notmuch] / 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 static const char rcsid[]="$Id: maildirmkdir.c,v 1.2 2002/03/15 03:09:21 mrsam Exp $";
22
23 int maildir_mkdir(const char *dir)
24 {
25 char    *buf, *p;
26 size_t  l;
27
28         if (dir == 0 || dir[0] == 0)
29         {
30                 errno = EINVAL;
31                 return (-1);
32         }
33         l = strlen(dir);
34         if ((buf = malloc(l + sizeof("/tmp"))) == 0)
35         {
36                 errno = ENOMEM;
37                 return (-1);
38         }
39         strcpy(buf, dir);
40         strcpy(buf+l, "/cur");
41
42         /* We do mkdir -p here */
43
44         p = buf+1;
45         while ((p = strchr(p, '/')) != 0)
46         {
47                 *p = '\0';
48                 if (mkdir(buf, 0700) < 0 && errno != EEXIST)
49                 {
50                         free(buf);
51                         return (-1);
52                 }
53                 *p++ = '/';
54         }
55
56         if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
57                 free(buf);
58                 return (-1);
59         }
60         strcpy(buf+l, "/new");
61         if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
62                 free(buf);
63                 return (-1);
64         }
65         /*
66          *  make /tmp last because this is the one we open first -
67          *  the existence of this directory implies the whole
68          *  Maildir structure is complete
69          */
70         strcpy(buf+l, "/tmp");
71         if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
72                 free(buf);
73                 return (-1);
74         }
75         free(buf);
76         return (0);
77 }
78