/*
- * $Id: enumerate.c,v 1.15 2002-01-24 16:32:12 jmarcus Exp $
- *
* Copyright (c) 1990,1993 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <dirent.h>
#include <errno.h>
-
-#include <atalk/logger.h>
-#include <sys/types.h>
-#include <sys/stat.h>
#include <sys/file.h>
#include <sys/param.h>
-#include <netatalk/endian.h>
+#include <atalk/logger.h>
#include <atalk/afp.h>
#include <atalk/adouble.h>
-#ifdef CNID_DB
+#include <atalk/vfs.h>
#include <atalk/cnid.h>
-#endif /* CNID_DB */
+#include <atalk/util.h>
+#include <atalk/bstrlib.h>
+#include <atalk/bstradd.h>
+
#include "desktop.h"
#include "directory.h"
+#include "dircache.h"
#include "volume.h"
#include "globals.h"
#include "file.h"
+#include "fork.h"
#include "filedir.h"
-/* check for mtab DID code */
-#ifdef DID_MTAB
-#include "parse_mtab.h"
-#endif /* DID_MTAB */
-
#define min(a,b) ((a)<(b)?(a):(b))
-struct dir *
- adddir( vol, dir, name, namlen, upath, upathlen, st )
- struct vol *vol;
-struct dir *dir;
-char *name, *upath;
-int namlen, upathlen;
-struct stat *st;
+/*
+ * Struct to save directory reading context in. Used to prevent
+ * O(n^2) searches on a directory.
+ */
+struct savedir {
+ u_short sd_vid;
+ u_int32_t sd_did;
+ int sd_buflen;
+ char *sd_buf;
+ char *sd_last;
+ unsigned int sd_sindex;
+};
+#define SDBUFBRK 2048
+
+static int enumerate_loop(struct dirent *de, char *mname _U_, void *data)
{
- struct dir *cdir, *edir;
-#if AD_VERSION > AD_VERSION1
- struct adouble ad;
-#endif /* AD_VERSION > AD_VERSION1 */
+ struct savedir *sd = data;
+ char *start, *end;
+ int len,lenm;
+
+ end = sd->sd_buf + sd->sd_buflen;
+ len = strlen(de->d_name);
+ *(sd->sd_last)++ = len;
+ lenm = 0; /* strlen(mname);*/
+ if ( sd->sd_last + len +lenm + 4 > end ) {
+ char *buf;
+
+ start = sd->sd_buf;
+ if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
+ LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
+ strerror(errno) );
+ errno = ENOMEM;
+ return -1;
+ }
+ sd->sd_buf = buf;
+ sd->sd_buflen += SDBUFBRK;
+ sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
+ end = sd->sd_buf + sd->sd_buflen;
+ }
-#ifndef USE_LASTDID
- struct stat lst, *lstp;
-#endif /* USE_LASTDID */
+ memcpy( sd->sd_last, de->d_name, len + 1 );
+ sd->sd_last += len + 1;
+#if 0
+ *(sd->sd_last)++ = lenm;
+ memcpy( sd->sd_last, mname, lenm + 1 );
+ sd->sd_last += lenm + 1;
+#endif
+ return 0;
+}
- if ((cdir = dirnew(namlen + 1)) == NULL) {
- LOG(log_error, logtype_default, "adddir: malloc: %s", strerror(errno) );
+/* -----------------------------
+ * FIXME:
+ * Doesn't work with dangling symlink
+ * ie:
+ * - Move a folder with a dangling symlink in the trash
+ * - empty the trash
+ * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
+ * and the Mac doesn't try to call afp_delete!
+ *
+ * Another option for symlink
+ * cf:
+ * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
+ *
+*/
+char *check_dirent(const struct vol *vol, char *name)
+{
+ if (!strcmp(name, "..") || !strcmp(name, "."))
return NULL;
+
+ if (!vol->vfs->vfs_validupath(vol, name))
+ return NULL;
+
+ /* check for vetoed filenames */
+ if (veto_file(vol->v_veto, name))
+ return NULL;
+
+#if 0
+ char *m_name = NULL;
+
+ if (NULL == (m_name = utompath(vol, name, 0, utf8_encoding())))
+ return NULL;
+
+ /* now check against too big a file */
+ if (strlen(m_name) > vol->max_filename)
+ return NULL;
+#endif
+ return name;
+}
+
+/* ----------------------------- */
+int
+for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
+{
+ DIR *dp;
+ struct dirent *de;
+ char *m_name;
+ int ret;
+
+ if (NULL == ( dp = opendir( name)) ) {
+ return -1;
}
- strcpy( cdir->d_name, name );
- cdir->d_name[namlen] = '\0';
-
- cdir->d_did = 0;
-
-#if AD_VERSION > AD_VERSION1
- /* look in AD v2 header */
- memset(&ad, 0, sizeof(ad));
- if (ad_open(upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY, 0, &ad) >= 0) {
- /* if we can parse the AppleDouble header, retrieve the DID entry into cdir->d_did */
- memcpy(&cdir->d_did, ad_entry(&ad, ADEID_DID), sizeof(cdir->d_did));
- ad_close(&ad, ADFLAGS_HF);
- }
-#endif /* AD_VERSION */
-
-#ifdef CNID_DB
- /* add to cnid db */
- cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
- upathlen, cdir->d_did);
- /* Fail out if things go bad with CNID. */
- if (cdir->d_did > CNID_MAX) {
- switch (cdir->d_did) {
- case CNID_ERR_PARAM:
- LOG(log_error, logtype_default, "adddir: Incorrect parameters passed to cnid_add");
- return NULL;
- case CNID_ERR_PATH:
- case CNID_ERR_DB:
- case CNID_ERR_MAX:
- return NULL;
- }
- }
-#endif /* CNID_DB */
-
- if (cdir->d_did == 0) {
-#ifdef USE_LASTDID
- /* last way of doing DIDs */
- cdir->d_did = htonl( vol->v_lastdid++ );
-#else /* USE_LASTDID */
- lstp = lstat(upath, &lst) < 0 ? st : &lst;
-#ifdef DID_MTAB
- /* mtab way of doing DIDs */
- cdir->d_did = htonl( afpd_st_cnid ( lstp ) );
-#else /* DID_MTAB */
- /* the old way of doing DIDs (default) */
- cdir->d_did = htonl( CNID(lstp, 0) );
-#endif /* DID_MTAB */
-#endif /* USE_LASTDID */
- }
+ ret = 0;
+ for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
+ if (!(m_name = check_dirent(vol, de->d_name)))
+ continue;
- if ((edir = dirinsert( vol, cdir ))) {
-#ifndef CNID_DB
- if (edir->d_name) {
- if (strcmp(edir->d_name, cdir->d_name)) {
- LOG(log_info, logtype_default, "WARNING: DID conflict for '%s' and '%s'. Are these the same file?", edir->d_name, cdir->d_name);
- }
- free(cdir->d_name);
- free(cdir);
- return edir;
+ ret++;
+ if (fn && fn(de,m_name, data) < 0) {
+ closedir(dp);
+ return -1;
}
-#endif /* CNID_DB */
- edir->d_name = cdir->d_name;
- free(cdir);
- cdir = edir;
}
-
- /* parent/child directories */
- cdir->d_parent = dir;
- dirchildadd(dir, cdir);
- return( cdir );
+ closedir(dp);
+ return ret;
}
-/*
- * Struct to save directory reading context in. Used to prevent
- * O(n^2) searches on a directory.
- */
-struct savedir {
- u_short sd_vid;
- int sd_did;
- int sd_buflen;
- char *sd_buf;
- char *sd_last;
- int sd_sindex;
-};
-#define SDBUFBRK 1024
+/* This is the maximal length of a single entry for a file/dir in the reply
+ block if all bits in the file/dir bitmap are set: header(4) + params(104) +
+ macnamelength(1) + macname(31) + utf8(4) + utf8namelen(2) + utf8name(255) +
+ oddpadding(1) */
+
+#define REPLY_PARAM_MAXLEN (4 + 104 + 1 + MACFILELEN + 4 + 2 + 255 + 1)
-int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj *obj;
-char *ibuf, *rbuf;
-int ibuflen, *rbuflen;
+/* ----------------------------- */
+static int enumerate(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,
+ char *rbuf,
+ size_t *rbuflen,
+ int ext)
{
- struct stat st;
static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
struct vol *vol;
struct dir *dir;
- struct dirent *de;
- DIR *dp;
- int did, ret, esz, len, first = 1;
- char *path, *data, *end, *start;
+ int did, ret, len, first = 1;
+ size_t esz;
+ char *data, *start;
u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
- u_int16_t sindex, maxsz, sz = 0;
-
+ u_int16_t temp16;
+ u_int32_t sindex, maxsz, sz = 0;
+ struct path *o_path;
+ struct path s_path;
+ int header;
+
if ( sd.sd_buflen == 0 ) {
if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
- LOG(log_error, logtype_default, "afp_enumerate: malloc: %s", strerror(errno) );
+ LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
*rbuflen = 0;
return AFPERR_MISC;
}
memcpy( &vid, ibuf, sizeof( vid ));
ibuf += sizeof( vid );
- if (( vol = getvolbyvid( vid )) == NULL ) {
+ if (NULL == ( vol = getvolbyvid( vid )) ) {
*rbuflen = 0;
return( AFPERR_PARAM );
}
memcpy( &did, ibuf, sizeof( did ));
ibuf += sizeof( did );
- if (( dir = dirsearch( vol, did )) == NULL ) {
+ if (NULL == ( dir = dirlookup( vol, did )) ) {
*rbuflen = 0;
- return( AFPERR_NODIR );
+ return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
}
memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
reqcnt = ntohs( reqcnt );
ibuf += sizeof( reqcnt );
- memcpy( &sindex, ibuf, sizeof( sindex ));
- sindex = ntohs( sindex );
- ibuf += sizeof( sindex );
+ if (ext == 2) {
+ memcpy( &sindex, ibuf, sizeof( sindex ));
+ sindex = ntohl( sindex );
+ ibuf += sizeof( sindex );
+ }
+ else {
+ memcpy( &temp16, ibuf, sizeof( temp16 ));
+ sindex = ntohs( temp16 );
+ ibuf += sizeof( temp16 );
+ }
- memcpy( &maxsz, ibuf, sizeof( maxsz ));
- maxsz = ntohs( maxsz );
- ibuf += sizeof( maxsz );
+ if (!sindex) {
+ *rbuflen = 0;
+ return AFPERR_PARAM ;
+ }
+
+ if (ext == 2) {
+ memcpy( &maxsz, ibuf, sizeof( maxsz ));
+ maxsz = ntohl( maxsz );
+ ibuf += sizeof( maxsz );
+ }
+ else {
+ memcpy( &temp16, ibuf, sizeof( temp16 ));
+ maxsz = ntohs( temp16 );
+ ibuf += sizeof( temp16 );
+ }
+
+ header = (ext)?4:2;
+ header *=sizeof( u_char );
+
+ maxsz = min(maxsz, *rbuflen - REPLY_PARAM_MAXLEN);
+ o_path = cname( vol, dir, &ibuf );
- maxsz = min(maxsz, *rbuflen);
+ if (afp_errno == AFPERR_NOOBJ)
+ afp_errno = AFPERR_NODIR;
- if (( path = cname( vol, dir, &ibuf )) == NULL ) {
- *rbuflen = 0;
- return( AFPERR_NODIR );
+ *rbuflen = 0;
+ if (NULL == o_path ) {
+ return get_afp_errno(AFPERR_NOOBJ);
+ }
+ if ( *o_path->m_name != '\0') {
+ /* it's a file or it's a dir and extendir() was unable to chdir in it */
+ return path_error(o_path, AFPERR_NODIR );
}
+
+ LOG(log_debug, logtype_afpd, "enumerate(\"%s/%s\", f/d:%04x/%04x, rc:%u, i:%u, max:%u)",
+ getcwdpath(), o_path->u_name, fbitmap, dbitmap, reqcnt, sindex, maxsz);
+
data = rbuf + 3 * sizeof( u_int16_t );
- sz = 3 * sizeof( u_int16_t );
+ sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
/*
* Read the directory into a pre-malloced buffer, stored
*/
if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
sd.sd_last = sd.sd_buf;
-
- if (( dp = opendir( mtoupath(vol, path ))) == NULL ) {
- *rbuflen = 0;
- return (errno == ENOTDIR) ? AFPERR_BADTYPE : AFPERR_NODIR;
- }
-
- end = sd.sd_buf + sd.sd_buflen;
- for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
- if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
- continue;
-
- if (!(validupath(vol, de->d_name)))
- continue;
-
- /* check for vetoed filenames */
- if (veto_file(vol->v_veto, de->d_name))
- continue;
-
- /* now check against too big a file */
- if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
- continue;
-
- len = strlen(de->d_name);
- *(sd.sd_last)++ = len;
-
- if ( sd.sd_last + len + 2 > end ) {
- char *buf;
-
- start = sd.sd_buf;
- if ((buf = (char *) realloc( sd.sd_buf, sd.sd_buflen +
- SDBUFBRK )) == NULL ) {
- LOG(log_error, logtype_default, "afp_enumerate: realloc: %s",
- strerror(errno) );
- closedir(dp);
- *rbuflen = 0;
- return AFPERR_MISC;
- }
- sd.sd_buf = buf;
- sd.sd_buflen += SDBUFBRK;
- sd.sd_last = ( sd.sd_last - start ) + sd.sd_buf;
- end = sd.sd_buf + sd.sd_buflen;
+ /* if dir was in the cache we don't have the inode */
+ if (( !o_path->st_valid && lstat( ".", &o_path->st ) < 0 ) ||
+ (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
+ {
+ switch (errno) {
+ case EACCES:
+ return AFPERR_ACCESS;
+ case ENOTDIR:
+ return AFPERR_BADTYPE;
+ case ENOMEM:
+ return AFPERR_MISC;
+ default:
+ return AFPERR_NODIR;
}
-
- memcpy( sd.sd_last, de->d_name, len + 1 );
- sd.sd_last += len + 1;
}
+ setdiroffcnt(curdir, &o_path->st, ret);
*sd.sd_last = 0;
sd.sd_last = sd.sd_buf;
sd.sd_sindex = 1;
- closedir( dp );
sd.sd_vid = vid;
- sd.sd_did = did;
+ sd.sd_did = curdir->d_did;
}
/*
sd.sd_last = sd.sd_buf;
}
while ( sd.sd_sindex < sindex ) {
- len = *(sd.sd_last)++;
+ len = (unsigned char)*(sd.sd_last)++;
if ( len == 0 ) {
- sd.sd_did = -1; /* invalidate sd struct to force re-read */
- *rbuflen = 0;
+ sd.sd_did = 0; /* invalidate sd struct to force re-read */
return( AFPERR_NOOBJ );
}
sd.sd_last += len + 1;
sd.sd_sindex++;
}
- while (( len = *(sd.sd_last)) != 0 ) {
+ while (( len = (unsigned char)*(sd.sd_last)) != 0 ) {
/*
* If we've got all we need, send it.
*/
start = sd.sd_last;
sd.sd_last++;
- if ( stat( sd.sd_last, &st ) < 0 ) {
- LOG(log_debug, logtype_default, "afp_enumerate: stat %s: %s",
- sd.sd_last, strerror(errno) );
+ if (*sd.sd_last == 0) {
+ /* stat() already failed on this one */
+ sd.sd_last += len + 1;
+ continue;
+ }
+ memset(&s_path, 0, sizeof(s_path));
+ s_path.u_name = sd.sd_last;
+ if (of_stat( &s_path) < 0 ) {
+ /*
+ * Somebody else plays with the dir, well it can be us with
+ * "Empty Trash..."
+ */
+
+ /* so the next time it won't try to stat it again
+ * another solution would be to invalidate the cache with
+ * sd.sd_did = 0 but if it's not ENOENT error it will start again
+ */
+ *sd.sd_last = 0;
sd.sd_last += len + 1;
+ curdir->offcnt--; /* a little lie */
continue;
}
+ sd.sd_last += len + 1;
+ s_path.m_name = NULL;
/*
* If a fil/dir is not a dir, it's a file. This is slightly
* inaccurate, since that means /dev/null is a file, /dev/printer
* is a file, etc.
*/
- if ( S_ISDIR(st.st_mode)) {
+ if ( S_ISDIR(s_path.st.st_mode)) {
if ( dbitmap == 0 ) {
- sd.sd_last += len + 1;
continue;
}
- path = utompath(vol, sd.sd_last);
- dir = curdir->d_child;
- while (dir) {
- if ( strcmp( dir->d_name, path ) == 0 ) {
- break;
+ int len = strlen(s_path.u_name);
+ if ((dir = dircache_search_by_name(vol, curdir, s_path.u_name, len, s_path.st.st_ctime)) == NULL) {
+ if ((dir = dir_add(vol, curdir, &s_path, len)) == NULL) {
+ LOG(log_error, logtype_afpd, "enumerate(vid:%u, did:%u, name:'%s'): error adding dir: '%s'",
+ ntohs(vid), ntohl(did), o_path->u_name, s_path.u_name);
+ return AFPERR_MISC;
}
- dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
}
- if (!dir && ((dir = adddir( vol, curdir, path, strlen( path ),
- sd.sd_last, len, &st)) == NULL)) {
- *rbuflen = 0;
- return AFPERR_MISC;
- }
-
-
- if (( ret = getdirparams(vol, dbitmap, sd.sd_last, dir,
- &st, data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
- *rbuflen = 0;
+ if ((ret = getdirparams(vol, dbitmap, &s_path, dir, data + header , &esz)) != AFP_OK)
return( ret );
- }
} else {
if ( fbitmap == 0 ) {
- sd.sd_last += len + 1;
continue;
}
-
- if (( ret = getfilparams(vol, fbitmap, utompath(vol, sd.sd_last),
- curdir, &st, data + 2 * sizeof( u_char ), &esz )) !=
- AFP_OK ) {
- *rbuflen = 0;
+ /* files are added to the dircache in getfilparams() -> getmetadata() */
+ if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
+ data + header , &esz )) ) {
return( ret );
}
}
* Make sure entry is an even length, possibly with a null
* byte on the end.
*/
- if ( esz & 1 ) {
- *(data + 2 * sizeof( u_char ) + esz ) = '\0';
+ if ( (esz + header) & 1 ) {
+ *(data + header + esz ) = '\0';
esz++;
}
/*
* Check if we've exceeded the size limit.
*/
- if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
+ if ( maxsz < sz + esz + header) {
if (first) { /* maxsz can't hold a single reply */
- *rbuflen = 0;
return AFPERR_PARAM;
}
sd.sd_last = start;
if (first)
first = 0;
- sz += esz + 2 * sizeof( u_char );
- *data++ = esz + 2 * sizeof( u_char );
- *data++ = S_ISDIR(st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
+ sz += esz + header;
+ if (ext) {
+ temp16 = htons( esz + header );
+ memcpy( data, &temp16, sizeof( temp16 ));
+ data += sizeof(temp16);
+ }
+ else {
+ *data++ = esz + header;
+ }
+
+ *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
+ if (ext) {
+ *data++ = 0;
+ }
data += esz;
actcnt++;
- sd.sd_last += len + 1;
+ /* FIXME if we rollover 16 bits and it's not FPEnumerateExt2 */
}
if ( actcnt == 0 ) {
- *rbuflen = 0;
- sd.sd_did = -1; /* invalidate sd struct to force re-read */
+ sd.sd_did = 0; /* invalidate sd struct to force re-read */
return( AFPERR_NOOBJ );
}
sd.sd_sindex = sindex + actcnt;
return( AFP_OK );
}
-
-/* why is this here? well, FPCatSearch is essentially an FPEnumerate
- * with filters. */
-int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
- char *rbuf, int *rbuflen)
+/* ----------------------------- */
+int afp_enumerate(AFPObj *obj, char *ibuf, size_t ibuflen,
+ char *rbuf,
+ size_t *rbuflen)
{
- struct vol *vol;
- u_int16_t vid;
-
- ibuf += 2;
- memcpy(&vid, ibuf, sizeof(vid));
- ibuf += sizeof(vid);
+ return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
+}
- *rbuflen = 0;
- if ((vol = getvolbyvid(vid)) == NULL)
- return AFPERR_PARAM;
-
- /* the ritual:
- * do a breadth-first search of directories:
- * lookup did/name info.
- * add to result if match
- * check to see if we've exceeded our timelimit
- * if yes, return current position
- * if not, continue
- *
- * we keep a copy of our current position in struct vol.
- * if the next catsearch request for that volume isn't at
- * at the current position, bail and return catchanged.
- */
+/* ----------------------------- */
+int afp_enumerate_ext(AFPObj *obj, char *ibuf, size_t ibuflen,
+ char *rbuf,
+ size_t *rbuflen)
+{
+ return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
+}
- /* eof when done */
- return AFPERR_EOF;
+/* ----------------------------- */
+int afp_enumerate_ext2(AFPObj *obj, char *ibuf, size_t ibuflen,
+ char *rbuf,
+ size_t *rbuflen)
+{
+ return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);
}
+