Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/cycle-check.c @ 17476:6057744acd2c default tip master
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Fri, 16 Aug 2013 06:32:22 -0700 |
parents | e542fd46ad6f |
children |
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 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
17181
diff
changeset
|
3 Copyright (C) 2003-2006, 2009-2013 Free Software Foundation, Inc. |
5118
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
5118
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 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
8 (at your option) any later version. |
5118
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 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
5118
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
17 |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
18 /* Written by Jim Meyering */ |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
19 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6912
diff
changeset
|
20 #include <config.h> |
5118
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
21 |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
22 #include <sys/types.h> |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
23 #include <sys/stat.h> |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
24 #include <stdio.h> |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
25 #include <assert.h> |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
26 #include <stdlib.h> |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
27 |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
28 #include <stdbool.h> |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
29 |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
30 #include "cycle-check.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 #define CC_MAGIC 9827862 |
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 /* 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
|
35 |
17181
a80e4b259d9b
chdir-long, cycle-check, savewd: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16235
diff
changeset
|
36 static bool |
5118
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
37 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
|
38 { |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
39 return (i & (i - 1)) == 0; |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
40 } |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
41 |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
42 void |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
43 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
|
44 { |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
45 state->chdir_counter = 0; |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
46 state->magic = CC_MAGIC; |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
47 } |
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 /* 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
|
50 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
|
51 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
|
52 return true to indicate that there is a directory cycle. |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
53 Note that this is done "lazily", which means that some of |
5118
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
54 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
|
55 the cycle is detected. */ |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
56 |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
57 bool |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
58 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
|
59 { |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
60 assert (state->magic == CC_MAGIC); |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
61 |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
62 /* 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
|
63 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
|
64 then it's obviously part of a cycle. */ |
12069
1a57a12a6e7f
same-inode: revert prior change; it is not yet ready
Eric Blake <ebb9@byu.net>
parents:
12065
diff
changeset
|
65 if (state->chdir_counter && SAME_INODE (*sb, state->dev_ino)) |
5118
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
66 return true; |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
67 |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
68 /* If the number of "descending" chdir calls is a power of two, |
5118
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
69 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
|
70 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
|
71 { |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
72 /* On all architectures that we know about, if the counter |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
73 overflows then there is a directory cycle here somewhere, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
74 even if we haven't detected it yet. Typically this happens |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
75 only after the counter is incremented 2**64 times, so it's a |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
76 fairly theoretical point. */ |
5118
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
77 if (state->chdir_counter == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
78 return true; |
5118
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
79 |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
80 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
|
81 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
|
82 } |
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 return false; |
20e26e1fcaec
New cycle-check module imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
85 } |