dir.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * dir.c - directory handling functions
  4. *
  5. * Copyright © 2010 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 <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <dirent.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <dpkg/dpkg.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dir.h>
  32. /**
  33. * Sync a directory to disk from a DIR structure.
  34. *
  35. * @param dir The directory to sync.
  36. * @param path The name of the directory to sync (for error messages).
  37. */
  38. static void
  39. dir_sync(DIR *dir, const char *path)
  40. {
  41. int fd;
  42. fd = dirfd(dir);
  43. if (fd < 0)
  44. ohshite(_("unable to get file descriptor for directory '%s'"),
  45. path);
  46. if (fsync(fd))
  47. ohshite(_("unable to sync directory '%s'"), path);
  48. }
  49. /**
  50. * Sync a directory to disk from a pathname.
  51. *
  52. * @param path The name of the directory to sync.
  53. */
  54. void
  55. dir_sync_path(const char *path)
  56. {
  57. DIR *dir;
  58. dir = opendir(path);
  59. if (!dir)
  60. ohshite(_("unable to open directory '%s'"), path);
  61. dir_sync(dir, path);
  62. closedir(dir);
  63. }
  64. /**
  65. * Sync to disk the parent directory of a pathname.
  66. *
  67. * @param path The child pathname of the directory to sync.
  68. */
  69. void
  70. dir_sync_path_parent(const char *path)
  71. {
  72. char *dirname, *slash;
  73. dirname = m_strdup(path);
  74. slash = strrchr(dirname, '/');
  75. if (slash != NULL) {
  76. *slash = '\0';
  77. dir_sync_path(dirname);
  78. }
  79. free(dirname);
  80. }
  81. /* FIXME: Ideally we'd use openat() here, to avoid the path mangling, but
  82. * it's not widely available yet, so this will do for now. */
  83. static void
  84. dir_file_sync(const char *dir, const char *filename)
  85. {
  86. char *path;
  87. int fd;
  88. m_asprintf(&path, "%s/%s", dir, filename);
  89. fd = open(path, O_WRONLY);
  90. if (fd < 0)
  91. ohshite(_("unable to open file '%s'"), path);
  92. if (fsync(fd))
  93. ohshite(_("unable to sync file '%s'"), path);
  94. if (close(fd))
  95. ohshite(_("unable to close file '%s'"), path);
  96. free(path);
  97. }
  98. /**
  99. * Sync to disk the contents and the directory specified.
  100. *
  101. * @param path The pathname to sync.
  102. */
  103. void
  104. dir_sync_contents(const char *path)
  105. {
  106. DIR *dir;
  107. struct dirent *dent;
  108. dir = opendir(path);
  109. if (!dir)
  110. ohshite(_("unable to open directory '%s'"), path);
  111. while ((dent = readdir(dir)) != NULL) {
  112. if (strcmp(dent->d_name, ".") == 0 ||
  113. strcmp(dent->d_name, "..") == 0)
  114. continue;
  115. dir_file_sync(path, dent->d_name);
  116. }
  117. dir_sync(dir, path);
  118. closedir(dir);
  119. }