Skip to content

Commit c7299d3

Browse files
committed
Backport "Expose fsync_fname as a public API".
Backport commit cc52d5b back to 9.1 to allow backpatching some unlogged table fixes that use fsync_fname.
1 parent d45e8dc commit c7299d3

File tree

3 files changed

+57
-59
lines changed

3 files changed

+57
-59
lines changed

src/backend/storage/file/copydir.c

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
#include "miscadmin.h"
2828

2929

30-
static void fsync_fname(char *fname, bool isdir);
31-
32-
3330
/*
3431
* copydir: copy a directory
3532
*
@@ -207,59 +204,3 @@ copy_file(char *fromfile, char *tofile)
207204

208205
pfree(buffer);
209206
}
210-
211-
212-
/*
213-
* fsync a file
214-
*
215-
* Try to fsync directories but ignore errors that indicate the OS
216-
* just doesn't allow/require fsyncing directories.
217-
*/
218-
static void
219-
fsync_fname(char *fname, bool isdir)
220-
{
221-
int fd;
222-
int returncode;
223-
224-
/*
225-
* Some OSs require directories to be opened read-only whereas other
226-
* systems don't allow us to fsync files opened read-only; so we need both
227-
* cases here
228-
*/
229-
if (!isdir)
230-
fd = OpenTransientFile(fname,
231-
O_RDWR | PG_BINARY,
232-
S_IRUSR | S_IWUSR);
233-
else
234-
fd = OpenTransientFile(fname,
235-
O_RDONLY | PG_BINARY,
236-
S_IRUSR | S_IWUSR);
237-
238-
/*
239-
* Some OSs don't allow us to open directories at all (Windows returns
240-
* EACCES)
241-
*/
242-
if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES))
243-
return;
244-
245-
else if (fd < 0)
246-
ereport(ERROR,
247-
(errcode_for_file_access(),
248-
errmsg("could not open file \"%s\": %m", fname)));
249-
250-
returncode = pg_fsync(fd);
251-
252-
/* Some OSs don't allow us to fsync directories at all */
253-
if (returncode != 0 && isdir && errno == EBADF)
254-
{
255-
CloseTransientFile(fd);
256-
return;
257-
}
258-
259-
if (returncode != 0)
260-
ereport(ERROR,
261-
(errcode_for_file_access(),
262-
errmsg("could not fsync file \"%s\": %m", fname)));
263-
264-
CloseTransientFile(fd);
265-
}

src/backend/storage/file/fd.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,62 @@ pg_flush_data(int fd, off_t offset, off_t amount)
384384
}
385385

386386

387+
/*
388+
* fsync_fname -- fsync a file or directory, handling errors properly
389+
*
390+
* Try to fsync a file or directory. When doing the latter, ignore errors that
391+
* indicate the OS just doesn't allow/require fsyncing directories.
392+
*/
393+
void
394+
fsync_fname(char *fname, bool isdir)
395+
{
396+
int fd;
397+
int returncode;
398+
399+
/*
400+
* Some OSs require directories to be opened read-only whereas other
401+
* systems don't allow us to fsync files opened read-only; so we need both
402+
* cases here
403+
*/
404+
if (!isdir)
405+
fd = OpenTransientFile(fname,
406+
O_RDWR | PG_BINARY,
407+
S_IRUSR | S_IWUSR);
408+
else
409+
fd = OpenTransientFile(fname,
410+
O_RDONLY | PG_BINARY,
411+
S_IRUSR | S_IWUSR);
412+
413+
/*
414+
* Some OSs don't allow us to open directories at all (Windows returns
415+
* EACCES)
416+
*/
417+
if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES))
418+
return;
419+
420+
else if (fd < 0)
421+
ereport(ERROR,
422+
(errcode_for_file_access(),
423+
errmsg("could not open file \"%s\": %m", fname)));
424+
425+
returncode = pg_fsync(fd);
426+
427+
/* Some OSs don't allow us to fsync directories at all */
428+
if (returncode != 0 && isdir && errno == EBADF)
429+
{
430+
CloseTransientFile(fd);
431+
return;
432+
}
433+
434+
if (returncode != 0)
435+
ereport(ERROR,
436+
(errcode_for_file_access(),
437+
errmsg("could not fsync file \"%s\": %m", fname)));
438+
439+
CloseTransientFile(fd);
440+
}
441+
442+
387443
/*
388444
* InitFileAccess --- initialize this module during backend startup
389445
*

src/include/storage/fd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ extern int pg_fsync_no_writethrough(int fd);
113113
extern int pg_fsync_writethrough(int fd);
114114
extern int pg_fdatasync(int fd);
115115
extern int pg_flush_data(int fd, off_t offset, off_t amount);
116+
extern void fsync_fname(char *fname, bool isdir);
116117

117118
/* Filename components for OpenTemporaryFile */
118119
#define PG_TEMP_FILES_DIR "pgsql_tmp"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy