[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [coreutils] cp enhancement ?
From: |
Dmitry Ilyin |
Subject: |
Re: [coreutils] cp enhancement ? |
Date: |
Sun, 19 Sep 2010 11:55:22 +0400 |
I made a patch to implement progress bar. It is very primitive (i'm new to c
programming)
--- a/src/copy.c 2010-04-20 23:52:04.000000000 +0400
+++ b/src/copy.c 2010-09-12 17:50:57.544775657 +0400
@@ -709,6 +709,20 @@
buf_alloc = xmalloc (buf_size + buf_alignment_slop);
buf = ptr_align (buf_alloc, buf_alignment);
+ int counter=0;
+ int writes_per_percent;
+ bool print_progress = false;
+
+ //decide should we print progress bar
+ if ((x->verbose) && (src_open_sb.st_size > 0))
+ print_progress = true;
+
+ //calculate number of writes per one percent of copy progress
+ if (print_progress)
+ {
+ writes_per_percent = (int)src_open_sb.st_size / (int)buf_size / 100;
+ printf("%d bytes: ",(int)src_open_sb.st_size);
+ }
for (;;)
{
word *wp = NULL;
@@ -783,15 +797,29 @@
return_val = false;
goto close_src_and_dst_desc;
}
+
+ //print dot per percent of copy progress
+ if ((print_progress) && ((counter >= writes_per_percent) ||
(counter == 0)))
+ {
+ counter=0;
+ printf("*");
+ fflush(stdout);
+ }
+ counter++;
+
last_write_made_hole = false;
/* It is tempting to return early here upon a short read from a
regular file. That would save the final read syscall for each
file. Unfortunately that doesn't work for certain files in
/proc with linux kernels from at least 2.6.9 .. 2.6.29. */
- }
+ }
}
-
+
+ //print newline after line of dots
+ if (print_progress)
+ printf(" Done!\n");
+
/* 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
the job. On systems without native ftruncate support, we have to
Example:
cp -v 1 2
"1" -> "2"
104857600 bytes:
****************************************************************************************************
Done!
It prints "*" per percent of copy. Works with mv too.
Todo:
I have not thinked abount integer overflow.
If there are less then 100 buffer sizes length there will be less then 100 "*"s
Maybe fancier progress bar? -|========-----|- for example.
Maybe activate bar on some new option not -v?