tree.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*-
  2. * Copyright 2006-2008 Colin Percival
  3. * All rights reserved.
  4. *
  5. * Portions of the file below are covered by the following license:
  6. *
  7. * Copyright (c) 2003-2007 Tim Kientzle
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * $FreeBSD: src/usr.bin/tar/tree.h,v 1.4 2008/11/27 05:49:52 kientzle Exp $
  31. */
  32. /*-
  33. * A set of routines for traversing directory trees.
  34. * Similar in concept to the fts library, but with a few
  35. * important differences:
  36. * * Uses less memory. In particular, fts stores an entire directory
  37. * in memory at a time. This package only keeps enough subdirectory
  38. * information in memory to track the traversal. Information
  39. * about non-directories is discarded as soon as possible.
  40. * * Supports very deep logical traversals. The fts package
  41. * uses "non-chdir" approach for logical traversals. This
  42. * package does use a chdir approach for logical traversals
  43. * and can therefore handle pathnames much longer than PATH_MAX.
  44. * * Supports deep physical traversals "out of the box."
  45. * Due to the memory optimizations above, there's no need to
  46. * limit dir names to 32k.
  47. */
  48. #include <sys/stat.h>
  49. #include <stdio.h>
  50. struct tree;
  51. /* Initiate/terminate a tree traversal. */
  52. struct tree *tree_open(const char * /* pathname */);
  53. int tree_close(struct tree *);
  54. /*
  55. * tree_next() returns Zero if there is no next entry, non-zero if
  56. * there is. Note that directories are potentially visited three
  57. * times. Directories are always visited first as part of enumerating
  58. * their parent. If tree_descend() is invoked at that time, the
  59. * directory is added to a work list and will subsequently be visited
  60. * two more times: once just after descending into the directory and
  61. * again just after ascending back to the parent.
  62. *
  63. * TREE_ERROR_DIR is returned if the descent failed (because the
  64. * directory couldn't be opened, for instance). This is returned
  65. * instead of TREE_PREVISIT/TREE_POSTVISIT. TREE_ERROR_DIR is not a
  66. * fatal error, but it does imply that the relevant subtree won't be
  67. * visited. TREE_ERROR_FATAL is returned for an error that left the
  68. * traversal completely hosed. Right now, this is only returned for
  69. * chdir() failures during ascent, or readdir() failures when looking
  70. * for the next entry.
  71. */
  72. #define TREE_REGULAR 1
  73. #define TREE_POSTDESCENT 2
  74. #define TREE_POSTASCENT 3
  75. #define TREE_ERROR_DIR -1
  76. #define TREE_ERROR_FATAL -2
  77. int tree_next(struct tree *);
  78. /* Errno value associated with the last traversal error. */
  79. int tree_errno(struct tree *);
  80. /*
  81. * Request that current entry be visited. If you invoke it on every
  82. * directory, you'll get a physical traversal. This is ignored if the
  83. * current entry isn't a directory or a link to a directory. So, if
  84. * you invoke this on every returned path, you'll get a full logical
  85. * traversal.
  86. */
  87. void tree_descend(struct tree *);
  88. /*
  89. * Return information about the current entry.
  90. */
  91. /* Current depth in the traversal. */
  92. int tree_current_depth(struct tree *);
  93. /*
  94. * The current full pathname, length of the full pathname,
  95. * and a name that can be used to access the file.
  96. * Because tree does use chdir extensively, the access path is
  97. * almost never the same as the full current path.
  98. */
  99. const char *tree_current_path(struct tree *);
  100. size_t tree_current_pathlen(struct tree *);
  101. const char *tree_current_access_path(struct tree *);
  102. /*
  103. * What you would get by calling realpath(3) on the path returned by
  104. * tree_current_access_path(t). In most cases this avoids needing to
  105. * call realpath(3).
  106. */
  107. const char * tree_current_realpath(struct tree *t);
  108. /*
  109. * Request the lstat() or stat() data for the current path. Since the
  110. * tree package needs to do some of this anyway, and caches the
  111. * results, you should take advantage of it here if you need it rather
  112. * than make a redundant stat() or lstat() call of your own.
  113. */
  114. const struct stat *tree_current_stat(struct tree *);
  115. const struct stat *tree_current_lstat(struct tree *);
  116. /* The following functions use tricks to avoid a certain number of
  117. * stat()/lstat() calls. */
  118. /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
  119. int tree_current_is_physical_dir(struct tree *);
  120. /* "is_physical_link" is equivalent to S_ISLNK(tree_current_lstat()->st_mode) */
  121. int tree_current_is_physical_link(struct tree *);
  122. /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
  123. int tree_current_is_dir(struct tree *);
  124. /* For testing/debugging: Dump the internal status to the given filehandle. */
  125. void tree_dump(struct tree *, FILE *);