If there is no lchown(), don't try to set the user & group of a symlink.
[rsync/rsync.git] / sender.c
1 /*
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "rsync.h"
21
22 extern int verbose;
23 extern int csum_length;
24 extern struct stats stats;
25 extern int io_error;
26 extern int dry_run;
27 extern int am_server;
28 extern int am_daemon;
29 extern int protocol_version;
30 extern int make_backups;
31 extern struct stats stats;
32
33
34 /**
35  * @file
36  *
37  * The sender gets checksums from the generator, calculates deltas,
38  * and transmits them to the receiver.  The sender process runs on the
39  * machine holding the source files.
40  **/
41 void read_sum_head(int f, struct sum_struct *sum)
42 {
43         sum->count = read_int(f);
44         sum->blength = read_int(f);
45         if (protocol_version < 27) {
46                 sum->s2length = csum_length;
47         } else {
48                 sum->s2length = read_int(f);
49                 if (sum->s2length > MD4_SUM_LENGTH) {
50                         rprintf(FERROR, "Invalid checksum length %ld\n",
51                             (long)sum->s2length);
52                         exit_cleanup(RERR_PROTOCOL);
53                 }
54         }
55         sum->remainder = read_int(f);
56 }
57
58 /**
59  * Receive the checksums for a buffer
60  **/
61 static struct sum_struct *receive_sums(int f)
62 {
63         struct sum_struct *s;
64         int i;
65         OFF_T offset = 0;
66
67         if (!(s = new(struct sum_struct)))
68                 out_of_memory("receive_sums");
69
70         read_sum_head(f, s);
71
72         s->sums = NULL;
73
74         if (verbose > 3) {
75                 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
76                         (double)s->count, (long)s->blength, (long)s->remainder);
77         }
78
79         if (s->count == 0)
80                 return(s);
81
82         if (!(s->sums = new_array(struct sum_buf, s->count)))
83                 out_of_memory("receive_sums");
84
85         for (i = 0; i < (int)s->count; i++) {
86                 s->sums[i].sum1 = read_int(f);
87                 read_buf(f, s->sums[i].sum2, s->s2length);
88
89                 s->sums[i].offset = offset;
90                 s->sums[i].flags = 0;
91
92                 if (i == (int)s->count-1 && s->remainder != 0)
93                         s->sums[i].len = s->remainder;
94                 else
95                         s->sums[i].len = s->blength;
96                 offset += s->sums[i].len;
97
98                 if (verbose > 3) {
99                         rprintf(FINFO,
100                                 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
101                                 i, s->sums[i].len, (double)s->sums[i].offset,
102                                 s->sums[i].sum1);
103                 }
104         }
105
106         s->flength = offset;
107
108         return s;
109 }
110
111
112
113 void send_files(struct file_list *flist, int f_out, int f_in)
114 {
115         int fd = -1;
116         struct sum_struct *s;
117         struct map_struct *mbuf = NULL;
118         STRUCT_STAT st;
119         char *fname2, fname[MAXPATHLEN];
120         int i;
121         struct file_struct *file;
122         int phase = 0;
123         struct stats initial_stats;
124         int save_make_backups = make_backups;
125         int j;
126
127         if (verbose > 2)
128                 rprintf(FINFO, "send_files starting\n");
129
130         while (1) {
131                 unsigned int offset;
132
133                 i = read_int(f_in);
134                 if (i == -1) {
135                         if (phase == 0) {
136                                 phase++;
137                                 csum_length = SUM_LENGTH;
138                                 write_int(f_out, -1);
139                                 if (verbose > 2)
140                                         rprintf(FINFO, "send_files phase=%d\n", phase);
141                                 /* For inplace: redo phase turns off the backup
142                                  * flag so that we do a regular inplace send. */
143                                 make_backups = 0;
144                                 continue;
145                         }
146                         break;
147                 }
148
149                 if (i < 0 || i >= flist->count) {
150                         rprintf(FERROR, "Invalid file index %d (count=%d)\n",
151                                 i, flist->count);
152                         exit_cleanup(RERR_PROTOCOL);
153                 }
154
155                 file = flist->files[i];
156
157                 stats.current_file_index = i;
158                 stats.num_transferred_files++;
159                 stats.total_transferred_size += file->length;
160
161                 if (file->basedir) {
162                         /* N.B. We're sure that this fits, so offset is OK. */
163                         offset = strlcpy(fname, file->basedir, sizeof fname);
164                         if (!offset || fname[offset-1] != '/')
165                                 fname[offset++] = '/';
166                 } else
167                         offset = 0;
168                 fname2 = f_name_to(file, fname + offset);
169
170                 if (verbose > 2)
171                         rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
172
173                 if (dry_run) {
174                         if (!am_server && verbose) /* log the transfer */
175                                 rprintf(FINFO, "%s\n", safe_fname(fname2));
176                         write_int(f_out, i);
177                         continue;
178                 }
179
180                 initial_stats = stats;
181
182                 if (!(s = receive_sums(f_in))) {
183                         io_error |= IOERR_GENERAL;
184                         rprintf(FERROR, "receive_sums failed\n");
185                         return;
186                 }
187
188                 fd = do_open(fname, O_RDONLY, 0);
189                 if (fd == -1) {
190                         if (errno == ENOENT) {
191                                 enum logcode c = am_daemon
192                                     && protocol_version < 28 ? FERROR
193                                                              : FINFO;
194                                 io_error |= IOERR_VANISHED;
195                                 rprintf(c, "file has vanished: %s\n",
196                                         full_fname(fname));
197                         } else {
198                                 io_error |= IOERR_GENERAL;
199                                 rsyserr(FERROR, errno,
200                                         "send_files failed to open %s",
201                                         full_fname(fname));
202                         }
203                         free_sums(s);
204                         continue;
205                 }
206
207                 /* map the local file */
208                 if (do_fstat(fd, &st) != 0) {
209                         io_error |= IOERR_GENERAL;
210                         rsyserr(FERROR, errno, "fstat failed");
211                         free_sums(s);
212                         close(fd);
213                         return;
214                 }
215
216                 if (st.st_size) {
217                         OFF_T map_size = MAX((OFF_T)s->blength * 3, MAX_MAP_SIZE);
218                         mbuf = map_file(fd, st.st_size, map_size, s->blength);
219                 } else
220                         mbuf = NULL;
221
222                 if (verbose > 2) {
223                         rprintf(FINFO, "send_files mapped %s of size %.0f\n",
224                                 safe_fname(fname), (double)st.st_size);
225                 }
226
227                 write_int(f_out, i);
228                 write_sum_head(f_out, s);
229
230                 if (verbose > 2) {
231                         rprintf(FINFO, "calling match_sums %s\n",
232                                 safe_fname(fname));
233                 }
234
235                 if (!am_server && verbose) /* log the transfer */
236                         rprintf(FINFO, "%s\n", safe_fname(fname2));
237
238                 set_compression(fname);
239
240                 match_sums(f_out, s, mbuf, st.st_size);
241                 log_send(file, &initial_stats);
242
243                 if (mbuf) {
244                         j = unmap_file(mbuf);
245                         if (j) {
246                                 io_error |= IOERR_GENERAL;
247                                 rsyserr(FERROR, j,
248                                         "read errors mapping %s",
249                                         full_fname(fname));
250                         }
251                 }
252                 close(fd);
253
254                 free_sums(s);
255
256                 if (verbose > 2) {
257                         rprintf(FINFO, "sender finished %s\n",
258                                 safe_fname(fname));
259                 }
260         }
261         make_backups = save_make_backups;
262
263         if (verbose > 2)
264                 rprintf(FINFO, "send files finished\n");
265
266         match_report();
267
268         write_int(f_out, -1);
269 }