Content-Length: 508062 | pFad | https://github.com/postgrespro/postgres/commit/b0a48e996bd7ff336ea26344d3d97ad32b22a61a

B1 Backport "Expose fsync_fname as a public API". · postgrespro/postgres@b0a48e9 · GitHub
Skip to content

Commit b0a48e9

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 79b2fa5 commit b0a48e9

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
@@ -345,6 +345,62 @@ pg_flush_data(int fd, off_t offset, off_t amount)
345345
}
346346

347347

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

src/include/storage/fd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ extern int pg_fsync_no_writethrough(int fd);
9999
extern int pg_fsync_writethrough(int fd);
100100
extern int pg_fdatasync(int fd);
101101
extern int pg_flush_data(int fd, off_t offset, off_t amount);
102+
extern void fsync_fname(char *fname, bool isdir);
102103

103104
/* Filename components for OpenTemporaryFile */
104105
#define PG_TEMP_FILES_DIR "pgsql_tmp"

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/postgrespro/postgres/commit/b0a48e996bd7ff336ea26344d3d97ad32b22a61a

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy