From ac0f84ace1287afca122f96107c4d17d3ccd9d02 Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 29 Jul 2009 16:33:40 +0000 Subject: [PATCH] mkdir issues --- direct/src/plugin/mkdir_complete.cxx | 22 ++++++++++++++++------ direct/src/plugin/mkdir_complete.h | 5 +++-- direct/src/plugin/p3dFileDownload.cxx | 3 +-- direct/src/plugin/p3dInstanceManager.cxx | 1 - direct/src/plugin/p3dMultifileReader.cxx | 3 +-- direct/src/plugin/p3dPackage.cxx | 5 ++--- direct/src/plugin_npapi/ppInstance.cxx | 2 +- direct/src/plugin_standalone/panda3d.cxx | 3 ++- 8 files changed, 26 insertions(+), 18 deletions(-) diff --git a/direct/src/plugin/mkdir_complete.cxx b/direct/src/plugin/mkdir_complete.cxx index d0aac7ea27..5a52329321 100755 --- a/direct/src/plugin/mkdir_complete.cxx +++ b/direct/src/plugin/mkdir_complete.cxx @@ -21,6 +21,7 @@ #include #include // for mkdir() #include +#include // strerror() #endif @@ -54,7 +55,7 @@ get_dirname(const string &filename) { // necessary. //////////////////////////////////////////////////////////////////// bool -mkdir_complete(const string &dirname) { +mkdir_complete(const string &dirname, ostream &logfile) { #ifdef _WIN32 if (CreateDirectory(dirname.c_str(), NULL) != 0) { // Success! @@ -77,6 +78,8 @@ mkdir_complete(const string &dirname) { // Got it! return true; } + logfile + << "Couldn't create " << dirname << "\n"; } } return false; @@ -93,15 +96,18 @@ mkdir_complete(const string &dirname) { return true; } - if (errno == ENOENT) { + if (errno == ENOENT || errno == EACCES) { // We need to make the parent directory first. string parent = get_dirname(dirname); - if (!parent.empty() && mkdir_complete(parent)) { + if (!parent.empty() && mkdir_complete(parent, logfile)) { // Parent successfully created. Try again to make the child. if (mkdir(dirname.c_str(), 0777) == 0) { // Got it! return true; } + // Couldn't create the directory. :( + logfile + << "Couldn't create " << dirname << ": " << strerror(errno) << "\n"; } } return false; @@ -117,7 +123,7 @@ mkdir_complete(const string &dirname) { // needed. //////////////////////////////////////////////////////////////////// bool -mkfile_complete(const string &filename) { +mkfile_complete(const string &filename, ostream &logfile) { #ifdef _WIN32 HANDLE file = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, @@ -125,13 +131,15 @@ mkfile_complete(const string &filename) { if (file == INVALID_HANDLE_VALUE) { // Try to make the parent directory first. string parent = get_dirname(filename); - if (!parent.empty() && mkdir_complete(parent)) { + if (!parent.empty() && mkdir_complete(parent, logfile)) { // Parent successfully created. Try again to make the file. file = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); } if (file == INVALID_HANDLE_VALUE) { + logfile + << "Couldn't create " << filename << "\n"; return false; } } @@ -143,11 +151,13 @@ mkfile_complete(const string &filename) { if (fd == -1) { // Try to make the parent directory first. string parent = get_dirname(filename); - if (!parent.empty() && mkdir_complete(parent)) { + if (!parent.empty() && mkdir_complete(parent, logfile)) { // Parent successfully created. Try again to make the file. fd = creat(filename.c_str(), 0777); } if (fd == -1) { + logfile + << "Couldn't create " << filename << ": " << strerror(errno) << "\n"; return false; } } diff --git a/direct/src/plugin/mkdir_complete.h b/direct/src/plugin/mkdir_complete.h index c10398de3e..e55c29f7ec 100755 --- a/direct/src/plugin/mkdir_complete.h +++ b/direct/src/plugin/mkdir_complete.h @@ -16,10 +16,11 @@ #define MKDIR_COMPLETE_H #include +#include using namespace std; -bool mkdir_complete(const string &dirname); -bool mkfile_complete(const string &dirname); +bool mkdir_complete(const string &dirname, ostream &logfile); +bool mkfile_complete(const string &dirname, ostream &logfile); #endif diff --git a/direct/src/plugin/p3dFileDownload.cxx b/direct/src/plugin/p3dFileDownload.cxx index 4c7e881d19..81a3521eba 100755 --- a/direct/src/plugin/p3dFileDownload.cxx +++ b/direct/src/plugin/p3dFileDownload.cxx @@ -46,8 +46,7 @@ set_filename(const string &filename) { //////////////////////////////////////////////////////////////////// bool P3DFileDownload:: open_file() { - if (!mkfile_complete(_filename)) { - nout << "Unable to create " << _filename << "\n"; + if (!mkfile_complete(_filename, nout)) { return false; } diff --git a/direct/src/plugin/p3dInstanceManager.cxx b/direct/src/plugin/p3dInstanceManager.cxx index 4ba7e08e0a..f4c7cb0e20 100644 --- a/direct/src/plugin/p3dInstanceManager.cxx +++ b/direct/src/plugin/p3dInstanceManager.cxx @@ -22,7 +22,6 @@ #include "p3dNoneObject.h" #include "p3dBoolObject.h" #include "find_root_dir.h" -#include "mkdir_complete.h" #ifdef _WIN32 #include diff --git a/direct/src/plugin/p3dMultifileReader.cxx b/direct/src/plugin/p3dMultifileReader.cxx index fcd65e4312..828c65d4e1 100644 --- a/direct/src/plugin/p3dMultifileReader.cxx +++ b/direct/src/plugin/p3dMultifileReader.cxx @@ -106,8 +106,7 @@ extract(const string &pathname, const string &to_dir, const Subfile &s = (*si); string output_pathname = to_dir + "/" + s._filename; - if (!mkfile_complete(output_pathname)) { - nout << "Unable to create " << output_pathname << "\n"; + if (!mkfile_complete(output_pathname, nout)) { return false; } diff --git a/direct/src/plugin/p3dPackage.cxx b/direct/src/plugin/p3dPackage.cxx index f342dbdd65..f4daafe86f 100755 --- a/direct/src/plugin/p3dPackage.cxx +++ b/direct/src/plugin/p3dPackage.cxx @@ -60,7 +60,7 @@ P3DPackage(const string &package_name, const string &package_version, // Ensure the package directory exists; create it if it does not. _package_dir += string("/") + _package_version; - mkdir_complete(_package_dir); + mkdir_complete(_package_dir, nout); _desc_file_basename = _package_fullname + ".xml"; _desc_file_pathname = _package_dir + "/" + _desc_file_basename; @@ -389,8 +389,7 @@ uncompress_archive() { return; } - if (!mkfile_complete(target_pathname)) { - nout << "Unable to create " << target_pathname << "\n"; + if (!mkfile_complete(target_pathname, nout)) { report_done(false); return; } diff --git a/direct/src/plugin_npapi/ppInstance.cxx b/direct/src/plugin_npapi/ppInstance.cxx index b47094f3e2..afca7e10f0 100644 --- a/direct/src/plugin_npapi/ppInstance.cxx +++ b/direct/src/plugin_npapi/ppInstance.cxx @@ -862,7 +862,7 @@ downloaded_plugin(const string &filename) { // Copy the file onto the target. string pathname = _core_api_dll.get_pathname(_root_dir); - mkfile_complete(pathname); + mkfile_complete(pathname, nout); ifstream in(filename.c_str(), ios::in | ios::binary); ofstream out(pathname.c_str(), ios::out | ios::binary); diff --git a/direct/src/plugin_standalone/panda3d.cxx b/direct/src/plugin_standalone/panda3d.cxx index 270e5ec054..caa25a828a 100644 --- a/direct/src/plugin_standalone/panda3d.cxx +++ b/direct/src/plugin_standalone/panda3d.cxx @@ -299,7 +299,7 @@ get_plugin(const string &root_url, const string &this_platform, bool force_downl HTTPClient *http = HTTPClient::get_global_ptr(); PT(HTTPChannel) channel = http->get_document(url); - Ramfile rf; + contents.make_dir(); if (!channel->download_to_file(contents)) { cerr << "Unable to download " << url << "\n"; return false; @@ -370,6 +370,7 @@ get_core_api(const string &root_url, TiXmlElement *xpackage) { Filename pathname = Filename::from_os_specific(_core_api_dll.get_pathname(_root_dir)); HTTPClient *http = HTTPClient::get_global_ptr(); PT(HTTPChannel) channel = http->get_document(url); + pathname.make_dir(); if (!channel->download_to_file(pathname)) { cerr << "Unable to download " << url << "\n"; return false;