chiark / gitweb /
fs-util,test: add helper to remove tempfiles
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 16 May 2018 09:35:41 +0000 (11:35 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
This simplifies the use of tempfiles in tests and fixes "leaked"
temporary files in test-fileio, test-catalog, test-conf-parser.

Not the whole tree is converted.

src/basic/fs-util.c
src/basic/fs-util.h
src/test/test-conf-parser.c

index 4e74cb7b0fc1e0edaf75833845f4d6a8e5be283a..24c4470363235728b8620e796e51189947a25ca8 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(*p);
+}
+
 int unlinkat_deallocate(int fd, const char *name, int flags) {
         _cleanup_close_ int truncate_fd = -1;
         struct stat st;
index b5bc5c3bc7e8095d40ecf2e9156b3341cd4090c4..a337b5f5c0675ea4b03ecc0bfbb69748756f3b0d 100644 (file)
@@ -122,6 +122,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
 
 int access_fd(int fd, int mode);
 
+void unlink_tempfilep(char (*p)[]);
 int unlinkat_deallocate(int fd, const char *name, int flags);
 
 int fsync_directory_of_file(int fd);
index 76419a74b50dfc8f6e5689f80fe46b9ffec553ac..85954e097944c3f5e2a05cd31cfc8d18b00e7cbb 100644 (file)
@@ -8,6 +8,7 @@
 #include "conf-parser.h"
 #include "fd-util.h"
 #include "fileio.h"
+//#include "fs-util.h"
 #include "log.h"
 #include "macro.h"
 #include "string-util.h"
@@ -329,7 +330,7 @@ static const char* const config_file[] = {
 };
 
 static void test_config_parse(unsigned i, const char *s) {
-        char name[] = "/tmp/test-conf-parser.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
         int fd, r;
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *setting1 = NULL;