diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2015-09-23 05:08:35 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-09-23 05:10:43 -0700 |
commit | aeedc2a619564a957c8fca000a7fa0148e3d78b1 (patch) | |
tree | 19792af0b898d1d7cab771e55b4c766e031c0cd7 /qthelper.cpp | |
parent | c7ae8c8655a618de92ca6072dbe51dcea4493cb7 (diff) | |
download | subsurface-aeedc2a619564a957c8fca000a7fa0148e3d78b1.tar.gz |
Add helper function to safely move away file or directory
Try numberical suffix until you find one that isn't used yet.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qthelper.cpp')
-rw-r--r-- | qthelper.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/qthelper.cpp b/qthelper.cpp index 6e3c957a6..3a4daf833 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -627,6 +627,28 @@ extern "C" const char *system_default_directory(void) return filename; } +extern "C" char *move_away(const char *old_path) +{ + if (verbose > 1) + qDebug() << "move away" << old_path; + QFile oldFile(old_path); + QFile newFile; + QString newPath; + int i = 0; + do { + newPath = QString(old_path) + QString(".%1").arg(++i); + newFile.setFileName(newPath); + } while(newFile.exists()); + if (verbose > 1) + qDebug() << "renaming to" << newPath; + if (!oldFile.rename(newPath)) { + qDebug() << "rename of" << old_path << "to" << newPath << "failed"; + return strdup(""); + } + return strdup(qPrintable(newPath)); + +} + extern "C" char *get_file_name(const char *fileName) { QFileInfo fileInfo(fileName); |