8 my $toplevel = $ARGV[0];
10 # Assume our current directory is .AppleDB in the share directory as the default.
12 $toplevel = ".." unless $toplevel;
14 # Main file information data structure. Each entry in did_table points
15 # to a list of hashes. Key is a DID and each list member a hash
16 # describing a file or directory in the directory corresponding to the
17 # DID. n_entries is the number of items found, output_list contains
18 # all entries that are eventually written to the output file.
24 # RootInfo values in the new format
26 my $ri_cnid = "00000000";
27 my $ri_dev = "1122334400000000";
28 my $ri_ino = "0000000000000000";
29 my $ri_did = "00000000";
30 my $ri_hexname = "526f6f74496e666f00"; # RootInfo\0
33 # Current CNID found in didname.db
36 # Largest CNID from cnid.db, used if we cannot find the current CNID in didname.db
39 # Number of bogus/invalid entries found in the DB
44 my $didname_dump = "didname.dump";
45 my $cnid_dump = "cnid.dump";
46 my $cnid2_dump = "cnid2.dump";
57 if ($_ eq "HEADER=END") {
62 die "No valid header found, invalid input file?" unless $header_ok;
70 printf "??:%-6s%-9s%-9s%-9s%-9s%s\n",
81 sub find_current_cnid()
86 my $compare = " " . $ri_did . $ri_hexname;
88 # get rid of "00" at the end, RootInfo in the old format does not have a trailing \0
92 open DIDNAME, "< $didname_dump" or die "Unable to open $didname_dump: $!";
93 skip_header(*DIDNAME);
98 last if $key eq "DATA=END";
101 last unless defined($key) and defined($val);
102 if ($key eq $compare) {
103 # \00\00\00\00RootInfo
117 if (length($entry->{cnid}) != 8 or length($entry->{name}) == 0) {
118 print_eentry("fmt", $entry);
125 sub create_did_table()
135 open CNID, "< $cnid_dump" or die "Unable to open $cnid_dump: $!";
142 if ($key eq "DATA=END") {
150 last unless defined($key) and defined($val);
152 # We do not worry about converting any of the
153 # integer values into binary form. They are in network byte order,
154 # so we know how to extend them. We just treat them as hexadecimal ASCII strings.
155 # The file name is also stored as a proper string, since we need to
156 # look for it in the file system.
160 $entry->{cnid} = $key;
161 $entry->{dev} = substr($val, 0, 8);
162 $entry->{ino} = substr($val, 8, 8);
163 $entry->{did} = substr($val, 16, 8);
164 $entry->{hexname} = substr($val, 24);
166 $len = length($entry->{hexname}) - 2;
170 $name .= chr(hex(substr($entry->{hexname}, $i, 2)));
173 $entry->{name} = $name;
175 if (verify_entry($entry)) {
176 push @{$did_table{$entry->{did}}}, $entry;
178 $cmax = $entry->{cnid} if $entry->{cnid} gt $cmax;
182 die "No valid end of data found, invalid input file?" unless $data_ok;
186 sub output_header($$$)
192 printf $handle "VERSION=3\nformat=bytevalue\ndatabase=%s\n", $dbname;
193 printf $handle "type=hash\nh_nelem=%i\nHEADER=END\n", $n;
196 sub expand_did_table($$)
199 my $basename = shift;
203 foreach $entry (@{$did_table{$did}}) {
204 $name = $basename . "/" . $entry->{name};
208 $entry->{type} = "00000001";
210 $entry->{type} = "00000000";
214 # The file/dir does not exist in the file system. This could result
215 # from a non-afpd rename that has not yet been picked up by afpd and is
216 # fixable. We need a guess at the type, though.
218 if (scalar(@{$did_table{$entry->{cnid}}}) > 0) {
220 # We have entries hanging off this entry in our table,
221 # so this must be a directory
222 $entry->{type} = "00000001";
224 # If this is actually an empty directory that was renamed,
225 # the entry will be deleted by afpd on the next access,
226 # so this should be safe
227 $entry->{type} = "00000000";
231 $entry->{reached} = 1;
232 push @output_list, $entry;
233 expand_did_table($entry->{cnid}, $name);
237 sub find_unreachable()
243 while (($did, $list) = each %did_table) {
244 foreach $entry (@{$list}) {
245 print_eentry("reach", $entry) unless $entry->{reached};
250 print "Finding the current CNID from $didname_dump\n";
251 $current_cnid = find_current_cnid();
252 print "Reading in entries from $cnid_dump\n";
253 $max_cnid = create_did_table();
254 print "Found $n_entries entries\n";
256 if (not $current_cnid) {
257 print "Warning: could not find a valid entry for the current CNID in $didname_dump.\n";
258 print "Using maximum CNID value $max_cnid from $cnid_dump instead\n";
259 $current_cnid = $max_cnid;
261 print "Current CNID is $current_cnid\n";
264 print "Building directory tree according to cnid.db\n";
265 expand_did_table("00000002", $toplevel);
267 if ($n_entries > scalar(@output_list)) {
268 print "Looking for unreachable nodes in the directory tree:\n";
272 print "Creating $cnid2_dump\n";
273 open CNID2, "> $cnid2_dump" or die "Unable to open $cnid2_dump for writing: $!";
274 output_header(*CNID2, "cnid2.db", scalar(@output_list) + 1);
275 foreach my $entry (@output_list) {
276 print CNID2 " ", $entry->{cnid}, "\n";
277 print CNID2 " ", $entry->{cnid},
278 "00000000", $entry->{dev}, "00000000", $entry->{ino},
280 $entry->{did}, $entry->{hexname}, "\n";
282 print CNID2 " ", $ri_cnid, "\n";
283 print CNID2 " ", $ri_cnid, $ri_dev, $ri_ino, $current_cnid, $ri_did, $ri_hexname, "\n";
284 print CNID2 "DATA=END\n";
286 output_header(*CNID2, "devino.db", scalar(@output_list) + 1);
287 foreach my $entry (@output_list) {
288 print CNID2 " ", "00000000", $entry->{dev}, "00000000", $entry->{ino}, "\n";
289 print CNID2 " ", $entry->{cnid}, "\n";
291 print CNID2 " ", $ri_dev, $ri_ino, "\n";
292 print CNID2 " ", $ri_cnid, "\n";
293 print CNID2 "DATA=END\n";
295 output_header(*CNID2, "didname.db", scalar(@output_list) + 1);
296 foreach my $entry (@output_list) {
297 print CNID2 " ", $entry->{did}, $entry->{hexname}, "\n";
298 print CNID2 " ", $entry->{cnid}, "\n";
300 print CNID2 " ", $ri_did, $ri_hexname, "\n";
301 print CNID2 " ", $ri_cnid, "\n";
302 print CNID2 "DATA=END\n";