The patches for 3.3.0.
[rsync-patches.git] / write-devices.diff
1 This patch adds the --write-devices option, which will try to write
2 data into a device when used as a destination.
3
4 To use this patch, run these commands for a successful build:
5
6     patch -p1 <patches/write-devices.diff
7     ./configure                      (optional if already run)
8     make
9
10 This patch has not yet been tested by me (Wayne), but was provided
11 Darryl Dixon.  Thanks!
12
13 based-on: d73762eea3f15f2c56bb3fa9394ad1883c25c949
14 diff --git a/generator.c b/generator.c
15 --- a/generator.c
16 +++ b/generator.c
17 @@ -39,6 +39,7 @@ extern int preserve_acls;
18  extern int preserve_xattrs;
19  extern int preserve_links;
20  extern int preserve_devices;
21 +extern int write_devices;
22  extern int preserve_specials;
23  extern int preserve_hard_links;
24  extern int preserve_executability;
25 @@ -1693,7 +1694,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
26  
27         fnamecmp_type = FNAMECMP_FNAME;
28  
29 -       if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
30 +       if (statret == 0 && !(S_ISREG(sx.st.st_mode) || (write_devices && IS_DEVICE(sx.st.st_mode)))) {
31                 if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
32                         goto cleanup;
33                 statret = -1;
34 diff --git a/options.c b/options.c
35 --- a/options.c
36 +++ b/options.c
37 @@ -50,6 +50,7 @@ int append_mode = 0;
38  int keep_dirlinks = 0;
39  int copy_dirlinks = 0;
40  int copy_links = 0;
41 +int write_devices = 0;
42  int preserve_links = 0;
43  int preserve_hard_links = 0;
44  int preserve_acls = 0;
45 @@ -705,6 +706,7 @@ void usage(enum logcode F)
46    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
47    rprintf(F," -g, --group                 preserve group\n");
48    rprintf(F,"     --devices               preserve device files (super-user only)\n");
49 +  rprintf(F," -w  --write-devices         write to devices as regular files (implies --inplace)\n");
50    rprintf(F,"     --specials              preserve special files\n");
51    rprintf(F," -D                          same as --devices --specials\n");
52    rprintf(F," -t, --times                 preserve modification times\n");
53 @@ -887,6 +889,7 @@ static struct poptOption long_options[] = {
54    {"no-D",             0,  POPT_ARG_NONE,   0, OPT_NO_D, 0, 0 },
55    {"devices",          0,  POPT_ARG_VAL,    &preserve_devices, 1, 0, 0 },
56    {"no-devices",       0,  POPT_ARG_VAL,    &preserve_devices, 0, 0, 0 },
57 +  {"write-devices",   'w', POPT_ARG_NONE,   0, 'w', 0, 0 },
58    {"specials",         0,  POPT_ARG_VAL,    &preserve_specials, 1, 0, 0 },
59    {"no-specials",      0,  POPT_ARG_VAL,    &preserve_specials, 0, 0, 0 },
60    {"links",           'l', POPT_ARG_VAL,    &preserve_links, 1, 0, 0 },
61 @@ -1808,6 +1811,11 @@ int parse_arguments(int *argc_p, const char ***argv_p)
62                         return 0;
63  #endif
64  
65 +               case 'w':
66 +                       write_devices = 1;
67 +                       inplace = 1;
68 +                       break;
69 +
70                 default:
71                         /* A large opt value means that set_refuse_options()
72                          * turned this option off. */
73 @@ -2792,6 +2800,9 @@ void server_options(char **args, int *argc_p)
74         if (relative_paths && !implied_dirs && (!am_sender || protocol_version >= 30))
75                 args[ac++] = "--no-implied-dirs";
76  
77 +       if (write_devices)
78 +               args[ac++] = "--write-devices";
79 +
80         if (remove_source_files == 1)
81                 args[ac++] = "--remove-source-files";
82         else if (remove_source_files)
83 diff --git a/receiver.c b/receiver.c
84 --- a/receiver.c
85 +++ b/receiver.c
86 @@ -39,6 +39,7 @@ extern int protocol_version;
87  extern int relative_paths;
88  extern int preserve_hard_links;
89  extern int preserve_perms;
90 +extern int write_devices;
91  extern int preserve_xattrs;
92  extern int basis_dir_cnt;
93  extern int make_backups;
94 @@ -231,7 +232,7 @@ int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
95  }
96  
97  static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
98 -                       const char *fname, int fd, OFF_T total_size)
99 +                       const char *fname, int fd, struct file_struct *file)
100  {
101         static char file_sum1[MAX_DIGEST_LEN];
102         struct map_struct *mapbuf;
103 @@ -245,10 +246,10 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
104         char *map = NULL;
105  
106  #ifdef SUPPORT_PREALLOCATION
107 -       if (preallocate_files && fd != -1 && total_size > 0 && (!inplace || total_size > size_r)) {
108 +       if (preallocate_files && fd != -1 && F_LENGTH(file) > 0 && (!inplace || F_LENGTH(file) > size_r)) {
109                 /* Try to preallocate enough space for file's eventual length.  Can
110                  * reduce fragmentation on filesystems like ext4, xfs, and NTFS. */
111 -               if ((preallocated_len = do_fallocate(fd, 0, total_size)) < 0)
112 +               if ((preallocated_len = do_fallocate(fd, 0, F_LENGTH(file))) < 0)
113                         rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname));
114         } else
115  #endif
116 @@ -285,7 +286,7 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
117                 if (append_mode == 2 && mapbuf) {
118                         for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
119                                 if (INFO_GTE(PROGRESS, 1))
120 -                                       show_progress(offset, total_size);
121 +                                       show_progress(offset, F_LENGTH(file));
122                                 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
123                                            CHUNK_SIZE);
124                                 offset = j;
125 @@ -293,7 +294,7 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
126                         if (offset < sum.flength) {
127                                 int32 len = (int32)(sum.flength - offset);
128                                 if (INFO_GTE(PROGRESS, 1))
129 -                                       show_progress(offset, total_size);
130 +                                       show_progress(offset, F_LENGTH(file));
131                                 sum_update(map_ptr(mapbuf, offset, len), len);
132                         }
133                 }
134 @@ -307,7 +308,7 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
135  
136         while ((i = recv_token(f_in, &data)) != 0) {
137                 if (INFO_GTE(PROGRESS, 1))
138 -                       show_progress(offset, total_size);
139 +                       show_progress(offset, F_LENGTH(file));
140  
141                 if (allowed_lull)
142                         maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH | MSK_ACTIVE_RECEIVER);
143 @@ -377,16 +378,16 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
144  
145  #ifdef HAVE_FTRUNCATE
146         /* inplace: New data could be shorter than old data.
147 -        * preallocate_files: total_size could have been an overestimate.
148 +        * preallocate_files: F_LENGTH(file) could have been an overestimate.
149          *     Cut off any extra preallocated zeros from dest file. */
150 -       if ((inplace || preallocated_len > offset) && fd != -1 && do_ftruncate(fd, offset) < 0) {
151 +       if ((inplace || preallocated_len > offset) && fd != -1 && !IS_DEVICE(file->mode) && do_ftruncate(fd, offset) < 0) {
152                 rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
153                         full_fname(fname));
154         }
155  #endif
156  
157         if (INFO_GTE(PROGRESS, 1))
158 -               end_progress(total_size);
159 +               end_progress(F_LENGTH(file));
160  
161         sum_len = sum_end(file_sum1);
162  
163 @@ -402,9 +403,9 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
164  }
165  
166  
167 -static void discard_receive_data(int f_in, OFF_T length)
168 +static void discard_receive_data(int f_in, struct file_struct *file)
169  {
170 -       receive_data(f_in, NULL, -1, 0, NULL, -1, length);
171 +       receive_data(f_in, NULL, -1, 0, NULL, -1, file);
172  }
173  
174  static void handle_delayed_updates(char *local_name)
175 @@ -660,7 +661,7 @@ int recv_files(int f_in, int f_out, char *local_name)
176                                         "(Skipping batched update for%s \"%s\")\n",
177                                         redoing ? " resend of" : "",
178                                         fname);
179 -                               discard_receive_data(f_in, F_LENGTH(file));
180 +                               discard_receive_data(f_in, file);
181                                 file->flags |= FLAG_FILE_SENT;
182                                 continue;
183                         }
184 @@ -671,13 +672,13 @@ int recv_files(int f_in, int f_out, char *local_name)
185                 if (!do_xfers) { /* log the transfer */
186                         log_item(FCLIENT, file, iflags, NULL);
187                         if (read_batch)
188 -                               discard_receive_data(f_in, F_LENGTH(file));
189 +                               discard_receive_data(f_in, file);
190                         continue;
191                 }
192                 if (write_batch < 0) {
193                         log_item(FCLIENT, file, iflags, NULL);
194                         if (!am_server)
195 -                               discard_receive_data(f_in, F_LENGTH(file));
196 +                               discard_receive_data(f_in, file);
197                         if (inc_recurse)
198                                 send_msg_int(MSG_SUCCESS, ndx);
199                         continue;
200 @@ -767,7 +768,7 @@ int recv_files(int f_in, int f_out, char *local_name)
201                 } else if (do_fstat(fd1,&st) != 0) {
202                         rsyserr(FERROR_XFER, errno, "fstat %s failed",
203                                 full_fname(fnamecmp));
204 -                       discard_receive_data(f_in, F_LENGTH(file));
205 +                       discard_receive_data(f_in, file);
206                         close(fd1);
207                         if (inc_recurse)
208                                 send_msg_int(MSG_NO_SEND, ndx);
209 @@ -782,18 +783,32 @@ int recv_files(int f_in, int f_out, char *local_name)
210                          */
211                         rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
212                                 full_fname(fnamecmp));
213 -                       discard_receive_data(f_in, F_LENGTH(file));
214 +                       discard_receive_data(f_in, file);
215                         close(fd1);
216                         if (inc_recurse)
217                                 send_msg_int(MSG_NO_SEND, ndx);
218                         continue;
219                 }
220  
221 -               if (fd1 != -1 && !S_ISREG(st.st_mode)) {
222 +               if (fd1 != -1 && !(S_ISREG(st.st_mode) || (write_devices && IS_DEVICE(st.st_mode)))) {
223                         close(fd1);
224                         fd1 = -1;
225                 }
226  
227 +               /* On Linux systems (at least), st_size is typically 0 for devices.
228 +                * If so, try to determine the actual device size. */
229 +               if (fd1 != -1 && IS_DEVICE(st.st_mode) && st.st_size == 0) {
230 +                       OFF_T off = lseek(fd1, 0, SEEK_END);
231 +                       if (off == (OFF_T) -1)
232 +                               rsyserr(FERROR, errno, "failed to seek to end of %s to determine size", fname);
233 +                       else {
234 +                               st.st_size = off;
235 +                               off = lseek(fd1, 0, SEEK_SET);
236 +                               if (off != 0)
237 +                                       rsyserr(FERROR, errno, "failed to seek back to beginning of %s to read it", fname);
238 +                       }
239 +               }
240 +
241                 /* If we're not preserving permissions, change the file-list's
242                  * mode based on the local permissions and some heuristics. */
243                 if (!preserve_perms) {
244 @@ -825,7 +840,7 @@ int recv_files(int f_in, int f_out, char *local_name)
245                 }
246  
247                 if (fd2 == -1) {
248 -                       discard_receive_data(f_in, F_LENGTH(file));
249 +                       discard_receive_data(f_in, file);
250                         if (fd1 != -1)
251                                 close(fd1);
252                         if (inc_recurse)
253 @@ -840,8 +855,7 @@ int recv_files(int f_in, int f_out, char *local_name)
254                         rprintf(FINFO, "%s\n", fname);
255  
256                 /* recv file data */
257 -               recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
258 -                                      fname, fd2, F_LENGTH(file));
259 +               recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, fname, fd2, file);
260  
261                 log_item(log_code, file, iflags, NULL);
262