]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/cmd_dbd_scanvol.c
Use dev, ino and syn entries in adouble:ea and remove ADVOL_CACHE stuff
[netatalk.git] / etc / cnid_dbd / cmd_dbd_scanvol.c
1 /*
2   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <dirent.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <setjmp.h>
28
29 #include <atalk/adouble.h>
30 #include <atalk/unicode.h>
31 #include <atalk/volinfo.h>
32 #include <atalk/cnid_dbd_private.h>
33 #include <atalk/volume.h>
34 #include <atalk/ea.h>
35 #include <atalk/util.h>
36 #include <atalk/acl.h>
37 #include <atalk/compat.h>
38
39 #include "cmd_dbd.h"
40 #include "dbif.h"
41 #include "db_param.h"
42 #include "dbd.h"
43
44 /* Some defines to ease code parsing */
45 #define ADDIR_OK (addir_ok == 0)
46 #define ADFILE_OK (adfile_ok == 0)
47
48
49 static struct volinfo *myvolinfo;
50 static char           cwdbuf[MAXPATHLEN+1];
51 static DBD            *dbd;
52 static DBD            *dbd_rebuild;
53 static dbd_flags_t    dbd_flags;
54 static char           stamp[CNID_DEV_LEN];
55 static char           *netatalk_dirs[] = {
56     ".AppleDB",
57     ".AppleDesktop",
58     NULL
59 };
60 static char           *special_dirs[] = {
61     ".zfs",
62     NULL
63 };
64 static struct cnid_dbd_rqst rqst;
65 static struct cnid_dbd_rply rply;
66 static jmp_buf jmp;
67 static struct vol volume; /* fake it for ea_open */
68 static char pname[MAXPATHLEN] = "../";
69
70 /*
71   Taken form afpd/desktop.c
72 */
73 static char *utompath(char *upath)
74 {
75     static char  mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
76     char         *m, *u;
77     uint16_t     flags = CONV_IGNORE | CONV_UNESCAPEHEX;
78     size_t       outlen;
79
80     if (!upath)
81         return NULL;
82
83     m = mpath;
84     u = upath;
85     outlen = strlen(upath);
86
87     if ((myvolinfo->v_casefold & AFPVOL_UTOMUPPER))
88         flags |= CONV_TOUPPER;
89     else if ((myvolinfo->v_casefold & AFPVOL_UTOMLOWER))
90         flags |= CONV_TOLOWER;
91
92     if ((myvolinfo->v_flags & AFPVOL_EILSEQ)) {
93         flags |= CONV__EILSEQ;
94     }
95
96     /* convert charsets */
97     if ((size_t)-1 == ( outlen = convert_charset(myvolinfo->v_volcharset,
98                                                  CH_UTF8_MAC,
99                                                  myvolinfo->v_maccharset,
100                                                  u, outlen, mpath, MAXPATHLEN, &flags)) ) {
101         dbd_log( LOGSTD, "Conversion from %s to %s for %s failed.",
102                  myvolinfo->v_volcodepage, myvolinfo->v_maccodepage, u);
103         return NULL;
104     }
105
106     return(m);
107 }
108
109 /*
110   Taken form afpd/desktop.c
111 */
112 static char *mtoupath(char *mpath)
113 {
114     static char  upath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
115     char    *m, *u;
116     size_t       inplen;
117     size_t       outlen;
118     u_int16_t    flags = 0;
119
120     if (!mpath)
121         return NULL;
122
123     if ( *mpath == '\0' ) {
124         return( "." );
125     }
126
127     /* set conversion flags */
128     if (!(myvolinfo->v_flags & AFPVOL_NOHEX))
129         flags |= CONV_ESCAPEHEX;
130     if (!(myvolinfo->v_flags & AFPVOL_USEDOTS))
131         flags |= CONV_ESCAPEDOTS;
132
133     if ((myvolinfo->v_casefold & AFPVOL_MTOUUPPER))
134         flags |= CONV_TOUPPER;
135     else if ((myvolinfo->v_casefold & AFPVOL_MTOULOWER))
136         flags |= CONV_TOLOWER;
137
138     if ((myvolinfo->v_flags & AFPVOL_EILSEQ)) {
139         flags |= CONV__EILSEQ;
140     }
141
142     m = mpath;
143     u = upath;
144
145     inplen = strlen(m);
146     outlen = MAXPATHLEN;
147
148     if ((size_t)-1 == (outlen = convert_charset(CH_UTF8_MAC,
149                                                 myvolinfo->v_volcharset,
150                                                 myvolinfo->v_maccharset,
151                                                 m, inplen, u, outlen, &flags)) ) {
152         dbd_log( LOGSTD, "conversion from UTF8-MAC to %s for %s failed.",
153                  myvolinfo->v_volcodepage, mpath);
154         return NULL;
155     }
156
157     return( upath );
158 }
159
160 /*
161   Check for wrong encoding e.g. "." at the beginning is not CAP encoded (:2e) although volume is default !AFPVOL_USEDOTS.
162   We do it by roundtripiping from volcharset to UTF8-MAC and back and then compare the result.
163 */
164 static int check_name_encoding(char *uname)
165 {
166     char *roundtripped;
167
168     roundtripped = mtoupath(utompath(uname));
169     if (!roundtripped) {
170         dbd_log( LOGSTD, "Error checking encoding for '%s/%s'", cwdbuf, uname);
171         return -1;
172     }
173
174     if ( STRCMP(uname, !=, roundtripped)) {
175         dbd_log( LOGSTD, "Bad encoding for '%s/%s'", cwdbuf, uname);
176         return -1;
177     }
178
179     return 0;
180 }
181
182 /*
183   Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
184   Returns pointer to name or NULL.
185 */
186 static const char *check_netatalk_dirs(const char *name)
187 {
188     int c;
189
190     for (c=0; netatalk_dirs[c]; c++) {
191         if ((strcmp(name, netatalk_dirs[c])) == 0)
192             return netatalk_dirs[c];
193     }
194     return NULL;
195 }
196
197 /*
198   Check for special names
199   Returns pointer to name or NULL.
200 */
201 static const char *check_special_dirs(const char *name)
202 {
203     int c;
204
205     for (c=0; special_dirs[c]; c++) {
206         if ((strcmp(name, special_dirs[c])) == 0)
207             return special_dirs[c];
208     }
209     return NULL;
210 }
211
212 /*
213   Check for .AppleDouble file, create if missing
214 */
215 static int check_adfile(const char *fname, const struct stat *st)
216 {
217     int ret;
218     int adflags = ADFLAGS_HF;
219     struct adouble ad;
220     const char *adname;
221
222     if (volume.v_adouble == AD_VERSION_EA)
223         return 0;
224
225     if (dbd_flags & DBD_FLAGS_CLEANUP)
226         return 0;
227
228     if (S_ISREG(st->st_mode))
229         adflags |= ADFLAGS_DIR;
230
231     adname = myvolinfo->ad_path(fname, adflags);
232
233     if ((ret = access( adname, F_OK)) != 0) {
234         if (errno != ENOENT) {
235             dbd_log(LOGSTD, "Access error for ad-file '%s/%s': %s",
236                     cwdbuf, adname, strerror(errno));
237             return -1;
238         }
239         /* Missing. Log and create it */
240         dbd_log(LOGSTD, "Missing AppleDouble file '%s/%s'", cwdbuf, adname);
241
242         if (dbd_flags & DBD_FLAGS_SCAN)
243             /* Scan only requested, dont change anything */
244             return -1;
245
246         /* Create ad file */
247         ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
248
249         if ((ret = ad_open(&ad, fname, adflags | ADFLAGS_CREATE | ADFLAGS_RDWR, 0666)) != 0) {
250             dbd_log( LOGSTD, "Error creating AppleDouble file '%s/%s': %s",
251                      cwdbuf, adname, strerror(errno));
252
253             return -1;
254         }
255
256         /* Set name in ad-file */
257         ad_setname(&ad, utompath((char *)fname));
258         ad_flush(&ad);
259         ad_close(&ad, ADFLAGS_HF);
260
261         chown(adname, st->st_uid, st->st_gid);
262         /* FIXME: should we inherit mode too here ? */
263 #if 0
264         chmod(adname, st->st_mode);
265 #endif
266     } else {
267         ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
268         if (ad_open(&ad, fname, adflags | ADFLAGS_RDONLY) != 0) {
269             dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s'", cwdbuf, fname);
270             return -1;
271         }
272         ad_close(&ad, ADFLAGS_HF);
273     }
274     return 0;
275 }
276
277 /* 
278    Remove all files with file::EA* from adouble dir
279 */
280 static void remove_eafiles(const char *name, struct ea *ea)
281 {
282     DIR *dp = NULL;
283     struct dirent *ep;
284     char eaname[MAXPATHLEN];
285
286     strlcpy(eaname, name, sizeof(eaname));
287     if (strlcat(eaname, "::EA", sizeof(eaname)) >= sizeof(eaname)) {
288         dbd_log(LOGSTD, "name too long: '%s/%s/%s'", cwdbuf, ADv2_DIRNAME, name);
289         return;
290     }
291
292     if ((chdir(ADv2_DIRNAME)) != 0) {
293         dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
294                 cwdbuf, ADv2_DIRNAME, strerror(errno));
295         return;
296     }
297
298     if ((dp = opendir(".")) == NULL) {
299         dbd_log(LOGSTD, "Couldn't open the directory '%s/%s': %s",
300                 cwdbuf, ADv2_DIRNAME, strerror(errno));
301         goto exit;
302     }
303
304     while ((ep = readdir(dp))) {
305         if (strstr(ep->d_name, eaname) != NULL) {
306             dbd_log(LOGSTD, "Removing EA file: '%s/%s/%s'",
307                     cwdbuf, ADv2_DIRNAME, ep->d_name);
308             if ((unlink(ep->d_name)) != 0) {
309                 dbd_log(LOGSTD, "Error unlinking EA file '%s/%s/%s': %s",
310                         cwdbuf, ADv2_DIRNAME, ep->d_name, strerror(errno));
311             }
312         } /* if */
313     } /* while */
314
315 exit:
316     if (dp)
317         closedir(dp);
318     if ((chdir("..")) != 0) {
319         dbd_log(LOGSTD, "Couldn't chdir to '%s': %s", cwdbuf, strerror(errno));
320         /* we can't proceed */
321         longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
322     }    
323 }
324
325 /*
326   Check Extended Attributes files
327 */
328 static int check_eafiles(const char *fname)
329 {
330     unsigned int  count = 0;
331     int ret = 0, remove;
332     struct ea ea;
333     struct stat st;
334     char *eaname;
335
336     if ((ret = ea_open(&volume, fname, EA_RDWR, &ea)) != 0) {
337         if (errno == ENOENT)
338             return 0;
339         dbd_log(LOGSTD, "Error calling ea_open for file: %s/%s, removing EA files", cwdbuf, fname);
340         if ( ! (dbd_flags & DBD_FLAGS_SCAN))
341             remove_eafiles(fname, &ea);
342         return -1;
343     }
344
345     /* Check all EAs */
346     while (count < ea.ea_count) {        
347         dbd_log(LOGDEBUG, "EA: %s", (*ea.ea_entries)[count].ea_name);
348         remove = 0;
349
350         eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name, 0);
351
352         if (lstat(eaname, &st) != 0) {
353             if (errno == ENOENT)
354                 dbd_log(LOGSTD, "Missing EA: %s/%s", cwdbuf, eaname);
355             else
356                 dbd_log(LOGSTD, "Bogus EA: %s/%s", cwdbuf, eaname);
357             remove = 1;
358         } else if (st.st_size != (*ea.ea_entries)[count].ea_size) {
359             dbd_log(LOGSTD, "Bogus EA: %s/%s, removing it...", cwdbuf, eaname);
360             remove = 1;
361             if ((unlink(eaname)) != 0)
362                 dbd_log(LOGSTD, "Error removing EA file '%s/%s': %s",
363                         cwdbuf, eaname, strerror(errno));
364         }
365
366         if (remove) {
367             /* Be CAREFUL here! This should do what ea_delentry does. ea_close relies on it !*/
368             free((*ea.ea_entries)[count].ea_name);
369             (*ea.ea_entries)[count].ea_name = NULL;
370         }
371
372         count++;
373     } /* while */
374
375     ea_close(&ea);
376     return ret;
377 }
378
379 /*
380   Check for .AppleDouble folder and .Parent, create if missing
381 */
382 static int check_addir(int volroot)
383 {
384     int addir_ok, adpar_ok;
385     struct stat st;
386     struct adouble ad;
387     char *mname = NULL;
388
389     if (dbd_flags & DBD_FLAGS_CLEANUP)
390         return 0;
391
392     if (volume.v_adouble == AD_VERSION_EA)
393         return 0;
394
395     /* Check for ad-dir */
396     if ( (addir_ok = access(ADv2_DIRNAME, F_OK)) != 0) {
397         if (errno != ENOENT) {
398             dbd_log(LOGSTD, "Access error in directory %s: %s", cwdbuf, strerror(errno));
399             return -1;
400         }
401         dbd_log(LOGSTD, "Missing %s for '%s'", ADv2_DIRNAME, cwdbuf);
402     }
403
404     if (volume.v_adouble == AD_VERSION_EA)
405         return 0;
406
407     /* Check for ".Parent" */
408     if ( (adpar_ok = access(myvolinfo->ad_path(".", ADFLAGS_DIR), F_OK)) != 0) {
409         if (errno != ENOENT) {
410             dbd_log(LOGSTD, "Access error on '%s/%s': %s",
411                     cwdbuf, myvolinfo->ad_path(".", ADFLAGS_DIR), strerror(errno));
412             return -1;
413         }
414         dbd_log(LOGSTD, "Missing .AppleDouble/.Parent for '%s'", cwdbuf);
415     }
416
417     /* Is one missing ? */
418     if ((addir_ok != 0) || (adpar_ok != 0)) {
419         /* Yes, but are we only scanning ? */
420         if (dbd_flags & DBD_FLAGS_SCAN) {
421             /* Yes:  missing .Parent is not a problem, but missing ad-dir
422                causes later checking of ad-files to fail. So we have to return appropiately */
423             if (addir_ok != 0)
424                 return -1;
425             else  /* (adpar_ok != 0) */
426                 return 0;
427         }
428
429         /* Create ad dir and set name */
430         ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
431
432         if (ad_open(&ad, ".", ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_CREATE | ADFLAGS_RDWR, 0777) != 0) {
433             dbd_log( LOGSTD, "Error creating AppleDouble dir in %s: %s", cwdbuf, strerror(errno));
434             return -1;
435         }
436
437         /* Get basename of cwd from cwdbuf */
438         mname = utompath(strrchr(cwdbuf, '/') + 1);
439
440         /* Update name in ad file */
441         ad_setname(&ad, mname);
442         ad_flush(&ad);
443         ad_close(&ad, ADFLAGS_HF);
444
445         /* Inherit owner/group from "." to ".AppleDouble" and ".Parent" */
446         if ((lstat(".", &st)) != 0) {
447             dbd_log( LOGSTD, "Couldnt stat %s: %s", cwdbuf, strerror(errno));
448             return -1;
449         }
450         chown(ADv2_DIRNAME, st.st_uid, st.st_gid);
451         chown(myvolinfo->ad_path(".", ADFLAGS_DIR), st.st_uid, st.st_gid);
452     }
453
454     return 0;
455 }
456
457 /*
458   Check if file cotains "::EA" and if it does check if its correspondig data fork exists.
459   Returns:
460   0 = name is not an EA file
461   1 = name is an EA file and no problem was found
462   -1 = name is an EA file and data fork is gone
463  */
464 static int check_eafile_in_adouble(const char *name)
465 {
466     int ret = 0;
467     char *namep, *namedup = NULL;
468
469     /* Check if this is an AFPVOL_EA_AD vol */
470     if (myvolinfo->v_vfs_ea == AFPVOL_EA_AD) {
471         /* Does the filename contain "::EA" ? */
472         namedup = strdup(name);
473         if ((namep = strstr(namedup, "::EA")) == NULL) {
474             ret = 0;
475             goto ea_check_done;
476         } else {
477             /* File contains "::EA" so it's an EA file. Check for data file  */
478
479             /* Get string before "::EA" from EA filename */
480             namep[0] = 0;
481             strlcpy(pname + 3, namedup, sizeof(pname)); /* Prepends "../" */
482
483             if ((access( pname, F_OK)) == 0) {
484                 ret = 1;
485                 goto ea_check_done;
486             } else {
487                 ret = -1;
488                 if (errno != ENOENT) {
489                     dbd_log(LOGSTD, "Access error for file '%s/%s': %s",
490                             cwdbuf, name, strerror(errno));
491                     goto ea_check_done;
492                 }
493
494                 /* Orphaned EA file*/
495                 dbd_log(LOGSTD, "Orphaned Extended Attribute file '%s/%s/%s'",
496                         cwdbuf, ADv2_DIRNAME, name);
497
498                 if (dbd_flags & DBD_FLAGS_SCAN)
499                     /* Scan only requested, dont change anything */
500                     goto ea_check_done;
501
502                 if ((unlink(name)) != 0) {
503                     dbd_log(LOGSTD, "Error unlinking orphaned Extended Attribute file '%s/%s/%s'",
504                             cwdbuf, ADv2_DIRNAME, name);
505                 }
506             } /* if (access) */
507         } /* if strstr */
508     } /* if AFPVOL_EA_AD */
509
510 ea_check_done:
511     if (namedup)
512         free(namedup);
513
514     return ret;
515 }
516
517 /*
518   Check files and dirs inside .AppleDouble folder:
519   - remove orphaned files
520   - bail on dirs
521 */
522 static int read_addir(void)
523 {
524     DIR *dp;
525     struct dirent *ep;
526     struct stat st;
527
528     if (volume.v_adouble == AD_VERSION_EA)
529         return 0;
530
531     if ((chdir(ADv2_DIRNAME)) != 0) {
532         dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
533                 cwdbuf, ADv2_DIRNAME, strerror(errno));
534         return -1;
535     }
536
537     if ((dp = opendir(".")) == NULL) {
538         dbd_log(LOGSTD, "Couldn't open the directory '%s/%s': %s",
539                 cwdbuf, ADv2_DIRNAME, strerror(errno));
540         return -1;
541     }
542
543     while ((ep = readdir(dp))) {
544         /* Check if its "." or ".." */
545         if (DIR_DOT_OR_DOTDOT(ep->d_name))
546             continue;
547
548         /* Skip ".Parent" */
549         if (STRCMP(ep->d_name, ==, ".Parent"))
550             continue;
551
552         if ((lstat(ep->d_name, &st)) < 0) {
553             dbd_log( LOGSTD, "Lost file or dir while enumeratin dir '%s/%s/%s', probably removed: %s",
554                      cwdbuf, ADv2_DIRNAME, ep->d_name, strerror(errno));
555             continue;
556         }
557
558         /* Check for dirs */
559         if (S_ISDIR(st.st_mode)) {
560             dbd_log( LOGSTD, "Unexpected directory '%s' in AppleDouble dir '%s/%s'",
561                      ep->d_name, cwdbuf, ADv2_DIRNAME);
562             continue;
563         }
564
565         /* Check if for orphaned and corrupt Extended Attributes file */
566         if (check_eafile_in_adouble(ep->d_name) != 0)
567             continue;
568
569         /* Check for data file */
570         strcpy(pname + 3, ep->d_name);
571         if ((access( pname, F_OK)) != 0) {
572             if (errno != ENOENT) {
573                 dbd_log(LOGSTD, "Access error for file '%s/%s': %s",
574                         cwdbuf, pname, strerror(errno));
575                 continue;
576             }
577             /* Orphaned ad-file*/
578             dbd_log(LOGSTD, "Orphaned AppleDoube file '%s/%s/%s'",
579                     cwdbuf, ADv2_DIRNAME, ep->d_name);
580
581             if (dbd_flags & DBD_FLAGS_SCAN)
582                 /* Scan only requested, dont change anything */
583                 continue;;
584
585             if ((unlink(ep->d_name)) != 0) {
586                 dbd_log(LOGSTD, "Error unlinking orphaned AppleDoube file '%s/%s/%s'",
587                         cwdbuf, ADv2_DIRNAME, ep->d_name);
588
589             }
590         }
591     }
592
593     if ((chdir("..")) != 0) {
594         dbd_log(LOGSTD, "Couldn't chdir back to '%s' from AppleDouble dir: %s",
595                 cwdbuf, strerror(errno));
596         /* This really is EOT! */
597         longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
598     }
599
600     closedir(dp);
601
602     return 0;
603 }
604
605 /*
606   Check CNID for a file/dir, both from db and from ad-file.
607   For detailed specs see intro.
608
609   @return Correct CNID of object or CNID_INVALID (ie 0) on error
610 */
611 static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfile_ok, int adflags)
612 {
613     int ret;
614     cnid_t db_cnid, ad_cnid;
615     struct adouble ad;
616
617     /* Force checkout every X items */
618     static int cnidcount = 0;
619     cnidcount++;
620     if (cnidcount > 10000) {
621         cnidcount = 0;
622         if (dbif_txn_checkpoint(dbd, 0, 0, 0) < 0) {
623             dbd_log(LOGSTD, "Error checkpointing!");
624             return CNID_INVALID;
625         }
626     }
627
628     /* Get CNID from ad-file */
629     ad_cnid = 0;
630     if (ADFILE_OK) {
631         ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
632         if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
633             
634             if (dbd_flags & DBD_FLAGS_CLEANUP)
635                 return CNID_INVALID;
636
637             dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s': %s", cwdbuf, name, strerror(errno));
638             return CNID_INVALID;
639         }
640
641         if (dbd_flags & DBD_FLAGS_FORCE) {
642             ad_cnid = ad_forcegetid(&ad);
643             /* This ensures the changed stamp is written */
644             ad_setid( &ad, st->st_dev, st->st_ino, ad_cnid, did, stamp);
645             ad_flush(&ad);
646         }
647         else
648             ad_cnid = ad_getid(&ad, st->st_dev, st->st_ino, 0, stamp);
649
650         if (ad_cnid == 0)
651             dbd_log( LOGSTD, "Bad CNID in adouble file of '%s/%s'", cwdbuf, name);
652         else
653             dbd_log( LOGDEBUG, "CNID from .AppleDouble file for '%s/%s': %u", cwdbuf, name, ntohl(ad_cnid));
654
655         ad_close(&ad, ADFLAGS_HF);
656     }
657
658     /* Get CNID from database */
659
660     /* Prepare request data */
661     memset(&rqst, 0, sizeof(struct cnid_dbd_rqst));
662     memset(&rply, 0, sizeof(struct cnid_dbd_rply));
663     rqst.did = did;
664     rqst.cnid = ad_cnid;
665     if ( ! (myvolinfo->v_flags & AFPVOL_NODEV))
666         rqst.dev = st->st_dev;
667     rqst.ino = st->st_ino;
668     rqst.type = S_ISDIR(st->st_mode)?1:0;
669     rqst.name = (char *)name;
670     rqst.namelen = strlen(name);
671
672     /* Query the database */
673     ret = dbd_lookup(dbd, &rqst, &rply, (dbd_flags & DBD_FLAGS_SCAN) ? 1 : 0);
674     if (dbif_txn_close(dbd, ret) != 0)
675         return CNID_INVALID;
676     if (rply.result == CNID_DBD_RES_OK) {
677         db_cnid = rply.cnid;
678     } else if (rply.result == CNID_DBD_RES_NOTFOUND) {
679         if ( ! (dbd_flags & DBD_FLAGS_FORCE))
680             dbd_log( LOGSTD, "No CNID for '%s/%s' in database", cwdbuf, name);
681         db_cnid = 0;
682     } else {
683         dbd_log( LOGSTD, "Fatal error resolving '%s/%s'", cwdbuf, name);
684         db_cnid = 0;
685     }
686
687     /* Compare results from both CNID searches */
688     if (ad_cnid && db_cnid && (ad_cnid == db_cnid)) {
689         /* Everything is fine */
690         return db_cnid;
691     } else if (ad_cnid && db_cnid && (ad_cnid != db_cnid)) {
692         /* Mismatch, overwrite ad file with value from db */
693         dbd_log( LOGSTD, "CNID mismatch for '%s/%s', db: %u, ad-file: %u", cwdbuf, name, ntohl(db_cnid), ntohl(ad_cnid));
694         if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
695             dbd_log(LOGSTD, "Updating AppleDouble file for '%s/%s' with CNID: %u from database",
696                             cwdbuf, name, ntohl(db_cnid));
697             ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
698             if (ad_open(&ad, name, adflags | ADFLAGS_HF | ADFLAGS_RDWR) != 0) {
699                 dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
700                         cwdbuf, name, strerror(errno));
701                 return CNID_INVALID;
702             }
703             ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
704             ad_flush(&ad);
705             ad_close(&ad, ADFLAGS_HF);
706         }
707         return db_cnid;
708     } else if (ad_cnid && (db_cnid == 0)) {
709         /* in ad-file but not in db */
710         if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
711             /* Ensure the cnid from the ad-file is not already occupied by another file */
712             dbd_log(LOGDEBUG, "Checking whether CNID %u from ad-file is occupied",
713                     ntohl(ad_cnid));
714
715             rqst.cnid = ad_cnid;
716             ret = dbd_resolve(dbd, &rqst, &rply);
717             if (rply.result == CNID_DBD_RES_OK) {
718                 /* Occupied! Choose another, update ad-file */
719                 ret = dbd_add(dbd, &rqst, &rply, 1);
720                 if (dbif_txn_close(dbd, ret) != 0)
721                     return CNID_INVALID;
722                 db_cnid = rply.cnid;
723                 dbd_log(LOGSTD, "New CNID for '%s/%s': %u", cwdbuf, name, ntohl(db_cnid));
724
725                 if (ADFILE_OK && ( ! (dbd_flags & DBD_FLAGS_SCAN))) {
726                     dbd_log(LOGSTD, "Writing CNID data for '%s/%s' to AppleDouble file",
727                             cwdbuf, name, ntohl(db_cnid));
728                     ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
729                     if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
730                         dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
731                                 cwdbuf, name, strerror(errno));
732                         return CNID_INVALID;
733                     }
734                     ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
735                     ad_flush(&ad);
736                     ad_close(&ad, ADFLAGS_HF);
737                 }
738                 return db_cnid;
739             }
740
741             dbd_log(LOGDEBUG, "CNID rebuild add '%s/%s' with CNID from ad-file %u",
742                     cwdbuf, name, ntohl(ad_cnid));
743             rqst.cnid = ad_cnid;
744             ret = dbd_rebuild_add(dbd, &rqst, &rply);
745             if (dbif_txn_close(dbd, ret) != 0)
746                 return CNID_INVALID;
747         }
748         return ad_cnid;
749     } else if ((db_cnid == 0) && (ad_cnid == 0)) {
750         /* No CNID at all, we clearly have to allocate a fresh one... */
751         /* Note: the next test will use this new CNID too! */
752         if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
753             /* add to db */
754             ret = dbd_add(dbd, &rqst, &rply, 1);
755             if (dbif_txn_close(dbd, ret) != 0)
756                 return CNID_INVALID;
757             db_cnid = rply.cnid;
758             dbd_log(LOGSTD, "New CNID for '%s/%s': %u", cwdbuf, name, ntohl(db_cnid));
759         }
760     }
761
762     if ((ad_cnid == 0) && db_cnid) {
763         /* in db but zeroID in ad-file, write it to ad-file */
764         if (ADFILE_OK) {
765             if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
766                 dbd_log(LOGSTD, "Writing CNID data for '%s/%s' to AppleDouble file",
767                         cwdbuf, name, ntohl(db_cnid));
768                 ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
769                 if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
770                     dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
771                             cwdbuf, name, strerror(errno));
772                     return CNID_INVALID;
773                 }
774                 ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
775                 ad_flush(&ad);
776                 ad_close(&ad, ADFLAGS_HF);
777             }
778         }
779         return db_cnid;
780     }
781
782     return CNID_INVALID;
783 }
784
785 /*
786   This is called recursively for all dirs.
787   volroot=1 means we're in the volume root dir, 0 means we aren't.
788   We use this when checking for netatalk private folders like .AppleDB.
789   did is our parents CNID.
790 */
791 static int dbd_readdir(int volroot, cnid_t did)
792 {
793     int cwd, ret = 0, adflags, adfile_ok, addir_ok, encoding_ok;
794     cnid_t cnid = 0;
795     const char *name;
796     DIR *dp;
797     struct dirent *ep;
798     static struct stat st;      /* Save some stack space */
799
800     /* Check again for .AppleDouble folder, check_adfile also checks/creates it */
801     if ((addir_ok = check_addir(volroot)) != 0)
802         if ( ! (dbd_flags & DBD_FLAGS_SCAN))
803             /* Fatal on rebuild run, continue if only scanning ! */
804             return -1;
805
806     /* Check AppleDouble files in AppleDouble folder, but only if it exists or could be created */
807     if (volume.v_adouble == AD_VERSION2 && ADDIR_OK)
808         if ((read_addir()) != 0)
809             if ( ! (dbd_flags & DBD_FLAGS_SCAN))
810                 /* Fatal on rebuild run, continue if only scanning ! */
811                 return -1;
812
813     if ((dp = opendir (".")) == NULL) {
814         dbd_log(LOGSTD, "Couldn't open the directory: %s",strerror(errno));
815         return -1;
816     }
817
818     while ((ep = readdir (dp))) {
819         /* Check if we got a termination signal */
820         if (alarmed)
821             longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
822
823         /* Check if its "." or ".." */
824         if (DIR_DOT_OR_DOTDOT(ep->d_name))
825             continue;
826
827         /* Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop" */
828         if ((name = check_netatalk_dirs(ep->d_name)) != NULL) {
829             if (! volroot)
830                 dbd_log(LOGSTD, "Nested %s in %s", name, cwdbuf);
831             continue;
832         }
833
834         /* Check for special folders in volume root e.g. ".zfs" */
835         if (volroot) {
836             if ((name = check_special_dirs(ep->d_name)) != NULL) {
837                 dbd_log(LOGSTD, "Ignoring special dir \"%s\"", name);
838                 continue;
839             }
840         }
841
842         /* Skip .AppleDouble dir in this loop */
843         if (STRCMP(ep->d_name, == , ADv2_DIRNAME))
844             continue;
845
846         if ((ret = lstat(ep->d_name, &st)) < 0) {
847             dbd_log( LOGSTD, "Lost file while reading dir '%s/%s', probably removed: %s",
848                      cwdbuf, ep->d_name, strerror(errno));
849             continue;
850         }
851         
852         switch (st.st_mode & S_IFMT) {
853         case S_IFREG:
854             adflags = 0;
855             break;
856         case S_IFDIR:
857             adflags = ADFLAGS_DIR;
858             break;
859         case S_IFLNK:
860             dbd_log(LOGDEBUG, "Ignoring symlink %s/%s", cwdbuf, ep->d_name);
861             continue;
862         default:
863             dbd_log(LOGSTD, "Bad filetype: %s/%s", cwdbuf, ep->d_name);
864             if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
865                 if ((unlink(ep->d_name)) != 0) {
866                     dbd_log(LOGSTD, "Error removing: %s/%s: %s", cwdbuf, ep->d_name, strerror(errno));
867                 }
868             }
869             continue;
870         }
871
872         /**************************************************************************
873            Statistics
874          **************************************************************************/
875         static unsigned long long statcount = 0;
876         static time_t t = 0;
877
878         if (t == 0)
879             t = time(NULL);
880
881         statcount++;
882         if ((statcount % 10000) == 0) {
883             if (dbd_flags & DBD_FLAGS_STATS)            
884                 dbd_log(LOGSTD, "Scanned: %10llu, time: %10llu s",
885                         statcount, (unsigned long long)(time(NULL) - t));
886         }
887
888         /**************************************************************************
889            Tests
890         **************************************************************************/
891
892         /* Check encoding */
893         if ( -1 == (encoding_ok = check_name_encoding(ep->d_name)) ) {
894             /* If its a file: skipp all other tests now ! */
895             /* For dirs we could try to get a CNID for it and recurse, but currently I prefer not to */
896             continue;
897         }
898
899         /* Check for appledouble file, create if missing, but only if we have addir */
900         adfile_ok = -1;
901         if (ADDIR_OK)
902             adfile_ok = check_adfile(ep->d_name, &st);
903
904         if ( ! nocniddb) {
905             /* Check CNIDs */
906             cnid = check_cnid(ep->d_name, did, &st, adfile_ok, adflags);
907
908             /* Now add this object to our rebuild dbd */
909             if (cnid && dbd_rebuild) {
910                 static uint count = 0;
911                 rqst.cnid = rply.cnid;
912                 ret = dbd_rebuild_add(dbd_rebuild, &rqst, &rply);
913                 if (dbif_txn_close(dbd_rebuild, ret) != 0)
914                     return -1;
915                 if (rply.result != CNID_DBD_RES_OK) {
916                     dbd_log( LOGSTD, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
917                              cnid, cwdbuf, ep->d_name);
918                     return -1;
919                 }
920                 count++;
921                 if (count == 10000) {
922                     if (dbif_txn_checkpoint(dbd_rebuild, 0, 0, 0) < 0) {
923                         dbd_log(LOGSTD, "Error checkpointing!");
924                         return -1;
925                     }
926                     count = 0;
927                 }
928             }
929         }
930
931         /* Check EA files */
932         if (myvolinfo->v_vfs_ea == AFPVOL_EA_AD)
933             check_eafiles(ep->d_name);
934
935         /**************************************************************************
936           Recursion
937         **************************************************************************/
938         if (S_ISDIR(st.st_mode) && (cnid || nocniddb)) { /* If we have no cnid for it we cant recur */
939             strcat(cwdbuf, "/");
940             strcat(cwdbuf, ep->d_name);
941             dbd_log( LOGDEBUG, "Entering directory: %s", cwdbuf);
942             if (-1 == (cwd = open(".", O_RDONLY))) {
943                 dbd_log( LOGSTD, "Cant open directory '%s': %s", cwdbuf, strerror(errno));
944                 continue;
945             }
946             if (0 != chdir(ep->d_name)) {
947                 dbd_log( LOGSTD, "Cant chdir to directory '%s': %s", cwdbuf, strerror(errno));
948                 close(cwd);
949                 continue;
950             }
951
952             ret = dbd_readdir(0, cnid);
953
954             fchdir(cwd);
955             close(cwd);
956             *(strrchr(cwdbuf, '/')) = 0;
957             if (ret < 0)
958                 return -1;
959         }
960     }
961
962     /*
963       Use results of previous checks
964     */
965
966     closedir(dp);
967     return ret;
968 }
969
970 static int scanvol(struct volinfo *vi, dbd_flags_t flags)
971 {
972     /* Make this stuff accessible from all funcs easily */
973     myvolinfo = vi;
974     dbd_flags = flags;
975
976     /* Init a fake struct vol with just enough so we can call ea_open and friends */
977     volume.v_adouble = vi->v_adouble;
978     volume.v_vfs_ea = myvolinfo->v_vfs_ea;
979     initvol_vfs(&volume);
980
981     /* Run with umask 0 */
982     umask(0);
983
984     /* Remove trailing slash from volume, chdir to vol */
985     if (myvolinfo->v_path[strlen(myvolinfo->v_path) - 1] == '/')
986         myvolinfo->v_path[strlen(myvolinfo->v_path) - 1] = 0;
987     strcpy(cwdbuf, myvolinfo->v_path);
988     chdir(myvolinfo->v_path);
989
990     /* Start recursion */
991     if (dbd_readdir(1, htonl(2)) < 0)  /* 2 = volumeroot CNID */
992         return -1;
993
994     return 0;
995 }
996
997 /*
998   Remove all CNIDs from dbd that are not in dbd_rebuild
999 */
1000 static void delete_orphaned_cnids(DBD *dbd, DBD *dbd_rebuild, dbd_flags_t flags)
1001 {
1002     int ret = 0, deleted = 0;
1003     cnid_t dbd_cnid = 0, rebuild_cnid = 0;
1004     struct cnid_dbd_rqst rqst;
1005     struct cnid_dbd_rply rply;
1006
1007     /* jump over rootinfo key */
1008     if ( dbif_idwalk(dbd, &dbd_cnid, 0) != 1)
1009         return;
1010     if ( dbif_idwalk(dbd_rebuild, &rebuild_cnid, 0) != 1)
1011         return;
1012
1013     /* Get first id from dbd_rebuild */
1014     if ((dbif_idwalk(dbd_rebuild, &rebuild_cnid, 0)) == -1)
1015         return;
1016
1017     /* Start main loop through dbd: get CNID from dbd */
1018     while ((dbif_idwalk(dbd, &dbd_cnid, 0)) == 1) {
1019         /* Check if we got a termination signal */
1020         if (alarmed)
1021             longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
1022
1023         if (deleted > 1000) {
1024             deleted = 0;
1025             if (dbif_txn_checkpoint(dbd, 0, 0, 0) < 0) {
1026                 dbd_log(LOGSTD, "Error checkpointing!");
1027                 goto cleanup;
1028             }
1029         }
1030
1031         /* This should be the normal case: CNID is in both dbs */
1032         if (dbd_cnid == rebuild_cnid) {
1033             /* Get next CNID from rebuild db */
1034             if ((ret = dbif_idwalk(dbd_rebuild, &rebuild_cnid, 0)) == -1) {
1035                 /* Some error */
1036                 goto cleanup;
1037             } else if (ret == 0) {
1038                 /* end of rebuild_cnid, delete all remaining CNIDs from dbd */
1039                 while ((dbif_idwalk(dbd, &dbd_cnid, 0)) == 1) {
1040                     dbd_log(LOGSTD, "Orphaned CNID in database: %u", dbd_cnid);
1041                     if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
1042                         rqst.cnid = htonl(dbd_cnid);
1043                         if ((ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID)) == -1) {
1044                             dbd_log(LOGSTD, "Error deleting CNID %u", dbd_cnid);
1045                             (void)dbif_txn_abort(dbd);
1046                             goto cleanup;
1047                         }
1048                         
1049                         if (dbif_txn_close(dbd, ret) != 0)
1050                             return;
1051                         deleted++;
1052                     }
1053                     /* Check if we got a termination signal */
1054                     if (alarmed)
1055                         longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
1056                 }
1057                 return;
1058             } else
1059                 /* Normal case (ret=1): continue while loop */
1060                 continue;
1061         }
1062
1063         if (dbd_cnid < rebuild_cnid) {
1064             /* CNID is orphaned -> delete */
1065             dbd_log(LOGSTD, "One orphaned CNID in database: %u.", dbd_cnid);
1066             if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
1067                 rqst.cnid = htonl(dbd_cnid);
1068                 if ((ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID)) == -1) {
1069                     dbd_log(LOGSTD, "Error deleting CNID %u", dbd_cnid);
1070                     (void)dbif_txn_abort(dbd);
1071                     goto cleanup;
1072                 }
1073                 if (dbif_txn_close(dbd, ret) != 0)
1074                     return;
1075                 deleted++;
1076             }
1077             continue;
1078         }
1079
1080         if (dbd_cnid > rebuild_cnid) {
1081             dbif_idwalk(dbd, NULL, 1); /* Close cursor */
1082             dbif_idwalk(dbd_rebuild, NULL, 1); /* Close cursor */
1083             (void)dbif_txn_close(dbd, 2);
1084             (void)dbif_txn_close(dbd_rebuild, 2);                
1085             dbd_log(LOGSTD, "Ghost CNID: %u. This is fatal! Dumping rebuild db:\n", rebuild_cnid);
1086             dbif_dump(dbd_rebuild, 0);
1087             dbd_log(LOGSTD, "Send this dump and a `dbd -d ...` dump to the Netatalk Dev team!");
1088             goto cleanup;
1089         }
1090     } /* while ((dbif_idwalk(dbd, &dbd_cnid, 0)) == 1) */
1091
1092 cleanup:
1093     dbif_idwalk(dbd, NULL, 1); /* Close cursor */
1094     dbif_idwalk(dbd_rebuild, NULL, 1); /* Close cursor */
1095     return;
1096 }
1097
1098 static const char *get_tmpdb_path(void)
1099 {
1100     pid_t pid = getpid();
1101     static char path[MAXPATHLEN];
1102     snprintf(path, MAXPATHLEN, "/tmp/tmpdb-dbd.%u", pid);
1103     if (mkdir(path, 0755) != 0)
1104         return NULL;
1105     return path;
1106 }
1107
1108 /*
1109   Main func called from cmd_dbd.c
1110 */
1111 int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *vi, dbd_flags_t flags)
1112 {
1113     int ret = 0;
1114     struct db_param db_param = { 0 };
1115     const char *tmpdb_path = NULL;
1116
1117     /* Set cachesize for in-memory rebuild db */
1118     db_param.cachesize = 64 * 1024;         /* 64 MB */
1119     db_param.maxlocks = DEFAULT_MAXLOCKS;
1120     db_param.maxlockobjs = DEFAULT_MAXLOCKOBJS;
1121     db_param.logfile_autoremove = 1;
1122
1123     /* Make it accessible for all funcs */
1124     dbd = dbd_ref;
1125
1126     /* We only support unicode volumes ! */
1127     if ( vi->v_volcharset != CH_UTF8) {
1128         dbd_log( LOGSTD, "Not a Unicode volume: %s, %u != %u", vi->v_volcodepage, vi->v_volcharset, CH_UTF8);
1129         return -1;
1130     }
1131
1132     /* Get volume stamp */
1133     dbd_getstamp(dbd, &rqst, &rply);
1134     if (rply.result != CNID_DBD_RES_OK) {
1135         ret = -1;
1136         goto exit;
1137     }
1138     memcpy(stamp, rply.name, CNID_DEV_LEN);
1139
1140     /* temporary rebuild db, used with -re rebuild to delete unused CNIDs, not used with -f */
1141     if (! nocniddb && (flags & DBD_FLAGS_EXCL) && !(flags & DBD_FLAGS_FORCE)) {
1142         tmpdb_path = get_tmpdb_path();
1143         if (NULL == (dbd_rebuild = dbif_init(tmpdb_path, "cnid2.db"))) {
1144             ret = -1;
1145             goto exit;
1146         }
1147
1148         if (dbif_env_open(dbd_rebuild,
1149                           &db_param,
1150                           DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN) < 0) {
1151             dbd_log(LOGSTD, "error opening tmp database!");
1152             goto exit;
1153         }
1154
1155         if (0 != (dbif_open(dbd_rebuild, NULL, 0))) {
1156             ret = -1;
1157             goto exit;
1158         }
1159
1160         if (0 != (dbif_copy_rootinfokey(dbd, dbd_rebuild))) {
1161             ret = -1;
1162             goto exit;
1163         }
1164     }
1165
1166     if (setjmp(jmp) != 0) {
1167         ret = 0;                /* Got signal, jump from dbd_readdir */
1168         goto exit;
1169     }
1170
1171     /* scanvol */
1172     if ( (scanvol(vi, flags)) != 0) {
1173         ret = -1;
1174         goto exit;
1175     }
1176
1177 exit:
1178     if (! nocniddb) {
1179         if (dbif_txn_close(dbd, ret == 0 ? 1 : 0) != 0)
1180             ret = -1;
1181         if (dbd_rebuild)
1182             if (dbif_txn_close(dbd_rebuild, ret == 0 ? 1 : 0) != 0)
1183                 ret = -1;
1184         if ((ret == 0) && dbd_rebuild && (flags & DBD_FLAGS_EXCL) && !(flags & DBD_FLAGS_FORCE))
1185             /* We can only do this in exclusive mode, otherwise we might delete CNIDs added from
1186                other clients in between our pass 1 and 2 */
1187             delete_orphaned_cnids(dbd, dbd_rebuild, flags);
1188     }
1189
1190     if (dbd_rebuild) {
1191         dbd_log(LOGDEBUG, "Closing tmp db");
1192         dbif_close(dbd_rebuild);
1193
1194         if (tmpdb_path) {
1195             char cmd[8 + MAXPATHLEN];
1196             snprintf(cmd, 8 + MAXPATHLEN, "rm -f %s/*", tmpdb_path);
1197             dbd_log( LOGDEBUG, "Removing temp database '%s'", tmpdb_path);
1198             system(cmd);
1199             snprintf(cmd, 8 + MAXPATHLEN, "rmdir %s", tmpdb_path);
1200             system(cmd);
1201         }        
1202     }
1203     return ret;
1204 }