PostgreSQL Source Code git master
copy_file.h File Reference
#include "c.h"
#include "common/checksum_helper.h"
Include dependency graph for copy_file.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef enum CopyMethod CopyMethod
 

Enumerations

enum  CopyMethod { COPY_METHOD_CLONE , COPY_METHOD_COPY , COPY_METHOD_COPY_FILE_RANGE , COPY_METHOD_LINK }
 

Functions

void copy_file (const char *src, const char *dst, pg_checksum_context *checksum_ctx, CopyMethod copy_method, bool dry_run)
 

Typedef Documentation

◆ CopyMethod

typedef enum CopyMethod CopyMethod

Enumeration Type Documentation

◆ CopyMethod

enum CopyMethod
Enumerator
COPY_METHOD_CLONE 
COPY_METHOD_COPY 
COPY_METHOD_COPY_FILE_RANGE 
COPY_METHOD_LINK 

Definition at line 20 of file copy_file.h.

21{
25#ifdef WIN32
26 COPY_METHOD_COPYFILE,
27#endif
CopyMethod
Definition: copy_file.h:21
@ COPY_METHOD_CLONE
Definition: copy_file.h:22
@ COPY_METHOD_LINK
Definition: copy_file.h:28
@ COPY_METHOD_COPY
Definition: copy_file.h:23
@ COPY_METHOD_COPY_FILE_RANGE
Definition: copy_file.h:24

Function Documentation

◆ copy_file()

void copy_file ( const char *  src,
const char *  dst,
pg_checksum_context checksum_ctx,
CopyMethod  copy_method,
bool  dry_run 
)

Definition at line 52 of file copy_file.c.

55{
56 char *strategy_name = NULL;
57 void (*strategy_implementation) (const char *, const char *,
58 pg_checksum_context *checksum_ctx) = NULL;
59
60 /*
61 * In dry-run mode, we don't actually copy anything, nor do we read any
62 * data from the source file, but we do verify that we can open it.
63 */
64 if (dry_run)
65 {
66 int fd;
67
68 if ((fd = open(src, O_RDONLY | PG_BINARY, 0)) < 0)
69 pg_fatal("could not open file \"%s\": %m", src);
70 if (close(fd) < 0)
71 pg_fatal("could not close file \"%s\": %m", src);
72 }
73
74#ifdef WIN32
75
76 /*
77 * We have no specific switch to enable CopyFile on Windows, because it's
78 * supported (as far as we know) on all Windows machines. So,
79 * automatically enable it unless some other strategy was selected.
80 */
81 if (copy_method == COPY_METHOD_COPY)
82 copy_method = COPY_METHOD_COPYFILE;
83#endif
84
85 /* Determine the name of the copy strategy for use in log messages. */
86 switch (copy_method)
87 {
89 strategy_name = "clone";
90 strategy_implementation = copy_file_clone;
91 break;
93 /* leave NULL for simple block-by-block copy */
94 strategy_implementation = copy_file_blocks;
95 break;
97 strategy_name = "copy_file_range";
98 strategy_implementation = copy_file_by_range;
99 break;
100#ifdef WIN32
101 case COPY_METHOD_COPYFILE:
102 strategy_name = "CopyFile";
103 strategy_implementation = copy_file_copyfile;
104 break;
105#endif
106 case COPY_METHOD_LINK:
107 strategy_name = "link";
108 strategy_implementation = copy_file_link;
109 break;
110 }
111
112 if (dry_run)
113 {
114 if (strategy_name)
115 pg_log_debug("would copy \"%s\" to \"%s\" using strategy %s",
116 src, dst, strategy_name);
117 else
118 pg_log_debug("would copy \"%s\" to \"%s\"",
119 src, dst);
120 }
121 else
122 {
123 if (strategy_name)
124 pg_log_debug("copying \"%s\" to \"%s\" using strategy %s",
125 src, dst, strategy_name);
126 else if (checksum_ctx->type == CHECKSUM_TYPE_NONE)
127 pg_log_debug("copying \"%s\" to \"%s\"",
128 src, dst);
129 else
130 pg_log_debug("copying \"%s\" to \"%s\" and checksumming with %s",
131 src, dst, pg_checksum_type_name(checksum_ctx->type));
132
133 strategy_implementation(src, dst, checksum_ctx);
134 }
135}
#define PG_BINARY
Definition: c.h:1244
char * pg_checksum_type_name(pg_checksum_type type)
@ CHECKSUM_TYPE_NONE
static void copy_file_blocks(const char *src, const char *dst, pg_checksum_context *checksum_ctx)
Definition: copy_file.c:174
static void copy_file_clone(const char *src, const char *dest, pg_checksum_context *checksum_ctx)
Definition: copy_file.c:227
static void copy_file_link(const char *src, const char *dest, pg_checksum_context *checksum_ctx)
Definition: copy_file.c:329
static void copy_file_by_range(const char *src, const char *dest, pg_checksum_context *checksum_ctx)
Definition: copy_file.c:273
#define close(a)
Definition: win32.h:12
#define pg_log_debug(...)
Definition: logging.h:133
#define pg_fatal(...)
static bool dry_run
static int fd(const char *x, int i)
Definition: preproc-init.c:105
pg_checksum_type type

References CHECKSUM_TYPE_NONE, close, copy_file_blocks(), copy_file_by_range(), copy_file_clone(), copy_file_link(), COPY_METHOD_CLONE, COPY_METHOD_COPY, COPY_METHOD_COPY_FILE_RANGE, COPY_METHOD_LINK, dry_run, fd(), PG_BINARY, pg_checksum_type_name(), pg_fatal, pg_log_debug, and pg_checksum_context::type.