Bitcoin Core  31.0.0
P2P Digital Currency
freespacechecker.cpp
Go to the documentation of this file.
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 <util/fs.h>
9 
10 #include <QDir>
11 #include <QString>
12 
13 #include <cstdint>
14 
16 {
17  QString dataDirStr = intro->getPathToCheck();
18  fs::path dataDir = GUIUtil::QStringToPath(dataDirStr);
19  uint64_t freeBytesAvailable = 0;
20  int replyStatus = ST_OK;
21  QString replyMessage = tr("A new data directory will be created.");
22 
23  /* Find first parent that exists, so that fs::space does not fail */
24  fs::path parentDir = dataDir;
25  fs::path parentDirOld = fs::path();
26  while(parentDir.has_parent_path() && !fs::exists(parentDir))
27  {
28  parentDir = parentDir.parent_path();
29 
30  /* Check if we make any progress, break if not to prevent an infinite loop here */
31  if (parentDirOld == parentDir)
32  break;
33 
34  parentDirOld = parentDir;
35  }
36 
37  try {
38  freeBytesAvailable = fs::space(parentDir).available;
39  if(fs::exists(dataDir))
40  {
41  if(fs::is_directory(dataDir))
42  {
43  QString separator = "<code>" + QDir::toNativeSeparators("/") + tr("name") + "</code>";
44  replyStatus = ST_OK;
45  replyMessage = tr("Directory already exists. Add %1 if you intend to create a new directory here.").arg(separator);
46  } else {
47  replyStatus = ST_ERROR;
48  replyMessage = tr("Path already exists, and is not a directory.");
49  }
50  }
51  } catch (const fs::filesystem_error&)
52  {
53  /* Parent directory does not exist or is not accessible */
54  replyStatus = ST_ERROR;
55  replyMessage = tr("Cannot create data directory here.");
56  }
57  Q_EMIT reply(replyStatus, replyMessage, freeBytesAvailable);
58 }
void reply(int status, const QString &message, quint64 available)
virtual QString getPathToCheck()=0
static bool exists(const path &p)
Definition: fs.h:95
fs::path QStringToPath(const QString &path)
Convert QString to OS specific boost path through UTF-8.
Definition: guiutil.cpp:670
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33