Blame SOURCES/coreutils-8.22-ls-interruption.patch

dd59ef
From e56f09afdbd4bd920e4a1f3b03e29eaccd954dac Mon Sep 17 00:00:00 2001
dd59ef
From: Kamil Dudka <kdudka@redhat.com>
dd59ef
Date: Tue, 6 Sep 2016 17:38:26 +0200
dd59ef
Subject: [PATCH] ls: allow interruption when reading slow directories
dd59ef
dd59ef
Postpone installation of signal handlers until they're needed.
dd59ef
That is right before the first escape sequence is printed.
dd59ef
dd59ef
* src/ls.c (signal_setup): A new function refactored from main()
dd59ef
to set and restore signal handlers.
dd59ef
(main): Move signal handler setup to put_indicator()
dd59ef
so that the default signal handling is untouched as long as possible.
dd59ef
Adjusted condition for restoring signal handlers to reflect the change.
dd59ef
(put_indicator): Install signal handlers if called for the very first
dd59ef
time.  It uses the same code that was in main() prior to this commit.
dd59ef
* NEWS: Mention the improvement.
dd59ef
dd59ef
See https://bugzilla.redhat.com/1365933
dd59ef
Fixes http://bugs.gnu.org/24232
dd59ef
dd59ef
Upstream-commit: 5445f7811ff945ea13aa2a0fd797eb4c0a0e4db0
dd59ef
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
dd59ef
---
dd59ef
 src/ls.c | 161 ++++++++++++++++++++++++++++++++++++---------------------------
dd59ef
 1 file changed, 93 insertions(+), 68 deletions(-)
dd59ef
dd59ef
diff --git a/src/ls.c b/src/ls.c
dd59ef
index a89c87a..1300938 100644
dd59ef
--- a/src/ls.c
dd59ef
+++ b/src/ls.c
dd59ef
@@ -1246,13 +1246,12 @@ process_signals (void)
dd59ef
     }
dd59ef
 }
dd59ef
 
