|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.93 2000/03/17 02:36:27 tgl Exp $ |
| 11 | + * $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.94 2000/03/31 19:39:22 tgl Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
|
33 | 33 | #include <errno.h>
|
34 | 34 | #include <sys/file.h>
|
35 | 35 | #include <fcntl.h>
|
| 36 | +#include <unistd.h> |
36 | 37 |
|
37 | 38 | #include "postgres.h"
|
38 | 39 |
|
@@ -2266,14 +2267,26 @@ write_irels(void)
|
2266 | 2267 | int i;
|
2267 | 2268 | int relno;
|
2268 | 2269 | RelationBuildDescInfo bi;
|
| 2270 | + char tempfilename[MAXPGPATH]; |
| 2271 | + char finalfilename[MAXPGPATH]; |
| 2272 | + |
| 2273 | + /* |
| 2274 | + * We must write a temporary file and rename it into place. Otherwise, |
| 2275 | + * another backend starting at about the same time might crash trying to |
| 2276 | + * read the partially-complete file. |
| 2277 | + */ |
| 2278 | + snprintf(tempfilename, sizeof(tempfilename), "%s%c%s.%d", |
| 2279 | + DatabasePath, SEP_CHAR, RELCACHE_INIT_FILENAME, MyProcPid); |
| 2280 | + snprintf(finalfilename, sizeof(finalfilename), "%s%c%s", |
| 2281 | + DatabasePath, SEP_CHAR, RELCACHE_INIT_FILENAME); |
2269 | 2282 |
|
2270 | 2283 | #ifndef __CYGWIN32__
|
2271 |
| - fd = FileNameOpenFile(RELCACHE_INIT_FILENAME, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 2284 | + fd = PathNameOpenFile(tempfilename, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
2272 | 2285 | #else
|
2273 |
| - fd = FileNameOpenFile(RELCACHE_INIT_FILENAME, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600); |
| 2286 | + fd = PathNameOpenFile(tempfilename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600); |
2274 | 2287 | #endif
|
2275 | 2288 | if (fd < 0)
|
2276 |
| - elog(FATAL, "cannot create init file %s", RELCACHE_INIT_FILENAME); |
| 2289 | + elog(FATAL, "cannot create init file %s", tempfilename); |
2277 | 2290 |
|
2278 | 2291 | FileSeek(fd, 0L, SEEK_SET);
|
2279 | 2292 |
|
@@ -2397,4 +2410,10 @@ write_irels(void)
|
2397 | 2410 | }
|
2398 | 2411 |
|
2399 | 2412 | FileClose(fd);
|
| 2413 | + |
| 2414 | + /* |
| 2415 | + * And rename the temp file to its final name, deleting any previously- |
| 2416 | + * existing init file. |
| 2417 | + */ |
| 2418 | + rename(tempfilename, finalfilename); |
2400 | 2419 | }
|
0 commit comments