5c5e1c2cbec4477cfeecc8c9c3508d8487cff0c8
[gnulib.git] / lib / fts_.h
1 /* Traverse a file hierarchy.
2
3    Copyright (C) 2004, 2005 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 # else
56 #  undef __THROW
57 #  define __THROW
58 #  undef __BEGIN_DECLS
59 #  define __BEGIN_DECLS
60 #  undef __END_DECLS
61 #  define __END_DECLS
62 # endif
63
64 # include <stddef.h>
65 # include <sys/types.h>
66 # include <sys/stat.h>
67
68 typedef struct {
69         struct _ftsent *fts_cur;        /* current node */
70         struct _ftsent *fts_child;      /* linked list of children */
71         struct _ftsent **fts_array;     /* sort array */
72         dev_t fts_dev;                  /* starting device # */
73         char *fts_path;                 /* path for this descent */
74         int fts_rfd;                    /* fd for root */
75         size_t fts_pathlen;             /* sizeof(path) */
76         size_t fts_nitems;                      /* elements in the sort array */
77         int (*fts_compar) (struct _ftsent const **, struct _ftsent const **);
78                                         /* compare fn */
79
80 # define FTS_COMFOLLOW  0x0001          /* follow command line symlinks */
81 # define FTS_LOGICAL    0x0002          /* logical walk */
82 # define FTS_NOCHDIR    0x0004          /* don't change directories */
83 # define FTS_NOSTAT     0x0008          /* don't get stat info */
84 # define FTS_PHYSICAL   0x0010          /* physical walk */
85 # define FTS_SEEDOT     0x0020          /* return dot and dot-dot */
86 # define FTS_XDEV       0x0040          /* don't cross devices */
87 # define FTS_WHITEOUT   0x0080          /* return whiteout information */
88
89   /* There are two ways to detect cycles.
90      The lazy way, with which one may process a directory that is a
91      part of the cycle several times before detecting the cycle.
92      The `tight' way, whereby fts uses more memory (proportional
93      to number of `active' directories, aka distance from root
94      of current tree to current directory -- see active_dir_ht)
95      to detect any cycle right away.  For example, du must use
96      this option to avoid counting disk space in a cycle multiple
97      times, but chown -R need not.
98      The default is to use the constant-memory lazy way. */
99 # define FTS_TIGHT_CYCLE_CHECK  0x0100
100
101 # define FTS_OPTIONMASK 0x01ff          /* valid user option mask */
102
103 # define FTS_NAMEONLY   0x1000          /* (private) child names only */
104 # define FTS_STOP       0x2000          /* (private) unrecoverable error */
105         int fts_options;                /* fts_open options, global flags */
106
107         /* This data structure records the directories between a starting
108            point and the current directory.  I.e., a directory is recorded
109            here IFF we have visited it once, but we have not yet completed
110            processing of all its entries.  Every time we visit a new directory,
111            we add that directory to this set.  When we finish with a directory
112            (usually by visiting it a second time), we remove it from this
113            set.  Each entry in this data structure is a device/inode pair.
114            This data structure is used to detect directory cycles efficiently
115            and promptly even when the depth of a hierarchy is in the tens
116            of thousands.  Lazy checking, as done by GNU rm via cycle-check.c,
117            wouldn't be appropriate for du.  */
118         struct hash_table *active_dir_ht;
119         struct cycle_check_state *cycle_state;
120 } FTS;
121
122 typedef struct _ftsent {
123         struct _ftsent *fts_cycle;      /* cycle node */
124         struct _ftsent *fts_parent;     /* parent directory */
125         struct _ftsent *fts_link;       /* next file in directory */
126         long fts_number;                /* local numeric value */
127         void *fts_pointer;              /* local address value */
128         char *fts_accpath;              /* access path */
129         char *fts_path;                 /* root path; == fts_fts->fts_path */
130         int fts_errno;                  /* errno for this node */
131         int fts_symfd;                  /* fd for symlink */
132         size_t fts_pathlen;             /* strlen(fts_path) */
133
134         FTS *fts_fts;                   /* the file hierarchy itself */
135
136 # define FTS_ROOTPARENTLEVEL    (-1)
137 # define FTS_ROOTLEVEL           0
138         ptrdiff_t fts_level;            /* depth (-1 to N) */
139
140         size_t fts_namelen;             /* strlen(fts_name) */
141
142 # define FTS_D           1              /* preorder directory */
143 # define FTS_DC          2              /* directory that causes cycles */
144 # define FTS_DEFAULT     3              /* none of the above */
145 # define FTS_DNR         4              /* unreadable directory */
146 # define FTS_DOT         5              /* dot or dot-dot */
147 # define FTS_DP          6              /* postorder directory */
148 # define FTS_ERR         7              /* error; errno is set */
149 # define FTS_F           8              /* regular file */
150 # define FTS_INIT        9              /* initialized only */
151 # define FTS_NS         10              /* stat(2) failed */
152 # define FTS_NSOK       11              /* no stat(2) requested */
153 # define FTS_SL         12              /* symbolic link */
154 # define FTS_SLNONE     13              /* symbolic link without target */
155 # define FTS_W          14              /* whiteout object */
156         unsigned short int fts_info;    /* user flags for FTSENT structure */
157
158 # define FTS_DONTCHDIR   0x01           /* don't chdir .. to the parent */
159 # define FTS_SYMFOLLOW   0x02           /* followed a symlink to get here */
160         unsigned short int fts_flags;   /* private flags for FTSENT structure */
161
162 # define FTS_AGAIN       1              /* read node again */
163 # define FTS_FOLLOW      2              /* follow symbolic link */
164 # define FTS_NOINSTR     3              /* no instructions */
165 # define FTS_SKIP        4              /* discard node */
166         unsigned short int fts_instr;   /* fts_set() instructions */
167
168         struct stat fts_statp[1];       /* stat(2) information */
169         char fts_name[1];               /* file name */
170 } FTSENT;
171
172 __BEGIN_DECLS
173 FTSENT  *fts_children (FTS *, int) __THROW;
174 int      fts_close (FTS *) __THROW;
175 FTS     *fts_open (char * const *, int,
176                    int (*)(const FTSENT **, const FTSENT **)) __THROW;
177 FTSENT  *fts_read (FTS *) __THROW;
178 int      fts_set (FTS *, FTSENT *, int) __THROW;
179 __END_DECLS
180
181 #endif /* fts.h */