bincimap

Log | Files | Refs | LICENSE

maildir-create.cc (3084B)


      1 /* -*- Mode: c++; -*- */
      2 /*  --------------------------------------------------------------------
      3  *  Filename:
      4  *    maildir-create.cc
      5  *  
      6  *  Description:
      7  *    Implementation of the Maildir class.
      8  *
      9  *  Authors:
     10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
     11  *
     12  *  Bugs:
     13  *
     14  *  ChangeLog:
     15  *
     16  *  --------------------------------------------------------------------
     17  *  Copyright 2002-2005 Andreas Aardal Hanssen
     18  *
     19  *  This program is free software; you can redistribute it and/or modify
     20  *  it under the terms of the GNU General Public License as published by
     21  *  the Free Software Foundation; either version 2 of the License, or
     22  *  (at your option) any later version.
     23  *
     24  *  This program is distributed in the hope that it will be useful,
     25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     27  *  GNU General Public License for more details.
     28  *
     29  *  You should have received a copy of the GNU General Public License
     30  *  along with this program; if not, write to the Free Software
     31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
     32  *  --------------------------------------------------------------------
     33  */
     34 #ifdef HAVE_CONFIG_H
     35 #include <config.h>
     36 #endif
     37 
     38 #include "maildir.h"
     39 
     40 #include <fcntl.h>
     41 #include <unistd.h>
     42 
     43 using namespace ::std;
     44 using namespace Binc;
     45 
     46 //------------------------------------------------------------------------
     47 bool Binc::Maildir::createMailbox(const string &s_in, mode_t mode,
     48 				  uid_t owner, gid_t group, bool root)
     49 {
     50   if (s_in != "." && mkdir(s_in.c_str(), mode) == -1) {
     51     setLastError("unable to create " + s_in + ": " 
     52 		 + string(strerror(errno)));
     53     return false;
     54   }
     55 
     56   // Allow uidvalidity, which is generated from time(0), to
     57   // increase with one second to avoid race conditions.
     58   sleep(1);
     59 
     60   if (mkdir((s_in + "/cur").c_str(), mode) == -1) {
     61     setLastError("unable to create " + s_in + "/cur: " 
     62 		 + string(strerror(errno)));
     63     return false;
     64   }
     65 
     66   if (mkdir((s_in + "/new").c_str(), mode) == -1) {
     67     setLastError("unable to create " + s_in + "/new: " 
     68 		 + string(strerror(errno)));
     69     return false;
     70   }
     71 
     72   if (mkdir((s_in + "/tmp").c_str(), mode) == -1) {
     73     setLastError("unable to create " + s_in + "/tmp: " 
     74 		 + string(strerror(errno)));
     75     return false;
     76   }
     77 
     78   if (owner == 0 && group == 0)
     79     return true;
     80 
     81   if (chown(s_in.c_str(), owner, group) == -1) {
     82     setLastError("unable to chown " + s_in + ": " 
     83 		 + string(strerror(errno)));
     84     return false;
     85   }
     86 
     87   if (chown((s_in + "/cur").c_str(), owner, group) == -1) {
     88     setLastError("unable to chown " + s_in + "/cur: "
     89 		 + string(strerror(errno)));
     90     return false;
     91   }
     92 
     93   if (chown((s_in + "/new").c_str(), owner, group) == -1) {
     94     setLastError("unable to chown " + s_in + "/new: "
     95 		 + string(strerror(errno)));
     96     return false;
     97   }
     98 
     99   if (chown((s_in + "/tmp").c_str(), owner, group) == -1) {
    100     setLastError("unable to chown " + s_in + "/tmp: "
    101 		 + string(strerror(errno)));
    102     return false;
    103   }
    104 
    105   return true;
    106 }