/*
- * $Id: enumerate.c,v 1.29 2003-01-07 22:13:39 didg Exp $
+ * $Id: enumerate.c,v 1.38 2003-04-16 06:55:44 didg Exp $
*
* Copyright (c) 1990,1993 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
int upathlen;
char *name;
char *upath;
-#ifndef USE_LASTDID
- struct stat lst, *lstp;
-#endif /* USE_LASTDID */
struct stat *st;
+ int deleted;
upath = path->u_name;
name = path->m_name;
return NULL;
}
- cdir->d_did = 0;
-
-#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_INVALID) {
- switch (errno) {
- case CNID_ERR_PARAM:
- LOG(log_error, logtype_afpd, "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;
- /* the old way of doing DIDs (default) */
- cdir->d_did = htonl( CNID(lstp, 0) );
-#endif /* USE_LASTDID */
- }
+ cdir->d_did = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
+ if (cdir->d_did == 0)
+ return NULL;
if ((edir = dirinsert( vol, cdir ))) {
+ /* it's not possible with LASTDID
+ for CNID:
+ - someone else have moved the directory.
+ - it's a symlink inside the share.
+ - it's an ID reused, the old directory was deleted but not
+ the cnid record and the server've reused the inode for
+ the new dir.
+ for HASH (we should get ride of HASH)
+ - someone else have moved the directory.
+ - it's an ID reused as above
+ - it's a hash duplicate and we are in big trouble
+ */
+ deleted = (edir->d_m_name == NULL);
+ dirfreename(edir);
edir->d_m_name = cdir->d_m_name;
edir->d_u_name = cdir->d_u_name;
free(cdir);
cdir = edir;
+ if (!cdir->d_parent || (cdir->d_parent == dir && !deleted))
+ return cdir;
+ /* the old was not in the same folder */
+ if (!deleted)
+ dirchildremove(cdir->d_parent, cdir);
}
/* parent/child directories */
char *sd_last;
unsigned int sd_sindex;
};
-#define SDBUFBRK 1024
+#define SDBUFBRK 2048
static int enumerate_loop(struct dirent *de, char *mname, void *data)
{
struct savedir *sd = data;
char *start, *end;
- int len;
+ int len,lenm;
end = sd->sd_buf + sd->sd_buflen;
len = strlen(de->d_name);
*(sd->sd_last)++ = len;
+ lenm = strlen(mname);
- if ( sd->sd_last + len + 2 > end ) {
+ if ( sd->sd_last + len +lenm + 4 > end ) {
char *buf;
start = sd->sd_buf;
memcpy( sd->sd_last, de->d_name, len + 1 );
sd->sd_last += len + 1;
+
+ *(sd->sd_last)++ = lenm;
+ memcpy( sd->sd_last, mname, lenm + 1 );
+ sd->sd_last += lenm + 1;
+
return 0;
}
/* check for vetoed filenames */
if (veto_file(vol->v_veto, name))
return NULL;
+ if (NULL == (m_name = utompath(vol, name, utf8_encoding())))
+ return NULL;
/* now check against too big a file */
- if (strlen(m_name = utompath(vol, name)) > vol->max_filename)
+ if (strlen(m_name) > vol->max_filename)
return NULL;
return m_name;
char *m_name;
int ret;
- if (( dp = opendir( name)) == NULL ) {
+ if (NULL == ( dp = opendir( name)) ) {
return -1;
}
ret = 0;
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 = dirlookup( 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 ));
header *=sizeof( u_char );
maxsz = min(maxsz, *rbuflen);
+ o_path = cname( vol, dir, &ibuf );
- if (( o_path = cname( vol, dir, &ibuf )) == NULL) {
- *rbuflen = 0;
- return( AFPERR_NODIR );
- }
+ if (afp_errno == AFPERR_NOOBJ)
+ afp_errno = AFPERR_NODIR;
+ *rbuflen = 0;
+ if (NULL == o_path ) {
+ return get_afp_errno(AFPERR_NOOBJ);
+ }
if ( *o_path->m_name != '\0') {
- *rbuflen = 0;
- return( AFPERR_BADTYPE );
+ /* it's a file or it's a dir and extendir() was unable to chdir in it */
+ return (path_isadir(o_path))? afp_errno:AFPERR_BADTYPE ;
}
data = rbuf + 3 * sizeof( u_int16_t );
*/
if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
sd.sd_last = sd.sd_buf;
- if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
+ /* if dir was in the cache we don't have the inode */
+ if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
+ (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
+ {
switch (errno) {
case EACCES:
return AFPERR_ACCESS;
}
}
curdir->ctime = o_path->st.st_ctime; /* play safe */
- if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
- *rbuflen = 0;
- switch (errno) {
- case EACCES:
- return AFPERR_ACCESS;
- case ENOTDIR:
- return AFPERR_BADTYPE;
- case ENOMEM:
- return AFPERR_MISC;
- default:
- return AFPERR_NODIR;
- }
- }
curdir->offcnt = ret;
*sd.sd_last = 0;
len = *(sd.sd_last)++;
if ( len == 0 ) {
sd.sd_did = 0; /* invalidate sd struct to force re-read */
- *rbuflen = 0;
return( AFPERR_NOOBJ );
}
sd.sd_last += len + 1;
+ len = *(sd.sd_last)++;
+ sd.sd_last += len + 1;
sd.sd_sindex++;
}
if (*sd.sd_last == 0) {
/* stat() already failed on this one */
sd.sd_last += len + 1;
+ len = *(sd.sd_last)++;
+ sd.sd_last += len + 1;
continue;
}
s_path.u_name = sd.sd_last;
*/
*sd.sd_last = 0;
sd.sd_last += len + 1;
+ len = *(sd.sd_last)++;
+ sd.sd_last += len + 1;
curdir->offcnt--; /* a little lie */
continue;
}
+ sd.sd_last += len + 1;
+ len = *(sd.sd_last)++;
+ s_path.m_name = sd.sd_last;
+ sd.sd_last += len + 1;
/*
* 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
*/
if ( S_ISDIR(s_path.st.st_mode)) {
if ( dbitmap == 0 ) {
- sd.sd_last += len + 1;
continue;
}
- dir = curdir->d_child;
- s_path.m_name = NULL;
- while (dir) {
- if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
- break;
- }
- dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
- }
+ dir = dirsearch_byname(curdir, s_path.u_name);
if (!dir) {
- s_path.m_name = utompath(vol, s_path.u_name);
- if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
- *rbuflen = 0;
+ if (s_path.m_name == NULL || (dir = adddir( vol, curdir, &s_path)) == NULL) {
return AFPERR_MISC;
}
}
-
- if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
- data + header , &esz )) != AFP_OK ) {
- *rbuflen = 0;
+ else {
+ s_path.m_name = NULL;
+ }
+ if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
+ data + header , &esz ))) {
return( ret );
}
} else {
if ( fbitmap == 0 ) {
- sd.sd_last += len + 1;
continue;
}
- s_path.m_name = utompath(vol, s_path.u_name);
- if (( ret = getfilparams(vol, fbitmap, &s_path, curdir,
- data + header , &esz )) != AFP_OK ) {
- *rbuflen = 0;
+ if (s_path.m_name == NULL ) {
+ return AFPERR_MISC;
+ }
+ if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
+ data + header , &esz )) ) {
return( ret );
}
}
*/
if ( maxsz < sz + esz + header) {
if (first) { /* maxsz can't hold a single reply */
- *rbuflen = 0;
return AFPERR_PARAM;
}
sd.sd_last = start;
}
data += esz;
actcnt++;
- sd.sd_last += len + 1;
}
if ( actcnt == 0 ) {
- *rbuflen = 0;
sd.sd_did = 0; /* invalidate sd struct to force re-read */
return( AFPERR_NOOBJ );
}