2 * $Id: enumerate.c,v 1.13 2002-01-04 04:45:47 sibaz 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"
43 adddir( vol, dir, name, namlen, upath, upathlen, st )
50 struct dir *cdir, *edir;
51 #if AD_VERSION > AD_VERSION1
53 #endif /* AD_VERSION > AD_VERSION1 */
56 struct stat lst, *lstp;
57 #endif /* USE_LASTDID */
59 if ((cdir = dirnew(namlen + 1)) == NULL) {
60 LOG(log_error, logtype_default, "adddir: malloc: %s", strerror(errno) );
63 strcpy( cdir->d_name, name );
64 cdir->d_name[namlen] = '\0';
68 #if AD_VERSION > AD_VERSION1
69 /* look in AD v2 header */
70 memset(&ad, 0, sizeof(ad));
71 if (ad_open(upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY, 0, &ad) >= 0) {
72 /* if we can parse the AppleDouble header, retrieve the DID entry into cdir->d_did */
73 memcpy(&cdir->d_did, ad_entry(&ad, ADEID_DID), sizeof(cdir->d_did));
74 ad_close(&ad, ADFLAGS_HF);
76 #endif /* AD_VERSION */
80 cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
81 upathlen, cdir->d_did);
84 if (cdir->d_did == 0) {
86 /* last way of doing DIDs */
87 cdir->d_did = htonl( vol->v_lastdid++ );
88 #else /* USE_LASTDID */
89 lstp = lstat(upath, &lst) < 0 ? st : &lst;
91 /* mtab way of doing DIDs */
92 cdir->d_did = htonl( afpd_st_cnid ( lstp ) );
94 /* the old way of doing DIDs (default) */
95 cdir->d_did = htonl( CNID(lstp, 0) );
97 #endif /* USE_LASTDID */
100 if ((edir = dirinsert( vol, cdir ))) {
103 if (strcmp(edir->d_name, cdir->d_name)) {
104 LOG(log_info, logtype_default, "WARNING: DID conflict for '%s' and '%s'. Are these the same file?", edir->d_name, cdir->d_name);
111 edir->d_name = cdir->d_name;
116 /* parent/child directories */
117 cdir->d_parent = dir;
118 dirchildadd(dir, cdir);
123 * Struct to save directory reading context in. Used to prevent
124 * O(n^2) searches on a directory.
134 #define SDBUFBRK 1024
136 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
139 int ibuflen, *rbuflen;
142 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
147 int did, ret, esz, len, first = 1;
148 char *path, *data, *end, *start;
149 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
150 u_int16_t sindex, maxsz, sz = 0;
152 if ( sd.sd_buflen == 0 ) {
153 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
154 LOG(log_error, logtype_default, "afp_enumerate: malloc: %s", strerror(errno) );
158 sd.sd_buflen = SDBUFBRK;
163 memcpy( &vid, ibuf, sizeof( vid ));
164 ibuf += sizeof( vid );
166 if (( vol = getvolbyvid( vid )) == NULL ) {
168 return( AFPERR_PARAM );
171 memcpy( &did, ibuf, sizeof( did ));
172 ibuf += sizeof( did );
174 if (( dir = dirsearch( vol, did )) == NULL ) {
176 return( AFPERR_NODIR );
179 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
180 fbitmap = ntohs( fbitmap );
181 ibuf += sizeof( fbitmap );
183 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
184 dbitmap = ntohs( dbitmap );
185 ibuf += sizeof( dbitmap );
187 /* check for proper bitmaps -- the stuff in comments is for
188 * variable directory ids. */
189 if (!(fbitmap || dbitmap)
190 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
191 (dbitmap & (1 << DIRPBIT_PDID))*/) {
193 return AFPERR_BITMAP;
196 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
197 reqcnt = ntohs( reqcnt );
198 ibuf += sizeof( reqcnt );
200 memcpy( &sindex, ibuf, sizeof( sindex ));
201 sindex = ntohs( sindex );
202 ibuf += sizeof( sindex );
204 memcpy( &maxsz, ibuf, sizeof( maxsz ));
205 maxsz = ntohs( maxsz );
206 ibuf += sizeof( maxsz );
208 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
210 return( AFPERR_NODIR );
212 data = rbuf + 3 * sizeof( u_int16_t );
213 sz = 3 * sizeof( u_int16_t );
216 * Read the directory into a pre-malloced buffer, stored
218 * The end is indicated by a len of 0.
220 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
221 sd.sd_last = sd.sd_buf;
223 if (( dp = opendir( mtoupath(vol, path ))) == NULL ) {
225 return (errno == ENOTDIR) ? AFPERR_BADTYPE : AFPERR_NODIR;
228 end = sd.sd_buf + sd.sd_buflen;
229 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
230 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
233 if (!(validupath(vol, de->d_name)))
236 /* check for vetoed filenames */
237 if (veto_file(vol->v_veto, de->d_name))
240 /* now check against too big a file */
241 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
244 len = strlen(de->d_name);
245 *(sd.sd_last)++ = len;
247 if ( sd.sd_last + len + 2 > end ) {
251 if ((buf = (char *) realloc( sd.sd_buf, sd.sd_buflen +
252 SDBUFBRK )) == NULL ) {
253 LOG(log_error, logtype_default, "afp_enumerate: realloc: %s",
260 sd.sd_buflen += SDBUFBRK;
261 sd.sd_last = ( sd.sd_last - start ) + sd.sd_buf;
262 end = sd.sd_buf + sd.sd_buflen;
265 memcpy( sd.sd_last, de->d_name, len + 1 );
266 sd.sd_last += len + 1;
270 sd.sd_last = sd.sd_buf;
279 * Position sd_last as dictated by sindex.
281 if ( sindex < sd.sd_sindex ) {
283 sd.sd_last = sd.sd_buf;
285 while ( sd.sd_sindex < sindex ) {
286 len = *(sd.sd_last)++;
288 sd.sd_did = -1; /* invalidate sd struct to force re-read */
290 return( AFPERR_NOOBJ );
292 sd.sd_last += len + 1;
296 while (( len = *(sd.sd_last)) != 0 ) {
298 * If we've got all we need, send it.
300 if ( actcnt == reqcnt ) {
305 * Save the start position, in case we exceed the buffer
306 * limitation, and have to back up one.
311 if ( stat( sd.sd_last, &st ) < 0 ) {
312 LOG(log_debug, logtype_default, "afp_enumerate: stat %s: %s",
313 sd.sd_last, strerror(errno) );
314 sd.sd_last += len + 1;
319 * If a fil/dir is not a dir, it's a file. This is slightly
320 * inaccurate, since that means /dev/null is a file, /dev/printer
323 if ( S_ISDIR(st.st_mode)) {
324 if ( dbitmap == 0 ) {
325 sd.sd_last += len + 1;
328 path = utompath(vol, sd.sd_last);
329 dir = curdir->d_child;
331 if ( strcmp( dir->d_name, path ) == 0 ) {
334 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
336 if (!dir && ((dir = adddir( vol, curdir, path, strlen( path ),
337 sd.sd_last, len, &st)) == NULL)) {
343 if (( ret = getdirparams(vol, dbitmap, sd.sd_last, dir,
344 &st, data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
350 if ( fbitmap == 0 ) {
351 sd.sd_last += len + 1;
355 if (( ret = getfilparams(vol, fbitmap, utompath(vol, sd.sd_last),
356 curdir, &st, data + 2 * sizeof( u_char ), &esz )) !=
364 * Make sure entry is an even length, possibly with a null
368 *(data + 2 * sizeof( u_char ) + esz ) = '\0';
373 * Check if we've exceeded the size limit.
375 if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
376 if (first) { /* maxsz can't hold a single reply */
387 sz += esz + 2 * sizeof( u_char );
388 *data++ = esz + 2 * sizeof( u_char );
389 *data++ = S_ISDIR(st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
392 sd.sd_last += len + 1;
397 sd.sd_did = -1; /* invalidate sd struct to force re-read */
398 return( AFPERR_NOOBJ );
400 sd.sd_sindex = sindex + actcnt;
403 * All done, fill in misc junk in rbuf
405 fbitmap = htons( fbitmap );
406 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
407 rbuf += sizeof( fbitmap );
408 dbitmap = htons( dbitmap );
409 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
410 rbuf += sizeof( dbitmap );
411 actcnt = htons( actcnt );
412 memcpy( rbuf, &actcnt, sizeof( actcnt ));
413 rbuf += sizeof( actcnt );
419 /* why is this here? well, FPCatSearch is essentially an FPEnumerate
421 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
422 char *rbuf, int *rbuflen)
428 memcpy(&vid, ibuf, sizeof(vid));
432 if ((vol = getvolbyvid(vid)) == NULL)
436 * do a breadth-first search of directories:
437 * lookup did/name info.
438 * add to result if match
439 * check to see if we've exceeded our timelimit
440 * if yes, return current position
443 * we keep a copy of our current position in struct vol.
444 * if the next catsearch request for that volume isn't at
445 * at the current position, bail and return catchanged.