X-Git-Url: https://git.tld-linux.org/?p=packages%2Fcoreutils.git;a=blobdiff_plain;f=coreutils-advcopy.patch;h=a05e96a096c1f354d1950a378d858bed4f7c2070;hp=1371326b500cecfdebc06786fae7e9a6eae0a692;hb=HEAD;hpb=c15795f273c4569d3441eb7522d227bd518f0c58 diff --git a/coreutils-advcopy.patch b/coreutils-advcopy.patch index 1371326..e466c9a 100644 --- a/coreutils-advcopy.patch +++ b/coreutils-advcopy.patch @@ -1,34 +1,111 @@ -diff -ru coreutils-8.4.orig/src/copy.c coreutils-8.4/src/copy.c ---- coreutils-8.4.orig/src/copy.c 2010-01-25 16:03:29.606930239 +0100 -+++ coreutils-8.4/src/copy.c 2010-01-26 15:37:24.544158220 +0100 -@@ -457,6 +457,56 @@ - return lchmod (name, mode); - } +diff -aur coreutils-9.0/src/copy.c coreutils-9.0-patched/src/copy.c +--- coreutils-9.0/src/copy.c 2021-09-24 17:01:05.000000000 +0530 ++++ coreutils-9.0-patched/src/copy.c 2022-03-27 05:54:15.840209369 +0530 +@@ -129,6 +129,133 @@ + dev_t dev; + }; +/* BEGIN progress mod */ -+static void file_progress_bar ( char * _cDest, int _iBarLength, int _iProgress, int _iTotal ) ++struct progress_status { ++ int iCountDown; ++ char ** cProgressField; ++ struct timeval last_time; ++ int last_size, iBarLength; ++ struct stat src_open_sb; ++}; ++ ++FILE * spawn( const char *cmd, char *const argv[] ) ++{ ++ FILE *ret = NULL; ++ int pfd_read[2]; ++ pid_t pid; ++ ++ if (cmd == NULL || argv == NULL) ++ return ret; ++ ++ if (pipe(pfd_read) < 0) { ++ error(0, errno, "pipe: %s", cmd); ++ return ret; ++ } ++ ++ if ((pid = fork()) == 0) { ++ int err = dup2(pfd_read[1], 1) < 0; ++ close(pfd_read[0]); ++ close(pfd_read[1]); ++ ++ if (err) ++ error(EXIT_FAILURE, errno, "dup2: %s", cmd); ++ execvp(cmd, argv); ++ error(EXIT_FAILURE, errno, "exec: %s", cmd); ++ } ++ ++ close(pfd_read[1]); ++ ++ if (pid < 0) { ++ close(pfd_read[0]); ++ error(0, errno, "fork: %s", cmd); ++ return ret; ++ } ++ ++ ret = fdopen(pfd_read[0], "r"); ++ return ret; ++} ++ ++void format_time ( char * _cDest, double seconds, bool showall ) +{ -+ // write number to progress bar -+ float fPercent = ( float ) _iProgress / ( float ) _iTotal * 100.f; -+ sprintf ( _cDest + ( _iBarLength - 6 ), "%4.1f", fPercent ); -+ // remove zero -+ _cDest[_iBarLength - 2] = ' '; -+ -+ // fill rest with '-' -+ int i; -+ for ( i = 1; i <= _iBarLength - 9; i++ ) ++ // hours ++ int hr = ( (int) seconds / (60 * 60)) % 24; ++ // minutes ++ int min = ( (int) seconds / 60) % 60; ++ // seconds ++ double sec = seconds - (hr * (60 * 60)) - (min * 60); ++ if ( showall ) + { -+ if ( fPercent > ( float ) ( i - 1 ) / ( _iBarLength - 10 ) * 100.f ) -+ _cDest[i] = '|'; ++ if ( seconds < 0 ) ++ sprintf(_cDest, "%2ch %2cm %2cs", '0', '0', '?'); + else -+ _cDest[i] = '-'; ++ sprintf(_cDest, "%2dh %2dm %2ds", hr, min, (int) sec); ++ } else if ( seconds >= 3600 ) ++ { ++ sprintf(_cDest, "%2dh %2dm %4.1fs", hr, min, sec); ++ } else if ( seconds >= 60 ) ++ { ++ sprintf(_cDest, "%2dm %4.1fs", min, sec); ++ } else ++ { ++ sprintf(_cDest, "%4.1fs", sec); + } +} + -+int file_size_format ( char * _cDst, int _iSize, int _iCounter ) ++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 ) _iSize; ++ double dSize = ( double ) _lSize; + while ( dSize >= 1000. ) + { + dSize /= 1024.; @@ -55,11 +132,189 @@ diff -ru coreutils-8.4.orig/src/copy.c coreutils-8.4/src/copy.c +} +/* END progress mod */ + - /* Copy a regular file from SRC_NAME to DST_NAME. - If the source file contains holes, copies holes and blocks of zeros - in the source file as holes in the destination file. -@@ -706,8 +756,146 @@ - buf_alloc = xmalloc (buf_size + buf_alignment_slop); + /* Initial size of the cp.dest_info hash table. */ + #define DEST_INFO_INITIAL_CAPACITY 61 + +@@ -299,14 +426,23 @@ + bytes read. */ + static bool + 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, bool move_mode, + 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, ++ struct progress_status *s_progress) + { + *last_write_made_hole = false; + *total_n_read = 0; + ++ /* BEGIN progress mod */ ++ gettimeofday ( & g_oFStartTime, NULL ); ++ g_iFTotalWritten = 0; ++ struct stat st; ++ stat(src_name, &st); ++ g_iFTotalSize = st.st_size/1024; ++ /* END progress mod */ ++ + /* If not looking for holes, use copy_file_range if functional, + but don't use if reflink disallowed as that may be implicit. */ + if ((! hole_size) && allow_reflink && functional_copy_file_range ()) +@@ -362,6 +498,103 @@ + + 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 cur_fsize = g_iFTotalWritten + *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 isec_felapsed = cur_time.tv_sec - g_oFStartTime.tv_sec; ++ int sec_remaining = ( int ) ( ( double ) isec_elapsed / cur_size ++ * g_iTotalSize ) - isec_elapsed; ++ int sec_fremaining = ( int ) ( ( double ) isec_felapsed / cur_fsize ++ * g_iFTotalSize ) - isec_felapsed; ++ /* 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], ++ move_mode ++ ? "%d of %d files moved (about %s remaining) " ++ : "%d of %d files copied (about %s remaining) ", ++ g_iFilesCopied, g_iTotalFiles, f_ttime ); ++ ++ char s_ftime[40] = ""; ++ ++ if (g_iTotalFiles > 1) ++ sprintf ( s_ftime, "(about %s remaining)", f_ftime ); ++ else ++ sprintf ( s_ftime, "(about %s remaining)", f_ttime ); ++ ++ sprintf ( s_progress->cProgressField[3], ++ move_mode ++ ? "moving at %s/s %s" ++ : "copying at %s/s %s", s_copy_speed, s_ftime ); ++ ++ 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) + { +@@ -446,6 +679,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)) +@@ -516,9 +757,11 @@ + static bool + 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, ++ enum Sparse_type sparse_mode, bool move_mode, + bool allow_reflink, +- char const *src_name, char const *dst_name) ++ char const *src_name, char const *dst_name, ++ int iCountDown, char ** cProgressField, struct timeval last_time, ++ int last_size, int iBarLength, struct stat src_open_sb) + { + off_t last_ext_start = 0; + off_t last_ext_len = 0; +@@ -590,10 +833,16 @@ + is conservative and may miss some holes. */ + off_t n_read; + bool read_hole; ++ ++ struct timeval a; ++ struct stat b; ++ ++ struct progress_status s_progress={iCountDown, cProgressField, last_time, last_size, iBarLength, src_open_sb}; ++ + if ( ! sparse_copy (src_fd, dest_fd, buf, buf_size, + sparse_mode == SPARSE_NEVER ? 0 : hole_size, +- true, allow_reflink, src_name, dst_name, +- ext_len, &n_read, &read_hole)) ++ true, allow_reflink, move_mode, src_name, ++ dst_name, ext_len, &n_read, &read_hole, &s_progress)) + return false; + + dest_pos = ext_start + n_read; +@@ -1374,8 +1623,80 @@ + buf_alloc = xmalloc (buf_size + buf_alignment); buf = ptr_align (buf_alloc, buf_alignment); + /* BEGIN progress mod */ @@ -69,7 +324,8 @@ diff -ru coreutils-8.4.orig/src/copy.c coreutils-8.4/src/copy.c + int iBarLength = 80; + struct winsize win; + if ( ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win) == 0 && win.ws_col > 0 ) -+ iBarLength = win.ws_col; ++ 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++ ) @@ -83,7 +339,7 @@ diff -ru coreutils-8.4.orig/src/copy.c coreutils-8.4/src/copy.c + } + + /* global progress bar? */ -+ if ( g_iTotalSize ) ++ if ( g_iTotalFiles > 1 ) + { + /* init global progress bar */ + cProgressField[2][0] = '['; @@ -96,7 +352,11 @@ diff -ru coreutils-8.4.orig/src/copy.c coreutils-8.4/src/copy.c + file_size_format ( cProgressField[1] + iBarLength - 9, g_iTotalSize, 1 ); + + /* show how many files were written */ -+ int sum_length = sprintf ( cProgressField[1], "%d files copied so far...", g_iFilesCopied ); ++ int sum_length = 0; ++ sum_length = sprintf ( cProgressField[1], ++ x->move_mode ++ ? "%d of %d files moved so far" ++ : "%d of %d files copied so far", g_iFilesCopied, g_iTotalFiles ); + cProgressField[1][sum_length] = ' '; + } + @@ -126,227 +386,382 @@ diff -ru coreutils-8.4.orig/src/copy.c coreutils-8.4/src/copy.c + int last_size = g_iTotalWritten; + /* END progress mod */ + - while (true) + off_t n_read; + bool wrote_hole_at_eof = false; ++ ++ struct progress_status s_progress = { iCountDown, cProgressField, last_time, last_size, iBarLength, src_open_sb}; ++ + if (! ( + #ifdef SEEK_HOLE + scantype == LSEEK_SCANTYPE +@@ -1383,15 +1704,17 @@ + scan_inference.ext_start, src_open_sb.st_size, + make_holes ? x->sparse_mode : SPARSE_NEVER, + x->reflink_mode != REFLINK_NEVER, +- src_name, dst_name) ++ x->move_mode, src_name, dst_name, ++ iCountDown, cProgressField, last_time, last_size, ++ iBarLength, src_open_sb) + : + #endif + sparse_copy (source_desc, dest_desc, buf, buf_size, + make_holes ? hole_size : 0, + x->sparse_mode == SPARSE_ALWAYS, + x->reflink_mode != REFLINK_NEVER, +- src_name, dst_name, UINTMAX_MAX, &n_read, +- &wrote_hole_at_eof))) ++ x->move_mode, src_name, dst_name, UINTMAX_MAX, ++ &n_read, &wrote_hole_at_eof, &s_progress))) { -+ if (progress) { -+ /* BEGIN progress mod */ -+ /* update countdown */ -+ iCountDown--; -+ if ( iCountDown < 0 ) -+ iCountDown = 100; -+ -+ /* just print one line with the percentage, but not always */ -+ if ( iCountDown == 0 ) -+ { -+ /* calculate current speed */ -+ struct timeval cur_time; -+ gettimeofday ( & cur_time, NULL ); -+ int cur_size = g_iTotalWritten + n_read_total / 1024; -+ int usec_elapsed = cur_time.tv_usec - last_time.tv_usec; -+ double sec_elapsed = ( double ) usec_elapsed / 1000000.f; -+ sec_elapsed += ( double ) ( cur_time.tv_sec - last_time.tv_sec ); -+ int copy_speed = ( int ) ( ( double ) ( cur_size - last_size ) -+ / sec_elapsed ); -+ char s_copy_speed[20]; -+ file_size_format ( s_copy_speed, copy_speed, 1 ); -+ /* update vars */ -+ last_time = cur_time; -+ 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 ( cProgressField[3], -+ "Copying at %s/s (about %dh %dm %ds remaining)", s_copy_speed, -+ hours_remaining, min_remaining, sec_remaining ); -+ -+ int fs_len; -+ if ( g_iTotalSize ) -+ { -+ /* global progress bar */ -+ file_progress_bar ( cProgressField[2], iBarLength, -+ g_iTotalWritten + n_read_total / 1024, g_iTotalSize ); -+ -+ /* print the global status */ -+ fs_len = file_size_format ( cProgressField[1] + iBarLength - 21, -+ g_iTotalWritten + n_read_total / 1024, 1 ); -+ cProgressField[1][iBarLength - 21 + fs_len] = ' '; -+ } -+ -+ /* current progress bar */ -+ file_progress_bar ( sProgressBar, iBarLength, n_read_total, src_open_sb.st_size ); -+ -+ /* print the status */ -+ fs_len = file_size_format ( cProgressField[4] + iBarLength - 21, n_read_total, 0 ); -+ cProgressField[4][iBarLength - 21 + fs_len] = ' '; -+ -+ /* print the field */ -+ for ( it = g_iTotalSize ? 0 : 3; it < 6; it++ ) -+ { -+ printf ( "\033[K%s\n", cProgressField[it] ); -+ if ( strlen ( cProgressField[it] ) < iBarLength ) -+ printf ( "" ); -+ } -+ if ( g_iTotalSize ) -+ printf ( "\r\033[6A" ); -+ else -+ printf ( "\r\033[3A" ); -+ fflush ( stdout ); -+ } -+ /* END progress mod */ -+ } -+ - word *wp = NULL; - - ssize_t n_read = read (source_desc, buf, buf_size); -@@ -788,6 +976,19 @@ - /proc with linux kernels from at least 2.6.9 .. 2.6.29. */ - } + return_val = false; + goto close_src_and_dst_desc; +@@ -1402,6 +1725,14 @@ + return_val = false; + goto close_src_and_dst_desc; } -+if (progress) { + /* BEGIN progress mod */ -+ /* update total size */ -+ g_iTotalWritten += n_read_total / 1024; -+ g_iFilesCopied++; -+ -+ int i; -+ for ( i = 0; i < 6; i++ ) -+ free ( cProgressField[i] ); -+ free ( cProgressField ); ++ if (progress) { ++ int i; ++ for ( i = 0; i < 6; i++ ) ++ free ( cProgressField[i] ); ++ free ( cProgressField ); ++ } + /* END progress mod */ -+} -+ + } - /* If the file ends with a `hole', we need to do something to record - the length of the file. On modern systems, calling ftruncate does -diff -ru coreutils-8.4.orig/src/copy.h coreutils-8.4/src/copy.h ---- coreutils-8.4.orig/src/copy.h 2010-01-25 16:03:29.606930239 +0100 -+++ coreutils-8.4/src/copy.h 2010-01-26 15:35:30.934163303 +0100 -@@ -222,6 +222,9 @@ + 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 17:01:05.000000000 +0530 ++++ coreutils-9.0-patched/src/copy.h 2022-03-27 05:54:15.844209471 +0530 +@@ -236,6 +236,9 @@ Create destination directories as usual. */ bool symbolic_link; -+ /* if true, draw a nice progress bar on screen */ ++ /* If true, draw a nice progress bar on screen */ + bool progress_bar; + /* If true, do not copy a nondirectory that has an existing destination with the same or newer modification time. */ bool update; -@@ -280,4 +283,15 @@ - bool chown_failure_ok (struct cp_options const *); +@@ -308,4 +311,22 @@ + bool chown_failure_ok (struct cp_options const *) _GL_ATTRIBUTE_PURE; mode_t cached_umask (void); +/* BEGIN progress mod */ -+int file_size_format ( char * _cDst, int _iSize, int _iCounter ); -+ -+long g_iTotalSize; -+long g_iTotalWritten; -+int g_iFilesCopied; -+struct timeval g_oStartTime; -+int g_iTotalFiles; -+bool progress; ++FILE * spawn( const char *cmd, char *const argv[] ); ++void format_time ( char * _cDst, double seconds, bool showall ); ++ ++int file_size_format ( char * _cDst, long _lSize, int _iCounter ); ++ ++__attribute__((__common__)) long g_iTotalSize; ++__attribute__((__common__)) long g_iFTotalSize; ++__attribute__((__common__)) long g_iTotalWritten; ++__attribute__((__common__)) long g_iFTotalWritten; ++__attribute__((__common__)) int g_iFilesCopied; ++__attribute__((__common__)) int g_iDirectoriesCopied; ++__attribute__((__common__)) struct timeval g_oStartTime; ++__attribute__((__common__)) struct timeval g_oFStartTime; ++__attribute__((__common__)) int g_iTotalFiles; ++__attribute__((__common__)) bool progress; +/* END progress mod */ + #endif ---- coreutils-8.9/src/cp.c.orig 2011-01-01 22:19:23.000000000 +0100 -+++ coreutils-8.9/src/cp.c 2011-01-09 13:07:02.769898177 +0100 -@@ -141,6 +141,7 @@ +diff -aur coreutils-9.0/src/cp.c coreutils-9.0-patched/src/cp.c +--- coreutils-9.0/src/cp.c 2021-09-24 17:01:05.000000000 +0530 ++++ coreutils-9.0-patched/src/cp.c 2022-03-27 06:28:53.896713403 +0530 +@@ -131,6 +131,7 @@ + {"symbolic-link", no_argument, NULL, 's'}, {"target-directory", required_argument, NULL, 't'}, {"update", no_argument, NULL, 'u'}, - {"verbose", no_argument, NULL, 'v'}, + {"progress-bar", no_argument, NULL, 'g'}, + {"verbose", no_argument, NULL, 'v'}, + {GETOPT_SELINUX_CONTEXT_OPTION_DECL}, {GETOPT_HELP_OPTION_DECL}, - {GETOPT_VERSION_OPTION_DECL}, - {NULL, 0, NULL, 0} -@@ -181,6 +182,7 @@ - opened, remove it and try again (redundant if\ - \n\ - the -n option is used)\n\ -+ -g, --progress-bar add progress-bar\n\ +@@ -170,6 +171,9 @@ + -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\ ++ -g, --progress-bar add a progress bar.\n\ ++ Note that this doesn't work with reflink,\n\ ++ reflink will be automatically disabled\n\ -i, --interactive prompt before overwrite (overrides a previous -n\ \n\ option)\n\ -@@ -621,6 +623,57 @@ - quote (file[n_files - 1])); +@@ -634,6 +638,82 @@ + die (EXIT_FAILURE, 0, _("target %s is not a directory"), + quoteaf (file[n_files - 1])); } - -+ struct timeval start_time; -+if (progress) { + /* BEGIN progress mod */ -+ g_iTotalSize = 0; -+ g_iFilesCopied = 0; -+ g_iTotalWritten = 0; -+ -+ /* save time */ -+ gettimeofday ( & start_time, NULL ); -+ g_oStartTime = start_time; ++ 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; + -+ printf ( "Calculating total size... \r" ); -+ fflush ( stdout ); -+ long iTotalSize = 0; -+ int iFiles = n_files; -+ if ( ! target_directory ) -+ iFiles = n_files - 1; -+ int j; -+ for (j = 0; j < iFiles; j++) -+ { -+ /* call du -s for each file */ -+ /* create command */ -+ char command[1024]; -+ sprintf ( command, "du -s \"%s\"", file[j] ); -+ /* TODO: replace all quote signs in file[i] */ -+ -+ 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.\n" ); -+ } -+ else -+ { -+ /* isolate size */ -+ strchr ( output, '\t' )[0] = '\0'; -+ iTotalSize += atol ( output ); ++ /* how many files are we copying */ ++ FILE *fp ; ++ char output[1024]; ++ fp = spawn("find", (char *[]){ "find", file[0], "-type", "f", NULL }); ++ if ( fp == NULL) ++ printf("failed to run find\r"); ++ else ++ { ++ char *line_buf = NULL; ++ size_t line_buf_size = 0; ++ int line_count = 0; ++ ssize_t line_size; ++ line_size = getline(&line_buf, &line_buf_size, fp); ++ while (line_size > 0) ++ { ++ line_count++; ++ line_size = getline(&line_buf, &line_buf_size, fp); ++ } ++ free (line_buf); ++ if ( line_count > n_files ) ++ g_iTotalFiles = line_count; ++ } ++ ++ for (j = 0; j < iFiles; j++) ++ { ++ /* call du -s for each file */ ++ fp = spawn("du", (char *[]){ "du", "-s", file[j], NULL }); ++ 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... %d\r", iTotalSize ); -+ fflush ( stdout ); -+ } ++ printf ( "calculating total size... %ld\r", iTotalSize ); ++ fflush ( stdout ); ++ } + -+ /* close */ -+ pclose(fp); ++ /* close */ ++ pclose(fp); ++ } ++ g_iTotalSize += iTotalSize; + } -+ g_iTotalSize = iTotalSize; + /* END progress mod */ -+} + + if (target_directory) { - /* cp file1...filen edir -@@ -763,6 +816,46 @@ +@@ -781,6 +861,56 @@ ok = copy (source, new_dest, 0, x, &unused, NULL); } -+if (progress) { + /* BEGIN progress mod */ -+ /* remove everything */ -+ int i; -+ if ( g_iTotalSize ) ++ 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)" ); ++ ++ char f_time[20]; ++ format_time(f_time, sec_elapsed, false); ++ printf ( "%d %s (%s) copied in %s (%s/s).\n", g_iFilesCopied, sFType, ++ sTotalWritten, f_time, s_copy_speed ); ++ } ++ /* END progress mod */ ++ + return ok; + } + +@@ -816,6 +946,9 @@ + x->recursive = false; + x->sparse_mode = SPARSE_AUTO; + x->symbolic_link = false; ++ ++ x->progress_bar = false; ++ + x->set_mode = false; + x->mode = 0; + +@@ -954,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) + { +@@ -1011,6 +1145,10 @@ + x.unlink_dest_after_failed_open = true; + break; + ++ case 'g': ++ progress = true; ++ break; ++ + case 'H': + x.dereference = DEREF_COMMAND_LINE_ARGUMENTS; + break; +@@ -1171,6 +1309,9 @@ + usage (EXIT_FAILURE); + } + ++ if (progress) ++ x.reflink_mode = REFLINK_NEVER; ++ + x.backup_type = (make_backups + ? xget_version (_("backup type"), + 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 17:01:05.000000000 +0530 ++++ coreutils-9.0-patched/src/mv.c 2022-03-27 06:38:49.800838574 +0530 +@@ -66,6 +66,7 @@ + {"target-directory", required_argument, NULL, 't'}, + {"update", no_argument, NULL, 'u'}, + {"verbose", no_argument, NULL, 'v'}, ++ {"progress-bar", no_argument, NULL, 'g'}, + {GETOPT_HELP_OPTION_DECL}, + {GETOPT_VERSION_OPTION_DECL}, + {NULL, 0, NULL, 0} +@@ -170,8 +171,128 @@ + { + 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 */ ++ FILE *fp ; ++ char output[1024]; ++ fp = spawn("find", (char *[]){ "find", (char *)source, "-type", "f", NULL }); ++ if ( fp == NULL) ++ printf("failed to run find\r"); ++ else + { -+ for ( i = 0; i < 6; i++ ) -+ printf ( "\033[K\n" ); -+ printf ( "\r\033[6A" ); ++ char *line_buf = NULL; ++ size_t line_buf_size = 0; ++ int line_count = 0; ++ ssize_t line_size; ++ line_size = getline(&line_buf, &line_buf_size, fp); ++ while (line_size > 0) ++ { ++ line_count++; ++ line_size = getline(&line_buf, &line_buf_size, fp); ++ } ++ free (line_buf); ++ g_iTotalFiles = line_count; ++ } ++ /* close */ ++ pclose(fp); ++ ++ printf ("calculating total size... \r"); ++ fflush (stdout); ++ long iTotalSize = 0; ++ /* call du -s for source */ ++ fp = spawn("du", (char *[]){ "du", "-s", (unsigned char *)(size_t)source, NULL }); ++ if (fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL) { ++ printf("failed to run du\r" ); + } + else + { -+ for ( i = 0; i < 3; i++ ) ++ /* 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, ©_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" ); + } @@ -368,40 +783,55 @@ diff -ru coreutils-8.4.orig/src/copy.h coreutils-8.4/src/copy.h + char s_copy_speed[20]; + file_size_format ( s_copy_speed, copy_speed, 1 ); + ++ /* increase counter */ ++ g_iFilesCopied++; ++ + /* good-bye message */ -+ printf ( "%d files (%s) copied in %.1f seconds (%s/s).\n", g_iFilesCopied, sTotalWritten, -+ sec_elapsed, s_copy_speed ); -+ /* END progress mod */ -+} ++ 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)" ); + - return ok; - } - -@@ -795,6 +888,7 @@ - x->recursive = false; - x->sparse_mode = SPARSE_AUTO; - x->symbolic_link = false; -+ x->progress_bar = false; - x->set_mode = false; - x->mode = 0; - -@@ -933,7 +1027,7 @@ - we'll actually use backup_suffix_string. */ - backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX"); ++ char f_time[20]; ++ format_time(f_time, sec_elapsed, false); ++ printf ( "%d %s (%s) moved in %s (%s/s).\n", g_iFilesCopied, sFType, ++ sTotalWritten, f_time, s_copy_speed ); ++ } ++ } ++ /* END progress mod */ ++ + if (ok) + { + char const *dir_to_remove; +@@ -306,6 +427,7 @@ + \n\ + -b like --backup but does not accept an argument\n\ + -f, --force do not prompt before overwriting\n\ ++ -g, --progress-bar add progress-bar\n\ + -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 +483,7 @@ + /* Try to disable the ability to unlink a directory. */ + priv_set_remove_linkdir (); -- while ((c = getopt_long (argc, argv, "abdfHilLnprst:uvxPRS:T", -+ while ((c = getopt_long (argc, argv, "abdfgHilLnprst:uvxPRS:T", - long_opts, NULL)) +- while ((c = getopt_long (argc, argv, "bfint:uvS:TZ", long_options, NULL)) ++ while ((c = getopt_long (argc, argv, "bfint:uvgS:TZ", long_options, NULL)) != -1) { -@@ -990,6 +1084,10 @@ - x.unlink_dest_after_failed_open = true; + switch (c) +@@ -407,6 +529,9 @@ + case 'v': + x.verbose = true; break; - + case 'g': + progress = true; + break; -+ - case 'H': - x.dereference = DEREF_COMMAND_LINE_ARGUMENTS; - break; + case 'S': + make_backups = true; + backup_suffix = optarg;