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