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/types.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 72 of file file_ops.c.

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

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

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

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

149 {
150  Assert(entry->action == FILE_ACTION_CREATE);
151 
152  switch (entry->type)
153  {
154  case FILE_TYPE_DIRECTORY:
155  create_target_dir(entry->path);
156  break;
157 
158  case FILE_TYPE_SYMLINK:
159  create_target_symlink(entry->path, entry->link_target);
160  break;
161 
162  case FILE_TYPE_REGULAR:
163  /* can't happen. Regular files are created with open_target_file. */
164  pg_fatal("invalid action (CREATE) for regular file\n");
165  break;
166  }
167 }
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:235
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:207
#define Assert(condition)
Definition: c.h:671
char * path
Definition: filemap.h:44
static void create_target_dir ( const char *  path)
static

Definition at line 207 of file file_ops.c.

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

Referenced by create_target().

208 {
209  char dstpath[MAXPGPATH];
210 
211  if (dry_run)
212  return;
213 
214  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
215  if (mkdir(dstpath, S_IRWXU) != 0)
216  pg_fatal("could not create directory \"%s\": %s\n",
217  dstpath, strerror(errno));
218 }
char * datadir_target
Definition: pg_rewind.c:49
bool dry_run
Definition: pg_rewind.c:55
#define mkdir(a, b)
Definition: win32.h:65
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:31
const char * strerror(int errnum)
Definition: strerror.c:19
static void create_target_symlink ( const char *  path,
const char *  link 
)
static

Definition at line 235 of file file_ops.c.

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

Referenced by create_target().

236 {
237  char dstpath[MAXPGPATH];
238 
239  if (dry_run)
240  return;
241 
242  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
243  if (symlink(link, dstpath) != 0)
244  pg_fatal("could not create symbolic link at \"%s\": %s\n",
245  dstpath, strerror(errno));
246 }
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:31
const char * strerror(int errnum)
Definition: strerror.c:19
void open_target_file ( const char *  path,
bool  trunc 
)

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

45 {
46  int mode;
47 
48  if (dry_run)
49  return;
50 
51  if (dstfd != -1 && !trunc &&
52  strcmp(path, &dstpath[strlen(datadir_target) + 1]) == 0)
53  return; /* already open */
54 
56 
57  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
58 
59  mode = O_WRONLY | O_CREAT | PG_BINARY;
60  if (trunc)
61  mode |= O_TRUNC;
62  dstfd = open(dstpath, mode, 0600);
63  if (dstfd < 0)
64  pg_fatal("could not open target file \"%s\": %s\n",
65  dstpath, strerror(errno));
66 }
char * datadir_target
Definition: pg_rewind.c:49
static int dstfd
Definition: file_ops.c:30
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:31
void close_target_file(void)
Definition: file_ops.c:72
const char * strerror(int errnum)
Definition: strerror.c:19
void remove_target ( file_entry_t entry)

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

128 {
129  Assert(entry->action == FILE_ACTION_REMOVE);
130 
131  switch (entry->type)
132  {
133  case FILE_TYPE_DIRECTORY:
134  remove_target_dir(entry->path);
135  break;
136 
137  case FILE_TYPE_REGULAR:
138  remove_target_file(entry->path);
139  break;
140 
141  case FILE_TYPE_SYMLINK:
142  remove_target_symlink(entry->path);
143  break;
144  }
145 }
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:249
static void remove_target_dir(const char *path)
Definition: file_ops.c:221
#define Assert(condition)
Definition: c.h:671
static void remove_target_file(const char *path)
Definition: file_ops.c:170
char * path
Definition: filemap.h:44
static void remove_target_dir ( const char *  path)
static

Definition at line 221 of file file_ops.c.

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

Referenced by remove_target().

222 {
223  char dstpath[MAXPGPATH];
224 
225  if (dry_run)
226  return;
227 
228  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
229  if (rmdir(dstpath) != 0)
230  pg_fatal("could not remove directory \"%s\": %s\n",
231  dstpath, strerror(errno));
232 }
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:31
const char * strerror(int errnum)
Definition: strerror.c:19
static void remove_target_file ( const char *  path)
static

Definition at line 170 of file file_ops.c.

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

Referenced by remove_target().

171 {
172  char dstpath[MAXPGPATH];
173 
174  if (dry_run)
175  return;
176 
177  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
178  if (unlink(dstpath) != 0)
179  pg_fatal("could not remove file \"%s\": %s\n",
180  dstpath, strerror(errno));
181 }
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:31
const char * strerror(int errnum)
Definition: strerror.c:19
static void remove_target_symlink ( const char *  path)
static

Definition at line 249 of file file_ops.c.

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

Referenced by remove_target().

250 {
251  char dstpath[MAXPGPATH];
252 
253  if (dry_run)
254  return;
255 
256  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
257  if (unlink(dstpath) != 0)
258  pg_fatal("could not remove symbolic link \"%s\": %s\n",
259  dstpath, strerror(errno));
260 }
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:31
const char * strerror(int errnum)
Definition: strerror.c:19
char* slurpFile ( const char *  datadir,
const char *  path,
size_t *  filesize 
)

Definition at line 277 of file file_ops.c.

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

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

278 {
279  int fd;
280  char *buffer;
281  struct stat statbuf;
282  char fullpath[MAXPGPATH];
283  int len;
284 
285  snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
286 
287  if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) == -1)
288  pg_fatal("could not open file \"%s\" for reading: %s\n",
289  fullpath, strerror(errno));
290 
291  if (fstat(fd, &statbuf) < 0)
292  pg_fatal("could not open file \"%s\" for reading: %s\n",
293  fullpath, strerror(errno));
294 
295  len = statbuf.st_size;
296 
297  buffer = pg_malloc(len + 1);
298 
299  if (read(fd, buffer, len) != len)
300  pg_fatal("could not read file \"%s\": %s\n",
301  fullpath, strerror(errno));
302  close(fd);
303 
304  /* Zero-terminate the buffer. */
305  buffer[len] = '\0';
306 
307  if (filesize)
308  *filesize = len;
309  return buffer;
310 }
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
const char * strerror(int errnum)
Definition: strerror.c:19
#define close(a)
Definition: win32.h:17
#define read(a, b, c)
Definition: win32.h:18
void truncate_target_file ( const char *  path,
off_t  newsize 
)

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

185 {
186  char dstpath[MAXPGPATH];
187  int fd;
188 
189  if (dry_run)
190  return;
191 
192  snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
193 
194  fd = open(dstpath, O_WRONLY, 0);
195  if (fd < 0)
196  pg_fatal("could not open file \"%s\" for truncation: %s\n",
197  dstpath, strerror(errno));
198 
199  if (ftruncate(fd, newsize) != 0)
200  pg_fatal("could not truncate file \"%s\" to %u: %s\n",
201  dstpath, (unsigned int) newsize, strerror(errno));
202 
203  close(fd);
204 }
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:31
#define ftruncate(a, b)
Definition: win32.h:67
const char * strerror(int errnum)
Definition: strerror.c:19
#define close(a)
Definition: win32.h:17
void write_target_range ( char *  buf,
off_t  begin,
size_t  size 
)

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

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

Variable Documentation

int dstfd = -1
static

Definition at line 30 of file file_ops.c.

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