Skip to content

Commit 25884bd

Browse files
committed
qt, refactor: Move FreespaceChecker class into its own module
1 parent 75a5c82 commit 25884bd

File tree

6 files changed

+116
-86
lines changed

6 files changed

+116
-86
lines changed

src/qt/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ add_library(bitcoinqt STATIC EXCLUDE_FROM_ALL
7575
clientmodel.h
7676
csvmodelwriter.cpp
7777
csvmodelwriter.h
78+
freespacechecker.cpp
79+
freespacechecker.h
7880
guiutil.cpp
7981
guiutil.h
8082
initexecutor.cpp

src/qt/freespacechecker.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2011-present The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <qt/freespacechecker.h>
6+
7+
#include <qt/guiutil.h>
8+
#include <qt/intro.h>
9+
#include <util/fs.h>
10+
11+
#include <QDir>
12+
#include <QString>
13+
14+
#include <cstdint>
15+
16+
FreespaceChecker::FreespaceChecker(Intro *_intro)
17+
{
18+
this->intro = _intro;
19+
}
20+
21+
void FreespaceChecker::check()
22+
{
23+
QString dataDirStr = intro->getPathToCheck();
24+
fs::path dataDir = GUIUtil::QStringToPath(dataDirStr);
25+
uint64_t freeBytesAvailable = 0;
26+
int replyStatus = ST_OK;
27+
QString replyMessage = tr("A new data directory will be created.");
28+
29+
/* Find first parent that exists, so that fs::space does not fail */
30+
fs::path parentDir = dataDir;
31+
fs::path parentDirOld = fs::path();
32+
while(parentDir.has_parent_path() && !fs::exists(parentDir))
33+
{
34+
parentDir = parentDir.parent_path();
35+
36+
/* Check if we make any progress, break if not to prevent an infinite loop here */
37+
if (parentDirOld == parentDir)
38+
break;
39+
40+
parentDirOld = parentDir;
41+
}
42+
43+
try {
44+
freeBytesAvailable = fs::space(parentDir).available;
45+
if(fs::exists(dataDir))
46+
{
47+
if(fs::is_directory(dataDir))
48+
{
49+
QString separator = "<code>" + QDir::toNativeSeparators("/") + tr("name") + "</code>";
50+
replyStatus = ST_OK;
51+
replyMessage = tr("Directory already exists. Add %1 if you intend to create a new directory here.").arg(separator);
52+
} else {
53+
replyStatus = ST_ERROR;
54+
replyMessage = tr("Path already exists, and is not a directory.");
55+
}
56+
}
57+
} catch (const fs::filesystem_error&)
58+
{
59+
/* Parent directory does not exist or is not accessible */
60+
replyStatus = ST_ERROR;
61+
replyMessage = tr("Cannot create data directory here.");
62+
}
63+
Q_EMIT reply(replyStatus, replyMessage, freeBytesAvailable);
64+
}

src/qt/freespacechecker.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2011-present The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_QT_FREESPACECHECKER_H
6+
#define BITCOIN_QT_FREESPACECHECKER_H
7+
8+
#include <QObject>
9+
#include <QString>
10+
#include <QtGlobal>
11+
12+
class Intro;
13+
14+
/* Check free space asynchronously to prevent hanging the UI thread.
15+
16+
Up to one request to check a path is in flight to this thread; when the check()
17+
function runs, the current path is requested from the associated Intro object.
18+
The reply is sent back through a signal.
19+
20+
This ensures that no queue of checking requests is built up while the user is
21+
still entering the path, and that always the most recently entered path is checked as
22+
soon as the thread becomes available.
23+
*/
24+
class FreespaceChecker : public QObject
25+
{
26+
Q_OBJECT
27+
28+
public:
29+
explicit FreespaceChecker(Intro *intro);
30+
31+
enum Status {
32+
ST_OK,
33+
ST_ERROR
34+
};
35+
36+
public Q_SLOTS:
37+
void check();
38+
39+
Q_SIGNALS:
40+
void reply(int status, const QString &message, quint64 available);
41+
42+
private:
43+
Intro *intro;
44+
};
45+
46+
#endif // BITCOIN_QT_FREESPACECHECKER_H

src/qt/intro.cpp

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2022 The Bitcoin Core developers
1+
// Copyright (c) 2011-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -10,6 +10,7 @@
1010
#include <util/chaintype.h>
1111
#include <util/fs.h>
1212

13+
#include <qt/freespacechecker.h>
1314
#include <qt/guiconstants.h>
1415
#include <qt/guiutil.h>
1516
#include <qt/optionsmodel.h>
@@ -25,90 +26,6 @@
2526

2627
#include <cmath>
2728

28-
/* Check free space asynchronously to prevent hanging the UI thread.
29-
30-
Up to one request to check a path is in flight to this thread; when the check()
31-
function runs, the current path is requested from the associated Intro object.
32-
The reply is sent back through a signal.
33-
34-
This ensures that no queue of checking requests is built up while the user is
35-
still entering the path, and that always the most recently entered path is checked as
36-
soon as the thread becomes available.
37-
*/
38-
class FreespaceChecker : public QObject
39-
{
40-
Q_OBJECT
41-
42-
public:
43-
explicit FreespaceChecker(Intro *intro);
44-
45-
enum Status {
46-
ST_OK,
47-
ST_ERROR
48-
};
49-
50-
public Q_SLOTS:
51-
void check();
52-
53-
Q_SIGNALS:
54-
void reply(int status, const QString &message, quint64 available);
55-
56-
private:
57-
Intro *intro;
58-
};
59-
60-
#include <qt/intro.moc>
61-
62-
FreespaceChecker::FreespaceChecker(Intro *_intro)
63-
{
64-
this->intro = _intro;
65-
}
66-
67-
void FreespaceChecker::check()
68-
{
69-
QString dataDirStr = intro->getPathToCheck();
70-
fs::path dataDir = GUIUtil::QStringToPath(dataDirStr);
71-
uint64_t freeBytesAvailable = 0;
72-
int replyStatus = ST_OK;
73-
QString replyMessage = tr("A new data directory will be created.");
74-
75-
/* Find first parent that exists, so that fs::space does not fail */
76-
fs::path parentDir = dataDir;
77-
fs::path parentDirOld = fs::path();
78-
while(parentDir.has_parent_path() && !fs::exists(parentDir))
79-
{
80-
parentDir = parentDir.parent_path();
81-
82-
/* Check if we make any progress, break if not to prevent an infinite loop here */
83-
if (parentDirOld == parentDir)
84-
break;
85-
86-
parentDirOld = parentDir;
87-
}
88-
89-
try {
90-
freeBytesAvailable = fs::space(parentDir).available;
91-
if(fs::exists(dataDir))
92-
{
93-
if(fs::is_directory(dataDir))
94-
{
95-
QString separator = "<code>" + QDir::toNativeSeparators("/") + tr("name") + "</code>";
96-
replyStatus = ST_OK;
97-
replyMessage = tr("Directory already exists. Add %1 if you intend to create a new directory here.").arg(separator);
98-
} else {
99-
replyStatus = ST_ERROR;
100-
replyMessage = tr("Path already exists, and is not a directory.");
101-
}
102-
}
103-
} catch (const fs::filesystem_error&)
104-
{
105-
/* Parent directory does not exist or is not accessible */
106-
replyStatus = ST_ERROR;
107-
replyMessage = tr("Cannot create data directory here.");
108-
}
109-
Q_EMIT reply(replyStatus, replyMessage, freeBytesAvailable);
110-
}
111-
11229
namespace {
11330
//! Return pruning size that will be used if automatic pruning is enabled.
11431
int GetPruneTargetGB()

src/qt/intro.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2021 The Bitcoin Core developers
1+
// Copyright (c) 2011-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

test/lint/lint-circular-dependencies.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"node/blockstorage -> validation -> node/blockstorage",
1717
"node/utxo_snapshot -> validation -> node/utxo_snapshot",
1818
"qt/addresstablemodel -> qt/walletmodel -> qt/addresstablemodel",
19+
"qt/freespacechecker -> qt/intro -> qt/freespacechecker",
1920
"qt/recentrequeststablemodel -> qt/walletmodel -> qt/recentrequeststablemodel",
2021
"qt/sendcoinsdialog -> qt/walletmodel -> qt/sendcoinsdialog",
2122
"qt/transactiontablemodel -> qt/walletmodel -> qt/transactiontablemodel",

0 commit comments

Comments
 (0)