pastebin - collaborative debugging tool
kpaste.net RSS

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

    
11
#!/usr/bin/perl
22
33
use 5.14.0;
44
use strict;
55
use warnings;
66
use Cwd qw(abs_path);
77
use File::Basename qw(basename);
88
use File::Copy qw(copy);
99
use POSIX qw(ceil);
1010
1111
sub usage {
1212
	say "Usage: " . basename($0) . " [ infile ] [ outdir ]";
1313
	exit;
1414
}
1515
1616
if (!$ARGV[0] || !$ARGV[1]) { usage(); }
1717
elsif (! -f $ARGV[0] || ! -d $ARGV[1]) { usage(); }
1818
1919
my $limit = int((4294967295 / 1024 / 1024));
2020
my $infile = abs_path($ARGV[0]);
2121
my $outfile;
2222
my $outdir = abs_path($ARGV[1]);
2323
2424
chomp(my @free = split(' ',
2525
`df --block-size=1 "$outdir" | tail --lines 1`));
2626
my $free = int(($free[3] / 1024 / 1024));
2727
my $size = ceil(((stat($infile))[7] / 1024 / 1024));
2828
2929
say "$free megabytes free in $outdir";
3030
say "$infile is $size megabytes";
3131
3232
if ($size > $free) {
3333
	say "Not enough free space in '$outdir'";
3434
	exit;
3535
}
3636
3737
if ($size > $limit) {
3838
	my $parts = ceil(( $size / $limit ));
3939
	my $last = $parts;
4040
	my $skip = 0;
4141
	foreach my $n (1 .. $parts) {
4242
4343
		$outfile = $outdir . '/' . basename($infile) . '.part' .
4444
		$n;
4545
4646
		if (-f $outfile) {
4747
			say "${outfile}: already exists!";
4848
			exit;
4949
		}
5050
5151
		say "${outfile}: writing...";
5252
5353
		if ($n == 1) {
5454
			system('dd', 'if=' . "$infile", 'of=' . "$outfile",
5555
			'bs=1MB', 'count=' . $limit);
5656
			$skip += $limit;
5757
		} elsif ($n == $last) {
5858
			system('dd', 'if=' . "$infile", 'of=' . "$outfile",
5959
			'bs=1MB', 'skip=' . "$skip");
6060
		} else {
6161
			system('dd', 'if=' . "$infile", 'of=' . "$outfile",
6262
			'bs=1MB', 'skip=' . "$skip", 'count=' . $limit);
6363
			$skip += $limit;
6464
		}
6565
6666
		if ($? != 0 && $? != 2) {
6767
			say "Something went wrong with 'dd': $!";
6868
			exit;
69+
		} elsif ($? == 2) {
6972
		}
7073
	}
7174
} else {
7275
	$outfile = $outdir . '/' . basename($infile);
7376
7477
	if (! -f $outfile) {
7578
		say "${outfile}: writing...";
7679
		copy($infile, $outfile) or die "Can't copy '$infile': $!";
7780
	} else { say "${outfile}: already exists!"; exit; }
7881
}
7982
8083
say "\nDone copying file!";
8184

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