]> arthur.barton.de Git - netatalk.git/blob - bin/cnid/cnid2_create.in
A FPEnumerate request with a reply size of ~8192 or more can cause
[netatalk.git] / bin / cnid / cnid2_create.in
1 #!@PERL@
2
3 #
4 # Upgrade version 1 CNID databases to version 2
5 #
6 # $Id: cnid2_create.in,v 1.1.2.2 2004-01-02 17:20:57 lenneis Exp $
7 #
8 # Copyright (C) Joerg Lenneis 2003
9 # All Rights Reserved.  See COPYRIGHT.
10 #
11 #
12
13 use strict;
14
15
16 ### Globals
17
18 my $toplevel = $ARGV[0];
19
20 # Assume our current directory is .AppleDB in the share directory as the default. 
21
22 $toplevel = ".." unless $toplevel;
23
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.
29
30 my %did_table;
31 my $n_entries;
32 my @output_list;
33
34 # RootInfo values in the new format
35
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
41
42
43 # Current CNID found in didname.db
44 my $current_cnid;
45
46 # Largest CNID from cnid.db, used if we cannot find the current CNID in didname.db 
47 my $max_cnid;
48
49 # Number of bogus/invalid entries found in the DB 
50 my $errors_found;
51
52 # Filenames
53
54 my $didname_dump = "didname.dump";
55 my $cnid_dump    = "cnid.dump";
56 my $cnid2_dump   = "cnid2.dump";
57
58 ### Subroutines
59
60 sub skip_header($)
61 {
62     my $handle = shift;
63     my $header_ok;
64
65     while (<$handle>) {
66         chomp;
67         if ($_ eq "HEADER=END") {
68             $header_ok = 1;
69             last;
70         }
71     }
72     die "No valid header found, invalid input file?" unless $header_ok;
73 }
74
75 sub print_eentry($$)
76 {
77     my $reason = shift;
78     my $entry  = shift;
79
80     printf "??:%-6s%-9s%-9s%-9s%-9s%s\n",
81     $reason,
82     $entry->{cnid},
83     $entry->{dev},
84     $entry->{ino},
85     $entry->{did},
86     $entry->{name};
87     
88     $errors_found++;
89 }
90
91 sub find_current_cnid()
92 {
93     my $key;
94     my $val;
95     my $cnid;
96     my $compare = " " . $ri_did . $ri_hexname;
97
98     # get rid of "00" at the end, RootInfo in the old format does not have a trailing \0
99
100     $compare =~ s/00$//;
101     
102     open DIDNAME, "< $didname_dump" or die "Unable to open $didname_dump: $!";
103     skip_header(*DIDNAME);
104
105     while (1) {
106         $key = <DIDNAME>;
107         chomp $key;
108         last if $key eq "DATA=END";
109         $val = <DIDNAME>;
110         chomp $val;
111         last unless defined($key) and defined($val);    
112         if ($key eq $compare) {
113             # \00\00\00\00RootInfo
114             $val =~ s/^ //;
115             $cnid = $val;
116             last;
117         }       
118     }
119     close DIDNAME;
120     return $cnid;
121 }
122
123 sub verify_entry($)
124 {
125     my $entry = shift;
126
127     if (length($entry->{cnid}) != 8 or length($entry->{name}) == 0) {
128         print_eentry("fmt", $entry);
129         return 0;
130     } else {
131         return 1;
132     }
133 }
134
135 sub create_did_table()
136 {    
137     my $data_ok;
138     my $key;
139     my $val;
140     my $len;    
141     my $i;
142     my $name;
143     my $cmax;
144
145     open CNID, "< $cnid_dump" or die "Unable to open $cnid_dump: $!";
146     skip_header(*CNID);
147
148     while (1) {
149         $key = <CNID>;
150         chomp $key;
151         $key =~ s/^ //;
152         if ($key eq "DATA=END") {
153             $data_ok = 1;
154             last;
155         }
156         my $val = <CNID>;
157         chomp $val;
158         $val =~ s/^ //;
159
160         last unless defined($key) and defined($val);
161     
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.
167
168         my $entry;
169     
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);
175     
176         $len = length($entry->{hexname}) - 2;
177         $i = 0;
178         $name = '';
179         while ($i < $len) {
180             $name .= chr(hex(substr($entry->{hexname}, $i, 2)));
181             $i += 2;
182         }
183         $entry->{name} = $name;
184
185         if (verify_entry($entry)) {
186             push @{$did_table{$entry->{did}}}, $entry;
187             $n_entries++;
188             $cmax = $entry->{cnid} if $entry->{cnid} gt $cmax;
189         }
190     }
191     close CNID;
192     die "No valid end of data found, invalid input file?" unless $data_ok;
193     return $cmax;
194 }
195
196 sub output_header($$$)
197 {
198     my $handle = shift;
199     my $dbname = shift;
200     my $n      = shift;
201     
202     printf $handle "VERSION=3\nformat=bytevalue\ndatabase=%s\n", $dbname;
203     printf $handle "type=hash\nh_nelem=%i\nHEADER=END\n", $n;
204 }
205
206 sub expand_did_table($$)
207 {
208     my $did = shift;
209     my $basename = shift;
210     my $entry;
211     my $name;
212
213     foreach $entry (@{$did_table{$did}}) {
214         $name = $basename . "/" . $entry->{name};
215
216         if ( -e $name ) { 
217             if ( -d $name ) { 
218                 $entry->{type} = "00000001"; 
219             } else { 
220                 $entry->{type} = "00000000"; 
221             } 
222         } else { 
223             
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.
227             
228             if (scalar(@{$did_table{$entry->{cnid}}}) > 0) {
229
230                 # We have entries hanging off this entry in our table,
231                 # so this must be a directory
232                 $entry->{type} = "00000001";
233             } else {
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";            
238             }
239         }
240
241         $entry->{reached} = 1;
242         push @output_list, $entry;
243         expand_did_table($entry->{cnid}, $name);
244     }    
245 }
246
247 sub find_unreachable()
248 {
249     my $did;
250     my $list;
251     my $entry;
252
253     while (($did, $list) = each %did_table) {
254         foreach $entry (@{$list}) {
255             print_eentry("reach", $entry) unless $entry->{reached};
256         }
257     }
258 }
259
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";
265
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;
270 } else {
271     print "Current CNID is $current_cnid\n";
272 }
273
274 print "Building directory tree according to cnid.db\n";
275 expand_did_table("00000002", $toplevel);
276
277 if ($n_entries > scalar(@output_list)) {
278     print "Looking for unreachable nodes in the directory tree:\n";
279     find_unreachable();
280 }
281
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},
289                      $entry->{type},
290                      $entry->{did}, $entry->{hexname}, "\n";
291 }
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";
295
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";
300 }
301 print CNID2 " ", $ri_dev, $ri_ino, "\n";
302 print CNID2 " ", $ri_cnid, "\n";
303 print CNID2 "DATA=END\n";
304
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";
309 }
310 print CNID2 " ", $ri_did, $ri_hexname, "\n";
311 print CNID2 " ", $ri_cnid, "\n";
312 print CNID2 "DATA=END\n";
313
314 close CNID2;
315
316 exit 0;