Files
advcpmv/advcpmv-0.8-8.32.patch
2022-01-04 22:00:35 +01:00

850 lines
29 KiB
Diff

diff -aur coreutils-8.32/src/copy.c coreutils-8.32-patched/src/copy.c
--- coreutils-8.32/src/copy.c 2020-01-01 15:13:12.000000000 +0100
+++ coreutils-8.32-patched/src/copy.c 2022-01-04 21:56:14.545035647 +0100
@@ -129,6 +129,119 @@
dev_t dev;
};
+/* BEGIN progress mod */
+struct progress_status {
+ int iCountDown;
+ char ** cProgressField;
+ struct timeval last_time;
+ int last_size, iBarLength;
+ struct stat src_open_sb;
+};
+
+char * escape_double_quotes ( char * _cUnescapedString )
+{
+ char * _cEscapedString;
+ _cEscapedString = (char *) calloc( sizeof(char) * strlen(_cUnescapedString) * 2, sizeof(char) );
+ size_t ipos = 0;
+ size_t rpos = 0;
+ for(rpos = 0, ipos = 0; _cUnescapedString[rpos] != '\0'; rpos++, ipos++)
+ {
+ if(_cUnescapedString[rpos] == '"')
+ {
+ _cEscapedString[ipos] = '\\';
+ ipos++;
+ }
+ if(_cUnescapedString[rpos] == '`')
+ {
+ _cEscapedString[ipos] = '\\';
+ ipos++;
+ }
+ _cEscapedString[ipos] = _cUnescapedString[rpos];
+ }
+ ipos++;
+ _cEscapedString[ipos] = '\0';
+ return _cEscapedString;
+}
+
+char * format_elapsed_time ( double sec_elapsed )
+{
+ // hours
+ long hr = ( (int) sec_elapsed / (60 * 60)) % 24;
+ // minutes
+ int min = ( (int) sec_elapsed / 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 )
+ {
+ sprintf(_cElapsedTimeString, "%ldh %dm %.1fs", hr, min, sec);
+ } else if ( sec_elapsed >= 60 )
+ {
+ sprintf(_cElapsedTimeString, "%dm %.1fs", min, sec);
+ } else
+ {
+ sprintf(_cElapsedTimeString, "%.1fs", sec);
+ }
+ return _cElapsedTimeString;
+}
+
+static void file_progress_bar ( char * _cDest, int _iBarLength, long _lProgress, long _lTotal )
+{
+ double dPercent = (double) _lProgress / (double) _lTotal * 100.f;
+ sprintf( _cDest + ( _iBarLength - 6), "%4.1f", dPercent );
+ _cDest[_iBarLength - 2] = ' ';
+
+ int i;
+ for ( i=1; i<=_iBarLength - 9; i++)
+ {
+ if ( dPercent > (double) (i-1) / (_iBarLength - 10) * 100.f )
+ {
+ _cDest[i] = '=';
+ }
+ else
+ {
+ _cDest[i] = ' ';
+ }
+ }
+ for ( i=1; i<_iBarLength - 9; i++)
+ {
+ if ( ( _cDest[i+1] == ' ' ) && ( _cDest[i] == '=' ) )
+ _cDest[i] = '>' ;
+ }
+}
+
+int file_size_format ( char * _cDst, long _lSize, int _iCounter )
+{
+ int iCounter = _iCounter;
+ double dSize = ( double ) _lSize;
+ while ( dSize >= 1000. )
+ {
+ dSize /= 1024.;
+ iCounter++;
+ }
+
+ /* get unit */
+ char * sUnit;
+ if ( iCounter == 0 )
+ sUnit = "B";
+ else if ( iCounter == 1 )
+ sUnit = "KiB";
+ else if ( iCounter == 2 )
+ sUnit = "MiB";
+ else if ( iCounter == 3 )
+ sUnit = "GiB";
+ else if ( iCounter == 4 )
+ sUnit = "TiB";
+ else
+ sUnit = "N/A";
+
+ /* write number */
+ return sprintf ( _cDst, "%5.1f %s", dSize, sUnit );
+}
+/* END progress mod */
+
/* Initial size of the cp.dest_info hash table. */
#define DEST_INFO_INITIAL_CAPACITY 61
@@ -259,9 +372,16 @@
static bool
sparse_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
size_t hole_size, bool punch_holes,
+ /* BEGIN progress mod */
+ bool move_mode,
+ /* END progress mod */
char const *src_name, char const *dst_name,
uintmax_t max_n_read, off_t *total_n_read,
- bool *last_write_made_hole)
+ bool *last_write_made_hole
+ /* BEGIN progress mod */
+ , struct progress_status *s_progress
+ /* END progress mod */
+ )
{
*last_write_made_hole = false;
*total_n_read = 0;
@@ -270,6 +390,85 @@
while (max_n_read)
{
+
+ /* BEGIN progress mod */
+ if (progress) {
+ /* update countdown */
+ s_progress->iCountDown--;
+ char * sProgressBar = s_progress->cProgressField[5];
+ if ( s_progress->iCountDown < 0 )
+ s_progress->iCountDown = 100;
+
+ /* just print one line with the percentage, but not always */
+ if ( s_progress->iCountDown == 0 )
+ {
+ /* calculate current speed */
+ struct timeval cur_time;
+ gettimeofday ( & cur_time, NULL );
+ int cur_size = g_iTotalWritten + *total_n_read / 1024;
+ int usec_elapsed = cur_time.tv_usec - s_progress->last_time.tv_usec;
+ double sec_elapsed = ( double ) usec_elapsed / 1000000.f;
+ sec_elapsed += ( double ) ( cur_time.tv_sec - s_progress->last_time.tv_sec );
+ int copy_speed = ( int ) ( ( double ) ( cur_size - s_progress->last_size )
+ / sec_elapsed );
+ char s_copy_speed[20];
+ file_size_format ( s_copy_speed, copy_speed >= 0 ? copy_speed : 0, 1 );
+ /* update vars */
+ s_progress->last_time = cur_time;
+ s_progress->last_size = cur_size;
+
+ /* how many time has passed since the start? */
+ 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 );
+
+ int fs_len;
+ if ( g_iTotalFiles > 1 )
+ {
+ /* global progress bar */
+ file_progress_bar ( s_progress->cProgressField[2], s_progress->iBarLength,
+ g_iTotalWritten + *total_n_read / 1024, g_iTotalSize );
+
+ /* print the global status */
+ fs_len = file_size_format ( s_progress->cProgressField[1] + s_progress->iBarLength - 21,
+ g_iTotalWritten + *total_n_read / 1024, 1 );
+ s_progress->cProgressField[1][s_progress->iBarLength - 21 + fs_len] = ' ';
+ }
+
+ /* current progress bar */
+ file_progress_bar ( sProgressBar, s_progress->iBarLength, *total_n_read, s_progress->src_open_sb.st_size );
+
+ /* print the status */
+ fs_len = file_size_format ( s_progress->cProgressField[4] + s_progress->iBarLength - 21, *total_n_read, 0 );
+ s_progress->cProgressField[4][s_progress->iBarLength - 21 + fs_len] = ' ';
+
+ /* print the field */
+ int it;
+ for ( it = g_iTotalFiles>1 ? 0 : 3; it < 6; it++ )
+ {
+ printf ( "\033[K%s\n", s_progress->cProgressField[it] );
+ if ( strlen ( s_progress->cProgressField[it] ) < s_progress->iBarLength )
+ printf ( "%s", "" );
+ }
+ if ( g_iTotalFiles > 1 )
+ printf ( "\r\033[6A" );
+ else
+ printf ( "\r\033[3A" );
+ fflush ( stdout );
+ }
+ }
+ /* END progress mod */
+
ssize_t n_read = read (src_fd, buf, MIN (max_n_read, buf_size));
if (n_read < 0)
{
@@ -354,6 +553,14 @@
certain files in /proc or /sys with linux kernels. */
}
+ /* BEGIN progress mod */
+ if (progress) {
+ /* update total size */
+ g_iTotalWritten += *total_n_read / 1024;
+ g_iFilesCopied++;
+ }
+ /* END progress mod */
+
/* 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))
@@ -421,8 +628,16 @@
extent_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
size_t hole_size, off_t src_total_size,
enum Sparse_type sparse_mode,
+ /* BEGIN progress mod */
+ bool move_mode,
+ /* END progress mod */
char const *src_name, char const *dst_name,
- bool *require_normal_copy)
+ bool *require_normal_copy
+ /* BEGIN progress mod */
+ , int iCountDown, char ** cProgressField, struct timeval last_time,
+ int last_size, int iBarLength, struct stat src_open_sb
+ /* END progress mod */
+ )
{
struct extent_scan scan;
off_t last_ext_start = 0;
@@ -553,10 +768,26 @@
last_ext_len = ext_len;
bool read_hole;
+ /* BEGIN progress mod */
+ struct timeval a;
+ struct stat b;
+
+ struct progress_status s_progress={iCountDown, cProgressField, last_time, last_size, iBarLength, src_open_sb};
+ /* END progress mod */
+
+
if ( ! sparse_copy (src_fd, dest_fd, buf, buf_size,
sparse_mode == SPARSE_ALWAYS ? hole_size: 0,
- true, src_name, dst_name, ext_len, &n_read,
- &read_hole))
+ true,
+ /* BEGIN progress mod */
+ move_mode,
+ /* END progress mod */
+ src_name, dst_name, ext_len, &n_read,
+ &read_hole
+ /* BEGIN progress mod */
+ ,&s_progress
+ /* END progress mod */
+ ))
goto fail;
dest_pos = ext_start + n_read;
@@ -1305,6 +1536,75 @@
buf_alloc = xmalloc (buf_size + buf_alignment);
buf = ptr_align (buf_alloc, buf_alignment);
+ /* BEGIN progress mod */
+ /* create a field of 6 lines */
+ char ** cProgressField = ( char ** ) calloc ( 6, sizeof ( char * ) );
+ /* get console width */
+ int iBarLength = 80;
+ struct winsize win;
+ if ( ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win) == 0 && win.ws_col > 0 )
+ if (win.ws_col > iBarLength) /* String printed may be longer on smaller screens */
+ iBarLength = win.ws_col;
+ /* create rows */
+ int it;
+ for ( it = 0; it < 6; it++ )
+ {
+ cProgressField[it] = ( char * ) malloc ( iBarLength + 1 );
+ /* init with spaces */
+ int j;
+ for ( j = 0; j < iBarLength; j++ )
+ cProgressField[it][j] = ' ';
+ cProgressField[it][iBarLength] = '\0';
+ }
+
+ /* global progress bar? */
+ if ( g_iTotalFiles > 1 )
+ {
+ /* init global progress bar */
+ cProgressField[2][0] = '[';
+ cProgressField[2][iBarLength - 8] = ']';
+ cProgressField[2][iBarLength - 7] = ' ';
+ cProgressField[2][iBarLength - 1] = '%';
+
+ /* total size */
+ cProgressField[1][iBarLength - 11] = '/';
+ file_size_format ( cProgressField[1] + iBarLength - 9, g_iTotalSize, 1 );
+
+ /* show how many files were written */
+ int sum_length = 0;
+ if ( x->move_mode )
+ sum_length = sprintf ( cProgressField[1], "%d files moved so far...", g_iFilesCopied );
+ else
+ sum_length = sprintf ( cProgressField[1], "%d files copied so far...", g_iFilesCopied );
+ cProgressField[1][sum_length] = ' ';
+ }
+
+ /* truncate filename? */
+ int fn_length;
+ if ( strlen ( src_name ) > iBarLength - 22 )
+ fn_length =
+ sprintf ( cProgressField[4], "...%s", src_name + ( strlen ( src_name ) - iBarLength + 25 ) );
+ else
+ fn_length = sprintf ( cProgressField[4], "%s", src_name );
+ cProgressField[4][fn_length] = ' ';
+
+ /* filesize */
+ cProgressField[4][iBarLength - 11] = '/';
+ file_size_format ( cProgressField[4] + iBarLength - 9, src_open_sb.st_size, 0 );
+
+ int iCountDown = 1;
+ char * sProgressBar = cProgressField[5];
+ sProgressBar[0] = '[';
+ sProgressBar[iBarLength - 8] = ']';
+ sProgressBar[iBarLength - 7] = ' ';
+ sProgressBar[iBarLength - 1] = '%';
+
+ /* this will always save the time in between */
+ struct timeval last_time;
+ gettimeofday ( & last_time, NULL );
+ int last_size = g_iTotalWritten;
+ /* END progress mod */
+
if (sparse_src)
{
bool normal_copy_required;
@@ -1316,7 +1616,15 @@
if (extent_copy (source_desc, dest_desc, buf, buf_size, hole_size,
src_open_sb.st_size,
make_holes ? x->sparse_mode : SPARSE_NEVER,
- src_name, dst_name, &normal_copy_required))
+ /* BEGIN progress mod */
+ x->move_mode,
+ /* END progress mod */
+ src_name, dst_name, &normal_copy_required
+ /* BEGIN progress mod */
+ , iCountDown, cProgressField, last_time, last_size,
+ iBarLength, src_open_sb
+ /* END progress mod */
+ ))
goto preserve_metadata;
if (! normal_copy_required)
@@ -1328,11 +1636,24 @@
off_t n_read;
bool wrote_hole_at_eof;
+
+ /* BEGIN progress mod */
+ struct progress_status s_progress = { iCountDown, cProgressField, last_time, last_size, iBarLength, src_open_sb};
+ /* END progress mod */
+
if (! sparse_copy (source_desc, dest_desc, buf, buf_size,
make_holes ? hole_size : 0,
- x->sparse_mode == SPARSE_ALWAYS, src_name, dst_name,
+ x->sparse_mode == SPARSE_ALWAYS,
+ /* BEGIN progress mod */
+ x->move_mode,
+ /* END progress mod */
+ src_name, dst_name,
UINTMAX_MAX, &n_read,
- &wrote_hole_at_eof))
+ &wrote_hole_at_eof
+ /* BEGIN progress mod */
+ , &s_progress
+ /* END progress mod */
+ ))
{
return_val = false;
goto close_src_and_dst_desc;
@@ -1343,6 +1664,14 @@
return_val = false;
goto close_src_and_dst_desc;
}
+ /* BEGIN progress mod */
+ if (progress) {
+ int i;
+ for ( i = 0; i < 6; i++ )
+ free ( cProgressField[i] );
+ free ( cProgressField );
+ }
+ /* END progress mod */
}
preserve_metadata:
@@ -1716,15 +2045,19 @@
fprintf (stderr,
(x->move_mode || x->unlink_dest_before_opening
|| x->unlink_dest_after_failed_open)
- ? _("%s: replace %s, overriding mode %04lo (%s)? ")
- : _("%s: unwritable %s (mode %04lo, %s); try anyway? "),
+ /* BEGIN progress mod - remove \n\n in string!*/
+ ? _("\n\n%s: replace %s, overriding mode %04lo (%s)? ")
+ : _("\n\n%s: unwritable %s (mode %04lo, %s); try anyway? "),
+ /* END progress mod */
program_name, quoteaf (dst_name),
(unsigned long int) (dst_sb->st_mode & CHMOD_MODE_BITS),
&perms[1]);
}
else
{
- fprintf (stderr, _("%s: overwrite %s? "),
+ /* BEGIN progress mod - remove \n\n in string!*/
+ fprintf (stderr, _("\n\n%s: overwrite %s? "),
+ /* END progress mod - remove \n\n in string!*/
program_name, quoteaf (dst_name));
}
diff -aur coreutils-8.32/src/copy.h coreutils-8.32-patched/src/copy.h
--- coreutils-8.32/src/copy.h 2020-01-01 15:13:12.000000000 +0100
+++ coreutils-8.32-patched/src/copy.h 2022-01-04 21:56:14.545035647 +0100
@@ -234,6 +234,11 @@
Create destination directories as usual. */
bool symbolic_link;
+ /* BEGIN progress mod */
+ /* If true, draw a nice progress bar on screen */
+ bool progress_bar;
+ /* END progress mod */
+
/* If true, do not copy a nondirectory that has an existing destination
with the same or newer modification time. */
bool update;
@@ -304,4 +309,19 @@
bool chown_failure_ok (struct cp_options const *) _GL_ATTRIBUTE_PURE;
mode_t cached_umask (void);
+/* BEGIN progress mod */
+char * escape_double_quotes ( char * _cUnescapedString );
+char * format_elapsed_time ( double sec_elapsed );
+
+int file_size_format ( char * _cDst, long _lSize, int _iCounter );
+
+__attribute__((__common__)) long g_iTotalSize;
+__attribute__((__common__)) long g_iTotalWritten;
+__attribute__((__common__)) int g_iFilesCopied;
+__attribute__((__common__)) int g_iDirectoriesCopied;
+__attribute__((__common__)) struct timeval g_oStartTime;
+__attribute__((__common__)) int g_iTotalFiles;
+__attribute__((__common__)) bool progress;
+/* END progress mod */
+
#endif
diff -aur coreutils-8.32/src/cp.c coreutils-8.32-patched/src/cp.c
--- coreutils-8.32/src/cp.c 2020-01-01 15:13:12.000000000 +0100
+++ coreutils-8.32-patched/src/cp.c 2022-01-04 21:56:14.545035647 +0100
@@ -131,6 +131,9 @@
{"symbolic-link", no_argument, NULL, 's'},
{"target-directory", required_argument, NULL, 't'},
{"update", no_argument, NULL, 'u'},
+ /* BEGIN progress mod */
+ {"progress-bar", no_argument, NULL, 'g'},
+ /* END progress mod */
{"verbose", no_argument, NULL, 'v'},
{GETOPT_SELINUX_CONTEXT_OPTION_DECL},
{GETOPT_HELP_OPTION_DECL},
@@ -170,6 +173,13 @@
-f, --force if an existing destination file cannot be\n\
opened, remove it and try again (this option\n\
is ignored when the -n option is also used)\n\
+"), stdout); /* BEGIN progress mod - remove the complete line!*/
+ fputs (_("\
+ -g, --progress-bar add a progress bar.\n\
+ Note that this doesn't work with reflink,\n\
+ reflink will be automatically disabled\n\
+"), stdout);
+/* END progress mod - remove the complete line!*/ fputs (_("\
-i, --interactive prompt before overwrite (overrides a previous -n\
\n\
option)\n\
@@ -635,6 +645,83 @@
die (EXIT_FAILURE, 0, _("target %s is not a directory"),
quoteaf (file[n_files - 1]));
}
+ /* BEGIN progress mod */
+ struct timeval start_time;
+ if (progress) {
+ if (g_iTotalSize == 0)
+ g_iTotalSize = 0;
+ if (g_iTotalFiles == 0)
+ g_iTotalFiles = n_files;
+ if (g_iFilesCopied == 0)
+ g_iFilesCopied = 0;
+ if (g_iDirectoriesCopied == 0)
+ g_iDirectoriesCopied = 0;
+ if (g_iTotalWritten == 0)
+ g_iTotalWritten = 0;
+
+ if (target_directory_operand (file[0], &sb, &new_dst, forcing))
+ g_iDirectoriesCopied++;
+
+ /* save time */
+ gettimeofday ( & start_time, NULL );
+ g_oStartTime = start_time;
+
+ printf ( "calculating total size... \r" );
+ fflush ( stdout );
+ long iTotalSize = 0;
+ int iFiles = n_files;
+ if ( ! target_directory )
+ iFiles = n_files - 1;
+ int j;
+
+ /* how many files are we copying */
+ char command[1024];
+ char * _cEscapedString = escape_double_quotes( file[0] );
+ sprintf( command, "find \"%s\" -type f | wc -l", _cEscapedString );
+ free( _cEscapedString );
+ FILE *fp ;
+ char output[1024];
+ fp = popen(command,"r");
+ if ( fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL)
+ printf("failed to run find\r");
+ else
+ if ( atoi(output) > n_files )
+ g_iTotalFiles = atoi( output );
+
+ for (j = 0; j < iFiles; j++)
+ {
+ /* call du -s for each file */
+ /* create command */
+ char command[1024];
+ char * _cEscapedString = escape_double_quotes( file[j] );
+ sprintf ( command, "du -s \"%s\"", _cEscapedString );
+ free( _cEscapedString );
+
+ FILE *fp;
+ char output[1024];
+
+ /* run command */
+ fp = popen(command, "r");
+ if (fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL) {
+ printf("failed to run du\r" );
+ }
+ else
+ {
+ /* isolate size */
+ strchr ( output, '\t' )[0] = '\0';
+ iTotalSize += atol ( output );
+
+ printf ( "calculating total size... %ld\r", iTotalSize );
+ fflush ( stdout );
+ }
+
+ /* close */
+ pclose(fp);
+ }
+ g_iTotalSize += iTotalSize;
+ }
+ /* END progress mod */
+
if (target_directory)
{
@@ -777,6 +864,53 @@
ok = copy (source, new_dest, 0, x, &unused, NULL);
}
+ /* BEGIN progress mod */
+ if (progress) {
+ /* remove everything */
+ int i;
+ if ( g_iTotalFiles > 1 )
+ {
+ for ( i = 0; i < 6; i++ )
+ printf ( "\033[K\n" );
+ printf ( "\r\033[6A" );
+ }
+ else
+ {
+ for ( i = 0; i < 3; i++ )
+ printf ( "\033[K\n" );
+ printf ( "\r\033[3A" );
+ }
+
+ /* save time */
+ struct timeval end_time;
+ gettimeofday ( & end_time, NULL );
+ int usec_elapsed = end_time.tv_usec - start_time.tv_usec;
+ double sec_elapsed = ( double ) usec_elapsed / 1000000.f;
+ sec_elapsed += ( double ) ( end_time.tv_sec - start_time.tv_sec );
+
+ /* get total size */
+ char sTotalWritten[20];
+ file_size_format ( sTotalWritten, g_iTotalSize, 1 );
+ /* TODO: using g_iTotalWritten would be more correct, but is less accurate */
+
+ /* calculate speed */
+ int copy_speed = ( int ) ( ( double ) g_iTotalWritten / sec_elapsed );
+ char s_copy_speed[20];
+ file_size_format ( s_copy_speed, copy_speed, 1 );
+
+ /* good-bye message */
+ char sFType[20];
+ if ( g_iDirectoriesCopied > 0 && g_iDirectoriesCopied == g_iFilesCopied )
+ sprintf ( sFType, "%s", "folder(s)" );
+ else if ( g_iDirectoriesCopied > 0 && g_iDirectoriesCopied < g_iFilesCopied )
+ sprintf ( sFType, "%s", "folder(s)/file(s)" );
+ 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 );
+ }
+ /* END progress mod */
+
return ok;
}
@@ -812,6 +946,9 @@
x->recursive = false;
x->sparse_mode = SPARSE_AUTO;
x->symbolic_link = false;
+ /* BEGIN progress mod */
+ x->progress_bar = false;
+ /* END progress mod */
x->set_mode = false;
x->mode = 0;
@@ -950,7 +1087,8 @@
selinux_enabled = (0 < is_selinux_enabled ());
cp_option_init (&x);
- while ((c = getopt_long (argc, argv, "abdfHilLnprst:uvxPRS:TZ",
+ /* BEGIN and END progress mod - remove the g in the next line!*/
+ while ((c = getopt_long (argc, argv, "abdfgHilLnprst:uvxPRS:TZ",
long_opts, NULL))
!= -1)
{
@@ -1007,6 +1145,12 @@
x.unlink_dest_after_failed_open = true;
break;
+ /* BEGIN progress mod */
+ case 'g':
+ progress = true;
+ break;
+ /* END progress mod */
+
case 'H':
x.dereference = DEREF_COMMAND_LINE_ARGUMENTS;
break;
diff -aur coreutils-8.32/src/mv.c coreutils-8.32-patched/src/mv.c
--- coreutils-8.32/src/mv.c 2020-01-01 15:13:12.000000000 +0100
+++ coreutils-8.32-patched/src/mv.c 2022-01-04 21:56:14.549035719 +0100
@@ -66,6 +66,9 @@
{"target-directory", required_argument, NULL, 't'},
{"update", no_argument, NULL, 'u'},
{"verbose", no_argument, NULL, 'v'},
+ /* BEGIN progress mod */
+ {"progress-bar", no_argument, NULL, 'g'},
+ /* END progress mod */
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
@@ -170,8 +173,121 @@
{
bool copy_into_self;
bool rename_succeeded;
+
+ /* BEGIN progress mod */
+ struct timeval start_time;
+
+ if(progress && x->rename_errno != 0) {
+ if (g_iTotalSize == 0)
+ g_iTotalSize = 0;
+ if (g_iTotalFiles == 0)
+ g_iTotalFiles = 0;
+ if (g_iFilesCopied == 0)
+ g_iFilesCopied = 0;
+ if (g_iDirectoriesCopied == 0)
+ g_iDirectoriesCopied = 0;
+ if (g_iTotalWritten == 0)
+ g_iTotalWritten = 0;
+
+ if (target_directory_operand (source))
+ g_iDirectoriesCopied++;
+
+ gettimeofday (& start_time, NULL);
+ g_oStartTime = start_time;
+
+ /* how many files are we copying */
+ char command[1024];
+ char output[1024];
+ FILE *fp ;
+
+ char * _cEscapedString = escape_double_quotes( (unsigned char *)(size_t)source );
+ sprintf( command, "find \"%s\" -type f | wc -l", _cEscapedString );
+ free( _cEscapedString );
+ fp = popen(command,"r");
+ if ( fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL)
+ printf("failed to run find\r");
+ else
+ g_iTotalFiles = atoi( output );
+
+ printf ("calculating total size... \r");
+ fflush (stdout);
+ long iTotalSize = 0;
+ /* call du -s for each file */
+ /* create command */
+ _cEscapedString = escape_double_quotes( (unsigned char *)(size_t)source );
+ sprintf ( command, "du -s \"%s\"", _cEscapedString );
+ free( _cEscapedString );
+
+ /* run command */
+ fp = popen(command, "r");
+ if (fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL) {
+ printf("failed to run du\r" );
+ }
+ else
+ {
+ /* isolate size */
+ strchr ( output, '\t' )[0] = '\0';
+ iTotalSize += atol ( output );
+ printf ( "calculating total size... %ld\r", iTotalSize );
+ fflush ( stdout );
+ }
+
+ /* close */
+ pclose(fp);
+ g_iTotalSize += iTotalSize;
+ }
+ /* END progress mod */
+
bool ok = copy (source, dest, false, x, &copy_into_self, &rename_succeeded);
+ /* BEGIN progress mod */
+ if (progress && (x->rename_errno != 0 && ok)) {
+ /* remove everything */
+ int i;
+ int limit = (g_iTotalFiles > 1 ? 6 : 3);
+ if (!rename_succeeded)
+ {
+ for ( i = 0; i < limit; i++ )
+ printf ( "\033[K\n" );
+ printf ( "\r\033[3A" );
+ }
+
+ /* save time */
+ struct timeval end_time;
+ gettimeofday ( & end_time, NULL );
+ int usec_elapsed = end_time.tv_usec - start_time.tv_usec;
+ double sec_elapsed = ( double ) usec_elapsed / 1000000.f;
+ sec_elapsed += ( double ) ( end_time.tv_sec - start_time.tv_sec );
+
+ /* get total size */
+ char sTotalWritten[20];
+ file_size_format ( sTotalWritten, g_iTotalSize, 1 );
+ /* TODO: using g_iTotalWritten would be more correct, but is less accurate */
+
+ /* calculate speed */
+ int copy_speed = ( int ) ( ( double ) g_iTotalWritten / sec_elapsed );
+ char s_copy_speed[20];
+ file_size_format ( s_copy_speed, copy_speed, 1 );
+
+ /* increase counter */
+ g_iFilesCopied++;
+
+ /* good-bye message */
+ if ( x->last_file )
+ {
+ char sFType[20];
+ if ( g_iDirectoriesCopied > 0 && g_iDirectoriesCopied == g_iFilesCopied )
+ sprintf ( sFType, "%s", "folder(s)" );
+ else if ( g_iDirectoriesCopied > 0 && g_iDirectoriesCopied < g_iFilesCopied )
+ sprintf ( sFType, "%s", "folder(s)/file(s)" );
+ 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 );
+ }
+ }
+ /* END progress mod */
+
if (ok)
{
char const *dir_to_remove;
@@ -306,6 +422,11 @@
\n\
-b like --backup but does not accept an argument\n\
-f, --force do not prompt before overwriting\n\
+"), stdout); /* BEGIN progress mod - remove the complete line!*/
+fputs (_("\
+ -g, --progress-bar add progress-bar\n\
+"), stdout);
+/* END progress mod - remove the complete line!*/ fputs (_("\
-i, --interactive prompt before overwrite\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\
@@ -361,7 +482,8 @@
/* Try to disable the ability to unlink a directory. */
priv_set_remove_linkdir ();
- while ((c = getopt_long (argc, argv, "bfint:uvS:TZ", long_options, NULL))
+ /* BEGIN and END progress mod - remove the g in the next line!*/
+ while ((c = getopt_long (argc, argv, "bfint:uvgS:TZ", long_options, NULL))
!= -1)
{
switch (c)
@@ -407,6 +529,11 @@
case 'v':
x.verbose = true;
break;
+ /* BEGIN progress mod */
+ case 'g':
+ progress = true;
+ break;
+ /* END progress mod */
case 'S':
make_backups = true;
backup_suffix = optarg;