Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "test_utils.h" |
| 18 | |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame^] | 19 | #include <fcntl.h> |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame^] | 22 | #include <sys/stat.h> |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame^] | 25 | #if defined(_WIN32) |
| 26 | #include <windows.h> |
| 27 | #endif |
| 28 | |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 29 | TemporaryFile::TemporaryFile() { |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame^] | 30 | #if defined(__ANDROID__) |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 31 | init("/data/local/tmp"); |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame^] | 32 | #elif defined(_WIN32) |
| 33 | char wd[MAX_PATH] = {}; |
| 34 | _getcwd(wd, sizeof(wd)); |
| 35 | init(wd); |
| 36 | #else |
| 37 | init("/tmp"); |
| 38 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | TemporaryFile::~TemporaryFile() { |
| 42 | close(fd); |
| 43 | unlink(filename); |
| 44 | } |
| 45 | |
| 46 | void TemporaryFile::init(const char* tmp_dir) { |
| 47 | snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir); |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame^] | 48 | #if !defined(_WIN32) |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 49 | fd = mkstemp(filename); |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame^] | 50 | #else |
| 51 | // Windows doesn't have mkstemp, and tmpfile creates the file in the root |
| 52 | // directory, requiring root (?!) permissions. We have to settle for mktemp. |
| 53 | if (mktemp(filename) == nullptr) { |
| 54 | abort(); |
| 55 | } |
| 56 | |
| 57 | fd = open(filename, O_RDWR | O_NOINHERIT | O_CREAT, _S_IREAD | _S_IWRITE); |
| 58 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 59 | } |