| 1 |
/* Traverse a file hierarchy. |
| 2 |
|
| 3 |
Copyright (C) 2004-2026 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This program is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation, either version 3 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* |
| 19 |
* Copyright (c) 1989, 1993 |
| 20 |
* The Regents of the University of California. All rights reserved. |
| 21 |
* |
| 22 |
* Redistribution and use in source and binary forms, with or without |
| 23 |
* modification, are permitted provided that the following conditions |
| 24 |
* are met: |
| 25 |
* 1. Redistributions of source code must retain the above copyright |
| 26 |
* notice, this list of conditions and the following disclaimer. |
| 27 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 28 |
* notice, this list of conditions and the following disclaimer in the |
| 29 |
* documentation and/or other materials provided with the distribution. |
| 30 |
* 4. Neither the name of the University nor the names of its contributors |
| 31 |
* may be used to endorse or promote products derived from this software |
| 32 |
* without specific prior written permission. |
| 33 |
* |
| 34 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
| 35 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 36 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 37 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 38 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 39 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 40 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 41 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 42 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 43 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 44 |
* SUCH DAMAGE. |
| 45 |
* |
| 46 |
* @(#)fts.h 8.3 (Berkeley) 8/14/94 |
| 47 |
*/ |
| 48 |
|
| 49 |
#ifndef _FTS_H |
| 50 |
# define _FTS_H 1 |
| 51 |
|
| 52 |
/* This file uses _GL_ATTRIBUTE_DEALLOC, _GL_ATTRIBUTE_NODISCARD. */ |
| 53 |
# if !_LIBC && !_GL_CONFIG_H_INCLUDED |
| 54 |
# error "Please include config.h first." |
| 55 |
# endif |
| 56 |
|
| 57 |
# ifdef _LIBC |
| 58 |
# include <features.h> |
| 59 |
# if __STDC_VERSION__ < 199901L |
| 60 |
# define __FLEXIBLE_ARRAY_MEMBER 1 |
| 61 |
# else |
| 62 |
# define __FLEXIBLE_ARRAY_MEMBER |
| 63 |
# endif |
| 64 |
# else |
| 65 |
# if @HAVE_SYS_CDEFS_H@ |
| 66 |
# include <sys/cdefs.h> |
| 67 |
# endif |
| 68 |
# define __FLEXIBLE_ARRAY_MEMBER FLEXIBLE_ARRAY_MEMBER |
| 69 |
# if defined __clang__ |
| 70 |
/* clang really only groks GNU C 4.2, regardless of its value of __GNUC__. */ |
| 71 |
# undef __GNUC_PREREQ |
| 72 |
# define __GNUC_PREREQ(maj, min) ((maj) < 4 + ((min) <= 2)) |
| 73 |
# endif |
| 74 |
# ifndef __GNUC_PREREQ |
| 75 |
# if defined __GNUC__ && defined __GNUC_MINOR__ |
| 76 |
# define __GNUC_PREREQ(maj, min) ((maj) < __GNUC__ + ((min) <= __GNUC_MINOR__)) |
| 77 |
# else |
| 78 |
# define __GNUC_PREREQ(maj, min) 0 |
| 79 |
# endif |
| 80 |
# endif |
| 81 |
# ifndef __THROW |
| 82 |
# if defined __cplusplus && (__GNUC_PREREQ (2,8) || __clang_major__ >= 4) |
| 83 |
# if __cplusplus >= 201103L |
| 84 |
# define __THROW noexcept (true) |
| 85 |
# else |
| 86 |
# define __THROW throw () |
| 87 |
# endif |
| 88 |
# else |
| 89 |
# define __THROW |
| 90 |
# endif |
| 91 |
# endif |
| 92 |
# ifndef __BEGIN_DECLS |
| 93 |
# ifdef __cplusplus |
| 94 |
# define __BEGIN_DECLS extern "C" { |
| 95 |
# else |
| 96 |
# define __BEGIN_DECLS /* nothing */ |
| 97 |
# endif |
| 98 |
# endif |
| 99 |
# ifndef __END_DECLS |
| 100 |
# ifdef __cplusplus |
| 101 |
# define __END_DECLS } |
| 102 |
# else |
| 103 |
# define __END_DECLS /* nothing */ |
| 104 |
# endif |
| 105 |
# endif |
| 106 |
# endif |
| 107 |
|
| 108 |
# include <stddef.h> |
| 109 |
# include <sys/types.h> |
| 110 |
# include <dirent.h> |
| 111 |
# include <sys/stat.h> |
| 112 |
# include "i-ring.h" |
| 113 |
|
| 114 |
typedef struct { |
| 115 |
struct _ftsent *fts_cur; /* current node */ |
| 116 |
struct _ftsent *fts_child; /* linked list of children */ |
| 117 |
struct _ftsent **fts_array; /* sort array */ |
| 118 |
dev_t fts_dev; /* starting device # */ |
| 119 |
char *fts_path; /* file name for this descent */ |
| 120 |
int fts_rfd; /* fd for root */ |
| 121 |
int fts_cwd_fd; /* the file descriptor on which the |
| 122 |
virtual cwd is open, or AT_FDCWD */ |
| 123 |
size_t fts_pathlen; /* sizeof(path) */ |
| 124 |
size_t fts_nitems; /* elements in the sort array */ |
| 125 |
int (*fts_compar) (struct _ftsent const **, struct _ftsent const **); |
| 126 |
/* compare fn */ |
| 127 |
|
| 128 |
# define FTS_COMFOLLOW 0x0001 /* follow command line symlinks */ |
| 129 |
# define FTS_LOGICAL 0x0002 /* logical walk */ |
| 130 |
# define FTS_NOCHDIR 0x0004 /* don't change directories */ |
| 131 |
# define FTS_NOSTAT 0x0008 /* don't get stat info */ |
| 132 |
# define FTS_PHYSICAL 0x0010 /* physical walk */ |
| 133 |
# define FTS_SEEDOT 0x0020 /* return dot and dot-dot */ |
| 134 |
# define FTS_XDEV 0x0040 /* don't cross devices */ |
| 135 |
# define FTS_WHITEOUT 0x0080 /* return whiteout information */ |
| 136 |
|
| 137 |
/* There are two ways to detect cycles. |
| 138 |
The lazy way (which works only with FTS_PHYSICAL), |
| 139 |
with which one may process a directory that is a |
| 140 |
part of the cycle several times before detecting the cycle. |
| 141 |
The "tight" way, whereby fts uses more memory (proportional |
| 142 |
to number of "active" directories, aka distance from root |
| 143 |
of current tree to current directory -- see active_dir_ht) |
| 144 |
to detect any cycle right away. For example, du must use |
| 145 |
this option to avoid counting disk space in a cycle multiple |
| 146 |
times, but chown -R need not. |
| 147 |
The default is to use the constant-memory lazy way, when possible |
| 148 |
(see below). |
| 149 |
|
| 150 |
However, with FTS_LOGICAL (when following symlinks, e.g., chown -L) |
| 151 |
using lazy cycle detection is inadequate. For example, traversing |
| 152 |
a directory containing a symbolic link to a peer directory, it is |
| 153 |
possible to encounter the same directory twice even though there |
| 154 |
is no cycle: |
| 155 |
dir |
| 156 |
... |
| 157 |
slink -> dir |
| 158 |
So, when FTS_LOGICAL is selected, we have to use a different |
| 159 |
mode of cycle detection: FTS_TIGHT_CYCLE_CHECK. */ |
| 160 |
# define FTS_TIGHT_CYCLE_CHECK 0x0100 |
| 161 |
|
| 162 |
/* Use this flag to enable semantics with which the parent |
| 163 |
application may be made both more efficient and more robust. |
| 164 |
Whereas the default is to visit each directory in a recursive |
| 165 |
traversal (via chdir), using this flag makes it so the initial |
| 166 |
working directory is never changed. Instead, these functions |
| 167 |
perform the traversal via a virtual working directory, maintained |
| 168 |
through the file descriptor member, fts_cwd_fd. */ |
| 169 |
# define FTS_CWDFD 0x0200 |
| 170 |
|
| 171 |
/* Historically, for each directory that fts initially encounters, it would |
| 172 |
open it, read all entries, and stat each entry, storing the results, and |
| 173 |
then it would process the first entry. But that behavior is bad for |
| 174 |
locality of reference, and also causes trouble with inode-simulating |
| 175 |
file systems like FAT, CIFS, FUSE-based ones, etc., when entries from |
| 176 |
their name/inode cache are flushed too early. |
| 177 |
Use this flag to make fts_open and fts_read defer the stat/lstat/fststat |
| 178 |
of each entry until it is actually processed. However, note that if you |
| 179 |
use this option and also specify a comparison function, that function may |
| 180 |
not examine any data via fts_statp. However, when fts_statp->st_mode is |
| 181 |
nonzero, the S_IFMT type bits are valid, with mapped dirent.d_type data. |
| 182 |
Of course, that happens only on file systems that provide useful |
| 183 |
dirent.d_type data. */ |
| 184 |
# define FTS_DEFER_STAT 0x0400 |
| 185 |
|
| 186 |
/* Use this flag to disable stripping of trailing slashes |
| 187 |
from input path names during fts_open initialization. */ |
| 188 |
# define FTS_VERBATIM 0x0800 |
| 189 |
# define FTS_MOUNT 0x1000 /* skip other devices */ |
| 190 |
|
| 191 |
# define FTS_OPTIONMASK 0x1fff /* valid user option mask */ |
| 192 |
|
| 193 |
# define FTS_NAMEONLY 0x2000 /* (private) child names only */ |
| 194 |
# define FTS_STOP 0x4000 /* (private) unrecoverable error */ |
| 195 |
int fts_options; /* fts_open options, global flags */ |
| 196 |
|
| 197 |
/* Map a directory's device number to a boolean. The boolean is |
| 198 |
true if for that file system (type determined by a single fstatfs |
| 199 |
call per FS) st_nlink can be used to calculate the number of |
| 200 |
sub-directory entries in a directory. |
| 201 |
Using this table is an optimization that permits us to look up |
| 202 |
file system type on a per-inode basis at the minimal cost of |
| 203 |
calling fstatfs only once per traversed device. */ |
| 204 |
struct hash_table *fts_leaf_optimization_works_ht; |
| 205 |
|
| 206 |
union { |
| 207 |
/* This data structure is used if FTS_TIGHT_CYCLE_CHECK is |
| 208 |
specified. It records the directories between a starting |
| 209 |
point and the current directory. I.e., a directory is |
| 210 |
recorded here IFF we have visited it once, but we have not |
| 211 |
yet completed processing of all its entries. Every time we |
| 212 |
visit a new directory, we add that directory to this set. |
| 213 |
When we finish with a directory (usually by visiting it a |
| 214 |
second time), we remove it from this set. Each entry in |
| 215 |
this data structure is a device/inode pair. This data |
| 216 |
structure is used to detect directory cycles efficiently and |
| 217 |
promptly even when the depth of a hierarchy is in the tens |
| 218 |
of thousands. */ |
| 219 |
struct hash_table *ht; |
| 220 |
|
| 221 |
/* FIXME: rename these two members to have the fts_ prefix */ |
| 222 |
/* This data structure uses a lazy cycle-detection algorithm, |
| 223 |
as done by rm via cycle-check.c. It's the default, |
| 224 |
but it's not appropriate for programs like du. */ |
| 225 |
struct cycle_check_state *state; |
| 226 |
} fts_cycle; |
| 227 |
|
| 228 |
/* A stack of the file descriptors corresponding to the |
| 229 |
most-recently traversed parent directories. |
| 230 |
Currently used only in FTS_CWDFD mode. */ |
| 231 |
I_ring fts_fd_ring; |
| 232 |
} FTS; |
| 233 |
|
| 234 |
typedef struct _ftsent { |
| 235 |
struct _ftsent *fts_cycle; /* cycle node */ |
| 236 |
struct _ftsent *fts_parent; /* parent directory */ |
| 237 |
struct _ftsent *fts_link; /* next file in directory */ |
| 238 |
DIR *fts_dirp; /* Dir pointer for any directory |
| 239 |
containing more entries than we |
| 240 |
read at one time. */ |
| 241 |
long fts_number; /* local numeric value */ |
| 242 |
void *fts_pointer; /* local address value */ |
| 243 |
char *fts_accpath; /* access file name */ |
| 244 |
char *fts_path; /* root name; == fts_fts->fts_path */ |
| 245 |
int fts_errno; /* errno for this node */ |
| 246 |
int fts_symfd; /* fd for symlink */ |
| 247 |
size_t fts_pathlen; /* strlen(fts_path) */ |
| 248 |
|
| 249 |
FTS *fts_fts; /* the file hierarchy itself */ |
| 250 |
|
| 251 |
# define FTS_ROOTPARENTLEVEL (-1) |
| 252 |
# define FTS_ROOTLEVEL 0 |
| 253 |
ptrdiff_t fts_level; /* depth (-1 to N) */ |
| 254 |
|
| 255 |
size_t fts_namelen; /* strlen(fts_name) */ |
| 256 |
|
| 257 |
# define FTS_D 1 /* preorder directory */ |
| 258 |
# define FTS_DC 2 /* directory that causes cycles */ |
| 259 |
# define FTS_DEFAULT 3 /* none of the above */ |
| 260 |
# define FTS_DNR 4 /* unreadable directory */ |
| 261 |
# define FTS_DOT 5 /* dot or dot-dot */ |
| 262 |
# define FTS_DP 6 /* postorder directory */ |
| 263 |
# define FTS_ERR 7 /* error; errno is set */ |
| 264 |
# define FTS_F 8 /* regular file */ |
| 265 |
# define FTS_INIT 9 /* initialized only */ |
| 266 |
# define FTS_NS 10 /* stat(2) failed */ |
| 267 |
# define FTS_NSOK 11 /* no stat(2) requested */ |
| 268 |
# define FTS_SL 12 /* symbolic link */ |
| 269 |
# define FTS_SLNONE 13 /* symbolic link without target */ |
| 270 |
# define FTS_W 14 /* whiteout object */ |
| 271 |
unsigned short int fts_info; /* user flags for FTSENT structure */ |
| 272 |
|
| 273 |
# define FTS_DONTCHDIR 0x01 /* don't chdir .. to the parent */ |
| 274 |
# define FTS_SYMFOLLOW 0x02 /* followed a symlink to get here */ |
| 275 |
unsigned short int fts_flags; /* private flags for FTSENT structure */ |
| 276 |
|
| 277 |
# define FTS_AGAIN 1 /* read node again */ |
| 278 |
# define FTS_FOLLOW 2 /* follow symbolic link */ |
| 279 |
# define FTS_NOINSTR 3 /* no instructions */ |
| 280 |
# define FTS_SKIP 4 /* discard node */ |
| 281 |
unsigned short int fts_instr; /* fts_set() instructions */ |
| 282 |
|
| 283 |
struct stat fts_statp[1]; /* stat(2) information */ |
| 284 |
char fts_name[__FLEXIBLE_ARRAY_MEMBER]; /* file name */ |
| 285 |
} FTSENT; |
| 286 |
|
| 287 |
__BEGIN_DECLS |
| 288 |
|
| 289 |
_GL_ATTRIBUTE_NODISCARD |
| 290 |
FTSENT *fts_children (FTS *, int) __THROW; |
| 291 |
|
| 292 |
_GL_ATTRIBUTE_NODISCARD |
| 293 |
int fts_close (FTS *) __THROW; |
| 294 |
|
| 295 |
_GL_ATTRIBUTE_NODISCARD |
| 296 |
FTS *fts_open (char * const *, int, |
| 297 |
int (*)(const FTSENT **, const FTSENT **)) |
| 298 |
_GL_ATTRIBUTE_DEALLOC (fts_close, 1) __THROW; |
| 299 |
|
| 300 |
_GL_ATTRIBUTE_NODISCARD |
| 301 |
FTSENT *fts_read (FTS *) __THROW; |
| 302 |
|
| 303 |
int fts_set (FTS *, FTSENT *, int) __THROW; |
| 304 |
|
| 305 |
#if GNULIB_FTS_DEBUG |
| 306 |
extern bool fts_debug; |
| 307 |
void fts_cross_check (FTS const *); |
| 308 |
#endif |
| 309 |
__END_DECLS |
| 310 |
|
| 311 |
#endif /* fts.h */ |