diff options
author | Peter Eisentraut <peter_e@gmx.net> | 2017-09-23 09:49:22 -0400 |
---|---|---|
committer | Peter Eisentraut <peter_e@gmx.net> | 2017-09-23 10:16:18 -0400 |
commit | 0c5803b450e0cc29b3527df3f352e6f18a038cc6 (patch) | |
tree | ea9dfa278b42aae6cb47108e50133e9716fcefc2 /src/backend/storage/file/fd.c | |
parent | 404ba54e8fd3036eee0f9241f68b17092ce734ee (diff) | |
download | postgresql-0c5803b450e0cc29b3527df3f352e6f18a038cc6.tar.gz postgresql-0c5803b450e0cc29b3527df3f352e6f18a038cc6.zip |
Refactor new file permission handling
The file handling functions from fd.c were called with a diverse mix of
notations for the file permissions when they were opening new files.
Almost all files created by the server should have the same permissions
set. So change the API so that e.g. OpenTransientFile() automatically
uses the standard permissions set, and OpenTransientFilePerm() is a new
function that takes an explicit permissions set for the few cases where
it is needed. This also saves an unnecessary argument for call sites
that are just opening an existing file.
While we're reviewing these APIs, get rid of the FileName typedef and
use the standard const char * for the file name and mode_t for the file
mode. This makes these functions match other file handling functions
and removes an unnecessary layer of mysteriousness. We can also get rid
of a few casts that way.
Author: David Steele <david@pgmasters.net>
Diffstat (limited to 'src/backend/storage/file/fd.c')
-rw-r--r-- | src/backend/storage/file/fd.c | 68 |
1 files changed, 50 insertions, 18 deletions
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 83b061a0362..b0c174284b4 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -116,6 +116,11 @@ */ #define FD_MINFREE 10 +/* + * Default mode for created files, unless something else is specified using + * the *Perm() function variants. + */ +#define PG_FILE_MODE_DEFAULT (S_IRUSR | S_IWUSR) /* * A number of platforms allow individual processes to open many more files @@ -186,7 +191,7 @@ typedef struct vfd char *fileName; /* name of file, or NULL for unused VFD */ /* NB: fileName is malloc'd, and must be free'd when closing the VFD */ int fileFlags; /* open(2) flags for (re)opening the file */ - int fileMode; /* mode to pass to open(2) */ + mode_t fileMode; /* mode to pass to open(2) */ } Vfd; /* @@ -604,7 +609,7 @@ durable_rename(const char *oldfile, const char *newfile, int elevel) if (fsync_fname_ext(oldfile, false, false, elevel) != 0) return -1; - fd = OpenTransientFile((char *) newfile, PG_BINARY | O_RDWR, 0); + fd = OpenTransientFile(newfile, PG_BINARY | O_RDWR); if (fd < 0) { if (errno != ENOENT) @@ -917,7 +922,17 @@ set_max_safe_fds(void) } /* - * BasicOpenFile --- same as open(2) except can free other FDs if needed + * Open a file with BasicOpenFilePerm() and pass default file mode for the + * fileMode parameter. + */ +int +BasicOpenFile(const char *fileName, int fileFlags) +{ + return BasicOpenFilePerm(fileName, fileFlags, PG_FILE_MODE_DEFAULT); +} + +/* + * BasicOpenFilePerm --- same as open(2) except can free other FDs if needed * * This is exported for use by places that really want a plain kernel FD, * but need to be proof against running out of FDs. Once an FD has been @@ -933,7 +948,7 @@ set_max_safe_fds(void) * this module wouldn't have any open files to close at that point anyway. */ int -BasicOpenFile(FileName fileName, int fileFlags, int fileMode) +BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode) { int fd; @@ -1084,8 +1099,8 @@ LruInsert(File file) * overall system file table being full. So, be prepared to release * another FD if necessary... */ - vfdP->fd = BasicOpenFile(vfdP->fileName, vfdP->fileFlags, - vfdP->fileMode); + vfdP->fd = BasicOpenFilePerm(vfdP->fileName, vfdP->fileFlags, + vfdP->fileMode); if (vfdP->fd < 0) { DO_DB(elog(LOG, "re-open failed: %m")); @@ -1293,6 +1308,16 @@ FileInvalidate(File file) #endif /* + * Open a file with PathNameOpenFilePerm() and pass default file mode for the + * fileMode parameter. + */ +File +PathNameOpenFile(const char *fileName, int fileFlags) +{ + return PathNameOpenFilePerm(fileName, fileFlags, PG_FILE_MODE_DEFAULT); +} + +/* * open a file in an arbitrary directory * * NB: if the passed pathname is relative (which it usually is), @@ -1300,13 +1325,13 @@ FileInvalidate(File file) * (which should always be $PGDATA when this code is running). */ File -PathNameOpenFile(FileName fileName, int fileFlags, int fileMode) +PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode) { char *fnamecopy; File file; Vfd *vfdP; - DO_DB(elog(LOG, "PathNameOpenFile: %s %x %o", + DO_DB(elog(LOG, "PathNameOpenFilePerm: %s %x %o", fileName, fileFlags, fileMode)); /* @@ -1324,7 +1349,7 @@ PathNameOpenFile(FileName fileName, int fileFlags, int fileMode) /* Close excess kernel FDs. */ ReleaseLruFiles(); - vfdP->fd = BasicOpenFile(fileName, fileFlags, fileMode); + vfdP->fd = BasicOpenFilePerm(fileName, fileFlags, fileMode); if (vfdP->fd < 0) { @@ -1461,8 +1486,7 @@ OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError) * temp file that can be reused. */ file = PathNameOpenFile(tempfilepath, - O_RDWR | O_CREAT | O_TRUNC | PG_BINARY, - 0600); + O_RDWR | O_CREAT | O_TRUNC | PG_BINARY); if (file <= 0) { /* @@ -1476,8 +1500,7 @@ OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError) mkdir(tempdirpath, S_IRWXU); file = PathNameOpenFile(tempfilepath, - O_RDWR | O_CREAT | O_TRUNC | PG_BINARY, - 0600); + O_RDWR | O_CREAT | O_TRUNC | PG_BINARY); if (file <= 0 && rejectError) elog(ERROR, "could not create temporary file \"%s\": %m", tempfilepath); @@ -2006,7 +2029,7 @@ FileGetRawFlags(File file) /* * FileGetRawMode - returns the mode bitmask passed to open(2) */ -int +mode_t FileGetRawMode(File file) { Assert(FileIsValid(file)); @@ -2136,12 +2159,21 @@ TryAgain: return NULL; } +/* + * Open a file with OpenTransientFilePerm() and pass default file mode for + * the fileMode parameter. + */ +int +OpenTransientFile(const char *fileName, int fileFlags) +{ + return OpenTransientFilePerm(fileName, fileFlags, PG_FILE_MODE_DEFAULT); +} /* * Like AllocateFile, but returns an unbuffered fd like open(2) */ int -OpenTransientFile(FileName fileName, int fileFlags, int fileMode) +OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode) { int fd; @@ -2158,7 +2190,7 @@ OpenTransientFile(FileName fileName, int fileFlags, int fileMode) /* Close excess kernel FDs. */ ReleaseLruFiles(); - fd = BasicOpenFile(fileName, fileFlags, fileMode); + fd = BasicOpenFilePerm(fileName, fileFlags, fileMode); if (fd >= 0) { @@ -3081,7 +3113,7 @@ pre_sync_fname(const char *fname, bool isdir, int elevel) if (isdir) return; - fd = OpenTransientFile((char *) fname, O_RDONLY | PG_BINARY, 0); + fd = OpenTransientFile(fname, O_RDONLY | PG_BINARY); if (fd < 0) { @@ -3141,7 +3173,7 @@ fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel) else flags |= O_RDONLY; - fd = OpenTransientFile((char *) fname, flags, 0); + fd = OpenTransientFile(fname, flags); /* * Some OSs don't allow us to open directories at all (Windows returns |