ls: fix coloring of dangling symlinks in default listing mode
authorPádraig Brady <P@draigBrady.com>
Sun, 16 Sep 2012 03:10:50 +0000 (04:10 +0100)
committerPádraig Brady <P@draigBrady.com>
Sun, 16 Sep 2012 10:41:50 +0000 (11:41 +0100)
When listing a directory containing dangling symlinks,
and not outputting a long format listing, and orphaned links
are set to no coloring in LS_COLORS, then the symlinks
would get no color rather than reverting to the standard
symlink color.  The issue was introduced in v8.13-19-g84457c4

* src/ls.c (print_color_indicator): Use the standard method
to check if coloring is specified for orphaned symlinks.
The existing method would consider 'or=00' or 'or=0' as significant
in LS_COLORS. Even 'or=' was significant as in that case the
string='or=' and the length=0.  Also apply the same change
for missing symlinks for consistency.
(gobble_file): Remove the simulation of linkok, which is only
tested in print_color_indicator() which now handles this directly
by keying on the LS_COLORS values correctly.
* tests/misc/ls-misc.pl: Add a test case.
* THANKS: Add the reporter.
* NEWS: Mention the fix.
Reported-by: David Matei
NEWS
THANKS.in
src/ls.c
tests/misc/ls-misc.pl

diff --git a/NEWS b/NEWS
index 2e69391..ab205f5 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   numbers, and up to 8 times slower for some worst-case individual numbers.
   [bug introduced in coreutils-7.0, with GNU MP support]
 
+  ls now correctly colors dangling symlinks when listing their containing
+  directories, with orphaned symlink coloring disabled in LS_COLORS.
+  [bug introduced in coreutils-8.14]
+
   rm -i -d now prompts the user then removes an empty directory, rather
   than ignoring the -d option and failing with an 'Is a directory' error.
   [bug introduced in coreutils-8.19, with the addition of --dir (-d)]
index 2c3f83c..b0061b3 100644 (file)
--- a/THANKS.in
+++ b/THANKS.in
@@ -153,6 +153,7 @@ David Godfrey                       dave@delta.demon.co.uk
 David Luyer                         david_luyer@pacific.net.au
 David Madore                        david.madore@ens.fr
 David Malone                        dwmalone@cnri.dit.ie
+David Matei                         matei@cs.toronto.edu
 Davide Canova                       kc.canova@gmail.com
 Dawson Engler                       engler@stanford.edu
 Dean Gaudet                         dean-savannah@arctic.org
index 9494ae9..106d234 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -3064,12 +3064,6 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
           free (linkname);
         }
 
-      /* When not distinguishing types of symlinks, pretend we know that
-         it is stat'able, so that it will be colored as a regular symlink,
-         and not as an orphan.  */
-      if (S_ISLNK (f->stat.st_mode) && !check_symlink_color)
-        f->linkok = true;
-
       if (S_ISLNK (f->stat.st_mode))
         f->filetype = symbolic_link;
       else if (S_ISDIR (f->stat.st_mode))
@@ -4293,7 +4287,7 @@ print_color_indicator (const struct fileinfo *f, bool symlink_target)
 
   /* Is this a nonexistent file?  If so, linkok == -1.  */
 
-  if (linkok == -1 && color_indicator[C_MISSING].string != NULL)
+  if (linkok == -1 && is_colored (C_MISSING))
     type = C_MISSING;
   else if (!f->stat_ok)
     {
@@ -4368,8 +4362,7 @@ print_color_indicator (const struct fileinfo *f, bool symlink_target)
   /* Adjust the color for orphaned symlinks.  */
   if (type == C_LINK && !linkok)
     {
-      if (color_symlink_as_referent
-          || color_indicator[C_ORPHAN].string)
+      if (color_symlink_as_referent || is_colored (C_ORPHAN))
         type = C_ORPHAN;
     }
 
index 71647f9..85e3808 100755 (executable)
@@ -263,6 +263,21 @@ my @Tests =
       {POST => sub {unlink 's' or die "s: $!\n";
                     restore_ls_colors; }},
      ],
+     # The patch associated with sl-dangle[678] introduced a regression
+     # that was fixed after coreutils-8.19.  This edge case triggers when
+     # listing a dir containing dangling symlinks, but with orphans uncolored.
+     # I.E. the same as the previous test, but listing the directory
+     # rather than the symlink directly.
+     ['sl-dangle9', '--color=always d',
+      {OUT => "$e\e[1;36ms$e\n"},
+      {PRE => sub {mkdir 'd',0755 or die "d: $!\n";
+                   symlink 'dangle', 'd/s' or die "d/s: $!\n";
+                   push_ls_colors('ln=1;36:or=:')
+       }},
+      {POST => sub {unlink 'd/s' or die "d/s: $!\n";
+                    rmdir 'd' or die "d: $!\n";
+                    restore_ls_colors; }},
+     ],
 
      # Test for a bug that was introduced in coreutils-4.5.4; fixed in 4.5.5.
      # To demonstrate it, the file in question (with executable bit set)