Skip to content

Commit f1d7516

Browse files
committed
Backport "Expose fsync_fname as a public API".
Backport commit b0a48e9 back to 9.0 to allow back-patching another fix that uses fsync_fname.
1 parent e8837fc commit f1d7516

File tree

3 files changed

+57
-57
lines changed

3 files changed

+57
-57
lines changed

src/backend/storage/file/copydir.c

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939

4040
static void copy_file(char *fromfile, char *tofile);
41-
static void fsync_fname(char *fname, bool isdir);
4241

4342

4443
/*
@@ -214,59 +213,3 @@ copy_file(char *fromfile, char *tofile)
214213

215214
pfree(buffer);
216215
}
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
@@ -1948,3 +1948,59 @@ RemovePgTempFilesInDir(const char *tmpdirname)
19481948

19491949
FreeDir(temp_dir);
19501950
}
1951+
1952+
1953+
/*
1954+
* fsync a file
1955+
*
1956+
* Try to fsync directories but ignore errors that indicate the OS
1957+
* just doesn't allow/require fsyncing directories.
1958+
*/
1959+
void
1960+
fsync_fname(char *fname, bool isdir)
1961+
{
1962+
int fd;
1963+
int returncode;
1964+
1965+
/*
1966+
* Some OSs require directories to be opened read-only whereas other
1967+
* systems don't allow us to fsync files opened read-only; so we need both
1968+
* cases here
1969+
*/
1970+
if (!isdir)
1971+
fd = BasicOpenFile(fname,
1972+
O_RDWR | PG_BINARY,
1973+
S_IRUSR | S_IWUSR);
1974+
else
1975+
fd = BasicOpenFile(fname,
1976+
O_RDONLY | PG_BINARY,
1977+
S_IRUSR | S_IWUSR);
1978+
1979+
/*
1980+
* Some OSs don't allow us to open directories at all (Windows returns
1981+
* EACCES)
1982+
*/
1983+
if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES))
1984+
return;
1985+
1986+
else if (fd < 0)
1987+
ereport(ERROR,
1988+
(errcode_for_file_access(),
1989+
errmsg("could not open file \"%s\": %m", fname)));
1990+
1991+
returncode = pg_fsync(fd);
1992+
1993+
/* Some OSs don't allow us to fsync directories at all */
1994+
if (returncode != 0 && isdir && errno == EBADF)
1995+
{
1996+
close(fd);
1997+
return;
1998+
}
1999+
2000+
if (returncode != 0)
2001+
ereport(ERROR,
2002+
(errcode_for_file_access(),
2003+
errmsg("could not fsync file \"%s\": %m", fname)));
2004+
2005+
close(fd);
2006+
}

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