escape double quotes, so system commands don't break anymore

This commit is contained in:
Michael Wehr
2021-12-22 21:16:20 +01:00
parent 7e2df2e0b7
commit edec49ec6f

View File

@@ -1,7 +1,7 @@
diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
--- coreutils-9.0/src/copy.c 2021-09-24 13:31:05.000000000 +0200 --- coreutils-9.0/src/copy.c 2021-09-24 13:31:05.000000000 +0200
+++ coreutils-9.0-patched/src/copy.c 2021-12-22 14:00:42.663699889 +0100 +++ coreutils-9.0-patched/src/copy.c 2021-12-22 20:21:15.702583886 +0100
@@ -129,6 +129,70 @@ @@ -129,6 +129,101 @@
dev_t dev; dev_t dev;
}; };
@@ -14,6 +14,37 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
+}; +};
+ +
+/* BEGIN progress mod */ +/* BEGIN progress mod */
+char * escape_double_quotes ( char * _cUnescapedString )
+{
+ int pos = 0;
+ int count = 0;
+ for(pos = 0; _cUnescapedString[pos] != '\0'; pos++)
+ {
+ if(_cUnescapedString[pos] == '"')
+ ++count;
+ }
+
+ char * _cEscapedString;
+ _cEscapedString = calloc( sizeof(char) * ( pos + 1 + count * 2 ), sizeof(char) );
+
+ int epos = 0;
+ for(pos = 0; _cUnescapedString[pos] != '\0'; pos++)
+ {
+ if(_cUnescapedString[pos] == '"')
+ {
+ _cEscapedString[epos] = '\\';
+ epos++;
+ _cEscapedString[epos] = _cUnescapedString[pos];
+ }
+ else
+ _cEscapedString[epos] = _cUnescapedString[pos];
+ epos++;
+ }
+ epos++;
+ _cEscapedString[epos] = '\0';
+ return _cEscapedString;
+}
+
+static void file_progress_bar ( char * _cDest, int _iBarLength, long _lProgress, long _lTotal ) +static void file_progress_bar ( char * _cDest, int _iBarLength, long _lProgress, long _lTotal )
+{ +{
+ double dPercent = (double) _lProgress / (double) _lTotal * 100.f; + double dPercent = (double) _lProgress / (double) _lTotal * 100.f;
@@ -72,7 +103,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
/* Initial size of the cp.dest_info hash table. */ /* Initial size of the cp.dest_info hash table. */
#define DEST_INFO_INITIAL_CAPACITY 61 #define DEST_INFO_INITIAL_CAPACITY 61
@@ -299,10 +363,11 @@ @@ -299,10 +394,11 @@
bytes read. */ bytes read. */
static bool static bool
sparse_copy (int src_fd, int dest_fd, char *buf, size_t buf_size, sparse_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
@@ -86,7 +117,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
{ {
*last_write_made_hole = false; *last_write_made_hole = false;
*total_n_read = 0; *total_n_read = 0;
@@ -362,6 +427,85 @@ @@ -362,6 +458,85 @@
while (max_n_read) while (max_n_read)
{ {
@@ -172,7 +203,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
ssize_t n_read = read (src_fd, buf, MIN (max_n_read, buf_size)); ssize_t n_read = read (src_fd, buf, MIN (max_n_read, buf_size));
if (n_read < 0) if (n_read < 0)
{ {
@@ -446,6 +590,14 @@ @@ -446,6 +621,14 @@
certain files in /proc or /sys with linux kernels. */ certain files in /proc or /sys with linux kernels. */
} }
@@ -187,7 +218,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
/* Ensure a trailing hole is created, so that subsequent /* Ensure a trailing hole is created, so that subsequent
calls of sparse_copy() start at the correct offset. */ calls of sparse_copy() start at the correct offset. */
if (make_hole && ! create_hole (dest_fd, dst_name, punch_holes, psize)) if (make_hole && ! create_hole (dest_fd, dst_name, punch_holes, psize))
@@ -516,9 +668,11 @@ @@ -516,9 +699,11 @@
static bool static bool
lseek_copy (int src_fd, int dest_fd, char *buf, size_t buf_size, lseek_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
size_t hole_size, off_t ext_start, off_t src_total_size, size_t hole_size, off_t ext_start, off_t src_total_size,
@@ -201,7 +232,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
{ {
off_t last_ext_start = 0; off_t last_ext_start = 0;
off_t last_ext_len = 0; off_t last_ext_len = 0;
@@ -590,10 +744,16 @@ @@ -590,10 +775,16 @@
is conservative and may miss some holes. */ is conservative and may miss some holes. */
off_t n_read; off_t n_read;
bool read_hole; bool read_hole;
@@ -220,7 +251,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
return false; return false;
dest_pos = ext_start + n_read; dest_pos = ext_start + n_read;
@@ -1374,8 +1534,74 @@ @@ -1374,8 +1565,74 @@
buf_alloc = xmalloc (buf_size + buf_alignment); buf_alloc = xmalloc (buf_size + buf_alignment);
buf = ptr_align (buf_alloc, buf_alignment); buf = ptr_align (buf_alloc, buf_alignment);
@@ -295,7 +326,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
if (! ( if (! (
#ifdef SEEK_HOLE #ifdef SEEK_HOLE
scantype == LSEEK_SCANTYPE scantype == LSEEK_SCANTYPE
@@ -1383,15 +1609,17 @@ @@ -1383,15 +1640,17 @@
scan_inference.ext_start, src_open_sb.st_size, scan_inference.ext_start, src_open_sb.st_size,
make_holes ? x->sparse_mode : SPARSE_NEVER, make_holes ? x->sparse_mode : SPARSE_NEVER,
x->reflink_mode != REFLINK_NEVER, x->reflink_mode != REFLINK_NEVER,
@@ -316,7 +347,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
{ {
return_val = false; return_val = false;
goto close_src_and_dst_desc; goto close_src_and_dst_desc;
@@ -1402,6 +1630,14 @@ @@ -1402,6 +1661,14 @@
return_val = false; return_val = false;
goto close_src_and_dst_desc; goto close_src_and_dst_desc;
} }
@@ -333,7 +364,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
if (x->preserve_timestamps) if (x->preserve_timestamps)
diff -aur coreutils-9.0/src/copy.h coreutils-9.0-patched/src/copy.h diff -aur coreutils-9.0/src/copy.h coreutils-9.0-patched/src/copy.h
--- coreutils-9.0/src/copy.h 2021-09-24 13:31:05.000000000 +0200 --- coreutils-9.0/src/copy.h 2021-09-24 13:31:05.000000000 +0200
+++ coreutils-9.0-patched/src/copy.h 2021-12-22 13:54:36.317440119 +0100 +++ coreutils-9.0-patched/src/copy.h 2021-12-22 15:34:28.162542540 +0100
@@ -236,6 +236,9 @@ @@ -236,6 +236,9 @@
Create destination directories as usual. */ Create destination directories as usual. */
bool symbolic_link; bool symbolic_link;
@@ -344,11 +375,13 @@ diff -aur coreutils-9.0/src/copy.h coreutils-9.0-patched/src/copy.h
/* If true, do not copy a nondirectory that has an existing destination /* If true, do not copy a nondirectory that has an existing destination
with the same or newer modification time. */ with the same or newer modification time. */
bool update; bool update;
@@ -308,4 +311,15 @@ @@ -308,4 +311,17 @@
bool chown_failure_ok (struct cp_options const *) _GL_ATTRIBUTE_PURE; bool chown_failure_ok (struct cp_options const *) _GL_ATTRIBUTE_PURE;
mode_t cached_umask (void); mode_t cached_umask (void);
+/* BEGIN progress mod */ +/* BEGIN progress mod */
+char * escape_double_quotes ( char * _cUnescapedString );
+
+int file_size_format ( char * _cDst, long _lSize, int _iCounter ); +int file_size_format ( char * _cDst, long _lSize, int _iCounter );
+ +
+__attribute__((__common__)) long g_iTotalSize; +__attribute__((__common__)) long g_iTotalSize;
@@ -362,7 +395,7 @@ diff -aur coreutils-9.0/src/copy.h coreutils-9.0-patched/src/copy.h
#endif #endif
diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
--- coreutils-9.0/src/cp.c 2021-09-24 13:31:05.000000000 +0200 --- coreutils-9.0/src/cp.c 2021-09-24 13:31:05.000000000 +0200
+++ coreutils-9.0-patched/src/cp.c 2021-12-22 14:02:52.053943727 +0100 +++ coreutils-9.0-patched/src/cp.c 2021-12-22 18:28:14.781215839 +0100
@@ -131,6 +131,7 @@ @@ -131,6 +131,7 @@
{"symbolic-link", no_argument, NULL, 's'}, {"symbolic-link", no_argument, NULL, 's'},
{"target-directory", required_argument, NULL, 't'}, {"target-directory", required_argument, NULL, 't'},
@@ -381,7 +414,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
-i, --interactive prompt before overwrite (overrides a previous -n\ -i, --interactive prompt before overwrite (overrides a previous -n\
\n\ \n\
option)\n\ option)\n\
@@ -634,6 +638,70 @@ @@ -634,6 +638,73 @@
die (EXIT_FAILURE, 0, _("target %s is not a directory"), die (EXIT_FAILURE, 0, _("target %s is not a directory"),
quoteaf (file[n_files - 1])); quoteaf (file[n_files - 1]));
} }
@@ -407,7 +440,9 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
+ +
+ /* how many files are we copying */ + /* how many files are we copying */
+ char command[1024]; + char command[1024];
+ sprintf( command, "find \"%s\" -type f | wc -l", file[0]); + char * _cEscapedString = escape_double_quotes( file[0] );
+ sprintf( command, "find \"%s\" -type f | wc -l", _cEscapedString );
+ free( _cEscapedString );
+ FILE *fp ; + FILE *fp ;
+ char output[1024]; + char output[1024];
+ fp = popen(command,"r"); + fp = popen(command,"r");
@@ -421,8 +456,9 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
+ /* call du -s for each file */ + /* call du -s for each file */
+ /* create command */ + /* create command */
+ char command[1024]; + char command[1024];
+ sprintf ( command, "du -s \"%s\"", file[j] ); + char * _cEscapedString = escape_double_quotes( file[j] );
+ /* TODO: replace all quote signs in file[i] */ + sprintf ( command, "du -s \"%s\"", _cEscapedString );
+ free( _cEscapedString );
+ +
+ FILE *fp; + FILE *fp;
+ char output[1024]; + char output[1024];
@@ -452,7 +488,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
if (target_directory) if (target_directory)
{ {
@@ -781,6 +849,46 @@ @@ -781,6 +852,46 @@
ok = copy (source, new_dest, 0, x, &unused, NULL); ok = copy (source, new_dest, 0, x, &unused, NULL);
} }
@@ -499,7 +535,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
return ok; return ok;
} }
@@ -816,6 +924,7 @@ @@ -816,6 +927,7 @@
x->recursive = false; x->recursive = false;
x->sparse_mode = SPARSE_AUTO; x->sparse_mode = SPARSE_AUTO;
x->symbolic_link = false; x->symbolic_link = false;
@@ -507,7 +543,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
x->set_mode = false; x->set_mode = false;
x->mode = 0; x->mode = 0;
@@ -954,7 +1063,7 @@ @@ -954,7 +1066,7 @@
selinux_enabled = (0 < is_selinux_enabled ()); selinux_enabled = (0 < is_selinux_enabled ());
cp_option_init (&x); cp_option_init (&x);
@@ -516,7 +552,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
long_opts, NULL)) long_opts, NULL))
!= -1) != -1)
{ {
@@ -1011,6 +1120,10 @@ @@ -1011,6 +1123,10 @@
x.unlink_dest_after_failed_open = true; x.unlink_dest_after_failed_open = true;
break; break;
@@ -527,7 +563,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
case 'H': case 'H':
x.dereference = DEREF_COMMAND_LINE_ARGUMENTS; x.dereference = DEREF_COMMAND_LINE_ARGUMENTS;
break; break;
@@ -1171,6 +1284,9 @@ @@ -1171,6 +1287,9 @@
usage (EXIT_FAILURE); usage (EXIT_FAILURE);
} }
@@ -539,7 +575,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
version_control_string) version_control_string)
diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c
--- coreutils-9.0/src/mv.c 2021-09-24 13:31:05.000000000 +0200 --- coreutils-9.0/src/mv.c 2021-09-24 13:31:05.000000000 +0200
+++ coreutils-9.0-patched/src/mv.c 2021-12-22 14:16:55.425936987 +0100 +++ coreutils-9.0-patched/src/mv.c 2021-12-22 18:29:46.254836707 +0100
@@ -66,6 +66,7 @@ @@ -66,6 +66,7 @@
{"target-directory", required_argument, NULL, 't'}, {"target-directory", required_argument, NULL, 't'},
{"update", no_argument, NULL, 'u'}, {"update", no_argument, NULL, 'u'},
@@ -548,7 +584,7 @@ diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c
{GETOPT_HELP_OPTION_DECL}, {GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL}, {GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
@@ -168,10 +169,86 @@ @@ -168,10 +169,87 @@
static bool static bool
do_move (char const *source, char const *dest, const struct cp_options *x) do_move (char const *source, char const *dest, const struct cp_options *x)
{ {
@@ -572,8 +608,9 @@ diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c
+ /* call du -s for each file */ + /* call du -s for each file */
+ /* create command */ + /* create command */
+ char command[1024]; + char command[1024];
+ sprintf ( command, "du -s \"%s\"", source ); + char * _cEscapedString = escape_double_quotes( (unsigned char *)(size_t)source );
+ /* TODO: replace all quote signs in file[i] */ + sprintf ( command, "du -s \"%s\"", _cEscapedString );
+ free( _cEscapedString );
+ +
+ FILE *fp; + FILE *fp;
+ char output[1024]; + char output[1024];
@@ -635,7 +672,7 @@ diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c
if (ok) if (ok)
{ {
char const *dir_to_remove; char const *dir_to_remove;
@@ -306,6 +383,7 @@ @@ -306,6 +384,7 @@
\n\ \n\
-b like --backup but does not accept an argument\n\ -b like --backup but does not accept an argument\n\
-f, --force do not prompt before overwriting\n\ -f, --force do not prompt before overwriting\n\
@@ -643,7 +680,7 @@ diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c
-i, --interactive prompt before overwrite\n\ -i, --interactive prompt before overwrite\n\
-n, --no-clobber do not overwrite an existing file\n\ -n, --no-clobber do not overwrite an existing file\n\
If you specify more than one of -i, -f, -n, only the final one takes effect.\n\ If you specify more than one of -i, -f, -n, only the final one takes effect.\n\
@@ -361,7 +439,7 @@ @@ -361,7 +440,7 @@
/* Try to disable the ability to unlink a directory. */ /* Try to disable the ability to unlink a directory. */
priv_set_remove_linkdir (); priv_set_remove_linkdir ();
@@ -652,7 +689,7 @@ diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c
!= -1) != -1)
{ {
switch (c) switch (c)
@@ -407,6 +485,9 @@ @@ -407,6 +486,9 @@
case 'v': case 'v':
x.verbose = true; x.verbose = true;
break; break;