update readme and add gitignore
[client-tools.git] / tools / trickle_data_patch.pl
blob3438e0d12404633a250e5a28a277283707fcea58
1 #! /usr/bin/perl
3 use warnings;
4 use strict;
6 die "usage: $0 [pieces] [filename]\n" if (@ARGV != 2);
8 my $pieces = shift;
9 my $file = shift;
11 die "$file does not exist\n" if (-e $file == 0);
13 my $copyLoopSize = 16 * 1024;
14 my $copyLoops = ((-s $file) / $copyLoopSize) / $pieces;
16 # write out all the interim files, excluding the last (since it's the entire file)
17 foreach my $piece (1 .. $pieces-1)
19 open(IN, $file);
20 binmode(IN);
22 my $dir = "piece" . $piece;
23 mkdir($dir);
24 open(OUT, ">" . $dir . "/" . $file);
25 binmode(OUT);
27 foreach (1 .. ($piece * $copyLoops))
29 my $buffer;
30 read IN, $buffer, $copyLoopSize;
31 print OUT $buffer;
34 close(IN);
35 close(OUT);
38 # copy the last piece
40 open(IN, $file);
41 binmode(IN);
43 my $dir = "piece" . $pieces;
44 mkdir($dir);
45 open(OUT, ">" . $dir . "/" . $file);
46 binmode(OUT);
48 while (1)
50 my $buffer;
51 read IN, $buffer, $copyLoopSize;
52 last if (length $buffer == 0);
53 print OUT $buffer;
56 close(IN);
57 close(OUT);
60 system("md5sum -b $file piece*/*");