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>
This commit is contained in:
Dirk Hohndel 2015-09-23 05:08:35 -07:00
parent c7ae8c8655
commit aeedc2a619
2 changed files with 23 additions and 0 deletions

View file

@ -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);

View file

@ -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);