2 * $Id: enumerate.c,v 1.20 2002-03-24 07:46:11 jmarcus Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
18 #include <atalk/logger.h>
19 #include <sys/types.h>
22 #include <sys/param.h>
24 #include <netatalk/endian.h>
25 #include <atalk/afp.h>
26 #include <atalk/adouble.h>
28 #include <atalk/cnid.h>
31 #include "directory.h"
37 /* check for mtab DID code */
39 #include "parse_mtab.h"
42 #define min(a,b) ((a)<(b)?(a):(b))
45 adddir( vol, dir, name, namlen, upath, upathlen, st )
52 struct dir *cdir, *edir;
53 #if AD_VERSION > AD_VERSION1
55 #endif /* AD_VERSION > AD_VERSION1 */
58 struct stat lst, *lstp;
59 #endif /* USE_LASTDID */
61 if ((cdir = dirnew(namlen + 1)) == NULL) {
62 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
65 strcpy( cdir->d_name, name );
66 cdir->d_name[namlen] = '\0';
70 #if AD_VERSION > AD_VERSION1
71 /* look in AD v2 header */
72 memset(&ad, 0, sizeof(ad));
73 if (ad_open(upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY, 0, &ad) >= 0) {
74 /* if we can parse the AppleDouble header, retrieve the DID entry into cdir->d_did */
75 memcpy(&cdir->d_did, ad_entry(&ad, ADEID_DID), sizeof(cdir->d_did));
76 ad_close(&ad, ADFLAGS_HF);
78 #endif /* AD_VERSION */
82 cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
83 upathlen, cdir->d_did);
84 /* Fail out if things go bad with CNID. */
85 if (cdir->d_did == CNID_INVALID) {
88 LOG(log_error, logtype_afpd, "adddir: Incorrect parameters passed to cnid_add");
98 if (cdir->d_did == 0) {
100 /* last way of doing DIDs */
101 cdir->d_did = htonl( vol->v_lastdid++ );
102 #else /* USE_LASTDID */
103 lstp = lstat(upath, &lst) < 0 ? st : &lst;
105 /* mtab way of doing DIDs */
106 cdir->d_did = htonl( afpd_st_cnid ( lstp ) );
108 /* the old way of doing DIDs (default) */
109 cdir->d_did = htonl( CNID(lstp, 0) );
110 #endif /* DID_MTAB */
111 #endif /* USE_LASTDID */
114 if ((edir = dirinsert( vol, cdir ))) {
117 if (strcmp(edir->d_name, cdir->d_name)) {
118 LOG(log_info, logtype_afpd, "WARNING: DID conflict for '%s' and '%s'. Are these the same file?", edir->d_name, cdir->d_name);
125 edir->d_name = cdir->d_name;
130 /* parent/child directories */
131 cdir->d_parent = dir;
132 dirchildadd(dir, cdir);
137 * Struct to save directory reading context in. Used to prevent
138 * O(n^2) searches on a directory.
148 #define SDBUFBRK 1024
150 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
153 int ibuflen, *rbuflen;
156 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
161 int did, ret, esz, len, first = 1;
162 char *path, *data, *end, *start;
163 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
164 u_int16_t sindex, maxsz, sz = 0;
166 if ( sd.sd_buflen == 0 ) {
167 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
168 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
172 sd.sd_buflen = SDBUFBRK;
177 memcpy( &vid, ibuf, sizeof( vid ));
178 ibuf += sizeof( vid );
180 if (( vol = getvolbyvid( vid )) == NULL ) {
182 return( AFPERR_PARAM );
185 memcpy( &did, ibuf, sizeof( did ));
186 ibuf += sizeof( did );
188 if (( dir = dirsearch( vol, did )) == NULL ) {
190 return( AFPERR_NODIR );
193 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
194 fbitmap = ntohs( fbitmap );
195 ibuf += sizeof( fbitmap );
197 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
198 dbitmap = ntohs( dbitmap );
199 ibuf += sizeof( dbitmap );
201 /* check for proper bitmaps -- the stuff in comments is for
202 * variable directory ids. */
203 if (!(fbitmap || dbitmap)
204 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
205 (dbitmap & (1 << DIRPBIT_PDID))*/) {
207 return AFPERR_BITMAP;
210 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
211 reqcnt = ntohs( reqcnt );
212 ibuf += sizeof( reqcnt );
214 memcpy( &sindex, ibuf, sizeof( sindex ));
215 sindex = ntohs( sindex );
216 ibuf += sizeof( sindex );
218 memcpy( &maxsz, ibuf, sizeof( maxsz ));
219 maxsz = ntohs( maxsz );
220 ibuf += sizeof( maxsz );
222 maxsz = min(maxsz, *rbuflen);
224 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
226 return( AFPERR_NODIR );
228 data = rbuf + 3 * sizeof( u_int16_t );
229 sz = 3 * sizeof( u_int16_t );
232 * Read the directory into a pre-malloced buffer, stored
234 * The end is indicated by a len of 0.
236 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
237 sd.sd_last = sd.sd_buf;
239 if (( dp = opendir( mtoupath(vol, path ))) == NULL ) {
241 return (errno == ENOTDIR) ? AFPERR_BADTYPE : AFPERR_NODIR;
244 end = sd.sd_buf + sd.sd_buflen;
245 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
246 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
249 if (!(validupath(vol, de->d_name)))
252 /* check for vetoed filenames */
253 if (veto_file(vol->v_veto, de->d_name))
256 /* now check against too big a file */
257 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
260 len = strlen(de->d_name);
261 *(sd.sd_last)++ = len;
263 if ( sd.sd_last + len + 2 > end ) {
267 if ((buf = (char *) realloc( sd.sd_buf, sd.sd_buflen +
268 SDBUFBRK )) == NULL ) {
269 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
276 sd.sd_buflen += SDBUFBRK;
277 sd.sd_last = ( sd.sd_last - start ) + sd.sd_buf;
278 end = sd.sd_buf + sd.sd_buflen;
281 memcpy( sd.sd_last, de->d_name, len + 1 );
282 sd.sd_last += len + 1;
286 sd.sd_last = sd.sd_buf;
295 * Position sd_last as dictated by sindex.
297 if ( sindex < sd.sd_sindex ) {
299 sd.sd_last = sd.sd_buf;
301 while ( sd.sd_sindex < sindex ) {
302 len = *(sd.sd_last)++;
304 sd.sd_did = -1; /* invalidate sd struct to force re-read */
306 return( AFPERR_NOOBJ );
308 sd.sd_last += len + 1;
312 while (( len = *(sd.sd_last)) != 0 ) {
314 * If we've got all we need, send it.
316 if ( actcnt == reqcnt ) {
321 * Save the start position, in case we exceed the buffer
322 * limitation, and have to back up one.
327 if (*sd.sd_last == 0) {
328 /* stat() already failed on this one */
329 sd.sd_last += len + 1;
333 if (stat( sd.sd_last, &st ) < 0 ) {
335 * Somebody else plays with the dir, well it can be us with
339 /* so the next time it won't try to stat it again
340 * another solution would be to invalidate the cache with
341 * sd.sd_did = -1 but if it's not ENOENT error it will start again
344 sd.sd_last += len + 1;
349 * If a fil/dir is not a dir, it's a file. This is slightly
350 * inaccurate, since that means /dev/null is a file, /dev/printer
353 if ( S_ISDIR(st.st_mode)) {
354 if ( dbitmap == 0 ) {
355 sd.sd_last += len + 1;
358 path = utompath(vol, sd.sd_last);
359 dir = curdir->d_child;
361 if ( strcmp( dir->d_name, path ) == 0 ) {
364 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
366 if (!dir && ((dir = adddir( vol, curdir, path, strlen( path ),
367 sd.sd_last, len, &st)) == NULL)) {
373 if (( ret = getdirparams(vol, dbitmap, sd.sd_last, dir,
374 &st, data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
380 if ( fbitmap == 0 ) {
381 sd.sd_last += len + 1;
385 if (( ret = getfilparams(vol, fbitmap, utompath(vol, sd.sd_last),
386 curdir, &st, data + 2 * sizeof( u_char ), &esz )) !=
394 * Make sure entry is an even length, possibly with a null
398 *(data + 2 * sizeof( u_char ) + esz ) = '\0';
403 * Check if we've exceeded the size limit.
405 if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
406 if (first) { /* maxsz can't hold a single reply */
417 sz += esz + 2 * sizeof( u_char );
418 *data++ = esz + 2 * sizeof( u_char );
419 *data++ = S_ISDIR(st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
422 sd.sd_last += len + 1;
427 sd.sd_did = -1; /* invalidate sd struct to force re-read */
428 return( AFPERR_NOOBJ );
430 sd.sd_sindex = sindex + actcnt;
433 * All done, fill in misc junk in rbuf
435 fbitmap = htons( fbitmap );
436 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
437 rbuf += sizeof( fbitmap );
438 dbitmap = htons( dbitmap );
439 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
440 rbuf += sizeof( dbitmap );
441 actcnt = htons( actcnt );
442 memcpy( rbuf, &actcnt, sizeof( actcnt ));
443 rbuf += sizeof( actcnt );
449 /* why is this here? well, FPCatSearch is essentially an FPEnumerate
451 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
452 char *rbuf, int *rbuflen)
458 memcpy(&vid, ibuf, sizeof(vid));
462 if ((vol = getvolbyvid(vid)) == NULL)
466 * do a breadth-first search of directories:
467 * lookup did/name info.
468 * add to result if match
469 * check to see if we've exceeded our timelimit
470 * if yes, return current position
473 * we keep a copy of our current position in struct vol.
474 * if the next catsearch request for that volume isn't at
475 * at the current position, bail and return catchanged.