PostgreSQL Source Code  git master
local_source.c File Reference
#include "postgres_fe.h"
#include <fcntl.h>
#include <unistd.h>
#include "datapagemap.h"
#include "file_ops.h"
#include "filemap.h"
#include "pg_rewind.h"
#include "rewind_source.h"
Include dependency graph for local_source.c:

Go to the source code of this file.

Data Structures

struct  local_source
 

Functions

static void local_traverse_files (rewind_source *source, process_file_callback_t callback)
 
static char * local_fetch_file (rewind_source *source, const char *path, size_t *filesize)
 
static void local_fetch_file_range (rewind_source *source, const char *path, off_t off, size_t len)
 
static void local_finish_fetch (rewind_source *source)
 
static void local_destroy (rewind_source *source)
 
rewind_sourceinit_local_source (const char *datadir)
 

Function Documentation

◆ init_local_source()

rewind_source* init_local_source ( const char *  datadir)

Definition at line 38 of file local_source.c.

References local_source::common, local_source::datadir, datadir, rewind_source::destroy, rewind_source::fetch_file, rewind_source::finish_fetch, rewind_source::get_current_wal_insert_lsn, local_destroy(), local_fetch_file(), local_fetch_file_range(), local_finish_fetch(), local_traverse_files(), pg_malloc0(), rewind_source::queue_fetch_range, and rewind_source::traverse_files.

Referenced by main().

39 {
40  local_source *src;
41 
42  src = pg_malloc0(sizeof(local_source));
43 
50 
51  src->datadir = datadir;
52 
53  return &src->common;
54 }
static void local_traverse_files(rewind_source *source, process_file_callback_t callback)
Definition: local_source.c:57
static void local_destroy(rewind_source *source)
Definition: local_source.c:128
void(* finish_fetch)(struct rewind_source *)
Definition: rewind_source.h:53
rewind_source common
Definition: local_source.c:23
void(* traverse_files)(struct rewind_source *, process_file_callback_t callback)
Definition: rewind_source.h:29
char *(* fetch_file)(struct rewind_source *, const char *path, size_t *filesize)
Definition: rewind_source.h:37
static char * local_fetch_file(rewind_source *source, const char *path, size_t *filesize)
Definition: local_source.c:63
static void local_fetch_file_range(rewind_source *source, const char *path, off_t off, size_t len)
Definition: local_source.c:72
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
XLogRecPtr(* get_current_wal_insert_lsn)(struct rewind_source *)
Definition: rewind_source.h:58
char * datadir
void(* queue_fetch_range)(struct rewind_source *, const char *path, off_t offset, size_t len)
Definition: rewind_source.h:47
static void local_finish_fetch(rewind_source *source)
Definition: local_source.c:120
void(* destroy)(struct rewind_source *)
Definition: rewind_source.h:63
const char * datadir
Definition: local_source.c:25

◆ local_destroy()

static void local_destroy ( rewind_source source)
static

Definition at line 128 of file local_source.c.

References pfree().

Referenced by init_local_source().

129 {
130  pfree(source);
131 }
void pfree(void *pointer)
Definition: mcxt.c:1169

◆ local_fetch_file()

static char * local_fetch_file ( rewind_source source,
const char *  path,
size_t *  filesize 
)
static

Definition at line 63 of file local_source.c.

References datadir, and slurpFile().

Referenced by init_local_source().

64 {
65  return slurpFile(((local_source *) source)->datadir, path, filesize);
66 }
char * datadir
char * slurpFile(const char *datadir, const char *path, size_t *filesize)
Definition: file_ops.c:314

◆ local_fetch_file_range()

static void local_fetch_file_range ( rewind_source source,
const char *  path,
off_t  off,
size_t  len 
)
static

Definition at line 72 of file local_source.c.

References buf, close, PGAlignedBlock::data, datadir, MAXPGPATH, open_target_file(), PG_BINARY, pg_fatal, read, snprintf, and write_target_range().

Referenced by init_local_source().

74 {
75  const char *datadir = ((local_source *) source)->datadir;
77  char srcpath[MAXPGPATH];
78  int srcfd;
79  off_t begin = off;
80  off_t end = off + len;
81 
82  snprintf(srcpath, sizeof(srcpath), "%s/%s", datadir, path);
83 
84  srcfd = open(srcpath, O_RDONLY | PG_BINARY, 0);
85  if (srcfd < 0)
86  pg_fatal("could not open source file \"%s\": %m",
87  srcpath);
88 
89  if (lseek(srcfd, begin, SEEK_SET) == -1)
90  pg_fatal("could not seek in source file: %m");
91 
92  open_target_file(path, false);
93 
94  while (end - begin > 0)
95  {
96  ssize_t readlen;
97  size_t len;
98 
99  if (end - begin > sizeof(buf))
100  len = sizeof(buf);
101  else
102  len = end - begin;
103 
104  readlen = read(srcfd, buf.data, len);
105 
106  if (readlen < 0)
107  pg_fatal("could not read file \"%s\": %m", srcpath);
108  else if (readlen == 0)
109  pg_fatal("unexpected EOF while reading file \"%s\"", srcpath);
110 
111  write_target_range(buf.data, begin, readlen);
112  begin += readlen;
113  }
114 
115  if (close(srcfd) != 0)
116  pg_fatal("could not close file \"%s\": %m", srcpath);
117 }
void open_target_file(const char *path, bool trunc)
Definition: file_ops.c:47
void write_target_range(char *buf, off_t begin, size_t size)
Definition: file_ops.c:88
#define pg_fatal(...)
Definition: pg_rewind.h:37
#define PG_BINARY
Definition: c.h:1271
char data[BLCKSZ]
Definition: c.h:1141
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:68
char * datadir
#define close(a)
Definition: win32.h:12
#define snprintf
Definition: port.h:216
#define read(a, b, c)
Definition: win32.h:13

◆ local_finish_fetch()

static void local_finish_fetch ( rewind_source source)
static

Definition at line 120 of file local_source.c.

Referenced by init_local_source().

121 {
122  /*
123  * Nothing to do, local_fetch_file_range() copies the ranges immediately.
124  */
125 }

◆ local_traverse_files()

static void local_traverse_files ( rewind_source source,
process_file_callback_t  callback 
)
static

Definition at line 57 of file local_source.c.

References datadir, process_source_file(), and traverse_datadir().

Referenced by init_local_source().

58 {
60 }
void traverse_datadir(const char *datadir, process_file_callback_t callback)
Definition: file_ops.c:362
char * datadir
void process_source_file(const char *path, file_type_t type, size_t size, const char *link_target)
Definition: filemap.c:219