Skip to content

Commit ccd9f0d

Browse files
authored
Merge pull request #1582 from cyrossignol/fix-fsbridge-paths
Fix lingering peers.dat temp files and clean up remaining paths
2 parents 0eba980 + 00cf411 commit ccd9f0d

21 files changed

+144
-218
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ GRIDCOIN_TESTS =\
3434
test/beacon_tests.cpp \
3535
test/bignum_tests.cpp \
3636
test/block_tests.cpp \
37+
test/fs_tests.cpp \
3738
test/getarg_tests.cpp \
3839
test/gridcoin_tests.cpp \
3940
test/key_tests.cpp \

src/addrdb.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,30 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
4848
fs::path pathTmp = GetDataDir() / tmpfn;
4949
FILE *file = fsbridge::fopen(pathTmp, "wb");
5050
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
51-
if (fileout.IsNull())
51+
if (fileout.IsNull()) {
52+
fileout.fclose();
53+
remove(pathTmp);
5254
return error("%s: Failed to open file %s", __func__, pathTmp.string());
55+
}
5356

5457
// Serialize
55-
if (!SerializeDB(fileout, data)) return false;
56-
if (!FileCommit(fileout.Get()))
58+
if (!SerializeDB(fileout, data)) {
59+
fileout.fclose();
60+
remove(pathTmp);
61+
return false;
62+
}
63+
if (!FileCommit(fileout.Get())) {
64+
fileout.fclose();
65+
remove(pathTmp);
5766
return error("%s: Failed to flush file %s", __func__, pathTmp.string());
67+
}
5868
fileout.fclose();
5969

6070
// replace existing file, if any, with new file
61-
if (!RenameOver(pathTmp, path))
71+
if (!RenameOver(pathTmp, path)) {
72+
remove(pathTmp);
6273
return error("%s: Rename-into-place failed", __func__);
74+
}
6375

6476
return true;
6577
}

