Skip to content

Commit 86673a4

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 3c5ce51 commit 86673a4

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
@@ -38,9 +38,6 @@
3838
#endif
3939

4040

41-
static void fsync_fname(char *fname, bool isdir);
42-
43-
4441
/*
4542
* copydir: copy a directory
4643
*
@@ -214,59 +211,3 @@ copy_file(char *fromfile, char *tofile)
214211

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

src/backend/storage/file/fd.c

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

357357

358+
/*
359+
* fsync_fname -- fsync a file or directory, handling errors properly
360+
*
361+
* Try to fsync a file or directory. When doing the latter, ignore errors that
362+
* indicate the OS just doesn't allow/require fsyncing directories.
363+
*/
364+
void
365+
fsync_fname(char *fname, bool isdir)
366+
{
367+
int fd;
368+
int returncode;
369+
370+
/*
371+
* Some OSs require directories to be opened read-only whereas other
372+
* systems don't allow us to fsync files opened read-only; so we need both
373+
* cases here
374+
*/
375+
if (!isdir)
376+
fd = BasicOpenFile(fname,
377+
O_RDWR | PG_BINARY,
378+
S_IRUSR | S_IWUSR);
379+
else
380+
fd = BasicOpenFile(fname,
381+
O_RDONLY | PG_BINARY,
382+
S_IRUSR | S_IWUSR);
383+
384+
/*
385+
* Some OSs don't allow us to open directories at all (Windows returns
386+
* EACCES)
387+
*/
388+
if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES))
389+
return;
390+
391+
else if (fd < 0)
392+
ereport(ERROR,
393+
(errcode_for_file_access(),
394+
errmsg("could not open file \"%s\": %m", fname)));
395+
396+
returncode = pg_fsync(fd);
397+
398+
/* Some OSs don't allow us to fsync directories at all */
399+
if (returncode != 0 && isdir && errno == EBADF)
400+
{
401+
close(fd);
402+
return;
403+
}
404+
405+
if (returncode != 0)
406+
ereport(ERROR,
407+
(errcode_for_file_access(),
408+
errmsg("could not fsync file \"%s\": %m", fname)));
409+
410+
close(fd);
411+
}
412+
413+
358414
/*
359415
* InitFileAccess --- initialize this module during backend startup
360416
*

src/include/storage/fd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ extern int pg_fsync_no_writethrough(int fd);
104104
extern int pg_fsync_writethrough(int fd);
105105
extern int pg_fdatasync(int fd);
106106
extern int pg_flush_data(int fd, off_t offset, off_t amount);
107+
extern void fsync_fname(char *fname, bool isdir);
107108

108109
/* Filename components for OpenTemporaryFile */
109110
#define PG_TEMP_FILES_DIR "pgsql_tmp"

0 commit comments

Comments
 (0)