summaryrefslogtreecommitdiffstats
path: root/tests/TemporaryFile.h
diff options
context:
space:
mode:
authorCalin Juravle <calin@google.com>2014-02-24 16:13:50 +0000
committerCalin Juravle <calin@google.com>2014-02-24 20:22:11 +0000
commitd4934a70e69365c97b1378820152e134a0089b5e (patch)
treec26c1ef275cd79486a03d9e4df76193c4a4ccc33 /tests/TemporaryFile.h
parentfe317a3775e16d466bb884a8e054fd77f7087bb3 (diff)
downloadbionic-d4934a70e69365c97b1378820152e134a0089b5e.zip
bionic-d4934a70e69365c97b1378820152e134a0089b5e.tar.gz
bionic-d4934a70e69365c97b1378820152e134a0089b5e.tar.bz2
Added ftw64, nftw64
Bug: 13076637 Change-Id: I5b926526f935b00bba14c2807b61d85f95089c33
Diffstat (limited to 'tests/TemporaryFile.h')
-rw-r--r--tests/TemporaryFile.h46
1 files changed, 36 insertions, 10 deletions
diff --git a/tests/TemporaryFile.h b/tests/TemporaryFile.h
index 2c6fb1c..a7b13b0 100644
--- a/tests/TemporaryFile.h
+++ b/tests/TemporaryFile.h
@@ -16,17 +16,21 @@
#include <unistd.h>
-template<int (*mk_func)(char*)>
+template<int (*mk_fn)(char*)>
class GenericTemporaryFile {
public:
- GenericTemporaryFile() {
- // 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 = 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");
+ }
}
}
@@ -41,8 +45,30 @@ class GenericTemporaryFile {
private:
void init(const char* tmp_dir) {
snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
- fd = mk_func(filename);
+ fd = mk_fn(filename);
}
};
typedef GenericTemporaryFile<mkstemp> TemporaryFile;
+
+class TemporaryDir {
+ public:
+ TemporaryDir() {
+ if (!init("/data/local/tmp")) {
+ init("/tmp");
+ }
+ }
+
+ ~TemporaryDir() {
+ rmdir(dirname);
+ }
+
+ char dirname[1024];
+
+ private:
+ bool init(const char* tmp_dir) {
+ snprintf(dirname, sizeof(dirname), "%s/TemporaryDir-XXXXXX", tmp_dir);
+ return (mkdtemp(dirname) != NULL);
+ }
+
+};