PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
file_ops.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "file_ops.h"
#include "filemap.h"
#include "logging.h"
#include "pg_rewind.h"
Include dependency graph for file_ops.c:

Go to the source code of this file.

Functions

static void remove_target_file (const char *path)
 
static void create_target_dir (const char *path)
 
static void remove_target_dir (const char *path)
 
static void create_target_symlink (const char *path, const char *link)
 
static void remove_target_symlink (const char *path)
 
void open_target_file (const char *path, bool trunc)
 
void close_target_file (void)
 
void write_target_range (char *buf, off_t begin, size_t size)
 
void remove_target (file_entry_t *entry)
 
void create_target (file_entry_t *entry)
 
void truncate_target_file (const char *path, off_t newsize)
 
char * slurpFile (const char *datadir, const char *path, size_t *filesize)
 

Variables

static int dstfd = -1
 
static char dstpath [MAXPGPATH] = ""
 

Function Documentation

void close_target_file ( void  )

Definition at line 71 of file file_ops.c.

References close, dstfd, dstpath, pg_fatal(), and strerror().

Referenced by copy_executeFileMap(), createBackupLabel(), open_target_file(), and updateControlFile().

72 {
73  if (dstfd == -1)
74  return;
75 
76  if (close(dstfd) != 0)
77  pg_fatal("could not close target file \"%s\": %s\n",
78  dstpath, strerror(errno));
79 
80  dstfd = -1;
81 }
static int dstfd
Definition: file_ops.c:29
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
const char * strerror(int errnum)
Definition: strerror.c:19
#define close(a)
Definition: win32.h:12
void create_target ( file_entry_t entry)

Definition at line 147 of file file_ops.c.

References file_entry_t::action, Assert, create_target_dir(), create_target_symlink(), FILE_ACTION_CREATE, FILE_TYPE_DIRECTORY, FILE_TYPE_REGULAR, FILE_TYPE_SYMLINK, file_entry_t::link_target, file_entry_t::path, pg_fatal(), and file_entry_t::type.

Referenced by copy_executeFileMap(), and libpq_executeFileMap().

148 {
149  Assert(entry->action == FILE_ACTION_CREATE);
150 
151  switch (entry->type)
152  {
153  case FILE_TYPE_DIRECTORY:
154  create_target_dir(entry->path);
155  break;
156 
157  case FILE_TYPE_SYMLINK:
158  create_target_symlink(entry->path, entry->link_target);
159  break;
160 
161  case FILE_TYPE_REGULAR:
162  /* can't happen. Regular files are created with open_target_file. */
163  pg_fatal("invalid action (CREATE) for regular file\n");
164  break;
165  }
166 }
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
file_type_t type
Definition: filemap.h:45
static void create_target_symlink(const char *path, const char *link)
Definition: file_ops.c:234
file_action_t action
Definition: filemap.h:47
char * link_target
Definition: filemap.h:57
static void create_target_dir(const char *path)
Definition: file_ops.c:206
#define Assert(condition)
Definition: c.h:675
char * path
Definition: filemap.h:44
static void create_target_dir ( const char *  path)
static

Definition at line 206 of file file_ops.c.

References datadir_target, dry_run, dstpath, MAXPGPATH, mkdir, pg_fatal(), snprintf(), and strerror().

Referenced by create_target().

207 {
208  char dstpath[MAXPGPATH];
209 
210  if (dry_run)
211  return;
212 
213  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
214  if (mkdir(dstpath, S_IRWXU) != 0)
215  pg_fatal("could not create directory \"%s\": %s\n",
216  dstpath, strerror(errno));
217 }
char * datadir_target
Definition: pg_rewind.c:49
bool dry_run
Definition: pg_rewind.c:55
#define mkdir(a, b)
Definition: win32.h:57
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
#define MAXPGPATH
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
const char * strerror(int errnum)
Definition: strerror.c:19
static void create_target_symlink ( const char *  path,
const char *  link 
)
static

Definition at line 234 of file file_ops.c.

References datadir_target, dry_run, dstpath, MAXPGPATH, pg_fatal(), snprintf(), and strerror().

Referenced by create_target().

235 {
236  char dstpath[MAXPGPATH];
237 
238  if (dry_run)
239  return;
240 
241  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
242  if (symlink(link, dstpath) != 0)
243  pg_fatal("could not create symbolic link at \"%s\": %s\n",
244  dstpath, strerror(errno));
245 }
char * datadir_target
Definition: pg_rewind.c:49
bool dry_run
Definition: pg_rewind.c:55
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
#define MAXPGPATH
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
const char * strerror(int errnum)
Definition: strerror.c:19
void open_target_file ( const char *  path,
bool  trunc 
)

Definition at line 43 of file file_ops.c.

