2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
12 #include <sys/syslog.h>
13 #include <sys/types.h>
16 #include <sys/param.h>
18 #include <netatalk/endian.h>
19 #include <atalk/afp.h>
20 #include <atalk/adouble.h>
21 #include <atalk/cnid.h>
24 #include "directory.h"
31 adddir( vol, dir, name, namlen, upath, upathlen, st )
38 struct dir *cdir, *edir;
39 #if AD_VERSION > AD_VERSION1
44 struct stat lst, *lstp;
47 if ((cdir = dirnew(namlen + 1)) == NULL) {
48 syslog( LOG_ERR, "adddir: malloc: %m" );
51 strcpy( cdir->d_name, name );
52 cdir->d_name[namlen] = '\0';
54 #if AD_VERSION > AD_VERSION1
55 /* find out if we have a fixed did already */
56 if (!(cdir->d_did = cnid_lookup(vol->v_db, st, dir->d_did, upath,
58 memset(&ad, 0, sizeof(ad));
59 if (ad_open(upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY, 0, &ad) < 0)
62 memcpy(&cdir->d_did, ad_entry(&ad, ADEID_DID), sizeof(cdir->d_did));
63 ad_close(&ad, ADFLAGS_HF);
66 if (!(cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
67 upathlen, cdir->d_did))) {
69 cdir->d_did = htonl( vol->v_lastdid++ );
71 lstp = lstat(upath, &lst) < 0 ? st : &lst;
72 cdir->d_did = htonl( CNID(lstp, 0) );
79 cdir->d_did = htonl( vol->v_lastdid++ );
81 lstp = lstat(upath, &lst) < 0 ? st : &lst;
82 cdir->d_did = htonl( CNID(lstp, 0) );
86 if (edir = dirinsert( vol, cdir )) {
88 if (strcmp(edir->d_name, cdir->d_name)) {
89 syslog(LOG_INFO, "WARNING: DID conflict for '%s' and '%s'. Are these the same file?", edir->d_name, cdir->d_name);
95 edir->d_name = cdir->d_name;
100 /* parent/child directories */
101 cdir->d_parent = dir;
102 dirchildadd(dir, cdir);
107 * Struct to save directory reading context in. Used to prevent
108 * O(n^2) searches on a directory.
118 #define SDBUFBRK 1024
120 int afp_enumerate(obj, ibuf, ibuflen, rbuf, rbuflen )
123 int ibuflen, *rbuflen;
126 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
131 int did, ret, esz, len, first = 1;
132 char *path, *data, *end, *start;
133 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
134 u_int16_t sindex, maxsz, sz = 0;
136 if ( sd.sd_buflen == 0 ) {
137 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
138 syslog( LOG_ERR, "afp_enumerate: malloc: %m" );
142 sd.sd_buflen = SDBUFBRK;
147 memcpy( &vid, ibuf, sizeof( vid ));
148 ibuf += sizeof( vid );
150 if (( vol = getvolbyvid( vid )) == NULL ) {
152 return( AFPERR_PARAM );
155 memcpy( &did, ibuf, sizeof( did ));
156 ibuf += sizeof( did );
158 if (( dir = dirsearch( vol, did )) == NULL ) {
160 return( AFPERR_NODIR );
163 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
164 fbitmap = ntohs( fbitmap );
165 ibuf += sizeof( fbitmap );
167 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
168 dbitmap = ntohs( dbitmap );
169 ibuf += sizeof( dbitmap );
171 /* check for proper bitmaps -- the stuff in comments is for
172 * variable directory ids. */
173 if (!(fbitmap || dbitmap)
174 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
175 (dbitmap & (1 << DIRPBIT_PDID))*/) {
177 return AFPERR_BITMAP;
180 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
181 reqcnt = ntohs( reqcnt );
182 ibuf += sizeof( reqcnt );
184 memcpy( &sindex, ibuf, sizeof( sindex ));
185 sindex = ntohs( sindex );
186 ibuf += sizeof( sindex );
188 memcpy( &maxsz, ibuf, sizeof( maxsz ));
189 maxsz = ntohs( maxsz );
190 ibuf += sizeof( maxsz );
192 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
194 return( AFPERR_NODIR );
196 data = rbuf + 3 * sizeof( u_int16_t );
197 sz = 3 * sizeof( u_int16_t );
200 * Read the directory into a pre-malloced buffer, stored
202 * The end is indicated by a len of 0.
204 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
205 sd.sd_last = sd.sd_buf;
207 if (( dp = opendir( mtoupath(vol, path ))) == NULL ) {
209 return (errno == ENOTDIR) ? AFPERR_BADTYPE : AFPERR_NODIR;
212 end = sd.sd_buf + sd.sd_buflen;
213 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
214 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
217 if (!(validupath(vol, de->d_name)))
220 /* now check against too big a file */
221 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
224 len = strlen(de->d_name);
225 *(sd.sd_last)++ = len;
227 if ( sd.sd_last + len + 2 > end ) {
231 if ((buf = (char *) realloc( sd.sd_buf, sd.sd_buflen +
232 SDBUFBRK )) == NULL ) {
233 syslog( LOG_ERR, "afp_enumerate: realloc: %m" );
239 sd.sd_buflen += SDBUFBRK;
240 sd.sd_last = ( sd.sd_last - start ) + sd.sd_buf;
241 end = sd.sd_buf + sd.sd_buflen;
244 memcpy( sd.sd_last, de->d_name, len + 1 );
245 sd.sd_last += len + 1;
249 sd.sd_last = sd.sd_buf;
258 * Position sd_last as dictated by sindex.
260 if ( sindex < sd.sd_sindex ) {
262 sd.sd_last = sd.sd_buf;
264 while ( sd.sd_sindex < sindex ) {
265 len = *(sd.sd_last)++;
267 sd.sd_did = -1; /* invalidate sd struct to force re-read */
269 return( AFPERR_NOOBJ );
271 sd.sd_last += len + 1;
275 while (( len = *(sd.sd_last)) != 0 ) {
277 * If we've got all we need, send it.
279 if ( actcnt == reqcnt ) {
284 * Save the start position, in case we exceed the buffer
285 * limitation, and have to back up one.
290 if ( stat( sd.sd_last, &st ) < 0 ) {
291 syslog( LOG_DEBUG, "afp_enumerate: stat %s: %m", sd.sd_last );
292 sd.sd_last += len + 1;
297 * If a fil/dir is not a dir, it's a file. This is slightly
298 * inaccurate, since that means /dev/null is a file, /dev/printer
301 if ( S_ISDIR(st.st_mode)) {
302 if ( dbitmap == 0 ) {
303 sd.sd_last += len + 1;
306 path = utompath(vol, sd.sd_last);
307 dir = curdir->d_child;
309 if ( strcmp( dir->d_name, path ) == 0 ) {
312 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
314 if (!dir && ((dir = adddir( vol, curdir, path, strlen( path ),
315 sd.sd_last, len, &st)) == NULL)) {
321 if (( ret = getdirparams(vol, dbitmap, sd.sd_last, dir,
322 &st, data + 2 * sizeof( u_char ), &esz )) != AFP_OK ) {
328 if ( fbitmap == 0 ) {
329 sd.sd_last += len + 1;
333 if (( ret = getfilparams(vol, fbitmap, utompath(vol, sd.sd_last),
334 curdir, &st, data + 2 * sizeof( u_char ), &esz )) !=
342 * Make sure entry is an even length, possibly with a null
346 *(data + 2 * sizeof( u_char ) + esz ) = '\0';
351 * Check if we've exceeded the size limit.
353 if ( maxsz < sz + esz + 2 * sizeof( u_char )) {
354 if (first) { /* maxsz can't hold a single reply */
365 sz += esz + 2 * sizeof( u_char );
366 *data++ = esz + 2 * sizeof( u_char );
367 *data++ = S_ISDIR(st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
370 sd.sd_last += len + 1;
375 sd.sd_did = -1; /* invalidate sd struct to force re-read */
376 return( AFPERR_NOOBJ );
378 sd.sd_sindex = sindex + actcnt;
381 * All done, fill in misc junk in rbuf
383 fbitmap = htons( fbitmap );
384 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
385 rbuf += sizeof( fbitmap );
386 dbitmap = htons( dbitmap );
387 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
388 rbuf += sizeof( dbitmap );
389 actcnt = htons( actcnt );
390 memcpy( rbuf, &actcnt, sizeof( actcnt ));
391 rbuf += sizeof( actcnt );
397 /* why is this here? well, FPCatSearch is essentially an FPEnumerate
399 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
400 char *rbuf, int *rbuflen)
406 memcpy(&vid, ibuf, sizeof(vid));
410 if ((vol = getvolbyvid(vid)) == NULL)
414 * do a breadth-first search of directories:
415 * lookup did/name info.
416 * add to result if match
417 * check to see if we've exceeded our timelimit
418 * if yes, return current position
421 * we keep a copy of our current position in struct vol.
422 * if the next catsearch request for that volume isn't at
423 * at the current position, bail and return catchanged.