2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
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.
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.
17 #endif /* HAVE_CONFIG_H */
21 #include <sys/types.h>
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>
35 #include <atalk/util.h>
36 #include <atalk/acl.h>
37 #include <atalk/compat.h>
44 /* Some defines to ease code parsing */
45 #define ADDIR_OK (addir_ok == 0)
46 #define ADFILE_OK (adfile_ok == 0)
49 static struct volinfo *myvolinfo;
50 static char cwdbuf[MAXPATHLEN+1];
52 static DBD *dbd_rebuild;
53 static dbd_flags_t dbd_flags;
54 static char stamp[CNID_DEV_LEN];
55 static char *netatalk_dirs[] = {
60 static char *special_dirs[] = {
64 static struct cnid_dbd_rqst rqst;
65 static struct cnid_dbd_rply rply;
67 static struct vol volume; /* fake it for ea_open */
68 static char pname[MAXPATHLEN] = "../";
71 Taken form afpd/desktop.c
73 static char *utompath(char *upath)
75 static char mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
77 uint16_t flags = CONV_IGNORE | CONV_UNESCAPEHEX;
85 outlen = strlen(upath);
87 if ((myvolinfo->v_casefold & AFPVOL_UTOMUPPER))
88 flags |= CONV_TOUPPER;
89 else if ((myvolinfo->v_casefold & AFPVOL_UTOMLOWER))
90 flags |= CONV_TOLOWER;
92 if ((myvolinfo->v_flags & AFPVOL_EILSEQ)) {
93 flags |= CONV__EILSEQ;
96 /* convert charsets */
97 if ((size_t)-1 == ( outlen = convert_charset(myvolinfo->v_volcharset,
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);
110 Taken form afpd/desktop.c
112 static char *mtoupath(char *mpath)
114 static char upath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
123 if ( *mpath == '\0' ) {
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;
133 if ((myvolinfo->v_casefold & AFPVOL_MTOUUPPER))
134 flags |= CONV_TOUPPER;
135 else if ((myvolinfo->v_casefold & AFPVOL_MTOULOWER))
136 flags |= CONV_TOLOWER;
138 if ((myvolinfo->v_flags & AFPVOL_EILSEQ)) {
139 flags |= CONV__EILSEQ;
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);
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.
164 static int check_name_encoding(char *uname)
168 roundtripped = mtoupath(utompath(uname));
170 dbd_log( LOGSTD, "Error checking encoding for '%s/%s'", cwdbuf, uname);
174 if ( STRCMP(uname, !=, roundtripped)) {
175 dbd_log( LOGSTD, "Bad encoding for '%s/%s'", cwdbuf, uname);
183 Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
184 Returns pointer to name or NULL.
186 static const char *check_netatalk_dirs(const char *name)
190 for (c=0; netatalk_dirs[c]; c++) {
191 if ((strcmp(name, netatalk_dirs[c])) == 0)
192 return netatalk_dirs[c];
198 Check for special names
199 Returns pointer to name or NULL.
201 static const char *check_special_dirs(const char *name)
205 for (c=0; special_dirs[c]; c++) {
206 if ((strcmp(name, special_dirs[c])) == 0)
207 return special_dirs[c];
213 Check for .AppleDouble file, create if missing
215 static int check_adfile(const char *fname, const struct stat *st)
218 int adflags = ADFLAGS_HF;
222 if (volume.v_adouble == AD_VERSION_EA)
225 if (dbd_flags & DBD_FLAGS_CLEANUP)
228 if (S_ISREG(st->st_mode))
229 adflags |= ADFLAGS_DIR;
231 adname = volume.ad_path(fname, adflags);
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));
239 /* Missing. Log and create it */
240 dbd_log(LOGSTD, "Missing AppleDouble file '%s/%s'", cwdbuf, adname);
242 if (dbd_flags & DBD_FLAGS_SCAN)
243 /* Scan only requested, dont change anything */
247 ad_init(&ad, &volume);
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));
256 /* Set name in ad-file */
257 ad_setname(&ad, utompath((char *)fname));
259 ad_close(&ad, ADFLAGS_HF);
261 chown(adname, st->st_uid, st->st_gid);
262 /* FIXME: should we inherit mode too here ? */
264 chmod(adname, st->st_mode);
267 ad_init(&ad, &volume);
268 if (ad_open(&ad, fname, adflags | ADFLAGS_RDONLY) != 0) {
269 dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s'", cwdbuf, fname);
272 ad_close(&ad, ADFLAGS_HF);
278 Remove all files with file::EA* from adouble dir
280 static void remove_eafiles(const char *name, struct ea *ea)
284 char eaname[MAXPATHLEN];
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);
292 if ((chdir(ADv2_DIRNAME)) != 0) {
293 dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
294 cwdbuf, ADv2_DIRNAME, strerror(errno));
298 if ((dp = opendir(".")) == NULL) {
299 dbd_log(LOGSTD, "Couldn't open the directory '%s/%s': %s",
300 cwdbuf, ADv2_DIRNAME, strerror(errno));
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));
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() */
326 Check Extended Attributes files
328 static int check_eafiles(const char *fname)
330 unsigned int count = 0;
336 if ((ret = ea_open(&volume, fname, EA_RDWR, &ea)) != 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);
346 while (count < ea.ea_count) {
347 dbd_log(LOGDEBUG, "EA: %s", (*ea.ea_entries)[count].ea_name);
350 eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name, 0);
352 if (lstat(eaname, &st) != 0) {
354 dbd_log(LOGSTD, "Missing EA: %s/%s", cwdbuf, eaname);
356 dbd_log(LOGSTD, "Bogus EA: %s/%s", cwdbuf, eaname);
358 } else if (st.st_size != (*ea.ea_entries)[count].ea_size) {
359 dbd_log(LOGSTD, "Bogus EA: %s/%s, removing it...", cwdbuf, eaname);
361 if ((unlink(eaname)) != 0)
362 dbd_log(LOGSTD, "Error removing EA file '%s/%s': %s",
363 cwdbuf, eaname, strerror(errno));
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;
380 Check for .AppleDouble folder and .Parent, create if missing
382 static int check_addir(int volroot)
384 int addir_ok, adpar_ok;
389 if (dbd_flags & DBD_FLAGS_CLEANUP)
392 if (volume.v_adouble == AD_VERSION_EA)
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));
401 dbd_log(LOGSTD, "Missing %s for '%s'", ADv2_DIRNAME, cwdbuf);
404 /* Check for ".Parent" */
405 if ( (adpar_ok = access(volume.ad_path(".", ADFLAGS_DIR), F_OK)) != 0) {
406 if (errno != ENOENT) {
407 dbd_log(LOGSTD, "Access error on '%s/%s': %s",
408 cwdbuf, volume.ad_path(".", ADFLAGS_DIR), strerror(errno));
411 dbd_log(LOGSTD, "Missing .AppleDouble/.Parent for '%s'", cwdbuf);
414 /* Is one missing ? */
415 if ((addir_ok != 0) || (adpar_ok != 0)) {
416 /* Yes, but are we only scanning ? */
417 if (dbd_flags & DBD_FLAGS_SCAN) {
418 /* Yes: missing .Parent is not a problem, but missing ad-dir
419 causes later checking of ad-files to fail. So we have to return appropiately */
422 else /* (adpar_ok != 0) */
426 /* Create ad dir and set name */
427 ad_init(&ad, &volume);
429 if (ad_open(&ad, ".", ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_CREATE | ADFLAGS_RDWR, 0777) != 0) {
430 dbd_log( LOGSTD, "Error creating AppleDouble dir in %s: %s", cwdbuf, strerror(errno));
434 /* Get basename of cwd from cwdbuf */
435 mname = utompath(strrchr(cwdbuf, '/') + 1);
437 /* Update name in ad file */
438 ad_setname(&ad, mname);
440 ad_close(&ad, ADFLAGS_HF);
442 /* Inherit owner/group from "." to ".AppleDouble" and ".Parent" */
443 if ((lstat(".", &st)) != 0) {
444 dbd_log( LOGSTD, "Couldnt stat %s: %s", cwdbuf, strerror(errno));
447 chown(ADv2_DIRNAME, st.st_uid, st.st_gid);
448 chown(volume.ad_path(".", ADFLAGS_DIR), st.st_uid, st.st_gid);
455 Check if file cotains "::EA" and if it does check if its correspondig data fork exists.
457 0 = name is not an EA file
458 1 = name is an EA file and no problem was found
459 -1 = name is an EA file and data fork is gone
461 static int check_eafile_in_adouble(const char *name)
464 char *namep, *namedup = NULL;
466 /* Check if this is an AFPVOL_EA_AD vol */
467 if (myvolinfo->v_vfs_ea == AFPVOL_EA_AD) {
468 /* Does the filename contain "::EA" ? */
469 namedup = strdup(name);
470 if ((namep = strstr(namedup, "::EA")) == NULL) {
474 /* File contains "::EA" so it's an EA file. Check for data file */
476 /* Get string before "::EA" from EA filename */
478 strlcpy(pname + 3, namedup, sizeof(pname)); /* Prepends "../" */
480 if ((access( pname, F_OK)) == 0) {
485 if (errno != ENOENT) {
486 dbd_log(LOGSTD, "Access error for file '%s/%s': %s",
487 cwdbuf, name, strerror(errno));
491 /* Orphaned EA file*/
492 dbd_log(LOGSTD, "Orphaned Extended Attribute file '%s/%s/%s'",
493 cwdbuf, ADv2_DIRNAME, name);
495 if (dbd_flags & DBD_FLAGS_SCAN)
496 /* Scan only requested, dont change anything */
499 if ((unlink(name)) != 0) {
500 dbd_log(LOGSTD, "Error unlinking orphaned Extended Attribute file '%s/%s/%s'",
501 cwdbuf, ADv2_DIRNAME, name);
505 } /* if AFPVOL_EA_AD */
515 Check files and dirs inside .AppleDouble folder:
516 - remove orphaned files
519 static int read_addir(void)
525 if (volume.v_adouble == AD_VERSION_EA)
528 if ((chdir(ADv2_DIRNAME)) != 0) {
529 dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
530 cwdbuf, ADv2_DIRNAME, strerror(errno));
534 if ((dp = opendir(".")) == NULL) {
535 dbd_log(LOGSTD, "Couldn't open the directory '%s/%s': %s",
536 cwdbuf, ADv2_DIRNAME, strerror(errno));
540 while ((ep = readdir(dp))) {
541 /* Check if its "." or ".." */
542 if (DIR_DOT_OR_DOTDOT(ep->d_name))
546 if (STRCMP(ep->d_name, ==, ".Parent"))
549 if ((lstat(ep->d_name, &st)) < 0) {
550 dbd_log( LOGSTD, "Lost file or dir while enumeratin dir '%s/%s/%s', probably removed: %s",
551 cwdbuf, ADv2_DIRNAME, ep->d_name, strerror(errno));
556 if (S_ISDIR(st.st_mode)) {
557 dbd_log( LOGSTD, "Unexpected directory '%s' in AppleDouble dir '%s/%s'",
558 ep->d_name, cwdbuf, ADv2_DIRNAME);
562 /* Check if for orphaned and corrupt Extended Attributes file */
563 if (check_eafile_in_adouble(ep->d_name) != 0)
566 /* Check for data file */
567 strcpy(pname + 3, ep->d_name);
568 if ((access( pname, F_OK)) != 0) {
569 if (errno != ENOENT) {
570 dbd_log(LOGSTD, "Access error for file '%s/%s': %s",
571 cwdbuf, pname, strerror(errno));
574 /* Orphaned ad-file*/
575 dbd_log(LOGSTD, "Orphaned AppleDoube file '%s/%s/%s'",
576 cwdbuf, ADv2_DIRNAME, ep->d_name);
578 if (dbd_flags & DBD_FLAGS_SCAN)
579 /* Scan only requested, dont change anything */
582 if ((unlink(ep->d_name)) != 0) {
583 dbd_log(LOGSTD, "Error unlinking orphaned AppleDoube file '%s/%s/%s'",
584 cwdbuf, ADv2_DIRNAME, ep->d_name);
590 if ((chdir("..")) != 0) {
591 dbd_log(LOGSTD, "Couldn't chdir back to '%s' from AppleDouble dir: %s",
592 cwdbuf, strerror(errno));
593 /* This really is EOT! */
594 longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
603 Check CNID for a file/dir, both from db and from ad-file.
604 For detailed specs see intro.
606 @return Correct CNID of object or CNID_INVALID (ie 0) on error
608 static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfile_ok)
610 int ret, adflags = ADFLAGS_HF;
611 cnid_t db_cnid, ad_cnid;
614 adflags = ADFLAGS_HF | (S_ISDIR(st->st_mode) ? ADFLAGS_DIR : 0);
616 /* Force checkout every X items */
617 static int cnidcount = 0;
619 if (cnidcount > 10000) {
621 if (dbif_txn_checkpoint(dbd, 0, 0, 0) < 0) {
622 dbd_log(LOGSTD, "Error checkpointing!");
627 /* Get CNID from ad-file */
630 ad_init(&ad, &volume);
631 if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
633 if (dbd_flags & DBD_FLAGS_CLEANUP)
636 if (volume.v_adouble != AD_VERSION_EA) {
637 dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s': %s", cwdbuf, name, strerror(errno));
640 dbd_log( LOGDEBUG, "File without meta EA: \"%s/%s\"", cwdbuf, name);
643 if (dbd_flags & DBD_FLAGS_FORCE) {
644 ad_cnid = ad_forcegetid(&ad);
645 /* This ensures the changed stamp is written */
646 ad_setid( &ad, st->st_dev, st->st_ino, ad_cnid, did, stamp);
649 ad_cnid = ad_getid(&ad, st->st_dev, st->st_ino, 0, stamp);
652 dbd_log( LOGSTD, "Bad CNID in adouble file of '%s/%s'", cwdbuf, name);
654 dbd_log( LOGDEBUG, "CNID from .AppleDouble file for '%s/%s': %u", cwdbuf, name, ntohl(ad_cnid));
655 ad_close(&ad, ADFLAGS_HF);
659 /* Get CNID from database */
661 /* Prepare request data */
662 memset(&rqst, 0, sizeof(struct cnid_dbd_rqst));
663 memset(&rply, 0, sizeof(struct cnid_dbd_rply));
666 if ( ! (myvolinfo->v_flags & AFPVOL_NODEV))
667 rqst.dev = st->st_dev;
668 rqst.ino = st->st_ino;
669 rqst.type = S_ISDIR(st->st_mode)?1:0;
670 rqst.name = (char *)name;
671 rqst.namelen = strlen(name);
673 /* Query the database */
674 ret = dbd_lookup(dbd, &rqst, &rply, (dbd_flags & DBD_FLAGS_SCAN) ? 1 : 0);
675 if (dbif_txn_close(dbd, ret) != 0)
677 if (rply.result == CNID_DBD_RES_OK) {
679 } else if (rply.result == CNID_DBD_RES_NOTFOUND) {
680 if ( ! (dbd_flags & DBD_FLAGS_FORCE))
681 dbd_log( LOGSTD, "No CNID for '%s/%s' in database", cwdbuf, name);
684 dbd_log( LOGSTD, "Fatal error resolving '%s/%s'", cwdbuf, name);
688 /* Compare results from both CNID searches */
689 if (ad_cnid && db_cnid && (ad_cnid == db_cnid)) {
690 /* Everything is fine */
692 } else if (ad_cnid && db_cnid && (ad_cnid != db_cnid)) {
693 /* Mismatch, overwrite ad file with value from db */
694 dbd_log( LOGSTD, "CNID mismatch for '%s/%s', db: %u, ad-file: %u", cwdbuf, name, ntohl(db_cnid), ntohl(ad_cnid));
695 if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
696 dbd_log(LOGSTD, "Updating AppleDouble file for '%s/%s' with CNID: %u from database",
697 cwdbuf, name, ntohl(db_cnid));
698 ad_init(&ad, &volume);
699 if (ad_open(&ad, name, adflags | ADFLAGS_HF | ADFLAGS_RDWR) != 0) {
700 dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
701 cwdbuf, name, strerror(errno));
704 ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
706 ad_close(&ad, ADFLAGS_HF);
709 } else if (ad_cnid && (db_cnid == 0)) {
710 /* in ad-file but not in db */
711 if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
712 /* Ensure the cnid from the ad-file is not already occupied by another file */
713 dbd_log(LOGDEBUG, "Checking whether CNID %u from ad-file is occupied",
717 ret = dbd_resolve(dbd, &rqst, &rply);
718 if (rply.result == CNID_DBD_RES_OK) {
719 /* Occupied! Choose another, update ad-file */
720 ret = dbd_add(dbd, &rqst, &rply, 1);
721 if (dbif_txn_close(dbd, ret) != 0)
724 dbd_log(LOGSTD, "New CNID for '%s/%s': %u", cwdbuf, name, ntohl(db_cnid));
726 if (ADFILE_OK && ( ! (dbd_flags & DBD_FLAGS_SCAN))) {
727 dbd_log(LOGSTD, "Writing CNID data for '%s/%s' to AppleDouble file",
728 cwdbuf, name, ntohl(db_cnid));
729 ad_init(&ad, &volume);
730 if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
731 dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
732 cwdbuf, name, strerror(errno));
735 ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
737 ad_close(&ad, ADFLAGS_HF);
742 dbd_log(LOGDEBUG, "CNID rebuild add '%s/%s' with CNID from ad-file %u",
743 cwdbuf, name, ntohl(ad_cnid));
745 ret = dbd_rebuild_add(dbd, &rqst, &rply);
746 if (dbif_txn_close(dbd, ret) != 0)
750 } else if ((db_cnid == 0) && (ad_cnid == 0)) {
751 /* No CNID at all, we clearly have to allocate a fresh one... */
752 /* Note: the next test will use this new CNID too! */
753 if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
755 ret = dbd_add(dbd, &rqst, &rply, 1);
756 if (dbif_txn_close(dbd, ret) != 0)
759 dbd_log(LOGSTD, "New CNID for '%s/%s': %u", cwdbuf, name, ntohl(db_cnid));
763 if ((ad_cnid == 0) && db_cnid) {
764 /* in db but zeroID in ad-file, write it to ad-file */
766 && (volume.v_adouble == AD_VERSION2)
767 && ! (dbd_flags & DBD_FLAGS_SCAN)) {
768 dbd_log(LOGSTD, "Writing CNID data for '%s/%s' to AppleDouble file",
769 cwdbuf, name, ntohl(db_cnid));
770 ad_init(&ad, &volume);
771 if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
772 dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
773 cwdbuf, name, strerror(errno));
776 ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
778 ad_close(&ad, ADFLAGS_HF);
787 This is called recursively for all dirs.
788 volroot=1 means we're in the volume root dir, 0 means we aren't.
789 We use this when checking for netatalk private folders like .AppleDB.
790 did is our parents CNID.
792 static int dbd_readdir(int volroot, cnid_t did)
794 int cwd, ret = 0, adfile_ok, addir_ok, encoding_ok;
799 static struct stat st; /* Save some stack space */
801 /* Check again for .AppleDouble folder, check_adfile also checks/creates it */
802 if ((addir_ok = check_addir(volroot)) != 0)
803 if ( ! (dbd_flags & DBD_FLAGS_SCAN))
804 /* Fatal on rebuild run, continue if only scanning ! */
807 /* Check AppleDouble files in AppleDouble folder, but only if it exists or could be created */
808 if (volume.v_adouble == AD_VERSION2 && ADDIR_OK)
809 if ((read_addir()) != 0)
810 if ( ! (dbd_flags & DBD_FLAGS_SCAN))
811 /* Fatal on rebuild run, continue if only scanning ! */
814 if ((dp = opendir (".")) == NULL) {
815 dbd_log(LOGSTD, "Couldn't open the directory: %s",strerror(errno));
819 while ((ep = readdir (dp))) {
820 /* Check if we got a termination signal */
822 longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
824 /* Check if its "." or ".." */
825 if (DIR_DOT_OR_DOTDOT(ep->d_name))
828 /* Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop" */
829 if ((name = check_netatalk_dirs(ep->d_name)) != NULL) {
831 dbd_log(LOGSTD, "Nested %s in %s", name, cwdbuf);
835 /* Check for special folders in volume root e.g. ".zfs" */
837 if ((name = check_special_dirs(ep->d_name)) != NULL) {
838 dbd_log(LOGSTD, "Ignoring special dir \"%s\"", name);
843 /* Skip .AppleDouble dir in this loop */
844 if (STRCMP(ep->d_name, == , ADv2_DIRNAME))
847 if ((ret = lstat(ep->d_name, &st)) < 0) {
848 dbd_log( LOGSTD, "Lost file while reading dir '%s/%s', probably removed: %s",
849 cwdbuf, ep->d_name, strerror(errno));
853 switch (st.st_mode & S_IFMT) {
858 dbd_log(LOGDEBUG, "Ignoring symlink %s/%s", cwdbuf, ep->d_name);
861 dbd_log(LOGSTD, "Bad filetype: %s/%s", cwdbuf, ep->d_name);
862 if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
863 if ((unlink(ep->d_name)) != 0) {
864 dbd_log(LOGSTD, "Error removing: %s/%s: %s", cwdbuf, ep->d_name, strerror(errno));
870 /**************************************************************************
872 **************************************************************************/
873 static unsigned long long statcount = 0;
880 if ((statcount % 10000) == 0) {
881 if (dbd_flags & DBD_FLAGS_STATS)
882 dbd_log(LOGSTD, "Scanned: %10llu, time: %10llu s",
883 statcount, (unsigned long long)(time(NULL) - t));
886 /**************************************************************************
888 **************************************************************************/
891 if ( -1 == (encoding_ok = check_name_encoding(ep->d_name)) ) {
892 /* If its a file: skipp all other tests now ! */
893 /* For dirs we could try to get a CNID for it and recurse, but currently I prefer not to */
897 /* Check for appledouble file, create if missing, but only if we have addir */
900 adfile_ok = check_adfile(ep->d_name, &st);
904 cnid = check_cnid(ep->d_name, did, &st, adfile_ok);
906 /* Now add this object to our rebuild dbd */
907 if (cnid && dbd_rebuild) {
908 static uint count = 0;
909 rqst.cnid = rply.cnid;
910 ret = dbd_rebuild_add(dbd_rebuild, &rqst, &rply);
911 if (dbif_txn_close(dbd_rebuild, ret) != 0)
913 if (rply.result != CNID_DBD_RES_OK) {
914 dbd_log( LOGSTD, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
915 cnid, cwdbuf, ep->d_name);
919 if (count == 10000) {
920 if (dbif_txn_checkpoint(dbd_rebuild, 0, 0, 0) < 0) {
921 dbd_log(LOGSTD, "Error checkpointing!");
930 if (myvolinfo->v_vfs_ea == AFPVOL_EA_AD)
931 check_eafiles(ep->d_name);
933 /**************************************************************************
935 **************************************************************************/
936 if (S_ISDIR(st.st_mode) && (cnid || nocniddb)) { /* If we have no cnid for it we cant recur */
938 strcat(cwdbuf, ep->d_name);
939 dbd_log( LOGDEBUG, "Entering directory: %s", cwdbuf);
940 if (-1 == (cwd = open(".", O_RDONLY))) {
941 dbd_log( LOGSTD, "Cant open directory '%s': %s", cwdbuf, strerror(errno));
944 if (0 != chdir(ep->d_name)) {
945 dbd_log( LOGSTD, "Cant chdir to directory '%s': %s", cwdbuf, strerror(errno));
950 ret = dbd_readdir(0, cnid);
954 *(strrchr(cwdbuf, '/')) = 0;
961 Use results of previous checks
968 static int scanvol(struct volinfo *vi, dbd_flags_t flags)
970 /* Make this stuff accessible from all funcs easily */
974 /* Init a fake struct vol so that we can call ad_init(.., &volume) and initvol_vfs(&volume) */
975 volume.v_adouble = vi->v_adouble;
976 volume.v_ad_options = vi->v_flags;
977 volume.ad_path = vi->ad_path;
978 volume.v_vfs_ea = myvolinfo->v_vfs_ea;
979 initvol_vfs(&volume);
981 /* Run with umask 0 */
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);
990 /* Start recursion */
991 if (dbd_readdir(1, htonl(2)) < 0) /* 2 = volumeroot CNID */
998 Remove all CNIDs from dbd that are not in dbd_rebuild
1000 static void delete_orphaned_cnids(DBD *dbd, DBD *dbd_rebuild, dbd_flags_t flags)
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;
1007 /* jump over rootinfo key */
1008 if ( dbif_idwalk(dbd, &dbd_cnid, 0) != 1)
1010 if ( dbif_idwalk(dbd_rebuild, &rebuild_cnid, 0) != 1)
1013 /* Get first id from dbd_rebuild */
1014 if ((dbif_idwalk(dbd_rebuild, &rebuild_cnid, 0)) == -1)
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 */
1021 longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
1023 if (deleted > 1000) {
1025 if (dbif_txn_checkpoint(dbd, 0, 0, 0) < 0) {
1026 dbd_log(LOGSTD, "Error checkpointing!");
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) {
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);
1049 if (dbif_txn_close(dbd, ret) != 0)
1053 /* Check if we got a termination signal */
1055 longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
1059 /* Normal case (ret=1): continue while loop */
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);
1073 if (dbif_txn_close(dbd, ret) != 0)
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!");
1090 } /* while ((dbif_idwalk(dbd, &dbd_cnid, 0)) == 1) */
1093 dbif_idwalk(dbd, NULL, 1); /* Close cursor */
1094 dbif_idwalk(dbd_rebuild, NULL, 1); /* Close cursor */
1098 static const char *get_tmpdb_path(void)
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)
1109 Main func called from cmd_dbd.c
1111 int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *vi, dbd_flags_t flags)
1114 struct db_param db_param = { 0 };
1115 const char *tmpdb_path = NULL;
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;
1123 /* Make it accessible for all funcs */
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);
1132 /* Get volume stamp */
1133 dbd_getstamp(dbd, &rqst, &rply);
1134 if (rply.result != CNID_DBD_RES_OK) {
1138 memcpy(stamp, rply.name, CNID_DEV_LEN);
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"))) {
1148 if (dbif_env_open(dbd_rebuild,
1150 DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN) < 0) {
1151 dbd_log(LOGSTD, "error opening tmp database!");
1155 if (0 != (dbif_open(dbd_rebuild, NULL, 0))) {
1160 if (0 != (dbif_copy_rootinfokey(dbd, dbd_rebuild))) {
1166 if (setjmp(jmp) != 0) {
1167 ret = 0; /* Got signal, jump from dbd_readdir */
1172 if ( (scanvol(vi, flags)) != 0) {
1179 if (dbif_txn_close(dbd, ret == 0 ? 1 : 0) != 0)
1182 if (dbif_txn_close(dbd_rebuild, ret == 0 ? 1 : 0) != 0)
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);
1191 dbd_log(LOGDEBUG, "Closing tmp db");
1192 dbif_close(dbd_rebuild);
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);
1199 snprintf(cmd, 8 + MAXPATHLEN, "rmdir %s", tmpdb_path);