2 * $Id: enumerate.c,v 1.24 2002-10-11 14:18:28 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)) > MACFILELEN)
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, is64 )
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;
219 if ( sd.sd_buflen == 0 ) {
220 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
221 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
225 sd.sd_buflen = SDBUFBRK;
230 memcpy( &vid, ibuf, sizeof( vid ));
231 ibuf += sizeof( vid );
233 if (( vol = getvolbyvid( vid )) == NULL ) {
235 return( AFPERR_PARAM );
238 memcpy( &did, ibuf, sizeof( did ));
239 ibuf += sizeof( did );
241 if (( dir = dirlookup( vol, did )) == NULL ) {
243 return( AFPERR_NODIR );
246 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
247 fbitmap = ntohs( fbitmap );
248 ibuf += sizeof( fbitmap );
250 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
251 dbitmap = ntohs( dbitmap );
252 ibuf += sizeof( dbitmap );
254 /* check for proper bitmaps -- the stuff in comments is for
255 * variable directory ids. */
256 if (!(fbitmap || dbitmap)
257 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
258 (dbitmap & (1 << DIRPBIT_PDID))*/) {
260 return AFPERR_BITMAP;
263 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
264 reqcnt = ntohs( reqcnt );
265 ibuf += sizeof( reqcnt );
268 memcpy( &sindex, ibuf, sizeof( sindex ));
269 sindex = ntohs( sindex );
270 ibuf += sizeof( sindex );
273 memcpy( &temp16, ibuf, sizeof( temp16 ));
274 sindex = ntohs( temp16 );
275 ibuf += sizeof( temp16 );
279 memcpy( &maxsz, ibuf, sizeof( maxsz ));
280 maxsz = ntohs( maxsz );
281 ibuf += sizeof( maxsz );
284 memcpy( &temp16, ibuf, sizeof( temp16 ));
285 maxsz = ntohs( temp16 );
286 ibuf += sizeof( temp16 );
289 maxsz = min(maxsz, *rbuflen);
291 if (( o_path = cname( vol, dir, &ibuf )) == NULL) {
293 return( AFPERR_NODIR );
296 if ( *o_path->m_name != '\0') {
298 return( AFPERR_BADTYPE );
301 data = rbuf + 3 * sizeof( u_int16_t );
302 sz = 3 * sizeof( u_int16_t );
305 * Read the directory into a pre-malloced buffer, stored
307 * The end is indicated by a len of 0.
309 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
310 sd.sd_last = sd.sd_buf;
311 if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
314 return AFPERR_ACCESS;
316 return AFPERR_BADTYPE;
323 curdir->ctime = o_path->st.st_ctime; /* play safe */
324 if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
328 return AFPERR_ACCESS;
330 return AFPERR_BADTYPE;
337 curdir->offcnt = ret;
340 sd.sd_last = sd.sd_buf;
348 * Position sd_last as dictated by sindex.
350 if ( sindex < sd.sd_sindex ) {
352 sd.sd_last = sd.sd_buf;
354 while ( sd.sd_sindex < sindex ) {
355 len = *(sd.sd_last)++;
357 sd.sd_did = 0; /* invalidate sd struct to force re-read */
359 return( AFPERR_NOOBJ );
361 sd.sd_last += len + 1;
365 while (( len = *(sd.sd_last)) != 0 ) {
367 * If we've got all we need, send it.
369 if ( actcnt == reqcnt ) {
374 * Save the start position, in case we exceed the buffer
375 * limitation, and have to back up one.
380 if (*sd.sd_last == 0) {
381 /* stat() already failed on this one */
382 sd.sd_last += len + 1;
385 s_path.u_name = sd.sd_last;
386 if (of_stat( &s_path) < 0 ) {
388 * Somebody else plays with the dir, well it can be us with
392 /* so the next time it won't try to stat it again
393 * another solution would be to invalidate the cache with
394 * sd.sd_did = 0 but if it's not ENOENT error it will start again
397 sd.sd_last += len + 1;
402 * If a fil/dir is not a dir, it's a file. This is slightly
403 * inaccurate, since that means /dev/null is a file, /dev/printer
406 if ( S_ISDIR(s_path.st.st_mode)) {
407 if ( dbitmap == 0 ) {
408 sd.sd_last += len + 1;
411 dir = curdir->d_child;
412 s_path.m_name = NULL;
414 if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
417 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
420 s_path.m_name = utompath(vol, s_path.u_name);
421 if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
427 if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
428 data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
434 if ( fbitmap == 0 ) {
435 sd.sd_last += len + 1;
438 s_path.m_name = utompath(vol, s_path.u_name);
439 if (( ret = getfilparams(vol, fbitmap, &s_path, curdir,
440 data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
447 * Make sure entry is an even length, possibly with a null
451 *(data + 2 * sizeof( u_char ) + esz ) = '\0';
456 * Check if we've exceeded the size limit.
458 if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
459 if (first) { /* maxsz can't hold a single reply */
470 sz += esz + 2 * sizeof( u_char );
471 *data++ = esz + 2 * sizeof( u_char );
472 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
475 sd.sd_last += len + 1;
480 sd.sd_did = 0; /* invalidate sd struct to force re-read */
481 return( AFPERR_NOOBJ );
483 sd.sd_sindex = sindex + actcnt;
486 * All done, fill in misc junk in rbuf
488 fbitmap = htons( fbitmap );
489 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
490 rbuf += sizeof( fbitmap );
491 dbitmap = htons( dbitmap );
492 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
493 rbuf += sizeof( dbitmap );
494 actcnt = htons( actcnt );
495 memcpy( rbuf, &actcnt, sizeof( actcnt ));
496 rbuf += sizeof( actcnt );
501 /* ----------------------------- */
502 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
505 unsigned int ibuflen, *rbuflen;
507 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
510 /* ----------------------------- */
511 int afp_enumerate_ext2(obj, ibuf, ibuflen, rbuf, rbuflen )
514 unsigned int ibuflen, *rbuflen;
516 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);