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

rpms/sed/devel sed-4.1.5-bz185374.patch, NONE, 1.1 sed.spec, 1.30, 1.31



Author: pmachata

Update of /cvs/dist/rpms/sed/devel
In directory cvs.devel.redhat.com:/tmp/cvs-serv29413

Modified Files:
	sed.spec 
Added Files:
	sed-4.1.5-bz185374.patch 
Log Message:
- #185374:
  - Follow symlinks before rename (avoid symlink overwrite)
  - Add -c flag for copy instead of rename (avoid ownership change)


sed-4.1.5-bz185374.patch:
 doc/sed.1     |    7 ++
 lib/utils.c   |  146 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 lib/utils.h   |    3 +
 sed/execute.c |    8 ++-
 sed/sed.c     |   22 +++++++-
 sed/sed.h     |    4 +
 6 files changed, 172 insertions(+), 18 deletions(-)

--- NEW FILE sed-4.1.5-bz185374.patch ---
diff -urp sed-4.1.5-orig/doc/sed.1 sed-4.1.5/doc/sed.1
--- sed-4.1.5-orig/doc/sed.1	2006-02-03 10:27:35.000000000 +0100
+++ sed-4.1.5/doc/sed.1	2006-06-29 15:42:07.000000000 +0200
@@ -1,5 +1,5 @@
 .\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.28.
-.TH SED "1" "February 2006" "sed version 4.1.4" "User Commands"
+.TH SED "1" "June 2006" "sed version 4.1.5" "User Commands"
 .SH NAME
 sed \- stream editor for filtering and transforming text
 .SH SYNOPSIS
@@ -36,6 +36,11 @@ add the contents of script-file to the c
 .IP
 edit files in place (makes backup if extension supplied)
 .HP
