13 #ifdef HAVE_COPYFILE_H
35 static void copy_file_copyfile(
const char *src,
const char *dst,
49 char *strategy_name = NULL;
50 void (*strategy_implementation) (
const char *,
const char *,
62 pg_fatal(
"could not open \"%s\": %m", src);
64 pg_fatal(
"could not close \"%s\": %m", src);
68 copy_method = COPY_METHOD_COPYFILE;
75 strategy_name =
"clone";
83 strategy_name =
"copy_file_range";
87 case COPY_METHOD_COPYFILE:
88 strategy_name =
"CopyFile";
89 strategy_implementation = copy_file_copyfile;
97 pg_log_debug(
"would copy \"%s\" to \"%s\" using strategy %s",
98 src, dst, strategy_name);
106 pg_log_debug(
"copying \"%s\" to \"%s\" using strategy %s",
107 src, dst, strategy_name);
112 pg_log_debug(
"copying \"%s\" to \"%s\" and checksumming with %s",
115 strategy_implementation(src, dst, checksum_ctx);
127 const int buffer_size = 50 * BLCKSZ;
134 if ((src_fd = open(src, O_RDONLY |
PG_BINARY, 0)) < 0)
135 pg_fatal(
"could not open file \"%s\": %m", src);
139 while ((rb =
read(src_fd, buffer, buffer_size)) > 0)
142 pg_fatal(
"could not update checksum of file \"%s\"", src);
146 pg_fatal(
"could not read file \"%s\": %m", src);
162 const int buffer_size = 50 * BLCKSZ;
166 if ((src_fd = open(src, O_RDONLY |
PG_BINARY, 0)) < 0)
167 pg_fatal(
"could not open file \"%s\": %m", src);
169 if ((dest_fd = open(dst, O_WRONLY | O_CREAT | O_EXCL |
PG_BINARY,
171 pg_fatal(
"could not open file \"%s\": %m", dst);
175 while ((rb =
read(src_fd, buffer, buffer_size)) > 0)
179 if ((wb =
write(dest_fd, buffer, rb)) != rb)
182 pg_fatal(
"could not write file \"%s\": %m", dst);
184 pg_fatal(
"could not write file \"%s\": wrote only %d of %d bytes at offset %u",
185 dst, (
int) wb, (
int) rb, offset);
189 pg_fatal(
"could not update checksum of file \"%s\"", dst);
195 pg_fatal(
"could not read file \"%s\": %m", dst);
212 #if defined(HAVE_COPYFILE) && defined(COPYFILE_CLONE_FORCE)
213 if (copyfile(src,
dest, NULL, COPYFILE_CLONE_FORCE) < 0)
214 pg_fatal(
"error while cloning file \"%s\" to \"%s\": %m", src,
dest);
215 #elif defined(__linux__) && defined(FICLONE)
217 if ((src_fd = open(src, O_RDONLY |
PG_BINARY, 0)) < 0)
218 pg_fatal(
"could not open file \"%s\": %m", src);
220 if ((dest_fd = open(
dest, O_RDWR | O_CREAT | O_EXCL |
PG_BINARY,
224 if (ioctl(dest_fd, FICLONE, src_fd) < 0)
226 int save_errno = errno;
230 pg_fatal(
"error while cloning file \"%s\" to \"%s\": %s",
235 pg_fatal(
"file cloning not supported on this platform");
252 #if defined(HAVE_COPY_FILE_RANGE)
257 if ((src_fd = open(src, O_RDONLY |
PG_BINARY, 0)) < 0)
258 pg_fatal(
"could not open file \"%s\": %m", src);
260 if ((dest_fd = open(
dest, O_RDWR | O_CREAT | O_EXCL |
PG_BINARY,
266 nbytes = copy_file_range(src_fd, NULL, dest_fd, NULL, SSIZE_MAX, 0);
268 pg_fatal(
"error while copying file range from \"%s\" to \"%s\": %m",
270 }
while (nbytes > 0);
275 pg_fatal(
"copy_file_range not supported on this platform");
284 copy_file_copyfile(
const char *src,
const char *dst,
287 if (CopyFile(src, dst,
true) == 0)
290 pg_fatal(
"could not copy \"%s\" to \"%s\": %m", src, dst);
char * pg_checksum_type_name(pg_checksum_type type)
int pg_checksum_update(pg_checksum_context *context, const uint8 *input, size_t len)
static void copy_file_blocks(const char *src, const char *dst, pg_checksum_context *checksum_ctx)
void copy_file(const char *src, const char *dst, pg_checksum_context *checksum_ctx, CopyMethod copy_method, bool dry_run)
static void copy_file_by_range(const char *src, const char *dst, pg_checksum_context *checksum_ctx)
static void checksum_file(const char *src, pg_checksum_context *checksum_ctx)
static void copy_file_clone(const char *src, const char *dst, pg_checksum_context *checksum_ctx)
@ COPY_METHOD_COPY_FILE_RANGE
void * pg_malloc(size_t size)
#define pg_log_debug(...)
static int fd(const char *x, int i)
void _dosmaperr(unsigned long)