2 * $Id: enumerate.c,v 1.35 2003-03-09 19:55:34 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 1024
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;
118 if ( sd->sd_last + len + 2 > end ) {
122 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
123 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
129 sd->sd_buflen += SDBUFBRK;
130 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
131 end = sd->sd_buf + sd->sd_buflen;
134 memcpy( sd->sd_last, de->d_name, len + 1 );
135 sd->sd_last += len + 1;
139 /* -----------------------------
141 * Doesn't work with dangling symlink
143 * - Move a folder with a dangling symlink in the trash
145 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
146 * and the Mac doesn't try to call afp_delete!
148 * Another option for symlink
150 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
153 char *check_dirent(const struct vol *vol, char *name)
157 if (!strcmp(name, "..") || !strcmp(name, "."))
160 if (!(validupath(vol, name)))
163 /* check for vetoed filenames */
164 if (veto_file(vol->v_veto, name))
166 if (NULL == (m_name = utompath(vol, name, utf8_encoding())))
169 /* now check against too big a file */
170 if (strlen(m_name) > vol->max_filename)
176 /* ----------------------------- */
178 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
185 if (NULL == ( dp = opendir( name)) ) {
189 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
190 if (!(m_name = check_dirent(vol, de->d_name)))
194 if (fn && fn(de,m_name, data) < 0) {
203 /* ----------------------------- */
204 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
207 unsigned int ibuflen, *rbuflen;
210 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
213 int did, ret, esz, len, first = 1;
215 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
217 u_int32_t sindex, maxsz, sz = 0;
222 if ( sd.sd_buflen == 0 ) {
223 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
224 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
228 sd.sd_buflen = SDBUFBRK;
233 memcpy( &vid, ibuf, sizeof( vid ));
234 ibuf += sizeof( vid );
236 if (NULL == ( vol = getvolbyvid( vid )) ) {
238 return( AFPERR_PARAM );
241 memcpy( &did, ibuf, sizeof( did ));
242 ibuf += sizeof( did );
244 if (NULL == ( dir = dirlookup( vol, did )) ) {
246 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
249 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
250 fbitmap = ntohs( fbitmap );
251 ibuf += sizeof( fbitmap );
253 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
254 dbitmap = ntohs( dbitmap );
255 ibuf += sizeof( dbitmap );
257 /* check for proper bitmaps -- the stuff in comments is for
258 * variable directory ids. */
259 if (!(fbitmap || dbitmap)
260 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
261 (dbitmap & (1 << DIRPBIT_PDID))*/) {
263 return AFPERR_BITMAP;
266 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
267 reqcnt = ntohs( reqcnt );
268 ibuf += sizeof( reqcnt );
271 memcpy( &sindex, ibuf, sizeof( sindex ));
272 sindex = ntohl( sindex );
273 ibuf += sizeof( sindex );
276 memcpy( &temp16, ibuf, sizeof( temp16 ));
277 sindex = ntohs( temp16 );
278 ibuf += sizeof( temp16 );
283 return AFPERR_PARAM ;
287 memcpy( &maxsz, ibuf, sizeof( maxsz ));
288 maxsz = ntohl( maxsz );
289 ibuf += sizeof( maxsz );
292 memcpy( &temp16, ibuf, sizeof( temp16 ));
293 maxsz = ntohs( temp16 );
294 ibuf += sizeof( temp16 );
298 header *=sizeof( u_char );
300 maxsz = min(maxsz, *rbuflen);
301 o_path = cname( vol, dir, &ibuf );
303 if (afp_errno == AFPERR_NOOBJ)
304 afp_errno = AFPERR_NODIR;
307 if (NULL == o_path ) {
310 if ( *o_path->m_name != '\0') {
311 /* it's a file or it's a dir and extendir() was unable to chdir in it */
312 return (path_isadir(o_path))? afp_errno:AFPERR_BADTYPE ;
315 data = rbuf + 3 * sizeof( u_int16_t );
316 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
319 * Read the directory into a pre-malloced buffer, stored
321 * The end is indicated by a len of 0.
323 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
324 sd.sd_last = sd.sd_buf;
325 /* if dir was in the cache we don't have the inode */
326 if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
327 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
331 return AFPERR_ACCESS;
333 return AFPERR_BADTYPE;
340 curdir->ctime = o_path->st.st_ctime; /* play safe */
341 curdir->offcnt = ret;
344 sd.sd_last = sd.sd_buf;
352 * Position sd_last as dictated by sindex.
354 if ( sindex < sd.sd_sindex ) {
356 sd.sd_last = sd.sd_buf;
358 while ( sd.sd_sindex < sindex ) {
359 len = *(sd.sd_last)++;
361 sd.sd_did = 0; /* invalidate sd struct to force re-read */
362 return( AFPERR_NOOBJ );
364 sd.sd_last += len + 1;
368 while (( len = *(sd.sd_last)) != 0 ) {
370 * If we've got all we need, send it.
372 if ( actcnt == reqcnt ) {
377 * Save the start position, in case we exceed the buffer
378 * limitation, and have to back up one.
383 if (*sd.sd_last == 0) {
384 /* stat() already failed on this one */
385 sd.sd_last += len + 1;
388 s_path.u_name = sd.sd_last;
389 if (of_stat( &s_path) < 0 ) {
391 * Somebody else plays with the dir, well it can be us with
395 /* so the next time it won't try to stat it again
396 * another solution would be to invalidate the cache with
397 * sd.sd_did = 0 but if it's not ENOENT error it will start again
400 sd.sd_last += len + 1;
401 curdir->offcnt--; /* a little lie */
406 * If a fil/dir is not a dir, it's a file. This is slightly
407 * inaccurate, since that means /dev/null is a file, /dev/printer
410 if ( S_ISDIR(s_path.st.st_mode)) {
411 if ( dbitmap == 0 ) {
412 sd.sd_last += len + 1;
415 dir = dirsearch_byname(curdir, s_path.u_name);
417 s_path.m_name = utompath(vol, s_path.u_name, utf8_encoding() );
418 if (s_path.m_name == NULL || (dir = adddir( vol, curdir, &s_path)) == NULL) {
423 s_path.m_name = NULL;
425 if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
426 data + header , &esz ))) {
431 if ( fbitmap == 0 ) {
432 sd.sd_last += len + 1;
435 s_path.m_name = utompath(vol, s_path.u_name, utf8_encoding());
436 if (s_path.m_name == NULL ) {
439 if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
440 data + header , &esz )) ) {
446 * Make sure entry is an even length, possibly with a null
449 if ( (esz + header) & 1 ) {
450 *(data + header + esz ) = '\0';
455 * Check if we've exceeded the size limit.
457 if ( maxsz < sz + esz + header) {
458 if (first) { /* maxsz can't hold a single reply */
470 temp16 = htons( esz + header );
471 memcpy( data, &temp16, sizeof( temp16 ));
472 data += sizeof(temp16);
475 *data++ = esz + header;
478 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
484 sd.sd_last += len + 1;
488 sd.sd_did = 0; /* invalidate sd struct to force re-read */
489 return( AFPERR_NOOBJ );
491 sd.sd_sindex = sindex + actcnt;
494 * All done, fill in misc junk in rbuf
496 fbitmap = htons( fbitmap );
497 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
498 rbuf += sizeof( fbitmap );
499 dbitmap = htons( dbitmap );
500 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
501 rbuf += sizeof( dbitmap );
502 actcnt = htons( actcnt );
503 memcpy( rbuf, &actcnt, sizeof( actcnt ));
504 rbuf += sizeof( actcnt );
509 /* ----------------------------- */
510 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
513 unsigned int ibuflen, *rbuflen;
515 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
518 /* ----------------------------- */
519 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
522 unsigned int ibuflen, *rbuflen;
524 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
527 /* ----------------------------- */
528 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
531 unsigned int ibuflen, *rbuflen;
533 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);