+/*
+ * Struct to save directory reading context in. Used to prevent
+ * O(n^2) searches on a directory.
+ */
+struct savedir {
+ u_short sd_vid;
+ u_int32_t sd_did;
+ int sd_buflen;
+ char *sd_buf;
+ char *sd_last;
+ unsigned int sd_sindex;
+};
+#define SDBUFBRK 2048
+
+static int enumerate_loop(struct dirent *de, char *mname _U_, void *data)
+{
+ struct savedir *sd = data;
+ char *start, *end;
+ int len,lenm;
+
+ end = sd->sd_buf + sd->sd_buflen;
+ len = strlen(de->d_name);
+ *(sd->sd_last)++ = len;
+ lenm = 0; /* strlen(mname);*/
+ if ( sd->sd_last + len +lenm + 4 > end ) {
+ char *buf;
+
+ start = sd->sd_buf;
+ if (!(buf = realloc( sd->sd_buf, sd->sd_buflen +SDBUFBRK )) ) {
+ LOG(log_error, logtype_afpd, "afp_enumerate: realloc: %s",
+ strerror(errno) );
+ errno = ENOMEM;
+ return -1;
+ }
+ sd->sd_buf = buf;
+ sd->sd_buflen += SDBUFBRK;
+ sd->sd_last = ( sd->sd_last - start ) + sd->sd_buf;
+ end = sd->sd_buf + sd->sd_buflen;
+ }
+
+ memcpy( sd->sd_last, de->d_name, len + 1 );
+ sd->sd_last += len + 1;
+#if 0
+ *(sd->sd_last)++ = lenm;
+ memcpy( sd->sd_last, mname, lenm + 1 );
+ sd->sd_last += lenm + 1;
+#endif
+ return 0;
+}
+
+/* -----------------------------
+ * FIXME:
+ * Doesn't work with dangling symlink
+ * ie:
+ * - Move a folder with a dangling symlink in the trash
+ * - empty the trash
+ * afp_enumerate return an empty listing but offspring count != 0 in afp_getdirparams
+ * and the Mac doesn't try to call afp_delete!
+ *
+ * Another option for symlink
+ * cf:
+ * http://sourceforge.net/tracker/index.php?func=detail&aid=461938&group_id=8642&atid=108642
+ *
+*/
+char *check_dirent(const struct vol *vol, char *name)