aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-12-09 15:38:55 +0000
committerChristian Grothoff <christian@grothoff.org>2012-12-09 15:38:55 +0000
commit101b56cdd67b56b4db3be7a50d9050d2a1743715 (patch)
tree65fa45ef0f625551c6e94c6271ce3e74a3b36563 /src/util
parent36a48508667b24317d2c64cc261d6c03141111c4 (diff)
downloadgnunet-101b56cdd67b56b4db3be7a50d9050d2a1743715.tar.gz
gnunet-101b56cdd67b56b4db3be7a50d9050d2a1743715.zip
adding GNUNET_DISK_file_backup function; fixing #2646
Diffstat (limited to 'src/util')
-rw-r--r--src/util/crypto_rsa.c13
-rw-r--r--src/util/disk.c32
2 files changed, 36 insertions, 9 deletions
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index ff99ecf0b..cd9a33f61 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -641,12 +641,11 @@ try_read_key (const char *filename)
641 if (fs > UINT16_MAX) 641 if (fs > UINT16_MAX)
642 { 642 {
643 LOG (GNUNET_ERROR_TYPE_ERROR, 643 LOG (GNUNET_ERROR_TYPE_ERROR,
644 _("File `%s' does not contain a valid private key (too long, %llu bytes). Deleting it.\n"), 644 _("File `%s' does not contain a valid private key (too long, %llu bytes). Renaming it.\n"),
645 filename, 645 filename,
646 (unsigned long long) fs); 646 (unsigned long long) fs);
647 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd)); 647 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
648 if (0 != UNLINK (filename)) 648 GNUNET_DISK_file_backup (filename);
649 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
650 return NULL; 649 return NULL;
651 } 650 }
652 651
@@ -662,8 +661,7 @@ try_read_key (const char *filename)
662 filename, 661 filename,
663 (unsigned long long) fs); 662 (unsigned long long) fs);
664 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd)); 663 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
665 if (0 != UNLINK (filename)) 664 GNUNET_DISK_file_backup (filename);
666 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
667 GNUNET_free (enc); 665 GNUNET_free (enc);
668 return NULL; 666 return NULL;
669 } 667 }
@@ -857,10 +855,7 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
857 LOG (GNUNET_ERROR_TYPE_ERROR, 855 LOG (GNUNET_ERROR_TYPE_ERROR,
858 _("File `%s' does not contain a valid private key. Deleting it.\n"), 856 _("File `%s' does not contain a valid private key. Deleting it.\n"),
859 filename); 857 filename);
860 if (0 != UNLINK (filename)) 858 GNUNET_DISK_file_backup (filename);
861 {
862 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
863 }
864 } 859 }
865 GNUNET_free (enc); 860 GNUNET_free (enc);
866 if (GNUNET_YES != 861 if (GNUNET_YES !=
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 */
504void
505GNUNET_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