+\fB\-c\fR, \fB\-\-copy\fR
+.IP
+use copy instead of rename when shuffling files in \fB\-i\fR mode
+(avoids change of input file ownership)
+.HP
 \fB\-l\fR N, \fB\-\-line\-length\fR=\fIN\fR
 .IP
 specify the desired line-wrap length for the `l' command
diff -urp sed-4.1.5-orig/lib/utils.c sed-4.1.5/lib/utils.c
--- sed-4.1.5-orig/lib/utils.c	2005-06-21 16:09:40.000000000 +0200
+++ sed-4.1.5/lib/utils.c	2006-06-29 16:28:05.000000000 +0200
@@ -35,6 +35,12 @@
 # include <stdlib.h>
 #endif /* HAVE_STDLIB_H */
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/sendfile.h>
+#include <fcntl.h>
+
 #include "utils.h"
 
 const char *myname;
@@ -315,32 +321,146 @@ do_ck_fclose(fp)
 }
 
 
-/* Panic on failing rename */
+/* Temporary buffer for ck_follow_symlink and ck_fcopy. */
+static char buf[4096];
+static size_t bufsize = sizeof (buf);
+
+/* Follow symlink and panic if something fails.  Returned value is
+   ultimate symlink target, stored in temporary buffer.  You need to
+   strdup the return value, if you intend to use it later.*/
+const char*
+ck_follow_symlink(const char * fname)
+{
+  static struct stat statbuf;
+  int err;
+
+  if (strlen (fname) >= bufsize)
+    panic("ck_follow_symlink: file name too long");
+  strcpy (buf, fname);
+
+  while (1)
+    {
+      err = lstat (buf, &statbuf);
+
+      if (err != 0)
+	panic("ck_follow_symlink: couldn't lstat %s: %s", buf, strerror(errno));
+
+      if ((statbuf.st_mode & S_IFLNK) == S_IFLNK)
+	{
+	  err = readlink (buf, buf, bufsize);
+	  if (err < 0)
+	    panic("ck_follow_symlink: readlink failed on %s: %s", buf, strerror(errno));
+	  else if (err == bufsize)
+	    panic("ck_follow_symlink: pointee name too long");
+	  else
+	    buf [err] = '\0';
+	}
+      else
+	break;
+    }
+
+  return buf;
+}
+
+/* Panic on failing unlink */
 void
-ck_rename (from, to, unlink_if_fail)
-  const char *from, *to;
-  const char *unlink_if_fail;
+ck_unlink (name)
+  const char *name;
 {
-  int rd = rename (from, to);
-  if (rd != -1)
-    return;
+  if (unlink (name) == -1)
+    panic (_("cannot remove %s: %s"), name, strerror (errno));
+}
 
-  if (unlink_if_fail)
+/* Attempt to unlink denoted file if operation rd failed. */
+static int
+_unlink_if_fail (rd, unlink_if_fail)
+  int rd;
+  const char *unlink_if_fail;
+{
+  if (rd == -1 && unlink_if_fail)
     {
       int save_errno = errno;
+      ck_unlink (unlink_if_fail);
+      errno = save_errno;
+    }
+
+  return rd != -1;
+}
+
+/* Copy contents between files. */
+static int
+_copy (from, to)
+  const char *from, *to;
+{
+  FILE *infile, *outfile;
+  int c, retval = 0;
       errno = 0;
-      unlink (unlink_if_fail);
 
-      /* Failure to remove the temporary file is more severe, so trigger it first.  */
-      if (errno != 0)
-        panic (_("cannot remove %s: %s"), unlink_if_fail, strerror (errno));
+  infile = fopen (from, "r");
+  if (infile == NULL)
+    return -1;
 
-      errno = save_errno;
+  outfile = fopen (to, "w");
+  if (outfile == NULL)
+    {
+      fclose (infile);
+      return -1;
+    }
+
+  while (1)
+    {
+      size_t bytes_in = fread (buf, 1, bufsize, infile);
+      size_t bytes_out;
+      if (bytes_in == 0)
+	{
+	  if (ferror (infile))
+	    retval = -1;
+	  break;
+	}
+
+      bytes_out = fwrite (buf, 1, bytes_in, outfile);
+      if (bytes_out != bytes_in)
+	{
+	  retval = -1;
+	  break;
+	}
     }
 
+  fclose (outfile);
+  fclose (infile);
+
+  return retval;
+}
+
+/* Panic on failing rename */
+void
+ck_rename (from, to, unlink_if_fail)
+  const char *from, *to;
+  const char *unlink_if_fail;
+{
+  if (!_unlink_if_fail (rename (from, to), unlink_if_fail))
   panic (_("cannot rename %s: %s"), from, strerror (errno));
 }
 
+/* Attempt to copy file contents between the files. */
+void
+ck_fcmove (from, to, unlink_if_fail)
+  const char *from, *to;
+  const char *unlink_if_fail;
+{
+  if (!_unlink_if_fail (_copy (from, to), unlink_if_fail))
+    panic (_("cannot copy %s to %s: %s"), from, to, strerror (errno));
+}
+
+/* Copy contents between files, and then unlink the source. */
+void
+ck_fcopy (from, to, unlink_if_fail)
+  const char *from, *to;
+  const char *unlink_if_fail;
+{
+  ck_fcmove (from, to, unlink_if_fail);
+  ck_unlink (from);
+}
 
 
 
diff -urp sed-4.1.5-orig/lib/utils.h sed-4.1.5/lib/utils.h
--- sed-4.1.5-orig/lib/utils.h	2005-06-21 16:09:40.000000000 +0200
+++ sed-4.1.5/lib/utils.h	2006-06-29 16:04:21.000000000 +0200
@@ -29,7 +29,10 @@ void ck_fflush P_((FILE *stream));
 void ck_fclose P_((FILE *stream));
 size_t ck_getline P_((char **text, size_t *buflen, FILE *stream));
 FILE * ck_mkstemp P_((char **p_filename, char *tmpdir, char *base));
+const char* ck_follow_symlink P_((const char * fname));
 void ck_rename P_((const char *from, const char *to, const char *unlink_if_fail));
+void ck_fcopy P_((const char *from, const char *to, const char *unlink_if_fail));
+void ck_fcmove P_((const char *from, const char *to, const char *unlink_if_fail));
 
 VOID *ck_malloc P_((size_t size));
 VOID *xmalloc P_((size_t size));
diff -urp sed-4.1.5-orig/sed/execute.c sed-4.1.5/sed/execute.c
--- sed-4.1.5-orig/sed/execute.c	2006-02-03 10:06:39.000000000 +0100
+++ sed-4.1.5/sed/execute.c	2006-06-29 16:12:43.000000000 +0200
@@ -711,16 +711,20 @@ closedown(input)
 
   if (in_place_extension && output_file.fp != NULL)
     {
+      char * target_name = ck_strdup (ck_follow_symlink (input->in_file_name));
       ck_fclose (output_file.fp);
       if (strcmp(in_place_extension, "*") != 0)
         {
           char *backup_file_name = get_backup_file_name(input->in_file_name);
-	  ck_rename (input->in_file_name, backup_file_name, input->out_file_name);
+	  (copy_instead_of_rename?ck_fcmove:ck_rename)
+	    (target_name, backup_file_name, input->out_file_name);
           free (backup_file_name);
 	}
 
-      ck_rename (input->out_file_name, input->in_file_name, input->out_file_name);
+      (copy_instead_of_rename?ck_fcopy:ck_rename)
+	(input->out_file_name, target_name, input->out_file_name);
       free (input->out_file_name);
+      free (target_name);
     }
 
   input->fp = NULL;
diff -urp sed-4.1.5-orig/sed/sed.c sed-4.1.5/sed/sed.c
--- sed-4.1.5-orig/sed/sed.c	2005-06-21 16:09:47.000000000 +0200
+++ sed-4.1.5/sed/sed.c	2006-06-29 15:41:52.000000000 +0200
@@ -73,6 +73,10 @@ bool separate_files = false;
 /* How do we edit files in-place? (we don't if NULL) */
 char *in_place_extension = NULL;
 
+/* Do we use copy or rename when in in-place edit mode? (boolean
+   value, non-zero for copy, zero for rename).*/
+int copy_instead_of_rename = 0;
+
 /* Do we need to be pedantically POSIX compliant? */
 enum posixicity_types posixicity;
 
@@ -107,6 +111,9 @@ Usage: %s [OPTION]... {script-only-if-no
                  add the contents of script-file to the commands to be executed\n"));
   fprintf(out, _("  -i[SUFFIX], --in-place[=SUFFIX]\n\
                  edit files in place (makes backup if extension supplied)\n"));
+  fprintf(out, _("  -c, --copy\n\
+                 use copy instead of rename when shuffling files in -i mode\n\
+		 (avoids change of input file ownership)\n"));
   fprintf(out, _("  -l N, --line-length=N\n\
                  specify the desired line-wrap length for the `l' command\n"));
   fprintf(out, _("  --posix\n\
@@ -142,9 +149,9 @@ main(argc, argv)
   char **argv;
 {
 #ifdef REG_PERL
-#define SHORTOPTS "snrRue:f:l:i::V:"
+#define SHORTOPTS "csnrRue:f:l:i::V:"
 #else
-#define SHORTOPTS "snrue:f:l:i::V:"
+#define SHORTOPTS "csnrue:f:l:i::V:"
 #endif
 
   static struct option longopts[] = {
@@ -155,6 +162,7 @@ main(argc, argv)
     {"expression", 1, NULL, 'e'},
     {"file", 1, NULL, 'f'},
     {"in-place", 2, NULL, 'i'},
+    {"copy", 0, NULL, 'c'},
     {"line-length", 1, NULL, 'l'},
     {"quiet", 0, NULL, 'n'},
     {"posix", 0, NULL, 'p'},
@@ -215,6 +223,10 @@ main(argc, argv)
 	  the_program = compile_file(the_program, optarg);
 	  break;
 
+	case 'c':
+	  copy_instead_of_rename = true;
+	  break;
+
 	case 'i':
 	  separate_files = true;
 	  if (optarg == NULL)
@@ -284,6 +296,12 @@ to the extent permitted by law.\n\
 	}
     }
 
+  if (copy_instead_of_rename && in_place_extension == NULL)
+    {
+      fprintf (stderr, _("Error: -c used without -i.\n"));
+      usage(4);
+    }
+
   if (!the_program)
     {
       if (optind < argc)
diff -urp sed-4.1.5-orig/sed/sed.h sed-4.1.5/sed/sed.h
--- sed-4.1.5-orig/sed/sed.h	2005-06-21 16:09:47.000000000 +0200
+++ sed-4.1.5/sed/sed.h	2006-06-29 15:41:52.000000000 +0200
@@ -228,6 +228,10 @@ extern countT lcmd_out_line_len;
 /* How do we edit files in-place? (we don't if NULL) */
 extern char *in_place_extension;
 
+/* Do we use copy or rename when in in-place edit mode? (boolean
+   value, non-zero for copy, zero for rename).*/
+extern int copy_instead_of_rename;
+
 /* Should we use EREs? */
 extern bool use_extended_syntax_p;
 


Index: sed.spec
===================================================================
RCS file: /cvs/dist/rpms/sed/devel/sed.spec,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -r1.30 -r1.31
--- sed.spec	11 Feb 2006 05:40:47 -0000	1.30
+++ sed.spec	29 Jun 2006 14:55:33 -0000	1.31
@@ -5,11 +5,12 @@
 Summary: A GNU stream text editor.
 Name: sed
 Version: 4.1.5
-Release: 1.2
+Release: 2
 License: GPL
 Group: Applications/Text
 Source0: ftp://ftp.gnu.org/pub/gnu/sed/sed-%{version}.tar.gz
 Source1: http://sed.sourceforge.net/sedfaq.txt
+Patch0: sed-4.1.4-bz185374.patch
 Prereq: /sbin/install-info
 Prefix: %{_prefix}
 Buildroot: %{_tmppath}/%{name}-root
@@ -25,6 +26,7 @@
 
 %prep
 %setup -q
+%patch0 -p1
 
 %build
 %configure --without-included-regex
@@ -63,6 +65,11 @@
 %{_mandir}/man*/*
 
 %changelog
+* Thu Jun 29 2006 Petr Machata <pmachata redhat com> - 4.1.5-2
+- #185374:
+  - Follow symlinks before rename (avoid symlink overwrite)
+  - Add -c flag for copy instead of rename (avoid ownership change)
+
 * Fri Feb 10 2006 Jesse Keating <jkeating redhat com> - 4.1.5-1.2
 - bump again for double-long bug on ppc(64)
 


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