atomic-file.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * atomic-file.c - atomic file helper functions
  4. *
  5. * Copyright © 2011 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/stat.h>
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/dpkg.h>
  29. #include <dpkg/atomic-file.h>
  30. #define ATOMIC_FILE_NEW_EXT "-new"
  31. #define ATOMIC_FILE_OLD_EXT "-old"
  32. struct atomic_file *
  33. atomic_file_new(const char *filename, enum atomic_file_flags flags)
  34. {
  35. struct atomic_file *file;
  36. file = m_malloc(sizeof(*file));
  37. file->flags = flags;
  38. file->fp = NULL;
  39. file->name = m_strdup(filename);
  40. m_asprintf(&file->name_new, "%s%s", filename, ATOMIC_FILE_NEW_EXT);
  41. return file;
  42. }
  43. void
  44. atomic_file_open(struct atomic_file *file)
  45. {
  46. file->fp = fopen(file->name_new, "w");
  47. if (file->fp == NULL)
  48. ohshite(_("unable to create new file '%.250s'"),
  49. file->name_new);
  50. fchmod(fileno(file->fp), 0644);
  51. push_cleanup(cu_closestream, ~ehflag_normaltidy, NULL, 0, 1, file->fp);
  52. }
  53. void
  54. atomic_file_sync(struct atomic_file *file)
  55. {
  56. if (ferror(file->fp))
  57. ohshite(_("unable to write new file '%.250s'"), file->name_new);
  58. if (fflush(file->fp))
  59. ohshite(_("unable to flush new file '%.250s'"), file->name_new);
  60. if (fsync(fileno(file->fp)))
  61. ohshite(_("unable to sync new file '%.250s'"), file->name_new);
  62. }
  63. void
  64. atomic_file_close(struct atomic_file *file)
  65. {
  66. pop_cleanup(ehflag_normaltidy); /* fopen */
  67. if (fclose(file->fp))
  68. ohshite(_("unable to close new file `%.250s'"), file->name_new);
  69. }
  70. static void
  71. atomic_file_backup(struct atomic_file *file)
  72. {
  73. char *name_old;
  74. m_asprintf(&name_old, "%s%s", file->name, ATOMIC_FILE_OLD_EXT);
  75. if (unlink(name_old) && errno != ENOENT)
  76. ohshite(_("error removing old backup file '%s'"), name_old);
  77. if (link(file->name, name_old) && errno != ENOENT)
  78. ohshite(_("error creating new backup file '%s'"), name_old);
  79. free(name_old);
  80. }
  81. void
  82. atomic_file_remove(struct atomic_file *file)
  83. {
  84. if (unlink(file->name_new))
  85. ohshite(_("cannot remove `%.250s'"), file->name_new);
  86. if (unlink(file->name) && errno != ENOENT)
  87. ohshite(_("cannot remove `%.250s'"), file->name);
  88. }
  89. void
  90. atomic_file_commit(struct atomic_file *file)
  91. {
  92. if (file->flags & aff_backup)
  93. atomic_file_backup(file);
  94. if (rename(file->name_new, file->name))
  95. ohshite(_("error installing new file '%s'"), file->name);
  96. }
  97. void
  98. atomic_file_free(struct atomic_file *file)
  99. {
  100. free(file->name_new);
  101. free(file->name);
  102. free(file);
  103. }