]> git.pld-linux.org Git - packages/coreutils.git/blob - coreutils-advcopy.patch
- adds progress bar to cp, from http://www.beatex.org/web/advancedcopy.html
[packages/coreutils.git] / coreutils-advcopy.patch
1 diff -crB coreutils-8.4/src/copy.c coreutils-8.4-advcopy/src/copy.c
2 *** coreutils-8.4/src/copy.c    2010-01-03 18:06:20.000000000 +0100
3 --- coreutils-8.4-advcopy/src/copy.c    2010-01-25 10:54:50.000000000 +0100
4 ***************
5 *** 457,462 ****
6 --- 457,512 ----
7     return lchmod (name, mode);
8   }
9   
10 + /* BEGIN progress mod */
11 + static void file_progress_bar ( char * _cDest, int _iBarLength, int _iProgress, int _iTotal )
12 + {
13 +   // write number to progress bar
14 +   float fPercent = ( float ) _iProgress / ( float ) _iTotal * 100.f;
15 +   sprintf ( _cDest + ( _iBarLength - 6 ), "%4.1f", fPercent );
16 +   // remove zero
17 +   _cDest[_iBarLength - 2] = ' ';
18 +   
19 +   // fill rest with '-'
20 +   int i;
21 +   for ( i = 1; i <= _iBarLength - 9; i++ )
22 +   {
23 +     if ( fPercent > ( float ) ( i - 1 ) / ( _iBarLength - 10 ) * 100.f )
24 +       _cDest[i] = '|';
25 +     else
26 +       _cDest[i] = '-';
27 +   }
28 + }
29
30 + int file_size_format ( char * _cDst, int _iSize, int _iCounter )
31 + {
32 +   int iCounter = _iCounter;
33 +   double dSize = ( double ) _iSize;
34 +   while ( dSize >= 1000. )
35 +   {
36 +     dSize /= 1024.;
37 +     iCounter++;
38 +   }
39 +   
40 +   /* get unit */
41 +   char * sUnit;
42 +   if ( iCounter == 0 )
43 +     sUnit = "B";
44 +   else if ( iCounter == 1 )
45 +     sUnit = "KiB";
46 +   else if ( iCounter == 2 )
47 +     sUnit = "MiB";
48 +   else if ( iCounter == 3 )
49 +     sUnit = "GiB";
50 +   else if ( iCounter == 4 )
51 +     sUnit = "TiB";
52 +   else
53 +     sUnit = "N/A";
54
55 +   /* write number */
56 +   return sprintf ( _cDst, "%5.1f %s", dSize, sUnit );
57 + }
58 + /* END progress mod */
59
60   /* Copy a regular file from SRC_NAME to DST_NAME.
61      If the source file contains holes, copies holes and blocks of zeros
62      in the source file as holes in the destination file.
63 ***************
64 *** 630,636 ****
65         return_val = false;
66         goto close_src_and_dst_desc;
67       }
68
69     if (x->reflink_mode)
70       {
71         bool clone_ok = clone_file (dest_desc, source_desc) == 0;
72 --- 680,686 ----
73         return_val = false;
74         goto close_src_and_dst_desc;
75       }
76 !   
77     if (x->reflink_mode)
78       {
79         bool clone_ok = clone_file (dest_desc, source_desc) == 0;
80 ***************
81 *** 706,713 ****
82 --- 756,866 ----
83         buf_alloc = xmalloc (buf_size + buf_alignment_slop);
84         buf = ptr_align (buf_alloc, buf_alignment);
85   
86 +       /* BEGIN progress mod */
87 +       /* create a field of 6 lines */
88 +       char ** cProgressField = ( char ** ) calloc ( 6, sizeof ( char * ) );
89 +       /* get console width */
90 +       int iBarLength = 80;
91 +       struct winsize win;
92 +       if ( ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win) == 0 && win.ws_col > 0 )
93 +          iBarLength = win.ws_col;
94 +       /* create rows */
95 +       int it;
96 +       for ( it = 0; it < 6; it++ )
97 +       {
98 +         cProgressField[it] = ( char * ) malloc ( iBarLength + 1 );
99 +         /* init with spaces */
100 +         int j;
101 +         for ( j = 0; j < iBarLength; j++ )
102 +           cProgressField[it][j] = ' ';
103 +         cProgressField[it][iBarLength] = '\0';
104 +       }
105 +       
106 +       /* global progress bar? */
107 +       if ( g_iTotalSize )
108 +       {
109 +         /* init global progress bar */
110 +         cProgressField[2][0] = '[';
111 +         cProgressField[2][iBarLength - 8] = ']';
112 +         cProgressField[2][iBarLength - 7] = ' ';
113 +         cProgressField[2][iBarLength - 1] = '%';
114 +         
115 +         /* total size */
116 +         cProgressField[1][iBarLength - 11] = '/';
117 +         file_size_format ( cProgressField[1] + iBarLength - 9, g_iTotalSize, 1 );
118 +         
119 +         /* show how many files were written */
120 +         int sum_length = sprintf ( cProgressField[1], "%d files copied so far...", g_iFilesCopied );
121 +         cProgressField[1][sum_length] = ' ';
122 +       }
123 +       
124 +       /* truncate filename? */
125 +       int fn_length;
126 +       if ( strlen ( src_name ) > iBarLength - 22 )
127 +         fn_length =
128 +           sprintf ( cProgressField[4], "...%s", src_name + ( strlen ( src_name ) - iBarLength + 25 ) );
129 +       else
130 +         fn_length = sprintf ( cProgressField[4], "%s", src_name );
131 +       cProgressField[4][fn_length] = ' ';
132 +       
133 +       /* filesize */
134 +       cProgressField[4][iBarLength - 11] = '/';
135 +       file_size_format ( cProgressField[4] + iBarLength - 9, src_open_sb.st_size, 0 );
136 +       
137 +       int iCountDown = 1;
138 +       char * sProgressBar = cProgressField[5];
139 +       sProgressBar[0] = '[';
140 +       sProgressBar[iBarLength - 8] = ']';
141 +       sProgressBar[iBarLength - 7] = ' ';
142 +       sProgressBar[iBarLength - 1] = '%';
143 +       /* END progress mod */
144 +       
145         for (;;)
146           {
147 +           /* BEGIN progress mod */
148 +           /* update countdown */
149 +           iCountDown--;
150 +           if ( iCountDown < 0 )
151 +             iCountDown = 100;
152 +           
153 +           /* just print one line with the percentage, but not always */
154 +           if ( iCountDown == 0 )
155 +           {
156 +             int fs_len;
157 +             if ( g_iTotalSize )
158 +             {
159 +               /* global progress bar */
160 +               file_progress_bar ( cProgressField[2], iBarLength,
161 +                                   g_iTotalWritten + n_read_total / 1024, g_iTotalSize );
162 +               
163 +               /* print the global status */
164 +               fs_len = file_size_format ( cProgressField[1] + iBarLength - 21,
165 +                                               g_iTotalWritten + n_read_total / 1024, 1 );
166 +               cProgressField[1][iBarLength - 21 + fs_len] = ' ';
167 +             }
168 +             
169 +             /* current progress bar */
170 +             file_progress_bar ( sProgressBar, iBarLength, n_read_total, src_open_sb.st_size );
171 +             
172 +             /* print the status */
173 +             fs_len = file_size_format ( cProgressField[4] + iBarLength - 21, n_read_total, 0 );
174 +             cProgressField[4][iBarLength - 21 + fs_len] = ' ';
175 +             
176 +             /* print the field */
177 +             for ( it = g_iTotalSize ? 0 : 3; it < 6; it++ )
178 +             {
179 +               printf ( "%s\n", cProgressField[it] );
180 +               if ( strlen ( cProgressField[it] ) < iBarLength )
181 +                 printf ( "" );
182 +             }
183 +             if ( g_iTotalSize )
184 +               printf ( "\r\033[6A" );
185 +             else
186 +               printf ( "\r\033[3A" );
187 +             fflush ( stdout );
188 +           }
189 +           /* END progress mod */
190 +           
191             word *wp = NULL;
192   
193             ssize_t n_read = read (source_desc, buf, buf_size);
194 ***************
195 *** 788,793 ****
196 --- 941,957 ----
197                    /proc with linux kernels from at least 2.6.9 .. 2.6.29.  */
198               }
199           }
200 +       /* BEGIN progress mod */
201 +       /* update total size */
202 +       g_iTotalWritten += n_read_total / 1024;
203 +       g_iFilesCopied++;
204 +        
205 +       int i;
206 +       for ( i = 0; i < 6; i++ )
207 +         free ( cProgressField[i] );
208 +       free ( cProgressField );
209 +       /* END progress mod */
210
211   
212         /* If the file ends with a `hole', we need to do something to record
213            the length of the file.  On modern systems, calling ftruncate does
214 diff -crB coreutils-8.4/src/copy.h coreutils-8.4-advcopy/src/copy.h
215 *** coreutils-8.4/src/copy.h    2010-01-03 18:06:20.000000000 +0100
216 --- coreutils-8.4-advcopy/src/copy.h    2010-01-25 10:54:50.000000000 +0100
217 ***************
218 *** 280,283 ****
219 --- 280,291 ----
220   bool chown_failure_ok (struct cp_options const *);
221   mode_t cached_umask (void);
222   
223 + /* BEGIN progress mod */
224 + int file_size_format ( char * _cDst, int _iSize, int _iCounter );
225
226 + long g_iTotalSize;
227 + long g_iTotalWritten;
228 + int g_iFilesCopied;
229 + /* END progress mod */
230
231   #endif
232 diff -crB coreutils-8.4/src/cp.c coreutils-8.4-advcopy/src/cp.c
233 *** coreutils-8.4/src/cp.c      2010-01-04 16:46:06.000000000 +0100
234 --- coreutils-8.4-advcopy/src/cp.c      2010-01-25 10:54:50.000000000 +0100
235 ***************
236 *** 611,616 ****
237 --- 611,626 ----
238           error (EXIT_FAILURE, 0, _("target %s is not a directory"),
239                  quote (file[n_files - 1]));
240       }
241 +     
242 +     /* BEGIN progress mod */
243 +     g_iTotalSize = 0;
244 +     g_iFilesCopied = 0;
245 +     g_iTotalWritten = 0;
246 +     
247 +     /* save time */
248 +     struct timeval start_time;
249 +     gettimeofday ( & start_time, NULL );
250 +     /* END progress mod */
251   
252     if (target_directory)
253       {
254 ***************
255 *** 627,632 ****
256 --- 637,680 ----
257             dest_info_init (x);
258             src_info_init (x);
259           }
260 +         
261 +       /* BEGIN progress mod */
262 +       printf ( "Calculating total size... \r" );
263 +       fflush ( stdout );
264 +       long iTotalSize = 0;
265 +       for (i = 0; i < n_files; i++)
266 +       {
267 +         /* call du -s for each file */
268 +         /* create command */
269 +         char command[1024];
270 +         sprintf ( command, "/usr/bin/du -s \"%s\"", file[i] );
271 +         /* TODO: replace all quote signs in file[i] */
272 +         
273 +         FILE *fp;
274 +         char output[1024];
275
276 +         /* run command */
277 +         fp = popen(command, "r");
278 +         if (fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL) {
279 +           printf("failed to run du.\n" );
280 +         }
281 +         else
282 +         {
283 +           /* isolate size */
284 +           strchr ( output, '\t' )[0] = '\0';
285 +           iTotalSize += atol ( output );
286 +           
287 +           printf ( "Calculating total size... %d\r", iTotalSize );
288 +           fflush ( stdout );
289 +         }
290 +         
291 +         /* close */
292 +         pclose(fp);
293 +       }
294 +       g_iTotalSize = iTotalSize;
295 +       g_iTotalWritten = 0;
296 +       g_iFilesCopied = 0;
297 +       /* END progress mod */
298   
299         for (i = 0; i < n_files; i++)
300           {
301 ***************
302 *** 753,758 ****
303 --- 801,844 ----
304   
305         ok = copy (source, new_dest, 0, x, &unused, NULL);
306       }
307 +     
308 +     /* BEGIN progress mod */
309 +     /* remove everything */
310 +     int i;
311 +     if ( g_iTotalSize )
312 +     {
313 +       for ( i = 0; i < 6; i++ )
314 +         printf ( "\033[K\n" );
315 +       printf ( "\r\033[6A" );
316 +     }
317 +     else
318 +     {
319 +       for ( i = 0; i < 3; i++ )
320 +         printf ( "\033[K\n" );
321 +       printf ( "\r\033[3A" );
322 +     }
323 +     
324 +     /* save time */
325 +     struct timeval end_time;
326 +     gettimeofday ( & end_time, NULL );
327 +     int usec_elapsed = end_time.tv_usec - start_time.tv_usec;
328 +     double sec_elapsed = ( double ) usec_elapsed / 1000000.f;
329 +     sec_elapsed += ( double ) ( end_time.tv_sec - start_time.tv_sec );
330 +     
331 +     /* get total size */
332 +     char sTotalWritten[20];
333 +     file_size_format ( sTotalWritten, g_iTotalSize, 1 );
334 +     /* TODO: using g_iTotalWritten would be more correct, but is less accurate */
335 +     
336 +     /* calculate speed */
337 +     int copy_speed = ( int ) ( ( double ) g_iTotalWritten / sec_elapsed );
338 +     char s_copy_speed[20];
339 +     file_size_format ( s_copy_speed, copy_speed, 1 );
340 +     
341 +     /* good-bye message */
342 +     printf ( "%d files (%s) copied in %.1f seconds (%s/s).\n", g_iFilesCopied, sTotalWritten, 
343 +              sec_elapsed, s_copy_speed );
344 +     /* END progress mod */
345   
346     return ok;
347   }
This page took 0.084937 seconds and 4 git commands to generate.