2 * $Id: enumerate.c,v 1.38 2003-04-16 06:55:44 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 <atalk/logger.h>
18 #include <sys/types.h>
21 #include <sys/param.h>
23 #include <netatalk/endian.h>
24 #include <atalk/afp.h>
25 #include <atalk/adouble.h>
27 #include <atalk/cnid.h>
30 #include "directory.h"
37 #define min(a,b) ((a)<(b)?(a):(b))
39 /* ---------------------------- */
41 adddir( vol, dir, path)
46 struct dir *cdir, *edir;
56 upathlen = strlen(upath);
57 if ((cdir = dirnew(name, upath)) == NULL) {
58 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
62 cdir->d_did = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
66 if ((edir = dirinsert( vol, cdir ))) {
67 /* it's not possible with LASTDID
69 - someone else have moved the directory.
70 - it's a symlink inside the share.
71 - it's an ID reused, the old directory was deleted but not
72 the cnid record and the server've reused the inode for
74 for HASH (we should get ride of HASH)
75 - someone else have moved the directory.
76 - it's an ID reused as above
77 - it's a hash duplicate and we are in big trouble
79 deleted = (edir->d_m_name == NULL);
81 edir->d_m_name = cdir->d_m_name;
82 edir->d_u_name = cdir->d_u_name;
85 if (!cdir->d_parent || (cdir->d_parent == dir && !deleted))
87 /* the old was not in the same folder */
89 dirchildremove(cdir->d_parent, cdir);
92 /* parent/child directories */
94 dirchildadd(dir, cdir);
98 * Struct to save directory reading context in. Used to prevent
99 * O(n^2) searches on a directory.
107 unsigned int sd_sindex;
109 #define SDBUFBRK 2048
111 static int enumerate_loop(struct dirent *de, char *mname, void *data)
113 struct savedir *sd = data;
117 end = sd->sd_buf + sd->sd_buflen;
118 len = strlen(de->d_name);
119 *(sd->sd_last)++ = len;
120 lenm = strlen(mname);
122 if ( sd->sd_last + len +lenm + 4 > end ) {
126 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
127 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
133 sd->sd_buflen += SDBUFBRK;
134 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
135 end = sd->sd_buf + sd->sd_buflen;
138 memcpy( sd->sd_last, de->d_name, len + 1 );
139 sd->sd_last += len + 1;
141 *(sd->sd_last)++ = lenm;
142 memcpy( sd->sd_last, mname, lenm + 1 );
143 sd->sd_last += lenm + 1;
148 /* -----------------------------
150 * Doesn't work with dangling symlink
152 * - Move a folder with a dangling symlink in the trash
154 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
155 * and the Mac doesn't try to call afp_delete!
157 * Another option for symlink
159 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
162 char *check_dirent(const struct vol *vol, char *name)
166 if (!strcmp(name, "..") || !strcmp(name, "."))
169 if (!(validupath(vol, name)))
172 /* check for vetoed filenames */
173 if (veto_file(vol->v_veto, name))
175 if (NULL == (m_name = utompath(vol, name, utf8_encoding())))
178 /* now check against too big a file */
179 if (strlen(m_name) > vol->max_filename)
185 /* ----------------------------- */
187 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
194 if (NULL == ( dp = opendir( name)) ) {
198 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
199 if (!(m_name = check_dirent(vol, de->d_name)))
203 if (fn && fn(de,m_name, data) < 0) {
212 /* ----------------------------- */
213 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
216 unsigned int ibuflen, *rbuflen;
219 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
222 int did, ret, esz, len, first = 1;
224 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
226 u_int32_t sindex, maxsz, sz = 0;
231 if ( sd.sd_buflen == 0 ) {
232 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
233 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
237 sd.sd_buflen = SDBUFBRK;
242 memcpy( &vid, ibuf, sizeof( vid ));
243 ibuf += sizeof( vid );
245 if (NULL == ( vol = getvolbyvid( vid )) ) {
247 return( AFPERR_PARAM );
250 memcpy( &did, ibuf, sizeof( did ));
251 ibuf += sizeof( did );
253 if (NULL == ( dir = dirlookup( vol, did )) ) {
255 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
258 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
259 fbitmap = ntohs( fbitmap );
260 ibuf += sizeof( fbitmap );
262 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
263 dbitmap = ntohs( dbitmap );
264 ibuf += sizeof( dbitmap );
266 /* check for proper bitmaps -- the stuff in comments is for
267 * variable directory ids. */
268 if (!(fbitmap || dbitmap)
269 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
270 (dbitmap & (1 << DIRPBIT_PDID))*/) {
272 return AFPERR_BITMAP;
275 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
276 reqcnt = ntohs( reqcnt );
277 ibuf += sizeof( reqcnt );
280 memcpy( &sindex, ibuf, sizeof( sindex ));
281 sindex = ntohl( sindex );
282 ibuf += sizeof( sindex );
285 memcpy( &temp16, ibuf, sizeof( temp16 ));
286 sindex = ntohs( temp16 );
287 ibuf += sizeof( temp16 );
292 return AFPERR_PARAM ;
296 memcpy( &maxsz, ibuf, sizeof( maxsz ));
297 maxsz = ntohl( maxsz );
298 ibuf += sizeof( maxsz );
301 memcpy( &temp16, ibuf, sizeof( temp16 ));
302 maxsz = ntohs( temp16 );
303 ibuf += sizeof( temp16 );
307 header *=sizeof( u_char );
309 maxsz = min(maxsz, *rbuflen);
310 o_path = cname( vol, dir, &ibuf );
312 if (afp_errno == AFPERR_NOOBJ)
313 afp_errno = AFPERR_NODIR;
316 if (NULL == o_path ) {
317 return get_afp_errno(AFPERR_NOOBJ);
319 if ( *o_path->m_name != '\0') {
320 /* it's a file or it's a dir and extendir() was unable to chdir in it */
321 return (path_isadir(o_path))? afp_errno:AFPERR_BADTYPE ;
324 data = rbuf + 3 * sizeof( u_int16_t );
325 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
328 * Read the directory into a pre-malloced buffer, stored
330 * The end is indicated by a len of 0.
332 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
333 sd.sd_last = sd.sd_buf;
334 /* if dir was in the cache we don't have the inode */
335 if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
336 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
340 return AFPERR_ACCESS;
342 return AFPERR_BADTYPE;
349 curdir->ctime = o_path->st.st_ctime; /* play safe */
350 curdir->offcnt = ret;
353 sd.sd_last = sd.sd_buf;
361 * Position sd_last as dictated by sindex.
363 if ( sindex < sd.sd_sindex ) {
365 sd.sd_last = sd.sd_buf;
367 while ( sd.sd_sindex < sindex ) {
368 len = *(sd.sd_last)++;
370 sd.sd_did = 0; /* invalidate sd struct to force re-read */
371 return( AFPERR_NOOBJ );
373 sd.sd_last += len + 1;
374 len = *(sd.sd_last)++;
375 sd.sd_last += len + 1;
379 while (( len = *(sd.sd_last)) != 0 ) {
381 * If we've got all we need, send it.
383 if ( actcnt == reqcnt ) {
388 * Save the start position, in case we exceed the buffer
389 * limitation, and have to back up one.
394 if (*sd.sd_last == 0) {
395 /* stat() already failed on this one */
396 sd.sd_last += len + 1;
397 len = *(sd.sd_last)++;
398 sd.sd_last += len + 1;
401 s_path.u_name = sd.sd_last;
402 if (of_stat( &s_path) < 0 ) {
404 * Somebody else plays with the dir, well it can be us with
408 /* so the next time it won't try to stat it again
409 * another solution would be to invalidate the cache with
410 * sd.sd_did = 0 but if it's not ENOENT error it will start again
413 sd.sd_last += len + 1;
414 len = *(sd.sd_last)++;
415 sd.sd_last += len + 1;
416 curdir->offcnt--; /* a little lie */
420 sd.sd_last += len + 1;
421 len = *(sd.sd_last)++;
422 s_path.m_name = sd.sd_last;
423 sd.sd_last += len + 1;
425 * If a fil/dir is not a dir, it's a file. This is slightly
426 * inaccurate, since that means /dev/null is a file, /dev/printer
429 if ( S_ISDIR(s_path.st.st_mode)) {
430 if ( dbitmap == 0 ) {
433 dir = dirsearch_byname(curdir, s_path.u_name);
435 if (s_path.m_name == NULL || (dir = adddir( vol, curdir, &s_path)) == NULL) {
440 s_path.m_name = NULL;
442 if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
443 data + header , &esz ))) {
448 if ( fbitmap == 0 ) {
451 if (s_path.m_name == NULL ) {
454 if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
455 data + header , &esz )) ) {
461 * Make sure entry is an even length, possibly with a null
464 if ( (esz + header) & 1 ) {
465 *(data + header + esz ) = '\0';
470 * Check if we've exceeded the size limit.
472 if ( maxsz < sz + esz + header) {
473 if (first) { /* maxsz can't hold a single reply */
485 temp16 = htons( esz + header );
486 memcpy( data, &temp16, sizeof( temp16 ));
487 data += sizeof(temp16);
490 *data++ = esz + header;
493 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
502 sd.sd_did = 0; /* invalidate sd struct to force re-read */
503 return( AFPERR_NOOBJ );
505 sd.sd_sindex = sindex + actcnt;
508 * All done, fill in misc junk in rbuf
510 fbitmap = htons( fbitmap );
511 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
512 rbuf += sizeof( fbitmap );
513 dbitmap = htons( dbitmap );
514 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
515 rbuf += sizeof( dbitmap );
516 actcnt = htons( actcnt );
517 memcpy( rbuf, &actcnt, sizeof( actcnt ));
518 rbuf += sizeof( actcnt );
523 /* ----------------------------- */
524 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
527 unsigned int ibuflen, *rbuflen;
529 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
532 /* ----------------------------- */
533 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
536 unsigned int ibuflen, *rbuflen;
538 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
541 /* ----------------------------- */
542 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
545 unsigned int ibuflen, *rbuflen;
547 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);