1 diff -ru coreutils-8.8/src/copy.c coreutils-8.8-1/src/copy.c
2 --- coreutils-8.8/src/copy.c 2010-01-03 18:06:20.000000000 +0100
3 +++ coreutils-8.8-1/src/copy.c 2010-08-25 21:11:40.629752495 +0200
5 return lchmod (name, mode);
8 +/* BEGIN progress mod */
9 +static void file_progress_bar ( char * _cDest, int _iBarLength, int _iProgress, int _iTotal )
11 + // write number to progress bar
12 + float fPercent = ( float ) _iProgress / ( float ) _iTotal * 100.f;
13 + sprintf ( _cDest + ( _iBarLength - 6 ), "%4.1f", fPercent );
15 + _cDest[_iBarLength - 2] = ' ';
17 + // fill rest with '-'
19 + for ( i = 1; i <= _iBarLength - 9; i++ )
21 + if ( fPercent > ( float ) ( i - 1 ) / ( _iBarLength - 10 ) * 100.f )
28 +int file_size_format ( char * _cDst, int _iSize, int _iCounter )
30 + int iCounter = _iCounter;
31 + double dSize = ( double ) _iSize;
32 + while ( dSize >= 1000. )
40 + if ( iCounter == 0 )
42 + else if ( iCounter == 1 )
44 + else if ( iCounter == 2 )
46 + else if ( iCounter == 3 )
48 + else if ( iCounter == 4 )
54 + return sprintf ( _cDst, "%5.1f %s", dSize, sUnit );
56 +/* END progress mod */
58 /* Copy a regular file from SRC_NAME to DST_NAME.
59 If the source file contains holes, copies holes and blocks of zeros
60 in the source file as holes in the destination file.
62 buf_alloc = xmalloc (buf_size + buf_alignment_slop);
63 buf = ptr_align (buf_alloc, buf_alignment);
65 + /* BEGIN progress mod */
66 + /* create a field of 6 lines */
67 + char ** cProgressField = ( char ** ) calloc ( 6, sizeof ( char * ) );
68 + /* get console width */
69 + int iBarLength = 80;
71 + if ( ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win) == 0 && win.ws_col > 0 )
72 + iBarLength = win.ws_col;
75 + for ( it = 0; it < 6; it++ )
77 + cProgressField[it] = ( char * ) malloc ( iBarLength + 1 );
78 + /* init with spaces */
80 + for ( j = 0; j < iBarLength; j++ )
81 + cProgressField[it][j] = ' ';
82 + cProgressField[it][iBarLength] = '\0';
85 + /* global progress bar? */
88 + /* init global progress bar */
89 + cProgressField[2][0] = '[';
90 + cProgressField[2][iBarLength - 8] = ']';
91 + cProgressField[2][iBarLength - 7] = ' ';
92 + cProgressField[2][iBarLength - 1] = '%';
95 + cProgressField[1][iBarLength - 11] = '/';
96 + file_size_format ( cProgressField[1] + iBarLength - 9, g_iTotalSize, 1 );
98 + /* show how many files were written */
99 + int sum_length = sprintf ( cProgressField[1], "%d files copied so far...", g_iFilesCopied );
100 + cProgressField[1][sum_length] = ' ';
103 + /* truncate filename? */
105 + if ( strlen ( src_name ) > iBarLength - 22 )
107 + sprintf ( cProgressField[4], "...%s", src_name + ( strlen ( src_name ) - iBarLength + 25 ) );
109 + fn_length = sprintf ( cProgressField[4], "%s", src_name );
110 + cProgressField[4][fn_length] = ' ';
113 + cProgressField[4][iBarLength - 11] = '/';
114 + file_size_format ( cProgressField[4] + iBarLength - 9, src_open_sb.st_size, 0 );
116 + int iCountDown = 1;
117 + char * sProgressBar = cProgressField[5];
118 + sProgressBar[0] = '[';
119 + sProgressBar[iBarLength - 8] = ']';
120 + sProgressBar[iBarLength - 7] = ' ';
121 + sProgressBar[iBarLength - 1] = '%';
123 + /* this will always save the time in between */
124 + struct timeval last_time;
125 + gettimeofday ( & last_time, NULL );
126 + int last_size = g_iTotalWritten;
127 + /* END progress mod */
132 + /* BEGIN progress mod */
133 + /* update countdown */
135 + if ( iCountDown < 0 )
138 + /* just print one line with the percentage, but not always */
139 + if ( iCountDown == 0 )
141 + /* calculate current speed */
142 + struct timeval cur_time;
143 + gettimeofday ( & cur_time, NULL );
144 + int cur_size = g_iTotalWritten + n_read_total / 1024;
145 + int usec_elapsed = cur_time.tv_usec - last_time.tv_usec;
146 + double sec_elapsed = ( double ) usec_elapsed / 1000000.f;
147 + sec_elapsed += ( double ) ( cur_time.tv_sec - last_time.tv_sec );
148 + int copy_speed = ( int ) ( ( double ) ( cur_size - last_size )
150 + char s_copy_speed[20];
151 + file_size_format ( s_copy_speed, copy_speed, 1 );
153 + last_time = cur_time;
154 + last_size = cur_size;
156 + /* how many time has passed since the start? */
157 + int isec_elapsed = cur_time.tv_sec - g_oStartTime.tv_sec;
158 + int sec_remaining = ( int ) ( ( double ) isec_elapsed / cur_size
159 + * g_iTotalSize ) - isec_elapsed;
160 + int min_remaining = sec_remaining / 60;
161 + sec_remaining -= min_remaining * 60;
162 + int hours_remaining = min_remaining / 60;
163 + min_remaining -= hours_remaining * 60;
165 + sprintf ( cProgressField[3],
166 + "Copying at %s/s (about %dh %dm %ds remaining)", s_copy_speed,
167 + hours_remaining, min_remaining, sec_remaining );
170 + if ( g_iTotalSize )
172 + /* global progress bar */
173 + file_progress_bar ( cProgressField[2], iBarLength,
174 + g_iTotalWritten + n_read_total / 1024, g_iTotalSize );
176 + /* print the global status */
177 + fs_len = file_size_format ( cProgressField[1] + iBarLength - 21,
178 + g_iTotalWritten + n_read_total / 1024, 1 );
179 + cProgressField[1][iBarLength - 21 + fs_len] = ' ';
182 + /* current progress bar */
183 + file_progress_bar ( sProgressBar, iBarLength, n_read_total, src_open_sb.st_size );
185 + /* print the status */
186 + fs_len = file_size_format ( cProgressField[4] + iBarLength - 21, n_read_total, 0 );
187 + cProgressField[4][iBarLength - 21 + fs_len] = ' ';
189 + /* print the field */
190 + for ( it = g_iTotalSize ? 0 : 3; it < 6; it++ )
192 + printf ( "\033[K%s\n", cProgressField[it] );
193 + if ( strlen ( cProgressField[it] ) < iBarLength )
196 + if ( g_iTotalSize )
197 + printf ( "\r\033[6A" );
199 + printf ( "\r\033[3A" );
202 + /* END progress mod */
207 ssize_t n_read = read (source_desc, buf, buf_size);
209 /proc with linux kernels from at least 2.6.9 .. 2.6.29. */
213 + /* BEGIN progress mod */
214 + /* update total size */
215 + g_iTotalWritten += n_read_total / 1024;
219 + for ( i = 0; i < 6; i++ )
220 + free ( cProgressField[i] );
221 + free ( cProgressField );
222 + /* END progress mod */
226 /* If the file ends with a `hole', we need to do something to record
227 the length of the file. On modern systems, calling ftruncate does
228 diff -ru coreutils-8.8/src/copy.h coreutils-8.8-1/src/copy.h
229 --- coreutils-8.8/src/copy.h 2010-01-03 18:06:20.000000000 +0100
230 +++ coreutils-8.8-1/src/copy.h 2010-08-25 21:11:40.629752495 +0200
232 Create destination directories as usual. */
235 + /* if true, draw a nice progress bar on screen */
238 /* If true, do not copy a nondirectory that has an existing destination
239 with the same or newer modification time. */
242 bool chown_failure_ok (struct cp_options const *);
243 mode_t cached_umask (void);
245 +/* BEGIN progress mod */
246 +int file_size_format ( char * _cDst, int _iSize, int _iCounter );
249 +long g_iTotalWritten;
251 +struct timeval g_oStartTime;
254 +/* END progress mod */
257 diff -ru coreutils-8.8/src/cp.c coreutils-8.8-1/src/cp.c
258 --- coreutils-8.8/src/cp.c 2010-01-04 16:46:06.000000000 +0100
259 +++ coreutils-8.8-1/src/cp.c 2010-08-25 21:11:40.629752495 +0200
261 {"target-directory", required_argument, NULL, 't'},
262 {"update", no_argument, NULL, 'u'},
263 {"verbose", no_argument, NULL, 'v'},
264 + {"progress-bar", no_argument, NULL, 'g'},
265 {GETOPT_HELP_OPTION_DECL},
266 {GETOPT_VERSION_OPTION_DECL},
269 -f, --force if an existing destination file cannot be\n\
270 opened, remove it and try again (redundant if\n\
271 the -n option is used)\n\
272 + -g, --progress-bar add progress-bar\n\
273 -i, --interactive prompt before overwrite (overrides a previous -n\n\
275 -H follow command-line symbolic links in SOURCE\n\
277 quote (file[n_files - 1]));
280 + struct timeval start_time;
282 + /* BEGIN progress mod */
284 + g_iFilesCopied = 0;
285 + g_iTotalWritten = 0;
288 + gettimeofday ( & start_time, NULL );
289 + g_oStartTime = start_time;
291 + printf ( "Calculating total size... \r" );
293 + long iTotalSize = 0;
294 + int iFiles = n_files;
295 + if ( ! target_directory )
296 + iFiles = n_files - 1;
298 + for (j = 0; j < iFiles; j++)
300 + /* call du -s for each file */
301 + /* create command */
302 + char command[1024];
303 + sprintf ( command, "du -s \"%s\"", file[j] );
304 + /* TODO: replace all quote signs in file[i] */
310 + fp = popen(command, "r");
311 + if (fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL) {
312 + printf("failed to run du.\n" );
317 + strchr ( output, '\t' )[0] = '\0';
318 + iTotalSize += atol ( output );
320 + printf ( "Calculating total size... %ld\r", iTotalSize );
327 + g_iTotalSize = iTotalSize;
328 + /* END progress mod */
331 if (target_directory)
333 /* cp file1...filen edir
335 ok = copy (source, new_dest, 0, x, &unused, NULL);
339 + /* BEGIN progress mod */
340 + /* remove everything */
342 + if ( g_iTotalSize )
344 + for ( i = 0; i < 6; i++ )
345 + printf ( "\033[K\n" );
346 + printf ( "\r\033[6A" );
350 + for ( i = 0; i < 3; i++ )
351 + printf ( "\033[K\n" );
352 + printf ( "\r\033[3A" );
356 + struct timeval end_time;
357 + gettimeofday ( & end_time, NULL );
358 + int usec_elapsed = end_time.tv_usec - start_time.tv_usec;
359 + double sec_elapsed = ( double ) usec_elapsed / 1000000.f;
360 + sec_elapsed += ( double ) ( end_time.tv_sec - start_time.tv_sec );
362 + /* get total size */
363 + char sTotalWritten[20];
364 + file_size_format ( sTotalWritten, g_iTotalSize, 1 );
365 + /* TODO: using g_iTotalWritten would be more correct, but is less accurate */
367 + /* calculate speed */
368 + int copy_speed = ( int ) ( ( double ) g_iTotalWritten / sec_elapsed );
369 + char s_copy_speed[20];
370 + file_size_format ( s_copy_speed, copy_speed, 1 );
372 + /* good-bye message */
373 + printf ( "%d files (%s) copied in %.1f seconds (%s/s).\n", g_iFilesCopied, sTotalWritten,
374 + sec_elapsed, s_copy_speed );
375 + /* END progress mod */
382 x->recursive = false;
383 x->sparse_mode = SPARSE_AUTO;
384 x->symbolic_link = false;
385 + x->progress_bar = false;
390 we'll actually use backup_suffix_string. */
391 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
393 - while ((c = getopt_long (argc, argv, "abdfHilLnprst:uvxPRS:T",
394 + while ((c = getopt_long (argc, argv, "abdfgHilLnprst:uvxPRS:T",
398 @@ -975,6 +1069,10 @@
399 x.unlink_dest_after_failed_open = true;
407 x.dereference = DEREF_COMMAND_LINE_ARGUMENTS;
409 diff -ru coreutils-8.8/src/mv.c coreutils-8.8-1/src/mv.c
410 --- coreutils-8.8/src/mv.c 2010-01-03 18:06:20.000000000 +0100
411 +++ coreutils-8.8-1/src/mv.c 2010-08-25 21:11:40.629752495 +0200
413 {"target-directory", required_argument, NULL, 't'},
414 {"update", no_argument, NULL, 'u'},
415 {"verbose", no_argument, NULL, 'v'},
416 + {"progress-bar", no_argument, NULL, 'g'},
417 {GETOPT_HELP_OPTION_DECL},
418 {GETOPT_VERSION_OPTION_DECL},
420 @@ -159,13 +160,100 @@
422 do_move (const char *source, const char *dest, const struct cp_options *x)
425 + struct timeval start_time;
428 + /* BEGIN progress mod */
430 + g_iFilesCopied = 0;
431 + g_iTotalWritten = 0;
433 + gettimeofday (& start_time, NULL);
434 + g_oStartTime = start_time;
436 + printf ("Calculating total size... \r");
438 + long iTotalSize = 0;
439 + /* call du -s for each file */
440 + /* create command */
441 + char command[1024];
442 + sprintf ( command, "du -s \"%s\"", source );
443 + /* TODO: replace all quote signs in file[i] */
449 + fp = popen(command, "r");
450 + if (fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL) {
451 + printf("failed to run du.\n" );
456 + strchr ( output, '\t' )[0] = '\0';
457 + iTotalSize += atol ( output );
458 + printf ( "Calculating total size... %ld\r", iTotalSize );
464 + g_iTotalSize = iTotalSize;
465 + /* END progress mod */
470 bool rename_succeeded;
471 bool ok = copy (source, dest, false, x, ©_into_self, &rename_succeeded);
474 + /* BEGIN progress mod */
475 + /* remove everything */
477 + if ( g_iTotalSize )
479 + for ( i = 0; i < 6; i++ )
480 + printf ( "\033[K\n" );
481 + printf ( "\r\033[6A" );
485 + for ( i = 0; i < 3; i++ )
486 + printf ( "\033[K\n" );
487 + printf ( "\r\033[3A" );
491 + struct timeval end_time;
492 + gettimeofday ( & end_time, NULL );
493 + int usec_elapsed = end_time.tv_usec - start_time.tv_usec;
494 + double sec_elapsed = ( double ) usec_elapsed / 1000000.f;
495 + sec_elapsed += ( double ) ( end_time.tv_sec - start_time.tv_sec );
497 + /* get total size */
498 + char sTotalWritten[20];
499 + file_size_format ( sTotalWritten, g_iTotalSize, 1 );
500 + /* TODO: using g_iTotalWritten would be more correct, but is less accurate */
502 + /* calculate speed */
503 + int copy_speed = ( int ) ( ( double ) g_iTotalWritten / sec_elapsed );
504 + char s_copy_speed[20];
505 + file_size_format ( s_copy_speed, copy_speed, 1 );
507 + /* good-bye message */
508 + printf ( "%d files (%s) moved in %.1f seconds (%s/s).\n", g_iFilesCopied, sTotalWritten,
509 + sec_elapsed, s_copy_speed );
510 + /* END progress mod */
515 char const *dir_to_remove;
520 /* In general, when copy returns with copy_into_self set, SOURCE is
522 --backup[=CONTROL] make a backup of each existing destination file\n\
523 -b like --backup but does not accept an argument\n\
524 -f, --force do not prompt before overwriting\n\
525 + -g, --progress-bar add progress-bar\n\
526 -i, --interactive prompt before overwrite\n\
527 -n, --no-clobber do not overwrite an existing file\n\
528 If you specify more than one of -i, -f, -n, only the final one takes effect.\n\
530 we'll actually use backup_suffix_string. */
531 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
533 - while ((c = getopt_long (argc, argv, "bfint:uvS:T", long_options, NULL))
534 + while ((c = getopt_long (argc, argv, "bfint:uvgS:T", long_options, NULL))
547 backup_suffix_string = optarg;