summaryrefslogtreecommitdiffstats
path: root/tests/TemporaryFile.h
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2014-09-23 17:34:29 -0700
committerElliott Hughes <enh@google.com>2014-09-23 18:21:52 -0700
commit31165edf5733dae8fbe79551b18cbc0e56c8d808 (patch)
treee7ed184d94540342f16514a84e02c1f8d38882db /tests/TemporaryFile.h
parent87b6906f6e0c17b2541535be8e054324cc2fef4a (diff)
downloadbionic-31165edf5733dae8fbe79551b18cbc0e56c8d808.zip
bionic-31165edf5733dae8fbe79551b18cbc0e56c8d808.tar.gz
bionic-31165edf5733dae8fbe79551b18cbc0e56c8d808.tar.bz2
CLOEXEC support in fdopen, freopen, and mkostemp/mkostemps.
Change-Id: I74ea88e0d4973d6ab3c57da7d8bb643c31592b14
Diffstat (limited to 'tests/TemporaryFile.h')
-rw-r--r--tests/TemporaryFile.h35
1 files changed, 21 insertions, 14 deletions
diff --git a/tests/TemporaryFile.h b/tests/TemporaryFile.h
index c4ee2d5..5c2fe4f 100644
--- a/tests/TemporaryFile.h
+++ b/tests/TemporaryFile.h
@@ -17,24 +17,26 @@
#include <fcntl.h>
#include <unistd.h>
-template<int (*mk_fn)(char*)>
+#include "private/bionic_macros.h"
+
+template <typename T = int (*)(char*)>
class GenericTemporaryFile {
public:
- GenericTemporaryFile(const char* dirpath = NULL) {
- if (dirpath != NULL) {
- init(dirpath);
- } else {
- // Since we might be running on the host or the target, and if we're
- // running on the host we might be running under bionic or glibc,
- // let's just try both possible temporary directories and take the
- // first one that works.
- init("/data/local/tmp");
- if (fd == -1) {
- init("/tmp");
- }
+ GenericTemporaryFile(T mk_fn = mkstemp) : mk_fn(mk_fn) {
+ // Since we might be running on the host or the target, and if we're
+ // running on the host we might be running under bionic or glibc,
+ // let's just try both possible temporary directories and take the
+ // first one that works.
+ init("/data/local/tmp");
+ if (fd == -1) {
+ init("/tmp");
}
}
+ GenericTemporaryFile(const char* dirpath, T mk_fn = mkstemp) : mk_fn(mk_fn) {
+ init(dirpath);
+ }
+
~GenericTemporaryFile() {
close(fd);
unlink(filename);
@@ -49,13 +51,17 @@ class GenericTemporaryFile {
char filename[1024];
private:
+ T mk_fn;
+
void init(const char* tmp_dir) {
snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
fd = mk_fn(filename);
}
+
+ DISALLOW_COPY_AND_ASSIGN(GenericTemporaryFile);
};
-typedef GenericTemporaryFile<mkstemp> TemporaryFile;
+typedef GenericTemporaryFile<> TemporaryFile;
class TemporaryDir {
public:
@@ -77,4 +83,5 @@ class TemporaryDir {
return (mkdtemp(dirname) != NULL);
}
+ DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
};