1*86d7f5d3SJohn Marino /* help detect directory cycles efficiently
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino
5*86d7f5d3SJohn Marino This program is free software; you can redistribute it and/or modify
6*86d7f5d3SJohn Marino it under the terms of the GNU General Public License as published by
7*86d7f5d3SJohn Marino the Free Software Foundation; either version 2, or (at your option)
8*86d7f5d3SJohn Marino any later version.
9*86d7f5d3SJohn Marino
10*86d7f5d3SJohn Marino This program is distributed in the hope that it will be useful,
11*86d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
12*86d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*86d7f5d3SJohn Marino GNU General Public License for more details.
14*86d7f5d3SJohn Marino
15*86d7f5d3SJohn Marino You should have received a copy of the GNU General Public License
16*86d7f5d3SJohn Marino along with this program; see the file COPYING.
17*86d7f5d3SJohn Marino If not, write to the Free Software Foundation,
18*86d7f5d3SJohn Marino 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19*86d7f5d3SJohn Marino
20*86d7f5d3SJohn Marino /* Written by Jim Meyering */
21*86d7f5d3SJohn Marino
22*86d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
23*86d7f5d3SJohn Marino # include <config.h>
24*86d7f5d3SJohn Marino #endif
25*86d7f5d3SJohn Marino
26*86d7f5d3SJohn Marino #include <sys/types.h>
27*86d7f5d3SJohn Marino #include <sys/stat.h>
28*86d7f5d3SJohn Marino #include <stdio.h>
29*86d7f5d3SJohn Marino #include <assert.h>
30*86d7f5d3SJohn Marino #include <stdlib.h>
31*86d7f5d3SJohn Marino
32*86d7f5d3SJohn Marino #include <stdbool.h>
33*86d7f5d3SJohn Marino
34*86d7f5d3SJohn Marino #include "cycle-check.h"
35*86d7f5d3SJohn Marino
36*86d7f5d3SJohn Marino #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
37*86d7f5d3SJohn Marino ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
38*86d7f5d3SJohn Marino && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
39*86d7f5d3SJohn Marino
40*86d7f5d3SJohn Marino #define CC_MAGIC 9827862
41*86d7f5d3SJohn Marino
42*86d7f5d3SJohn Marino /* Return true if I is a power of 2, or is zero. */
43*86d7f5d3SJohn Marino
44*86d7f5d3SJohn Marino static inline bool
is_zero_or_power_of_two(uintmax_t i)45*86d7f5d3SJohn Marino is_zero_or_power_of_two (uintmax_t i)
46*86d7f5d3SJohn Marino {
47*86d7f5d3SJohn Marino return (i & (i - 1)) == 0;
48*86d7f5d3SJohn Marino }
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino void
cycle_check_init(struct cycle_check_state * state)51*86d7f5d3SJohn Marino cycle_check_init (struct cycle_check_state *state)
52*86d7f5d3SJohn Marino {
53*86d7f5d3SJohn Marino state->chdir_counter = 0;
54*86d7f5d3SJohn Marino state->magic = CC_MAGIC;
55*86d7f5d3SJohn Marino }
56*86d7f5d3SJohn Marino
57*86d7f5d3SJohn Marino /* In traversing a directory hierarchy, call this function once for each
58*86d7f5d3SJohn Marino descending chdir call, with SB corresponding to the chdir operand.
59*86d7f5d3SJohn Marino If SB corresponds to a directory that has already been seen,
60*86d7f5d3SJohn Marino return true to indicate that there is a directory cycle.
61*86d7f5d3SJohn Marino Note that this is done `lazily', which means that some of
62*86d7f5d3SJohn Marino the directories in the cycle may be processed twice before
63*86d7f5d3SJohn Marino the cycle is detected. */
64*86d7f5d3SJohn Marino
65*86d7f5d3SJohn Marino bool
cycle_check(struct cycle_check_state * state,struct stat const * sb)66*86d7f5d3SJohn Marino cycle_check (struct cycle_check_state *state, struct stat const *sb)
67*86d7f5d3SJohn Marino {
68*86d7f5d3SJohn Marino assert (state->magic == CC_MAGIC);
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino /* If the current directory ever happens to be the same
71*86d7f5d3SJohn Marino as the one we last recorded for the cycle detection,
72*86d7f5d3SJohn Marino then it's obviously part of a cycle. */
73*86d7f5d3SJohn Marino if (state->chdir_counter && SAME_INODE (*sb, state->dev_ino))
74*86d7f5d3SJohn Marino return true;
75*86d7f5d3SJohn Marino
76*86d7f5d3SJohn Marino /* If the number of `descending' chdir calls is a power of two,
77*86d7f5d3SJohn Marino record the dev/ino of the current directory. */
78*86d7f5d3SJohn Marino if (is_zero_or_power_of_two (++(state->chdir_counter)))
79*86d7f5d3SJohn Marino {
80*86d7f5d3SJohn Marino /* On all architectures that we know about, if the counter
81*86d7f5d3SJohn Marino overflows then there is a directory cycle here somewhere,
82*86d7f5d3SJohn Marino even if we haven't detected it yet. Typically this happens
83*86d7f5d3SJohn Marino only after the counter is incremented 2**64 times, so it's a
84*86d7f5d3SJohn Marino fairly theoretical point. */
85*86d7f5d3SJohn Marino if (state->chdir_counter == 0)
86*86d7f5d3SJohn Marino return true;
87*86d7f5d3SJohn Marino
88*86d7f5d3SJohn Marino state->dev_ino.st_dev = sb->st_dev;
89*86d7f5d3SJohn Marino state->dev_ino.st_ino = sb->st_ino;
90*86d7f5d3SJohn Marino }
91*86d7f5d3SJohn Marino
92*86d7f5d3SJohn Marino return false;
93*86d7f5d3SJohn Marino }
94