chiark / gitweb /
fs-util: don't alter errno in unlink_tempfilep()
authorLennart Poettering <lennart@poettering.net>
Fri, 18 May 2018 18:14:54 +0000 (20:14 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
Functions whose only purpose is to be used with _cleanup_() should not
touch errno, so that failing removals do not alter errno at unexpected
places.

This is already done in unlink_and_freep(), rmdir_and_freep(),
rm_rf_physical_and_freep(), hence do so for unlink_tempfilep(), too.

Follow-up for #9013

src/basic/fs-util.c

index 5fb8b88fdbf5d3f5f20d9856283556e79e02c282..b7d5c527122a7ed9411da3e8d81df8e9b8c11b5c 100644 (file)
@@ -1081,6 +1081,15 @@ int access_fd(int fd, int mode) {
         return r;
 }
 
+void unlink_tempfilep(char (*p)[]) {
+        /* If the file is created with mkstemp(), it will (almost always)
+         * change the suffix. Treat this as a sign that the file was
+         * successfully created. We ignore both the rare case where the
+         * original suffix is used and unlink failures. */
+        if (!endswith(*p, ".XXXXXX"))
+                (void) unlink_noerrno(*p);
+}
+
 int unlinkat_deallocate(int fd, const char *name, int flags) {
         _cleanup_close_ int truncate_fd = -1;
         struct stat st;