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 | |
Christopher Ferris | eda26bc | 2014-06-12 13:16:36 -0700 | [diff] [blame] | 17 | #include <fcntl.h> |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 20 | template<int (*mk_fn)(char*)> |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 21 | class GenericTemporaryFile { |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 22 | public: |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 23 | GenericTemporaryFile(const char* dirpath = NULL) { |
| 24 | if (dirpath != NULL) { |
| 25 | init(dirpath); |
| 26 | } else { |
| 27 | // Since we might be running on the host or the target, and if we're |
| 28 | // running on the host we might be running under bionic or glibc, |
| 29 | // let's just try both possible temporary directories and take the |
| 30 | // first one that works. |
| 31 | init("/data/local/tmp"); |
| 32 | if (fd == -1) { |
| 33 | init("/tmp"); |
| 34 | } |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 35 | } |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 38 | ~GenericTemporaryFile() { |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 39 | close(fd); |
| 40 | unlink(filename); |
| 41 | } |
| 42 | |
Christopher Ferris | eda26bc | 2014-06-12 13:16:36 -0700 | [diff] [blame] | 43 | void reopen() { |
| 44 | close(fd); |
| 45 | fd = open(filename, O_RDWR); |
| 46 | } |
| 47 | |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 48 | int fd; |
| 49 | char filename[1024]; |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | void init(const char* tmp_dir) { |
| 53 | snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 54 | fd = mk_fn(filename); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 55 | } |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 56 | }; |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 57 | |
| 58 | typedef GenericTemporaryFile<mkstemp> TemporaryFile; |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 59 | |
| 60 | class TemporaryDir { |
| 61 | public: |
| 62 | TemporaryDir() { |
| 63 | if (!init("/data/local/tmp")) { |
| 64 | init("/tmp"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | ~TemporaryDir() { |
| 69 | rmdir(dirname); |
| 70 | } |
| 71 | |
| 72 | char dirname[1024]; |
| 73 | |
| 74 | private: |
| 75 | bool init(const char* tmp_dir) { |
| 76 | snprintf(dirname, sizeof(dirname), "%s/TemporaryDir-XXXXXX", tmp_dir); |
| 77 | return (mkdtemp(dirname) != NULL); |
| 78 | } |
| 79 | |
| 80 | }; |