refactor format_time function and calls to fix memory leak

This commit is contained in:
Michael Wehr
2022-01-11 22:11:46 +01:00
parent 1e12a7c529
commit 30b6aff569

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 2022-01-11 20:51:54.334645363 +0100 +++ coreutils-9.0-patched/src/copy.c 2022-01-11 22:10:44.509858035 +0100
@@ -129,6 +129,125 @@ @@ -129,6 +129,121 @@
dev_t dev; dev_t dev;
}; };
@@ -39,7 +39,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
+ return _cEscapedString; + return _cEscapedString;
+} +}
+ +
+char * format_time ( double seconds, bool showall ) +void format_time ( char * _cDest, double seconds, bool showall )
+{ +{
+ // hours + // hours
+ int hr = ( (int) seconds / (60 * 60)) % 24; + int hr = ( (int) seconds / (60 * 60)) % 24;
@@ -47,26 +47,22 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
+ int min = ( (int) seconds / 60) % 60; + int min = ( (int) seconds / 60) % 60;
+ // seconds + // seconds
+ double sec = seconds - (hr * (60 * 60)) - (min * 60); + double sec = seconds - (hr * (60 * 60)) - (min * 60);
+ size_t maxneeded = snprintf(NULL, 0, "%2dh %2dm %4.1fs", hr, min, sec) + 1;
+ char * _cTimeString;
+ _cTimeString = (char *) calloc( sizeof(char) * maxneeded, sizeof(char) );
+ if ( showall ) + if ( showall )
+ { + {
+ if ( seconds < 0 ) + if ( seconds < 0 )
+ sprintf(_cTimeString, "%2ch %2cm %2cs", '0', '0', '?'); + sprintf(_cDest, "%2ch %2cm %2cs", '0', '0', '?');
+ else + else
+ sprintf(_cTimeString, "%2dh %2dm %2ds", hr, min, (int) sec); + sprintf(_cDest, "%2dh %2dm %2ds", hr, min, (int) sec);
+ } else if ( seconds >= 3600 ) + } else if ( seconds >= 3600 )
+ { + {
+ sprintf(_cTimeString, "%2dh %2dm %4.1fs", hr, min, sec); + sprintf(_cDest, "%2dh %2dm %4.1fs", hr, min, sec);
+ } else if ( seconds >= 60 ) + } else if ( seconds >= 60 )
+ { + {
+ sprintf(_cTimeString, "%2dm %4.1fs", min, sec); + sprintf(_cDest, "%2dm %4.1fs", min, sec);
+ } else + } else
+ { + {
+ sprintf(_cTimeString, "%4.1fs", sec); + sprintf(_cDest, "%4.1fs", sec);
+ } + }
+ return _cTimeString;
+} +}
+ +
+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 )
@@ -127,7 +123,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
@@ -300,13 +419,28 @@ @@ -300,13 +415,28 @@
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,
size_t hole_size, bool punch_holes, bool allow_reflink, size_t hole_size, bool punch_holes, bool allow_reflink,
@@ -157,7 +153,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
/* If not looking for holes, use copy_file_range if functional, /* If not looking for holes, use copy_file_range if functional,
but don't use if reflink disallowed as that may be implicit. */ but don't use if reflink disallowed as that may be implicit. */
if ((! hole_size) && allow_reflink && functional_copy_file_range ()) if ((! hole_size) && allow_reflink && functional_copy_file_range ())
@@ -362,6 +496,97 @@ @@ -362,6 +492,103 @@
while (max_n_read) while (max_n_read)
{ {
@@ -198,17 +194,23 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
+ * g_iFTotalSize ) - isec_felapsed; + * g_iFTotalSize ) - isec_felapsed;
+ /* print out */ + /* print out */
+ +
+ char f_ttime[20];
+ char f_ftime[20];
+ format_time(f_ttime, sec_remaining, true);
+ format_time(f_ftime, sec_fremaining, true);
+
+ sprintf ( s_progress->cProgressField[1], + sprintf ( s_progress->cProgressField[1],
+ move_mode + move_mode
+ ? "%d of %d files moved (about %s remaining) " + ? "%d of %d files moved (about %s remaining) "
+ : "%d of %d files copied (about %s remaining) ", + : "%d of %d files copied (about %s remaining) ",
+ g_iFilesCopied, g_iTotalFiles, format_time(sec_remaining, true) ); + g_iFilesCopied, g_iTotalFiles, f_ftime );
+
+ char s_ftime[40] = "";
+ +
+ char s_ftime[100] = "";
+ if (g_iTotalFiles > 1) + if (g_iTotalFiles > 1)
+ sprintf ( s_ftime, "(about %s remaining)", format_time(sec_fremaining, true) ); + sprintf ( s_ftime, "(about %s remaining)", f_ftime );
+ else + else
+ sprintf ( s_ftime, "(about %s remaining)", format_time(sec_remaining, true) ); + sprintf ( s_ftime, "(about %s remaining)", f_ttime );
+ +
+ sprintf ( s_progress->cProgressField[3], + sprintf ( s_progress->cProgressField[3],
+ move_mode + move_mode
@@ -255,7 +257,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 +671,14 @@ @@ -446,6 +673,14 @@
certain files in /proc or /sys with linux kernels. */ certain files in /proc or /sys with linux kernels. */
} }
@@ -270,7 +272,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))
@@ -517,8 +750,16 @@ @@ -517,8 +752,16 @@
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,
enum Sparse_type sparse_mode, enum Sparse_type sparse_mode,
@@ -288,7 +290,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 +831,26 @@ @@ -590,10 +833,26 @@
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;
@@ -317,7 +319,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 +1631,82 @@ @@ -1374,8 +1633,82 @@
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);
@@ -400,7 +402,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 +1714,30 @@ @@ -1383,15 +1716,30 @@
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,
@@ -433,7 +435,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 +1748,14 @@ @@ -1402,6 +1750,14 @@
return_val = false; return_val = false;
goto close_src_and_dst_desc; goto close_src_and_dst_desc;
} }
@@ -450,7 +452,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 2022-01-11 20:51:54.334645363 +0100 +++ coreutils-9.0-patched/src/copy.h 2022-01-11 22:10:44.509858035 +0100
@@ -236,6 +236,11 @@ @@ -236,6 +236,11 @@
Create destination directories as usual. */ Create destination directories as usual. */
bool symbolic_link; bool symbolic_link;
@@ -469,7 +471,7 @@ diff -aur coreutils-9.0/src/copy.h coreutils-9.0-patched/src/copy.h
+/* BEGIN progress mod */ +/* BEGIN progress mod */
+char * escape_double_quotes ( char * _cUnescapedString ); +char * escape_double_quotes ( char * _cUnescapedString );
+char * format_time ( double seconds, bool showall ); +void format_time ( char * _cDst, double seconds, bool showall );
+ +
+int file_size_format ( char * _cDst, long _lSize, int _iCounter ); +int file_size_format ( char * _cDst, long _lSize, int _iCounter );
+ +
@@ -488,7 +490,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 2022-01-11 20:51:54.338645406 +0100 +++ coreutils-9.0-patched/src/cp.c 2022-01-11 22:10:44.513858084 +0100
@@ -131,6 +131,9 @@ @@ -131,6 +131,9 @@
{"symbolic-link", no_argument, NULL, 's'}, {"symbolic-link", no_argument, NULL, 's'},
{"target-directory", required_argument, NULL, 't'}, {"target-directory", required_argument, NULL, 't'},
@@ -597,7 +599,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
if (target_directory) if (target_directory)
{ {
@@ -781,6 +868,53 @@ @@ -781,6 +868,56 @@
ok = copy (source, new_dest, 0, x, &unused, NULL); ok = copy (source, new_dest, 0, x, &unused, NULL);
} }
@@ -643,15 +645,18 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
+ sprintf ( sFType, "%s", "folder(s)/file(s)" ); + sprintf ( sFType, "%s", "folder(s)/file(s)" );
+ else + else
+ sprintf ( sFType, "%s", "file(s)" ); + sprintf ( sFType, "%s", "file(s)" );
+
+ char f_time[20];
+ format_time(f_time, sec_elapsed, true);
+ printf ( "%d %s (%s) copied in %s (%s/s).\n", g_iFilesCopied, sFType, + printf ( "%d %s (%s) copied in %s (%s/s).\n", g_iFilesCopied, sFType,
+ sTotalWritten, format_time(sec_elapsed, false), s_copy_speed ); + sTotalWritten, f_time, s_copy_speed );
+ } + }
+ /* END progress mod */ + /* END progress mod */
+ +
return ok; return ok;
} }
@@ -816,6 +950,11 @@ @@ -816,6 +953,11 @@
x->recursive = false; x->recursive = false;
x->sparse_mode = SPARSE_AUTO; x->sparse_mode = SPARSE_AUTO;
x->symbolic_link = false; x->symbolic_link = false;
@@ -663,7 +668,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 +1093,8 @@ @@ -954,7 +1096,8 @@
selinux_enabled = (0 < is_selinux_enabled ()); selinux_enabled = (0 < is_selinux_enabled ());
cp_option_init (&x); cp_option_init (&x);
@@ -673,7 +678,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 +1151,12 @@ @@ -1011,6 +1154,12 @@
x.unlink_dest_after_failed_open = true; x.unlink_dest_after_failed_open = true;
break; break;
@@ -686,7 +691,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 +1317,11 @@ @@ -1171,6 +1320,11 @@
usage (EXIT_FAILURE); usage (EXIT_FAILURE);
} }
@@ -700,7 +705,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 2022-01-11 20:51:54.338645406 +0100 +++ coreutils-9.0-patched/src/mv.c 2022-01-11 22:10:44.513858084 +0100
@@ -66,6 +66,9 @@ @@ -66,6 +66,9 @@
{"target-directory", required_argument, NULL, 't'}, {"target-directory", required_argument, NULL, 't'},
{"update", no_argument, NULL, 'u'}, {"update", no_argument, NULL, 'u'},
@@ -711,7 +716,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}
@@ -170,8 +173,121 @@ @@ -170,8 +173,124 @@
{ {
bool copy_into_self; bool copy_into_self;
bool rename_succeeded; bool rename_succeeded;
@@ -824,8 +829,11 @@ diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c
+ sprintf ( sFType, "%s", "folder(s)/file(s)" ); + sprintf ( sFType, "%s", "folder(s)/file(s)" );
+ else + else
+ sprintf ( sFType, "%s", "file(s)" ); + sprintf ( sFType, "%s", "file(s)" );
+
+ char f_time[20];
+ format_time(f_time, sec_elapsed, true);
+ printf ( "%d %s (%s) moved in %s (%s/s).\n", g_iFilesCopied, sFType, + printf ( "%d %s (%s) moved in %s (%s/s).\n", g_iFilesCopied, sFType,
+ sTotalWritten, format_time(sec_elapsed, false), s_copy_speed ); + sTotalWritten, f_time, s_copy_speed );
+ } + }
+ } + }
+ /* END progress mod */ + /* END progress mod */
@@ -833,7 +841,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 +422,11 @@ @@ -306,6 +425,11 @@
\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\
@@ -845,7 +853,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 +482,8 @@ @@ -361,7 +485,8 @@
/* 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 ();
@@ -855,7 +863,7 @@ diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c
!= -1) != -1)
{ {
switch (c) switch (c)
@@ -407,6 +529,11 @@ @@ -407,6 +532,11 @@
case 'v': case 'v':
x.verbose = true; x.verbose = true;
break; break;