|
1 | | -// Copyright (c) 2011-2022 The Bitcoin Core developers |
| 1 | +// Copyright (c) 2011-present The Bitcoin Core developers |
2 | 2 | // Distributed under the MIT software license, see the accompanying |
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | 4 |
|
|
10 | 10 | #include <util/chaintype.h> |
11 | 11 | #include <util/fs.h> |
12 | 12 |
|
| 13 | +#include <qt/freespacechecker.h> |
13 | 14 | #include <qt/guiconstants.h> |
14 | 15 | #include <qt/guiutil.h> |
15 | 16 | #include <qt/optionsmodel.h> |
|
25 | 26 |
|
26 | 27 | #include <cmath> |
27 | 28 |
|
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 | | - |
112 | 29 | namespace { |
113 | 30 | //! Return pruning size that will be used if automatic pruning is enabled. |
114 | 31 | int GetPruneTargetGB() |
|
0 commit comments