Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Library and tools for manipulating text and binary file ("KEYED STREAM file: / textfi...
[palacios.git] / linux_usr / checkpoint / v3_checkpoint_file.pm
1 package v3_checkpoint_file;
2
3 require Exporter;
4 @ISA = qw(Exporter);
5
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);
13
14 my $boundary=0xabcd0123;
15
16
17 #
18 # Takes: filename
19 #
20 #
21 # Returns:
22 # hashref
23 #   tags=>[list of tags in file, in order]
24 #   tagname=>blob
25 #   ..
26 #
27 # dies if it doesn't work
28 #
29 sub v3_read_checkpoint_from_binary_file {
30   $file = shift;
31   my %h;
32   my $buf;
33   my $bt;
34   my $taglen;
35   my $datalen;
36   my $curtag;
37
38   $h{tags}=[];
39
40   open(FILE,$file) or die "cannot open $file\n";
41   binmode(FILE);
42
43   while (1) { 
44     undef $buf;
45     if (sysread(FILE, $buf, 4)!=4) { 
46       close(FILE);
47       return \%h;
48     }
49     $bt=unpack("L",$buf);
50     $bt==$boundary or die "Boundary mismatch (read $bt)\n";
51     undef $buf;
52     sysread(FILE,$buf,8)==8 or die "Can't read taglen\n";
53     $taglen=unpack("Q",$buf);
54     undef $curtag;
55     sysread(FILE,$curtag,$taglen)==$taglen or die "Can't read tag\n";
56     undef $buf;
57     sysread(FILE,$buf,8)==8 or die "Can't read datalen\n";
58     $datalen=unpack("Q",$buf);
59     undef $buf;
60     sysread(FILE,$buf,$datalen)==$datalen or die "Can't read data\n";
61     push @{$h{tags}},$curtag;
62     $h{$curtag}=$buf;
63   }
64 }
65
66 sub inlist {
67   my ($key,@list)=@_;
68   my $k;
69   foreach $k (@list) { 
70     return 1 if ($k eq $key);
71   }
72   return 0;
73 }
74
75 # Takes: dirname [optional list of keys/files to skip]
76 # Returns:
77 # hashref
78 #   keys=>[list of keys in dir, in order]
79 #   keyname => [ tags=>[list of tags in file, in order], 
80 #               tagname=>blob
81 #               ..
82 #
83 # dies if it doesn't work
84 #
85 sub v3_read_checkpoint_from_binary_dir {
86   my ($dir,@skiplist)=@_;
87   my @files = `ls -1 $dir`; chomp(@files);
88   my $file;
89   my $key;
90   my %h;
91
92   $h{keys}=[];;
93   
94   foreach $file (@files) { 
95     $key=$file;
96     next if inlist($key,@skiplist);
97     $h{$key} = v3_read_checkpoint_from_binary_file("$dir/$file");
98     push @{$h{keys}}, $key;
99   }
100   
101   return \%h;
102 }
103
104 #
105 # Takes: filename
106 #
107 #
108 # Returns:
109 # hashref
110 #   tags=>[list of tags in file, in order]
111 #   tagname=>blob
112 #   ..
113 #
114 # dies if it doesn't work
115 #
116 sub v3_read_checkpoint_from_ini_file {
117   $file = shift;
118   my %h;
119   my $line;
120   my $key=undef;
121
122   $h{tags}=[];
123
124   open(FILE,$file) or die "cannot open $file\n";
125
126   # find key
127   while ($line=<FILE>) { 
128     chomp($line);
129     if ($line=~/\[(.*)\]$/) {
130       $key=$1;
131       last;
132     } 
133   }
134   defined($key) or die "cannot find key\n";
135   
136   while ($line=<FILE>) { 
137     chomp($line);
138     if ($line=~/^(.*)\=(.*)$/) { 
139       $h{$1}=pack("H*",$2);
140       push @{$h{tags}}, $1;
141     } else {
142       # skip
143     }
144   }
145   close(FILE);
146
147   return \%h;
148 }
149
150
151 # Takes: dirname [optional list of keys/files to skip]
152 # Returns:
153 # hashref
154 #   keys=>[list of keys in dir, in order]
155 #   keyname => [ tags=>[list of tags in file, in order], 
156 #               tagname=>blob
157 #               ..
158 #
159 # dies if it doesn't work
160 #
161 sub v3_read_checkpoint_from_ini_dir {
162   my ($dir,@skiplist)=@_;
163   my @files = `ls -1 $dir`; chomp(@files);
164   my $file;
165   my $key;
166   my %h;
167
168   $h{keys}=[];;
169   
170   foreach $file (@files) { 
171     $key=$file;
172     next if inlist($key,@skiplist);
173     $h{$key} = v3_read_checkpoint_from_ini_file("$dir/$file");
174     push @{$h{keys}}, $key;
175   }
176
177   return \%h;
178 }
179
180
181 sub v3_write_checkpoint_as_ini_stream {
182   my ($key,$hr,$stream)=@_;
183   my $tag;
184
185   print $stream "[$key]\n" if defined($key);
186
187   foreach $tag (@{$hr->{tags}}) { 
188     print $stream "$tag=".unpack("H*",$hr->{$tag})."\n";
189   }
190 }
191
192 #
193 # input: href, either for individual key, or group of keys
194 #        file to write
195 #        [key optional for single key]
196 #
197 sub v3_write_checkpoint_as_ini_file {
198   my ($hr,$file,$key)=@_;
199
200   open(FILE, ">$file") or die "cannot open $file\n";
201
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);
206     }
207   } else {
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);
211   }
212   close(FILE);
213 }
214
215 #
216 # input: href, either for individual key, or group of keys
217 #        stream to write
218 #
219 #
220 sub v3_write_checkpoint_as_ini_dir {
221   my ($hr,$dir)=@_;
222   my $file;
223   my $key;
224
225   defined($hr->{keys}) or die "cannot write dir without key list\n";
226   
227   mkdir($dir); # OK if it already exists
228
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);
232     close(FILE);
233   }
234 }
235
236 #
237 # input: href, either for individual key, or group of keys
238 #        file to write
239 #
240 sub v3_write_checkpoint_as_binary_file {
241   my ($hr,$file)=@_;
242   my $tag;
243   my $taglen;
244   my $datalen;
245   my $buf;
246
247   open(FILE, ">$file") or die "cannot open $file\n";
248   binmode(FILE);
249
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";
261   }
262   close(FILE);
263 }
264
265 #
266 # input: href, either for individual key, or group of keys
267 #        stream to write
268 #
269 #
270 sub v3_write_checkpoint_as_binary_dir {
271   my ($hr,$dir)=@_;
272   my $file;
273   my $key;
274
275   defined($hr->{keys}) or die "cannot write dir without key list\n";
276   
277   mkdir($dir); # OK if it already exists
278
279   foreach my $key (@{$hr->{keys}}) { 
280     v3_write_checkpoint_as_binary_file($hr->{$key},"$dir/$key");
281   }
282 }
283