src/backup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ bool BackupPrivateKeys(const CWallet& wallet, std::string& sTarget, std::string&
115115
filesystem::create_directories(PrivateKeysTarget.parent_path());
116116
sTarget = PrivateKeysTarget.string();
117117
fsbridge::ofstream myBackup;
118-
myBackup.open (PrivateKeysTarget.string().c_str());
118+
myBackup.open(PrivateKeysTarget);
119119
std::string sError;
120120
for(const auto& keyPair : wallet.GetAllPrivateKeys(sError))
121121
{

src/boinc.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#include "boinc.h"
22
#include "util.h"
33

4-
#include <boost/filesystem.hpp>
5-
6-
std::string GetBoincDataDir(){
4+
fs::path GetBoincDataDir(){
75

86
std::string path = GetArgument("boincdatadir", "");
97
if (!path.empty()){
10-
return path;
8+
return fs::path(std::move(path));
119
}
1210

1311
#ifdef WIN32
@@ -28,37 +26,37 @@ std::string GetBoincDataDir(){
2826
(LPBYTE)&szPath,
2927
&dwSize) == ERROR_SUCCESS){
3028
RegCloseKey(hKey);
31-
std::wstring wsPath = szPath;
32-
// TODO: Use and return wstring when all file operations use unicode
33-
std::string path(wsPath.begin(),wsPath.end());
34-
if (boost::filesystem::exists(path)){
29+
30+
fs::path path = std::wstring(szPath);
31+
32+
if (fs::exists(path)){
3533
return path;
3634
} else {
37-
LogPrintf("Cannot find BOINC data dir %s.", path);
35+
LogPrintf("Cannot find BOINC data dir %s.", path.string());
3836
}
3937
}
4038
RegCloseKey(hKey);
4139
}
4240

43-
if (boost::filesystem::exists("C:\\ProgramData\\BOINC\\")){
41+
if (fs::exists("C:\\ProgramData\\BOINC\\")){
4442
return "C:\\ProgramData\\BOINC\\";
4543
}
46-
else if(boost::filesystem::exists("C:\\Documents and Settings\\All Users\\Application Data\\BOINC\\")){
44+
else if(fs::exists("C:\\Documents and Settings\\All Users\\Application Data\\BOINC\\")){
4745
return "C:\\Documents and Settings\\All Users\\Application Data\\BOINC\\";
4846
}
4947
#endif
5048

5149
#ifdef __linux__
52-
if (boost::filesystem::exists("/var/lib/boinc-client/")){
50+
if (fs::exists("/var/lib/boinc-client/")){
5351
return "/var/lib/boinc-client/";
5452
}
55-
else if (boost::filesystem::exists("/var/lib/boinc/")){
53+
else if (fs::exists("/var/lib/boinc/")){
5654
return "/var/lib/boinc/";
5755
}
5856
#endif
5957

6058
#ifdef __APPLE__
61-
if (boost::filesystem::exists("/Library/Application Support/BOINC Data/")){
59+
if (fs::exists("/Library/Application Support/BOINC Data/")){
6260
return "/Library/Application Support/BOINC Data/";
6361
}
6462
#endif

src/boinc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef GRIDCOIN_BOINC_H
22
#define GRIDCOIN_BOINC_H
33

4-
#include <string>
4+
#include <fs.h>
55

6-
std::string GetBoincDataDir();
6+
fs::path GetBoincDataDir();
77

88
#endif

src/db.cpp

Lines changed: 5 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,16 @@
99
#include "ui_interface.h"
1010
#include "util.h"
1111

12-
#include <boost/filesystem.hpp>
13-
#include <boost/filesystem/fstream.hpp>
1412
#include <stdint.h>
1513

1614
#ifndef WIN32
1715
#include "sys/stat.h"
1816
#endif
1917

2018
using namespace std;
21-
using namespace boost;
22-
2319

2420
unsigned int nWalletDBUpdated;
2521

26-
27-
2822
//
2923
// CDB
3024
//
@@ -60,7 +54,7 @@ void CDBEnv::Close()
6054
EnvShutdown();
6155
}
6256

63-
bool CDBEnv::Open(boost::filesystem::path pathEnv_)
57+
bool CDBEnv::Open(fs::path pathEnv_)
6458
{
6559
if (fDbEnvInit)
6660
return true;
@@ -69,11 +63,11 @@ bool CDBEnv::Open(boost::filesystem::path pathEnv_)
6963
return false;
7064

7165
pathEnv = pathEnv_;
72-
filesystem::path pathDataDir = pathEnv;
66+
fs::path pathDataDir = pathEnv;
7367
strPath = pathDataDir.string();
74-
filesystem::path pathLogDir = pathDataDir / "database";
75-
filesystem::create_directory(pathLogDir);
76-
filesystem::path pathErrorFile = pathDataDir / "db.log";
68+
fs::path pathLogDir = pathDataDir / "database";
69+
fs::create_directory(pathLogDir);
70+
fs::path pathErrorFile = pathDataDir / "db.log";
7771
LogPrintf("dbenv.open LogDir=%s ErrorFile=%s", pathLogDir.string(), pathErrorFile.string());
7872

7973
unsigned int nEnvFlags = 0;
@@ -485,107 +479,3 @@ void CDBEnv::Flush(bool fShutdown)
485479
}
486480
}
487481
}
488-
489-
490-
//
491-
// CAddrDB
492-
//
493-
494-
/*
495-
CAddrDB::CAddrDB()
496-
{
497-
pathAddr = GetDataDir() / "peers.dat";
498-
}
499-
500-
bool CAddrDB::Write(const CAddrMan& addr)
501-
{
502-
// Generate random temporary filename
503-
unsigned short randv = 0;
504-
RAND_bytes((unsigned char *)&randv, sizeof(randv));
505-
std::string tmpfn = strprintf("peers.dat.%04x", randv);
506-
507-
// serialize addresses, checksum data up to that point, then append csum
508-
CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
509-
ssPeers << pchMessageStart;
510-
ssPeers << addr;
511-
uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
512-
ssPeers << hash;
513-
514-
// open temp output file, and associate with CAutoFile
515-
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
516-
FILE *file = fsbridge::fopen(pathTmp.string().c_str(), "wb");
517-
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
518-
if (fileout.IsNull())
519-
return error("CAddrman::Write() : open failed");
520-
521-
// Write and commit header, data
522-
try {
523-
fileout << ssPeers;
524-
}
525-
catch (std::exception &e) {
526-
return error("CAddrman::Write() : I/O error");
527-
}
528-
FileCommit(fileout.Get());
529-
fileout.fclose();
530-
531-
// replace existing peers.dat, if any, with new peers.dat.XXXX
532-
if (!RenameOver(pathTmp, pathAddr))
533-
return error("CAddrman::Write() : Rename-into-place failed");
534-
535-
return true;
536-
}
537-
538-
bool CAddrDB::Read(CAddrMan& addr)
539-
{
540-
// open input file, and associate with CAutoFile
541-
FILE *file = fsbridge::fopen(pathAddr.string().c_str(), "rb");
542-
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
543-
if (filein.IsNull())
544-
return error("CAddrman::Read() : open failed");
545-
546-
// use file size to size memory buffer
547-
int fileSize = boost::filesystem::file_size(pathAddr);
548-
int dataSize = fileSize - sizeof(uint256);
549-
// Don't try to resize to a negative number if file is small
550-
if ( dataSize < 0 ) dataSize = 0;
551-
vector<unsigned char> vchData;
552-
vchData.resize(dataSize);
553-
uint256 hashIn;
554-
555-
// read data and checksum from file
556-
try
557-
{
558-
filein.read((char *)&vchData[0], dataSize);
559-
filein >> hashIn;
560-
}
561-
catch (std::exception &e) {
562-
return error("CAddrman::Read() 2 : I/O error or stream data corrupted");
563-
}
564-
filein.fclose();
565-
566-
CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
567-
568-
// verify stored checksum matches input data
569-
uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
570-
if (hashIn != hashTmp)
571-
return error("CAddrman::Read() : checksum mismatch; data corrupted");
572-
573-
unsigned char pchMsgTmp[4];
574-
try {
575-
// de-serialize file header (pchMessageStart magic number) and
576-
ssPeers >> pchMsgTmp;
577-
578-
// verify the network matches ours
579-
if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
580-
return error("CAddrman::Read() : invalid network magic number");
581-
582-
// de-serialize address data into one CAddrMan object
583-
ssPeers >> addr;
584-
}
585-
catch (std::exception &e) {
586-
return error("CAddrman::Read() : I/O error or stream data corrupted");
587-
}
588-
589-
return true;
590-
}
591-
*/

src/db.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_DB_H
66
#define BITCOIN_DB_H
77

8+
#include "fs.h"
89
#include "main.h"
910
#include "streams.h"
1011

@@ -34,7 +35,7 @@ class CDBEnv
3435
private:
3536
bool fDbEnvInit;
3637
bool fMockDb;
37-
boost::filesystem::path pathEnv;
38+
fs::path pathEnv;
3839
std::string strPath;
3940

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

71-
bool Open(boost::filesystem::path pathEnv_);
72+
bool Open(fs::path pathEnv_);
7273
void Close();
7374
void Flush(bool fShutdown);
7475
void CheckpointLSN(std::string strFile);
@@ -307,18 +308,4 @@ class CDB
307308
bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
308309
};
309310

310-
311-
/** Access to the (IP) address database (peers.dat) */
312-
/*
313-
class CAddrDB
314-
{
315-
private:
316-
boost::filesystem::path pathAddr;
317-
public:
318-
CAddrDB();
319-
bool Write(const CAddrMan& addr);
320-
bool Read(CAddrMan& addr);
321-
};
322-
*/
323-
324311
#endif // BITCOIN_DB_H

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ bool AppInit2(ThreadHandlerPtr threads)
936936

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

950-
FILE *file = fsbridge::fopen(pathBootstrap.string().c_str(), "rb");
950+
FILE *file = fsbridge::fopen(pathBootstrap, "rb");
951951
if (file) {
952952
filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
953953
LoadExternalBlockFile(file);

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4221,7 +4221,7 @@ FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszM
42214221
{
42224222
if ((nFile < 1) || (nFile == (unsigned int) -1))
42234223
return NULL;
4224-
FILE* file = fsbridge::fopen(BlockFilePath(nFile).string().c_str(), pszMode);
4224+
FILE* file = fsbridge::fopen(BlockFilePath(nFile), pszMode);
42254225
if (!file)
42264226
return NULL;
42274227
if (nBlockPos != 0 && !strchr(pszMode, 'a') && !strchr(pszMode, 'w'))

src/neuralnet/researcher.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ bool ConfiguredForInvestorMode()
7676
//!
7777
boost::optional<std::string> ReadClientStateXml()
7878
{
79-
const std::string path = GetBoincDataDir();
80-
std::string contents = GetFileContents(path + "client_state.xml");
79+
const fs::path path = GetBoincDataDir();
80+
std::string contents = GetFileContents(path / "client_state.xml");
8181

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

8888
if (!GetArgument("boincdatadir", "").empty()) {
89-
LogPrintf("Could not access configured BOINC data directory %s", path);
89+
LogPrintf("Could not access configured BOINC data directory %s", path.string());
9090
} else {
9191
LogPrintf(
9292
"BOINC data directory is not installed in the default location.\n"

0 commit comments

Comments
 (0)