References close_target_file(), datadir_target, dry_run, dstfd, dstpath, PG_BINARY, pg_fatal(), snprintf(), and strerror().

Referenced by copy_file_range(), createBackupLabel(), libpq_executeFileMap(), receiveFileChunks(), and updateControlFile().

44 {
45  int mode;
46 
47  if (dry_run)
48  return;
49 
50  if (dstfd != -1 && !trunc &&
51  strcmp(path, &dstpath[strlen(datadir_target) + 1]) == 0)
52  return; /* already open */
53 
55 
56  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
57 
58  mode = O_WRONLY | O_CREAT | PG_BINARY;
59  if (trunc)
60  mode |= O_TRUNC;
61  dstfd = open(dstpath, mode, 0600);
62  if (dstfd < 0)
63  pg_fatal("could not open target file \"%s\": %s\n",
64  dstpath, strerror(errno));
65 }
char * datadir_target
Definition: pg_rewind.c:49
static int dstfd
Definition: file_ops.c:29
bool dry_run
Definition: pg_rewind.c:55
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
#define PG_BINARY
Definition: c.h:1038
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
void close_target_file(void)
Definition: file_ops.c:71
const char * strerror(int errnum)
Definition: strerror.c:19
void remove_target ( file_entry_t entry)

Definition at line 126 of file file_ops.c.

References file_entry_t::action, Assert, FILE_ACTION_REMOVE, FILE_TYPE_DIRECTORY, FILE_TYPE_REGULAR, FILE_TYPE_SYMLINK, file_entry_t::path, remove_target_dir(), remove_target_file(), remove_target_symlink(), and file_entry_t::type.

Referenced by copy_executeFileMap(), and libpq_executeFileMap().

127 {
128  Assert(entry->action == FILE_ACTION_REMOVE);
129 
130  switch (entry->type)
131  {
132  case FILE_TYPE_DIRECTORY:
133  remove_target_dir(entry->path);
134  break;
135 
136  case FILE_TYPE_REGULAR:
137  remove_target_file(entry->path);
138  break;
139 
140  case FILE_TYPE_SYMLINK:
141  remove_target_symlink(entry->path);
142  break;
143  }
144 }
file_type_t type
Definition: filemap.h:45
file_action_t action
Definition: filemap.h:47
static void remove_target_symlink(const char *path)
Definition: file_ops.c:248
static void remove_target_dir(const char *path)
Definition: file_ops.c:220
#define Assert(condition)
Definition: c.h:675
static void remove_target_file(const char *path)
Definition: file_ops.c:169
char * path
Definition: filemap.h:44
static void remove_target_dir ( const char *  path)
static

Definition at line 220 of file file_ops.c.

References datadir_target, dry_run, dstpath, MAXPGPATH, pg_fatal(), snprintf(), and strerror().

Referenced by remove_target().

221 {
222  char dstpath[MAXPGPATH];
223 
224  if (dry_run)
225  return;
226 
227  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
228  if (rmdir(dstpath) != 0)
229  pg_fatal("could not remove directory \"%s\": %s\n",
230  dstpath, strerror(errno));
231 }
char * datadir_target
Definition: pg_rewind.c:49
bool dry_run
Definition: pg_rewind.c:55
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
#define MAXPGPATH
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
const char * strerror(int errnum)
Definition: strerror.c:19
static void remove_target_file ( const char *  path)
static

Definition at line 169 of file file_ops.c.

References datadir_target, dry_run, dstpath, MAXPGPATH, pg_fatal(), snprintf(), strerror(), and unlink().

Referenced by remove_target().

170 {
171  char dstpath[MAXPGPATH];
172 
173  if (dry_run)
174  return;
175 
176  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
177  if (unlink(dstpath) != 0)
178  pg_fatal("could not remove file \"%s\": %s\n",
179  dstpath, strerror(errno));
180 }
char * datadir_target
Definition: pg_rewind.c:49
bool dry_run
Definition: pg_rewind.c:55
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
#define MAXPGPATH
int unlink(const char *filename)
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
const char * strerror(int errnum)
Definition: strerror.c:19
static void remove_target_symlink ( const char *  path)
static

Definition at line 248 of file file_ops.c.

References datadir_target, dry_run, dstpath, MAXPGPATH, pg_fatal(), snprintf(), strerror(), and unlink().

Referenced by remove_target().

249 {
250  char dstpath[MAXPGPATH];
251 
252  if (dry_run)
253  return;
254 
255  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
256  if (unlink(dstpath) != 0)
257  pg_fatal("could not remove symbolic link \"%s\": %s\n",
258  dstpath, strerror(errno));
259 }
char * datadir_target
Definition: pg_rewind.c:49
bool dry_run
Definition: pg_rewind.c:55
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
#define MAXPGPATH
int unlink(const char *filename)
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
const char * strerror(int errnum)
Definition: strerror.c:19
char* slurpFile ( const char *  datadir,
const char *  path,
size_t *  filesize 
)

