bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

patch "sort" to warn if a temporary file can't be removed


From: Paul Eggert
Subject: patch "sort" to warn if a temporary file can't be removed
Date: Fri, 12 Nov 2004 21:03:04 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed this patch to have "sort" warn if a temporary file can't
be removed in normal operation (e.g. because someone did a "chmod a-w
/tmp" in the middle of the "sort" :-).  Also, I removed some code from
the critical section and tuned zaptemp a bit.

2004-11-12  Paul Eggert  <address@hidden>

        * src/sort.c (zaptemp): Warn if a temporary file is not removed.
        Remove a bit of code from the critical section.  Take advantage of the
        fact that NAME is now always in the list.

Index: src/sort.c
===================================================================
RCS file: /fetish/cu/src/sort.c,v
retrieving revision 1.298
retrieving revision 1.300
diff -p -u -r1.298 -r1.300
--- src/sort.c  13 Nov 2004 00:50:56 -0000      1.298
+++ src/sort.c  13 Nov 2004 04:45:58 -0000      1.300
@@ -514,28 +514,34 @@ add_temp_dir (char const *dir)
   temp_dirs[temp_dir_count++] = dir;
 }
 
-/* Search through the list of temporary files for NAME;
-   remove it if it is found on the list. */
+/* Remove NAME from the list of temporary files.  */
 
 static void
 zaptemp (const char *name)
 {
   struct tempnode *volatile *pnode;
   struct tempnode *node;
+  struct tempnode *next;
   sigset_t oldset;
+  int unlink_status;
+  int unlink_errno = 0;
 
-  for (pnode = &temphead; (node = *pnode); pnode = &node->next)
-    if (node->name == name)
-      {
-       /* Unlink the temporary file in a critical section, to avoid races.  */
-       sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
-       unlink (name);
-       if (! (*pnode = node->next))
-         temptail = pnode;
-       sigprocmask (SIG_SETMASK, &oldset, NULL);
-       free (node);
-       break;
-      }
+  for (pnode = &temphead; (node = *pnode)->name != name; pnode = &node->next)
+    continue;
+
+  /* Unlink the temporary file in a critical section to avoid races.  */
+  next = node->next;
+  sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
+  unlink_status = unlink (name);
+  unlink_errno = errno;
+  *pnode = next;
+  sigprocmask (SIG_SETMASK, &oldset, NULL);
+
+  if (unlink_status != 0)
+    error (0, unlink_errno, "warning: cannot remove: %s", name);
+  if (! next)
+    temptail = pnode;
+  free (node);
 }
 
 #if HAVE_NL_LANGINFO




reply via email to

[Prev in Thread] Current Thread [Next in Thread]