2 * $Id: enumerate.c,v 1.45 2009-10-13 22:55:36 didg 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)
101 if (!strcmp(name, "..") || !strcmp(name, "."))
104 if (!vol->vfs->validupath(vol, name))
107 /* check for vetoed filenames */
108 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, unsigned int ibuflen _U_,
160 unsigned int *rbuflen,
163 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
166 int did, ret, esz, len, first = 1;
168 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
170 u_int32_t sindex, maxsz, sz = 0;
175 if ( sd.sd_buflen == 0 ) {
176 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
177 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
181 sd.sd_buflen = SDBUFBRK;
186 memcpy( &vid, ibuf, sizeof( vid ));
187 ibuf += sizeof( vid );
189 if (NULL == ( vol = getvolbyvid( vid )) ) {
191 return( AFPERR_PARAM );
194 memcpy( &did, ibuf, sizeof( did ));
195 ibuf += sizeof( did );
197 if (NULL == ( dir = dirlookup( vol, did )) ) {
199 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
202 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
203 fbitmap = ntohs( fbitmap );
204 ibuf += sizeof( fbitmap );
206 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
207 dbitmap = ntohs( dbitmap );
208 ibuf += sizeof( dbitmap );
210 /* check for proper bitmaps -- the stuff in comments is for
211 * variable directory ids. */
212 if (!(fbitmap || dbitmap)
213 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
214 (dbitmap & (1 << DIRPBIT_PDID))*/) {
216 return AFPERR_BITMAP;
219 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
220 reqcnt = ntohs( reqcnt );
221 ibuf += sizeof( reqcnt );
224 memcpy( &sindex, ibuf, sizeof( sindex ));
225 sindex = ntohl( sindex );
226 ibuf += sizeof( sindex );
229 memcpy( &temp16, ibuf, sizeof( temp16 ));
230 sindex = ntohs( temp16 );
231 ibuf += sizeof( temp16 );
236 return AFPERR_PARAM ;
240 memcpy( &maxsz, ibuf, sizeof( maxsz ));
241 maxsz = ntohl( maxsz );
242 ibuf += sizeof( maxsz );
245 memcpy( &temp16, ibuf, sizeof( temp16 ));
246 maxsz = ntohs( temp16 );
247 ibuf += sizeof( temp16 );
251 header *=sizeof( u_char );
253 maxsz = min(maxsz, *rbuflen - REPLY_PARAM_MAXLEN);
254 o_path = cname( vol, dir, &ibuf );
256 if (afp_errno == AFPERR_NOOBJ)
257 afp_errno = AFPERR_NODIR;
260 if (NULL == o_path ) {
261 return get_afp_errno(AFPERR_NOOBJ);
263 if ( *o_path->m_name != '\0') {
264 /* it's a file or it's a dir and extendir() was unable to chdir in it */
265 return path_error(o_path, AFPERR_NODIR );
268 data = rbuf + 3 * sizeof( u_int16_t );
269 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
272 * Read the directory into a pre-malloced buffer, stored
274 * The end is indicated by a len of 0.
276 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
277 sd.sd_last = sd.sd_buf;
278 /* if dir was in the cache we don't have the inode */
279 if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
280 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
284 return AFPERR_ACCESS;
286 return AFPERR_BADTYPE;
293 setdiroffcnt(curdir, &o_path->st, ret);
296 sd.sd_last = sd.sd_buf;
300 sd.sd_did = curdir->d_did;
304 * Position sd_last as dictated by sindex.
306 if ( sindex < sd.sd_sindex ) {
308 sd.sd_last = sd.sd_buf;
310 while ( sd.sd_sindex < sindex ) {
311 len = (unsigned char)*(sd.sd_last)++;
313 sd.sd_did = 0; /* invalidate sd struct to force re-read */
314 return( AFPERR_NOOBJ );
316 sd.sd_last += len + 1;
320 while (( len = (unsigned char)*(sd.sd_last)) != 0 ) {
322 * If we've got all we need, send it.
324 if ( actcnt == reqcnt ) {
329 * Save the start position, in case we exceed the buffer
330 * limitation, and have to back up one.
335 if (*sd.sd_last == 0) {
336 /* stat() already failed on this one */
337 sd.sd_last += len + 1;
340 memset(&s_path, 0, sizeof(s_path));
341 s_path.u_name = sd.sd_last;
342 if (of_stat( &s_path) < 0 ) {
344 * Somebody else plays with the dir, well it can be us with
348 /* so the next time it won't try to stat it again
349 * another solution would be to invalidate the cache with
350 * sd.sd_did = 0 but if it's not ENOENT error it will start again
353 sd.sd_last += len + 1;
354 curdir->offcnt--; /* a little lie */
358 sd.sd_last += len + 1;
359 s_path.m_name = NULL;
361 * If a fil/dir is not a dir, it's a file. This is slightly
362 * inaccurate, since that means /dev/null is a file, /dev/printer
365 if ( S_ISDIR(s_path.st.st_mode)) {
366 if ( dbitmap == 0 ) {
369 dir = dirsearch_byname(vol, curdir, s_path.u_name);
370 if (!dir && NULL == (dir = adddir( vol, curdir, &s_path) ) ) {
373 if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
374 data + header , &esz ))) {
379 if ( fbitmap == 0 ) {
382 if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
383 data + header , &esz )) ) {
389 * Make sure entry is an even length, possibly with a null
392 if ( (esz + header) & 1 ) {
393 *(data + header + esz ) = '\0';
398 * Check if we've exceeded the size limit.
400 if ( maxsz < sz + esz + header) {
401 if (first) { /* maxsz can't hold a single reply */
413 temp16 = htons( esz + header );
414 memcpy( data, &temp16, sizeof( temp16 ));
415 data += sizeof(temp16);
418 *data++ = esz + header;
421 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
427 /* FIXME if we rollover 16 bits and it's not FPEnumerateExt2 */
431 sd.sd_did = 0; /* invalidate sd struct to force re-read */
432 return( AFPERR_NOOBJ );
434 sd.sd_sindex = sindex + actcnt;
437 * All done, fill in misc junk in rbuf
439 fbitmap = htons( fbitmap );
440 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
441 rbuf += sizeof( fbitmap );
442 dbitmap = htons( dbitmap );
443 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
444 rbuf += sizeof( dbitmap );
445 actcnt = htons( actcnt );
446 memcpy( rbuf, &actcnt, sizeof( actcnt ));
447 rbuf += sizeof( actcnt );
452 /* ----------------------------- */
453 int afp_enumerate(AFPObj *obj, char *ibuf, unsigned int ibuflen,
455 unsigned int *rbuflen)
457 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
460 /* ----------------------------- */
461 int afp_enumerate_ext(AFPObj *obj, char *ibuf, unsigned int ibuflen,
463 unsigned int *rbuflen)
465 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
468 /* ----------------------------- */
469 int afp_enumerate_ext2(AFPObj *obj, char *ibuf, unsigned int ibuflen,
471 unsigned int *rbuflen)
473 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);