pastebin - collaborative debugging tool
kpaste.net RSS


FAT32 copy perl script
Posted by Anonymous on Tue 18th Sep 2012 03:56
raw | new post
modification of post by Anonymous (view diff)

  1. #!/usr/bin/perl
  2.  
  3. use 5.14.0;
  4. use strict;
  5. use warnings;
  6. use Cwd qw(abs_path);
  7. use File::Basename qw(basename);
  8. use File::Copy qw(copy);
  9. use POSIX qw(ceil);
  10.  
  11. sub usage {
  12.         say "Usage: " . basename($0) . " [ infile ] [ outdir ]";
  13.         exit;
  14. }
  15.  
  16. if (!$ARGV[0] || !$ARGV[1]) { usage(); }
  17. elsif (! -f $ARGV[0] || ! -d $ARGV[1]) { usage(); }
  18.  
  19. my $limit = int((4294967295 / 1024 / 1024));
  20. my $infile = abs_path($ARGV[0]);
  21. my $outfile;
  22. my $outdir = abs_path($ARGV[1]);
  23.  
  24. chomp(my @free = split(' ',
  25. `df --block-size=1 "$outdir" | tail --lines 1`));
  26. my $free = int(($free[3] / 1024 / 1024));
  27. my $size = ceil(((stat($infile))[7] / 1024 / 1024));
  28.  
  29. say "$free megabytes free in $outdir";
  30. say "$infile is $size megabytes";
  31.  
  32. if ($size > $free) {
  33.         say "Not enough free space in '$outdir'";
  34.         exit;
  35. }
  36.  
  37. if ($size > $limit) {
  38.         my $parts = ceil(( $size / $limit ));
  39.         my $last = $parts;
  40.         my $skip = 0;
  41.         foreach my $n (1 .. $parts) {
  42.  
  43.                 $outfile = $outdir . '/' . basename($infile) . '.part' .
  44.                 $n;
  45.  
  46.                 if (-f $outfile) {
  47.                         say "${outfile}: already exists!";
  48.                         exit;
  49.                 }
  50.  
  51.                 say "${outfile}: writing...";
  52.  
  53.                 if ($n == 1) {
  54.                         system('dd', 'if=' . "$infile", 'of=' . "$outfile",
  55.                         'bs=1MB', 'count=' . $limit);
  56.                         $skip += $limit;
  57.                 } elsif ($n == $last) {
  58.                         system('dd', 'if=' . "$infile", 'of=' . "$outfile",
  59.                         'bs=1MB', 'skip=' . "$skip");
  60.                 } else {
  61.                         system('dd', 'if=' . "$infile", 'of=' . "$outfile",
  62.                         'bs=1MB', 'skip=' . "$skip", 'count=' . $limit);
  63.                         $skip += $limit;
  64.                 }
  65.  
  66.                 if ($? != 0 && $? != 2) {
  67.                         say "Something went wrong with 'dd': $!";
  68.                         exit;
  69.                 } elsif ($? == 2) {
  70.                         say "Interrupted by user!";
  71.                         exit;
  72.                 }
  73.         }
  74. } else {
  75.         $outfile = $outdir . '/' . basename($infile);
  76.  
  77.         if (! -f $outfile) {
  78.                 say "${outfile}: writing...";
  79.                 copy($infile, $outfile) or die "Can't copy '$infile': $!";
  80.         } else { say "${outfile}: already exists!"; exit; }
  81. }
  82.  
  83. say "\nDone copying file!";

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at