2 * $Id: enumerate.c,v 1.6 2001-08-14 14:00:10 rufustfirefly 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 <sys/syslog.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"
36 /* check for mtab DID code */
38 #include "parse_mtab.h"
42 adddir( vol, dir, name, namlen, upath, upathlen, st )
49 struct dir *cdir, *edir;
50 #if AD_VERSION > AD_VERSION1
52 #endif /* AD_VERSION > AD_VERSION1 */
55 struct stat lst, *lstp;
56 #endif /* USE_LASTDID */
58 if ((cdir = dirnew(namlen + 1)) == NULL) {
59 syslog( LOG_ERR, "adddir: malloc: %m" );
62 strcpy( cdir->d_name, name );
63 cdir->d_name[namlen] = '\0';
68 /* find out if we have a fixed did already */
69 cdir->d_did = cnid_lookup(vol->v_db, st, dir->d_did, upath,
73 if (cdir->d_did == 0) {
74 #if AD_VERSION > AD_VERSION1
75 memset(&ad, 0, sizeof(ad));
76 if (ad_open(upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY, 0, &ad) < 0) {
77 /* if we can't parse the AppleDouble header, return 0 for the DID */
80 /* ... else retrieve the DID entry into cdir->d_did */
81 memcpy(&cdir->d_did, ad_entry(&ad, ADEID_DID), sizeof(cdir->d_did));
82 ad_close(&ad, ADFLAGS_HF);
84 #endif /* AD_VERSION */
88 cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
89 upathlen, cdir->d_did);
93 if (cdir->d_did == 0) {
95 /* last way of doing DIDs */
96 cdir->d_did = htonl( vol->v_lastdid++ );
97 #else /* USE_LASTDID */
98 lstp = lstat(upath, &lst) < 0 ? st : &lst;
100 /* mtab way of doing DIDs */
101 cdir->d_did = htonl( afpd_st_cnid ( lstp ) );
103 /* the old way of doing DIDs (default) */
104 cdir->d_did = htonl( CNID(lstp, 0) );
105 #endif /* DID_MTAB */
106 #endif /* USE_LASTDID */
109 if ((edir = dirinsert( vol, cdir ))) {
111 if (strcmp(edir->d_name, cdir->d_name)) {
112 syslog(LOG_INFO, "WARNING: DID conflict for '%s' and '%s'. Are these the same file?", edir->d_name, cdir->d_name);
118 edir->d_name = cdir->d_name;
123 /* parent/child directories */
124 cdir->d_parent = dir;
125 dirchildadd(dir, cdir);
130 * Struct to save directory reading context in. Used to prevent
131 * O(n^2) searches on a directory.
141 #define SDBUFBRK 1024
143 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
146 int ibuflen, *rbuflen;
149 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
154 int did, ret, esz, len, first = 1;
155 char *path, *data, *end, *start;
156 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
157 u_int16_t sindex, maxsz, sz = 0;
159 if ( sd.sd_buflen == 0 ) {
160 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
161 syslog( LOG_ERR, "afp_enumerate: malloc: %m" );
165 sd.sd_buflen = SDBUFBRK;
170 memcpy( &vid, ibuf, sizeof( vid ));
171 ibuf += sizeof( vid );
173 if (( vol = getvolbyvid( vid )) == NULL ) {
175 return( AFPERR_PARAM );
178 memcpy( &did, ibuf, sizeof( did ));
179 ibuf += sizeof( did );
181 if (( dir = dirsearch( vol, did )) == NULL ) {
183 return( AFPERR_NODIR );
186 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
187 fbitmap = ntohs( fbitmap );
188 ibuf += sizeof( fbitmap );
190 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
191 dbitmap = ntohs( dbitmap );
192 ibuf += sizeof( dbitmap );
194 /* check for proper bitmaps -- the stuff in comments is for
195 * variable directory ids. */
196 if (!(fbitmap || dbitmap)
197 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
198 (dbitmap & (1 << DIRPBIT_PDID))*/) {
200 return AFPERR_BITMAP;
203 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
204 reqcnt = ntohs( reqcnt );
205 ibuf += sizeof( reqcnt );
207 memcpy( &sindex, ibuf, sizeof( sindex ));
208 sindex = ntohs( sindex );
209 ibuf += sizeof( sindex );
211 memcpy( &maxsz, ibuf, sizeof( maxsz ));
212 maxsz = ntohs( maxsz );
213 ibuf += sizeof( maxsz );
215 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
217 return( AFPERR_NODIR );
219 data = rbuf + 3 * sizeof( u_int16_t );
220 sz = 3 * sizeof( u_int16_t );
223 * Read the directory into a pre-malloced buffer, stored
225 * The end is indicated by a len of 0.
227 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
228 sd.sd_last = sd.sd_buf;
230 if (( dp = opendir( mtoupath(vol, path ))) == NULL ) {
232 return (errno == ENOTDIR) ? AFPERR_BADTYPE : AFPERR_NODIR;
235 end = sd.sd_buf + sd.sd_buflen;
236 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
237 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
240 if (!(validupath(vol, de->d_name)))
243 /* now check against too big a file */
244 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
247 len = strlen(de->d_name);
248 *(sd.sd_last)++ = len;
250 if ( sd.sd_last + len + 2 > end ) {
254 if ((buf = (char *) realloc( sd.sd_buf, sd.sd_buflen +
255 SDBUFBRK )) == NULL ) {
256 syslog( LOG_ERR, "afp_enumerate: realloc: %m" );
262 sd.sd_buflen += SDBUFBRK;
263 sd.sd_last = ( sd.sd_last - start ) + sd.sd_buf;
264 end = sd.sd_buf + sd.sd_buflen;
267 memcpy( sd.sd_last, de->d_name, len + 1 );
268 sd.sd_last += len + 1;
272 sd.sd_last = sd.sd_buf;
281 * Position sd_last as dictated by sindex.
283 if ( sindex < sd.sd_sindex ) {
285 sd.sd_last = sd.sd_buf;
287 while ( sd.sd_sindex < sindex ) {
288 len = *(sd.sd_last)++;
290 sd.sd_did = -1; /* invalidate sd struct to force re-read */
292 return( AFPERR_NOOBJ );
294 sd.sd_last += len + 1;
298 while (( len = *(sd.sd_last)) != 0 ) {
300 * If we've got all we need, send it.
302 if ( actcnt == reqcnt ) {
307 * Save the start position, in case we exceed the buffer
308 * limitation, and have to back up one.
313 if ( stat( sd.sd_last, &st ) < 0 ) {
314 syslog( LOG_DEBUG, "afp_enumerate: stat %s: %m", sd.sd_last );
315 sd.sd_last += len + 1;
320 * If a fil/dir is not a dir, it's a file. This is slightly
321 * inaccurate, since that means /dev/null is a file, /dev/printer
324 if ( S_ISDIR(st.st_mode)) {
325 if ( dbitmap == 0 ) {
326 sd.sd_last += len + 1;
329 path = utompath(vol, sd.sd_last);
330 dir = curdir->d_child;
332 if ( strcmp( dir->d_name, path ) == 0 ) {
335 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
337 if (!dir && ((dir = adddir( vol, curdir, path, strlen( path ),
338 sd.sd_last, len, &st)) == NULL)) {
344 if (( ret = getdirparams(vol, dbitmap, sd.sd_last, dir,
345 &st, data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
351 if ( fbitmap == 0 ) {
352 sd.sd_last += len + 1;
356 if (( ret = getfilparams(vol, fbitmap, utompath(vol, sd.sd_last),
357 curdir, &st, data + 2 * sizeof( u_char ), &esz )) !=
365 * Make sure entry is an even length, possibly with a null
369 *(data + 2 * sizeof( u_char ) + esz ) = '\0';
374 * Check if we've exceeded the size limit.
376 if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
377 if (first) { /* maxsz can't hold a single reply */
388 sz += esz + 2 * sizeof( u_char );
389 *data++ = esz + 2 * sizeof( u_char );
390 *data++ = S_ISDIR(st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
393 sd.sd_last += len + 1;
398 sd.sd_did = -1; /* invalidate sd struct to force re-read */
399 return( AFPERR_NOOBJ );
401 sd.sd_sindex = sindex + actcnt;
404 * All done, fill in misc junk in rbuf
406 fbitmap = htons( fbitmap );
407 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
408 rbuf += sizeof( fbitmap );
409 dbitmap = htons( dbitmap );
410 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
411 rbuf += sizeof( dbitmap );
412 actcnt = htons( actcnt );
413 memcpy( rbuf, &actcnt, sizeof( actcnt ));
414 rbuf += sizeof( actcnt );
420 /* why is this here? well, FPCatSearch is essentially an FPEnumerate
422 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
423 char *rbuf, int *rbuflen)
429 memcpy(&vid, ibuf, sizeof(vid));
433 if ((vol = getvolbyvid(vid)) == NULL)
437 * do a breadth-first search of directories:
438 * lookup did/name info.
439 * add to result if match
440 * check to see if we've exceeded our timelimit
441 * if yes, return current position
444 * we keep a copy of our current position in struct vol.
445 * if the next catsearch request for that volume isn't at
446 * at the current position, bail and return catchanged.