Back to index

courier  0.68.2
maildirmake2.c
Go to the documentation of this file.
00001 #if    HAVE_CONFIG_H
00002 #include      "config.h"
00003 #endif
00004 #include      <stdio.h>
00005 #include      <string.h>
00006 #include      <stdlib.h>
00007 #if    HAVE_FCNTL_H
00008 #include      <fcntl.h>
00009 #endif
00010 #if    HAVE_UNISTD_H
00011 #include      <unistd.h>
00012 #endif
00013 #include      <sys/types.h>
00014 #if    HAVE_SYS_STAT_H
00015 #include      <sys/stat.h>
00016 #endif
00017 #include      <ctype.h>
00018 #include      <errno.h>
00019 
00020 #include      "maildirmisc.h"
00021 
00022 #if HAVE_DIRENT_H
00023 #include <dirent.h>
00024 #define NAMLEN(dirent) strlen((dirent)->d_name)
00025 #else
00026 #define dirent direct
00027 #define NAMLEN(dirent) (dirent)->d_namlen
00028 #if HAVE_SYS_NDIR_H
00029 #include <sys/ndir.h>
00030 #endif
00031 #if HAVE_SYS_DIR_H
00032 #include <sys/dir.h>
00033 #endif
00034 #if HAVE_NDIR_H
00035 #include <ndir.h>
00036 #endif
00037 #endif
00038 
00039 int maildir_make(const char *maildir, int perm, int subdirperm, int folder)
00040 {
00041        char *q=malloc(strlen(maildir)+sizeof("/maildirfolder"));
00042        int fd= -1;
00043 
00044        if (!q)
00045               return -1;
00046 
00047        if (mkdir(maildir, perm) < 0 ||
00048            chmod(maildir, perm) < 0 ||
00049            mkdir(strcat(strcpy(q, maildir), "/tmp"), subdirperm) < 0 ||
00050            chmod(q, subdirperm) < 0 ||
00051            mkdir(strcat(strcpy(q, maildir), "/new"), subdirperm) < 0 ||
00052            chmod(q, subdirperm) < 0 ||
00053            mkdir(strcat(strcpy(q, maildir), "/cur"), subdirperm) < 0 ||
00054            chmod(q, subdirperm) < 0 ||
00055            (folder && (fd=open(strcat(strcpy(q, maildir), "/maildirfolder"),
00056                             O_CREAT|O_WRONLY, 0600)) < 0))
00057        {
00058               free(q);
00059               return -1;
00060        }
00061        if (fd >= 0)
00062               close(fd);
00063        free(q);
00064        return 0;
00065 }
00066 
00067 int maildir_del_content(const char *maildir)
00068 {
00069        char *filenamebuf[100];
00070        int n, i;
00071        DIR *dirp;
00072        struct dirent *de;
00073 
00074        do
00075        {
00076               dirp=opendir(maildir);
00077               n=0;
00078               while (dirp && (de=readdir(dirp)) != 0)
00079               {
00080                      if (strcmp(de->d_name, ".") == 0 ||
00081                          strcmp(de->d_name, "..") == 0)
00082                             continue;
00083 
00084                      if ((filenamebuf[n]=malloc(strlen(maildir)+
00085                                              strlen(de->d_name)+2))
00086                          == NULL)
00087                      {
00088                             closedir(dirp);
00089                             while (n)
00090                                    free(filenamebuf[--n]);
00091                             return -1;
00092                      }
00093                      strcat(strcat(strcpy(filenamebuf[n], maildir),
00094                                   "/"), de->d_name);
00095                      if (++n >= sizeof(filenamebuf)/sizeof(filenamebuf[0]))
00096                             break;
00097               }
00098               if (dirp)
00099                      closedir(dirp);
00100 
00101               for (i=0; i<n; i++)
00102               {
00103                      struct stat s_buf;
00104 
00105                      if (lstat(filenamebuf[i], &s_buf) < 0)
00106                             continue;
00107 
00108                      if (S_ISDIR(s_buf.st_mode))
00109                      {
00110                             if (maildir_del(filenamebuf[i]) < 0)
00111                             {
00112                                    while (n)
00113                                           free(filenamebuf[--n]);
00114                                    return -1;
00115                             }
00116                      }
00117                      else if (unlink(filenamebuf[i]) < 0)
00118                      {
00119                             if (errno != ENOENT)
00120                             {
00121                                    while (n)
00122                                           free(filenamebuf[--n]);
00123                                    return -1;
00124 
00125                             }
00126                      }
00127               }
00128 
00129               for (i=0; i<n; i++)
00130                      free(filenamebuf[i]);
00131        } while (n);
00132        return 0; 
00133 }
00134 
00135 int maildir_del(const char *maildir)
00136 {
00137        int rc;
00138        if ((rc=maildir_del_content(maildir)) == -1)
00139            return rc;
00140        return rmdir(maildir) < 0 && errno != ENOENT ? -1:0;
00141 }