]> git.ipfire.org Git - thirdparty/rsync.git/commitdiff
Create a get_device_size() helper function.
authorWayne Davison <wayne@opencoder.net>
Thu, 11 Jun 2020 23:08:15 +0000 (16:08 -0700)
committerWayne Davison <wayne@opencoder.net>
Thu, 11 Jun 2020 23:09:36 +0000 (16:09 -0700)
flist.c
receiver.c

diff --git a/flist.c b/flist.c
index 82a5d6a7e97643f26137d9eca52b3c7d7ded39f7..dbb0f92182a5f64e9df3628a32a3a5e251128ad8 100644 (file)
--- a/flist.c
+++ b/flist.c
@@ -1420,6 +1420,20 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
        return file;
 }
 
+OFF_T get_device_size(int fd, const char *fname)
+{
+       OFF_T off = lseek(fd, 0, SEEK_END);
+
+       if (off == (OFF_T) -1) {
+               rsyserr(FERROR, errno, "failed to get device size via seek: %s", fname);
+               return 0;
+       }
+       if (lseek(fd, 0, SEEK_SET) != 0)
+               rsyserr(FERROR, errno, "failed to seek device back to start: %s", fname);
+
+       return off;
+}
+
 /* Only called for temporary file_struct entries created by make_file(). */
 void unmake_file(struct file_struct *file)
 {
index e95f1786d75a510677c37884acf9a0bb425043bd..8655e315ecc425de1088e83229684087b9671e10 100644 (file)
@@ -804,19 +804,8 @@ int recv_files(int f_in, int f_out, char *local_name)
                        fd1 = -1;
                }
 
-               /* On Linux systems (at least), st_size is typically 0 for devices.
-                * If so, try to determine the actual device size. */
-               if (fd1 != -1 && IS_DEVICE(st.st_mode) && st.st_size == 0) {
-                       OFF_T off = lseek(fd1, 0, SEEK_END);
-                       if (off == (OFF_T) -1)
-                               rsyserr(FERROR, errno, "failed to seek to end of %s to determine size", fname);
-                       else {
-                               st.st_size = off;
-                               off = lseek(fd1, 0, SEEK_SET);
-                               if (off != 0)
-                                       rsyserr(FERROR, errno, "failed to seek back to beginning of %s to read it", fname);
-                       }
-               }
+               if (fd1 != -1 && IS_DEVICE(st.st_mode) && st.st_size == 0)
+                       st.st_size = get_device_size(fd1, fname);
 
                /* If we're not preserving permissions, change the file-list's
                 * mode based on the local permissions and some heuristics. */