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

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