Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ GRIDCOIN_TESTS =\
test/beacon_tests.cpp \
test/bignum_tests.cpp \
test/block_tests.cpp \
test/fs_tests.cpp \
test/getarg_tests.cpp \
test/gridcoin_tests.cpp \
test/key_tests.cpp \
Expand Down
20 changes: 16 additions & 4 deletions src/addrdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,30 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
fs::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fsbridge::fopen(pathTmp, "wb");
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull())
if (fileout.IsNull()) {
fileout.fclose();
remove(pathTmp);
return error("%s: Failed to open file %s", __func__, pathTmp.string());
}

// Serialize
if (!SerializeDB(fileout, data)) return false;
if (!FileCommit(fileout.Get()))
if (!SerializeDB(fileout, data)) {
fileout.fclose();
remove(pathTmp);
return false;
}
if (!FileCommit(fileout.Get())) {
fileout.fclose();
remove(pathTmp);
return error("%s: Failed to flush file %s", __func__, pathTmp.string());
}
fileout.fclose();

// replace existing file, if any, with new file
if (!RenameOver(pathTmp, path))
if (!RenameOver(pathTmp, path)) {
remove(pathTmp);
return error("%s: Rename-into-place failed", __func__);
}

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool BackupPrivateKeys(const CWallet& wallet, std::string& sTarget, std::string&
filesystem::create_directories(PrivateKeysTarget.parent_path());
sTarget = PrivateKeysTarget.string();
fsbridge::ofstream myBackup;
myBackup.open (PrivateKeysTarget.string().c_str());
myBackup.open(PrivateKeysTarget);
std::string sError;
for(const auto& keyPair : wallet.GetAllPrivateKeys(sError))
{
Expand Down
26 changes: 12 additions & 14 deletions src/boinc.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include "boinc.h"
#include "util.h"

#include <boost/filesystem.hpp>

std::string GetBoincDataDir(){
fs::path GetBoincDataDir(){

std::string path = GetArgument("boincdatadir", "");
if (!path.empty()){
return path;
return fs::path(std::move(path));
}

#ifdef WIN32
Expand All @@ -28,37 +26,37 @@ std::string GetBoincDataDir(){
(LPBYTE)&szPath,
&dwSize) == ERROR_SUCCESS){
RegCloseKey(hKey);
std::wstring wsPath = szPath;
// TODO: Use and return wstring when all file operations use unicode
std::string path(wsPath.begin(),wsPath.end());
if (boost::filesystem::exists(path)){

fs::path path = std::wstring(szPath);

if (fs::exists(path)){
return path;
} else {
LogPrintf("Cannot find BOINC data dir %s.", path);
LogPrintf("Cannot find BOINC data dir %s.", path.string());
}
}
RegCloseKey(hKey);
}

if (boost::filesystem::exists("C:\\ProgramData\\BOINC\\")){
if (fs::exists("C:\\ProgramData\\BOINC\\")){
return "C:\\ProgramData\\BOINC\\";
}
else if(boost::filesystem::exists("C:\\Documents and Settings\\All Users\\Application Data\\BOINC\\")){
else if(fs::exists("C:\\Documents and Settings\\All Users\\Application Data\\BOINC\\")){
return "C:\\Documents and Settings\\All Users\\Application Data\\BOINC\\";
}
#endif

#ifdef __linux__
if (boost::filesystem::exists("/var/lib/boinc-client/")){
if (fs::exists("/var/lib/boinc-client/")){
return "/var/lib/boinc-client/";
}
else if (boost::filesystem::exists("/var/lib/boinc/")){
else if (fs::exists("/var/lib/boinc/")){
return "/var/lib/boinc/";
}
#endif

#ifdef __APPLE__
if (boost::filesystem::exists("/Library/Application Support/BOINC Data/")){
if (fs::exists("/Library/Application Support/BOINC Data/")){
return "/Library/Application Support/BOINC Data/";
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/boinc.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef GRIDCOIN_BOINC_H
#define GRIDCOIN_BOINC_H

#include <string>
#include <fs.h>

std::string GetBoincDataDir();
fs::path GetBoincDataDir();

#endif
120 changes: 5 additions & 115 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@
#include "ui_interface.h"
#include "util.h"

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <stdint.h>

#ifndef WIN32
#include "sys/stat.h"
#endif

using namespace std;
using namespace boost;


unsigned int nWalletDBUpdated;



//
// CDB
//
Expand Down Expand Up @@ -60,7 +54,7 @@ void CDBEnv::Close()
EnvShutdown();
}

bool CDBEnv::Open(boost::filesystem::path pathEnv_)
bool CDBEnv::Open(fs::path pathEnv_)
{
if (fDbEnvInit)
return true;
Expand All @@ -69,11 +63,11 @@ bool CDBEnv::Open(boost::filesystem::path pathEnv_)
return false;

pathEnv = pathEnv_;
filesystem::path pathDataDir = pathEnv;
fs::path pathDataDir = pathEnv;
strPath = pathDataDir.string();
filesystem::path pathLogDir = pathDataDir / "database";
filesystem::create_directory(pathLogDir);
filesystem::path pathErrorFile = pathDataDir / "db.log";
fs::path pathLogDir = pathDataDir / "database";
fs::create_directory(pathLogDir);
fs::path pathErrorFile = pathDataDir / "db.log";
LogPrintf("dbenv.open LogDir=%s ErrorFile=%s", pathLogDir.string(), pathErrorFile.string());

unsigned int nEnvFlags = 0;
Expand Down Expand Up @@ -485,107 +479,3 @@ void CDBEnv::Flush(bool fShutdown)
}
}
}


//
// CAddrDB
//

/*
CAddrDB::CAddrDB()
{
pathAddr = GetDataDir() / "peers.dat";
}

bool CAddrDB::Write(const CAddrMan& addr)
{
// Generate random temporary filename
unsigned short randv = 0;
RAND_bytes((unsigned char *)&randv, sizeof(randv));
std::string tmpfn = strprintf("peers.dat.%04x", randv);

// serialize addresses, checksum data up to that point, then append csum
CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
ssPeers << pchMessageStart;
ssPeers << addr;
uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
ssPeers << hash;

// open temp output file, and associate with CAutoFile
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fsbridge::fopen(pathTmp.string().c_str(), "wb");
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull())
return error("CAddrman::Write() : open failed");

// Write and commit header, data
try {
fileout << ssPeers;
}
catch (std::exception &e) {
return error("CAddrman::Write() : I/O error");
}
FileCommit(fileout.Get());
fileout.fclose();

// replace existing peers.dat, if any, with new peers.dat.XXXX
if (!RenameOver(pathTmp, pathAddr))
return error("CAddrman::Write() : Rename-into-place failed");

return true;
}

bool CAddrDB::Read(CAddrMan& addr)
{
// open input file, and associate with CAutoFile
FILE *file = fsbridge::fopen(pathAddr.string().c_str(), "rb");
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (filein.IsNull())
return error("CAddrman::Read() : open failed");

// use file size to size memory buffer
int fileSize = boost::filesystem::file_size(pathAddr);
int dataSize = fileSize - sizeof(uint256);
// Don't try to resize to a negative number if file is small
if ( dataSize < 0 ) dataSize = 0;
vector<unsigned char> vchData;
vchData.resize(dataSize);
uint256 hashIn;

// read data and checksum from file
try
{
filein.read((char *)&vchData[0], dataSize);
filein >> hashIn;
}
catch (std::exception &e) {
return error("CAddrman::Read() 2 : I/O error or stream data corrupted");
}
filein.fclose();

CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);

// verify stored checksum matches input data
uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
if (hashIn != hashTmp)
return error("CAddrman::Read() : checksum mismatch; data corrupted");

unsigned char pchMsgTmp[4];
try {
// de-serialize file header (pchMessageStart magic number) and
ssPeers >> pchMsgTmp;

// verify the network matches ours
if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
return error("CAddrman::Read() : invalid network magic number");

// de-serialize address data into one CAddrMan object
ssPeers >> addr;
}
catch (std::exception &e) {
return error("CAddrman::Read() : I/O error or stream data corrupted");
}

return true;
}
*/
19 changes: 3 additions & 16 deletions src/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BITCOIN_DB_H
#define BITCOIN_DB_H

#include "fs.h"
#include "main.h"
#include "streams.h"

Expand Down Expand Up @@ -34,7 +35,7 @@ class CDBEnv
private:
bool fDbEnvInit;
bool fMockDb;
boost::filesystem::path pathEnv;
fs::path pathEnv;
std::string strPath;

void EnvShutdown();
Expand Down Expand Up @@ -68,7 +69,7 @@ class CDBEnv
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);

bool Open(boost::filesystem::path pathEnv_);
bool Open(fs::path pathEnv_);
void Close();
void Flush(bool fShutdown);
void CheckpointLSN(std::string strFile);
Expand Down Expand Up @@ -307,18 +308,4 @@ class CDB
bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
};


/** Access to the (IP) address database (peers.dat) */
/*
class CAddrDB
{
private:
boost::filesystem::path pathAddr;
public:
CAddrDB();
bool Write(const CAddrMan& addr);
bool Read(CAddrMan& addr);
};
*/

#endif // BITCOIN_DB_H
4 changes: 2 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ bool AppInit2(ThreadHandlerPtr threads)

for (auto const& strFile : mapMultiArgs["-loadblock"])
{
FILE *file = fsbridge::fopen(strFile.c_str(), "rb");
FILE *file = fsbridge::fopen(strFile, "rb");
if (file)
LoadExternalBlockFile(file);
}
Expand All @@ -947,7 +947,7 @@ bool AppInit2(ThreadHandlerPtr threads)
if (filesystem::exists(pathBootstrap)) {
uiInterface.InitMessage(_("Importing bootstrap blockchain data file."));

FILE *file = fsbridge::fopen(pathBootstrap.string().c_str(), "rb");
FILE *file = fsbridge::fopen(pathBootstrap, "rb");
if (file) {
filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
LoadExternalBlockFile(file);
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4221,7 +4221,7 @@ FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszM
{
if ((nFile < 1) || (nFile == (unsigned int) -1))
return NULL;
FILE* file = fsbridge::fopen(BlockFilePath(nFile).string().c_str(), pszMode);
FILE* file = fsbridge::fopen(BlockFilePath(nFile), pszMode);
if (!file)
return NULL;
if (nBlockPos != 0 && !strchr(pszMode, 'a') && !strchr(pszMode, 'w'))
Expand Down
6 changes: 3 additions & 3 deletions src/neuralnet/researcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ bool ConfiguredForInvestorMode()
//!
boost::optional<std::string> ReadClientStateXml()
{
const std::string path = GetBoincDataDir();
std::string contents = GetFileContents(path + "client_state.xml");
const fs::path path = GetBoincDataDir();
std::string contents = GetFileContents(path / "client_state.xml");

if (contents != "-1") {
return boost::make_optional(std::move(contents));
Expand All @@ -86,7 +86,7 @@ boost::optional<std::string> ReadClientStateXml()
LogPrintf("WARNING: Unable to obtain BOINC CPIDs.");

if (!GetArgument("boincdatadir", "").empty()) {
LogPrintf("Could not access configured BOINC data directory %s", path);
LogPrintf("Could not access configured BOINC data directory %s", path.string());
} else {
LogPrintf(
"BOINC data directory is not installed in the default location.\n"
Expand Down
Loading