]> granicus.if.org Git - postgresql/commitdiff
Refactor new file permission handling
authorPeter Eisentraut <peter_e@gmx.net>
Sat, 23 Sep 2017 13:49:22 +0000 (09:49 -0400)
committerPeter Eisentraut <peter_e@gmx.net>
Sat, 23 Sep 2017 14:16:18 +0000 (10:16 -0400)
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>

21 files changed:
contrib/pg_stat_statements/pg_stat_statements.c
src/backend/access/heap/rewriteheap.c
src/backend/access/transam/slru.c
src/backend/access/transam/timeline.c
src/backend/access/transam/twophase.c
src/backend/access/transam/xlog.c
src/backend/access/transam/xlogutils.c
src/backend/catalog/catalog.c
src/backend/libpq/be-fsstubs.c
src/backend/replication/logical/origin.c
src/backend/replication/logical/reorderbuffer.c
src/backend/replication/logical/snapbuild.c
src/backend/replication/slot.c
src/backend/replication/walsender.c
src/backend/storage/file/copydir.c
src/backend/storage/file/fd.c
src/backend/storage/ipc/dsm_impl.c
src/backend/storage/smgr/md.c
src/backend/utils/cache/relmapper.c
src/backend/utils/misc/guc.c
src/include/storage/fd.h

index fa409d72b7b4b0a21b191bc68e62f1e475b75c2d..3ab1fd2db43ac369f5199482685d8819a8a4635a 100644 (file)
@@ -1869,8 +1869,7 @@ qtext_store(const char *query, int query_len,
        *query_offset = off;
 
        /* Now write the data into the successfully-reserved part of the file */
-       fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDWR | O_CREAT | PG_BINARY,
-                                                  S_IRUSR | S_IWUSR);
+       fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDWR | O_CREAT | PG_BINARY);
        if (fd < 0)
                goto error;
 
@@ -1934,7 +1933,7 @@ qtext_load_file(Size *buffer_size)
        int                     fd;
        struct stat stat;
 
