diff --git a/dtool/src/dtoolutil/filename.cxx b/dtool/src/dtoolutil/filename.cxx index 3ce530e1ed..0dc5a8f1ad 100644 --- a/dtool/src/dtoolutil/filename.cxx +++ b/dtool/src/dtoolutil/filename.cxx @@ -810,6 +810,31 @@ rename_to(const Filename &other) const { other.to_os_specific().c_str()) == 0); } +//////////////////////////////////////////////////////////////////// +// Function: Filename::mkdir +// Access: Public +// Description: Creates all the directories in the path to the file +// specified in the filename (useful for writing). +//////////////////////////////////////////////////////////////////// +bool Filename:: +make_dir() const { + size_t p = 0; + while (p < _filename.length()) { + size_t slash = _filename.find('/', p); + if (slash != string::npos) { + string component = _filename.substr(0, slash); + if (!(component == ".") || + !(component == "..")) { + mkdir(component.c_str(), 0xffff); + } + } + p = slash; + while (p < _filename.length() && _filename[p] == '/') + p++; + } + return true; +} + //////////////////////////////////////////////////////////////////// // Function: Filename::locate_basename diff --git a/dtool/src/dtoolutil/filename.h b/dtool/src/dtoolutil/filename.h index e343c70ffb..c236b4ce66 100644 --- a/dtool/src/dtoolutil/filename.h +++ b/dtool/src/dtoolutil/filename.h @@ -134,6 +134,8 @@ public: bool unlink() const; bool rename_to(const Filename &other) const; + bool make_dir() const; + // Comparison operators are handy. INLINE bool operator == (const string &other) const; INLINE bool operator != (const string &other) const; diff --git a/panda/src/express/multifile.cxx b/panda/src/express/multifile.cxx index f0fc7112af..438339b820 100644 --- a/panda/src/express/multifile.cxx +++ b/panda/src/express/multifile.cxx @@ -236,6 +236,7 @@ bool Multifile::Memfile:: write(void) { ofstream write_stream; _name.set_binary(); + _name.make_dir(); if (!_name.open_write(write_stream)) { express_cat.error() << "Multifile::Memfile::write() - Failed to open output file: " @@ -287,6 +288,7 @@ write(char *&start, int &size) { // Try to open the file for writing if (_file_open == false) { _name.set_binary(); + _name.make_dir(); if ((_file_open = _name.open_write(_write_stream)) == false) { express_cat.error() << "Multfile::Memfile::write() - Couldn't open file: "