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.

Typedefs

typedef void(* process_file_callback_t) (const char *path, file_type_t type, size_t size, const char *link_target)
 

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)
 
void sync_target_dir (void)
 
char * slurpFile (const char *datadir, const char *path, size_t *filesize)
 
void traverse_datadir (const char *datadir, process_file_callback_t callback)
 

Typedef Documentation

◆ process_file_callback_t

typedef void(* process_file_callback_t) (const char *path, file_type_t type, size_t size, const char *link_target)

Definition at line 26 of file file_ops.h.

Function Documentation

◆ close_target_file()

void close_target_file ( void  )

Definition at line 75 of file file_ops.c.

References close, dstfd, dstpath, and pg_fatal.

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

76 {
77  if (dstfd == -1)
78  return;
79 
80  if (close(dstfd) != 0)
81  pg_fatal("could not close target file \"%s\": %m",
82  dstpath);
83 
84  dstfd = -1;
85 }
static int dstfd
Definition: file_ops.c:31
#define pg_fatal(...)
Definition: pg_rewind.h:37
static char dstpath[MAXPGPATH]
Definition: file_ops.c:32
#define close(a)
Definition: win32.h:12

◆ create_target()

void create_target ( file_entry_t t)

Definition at line 156 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_TYPE_UNDEFINED, file_entry_t::path, pg_fatal, file_entry_t::source_link_target, file_entry_t::source_type, and file_entry_t::target_exists.

Referenced by perform_rewind().

157 {
158  Assert(entry->action == FILE_ACTION_CREATE);
159  Assert(!entry->target_exists);
160 
161  switch (entry->source_type)
162  {
163  case FILE_TYPE_DIRECTORY:
164  create_target_dir(entry->path);
165  break;
166 
167  case FILE_TYPE_SYMLINK:
168  create_target_symlink(entry->path, entry->source_link_target);
169  break;
170 
171  case FILE_TYPE_REGULAR:
172  /* can't happen. Regular files are created with open_target_file. */
173  pg_fatal("invalid action (CREATE) for regular file");
174  break;
175 
176  case FILE_TYPE_UNDEFINED:
177  pg_fatal("undefined file type for \"%s\"", entry->path);
178  break;
179  }
180 }
#define pg_fatal(...)
Definition: pg_rewind.h:37
static void create_target_symlink(const char *path, const char *link)
Definition: file_ops.c:257
static void create_target_dir(const char *path)
Definition: file_ops.c:229
#define Assert(condition)
Definition: c.h:804

◆ open_target_file()

void open_target_file ( const char *  path,
bool  trunc 
)

Definition at line 47 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(), local_fetch_file_range(), perform_rewind(), and process_queued_fetch_requests().

