#include <atalk/adouble.h>
#include <atalk/unicode.h>
#include <atalk/netatalk_conf.h>
-//#include <atalk/cnid_dbd_private.h>
#include <atalk/volume.h>
#include <atalk/ea.h>
#include <atalk/util.h>
#include <atalk/acl.h>
#include <atalk/compat.h>
#include <atalk/cnid.h>
+#include <atalk/errchk.h>
#include "cmd_dbd.h"
#include "dbif.h"
#define ADFILE_OK (adfile_ok == 0)
-static struct vol *myvol;
static char cwdbuf[MAXPATHLEN+1];
static struct vol *vol;
-static DBD *dbd_rebuild;
static dbd_flags_t dbd_flags;
static char stamp[CNID_DEV_LEN];
static char *netatalk_dirs[] = {
static struct cnid_dbd_rply rply;
static jmp_buf jmp;
static char pname[MAXPATHLEN] = "../";
-static char cnidResBuf[12 + MAXPATHLEN + 1];
/*
Taken form afpd/desktop.c
u = upath;
outlen = strlen(upath);
- if ((myvol->v_casefold & AFPVOL_UTOMUPPER))
+ if ((vol->v_casefold & AFPVOL_UTOMUPPER))
flags |= CONV_TOUPPER;
- else if ((myvol->v_casefold & AFPVOL_UTOMLOWER))
+ else if ((vol->v_casefold & AFPVOL_UTOMLOWER))
flags |= CONV_TOLOWER;
- if ((myvol->v_flags & AFPVOL_EILSEQ)) {
+ if ((vol->v_flags & AFPVOL_EILSEQ)) {
flags |= CONV__EILSEQ;
}
/* convert charsets */
- if ((size_t)-1 == ( outlen = convert_charset(myvol->v_volcharset,
+ if ((size_t)-1 == ( outlen = convert_charset(vol->v_volcharset,
CH_UTF8_MAC,
- myvol->v_maccharset,
+ vol->v_maccharset,
u, outlen, mpath, MAXPATHLEN, &flags)) ) {
dbd_log( LOGSTD, "Conversion from %s to %s for %s failed.",
- myvol->v_volcodepage, myvol->v_maccodepage, u);
+ vol->v_volcodepage, vol->v_maccodepage, u);
return NULL;
}
return(m);
}
-/*
- Taken form afpd/desktop.c
-*/
-static char *mtoupath(char *mpath)
-{
- static char upath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
- char *m, *u;
- size_t inplen;
- size_t outlen;
- u_int16_t flags = 0;
-
- if (!mpath)
- return NULL;
-
- if ( *mpath == '\0' ) {
- return( "." );
- }
-
- /* set conversion flags */
- if ((myvol->v_casefold & AFPVOL_MTOUUPPER))
- flags |= CONV_TOUPPER;
- else if ((myvol->v_casefold & AFPVOL_MTOULOWER))
- flags |= CONV_TOLOWER;
-
- if ((myvol->v_flags & AFPVOL_EILSEQ)) {
- flags |= CONV__EILSEQ;
- }
-
- m = mpath;
- u = upath;
-
- inplen = strlen(m);
- outlen = MAXPATHLEN;
-
- if ((size_t)-1 == (outlen = convert_charset(CH_UTF8_MAC,
- myvol->v_volcharset,
- myvol->v_maccharset,
- m, inplen, u, outlen, &flags)) ) {
- dbd_log( LOGSTD, "conversion from UTF8-MAC to %s for %s failed.",
- myvol->v_volcodepage, mpath);
- return NULL;
- }
-
- return( upath );
-}
-
/*
Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
Returns pointer to name or NULL.
*/
static int update_cnid(cnid_t did, const struct stat *sp, const char *oldname, const char *newname)
{
- int ret;
cnid_t id;
/* Query the database */
*newname = NULL;
- if (myvol->v_adouble == AD_VERSION_EA) {
+ if (vol->v_adouble == AD_VERSION_EA) {
if (!(dbd_flags & DBD_FLAGS_V2TOEA))
return 0;
- if (ad_convert(fname, st, myvol, newname) != 0) {
+ if (ad_convert(fname, st, vol, newname) != 0) {
switch (errno) {
case ENOENT:
break;
if (S_ISDIR(st->st_mode))
adflags |= ADFLAGS_DIR;
- adname = myvol->ad_path(fname, adflags);
+ adname = vol->ad_path(fname, adflags);
if ((ret = access( adname, F_OK)) != 0) {
if (errno != ENOENT) {
return -1;
/* Create ad file */
- ad_init(&ad, myvol);
+ ad_init(&ad, vol);
if ((ret = ad_open(&ad, fname, adflags | ADFLAGS_CREATE | ADFLAGS_RDWR, 0666)) != 0) {
dbd_log( LOGSTD, "Error creating AppleDouble file '%s/%s': %s",
chmod(adname, st->st_mode);
#endif
} else {
- ad_init(&ad, myvol);
+ ad_init(&ad, vol);
if (ad_open(&ad, fname, adflags | ADFLAGS_RDONLY) != 0) {
dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s'", cwdbuf, fname);
return -1;
struct stat st;
char *eaname;
- if ((ret = ea_open(myvol, fname, EA_RDWR, &ea)) != 0) {
+ if ((ret = ea_open(vol, fname, EA_RDWR, &ea)) != 0) {
if (errno == ENOENT)
return 0;
dbd_log(LOGSTD, "Error calling ea_open for file: %s/%s, removing EA files", cwdbuf, fname);
struct adouble ad;
char *mname = NULL;
- if (myvol->v_adouble == AD_VERSION_EA)
+ if (vol->v_adouble == AD_VERSION_EA)
return 0;
/* Check for ad-dir */
}
/* Check for ".Parent" */
- if ( (adpar_ok = access(myvol->ad_path(".", ADFLAGS_DIR), F_OK)) != 0) {
+ if ( (adpar_ok = access(vol->ad_path(".", ADFLAGS_DIR), F_OK)) != 0) {
if (errno != ENOENT) {
dbd_log(LOGSTD, "Access error on '%s/%s': %s",
- cwdbuf, myvol->ad_path(".", ADFLAGS_DIR), strerror(errno));
+ cwdbuf, vol->ad_path(".", ADFLAGS_DIR), strerror(errno));
return -1;
}
dbd_log(LOGSTD, "Missing .AppleDouble/.Parent for '%s'", cwdbuf);
}
/* Create ad dir and set name */
- ad_init(&ad, myvol);
+ ad_init(&ad, vol);
if (ad_open(&ad, ".", ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_CREATE | ADFLAGS_RDWR, 0777) != 0) {
dbd_log( LOGSTD, "Error creating AppleDouble dir in %s: %s", cwdbuf, strerror(errno));
return -1;
}
chown(ADv2_DIRNAME, st.st_uid, st.st_gid);
- chown(myvol->ad_path(".", ADFLAGS_DIR), st.st_uid, st.st_gid);
+ chown(vol->ad_path(".", ADFLAGS_DIR), st.st_uid, st.st_gid);
}
return 0;
char *namep, *namedup = NULL;
/* Check if this is an AFPVOL_EA_AD vol */
- if (myvol->v_vfs_ea == AFPVOL_EA_AD) {
+ if (vol->v_vfs_ea == AFPVOL_EA_AD) {
/* Does the filename contain "::EA" ? */
namedup = strdup(name);
if ((namep = strstr(namedup, "::EA")) == NULL) {
struct stat st;
if ((chdir(ADv2_DIRNAME)) != 0) {
- if (myvol->v_adouble == AD_VERSION_EA) {
+ if (vol->v_adouble == AD_VERSION_EA) {
return 0;
}
dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
*/
static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfile_ok)
{
- int ret, adflags = ADFLAGS_HF;
- cnid_t db_cnid, ad_cnid, tmpid;
+ int adflags = ADFLAGS_HF;
+ cnid_t db_cnid, ad_cnid;
struct adouble ad;
adflags = ADFLAGS_HF | (S_ISDIR(st->st_mode) ? ADFLAGS_DIR : 0);
/* Get CNID from ad-file */
ad_cnid = CNID_INVALID;
if (ADFILE_OK) {
- ad_init(&ad, myvol);
+ ad_init(&ad, vol);
if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
- if (myvol->v_adouble != AD_VERSION_EA) {
+ if (vol->v_adouble != AD_VERSION_EA) {
dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s': %s", cwdbuf, name, strerror(errno));
return CNID_INVALID;
}
/* Mismatch, overwrite ad file with value from db */
dbd_log(LOGSTD, "CNID mismatch for '%s/%s', CNID db: %u, ad-file: %u",
cwdbuf, name, ntohl(db_cnid), ntohl(ad_cnid));
- ad_init(&ad, myvol);
+ ad_init(&ad, vol);
if (ad_open(&ad, name, adflags | ADFLAGS_HF | ADFLAGS_RDWR) != 0) {
dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
cwdbuf, name, strerror(errno));
return db_cnid;
}
+static int check_orphaned(const char *name)
+{
+ int rc;
+ struct stat sb;
+
+ if (strlen(name) < 3)
+ return;
+
+ rc = lstat(&name[2], &sb);
+
+ if (rc != 0 && errno == ENOENT) {
+ dbd_log(LOGSTD, "Removing orphaned AppleDouble \"%s/%s\"", cwdbuf, name);
+ unlink(name);
+ return 1;
+ }
+ return 0;
+}
+
/*
This is called recursively for all dirs.
volroot=1 means we're in the volume root dir, 0 means we aren't.
*/
static int dbd_readdir(int volroot, cnid_t did)
{
- int cwd, ret = 0, adfile_ok, addir_ok, encoding_ok;
+ int cwd, ret = 0, adfile_ok, addir_ok;
cnid_t cnid = 0;
const char *name;
DIR *dp;
if (STRCMP(ep->d_name, == , ADv2_DIRNAME))
continue;
- if (!myvol->vfs->vfs_validupath(myvol, ep->d_name)) {
+ if (!vol->vfs->vfs_validupath(vol, ep->d_name)) {
dbd_log(LOGDEBUG, "Ignoring \"%s\"", ep->d_name);
continue;
}
switch (st.st_mode & S_IFMT) {
case S_IFREG:
case S_IFDIR:
- break;
case S_IFLNK:
- dbd_log(LOGDEBUG, "Ignoring symlink %s/%s", cwdbuf, ep->d_name);
- continue;
+ break;
default:
dbd_log(LOGSTD, "Bad filetype: %s/%s", cwdbuf, ep->d_name);
if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
Tests
**************************************************************************/
+ /* Check for invalid names and orphaned ._ files */
+ if (S_ISREG(st.st_mode) && (strncmp(ep->d_name, "._", strlen("._")) == 0)) {
+ if (check_orphaned(ep->d_name))
+ continue;
+ if (vol->vfs->vfs_validupath(vol, ep->d_name)) {
+ dbd_log(LOGSTD, "Bad AppleDouble \"%s/%s\"", cwdbuf, ep->d_name);
+ continue;
+ }
+ }
+
/* Check for appledouble file, create if missing, but only if we have addir */
const char *name = NULL;
adfile_ok = -1;
if (ADDIR_OK)
adfile_ok = check_adfile(ep->d_name, &st, &name);
- if (name == NULL) {
- name = ep->d_name;
- } else {
- update_cnid(did, &st, ep->d_name, name);
- }
+ if (!S_ISLNK(st.st_mode)) {
+ if (name == NULL) {
+ name = ep->d_name;
+ } else {
+ update_cnid(did, &st, ep->d_name, name);
+ }
- /* Check CNIDs */
- cnid = check_cnid(name, did, &st, adfile_ok);
+ /* Check CNIDs */
+ cnid = check_cnid(name, did, &st, adfile_ok);
- /* Now add this object to our rebuild dbd */
- if (cnid && dbd_rebuild) {
- static uint count = 0;
- rqst.cnid = rply.cnid;
- ret = dbd_rebuild_add(dbd_rebuild, &rqst, &rply);
- if (dbif_txn_close(dbd_rebuild, ret) != 0)
- return -1;
- if (rply.result != CNID_DBD_RES_OK) {
- dbd_log( LOGSTD, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
- cnid, cwdbuf, name);
- return -1;
- }
- count++;
- if (count == 10000) {
- if (dbif_txn_checkpoint(dbd_rebuild, 0, 0, 0) < 0) {
- dbd_log(LOGSTD, "Error checkpointing!");
- return -1;
- }
- count = 0;
- }
+ /* Check EA files */
+ if (vol->v_vfs_ea == AFPVOL_EA_AD)
+ check_eafiles(name);
}
- /* Check EA files */
- if (myvol->v_vfs_ea == AFPVOL_EA_AD)
- check_eafiles(name);
-
/**************************************************************************
Recursion
**************************************************************************/
/*
Use results of previous checks
*/
- if ((myvol->v_adouble == AD_VERSION_EA) && (dbd_flags & DBD_FLAGS_V2TOEA)) {
+ if ((vol->v_adouble == AD_VERSION_EA) && (dbd_flags & DBD_FLAGS_V2TOEA)) {
if (rmdir(ADv2_DIRNAME) != 0) {
switch (errno) {
case ENOENT:
return ret;
}
-static int scanvol(struct vol *vol, dbd_flags_t flags)
-{
- struct stat st;
-
- /* Make this stuff accessible from all funcs easily */
- myvol = vol;
- dbd_flags = flags;
-
- /* Run with umask 0 */
- umask(0);
-
- strcpy(cwdbuf, myvol->v_path);
- chdir(myvol->v_path);
-
- if ((myvol->v_adouble == AD_VERSION_EA) && (dbd_flags & DBD_FLAGS_V2TOEA)) {
- if (lstat(".", &st) != 0)
- return -1;
- if (ad_convert(".", &st, vol, NULL) != 0) {
- switch (errno) {
- case ENOENT:
- break;
- default:
- dbd_log(LOGSTD, "Conversion error for \"%s\": %s", myvol->v_path, strerror(errno));
- break;
- }
- }
- }
-
- /* Start recursion */
- if (dbd_readdir(1, htonl(2)) < 0) /* 2 = volumeroot CNID */
- return -1;
-
- return 0;
-}
-
/*
Main func called from cmd_dbd.c
*/
int cmd_dbd_scanvol(struct vol *vol_in, dbd_flags_t flags)
{
- int ret = 0;
+ EC_INIT;
+ struct stat st;
+
+ /* Run with umask 0 */
+ umask(0);
/* Make vol accessible for all funcs */
vol = vol_in;
+ dbd_flags = flags;
/* We only support unicode volumes ! */
if (vol->v_volcharset != CH_UTF8) {
dbd_log(LOGSTD, "Not a Unicode volume: %s, %u != %u", vol->v_volcodepage, vol->v_volcharset, CH_UTF8);
- return -1;
+ EC_FAIL;
}
/*
cnid_getstamp(vol->v_cdb, stamp, sizeof(stamp));
if (setjmp(jmp) != 0) {
- ret = 0; /* Got signal, jump from dbd_readdir */
- goto exit;
+ EC_EXIT_STATUS(0); /* Got signal, jump from dbd_readdir */
}
- /* scanvol */
- if ((scanvol(vol, flags)) != 0) {
- ret = -1;
- goto exit;
+ strcpy(cwdbuf, vol->v_path);
+ chdir(vol->v_path);
+
+ if ((vol->v_adouble == AD_VERSION_EA) && (dbd_flags & DBD_FLAGS_V2TOEA)) {
+ if (lstat(".", &st) != 0)
+ EC_FAIL;
+ if (ad_convert(".", &st, vol, NULL) != 0) {
+ switch (errno) {
+ case ENOENT:
+ break;
+ default:
+ dbd_log(LOGSTD, "Conversion error for \"%s\": %s", vol->v_path, strerror(errno));
+ break;
+ }
+ }
}
-exit:
- return ret;
+ /* Start recursion */
+ EC_NEG1( dbd_readdir(1, htonl(2)) ); /* 2 = volumeroot CNID */
+
+EC_CLEANUP:
+ EC_EXIT;
}