2 * $Id: enumerate.c,v 1.28 2002-12-23 00:21:35 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 (( vol = getvolbyvid( vid )) == NULL ) {
249 return( AFPERR_PARAM );
252 memcpy( &did, ibuf, sizeof( did ));
253 ibuf += sizeof( did );
255 if (( dir = dirlookup( vol, did )) == NULL ) {
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 );
293 memcpy( &maxsz, ibuf, sizeof( maxsz ));
294 maxsz = ntohl( maxsz );
295 ibuf += sizeof( maxsz );
298 memcpy( &temp16, ibuf, sizeof( temp16 ));
299 maxsz = ntohs( temp16 );
300 ibuf += sizeof( temp16 );
304 header *=sizeof( u_char );
306 maxsz = min(maxsz, *rbuflen);
308 if (( o_path = cname( vol, dir, &ibuf )) == NULL) {
310 return( AFPERR_NODIR );
313 if ( *o_path->m_name != '\0') {
315 return( AFPERR_BADTYPE );
318 data = rbuf + 3 * sizeof( u_int16_t );
319 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
322 * Read the directory into a pre-malloced buffer, stored
324 * The end is indicated by a len of 0.
326 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
327 sd.sd_last = sd.sd_buf;
328 if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
331 return AFPERR_ACCESS;
333 return AFPERR_BADTYPE;
340 curdir->ctime = o_path->st.st_ctime; /* play safe */
341 if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
345 return AFPERR_ACCESS;
347 return AFPERR_BADTYPE;
354 curdir->offcnt = ret;
357 sd.sd_last = sd.sd_buf;
365 * Position sd_last as dictated by sindex.
367 if ( sindex < sd.sd_sindex ) {
369 sd.sd_last = sd.sd_buf;
371 while ( sd.sd_sindex < sindex ) {
372 len = *(sd.sd_last)++;
374 sd.sd_did = 0; /* invalidate sd struct to force re-read */
376 return( AFPERR_NOOBJ );
378 sd.sd_last += len + 1;
382 while (( len = *(sd.sd_last)) != 0 ) {
384 * If we've got all we need, send it.
386 if ( actcnt == reqcnt ) {
391 * Save the start position, in case we exceed the buffer
392 * limitation, and have to back up one.
397 if (*sd.sd_last == 0) {
398 /* stat() already failed on this one */
399 sd.sd_last += len + 1;
402 s_path.u_name = sd.sd_last;
403 if (of_stat( &s_path) < 0 ) {
405 * Somebody else plays with the dir, well it can be us with
409 /* so the next time it won't try to stat it again
410 * another solution would be to invalidate the cache with
411 * sd.sd_did = 0 but if it's not ENOENT error it will start again
414 sd.sd_last += len + 1;
415 curdir->offcnt--; /* a little lie */
420 * If a fil/dir is not a dir, it's a file. This is slightly
421 * inaccurate, since that means /dev/null is a file, /dev/printer
424 if ( S_ISDIR(s_path.st.st_mode)) {
425 if ( dbitmap == 0 ) {
426 sd.sd_last += len + 1;
429 dir = curdir->d_child;
430 s_path.m_name = NULL;
432 if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
435 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
438 s_path.m_name = utompath(vol, s_path.u_name);
439 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
445 if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
446 data + header , &esz )) != AFP_OK ) {
452 if ( fbitmap == 0 ) {
453 sd.sd_last += len + 1;
456 s_path.m_name = utompath(vol, s_path.u_name);
457 if (( ret = getfilparams(vol, fbitmap, &s_path, curdir,
458 data + header , &esz )) != AFP_OK ) {
465 * Make sure entry is an even length, possibly with a null
468 if ( (esz + header) & 1 ) {
469 *(data + header + esz ) = '\0';
474 * Check if we've exceeded the size limit.
476 if ( maxsz < sz + esz + header) {
477 if (first) { /* maxsz can't hold a single reply */
490 temp16 = htons( esz + header );
491 memcpy( data, &temp16, sizeof( temp16 ));
492 data += sizeof(temp16);
495 *data++ = esz + header;
498 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
504 sd.sd_last += len + 1;
509 sd.sd_did = 0; /* invalidate sd struct to force re-read */
510 return( AFPERR_NOOBJ );
512 sd.sd_sindex = sindex + actcnt;
515 * All done, fill in misc junk in rbuf
517 fbitmap = htons( fbitmap );
518 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
519 rbuf += sizeof( fbitmap );
520 dbitmap = htons( dbitmap );
521 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
522 rbuf += sizeof( dbitmap );
523 actcnt = htons( actcnt );
524 memcpy( rbuf, &actcnt, sizeof( actcnt ));
525 rbuf += sizeof( actcnt );
530 /* ----------------------------- */
531 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
534 unsigned int ibuflen, *rbuflen;
536 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
539 /* ----------------------------- */
540 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
543 unsigned int ibuflen, *rbuflen;
545 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
548 /* ----------------------------- */
549 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
552 unsigned int ibuflen, *rbuflen;
554 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);