Back to index

courier  0.68.2
comqueuename2.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #if    HAVE_CONFIG_H
00007 #include      "courier_lib_config.h"
00008 #endif
00009 #include      "courier.h"
00010 #include      "numlib/numlib.h"
00011 #include      "comqueuename.h"
00012 #include      <stdlib.h>
00013 #include      <string.h>
00014 #if    HAVE_SYS_STAT_H
00015 #include      <sys/stat.h>
00016 #endif
00017 #if    HAVE_UNISTD_H
00018 #include      <unistd.h>
00019 #endif
00020 #include      <time.h>
00021 
00022 static int bigbrother_create_rename(const char *, char *);
00023 
00024 void qmsgunlink(ino_t n, time_t rightnow)
00025 {
00026        const char *bigbrother=getenv("ARCHIVEDIR");
00027 
00028        if (bigbrother && *bigbrother)
00029        {
00030               struct tm *tmptr=localtime(&rightnow);
00031 
00032               /* Archive this message */
00033 
00034               if (tmptr)
00035               {
00036                      const char *dname=qmsgsdatname(n);
00037                      const char *tname=strrchr(dname, '/');
00038 
00039                      char *p=courier_malloc(strlen(bigbrother)+
00040                                           strlen(tname)+40);
00041 
00042                      sprintf(p, "%s/%04d.%02d.%02d%s",
00043                             bigbrother, tmptr->tm_year + 1900,
00044                             tmptr->tm_mon+1,
00045                             tmptr->tm_mday, tname);
00046 
00047                      if (bigbrother_create_rename(dname, p) == 0)
00048                      {
00049                             dname=qmsgsctlname(n);
00050 
00051                             strcpy(strrchr(p, '/'), strrchr(dname, '/'));
00052 
00053                             if (rename(dname, p) == 0)
00054                             {
00055                                    free(p);
00056                                    return;
00057                             }
00058                      }
00059 
00060                      clog_msg_start_err();
00061                      clog_msg_str("Unable to move ");
00062                      clog_msg_str(dname);
00063                      clog_msg_str(" to ");
00064                      clog_msg_str(p);
00065                      clog_msg_send();
00066                      clog_msg_prerrno();
00067 
00068                      free(p);
00069               }
00070        }
00071 
00072        unlink(qmsgsdatname(n));
00073        unlink(qmsgsctlname(n));
00074 }
00075 
00076 static int bigbrother_create_rename(const char *from, char *to)
00077 {
00078        char *p;
00079 
00080        if (rename(from, to) == 0)
00081               return (0);
00082 
00083        /* Perhaps the directory has to be created? */
00084 
00085        p=strrchr(to, '/');
00086 
00087        *p=0;
00088        mkdir(to, 0700);
00089        *p='/';
00090        return (rename(from, to));
00091 }