2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #include <sys/param.h>
17 #include <atalk/logger.h>
18 #include <atalk/afp.h>
19 #include <atalk/adouble.h>
20 #include <atalk/vfs.h>
21 #include <atalk/cnid.h>
22 #include <atalk/util.h>
23 #include <atalk/bstrlib.h>
24 #include <atalk/bstradd.h>
25 #include <atalk/globals.h>
26 #include <atalk/netatalk_conf.h>
29 #include "directory.h"
36 #define min(a,b) ((a)<(b)?(a):(b))
39 * Struct to save directory reading context in. Used to prevent
40 * O(n^2) searches on a directory.
48 unsigned int sd_sindex;
52 static int enumerate_loop(struct dirent *de, char *mname _U_, void *data)
54 struct savedir *sd = data;
58 end = sd->sd_buf + sd->sd_buflen;
59 len = strlen(de->d_name);
60 *(sd->sd_last)++ = len;
61 lenm = 0; /* strlen(mname);*/
62 if ( sd->sd_last + len +lenm + 4 > end ) {
66 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
67 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
73 sd->sd_buflen += SDBUFBRK;
74 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
75 end = sd->sd_buf + sd->sd_buflen;
78 memcpy( sd->sd_last, de->d_name, len + 1 );
79 sd->sd_last += len + 1;
81 *(sd->sd_last)++ = lenm;
82 memcpy( sd->sd_last, mname, lenm + 1 );
83 sd->sd_last += lenm + 1;
88 /* -----------------------------
90 * Doesn't work with dangling symlink
92 * - Move a folder with a dangling symlink in the trash
94 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
95 * and the Mac doesn't try to call afp_delete!
97 * Another option for symlink
99 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
102 char *check_dirent(const struct vol *vol, char *name)
104 if (!strcmp(name, "..") || !strcmp(name, "."))
107 if (!vol->vfs->vfs_validupath(vol, name))
110 /* check for vetoed filenames */
111 if (veto_file(vol->v_veto, name))
117 if (NULL == (m_name = utompath(vol, name, 0, utf8_encoding())))
120 /* now check against too big a file */
121 if (strlen(m_name) > vol->max_filename)
127 /* ----------------------------- */
129 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
136 if (NULL == ( dp = opendir( name)) ) {
140 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
141 if (!(m_name = check_dirent(vol, de->d_name)))
145 if (fn && fn(de,m_name, data) < 0) {
154 /* This is the maximal length of a single entry for a file/dir in the reply
155 block if all bits in the file/dir bitmap are set: header(4) + params(104) +
156 macnamelength(1) + macname(31) + utf8(4) + utf8namelen(2) + utf8name(255) +
159 #define REPLY_PARAM_MAXLEN (4 + 104 + 1 + MACFILELEN + 4 + 2 + UTF8FILELEN_EARLY + 1)
161 /* ----------------------------- */
162 static int enumerate(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,
167 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
170 int did, ret, len, first = 1;
173 uint16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
175 uint32_t sindex, maxsz, sz = 0;
180 if ( sd.sd_buflen == 0 ) {
181 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
182 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
186 sd.sd_buflen = SDBUFBRK;
191 memcpy( &vid, ibuf, sizeof( vid ));
192 ibuf += sizeof( vid );
194 if (NULL == ( vol = getvolbyvid( vid )) ) {
196 return( AFPERR_PARAM );
199 memcpy( &did, ibuf, sizeof( did ));
200 ibuf += sizeof( did );
202 if (NULL == ( dir = dirlookup( vol, did )) ) {
204 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
207 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
208 fbitmap = ntohs( fbitmap );
209 ibuf += sizeof( fbitmap );
211 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
212 dbitmap = ntohs( dbitmap );
213 ibuf += sizeof( dbitmap );
215 /* check for proper bitmaps -- the stuff in comments is for
216 * variable directory ids. */
217 if (!(fbitmap || dbitmap)
218 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
219 (dbitmap & (1 << DIRPBIT_PDID))*/) {
221 return AFPERR_BITMAP;
224 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
225 reqcnt = ntohs( reqcnt );
226 ibuf += sizeof( reqcnt );
229 memcpy( &sindex, ibuf, sizeof( sindex ));
230 sindex = ntohl( sindex );
231 ibuf += sizeof( sindex );
234 memcpy( &temp16, ibuf, sizeof( temp16 ));
235 sindex = ntohs( temp16 );
236 ibuf += sizeof( temp16 );
241 return AFPERR_PARAM ;
245 memcpy( &maxsz, ibuf, sizeof( maxsz ));
246 maxsz = ntohl( maxsz );
247 ibuf += sizeof( maxsz );
250 memcpy( &temp16, ibuf, sizeof( temp16 ));
251 maxsz = ntohs( temp16 );
252 ibuf += sizeof( temp16 );
256 header *=sizeof( u_char );
258 maxsz = min(maxsz, *rbuflen - REPLY_PARAM_MAXLEN);
259 o_path = cname( vol, dir, &ibuf );
261 if (afp_errno == AFPERR_NOOBJ)
262 afp_errno = AFPERR_NODIR;
265 if (NULL == o_path ) {
266 return get_afp_errno(AFPERR_NOOBJ);
268 if ( *o_path->m_name != '\0') {
269 /* it's a file or it's a dir and extendir() was unable to chdir in it */
270 return path_error(o_path, AFPERR_NODIR );
273 LOG(log_debug, logtype_afpd, "enumerate(\"%s/%s\", f/d:%04x/%04x, rc:%u, i:%u, max:%u)",
274 getcwdpath(), o_path->u_name, fbitmap, dbitmap, reqcnt, sindex, maxsz);
276 data = rbuf + 3 * sizeof( uint16_t );
277 sz = 3 * sizeof( uint16_t ); /* fbitmap, dbitmap, reqcount */
280 * Read the directory into a pre-malloced buffer, stored
282 * The end is indicated by a len of 0.
284 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
285 sd.sd_last = sd.sd_buf;
286 /* if dir was in the cache we don't have the inode */
287 if (( !o_path->st_valid && lstat( ".", &o_path->st ) < 0 ) ||
288 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
290 LOG(log_error, logtype_afpd, "enumerate: loop error: %s (%d)", strerror(errno), errno);
293 return AFPERR_ACCESS;
295 return AFPERR_BADTYPE;
302 setdiroffcnt(curdir, &o_path->st, ret);
305 sd.sd_last = sd.sd_buf;
309 sd.sd_did = curdir->d_did;
313 * Position sd_last as dictated by sindex.
315 if ( sindex < sd.sd_sindex ) {
317 sd.sd_last = sd.sd_buf;
319 while ( sd.sd_sindex < sindex ) {
320 len = (unsigned char)*(sd.sd_last)++;
322 sd.sd_did = 0; /* invalidate sd struct to force re-read */
323 return( AFPERR_NOOBJ );
325 sd.sd_last += len + 1;
329 while (( len = (unsigned char)*(sd.sd_last)) != 0 ) {
331 * If we've got all we need, send it.
333 if ( actcnt == reqcnt ) {
338 * Save the start position, in case we exceed the buffer
339 * limitation, and have to back up one.
344 if (*sd.sd_last == 0) {
345 /* stat() already failed on this one */
346 sd.sd_last += len + 1;
349 memset(&s_path, 0, sizeof(s_path));
351 /* conversions on the fly */
352 const char *convname;
353 s_path.u_name = sd.sd_last;
354 if (ad_convert(sd.sd_last, &s_path.st, vol, &convname) == 0 && convname) {
355 s_path.u_name = (char *)convname;
358 if (of_stat( &s_path) < 0 ) {
359 /* so the next time it won't try to stat it again
360 * another solution would be to invalidate the cache with
361 * sd.sd_did = 0 but if it's not ENOENT error it will start again
364 sd.sd_last += len + 1;
365 curdir->d_offcnt--; /* a little lie */
369 /* Fixup CNID db if ad_convert resulted in a rename (then convname != NULL) */
371 s_path.id = cnid_lookup(vol->v_cdb, &s_path.st, curdir->d_did, sd.sd_last, strlen(sd.sd_last));
372 if (s_path.id != CNID_INVALID) {
373 if (cnid_update(vol->v_cdb, s_path.id, &s_path.st, curdir->d_did, convname, strlen(convname)) != 0)
374 LOG(log_error, logtype_afpd, "enumerate: error updating CNID of \"%s\"", fullpathname(convname));
378 sd.sd_last += len + 1;
379 s_path.m_name = NULL;
382 * If a fil/dir is not a dir, it's a file. This is slightly
383 * inaccurate, since that means /dev/null is a file, /dev/printer
386 if ( S_ISDIR(s_path.st.st_mode)) {
387 if ( dbitmap == 0 ) {
390 int len = strlen(s_path.u_name);
391 if ((dir = dircache_search_by_name(vol, curdir, s_path.u_name, len)) == NULL) {
392 if ((dir = dir_add(vol, curdir, &s_path, len)) == NULL) {
393 LOG(log_error, logtype_afpd, "enumerate(vid:%u, did:%u, name:'%s'): error adding dir: '%s'",
394 ntohs(vid), ntohl(did), o_path->u_name, s_path.u_name);
398 if ((ret = getdirparams(obj, vol, dbitmap, &s_path, dir, data + header , &esz)) != AFP_OK)
402 if ( fbitmap == 0 ) {
405 /* files are added to the dircache in getfilparams() -> getmetadata() */
406 if (AFP_OK != ( ret = getfilparams(obj, vol, fbitmap, &s_path, curdir,
407 data + header, &esz, 1)) ) {
413 * Make sure entry is an even length, possibly with a null
416 if ( (esz + header) & 1 ) {
417 *(data + header + esz ) = '\0';
422 * Check if we've exceeded the size limit.
424 if ( maxsz < sz + esz + header) {
425 if (first) { /* maxsz can't hold a single reply */
437 temp16 = htons( esz + header );
438 memcpy( data, &temp16, sizeof( temp16 ));
439 data += sizeof(temp16);
442 *data++ = esz + header;
445 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
451 /* FIXME if we rollover 16 bits and it's not FPEnumerateExt2 */
455 sd.sd_did = 0; /* invalidate sd struct to force re-read */
457 * in case were converting adouble stuff:
458 * after enumerating the whole dir we should have converted everything
459 * thus the .AppleDouble dir shouls be empty thus we can no try to
462 if (vol->v_adouble == AD_VERSION_EA && ! (vol->v_flags & AFPVOL_NOV2TOEACONV))
463 (void)rmdir(".AppleDouble");
465 return( AFPERR_NOOBJ );
467 sd.sd_sindex = sindex + actcnt;
470 * All done, fill in misc junk in rbuf
472 fbitmap = htons( fbitmap );
473 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
474 rbuf += sizeof( fbitmap );
475 dbitmap = htons( dbitmap );
476 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
477 rbuf += sizeof( dbitmap );
478 actcnt = htons( actcnt );
479 memcpy( rbuf, &actcnt, sizeof( actcnt ));
480 rbuf += sizeof( actcnt );
485 /* ----------------------------- */
486 int afp_enumerate(AFPObj *obj, char *ibuf, size_t ibuflen,
490 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
493 /* ----------------------------- */
494 int afp_enumerate_ext(AFPObj *obj, char *ibuf, size_t ibuflen,
498 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
501 /* ----------------------------- */
502 int afp_enumerate_ext2(AFPObj *obj, char *ibuf, size_t ibuflen,
506 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);