2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "private/bionic_macros.h"
22 template <typename T = int (*)(char*)>
23 class GenericTemporaryFile {
25 GenericTemporaryFile(T mk_fn = mkstemp) : mk_fn(mk_fn) {
26 // Since we might be running on the host or the target, and if we're
27 // running on the host we might be running under bionic or glibc,
28 // let's just try both possible temporary directories and take the
29 // first one that works.
30 init("/data/local/tmp");
36 GenericTemporaryFile(const char* dirpath, T mk_fn = mkstemp) : mk_fn(mk_fn) {
40 ~GenericTemporaryFile() {
47 fd = open(filename, O_RDWR);
56 void init(const char* tmp_dir) {
57 snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
61 DISALLOW_COPY_AND_ASSIGN(GenericTemporaryFile);
64 typedef GenericTemporaryFile<> TemporaryFile;
69 if (!init("/data/local/tmp")) {
81 bool init(const char* tmp_dir) {
82 snprintf(dirname, sizeof(dirname), "%s/TemporaryDir-XXXXXX", tmp_dir);
83 return (mkdtemp(dirname) != NULL);
86 DISALLOW_COPY_AND_ASSIGN(TemporaryDir);