2 * $Id: enumerate.c,v 1.26 2002-10-17 18:01:54 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 edir->d_m_name = cdir->d_m_name;
97 edir->d_u_name = cdir->d_u_name;
102 /* parent/child directories */
103 cdir->d_parent = dir;
104 dirchildadd(dir, cdir);
108 * Struct to save directory reading context in. Used to prevent
109 * O(n^2) searches on a directory.
117 unsigned int sd_sindex;
119 #define SDBUFBRK 1024
121 static int enumerate_loop(struct dirent *de, char *mname, void *data)
123 struct savedir *sd = data;
127 end = sd->sd_buf + sd->sd_buflen;
128 len = strlen(de->d_name);
129 *(sd->sd_last)++ = len;
131 if ( sd->sd_last + len + 2 > end ) {
135 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
136 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
142 sd->sd_buflen += SDBUFBRK;
143 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
144 end = sd->sd_buf + sd->sd_buflen;
147 memcpy( sd->sd_last, de->d_name, len + 1 );
148 sd->sd_last += len + 1;
152 /* ----------------------------- */
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))
167 /* now check against too big a file */
168 if (strlen(m_name = utompath(vol, name)) > vol->max_filename)
174 /* ----------------------------- */
176 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
183 if (( dp = opendir( name)) == NULL ) {
187 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
188 if (!(m_name = check_dirent(vol, de->d_name)))
192 if (fn && fn(de,m_name, data) < 0) {
201 /* ----------------------------- */
202 static int enumerate(obj, ibuf, ibuflen, rbuf, rbuflen, ext )
205 unsigned int ibuflen, *rbuflen;
208 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
211 int did, ret, esz, len, first = 1;
213 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
215 u_int32_t sindex, maxsz, sz = 0;
220 if ( sd.sd_buflen == 0 ) {
221 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
222 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
226 sd.sd_buflen = SDBUFBRK;
231 memcpy( &vid, ibuf, sizeof( vid ));
232 ibuf += sizeof( vid );
234 if (( vol = getvolbyvid( vid )) == NULL ) {
236 return( AFPERR_PARAM );
239 memcpy( &did, ibuf, sizeof( did ));
240 ibuf += sizeof( did );
242 if (( dir = dirlookup( vol, did )) == NULL ) {
244 return( AFPERR_NODIR );
247 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
248 fbitmap = ntohs( fbitmap );
249 ibuf += sizeof( fbitmap );
251 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
252 dbitmap = ntohs( dbitmap );
253 ibuf += sizeof( dbitmap );
255 /* check for proper bitmaps -- the stuff in comments is for
256 * variable directory ids. */
257 if (!(fbitmap || dbitmap)
258 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
259 (dbitmap & (1 << DIRPBIT_PDID))*/) {
261 return AFPERR_BITMAP;
264 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
265 reqcnt = ntohs( reqcnt );
266 ibuf += sizeof( reqcnt );
269 memcpy( &sindex, ibuf, sizeof( sindex ));
270 sindex = ntohs( sindex );
271 ibuf += sizeof( sindex );
274 memcpy( &temp16, ibuf, sizeof( temp16 ));
275 sindex = ntohs( temp16 );
276 ibuf += sizeof( temp16 );
280 memcpy( &maxsz, ibuf, sizeof( maxsz ));
281 maxsz = ntohs( maxsz );
282 ibuf += sizeof( maxsz );
285 memcpy( &temp16, ibuf, sizeof( temp16 ));
286 maxsz = ntohs( temp16 );
287 ibuf += sizeof( temp16 );
291 header *=sizeof( u_char );
293 maxsz = min(maxsz, *rbuflen);
295 if (( o_path = cname( vol, dir, &ibuf )) == NULL) {
297 return( AFPERR_NODIR );
300 if ( *o_path->m_name != '\0') {
302 return( AFPERR_BADTYPE );
305 data = rbuf + 3 * sizeof( u_int16_t );
306 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
309 * Read the directory into a pre-malloced buffer, stored
311 * The end is indicated by a len of 0.
313 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
314 sd.sd_last = sd.sd_buf;
315 if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
318 return AFPERR_ACCESS;
320 return AFPERR_BADTYPE;
327 curdir->ctime = o_path->st.st_ctime; /* play safe */
328 if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
332 return AFPERR_ACCESS;
334 return AFPERR_BADTYPE;
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 */
363 return( AFPERR_NOOBJ );
365 sd.sd_last += len + 1;
369 while (( len = *(sd.sd_last)) != 0 ) {
371 * If we've got all we need, send it.
373 if ( actcnt == reqcnt ) {
378 * Save the start position, in case we exceed the buffer
379 * limitation, and have to back up one.
384 if (*sd.sd_last == 0) {
385 /* stat() already failed on this one */
386 sd.sd_last += len + 1;
389 s_path.u_name = sd.sd_last;
390 if (of_stat( &s_path) < 0 ) {
392 * Somebody else plays with the dir, well it can be us with
396 /* so the next time it won't try to stat it again
397 * another solution would be to invalidate the cache with
398 * sd.sd_did = 0 but if it's not ENOENT error it will start again
401 sd.sd_last += len + 1;
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 = curdir->d_child;
416 s_path.m_name = NULL;
418 if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
421 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
424 s_path.m_name = utompath(vol, s_path.u_name);
425 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
431 if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
432 data + header , &esz )) != AFP_OK ) {
438 if ( fbitmap == 0 ) {
439 sd.sd_last += len + 1;
442 s_path.m_name = utompath(vol, s_path.u_name);
443 if (( ret = getfilparams(vol, fbitmap, &s_path, curdir,
444 data + header , &esz )) != AFP_OK ) {
451 * Make sure entry is an even length, possibly with a null
454 if ( (esz + header) & 1 ) {
455 *(data + header + esz ) = '\0';
460 * Check if we've exceeded the size limit.
462 if ( maxsz < sz + esz + header) {
463 if (first) { /* maxsz can't hold a single reply */
476 temp16 = htons( esz + header );
477 memcpy( data, &temp16, sizeof( temp16 ));
478 data += sizeof(temp16);
481 *data++ = esz + header;
484 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
490 sd.sd_last += len + 1;
495 sd.sd_did = 0; /* invalidate sd struct to force re-read */
496 return( AFPERR_NOOBJ );
498 sd.sd_sindex = sindex + actcnt;
501 * All done, fill in misc junk in rbuf
503 fbitmap = htons( fbitmap );
504 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
505 rbuf += sizeof( fbitmap );
506 dbitmap = htons( dbitmap );
507 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
508 rbuf += sizeof( dbitmap );
509 actcnt = htons( actcnt );
510 memcpy( rbuf, &actcnt, sizeof( actcnt ));
511 rbuf += sizeof( actcnt );
516 /* ----------------------------- */
517 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
520 unsigned int ibuflen, *rbuflen;
522 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
525 /* ----------------------------- */
526 int afp_enumerate_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
529 unsigned int ibuflen, *rbuflen;
531 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
534 /* ----------------------------- */
535 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
538 unsigned int ibuflen, *rbuflen;
540 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);