1 /* Detect cycles in file tree walks.
3 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
5 Written by Jim Meyering.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "cycle-check.h"
24 /* Use these each of these to map a device/inode pair to an FTSENT. */
33 AD_compare (void const *x, void const *y)
35 struct Active_dir const *ax = x;
36 struct Active_dir const *ay = y;
37 return ax->ino == ay->ino
38 && ax->dev == ay->dev;
42 AD_hash (void const *x, size_t table_size)
44 struct Active_dir const *ax = x;
45 return (uintmax_t) ax->ino % table_size;
48 /* Set up the cycle-detection machinery. */
53 if (fts->fts_options & FTS_TIGHT_CYCLE_CHECK)
55 enum { HT_INITIAL_SIZE = 31 };
56 fts->fts_cycle.ht = hash_initialize (HT_INITIAL_SIZE, NULL, AD_hash,
58 if (! fts->fts_cycle.ht)
63 fts->fts_cycle.state = malloc (sizeof *fts->fts_cycle.state);
64 if (! fts->fts_cycle.state)
66 cycle_check_init (fts->fts_cycle.state);
72 /* Enter a directory during a file tree walk. */
75 enter_dir (FTS *fts, FTSENT *ent)
77 if (fts->fts_options & FTS_TIGHT_CYCLE_CHECK)
79 struct stat const *st = ent->fts_statp;
80 struct Active_dir *ad = malloc (sizeof *ad);
81 struct Active_dir *ad_from_table;
90 /* See if we've already encountered this directory.
91 This can happen when following symlinks as well as
92 with a corrupted directory hierarchy. */
93 ad_from_table = hash_insert (fts->fts_cycle.ht, ad);
95 if (ad_from_table != ad)
101 /* There was an entry with matching dev/inode already in the table.
102 Record the fact that we've found a cycle. */
103 ent->fts_cycle = ad_from_table->fts_ent;
104 ent->fts_info = FTS_DC;
109 if (cycle_check (fts->fts_cycle.state, ent->fts_statp))
111 /* FIXME: setting fts_cycle like this isn't proper.
112 To do what the documentation requires, we'd have to
113 go around the cycle again and find the right entry.
114 But no callers in coreutils use the fts_cycle member. */
115 ent->fts_cycle = ent;
116 ent->fts_info = FTS_DC;
123 /* Leave a directory during a file tree walk. */
126 leave_dir (FTS *fts, FTSENT *ent)
128 if (fts->fts_options & FTS_TIGHT_CYCLE_CHECK)
130 struct stat const *st = ent->fts_statp;
131 struct Active_dir obj;
133 obj.dev = st->st_dev;
134 obj.ino = st->st_ino;
135 found = hash_delete (fts->fts_cycle.ht, &obj);
142 /* Free any memory used for cycle detection. */
147 if (sp->fts_options & FTS_TIGHT_CYCLE_CHECK)
149 if (sp->fts_cycle.ht)
150 hash_free (sp->fts_cycle.ht);
153 free (sp->fts_cycle.state);