]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: add fileutils function collection
authorSami Kerola <kerolasa@iki.fi>
Wed, 29 Feb 2012 14:58:51 +0000 (15:58 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 18 Mar 2012 13:28:04 +0000 (14:28 +0100)
The fileutils contains xmkstemp function will create temporary file
safe and reusable manner.

Reference: http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO.html#TEMPORARY-FILES
CC: Davidlohr Bueso <dave@gnu.org>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/Makefile.am
include/fileutils.h [new file with mode: 0644]
lib/Makefile.am
lib/fileutils.c [new file with mode: 0644]

index 4f5453f669a6139a5449ccedd42f7b350d0b30e5..5e4e54ee800f5a28f305cf856c6c0a6607e60024 100644 (file)
@@ -11,6 +11,7 @@ dist_noinst_HEADERS = \
        crc32.h \
        env.h \
        exitcodes.h \
+       fileutils.h \
        fsprobe.h \
        ismounted.h \
        linux_reboot.h \
@@ -38,5 +39,5 @@ dist_noinst_HEADERS = \
        wholedisk.h \
        widechar.h \
        writeall.h \
-       xgetpass.h \
-       xalloc.h
+       xalloc.h \
+       xgetpass.h
diff --git a/include/fileutils.h b/include/fileutils.h
new file mode 100644 (file)
index 0000000..27b5661
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef UTIL_LINUX_FILEUTILS
+#define UTIL_LINUX_FILEUTILS
+
+extern FILE * xmkstemp(char **tmpname);
+
+#endif
index 19a00f5c9ca90f227f27592edd866527ba13ff53..c34481de37cc0a904186b5646b4660d79ccf63c6 100644 (file)
@@ -6,6 +6,7 @@ noinst_PROGRAMS = \
        test_at \
        test_blkdev \
        test_canonicalize \
+       test_fileutils \
        test_ismounted \
        test_mangle \
        test_procutils \
@@ -45,6 +46,7 @@ test_loopdev_SOURCES = \
 test_loopdev_CFLAGS = -DTEST_PROGRAM_LOOPDEV
 endif
 
+test_fileutils_SOURCES = fileutils.c
 test_tt_SOURCES = tt.c $(top_srcdir)/lib/mbsalign.c
 
 test_canonicalize_SOURCES = canonicalize.c
diff --git a/lib/fileutils.c b/lib/fileutils.c
new file mode 100644 (file)
index 0000000..ed97967
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "c.h"
+#include "pathnames.h"
+
+/* Create open temporary file in safe way.  Please notice that the
+ * file permissions are -rw------- by default. */
+FILE *xmkstemp(char **tmpname)
+{
+       char *localtmp;
+       char *tmpenv;
+       mode_t old_mode;
+       int fd;
+       FILE *ret;
+
+       tmpenv = getenv("TMPDIR");
+       if (tmpenv)
+               asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
+                        program_invocation_short_name);
+       else
+               asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
+                        program_invocation_short_name);
+       old_mode = umask(077);
+       fd = mkstemp(localtmp);
+       umask(old_mode);
+       if (fd == -1)
+               return NULL;
+       if (!(ret = fdopen(fd, "w+")))
+               goto err;
+       *tmpname = localtmp;
+       return ret;
+ err:
+       close(fd);
+       return NULL;
+}
+
+#ifdef TEST_PROGRAM
+int main(void)
+{
+       FILE *f;
+       char *tmpname;
+       f = xmkstemp(&tmpname);
+       unlink(tmpname);
+       free(tmpname);
+       fclose(f);
+       return EXIT_FAILURE;
+}
+#endif