Definition at line 276 of file file_ops.c.

References buffer, close, fd(), MAXPGPATH, PG_BINARY, pg_fatal(), pg_malloc(), read, snprintf(), and strerror().

Referenced by fetchFile(), getTimelineHistory(), and main().

277 {
278  int fd;
279  char *buffer;
280  struct stat statbuf;
281  char fullpath[MAXPGPATH];
282  int len;
283 
284  snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
285 
286  if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) == -1)
287  pg_fatal("could not open file \"%s\" for reading: %s\n",
288  fullpath, strerror(errno));
289 
290  if (fstat(fd, &statbuf) < 0)
291  pg_fatal("could not open file \"%s\" for reading: %s\n",
292  fullpath, strerror(errno));
293 
294  len = statbuf.st_size;
295 
296  buffer = pg_malloc(len + 1);
297 
298  if (read(fd, buffer, len) != len)
299  pg_fatal("could not read file \"%s\": %s\n",
300  fullpath, strerror(errno));
301  close(fd);
302 
303  /* Zero-terminate the buffer. */
304  buffer[len] = '\0';
305 
306  if (filesize)
307  *filesize = len;
308  return buffer;
309 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1038
#define MAXPGPATH
char * datadir
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:211
const char * strerror(int errnum)
Definition: strerror.c:19
#define close(a)
Definition: win32.h:12
#define read(a, b, c)
Definition: win32.h:13
void truncate_target_file ( const char *  path,
off_t  newsize 
)

Definition at line 183 of file file_ops.c.

References close, datadir_target, dry_run, dstpath, fd(), ftruncate, MAXPGPATH, pg_fatal(), snprintf(), and strerror().

Referenced by copy_executeFileMap(), and libpq_executeFileMap().

184 {
185  char dstpath[MAXPGPATH];
186  int fd;
187 
188  if (dry_run)
189  return;
190 
191  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
192 
193  fd = open(dstpath, O_WRONLY, 0);
194  if (fd < 0)
195  pg_fatal("could not open file \"%s\" for truncation: %s\n",
196  dstpath, strerror(errno));
197 
198  if (ftruncate(fd, newsize) != 0)
199  pg_fatal("could not truncate file \"%s\" to %u: %s\n",
200  dstpath, (unsigned int) newsize, strerror(errno));
201 
202  close(fd);
203 }
char * datadir_target
Definition: pg_rewind.c:49
bool dry_run
Definition: pg_rewind.c:55
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define MAXPGPATH
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
#define ftruncate(a, b)
Definition: win32.h:59
const char * strerror(int errnum)
Definition: strerror.c:19
#define close(a)
Definition: win32.h:12
void write_target_range ( char *  buf,
off_t  begin,
size_t  size 
)

Definition at line 84 of file file_ops.c.

References buf, dry_run, dstfd, dstpath, fetch_done, pg_fatal(), progress_report(), strerror(), and write.

Referenced by copy_file_range(), createBackupLabel(), receiveFileChunks(), and updateControlFile().

85 {
86  int writeleft;
87  char *p;
88 
89  /* update progress report */
90  fetch_done += size;
91  progress_report(false);
92 
93  if (dry_run)
94  return;
95 
96  if (lseek(dstfd, begin, SEEK_SET) == -1)
97  pg_fatal("could not seek in target file \"%s\": %s\n",
98  dstpath, strerror(errno));
99 
100  writeleft = size;
101  p = buf;
102  while (writeleft > 0)
103  {
104  int writelen;
105 
106  errno = 0;
107  writelen = write(dstfd, p, writeleft);
108  if (writelen < 0)
109  {
110  /* if write didn't set errno, assume problem is no disk space */
111  if (errno == 0)
112  errno = ENOSPC;
113  pg_fatal("could not write file \"%s\": %s\n",
114  dstpath, strerror(errno));
115  }
116 
117  p += writelen;
118  writeleft -= writelen;
119  }
120 
121  /* keep the file open, in case we need to copy more blocks in it */
122 }
static int dstfd
Definition: file_ops.c:29
uint64 fetch_done
Definition: logging.c:22
#define write(a, b, c)
Definition: win32.h:14
bool dry_run
Definition: pg_rewind.c:55
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
static char * buf
Definition: pg_test_fsync.c:66
static void progress_report(int tablespacenum, const char *filename, bool force)
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
const char * strerror(int errnum)
Definition: strerror.c:19

Variable Documentation

int dstfd = -1
static

Definition at line 29 of file file_ops.c.

Referenced by close_target_file(), copy_file(), open_target_file(), and write_target_range().