5a67ee
From f336bde91e3fd9c3c2960aa548b8917eb1216678 Mon Sep 17 00:00:00 2001
5a67ee
From: Jakub Martisko <jamartis@redhat.com>
5a67ee
Date: Thu, 6 Feb 2020 15:26:33 +0100
5a67ee
Subject: [PATCH] -c flag
5a67ee
5a67ee
---
5a67ee
 sed/execute.c | 18 +++++++++--
5a67ee
 sed/sed.c     | 20 +++++++++++-
5a67ee
 sed/sed.h     |  4 +++
5a67ee
 sed/utils.c   | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++
5a67ee
 sed/utils.h   |  2 ++
5a67ee
 5 files changed, 127 insertions(+), 3 deletions(-)
5a67ee
5a67ee
diff --git a/sed/execute.c b/sed/execute.c
5a67ee
index c5f07cc..4e5f5b3 100644
5a67ee
--- a/sed/execute.c
5a67ee
+++ b/sed/execute.c
5a67ee
@@ -670,11 +670,25 @@ closedown (struct input *input)
5a67ee
       if (strcmp (in_place_extension, "*") != 0)
5a67ee
         {
5a67ee
           char *backup_file_name = get_backup_file_name (target_name);
5a67ee
-          ck_rename (target_name, backup_file_name, input->out_file_name);
5a67ee
+          if (copy_instead_of_rename)
5a67ee
+          {
5a67ee
+            ck_fccopy (target_name, backup_file_name, input->out_file_name);
5a67ee
+          }
5a67ee
+          else
5a67ee
+          {
5a67ee
+            ck_rename (target_name, backup_file_name, input->out_file_name);
5a67ee
+          }
5a67ee
           free (backup_file_name);
5a67ee
         }
5a67ee
 
5a67ee
-      ck_rename (input->out_file_name, target_name, input->out_file_name);
5a67ee
+      if (copy_instead_of_rename)
5a67ee
+      {
5a67ee
+        ck_fcmove (input->out_file_name, target_name, input->out_file_name);
5a67ee
+      }
5a67ee
+      else
5a67ee
+      {
5a67ee
+        ck_rename (input->out_file_name, target_name, input->out_file_name);
5a67ee
+      }
5a67ee
       cancel_cleanup ();
5a67ee
       free (input->out_file_name);
5a67ee
     }
5a67ee
diff --git a/sed/sed.c b/sed/sed.c
5a67ee
index 1ca5839..745159e 100644
5a67ee
--- a/sed/sed.c
5a67ee
+++ b/sed/sed.c
5a67ee
@@ -67,6 +67,10 @@ bool debug = false;
5a67ee
 /* How do we edit files in-place? (we don't if NULL) */
5a67ee
 char *in_place_extension = NULL;
5a67ee
 
5a67ee
+/* Do we use copy or rename when in in-place edit mode? (boolean
5a67ee
+ +   value, non-zero for copy, zero for rename).*/
5a67ee
+int copy_instead_of_rename = 0;
5a67ee
+
5a67ee
 /* The mode to use to read/write files, either "r"/"w" or "rb"/"wb".  */
5a67ee
 char const *read_mode = "r";
5a67ee
 char const *write_mode = "w";
5a67ee
@@ -170,6 +174,10 @@ Usage: %s [OPTION]... {script-only-if-no-other-script} [input-file]...\n\
5a67ee
 #endif
