pastebin - collaborative debugging tool
kpaste.net RSS


FAT32 copy perl script
Posted by Anonymous on Fri 5th Oct 2012 23:19
raw | new post

  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 $fat32_limit = 4294967295;
  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`))[3]);
  26. my $size = (stat($infile))[7];
  27.  
  28. say "$free bytes free in $outdir";
  29. say "$infile is $size bytes";
  30.  
  31. if ($size > $free) {
  32.         say "Not enough free space in '$outdir'";
  33.         exit;
  34. }
  35.  
  36. if ($size > $fat32_limit) {
  37.         my $parts = ceil(( $size / $fat32_limit ));
  38.  
  39.         open(INFILE, '<:raw', $infile) or die "Cant open $infile: $!";
  40.  
  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.                 open(OUTFILE, '>>:raw', $outfile)
  54.                 or die "Cant open $outfile: $!";
  55.                 my $temp_data;
  56.                 my $ram_disk = 1000000000;
  57.                 my $ram_buffer = $fat32_limit;
  58.                 my $ram_limit;
  59.                 while ( $ram_buffer ) {
  60.  
  61.                         if ($ram_buffer >= $ram_disk) {
  62.                                 $ram_buffer -= $ram_disk;
  63.                                 $ram_limit = $ram_disk;
  64.                         } else {
  65.                                 $ram_limit = $ram_buffer;
  66.                                 undef($ram_buffer);
  67.                         }
  68.                         sysread(INFILE, $temp_data, $ram_limit);
  69.                         print OUTFILE $temp_data;
  70.                         undef($temp_data);
  71.                 }
  72.                 close(OUTFILE) or die "Cant close $outfile: $!";
  73.         }
  74.         close(INFILE) or die "Cant close $infile: $!";
  75. } else {
  76.         $outfile = $outdir . '/' . basename($infile);
  77.  
  78.         if (! -f $outfile) {
  79.                 say "${outfile}: writing...";
  80.                 copy($infile, $outfile) or die "Can't copy '$infile': $!";
  81.         } else { say "${outfile}: already exists!"; exit; }
  82. }
  83.  
  84. 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