diff --git a/lib/io/include/qx/io/qx-common-io.h b/lib/io/include/qx/io/qx-common-io.h index af0fbeb3..e13ea717 100644 --- a/lib/io/include/qx/io/qx-common-io.h +++ b/lib/io/include/qx/io/qx-common-io.h @@ -71,12 +71,13 @@ QX_IO_EXPORT IoOpReport writeStringToFile(QSaveFile& textFile, const QString& te QX_IO_EXPORT IoOpReport deleteTextFromFile(QFile& textFile, TextPos startPos, TextPos endPos); // Directory: -QX_IO_EXPORT bool dirContainsFiles(QDir directory, QDirIterator::IteratorFlags iteratorFlags); -QX_IO_EXPORT IoOpReport dirContainsFiles(bool& returnBuffer, QDir directory, QDirIterator::IteratorFlags iteratorFlags); -QX_IO_EXPORT IoOpReport dirContentInfoList(QFileInfoList& returnBuffer, QDir directory, QStringList nameFilters = QStringList(), +QX_IO_EXPORT bool dirContainsFiles(const QDir& directory, QDirIterator::IteratorFlags iteratorFlags); +QX_IO_EXPORT IoOpReport dirContainsFiles(bool& returnBuffer, const QDir& directory, QDirIterator::IteratorFlags iteratorFlags); +QX_IO_EXPORT IoOpReport dirContentInfoList(QFileInfoList& returnBuffer, const QDir& directory, QStringList nameFilters = QStringList(), QDir::Filters filters = QDir::NoFilter, QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags); -QX_IO_EXPORT IoOpReport dirContentList(QStringList& returnBuffer, QDir directory, QStringList nameFilters = QStringList(), +QX_IO_EXPORT IoOpReport dirContentList(QStringList& returnBuffer, const QDir& directory, QStringList nameFilters = QStringList(), QDir::Filters filters = QDir::NoFilter, QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags, PathType pathType = Absolute); +QX_IO_EXPORT IoOpReport copyDirectory(const QDir& directory, const QDir& destination, bool recursive = true, bool overwrite = false); // Integrity QX_IO_EXPORT IoOpReport calculateFileChecksum(QString& returnBuffer, QFile& file, QCryptographicHash::Algorithm hashAlgorithm); diff --git a/lib/io/src/qx-common-io.cpp b/lib/io/src/qx-common-io.cpp index 9b1fdf87..0a8767bf 100644 --- a/lib/io/src/qx-common-io.cpp +++ b/lib/io/src/qx-common-io.cpp @@ -1270,7 +1270,7 @@ IoOpReport deleteTextFromFile(QFile& textFile, TextPos startPos, TextPos endPos) * * @warning This also returns false if the directory doesn't exist. */ -bool dirContainsFiles(QDir directory, QDirIterator::IteratorFlags iteratorFlags) +bool dirContainsFiles(const QDir& directory, QDirIterator::IteratorFlags iteratorFlags) { // Construct directory iterator QDirIterator listIterator(directory.path(), QDir::Files | QDir::NoDotAndDotDot, iteratorFlags); @@ -1285,7 +1285,7 @@ bool dirContainsFiles(QDir directory, QDirIterator::IteratorFlags iteratorFlags) * If the directory doesn't exist, @a returnBuffer will be set to false and an operation report noting the directory's absence * is returned. */ -IoOpReport dirContainsFiles(bool& returnBuffer, QDir directory, QDirIterator::IteratorFlags iteratorFlags) +IoOpReport dirContainsFiles(bool& returnBuffer, const QDir& directory, QDirIterator::IteratorFlags iteratorFlags) { // Assume false returnBuffer = false; @@ -1314,7 +1314,7 @@ IoOpReport dirContainsFiles(bool& returnBuffer, QDir directory, QDirIterator::It * * @sa QDir::entryInfoList */ -IoOpReport dirContentInfoList(QFileInfoList& returnBuffer, QDir directory, QStringList nameFilters, +IoOpReport dirContentInfoList(QFileInfoList& returnBuffer, const QDir& directory, QStringList nameFilters, QDir::Filters filters, QDirIterator::IteratorFlags flags) { // Empty buffer @@ -1358,7 +1358,7 @@ IoOpReport dirContentInfoList(QFileInfoList& returnBuffer, QDir directory, QStri * * @sa QDir::entryList */ -IoOpReport dirContentList(QStringList& returnBuffer, QDir directory, QStringList nameFilters, +IoOpReport dirContentList(QStringList& returnBuffer, const QDir& directory, QStringList nameFilters, QDir::Filters filters, QDirIterator::IteratorFlags flags, PathType pathType) { // Empty buffer @@ -1389,6 +1389,45 @@ IoOpReport dirContentList(QStringList& returnBuffer, QDir directory, QStringList return IoOpReport(IO_OP_ENUMERATE, IO_SUCCESS, directory); } +/*! + * Copies @a directory to @a destination, recursively if @a recursive is @c true. Existing files are overwritten if + * @a overwrite is @c true. + */ +IoOpReport copyDirectory(const QDir& directory, const QDir& destination, bool recursive, bool overwrite) +{ + // Ensure destination exists + if(!destination.mkpath(u"."_s)) + return IoOpReport(IO_OP_WRITE, IO_ERR_CANT_CREATE, destination); + + QDirIterator srcItr(directory.path(), QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs, recursive ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags); + while(srcItr.hasNext()) + { + srcItr.next(); + QFileInfo fi = srcItr.fileInfo(); + + QString subPath = fi.absoluteFilePath().mid(directory.absolutePath().length()); + QString absDestPath = destination.absoluteFilePath(subPath); + + if(fi.isDir() && !destination.mkpath(subPath)) + return IoOpReport(IO_OP_WRITE, IO_ERR_CANT_CREATE, QDir(absDestPath)); + else if(fi.isFile()) + { + if(QFile::exists(absDestPath)) + { + if(overwrite && !QFile::remove(absDestPath)) + return IoOpReport(IO_OP_WRITE, IO_ERR_REMOVE, QFile(absDestPath)); + else + return IoOpReport(IO_OP_WRITE, IO_ERR_EXISTS, QFile(absDestPath)); + } + + if(!QFile::copy(fi.absoluteFilePath(), absDestPath)) + return IoOpReport(IO_OP_WRITE, IO_ERR_COPY, QFile(absDestPath)); + } + } + + return IoOpReport(IO_OP_WRITE, IO_SUCCESS, destination); +} + /*! * Computes a file's checksum. *