dir.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <http://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/varbuf.h>
  32. #include <dpkg/dir.h>
  33. void
  34. dir_sync(DIR *dir, const char *path)
  35. {
  36. int fd;
  37. fd = dirfd(dir);
  38. if (fd < 0)
  39. ohshite(_("unable to get file descriptor for directory '%s'"),
  40. path);
  41. if (fsync(fd))
  42. ohshite(_("unable to sync directory '%s'"), path);
  43. }
  44. void
  45. dir_sync_path(const char *path)
  46. {
  47. DIR *dir;
  48. dir = opendir(path);
  49. if (!dir)
  50. ohshite(_("unable to open directory '%s'"), path);
  51. dir_sync(dir, path);
  52. closedir(dir);
  53. }
  54. void
  55. dir_sync_path_parent(const char *path)
  56. {
  57. char *dirname, *slash;
  58. dirname = m_strdup(path);
  59. slash = strrchr(dirname, '/');
  60. if (slash != NULL) {
  61. *slash = '\0';
  62. dir_sync_path(dirname);
  63. }
  64. free(dirname);
  65. }
  66. /* FIXME: Ideally we'd use openat() here, to avoid the path mangling, but
  67. * it's not widely available yet, so this will do for now. */
  68. static void
  69. dir_file_sync(const char *dir, const char *filename)
  70. {
  71. struct varbuf path = VARBUF_INIT;
  72. int fd;
  73. varbufprintf(&path, "%s/%s", dir, filename);
  74. fd = open(path.buf, O_WRONLY);
  75. if (fd < 0)
  76. ohshite(_("unable to open file '%s'"), path.buf);
  77. if (fsync(fd))
  78. ohshite(_("unable to sync file '%s'"), path.buf);
  79. if (close(fd))
  80. ohshite(_("unable to close file '%s'"), path.buf);
  81. varbuf_destroy(&path);
  82. }
  83. void
  84. dir_sync_contents(const char *path)
  85. {
  86. DIR *dir;
  87. struct dirent *dent;
  88. dir = opendir(path);
  89. if (!dir)
  90. ohshite(_("unable to open directory '%s'"), path);
  91. while ((dent = readdir(dir)) != NULL) {
  92. if (strcmp(dent->d_name, ".") == 0 ||
  93. strcmp(dent->d_name, "..") == 0)
  94. continue;
  95. dir_file_sync(path, dent->d_name);
  96. }
  97. dir_sync(dir, path);
  98. closedir(dir);
  99. }