use function to also calculate and format elapsed time

This commit is contained in:
Michael Wehr
2022-01-09 21:15:26 +01:00
parent d09e9fb8b6
commit ae89bfdb24

View File

@@ -1,6 +1,6 @@
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-patched/src/copy.c 2022-01-04 21:56:14.549035719 +0100
+++ coreutils-9.0-patched/src/copy.c 2022-01-09 21:12:20.602444195 +0100
@@ -129,6 +129,119 @@
dev_t dev;
};
@@ -39,28 +39,28 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
+ return _cEscapedString;
+}
+
+char * format_elapsed_time ( double sec_elapsed )
+char * format_time ( double seconds, bool showall )
+{
+ // hours
+ long hr = ( (int) sec_elapsed / (60 * 60)) % 24;
+ int hr = ( (int) seconds / (60 * 60)) % 24;
+ // minutes
+ int min = ( (int) sec_elapsed / 60) % 60;
+ int min = ( (int) seconds / 60) % 60;
+ // seconds
+ double sec = sec_elapsed - (hr * (60 * 60)) - (min * 60);
+ size_t maxneeded = snprintf(NULL, 0, "%ldh %dm %.1fs", hr, min, sec) + 1;
+ char * _cElapsedTimeString;
+ _cElapsedTimeString = (char *) calloc( sizeof(char) * maxneeded, sizeof(char) );
+ if ( sec_elapsed >= 3600 )
+ 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 ( seconds >= 3600 || showall)
+ {
+ sprintf(_cElapsedTimeString, "%ldh %dm %.1fs", hr, min, sec);
+ } else if ( sec_elapsed >= 60 )
+ sprintf(_cTimeString, "%2dh %2dm %4.1fs", hr, min, sec);
+ } else if ( seconds >= 60 )
+ {
+ sprintf(_cElapsedTimeString, "%dm %.1fs", min, sec);
+ sprintf(_cTimeString, "%2dm %4.1fs", min, sec);
+ } else
+ {
+ sprintf(_cElapsedTimeString, "%.1fs", sec);
+ sprintf(_cTimeString, "%4.1fs", sec);
+ }
+ return _cElapsedTimeString;
+ return _cTimeString;
+}
+
+static void file_progress_bar ( char * _cDest, int _iBarLength, long _lProgress, long _lTotal )
@@ -139,7 +139,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
{
*last_write_made_hole = false;
*total_n_read = 0;
@@ -362,6 +482,85 @@
@@ -362,6 +482,81 @@
while (max_n_read)
{
@@ -174,16 +174,12 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
+ int isec_elapsed = cur_time.tv_sec - g_oStartTime.tv_sec;
+ int sec_remaining = ( int ) ( ( double ) isec_elapsed / cur_size
+ * g_iTotalSize ) - isec_elapsed;
+ int min_remaining = sec_remaining / 60;
+ sec_remaining -= min_remaining * 60;
+ int hours_remaining = min_remaining / 60;
+ min_remaining -= hours_remaining * 60;
+ /* print out */
+ sprintf ( s_progress->cProgressField[3],
+ move_mode
+ ? "moving at %s/s (about %uh %um %us remaining)"
+ : "copying at %s/s (about %uh %um %us remaining)", s_copy_speed,
+ hours_remaining, min_remaining, sec_remaining );
+ ? "moving at %s/s (about %s remaining)"
+ : "copying at %s/s (about %s remaining)", s_copy_speed,
+ format_time(sec_remaining, true) );
+
+ int fs_len;
+ if ( g_iTotalFiles > 1 )
@@ -225,7 +221,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));
if (n_read < 0)
{
@@ -446,6 +645,14 @@
@@ -446,6 +641,14 @@
certain files in /proc or /sys with linux kernels. */
}
@@ -240,7 +236,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
calls of sparse_copy() start at the correct offset. */
if (make_hole && ! create_hole (dest_fd, dst_name, punch_holes, psize))
@@ -517,8 +724,16 @@
@@ -517,8 +720,16 @@
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,
enum Sparse_type sparse_mode,
@@ -258,7 +254,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_len = 0;
@@ -590,10 +805,26 @@
@@ -590,10 +801,26 @@
is conservative and may miss some holes. */
off_t n_read;
bool read_hole;
@@ -287,7 +283,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
return false;
dest_pos = ext_start + n_read;
@@ -1374,8 +1605,82 @@
@@ -1374,8 +1601,82 @@
buf_alloc = xmalloc (buf_size + buf_alignment);
buf = ptr_align (buf_alloc, buf_alignment);
@@ -370,7 +366,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
if (! (
#ifdef SEEK_HOLE
scantype == LSEEK_SCANTYPE
@@ -1383,15 +1688,30 @@
@@ -1383,15 +1684,30 @@
scan_inference.ext_start, src_open_sb.st_size,
make_holes ? x->sparse_mode : SPARSE_NEVER,
x->reflink_mode != REFLINK_NEVER,
@@ -403,7 +399,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
{
return_val = false;
goto close_src_and_dst_desc;
@@ -1402,6 +1722,14 @@
@@ -1402,6 +1718,14 @@
return_val = false;
goto close_src_and_dst_desc;
}
@@ -420,7 +416,7 @@ diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c
if (x->preserve_timestamps)
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-patched/src/copy.h 2022-01-04 21:56:14.553035790 +0100
+++ coreutils-9.0-patched/src/copy.h 2022-01-09 21:12:20.602444195 +0100
@@ -236,6 +236,11 @@
Create destination directories as usual. */
bool symbolic_link;
@@ -439,7 +435,7 @@ diff -aur coreutils-9.0/src/copy.h coreutils-9.0-patched/src/copy.h
+/* BEGIN progress mod */
+char * escape_double_quotes ( char * _cUnescapedString );
+char * format_elapsed_time ( double sec_elapsed );
+char * format_time ( double seconds, bool showall );
+
+int file_size_format ( char * _cDst, long _lSize, int _iCounter );
+
@@ -455,7 +451,7 @@ diff -aur coreutils-9.0/src/copy.h coreutils-9.0-patched/src/copy.h
#endif
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-patched/src/cp.c 2022-01-04 21:56:14.553035790 +0100
+++ coreutils-9.0-patched/src/cp.c 2022-01-09 21:12:20.606444248 +0100
@@ -131,6 +131,9 @@
{"symbolic-link", no_argument, NULL, 's'},
{"target-directory", required_argument, NULL, 't'},
@@ -611,7 +607,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
+ else
+ sprintf ( sFType, "%s", "file(s)" );
+ printf ( "%d %s (%s) copied in %s (%s/s).\n", g_iFilesCopied, sFType,
+ sTotalWritten, format_elapsed_time(sec_elapsed), s_copy_speed );
+ sTotalWritten, format_time(sec_elapsed, false), s_copy_speed );
+ }
+ /* END progress mod */
+
@@ -667,7 +663,7 @@ diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c
version_control_string)
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-patched/src/mv.c 2022-01-04 21:56:14.553035790 +0100
+++ coreutils-9.0-patched/src/mv.c 2022-01-09 21:12:20.606444248 +0100
@@ -66,6 +66,9 @@
{"target-directory", required_argument, NULL, 't'},
{"update", no_argument, NULL, 'u'},
@@ -792,7 +788,7 @@ diff -aur coreutils-9.0/src/mv.c coreutils-9.0-patched/src/mv.c
+ else
+ sprintf ( sFType, "%s", "file(s)" );
+ printf ( "%d %s (%s) moved in %s (%s/s).\n", g_iFilesCopied, sFType,
+ sTotalWritten, format_elapsed_time(sec_elapsed), s_copy_speed );
+ sTotalWritten, format_time(sec_elapsed, false), s_copy_speed );
+ }
+ }
+ /* END progress mod */