blob: c7b7178efd51c585c24233e2253971ede33a199b [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2021 The Dawn & Tint Authors
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ryan Harrisondbc13af2022-02-21 15:19:07 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
Ben Clayton5c69ff42023-11-23 13:32:36 +000028// GEN_BUILD:CONDITION(tint_build_is_linux || tint_build_is_mac)
Ben Clayton2821c292023-08-12 11:10:30 +000029
dan sinclair22b4dd22023-07-21 00:40:07 +000030#include "src/tint/utils/file/tmpfile.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000031
32#include <unistd.h>
33#include <limits>
34
Ben Claytonf848af22023-07-28 16:37:32 +000035#include "src/tint/utils/ice/ice.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000036
dan sinclairbae54e72023-07-28 15:01:54 +000037namespace tint {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000038
39namespace {
40
41std::string TmpFilePath(std::string ext) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000042 char const* dir = getenv("TMPDIR");
43 if (dir == nullptr) {
44 dir = "/tmp";
45 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000046
dan sinclair41e4d9a2022-05-01 14:40:55 +000047 // mkstemps requires an `int` for the file extension name but STL represents
48 // size_t. Pre-C++20 there the behavior for unsigned-to-signed conversion
49 // (when the source value exceeds the representable range) is implementation
50 // defined. While such a large file extension is unlikely in practice, we
51 // enforce this here at runtime.
Ben Claytonf848af22023-07-28 16:37:32 +000052 TINT_ASSERT(ext.length() <= static_cast<size_t>(std::numeric_limits<int>::max()));
dan sinclair41e4d9a2022-05-01 14:40:55 +000053 std::string name = std::string(dir) + "/tint_XXXXXX" + ext;
54 int file = mkstemps(&name[0], static_cast<int>(ext.length()));
55 if (file != -1) {
56 close(file);
57 return name;
58 }
59 return "";
Ryan Harrisondbc13af2022-02-21 15:19:07 +000060}
61
62} // namespace
63
dan sinclair41e4d9a2022-05-01 14:40:55 +000064TmpFile::TmpFile(std::string extension) : path_(TmpFilePath(std::move(extension))) {}
Ryan Harrisondbc13af2022-02-21 15:19:07 +000065
66TmpFile::~TmpFile() {
dan sinclair41e4d9a2022-05-01 14:40:55 +000067 if (!path_.empty()) {
68 remove(path_.c_str());
69 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000070}
71
72bool TmpFile::Append(const void* data, size_t size) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000073 if (auto* file = fopen(path_.c_str(), "ab")) {
74 fwrite(data, size, 1, file);
75 fclose(file);
76 return true;
77 }
78 return false;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000079}
80
dan sinclairbae54e72023-07-28 15:01:54 +000081} // namespace tint