48 {
49  int mode;
50 
51  if (dry_run)
52  return;
53 
54  if (dstfd != -1 && !trunc &&
55  strcmp(path, &dstpath[strlen(datadir_target) + 1]) == 0)
56  return; /* already open */
57 
59 
60  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
61 
62  mode = O_WRONLY | O_CREAT | PG_BINARY;
63  if (trunc)
64  mode |= O_TRUNC;
65  dstfd = open(dstpath, mode, pg_file_create_mode);
66  if (dstfd < 0)
67  pg_fatal("could not open target file \"%s\": %m",
68  dstpath);
69 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
char * datadir_target
Definition: pg_rewind.c:59
static int dstfd
Definition: file_ops.c:31
int pg_file_create_mode
Definition: file_perm.c:19
bool dry_run
Definition: pg_rewind.c:66
#define pg_fatal(...)
Definition: pg_rewind.h:37
#define PG_BINARY
Definition: c.h:1271
static char dstpath[MAXPGPATH]
Definition: file_ops.c:32
void close_target_file(void)
Definition: file_ops.c:75
#define snprintf
Definition: port.h:216

◆ remove_target()

void remove_target ( file_entry_t t)

Definition at line 130 of file file_ops.c.

References file_entry_t::action, Assert, FILE_ACTION_REMOVE, FILE_TYPE_DIRECTORY, FILE_TYPE_REGULAR, FILE_TYPE_SYMLINK, FILE_TYPE_UNDEFINED, file_entry_t::path, pg_fatal, remove_target_dir(), remove_target_file(), remove_target_symlink(), file_entry_t::target_exists, and file_entry_t::target_type.

Referenced by perform_rewind().

131 {
132  Assert(entry->action == FILE_ACTION_REMOVE);
133  Assert(entry->target_exists);
134 
135  switch (entry->target_type)
136  {
137  case FILE_TYPE_DIRECTORY:
138  remove_target_dir(entry->path);
139  break;
140 
141  case FILE_TYPE_REGULAR:
142  remove_target_file(entry->path, false);
143  break;
144 
145  case FILE_TYPE_SYMLINK:
146  remove_target_symlink(entry->path);
147  break;
148 
149  case FILE_TYPE_UNDEFINED:
150  pg_fatal("undefined file type for \"%s\"", entry->path);
151  break;
152  }
153 }
void remove_target_file(const char *path, bool missing_ok)
Definition: file_ops.c:187
#define pg_fatal(...)
Definition: pg_rewind.h:37
static void remove_target_symlink(const char *path)
Definition: file_ops.c:271
static void remove_target_dir(const char *path)
Definition: file_ops.c:243
#define Assert(condition)
Definition: c.h:804

◆ remove_target_file()

void remove_target_file ( const char *  path,
bool  missing_ok 
)

Definition at line 187 of file file_ops.c.

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

Referenced by process_queued_fetch_requests(), and remove_target().

188 {
189  char dstpath[MAXPGPATH];
190 
191  if (dry_run)
192  return;
193 
194  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
195  if (unlink(dstpath) != 0)
196  {
197  if (errno == ENOENT && missing_ok)
198  return;
199 
200  pg_fatal("could not remove file \"%s\": %m",
201  dstpath);
202  }
203 }
char * datadir_target
Definition: pg_rewind.c:59
bool dry_run
Definition: pg_rewind.c:66
#define pg_fatal(...)
Definition: pg_rewind.h:37
#define MAXPGPATH
static char dstpath[MAXPGPATH]
Definition: file_ops.c:32
#define snprintf
Definition: port.h:216

◆ slurpFile()

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

Definition at line 314 of file file_ops.c.

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

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

315 {
316  int fd;
317  char *buffer;
318  struct stat statbuf;
319  char fullpath[MAXPGPATH];
320  int len;
321  int r;
322 
323  snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
324 
325  if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) == -1)
326  pg_fatal("could not open file \"%s\" for reading: %m",
327  fullpath);
328 
329  if (fstat(fd, &statbuf) < 0)
330  pg_fatal("could not open file \"%s\" for reading: %m",
331  fullpath);
332 
333  len = statbuf.st_size;
334 
335  buffer = pg_malloc(len + 1);
336 
337  r = read(fd, buffer, len);
338  if (r != len)
339  {
340  if (r < 0)
341  pg_fatal("could not read file \"%s\": %m",
342  fullpath);
343  else
344  pg_fatal("could not read file \"%s\": read %d of %zu",
345  fullpath, r, (Size) len);
346  }
347  close(fd);
348 
349  /* Zero-terminate the buffer. */
350  buffer[len] = '\0';
351 
352  if (filesize)
353  *filesize = len;
354  return buffer;
355 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define pg_fatal(...)
Definition: pg_rewind.h:37
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1271
#define fstat
Definition: win32_port.h:274
#define MAXPGPATH
char * datadir
size_t Size
Definition: c.h:540
#define close(a)
Definition: win32.h:12
#define snprintf
Definition: port.h:216
#define read(a, b, c)
Definition: win32.h:13

◆ sync_target_dir()

void sync_target_dir ( void  )

Definition at line 294 of file file_ops.c.

References datadir_target, do_sync, and dry_run.

Referenced by main().

295 {
296  if (!do_sync || dry_run)
297  return;
298 
299  fsync_pgdata(datadir_target, PG_VERSION_NUM);
300 }
char * datadir_target
Definition: pg_rewind.c:59
bool dry_run
Definition: pg_rewind.c:66
static bool do_sync
Definition: initdb.c:143

◆ traverse_datadir()

void traverse_datadir ( const char *  datadir,
process_file_callback_t  callback 
)

Definition at line 362 of file file_ops.c.

References recurse_dir().

Referenced by local_traverse_files(), and main().

363 {
364  recurse_dir(datadir, NULL, callback);
365 }
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
char * datadir
static void recurse_dir(const char *datadir, const char *parentpath, process_file_callback_t callback)
Definition: file_ops.c:374

◆ truncate_target_file()

void truncate_target_file ( const char *  path,
off_t  newsize 
)

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

207 {
208  char dstpath[MAXPGPATH];
209  int fd;
210 
211  if (dry_run)
212  return;
213 
214  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
215 
216  fd = open(dstpath, O_WRONLY, pg_file_create_mode);
217  if (fd < 0)
218  pg_fatal("could not open file \"%s\" for truncation: %m",
219  dstpath);
220 
221  if (ftruncate(fd, newsize) != 0)
222  pg_fatal("could not truncate file \"%s\" to %u: %m",
223  dstpath, (unsigned int) newsize);
224 
225  close(fd);
226 }
char * datadir_target
Definition: pg_rewind.c:59
int pg_file_create_mode
Definition: file_perm.c:19
bool dry_run
Definition: pg_rewind.c:66
#define pg_fatal(...)
Definition: pg_rewind.h:37
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define MAXPGPATH
static char dstpath[MAXPGPATH]
Definition: file_ops.c:32
#define close(a)
Definition: win32.h:12
#define snprintf
Definition: port.h:216
#define ftruncate(a, b)
Definition: win32_port.h:65

◆ write_target_range()

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

Definition at line 88 of file file_ops.c.

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

Referenced by createBackupLabel(), local_fetch_file_range(), and process_queued_fetch_requests().

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