Plug more leaks (in the test, not the core)
[gnash.git] / libbase / GnashFileUtilities.cpp
blob65ed8759e95115476716abeb36777cfe3e0c5a6f
1 // GnashFileUtilities.cpp File handling for Gnash
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // 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
20 #ifndef GNASH_FILE_HEADERS_H
21 #define GNASH_FILE_HEADERS_H
23 #include "GnashFileUtilities.h"
25 #include <string>
26 #include <boost/tokenizer.hpp>
27 #include <cerrno>
29 namespace gnash {
31 /// Create a directory for a given filename.
33 /// Everything after the last '/' is assumed to be the filename.
34 bool
35 mkdirRecursive(const std::string& filename)
37 // Not a directory, nothing to do.
38 std::string::size_type pos = filename.rfind("/");
39 if (pos == std::string::npos) {
40 return true;
42 const std::string& target = filename.substr(0, pos);
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 += "/";
52 for (Tok::iterator tit = t.begin(); tit != t.end(); ++tit) {
54 newdir += *tit;
56 if (newdir.find("..") != std::string::npos) {
57 return false;
60 int ret = mkdirUserPermissions(newdir);
62 if ((errno != EEXIST) && (ret != 0)) {
63 return false;
65 newdir.push_back('/');
67 return true;
70 } // namespace gnash
72 #endif