-       fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDONLY | PG_BINARY, 0);
+       fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDONLY | PG_BINARY);
        if (fd < 0)
        {
                if (errno != ENOENT)
index bd560e47e195c1cec0d643f8981bfbee9025e996..f93c194e182c4fc4bd5680eb516baacdc3c8525c 100644 (file)
@@ -1013,8 +1013,7 @@ logical_rewrite_log_mapping(RewriteState state, TransactionId xid,
                src->off = 0;
                memcpy(src->path, path, sizeof(path));
                src->vfd = PathNameOpenFile(path,
-                                                                       O_CREAT | O_EXCL | O_WRONLY | PG_BINARY,
-                                                                       S_IRUSR | S_IWUSR);
+                                                                       O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
                if (src->vfd < 0)
                        ereport(ERROR,
                                        (errcode_for_file_access(),
@@ -1133,8 +1132,7 @@ heap_xlog_logical_rewrite(XLogReaderState *r)
                         xlrec->mapped_xid, XLogRecGetXid(r));
 
        fd = OpenTransientFile(path,
-                                                  O_CREAT | O_WRONLY | PG_BINARY,
-                                                  S_IRUSR | S_IWUSR);
+                                                  O_CREAT | O_WRONLY | PG_BINARY);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -1258,7 +1256,7 @@ CheckPointLogicalRewriteHeap(void)
                }
                else
                {
-                       int                     fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
+                       int                     fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
 
                        /*
                         * The file cannot vanish due to concurrency since this function
index 77edc51e1c9a725053273a4989edf550327091e8..9dd77190ec86759cec61ea77f6016fe0f2b175d7 100644 (file)
@@ -599,7 +599,7 @@ SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
 
        SlruFileName(ctl, path, segno);
 
-       fd = OpenTransientFile(path, O_RDWR | PG_BINARY, S_IRUSR | S_IWUSR);
+       fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
        if (fd < 0)
        {
                /* expected: file doesn't exist */
@@ -654,7 +654,7 @@ SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
         * SlruPhysicalWritePage).  Hence, if we are InRecovery, allow the case
         * where the file doesn't exist, and return zeroes instead.
         */
-       fd = OpenTransientFile(path, O_RDWR | PG_BINARY, S_IRUSR | S_IWUSR);
+       fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
        if (fd < 0)
        {
                if (errno != ENOENT || !InRecovery)
@@ -804,8 +804,7 @@ SlruPhysicalWritePage(SlruCtl ctl, int pageno, int slotno, SlruFlush fdata)
                 * don't use O_EXCL or O_TRUNC or anything like that.
                 */
                SlruFileName(ctl, path, segno);
-               fd = OpenTransientFile(path, O_RDWR | O_CREAT | PG_BINARY,
-                                                          S_IRUSR | S_IWUSR);
+               fd = OpenTransientFile(path, O_RDWR | O_CREAT | PG_BINARY);
                if (fd < 0)
                {
                        slru_errcause = SLRU_OPEN_FAILED;
index 63db8a981dce5aea56b2685796ac112d94c1b034..3d65e5624ad1d103a6d01227174eb68ea2c2504a 100644 (file)
@@ -307,8 +307,7 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
        unlink(tmppath);
 
        /* do not use get_sync_bit() here --- want to fsync only at end of fill */
-       fd = OpenTransientFile(tmppath, O_RDWR | O_CREAT | O_EXCL,
-                                                  S_IRUSR | S_IWUSR);
+       fd = OpenTransientFile(tmppath, O_RDWR | O_CREAT | O_EXCL);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -325,7 +324,7 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
        else
                TLHistoryFilePath(path, parentTLI);
 
-       srcfd = OpenTransientFile(path, O_RDONLY, 0);
+       srcfd = OpenTransientFile(path, O_RDONLY);
        if (srcfd < 0)
        {
                if (errno != ENOENT)
@@ -459,8 +458,7 @@ writeTimeLineHistoryFile(TimeLineID tli, char *content, int size)
        unlink(tmppath);
 
        /* do not use get_sync_bit() here --- want to fsync only at end of fill */
-       fd = OpenTransientFile(tmppath, O_RDWR | O_CREAT | O_EXCL,
-                                                  S_IRUSR | S_IWUSR);
+       fd = OpenTransientFile(tmppath, O_RDWR | O_CREAT | O_EXCL);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
index bfd800bc16b805f8417d272f6ca615a843a2ec64..cfaf8da7812e3d342cda7be5c4ab7fd7faa2ad3f 100644 (file)
@@ -1195,7 +1195,7 @@ ReadTwoPhaseFile(TransactionId xid, bool give_warnings)
 
        TwoPhaseFilePath(path, xid);
 
-       fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
+       fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
        if (fd < 0)
        {
                if (give_warnings)
@@ -1581,8 +1581,7 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
        TwoPhaseFilePath(path, xid);
 
        fd = OpenTransientFile(path,
-                                                  O_CREAT | O_TRUNC | O_WRONLY | PG_BINARY,
-                                                  S_IRUSR | S_IWUSR);
+                                                  O_CREAT | O_TRUNC | O_WRONLY | PG_BINARY);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
index 051347163b53d48ae58d8b5d81fd4505be228940..dd028a12a4027ad697b301085734c215327f8f3a 100644 (file)
@@ -3185,8 +3185,7 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
         */
        if (*use_existent)
        {
-               fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method),
-                                                  S_IRUSR | S_IWUSR);
+               fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method));
                if (fd < 0)
                {
                        if (errno != ENOENT)
@@ -3211,8 +3210,7 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
        unlink(tmppath);
 
        /* do not use get_sync_bit() here --- want to fsync only at end of fill */
-       fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
-                                          S_IRUSR | S_IWUSR);
+       fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -3308,8 +3306,7 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
        *use_existent = false;
 
        /* Now open original target segment (might not be file I just made) */
-       fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method),
-                                          S_IRUSR | S_IWUSR);
+       fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method));
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -3350,7 +3347,7 @@ XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno,
         * Open the source file
         */
        XLogFilePath(path, srcTLI, srcsegno, wal_segment_size);
