From aeedc2a619564a957c8fca000a7fa0148e3d78b1 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 23 Sep 2015 05:08:35 -0700 Subject: 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 --- qthelper.cpp | 22 ++++++++++++++++++++++ qthelperfromc.h | 1 + 2 files changed, 23 insertions(+) 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); diff --git a/qthelperfromc.h b/qthelperfromc.h index d64bfc04a..21b2a6f80 100644 --- a/qthelperfromc.h +++ b/qthelperfromc.h @@ -10,6 +10,7 @@ char *get_file_name(const char *fileName); void copy_image_and_overwrite(const char *cfileName, const char *path, const char *cnewName); char *hashstring(char *filename); bool picture_exists(struct picture *picture); +char *move_away(const char *path); const char *local_file_path(struct picture *picture); void savePictureLocal(struct picture *picture, const char *data, int len); void cache_picture(struct picture *picture); -- cgit v1.2.3-70-g09d2