Skip to content

Commit

Permalink
Turn TryCreateDirectory() into TryCreateDirectories()
Browse files Browse the repository at this point in the history
Use case: TryCreateDirectory(GetDataDir() / "blocks" / "index") would
fail if the blocks directory was not explicitly created before.

The line that did so was in a weird location and could be removed as a
result.
  • Loading branch information
Marko Bencun authored and furszy committed Apr 24, 2021
1 parent 6125823 commit 8b954c6
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bo
leveldb::Status result = leveldb::DestroyDB(path.string(), options);
dbwrapper_private::HandleError(result);
}
TryCreateDirectory(path);
TryCreateDirectories(path);
LogPrintf("Opening LevelDB in %s\n", path.string());
}
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
Expand Down
3 changes: 0 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,9 +1543,6 @@ bool AppInitMain()

fReindex = gArgs.GetBoolArg("-reindex", false);

// Create blocks directory if it doesn't already exist
fs::create_directories(GetBlocksDir());

// cache size calculations
int64_t nTotalCache = (gArgs.GetArg("-dbcache", nDefaultDbCache) << 20);
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
Expand Down
2 changes: 1 addition & 1 deletion src/qt/intro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ bool Intro::pickDataDirectory()
}
dataDir = intro.getDataDirectory();
try {
TryCreateDirectory(GUIUtil::qstringToBoostPath(dataDir));
TryCreateDirectories(GUIUtil::qstringToBoostPath(dataDir));
break;
} catch (const fs::filesystem_error& e) {
QMessageBox::critical(0, tr("PIVX Core"),
Expand Down
12 changes: 6 additions & 6 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ fs::path GetDefaultDataDir()
#ifdef MAC_OSX
// Mac
pathRet /= "Library/Application Support";
TryCreateDirectory(pathRet);
TryCreateDirectories(pathRet);
return pathRet / "PIVX";
#else
// Unix
Expand Down Expand Up @@ -344,7 +344,7 @@ static fs::path ZC_GetBaseParamsDir()
#ifdef MAC_OSX
// Mac
pathRet /= "Library/Application Support";
TryCreateDirectory(pathRet);
TryCreateDirectories(pathRet);
return pathRet / "PIVXParams";
#else
// Unix
Expand Down Expand Up @@ -597,20 +597,20 @@ bool RenameOver(fs::path src, fs::path dest)
}

/**
* Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
* Ignores exceptions thrown by Boost's create_directories 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 fs::path& p)
bool TryCreateDirectories(const fs::path& p)
{
try {
return fs::create_directory(p);
return fs::create_directories(p);
} catch (const fs::filesystem_error&) {
if (!fs::exists(p) || !fs::is_directory(p))
throw;
}

// create_directory didn't create the directory, it had to have existed already
// create_directories didn't create the directory, it had to have existed already
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool TruncateFile(FILE* file, unsigned int length);
int RaiseFileDescriptorLimit(int nMinFD);
void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length);
bool RenameOver(fs::path src, fs::path dest);
bool TryCreateDirectory(const fs::path& p);
bool TryCreateDirectories(const fs::path& p);
fs::path GetDefaultDataDir();
// The blocks directory is always net specific.
const fs::path &GetBlocksDir();
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool CDBEnv::Open(const fs::path& pathIn)

strPath = pathIn.string();
fs::path pathLogDir = pathIn / "database";
TryCreateDirectory(pathLogDir);
TryCreateDirectories(pathLogDir);
fs::path pathErrorFile = pathIn / "db.log";
LogPrintf("CDBEnv::Open: LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());

Expand Down

0 comments on commit 8b954c6

Please sign in to comment.