filehelper.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2018 Markus Hennecke <markus-hennecke@markus-hennecke.de>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <sys/stat.h>
  17. #include <sys/types.h>
  18. #include <dirent.h>
  19. #include <err.h>
  20. #include <fcntl.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include "filehelper.h"
  26. struct dir_list *
  27. get_dir_entries_fd(int _fd)
  28. {
  29. DIR *dir = fdopendir(_fd);
  30. if (dir) {
  31. struct dir_list *list = malloc(sizeof(struct dir_list));
  32. if (list == NULL)
  33. err(1, NULL);
  34. int cwd = open(".", O_DIRECTORY | O_RDONLY);
  35. if (cwd == -1)
  36. err(1, NULL);
  37. if (fchdir(_fd) == -1)
  38. err(1, NULL);
  39. TAILQ_INIT(&list->entries);
  40. list->newest = 0;
  41. list->path = NULL;
  42. struct dirent *dirent;
  43. while ((dirent = readdir(dir)) != NULL) {
  44. struct dir_entry *entry;
  45. if (dirent->d_name[0] == '.')
  46. continue;
  47. entry = dir_entry_new(dirent->d_name);
  48. TAILQ_INSERT_TAIL(&list->entries, entry, entries);
  49. if (entry->sb.st_mtim.tv_sec > list->newest)
  50. list->newest = entry->sb.st_mtim.tv_sec;
  51. }
  52. closedir(dir);
  53. fchdir(cwd);
  54. return list;
  55. }
  56. return NULL;
  57. }
  58. struct dir_list *
  59. get_dir_entries(const char *_directory)
  60. {
  61. int fd = open(_directory, O_DIRECTORY | O_RDONLY);
  62. if (-1 == fd)
  63. err(1, NULL);
  64. struct dir_list *res = get_dir_entries_fd(fd);
  65. if (res)
  66. res->path = strdup(_directory);
  67. return res;
  68. }
  69. struct dir_list *
  70. get_dir_entries_at(int _fd, const char *_directory)
  71. {
  72. int fd = openat(_fd, _directory, O_DIRECTORY | O_RDONLY);
  73. if (-1 == fd)
  74. err(1, NULL);
  75. struct dir_list *res = get_dir_entries_fd(fd);
  76. if (res)
  77. res->path = strdup(_directory);
  78. return res;
  79. }
  80. struct dir_entry *
  81. dir_entry_new(const char *_filename)
  82. {
  83. struct dir_entry *entry = calloc(1, sizeof(struct dir_entry));
  84. if (entry == NULL)
  85. err(1, NULL);
  86. entry->filename = strdup(_filename);
  87. if (lstat(_filename, &entry->sb) != 0)
  88. err(1, "stat error on file %s", _filename);
  89. return entry;
  90. }
  91. void
  92. dir_entry_free(struct dir_entry *_entry)
  93. {
  94. if (_entry) {
  95. free(_entry->filename);
  96. free(_entry);
  97. }
  98. }
  99. bool
  100. dir_entry_exists(const char *_name, struct dir_list *_dir)
  101. {
  102. struct dir_entry *entry;
  103. TAILQ_FOREACH(entry, &_dir->entries, entries) {
  104. if (strcmp(entry->filename, _name) == 0)
  105. return true;
  106. }
  107. return false;
  108. }
  109. void
  110. dir_list_free(struct dir_list *_list)
  111. {
  112. if (_list) {
  113. struct dir_entry *entry;
  114. while ((entry = TAILQ_FIRST(&_list->entries))) {
  115. TAILQ_REMOVE(&_list->entries, entry, entries);
  116. dir_entry_free(entry);
  117. }
  118. free(_list->path);
  119. }
  120. }
  121. bool
  122. file_exists(const char *_filename)
  123. {
  124. struct stat sb;
  125. if (lstat(_filename, &sb) == -1)
  126. return false;
  127. return true;
  128. }
  129. bool
  130. dir_exists(const char *_dirname)
  131. {
  132. struct stat sb;
  133. if (lstat(_dirname, &sb) == -1)
  134. return false;
  135. return (sb.st_mode & S_IFDIR);
  136. }
  137. bool
  138. dir_exists_at(int _fd, const char *_dirname)
  139. {
  140. struct stat sb;
  141. if (fstatat(_fd, _dirname, &sb, 0) == -1)
  142. return false;
  143. return (sb.st_mode & S_IFDIR);
  144. }