! 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
| 141 | Returns whether the operation succeeded. |
| 142 | */ |
| 143 | bool FileUtils::copyRecursively(const FileName &srcFilePath, const FileName &tgtFilePath, |
| 144 | QString *error, const std::function<bool (QFileInfo, QFileInfo, QString *)> ©Helper) |
| 145 | { |
| 146 | QFileInfo srcFileInfo = srcFilePath.toFileInfo(); |
| 147 | if (srcFileInfo.isDir()) { |
| 148 | if (!tgtFilePath.exists()) { |
| 149 | QDir targetDir(tgtFilePath.toString()); |
| 150 | targetDir.cdUp(); |
| 151 | if (!targetDir.mkdir(tgtFilePath.fileName())) { |
| 152 | if (error) { |
| 153 | *error = QCoreApplication::translate("Utils::FileUtils", "Failed to create directory \"%1\".") |
| 154 | .arg(tgtFilePath.toUserOutput()); |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | QDir sourceDir(srcFilePath.toString()); |
| 160 | QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot |
| 161 | | QDir::Hidden | QDir::System); |
| 162 | foreach (const QString &fileName, fileNames) { |
| 163 | FileName newSrcFilePath = srcFilePath; |
| 164 | newSrcFilePath.appendPath(fileName); |
| 165 | FileName newTgtFilePath = tgtFilePath; |
| 166 | newTgtFilePath.appendPath(fileName); |
| 167 | if (!copyRecursively(newSrcFilePath, newTgtFilePath, error, copyHelper)) |
| 168 | return false; |
| 169 | } |
| 170 | } else { |
| 171 | if (copyHelper) { |
| 172 | if (!copyHelper(srcFileInfo, tgtFilePath.toFileInfo(), error)) |
| 173 | return false; |
| 174 | } else { |
| 175 | if (!QFile::copy(srcFilePath.toString(), tgtFilePath.toString())) { |
| 176 | if (error) { |
| 177 | *error = QCoreApplication::translate("Utils::FileUtils", "Could not copy file \"%1\" to \"%2\".") |
| 178 | .arg(srcFilePath.toUserOutput(), tgtFilePath.toUserOutput()); |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | /*! |
| 188 | If \a filePath is a directory, the function will recursively check all files and return |
nothing calls this directly
no test coverage detected