]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/cmd_dbd_scanvol.c
Fix compiler diagnostics
[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/netatalk_conf.h>
32 #include <atalk/volume.h>
33 #include <atalk/ea.h>
34 #include <atalk/util.h>
35 #include <atalk/acl.h>
36 #include <atalk/compat.h>
37 #include <atalk/cnid.h>
38 #include <atalk/errchk.h>
39
40 #include "cmd_dbd.h"
41 #include "dbif.h"
42 #include "db_param.h"
43 #include "dbd.h"
44
45 /* Some defines to ease code parsing */
46 #define ADDIR_OK (addir_ok == 0)
47 #define ADFILE_OK (adfile_ok == 0)
48
49
50 static char           cwdbuf[MAXPATHLEN+1];
51 static struct vol     *vol;
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 char pname[MAXPATHLEN] = "../";
68
69 /*
70   Taken form afpd/desktop.c
71 */
72 static char *utompath(char *upath)
73 {
74     static char  mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
75     char         *m, *u;
76     uint16_t     flags = CONV_IGNORE | CONV_UNESCAPEHEX;
77     size_t       outlen;
78
79     if (!upath)
80         return NULL;
81
82     m = mpath;
83     u = upath;
84     outlen = strlen(upath);
85
86     if ((vol->v_casefold & AFPVOL_UTOMUPPER))
87         flags |= CONV_TOUPPER;
88     else if ((vol->v_casefold & AFPVOL_UTOMLOWER))
89         flags |= CONV_TOLOWER;
90
91     if ((vol->v_flags & AFPVOL_EILSEQ)) {
92         flags |= CONV__EILSEQ;
93     }
94
95     /* convert charsets */
96     if ((size_t)-1 == ( outlen = convert_charset(vol->v_volcharset,
97                                                  CH_UTF8_MAC,
98                                                  vol->v_maccharset,
99                                                  u, outlen, mpath, MAXPATHLEN, &flags)) ) {
100         dbd_log( LOGSTD, "Conversion from %s to %s for %s failed.",
101                  vol->v_volcodepage, vol->v_maccodepage, u);
102         return NULL;
103     }
104
105     return(m);
106 }
107
108 /*
109   Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
110   Returns pointer to name or NULL.
111 */
112 static const char *check_netatalk_dirs(const char *name)
113 {
114     int c;
115
116     for (c=0; netatalk_dirs[c]; c++) {
117         if ((strcmp(name, netatalk_dirs[c])) == 0)
118             return netatalk_dirs[c];
119     }
120     return NULL;
121 }
122
123 /*
124   Check for special names
125   Returns pointer to name or NULL.
126 */
127 static const char *check_special_dirs(const char *name)
128 {
129     int c;
130
131     for (c=0; special_dirs[c]; c++) {
132         if ((strcmp(name, special_dirs[c])) == 0)
133             return special_dirs[c];
134     }
135     return NULL;
136 }
137
138 /*
139  * We unCAPed a name, update CNID db
140  */
141 static int update_cnid(cnid_t did, const struct stat *sp, const char *oldname, const char *newname)
142 {
143     cnid_t id;
144
145     /* Query the database */
146     if ((id = cnid_lookup(vol->v_cdb, sp, did, (char *)oldname, strlen(oldname))) == CNID_INVALID)
147         /* not in db, no need to update */
148         return 0;
149
150     /* Update the database */
151     if (cnid_update(vol->v_cdb, id, sp, did, (char *)newname, strlen(newname)) < 0)
152         return -1;
153
154     return 0;
155 }
156
157 /*
158   Check for .AppleDouble file, create if missing
159 */
160 static int check_adfile(const char *fname, const struct stat *st, const char **newname)
161 {
162     int ret;
163     int adflags = ADFLAGS_HF;
164     struct adouble ad;
165     const char *adname;
166
167     *newname = NULL;
168
169     if (vol->v_adouble == AD_VERSION_EA) {
170         if (!(dbd_flags & DBD_FLAGS_V2TOEA))
171             return 0;
172         if (ad_convert(fname, st, vol, newname) != 0) {
173             switch (errno) {
174             case ENOENT:
175                 break;
176             default:
177                 dbd_log(LOGSTD, "Conversion error for \"%s/%s\": %s", cwdbuf, fname, strerror(errno));
178                 break;
179             }
180         }
181         return 0;
182     }
183     
184     if (S_ISDIR(st->st_mode))
185         adflags |= ADFLAGS_DIR;
186
187     adname = vol->ad_path(fname, adflags);
188
189     if ((ret = access( adname, F_OK)) != 0) {
190         if (errno != ENOENT) {
191             dbd_log(LOGSTD, "Access error for ad-file '%s/%s': %s",
192                     cwdbuf, adname, strerror(errno));
193             return -1;
194         }
195         /* Missing. Log and create it */
196         dbd_log(LOGSTD, "Missing AppleDouble file '%s/%s'", cwdbuf, adname);
197
198         if (dbd_flags & DBD_FLAGS_SCAN)
199             /* Scan only requested, dont change anything */
200             return -1;
201
202         /* Create ad file */
203         ad_init(&ad, vol);
204
205         if ((ret = ad_open(&ad, fname, adflags | ADFLAGS_CREATE | ADFLAGS_RDWR, 0666)) != 0) {
206             dbd_log( LOGSTD, "Error creating AppleDouble file '%s/%s': %s",
207                      cwdbuf, adname, strerror(errno));
208
209             return -1;
210         }
211
212         /* Set name in ad-file */
213         ad_setname(&ad, utompath((char *)fname));
214         ad_flush(&ad);
215         ad_close(&ad, ADFLAGS_HF);
216
217         chown(adname, st->st_uid, st->st_gid);
218         /* FIXME: should we inherit mode too here ? */
219 #if 0
220         chmod(adname, st->st_mode);
221 #endif
222     } else {
223         ad_init(&ad, vol);
224         if (ad_open(&ad, fname, adflags | ADFLAGS_RDONLY) != 0) {
225             dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s'", cwdbuf, fname);
226             return -1;
227         }
228         ad_close(&ad, ADFLAGS_HF);
229     }
230     return 0;
231 }
232
233 /* 
234    Remove all files with file::EA* from adouble dir
235 */
236 static void remove_eafiles(const char *name, struct ea *ea)
237 {
238     DIR *dp = NULL;
239     struct dirent *ep;
240     char eaname[MAXPATHLEN];
241
242     strlcpy(eaname, name, sizeof(eaname));
243     if (strlcat(eaname, "::EA", sizeof(eaname)) >= sizeof(eaname)) {
244         dbd_log(LOGSTD, "name too long: '%s/%s/%s'", cwdbuf, ADv2_DIRNAME, name);
245         return;
246     }
247
248     if ((chdir(ADv2_DIRNAME)) != 0) {
249         dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
250                 cwdbuf, ADv2_DIRNAME, strerror(errno));
251         return;
252     }
253
254     if ((dp = opendir(".")) == NULL) {
255         dbd_log(LOGSTD, "Couldn't open the directory '%s/%s': %s",
256                 cwdbuf, ADv2_DIRNAME, strerror(errno));
257         goto exit;
258     }
259
260     while ((ep = readdir(dp))) {
261         if (strstr(ep->d_name, eaname) != NULL) {
262             dbd_log(LOGSTD, "Removing EA file: '%s/%s/%s'",
263                     cwdbuf, ADv2_DIRNAME, ep->d_name);
264             if ((unlink(ep->d_name)) != 0) {
265                 dbd_log(LOGSTD, "Error unlinking EA file '%s/%s/%s': %s",
266                         cwdbuf, ADv2_DIRNAME, ep->d_name, strerror(errno));
267             }
268         } /* if */
269     } /* while */
270
271 exit:
272     if (dp)
273         closedir(dp);
274     if ((chdir("..")) != 0) {
275         dbd_log(LOGSTD, "Couldn't chdir to '%s': %s", cwdbuf, strerror(errno));
276         /* we can't proceed */
277         longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
278     }    
279 }
280
281 /*
282   Check Extended Attributes files
283 */
284 static int check_eafiles(const char *fname)
285 {
286     unsigned int  count = 0;
287     int ret = 0, remove;
288     struct ea ea;
289     struct stat st;
290     char *eaname;
291
292     if ((ret = ea_open(vol, fname, EA_RDWR, &ea)) != 0) {
293         if (errno == ENOENT)
294             return 0;
295         dbd_log(LOGSTD, "Error calling ea_open for file: %s/%s, removing EA files", cwdbuf, fname);
296         if ( ! (dbd_flags & DBD_FLAGS_SCAN))
297             remove_eafiles(fname, &ea);
298         return -1;
299     }
300
301     /* Check all EAs */
302     while (count < ea.ea_count) {        
303         dbd_log(LOGDEBUG, "EA: %s", (*ea.ea_entries)[count].ea_name);
304         remove = 0;
305
306         eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name, 0);
307
308         if (lstat(eaname, &st) != 0) {
309             if (errno == ENOENT)
310                 dbd_log(LOGSTD, "Missing EA: %s/%s", cwdbuf, eaname);
311             else
312                 dbd_log(LOGSTD, "Bogus EA: %s/%s", cwdbuf, eaname);
313             remove = 1;
314         } else if (st.st_size != (*ea.ea_entries)[count].ea_size) {
315             dbd_log(LOGSTD, "Bogus EA: %s/%s, removing it...", cwdbuf, eaname);
316             remove = 1;
317             if ((unlink(eaname)) != 0)
318                 dbd_log(LOGSTD, "Error removing EA file '%s/%s': %s",
319                         cwdbuf, eaname, strerror(errno));
320         }
321
322         if (remove) {
323             /* Be CAREFUL here! This should do what ea_delentry does. ea_close relies on it !*/
324             free((*ea.ea_entries)[count].ea_name);
325             (*ea.ea_entries)[count].ea_name = NULL;
326         }
327
328         count++;
329     } /* while */
330
331     ea_close(&ea);
332     return ret;
333 }
334
335 /*
336   Check for .AppleDouble folder and .Parent, create if missing
337 */
338 static int check_addir(int volroot)
339 {
340     int addir_ok, adpar_ok;
341     struct stat st;
342     struct adouble ad;
343     char *mname = NULL;
344
345     if (vol->v_adouble == AD_VERSION_EA)
346         return 0;
347
348     /* Check for ad-dir */
349     if ( (addir_ok = access(ADv2_DIRNAME, F_OK)) != 0) {
350         if (errno != ENOENT) {
351             dbd_log(LOGSTD, "Access error in directory %s: %s", cwdbuf, strerror(errno));
352             return -1;
353         }
354         dbd_log(LOGSTD, "Missing %s for '%s'", ADv2_DIRNAME, cwdbuf);
355     }
356
357     /* Check for ".Parent" */
358     if ( (adpar_ok = access(vol->ad_path(".", ADFLAGS_DIR), F_OK)) != 0) {
359         if (errno != ENOENT) {
360             dbd_log(LOGSTD, "Access error on '%s/%s': %s",
361                     cwdbuf, vol->ad_path(".", ADFLAGS_DIR), strerror(errno));
362             return -1;
363         }
364         dbd_log(LOGSTD, "Missing .AppleDouble/.Parent for '%s'", cwdbuf);
365     }
366
367     /* Is one missing ? */
368     if ((addir_ok != 0) || (adpar_ok != 0)) {
369         /* Yes, but are we only scanning ? */
370         if (dbd_flags & DBD_FLAGS_SCAN) {
371             /* Yes:  missing .Parent is not a problem, but missing ad-dir
372                causes later checking of ad-files to fail. So we have to return appropiately */
373             if (addir_ok != 0)
374                 return -1;
375             else  /* (adpar_ok != 0) */
376                 return 0;
377         }
378
379         /* Create ad dir and set name */
380         ad_init(&ad, vol);
381
382         if (ad_open(&ad, ".", ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_CREATE | ADFLAGS_RDWR, 0777) != 0) {
383             dbd_log( LOGSTD, "Error creating AppleDouble dir in %s: %s", cwdbuf, strerror(errno));
384             return -1;
385         }
386
387         /* Get basename of cwd from cwdbuf */
388         mname = utompath(strrchr(cwdbuf, '/') + 1);
389
390         /* Update name in ad file */
391         ad_setname(&ad, mname);
392         ad_flush(&ad);
393         ad_close(&ad, ADFLAGS_HF);
394
395         /* Inherit owner/group from "." to ".AppleDouble" and ".Parent" */
396         if ((lstat(".", &st)) != 0) {
397             dbd_log( LOGSTD, "Couldnt stat %s: %s", cwdbuf, strerror(errno));
398             return -1;
399         }
400         chown(ADv2_DIRNAME, st.st_uid, st.st_gid);
401         chown(vol->ad_path(".", ADFLAGS_DIR), st.st_uid, st.st_gid);
402     }
403
404     return 0;
405 }
406
407 /*
408   Check if file cotains "::EA" and if it does check if its correspondig data fork exists.
409   Returns:
410   0 = name is not an EA file
411   1 = name is an EA file and no problem was found
412   -1 = name is an EA file and data fork is gone
413  */
414 static int check_eafile_in_adouble(const char *name)
415 {
416     int ret = 0;
417     char *namep, *namedup = NULL;
418
419     /* Check if this is an AFPVOL_EA_AD vol */
420     if (vol->v_vfs_ea == AFPVOL_EA_AD) {
421         /* Does the filename contain "::EA" ? */
422         namedup = strdup(name);
423         if ((namep = strstr(namedup, "::EA")) == NULL) {
424             ret = 0;
425             goto ea_check_done;
426         } else {
427             /* File contains "::EA" so it's an EA file. Check for data file  */
428
429             /* Get string before "::EA" from EA filename */
430             namep[0] = 0;
431             strlcpy(pname + 3, namedup, sizeof(pname)); /* Prepends "../" */
432
433             if ((access( pname, F_OK)) == 0) {
434                 ret = 1;
435                 goto ea_check_done;
436             } else {
437                 ret = -1;
438                 if (errno != ENOENT) {
439                     dbd_log(LOGSTD, "Access error for file '%s/%s': %s",
440                             cwdbuf, name, strerror(errno));
441                     goto ea_check_done;
442                 }
443
444                 /* Orphaned EA file*/
445                 dbd_log(LOGSTD, "Orphaned Extended Attribute file '%s/%s/%s'",
446                         cwdbuf, ADv2_DIRNAME, name);
447
448                 if (dbd_flags & DBD_FLAGS_SCAN)
449                     /* Scan only requested, dont change anything */
450                     goto ea_check_done;
451
452                 if ((unlink(name)) != 0) {
453                     dbd_log(LOGSTD, "Error unlinking orphaned Extended Attribute file '%s/%s/%s'",
454                             cwdbuf, ADv2_DIRNAME, name);
455                 }
456             } /* if (access) */
457         } /* if strstr */
458     } /* if AFPVOL_EA_AD */
459
460 ea_check_done:
461     if (namedup)
462         free(namedup);
463
464     return ret;
465 }
466
467 /*
468   Check files and dirs inside .AppleDouble folder:
469   - remove orphaned files
470   - bail on dirs
471 */
472 static int read_addir(void)
473 {
474     DIR *dp;
475     struct dirent *ep;
476     struct stat st;
477
478     if ((chdir(ADv2_DIRNAME)) != 0) {
479         if (vol->v_adouble == AD_VERSION_EA) {
480             return 0;
481         }
482         dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
483                 cwdbuf, ADv2_DIRNAME, strerror(errno));
484         return -1;
485     }
486
487     if ((dp = opendir(".")) == NULL) {
488         dbd_log(LOGSTD, "Couldn't open the directory '%s/%s': %s",
489                 cwdbuf, ADv2_DIRNAME, strerror(errno));
490         return -1;
491     }
492
493     while ((ep = readdir(dp))) {
494         /* Check if its "." or ".." */
495         if (DIR_DOT_OR_DOTDOT(ep->d_name))
496             continue;
497
498         /* Skip ".Parent" */
499         if (STRCMP(ep->d_name, ==, ".Parent"))
500             continue;
501
502         if ((lstat(ep->d_name, &st)) < 0) {
503             dbd_log( LOGSTD, "Lost file or dir while enumeratin dir '%s/%s/%s', probably removed: %s",
504                      cwdbuf, ADv2_DIRNAME, ep->d_name, strerror(errno));
505             continue;
506         }
507
508         /* Check for dirs */
509         if (S_ISDIR(st.st_mode)) {
510             dbd_log( LOGSTD, "Unexpected directory '%s' in AppleDouble dir '%s/%s'",
511                      ep->d_name, cwdbuf, ADv2_DIRNAME);
512             continue;
513         }
514
515         /* Check if for orphaned and corrupt Extended Attributes file */
516         if (check_eafile_in_adouble(ep->d_name) != 0)
517             continue;
518
519         /* Check for data file */
520         strcpy(pname + 3, ep->d_name);
521         if ((access( pname, F_OK)) != 0) {
522             if (errno != ENOENT) {
523                 dbd_log(LOGSTD, "Access error for file '%s/%s': %s",
524                         cwdbuf, pname, strerror(errno));
525                 continue;
526             }
527             /* Orphaned ad-file*/
528             dbd_log(LOGSTD, "Orphaned AppleDoube file '%s/%s/%s'",
529                     cwdbuf, ADv2_DIRNAME, ep->d_name);
530
531             if (dbd_flags & DBD_FLAGS_SCAN)
532                 /* Scan only requested, dont change anything */
533                 continue;;
534
535             if ((unlink(ep->d_name)) != 0) {
536                 dbd_log(LOGSTD, "Error unlinking orphaned AppleDoube file '%s/%s/%s'",
537                         cwdbuf, ADv2_DIRNAME, ep->d_name);
538
539             }
540         }
541     }
542
543     if ((chdir("..")) != 0) {
544         dbd_log(LOGSTD, "Couldn't chdir back to '%s' from AppleDouble dir: %s",
545                 cwdbuf, strerror(errno));
546         /* This really is EOT! */
547         longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
548     }
549
550     closedir(dp);
551
552     return 0;
553 }
554
555 /*
556   Check CNID for a file/dir, both from db and from ad-file.
557   For detailed specs see intro.
558
559   @return Correct CNID of object or CNID_INVALID (ie 0) on error
560 */
561 static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfile_ok)
562 {
563     int adflags = ADFLAGS_HF;
564     cnid_t db_cnid, ad_cnid;
565     struct adouble ad;
566
567     adflags = ADFLAGS_HF | (S_ISDIR(st->st_mode) ? ADFLAGS_DIR : 0);
568
569     /* Get CNID from ad-file */
570     ad_cnid = CNID_INVALID;
571     if (ADFILE_OK) {
572         ad_init(&ad, vol);
573         if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
574             
575             if (vol->v_adouble != AD_VERSION_EA) {
576                 dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s': %s", cwdbuf, name, strerror(errno));
577                 return CNID_INVALID;
578             }
579             dbd_log( LOGDEBUG, "File without meta EA: \"%s/%s\"", cwdbuf, name);
580             adfile_ok = 1;
581         } else {
582             ad_cnid = ad_getid(&ad, st->st_dev, st->st_ino, 0, stamp);
583             if (ad_cnid == CNID_INVALID)
584                 dbd_log( LOGSTD, "Bad CNID in adouble file of '%s/%s'", cwdbuf, name);
585             else
586                 dbd_log( LOGDEBUG, "CNID from .AppleDouble file for '%s/%s': %u", cwdbuf, name, ntohl(ad_cnid));
587             ad_close(&ad, ADFLAGS_HF);
588         }
589     }
590
591     /* Get CNID from database */
592     if ((db_cnid = cnid_add(vol->v_cdb, st, did, (char *)name, strlen(name), ad_cnid)) == CNID_INVALID)
593         return CNID_INVALID;
594
595     /* Compare CNID from db and adouble file */
596     if (ad_cnid != db_cnid && adfile_ok == 0) {
597         /* Mismatch, overwrite ad file with value from db */
598         dbd_log(LOGSTD, "CNID mismatch for '%s/%s', CNID db: %u, ad-file: %u",
599                 cwdbuf, name, ntohl(db_cnid), ntohl(ad_cnid));
600         ad_init(&ad, vol);
601         if (ad_open(&ad, name, adflags | ADFLAGS_HF | ADFLAGS_RDWR) != 0) {
602             dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
603                     cwdbuf, name, strerror(errno));
604             return CNID_INVALID;
605         }
606         ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
607         ad_flush(&ad);
608         ad_close(&ad, ADFLAGS_HF);
609     }
610
611     return db_cnid;
612 }
613
614 /*
615   This is called recursively for all dirs.
616   volroot=1 means we're in the volume root dir, 0 means we aren't.
617   We use this when checking for netatalk private folders like .AppleDB.
618   did is our parents CNID.
619 */
620 static int dbd_readdir(int volroot, cnid_t did)
621 {
622     int cwd, ret = 0, adfile_ok, addir_ok;
623     cnid_t cnid = 0;
624     const char *name;
625     DIR *dp;
626     struct dirent *ep;
627     static struct stat st;      /* Save some stack space */
628
629     /* Check again for .AppleDouble folder, check_adfile also checks/creates it */
630     if ((addir_ok = check_addir(volroot)) != 0)
631         if ( ! (dbd_flags & DBD_FLAGS_SCAN))
632             /* Fatal on rebuild run, continue if only scanning ! */
633             return -1;
634
635     /* Check AppleDouble files in AppleDouble folder, but only if it exists or could be created */
636     if (ADDIR_OK)
637         if ((read_addir()) != 0)
638             if ( ! (dbd_flags & DBD_FLAGS_SCAN))
639                 /* Fatal on rebuild run, continue if only scanning ! */
640                 return -1;
641
642     if ((dp = opendir (".")) == NULL) {
643         dbd_log(LOGSTD, "Couldn't open the directory: %s",strerror(errno));
644         return -1;
645     }
646
647     while ((ep = readdir (dp))) {
648         /* Check if we got a termination signal */
649         if (alarmed)
650             longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
651
652         /* Check if its "." or ".." */
653         if (DIR_DOT_OR_DOTDOT(ep->d_name))
654             continue;
655
656         /* Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop" */
657         if ((name = check_netatalk_dirs(ep->d_name)) != NULL) {
658             if (! volroot)
659                 dbd_log(LOGSTD, "Nested %s in %s", name, cwdbuf);
660             continue;
661         }
662
663         /* Check for special folders in volume root e.g. ".zfs" */
664         if (volroot) {
665             if ((name = check_special_dirs(ep->d_name)) != NULL) {
666                 dbd_log(LOGSTD, "Ignoring special dir \"%s\"", name);
667                 continue;
668             }
669         }
670
671         /* Skip .AppleDouble dir in this loop */
672         if (STRCMP(ep->d_name, == , ADv2_DIRNAME))
673             continue;
674
675         if (!vol->vfs->vfs_validupath(vol, ep->d_name)) {
676             dbd_log(LOGDEBUG, "Ignoring \"%s\"", ep->d_name);
677             continue;
678         }
679
680         if ((ret = lstat(ep->d_name, &st)) < 0) {
681             dbd_log( LOGSTD, "Lost file while reading dir '%s/%s', probably removed: %s",
682                      cwdbuf, ep->d_name, strerror(errno));
683             continue;
684         }
685         
686         switch (st.st_mode & S_IFMT) {
687         case S_IFREG:
688         case S_IFDIR:
689             break;
690         case S_IFLNK:
691             dbd_log(LOGDEBUG, "Ignoring symlink %s/%s", cwdbuf, ep->d_name);
692             continue;
693         default:
694             dbd_log(LOGSTD, "Bad filetype: %s/%s", cwdbuf, ep->d_name);
695             if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
696                 if ((unlink(ep->d_name)) != 0) {
697                     dbd_log(LOGSTD, "Error removing: %s/%s: %s", cwdbuf, ep->d_name, strerror(errno));
698                 }
699             }
700             continue;
701         }
702
703         /**************************************************************************
704            Statistics
705          **************************************************************************/
706         static unsigned long long statcount = 0;
707         static time_t t = 0;
708
709         if (t == 0)
710             t = time(NULL);
711
712         statcount++;
713         if ((statcount % 10000) == 0) {
714             if (dbd_flags & DBD_FLAGS_STATS)            
715                 dbd_log(LOGSTD, "Scanned: %10llu, time: %10llu s",
716                         statcount, (unsigned long long)(time(NULL) - t));
717         }
718
719         /**************************************************************************
720            Tests
721         **************************************************************************/
722
723         /* Check for appledouble file, create if missing, but only if we have addir */
724         const char *name = NULL;
725         adfile_ok = -1;
726         if (ADDIR_OK)
727             adfile_ok = check_adfile(ep->d_name, &st, &name);
728
729         if (name == NULL) {
730             name = ep->d_name;
731         } else {
732             update_cnid(did, &st, ep->d_name, name);
733         }
734
735         /* Check CNIDs */
736         cnid = check_cnid(name, did, &st, adfile_ok);
737
738         /* Now add this object to our rebuild dbd */
739         if (cnid && dbd_rebuild) {
740             static uint count = 0;
741             rqst.cnid = rply.cnid;
742             ret = dbd_rebuild_add(dbd_rebuild, &rqst, &rply);
743             if (dbif_txn_close(dbd_rebuild, ret) != 0)
744                 return -1;
745             if (rply.result != CNID_DBD_RES_OK) {
746                 dbd_log( LOGSTD, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
747                          cnid, cwdbuf, name);
748                 return -1;
749             }
750             count++;
751             if (count == 10000) {
752                 if (dbif_txn_checkpoint(dbd_rebuild, 0, 0, 0) < 0) {
753                     dbd_log(LOGSTD, "Error checkpointing!");
754                     return -1;
755                 }
756                 count = 0;
757             }
758         }
759
760         /* Check EA files */
761         if (vol->v_vfs_ea == AFPVOL_EA_AD)
762             check_eafiles(name);
763
764         /**************************************************************************
765           Recursion
766         **************************************************************************/
767         if (S_ISDIR(st.st_mode) && cnid) { /* If we have no cnid for it we cant enter recursion */
768             strcat(cwdbuf, "/");
769             strcat(cwdbuf, name);
770             dbd_log( LOGDEBUG, "Entering directory: %s", cwdbuf);
771             if (-1 == (cwd = open(".", O_RDONLY))) {
772                 dbd_log( LOGSTD, "Cant open directory '%s': %s", cwdbuf, strerror(errno));
773                 continue;
774             }
775             if (0 != chdir(name)) {
776                 dbd_log( LOGSTD, "Cant chdir to directory '%s': %s", cwdbuf, strerror(errno));
777                 close(cwd);
778                 continue;
779             }
780
781             ret = dbd_readdir(0, cnid);
782
783             fchdir(cwd);
784             close(cwd);
785             *(strrchr(cwdbuf, '/')) = 0;
786             if (ret < 0)
787                 return -1;
788         }
789     }
790
791     /*
792       Use results of previous checks
793     */
794     if ((vol->v_adouble == AD_VERSION_EA) && (dbd_flags & DBD_FLAGS_V2TOEA)) {
795         if (rmdir(ADv2_DIRNAME) != 0) {
796             switch (errno) {
797             case ENOENT:
798                 break;
799             default:
800                 dbd_log(LOGSTD, "Error removing adouble dir \"%s/%s\": %s", cwdbuf, ADv2_DIRNAME, strerror(errno));
801                 break;
802             }
803         }
804     }
805     closedir(dp);
806     return ret;
807 }
808
809 /*
810   Main func called from cmd_dbd.c
811 */
812 int cmd_dbd_scanvol(struct vol *vol_in, dbd_flags_t flags)
813 {
814     EC_INIT;
815     struct stat st;
816
817     /* Run with umask 0 */
818     umask(0);
819
820     /* Make vol accessible for all funcs */
821     vol = vol_in;
822     dbd_flags = flags;
823
824     /* We only support unicode volumes ! */
825     if (vol->v_volcharset != CH_UTF8) {
826         dbd_log(LOGSTD, "Not a Unicode volume: %s, %u != %u", vol->v_volcodepage, vol->v_volcharset, CH_UTF8);
827         EC_FAIL;
828     }
829
830     /*
831      * Get CNID database stamp, cnid_getstamp() passes the buffer,
832      * then cnid_resolve() actually gets the value from the db
833      */
834     cnid_getstamp(vol->v_cdb, stamp, sizeof(stamp));
835
836     if (setjmp(jmp) != 0) {
837         EC_EXIT_STATUS(0); /* Got signal, jump from dbd_readdir */
838     }
839
840     strcpy(cwdbuf, vol->v_path);
841     chdir(vol->v_path);
842
843     if ((vol->v_adouble == AD_VERSION_EA) && (dbd_flags & DBD_FLAGS_V2TOEA)) {
844         if (lstat(".", &st) != 0)
845             EC_FAIL;
846         if (ad_convert(".", &st, vol, NULL) != 0) {
847             switch (errno) {
848             case ENOENT:
849                 break;
850             default:
851                 dbd_log(LOGSTD, "Conversion error for \"%s\": %s", vol->v_path, strerror(errno));
852                 break;
853             }
854         }
855     }
856
857     /* Start recursion */
858     EC_NEG1( dbd_readdir(1, htonl(2)) );  /* 2 = volumeroot CNID */
859
860 EC_CLEANUP:
861     EC_EXIT;
862 }