2 * $Id: enumerate.c,v 1.34 2003-02-16 12:35:04 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) {
73 case CNID_ERR_CLOSE: /* the db is closed */
76 LOG(log_error, logtype_afpd, "adddir: Incorrect parameters passed to cnid_add");
86 if (cdir->d_did == 0) {
88 /* last way of doing DIDs */
89 cdir->d_did = htonl( vol->v_lastdid++ );
90 #else /* USE_LASTDID */
91 lstp = lstat(upath, &lst) < 0 ? st : &lst;
92 /* the old way of doing DIDs (default) */
93 cdir->d_did = htonl( CNID(lstp, 0) );
94 #endif /* USE_LASTDID */
97 if ((edir = dirinsert( vol, cdir ))) {
98 /* it's not possible with LASTDID
100 - someone else have moved the directory.
101 - it's a symlink inside the share.
102 - it's an ID reused, the old directory was deleted but not
103 the cnid record and the server've reused the inode for
105 for HASH (we should get ride of HASH)
106 - someone else have moved the directory.
107 - it's an ID reused as above
108 - it's a hash duplicate and we are in big trouble
111 edir->d_m_name = cdir->d_m_name;
112 edir->d_u_name = cdir->d_u_name;
115 if (!cdir->d_parent || cdir->d_parent == dir)
117 /* the old was not in the same folder */
118 dirchildremove(cdir->d_parent, cdir);
121 /* parent/child directories */
122 cdir->d_parent = dir;
123 dirchildadd(dir, cdir);
127 * Struct to save directory reading context in. Used to prevent
128 * O(n^2) searches on a directory.
136 unsigned int sd_sindex;
138 #define SDBUFBRK 1024
140 static int enumerate_loop(struct dirent *de, char *mname, void *data)
142 struct savedir *sd = data;
146 end = sd->sd_buf + sd->sd_buflen;
147 len = strlen(de->d_name);
148 *(sd->sd_last)++ = len;
150 if ( sd->sd_last + len + 2 > end ) {
154 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
155 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
161 sd->sd_buflen += SDBUFBRK;
162 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
163 end = sd->sd_buf + sd->sd_buflen;
166 memcpy( sd->sd_last, de->d_name, len + 1 );
167 sd->sd_last += len + 1;
171 /* -----------------------------
173 * Doesn't work with dangling symlink
175 * - Move a folder with a dangling symlink in the trash
177 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
178 * and the Mac doesn't try to call afp_delete!
180 * Another option for symlink
182 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
185 char *check_dirent(const struct vol *vol, char *name)
189 if (!strcmp(name, "..") || !strcmp(name, "."))
192 if (!(validupath(vol, name)))
195 /* check for vetoed filenames */
196 if (veto_file(vol->v_veto, name))
199 /* now check against too big a file */
200 if (strlen(m_name = utompath(vol, name)) > vol->max_filename)
206 /* ----------------------------- */
208 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
215 if (NULL == ( dp = opendir( name)) ) {
219 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
220 if (!(m_name = check_dirent(vol, de->d_name)))
224 if (fn && fn(de,m_name, data) < 0) {
233 /* ----------------------------- */
234 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
237 unsigned int ibuflen, *rbuflen;
240 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
243 int did, ret, esz, len, first = 1;
245 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
247 u_int32_t sindex, maxsz, sz = 0;
252 if ( sd.sd_buflen == 0 ) {
253 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
254 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
258 sd.sd_buflen = SDBUFBRK;
263 memcpy( &vid, ibuf, sizeof( vid ));
264 ibuf += sizeof( vid );
266 if (NULL == ( vol = getvolbyvid( vid )) ) {
268 return( AFPERR_PARAM );
271 memcpy( &did, ibuf, sizeof( did ));
272 ibuf += sizeof( did );
274 if (NULL == ( dir = dirlookup( vol, did )) ) {
276 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
279 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
280 fbitmap = ntohs( fbitmap );
281 ibuf += sizeof( fbitmap );
283 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
284 dbitmap = ntohs( dbitmap );
285 ibuf += sizeof( dbitmap );
287 /* check for proper bitmaps -- the stuff in comments is for
288 * variable directory ids. */
289 if (!(fbitmap || dbitmap)
290 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
291 (dbitmap & (1 << DIRPBIT_PDID))*/) {
293 return AFPERR_BITMAP;
296 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
297 reqcnt = ntohs( reqcnt );
298 ibuf += sizeof( reqcnt );
301 memcpy( &sindex, ibuf, sizeof( sindex ));
302 sindex = ntohl( sindex );
303 ibuf += sizeof( sindex );
306 memcpy( &temp16, ibuf, sizeof( temp16 ));
307 sindex = ntohs( temp16 );
308 ibuf += sizeof( temp16 );
313 return AFPERR_PARAM ;
317 memcpy( &maxsz, ibuf, sizeof( maxsz ));
318 maxsz = ntohl( maxsz );
319 ibuf += sizeof( maxsz );
322 memcpy( &temp16, ibuf, sizeof( temp16 ));
323 maxsz = ntohs( temp16 );
324 ibuf += sizeof( temp16 );
328 header *=sizeof( u_char );
330 maxsz = min(maxsz, *rbuflen);
331 o_path = cname( vol, dir, &ibuf );
333 if (afp_errno == AFPERR_NOOBJ)
334 afp_errno = AFPERR_NODIR;
337 if (NULL == o_path ) {
340 if ( *o_path->m_name != '\0') {
341 /* it's a file or it's a dir and extendir() was unable to chdir in it */
342 return (path_isadir(o_path))? afp_errno:AFPERR_BADTYPE ;
345 data = rbuf + 3 * sizeof( u_int16_t );
346 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
349 * Read the directory into a pre-malloced buffer, stored
351 * The end is indicated by a len of 0.
353 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
354 sd.sd_last = sd.sd_buf;
355 /* if dir was in the cache we don't have the inode */
356 if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
357 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
361 return AFPERR_ACCESS;
363 return AFPERR_BADTYPE;
370 curdir->ctime = o_path->st.st_ctime; /* play safe */
371 curdir->offcnt = ret;
374 sd.sd_last = sd.sd_buf;
382 * Position sd_last as dictated by sindex.
384 if ( sindex < sd.sd_sindex ) {
386 sd.sd_last = sd.sd_buf;
388 while ( sd.sd_sindex < sindex ) {
389 len = *(sd.sd_last)++;
391 sd.sd_did = 0; /* invalidate sd struct to force re-read */
392 return( AFPERR_NOOBJ );
394 sd.sd_last += len + 1;
398 while (( len = *(sd.sd_last)) != 0 ) {
400 * If we've got all we need, send it.
402 if ( actcnt == reqcnt ) {
407 * Save the start position, in case we exceed the buffer
408 * limitation, and have to back up one.
413 if (*sd.sd_last == 0) {
414 /* stat() already failed on this one */
415 sd.sd_last += len + 1;
418 s_path.u_name = sd.sd_last;
419 if (of_stat( &s_path) < 0 ) {
421 * Somebody else plays with the dir, well it can be us with
425 /* so the next time it won't try to stat it again
426 * another solution would be to invalidate the cache with
427 * sd.sd_did = 0 but if it's not ENOENT error it will start again
430 sd.sd_last += len + 1;
431 curdir->offcnt--; /* a little lie */
436 * If a fil/dir is not a dir, it's a file. This is slightly
437 * inaccurate, since that means /dev/null is a file, /dev/printer
440 if ( S_ISDIR(s_path.st.st_mode)) {
441 if ( dbitmap == 0 ) {
442 sd.sd_last += len + 1;
445 dir = dirsearch_byname(curdir, s_path.u_name);
447 s_path.m_name = utompath(vol, s_path.u_name);
448 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
453 s_path.m_name = NULL;
455 if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
456 data + header , &esz ))) {
461 if ( fbitmap == 0 ) {
462 sd.sd_last += len + 1;
465 s_path.m_name = utompath(vol, s_path.u_name);
466 if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
467 data + header , &esz )) ) {
473 * Make sure entry is an even length, possibly with a null
476 if ( (esz + header) & 1 ) {
477 *(data + header + esz ) = '\0';
482 * Check if we've exceeded the size limit.
484 if ( maxsz < sz + esz + header) {
485 if (first) { /* maxsz can't hold a single reply */
497 temp16 = htons( esz + header );
498 memcpy( data, &temp16, sizeof( temp16 ));
499 data += sizeof(temp16);
502 *data++ = esz + header;
505 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
511 sd.sd_last += len + 1;
515 sd.sd_did = 0; /* invalidate sd struct to force re-read */
516 return( AFPERR_NOOBJ );
518 sd.sd_sindex = sindex + actcnt;
521 * All done, fill in misc junk in rbuf
523 fbitmap = htons( fbitmap );
524 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
525 rbuf += sizeof( fbitmap );
526 dbitmap = htons( dbitmap );
527 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
528 rbuf += sizeof( dbitmap );
529 actcnt = htons( actcnt );
530 memcpy( rbuf, &actcnt, sizeof( actcnt ));
531 rbuf += sizeof( actcnt );
536 /* ----------------------------- */
537 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
540 unsigned int ibuflen, *rbuflen;
542 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
545 /* ----------------------------- */
546 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
549 unsigned int ibuflen, *rbuflen;
551 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
554 /* ----------------------------- */
555 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
558 unsigned int ibuflen, *rbuflen;
560 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);