2 * $Id: enumerate.c,v 1.30 2003-01-12 14:39:59 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;
51 struct stat lst, *lstp;
52 #endif /* USE_LASTDID */
58 upathlen = strlen(upath);
59 if ((cdir = dirnew(name, upath)) == NULL) {
60 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
68 cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
69 upathlen, cdir->d_did);
70 /* Fail out if things go bad with CNID. */
71 if (cdir->d_did == CNID_INVALID) {
74 LOG(log_error, logtype_afpd, "adddir: Incorrect parameters passed to cnid_add");
84 if (cdir->d_did == 0) {
86 /* last way of doing DIDs */
87 cdir->d_did = htonl( vol->v_lastdid++ );
88 #else /* USE_LASTDID */
89 lstp = lstat(upath, &lst) < 0 ? st : &lst;
90 /* the old way of doing DIDs (default) */
91 cdir->d_did = htonl( CNID(lstp, 0) );
92 #endif /* USE_LASTDID */
95 if ((edir = dirinsert( vol, cdir ))) {
96 edir->d_m_name = cdir->d_m_name;
97 edir->d_u_name = cdir->d_u_name;
102 /* parent/child directories */
103 cdir->d_parent = dir;
104 dirchildadd(dir, cdir);
108 * Struct to save directory reading context in. Used to prevent
109 * O(n^2) searches on a directory.
117 unsigned int sd_sindex;
119 #define SDBUFBRK 1024
121 static int enumerate_loop(struct dirent *de, char *mname, void *data)
123 struct savedir *sd = data;
127 end = sd->sd_buf + sd->sd_buflen;
128 len = strlen(de->d_name);
129 *(sd->sd_last)++ = len;
131 if ( sd->sd_last + len + 2 > end ) {
135 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
136 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
142 sd->sd_buflen += SDBUFBRK;
143 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
144 end = sd->sd_buf + sd->sd_buflen;
147 memcpy( sd->sd_last, de->d_name, len + 1 );
148 sd->sd_last += len + 1;
152 /* -----------------------------
154 * Doesn't work with dangling symlink
156 * - Move a folder with a dangling symlink in the trash
158 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
159 * and the Mac doesn't try to call afp_delete!
161 * Another option for symlink
163 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
166 char *check_dirent(const struct vol *vol, char *name)
170 if (!strcmp(name, "..") || !strcmp(name, "."))
173 if (!(validupath(vol, name)))
176 /* check for vetoed filenames */
177 if (veto_file(vol->v_veto, name))
180 /* now check against too big a file */
181 if (strlen(m_name = utompath(vol, name)) > vol->max_filename)
187 /* ----------------------------- */
189 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
196 if (( dp = opendir( name)) == NULL ) {
200 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
201 if (!(m_name = check_dirent(vol, de->d_name)))
205 if (fn && fn(de,m_name, data) < 0) {
214 /* ----------------------------- */
215 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
218 unsigned int ibuflen, *rbuflen;
221 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
224 int did, ret, esz, len, first = 1;
226 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
228 u_int32_t sindex, maxsz, sz = 0;
233 if ( sd.sd_buflen == 0 ) {
234 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
235 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
239 sd.sd_buflen = SDBUFBRK;
244 memcpy( &vid, ibuf, sizeof( vid ));
245 ibuf += sizeof( vid );
247 if (NULL == ( vol = getvolbyvid( vid )) ) {
249 return( AFPERR_PARAM );
252 memcpy( &did, ibuf, sizeof( did ));
253 ibuf += sizeof( did );
255 if (NULL == ( dir = dirlookup( vol, did )) ) {
257 return( AFPERR_NODIR );
260 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
261 fbitmap = ntohs( fbitmap );
262 ibuf += sizeof( fbitmap );
264 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
265 dbitmap = ntohs( dbitmap );
266 ibuf += sizeof( dbitmap );
268 /* check for proper bitmaps -- the stuff in comments is for
269 * variable directory ids. */
270 if (!(fbitmap || dbitmap)
271 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
272 (dbitmap & (1 << DIRPBIT_PDID))*/) {
274 return AFPERR_BITMAP;
277 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
278 reqcnt = ntohs( reqcnt );
279 ibuf += sizeof( reqcnt );
282 memcpy( &sindex, ibuf, sizeof( sindex ));
283 sindex = ntohl( sindex );
284 ibuf += sizeof( sindex );
287 memcpy( &temp16, ibuf, sizeof( temp16 ));
288 sindex = ntohs( temp16 );
289 ibuf += sizeof( temp16 );
294 return AFPERR_PARAM ;
298 memcpy( &maxsz, ibuf, sizeof( maxsz ));
299 maxsz = ntohl( maxsz );
300 ibuf += sizeof( maxsz );
303 memcpy( &temp16, ibuf, sizeof( temp16 ));
304 maxsz = ntohs( temp16 );
305 ibuf += sizeof( temp16 );
309 header *=sizeof( u_char );
311 maxsz = min(maxsz, *rbuflen);
313 if (NULL == ( o_path = cname( vol, dir, &ibuf )) ) {
315 return( AFPERR_NODIR );
318 if ( *o_path->m_name != '\0') {
320 return( AFPERR_BADTYPE );
323 data = rbuf + 3 * sizeof( u_int16_t );
324 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
327 * Read the directory into a pre-malloced buffer, stored
329 * The end is indicated by a len of 0.
331 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
332 sd.sd_last = sd.sd_buf;
333 if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
336 return AFPERR_ACCESS;
338 return AFPERR_BADTYPE;
345 curdir->ctime = o_path->st.st_ctime; /* play safe */
346 if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
350 return AFPERR_ACCESS;
352 return AFPERR_BADTYPE;
359 curdir->offcnt = ret;
362 sd.sd_last = sd.sd_buf;
370 * Position sd_last as dictated by sindex.
372 if ( sindex < sd.sd_sindex ) {
374 sd.sd_last = sd.sd_buf;
376 while ( sd.sd_sindex < sindex ) {
377 len = *(sd.sd_last)++;
379 sd.sd_did = 0; /* invalidate sd struct to force re-read */
381 return( AFPERR_NOOBJ );
383 sd.sd_last += len + 1;
387 while (( len = *(sd.sd_last)) != 0 ) {
389 * If we've got all we need, send it.
391 if ( actcnt == reqcnt ) {
396 * Save the start position, in case we exceed the buffer
397 * limitation, and have to back up one.
402 if (*sd.sd_last == 0) {
403 /* stat() already failed on this one */
404 sd.sd_last += len + 1;
407 s_path.u_name = sd.sd_last;
408 if (of_stat( &s_path) < 0 ) {
410 * Somebody else plays with the dir, well it can be us with
414 /* so the next time it won't try to stat it again
415 * another solution would be to invalidate the cache with
416 * sd.sd_did = 0 but if it's not ENOENT error it will start again
419 sd.sd_last += len + 1;
420 curdir->offcnt--; /* a little lie */
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 ) {
431 sd.sd_last += len + 1;
434 dir = curdir->d_child;
435 s_path.m_name = NULL;
437 if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
440 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
443 s_path.m_name = utompath(vol, s_path.u_name);
444 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
450 if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
451 data + header , &esz )) != AFP_OK ) {
457 if ( fbitmap == 0 ) {
458 sd.sd_last += len + 1;
461 s_path.m_name = utompath(vol, s_path.u_name);
462 if (( ret = getfilparams(vol, fbitmap, &s_path, curdir,
463 data + header , &esz )) != AFP_OK ) {
470 * Make sure entry is an even length, possibly with a null
473 if ( (esz + header) & 1 ) {
474 *(data + header + esz ) = '\0';
479 * Check if we've exceeded the size limit.
481 if ( maxsz < sz + esz + header) {
482 if (first) { /* maxsz can't hold a single reply */
495 temp16 = htons( esz + header );
496 memcpy( data, &temp16, sizeof( temp16 ));
497 data += sizeof(temp16);
500 *data++ = esz + header;
503 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
509 sd.sd_last += len + 1;
514 sd.sd_did = 0; /* invalidate sd struct to force re-read */
515 return( AFPERR_NOOBJ );
517 sd.sd_sindex = sindex + actcnt;
520 * All done, fill in misc junk in rbuf
522 fbitmap = htons( fbitmap );
523 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
524 rbuf += sizeof( fbitmap );
525 dbitmap = htons( dbitmap );
526 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
527 rbuf += sizeof( dbitmap );
528 actcnt = htons( actcnt );
529 memcpy( rbuf, &actcnt, sizeof( actcnt ));
530 rbuf += sizeof( actcnt );
535 /* ----------------------------- */
536 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
539 unsigned int ibuflen, *rbuflen;
541 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
544 /* ----------------------------- */
545 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
548 unsigned int ibuflen, *rbuflen;
550 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
553 /* ----------------------------- */
554 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
557 unsigned int ibuflen, *rbuflen;
559 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);