2 * $Id: enumerate.c,v 1.37 2003-03-15 01:34: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;
55 upathlen = strlen(upath);
56 if ((cdir = dirnew(name, upath)) == NULL) {
57 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
61 cdir->d_did = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
65 if ((edir = dirinsert( vol, cdir ))) {
66 /* it's not possible with LASTDID
68 - someone else have moved the directory.
69 - it's a symlink inside the share.
70 - it's an ID reused, the old directory was deleted but not
71 the cnid record and the server've reused the inode for
73 for HASH (we should get ride of HASH)
74 - someone else have moved the directory.
75 - it's an ID reused as above
76 - it's a hash duplicate and we are in big trouble
79 edir->d_m_name = cdir->d_m_name;
80 edir->d_u_name = cdir->d_u_name;
83 if (!cdir->d_parent || cdir->d_parent == dir)
85 /* the old was not in the same folder */
86 dirchildremove(cdir->d_parent, cdir);
89 /* parent/child directories */
91 dirchildadd(dir, cdir);
95 * Struct to save directory reading context in. Used to prevent
96 * O(n^2) searches on a directory.
104 unsigned int sd_sindex;
106 #define SDBUFBRK 2048
108 static int enumerate_loop(struct dirent *de, char *mname, void *data)
110 struct savedir *sd = data;
114 end = sd->sd_buf + sd->sd_buflen;
115 len = strlen(de->d_name);
116 *(sd->sd_last)++ = len;
117 lenm = strlen(mname);
119 if ( sd->sd_last + len +lenm + 4 > end ) {
123 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
124 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
130 sd->sd_buflen += SDBUFBRK;
131 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
132 end = sd->sd_buf + sd->sd_buflen;
135 memcpy( sd->sd_last, de->d_name, len + 1 );
136 sd->sd_last += len + 1;
138 *(sd->sd_last)++ = lenm;
139 memcpy( sd->sd_last, mname, lenm + 1 );
140 sd->sd_last += lenm + 1;
145 /* -----------------------------
147 * Doesn't work with dangling symlink
149 * - Move a folder with a dangling symlink in the trash
151 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
152 * and the Mac doesn't try to call afp_delete!
154 * Another option for symlink
156 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
159 char *check_dirent(const struct vol *vol, char *name)
163 if (!strcmp(name, "..") || !strcmp(name, "."))
166 if (!(validupath(vol, name)))
169 /* check for vetoed filenames */
170 if (veto_file(vol->v_veto, name))
172 if (NULL == (m_name = utompath(vol, name, utf8_encoding())))
175 /* now check against too big a file */
176 if (strlen(m_name) > vol->max_filename)
182 /* ----------------------------- */
184 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
191 if (NULL == ( dp = opendir( name)) ) {
195 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
196 if (!(m_name = check_dirent(vol, de->d_name)))
200 if (fn && fn(de,m_name, data) < 0) {
209 /* ----------------------------- */
210 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
213 unsigned int ibuflen, *rbuflen;
216 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
219 int did, ret, esz, len, first = 1;
221 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
223 u_int32_t sindex, maxsz, sz = 0;
228 if ( sd.sd_buflen == 0 ) {
229 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
230 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
234 sd.sd_buflen = SDBUFBRK;
239 memcpy( &vid, ibuf, sizeof( vid ));
240 ibuf += sizeof( vid );
242 if (NULL == ( vol = getvolbyvid( vid )) ) {
244 return( AFPERR_PARAM );
247 memcpy( &did, ibuf, sizeof( did ));
248 ibuf += sizeof( did );
250 if (NULL == ( dir = dirlookup( vol, did )) ) {
252 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
255 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
256 fbitmap = ntohs( fbitmap );
257 ibuf += sizeof( fbitmap );
259 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
260 dbitmap = ntohs( dbitmap );
261 ibuf += sizeof( dbitmap );
263 /* check for proper bitmaps -- the stuff in comments is for
264 * variable directory ids. */
265 if (!(fbitmap || dbitmap)
266 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
267 (dbitmap & (1 << DIRPBIT_PDID))*/) {
269 return AFPERR_BITMAP;
272 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
273 reqcnt = ntohs( reqcnt );
274 ibuf += sizeof( reqcnt );
277 memcpy( &sindex, ibuf, sizeof( sindex ));
278 sindex = ntohl( sindex );
279 ibuf += sizeof( sindex );
282 memcpy( &temp16, ibuf, sizeof( temp16 ));
283 sindex = ntohs( temp16 );
284 ibuf += sizeof( temp16 );
289 return AFPERR_PARAM ;
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);
307 o_path = cname( vol, dir, &ibuf );
309 if (afp_errno == AFPERR_NOOBJ)
310 afp_errno = AFPERR_NODIR;
313 if (NULL == o_path ) {
314 return get_afp_errno(AFPERR_NOOBJ);
316 if ( *o_path->m_name != '\0') {
317 /* it's a file or it's a dir and extendir() was unable to chdir in it */
318 return (path_isadir(o_path))? afp_errno:AFPERR_BADTYPE ;
321 data = rbuf + 3 * sizeof( u_int16_t );
322 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
325 * Read the directory into a pre-malloced buffer, stored
327 * The end is indicated by a len of 0.
329 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
330 sd.sd_last = sd.sd_buf;
331 /* if dir was in the cache we don't have the inode */
332 if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
333 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
337 return AFPERR_ACCESS;
339 return AFPERR_BADTYPE;
346 curdir->ctime = o_path->st.st_ctime; /* play safe */
347 curdir->offcnt = ret;
350 sd.sd_last = sd.sd_buf;
358 * Position sd_last as dictated by sindex.
360 if ( sindex < sd.sd_sindex ) {
362 sd.sd_last = sd.sd_buf;
364 while ( sd.sd_sindex < sindex ) {
365 len = *(sd.sd_last)++;
367 sd.sd_did = 0; /* invalidate sd struct to force re-read */
368 return( AFPERR_NOOBJ );
370 sd.sd_last += len + 1;
371 len = *(sd.sd_last)++;
372 sd.sd_last += len + 1;
376 while (( len = *(sd.sd_last)) != 0 ) {
378 * If we've got all we need, send it.
380 if ( actcnt == reqcnt ) {
385 * Save the start position, in case we exceed the buffer
386 * limitation, and have to back up one.
391 if (*sd.sd_last == 0) {
392 /* stat() already failed on this one */
393 sd.sd_last += len + 1;
394 len = *(sd.sd_last)++;
395 sd.sd_last += len + 1;
398 s_path.u_name = sd.sd_last;
399 if (of_stat( &s_path) < 0 ) {
401 * Somebody else plays with the dir, well it can be us with
405 /* so the next time it won't try to stat it again
406 * another solution would be to invalidate the cache with
407 * sd.sd_did = 0 but if it's not ENOENT error it will start again
410 sd.sd_last += len + 1;
411 len = *(sd.sd_last)++;
412 sd.sd_last += len + 1;
413 curdir->offcnt--; /* a little lie */
417 sd.sd_last += len + 1;
418 len = *(sd.sd_last)++;
419 s_path.m_name = sd.sd_last;
420 sd.sd_last += len + 1;
422 * If a fil/dir is not a dir, it's a file. This is slightly
423 * inaccurate, since that means /dev/null is a file, /dev/printer
426 if ( S_ISDIR(s_path.st.st_mode)) {
427 if ( dbitmap == 0 ) {
430 dir = dirsearch_byname(curdir, s_path.u_name);
432 if (s_path.m_name == NULL || (dir = adddir( vol, curdir, &s_path)) == NULL) {
437 s_path.m_name = NULL;
439 if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
440 data + header , &esz ))) {
445 if ( fbitmap == 0 ) {
448 if (s_path.m_name == NULL ) {
451 if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
452 data + header , &esz )) ) {
458 * Make sure entry is an even length, possibly with a null
461 if ( (esz + header) & 1 ) {
462 *(data + header + esz ) = '\0';
467 * Check if we've exceeded the size limit.
469 if ( maxsz < sz + esz + header) {
470 if (first) { /* maxsz can't hold a single reply */
482 temp16 = htons( esz + header );
483 memcpy( data, &temp16, sizeof( temp16 ));
484 data += sizeof(temp16);
487 *data++ = esz + header;
490 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
499 sd.sd_did = 0; /* invalidate sd struct to force re-read */
500 return( AFPERR_NOOBJ );
502 sd.sd_sindex = sindex + actcnt;
505 * All done, fill in misc junk in rbuf
507 fbitmap = htons( fbitmap );
508 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
509 rbuf += sizeof( fbitmap );
510 dbitmap = htons( dbitmap );
511 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
512 rbuf += sizeof( dbitmap );
513 actcnt = htons( actcnt );
514 memcpy( rbuf, &actcnt, sizeof( actcnt ));
515 rbuf += sizeof( actcnt );
520 /* ----------------------------- */
521 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
524 unsigned int ibuflen, *rbuflen;
526 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
529 /* ----------------------------- */
530 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
533 unsigned int ibuflen, *rbuflen;
535 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
538 /* ----------------------------- */
539 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
542 unsigned int ibuflen, *rbuflen;
544 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);