+ STRUCT_STAT st;
+ int e;
+
+ if (verbose > 2)
+ rprintf(FINFO,"get_local_name count=%d %s\n",
+ flist->count, NS(name));
+
+ if (!name)
+ return NULL;
+
+ if (do_stat(name,&st) == 0) {
+ if (S_ISDIR(st.st_mode)) {
+ if (!push_dir(name)) {
+ rsyserr(FERROR, errno, "push_dir#1 %s failed",
+ full_fname(name));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ return NULL;
+ }
+ if (flist->count > 1) {
+ rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
+ exit_cleanup(RERR_FILESELECT);
+ }
+ return name;
+ }
+
+ if (flist->count <= 1 && ((e = strlen(name)) <= 1 || name[e-1] != '/'))
+ return name;
+
+ if (do_mkdir(name,0777 & ~orig_umask) != 0) {
+ rsyserr(FERROR, errno, "mkdir %s failed", full_fname(name));
+ exit_cleanup(RERR_FILEIO);
+ }
+ if (verbose > 0)
+ rprintf(FINFO, "created directory %s\n", name);
+
+ if (dry_run) {
+ dry_run++;
+ return NULL;
+ }
+
+ if (!push_dir(name)) {
+ rsyserr(FERROR, errno, "push_dir#2 %s failed",
+ full_fname(name));
+ exit_cleanup(RERR_FILESELECT);
+ }
+
+ return NULL;
+}
+
+
+/* This is only called by the sender. */
+static void read_final_goodbye(int f_in, int f_out)
+{
+ int i;
+
+ if (protocol_version < 29)
+ i = read_int(f_in);
+ else {
+ while ((i = read_int(f_in)) == the_file_list->count
+ && read_shortint(f_in) == ITEM_IS_NEW) {
+ /* Forward the keep-alive (no-op) to the receiver. */
+ write_int(f_out, the_file_list->count);
+ write_shortint(f_out, ITEM_IS_NEW);
+ }
+ }
+
+ if (i != -1) {
+ rprintf(FERROR, "Invalid packet at end of run (%d) [%s]\n",
+ i, who_am_i());
+ exit_cleanup(RERR_PROTOCOL);
+ }
+}
+
+
+static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
+{
+ struct file_list *flist;
+ char *dir = argv[0];
+
+ if (verbose > 2) {
+ rprintf(FINFO, "server_sender starting pid=%ld\n",
+ (long)getpid());
+ }
+
+ if (am_daemon && lp_write_only(module_id)) {
+ rprintf(FERROR, "ERROR: module is write only\n");
+ exit_cleanup(RERR_SYNTAX);
+ return;
+ }
+ if (am_daemon && lp_read_only(module_id) && remove_sent_files) {
+ rprintf(FERROR,
+ "ERROR: --remove-sent-files cannot be used with a read-only module\n");
+ exit_cleanup(RERR_SYNTAX);
+ return;
+ }
+
+ if (!relative_paths && !push_dir(dir)) {
+ rsyserr(FERROR, errno, "push_dir#3 %s failed",
+ full_fname(dir));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ argc--;
+ argv++;
+
+ if (argc == 0 && (recurse || list_only)) {
+ argc = 1;
+ argv--;
+ argv[0] = ".";
+ }
+
+ flist = send_file_list(f_out,argc,argv);
+ if (!flist || flist->count == 0) {
+ exit_cleanup(0);
+ }
+ the_file_list = flist;
+
+ io_start_buffering_in();
+ io_start_buffering_out();
+
+ send_files(flist,f_out,f_in);
+ io_flush(FULL_FLUSH);
+ handle_stats(f_out);
+ if (protocol_version >= 24)
+ read_final_goodbye(f_in, f_out);
+ io_flush(FULL_FLUSH);
+ exit_cleanup(0);
+}
+
+
+static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
+{
+ int pid;
+ int exit_code = 0;
+ int error_pipe[2];
+
+ /* The receiving side mustn't obey this, or an existing symlink that
+ * points to an identical file won't be replaced by the referent. */
+ copy_links = 0;
+
+ if (preserve_hard_links)
+ init_hard_links();
+
+ if (fd_pair(error_pipe) < 0) {
+ rsyserr(FERROR, errno, "pipe failed in do_recv");
+ exit_cleanup(RERR_IPC);
+ }
+
+ io_flush(NORMAL_FLUSH);
+
+ if ((pid = do_fork()) == -1) {
+ rsyserr(FERROR, errno, "fork failed in do_recv");
+ exit_cleanup(RERR_IPC);
+ }
+
+ if (pid == 0) {
+ close(error_pipe[0]);
+ if (f_in != f_out)
+ close(f_out);
+
+ /* we can't let two processes write to the socket at one time */
+ close_multiplexing_out();
+
+ /* set place to send errors */
+ set_msg_fd_out(error_pipe[1]);
+
+ recv_files(f_in, flist, local_name);
+ io_flush(FULL_FLUSH);
+ handle_stats(f_in);
+
+ send_msg(MSG_DONE, "", 0);
+ io_flush(FULL_FLUSH);
+
+ /* Handle any keep-alive packets from the post-processing work
+ * that the generator does. */
+ if (protocol_version >= 29) {
+ kluge_around_eof = -1;
+
+ /* This should only get stopped via a USR2 signal. */
+ while (read_int(f_in) == flist->count
+ && read_shortint(f_in) == ITEM_IS_NEW) {}
+
+ rprintf(FERROR, "Invalid packet at end of run [%s]\n",
+ who_am_i());
+ exit_cleanup(RERR_PROTOCOL);
+ }
+
+ /* Finally, we go to sleep until our parent kills us with a
+ * USR2 signal. We sleep for a short time, as on some OSes
+ * a signal won't interrupt a sleep! */
+ while (1)
+ msleep(20);
+ }
+
+ am_generator = 1;
+ close_multiplexing_in();
+ if (write_batch && !am_server)
+ stop_write_batch();
+
+ close(error_pipe[1]);
+ if (f_in != f_out)
+ close(f_in);
+
+ io_start_buffering_out();
+
+ set_msg_fd_in(error_pipe[0]);
+
+ generate_files(f_out, flist, local_name);
+
+ handle_stats(-1);
+ io_flush(FULL_FLUSH);
+ if (protocol_version >= 24) {
+ /* send a final goodbye message */
+ write_int(f_out, -1);
+ }
+ io_flush(FULL_FLUSH);
+
+ set_msg_fd_in(-1);
+ kill(pid, SIGUSR2);
+ wait_process_with_flush(pid, &exit_code);
+ return exit_code;
+}
+
+
+static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
+{
+ int exit_code;
+ struct file_list *flist;
+ char *local_name = NULL;
+ char *dir = NULL;
+ int save_verbose = verbose;
+
+ if (filesfrom_fd >= 0) {
+ /* We can't mix messages with files-from data on the socket,
+ * so temporarily turn off verbose messages. */
+ verbose = 0;
+ }
+
+ if (verbose > 2) {
+ rprintf(FINFO, "server_recv(%d) starting pid=%ld\n",
+ argc, (long)getpid());
+ }
+
+ if (am_daemon && lp_read_only(module_id)) {
+ rprintf(FERROR,"ERROR: module is read only\n");
+ exit_cleanup(RERR_SYNTAX);
+ return;
+ }
+
+ if (argc > 0) {
+ dir = argv[0];
+ argc--;
+ argv++;
+ if (!am_daemon && !push_dir(dir)) {
+ rsyserr(FERROR, errno, "push_dir#4 %s failed",
+ full_fname(dir));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ }
+
+ io_start_buffering_in();
+ recv_filter_list(f_in);
+
+ if (filesfrom_fd >= 0) {
+ /* We need to send the files-from names to the sender at the
+ * same time that we receive the file-list from them, so we
+ * need the IO routines to automatically write out the names
+ * onto our f_out socket as we read the file-list. This
+ * avoids both deadlock and extra delays/buffers. */
+ io_set_filesfrom_fds(filesfrom_fd, f_out);
+ filesfrom_fd = -1;
+ }
+
+ flist = recv_file_list(f_in);
+ verbose = save_verbose;
+ if (!flist) {
+ rprintf(FERROR,"server_recv: recv_file_list error\n");
+ exit_cleanup(RERR_FILESELECT);
+ }
+ the_file_list = flist;
+
+ if (argc > 0)
+ local_name = get_local_name(flist,argv[0]);
+
+ exit_code = do_recv(f_in,f_out,flist,local_name);
+ exit_cleanup(exit_code);