blob: dceb8b788890215939a1f69cfb9c01187252cf65 [file] [log] [blame]
Dan Albert58310b42015-03-13 23:06:01 -07001/*
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ée47d67c92015-05-06 16:26:00 -040017#include "base/test_utils.h"
Dan Albert58310b42015-03-13 23:06:01 -070018
Dan Albert0c4b3a32015-04-29 11:32:23 -070019#include <fcntl.h>
Dan Albert58310b42015-03-13 23:06:01 -070020#include <stdio.h>
21#include <stdlib.h>
Dan Albert0c4b3a32015-04-29 11:32:23 -070022#include <sys/stat.h>
Dan Albert58310b42015-03-13 23:06:01 -070023#include <unistd.h>
24
Dan Albert0c4b3a32015-04-29 11:32:23 -070025#if defined(_WIN32)
26#include <windows.h>
27#endif
28
Alex Vallée47d67c92015-05-06 16:26:00 -040029#include <string>
30
31static std::string GetSystemTempDir() {
Dan Albert0c4b3a32015-04-29 11:32:23 -070032#if defined(__ANDROID__)
Alex Vallée47d67c92015-05-06 16:26:00 -040033 return "/data/local/tmp";
Dan Albert0c4b3a32015-04-29 11:32:23 -070034#elif defined(_WIN32)
35 char wd[MAX_PATH] = {};
36 _getcwd(wd, sizeof(wd));
Alex Vallée47d67c92015-05-06 16:26:00 -040037 return wd;
Dan Albert0c4b3a32015-04-29 11:32:23 -070038#else
Alex Vallée47d67c92015-05-06 16:26:00 -040039 return "/tmp";
Dan Albert0c4b3a32015-04-29 11:32:23 -070040#endif
Dan Albert58310b42015-03-13 23:06:01 -070041}
42
Alex Vallée47d67c92015-05-06 16:26:00 -040043TemporaryFile::TemporaryFile() {
44 init(GetSystemTempDir());
45}
46
Dan Albert58310b42015-03-13 23:06:01 -070047TemporaryFile::~TemporaryFile() {
48 close(fd);
Alex Vallée47d67c92015-05-06 16:26:00 -040049 unlink(path);
Dan Albert58310b42015-03-13 23:06:01 -070050}
51
Alex Vallée47d67c92015-05-06 16:26:00 -040052void TemporaryFile::init(const std::string& tmp_dir) {
53 snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir.c_str());
Dan Albert0c4b3a32015-04-29 11:32:23 -070054#if !defined(_WIN32)
Alex Vallée47d67c92015-05-06 16:26:00 -040055 fd = mkstemp(path);
Dan Albert0c4b3a32015-04-29 11:32:23 -070056#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ée47d67c92015-05-06 16:26:00 -040059 if (mktemp(path) == nullptr) {
Dan Albert0c4b3a32015-04-29 11:32:23 -070060 abort();
61 }
62
Alex Vallée47d67c92015-05-06 16:26:00 -040063 fd = open(path, O_RDWR | O_NOINHERIT | O_CREAT, _S_IREAD | _S_IWRITE);
Dan Albert0c4b3a32015-04-29 11:32:23 -070064#endif
Dan Albert58310b42015-03-13 23:06:01 -070065}
Alex Vallée47d67c92015-05-06 16:26:00 -040066
67#if !defined(_WIN32)
68TemporaryDir::TemporaryDir() {
69 init(GetSystemTempDir());
70}
71
72TemporaryDir::~TemporaryDir() {
73 rmdir(path);
74}
75
76bool 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