pastebin - collaborative debugging tool
kpaste.net RSS


FAT32 copy perl script
Posted by Anonymous on Tue 18th Sep 2012 03:46
raw | new post
view followups (newest first): FAT32 copy perl script by Anonymous

  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.                 }
  70.         }
  71. } else {
  72.         $outfile = $outdir . '/' . basename($infile);
  73.  
  74.         if (! -f $outfile) {
  75.                 say "${outfile}: writing...";
  76.                 copy($infile, $outfile) or die "Can't copy '$infile': $!";
  77.         } else { say "${outfile}: already exists!"; exit; }
  78. }
  79.  
  80. 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