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>
27 #include "directory.h"
35 #define min(a,b) ((a)<(b)?(a):(b))
38 * Struct to save directory reading context in. Used to prevent
39 * O(n^2) searches on a directory.
47 unsigned int sd_sindex;
51 static int enumerate_loop(struct dirent *de, char *mname _U_, void *data)
53 struct savedir *sd = data;
57 end = sd->sd_buf + sd->sd_buflen;
58 len = strlen(de->d_name);
59 *(sd->sd_last)++ = len;
60 lenm = 0; /* strlen(mname);*/
61 if ( sd->sd_last + len +lenm + 4 > end ) {
65 if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
66 LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
72 sd->sd_buflen += SDBUFBRK;
73 sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
74 end = sd->sd_buf + sd->sd_buflen;
77 memcpy( sd->sd_last, de->d_name, len + 1 );
78 sd->sd_last += len + 1;
80 *(sd->sd_last)++ = lenm;
81 memcpy( sd->sd_last, mname, lenm + 1 );
82 sd->sd_last += lenm + 1;
87 /* -----------------------------
89 * Doesn't work with dangling symlink
91 * - Move a folder with a dangling symlink in the trash
93 * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
94 * and the Mac doesn't try to call afp_delete!
96 * Another option for symlink
98 * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
101 char *check_dirent(const struct vol *vol, char *name)
103 if (!strcmp(name, "..") || !strcmp(name, "."))
106 if (!vol->vfs->vfs_validupath(vol, name))
109 /* check for vetoed filenames */
110 if (veto_file(vol->v_veto, name))
116 if (NULL == (m_name = utompath(vol, name, 0, utf8_encoding())))
119 /* now check against too big a file */
120 if (strlen(m_name) > vol->max_filename)
126 /* ----------------------------- */
128 for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
135 if (NULL == ( dp = opendir( name)) ) {
139 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
140 if (!(m_name = check_dirent(vol, de->d_name)))
144 if (fn && fn(de,m_name, data) < 0) {
153 /* This is the maximal length of a single entry for a file/dir in the reply
154 block if all bits in the file/dir bitmap are set: header(4) + params(104) +
155 macnamelength(1) + macname(31) + utf8(4) + utf8namelen(2) + utf8name(255) +
158 #define REPLY_PARAM_MAXLEN (4 + 104 + 1 + MACFILELEN + 4 + 2 + UTF8FILELEN_EARLY + 1)
160 /* ----------------------------- */
161 static int enumerate(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,
166 static struct savedir sd = { 0, 0, 0, NULL, NULL, 0 };
169 int did, ret, len, first = 1;
172 u_int16_t vid, fbitmap, dbitmap, reqcnt, actcnt = 0;
174 u_int32_t sindex, maxsz, sz = 0;
179 if ( sd.sd_buflen == 0 ) {
180 if (( sd.sd_buf = (char *)malloc( SDBUFBRK )) == NULL ) {
181 LOG(log_error, logtype_afpd, "afp_enumerate: malloc: %s", strerror(errno) );
185 sd.sd_buflen = SDBUFBRK;
190 memcpy( &vid, ibuf, sizeof( vid ));
191 ibuf += sizeof( vid );
193 if (NULL == ( vol = getvolbyvid( vid )) ) {
195 return( AFPERR_PARAM );
198 memcpy( &did, ibuf, sizeof( did ));
199 ibuf += sizeof( did );
201 if (NULL == ( dir = dirlookup( vol, did )) ) {
203 return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
206 memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
207 fbitmap = ntohs( fbitmap );
208 ibuf += sizeof( fbitmap );
210 memcpy( &dbitmap, ibuf, sizeof( dbitmap ));
211 dbitmap = ntohs( dbitmap );
212 ibuf += sizeof( dbitmap );
214 /* check for proper bitmaps -- the stuff in comments is for
215 * variable directory ids. */
216 if (!(fbitmap || dbitmap)
217 /*|| (fbitmap & (1 << FILPBIT_PDID)) ||
218 (dbitmap & (1 << DIRPBIT_PDID))*/) {
220 return AFPERR_BITMAP;
223 memcpy( &reqcnt, ibuf, sizeof( reqcnt ));
224 reqcnt = ntohs( reqcnt );
225 ibuf += sizeof( reqcnt );
228 memcpy( &sindex, ibuf, sizeof( sindex ));
229 sindex = ntohl( sindex );
230 ibuf += sizeof( sindex );
233 memcpy( &temp16, ibuf, sizeof( temp16 ));
234 sindex = ntohs( temp16 );
235 ibuf += sizeof( temp16 );
240 return AFPERR_PARAM ;
244 memcpy( &maxsz, ibuf, sizeof( maxsz ));
245 maxsz = ntohl( maxsz );
246 ibuf += sizeof( maxsz );
249 memcpy( &temp16, ibuf, sizeof( temp16 ));
250 maxsz = ntohs( temp16 );
251 ibuf += sizeof( temp16 );
255 header *=sizeof( u_char );
257 maxsz = min(maxsz, *rbuflen - REPLY_PARAM_MAXLEN);
258 o_path = cname( vol, dir, &ibuf );
260 if (afp_errno == AFPERR_NOOBJ)
261 afp_errno = AFPERR_NODIR;
264 if (NULL == o_path ) {
265 return get_afp_errno(AFPERR_NOOBJ);
267 if ( *o_path->m_name != '\0') {
268 /* it's a file or it's a dir and extendir() was unable to chdir in it */
269 return path_error(o_path, AFPERR_NODIR );
272 LOG(log_debug, logtype_afpd, "enumerate(\"%s/%s\", f/d:%04x/%04x, rc:%u, i:%u, max:%u)",
273 getcwdpath(), o_path->u_name, fbitmap, dbitmap, reqcnt, sindex, maxsz);
275 data = rbuf + 3 * sizeof( u_int16_t );
276 sz = 3 * sizeof( u_int16_t ); /* fbitmap, dbitmap, reqcount */
279 * Read the directory into a pre-malloced buffer, stored
281 * The end is indicated by a len of 0.
283 if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
284 sd.sd_last = sd.sd_buf;
285 /* if dir was in the cache we don't have the inode */
286 if (( !o_path->st_valid && lstat( ".", &o_path->st ) < 0 ) ||
287 (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0)
289 LOG(log_error, logtype_afpd, "enumerate: loop error: %s (%d)", strerror(errno), errno);
292 return AFPERR_ACCESS;
294 return AFPERR_BADTYPE;
301 setdiroffcnt(curdir, &o_path->st, ret);
304 sd.sd_last = sd.sd_buf;
308 sd.sd_did = curdir->d_did;
312 * Position sd_last as dictated by sindex.
314 if ( sindex < sd.sd_sindex ) {
316 sd.sd_last = sd.sd_buf;
318 while ( sd.sd_sindex < sindex ) {
319 len = (unsigned char)*(sd.sd_last)++;
321 sd.sd_did = 0; /* invalidate sd struct to force re-read */
322 return( AFPERR_NOOBJ );
324 sd.sd_last += len + 1;
328 while (( len = (unsigned char)*(sd.sd_last)) != 0 ) {
330 * If we've got all we need, send it.
332 if ( actcnt == reqcnt ) {
337 * Save the start position, in case we exceed the buffer
338 * limitation, and have to back up one.
343 if (*sd.sd_last == 0) {
344 /* stat() already failed on this one */
345 sd.sd_last += len + 1;
348 memset(&s_path, 0, sizeof(s_path));
349 s_path.u_name = sd.sd_last;
350 if (of_stat( &s_path) < 0 ) {
352 * Somebody else plays with the dir, well it can be us with
356 /* so the next time it won't try to stat it again
357 * another solution would be to invalidate the cache with
358 * sd.sd_did = 0 but if it's not ENOENT error it will start again
361 sd.sd_last += len + 1;
362 curdir->d_offcnt--; /* a little lie */
366 sd.sd_last += len + 1;
367 s_path.m_name = NULL;
369 * If a fil/dir is not a dir, it's a file. This is slightly
370 * inaccurate, since that means /dev/null is a file, /dev/printer
373 if ( S_ISDIR(s_path.st.st_mode)) {
374 if ( dbitmap == 0 ) {
377 int len = strlen(s_path.u_name);
378 if ((dir = dircache_search_by_name(vol, curdir, s_path.u_name, len)) == NULL) {
379 if ((dir = dir_add(vol, curdir, &s_path, len)) == NULL) {
380 LOG(log_error, logtype_afpd, "enumerate(vid:%u, did:%u, name:'%s'): error adding dir: '%s'",
381 ntohs(vid), ntohl(did), o_path->u_name, s_path.u_name);
385 if ((ret = getdirparams(vol, dbitmap, &s_path, dir, data + header , &esz)) != AFP_OK)
389 if ( fbitmap == 0 ) {
392 /* files are added to the dircache in getfilparams() -> getmetadata() */
393 if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir,
394 data + header , &esz )) ) {
400 * Make sure entry is an even length, possibly with a null
403 if ( (esz + header) & 1 ) {
404 *(data + header + esz ) = '\0';
409 * Check if we've exceeded the size limit.
411 if ( maxsz < sz + esz + header) {
412 if (first) { /* maxsz can't hold a single reply */
424 temp16 = htons( esz + header );
425 memcpy( data, &temp16, sizeof( temp16 ));
426 data += sizeof(temp16);
429 *data++ = esz + header;
432 *data++ = S_ISDIR(s_path.st.st_mode) ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;
438 /* FIXME if we rollover 16 bits and it's not FPEnumerateExt2 */
442 sd.sd_did = 0; /* invalidate sd struct to force re-read */
443 return( AFPERR_NOOBJ );
445 sd.sd_sindex = sindex + actcnt;
448 * All done, fill in misc junk in rbuf
450 fbitmap = htons( fbitmap );
451 memcpy( rbuf, &fbitmap, sizeof( fbitmap ));
452 rbuf += sizeof( fbitmap );
453 dbitmap = htons( dbitmap );
454 memcpy( rbuf, &dbitmap, sizeof( dbitmap ));
455 rbuf += sizeof( dbitmap );
456 actcnt = htons( actcnt );
457 memcpy( rbuf, &actcnt, sizeof( actcnt ));
458 rbuf += sizeof( actcnt );
463 /* ----------------------------- */
464 int afp_enumerate(AFPObj *obj, char *ibuf, size_t ibuflen,
468 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 0);
471 /* ----------------------------- */
472 int afp_enumerate_ext(AFPObj *obj, char *ibuf, size_t ibuflen,
476 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 1);
479 /* ----------------------------- */
480 int afp_enumerate_ext2(AFPObj *obj, char *ibuf, size_t ibuflen,
484 return enumerate(obj, ibuf,ibuflen ,rbuf,rbuflen , 2);