-       srcfd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
+       srcfd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
        if (srcfd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -3364,8 +3361,7 @@ XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno,
        unlink(tmppath);
 
        /* do not use get_sync_bit() here --- want to fsync only at end of fill */
-       fd = OpenTransientFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
-                                                  S_IRUSR | S_IWUSR);
+       fd = OpenTransientFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -3543,8 +3539,7 @@ XLogFileOpen(XLogSegNo segno)
 
        XLogFilePath(path, ThisTimeLineID, segno, wal_segment_size);
 
-       fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method),
-                                          S_IRUSR | S_IWUSR);
+       fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method));
        if (fd < 0)
                ereport(PANIC,
                                (errcode_for_file_access(),
@@ -3610,7 +3605,7 @@ XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli,
                snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlogfname);
        }
 
-       fd = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
+       fd = BasicOpenFile(path, O_RDONLY | PG_BINARY);
        if (fd >= 0)
        {
                /* Success! */
@@ -4449,8 +4444,7 @@ WriteControlFile(void)
        memcpy(buffer, ControlFile, sizeof(ControlFileData));
 
        fd = BasicOpenFile(XLOG_CONTROL_FILE,
-                                          O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
-                                          S_IRUSR | S_IWUSR);
+                                          O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
        if (fd < 0)
                ereport(PANIC,
                                (errcode_for_file_access(),
@@ -4494,8 +4488,7 @@ ReadControlFile(void)
         * Read data...
         */
        fd = BasicOpenFile(XLOG_CONTROL_FILE,
-                                          O_RDWR | PG_BINARY,
-                                          S_IRUSR | S_IWUSR);
+                                          O_RDWR | PG_BINARY);
        if (fd < 0)
                ereport(PANIC,
                                (errcode_for_file_access(),
@@ -4695,8 +4688,7 @@ UpdateControlFile(void)
        FIN_CRC32C(ControlFile->crc);
 
        fd = BasicOpenFile(XLOG_CONTROL_FILE,
-                                          O_RDWR | PG_BINARY,
-                                          S_IRUSR | S_IWUSR);
+                                          O_RDWR | PG_BINARY);
        if (fd < 0)
                ereport(PANIC,
                                (errcode_for_file_access(),
index b11c94c9b6846751296d4faa34e563f5c90685b1..3af6e19c983d3f4fa784439f03e00df0a7733227 100644 (file)
@@ -694,7 +694,7 @@ XLogRead(char *buf, int segsize, TimeLineID tli, XLogRecPtr startptr,
 
                        XLogFilePath(path, tli, sendSegNo, segsize);
 
-                       sendFile = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
+                       sendFile = BasicOpenFile(path, O_RDONLY | PG_BINARY);
 
                        if (sendFile < 0)
                        {
index 92d943cac7584ff55381b551b37229bedc3ed524..f50ae3e41d518c69d711e887c4ca9a2913268894 100644 (file)
@@ -444,7 +444,7 @@ GetNewRelFileNode(Oid reltablespace, Relation pg_class, char relpersistence)
 
                /* Check for existing file of same name */
                rpath = relpath(rnode, MAIN_FORKNUM);
-               fd = BasicOpenFile(rpath, O_RDONLY | PG_BINARY, 0);
+               fd = BasicOpenFile(rpath, O_RDONLY | PG_BINARY);
 
                if (fd >= 0)
                {
index 19b34bfc84af0dc12f5ea7bf41db021e9c8a50ee..84c2d26402cc5b6a6f8b4bd0a3fbd316394011eb 100644 (file)
@@ -462,7 +462,7 @@ lo_import_internal(text *filename, Oid lobjOid)
         * open the file to be read in
         */
        text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
-       fd = OpenTransientFile(fnamebuf, O_RDONLY | PG_BINARY, S_IRWXU);
+       fd = OpenTransientFile(fnamebuf, O_RDONLY | PG_BINARY);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -540,8 +540,8 @@ be_lo_export(PG_FUNCTION_ARGS)
        oumask = umask(S_IWGRP | S_IWOTH);
        PG_TRY();
        {
-               fd = OpenTransientFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
-                                                          S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+               fd = OpenTransientFilePerm(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
+                                                                  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
        }
        PG_CATCH();
        {
index edc6efb8a6a1a16fef47f9d6ededa5c8048fbd1a..20d32679e0f8c26a8ffc1073e32adf7c379fc9e9 100644 (file)
@@ -546,9 +546,8 @@ CheckPointReplicationOrigin(void)
         * no other backend can perform this at the same time, we're protected by
         * CheckpointLock.
         */
-       tmpfd = OpenTransientFile((char *) tmppath,
-                                                         O_CREAT | O_EXCL | O_WRONLY | PG_BINARY,
-                                                         S_IRUSR | S_IWUSR);
+       tmpfd = OpenTransientFile(tmppath,
+                                                         O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
        if (tmpfd < 0)
                ereport(PANIC,
                                (errcode_for_file_access(),
@@ -660,7 +659,7 @@ StartupReplicationOrigin(void)
 
        elog(DEBUG2, "starting up replication origin progress state");
 
-       fd = OpenTransientFile((char *) path, O_RDONLY | PG_BINARY, 0);
+       fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
 
        /*
         * might have had max_replication_slots == 0 last run, or we just brought
index 68766d522d5b78e1085dfc708e3d3ae566319ec2..0f607bab7030a93e6efc709a82a216e616ab85a6 100644 (file)
@@ -2104,8 +2104,7 @@ ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
 
                        /* open segment, create it if necessary */
                        fd = OpenTransientFile(path,
-                                                                  O_CREAT | O_WRONLY | O_APPEND | PG_BINARY,
-                                                                  S_IRUSR | S_IWUSR);
+                                                                  O_CREAT | O_WRONLY | O_APPEND | PG_BINARY);
 
                        if (fd < 0)
                                ereport(ERROR,
@@ -2349,7 +2348,7 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
                                        NameStr(MyReplicationSlot->data.name), txn->xid,
                                        (uint32) (recptr >> 32), (uint32) recptr);
 
-                       *fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
+                       *fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
                        if (*fd < 0 && errno == ENOENT)
                        {
                                *fd = -1;
@@ -3038,7 +3037,7 @@ ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname)
        LogicalRewriteMappingData map;
 
        sprintf(path, "pg_logical/mappings/%s", fname);
-       fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
+       fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
index fba57a0470caed0156e62d22d08fae0e36808704..ad65b9831de6da3c60073d41c9dfce76a63fa1f3 100644 (file)
@@ -1597,8 +1597,7 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
 
        /* we have valid data now, open tempfile and write it there */
        fd = OpenTransientFile(tmppath,
-                                                  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY,
-                                                  S_IRUSR | S_IWUSR);
+                                                  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
        if (fd < 0)
                ereport(ERROR,
                                (errmsg("could not open file \"%s\": %m", path)));
@@ -1682,7 +1681,7 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
        sprintf(path, "pg_logical/snapshots/%X-%X.snap",
                        (uint32) (lsn >> 32), (uint32) lsn);
 
-       fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
+       fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
 
        if (fd < 0 && errno == ENOENT)
                return false;
index 23de2577effb682d9af38abc4a3bb933f8173f99..0d27b6f39ee2a9f4dddb446f1f86d52b091b4ea3 100644 (file)
@@ -1233,9 +1233,7 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
        sprintf(tmppath, "%s/state.tmp", dir);
        sprintf(path, "%s/state", dir);
 
-       fd = OpenTransientFile(tmppath,
-                                                  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY,
-                                                  S_IRUSR | S_IWUSR);
+       fd = OpenTransientFile(tmppath, O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
        if (fd < 0)
        {
                ereport(elevel,
@@ -1354,7 +1352,7 @@ RestoreSlotFromDisk(const char *name)
 
        elog(DEBUG1, "restoring replication slot from \"%s\"", path);
 
-       fd = OpenTransientFile(path, O_RDWR | PG_BINARY, 0);
+       fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
 
        /*
         * We do not need to handle this as we are rename()ing the directory into
index 56999e931577d852dd798d794cc32fa807b73eca..6ec4e631612f4d4228945822d084c9688b257451 100644 (file)
@@ -472,7 +472,7 @@ SendTimeLineHistory(TimeLineHistoryCmd *cmd)
        pq_sendint(&buf, len, 4);       /* col1 len */
        pq_sendbytes(&buf, histfname, len);
 
-       fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0666);
+       fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -2366,7 +2366,7 @@ retry:
 
                        XLogFilePath(path, curFileTimeLine, sendSegNo, wal_segment_size);
 
-                       sendFile = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
+                       sendFile = BasicOpenFile(path, O_RDONLY | PG_BINARY);
                        if (sendFile < 0)
                        {
                                /*
index 1e2691685a6a9cfbeb0fe170f6352837ab8782aa..a5e074ead806b7232a8874313c1b3ccb877a7696 100644 (file)
@@ -148,14 +148,13 @@ copy_file(char *fromfile, char *tofile)
        /*
         * Open the files
         */
-       srcfd = OpenTransientFile(fromfile, O_RDONLY | PG_BINARY, 0);
+       srcfd = OpenTransientFile(fromfile, O_RDONLY | PG_BINARY);
        if (srcfd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
                                 errmsg("could not open file \"%s\": %m", fromfile)));
 
-       dstfd = OpenTransientFile(tofile, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
-                                                         S_IRUSR | S_IWUSR);
+       dstfd = OpenTransientFile(tofile, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
        if (dstfd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
index 83b061a036239d150042b99d136faf97019a5ad0..b0c174284b4660918acd36aa83b26b17a1604b00 100644 (file)
  */
 #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"));
@@ -1292,6 +1307,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
  *
@@ -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
index 1500465d31cf8691c63ba094200c66e103f5bf9f..c63780139eb7adbc0555dd417a2cb64386561325 100644 (file)
@@ -835,7 +835,7 @@ dsm_impl_mmap(dsm_op op, dsm_handle handle, Size request_size,
 
        /* Create new segment or open an existing one for attach or resize. */
        flags = O_RDWR | (op == DSM_OP_CREATE ? O_CREAT | O_EXCL : 0);
-       if ((fd = OpenTransientFile(name, flags, 0600)) == -1)
+       if ((fd = OpenTransientFile(name, flags)) == -1)
        {
                if (errno != EEXIST)
                        ereport(elevel,
index 65e0abe9ec79c5d0884ad55321dad58ee329baee..64a4ccf0db26bb2f74b414d03a61db91f29dd692 100644 (file)
@@ -304,7 +304,7 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
 
        path = relpath(reln->smgr_rnode, forkNum);
 
-       fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, 0600);
+       fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
 
        if (fd < 0)
        {
@@ -317,7 +317,7 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
                 * already, even if isRedo is not set.  (See also mdopen)
                 */
                if (isRedo || IsBootstrapProcessingMode())
-                       fd = PathNameOpenFile(path, O_RDWR | PG_BINARY, 0600);
+                       fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
                if (fd < 0)
                {
                        /* be sure to report the error reported by create, not open */
@@ -430,7 +430,7 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
                /* truncate(2) would be easier here, but Windows hasn't got it */
                int                     fd;
 
-               fd = OpenTransientFile(path, O_RDWR | PG_BINARY, 0);
+               fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
                if (fd >= 0)
                {
                        int                     save_errno;
@@ -583,7 +583,7 @@ mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
 
        path = relpath(reln->smgr_rnode, forknum);
 
-       fd = PathNameOpenFile(path, O_RDWR | PG_BINARY, 0600);
+       fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
 
        if (fd < 0)
        {
@@ -594,7 +594,7 @@ mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
                 * substitute for mdcreate() in bootstrap mode only. (See mdcreate)
                 */
                if (IsBootstrapProcessingMode())
-                       fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, 0600);
+                       fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
                if (fd < 0)
                {
                        if ((behavior & EXTENSION_RETURN_NULL) &&
@@ -1780,7 +1780,7 @@ _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
        fullpath = _mdfd_segpath(reln, forknum, segno);
 
        /* open the file */
-       fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags, 0600);
+       fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags);
 
        pfree(fullpath);
 
index f5394dc43d4c7aa55047e813d7488f9e5aaa766e..41c2ba7f976bb17224a708b07e9847193799b55a 100644 (file)
@@ -644,8 +644,7 @@ load_relmap_file(bool shared)
        }
 
        /* Read data ... */
-       fd = OpenTransientFile(mapfilename,
-                                                  O_RDONLY | PG_BINARY, S_IRUSR | S_IWUSR);
+       fd = OpenTransientFile(mapfilename, O_RDONLY | PG_BINARY);
        if (fd < 0)
                ereport(FATAL,
                                (errcode_for_file_access(),
@@ -745,9 +744,7 @@ write_relmap_file(bool shared, RelMapFile *newmap,
                realmap = &local_map;
        }
 
-       fd = OpenTransientFile(mapfilename,
-                                                  O_WRONLY | O_CREAT | PG_BINARY,
-                                                  S_IRUSR | S_IWUSR);
+       fd = OpenTransientFile(mapfilename, O_WRONLY | O_CREAT | PG_BINARY);
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
index e1fd446ce51591d8059a123030e05f4e18d59b99..47a5f257071caf9f849985cd131412c283be1a2e 100644 (file)
@@ -7242,8 +7242,7 @@ AlterSystemSetConfigFile(AlterSystemStmt *altersysstmt)
         * truncate and reuse it.
         */
        Tmpfd = BasicOpenFile(AutoConfTmpFileName,
-                                                 O_CREAT | O_RDWR | O_TRUNC,
-                                                 S_IRUSR | S_IWUSR);
+                                                 O_CREAT | O_RDWR | O_TRUNC);
        if (Tmpfd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
index faef39e78d3b7a55be18f21b94a07eb398778d6e..6ea26e63b847352613fb68703ec350199dffc57d 100644 (file)
@@ -22,7 +22,7 @@
  * Use them for all file activity...
  *
  *     File fd;
- *     fd = PathNameOpenFile("foo", O_RDONLY, 0600);
+ *     fd = PathNameOpenFile("foo", O_RDONLY);
  *
  *     AllocateFile();
  *     FreeFile();
@@ -46,8 +46,6 @@
  * FileSeek uses the standard UNIX lseek(2) flags.
  */
 
-typedef char *FileName;
-
 typedef int File;
 
 
@@ -65,7 +63,8 @@ extern int    max_safe_fds;
  */
 
 /* Operations on virtual Files --- equivalent to Unix kernel file ops */
-extern File PathNameOpenFile(FileName fileName, int fileFlags, int fileMode);
+extern File PathNameOpenFile(const char *fileName, int fileFlags);
+extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
 extern File OpenTemporaryFile(bool interXact);
 extern void FileClose(File file);
 extern int     FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info);
@@ -78,7 +77,7 @@ extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_eve
 extern char *FilePathName(File file);
 extern int     FileGetRawDesc(File file);
 extern int     FileGetRawFlags(File file);
-extern int     FileGetRawMode(File file);
+extern mode_t FileGetRawMode(File file);
 
 /* Operations that allow use of regular stdio --- USE WITH CAUTION */
 extern FILE *AllocateFile(const char *name, const char *mode);
@@ -94,11 +93,13 @@ extern struct dirent *ReadDir(DIR *dir, const char *dirname);
 extern int     FreeDir(DIR *dir);
 
 /* Operations to allow use of a plain kernel FD, with automatic cleanup */
-extern int     OpenTransientFile(FileName fileName, int fileFlags, int fileMode);
+extern int     OpenTransientFile(const char *fileName, int fileFlags);
+extern int     OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
 extern int     CloseTransientFile(int fd);
 
 /* If you've really really gotta have a plain kernel FD, use this */
-extern int     BasicOpenFile(FileName fileName, int fileFlags, int fileMode);
+extern int     BasicOpenFile(const char *fileName, int fileFlags);
+extern int     BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
 
 /* Miscellaneous support routines */
 extern void InitFileAccess(void);