(root)/
coreutils-9.4/
lib/
cycle-check.c
       1  /* help detect directory cycles efficiently
       2  
       3     Copyright (C) 2003-2006, 2009-2023 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 3 of the License, or
       8     (at your option) 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.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  /* Written by Jim Meyering */
      19  
      20  #include <config.h>
      21  
      22  #include "cycle-check.h"
      23  
      24  #include <sys/types.h>
      25  #include <sys/stat.h>
      26  #include <stdio.h>
      27  #include <stdlib.h>
      28  
      29  #include "assure.h"
      30  
      31  #define CC_MAGIC 9827862
      32  
      33  /* Return true if I is a power of 2, or is zero.  */
      34  
      35  static bool
      36  is_zero_or_power_of_two (uintmax_t i)
      37  {
      38    return (i & (i - 1)) == 0;
      39  }
      40  
      41  void
      42  cycle_check_init (struct cycle_check_state *state)
      43  {
      44    state->chdir_counter = 0;
      45    state->magic = CC_MAGIC;
      46  }
      47  
      48  /* In traversing a directory hierarchy, call this function once for each
      49     descending chdir call, with SB corresponding to the chdir operand.
      50     If SB corresponds to a directory that has already been seen,
      51     return true to indicate that there is a directory cycle.
      52     Note that this is done "lazily", which means that some of
      53     the directories in the cycle may be processed twice before
      54     the cycle is detected.  */
      55  
      56  bool
      57  cycle_check (struct cycle_check_state *state, struct stat const *sb)
      58  {
      59    assure (state->magic == CC_MAGIC);
      60  
      61    /* If the current directory ever happens to be the same
      62       as the one we last recorded for the cycle detection,
      63       then it's obviously part of a cycle.  */
      64    if (state->chdir_counter && SAME_INODE (*sb, state->dev_ino))
      65      return true;
      66  
      67    /* If the number of "descending" chdir calls is a power of two,
      68       record the dev/ino of the current directory.  */
      69    if (is_zero_or_power_of_two (++(state->chdir_counter)))
      70      {
      71        /* On all architectures that we know about, if the counter
      72           overflows then there is a directory cycle here somewhere,
      73           even if we haven't detected it yet.  Typically this happens
      74           only after the counter is incremented 2**64 times, so it's a
      75           fairly theoretical point.  */
      76        if (state->chdir_counter == 0)
      77          return true;
      78  
      79        state->dev_ino.st_dev = sb->st_dev;
      80        state->dev_ino.st_ino = sb->st_ino;
      81      }
      82  
      83    return false;
      84  }