Skip to content

Commit

Permalink
Add copyDirectory() to Io
Browse files Browse the repository at this point in the history
  • Loading branch information
oblivioncth committed Jul 1, 2024
1 parent aeb3780 commit 5de7d8e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/io/include/qx/io/qx-common-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 43 additions & 4 deletions lib/io/src/qx-common-io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 5de7d8e

Please sign in to comment.