2 * $Id: enumerate.c,v 1.9 2001-09-04 13:52:45 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: %s", strerror(errno) );
62 strcpy( cdir->d_name, name );
63 cdir->d_name[namlen] = '\0';
67 #if AD_VERSION > AD_VERSION1
68 /* look in AD v2 header */
69 memset(&ad, 0, sizeof(ad));
70 if (ad_open(upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY, 0, &ad) >= 0) {
71 /* if we can parse the AppleDouble header, retrieve the DID entry into cdir->d_did */
72 memcpy(&cdir->d_did, ad_entry(&ad, ADEID_DID), sizeof(cdir->d_did));
73 ad_close(&ad, ADFLAGS_HF);
75 #endif /* AD_VERSION */
79 cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
80 upathlen, cdir->d_did);
83 if (cdir->d_did == 0) {
85 /* last way of doing DIDs */
86 cdir->d_did = htonl( vol->v_lastdid++ );
87 #else /* USE_LASTDID */
88 lstp = lstat(upath, &lst) < 0 ? st : &lst;
90 /* mtab way of doing DIDs */
91 cdir->d_did = htonl( afpd_st_cnid ( lstp ) );
93 /* the old way of doing DIDs (default) */
94 cdir->d_did = htonl( CNID(lstp, 0) );
96 #endif /* USE_LASTDID */
99 if ((edir = dirinsert( vol, cdir ))) {
101 if (strcmp(edir->d_name, cdir->d_name)) {
102 syslog(LOG_INFO, "WARNING: DID conflict for '%s' and '%s'. Are these the same file?", edir->d_name, cdir->d_name);
108 edir->d_name = cdir->d_name;
113 /* parent/child directories */
114 cdir->d_parent = dir;
115 dirchildadd(dir, cdir);
120 * Struct to save directory reading context in. Used to prevent
121 * O(n^2) searches on a directory.
131 #define SDBUFBRK 1024
133 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
136 int ibuflen, *rbuflen;
139 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
144 int did, ret, esz, len, first = 1;
145 char *path, *data, *end, *start;
146 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
147 u_int16_t sindex, maxsz, sz = 0;
149 if ( sd.sd_buflen == 0 ) {
150 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
151 syslog( LOG_ERR, "afp_enumerate: malloc: %s", strerror(errno) );
155 sd.sd_buflen = SDBUFBRK;
160 memcpy( &vid, ibuf, sizeof( vid ));
161 ibuf += sizeof( vid );
163 if (( vol = getvolbyvid( vid )) == NULL ) {
165 return( AFPERR_PARAM );
168 memcpy( &did, ibuf, sizeof( did ));
169 ibuf += sizeof( did );
171 if (( dir = dirsearch( vol, did )) == NULL ) {
173 return( AFPERR_NODIR );
176 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
177 fbitmap = ntohs( fbitmap );
178 ibuf += sizeof( fbitmap );
180 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
181 dbitmap = ntohs( dbitmap );
182 ibuf += sizeof( dbitmap );
184 /* check for proper bitmaps -- the stuff in comments is for
185 * variable directory ids. */
186 if (!(fbitmap || dbitmap)
187 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
188 (dbitmap & (1 << DIRPBIT_PDID))*/) {
190 return AFPERR_BITMAP;
193 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
194 reqcnt = ntohs( reqcnt );
195 ibuf += sizeof( reqcnt );
197 memcpy( &sindex, ibuf, sizeof( sindex ));
198 sindex = ntohs( sindex );
199 ibuf += sizeof( sindex );
201 memcpy( &maxsz, ibuf, sizeof( maxsz ));
202 maxsz = ntohs( maxsz );
203 ibuf += sizeof( maxsz );
205 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
207 return( AFPERR_NODIR );
209 data = rbuf + 3 * sizeof( u_int16_t );
210 sz = 3 * sizeof( u_int16_t );
213 * Read the directory into a pre-malloced buffer, stored
215 * The end is indicated by a len of 0.
217 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
218 sd.sd_last = sd.sd_buf;
220 if (( dp = opendir( mtoupath(vol, path ))) == NULL ) {
222 return (errno == ENOTDIR) ? AFPERR_BADTYPE : AFPERR_NODIR;
225 end = sd.sd_buf + sd.sd_buflen;
226 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
227 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
230 if (!(validupath(vol, de->d_name)))
233 /* check for vetoed filenames */
234 if (veto_file(vol->v_veto, de->d_name))
237 /* now check against too big a file */
238 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
241 len = strlen(de->d_name);
242 *(sd.sd_last)++ = len;
244 if ( sd.sd_last + len + 2 > end ) {
248 if ((buf = (char *) realloc( sd.sd_buf, sd.sd_buflen +
249 SDBUFBRK )) == NULL ) {
250 syslog( LOG_ERR, "afp_enumerate: realloc: %s",
257 sd.sd_buflen += SDBUFBRK;
258 sd.sd_last = ( sd.sd_last - start ) + sd.sd_buf;
259 end = sd.sd_buf + sd.sd_buflen;
262 memcpy( sd.sd_last, de->d_name, len + 1 );
263 sd.sd_last += len + 1;
267 sd.sd_last = sd.sd_buf;
276 * Position sd_last as dictated by sindex.
278 if ( sindex < sd.sd_sindex ) {
280 sd.sd_last = sd.sd_buf;
282 while ( sd.sd_sindex < sindex ) {
283 len = *(sd.sd_last)++;
285 sd.sd_did = -1; /* invalidate sd struct to force re-read */
287 return( AFPERR_NOOBJ );
289 sd.sd_last += len + 1;
293 while (( len = *(sd.sd_last)) != 0 ) {
295 * If we've got all we need, send it.
297 if ( actcnt == reqcnt ) {
302 * Save the start position, in case we exceed the buffer
303 * limitation, and have to back up one.
308 if ( stat( sd.sd_last, &st ) < 0 ) {
309 syslog( LOG_DEBUG, "afp_enumerate: stat %s: %s",
310 sd.sd_last, strerror(errno) );
311 sd.sd_last += len + 1;
316 * If a fil/dir is not a dir, it's a file. This is slightly
317 * inaccurate, since that means /dev/null is a file, /dev/printer
320 if ( S_ISDIR(st.st_mode)) {
321 if ( dbitmap == 0 ) {
322 sd.sd_last += len + 1;
325 path = utompath(vol, sd.sd_last);
326 dir = curdir->d_child;
328 if ( strcmp( dir->d_name, path ) == 0 ) {
331 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
333 if (!dir && ((dir = adddir( vol, curdir, path, strlen( path ),
334 sd.sd_last, len, &st)) == NULL)) {
340 if (( ret = getdirparams(vol, dbitmap, sd.sd_last, dir,
341 &st, data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
347 if ( fbitmap == 0 ) {
348 sd.sd_last += len + 1;
352 if (( ret = getfilparams(vol, fbitmap, utompath(vol, sd.sd_last),
353 curdir, &st, data + 2 * sizeof( u_char ), &esz )) !=
361 * Make sure entry is an even length, possibly with a null
365 *(data + 2 * sizeof( u_char ) + esz ) = '\0';
370 * Check if we've exceeded the size limit.
372 if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
373 if (first) { /* maxsz can't hold a single reply */
384 sz += esz + 2 * sizeof( u_char );
385 *data++ = esz + 2 * sizeof( u_char );
386 *data++ = S_ISDIR(st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
389 sd.sd_last += len + 1;
394 sd.sd_did = -1; /* invalidate sd struct to force re-read */
395 return( AFPERR_NOOBJ );
397 sd.sd_sindex = sindex + actcnt;
400 * All done, fill in misc junk in rbuf
402 fbitmap = htons( fbitmap );
403 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
404 rbuf += sizeof( fbitmap );
405 dbitmap = htons( dbitmap );
406 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
407 rbuf += sizeof( dbitmap );
408 actcnt = htons( actcnt );
409 memcpy( rbuf, &actcnt, sizeof( actcnt ));
410 rbuf += sizeof( actcnt );
416 /* why is this here? well, FPCatSearch is essentially an FPEnumerate
418 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
419 char *rbuf, int *rbuflen)
425 memcpy(&vid, ibuf, sizeof(vid));
429 if ((vol = getvolbyvid(vid)) == NULL)
433 * do a breadth-first search of directories:
434 * lookup did/name info.
435 * add to result if match
436 * check to see if we've exceeded our timelimit
437 * if yes, return current position
440 * we keep a copy of our current position in struct vol.
441 * if the next catsearch request for that volume isn't at
442 * at the current position, bail and return catchanged.