diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-12-09 15:38:55 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-12-09 15:38:55 +0000 |
commit | 101b56cdd67b56b4db3be7a50d9050d2a1743715 (patch) | |
tree | 65fa45ef0f625551c6e94c6271ce3e74a3b36563 /src/util/disk.c | |
parent | 36a48508667b24317d2c64cc261d6c03141111c4 (diff) | |
download | gnunet-101b56cdd67b56b4db3be7a50d9050d2a1743715.tar.gz gnunet-101b56cdd67b56b4db3be7a50d9050d2a1743715.zip |
adding GNUNET_DISK_file_backup function; fixing #2646
Diffstat (limited to 'src/util/disk.c')
-rw-r--r-- | src/util/disk.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/util/disk.c b/src/util/disk.c index 8d1fed897..8cb69d2a4 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -495,6 +495,38 @@ GNUNET_DISK_mkdtemp (const char *t) | |||
495 | 495 | ||
496 | 496 | ||
497 | /** | 497 | /** |
498 | * Move a file out of the way (create a backup) by | ||
499 | * renaming it to "orig.NUM~" where NUM is the smallest | ||
500 | * number that is not used yet. | ||
501 | * | ||
502 | * @param fil name of the file to back up | ||
503 | */ | ||
504 | void | ||
505 | GNUNET_DISK_file_backup (const char *fil) | ||
506 | { | ||
507 | size_t slen; | ||
508 | char *target; | ||
509 | unsigned int num; | ||
510 | |||
511 | slen = strlen (fil) + 20; | ||
512 | target = GNUNET_malloc (slen); | ||
513 | num = 0; | ||
514 | do | ||
515 | { | ||
516 | GNUNET_snprintf (target, slen, | ||
517 | "%s.%u~", | ||
518 | fil, | ||
519 | num++); | ||
520 | } while (0 == access (target, F_OK)); | ||
521 | if (0 != rename (fil, target)) | ||
522 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
523 | "rename", | ||
524 | fil); | ||
525 | GNUNET_free (target); | ||
526 | } | ||
527 | |||
528 | |||
529 | /** | ||
498 | * Create an (empty) temporary file on disk. If the given name is not | 530 | * Create an (empty) temporary file on disk. If the given name is not |
499 | * an absolute path, the current 'TMPDIR' will be prepended. In any case, | 531 | * an absolute path, the current 'TMPDIR' will be prepended. In any case, |
500 | * 6 random characters will be appended to the name to create a unique | 532 | * 6 random characters will be appended to the name to create a unique |