Re: checking return value from unlink in write_relcache_init_file - Mailing list pgsql-hackers

From Justin Pryzby
Subject Re: checking return value from unlink in write_relcache_init_file
Date
Msg-id 20210603225413.GM14099@telsasoft.com
Whole thread Raw
In response to checking return value from unlink in write_relcache_init_file  (Zhihong Yu <zyu@yugabyte.com>)
List pgsql-hackers
On Thu, Jun 03, 2021 at 03:44:13PM -0700, Zhihong Yu wrote:
> Hi,
> I was looking at write_relcache_init_file() where an attempt is made to
> unlink the tempfilename.
> 
> However, the return value is not checked.
> If the tempfilename is not removed (the file exists), I think we should log
> a warning and proceed.
> 
> Please comment on the proposed patch.

-       unlink(tempfilename);           /* in case it exists w/wrong permissions */
+       /* in case it exists w/wrong permissions */
+       if (unlink(tempfilename) && errno != ENOENT)
+       {
+               ereport(WARNING,
+                               (errcode_for_file_access(),
+                                errmsg("could not unlink relation-cache initialization file \"%s\": %m",
+                                               tempfilename),
+                                errdetail("Continuing anyway, but there's something wrong.")));
+               return;
+       }
+
 
        fp = AllocateFile(tempfilename, PG_BINARY_W);

The comment here is instructive: the unlink is in advance of AllocateFile(),
and if the file exists with wrong permissions, then AllocateFile would itself fail,
and then issue a warning:

                                 errmsg("could not create relation-cache initialization file \"%s\": %m",
                                                tempfilename),
                                 errdetail("Continuing anyway, but there's something wrong.")));

In that context, I don't think it's needed to check the return of unlink().

-- 
Justin



pgsql-hackers by date:

Previous
From: Zhihong Yu
Date:
Subject: checking return value from unlink in write_relcache_init_file
Next
From: Tom Lane
Date:
Subject: Re: checking return value from unlink in write_relcache_init_file