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 a0165e3
Show file tree
Hide file tree
Showing 2 changed files with 52 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
51 changes: 47 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,49 @@ 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.
*
* The destination directory will be the parent of the copy of @a directory; that is, @a destination is not itself the copy location.
*/
IoOpReport copyDirectory(const QDir& directory, const QDir& destination, bool recursive, bool overwrite)
{
QDir trueDest(destination.absoluteFilePath(directory.dirName()));

// Ensure destination exists
if(!trueDest.mkpath(u"."_s))
return IoOpReport(IO_OP_WRITE, IO_ERR_CANT_CREATE, trueDest);

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 = trueDest.absoluteFilePath(subPath);

if(fi.isDir() && !trueDest.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, trueDest);
}

/*!
* Computes a file's checksum.
*
Expand Down

0 comments on commit a0165e3

Please sign in to comment.