2 * $Id: enumerate.c,v 1.33 2003-01-24 07:08:42 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 /* it's not possible with LASTDID
98 - someone else have moved the directory.
99 - it's a symlink inside the share.
100 - it's an ID reused, the old directory was deleted but not
101 the cnid record and the server've reused the inode for
103 for HASH (we should get ride of HASH)
104 - someone else have moved the directory.
105 - it's an ID reused as above
106 - it's a hash duplicate and we are in big trouble
108 edir->d_m_name = cdir->d_m_name;
109 edir->d_u_name = cdir->d_u_name;
112 if (!cdir->d_parent || cdir->d_parent == dir)
114 /* the old was not in the same folder */
115 dirchildremove(cdir->d_parent, cdir);
118 /* parent/child directories */
119 cdir->d_parent = dir;
120 dirchildadd(dir, cdir);
124 * Struct to save directory reading context in. Used to prevent
125 * O(n^2) searches on a directory.
133 unsigned int sd_sindex;
135 #define SDBUFBRK 1024
137 static int enumerate_loop(struct dirent *de, char *mname, void *data)
139 struct savedir *sd = data;
143 end = sd->sd_buf + sd->sd_buflen;
144 len = strlen(de->d_name);
145 *(sd->sd_last)++ = len;
147 if ( sd->sd_last + len + 2 > end ) {
151 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
152 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
158 sd->sd_buflen += SDBUFBRK;
159 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
160 end = sd->sd_buf + sd->sd_buflen;
163 memcpy( sd->sd_last, de->d_name, len + 1 );
164 sd->sd_last += len + 1;
168 /* -----------------------------
170 * Doesn't work with dangling symlink
172 * - Move a folder with a dangling symlink in the trash
174 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
175 * and the Mac doesn't try to call afp_delete!
177 * Another option for symlink
179 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
182 char *check_dirent(const struct vol *vol, char *name)
186 if (!strcmp(name, "..") || !strcmp(name, "."))
189 if (!(validupath(vol, name)))
192 /* check for vetoed filenames */
193 if (veto_file(vol->v_veto, name))
196 /* now check against too big a file */
197 if (strlen(m_name = utompath(vol, name)) > vol->max_filename)
203 /* ----------------------------- */
205 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
212 if (NULL == ( dp = opendir( name)) ) {
216 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
217 if (!(m_name = check_dirent(vol, de->d_name)))
221 if (fn && fn(de,m_name, data) < 0) {
230 /* ----------------------------- */
231 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
234 unsigned int ibuflen, *rbuflen;
237 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
240 int did, ret, esz, len, first = 1;
242 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
244 u_int32_t sindex, maxsz, sz = 0;
249 if ( sd.sd_buflen == 0 ) {
250 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
251 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
255 sd.sd_buflen = SDBUFBRK;
260 memcpy( &vid, ibuf, sizeof( vid ));
261 ibuf += sizeof( vid );
263 if (NULL == ( vol = getvolbyvid( vid )) ) {
265 return( AFPERR_PARAM );
268 memcpy( &did, ibuf, sizeof( did ));
269 ibuf += sizeof( did );
271 if (NULL == ( dir = dirlookup( vol, did )) ) {
273 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
276 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
277 fbitmap = ntohs( fbitmap );
278 ibuf += sizeof( fbitmap );
280 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
281 dbitmap = ntohs( dbitmap );
282 ibuf += sizeof( dbitmap );
284 /* check for proper bitmaps -- the stuff in comments is for
285 * variable directory ids. */
286 if (!(fbitmap || dbitmap)
287 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
288 (dbitmap & (1 << DIRPBIT_PDID))*/) {
290 return AFPERR_BITMAP;
293 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
294 reqcnt = ntohs( reqcnt );
295 ibuf += sizeof( reqcnt );
298 memcpy( &sindex, ibuf, sizeof( sindex ));
299 sindex = ntohl( sindex );
300 ibuf += sizeof( sindex );
303 memcpy( &temp16, ibuf, sizeof( temp16 ));
304 sindex = ntohs( temp16 );
305 ibuf += sizeof( temp16 );
310 return AFPERR_PARAM ;
314 memcpy( &maxsz, ibuf, sizeof( maxsz ));
315 maxsz = ntohl( maxsz );
316 ibuf += sizeof( maxsz );
319 memcpy( &temp16, ibuf, sizeof( temp16 ));
320 maxsz = ntohs( temp16 );
321 ibuf += sizeof( temp16 );
325 header *=sizeof( u_char );
327 maxsz = min(maxsz, *rbuflen);
328 o_path = cname( vol, dir, &ibuf );
330 if (afp_errno == AFPERR_NOOBJ)
331 afp_errno = AFPERR_NODIR;
334 if (NULL == o_path ) {
337 if ( *o_path->m_name != '\0') {
338 /* it's a file or it's a dir and extendir() was unable to chdir in it */
339 return (path_isadir(o_path))? afp_errno:AFPERR_BADTYPE ;
342 data = rbuf + 3 * sizeof( u_int16_t );
343 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
346 * Read the directory into a pre-malloced buffer, stored
348 * The end is indicated by a len of 0.
350 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
351 sd.sd_last = sd.sd_buf;
352 /* if dir was in the cache we don't have the inode */
353 if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
354 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
358 return AFPERR_ACCESS;
360 return AFPERR_BADTYPE;
367 curdir->ctime = o_path->st.st_ctime; /* play safe */
368 curdir->offcnt = ret;
371 sd.sd_last = sd.sd_buf;
379 * Position sd_last as dictated by sindex.
381 if ( sindex < sd.sd_sindex ) {
383 sd.sd_last = sd.sd_buf;
385 while ( sd.sd_sindex < sindex ) {
386 len = *(sd.sd_last)++;
388 sd.sd_did = 0; /* invalidate sd struct to force re-read */
389 return( AFPERR_NOOBJ );
391 sd.sd_last += len + 1;
395 while (( len = *(sd.sd_last)) != 0 ) {
397 * If we've got all we need, send it.
399 if ( actcnt == reqcnt ) {
404 * Save the start position, in case we exceed the buffer
405 * limitation, and have to back up one.
410 if (*sd.sd_last == 0) {
411 /* stat() already failed on this one */
412 sd.sd_last += len + 1;
415 s_path.u_name = sd.sd_last;
416 if (of_stat( &s_path) < 0 ) {
418 * Somebody else plays with the dir, well it can be us with
422 /* so the next time it won't try to stat it again
423 * another solution would be to invalidate the cache with
424 * sd.sd_did = 0 but if it's not ENOENT error it will start again
427 sd.sd_last += len + 1;
428 curdir->offcnt--; /* a little lie */
433 * If a fil/dir is not a dir, it's a file. This is slightly
434 * inaccurate, since that means /dev/null is a file, /dev/printer
437 if ( S_ISDIR(s_path.st.st_mode)) {
438 if ( dbitmap == 0 ) {
439 sd.sd_last += len + 1;
442 dir = dirsearch_byname(curdir, s_path.u_name);
444 s_path.m_name = utompath(vol, s_path.u_name);
445 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
450 s_path.m_name = NULL;
452 if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
453 data + header , &esz )) != AFP_OK ) {
458 if ( fbitmap == 0 ) {
459 sd.sd_last += len + 1;
462 s_path.m_name = utompath(vol, s_path.u_name);
463 if (( ret = getfilparams(vol, fbitmap, &s_path, curdir,
464 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 */
494 temp16 = htons( esz + header );
495 memcpy( data, &temp16, sizeof( temp16 ));
496 data += sizeof(temp16);
499 *data++ = esz + header;
502 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
508 sd.sd_last += len + 1;
512 sd.sd_did = 0; /* invalidate sd struct to force re-read */
513 return( AFPERR_NOOBJ );
515 sd.sd_sindex = sindex + actcnt;
518 * All done, fill in misc junk in rbuf
520 fbitmap = htons( fbitmap );
521 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
522 rbuf += sizeof( fbitmap );
523 dbitmap = htons( dbitmap );
524 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
525 rbuf += sizeof( dbitmap );
526 actcnt = htons( actcnt );
527 memcpy( rbuf, &actcnt, sizeof( actcnt ));
528 rbuf += sizeof( actcnt );
533 /* ----------------------------- */
534 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
537 unsigned int ibuflen, *rbuflen;
539 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
542 /* ----------------------------- */
543 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
546 unsigned int ibuflen, *rbuflen;
548 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
551 /* ----------------------------- */
552 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
555 unsigned int ibuflen, *rbuflen;
557 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);