Wrap create_directory calls in try...catch blocks.
Ignores any exceptions thrown if directory exists, otherwise re-throws exception. Rebased-By: Wladimir J. van der Laan <laanwj@gmail.com>
This commit is contained in:
parent
5a2ed60a04
commit
2b7709dc84
5 changed files with 22 additions and 4 deletions
|
@ -72,7 +72,7 @@ bool CDBEnv::Open(const boost::filesystem::path& pathIn)
|
||||||
|
|
||||||
path = pathIn;
|
path = pathIn;
|
||||||
filesystem::path pathLogDir = path / "database";
|
filesystem::path pathLogDir = path / "database";
|
||||||
filesystem::create_directory(pathLogDir);
|
TryCreateDirectory(pathLogDir);
|
||||||
filesystem::path pathErrorFile = path / "db.log";
|
filesystem::path pathErrorFile = path / "db.log";
|
||||||
LogPrintf("dbenv.open LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());
|
LogPrintf("dbenv.open LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path &path, size_t nCa
|
||||||
LogPrintf("Wiping LevelDB in %s\n", path.string());
|
LogPrintf("Wiping LevelDB in %s\n", path.string());
|
||||||
leveldb::DestroyDB(path.string(), options);
|
leveldb::DestroyDB(path.string(), options);
|
||||||
}
|
}
|
||||||
boost::filesystem::create_directory(path);
|
TryCreateDirectory(path);
|
||||||
LogPrintf("Opening LevelDB in %s\n", path.string());
|
LogPrintf("Opening LevelDB in %s\n", path.string());
|
||||||
}
|
}
|
||||||
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
|
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
|
||||||
|
|
|
@ -178,7 +178,7 @@ void Intro::pickDataDirectory()
|
||||||
}
|
}
|
||||||
dataDir = intro.getDataDirectory();
|
dataDir = intro.getDataDirectory();
|
||||||
try {
|
try {
|
||||||
fs::create_directory(GUIUtil::qstringToBoostPath(dataDir));
|
TryCreateDirectory(GUIUtil::qstringToBoostPath(dataDir));
|
||||||
break;
|
break;
|
||||||
} catch(fs::filesystem_error &e) {
|
} catch(fs::filesystem_error &e) {
|
||||||
QMessageBox::critical(0, tr("Bitcoin"),
|
QMessageBox::critical(0, tr("Bitcoin"),
|
||||||
|
|
19
src/util.cpp
19
src/util.cpp
|
@ -976,7 +976,7 @@ boost::filesystem::path GetDefaultDataDir()
|
||||||
#ifdef MAC_OSX
|
#ifdef MAC_OSX
|
||||||
// Mac
|
// Mac
|
||||||
pathRet /= "Library/Application Support";
|
pathRet /= "Library/Application Support";
|
||||||
fs::create_directory(pathRet);
|
TryCreateDirectory(pathRet);
|
||||||
return pathRet / "Bitcoin";
|
return pathRet / "Bitcoin";
|
||||||
#else
|
#else
|
||||||
// Unix
|
// Unix
|
||||||
|
@ -1090,6 +1090,23 @@ bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
|
||||||
#endif /* WIN32 */
|
#endif /* WIN32 */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Ignores exceptions thrown by boost's create_directory if the requested directory exists.
|
||||||
|
// Specifically handles case where path p exists, but it wasn't possible for the user to write to the parent directory.
|
||||||
|
bool TryCreateDirectory(const boost::filesystem::path& p)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return boost::filesystem::create_directory(p);
|
||||||
|
} catch (boost::filesystem::filesystem_error) {
|
||||||
|
if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create_directory didn't create the directory, it had to have existed already
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void FileCommit(FILE *fileout)
|
void FileCommit(FILE *fileout)
|
||||||
{
|
{
|
||||||
fflush(fileout); // harmless if redundantly called
|
fflush(fileout); // harmless if redundantly called
|
||||||
|
|
|
@ -189,6 +189,7 @@ bool TruncateFile(FILE *file, unsigned int length);
|
||||||
int RaiseFileDescriptorLimit(int nMinFD);
|
int RaiseFileDescriptorLimit(int nMinFD);
|
||||||
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
|
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
|
||||||
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
|
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
|
||||||
|
bool TryCreateDirectory(const boost::filesystem::path& p);
|
||||||
boost::filesystem::path GetDefaultDataDir();
|
boost::filesystem::path GetDefaultDataDir();
|
||||||
const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
|
const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
|
||||||
boost::filesystem::path GetConfigFile();
|
boost::filesystem::path GetConfigFile();
|
||||||
|
|
Loading…
Reference in a new issue