dirtree.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* dirtree.c - Functions for dealing with directory trees.
  2. *
  3. * Copyright 2007 Rob Landley <rob@landley.net>
  4. */
  5. #include "toys.h"
  6. int isdotdot(char *name)
  7. {
  8. if (name[0]=='.' && (!name[1] || (name[1]=='.' && !name[2]))) return 1;
  9. return 0;
  10. }
  11. // Default callback, filters out "." and ".." except at top level.
  12. int dirtree_notdotdot(struct dirtree *catch)
  13. {
  14. // Should we skip "." and ".."?
  15. return (!catch->parent||!isdotdot(catch->name))
  16. *(DIRTREE_SAVE|DIRTREE_RECURSE);
  17. }
  18. // Create a dirtree node from a path, with stat and symlink info.
  19. // (This doesn't open directory filehandles yet so as not to exhaust the
  20. // filehandle space on large trees, dirtree_handle_callback() does that.)
  21. struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
  22. {
  23. struct dirtree *dt = 0;
  24. struct stat st;
  25. int len = 0, linklen = 0, statless = 0;
  26. if (name) {
  27. // open code fd = because haven't got node to call dirtree_parentfd() on yet
  28. int fd = parent ? parent->dirfd : AT_FDCWD,
  29. sym = AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW);
  30. // stat dangling symlinks
  31. if (fstatat(fd, name, &st, sym)) {
  32. // If we got ENOENT without NOFOLLOW, try again with NOFOLLOW.
  33. if (errno!=ENOENT || sym || fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW)) {
  34. if (flags&DIRTREE_STATLESS) statless++;
  35. else goto error;
  36. }
  37. }
  38. if (!statless && S_ISLNK(st.st_mode)) {
  39. if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
  40. libbuf[linklen++]=0;
  41. }
  42. len = strlen(name);
  43. }
  44. // Allocate/populate return structure
  45. memset(dt = xmalloc((len = sizeof(struct dirtree)+len+1)+linklen), 0,
  46. statless ? sizeof(struct dirtree) : offsetof(struct dirtree, st));
  47. dt->parent = parent;
  48. dt->again = statless ? 2 : 0;
  49. if (!statless) memcpy(&dt->st, &st, sizeof(struct stat));
  50. if (name) strcpy(dt->name, name);
  51. else *dt->name = 0, dt->st.st_mode = S_IFDIR;
  52. if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
  53. return dt;
  54. error:
  55. if (!(flags&DIRTREE_SHUTUP) && !isdotdot(name)) {
  56. char *path = parent ? dirtree_path(parent, 0) : "";
  57. perror_msg("%s%s%s", path, parent ? "/" : "", name);
  58. if (parent) free(path);
  59. }
  60. if (parent) parent->symlink = (char *)1;
  61. free(dt);
  62. return 0;
  63. }
  64. // Return path to this node.
  65. // Initial call can pass in NULL to plen, or point to an int initialized to 0
  66. // to return the length of the path, or a value greater than 0 to allocate
  67. // extra space if you want to append your own text to the string.
  68. char *dirtree_path(struct dirtree *node, int *plen)
  69. {
  70. struct dirtree *nn;
  71. char *path;
  72. int ii, ll, len;
  73. ll = len = plen ? *plen : 0;
  74. if (!node->parent)
  75. return strcpy(xmalloc(strlen(node->name)+ll+1), node->name);
  76. for (nn = node; nn; nn = nn->parent)
  77. if ((ii = strlen(nn->name))) len += ii+1-(nn->name[ii-1]=='/');
  78. if (plen) *plen = len;
  79. path = xmalloc(len)+len-ll;
  80. for (nn = node; nn; nn = nn->parent) if ((len = strlen(nn->name))) {
  81. *--path = '/'*(nn != node);
  82. if (nn->name[len-1]=='/') len--;
  83. memcpy(path -= len, nn->name, len);
  84. }
  85. return path;
  86. }
  87. int dirtree_parentfd(struct dirtree *node)
  88. {
  89. return node->parent ? node->parent->dirfd : AT_FDCWD;
  90. }
  91. // Handle callback for a node in the tree. Returns saved node(s) if
  92. // callback returns DIRTREE_SAVE, otherwise frees consumed nodes and
  93. // returns NULL. If !callback return top node unchanged.
  94. // If !new return DIRTREE_ABORTVAL
  95. static struct dirtree *dirtree_handle_callback(struct dirtree *new,
  96. int (*callback)(struct dirtree *node))
  97. {
  98. int flags;
  99. if (!new) return DIRTREE_ABORTVAL;
  100. if (!callback) return new;
  101. flags = callback(new);
  102. if (S_ISDIR(new->st.st_mode) && (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)))
  103. flags = dirtree_recurse(new, callback, !*new->name ? AT_FDCWD :
  104. openat(dirtree_parentfd(new), new->name, O_CLOEXEC), flags);
  105. // Free node that didn't request saving and has no saved children.
  106. if (!new->child && !(flags & DIRTREE_SAVE)) {
  107. free(new);
  108. new = 0;
  109. }
  110. return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
  111. }
  112. // Recursively read/process children of directory node, filtering through
  113. // callback(). Uses and closes supplied ->dirfd.
  114. int dirtree_recurse(struct dirtree *node,
  115. int (*callback)(struct dirtree *node), int dirfd, int flags)
  116. {
  117. struct dirtree *new, **ddt = &(node->child);
  118. struct dirent *entry;
  119. DIR *dir = 0;
  120. // Why doesn't fdopendir() support AT_FDCWD?
  121. if (AT_FDCWD == (node->dirfd = dirfd)) dir = opendir(".");
  122. else if (node->dirfd != -1) dir = fdopendir(node->dirfd);
  123. if (!dir) {
  124. if (!(flags & DIRTREE_SHUTUP)) {
  125. char *path = dirtree_path(node, 0);
  126. perror_msg_raw(path);
  127. free(path);
  128. }
  129. close(node->dirfd);
  130. return flags;
  131. }
  132. // according to the fddir() man page, the filehandle in the DIR * can still
  133. // be externally used by things that don't lseek() it.
  134. while ((entry = readdir(dir))) {
  135. if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue;
  136. if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
  137. if (!new->st.st_blksize && !new->st.st_mode)
  138. new->st.st_mode = entry->d_type<<12;
  139. new = dirtree_handle_callback(new, callback);
  140. if (new == DIRTREE_ABORTVAL) break;
  141. if (new) {
  142. *ddt = new;
  143. ddt = &((*ddt)->next);
  144. }
  145. }
  146. if (flags & DIRTREE_COMEAGAIN) {
  147. node->again |= 1;
  148. flags = callback(node);
  149. }
  150. // This closes filehandle as well, so note it
  151. closedir(dir);
  152. node->dirfd = -1;
  153. return flags;
  154. }
  155. // Create dirtree from path, using callback to filter nodes. If !callback
  156. // return just the top node. Use dirtree_notdotdot callback to allocate a
  157. // tree of struct dirtree nodes and return pointer to root node for later
  158. // processing.
  159. // Returns DIRTREE_ABORTVAL if path didn't exist (use DIRTREE_SHUTUP to handle
  160. // error message yourself).
  161. struct dirtree *dirtree_flagread(char *path, int flags,
  162. int (*callback)(struct dirtree *node))
  163. {
  164. return dirtree_handle_callback(dirtree_add_node(0, path, flags), callback);
  165. }
  166. // Common case
  167. struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
  168. {
  169. return dirtree_flagread(path, 0, callback);
  170. }