PostgreSQL Source Code  git master
file_ops.h File Reference
#include "filemap.h"
Include dependency graph for file_ops.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void open_target_file (const char *path, bool trunc)
 
void write_target_range (char *buf, off_t begin, size_t size)
 
void close_target_file (void)
 
void remove_target_file (const char *path, bool missing_ok)
 
void truncate_target_file (const char *path, off_t newsize)
 
void create_target (file_entry_t *t)
 
void remove_target (file_entry_t *t)
 
char * slurpFile (const char *datadir, const char *path, size_t *filesize)
 

Function Documentation

◆ close_target_file()

void close_target_file ( void  )

Definition at line 70 of file file_ops.c.

References close, dstfd, dstpath, and pg_fatal.

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

71 {
72  if (dstfd == -1)
73  return;
74 
75  if (close(dstfd) != 0)
76  pg_fatal("could not close target file \"%s\": %m",
77  dstpath);
78 
79  dstfd = -1;
80 }
static int dstfd
Definition: file_ops.c:29
#define pg_fatal(...)
Definition: pg_rewind.h:43
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
#define close(a)
Definition: win32.h:12

◆ create_target()

void create_target ( file_entry_t t)

Definition at line 146 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().

147 {
148  Assert(entry->action == FILE_ACTION_CREATE);
149 
150  switch (entry->type)
151  {
152  case FILE_TYPE_DIRECTORY:
153  create_target_dir(entry->path);
154  break;
155 
156  case FILE_TYPE_SYMLINK:
157  create_target_symlink(entry->path, entry->link_target);
158  break;
159 
160  case FILE_TYPE_REGULAR:
161  /* can't happen. Regular files are created with open_target_file. */
162  pg_fatal("invalid action (CREATE) for regular file");
163  break;
164  }
165 }
#define pg_fatal(...)
Definition: pg_rewind.h:43
static void create_target_symlink(const char *path, const char *link)
Definition: file_ops.c:242
static void create_target_dir(const char *path)
Definition: file_ops.c:214
#define Assert(condition)
Definition: c.h:732

◆ open_target_file()

void open_target_file ( const char *  path,
bool  trunc 
)

Definition at line 42 of file file_ops.c.

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

Referenced by createBackupLabel(), libpq_executeFileMap(), receiveFileChunks(), and rewind_copy_file_range().

