Use do_ftruncate() for the added call too.
[rsync-patches.git] / copy-devices.diff
1 This patch adds the --copy-devices option, which will try to copy
2 the data inside a device instead of duplicating the device node.
3
4 To use this patch, run these commands for a successful build:
5
6     patch -p1 <patches/copy-devices.diff
7     ./prepare-source
8     ./configure                      (optional if already run)
9     make
10
11 based-on: d64bda1c1e79dc385f194d74f7957ce7cd118654
12 diff --git a/generator.c b/generator.c
13 --- a/generator.c
14 +++ b/generator.c
15 @@ -39,6 +39,7 @@ extern int preserve_acls;
16  extern int preserve_xattrs;
17  extern int preserve_links;
18  extern int preserve_devices;
19 +extern int copy_devices;
20  extern int preserve_specials;
21  extern int preserve_hard_links;
22  extern int preserve_executability;
23 @@ -1497,7 +1498,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
24                 goto cleanup;
25         }
26  
27 -       if (!S_ISREG(file->mode)) {
28 +       if (!(S_ISREG(file->mode) || (copy_devices && IS_DEVICE(file->mode)))) {
29                 if (solo_file)
30                         fname = f_name(file, NULL);
31                 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
32 diff --git a/options.c b/options.c
33 --- a/options.c
34 +++ b/options.c
35 @@ -48,6 +48,7 @@ int append_mode = 0;
36  int keep_dirlinks = 0;
37  int copy_dirlinks = 0;
38  int copy_links = 0;
39 +int copy_devices = 0;
40  int preserve_links = 0;
41  int preserve_hard_links = 0;
42  int preserve_acls = 0;
43 @@ -696,6 +697,7 @@ void usage(enum logcode F)
44    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
45    rprintf(F," -g, --group                 preserve group\n");
46    rprintf(F,"     --devices               preserve device files (super-user only)\n");
47 +  rprintf(F,"     --copy-devices          copy device contents as regular file\n");
48    rprintf(F,"     --specials              preserve special files\n");
49    rprintf(F," -D                          same as --devices --specials\n");
50    rprintf(F," -t, --times                 preserve modification times\n");
51 @@ -868,6 +870,7 @@ static struct poptOption long_options[] = {
52    {"no-D",             0,  POPT_ARG_NONE,   0, OPT_NO_D, 0, 0 },
53    {"devices",          0,  POPT_ARG_VAL,    &preserve_devices, 1, 0, 0 },
54    {"no-devices",       0,  POPT_ARG_VAL,    &preserve_devices, 0, 0, 0 },
55 +  {"copy-devices",     0,  POPT_ARG_NONE,   &copy_devices, 0, 0, 0 },
56    {"specials",         0,  POPT_ARG_VAL,    &preserve_specials, 1, 0, 0 },
57    {"no-specials",      0,  POPT_ARG_VAL,    &preserve_specials, 0, 0, 0 },
58    {"links",           'l', POPT_ARG_VAL,    &preserve_links, 1, 0, 0 },
59 @@ -2674,6 +2677,9 @@ void server_options(char **args, int *argc_p)
60         else if (remove_source_files)
61                 args[ac++] = "--remove-sent-files";
62  
63 +       if (copy_devices)
64 +               args[ac++] = "--copy-devices";
65 +
66         if (ac > MAX_SERVER_ARGS) { /* Not possible... */
67                 rprintf(FERROR, "argc overflow in server_options().\n");
68                 exit_cleanup(RERR_MALLOC);
69 diff --git a/rsync.c b/rsync.c
70 --- a/rsync.c
71 +++ b/rsync.c
72 @@ -33,6 +33,7 @@ extern int preserve_xattrs;
73  extern int preserve_perms;
74  extern int preserve_executability;
75  extern int preserve_times;
76 +extern int copy_devices;
77  extern int am_root;
78  extern int am_server;
79  extern int am_sender;
80 @@ -397,7 +398,8 @@ int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr, uchar *type_ptr,
81  
82         if (iflags & ITEM_TRANSFER) {
83                 int i = ndx - cur_flist->ndx_start;
84 -               if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) {
85 +               struct file_struct *file = cur_flist->files[i];
86 +               if (i < 0 || !(S_ISREG(file->mode) || (copy_devices && IS_DEVICE(file->mode)))) {
87                         rprintf(FERROR,
88                                 "received request to transfer non-regular file: %d [%s]\n",
89                                 ndx, who_am_i());
90 diff --git a/sender.c b/sender.c
91 --- a/sender.c
92 +++ b/sender.c
93 @@ -340,6 +340,20 @@ void send_files(int f_in, int f_out)
94                         exit_cleanup(RERR_FILEIO);
95                 }
96  
97 +               /* On Matt's computer, st_size is falsely 0 for most devices.
98 +                * If this happens, try harder to determine the actual device size. */
99 +               if (IS_DEVICE(st.st_mode) && st.st_size == 0) {
100 +                       OFF_T off = lseek(fd, 0, SEEK_END);
101 +                       if (off == (OFF_T) -1)
102 +                               rsyserr(FERROR, errno, "failed to seek to end of %s to determine size", fname);
103 +                       else {
104 +                               st.st_size = off;
105 +                               off = lseek(fd, 0, SEEK_SET);
106 +                               if (off != 0)
107 +                                       rsyserr(FERROR, errno, "failed to seek back to beginning of %s to read it", fname);
108 +                       }
109 +               }
110 +
111                 if (st.st_size) {
112                         int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
113                         mbuf = map_file(fd, st.st_size, read_size, s->blength);