/*
- $Id: cmd_dbd_scanvol.c,v 1.3 2009-05-20 10:56:02 franklahm Exp $
+ $Id: cmd_dbd_scanvol.c,v 1.13 2009-12-09 15:25:28 franklahm Exp $
Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
GNU General Public License for more details.
*/
-/*
- dbd specs and implementation progress
- =====================================
-
- St := Status
-
- Force option
- ------------
-
- St Spec
- -- ----
- -- If -f is requested, ensure -e is too.
- Check if volumes is using AFPVOL_CACHE, then wipe db from disk.
-
- 1st pass: Scan volume
- --------------------
-
- St Type Check
- -- ---- -----
- OK F/D Make sure ad file exists
- OK D Make sure .AppleDouble dir exist, create if missing. Error creating
- it is fatal as that shouldn't happen as root.
- OK F/D Delete orphaned ad-files, log dirs in ad-dir
- OK F/D Check name encoding by roundtripping, log on error
- OK F/D try: read CNID from ad file (if cnid caching is on)
- try: fetch CNID from database
- -> on mismatch: use CNID from file, update database (deleting both found CNIDs first)
- -> if no CNID in ad file: write CNID from database to ad file
- -> if no CNID in database: add CNID from ad file to database
- -> on no CNID at all: create one and store in both places
- OK F/D Add found CNID, DID, filename, dev/inode, stamp to rebuild database
- OK F/D Check/update stamp (implicitly done while checking CNIDs)
-
- 2nd pass: Delete unused CNIDs
- -----------------------------
-
- St Spec
- -- ----
- -- Step through dbd (the one on disk) and rebuild-db from pass 1 and delete any CNID from
- dbd not in rebuild db.
-*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
return -1;
}
/* Missing. Log and create it */
- dbd_log(LOGSTD, "Missing AppleDoube file '%s/%s'", cwdbuf, adname);
+ dbd_log(LOGSTD, "Missing AppleDouble file '%s/%s'", cwdbuf, adname);
if (dbd_flags & DBD_FLAGS_SCAN)
/* Scan only requested, dont change anything */
if ((ret = ad_open_metadata( fname, adflags, O_CREAT, &ad)) != 0) {
dbd_log( LOGSTD, "Error creating AppleDouble file '%s/%s': %s",
cwdbuf, adname, strerror(errno));
+
return -1;
}
#if 0
chmod(adname, st->st_mode);
#endif
+ } else {
+ ad_init(&ad, volinfo->v_adouble, volinfo->v_ad_options);
+ if (ad_open_metadata( fname, adflags, O_RDONLY, &ad) != 0) {
+ dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s'", cwdbuf, fname);
+ return -1;
+ }
+ ad_close_metadata(&ad);
}
return 0;
}
int addir_ok, adpar_ok;
struct stat st;
struct adouble ad;
-
+ char *mname;
+
/* Check for ad-dir */
if ( (addir_ok = access(ADv2_DIRNAME, F_OK)) != 0) {
if (errno != ENOENT) {
}
/* Get basename of cwd from cwdbuf */
- char *mname = utompath(strrchr(cwdbuf, '/') + 1);
+ utompath(strrchr(cwdbuf, '/') + 1);
/* Update name in ad file */
ad_setname(&ad, mname);
dbd_log(LOGSTD, "Couldn't chdir back to '%s' from AppleDouble dir: %s",
cwdbuf, strerror(errno));
/* This really is EOT! */
- exit(1);
+ longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
}
closedir(dp);
dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s': %s", cwdbuf, name, strerror(errno));
return 0;
}
- ad_cnid = ad_getid(&ad, st->st_dev, st->st_ino, did, stamp);
+
+ if (dbd_flags & DBD_FLAGS_FORCE) {
+ ad_cnid = ad_forcegetid(&ad);
+ /* This ensures the changed stamp is written */
+ ad_setid( &ad, st->st_dev, st->st_ino, ad_cnid, did, stamp);
+ ad_flush(&ad);
+ }
+ else
+ ad_cnid = ad_getid(&ad, st->st_dev, st->st_ino, did, stamp);
+
if (ad_cnid == 0)
dbd_log( LOGSTD, "Incorrect CNID data in .AppleDouble data for '%s/%s' (bad stamp?)", cwdbuf, name);
+
ad_close_metadata(&ad);
}
memset(&rqst, 0, sizeof(struct cnid_dbd_rqst));
memset(&rply, 0, sizeof(struct cnid_dbd_rply));
rqst.did = did;
+ rqst.cnid = ad_cnid;
if ( ! (volinfo->v_flags & AFPVOL_NODEV))
rqst.dev = st->st_dev;
rqst.ino = st->st_ino;
rqst.namelen = strlen(name);
/* Query the database */
- ret = cmd_dbd_lookup(dbd, &rqst, &rply, (dbd_flags & DBD_FLAGS_SCAN) ? 1 : 0);
+ ret = dbd_lookup(dbd, &rqst, &rply, (dbd_flags & DBD_FLAGS_SCAN) ? 1 : 0);
dbif_txn_close(dbd, ret);
if (rply.result == CNID_DBD_RES_OK) {
db_cnid = rply.cnid;
} else if (rply.result == CNID_DBD_RES_NOTFOUND) {
- dbd_log( LOGSTD, "No CNID for '%s/%s' in database", cwdbuf, name);
+ if ( ! (dbd_flags & DBD_FLAGS_FORCE))
+ dbd_log( LOGSTD, "No CNID for '%s/%s' in database", cwdbuf, name);
db_cnid = 0;
} else {
dbd_log( LOGSTD, "Fatal error resolving '%s/%s'", cwdbuf, name);
dbd_log( LOGSTD, "CNID mismatch for '%s/%s', db: %u, ad-file: %u", cwdbuf, name, ntohl(db_cnid), ntohl(ad_cnid));
if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
rqst.cnid = db_cnid;
- ret = dbd_delete(dbd, &rqst, &rply);
+ ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
dbif_txn_close(dbd, ret);
rqst.cnid = ad_cnid;
- ret = dbd_delete(dbd, &rqst, &rply);
+ ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
dbif_txn_close(dbd, ret);
ret = dbd_rebuild_add(dbd, &rqst, &rply);
} else if (ad_cnid && (db_cnid == 0)) {
/* in ad-file but not in db */
if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
- dbd_log( LOGSTD, "CNID rebuild add for '%s/%s', adding with CNID from ad-file: %u", cwdbuf, name, ntohl(ad_cnid));
+ dbd_log( LOGDEBUG, "CNID rebuild add for '%s/%s', adding with CNID from ad-file: %u", cwdbuf, name, ntohl(ad_cnid));
rqst.cnid = ad_cnid;
- ret = dbd_delete(dbd, &rqst, &rply);
+ ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
dbif_txn_close(dbd, ret);
ret = dbd_rebuild_add(dbd, &rqst, &rply);
dbif_txn_close(dbd, ret);
/* Note: the next test will use this new CNID too! */
if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
/* add to db */
- ret = cmd_dbd_add(dbd, &rqst, &rply);
+ ret = dbd_add(dbd, &rqst, &rply, 1);
dbif_txn_close(dbd, ret);
db_cnid = rply.cnid;
dbd_log( LOGSTD, "New CNID for '%s/%s': %u", cwdbuf, name, ntohl(db_cnid));
if (ADDIR_OK)
adfile_ok = check_adfile(ep->d_name, &st);
- /* Check CNIDs */
- cnid = check_cnid(ep->d_name, did, &st, adfile_ok, adflags);
-
- /* Now add this object to our rebuild dbd */
- if (cnid) {
- rqst.cnid = rply.cnid;
- dbd_rebuild_add(dbd_rebuild, &rqst, &rply);
- if (rply.result != CNID_DBD_RES_OK) {
- dbd_log( LOGDEBUG, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
- cnid, cwdbuf, ep->d_name);
- exit(EXIT_FAILURE);
+ if ( ! nocniddb) {
+ /* Check CNIDs */
+ cnid = check_cnid(ep->d_name, did, &st, adfile_ok, adflags);
+
+ /* Now add this object to our rebuild dbd */
+ if (cnid) {
+ rqst.cnid = rply.cnid;
+ dbd_rebuild_add(dbd_rebuild, &rqst, &rply);
+ if (rply.result != CNID_DBD_RES_OK) {
+ dbd_log( LOGDEBUG, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
+ cnid, cwdbuf, ep->d_name);
+ longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
+ }
}
}
/**************************************************************************
Recursion
**************************************************************************/
- if (S_ISDIR(st.st_mode) && cnid) { /* If we have no cnid for it we cant recur */
+ if (S_ISDIR(st.st_mode) && (cnid || nocniddb)) { /* If we have no cnid for it we cant recur */
strcat(cwdbuf, "/");
strcat(cwdbuf, ep->d_name);
dbd_log( LOGDEBUG, "Entering directory: %s", cwdbuf);
return 0;
}
+/*
+ Remove all CNIDs from dbd that are not in dbd_rebuild
+*/
+static void delete_orphaned_cnids(DBD *dbd, DBD *dbd_rebuild, dbd_flags_t flags)
+{
+ int ret, deleted = 0;
+ cnid_t dbd_cnid = 0, rebuild_cnid = 0;
+ struct cnid_dbd_rqst rqst;
+ struct cnid_dbd_rply rply;
+
+ /* jump over rootinfo key */
+ if ( dbif_idwalk(dbd, &dbd_cnid, 0) != 1)
+ return;
+ if ( dbif_idwalk(dbd_rebuild, &rebuild_cnid, 0) != 1)
+ return;
+
+ /* Get first id from dbd_rebuild */
+ if ((dbif_idwalk(dbd_rebuild, &rebuild_cnid, 0)) == -1)
+ return;
+
+ /* Start main loop through dbd: get CNID from dbd */
+ while ((dbif_idwalk(dbd, &dbd_cnid, 0)) == 1) {
+
+ if (deleted > 50) {
+ deleted = 0;
+ if (dbif_txn_checkpoint(dbd, 0, 0, 0) < 0) {
+ dbd_log(LOGSTD, "Error checkpointing!");
+ goto cleanup;
+ }
+ }
+
+ /* This should be the normal case: CNID is in both dbs */
+ if (dbd_cnid == rebuild_cnid) {
+ /* Get next CNID from rebuild db */
+ if ((ret = dbif_idwalk(dbd_rebuild, &rebuild_cnid, 0)) == -1) {
+ /* Some error */
+ goto cleanup;
+ } else if (ret == 0) {
+ /* end of rebuild_cnid, delete all remaining CNIDs from dbd */
+ while ((dbif_idwalk(dbd, &dbd_cnid, 0)) == 1) {
+ dbd_log(LOGSTD, "Orphaned CNID in database: %u", dbd_cnid);
+ if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
+ rqst.cnid = htonl(dbd_cnid);
+ ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
+ dbif_txn_close(dbd, ret);
+ deleted++;
+ }
+ }
+ return;
+ } else
+ /* Normal case (ret=1): continue while loop */
+ continue;
+ }
+
+ if (dbd_cnid < rebuild_cnid) {
+ /* CNID is orphaned -> delete */
+ dbd_log(LOGSTD, "Orphaned CNID in database: %u.", dbd_cnid);
+ if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
+ rqst.cnid = htonl(dbd_cnid);
+ ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
+ dbif_txn_close(dbd, ret);
+ deleted++;
+ }
+ continue;
+ }
+
+ if (dbd_cnid > rebuild_cnid) {
+ dbd_log(LOGSTD, "Ghost CNID: %u. This is fatal! Dumping rebuild db:\n", rebuild_cnid);
+ dbif_dump(dbd_rebuild, 0);
+ dbd_log(LOGSTD, "Send this dump and a `dbd -d ...` dump to the Netatalk Dev team!");
+ dbif_txn_close(dbd, ret);
+ dbif_idwalk(dbd, NULL, 1); /* Close cursor */
+ dbif_idwalk(dbd_rebuild, NULL, 1); /* Close cursor */
+ goto cleanup;
+ }
+ }
+
+cleanup:
+ dbif_idwalk(dbd, NULL, 1); /* Close cursor */
+ dbif_idwalk(dbd_rebuild, NULL, 1); /* Close cursor */
+ return;
+}
+
+/*
+ Main func called from cmd_dbd.c
+*/
int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *volinfo, dbd_flags_t flags)
{
int ret = 0;
return -1;
}
- /* Get volume stamp */
- dbd_getstamp(dbd, &rqst, &rply);
- if (rply.result != CNID_DBD_RES_OK)
- goto exit_cleanup;
- memcpy(stamp, rply.name, CNID_DEV_LEN);
+ if (! nocniddb) {
+ /* Get volume stamp */
+ dbd_getstamp(dbd, &rqst, &rply);
+ if (rply.result != CNID_DBD_RES_OK)
+ goto exit_cleanup;
+ memcpy(stamp, rply.name, CNID_DEV_LEN);
- /* open/create rebuild dbd, copy rootinfo key */
- if (NULL == (dbd_rebuild = dbif_init(NULL, NULL)))
- return -1;
- if (0 != (dbif_open(dbd_rebuild, NULL, 0)))
- return -1;
- if (0 != (dbif_copy_rootinfokey(dbd, dbd_rebuild)))
- goto exit_cleanup;
-
-// dbif_dump(dbd_rebuild, 0);
+ /* open/create rebuild dbd, copy rootinfo key */
+ if (NULL == (dbd_rebuild = dbif_init(NULL, NULL)))
+ return -1;
+ if (0 != (dbif_open(dbd_rebuild, NULL, 0)))
+ return -1;
+ if (0 != (dbif_copy_rootinfokey(dbd, dbd_rebuild)))
+ goto exit_cleanup;
+ }
if (setjmp(jmp) != 0)
goto exit_cleanup; /* Got signal, jump from dbd_readdir */
if ( (scanvol(volinfo, flags)) != 0)
return -1;
-// dbif_dump(dbd_rebuild, 0);
+ if (! nocniddb) {
+ /* We can only do this in exclusive mode, otherwise we might delete CNIDs added from
+ other clients in between our pass 1 and 2 */
+ if (flags & DBD_FLAGS_EXCL)
+ delete_orphaned_cnids(dbd, dbd_rebuild, flags);
+ }
exit_cleanup:
- dbif_close(dbd_rebuild);
+ if (! nocniddb)
+ dbif_close(dbd_rebuild);
return ret;
}