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