5a67ee
   fprintf (out, _("  -i[SUFFIX], --in-place[=SUFFIX]\n\
5a67ee
                  edit files in place (makes backup if SUFFIX supplied)\n"));
5a67ee
+
5a67ee
+  fprintf(out, _("  -c, --copy\n\
5a67ee
+                 use copy instead of rename when shuffling files in -i mode\n"));
5a67ee
+
5a67ee
 #if O_BINARY
5a67ee
   fprintf (out, _("  -b, --binary\n\
5a67ee
                  open files in binary mode (CR+LFs are not" \
5a67ee
@@ -214,7 +222,7 @@ specified, then the standard input is read.\n\
5a67ee
 int
5a67ee
 main (int argc, char **argv)
5a67ee
 {
5a67ee
-#define SHORTOPTS "bsnrzuEe:f:l:i::V:"
5a67ee
+#define SHORTOPTS "bcsnrzuEe:f:l:i::V:"
5a67ee
 
5a67ee
   enum { SANDBOX_OPTION = CHAR_MAX+1,
5a67ee
          DEBUG_OPTION
5a67ee
@@ -228,6 +236,7 @@ main (int argc, char **argv)
5a67ee
     {"file", 1, NULL, 'f'},
5a67ee
     {"in-place", 2, NULL, 'i'},
5a67ee
     {"line-length", 1, NULL, 'l'},
5a67ee
+    {"copy", 0, NULL, 'c'},
5a67ee
     {"null-data", 0, NULL, 'z'},
5a67ee
     {"zero-terminated", 0, NULL, 'z'},
5a67ee
     {"quiet", 0, NULL, 'n'},
5a67ee
@@ -306,6 +315,10 @@ main (int argc, char **argv)
5a67ee
           follow_symlinks = true;
5a67ee
           break;
5a67ee
 
5a67ee
+        case 'c':
5a67ee
+          copy_instead_of_rename = true;
5a67ee
+          break;
5a67ee
+
5a67ee
         case 'i':
5a67ee
           separate_files = true;
5a67ee
           IF_LINT (free (in_place_extension));
5a67ee
@@ -376,6 +389,11 @@ main (int argc, char **argv)
5a67ee
         }
5a67ee
     }
5a67ee
 
5a67ee
+  if (copy_instead_of_rename && in_place_extension == NULL)
5a67ee
+    {
5a67ee
+      fprintf (stderr, _("Error: -c used without -i.\n"));
5a67ee
+      usage(4);
5a67ee
+    }
5a67ee
   if (!the_program)
5a67ee
     {
5a67ee
       if (optind < argc)
5a67ee
diff --git a/sed/sed.h b/sed/sed.h
5a67ee
index 1c8e83a..0859e72 100644
5a67ee
--- a/sed/sed.h
5a67ee
+++ b/sed/sed.h
5a67ee
@@ -236,6 +236,10 @@ extern countT lcmd_out_line_len;
5a67ee
 /* How do we edit files in-place? (we don't if NULL) */
5a67ee
 extern char *in_place_extension;
5a67ee
 
5a67ee
+/* Do we use copy or rename when in in-place edit mode? (boolean
5a67ee
+   value, non-zero for copy, zero for rename).*/
5a67ee
+extern int copy_instead_of_rename;
5a67ee
+
5a67ee
 /* The mode to use to read and write files, either "rt"/"w" or "rb"/"wb".  */
5a67ee
 extern char const *read_mode;
5a67ee
 extern char const *write_mode;
5a67ee
diff --git a/sed/utils.c b/sed/utils.c
5a67ee
index 9576dd1..371d5a9 100644
5a67ee
--- a/sed/utils.c
5a67ee
+++ b/sed/utils.c
5a67ee
@@ -25,6 +25,7 @@
5a67ee
 #include <sys/stat.h>
5a67ee
 #include <unistd.h>
5a67ee
 #include <limits.h>
5a67ee
+#include <fcntl.h>
5a67ee
 
5a67ee
 #include "binary-io.h"
5a67ee
 #include "unlocked-io.h"
5a67ee
@@ -400,7 +401,92 @@ ck_rename (const char *from, const char *to, const char *unlink_if_fail)
5a67ee
   panic (_("cannot rename %s: %s"), from, strerror (errno));
5a67ee
 }
5a67ee
 
5a67ee
+/* Downstream -c related functions */
5a67ee
 
5a67ee
+/* Panic on failing unlink */
5a67ee
+void
5a67ee
+ck_unlink (const char *name)
5a67ee
+{
5a67ee
+  if (unlink (name) == -1)
5a67ee
+    panic (_("cannot remove %s: %s"), name, strerror (errno));
5a67ee
+}
5a67ee
+
5a67ee
+/* Attempt to unlink denoted file if operation rd failed. */
5a67ee
+static int
5a67ee
+_unlink_if_fail (int rd,const char * unlink_if_fail)
5a67ee
+{
5a67ee
+  if (rd == -1 && unlink_if_fail)
5a67ee
+    {
5a67ee
+      int save_errno = errno;
5a67ee
+      ck_unlink (unlink_if_fail);
5a67ee
+      errno = save_errno;
5a67ee
+    }
5a67ee
+
5a67ee
+  return rd != -1;
5a67ee
+}
5a67ee
+
5a67ee
+/* Copy contents between files. */
5a67ee
+static int
5a67ee
+_copy (from, to)
5a67ee
+  const char *from, *to;
5a67ee
+{
5a67ee
+  static char buf[4096];
5a67ee
+
5a67ee
+  FILE *infile, *outfile;
5a67ee
+  int c, retval = 0;
5a67ee
+  errno = 0;
5a67ee
+
5a67ee
+  infile = fopen (from, "r");
5a67ee
+  if (infile == NULL)
5a67ee
+    return -1;
5a67ee
+
5a67ee
+  outfile = fopen (to, "w");
5a67ee
+  if (outfile == NULL)
5a67ee
+    {
5a67ee
+      fclose (infile);
5a67ee
+      return -1;
5a67ee
+    }
5a67ee
+
5a67ee
+  while (1)
5a67ee
+    {
5a67ee
+      size_t bytes_in = fread (buf, 1, sizeof (buf), infile);
5a67ee
+      size_t bytes_out;
5a67ee
+      if (bytes_in == 0)
5a67ee
+        {
5a67ee
+          if (ferror (infile))
5a67ee
+            retval = -1;
5a67ee
+          break;
5a67ee
+        }
5a67ee
+
5a67ee
+      bytes_out = fwrite (buf, 1, bytes_in, outfile);
5a67ee
+      if (bytes_out != bytes_in)
5a67ee
+        {
5a67ee
+          retval = -1;
5a67ee
+          break;
5a67ee
+        }
5a67ee
+    }
5a67ee
+
5a67ee
+  fclose (outfile);
5a67ee
+  fclose (infile);
5a67ee
+
5a67ee
+  return retval;
5a67ee
+}
5a67ee
+
5a67ee
+/* Attempt to copy file contents between the files. */
5a67ee
+void
5a67ee
+ck_fccopy (const char *from,const char *to, const char *unlink_if_fail)
5a67ee
+{
5a67ee
+   if (!_unlink_if_fail (_copy (from, to), unlink_if_fail))
5a67ee
+   panic (_("cannot copy %s to %s: %s"), from, to, strerror (errno));
5a67ee
+  }
5a67ee
+
5a67ee
+/* Copy contents between files, and then unlink the source. */
5a67ee
+void
5a67ee
+ck_fcmove (const char *from, const char *to,const char *unlink_if_fail)
5a67ee
+{
5a67ee
+   ck_fccopy (from, to, unlink_if_fail);
5a67ee
+    ck_unlink (from);
5a67ee
+}
5a67ee
 
5a67ee
 
5a67ee
 /* Implement a variable sized buffer of `stuff'.  We don't know what it is,
5a67ee
diff --git a/sed/utils.h b/sed/utils.h
5a67ee
index 47a029e..0aba107 100644
5a67ee
--- a/sed/utils.h
5a67ee
+++ b/sed/utils.h
5a67ee
@@ -40,6 +40,8 @@ size_t ck_getdelim (char **text, size_t *buflen, char buffer_delimiter,
5a67ee
 FILE * ck_mkstemp (char **p_filename, const char *tmpdir, const char *base,
5a67ee
                    const char *mode) _GL_ARG_NONNULL ((1, 2, 3, 4));
5a67ee
 void ck_rename (const char *from, const char *to, const char *unlink_if_fail);
5a67ee
+void ck_fccopy (const char *from, const char *to, const char *unlink_if_fail);
5a67ee
+void ck_fcmove (const char *from, const char *to, const char *unlink_if_fail);
5a67ee
 
5a67ee
 void *ck_malloc (size_t size);
5a67ee
 void *ck_realloc (void *ptr, size_t size);
5a67ee
-- 
5a67ee
2.24.1
5a67ee