Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #include <unistd.h> |
| 18 | |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 19 | template<int (*mk_fn)(char*)> |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 20 | class GenericTemporaryFile { |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 21 | public: |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 22 | GenericTemporaryFile(const char* dirpath = NULL) { |
| 23 | if (dirpath != NULL) { |
| 24 | init(dirpath); |
| 25 | } else { |
| 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"); |
| 31 | if (fd == -1) { |
| 32 | init("/tmp"); |
| 33 | } |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 34 | } |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 37 | ~GenericTemporaryFile() { |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 38 | close(fd); |
| 39 | unlink(filename); |
| 40 | } |
| 41 | |
| 42 | int fd; |
| 43 | char filename[1024]; |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 44 | |
| 45 | private: |
| 46 | void init(const char* tmp_dir) { |
| 47 | snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 48 | fd = mk_fn(filename); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 49 | } |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 50 | }; |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 51 | |
| 52 | typedef GenericTemporaryFile<mkstemp> TemporaryFile; |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 53 | |
| 54 | class TemporaryDir { |
| 55 | public: |
| 56 | TemporaryDir() { |
| 57 | if (!init("/data/local/tmp")) { |
| 58 | init("/tmp"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | ~TemporaryDir() { |
| 63 | rmdir(dirname); |
| 64 | } |
| 65 | |
| 66 | char dirname[1024]; |
| 67 | |
| 68 | private: |
| 69 | bool init(const char* tmp_dir) { |
| 70 | snprintf(dirname, sizeof(dirname), "%s/TemporaryDir-XXXXXX", tmp_dir); |
| 71 | return (mkdtemp(dirname) != NULL); |
| 72 | } |
| 73 | |
| 74 | }; |