4 # Upgrade version 1 CNID databases to version 2
6 # $Id: cnid2_create.in,v 1.1.2.2 2004-01-02 17:20:57 lenneis Exp $
8 # Copyright (C) Joerg Lenneis 2003
9 # All Rights Reserved. See COPYRIGHT.
18 my $toplevel = $ARGV[0];
20 # Assume our current directory is .AppleDB in the share directory as the default.
22 $toplevel = ".." unless $toplevel;
24 # Main file information data structure. Each entry in did_table points
25 # to a list of hashes. Key is a DID and each list member a hash
26 # describing a file or directory in the directory corresponding to the
27 # DID. n_entries is the number of items found, output_list contains
28 # all entries that are eventually written to the output file.
34 # RootInfo values in the new format
36 my $ri_cnid = "00000000";
37 my $ri_dev = "1122334400000000";
38 my $ri_ino = "0000000000000000";
39 my $ri_did = "00000000";
40 my $ri_hexname = "526f6f74496e666f00"; # RootInfo\0
43 # Current CNID found in didname.db
46 # Largest CNID from cnid.db, used if we cannot find the current CNID in didname.db
49 # Number of bogus/invalid entries found in the DB
54 my $didname_dump = "didname.dump";
55 my $cnid_dump = "cnid.dump";
56 my $cnid2_dump = "cnid2.dump";
67 if ($_ eq "HEADER=END") {
72 die "No valid header found, invalid input file?" unless $header_ok;
80 printf "??:%-6s%-9s%-9s%-9s%-9s%s\n",
91 sub find_current_cnid()
96 my $compare = " " . $ri_did . $ri_hexname;
98 # get rid of "00" at the end, RootInfo in the old format does not have a trailing \0
102 open DIDNAME, "< $didname_dump" or die "Unable to open $didname_dump: $!";
103 skip_header(*DIDNAME);
108 last if $key eq "DATA=END";
111 last unless defined($key) and defined($val);
112 if ($key eq $compare) {
113 # \00\00\00\00RootInfo
127 if (length($entry->{cnid}) != 8 or length($entry->{name}) == 0) {
128 print_eentry("fmt", $entry);
135 sub create_did_table()
145 open CNID, "< $cnid_dump" or die "Unable to open $cnid_dump: $!";
152 if ($key eq "DATA=END") {
160 last unless defined($key) and defined($val);
162 # We do not worry about converting any of the
163 # integer values into binary form. They are in network byte order,
164 # so we know how to extend them. We just treat them as hexadecimal ASCII strings.
165 # The file name is also stored as a proper string, since we need to
166 # look for it in the file system.
170 $entry->{cnid} = $key;
171 $entry->{dev} = substr($val, 0, 8);
172 $entry->{ino} = substr($val, 8, 8);
173 $entry->{did} = substr($val, 16, 8);
174 $entry->{hexname} = substr($val, 24);
176 $len = length($entry->{hexname}) - 2;
180 $name .= chr(hex(substr($entry->{hexname}, $i, 2)));
183 $entry->{name} = $name;
185 if (verify_entry($entry)) {
186 push @{$did_table{$entry->{did}}}, $entry;
188 $cmax = $entry->{cnid} if $entry->{cnid} gt $cmax;
192 die "No valid end of data found, invalid input file?" unless $data_ok;
196 sub output_header($$$)
202 printf $handle "VERSION=3\nformat=bytevalue\ndatabase=%s\n", $dbname;
203 printf $handle "type=hash\nh_nelem=%i\nHEADER=END\n", $n;
206 sub expand_did_table($$)
209 my $basename = shift;
213 foreach $entry (@{$did_table{$did}}) {
214 $name = $basename . "/" . $entry->{name};
218 $entry->{type} = "00000001";
220 $entry->{type} = "00000000";
224 # The file/dir does not exist in the file system. This could result
225 # from a non-afpd rename that has not yet been picked up by afpd and is
226 # fixable. We need a guess at the type, though.
228 if (scalar(@{$did_table{$entry->{cnid}}}) > 0) {
230 # We have entries hanging off this entry in our table,
231 # so this must be a directory
232 $entry->{type} = "00000001";
234 # If this is actually an empty directory that was renamed,
235 # the entry will be deleted by afpd on the next access,
236 # so this should be safe
237 $entry->{type} = "00000000";
241 $entry->{reached} = 1;
242 push @output_list, $entry;
243 expand_did_table($entry->{cnid}, $name);
247 sub find_unreachable()
253 while (($did, $list) = each %did_table) {
254 foreach $entry (@{$list}) {
255 print_eentry("reach", $entry) unless $entry->{reached};
260 print "Finding the current CNID from $didname_dump\n";
261 $current_cnid = find_current_cnid();
262 print "Reading in entries from $cnid_dump\n";
263 $max_cnid = create_did_table();
264 print "Found $n_entries entries\n";
266 if (not $current_cnid) {
267 print "Warning: could not find a valid entry for the current CNID in $didname_dump.\n";
268 print "Using maximum CNID value $max_cnid from $cnid_dump instead\n";
269 $current_cnid = $max_cnid;
271 print "Current CNID is $current_cnid\n";
274 print "Building directory tree according to cnid.db\n";
275 expand_did_table("00000002", $toplevel);
277 if ($n_entries > scalar(@output_list)) {
278 print "Looking for unreachable nodes in the directory tree:\n";
282 print "Creating $cnid2_dump\n";
283 open CNID2, "> $cnid2_dump" or die "Unable to open $cnid2_dump for writing: $!";
284 output_header(*CNID2, "cnid2.db", scalar(@output_list) + 1);
285 foreach my $entry (@output_list) {
286 print CNID2 " ", $entry->{cnid}, "\n";
287 print CNID2 " ", $entry->{cnid},
288 "00000000", $entry->{dev}, "00000000", $entry->{ino},
290 $entry->{did}, $entry->{hexname}, "\n";
292 print CNID2 " ", $ri_cnid, "\n";
293 print CNID2 " ", $ri_cnid, $ri_dev, $ri_ino, $current_cnid, $ri_did, $ri_hexname, "\n";
294 print CNID2 "DATA=END\n";
296 output_header(*CNID2, "devino.db", scalar(@output_list) + 1);
297 foreach my $entry (@output_list) {
298 print CNID2 " ", "00000000", $entry->{dev}, "00000000", $entry->{ino}, "\n";
299 print CNID2 " ", $entry->{cnid}, "\n";
301 print CNID2 " ", $ri_dev, $ri_ino, "\n";
302 print CNID2 " ", $ri_cnid, "\n";
303 print CNID2 "DATA=END\n";
305 output_header(*CNID2, "didname.db", scalar(@output_list) + 1);
306 foreach my $entry (@output_list) {
307 print CNID2 " ", $entry->{did}, $entry->{hexname}, "\n";
308 print CNID2 " ", $entry->{cnid}, "\n";
310 print CNID2 " ", $ri_did, $ri_hexname, "\n";
311 print CNID2 " ", $ri_cnid, "\n";
312 print CNID2 "DATA=END\n";