freespacechecker.cpp
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 15 void FreespaceChecker::check() 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 }