4 # Upgrade version 1 CNID databases to version 2
7 # Copyright (C) Joerg Lenneis 2003
8 # All Rights Reserved. See COPYING.
17 my $toplevel = $ARGV[0];
19 # Assume our current directory is .AppleDB in the share directory as the default.
21 $toplevel = ".." unless $toplevel;
23 # Main file information data structure. Each entry in did_table points
24 # to a list of hashes. Key is a DID and each list member is a hash
25 # describing a file or directory in the directory corresponding to the
26 # DID. n_entries is the number of items found, output_list contains
27 # all entries that are eventually written to the output file.
33 # RootInfo values in the new format
35 my $ri_cnid = "00000000";
36 my $ri_dev = "1122334400000000";
37 my $ri_ino = "0000000000000000";
38 my $ri_did = "00000000";
39 my $ri_hexname = "526f6f74496e666f00"; # RootInfo\0
42 # Current CNID found in didname.db
45 # Largest CNID from cnid.db, used if we cannot find the current CNID in didname.db
48 # Number of bogus/invalid entries found in the DB
53 my $didname_dump = "didname.dump";
54 my $cnid_dump = "cnid.dump";
55 my $cnid2_dump = "cnid2.dump";
66 if ($_ eq "HEADER=END") {
71 die "No valid header found, invalid input file?" unless $header_ok;
79 printf "??:%s %-9s%-9s%-9s%-9s%s\n",
90 sub find_current_cnid()
95 my $compare = " " . $ri_did . $ri_hexname;
97 # get rid of "00" at the end, RootInfo in the old format does not have a trailing \0
101 open DIDNAME, "< $didname_dump" or die "Unable to open $didname_dump: $!";
102 skip_header(*DIDNAME);
107 last if $key eq "DATA=END";
110 last unless defined($key) and defined($val);
111 if ($key eq $compare) {
112 # \00\00\00\00RootInfo
126 if (length($entry->{cnid}) != 8 or length($entry->{name}) == 0) {
127 print_eentry("fmt", $entry);
134 sub create_did_table()
144 open CNID, "< $cnid_dump" or die "Unable to open $cnid_dump: $!";
151 if ($key eq "DATA=END") {
159 last unless defined($key) and defined($val);
161 # We do not worry about converting any of the
162 # integer values into binary form. They are in network byte order,
163 # so we know how to extend them. We just treat them as hexadecimal ASCII strings.
164 # The file name is also stored as a proper string, since we need to
165 # look for it in the file system.
169 $entry->{cnid} = $key;
170 $entry->{dev} = substr($val, 0, 8);
171 $entry->{ino} = substr($val, 8, 8);
172 $entry->{did} = substr($val, 16, 8);
173 $entry->{hexname} = substr($val, 24);
175 $len = length($entry->{hexname}) - 2;
179 $name .= chr(hex(substr($entry->{hexname}, $i, 2)));
182 $entry->{name} = $name;
184 if (verify_entry($entry)) {
185 push @{$did_table{$entry->{did}}}, $entry;
187 $cmax = $entry->{cnid} if $entry->{cnid} gt $cmax;
191 die "No valid end of data found, invalid input file?" unless $data_ok;
195 sub output_header($$$)
201 printf $handle "VERSION=3\nformat=bytevalue\ndatabase=%s\ntype=btree\nHEADER=END\n", $dbname;
204 sub expand_did_table($$)
207 my $basename = shift;
211 foreach $entry (@{$did_table{$did}}) {
212 $name = $basename . "/" . $entry->{name};
216 $entry->{type} = "00000001";
218 $entry->{type} = "00000000";
222 # The file/dir does not exist in the file system. This could result
223 # from a non-afpd rename that has not yet been picked up by afpd and is
224 # fixable. We need a guess at the type, though.
226 if ($did_table{$entry->{cnid}} and scalar(@{$did_table{$entry->{cnid}}}) > 0) {
228 # We have entries hanging off this entry in our table,
229 # so this must be a directory
230 $entry->{type} = "00000001";
232 # If this is actually an empty directory that was renamed,
233 # the entry will be deleted by afpd on the next access,
234 # so this should be safe
235 $entry->{type} = "00000000";
239 $entry->{reached} = 1;
240 push @output_list, $entry;
241 expand_did_table($entry->{cnid}, $name);
245 sub find_unreachable()
251 while (($did, $list) = each %did_table) {
252 foreach $entry (@{$list}) {
253 print_eentry("reach", $entry) unless $entry->{reached};
258 sub find_duplicates()
269 for (my $i = 0; $i < scalar(@output_list); $i++) {
271 $entry = $output_list[$i];
272 $cnid = $entry->{cnid};
273 $devino = $entry->{dev} . $entry->{ino};
274 $didname = $entry->{did} . $entry->{name};
275 if ($seen_cnid{$cnid}) {
276 print_eentry("exist_cnid", $entry);
279 if ($seen_cnid{$cnid}) {
280 print_eentry("dupl_cnid", $entry);
283 if ($seen_devino{$devino}) {
284 print_eentry("dupl_devino", $entry);
287 if ($seen_didname{$didname}) {
288 print_eentry("dupl_didname", $entry);
292 splice(@output_list, $i, 1);
294 $seen_cnid{$cnid} = 1;
295 $seen_devino{$devino} = 1;
296 $seen_didname{$didname} = 1;
302 print "Finding the current CNID from $didname_dump\n";
303 $current_cnid = find_current_cnid();
304 print "Reading in entries from $cnid_dump\n";
305 $max_cnid = create_did_table();
306 print "Found $n_entries entries\n";
308 if (not $current_cnid) {
309 print "Warning: could not find a valid entry for the current CNID in $didname_dump.\n";
310 print "Using maximum CNID value $max_cnid from $cnid_dump instead\n";
311 $current_cnid = $max_cnid;
313 print "Current CNID is $current_cnid\n";
316 print "Building directory tree according to cnid.db\n";
317 expand_did_table("00000002", $toplevel);
319 if ($n_entries > scalar(@output_list)) {
320 print "Looking for unreachable nodes in the directory tree:\n";
324 print "Looking for duplicate entries\n";
327 print "Creating $cnid2_dump\n";
328 open CNID2, "> $cnid2_dump" or die "Unable to open $cnid2_dump for writing: $!";
329 output_header(*CNID2, "cnid2.db", scalar(@output_list) + 1);
330 foreach my $entry (@output_list) {
331 print CNID2 " ", $entry->{cnid}, "\n";
332 print CNID2 " ", $entry->{cnid},
333 "00000000", $entry->{dev}, "00000000", $entry->{ino},
335 $entry->{did}, $entry->{hexname}, "\n";
337 print CNID2 " ", $ri_cnid, "\n";
338 print CNID2 " ", $ri_cnid, $ri_dev, $ri_ino, $current_cnid, $ri_did, $ri_hexname, "\n";
339 print CNID2 "DATA=END\n";
341 output_header(*CNID2, "devino.db", scalar(@output_list) + 1);
342 foreach my $entry (@output_list) {
343 print CNID2 " ", "00000000", $entry->{dev}, "00000000", $entry->{ino}, "\n";
344 print CNID2 " ", $entry->{cnid}, "\n";
346 print CNID2 " ", $ri_dev, $ri_ino, "\n";
347 print CNID2 " ", $ri_cnid, "\n";
348 print CNID2 "DATA=END\n";
350 output_header(*CNID2, "didname.db", scalar(@output_list) + 1);
351 foreach my $entry (@output_list) {
352 print CNID2 " ", $entry->{did}, $entry->{hexname}, "\n";
353 print CNID2 " ", $entry->{cnid}, "\n";
355 print CNID2 " ", $ri_did, $ri_hexname, "\n";
356 print CNID2 " ", $ri_cnid, "\n";
357 print CNID2 "DATA=END\n";