1 package v3_checkpoint_file;
6 @EXPORT = qw(v3_read_checkpoint_from_binary_file
7 v3_read_checkpoint_from_binary_dir
8 v3_read_checkpoint_from_ini_file
9 v3_read_checkpoint_from_ini_dir
10 v3_write_checkpoint_as_ini_file
11 v3_write_checkpoint_as_ini_dir
12 v3_write_checkpoint_as_binary_dir);
14 my $boundary=0xabcd0123;
23 # tags=>[list of tags in file, in order]
27 # dies if it doesn't work
29 sub v3_read_checkpoint_from_binary_file {
40 open(FILE,$file) or die "cannot open $file\n";
45 if (sysread(FILE, $buf, 4)!=4) {
50 $bt==$boundary or die "Boundary mismatch (read $bt)\n";
52 sysread(FILE,$buf,8)==8 or die "Can't read taglen\n";
53 $taglen=unpack("Q",$buf);
55 sysread(FILE,$curtag,$taglen)==$taglen or die "Can't read tag\n";
57 sysread(FILE,$buf,8)==8 or die "Can't read datalen\n";
58 $datalen=unpack("Q",$buf);
60 sysread(FILE,$buf,$datalen)==$datalen or die "Can't read data\n";
61 push @{$h{tags}},$curtag;
70 return 1 if ($k eq $key);
75 # Takes: dirname [optional list of keys/files to skip]
78 # keys=>[list of keys in dir, in order]
79 # keyname => [ tags=>[list of tags in file, in order],
83 # dies if it doesn't work
85 sub v3_read_checkpoint_from_binary_dir {
86 my ($dir,@skiplist)=@_;
87 my @files = `ls -1 $dir`; chomp(@files);
94 foreach $file (@files) {
96 next if inlist($key,@skiplist);
97 $h{$key} = v3_read_checkpoint_from_binary_file("$dir/$file");
98 push @{$h{keys}}, $key;
110 # tags=>[list of tags in file, in order]
114 # dies if it doesn't work
116 sub v3_read_checkpoint_from_ini_file {
124 open(FILE,$file) or die "cannot open $file\n";
127 while ($line=<FILE>) {
129 if ($line=~/\[(.*)\]$/) {
134 defined($key) or die "cannot find key\n";
136 while ($line=<FILE>) {
138 if ($line=~/^(.*)\=(.*)$/) {
139 $h{$1}=pack("H*",$2);
140 push @{$h{tags}}, $1;
151 # Takes: dirname [optional list of keys/files to skip]
154 # keys=>[list of keys in dir, in order]
155 # keyname => [ tags=>[list of tags in file, in order],
159 # dies if it doesn't work
161 sub v3_read_checkpoint_from_ini_dir {
162 my ($dir,@skiplist)=@_;
163 my @files = `ls -1 $dir`; chomp(@files);
170 foreach $file (@files) {
172 next if inlist($key,@skiplist);
173 $h{$key} = v3_read_checkpoint_from_ini_file("$dir/$file");
174 push @{$h{keys}}, $key;
181 sub v3_write_checkpoint_as_ini_stream {
182 my ($key,$hr,$stream)=@_;
185 print $stream "[$key]\n" if defined($key);
187 foreach $tag (@{$hr->{tags}}) {
188 print $stream "$tag=".unpack("H*",$hr->{$tag})."\n";
193 # input: href, either for individual key, or group of keys
195 # [key optional for single key]
197 sub v3_write_checkpoint_as_ini_file {
198 my ($hr,$file,$key)=@_;
200 open(FILE, ">$file") or die "cannot open $file\n";
202 if (defined($hr->{keys})) {
203 # this is a whole checkpont we are being asked to write
204 foreach $key (@{$hr->{keys}}) {
205 v3_write_checkpoint_as_ini_stream($key,$hr->{$key},FILE);
208 # this is an individual file, and we don't know it's keyname.
209 # caller must have already wrote it.
210 v3_write_checkpoint_as_ini_stream($key,$hr,FILE);
216 # input: href, either for individual key, or group of keys
220 sub v3_write_checkpoint_as_ini_dir {
225 defined($hr->{keys}) or die "cannot write dir without key list\n";
227 mkdir($dir); # OK if it already exists
229 foreach my $key (@{$hr->{keys}}) {
230 open(FILE,">$dir/$key") or die "cannot open $dir/$key\n";
231 v3_write_checkpoint_as_ini_stream($key,$hr->{$key},FILE);
237 # input: href, either for individual key, or group of keys
240 sub v3_write_checkpoint_as_binary_file {
247 open(FILE, ">$file") or die "cannot open $file\n";
250 foreach $tag (@{$hr->{tags}}) {
251 $buf=pack("L",$boundary);
252 syswrite(FILE,$buf,4)==4 or die "cannot write boundary tag\n";
253 $taglen=length($tag);
254 $buf=pack("Q",$taglen);
255 syswrite(FILE,$buf,8)==8 or die "cannot write tag length\n";
256 syswrite(FILE,$tag,$taglen)==$taglen or die "cannot write tag\n";
257 $datalen=length($hr->{$tag});
258 $buf=pack("Q",$datalen);
259 syswrite(FILE,$buf,8)==8 or die "cannot write data length\n";
260 syswrite(FILE,$hr->{$tag},$datalen)==$datalen or die "cannot write data\n";
266 # input: href, either for individual key, or group of keys
270 sub v3_write_checkpoint_as_binary_dir {
275 defined($hr->{keys}) or die "cannot write dir without key list\n";
277 mkdir($dir); # OK if it already exists
279 foreach my $key (@{$hr->{keys}}) {
280 v3_write_checkpoint_as_binary_file($hr->{$key},"$dir/$key");