aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/fileutils.cpp
diff options
context:
space:
mode:
authorDaniel Molkentin <[email protected]>2011-07-05 12:59:14 +0200
committerDaniel Molkentin <[email protected]>2011-07-06 12:38:11 +0200
commit15732d1bd2eca57b1f3cc47818a18f8b23232470 (patch)
tree70bdf1969fea7f500a2ac0ac8dc9cb8e20577f40 /src/libs/utils/fileutils.cpp
parent1ea75f0cbf5329648cefaf17c8949dfebd5130fb (diff)
RemoteLinux: Move generic Dir/File functions into Utils.
- Make error parameter an optional pointer for consistency with DirUtils::copyRecursively() - Add documentation Change-Id: I6671142341dbdcf2c8ca1118c35ea05548920609 Reviewed-on: http://codereview.qt.nokia.com/1090 Reviewed-by: Qt Sanity Bot <[email protected]> Reviewed-by: Christian Kandeler <[email protected]>
Diffstat (limited to 'src/libs/utils/fileutils.cpp')
-rw-r--r--src/libs/utils/fileutils.cpp140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp
index 871e5ebb528..b4eb010a4ae 100644
--- a/src/libs/utils/fileutils.cpp
+++ b/src/libs/utils/fileutils.cpp
@@ -38,12 +38,152 @@
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QTemporaryFile>
+#include <QtCore/QDateTime>
#include <QtCore/QDataStream>
#include <QtCore/QTextStream>
#include <QtGui/QMessageBox>
namespace Utils {
+/*! \class Utils::FileUtils
+
+ \brief File- and directory-related convenience functions.
+
+ File- and directory-related convenience functions.
+*/
+
+/*!
+ \fn Utils::FileUtils::removeRecursively(const QString &filePath, QString *error)
+
+ Removes the directory \a filePath and its subdirectories recursively.
+
+ \note The \a error parameter is optional.
+
+ \return Whether the operation succeeded.
+*/
+
+/*!
+ \fn Utils::FileUtils::copyRecursively(const QString &srcFilePath, const QString &tgtFilePath, QString *error)
+
+ Copies the directory specified by \a srcFilePath recursively to \a tgtFilePath. \a tgtFilePath will contain
+ the target directory, which will be created. Example usage:
+
+ \code
+ QString error;
+ book ok = Utils::FileUtils::copyRecursively("/foo/bar", "/foo/baz", &error);
+ if (!ok)
+ qDebug() << error;
+ \endcode
+
+ This will copy the contents of /foo/bar into to the baz directory under /foo, which will be created in the process.
+
+ \note The \a error parameter is optional.
+
+ \return Whether the operation succeeded.
+*/
+
+/*!
+ \fn Utils::FileUtils::isFileNewerThan(const QString &filePath, const QDateTime &timeStamp)
+
+ If \a filePath is a directory, the function will recursively check all files and return
+ true if one of them is newer than \a timeStamp. If \a filePath is a single file, true will
+ be returned if the file is newer than \timeStamp.
+
+ \return Whether at least one file in \a filePath has a newer date than \a timeStamp.
+*/
+
+bool FileUtils::removeRecursively(const QString &filePath, QString *error)
+{
+ QFileInfo fileInfo(filePath);
+ if (!fileInfo.exists())
+ return true;
+ QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser);
+ if (fileInfo.isDir()) {
+ QDir dir(filePath);
+ QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden
+ | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot);
+ foreach (const QString &fileName, fileNames) {
+ if (!removeRecursively(filePath + QLatin1Char('/') + fileName, error))
+ return false;
+ }
+ dir.cdUp();
+ if (!dir.rmdir(fileInfo.fileName())) {
+ if (error) {
+ *error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove directory '%1'.")
+ .arg(QDir::toNativeSeparators(filePath));
+ }
+ return false;
+ }
+ } else {
+ if (!QFile::remove(filePath)) {
+ if (error) {
+ *error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove file '%1'.")
+ .arg(QDir::toNativeSeparators(filePath));
+ }
+ return false;
+ }
+ }
+ return true;
+}
+
+bool FileUtils::copyRecursively(const QString &srcFilePath,
+ const QString &tgtFilePath, QString *error)
+{
+ QFileInfo srcFileInfo(srcFilePath);
+ if (srcFileInfo.isDir()) {
+ QDir targetDir(tgtFilePath);
+ targetDir.cdUp();
+ if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName())) {
+ if (error) {
+ *error = QCoreApplication::translate("Utils::FileUtils", "Failed to create directory '%1'.")
+ .arg(QDir::toNativeSeparators(tgtFilePath));
+ return false;
+ }
+ }
+ QDir sourceDir(srcFilePath);
+ QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
+ foreach (const QString &fileName, fileNames) {
+ const QString newSrcFilePath
+ = srcFilePath + QLatin1Char('/') + fileName;
+ const QString newTgtFilePath
+ = tgtFilePath + QLatin1Char('/') + fileName;
+ if (!copyRecursively(newSrcFilePath, newTgtFilePath, error))
+ return false;
+ }
+ } else {
+ if (!QFile::copy(srcFilePath, tgtFilePath)) {
+ if (error) {
+ *error = QCoreApplication::translate("Utils::FileUtils", "Could not copy file '%1' to '%2'.")
+ .arg(QDir::toNativeSeparators(srcFilePath),
+ QDir::toNativeSeparators(tgtFilePath));
+ }
+ return false;
+ }
+ }
+ return true;
+}
+
+bool FileUtils::isFileNewerThan(const QString &filePath,
+ const QDateTime &timeStamp)
+{
+ QFileInfo fileInfo(filePath);
+ if (!fileInfo.exists() || fileInfo.lastModified() >= timeStamp)
+ return true;
+ if (fileInfo.isDir()) {
+ const QStringList dirContents = QDir(filePath)
+ .entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
+ foreach (const QString &curFileName, dirContents) {
+ const QString curFilePath
+ = filePath + QLatin1Char('/') + curFileName;
+ if (isFileNewerThan(curFilePath, timeStamp))
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
QByteArray FileReader::fetchQrc(const QString &fileName)
{
QTC_ASSERT(fileName.startsWith(QLatin1Char(':')), return QByteArray())