PostgreSQL Source Code  git master
copy_file.h File Reference
#include "c.h"
#include "common/checksum_helper.h"
#include "common/file_utils.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 }
 

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 

Definition at line 21 of file copy_file.h.

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

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 45 of file copy_file.c.

48 {
49  char *strategy_name = NULL;
50  void (*strategy_implementation) (const char *, const char *,
51  pg_checksum_context *checksum_ctx) = NULL;
52 
53  /*
54  * In dry-run mode, we don't actually copy anything, nor do we read any
55  * data from the source file, but we do verify that we can open it.
56  */
57  if (dry_run)
58  {
59  int fd;
60 
61  if ((fd = open(src, O_RDONLY | PG_BINARY, 0)) < 0)
62  pg_fatal("could not open \"%s\": %m", src);
63  if (close(fd) < 0)
64  pg_fatal("could not close \"%s\": %m", src);
65  }
66 
67 #ifdef WIN32
68  copy_method = COPY_METHOD_COPYFILE;
69 #endif
70 
71  /* Determine the name of the copy strategy for use in log messages. */
72  switch (copy_method)
73  {
74  case COPY_METHOD_CLONE:
75  strategy_name = "clone";
76  strategy_implementation = copy_file_clone;
77  break;
78  case COPY_METHOD_COPY:
79  /* leave NULL for simple block-by-block copy */
80  strategy_implementation = copy_file_blocks;
81  break;
83  strategy_name = "copy_file_range";
84  strategy_implementation = copy_file_by_range;
85  break;
86 #ifdef WIN32
87  case COPY_METHOD_COPYFILE:
88  strategy_name = "CopyFile";
89  strategy_implementation = copy_file_copyfile;
90  break;
91 #endif
92  }
93 
94  if (dry_run)
95  {
96  if (strategy_name)
97  pg_log_debug("would copy \"%s\" to \"%s\" using strategy %s",
98  src, dst, strategy_name);
99  else
100  pg_log_debug("would copy \"%s\" to \"%s\"",
101  src, dst);
102  }
103  else
104  {
105  if (strategy_name)
106  pg_log_debug("copying \"%s\" to \"%s\" using strategy %s",
107  src, dst, strategy_name);
108  else if (checksum_ctx->type == CHECKSUM_TYPE_NONE)
109  pg_log_debug("copying \"%s\" to \"%s\"",
110  src, dst);
111  else
112  pg_log_debug("copying \"%s\" to \"%s\" and checksumming with %s",
113  src, dst, pg_checksum_type_name(checksum_ctx->type));
114 
115  strategy_implementation(src, dst, checksum_ctx);
116  }
117 }
#define PG_BINARY
Definition: c.h:1273
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:156
static void copy_file_by_range(const char *src, const char *dst, pg_checksum_context *checksum_ctx)
Definition: copy_file.c:249
static void copy_file_clone(const char *src, const char *dst, pg_checksum_context *checksum_ctx)
Definition: copy_file.c:209
#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_METHOD_CLONE, COPY_METHOD_COPY, COPY_METHOD_COPY_FILE_RANGE, dry_run, fd(), PG_BINARY, pg_checksum_type_name(), pg_fatal, pg_log_debug, and pg_checksum_context::type.