1 // GnashFileUtilities.cpp     File handling for Gnash
2 //
3 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 //   Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 
20 #ifndef GNASH_FILE_HEADERS_H
21 #define GNASH_FILE_HEADERS_H
22 
23 #include "GnashFileUtilities.h"
24 
25 #include <string>
26 #include <boost/tokenizer.hpp>
27 #include <cerrno>
28 
29 namespace gnash {
30 
31 /// Create a directory for a given filename.
32 //
33 /// Everything after the last '/' is assumed to be the filename.
34 bool
mkdirRecursive(const std::string & filename)35 mkdirRecursive(const std::string& filename)
36 {
37     // Not a directory, nothing to do.
38     std::string::size_type pos = filename.rfind("/");
39     if (pos == std::string::npos) {
40         return true;
41     }
42     const std::string& target = filename.substr(0, pos);
43 
44     typedef boost::tokenizer<boost::char_separator<char> > Tok;
45     boost::char_separator<char> sep("/");
46     Tok t(target, sep);
47     std::string newdir;
48     // Start from the root if the given filename was given
49     // with an absolute path
50     if ( filename[0] == '/' ) newdir += "/";
51 
52     for (Tok::iterator tit = t.begin(); tit != t.end(); ++tit) {
53 
54         newdir += *tit;
55 
56         if (newdir.find("..") != std::string::npos) {
57             return false;
58         }
59 
60         int ret = mkdirUserPermissions(newdir);
61 
62         if ((errno != EEXIST) && (ret != 0)) {
63             return false;
64         }
65         newdir.push_back('/');
66     }
67     return true;
68 }
69 
70 } // namespace gnash
71 
72 #endif
73