dd59ef
-int
dd59ef
-main (int argc, char **argv)
dd59ef
-{
dd59ef
-  int i;
dd59ef
-  struct pending *thispend;
dd59ef
-  int n_files;
dd59ef
+/* Setup signal handlers if INIT is true,
dd59ef
+   otherwise restore to the default.  */
dd59ef
 
dd59ef
+static void
dd59ef
+signal_setup (bool init)
dd59ef
+{
dd59ef
   /* The signals that are trapped, and the number of such signals.  */
dd59ef
   static int const sig[] =
dd59ef
     {
dd59ef
@@ -1280,8 +1279,77 @@ main (int argc, char **argv)
dd59ef
   enum { nsigs = ARRAY_CARDINALITY (sig) };
dd59ef
 
dd59ef
 #if ! SA_NOCLDSTOP
dd59ef
-  bool caught_sig[nsigs];
dd59ef
+  static bool caught_sig[nsigs];
dd59ef
+#endif
dd59ef
+
dd59ef
+  int j;
dd59ef
+
dd59ef
+  if (init)
dd59ef
+    {
dd59ef
+#if SA_NOCLDSTOP
dd59ef
+      struct sigaction act;
dd59ef
+
dd59ef
+      sigemptyset (&caught_signals);
dd59ef
+      for (j = 0; j < nsigs; j++)
dd59ef
+        {
dd59ef
+          sigaction (sig[j], NULL, &act;;
dd59ef
+          if (act.sa_handler != SIG_IGN)
dd59ef
+            sigaddset (&caught_signals, sig[j]);
dd59ef
+        }
dd59ef
+
dd59ef
+      act.sa_mask = caught_signals;
dd59ef
+      act.sa_flags = SA_RESTART;
dd59ef
+
dd59ef
+      for (j = 0; j < nsigs; j++)
dd59ef
+        if (sigismember (&caught_signals, sig[j]))
dd59ef
+          {
dd59ef
+            act.sa_handler = sig[j] == SIGTSTP ? stophandler : sighandler;
dd59ef
+            sigaction (sig[j], &act, NULL);
dd59ef
+          }
dd59ef
+#else
dd59ef
+      for (j = 0; j < nsigs; j++)
dd59ef
+        {
dd59ef
+          caught_sig[j] = (signal (sig[j], SIG_IGN) != SIG_IGN);
dd59ef
+          if (caught_sig[j])
dd59ef
+            {
dd59ef
+              signal (sig[j], sig[j] == SIGTSTP ? stophandler : sighandler);
dd59ef
+              siginterrupt (sig[j], 0);
dd59ef
+            }
dd59ef
+        }
dd59ef
 #endif
dd59ef
+    }
dd59ef
+  else /* restore.  */
dd59ef
+    {
dd59ef
+#if SA_NOCLDSTOP
dd59ef
+      for (j = 0; j < nsigs; j++)
dd59ef
+        if (sigismember (&caught_signals, sig[j]))
dd59ef
+          signal (sig[j], SIG_DFL);
dd59ef
+#else
dd59ef
+      for (j = 0; j < nsigs; j++)
dd59ef
+        if (caught_sig[j])
dd59ef
+          signal (sig[j], SIG_DFL);
dd59ef
+#endif
dd59ef
+    }
dd59ef
+}
dd59ef
+
dd59ef
+static inline void
dd59ef
+signal_init (void)
dd59ef
+{
dd59ef
+  signal_setup (true);
dd59ef
+}
dd59ef
+
dd59ef
+static inline void
dd59ef
+signal_restore (void)
dd59ef
+{
dd59ef
+  signal_setup (false);
dd59ef
+}
dd59ef
+
dd59ef
+int
dd59ef
+main (int argc, char **argv)
dd59ef
+{
dd59ef
+  int i;
dd59ef
+  struct pending *thispend;
dd59ef
+  int n_files;
dd59ef
 
dd59ef
   initialize_main (&argc, &argv);
dd59ef
   set_program_name (argv[0]);
dd59ef
@@ -1317,46 +1385,6 @@ main (int argc, char **argv)
dd59ef
           || (is_colored (C_MISSING) && (format == long_format
dd59ef
               || format == security_format)))
dd59ef
         check_symlink_color = true;
dd59ef
-
dd59ef
-      /* If the standard output is a controlling terminal, watch out
dd59ef
-         for signals, so that the colors can be restored to the
dd59ef
-         default state if "ls" is suspended or interrupted.  */
dd59ef
-
dd59ef
-      if (0 <= tcgetpgrp (STDOUT_FILENO))
dd59ef
-        {
dd59ef
-          int j;
dd59ef
-#if SA_NOCLDSTOP
dd59ef
-          struct sigaction act;
dd59ef
-
dd59ef
-          sigemptyset (&caught_signals);
dd59ef
-          for (j = 0; j < nsigs; j++)
dd59ef
-            {
dd59ef
-              sigaction (sig[j], NULL, &act;;
dd59ef
-              if (act.sa_handler != SIG_IGN)
dd59ef
-                sigaddset (&caught_signals, sig[j]);
dd59ef
-            }
dd59ef
-
dd59ef
-          act.sa_mask = caught_signals;
dd59ef
-          act.sa_flags = SA_RESTART;
dd59ef
-
dd59ef
-          for (j = 0; j < nsigs; j++)
dd59ef
-            if (sigismember (&caught_signals, sig[j]))
dd59ef
-              {
dd59ef
-                act.sa_handler = sig[j] == SIGTSTP ? stophandler : sighandler;
dd59ef
-                sigaction (sig[j], &act, NULL);
dd59ef
-              }
dd59ef
-#else
dd59ef
-          for (j = 0; j < nsigs; j++)
dd59ef
-            {
dd59ef
-              caught_sig[j] = (signal (sig[j], SIG_IGN) != SIG_IGN);
dd59ef
-              if (caught_sig[j])
dd59ef
-                {
dd59ef
-                  signal (sig[j], sig[j] == SIGTSTP ? stophandler : sighandler);
dd59ef
-                  siginterrupt (sig[j], 0);
dd59ef
-                }
dd59ef
-            }
dd59ef
-#endif
dd59ef
-        }
dd59ef
     }
dd59ef
 
dd59ef
   if (dereference == DEREF_UNDEFINED)
dd59ef
@@ -1467,32 +1495,21 @@ main (int argc, char **argv)
dd59ef
       print_dir_name = true;
dd59ef
     }
dd59ef
 
dd59ef
-  if (print_with_color)
dd59ef
+  if (print_with_color && used_color)
dd59ef
     {
dd59ef
       int j;
dd59ef
 
dd59ef
-      if (used_color)
dd59ef
-        {
dd59ef
-          /* Skip the restore when it would be a no-op, i.e.,
dd59ef
-             when left is "\033[" and right is "m".  */
dd59ef
-          if (!(color_indicator[C_LEFT].len == 2
dd59ef
-                && memcmp (color_indicator[C_LEFT].string, "\033[", 2) == 0
dd59ef
-                && color_indicator[C_RIGHT].len == 1
dd59ef
-                && color_indicator[C_RIGHT].string[0] == 'm'))
dd59ef
-            restore_default_color ();
dd59ef
-        }
dd59ef
+      /* Skip the restore when it would be a no-op, i.e.,
dd59ef
+         when left is "\033[" and right is "m".  */
dd59ef
+      if (!(color_indicator[C_LEFT].len == 2
dd59ef
+            && memcmp (color_indicator[C_LEFT].string, "\033[", 2) == 0
dd59ef
+            && color_indicator[C_RIGHT].len == 1
dd59ef
+            && color_indicator[C_RIGHT].string[0] == 'm'))
dd59ef
+        restore_default_color ();
dd59ef
+
dd59ef
       fflush (stdout);
dd59ef
 
dd59ef
-      /* Restore the default signal handling.  */
dd59ef
-#if SA_NOCLDSTOP
dd59ef
-      for (j = 0; j < nsigs; j++)
dd59ef
-        if (sigismember (&caught_signals, sig[j]))
dd59ef
-          signal (sig[j], SIG_DFL);
dd59ef
-#else
dd59ef
-      for (j = 0; j < nsigs; j++)
dd59ef
-        if (caught_sig[j])
dd59ef
-          signal (sig[j], SIG_DFL);
dd59ef
-#endif
dd59ef
+      signal_restore ();
dd59ef
 
dd59ef
       /* Act on any signals that arrived before the default was restored.
dd59ef
          This can process signals out of order, but there doesn't seem to
dd59ef
@@ -4512,6 +4529,14 @@ put_indicator (const struct bin_str *ind)
dd59ef
   if (! used_color)
dd59ef
     {
dd59ef
       used_color = true;
dd59ef
+
dd59ef
+      /* If the standard output is a controlling terminal, watch out
dd59ef
+         for signals, so that the colors can be restored to the
dd59ef
+         default state if "ls" is suspended or interrupted.  */
dd59ef
+
dd59ef
+      if (0 <= tcgetpgrp (STDOUT_FILENO))
dd59ef
+        signal_init ();
dd59ef
+
dd59ef
       prep_non_filename_text ();
dd59ef
     }
dd59ef
 
dd59ef
-- 
dd59ef
2.13.5
dd59ef