PostgreSQL Source Code: src/bin/pg_rewind/local_source.c Source File (original) (raw)
1
2
3
4
5
6
7
8
9
11
12#include <fcntl.h>
14
18
19typedef struct
20{
22
25
29 size_t *filesize);
31 size_t len);
33 off_t off, size_t len);
36
39{
41
43
51
53
55}
56
57static void
59{
61}
62
63static char *
65{
67}
68
69
70
71
72
73
74static void
76{
80 int srcfd;
81 size_t written_len;
82
83 snprintf(srcpath, sizeof(srcpath), "%s/%s", datadir, path);
84
85
86 srcfd = open(srcpath, O_RDONLY | PG_BINARY, 0);
87 if (srcfd < 0)
88 pg_fatal("could not open source file \"%s\": %m",
89 srcpath);
90
91
93
94 written_len = 0;
95 for (;;)
96 {
97 ssize_t read_len;
98
99 read_len = read(srcfd, buf.data, sizeof(buf));
100
101 if (read_len < 0)
102 pg_fatal("could not read file \"%s\": %m", srcpath);
103 else if (read_len == 0)
104 break;
105
107 written_len += read_len;
108 }
109
110
111
112
113
114 if (written_len != len)
115 pg_fatal("size of source file \"%s\" changed concurrently: %d bytes expected, %d copied",
116 srcpath, (int) len, (int) written_len);
117
118 if (close(srcfd) != 0)
119 pg_fatal("could not close file \"%s\": %m", srcpath);
120}
121
122
123
124
125static void
127 size_t len)
128{
132 int srcfd;
133 off_t begin = off;
134 off_t end = off + len;
135
136 snprintf(srcpath, sizeof(srcpath), "%s/%s", datadir, path);
137
138 srcfd = open(srcpath, O_RDONLY | PG_BINARY, 0);
139 if (srcfd < 0)
140 pg_fatal("could not open source file \"%s\": %m",
141 srcpath);
142
143 if (lseek(srcfd, begin, SEEK_SET) == -1)
144 pg_fatal("could not seek in source file: %m");
145
147
148 while (end - begin > 0)
149 {
150 ssize_t readlen;
151 size_t thislen;
152
153 if (end - begin > sizeof(buf))
154 thislen = sizeof(buf);
155 else
156 thislen = end - begin;
157
158 readlen = read(srcfd, buf.data, thislen);
159
160 if (readlen < 0)
161 pg_fatal("could not read file \"%s\": %m", srcpath);
162 else if (readlen == 0)
163 pg_fatal("unexpected EOF while reading file \"%s\"", srcpath);
164
166 begin += readlen;
167 }
168
169 if (close(srcfd) != 0)
170 pg_fatal("could not close file \"%s\": %m", srcpath);
171}
172
173static void
175{
176
177
178
179}
180
181static void
183{
185}
void * pg_malloc0(size_t size)
void traverse_datadir(const char *datadir, process_file_callback_t callback)
char * slurpFile(const char *datadir, const char *path, size_t *filesize)
void open_target_file(const char *path, bool trunc)
void write_target_range(char *buf, off_t begin, size_t size)
void(* process_file_callback_t)(const char *path, file_type_t type, size_t size, const char *link_target)
static void local_queue_fetch_file(rewind_source *source, const char *path, size_t len)
static void local_destroy(rewind_source *source)
rewind_source * init_local_source(const char *datadir)
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_finish_fetch(rewind_source *source)
static void local_queue_fetch_range(rewind_source *source, const char *path, off_t off, size_t len)
void pfree(void *pointer)
static rewind_source * source
void(* queue_fetch_file)(struct rewind_source *, const char *path, size_t len)
void(* traverse_files)(struct rewind_source *, process_file_callback_t callback)
void(* finish_fetch)(struct rewind_source *)
XLogRecPtr(* get_current_wal_insert_lsn)(struct rewind_source *)
void(* queue_fetch_range)(struct rewind_source *, const char *path, off_t offset, size_t len)
char *(* fetch_file)(struct rewind_source *, const char *path, size_t *filesize)
void(* destroy)(struct rewind_source *)
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)