Advertisement
jorge_barroso

advcpmv patch

Aug 9th, 2012
55
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff -ru coreutils-8.4/src/copy.c coreutils-8.4-1/src/copy.c
  2. --- coreutils-8.4/src/copy.c    2010-01-03 18:06:20.000000000 +0100
  3. +++ coreutils-8.4-1/src/copy.c  2010-08-25 21:11:40.629752495 +0200
  4. @@ -457,6 +457,56 @@
  5.    return lchmod (name, mode);
  6.  }
  7.  
  8. +/* BEGIN progress mod */
  9. +static void file_progress_bar ( char * _cDest, int _iBarLength, int _iProgress, int _iTotal )
  10. +{
  11. +  // write number to progress bar
  12. +  float fPercent = ( float ) _iProgress / ( float ) _iTotal * 100.f;
  13. +  sprintf ( _cDest + ( _iBarLength - 6 ), "%4.1f", fPercent );
  14. +  // remove zero
  15. +  _cDest[_iBarLength - 2] = ' ';
  16. +
  17. +  // fill rest with '-'
  18. +  int i;
  19. +  for ( i = 1; i <= _iBarLength - 9; i++ )
  20. +  {
  21. +    if ( fPercent > ( float ) ( i - 1 ) / ( _iBarLength - 10 ) * 100.f )
  22. +      _cDest[i] = '|';
  23. +    else
  24. +      _cDest[i] = '-';
  25. +  }
  26. +}
  27. +
  28. +int file_size_format ( char * _cDst, int _iSize, int _iCounter )
  29. +{
  30. +  int iCounter = _iCounter;
  31. +  double dSize = ( double ) _iSize;
  32. +  while ( dSize >= 1000. )
  33. +  {
  34. +    dSize /= 1024.;
  35. +    iCounter++;
  36. +  }
  37. +
  38. +  /* get unit */
  39. +  char * sUnit;
  40. +  if ( iCounter == 0 )
  41. +    sUnit = "B";
  42. +  else if ( iCounter == 1 )
  43. +    sUnit = "KiB";
  44. +  else if ( iCounter == 2 )
  45. +    sUnit = "MiB";
  46. +  else if ( iCounter == 3 )
  47. +    sUnit = "GiB";
  48. +  else if ( iCounter == 4 )
  49. +    sUnit = "TiB";
  50. +  else
  51. +    sUnit = "N/A";
  52. +
  53. +  /* write number */
  54. +  return sprintf ( _cDst, "%5.1f %s", dSize, sUnit );
  55. +}
  56. +/* END progress mod */
  57. +
  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.
  61. @@ -706,8 +756,146 @@
  62.        buf_alloc = xmalloc (buf_size + buf_alignment_slop);
  63.        buf = ptr_align (buf_alloc, buf_alignment);
  64.  
  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;
  70. +      struct winsize win;
  71. +      if ( ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win) == 0 && win.ws_col > 0 )
  72. +         iBarLength = win.ws_col;
  73. +      /* create rows */
  74. +      int it;
  75. +      for ( it = 0; it < 6; it++ )
  76. +      {
  77. +        cProgressField[it] = ( char * ) malloc ( iBarLength + 1 );
  78. +        /* init with spaces */
  79. +        int j;
  80. +        for ( j = 0; j < iBarLength; j++ )
  81. +          cProgressField[it][j] = ' ';
  82. +        cProgressField[it][iBarLength] = '\0';
  83. +      }
  84. +
  85. +      /* global progress bar? */
  86. +      if ( g_iTotalSize )
  87. +      {
  88. +        /* init global progress bar */
  89. +        cProgressField[2][0] = '[';
  90. +        cProgressField[2][iBarLength - 8] = ']';
  91. +        cProgressField[2][iBarLength - 7] = ' ';
  92. +        cProgressField[2][iBarLength - 1] = '%';
  93. +
  94. +        /* total size */
  95. +        cProgressField[1][iBarLength - 11] = '/';
  96. +        file_size_format ( cProgressField[1] + iBarLength - 9, g_iTotalSize, 1 );
  97. +
  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] = ' ';
  101. +      }
  102. +
  103. +      /* truncate filename? */
  104. +      int fn_length;
  105. +      if ( strlen ( src_name ) > iBarLength - 22 )
  106. +        fn_length =
  107. +          sprintf ( cProgressField[4], "...%s", src_name + ( strlen ( src_name ) - iBarLength + 25 ) );
  108. +      else
  109. +        fn_length = sprintf ( cProgressField[4], "%s", src_name );
  110. +      cProgressField[4][fn_length] = ' ';
  111. +
  112. +      /* filesize */
  113. +      cProgressField[4][iBarLength - 11] = '/';
  114. +      file_size_format ( cProgressField[4] + iBarLength - 9, src_open_sb.st_size, 0 );
  115. +
  116. +      int iCountDown = 1;
  117. +      char * sProgressBar = cProgressField[5];
  118. +      sProgressBar[0] = '[';
  119. +      sProgressBar[iBarLength - 8] = ']';
  120. +      sProgressBar[iBarLength - 7] = ' ';
  121. +      sProgressBar[iBarLength - 1] = '%';
  122. +
  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 */
  128. +
  129.        for (;;)
  130.          {
  131. +          if (progress) {
  132. +          /* BEGIN progress mod */
  133. +          /* update countdown */
  134. +          iCountDown--;
  135. +          if ( iCountDown < 0 )
  136. +            iCountDown = 100;
  137. +
  138. +          /* just print one line with the percentage, but not always */
  139. +          if ( iCountDown == 0 )
  140. +          {
  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 )
  149. +              / sec_elapsed );
  150. +            char s_copy_speed[20];
  151. +            file_size_format ( s_copy_speed, copy_speed, 1 );
  152. +            /* update vars */
  153. +            last_time = cur_time;
  154. +            last_size = cur_size;
  155. +
  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;
  164. +            /* print out */
  165. +            sprintf ( cProgressField[3],
  166. +              "Copying at %s/s (about %dh %dm %ds remaining)", s_copy_speed,
  167. +              hours_remaining, min_remaining, sec_remaining );
  168. +
  169. +            int fs_len;
  170. +            if ( g_iTotalSize )
  171. +            {
  172. +              /* global progress bar */
  173. +              file_progress_bar ( cProgressField[2], iBarLength,
  174. +                                  g_iTotalWritten + n_read_total / 1024, g_iTotalSize );
  175. +
  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] = ' ';
  180. +            }
  181. +
  182. +            /* current progress bar */
  183. +            file_progress_bar ( sProgressBar, iBarLength, n_read_total, src_open_sb.st_size );
  184. +
  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] = ' ';
  188. +
  189. +            /* print the field */
  190. +            for ( it = g_iTotalSize ? 0 : 3; it < 6; it++ )
  191. +            {
  192. +              printf ( "\033[K%s\n", cProgressField[it] );
  193. +              if ( strlen ( cProgressField[it] ) < iBarLength )
  194. +                printf ( "" );
  195. +            }
  196. +            if ( g_iTotalSize )
  197. +              printf ( "\r\033[6A" );
  198. +            else
  199. +              printf ( "\r\033[3A" );
  200. +            fflush ( stdout );
  201. +          }
  202. +          /* END progress mod */
  203. +          }
  204. +
  205.            word *wp = NULL;
  206.  
  207.            ssize_t n_read = read (source_desc, buf, buf_size);
  208. @@ -788,6 +976,19 @@
  209.                   /proc with linux kernels from at least 2.6.9 .. 2.6.29.  */
  210.              }
  211.          }
  212. +if (progress) {
  213. +      /* BEGIN progress mod */
  214. +      /* update total size */
  215. +      g_iTotalWritten += n_read_total / 1024;
  216. +      g_iFilesCopied++;
  217. +
  218. +      int i;
  219. +      for ( i = 0; i < 6; i++ )
  220. +        free ( cProgressField[i] );
  221. +      free ( cProgressField );
  222. +      /* END progress mod */
  223. +}
  224. +
  225.  
  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.4/src/copy.h coreutils-8.4-1/src/copy.h
  229. --- coreutils-8.4/src/copy.h    2010-01-03 18:06:20.000000000 +0100
  230. +++ coreutils-8.4-1/src/copy.h  2010-08-25 21:11:40.629752495 +0200
  231. @@ -222,6 +222,9 @@
  232.      Create destination directories as usual. */
  233.   bool symbolic_link;
  234.  
  235. +  /* if true, draw a nice progress bar on screen */
  236. +  bool progress_bar;
  237. +
  238.   /* If true, do not copy a nondirectory that has an existing destination
  239.      with the same or newer modification time. */
  240.   bool update;
  241. @@ -280,4 +283,15 @@
  242. bool chown_failure_ok (struct cp_options const *);
  243. mode_t cached_umask (void);
  244.  
  245. +/* BEGIN progress mod */
  246. +int file_size_format ( char * _cDst, int _iSize, int _iCounter );
  247. +
  248. +long g_iTotalSize;
  249. +long g_iTotalWritten;
  250. +int g_iFilesCopied;
  251. +struct timeval g_oStartTime;
  252. +int g_iTotalFiles;
  253. +bool progress;
  254. +/* END progress mod */
  255. +
  256. #endif
  257. diff -ru coreutils-8.4/src/cp.c coreutils-8.4-1/src/cp.c
  258. --- coreutils-8.4/src/cp.c  2010-01-04 16:46:06.000000000 +0100
  259. +++ coreutils-8.4-1/src/cp.c    2010-08-25 21:11:40.629752495 +0200
  260. @@ -139,6 +139,7 @@
  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},
  267.   {NULL, 0, NULL, 0}
  268. @@ -176,6 +177,7 @@
  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\
  274.                                   option)\n\
  275.   -H                           follow command-line symbolic links in SOURCE\n\
  276. @@ -612,6 +614,57 @@
  277.                quote (file[n_files - 1]));
  278.     }
  279.  
  280. +    struct timeval start_time;
  281. +if (progress) {
  282. +    /* BEGIN progress mod */
  283. +    g_iTotalSize = 0;
  284. +    g_iFilesCopied = 0;
  285. +    g_iTotalWritten = 0;
  286. +
  287. +    /* save time */
  288. +    gettimeofday ( & start_time, NULL );
  289. +    g_oStartTime = start_time;
  290. +
  291. +    printf ( "Calculating total size... \r" );
  292. +    fflush ( stdout );
  293. +    long iTotalSize = 0;
  294. +    int iFiles = n_files;
  295. +    if ( ! target_directory )
  296. +      iFiles = n_files - 1;
  297. +    int j;
  298. +    for (j = 0; j < iFiles; j++)
  299. +    {
  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] */
  305. +
  306. +      FILE *fp;
  307. +      char output[1024];
  308. +
  309. +      /* run command */
  310. +      fp = popen(command, "r");
  311. +      if (fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL) {
  312. +        printf("failed to run du.\n" );
  313. +      }
  314. +      else
  315. +      {
  316. +        /* isolate size */
  317. +        strchr ( output, '\t' )[0] = '\0';
  318. +        iTotalSize += atol ( output );
  319. +
  320. +        printf ( "Calculating total size... %ld\r", iTotalSize );
  321. +        fflush ( stdout );
  322. +      }
  323. +
  324. +      /* close */
  325. +      pclose(fp);
  326. +    }
  327. +    g_iTotalSize = iTotalSize;
  328. +    /* END progress mod */
  329. +}
  330. +
  331.   if (target_directory)
  332.     {
  333.       /* cp file1...filen edir
  334. @@ -754,6 +807,46 @@
  335.       ok = copy (source, new_dest, 0, x, &unused, NULL);
  336.     }
  337.  
  338. +if (progress) {
  339. +    /* BEGIN progress mod */
  340. +    /* remove everything */
  341. +    int i;
  342. +    if ( g_iTotalSize )
  343. +    {
  344. +      for ( i = 0; i < 6; i++ )
  345. +        printf ( "\033[K\n" );
  346. +      printf ( "\r\033[6A" );
  347. +    }
  348. +    else
  349. +    {
  350. +      for ( i = 0; i < 3; i++ )
  351. +        printf ( "\033[K\n" );
  352. +      printf ( "\r\033[3A" );
  353. +    }
  354. +
  355. +    /* save time */
  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 );
  361. +
  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 */
  366. +
  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 );
  371. +
  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 */
  376. +}
  377. +
  378.   return ok;
  379. }
  380.  
  381. @@ -785,6 +878,7 @@
  382.   x->recursive = false;
  383.   x->sparse_mode = SPARSE_AUTO;
  384.   x->symbolic_link = false;
  385. +  x->progress_bar = false;
  386.   x->set_mode = false;
  387.   x->mode = 0;
  388.  
  389. @@ -923,7 +1017,7 @@
  390.      we'll actually use backup_suffix_string.  */
  391.    backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
  392.  
  393. -  while ((c = getopt_long (argc, argv, "abdfHilLnprst:uvxPRS:T",
  394. +  while ((c = getopt_long (argc, argv, "abdfgHilLnprst:uvxPRS:T",
  395.                             long_opts, NULL))
  396.           != -1)
  397.      {
  398. @@ -975,6 +1069,10 @@
  399.            x.unlink_dest_after_failed_open = true;
  400.            break;
  401.  
  402. +        case 'g':
  403. +          progress = true;
  404. +          break;
  405. +
  406.          case 'H':
  407.            x.dereference = DEREF_COMMAND_LINE_ARGUMENTS;
  408.            break;
  409. diff -ru coreutils-8.4/src/mv.c coreutils-8.4-1/src/mv.c
  410. --- coreutils-8.4/src/mv.c  2010-01-03 18:06:20.000000000 +0100
  411. +++ coreutils-8.4-1/src/mv.c    2010-08-25 21:11:40.629752495 +0200
  412. @@ -64,6 +64,7 @@
  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},
  419.    {NULL, 0, NULL, 0}
  420. @@ -159,13 +160,100 @@
  421.  static bool
  422.  do_move (const char *source, const char *dest, const struct cp_options *x)
  423.  {
  424. +
  425. +  struct timeval start_time;
  426. +
  427. +  if(progress) {
  428. +    /* BEGIN progress mod */
  429. +    g_iTotalSize = 0;
  430. +    g_iFilesCopied = 0;
  431. +    g_iTotalWritten = 0;
  432. +
  433. +    gettimeofday (& start_time, NULL);
  434. +    g_oStartTime = start_time;
  435. +
  436. +    printf ("Calculating total size... \r");
  437. +    fflush (stdout);
  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] */
  444. +
  445. +    FILE *fp;
  446. +    char output[1024];
  447. +
  448. +    /* run command */
  449. +    fp = popen(command, "r");
  450. +    if (fp == NULL || fgets(output, sizeof(output)-1, fp) == NULL) {
  451. +      printf("failed to run du.\n" );
  452. +    }
  453. +    else
  454. +    {
  455. +      /* isolate size */
  456. +      strchr ( output, '\t' )[0] = '\0';
  457. +      iTotalSize += atol ( output );
  458. +      printf ( "Calculating total size... %ld\r", iTotalSize );
  459. +      fflush ( stdout );
  460. +    }
  461. +
  462. +    /* close */
  463. +    pclose(fp);
  464. +    g_iTotalSize = iTotalSize;
  465. +    /* END progress mod */
  466. +
  467. +  }
  468. +
  469.    bool copy_into_self;
  470.    bool rename_succeeded;
  471.    bool ok = copy (source, dest, false, x, &copy_into_self, &rename_succeeded);
  472.  
  473. +  if (progress) {
  474. +    /* BEGIN progress mod */
  475. +    /* remove everything */
  476. +    int i;
  477. +    if ( g_iTotalSize )
  478. +    {
  479. +      for ( i = 0; i < 6; i++ )
  480. +        printf ( "\033[K\n" );
  481. +      printf ( "\r\033[6A" );
  482. +    }
  483. +    else
  484. +    {
  485. +      for ( i = 0; i < 3; i++ )
  486. +        printf ( "\033[K\n" );
  487. +      printf ( "\r\033[3A" );
  488. +    }
  489. +
  490. +    /* save time */
  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 );
  496. +
  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 */
  501. +
  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 );
  506. +
  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 */
  511. +  }
  512. +
  513.    if (ok)
  514.      {
  515.        char const *dir_to_remove;
  516. +
  517. +
  518.        if (copy_into_self)
  519.          {
  520.            /* In general, when copy returns with copy_into_self set, SOURCE is
  521. @@ -298,6 +386,7 @@
  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\
  529. @@ -366,7 +455,7 @@
  530.       we'll actually use backup_suffix_string.  */
  531.   backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
  532.  
  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))
  535.          != -1)
  536.     {
  537.       switch (c)
  538. @@ -411,6 +500,9 @@
  539.         case 'v':
  540.           x.verbose = true;
  541.           break;
  542. +   case 'g':
  543. +     progress = true;
  544. +     break;
  545.         case 'S':
  546.           make_backups = true;
  547.           backup_suffix_string = optarg;
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement