X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ffts-cycle.c;h=95da56e236a6a01c3a3659811f0c1d897cdea6cb;hb=1276a2c5f24c0c932426aca9c899fa524d2443f2;hp=a0310c2e57c39394b5a25aa291ece09e771e25a6;hpb=c2546905a0f3958439581290d150c9ae4ae52a50;p=gnulib.git diff --git a/lib/fts-cycle.c b/lib/fts-cycle.c index a0310c2e5..95da56e23 100644 --- a/lib/fts-cycle.c +++ b/lib/fts-cycle.c @@ -1,13 +1,13 @@ /* Detect cycles in file tree walks. - Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. + Copyright (C) 2003-2006, 2009-2014 Free Software Foundation, Inc. Written by Jim Meyering. - This program is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -15,13 +15,12 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see . */ #include "cycle-check.h" #include "hash.h" -/* Use these each of these to map a device/inode pair to an FTSENT. */ +/* Use each of these to map a device/inode pair to an FTSENT. */ struct Active_dir { dev_t dev; @@ -50,19 +49,19 @@ AD_hash (void const *x, size_t table_size) static bool setup_dir (FTS *fts) { - if (fts->fts_options & FTS_TIGHT_CYCLE_CHECK) + if (fts->fts_options & (FTS_TIGHT_CYCLE_CHECK | FTS_LOGICAL)) { enum { HT_INITIAL_SIZE = 31 }; fts->fts_cycle.ht = hash_initialize (HT_INITIAL_SIZE, NULL, AD_hash, - AD_compare, free); + AD_compare, free); if (! fts->fts_cycle.ht) - return false; + return false; } else { fts->fts_cycle.state = malloc (sizeof *fts->fts_cycle.state); if (! fts->fts_cycle.state) - return false; + return false; cycle_check_init (fts->fts_cycle.state); } @@ -74,47 +73,47 @@ setup_dir (FTS *fts) static bool enter_dir (FTS *fts, FTSENT *ent) { - if (fts->fts_options & FTS_TIGHT_CYCLE_CHECK) + if (fts->fts_options & (FTS_TIGHT_CYCLE_CHECK | FTS_LOGICAL)) { struct stat const *st = ent->fts_statp; struct Active_dir *ad = malloc (sizeof *ad); struct Active_dir *ad_from_table; if (!ad) - return false; + return false; ad->dev = st->st_dev; ad->ino = st->st_ino; ad->fts_ent = ent; /* See if we've already encountered this directory. - This can happen when following symlinks as well as - with a corrupted directory hierarchy. */ + This can happen when following symlinks as well as + with a corrupted directory hierarchy. */ ad_from_table = hash_insert (fts->fts_cycle.ht, ad); if (ad_from_table != ad) - { - free (ad); - if (!ad_from_table) - return false; - - /* There was an entry with matching dev/inode already in the table. - Record the fact that we've found a cycle. */ - ent->fts_cycle = ad_from_table->fts_ent; - ent->fts_info = FTS_DC; - } + { + free (ad); + if (!ad_from_table) + return false; + + /* There was an entry with matching dev/inode already in the table. + Record the fact that we've found a cycle. */ + ent->fts_cycle = ad_from_table->fts_ent; + ent->fts_info = FTS_DC; + } } else { if (cycle_check (fts->fts_cycle.state, ent->fts_statp)) - { - /* FIXME: setting fts_cycle like this isn't proper. - To do what the documentation requires, we'd have to - go around the cycle again and find the right entry. - But no callers in coreutils use the fts_cycle member. */ - ent->fts_cycle = ent; - ent->fts_info = FTS_DC; - } + { + /* FIXME: setting fts_cycle like this isn't proper. + To do what the documentation requires, we'd have to + go around the cycle again and find the right entry. + But no callers in coreutils use the fts_cycle member. */ + ent->fts_cycle = ent; + ent->fts_info = FTS_DC; + } } return true; @@ -125,18 +124,25 @@ enter_dir (FTS *fts, FTSENT *ent) static void leave_dir (FTS *fts, FTSENT *ent) { - if (fts->fts_options & FTS_TIGHT_CYCLE_CHECK) + struct stat const *st = ent->fts_statp; + if (fts->fts_options & (FTS_TIGHT_CYCLE_CHECK | FTS_LOGICAL)) { - struct stat const *st = ent->fts_statp; struct Active_dir obj; void *found; obj.dev = st->st_dev; obj.ino = st->st_ino; found = hash_delete (fts->fts_cycle.ht, &obj); if (!found) - abort (); + abort (); free (found); } + else + { + FTSENT *parent = ent->fts_parent; + if (parent != NULL && 0 <= parent->fts_level) + CYCLE_CHECK_REFLECT_CHDIR_UP (fts->fts_cycle.state, + *(parent->fts_statp), *st); + } } /* Free any memory used for cycle detection. */ @@ -144,10 +150,10 @@ leave_dir (FTS *fts, FTSENT *ent) static void free_dir (FTS *sp) { - if (sp->fts_options & FTS_TIGHT_CYCLE_CHECK) + if (sp->fts_options & (FTS_TIGHT_CYCLE_CHECK | FTS_LOGICAL)) { if (sp->fts_cycle.ht) - hash_free (sp->fts_cycle.ht); + hash_free (sp->fts_cycle.ht); } else free (sp->fts_cycle.state);