Add headers needed by pg_combinebackup --clone
authorTomas Vondra <[email protected]>
Sun, 30 Jun 2024 17:02:00 +0000 (19:02 +0200)
committerTomas Vondra <[email protected]>
Sun, 30 Jun 2024 18:51:18 +0000 (20:51 +0200)
The code for file cloning existed, but was not reachable as it relied on
constants from missing headers. Due to that, on Linux --clone always
failed with

  error: file cloning not supported on this platform

Fixed by including the missing headers to relevant places. Adding the
headers revealed a couple compile errors in copy_file_clone(), so fix
those too.

Reported-by: Peter Eisentraut
Discussion: https://postgr.es/m/48da4a1f-ccd9-4988-9622-24f37b1de2b4%40eisentraut.org

src/bin/pg_combinebackup/copy_file.c
src/bin/pg_combinebackup/pg_combinebackup.c

index 05b3e86bd084cddb03110aeb9311e7325c82d449..57a7c3a202c39bb60cb25f089d1e7a3788b7af02 100644 (file)
 #ifdef HAVE_COPYFILE_H
 #include <copyfile.h>
 #endif
+#ifdef __linux__
+#include <sys/ioctl.h>
+#include <linux/fs.h>
+#endif
 #include <fcntl.h>
 #include <limits.h>
 #include <sys/stat.h>
@@ -214,6 +218,9 @@ copy_file_clone(const char *src, const char *dest,
        pg_fatal("error while cloning file \"%s\" to \"%s\": %m", src, dest);
 #elif defined(__linux__) && defined(FICLONE)
    {
+       int         src_fd;
+       int         dest_fd;
+
        if ((src_fd = open(src, O_RDONLY | PG_BINARY, 0)) < 0)
            pg_fatal("could not open file \"%s\": %m", src);
 
@@ -228,8 +235,11 @@ copy_file_clone(const char *src, const char *dest,
            unlink(dest);
 
            pg_fatal("error while cloning file \"%s\" to \"%s\": %s",
-                    src, dest);
+                    src, dest, strerror(save_errno));
        }
+
+       close(src_fd);
+       close(dest_fd);
    }
 #else
    pg_fatal("file cloning not supported on this platform");
index d8e928862b2fdab59537682f49e428ee49881519..363fae234e93f158d9114312c8e15c040150af19 100644 (file)
 #include <fcntl.h>
 #include <limits.h>
 
+#ifdef HAVE_COPYFILE_H
+#include <copyfile.h>
+#endif
+#ifdef __linux__
+#include <sys/ioctl.h>
+#include <linux/fs.h>
+#endif
+
 #include "backup_label.h"
 #include "common/blkreftable.h"
 #include "common/checksum_helper.h"