PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 *entry)
 
void remove_target (file_entry_t *entry)
 
void sync_target_dir (void)
 
charslurpFile (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  )
extern

Definition at line 78 of file file_ops.c.

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

References close, dstfd, dstpath, and pg_fatal.

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

◆ create_target()

void create_target ( file_entry_t entry)
extern

Definition at line 159 of file file_ops.c.

160{
162 Assert(!entry->target_exists);
163
164 switch (entry->source_type)
165 {
167 create_target_dir(entry->path);
168 break;
169
172 break;
173
175 /* can't happen. Regular files are created with open_target_file. */
176 pg_fatal("invalid action (CREATE) for regular file");
177 break;
178
180 pg_fatal("undefined file type for \"%s\"", entry->path);
181 break;
182 }
183}
#define Assert(condition)
Definition c.h:943
static void create_target_symlink(const char *path, const char *link)
Definition file_ops.c:274
static void create_target_dir(const char *path)
Definition file_ops.c:238
@ FILE_ACTION_CREATE
Definition filemap.h:21
@ FILE_TYPE_UNDEFINED
Definition filemap.h:33
@ FILE_TYPE_REGULAR
Definition filemap.h:35
@ FILE_TYPE_SYMLINK
Definition filemap.h:37
@ FILE_TYPE_DIRECTORY
Definition filemap.h:36
const char * path
Definition filemap.h:61
bool target_exists
Definition filemap.h:67
char * source_link_target
Definition filemap.h:84
file_type_t source_type
Definition filemap.h:82
file_action_t action
Definition filemap.h:89

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().

◆ open_target_file()

void open_target_file ( const char path,
bool  trunc 
)
extern

Definition at line 47 of file file_ops.c.

48{
49 int mode;
50
52 pg_fatal("target file path is unsafe for open: \"%s\"", path);
53
54 if (dry_run)
55 return;
56
57 if (dstfd != -1 && !trunc &&
58 strcmp(path, &dstpath[strlen(datadir_target) + 1]) == 0)
59 return; /* already open */
60
62
63 snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
64
66 if (trunc)
67 mode |= O_TRUNC;
69 if (dstfd < 0)
70 pg_fatal("could not open target file \"%s\": %m",
71 dstpath);
72}
#define PG_BINARY
Definition c.h:1374
void close_target_file(void)
Definition file_ops.c:78
int pg_file_create_mode
Definition file_perm.c:19
static PgChecksumMode mode
static bool dry_run
char * datadir_target
Definition pg_rewind.c:67
bool path_is_safe_for_extraction(const char *path)
Definition path.c:637
#define snprintf
Definition port.h:261
static int fb(int x)

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

Referenced by createBackupLabel(), libpq_queue_fetch_file(), local_queue_fetch_file(), local_queue_fetch_range(), and process_queued_fetch_requests().

◆ remove_target()

void remove_target ( file_entry_t entry)
extern

Definition at line 133 of file file_ops.c.

134{
136 Assert(entry->target_exists);
137
138 switch (entry->target_type)
139 {
141 remove_target_dir(entry->path);
142 break;
143
145 remove_target_file(entry->path, false);
146 break;
147
150 break;
151
153 pg_fatal("undefined file type for \"%s\"", entry->path);
154 break;
155 }
156}
void remove_target_file(const char *path, bool missing_ok)
Definition file_ops.c:190
static void remove_target_dir(const char *path)
Definition file_ops.c:256
static void remove_target_symlink(const char *path)
Definition file_ops.c:291
@ FILE_ACTION_REMOVE
Definition filemap.h:28
file_type_t target_type
Definition filemap.h:68

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().

◆ remove_target_file()

void remove_target_file ( const char path,
bool  missing_ok 
)
extern

Definition at line 190 of file file_ops.c.

191{
192 char dstpath[MAXPGPATH];
193
195 pg_fatal("target file path is unsafe for removal: \"%s\"", path);
196
197 if (dry_run)
198 return;
199
200 snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
201 if (unlink(dstpath) != 0)
202 {
203 if (errno == ENOENT && missing_ok)
204 return;
205
206 pg_fatal("could not remove file \"%s\": %m",
207 dstpath);
208 }
209}
#define MAXPGPATH

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

Referenced by process_queued_fetch_requests(), and remove_target().

◆ slurpFile()

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

Definition at line 337 of file file_ops.c.

338{
339 int fd;
340 char *buffer;
341 struct stat statbuf;
342 char fullpath[MAXPGPATH];
343 int len;
344 int r;
345
346 snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
347
348 if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) == -1)
349 pg_fatal("could not open file \"%s\" for reading: %m",
350 fullpath);
351
352 if (fstat(fd, &statbuf) < 0)
353 pg_fatal("could not stat file \"%s\": %m",
354 fullpath);
355
356 len = statbuf.st_size;
357
358 buffer = pg_malloc(len + 1);
359
360 r = read(fd, buffer, len);
361 if (r != len)
362 {
363 if (r < 0)
364 pg_fatal("could not read file \"%s\": %m",
365 fullpath);
366 else
367 pg_fatal("could not read file \"%s\": read %d of %zu",
368 fullpath, r, (Size) len);
369 }
370 close(fd);
371
372 /* Zero-terminate the buffer. */
373 buffer[len] = '\0';
374
375 if (filesize)
376 *filesize = len;
377 return buffer;
378}
size_t Size
Definition c.h:689
void * pg_malloc(size_t size)
Definition fe_memutils.c:53
#define read(a, b, c)
Definition win32.h:13
const void size_t len
char * datadir
static int fd(const char *x, int i)
#define fstat
Definition win32_port.h:73

References close, datadir, fb(), fd(), fstat, len, MAXPGPATH, PG_BINARY, pg_fatal, pg_malloc(), read, and snprintf.

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

◆ sync_target_dir()

void sync_target_dir ( void  )
extern

Definition at line 317 of file file_ops.c.

318{
319 if (!do_sync || dry_run)
320 return;
321
323}
static bool do_sync
Definition initdb.c:164
static DataDirSyncMethod sync_method
Definition initdb.c:170

References datadir_target, do_sync, dry_run, fb(), and sync_method.

Referenced by main().

◆ traverse_datadir()

void traverse_datadir ( const char datadir,
process_file_callback_t  callback 
)
extern

Definition at line 385 of file file_ops.c.

386{
388}
static void recurse_dir(const char *datadir, const char *parentpath, process_file_callback_t callback)
Definition file_ops.c:397
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)

References callback(), datadir, fb(), and recurse_dir().

Referenced by local_traverse_files(), and main().

◆ truncate_target_file()

void truncate_target_file ( const char path,
off_t  newsize 
)
extern

Definition at line 212 of file file_ops.c.

213{
214 char dstpath[MAXPGPATH];
215 int fd;
216
218 pg_fatal("target file path is unsafe for truncation: \"%s\"", path);
219
220 if (dry_run)
221 return;
222
223 snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
224
226 if (fd < 0)
227 pg_fatal("could not open file \"%s\" for truncation: %m",
228 dstpath);
229
230 if (ftruncate(fd, newsize) != 0)
231 pg_fatal("could not truncate file \"%s\" to %u: %m",
232 dstpath, (unsigned int) newsize);
233
234 close(fd);
235}

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

Referenced by perform_rewind().

◆ write_target_range()

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

Definition at line 91 of file file_ops.c.

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

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

Referenced by createBackupLabel(), local_queue_fetch_file(), local_queue_fetch_range(), and process_queued_fetch_requests().