From 296f4cdbd517713928d5a062e5258e9a88574deb Mon Sep 17 00:00:00 2001 From: kervala Date: Thu, 29 Sep 2016 17:05:20 +0200 Subject: [PATCH] Changed: New checks after selecting a directory (must be empty and write permissions) --- .../ryzom_installer/src/installdialog.cpp | 34 +++++++++++++++++++ .../ryzom_installer/src/migratedialog.cpp | 34 +++++++++++++++++++ .../client/ryzom_installer/src/utils.cpp | 31 +++++++++++++++++ .../tools/client/ryzom_installer/src/utils.h | 2 ++ 4 files changed, 101 insertions(+) diff --git a/code/ryzom/tools/client/ryzom_installer/src/installdialog.cpp b/code/ryzom/tools/client/ryzom_installer/src/installdialog.cpp index 03f23557b..23379bea5 100644 --- a/code/ryzom/tools/client/ryzom_installer/src/installdialog.cpp +++ b/code/ryzom/tools/client/ryzom_installer/src/installdialog.cpp @@ -158,6 +158,40 @@ void CInstallDialog::accept() return; } + // create directory if doesn't exist + bool succeedsToWrite = QDir().mkpath(m_dstDirectory); + + // if unable to create directory, don't expect to write a file in it + if (succeedsToWrite) + { + // check if directory is writable by current user + QFile file(m_dstDirectory + "/writable_test_for_ryzom_installer.txt"); + + if (file.open(QFile::WriteOnly)) + { + file.close(); + + // remove it + file.remove(); + } + else + { + succeedsToWrite = false; + } + } + + if (!succeedsToWrite) + { + QMessageBox::StandardButton res = QMessageBox::warning(this, tr("Unable to write in directory"), tr("You don't have the permission to write in this directory with your current user account, please choose another directory.")); + return; + } + + if (!isDirectoryEmpty(m_dstDirectory, true)) + { + QMessageBox::StandardButton res = QMessageBox::warning(this, tr("Directory not empty"), tr("This directory is not empty, please choose another one.")); + return; + } + if (oldDirectoryRadioButton->isChecked()) { CConfigFile::getInstance()->setSrcServerDirectory(m_oldDirectory); diff --git a/code/ryzom/tools/client/ryzom_installer/src/migratedialog.cpp b/code/ryzom/tools/client/ryzom_installer/src/migratedialog.cpp index baf89ad29..b930d39cc 100644 --- a/code/ryzom/tools/client/ryzom_installer/src/migratedialog.cpp +++ b/code/ryzom/tools/client/ryzom_installer/src/migratedialog.cpp @@ -127,6 +127,40 @@ void CMigrateDialog::accept() return; } + // create directory if doesn't exist + bool succeedsToWrite = QDir().mkpath(m_dstDirectory); + + // if unable to create directory, don't expect to write a file in it + if (succeedsToWrite) + { + // check if directory is writable by current user + QFile file(m_dstDirectory + "/writable_test_for_ryzom_installer.txt"); + + if (file.open(QFile::WriteOnly)) + { + file.close(); + + // remove it + file.remove(); + } + else + { + succeedsToWrite = false; + } + } + + if (!succeedsToWrite) + { + QMessageBox::StandardButton res = QMessageBox::warning(this, tr("Unable to write in directory"), tr("You don't have the permission to write in this directory with your current user account, please choose another directory.")); + return; + } + + if (!isDirectoryEmpty(m_dstDirectory, true)) + { + QMessageBox::StandardButton res = QMessageBox::warning(this, tr("Directory not empty"), tr("This directory is not empty, please choose another one.")); + return; + } + CConfigFile::getInstance()->setSrcServerDirectory(m_currentDirectory); CConfigFile::getInstance()->setInstallationDirectory(m_dstDirectory); CConfigFile::getInstance()->setUse64BitsClient(clientArch64RadioButton->isChecked()); diff --git a/code/ryzom/tools/client/ryzom_installer/src/utils.cpp b/code/ryzom/tools/client/ryzom_installer/src/utils.cpp index 3408e28bc..e150805e0 100644 --- a/code/ryzom/tools/client/ryzom_installer/src/utils.cpp +++ b/code/ryzom/tools/client/ryzom_installer/src/utils.cpp @@ -38,6 +38,37 @@ QString qBytesToHumanReadable(qint64 bytes) return QString::fromUtf8(NLMISC::bytesToHumanReadableUnits(bytes, units).c_str()); } +bool isDirectoryEmpty(const QString &directory, bool recursize) +{ + bool res = true; + + if (!directory.isEmpty()) + { + QDir dir(directory); + + if (dir.exists()) + { + QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot); + + for (int i = 0; i < list.size(); ++i) + { + QFileInfo fileInfo = list.at(i); + + if (fileInfo.isDir()) + { + if (recursize) if (!isDirectoryEmpty(fileInfo.absoluteFilePath(), true)) return false; + } + else + { + return false; + } + } + } + } + + return res; +} + qint64 getDirectorySize(const QString &directory, bool recursize) { qint64 size = 0; diff --git a/code/ryzom/tools/client/ryzom_installer/src/utils.h b/code/ryzom/tools/client/ryzom_installer/src/utils.h index 1d6519a85..1ad94b24b 100644 --- a/code/ryzom/tools/client/ryzom_installer/src/utils.h +++ b/code/ryzom/tools/client/ryzom_installer/src/utils.h @@ -30,6 +30,8 @@ QString qBytesToHumanReadable(qint64 bytes); +bool isDirectoryEmpty(const QString &directory, bool recursize); + qint64 getDirectorySize(const QString &directory, bool recursize); // Convert a UTF-8 string to QString