Make fts (in FTS_CWDFD mode) more efficient by caching a few open
[gnulib.git] / lib / fts_.h
1 /* Traverse a file hierarchy.
2
3    Copyright (C) 2004, 2005, 2006 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /*
20  * Copyright (c) 1989, 1993
21  *      The Regents of the University of California.  All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 4. Neither the name of the University nor the names of its contributors
32  *    may be used to endorse or promote products derived from this software
33  *    without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  *
47  *      @(#)fts.h       8.3 (Berkeley) 8/14/94
48  */
49
50 #ifndef _FTS_H
51 # define _FTS_H 1
52
53 # ifdef _LIBC
54 #  include <features.h>
55 #  define _LGPL_PACKAGE 1
56 # else
57 #  undef __THROW
58 #  define __THROW
59 #  undef __BEGIN_DECLS
60 #  define __BEGIN_DECLS
61 #  undef __END_DECLS
62 #  define __END_DECLS
63 # endif
64
65 # include <stddef.h>
66 # include <sys/types.h>
67 # include <sys/stat.h>
68 # include "i-ring.h"
69
70 typedef struct {
71         struct _ftsent *fts_cur;        /* current node */
72         struct _ftsent *fts_child;      /* linked list of children */
73         struct _ftsent **fts_array;     /* sort array */
74         dev_t fts_dev;                  /* starting device # */
75         char *fts_path;                 /* file name for this descent */
76         int fts_rfd;                    /* fd for root */
77         int fts_cwd_fd;                 /* the file descriptor on which the
78                                            virtual cwd is open, or AT_FDCWD */
79         size_t fts_pathlen;             /* sizeof(path) */
80         size_t fts_nitems;              /* elements in the sort array */
81         int (*fts_compar) (struct _ftsent const **, struct _ftsent const **);
82                                         /* compare fn */
83
84 # define FTS_COMFOLLOW  0x0001          /* follow command line symlinks */
85 # define FTS_LOGICAL    0x0002          /* logical walk */
86 # define FTS_NOCHDIR    0x0004          /* don't change directories */
87 # define FTS_NOSTAT     0x0008          /* don't get stat info */
88 # define FTS_PHYSICAL   0x0010          /* physical walk */
89 # define FTS_SEEDOT     0x0020          /* return dot and dot-dot */
90 # define FTS_XDEV       0x0040          /* don't cross devices */
91 # define FTS_WHITEOUT   0x0080          /* return whiteout information */
92
93   /* There are two ways to detect cycles.
94      The lazy way (which works only with FTS_PHYSICAL),
95      with which one may process a directory that is a
96      part of the cycle several times before detecting the cycle.
97      The `tight' way, whereby fts uses more memory (proportional
98      to number of `active' directories, aka distance from root
99      of current tree to current directory -- see active_dir_ht)
100      to detect any cycle right away.  For example, du must use
101      this option to avoid counting disk space in a cycle multiple
102      times, but chown -R need not.
103      The default is to use the constant-memory lazy way, when possible
104      (see below).
105
106      However, with FTS_LOGICAL (when following symlinks, e.g., chown -L)
107      using lazy cycle detection is inadequate.  For example, traversing
108      a directory containing a symbolic link to a peer directory, it is
109      possible to encounter the same directory twice even though there
110      is no cycle:
111      dir
112      ...
113      slink -> dir
114      So, when FTS_LOGICAL is selected, we have to use a different
115      mode of cycle detection: FTS_TIGHT_CYCLE_CHECK.  */
116 # define FTS_TIGHT_CYCLE_CHECK  0x0100
117
118   /* Use this flag to enable semantics with which the parent
119      application may be made both more efficient and more robust.
120      Whereas the default is to visit each directory in a recursive
121      traversal (via chdir), using this flag makes it so the initial
122      working directory is never changed.  Instead, these functions
123      perform the traversal via a virtual working directory, maintained
124      through the file descriptor member, fts_cwd_fd.  */
125 # define FTS_CWDFD              0x0200
126
127   /* Historically, for each directory that fts initially encounters, it would
128      open it, read all entries, and stat each entry, storing the results, and
129      then it would process the first entry.  But that behavior is bad for
130      locality of reference, and also causes trouble with inode-simulating
131      file systems like FAT, CIFS, FUSE-based ones, etc., when entries from
132      their name/inode cache are flushed too early.
133      Use this flag to make fts_open and fts_read defer the stat/lstat/fststat
134      of each entry until it actually processed.  However, note that if you use
135      this option and also specify a comparison function, that function may not
136      examine any data via fts_statp.  */
137 # define FTS_DEFER_STAT         0x0400
138
139 # define FTS_OPTIONMASK 0x07ff          /* valid user option mask */
140
141 # define FTS_NAMEONLY   0x1000          /* (private) child names only */
142 # define FTS_STOP       0x2000          /* (private) unrecoverable error */
143         int fts_options;                /* fts_open options, global flags */
144
145 # if !_LGPL_PACKAGE
146         union {
147                 /* This data structure is used if FTS_TIGHT_CYCLE_CHECK is
148                    specified.  It records the directories between a starting
149                    point and the current directory.  I.e., a directory is
150                    recorded here IFF we have visited it once, but we have not
151                    yet completed processing of all its entries.  Every time we
152                    visit a new directory, we add that directory to this set.
153                    When we finish with a directory (usually by visiting it a
154                    second time), we remove it from this set.  Each entry in
155                    this data structure is a device/inode pair.  This data
156                    structure is used to detect directory cycles efficiently and
157                    promptly even when the depth of a hierarchy is in the tens
158                    of thousands.  */
159                 struct hash_table *ht;
160
161                 /* FIXME: rename these two members to have the fts_ prefix */
162                 /* This data structure uses a lazy cycle-detection algorithm,
163                    as done by rm via cycle-check.c.  It's the default,
164                    but it's not appropriate for programs like du.  */
165                 struct cycle_check_state *state;
166         } fts_cycle;
167
168 # endif
169         /* A stack of the file descriptors corresponding to the
170            most-recently traversed parent directories.
171            Currently used only in FTS_CWDFD mode.  */
172         I_ring fts_fd_ring;
173 } FTS;
174
175 typedef struct _ftsent {
176         struct _ftsent *fts_cycle;      /* cycle node */
177         struct _ftsent *fts_parent;     /* parent directory */
178         struct _ftsent *fts_link;       /* next file in directory */
179         long fts_number;                /* local numeric value */
180         void *fts_pointer;              /* local address value */
181         char *fts_accpath;              /* access file name */
182         char *fts_path;                 /* root name; == fts_fts->fts_path */
183         int fts_errno;                  /* errno for this node */
184         int fts_symfd;                  /* fd for symlink */
185         size_t fts_pathlen;             /* strlen(fts_path) */
186
187         FTS *fts_fts;                   /* the file hierarchy itself */
188
189 # define FTS_ROOTPARENTLEVEL    (-1)
190 # define FTS_ROOTLEVEL           0
191         ptrdiff_t fts_level;            /* depth (-1 to N) */
192
193         size_t fts_namelen;             /* strlen(fts_name) */
194
195 # define FTS_D           1              /* preorder directory */
196 # define FTS_DC          2              /* directory that causes cycles */
197 # define FTS_DEFAULT     3              /* none of the above */
198 # define FTS_DNR         4              /* unreadable directory */
199 # define FTS_DOT         5              /* dot or dot-dot */
200 # define FTS_DP          6              /* postorder directory */
201 # define FTS_ERR         7              /* error; errno is set */
202 # define FTS_F           8              /* regular file */
203 # define FTS_INIT        9              /* initialized only */
204 # define FTS_NS         10              /* stat(2) failed */
205 # define FTS_NSOK       11              /* no stat(2) requested */
206 # define FTS_SL         12              /* symbolic link */
207 # define FTS_SLNONE     13              /* symbolic link without target */
208 # define FTS_W          14              /* whiteout object */
209         unsigned short int fts_info;    /* user flags for FTSENT structure */
210
211 # define FTS_DONTCHDIR   0x01           /* don't chdir .. to the parent */
212 # define FTS_SYMFOLLOW   0x02           /* followed a symlink to get here */
213         unsigned short int fts_flags;   /* private flags for FTSENT structure */
214
215 # define FTS_AGAIN       1              /* read node again */
216 # define FTS_FOLLOW      2              /* follow symbolic link */
217 # define FTS_NOINSTR     3              /* no instructions */
218 # define FTS_SKIP        4              /* discard node */
219         unsigned short int fts_instr;   /* fts_set() instructions */
220
221         struct stat fts_statp[1];       /* stat(2) information */
222         char fts_name[1];               /* file name */
223 } FTSENT;
224
225 __BEGIN_DECLS
226 FTSENT  *fts_children (FTS *, int) __THROW;
227 int      fts_close (FTS *) __THROW;
228 FTS     *fts_open (char * const *, int,
229                    int (*)(const FTSENT **, const FTSENT **)) __THROW;
230 FTSENT  *fts_read (FTS *) __THROW;
231 int      fts_set (FTS *, FTSENT *, int) __THROW;
232 __END_DECLS
233
234 #endif /* fts.h */