Back to index

courier  0.68.2
maildirmkdir.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2000 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #if HAVE_CONFIG_H
00007 #include "config.h"
00008 #endif
00009 
00010 #include      <sys/types.h>
00011 #include      <sys/stat.h>
00012 #include      <string.h>
00013 #include      <stdlib.h>
00014 #if    HAVE_UNISTD_H
00015 #include      <unistd.h>
00016 #endif
00017 #include      <errno.h>
00018 
00019 #include      "maildirmisc.h"
00020 
00021 
00022 int maildir_mkdir(const char *dir)
00023 {
00024 char   *buf, *p;
00025 size_t l;
00026 
00027        if (dir == 0 || dir[0] == 0)
00028        {
00029               errno = EINVAL;
00030               return (-1);
00031        }
00032        l = strlen(dir);
00033        if ((buf = malloc(l + sizeof("/tmp"))) == 0)
00034        {
00035               errno = ENOMEM;
00036               return (-1);
00037        }
00038        strcpy(buf, dir);
00039        strcpy(buf+l, "/cur");
00040 
00041        /* We do mkdir -p here */
00042 
00043        p = buf+1;
00044        while ((p = strchr(p, '/')) != 0)
00045        {
00046               *p = '\0';
00047               if (mkdir(buf, 0700) < 0 && errno != EEXIST)
00048               {
00049                      free(buf);
00050                      return (-1);
00051               }
00052               *p++ = '/';
00053        }
00054 
00055        if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
00056               free(buf);
00057               return (-1);
00058        }
00059        strcpy(buf+l, "/new");
00060        if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
00061               free(buf);
00062               return (-1);
00063        }
00064        /*
00065         *  make /tmp last because this is the one we open first -
00066         *  the existence of this directory implies the whole
00067         *  Maildir structure is complete
00068         */
00069        strcpy(buf+l, "/tmp");
00070        if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
00071               free(buf);
00072               return (-1);
00073        }
00074        free(buf);
00075        return (0);
00076 }
00077