+static char lastpath[MAXPATHLEN] = "";
+static int lastpath_len = 0;
+static struct file_struct *lastpath_struct;
+
+static void send_implied_dirs(int f, struct file_list *flist, char *fname,
+ char *start, char *limit, int flags, int is_dot_dir)
+{
+ struct file_struct *file;
+ item_list *rel_list;
+ char **ep, *slash;
+ int len, need_new_dir;
+
+ flags &= ~FLAG_XFER_DIR;
+
+ if (inc_recurse) {
+ if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
+ && lastpath_len == limit - fname
+ && strncmp(lastpath, fname, lastpath_len) == 0)
+ need_new_dir = 0;
+ else
+ need_new_dir = 1;
+ } else
+ need_new_dir = 1;
+
+ if (need_new_dir) {
+ int save_copy_links = copy_links;
+ int save_xfer_dirs = xfer_dirs;
+
+ copy_links |= copy_unsafe_links;
+ xfer_dirs = 1;
+
+ *limit = '\0';
+
+ for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
+ *slash = '\0';
+ send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
+ *slash = '/';
+ }
+
+ file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
+ if (inc_recurse) {
+ if (file && !S_ISDIR(file->mode))
+ file = NULL;
+ else if (file)
+ memset(F_DIR_RELS_P(file), 0, sizeof (item_list*));
+ lastpath_struct = file;
+ }
+
+ strlcpy(lastpath, fname, sizeof lastpath);
+ lastpath_len = limit - fname;
+
+ *limit = '/';
+
+ copy_links = save_copy_links;
+ xfer_dirs = save_xfer_dirs;
+
+ if (!inc_recurse)
+ return;
+ }
+
+ if (!lastpath_struct)
+ return; /* dir must have vanished */
+
+ len = strlen(limit+1);
+ memcpy(&rel_list, F_DIR_RELS_P(lastpath_struct), sizeof rel_list);
+ if (!rel_list) {
+ if (!(rel_list = new0(item_list)))
+ out_of_memory("send_implied_dirs");
+ memcpy(F_DIR_RELS_P(lastpath_struct), &rel_list, sizeof rel_list);
+ }
+ ep = EXPAND_ITEM_LIST(rel_list, char *, 32);
+ if (!(*ep = new_array(char, 1 + len + 1)))
+ out_of_memory("send_implied_dirs");
+ **ep = is_dot_dir;
+ strlcpy(*ep + 1, limit+1, len + 1);
+}
+
+static void send1extra(int f, struct file_struct *file, struct file_list *flist)
+{
+ char fbuf[MAXPATHLEN];
+ item_list *rel_list;
+ int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_XFER_DIR;
+ size_t j;
+
+ f_name(file, fbuf);
+ dlen = strlen(fbuf);
+
+ if (F_PATHNAME(file) != pathname) {
+ if (!push_pathname(F_PATHNAME(file), -1))
+ exit_cleanup(RERR_FILESELECT);
+ }
+
+ change_local_filter_dir(fbuf, dlen, send_dir_depth);
+
+ if (file->flags & FLAG_XFER_DIR)
+ send_directory(f, flist, fbuf, dlen, flags);
+
+ if (!relative_paths)
+ return;
+
+ memcpy(&rel_list, F_DIR_RELS_P(file), sizeof rel_list);
+ if (!rel_list)
+ return;
+
+ for (j = 0; j < rel_list->count; j++) {
+ char *slash, *ep = ((char**)rel_list->items)[j];
+ int is_dot_dir = *ep;
+
+ fbuf[dlen] = '/';
+ len = strlcpy(fbuf + dlen + 1, ep+1, sizeof fbuf - dlen - 1);
+ free(ep);
+ if (len >= (int)sizeof fbuf)
+ continue; /* Impossible... */
+
+ slash = strchr(fbuf+dlen+1, '/');
+ if (slash) {
+ send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, is_dot_dir);
+ continue;
+ }
+
+ if (is_dot_dir) {
+ STRUCT_STAT st;
+ if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
+ io_error |= IOERR_GENERAL;
+ rsyserr(FERROR, errno, "link_stat %s failed",
+ full_fname(fbuf));
+ continue;
+ }
+ send_file_name(f, flist, fbuf, &st,
+ recurse || xfer_dirs ? FLAG_TOP_DIR | flags : flags,
+ ALL_FILTERS);
+ } else {
+ send_file_name(f, flist, fbuf, NULL,
+ recurse ? FLAG_TOP_DIR | flags : flags,
+ ALL_FILTERS);
+ }
+ }
+
+ free(rel_list);
+}
+
+void send_extra_file_list(int f, int at_least)
+{
+ struct file_list *flist;
+ int64 start_write;
+ int future_cnt, save_io_error = io_error;
+
+ if (flist_eof)
+ return;
+
+ /* Keep sending data until we have the requested number of
+ * files in the upcoming file-lists. */
+ if (cur_flist->next) {
+ flist = first_flist->prev; /* the newest flist */
+ future_cnt = flist->ndx_end - cur_flist->next->ndx_start + 1;
+ } else
+ future_cnt = 0;
+ while (future_cnt < at_least) {
+ struct file_struct *file = dir_flist->sorted[send_dir_ndx];
+ int dir_ndx, dstart = dir_count;
+ const char *pathname = F_PATHNAME(file);
+ int32 *dp;
+
+ flist = flist_new(0, "send_extra_file_list");
+ start_write = stats.total_written;
+
+#ifdef ICONV_OPTION
+ if (ic_ndx)
+ dir_ndx = F_NDX(file);
+ else
+#endif
+ dir_ndx = send_dir_ndx;
+ write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
+ flist->parent_ndx = dir_ndx;
+
+ send1extra(f, file, flist);
+ dp = F_DIR_NODE_P(file);
+
+ /* If there are any duplicate directory names that follow, we
+ * send all the dirs together in one file-list. The dir_flist
+ * tree links all the child subdirs onto the last dup dir. */
+ while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
+ && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
+ send_dir_ndx = dir_ndx;
+ file = dir_flist->sorted[dir_ndx];
+ if (F_PATHNAME(file) != pathname)
+ send1extra(f, file, flist);
+ dp = F_DIR_NODE_P(file);
+ }
+
+ write_byte(f, 0);
+
+#ifdef ICONV_OPTION
+ if (need_unsorted_flist) {
+ if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
+ out_of_memory("send_extra_file_list");
+ memcpy(flist->sorted, flist->files,
+ flist->used * sizeof (struct file_struct*));
+ } else
+#endif
+ flist->sorted = flist->files;
+
+ clean_flist(flist, 0);
+
+ flist->ndx_end = flist->ndx_start + flist->used - 1
+ - (dir_count - dstart);
+
+ add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
+ flist_done_allocating(flist);
+
+ file_total += flist->used;
+ future_cnt += flist->used;
+ stats.flist_size += stats.total_written - start_write;
+ stats.num_files += flist->used;
+ if (verbose > 3)
+ output_flist(flist);
+
+ if (DIR_FIRST_CHILD(dp) >= 0) {
+ send_dir_ndx = DIR_FIRST_CHILD(dp);
+ send_dir_depth++;
+ } else {
+ while (DIR_NEXT_SIBLING(dp) < 0) {
+ if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
+ write_ndx(f, NDX_FLIST_EOF);
+ flist_eof = 1;
+ change_local_filter_dir(NULL, 0, 0);
+ goto finish;
+ }
+ send_dir_depth--;
+ file = dir_flist->sorted[send_dir_ndx];
+ dp = F_DIR_NODE_P(file);
+ }
+ send_dir_ndx = DIR_NEXT_SIBLING(dp);
+ }
+ }
+
+ finish:
+ if (io_error != save_io_error && !ignore_errors)
+ send_msg_int(MSG_IO_ERROR, io_error);
+}
+