2 * $Id: enumerate.c,v 1.49 2010-02-10 14:05:37 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
19 #include <atalk/logger.h>
20 #include <atalk/afp.h>
21 #include <atalk/adouble.h>
22 #include <atalk/vfs.h>
23 #include <atalk/cnid.h>
25 #include "directory.h"
32 #define min(a,b) ((a)<(b)?(a):(b))
35 * Struct to save directory reading context in. Used to prevent
36 * O(n^2) searches on a directory.
44 unsigned int sd_sindex;
48 static int enumerate_loop(struct dirent *de, char *mname _U_, void *data)
50 struct savedir *sd = data;
54 end = sd->sd_buf + sd->sd_buflen;
55 len = strlen(de->d_name);
56 *(sd->sd_last)++ = len;
57 lenm = 0; /* strlen(mname);*/
58 if ( sd->sd_last + len +lenm + 4 > end ) {
62 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
63 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
69 sd->sd_buflen += SDBUFBRK;
70 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
71 end = sd->sd_buf + sd->sd_buflen;
74 memcpy( sd->sd_last, de->d_name, len + 1 );
75 sd->sd_last += len + 1;
77 *(sd->sd_last)++ = lenm;
78 memcpy( sd->sd_last, mname, lenm + 1 );
79 sd->sd_last += lenm + 1;
84 /* -----------------------------
86 * Doesn't work with dangling symlink
88 * - Move a folder with a dangling symlink in the trash
90 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
91 * and the Mac doesn't try to call afp_delete!
93 * Another option for symlink
95 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
98 char *check_dirent(const struct vol *vol, char *name)
100 if (!strcmp(name, "..") || !strcmp(name, "."))
103 if (!vol->vfs->vfs_validupath(vol, name))
106 /* check for vetoed filenames */
107 if (veto_file(vol->v_veto, name))
113 if (NULL == (m_name = utompath(vol, name, 0, utf8_encoding())))
116 /* now check against too big a file */
117 if (strlen(m_name) > vol->max_filename)
123 /* ----------------------------- */
125 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
132 if (NULL == ( dp = opendir( name)) ) {
136 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
137 if (!(m_name = check_dirent(vol, de->d_name)))
141 if (fn && fn(de,m_name, data) < 0) {
150 /* This is the maximal length of a single entry for a file/dir in the reply
151 block if all bits in the file/dir bitmap are set: header(4) + params(104) +
152 macnamelength(1) + macname(31) + utf8(4) + utf8namelen(2) + utf8name(255) +
155 #define REPLY_PARAM_MAXLEN (4 + 104 + 1 + MACFILELEN + 4 + 2 + 255 + 1)
157 /* ----------------------------- */
158 static int enumerate(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,
163 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
166 int did, ret, len, first = 1;
169 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
171 u_int32_t sindex, maxsz, sz = 0;
176 if ( sd.sd_buflen == 0 ) {
177 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
178 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
182 sd.sd_buflen = SDBUFBRK;
187 memcpy( &vid, ibuf, sizeof( vid ));
188 ibuf += sizeof( vid );
190 if (NULL == ( vol = getvolbyvid( vid )) ) {
192 return( AFPERR_PARAM );
195 memcpy( &did, ibuf, sizeof( did ));
196 ibuf += sizeof( did );
198 if (NULL == ( dir = dirlookup( vol, did )) ) {
200 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
203 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
204 fbitmap = ntohs( fbitmap );
205 ibuf += sizeof( fbitmap );
207 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
208 dbitmap = ntohs( dbitmap );
209 ibuf += sizeof( dbitmap );
211 /* check for proper bitmaps -- the stuff in comments is for
212 * variable directory ids. */
213 if (!(fbitmap || dbitmap)
214 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
215 (dbitmap & (1 << DIRPBIT_PDID))*/) {
217 return AFPERR_BITMAP;
220 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
221 reqcnt = ntohs( reqcnt );
222 ibuf += sizeof( reqcnt );
225 memcpy( &sindex, ibuf, sizeof( sindex ));
226 sindex = ntohl( sindex );
227 ibuf += sizeof( sindex );
230 memcpy( &temp16, ibuf, sizeof( temp16 ));
231 sindex = ntohs( temp16 );
232 ibuf += sizeof( temp16 );
237 return AFPERR_PARAM ;
241 memcpy( &maxsz, ibuf, sizeof( maxsz ));
242 maxsz = ntohl( maxsz );
243 ibuf += sizeof( maxsz );
246 memcpy( &temp16, ibuf, sizeof( temp16 ));
247 maxsz = ntohs( temp16 );
248 ibuf += sizeof( temp16 );
252 header *=sizeof( u_char );
254 maxsz = min(maxsz, *rbuflen - REPLY_PARAM_MAXLEN);
255 o_path = cname( vol, dir, &ibuf );
257 if (afp_errno == AFPERR_NOOBJ)
258 afp_errno = AFPERR_NODIR;
261 if (NULL == o_path ) {
262 return get_afp_errno(AFPERR_NOOBJ);
264 if ( *o_path->m_name != '\0') {
265 /* it's a file or it's a dir and extendir() was unable to chdir in it */
266 return path_error(o_path, AFPERR_NODIR );
269 LOG(log_debug, logtype_afpd, "enumerate(vid:%u, did:%u, name:'%s', f/d:%04x/%04x, rc:%u, i:%u, max:%u)",
270 ntohs(vid), ntohl(did), o_path->u_name, fbitmap, dbitmap, reqcnt, sindex, maxsz);
272 data = rbuf + 3 * sizeof( u_int16_t );
273 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
276 * Read the directory into a pre-malloced buffer, stored
278 * The end is indicated by a len of 0.
280 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
281 sd.sd_last = sd.sd_buf;
282 /* if dir was in the cache we don't have the inode */
283 if (( !o_path->st_valid && lstat( ".", &o_path->st ) < 0 ) ||
284 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
288 return AFPERR_ACCESS;
290 return AFPERR_BADTYPE;
297 setdiroffcnt(curdir, &o_path->st, ret);
300 sd.sd_last = sd.sd_buf;
304 sd.sd_did = curdir->d_did;
308 * Position sd_last as dictated by sindex.
310 if ( sindex < sd.sd_sindex ) {
312 sd.sd_last = sd.sd_buf;
314 while ( sd.sd_sindex < sindex ) {
315 len = (unsigned char)*(sd.sd_last)++;
317 sd.sd_did = 0; /* invalidate sd struct to force re-read */
318 return( AFPERR_NOOBJ );
320 sd.sd_last += len + 1;
324 while (( len = (unsigned char)*(sd.sd_last)) != 0 ) {
326 * If we've got all we need, send it.
328 if ( actcnt == reqcnt ) {
333 * Save the start position, in case we exceed the buffer
334 * limitation, and have to back up one.
339 if (*sd.sd_last == 0) {
340 /* stat() already failed on this one */
341 sd.sd_last += len + 1;
344 memset(&s_path, 0, sizeof(s_path));
345 s_path.u_name = sd.sd_last;
346 if (of_stat( &s_path) < 0 ) {
348 * Somebody else plays with the dir, well it can be us with
352 /* so the next time it won't try to stat it again
353 * another solution would be to invalidate the cache with
354 * sd.sd_did = 0 but if it's not ENOENT error it will start again
357 sd.sd_last += len + 1;
358 curdir->offcnt--; /* a little lie */
362 sd.sd_last += len + 1;
363 s_path.m_name = NULL;
365 * If a fil/dir is not a dir, it's a file. This is slightly
366 * inaccurate, since that means /dev/null is a file, /dev/printer
369 if ( S_ISDIR(s_path.st.st_mode)) {
370 if ( dbitmap == 0 ) {
373 dir = dirsearch_byname(vol, curdir, s_path.u_name);
374 if (!dir && NULL == (dir = adddir( vol, curdir, &s_path) ) ) {
377 if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
378 data + header , &esz ))) {
383 if ( fbitmap == 0 ) {
386 if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
387 data + header , &esz )) ) {
393 * Make sure entry is an even length, possibly with a null
396 if ( (esz + header) & 1 ) {
397 *(data + header + esz ) = '\0';
402 * Check if we've exceeded the size limit.
404 if ( maxsz < sz + esz + header) {
405 if (first) { /* maxsz can't hold a single reply */
417 temp16 = htons( esz + header );
418 memcpy( data, &temp16, sizeof( temp16 ));
419 data += sizeof(temp16);
422 *data++ = esz + header;
425 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
431 /* FIXME if we rollover 16 bits and it's not FPEnumerateExt2 */
435 sd.sd_did = 0; /* invalidate sd struct to force re-read */
436 return( AFPERR_NOOBJ );
438 sd.sd_sindex = sindex + actcnt;
441 * All done, fill in misc junk in rbuf
443 fbitmap = htons( fbitmap );
444 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
445 rbuf += sizeof( fbitmap );
446 dbitmap = htons( dbitmap );
447 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
448 rbuf += sizeof( dbitmap );
449 actcnt = htons( actcnt );
450 memcpy( rbuf, &actcnt, sizeof( actcnt ));
451 rbuf += sizeof( actcnt );
456 /* ----------------------------- */
457 int afp_enumerate(AFPObj *obj, char *ibuf, size_t ibuflen,
461 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
464 /* ----------------------------- */
465 int afp_enumerate_ext(AFPObj *obj, char *ibuf, size_t ibuflen,
469 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
472 /* ----------------------------- */
473 int afp_enumerate_ext2(AFPObj *obj, char *ibuf, size_t ibuflen,
477 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);