2 * $Id: enumerate.c,v 1.40 2003-05-16 14:38:20 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;
56 upathlen = strlen(upath);
57 if ((cdir = dirnew(name, upath)) == NULL) {
58 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
62 cdir->d_did = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
66 if ((edir = dirinsert( vol, cdir ))) {
67 /* it's not possible with LASTDID
69 - someone else have moved the directory.
70 - it's a symlink inside the share.
71 - it's an ID reused, the old directory was deleted but not
72 the cnid record and the server've reused the inode for
74 for HASH (we should get ride of HASH)
75 - someone else have moved the directory.
76 - it's an ID reused as above
77 - it's a hash duplicate and we are in big trouble
79 deleted = (edir->d_m_name == NULL);
81 edir->d_m_name = cdir->d_m_name;
82 edir->d_u_name = cdir->d_u_name;
85 if (!cdir->d_parent || (cdir->d_parent == dir && !deleted))
87 /* the old was not in the same folder */
89 dirchildremove(cdir->d_parent, cdir);
92 /* parent/child directories */
94 dirchildadd(dir, cdir);
98 * Struct to save directory reading context in. Used to prevent
99 * O(n^2) searches on a directory.
107 unsigned int sd_sindex;
109 #define SDBUFBRK 2048
111 static int enumerate_loop(struct dirent *de, char *mname, void *data)
113 struct savedir *sd = data;
117 end = sd->sd_buf + sd->sd_buflen;
118 len = strlen(de->d_name);
119 *(sd->sd_last)++ = len;
120 lenm = strlen(mname);
121 if ( sd->sd_last + len +lenm + 4 > end ) {
125 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
126 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
132 sd->sd_buflen += SDBUFBRK;
133 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
134 end = sd->sd_buf + sd->sd_buflen;
137 memcpy( sd->sd_last, de->d_name, len + 1 );
138 sd->sd_last += len + 1;
140 *(sd->sd_last)++ = lenm;
141 memcpy( sd->sd_last, mname, lenm + 1 );
142 sd->sd_last += lenm + 1;
147 /* -----------------------------
149 * Doesn't work with dangling symlink
151 * - Move a folder with a dangling symlink in the trash
153 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
154 * and the Mac doesn't try to call afp_delete!
156 * Another option for symlink
158 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
161 char *check_dirent(const struct vol *vol, char *name)
165 if (!strcmp(name, "..") || !strcmp(name, "."))
168 if (!(validupath(vol, name)))
171 /* check for vetoed filenames */
172 if (veto_file(vol->v_veto, name))
174 if (NULL == (m_name = utompath(vol, name, utf8_encoding())))
177 /* now check against too big a file */
178 if (strlen(m_name) > vol->max_filename)
184 /* ----------------------------- */
186 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
193 if (NULL == ( dp = opendir( name)) ) {
197 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
198 if (!(m_name = check_dirent(vol, de->d_name)))
202 if (fn && fn(de,m_name, data) < 0) {
211 /* ----------------------------- */
212 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
215 unsigned int ibuflen, *rbuflen;
218 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
221 int did, ret, esz, len, first = 1;
223 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
225 u_int32_t sindex, maxsz, sz = 0;
230 if ( sd.sd_buflen == 0 ) {
231 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
232 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
236 sd.sd_buflen = SDBUFBRK;
241 memcpy( &vid, ibuf, sizeof( vid ));
242 ibuf += sizeof( vid );
244 if (NULL == ( vol = getvolbyvid( vid )) ) {
246 return( AFPERR_PARAM );
249 memcpy( &did, ibuf, sizeof( did ));
250 ibuf += sizeof( did );
252 if (NULL == ( dir = dirlookup( vol, did )) ) {
254 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
257 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
258 fbitmap = ntohs( fbitmap );
259 ibuf += sizeof( fbitmap );
261 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
262 dbitmap = ntohs( dbitmap );
263 ibuf += sizeof( dbitmap );
265 /* check for proper bitmaps -- the stuff in comments is for
266 * variable directory ids. */
267 if (!(fbitmap || dbitmap)
268 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
269 (dbitmap & (1 << DIRPBIT_PDID))*/) {
271 return AFPERR_BITMAP;
274 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
275 reqcnt = ntohs( reqcnt );
276 ibuf += sizeof( reqcnt );
279 memcpy( &sindex, ibuf, sizeof( sindex ));
280 sindex = ntohl( sindex );
281 ibuf += sizeof( sindex );
284 memcpy( &temp16, ibuf, sizeof( temp16 ));
285 sindex = ntohs( temp16 );
286 ibuf += sizeof( temp16 );
291 return AFPERR_PARAM ;
295 memcpy( &maxsz, ibuf, sizeof( maxsz ));
296 maxsz = ntohl( maxsz );
297 ibuf += sizeof( maxsz );
300 memcpy( &temp16, ibuf, sizeof( temp16 ));
301 maxsz = ntohs( temp16 );
302 ibuf += sizeof( temp16 );
306 header *=sizeof( u_char );
308 maxsz = min(maxsz, *rbuflen);
309 o_path = cname( vol, dir, &ibuf );
311 if (afp_errno == AFPERR_NOOBJ)
312 afp_errno = AFPERR_NODIR;
315 if (NULL == o_path ) {
316 return get_afp_errno(AFPERR_NOOBJ);
318 if ( *o_path->m_name != '\0') {
319 /* it's a file or it's a dir and extendir() was unable to chdir in it */
320 return path_error(o_path, AFPERR_NODIR );
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 dir was in the cache we don't have the inode */
334 if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
335 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
339 return AFPERR_ACCESS;
341 return AFPERR_BADTYPE;
348 curdir->ctime = o_path->st.st_ctime; /* play safe */
349 curdir->offcnt = ret;
352 sd.sd_last = sd.sd_buf;
356 sd.sd_did = curdir->d_did;
360 * Position sd_last as dictated by sindex.
362 if ( sindex < sd.sd_sindex ) {
364 sd.sd_last = sd.sd_buf;
366 while ( sd.sd_sindex < sindex ) {
367 len = *(sd.sd_last)++;
369 sd.sd_did = 0; /* invalidate sd struct to force re-read */
370 return( AFPERR_NOOBJ );
372 sd.sd_last += len + 1;
373 len = *(sd.sd_last)++;
374 sd.sd_last += len + 1;
378 while (( len = *(sd.sd_last)) != 0 ) {
380 * If we've got all we need, send it.
382 if ( actcnt == reqcnt ) {
387 * Save the start position, in case we exceed the buffer
388 * limitation, and have to back up one.
393 if (*sd.sd_last == 0) {
394 /* stat() already failed on this one */
395 sd.sd_last += len + 1;
396 len = *(sd.sd_last)++;
397 sd.sd_last += len + 1;
400 s_path.u_name = sd.sd_last;
401 if (of_stat( &s_path) < 0 ) {
403 * Somebody else plays with the dir, well it can be us with
407 /* so the next time it won't try to stat it again
408 * another solution would be to invalidate the cache with
409 * sd.sd_did = 0 but if it's not ENOENT error it will start again
412 sd.sd_last += len + 1;
413 len = *(sd.sd_last)++;
414 sd.sd_last += len + 1;
415 curdir->offcnt--; /* a little lie */
419 sd.sd_last += len + 1;
420 len = *(sd.sd_last)++;
421 s_path.m_name = sd.sd_last;
422 sd.sd_last += len + 1;
424 * If a fil/dir is not a dir, it's a file. This is slightly
425 * inaccurate, since that means /dev/null is a file, /dev/printer
428 if ( S_ISDIR(s_path.st.st_mode)) {
429 if ( dbitmap == 0 ) {
432 dir = dirsearch_byname(curdir, s_path.u_name);
434 if (s_path.m_name == NULL || (dir = adddir( vol, curdir, &s_path)) == NULL) {
439 s_path.m_name = NULL;
441 if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
442 data + header , &esz ))) {
447 if ( fbitmap == 0 ) {
450 if (s_path.m_name == NULL ) {
453 if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
454 data + header , &esz )) ) {
460 * Make sure entry is an even length, possibly with a null
463 if ( (esz + header) & 1 ) {
464 *(data + header + esz ) = '\0';
469 * Check if we've exceeded the size limit.
471 if ( maxsz < sz + esz + header) {
472 if (first) { /* maxsz can't hold a single reply */
484 temp16 = htons( esz + header );
485 memcpy( data, &temp16, sizeof( temp16 ));
486 data += sizeof(temp16);
489 *data++ = esz + header;
492 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
501 sd.sd_did = 0; /* invalidate sd struct to force re-read */
502 return( AFPERR_NOOBJ );
504 sd.sd_sindex = sindex + actcnt;
507 * All done, fill in misc junk in rbuf
509 fbitmap = htons( fbitmap );
510 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
511 rbuf += sizeof( fbitmap );
512 dbitmap = htons( dbitmap );
513 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
514 rbuf += sizeof( dbitmap );
515 actcnt = htons( actcnt );
516 memcpy( rbuf, &actcnt, sizeof( actcnt ));
517 rbuf += sizeof( actcnt );
522 /* ----------------------------- */
523 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
526 unsigned int ibuflen, *rbuflen;
528 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
531 /* ----------------------------- */
532 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
535 unsigned int ibuflen, *rbuflen;
537 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
540 /* ----------------------------- */
541 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
544 unsigned int ibuflen, *rbuflen;
546 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);