blob: 640ca738da108f07e7b71fb2ed98b54120959e88 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Ben Clayton2821c292023-08-12 11:10:30 +000015// GEN_BUILD:CONDITION(is_linux || is_mac)
16
dan sinclair22b4dd22023-07-21 00:40:07 +000017#include "src/tint/utils/file/tmpfile.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000018
19#include <unistd.h>
20#include <limits>
21
Ben Claytonf848af22023-07-28 16:37:32 +000022#include "src/tint/utils/ice/ice.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000023
dan sinclairbae54e72023-07-28 15:01:54 +000024namespace tint {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000025
26namespace {
27
28std::string TmpFilePath(std::string ext) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000029 char const* dir = getenv("TMPDIR");
30 if (dir == nullptr) {
31 dir = "/tmp";
32 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000033
dan sinclair41e4d9a2022-05-01 14:40:55 +000034 // mkstemps requires an `int` for the file extension name but STL represents
35 // size_t. Pre-C++20 there the behavior for unsigned-to-signed conversion
36 // (when the source value exceeds the representable range) is implementation
37 // defined. While such a large file extension is unlikely in practice, we
38 // enforce this here at runtime.
Ben Claytonf848af22023-07-28 16:37:32 +000039 TINT_ASSERT(ext.length() <= static_cast<size_t>(std::numeric_limits<int>::max()));
dan sinclair41e4d9a2022-05-01 14:40:55 +000040 std::string name = std::string(dir) + "/tint_XXXXXX" + ext;
41 int file = mkstemps(&name[0], static_cast<int>(ext.length()));
42 if (file != -1) {
43 close(file);
44 return name;
45 }
46 return "";
Ryan Harrisondbc13af2022-02-21 15:19:07 +000047}
48
49} // namespace
50
dan sinclair41e4d9a2022-05-01 14:40:55 +000051TmpFile::TmpFile(std::string extension) : path_(TmpFilePath(std::move(extension))) {}
Ryan Harrisondbc13af2022-02-21 15:19:07 +000052
53TmpFile::~TmpFile() {
dan sinclair41e4d9a2022-05-01 14:40:55 +000054 if (!path_.empty()) {
55 remove(path_.c_str());
56 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000057}
58
59bool TmpFile::Append(const void* data, size_t size) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000060 if (auto* file = fopen(path_.c_str(), "ab")) {
61 fwrite(data, size, 1, file);
62 fclose(file);
63 return true;
64 }
65 return false;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000066}
67
dan sinclairbae54e72023-07-28 15:01:54 +000068} // namespace tint