Back to index

courier  0.68.2
maildirgetnew.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2000-2004 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 #if HAVE_DIRENT_H
00012 #include <dirent.h>
00013 #define NAMLEN(dirent) strlen((dirent)->d_name)
00014 #else
00015 #define dirent direct
00016 #define NAMLEN(dirent) (dirent)->d_namlen
00017 #if HAVE_SYS_NDIR_H
00018 #include <sys/ndir.h>
00019 #endif
00020 #if HAVE_SYS_DIR_H
00021 #include <sys/dir.h>
00022 #endif
00023 #if HAVE_NDIR_H
00024 #include <ndir.h>
00025 #endif
00026 #endif
00027 #include      <sys/types.h>
00028 #include      <sys/stat.h>
00029 #include      <stdio.h>
00030 #include      <string.h>
00031 #include      <stdlib.h>
00032 #include      <time.h>
00033 #include      <errno.h>
00034 #if    HAVE_UNISTD_H
00035 #include      <unistd.h>
00036 #endif
00037 
00038 #include      "maildirmisc.h"
00039 
00040 
00041 static void do_maildir_getnew(const char *, const char *,
00042                            void (*)(const char *, void *),
00043                            void *);
00044 
00045 void maildir_getnew(const char *maildir, const char *folder,
00046                   void (*callback_func)(const char *, void *),
00047                   void *callback_arg)
00048 {
00049 char   *dir=maildir_folderdir(maildir, folder);
00050 char   *newd, *curd;
00051 
00052        if (!dir)     return;
00053 
00054        newd=malloc(strlen(dir)+sizeof("/new"));
00055        curd=malloc(strlen(dir)+sizeof("/cur"));
00056 
00057        if (newd && curd)
00058        {
00059               strcat(strcpy(newd, dir), "/new");
00060               strcat(strcpy(curd, dir), "/cur");
00061               do_maildir_getnew(newd, curd, callback_func, callback_arg);
00062        }
00063 
00064        if (newd)     free(newd);
00065        if (curd)     free(curd);
00066        free(dir);
00067 }
00068 
00069 static void do_maildir_getnew(const char *newd, const char *curd,
00070                            void (*callback_func)(const char *, void *),
00071                            void *callback_arg)
00072 {
00073        DIR    *dirp;
00074        struct dirent *de;
00075        int keepgoing;
00076        int n;
00077        char *new_buf[20];
00078        char *cur_buf[20];
00079 
00080        do
00081        {
00082               keepgoing=0;
00083               n=0;
00084 
00085               dirp=opendir(newd);
00086               while (dirp && (de=readdir(dirp)) != 0)
00087               {
00088                      char   *np, *cp;
00089 
00090                      if (de->d_name[0] == '.')   continue;
00091 
00092                      if ((np=malloc(strlen(newd)+strlen(de->d_name)+2))
00093                          != 0)
00094                      {
00095                             if ((cp=malloc(strlen(curd)+strlen(de->d_name)
00096                                           + sizeof("/" MDIRSEP "2,")))
00097                                 != 0)
00098                             {
00099                                    char *a;
00100 
00101                                    strcat(strcat(strcpy(np, newd), "/"),
00102                                           de->d_name);
00103                                    strcat(strcat(strcpy(cp, curd), "/"),
00104                                           de->d_name);
00105                                    a=strchr(cp+strlen(curd), MDIRSEP[0]);
00106                                    if (a && strncmp(a, MDIRSEP "2,", 3))
00107                                    {
00108                                           *a=0;
00109                                           a=0;
00110                                    }
00111                                    if (!a)       strcat(cp, MDIRSEP "2,");
00112                                    new_buf[n]=np;
00113                                    cur_buf[n]=cp;
00114 
00115                                    if (++n >= sizeof(cur_buf)/
00116                                        sizeof(cur_buf[0]))
00117                                    {
00118                                           keepgoing=1;
00119                                           break;
00120                                    }
00121                             }
00122                             else
00123                                    free(np);
00124                      }
00125               }
00126               if (dirp)     closedir(dirp);
00127 
00128               while (n)
00129               {
00130                      char *np, *cp;
00131 
00132                      --n;
00133 
00134                      np=new_buf[n];
00135                      cp=cur_buf[n];
00136 
00137                      if (rename(np, cp))
00138                      {
00139                             fprintf(stderr,
00140                                    "ERR: rename(%s,%s) failed:"
00141                                    " %s\n", np, cp, strerror(errno));
00142                             keepgoing=0;
00143                             /* otherwise we could have infinite loop */
00144                      }
00145 
00146                      if (callback_func)
00147                             (*callback_func)(strrchr(cp, '/')+1,
00148                                            callback_arg);
00149                      free(np);
00150                      free(cp);
00151               }
00152        } while (keepgoing);
00153 }