X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=etc%2Fafpd%2Fenumerate.c;h=fe5a66d993746610ce45747d347abc1e409036e4;hb=e22bbf78a53bf739dd38383de42a23c95d43c15d;hp=db831b2b58a19473a0cfc502cceedaaa52891a8b;hpb=87502dc8efa57f6335a2584d29a5a234719b49b7;p=netatalk.git diff --git a/etc/afpd/enumerate.c b/etc/afpd/enumerate.c index db831b2b..fe5a66d9 100644 --- a/etc/afpd/enumerate.c +++ b/etc/afpd/enumerate.c @@ -1,6 +1,4 @@ /* - * $Id: enumerate.c,v 1.36 2003-03-09 21:14:19 didg Exp $ - * * Copyright (c) 1990,1993 Regents of The University of Michigan. * All Rights Reserved. See COPYRIGHT. */ @@ -13,84 +11,29 @@ #include #include #include - -#include -#include -#include #include #include -#include +#include #include #include -#ifdef CNID_DB +#include #include -#endif /* CNID_DB */ +#include +#include +#include +#include + #include "desktop.h" #include "directory.h" +#include "dircache.h" #include "volume.h" -#include "globals.h" #include "file.h" #include "fork.h" #include "filedir.h" #define min(a,b) ((a)<(b)?(a):(b)) -/* ---------------------------- */ -struct dir * - adddir( vol, dir, path) -struct vol *vol; -struct dir *dir; -struct path *path; -{ - struct dir *cdir, *edir; - int upathlen; - char *name; - char *upath; - struct stat *st; - - upath = path->u_name; - name = path->m_name; - st = &path->st; - upathlen = strlen(upath); - if ((cdir = dirnew(name, upath)) == NULL) { - LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) ); - return NULL; - } - - 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 - */ - 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) - return cdir; - /* the old was not in the same folder */ - dirchildremove(cdir->d_parent, cdir); - } - - /* parent/child directories */ - cdir->d_parent = dir; - dirchildadd(dir, cdir); - return( cdir ); -} /* * Struct to save directory reading context in. Used to prevent * O(n^2) searches on a directory. @@ -105,7 +48,7 @@ struct savedir { }; #define SDBUFBRK 2048 -static int enumerate_loop(struct dirent *de, char *mname, void *data) +static int enumerate_loop(struct dirent *de, char *mname _U_, void *data) { struct savedir *sd = data; char *start, *end; @@ -114,8 +57,7 @@ static int enumerate_loop(struct dirent *de, char *mname, void *data) end = sd->sd_buf + sd->sd_buflen; len = strlen(de->d_name); *(sd->sd_last)++ = len; - lenm = strlen(mname); - + lenm = 0; /* strlen(mname);*/ if ( sd->sd_last + len +lenm + 4 > end ) { char *buf; @@ -134,11 +76,11 @@ static int enumerate_loop(struct dirent *de, char *mname, void *data) 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; } @@ -158,25 +100,27 @@ static int enumerate_loop(struct dirent *de, char *mname, void *data) */ char *check_dirent(const struct vol *vol, char *name) { - char *m_name = NULL; - if (!strcmp(name, "..") || !strcmp(name, ".")) return NULL; - if (!(validupath(vol, name))) + if (!vol->vfs->vfs_validupath(vol, name)) return NULL; /* check for vetoed filenames */ if (veto_file(vol->v_veto, name)) return NULL; - if (NULL == (m_name = utompath(vol, name, utf8_encoding()))) + +#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; - - return m_name; +#endif + return name; } /* ----------------------------- */ @@ -206,17 +150,24 @@ for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data) return ret; } +/* 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 + UTF8FILELEN_EARLY + 1) + /* ----------------------------- */ -static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext ) -AFPObj *obj; -char *ibuf, *rbuf; -unsigned int ibuflen, *rbuflen; -int ext; +static int enumerate(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, + char *rbuf, + size_t *rbuflen, + int ext) { static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 }; struct vol *vol; struct dir *dir; - int did, ret, esz, len, first = 1; + int did, ret, len, first = 1; + size_t esz; char *data, *start; u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0; u_int16_t temp16; @@ -303,7 +254,7 @@ int ext; header = (ext)?4:2; header *=sizeof( u_char ); - maxsz = min(maxsz, *rbuflen); + maxsz = min(maxsz, *rbuflen - REPLY_PARAM_MAXLEN); o_path = cname( vol, dir, &ibuf ); if (afp_errno == AFPERR_NOOBJ) @@ -311,13 +262,16 @@ int ext; *rbuflen = 0; if (NULL == o_path ) { - return afp_errno; + 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_isadir(o_path))? afp_errno:AFPERR_BADTYPE ; + 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 ); /* fbitmap, dbitmap, reqcount */ @@ -329,12 +283,13 @@ int ext; if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) { sd.sd_last = sd.sd_buf; /* 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) + if (( !o_path->st_valid && lstat( ".", &o_path->st ) < 0 ) || + (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) { + LOG(log_error, logtype_afpd, "enumerate: loop error: %s (%d)", strerror(errno), errno); switch (errno) { case EACCES: - return AFPERR_ACCESS; + return AFPERR_ACCESS; case ENOTDIR: return AFPERR_BADTYPE; case ENOMEM: @@ -343,15 +298,14 @@ int ext; return AFPERR_NODIR; } } - curdir->ctime = o_path->st.st_ctime; /* play safe */ - curdir->offcnt = ret; + setdiroffcnt(curdir, &o_path->st, ret); *sd.sd_last = 0; sd.sd_last = sd.sd_buf; sd.sd_sindex = 1; sd.sd_vid = vid; - sd.sd_did = did; + sd.sd_did = curdir->d_did; } /* @@ -362,18 +316,16 @@ int ext; 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 = 0; /* invalidate sd struct to force re-read */ return( AFPERR_NOOBJ ); } sd.sd_last += len + 1; - len = *(sd.sd_last)++; - 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. */ @@ -391,10 +343,9 @@ int ext; 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; } + memset(&s_path, 0, sizeof(s_path)); s_path.u_name = sd.sd_last; if (of_stat( &s_path) < 0 ) { /* @@ -408,16 +359,12 @@ int ext; */ *sd.sd_last = 0; sd.sd_last += len + 1; - len = *(sd.sd_last)++; - sd.sd_last += len + 1; - curdir->offcnt--; /* a little lie */ + curdir->d_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; + 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 @@ -427,27 +374,22 @@ int ext; if ( dbitmap == 0 ) { continue; } - dir = dirsearch_byname(curdir, s_path.u_name); - if (!dir) { - if (s_path.m_name == NULL || (dir = adddir( vol, curdir, &s_path)) == NULL) { + int len = strlen(s_path.u_name); + if ((dir = dircache_search_by_name(vol, curdir, s_path.u_name, len)) == 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; } } - else { - s_path.m_name = NULL; - } - if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir, - data + header , &esz ))) { + if ((ret = getdirparams(vol, dbitmap, &s_path, dir, data + header , &esz)) != AFP_OK) return( ret ); - } } else { if ( fbitmap == 0 ) { continue; } - if (s_path.m_name == NULL ) { - return AFPERR_MISC; - } + /* files are added to the dircache in getfilparams() -> getmetadata() */ if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir, data + header , &esz )) ) { return( ret ); @@ -493,6 +435,7 @@ int ext; } data += esz; actcnt++; + /* FIXME if we rollover 16 bits and it's not FPEnumerateExt2 */ } if ( actcnt == 0 ) { @@ -518,28 +461,25 @@ int ext; } /* ----------------------------- */ -int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen ) -AFPObj *obj; -char *ibuf, *rbuf; -unsigned int ibuflen, *rbuflen; +int afp_enumerate(AFPObj *obj, char *ibuf, size_t ibuflen, + char *rbuf, + size_t *rbuflen) { return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0); } /* ----------------------------- */ -int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen ) -AFPObj *obj; -char *ibuf, *rbuf; -unsigned int ibuflen, *rbuflen; +int afp_enumerate_ext(AFPObj *obj, char *ibuf, size_t ibuflen, + char *rbuf, + size_t *rbuflen) { return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1); } /* ----------------------------- */ -int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen ) -AFPObj *obj; -char *ibuf, *rbuf; -unsigned int ibuflen, *rbuflen; +int afp_enumerate_ext2(AFPObj *obj, char *ibuf, size_t ibuflen, + char *rbuf, + size_t *rbuflen) { return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2); }