diff options
author | Greg Stark <stark@mit.edu> | 2010-02-14 17:50:43 +0000 |
---|---|---|
committer | Greg Stark <stark@mit.edu> | 2010-02-14 17:50:43 +0000 |
commit | 60e45730a9c15a842c904c84ba8d4bfeca7b8e53 (patch) | |
tree | 4e4e05058a7d289d1c2db8d48fe71790a2f54ae0 | |
parent | bd11a4690bf8e46d1291e1377ce56d661c11cb61 (diff) |
Make CREATE DATABASE safe against losing whole files by fsyncing the
directory and not just the individual files.
Back-patch to 8.1 -- before that we just called "cp -r" and never
fsynced anything anyways.
-rw-r--r-- | src/port/copydir.c | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/port/copydir.c b/src/port/copydir.c index ccad931d76a..37931f0afb1 100644 --- a/src/port/copydir.c +++ b/src/port/copydir.c @@ -11,7 +11,7 @@ * as a service. * * IDENTIFICATION - * $PostgreSQL: pgsql/src/port/copydir.c,v 1.21.2.1 2008/03/31 01:32:01 tgl Exp $ + * $PostgreSQL: pgsql/src/port/copydir.c,v 1.21.2.2 2010/02/14 17:50:43 stark Exp $ * *------------------------------------------------------------------------- */ @@ -50,6 +50,7 @@ copydir(char *fromdir, char *todir, bool recurse) { DIR *xldir; struct dirent *xlde; + int dirfd; char fromfile[MAXPGPATH]; char tofile[MAXPGPATH]; @@ -91,6 +92,26 @@ copydir(char *fromdir, char *todir, bool recurse) } FreeDir(xldir); + + /* + * fsync the directory to make sure not just the data but also the + * new directory file entries have reached the disk. While needed + * by most filesystems, the window got bigger with newer ones like + * ext4. + */ + dirfd = BasicOpenFile(todir, + O_RDONLY | PG_BINARY, + S_IRUSR | S_IWUSR); + if(dirfd == -1) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open directory for fsync \"%s\": %m", todir))); + + if(pg_fsync(dirfd) == -1) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not fsync directory \"%s\": %m", todir))); + close(dirfd); } /* |