2 * $Id: enumerate.c,v 1.9.2.3 2002-02-07 23:56:04 srittau 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"
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 syslog( LOG_ERR, "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);
86 if (cdir->d_did == 0) {
88 /* last way of doing DIDs */
89 cdir->d_did = htonl( vol->v_lastdid++ );
90 #else /* USE_LASTDID */
91 lstp = lstat(upath, &lst) < 0 ? st : &lst;
93 /* mtab way of doing DIDs */
94 cdir->d_did = htonl( afpd_st_cnid ( lstp ) );
96 /* the old way of doing DIDs (default) */
97 cdir->d_did = htonl( CNID(lstp, 0) );
99 #endif /* USE_LASTDID */
102 if ((edir = dirinsert( vol, cdir ))) {
105 if (strcmp(edir->d_name, cdir->d_name)) {
106 syslog(LOG_INFO, "WARNING: DID conflict for '%s' and '%s'. Are these the same file?", edir->d_name, cdir->d_name);
113 edir->d_name = cdir->d_name;
118 /* parent/child directories */
119 cdir->d_parent = dir;
120 dirchildadd(dir, cdir);
125 * Struct to save directory reading context in. Used to prevent
126 * O(n^2) searches on a directory.
136 #define SDBUFBRK 1024
138 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
141 int ibuflen, *rbuflen;
144 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
149 int did, ret, esz, len, first = 1;
150 char *path, *data, *end, *start;
151 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
152 u_int16_t sindex, maxsz, sz = 0;
154 if ( sd.sd_buflen == 0 ) {
155 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
156 syslog( LOG_ERR, "afp_enumerate: malloc: %s", strerror(errno) );
160 sd.sd_buflen = SDBUFBRK;
165 memcpy( &vid, ibuf, sizeof( vid ));
166 ibuf += sizeof( vid );
168 if (( vol = getvolbyvid( vid )) == NULL ) {
170 return( AFPERR_PARAM );
173 memcpy( &did, ibuf, sizeof( did ));
174 ibuf += sizeof( did );
176 if (( dir = dirsearch( vol, did )) == NULL ) {
178 return( AFPERR_NODIR );
181 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
182 fbitmap = ntohs( fbitmap );
183 ibuf += sizeof( fbitmap );
185 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
186 dbitmap = ntohs( dbitmap );
187 ibuf += sizeof( dbitmap );
189 /* check for proper bitmaps -- the stuff in comments is for
190 * variable directory ids. */
191 if (!(fbitmap || dbitmap)
192 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
193 (dbitmap & (1 << DIRPBIT_PDID))*/) {
195 return AFPERR_BITMAP;
198 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
199 reqcnt = ntohs( reqcnt );
200 ibuf += sizeof( reqcnt );
202 memcpy( &sindex, ibuf, sizeof( sindex ));
203 sindex = ntohs( sindex );
204 ibuf += sizeof( sindex );
206 memcpy( &maxsz, ibuf, sizeof( maxsz ));
207 maxsz = ntohs( maxsz );
208 ibuf += sizeof( maxsz );
210 maxsz = min(maxsz, *rbuflen);
212 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
214 return( AFPERR_NODIR );
216 data = rbuf + 3 * sizeof( u_int16_t );
217 sz = 3 * sizeof( u_int16_t );
220 * Read the directory into a pre-malloced buffer, stored
222 * The end is indicated by a len of 0.
224 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
225 sd.sd_last = sd.sd_buf;
227 if (( dp = opendir( mtoupath(vol, path ))) == NULL ) {
229 return (errno == ENOTDIR) ? AFPERR_BADTYPE : AFPERR_NODIR;
232 end = sd.sd_buf + sd.sd_buflen;
233 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
234 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
237 if (!(validupath(vol, de->d_name)))
240 /* check for vetoed filenames */
241 if (veto_file(vol->v_veto, de->d_name))
244 /* now check against too big a file */
245 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
248 len = strlen(de->d_name);
249 *(sd.sd_last)++ = len;
251 if ( sd.sd_last + len + 2 > end ) {
255 if ((buf = (char *) realloc( sd.sd_buf, sd.sd_buflen +
256 SDBUFBRK )) == NULL ) {
257 syslog( LOG_ERR, "afp_enumerate: realloc: %s",
264 sd.sd_buflen += SDBUFBRK;
265 sd.sd_last = ( sd.sd_last - start ) + sd.sd_buf;
266 end = sd.sd_buf + sd.sd_buflen;
269 memcpy( sd.sd_last, de->d_name, len + 1 );
270 sd.sd_last += len + 1;
274 sd.sd_last = sd.sd_buf;
283 * Position sd_last as dictated by sindex.
285 if ( sindex < sd.sd_sindex ) {
287 sd.sd_last = sd.sd_buf;
289 while ( sd.sd_sindex < sindex ) {
290 len = *(sd.sd_last)++;
292 sd.sd_did = -1; /* invalidate sd struct to force re-read */
294 return( AFPERR_NOOBJ );
296 sd.sd_last += len + 1;
300 while (( len = *(sd.sd_last)) != 0 ) {
302 * If we've got all we need, send it.
304 if ( actcnt == reqcnt ) {
309 * Save the start position, in case we exceed the buffer
310 * limitation, and have to back up one.
315 if ( stat( sd.sd_last, &st ) < 0 ) {
316 syslog( LOG_DEBUG, "afp_enumerate: stat %s: %s",
317 sd.sd_last, strerror(errno) );
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.