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 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 17 | #include "base/test_utils.h" |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 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 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 29 | #include <string> |
| 30 | |
| 31 | static std::string GetSystemTempDir() { |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 32 | #if defined(__ANDROID__) |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 33 | return "/data/local/tmp"; |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 34 | #elif defined(_WIN32) |
| 35 | char wd[MAX_PATH] = {}; |
| 36 | _getcwd(wd, sizeof(wd)); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 37 | return wd; |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 38 | #else |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 39 | return "/tmp"; |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 40 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 43 | TemporaryFile::TemporaryFile() { |
| 44 | init(GetSystemTempDir()); |
| 45 | } |
| 46 | |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 47 | TemporaryFile::~TemporaryFile() { |
| 48 | close(fd); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 49 | unlink(path); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 52 | void TemporaryFile::init(const std::string& tmp_dir) { |
| 53 | snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir.c_str()); |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 54 | #if !defined(_WIN32) |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 55 | fd = mkstemp(path); |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 56 | #else |
| 57 | // Windows doesn't have mkstemp, and tmpfile creates the file in the root |
| 58 | // directory, requiring root (?!) permissions. We have to settle for mktemp. |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 59 | if (mktemp(path) == nullptr) { |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 60 | abort(); |
| 61 | } |
| 62 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 63 | fd = open(path, O_RDWR | O_NOINHERIT | O_CREAT, _S_IREAD | _S_IWRITE); |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 64 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 65 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame^] | 66 | |
| 67 | #if !defined(_WIN32) |
| 68 | TemporaryDir::TemporaryDir() { |
| 69 | init(GetSystemTempDir()); |
| 70 | } |
| 71 | |
| 72 | TemporaryDir::~TemporaryDir() { |
| 73 | rmdir(path); |
| 74 | } |
| 75 | |
| 76 | bool TemporaryDir::init(const std::string& tmp_dir) { |
| 77 | snprintf(path, sizeof(path), "%s/TemporaryDir-XXXXXX", tmp_dir.c_str()); |
| 78 | return (mkdtemp(path) != nullptr); |
| 79 | } |
| 80 | #endif |