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