sitecopy-0.16.6-10-bts410703-preserve-storage-files-sigint.patch 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. From: Andreas Henriksson <andreas@fatal.se>
  2. Subject: Preserve storage files it SIGINT (Ctrl+C) is sent
  3. --- sitecopy-0.16.3/src/sitestore.c 2006-02-04 11:18:08.000000000 +0100
  4. +++ sitecopy-0.16.3-pending/src/sitestore.c 2008-06-08 21:56:23.000000000 +0200
  5. @@ -60,17 +60,42 @@
  6. /* Opens the storage file for writing */
  7. FILE *site_open_storage_file(struct site *site)
  8. {
  9. + char filebuf[PATH_MAX];
  10. +
  11. + /* open a temporary "pending" file, to not corrupt the site file in case
  12. + * the program aborts while we are updating it.
  13. + * The site_close_storage_file() function will rename it to it's proper
  14. + * name.
  15. + * FIXME: something should clean up old *.pending files,
  16. + * which never got properly closed.
  17. + */
  18. + snprintf(filebuf, sizeof(filebuf), "%s.pending", site->infofile);
  19. +
  20. if (site->storage_file == NULL) {
  21. - site->storage_file = fopen(site->infofile, "w" FOPEN_BINARY_FLAGS);
  22. + site->storage_file = fopen(filebuf, "w" FOPEN_BINARY_FLAGS);
  23. }
  24. return site->storage_file;
  25. }
  26. int site_close_storage_file(struct site *site)
  27. {
  28. - int ret = fclose(site->storage_file);
  29. + char filebuf[PATH_MAX];
  30. + int err;
  31. +
  32. + /* close filehandle */
  33. + err = fclose(site->storage_file);
  34. site->storage_file = NULL;
  35. - return ret;
  36. + if (err) {
  37. + perror("fclose");
  38. + return err;
  39. + }
  40. +
  41. + /* rename pending file to real filename (overwriting existing file). */
  42. + snprintf(filebuf, sizeof(filebuf), "%s.pending", site->infofile);
  43. + err = rename(filebuf, site->infofile);
  44. + if (err)
  45. + perror("rename");
  46. + return err;
  47. }
  48. /* Return escaped form of 'filename'; any XML-unsafe characters are