43 {
44  int mode;
45 
46  if (dry_run)
47  return;
48 
49  if (dstfd != -1 && !trunc &&
50  strcmp(path, &dstpath[strlen(datadir_target) + 1]) == 0)
51  return; /* already open */
52 
54 
55  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
56 
57  mode = O_WRONLY | O_CREAT | PG_BINARY;
58  if (trunc)
59  mode |= O_TRUNC;
60  dstfd = open(dstpath, mode, pg_file_create_mode);
61  if (dstfd < 0)
62  pg_fatal("could not open target file \"%s\": %m",
63  dstpath);
64 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
char * datadir_target
Definition: pg_rewind.c:54
static int dstfd
Definition: file_ops.c:29
int pg_file_create_mode
Definition: file_perm.c:19
bool dry_run
Definition: pg_rewind.c:60
#define pg_fatal(...)
Definition: pg_rewind.h:43
#define PG_BINARY
Definition: c.h:1191
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
void close_target_file(void)
Definition: file_ops.c:70
#define snprintf
Definition: port.h:192

◆ remove_target()

void remove_target ( file_entry_t t)

Definition at line 125 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().

126 {
127  Assert(entry->action == FILE_ACTION_REMOVE);
128 
129  switch (entry->type)
130  {
131  case FILE_TYPE_DIRECTORY:
132  remove_target_dir(entry->path);
133  break;
134 
135  case FILE_TYPE_REGULAR:
136  remove_target_file(entry->path, false);
137  break;
138 
139  case FILE_TYPE_SYMLINK:
140  remove_target_symlink(entry->path);
141  break;
142  }
143 }
void remove_target_file(const char *path, bool missing_ok)
Definition: file_ops.c:172
static void remove_target_symlink(const char *path)
Definition: file_ops.c:256
static void remove_target_dir(const char *path)
Definition: file_ops.c:228
#define Assert(condition)
Definition: c.h:732

◆ remove_target_file()

void remove_target_file ( const char *  path,
bool  missing_ok 
)

Definition at line 172 of file file_ops.c.

References datadir_target, dry_run, dstpath, MAXPGPATH, pg_fatal, and snprintf.

Referenced by receiveFileChunks(), and remove_target().

173 {
174  char dstpath[MAXPGPATH];
175 
176  if (dry_run)
177  return;
178 
179  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
180  if (unlink(dstpath) != 0)
181  {
182  if (errno == ENOENT && missing_ok)
183  return;
184 
185  pg_fatal("could not remove file \"%s\": %m",
186  dstpath);
187  }
188 }
char * datadir_target
Definition: pg_rewind.c:54
bool dry_run
Definition: pg_rewind.c:60
#define pg_fatal(...)
Definition: pg_rewind.h:43
#define MAXPGPATH
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
#define snprintf
Definition: port.h:192

◆ slurpFile()

char* slurpFile ( const char *  datadir,
const char *  path,
size_t *  filesize 
)

Definition at line 284 of file file_ops.c.

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

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

285 {
286  int fd;
287  char *buffer;
288  struct stat statbuf;
289  char fullpath[MAXPGPATH];
290  int len;
291  int r;
292 
293  snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
294 
295  if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) == -1)
296  pg_fatal("could not open file \"%s\" for reading: %m",
297  fullpath);
298 
299  if (fstat(fd, &statbuf) < 0)
300  pg_fatal("could not open file \"%s\" for reading: %m",
301  fullpath);
302 
303  len = statbuf.st_size;
304 
305  buffer = pg_malloc(len + 1);
306 
307  r = read(fd, buffer, len);
308  if (r != len)
309  {
310  if (r < 0)
311  pg_fatal("could not read file \"%s\": %m",
312  fullpath);
313  else
314  pg_fatal("could not read file \"%s\": read %d of %zu",
315  fullpath, r, (Size) len);
316  }
317  close(fd);
318 
319  /* Zero-terminate the buffer. */
320  buffer[len] = '\0';
321 
322  if (filesize)
323  *filesize = len;
324  return buffer;
325 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define pg_fatal(...)
Definition: pg_rewind.h:43
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1191
#define MAXPGPATH
char * datadir
#define stat(a, b)
Definition: win32_port.h:255
size_t Size
Definition: c.h:466
#define close(a)
Definition: win32.h:12
#define snprintf
Definition: port.h:192
#define read(a, b, c)
Definition: win32.h:13

◆ truncate_target_file()

void truncate_target_file ( const char *  path,
off_t  newsize 
)

Definition at line 191 of file file_ops.c.

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

Referenced by copy_executeFileMap(), and libpq_executeFileMap().

192 {
193  char dstpath[MAXPGPATH];
194  int fd;
195 
196  if (dry_run)
197  return;
198 
199  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
200 
201  fd = open(dstpath, O_WRONLY, pg_file_create_mode);
202  if (fd < 0)
203  pg_fatal("could not open file \"%s\" for truncation: %m",
204  dstpath);
205 
206  if (ftruncate(fd, newsize) != 0)
207  pg_fatal("could not truncate file \"%s\" to %u: %m",
208  dstpath, (unsigned int) newsize);
209 
210  close(fd);
211 }
char * datadir_target
Definition: pg_rewind.c:54
int pg_file_create_mode
Definition: file_perm.c:19
bool dry_run
Definition: pg_rewind.c:60
#define pg_fatal(...)
Definition: pg_rewind.h:43
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 close(a)
Definition: win32.h:12
#define snprintf
Definition: port.h:192
#define ftruncate(a, b)
Definition: win32_port.h:60

◆ write_target_range()

void write_target_range ( char *  buf,
off_t  begin,
size_t  size 
)

Definition at line 83 of file file_ops.c.

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

Referenced by createBackupLabel(), receiveFileChunks(), and rewind_copy_file_range().

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