From 58034aa68fd45ba3ff8bbb554d2cdef6ac31de17 Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Wed, 8 Aug 2012 15:24:05 +0200 Subject: [PATCH] du: Fix an issue with bogus warnings on bind-mounted directories * NEWS: Mention the fix. * src/du.c: Add a function to read+stat mount points and properly check cyclic directory entries. --- NEWS | 3 +++ gnulib | 2 +- src/du.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index ca4568a..8f85c2b 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,9 @@ GNU coreutils NEWS -*- outline -*- ** Bug fixes + du no longer emits bogus warnings when traversing bind-mounted + directory cycles. + cksum now prints checksums atomically so that concurrent processes will not intersperse their output. [the bug dates back to the initial implementation] diff --git a/gnulib b/gnulib index 39cedf6..696aa74 160000 --- a/gnulib +++ b/gnulib @@ -1 +1 @@ -Subproject commit 39cedf6f427350ac47118d231c05a7b73b609f89 +Subproject commit 696aa74b38f8c109c21e4c01194faab53fb4cefc diff --git a/src/du.c b/src/du.c index 7333941..b915320 100644 --- a/src/du.c +++ b/src/du.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "system.h" #include "argmatch.h" #include "argv-iter.h" @@ -60,9 +61,14 @@ extern bool fts_debug; # define FTS_CROSS_CHECK(Fts) #endif +#define MTAB "/etc/mtab" + /* A set of dev/ino pairs. */ static struct di_set *di_set; +/* A hash table for mount points. */ +static struct di_set *di_mnt; + /* Keep track of the preceding "level" (depth in hierarchy) from one call of process_file to the next. */ static size_t prev_level; @@ -333,11 +339,11 @@ Mandatory arguments to long options are mandatory for short options too.\n\ exit (status); } -/* Try to insert the INO/DEV pair into the global table, HTAB. +/* Try to insert the INO/DEV pair into the di_set table. Return true if the pair is successfully inserted, false if the pair is already in the table. */ static bool -hash_ins (ino_t ino, dev_t dev) +hash_ins (struct di_set *di_set, ino_t ino, dev_t dev) { int inserted = di_set_insert (di_set, dev, ino); if (inserted < 0) @@ -461,7 +467,7 @@ process_file (FTS *fts, FTSENT *ent) if (excluded || (! opt_count_all && (hash_all || (! S_ISDIR (sb->st_mode) && 1 < sb->st_nlink)) - && ! hash_ins (sb->st_ino, sb->st_dev))) + && ! hash_ins (di_set, sb->st_ino, sb->st_dev))) { /* If ignoring a directory in preorder, skip its children. Ignore the next fts_read output too, as it's a postorder @@ -476,6 +482,17 @@ process_file (FTS *fts, FTSENT *ent) return true; } + /*Check if dir is already in the mount table */ + if (S_ISDIR (sb->st_mode)) + { + if (di_set_lookup (di_mnt, sb->st_dev, sb->st_ino)) + { + fts_set (fts, ent, FTS_SKIP); + error (0, 0, _("mount point %s already traversed"), quote (file)); + return false; + } + } + switch (info) { case FTS_D: @@ -623,6 +640,29 @@ du_files (char **files, int bit_flags) return ok; } +/* Fill the di_mnt table with dev/ino pairs + * of mount points */ + +static void +fill_mount_table (void) +{ + FILE *mtab_fp; + struct mntent *mnt_ent; + struct stat *buf; + buf = xmalloc (sizeof *buf); + + mtab_fp = setmntent (MTAB, "r"); + + while (mnt_ent = getmntent (mtab_fp)) + { + if (!stat (mnt_ent->mnt_dir, buf)) + hash_ins (di_mnt, buf->st_ino, buf->st_dev); + } + + free (buf); + endmntent (mtab_fp); +} + int main (int argc, char **argv) { @@ -922,6 +962,13 @@ main (int argc, char **argv) xalloc_die (); /* Initialize the set of dev,inode pairs. */ + + di_mnt = di_set_alloc (); + if (!di_mnt) + xalloc_die (); + + fill_mount_table (); + di_set = di_set_alloc (); if (!di_set) xalloc_die (); @@ -1002,6 +1049,7 @@ main (int argc, char **argv) argv_iter_free (ai); di_set_free (di_set); + di_set_free (di_mnt); if (files_from && (ferror (stdin) || fclose (stdin) != 0) && ok) error (EXIT_FAILURE, 0, _("error reading %s"), quote (files_from)); -- 1.7.11.2