mkdir issues

This commit is contained in:
David Rose 2009-07-29 16:33:40 +00:00
parent 357fc73101
commit ac0f84ace1
8 changed files with 26 additions and 18 deletions

View File

@ -21,6 +21,7 @@
#include <fcntl.h>
#include <sys/stat.h> // for mkdir()
#include <errno.h>
#include <string.h> // 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;
}
}

View File

@ -16,10 +16,11 @@
#define MKDIR_COMPLETE_H
#include <string>
#include <iostream>
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

View File

@ -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;
}

View File

@ -22,7 +22,6 @@
#include "p3dNoneObject.h"
#include "p3dBoolObject.h"
#include "find_root_dir.h"
#include "mkdir_complete.h"
#ifdef _WIN32
#include <shlobj.h>

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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;