1 // src/create_dir.hh
2 // This file is part of libpbe; see http://svn.chezphil.org/libpbe
3 // (C) 2009 Philip Endecott
4 
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 
20 // Create directories.
21 
22 
23 #include "create_dir.hh"
24 
25 #include "FileType.hh"
26 
27 #include <algorithm>
28 
29 using namespace std;
30 
31 namespace pbe {
32 
33 // Check for and create necessary parent directories.  Succeeds if the
34 // directory already exists; fails if it is a file.
35 
create_dir_with_parents(const string path,mode_t mode)36 void create_dir_with_parents(const string path, mode_t mode)
37 {
38   string::const_iterator i = path.begin();
39   while (1) {
40     string::const_iterator j = find(i,path.end(),'/');
41     string elem(i,j);
42     string to_here(path.begin(),j);
43     if (elem=="" || elem==".") {
44     } else {
45       switch (get_link_filetype(to_here,true)) {
46         case does_not_exist:
47           create_dir(to_here,mode);
48           break;
49         case directory:
50           break;
51         default:
52           throw_ErrnoException("create_dir_with_parents()",EEXIST);
53           break;
54       }
55     }
56 
57     if (j==path.end()) {
58       break;
59     }
60 
61     i = j;
62     ++i;
63   }
64 }
65 
66 
67 };
68 
69