2 * $Id: enumerate.c,v 1.4 2001-06-06 13:36:36 rufustfirefly Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
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>
27 #include <atalk/cnid.h>
30 #include "directory.h"
35 /* check for mtab DID code */
37 #include "parse_mtab.h"
41 adddir( vol, dir, name, namlen, upath, upathlen, st )
48 struct dir *cdir, *edir;
49 #if AD_VERSION > AD_VERSION1
54 struct stat lst, *lstp;
55 #endif /* USE_LASTDID */
57 if ((cdir = dirnew(namlen + 1)) == NULL) {
58 syslog( LOG_ERR, "adddir: malloc: %m" );
61 strcpy( cdir->d_name, name );
62 cdir->d_name[namlen] = '\0';
64 #if AD_VERSION > AD_VERSION1
65 /* find out if we have a fixed did already */
66 if (!(cdir->d_did = cnid_lookup(vol->v_db, st, dir->d_did, upath,
68 memset(&ad, 0, sizeof(ad));
69 if (ad_open(upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY, 0, &ad) < 0) {
70 /* if we can't parse the AppleDouble header, return 0 for the DID */
73 /* ... else retrieve the DID entry into cdir->d_did */
74 memcpy(&cdir->d_did, ad_entry(&ad, ADEID_DID), sizeof(cdir->d_did));
75 ad_close(&ad, ADFLAGS_HF);
78 if (!(cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
79 upathlen, cdir->d_did))) {
81 /* last way of doing DIDs */
82 cdir->d_did = htonl( vol->v_lastdid++ );
83 #else /* USE_LASTDID */
84 lstp = lstat(upath, &lst) < 0 ? st : &lst;
86 /* mtab way of doing DIDs */
87 cdir->d_did = htonl( afpd_st_cnid ( lstp ) );
89 /* the old way of doing DIDs (default) */
90 cdir->d_did = htonl( CNID(lstp, 0) );
92 #endif /* USE_LASTDID */
95 #else /* AD_VERSION */
98 /* last way of doing DIDs */
99 cdir->d_did = htonl( vol->v_lastdid++ );
100 #else /* USE_LASTDID */
101 lstp = lstat(upath, &lst) < 0 ? st : &lst;
103 /* mtab way of doing DIDs */
104 cdir->d_did = htonl( afpd_st_cnid ( lstp ) );
106 /* the old way of doing DIDs (default) */
107 cdir->d_did = htonl( CNID(lstp, 0) );
108 #endif /* DID_MTAB */
109 #endif /* USE_LASTDID */
110 #endif /* AD_VERSION */
112 if ((edir = dirinsert( vol, cdir ))) {
114 if (strcmp(edir->d_name, cdir->d_name)) {
115 syslog(LOG_INFO, "WARNING: DID conflict for '%s' and '%s'. Are these the same file?", edir->d_name, cdir->d_name);
121 edir->d_name = cdir->d_name;
126 /* parent/child directories */
127 cdir->d_parent = dir;
128 dirchildadd(dir, cdir);
133 * Struct to save directory reading context in. Used to prevent
134 * O(n^2) searches on a directory.
144 #define SDBUFBRK 1024
146 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
149 int ibuflen, *rbuflen;
152 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
157 int did, ret, esz, len, first = 1;
158 char *path, *data, *end, *start;
159 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
160 u_int16_t sindex, maxsz, sz = 0;
162 if ( sd.sd_buflen == 0 ) {
163 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
164 syslog( LOG_ERR, "afp_enumerate: malloc: %m" );
168 sd.sd_buflen = SDBUFBRK;
173 memcpy( &vid, ibuf, sizeof( vid ));
174 ibuf += sizeof( vid );
176 if (( vol = getvolbyvid( vid )) == NULL ) {
178 return( AFPERR_PARAM );
181 memcpy( &did, ibuf, sizeof( did ));
182 ibuf += sizeof( did );
184 if (( dir = dirsearch( vol, did )) == NULL ) {
186 return( AFPERR_NODIR );
189 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
190 fbitmap = ntohs( fbitmap );
191 ibuf += sizeof( fbitmap );
193 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
194 dbitmap = ntohs( dbitmap );
195 ibuf += sizeof( dbitmap );
197 /* check for proper bitmaps -- the stuff in comments is for
198 * variable directory ids. */
199 if (!(fbitmap || dbitmap)
200 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
201 (dbitmap & (1 << DIRPBIT_PDID))*/) {
203 return AFPERR_BITMAP;
206 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
207 reqcnt = ntohs( reqcnt );
208 ibuf += sizeof( reqcnt );
210 memcpy( &sindex, ibuf, sizeof( sindex ));
211 sindex = ntohs( sindex );
212 ibuf += sizeof( sindex );
214 memcpy( &maxsz, ibuf, sizeof( maxsz ));
215 maxsz = ntohs( maxsz );
216 ibuf += sizeof( maxsz );
218 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
220 return( AFPERR_NODIR );
222 data = rbuf + 3 * sizeof( u_int16_t );
223 sz = 3 * sizeof( u_int16_t );
226 * Read the directory into a pre-malloced buffer, stored
228 * The end is indicated by a len of 0.
230 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
231 sd.sd_last = sd.sd_buf;
233 if (( dp = opendir( mtoupath(vol, path ))) == NULL ) {
235 return (errno == ENOTDIR) ? AFPERR_BADTYPE : AFPERR_NODIR;
238 end = sd.sd_buf + sd.sd_buflen;
239 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
240 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
243 if (!(validupath(vol, de->d_name)))
246 /* now check against too big a file */
247 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
250 len = strlen(de->d_name);
251 *(sd.sd_last)++ = len;
253 if ( sd.sd_last + len + 2 > end ) {
257 if ((buf = (char *) realloc( sd.sd_buf, sd.sd_buflen +
258 SDBUFBRK )) == NULL ) {
259 syslog( LOG_ERR, "afp_enumerate: realloc: %m" );
265 sd.sd_buflen += SDBUFBRK;
266 sd.sd_last = ( sd.sd_last - start ) + sd.sd_buf;
267 end = sd.sd_buf + sd.sd_buflen;
270 memcpy( sd.sd_last, de->d_name, len + 1 );
271 sd.sd_last += len + 1;
275 sd.sd_last = sd.sd_buf;
284 * Position sd_last as dictated by sindex.
286 if ( sindex < sd.sd_sindex ) {
288 sd.sd_last = sd.sd_buf;
290 while ( sd.sd_sindex < sindex ) {
291 len = *(sd.sd_last)++;
293 sd.sd_did = -1; /* invalidate sd struct to force re-read */
295 return( AFPERR_NOOBJ );
297 sd.sd_last += len + 1;
301 while (( len = *(sd.sd_last)) != 0 ) {
303 * If we've got all we need, send it.
305 if ( actcnt == reqcnt ) {
310 * Save the start position, in case we exceed the buffer
311 * limitation, and have to back up one.
316 if ( stat( sd.sd_last, &st ) < 0 ) {
317 syslog( LOG_DEBUG, "afp_enumerate: stat %s: %m", sd.sd_last );
318 sd.sd_last += len + 1;
323 * If a fil/dir is not a dir, it's a file. This is slightly
324 * inaccurate, since that means /dev/null is a file, /dev/printer
327 if ( S_ISDIR(st.st_mode)) {
328 if ( dbitmap == 0 ) {
329 sd.sd_last += len + 1;
332 path = utompath(vol, sd.sd_last);
333 dir = curdir->d_child;
335 if ( strcmp( dir->d_name, path ) == 0 ) {
338 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
340 if (!dir && ((dir = adddir( vol, curdir, path, strlen( path ),
341 sd.sd_last, len, &st)) == NULL)) {
347 if (( ret = getdirparams(vol, dbitmap, sd.sd_last, dir,
348 &st, data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
354 if ( fbitmap == 0 ) {
355 sd.sd_last += len + 1;
359 if (( ret = getfilparams(vol, fbitmap, utompath(vol, sd.sd_last),
360 curdir, &st, data + 2 * sizeof( u_char ), &esz )) !=
368 * Make sure entry is an even length, possibly with a null
372 *(data + 2 * sizeof( u_char ) + esz ) = '\0';
377 * Check if we've exceeded the size limit.
379 if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
380 if (first) { /* maxsz can't hold a single reply */
391 sz += esz + 2 * sizeof( u_char );
392 *data++ = esz + 2 * sizeof( u_char );
393 *data++ = S_ISDIR(st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
396 sd.sd_last += len + 1;
401 sd.sd_did = -1; /* invalidate sd struct to force re-read */
402 return( AFPERR_NOOBJ );
404 sd.sd_sindex = sindex + actcnt;
407 * All done, fill in misc junk in rbuf
409 fbitmap = htons( fbitmap );
410 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
411 rbuf += sizeof( fbitmap );
412 dbitmap = htons( dbitmap );
413 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
414 rbuf += sizeof( dbitmap );
415 actcnt = htons( actcnt );
416 memcpy( rbuf, &actcnt, sizeof( actcnt ));
417 rbuf += sizeof( actcnt );
423 /* why is this here? well, FPCatSearch is essentially an FPEnumerate
425 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
426 char *rbuf, int *rbuflen)
432 memcpy(&vid, ibuf, sizeof(vid));
436 if ((vol = getvolbyvid(vid)) == NULL)
440 * do a breadth-first search of directories:
441 * lookup did/name info.
442 * add to result if match
443 * check to see if we've exceeded our timelimit
444 * if yes, return current position
447 * we keep a copy of our current position in struct vol.
448 * if the next catsearch request for that volume isn't at
449 * at the current position, bail and return catchanged.