- int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
- if (create_bak_dir && errno == ENOENT && make_bak_dir(dest) == 0) {
- if ((ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode)) < 0)
- save_errno = errno ? errno : save_errno;
- else
- save_errno = 0;
- }
- if (save_errno) {
- rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(dest));
- close(ifd);
- errno = save_errno;
- return -1;
- }
+ int save_errno = errno;
+ rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(dest));
+ close(ifd);
+ errno = save_errno;
+ return -1;
+ }
+ }
+
+#ifdef SUPPORT_PREALLOCATION
+ if (preallocate_files) {
+ STRUCT_STAT srcst;
+
+ /* Try to preallocate enough space for file's eventual length. Can
+ * reduce fragmentation on filesystems like ext4, xfs, and NTFS. */
+ if (do_fstat(ifd, &srcst) < 0)
+ rsyserr(FWARNING, errno, "fstat %s", full_fname(source));
+ else if (srcst.st_size > 0) {
+ if (do_fallocate(ofd, 0, srcst.st_size) == 0) {
+#ifdef PREALLOCATE_NEEDS_TRUNCATE
+ preallocated_len = srcst.st_size;
+#endif
+ } else
+ rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(dest));