New cycle-check module imported from coreutils.
[gnulib.git] / lib / cycle-check.c
1 /* help detect directory cycles efficiently
2
3    Copyright (C) 2003, 2004 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; see the file COPYING.
17    If not, write to the Free Software Foundation,
18    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by Jim Meyering */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <stdlib.h>
31
32 #include <stdbool.h>
33
34 #include "cycle-check.h"
35 #include "xalloc.h"
36
37 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
38   ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
39    && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
40
41 #define CC_MAGIC 9827862
42
43 /* Return true if I is a power of 2, or is zero.  */
44
45 static inline bool
46 is_zero_or_power_of_two (uintmax_t i)
47 {
48   return (i & (i - 1)) == 0;
49 }
50
51 void
52 cycle_check_init (struct cycle_check_state *state)
53 {
54   state->chdir_counter = 0;
55   state->magic = CC_MAGIC;
56 }
57
58 /* In traversing a directory hierarchy, call this function once for each
59    descending chdir call, with SB corresponding to the chdir operand.
60    If SB corresponds to a directory that has already been seen,
61    return true to indicate that there is a directory cycle.
62    Note that this is done `lazily', which means that some of
63    the directories in the cycle may be processed twice before
64    the cycle is detected.  */
65
66 bool
67 cycle_check (struct cycle_check_state *state, struct stat const *sb)
68 {
69   assert (state->magic == CC_MAGIC);
70
71   /* If the current directory ever happens to be the same
72      as the one we last recorded for the cycle detection,
73      then it's obviously part of a cycle.  */
74   if (state->chdir_counter && SAME_INODE (*sb, state->dev_ino))
75     return true;
76
77   /* If the number of `descending' chdir calls is a power of two,
78      record the dev/ino of the current directory.  */
79   if (is_zero_or_power_of_two (++(state->chdir_counter)))
80     {
81       /* On all architectures that we know about, if the counter
82          overflows then there is a directory cycle here somewhere,
83          even if we haven't detected it yet.  Typically this happens
84          only after the counter is incremented 2**64 times, so it's a
85          fairly theoretical point.  */
86       if (state->chdir_counter == 0)
87         return true;
88
89       state->dev_ino.st_dev = sb->st_dev;
90       state->dev_ino.st_ino = sb->st_ino;
91     }
92
93   return false;
94 }