Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // 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 Clayton | 2821c29 | 2023-08-12 11:10:30 +0000 | [diff] [blame] | 15 | // GEN_BUILD:CONDITION(is_linux || is_mac) |
| 16 | |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 17 | #include "src/tint/utils/file/tmpfile.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 18 | |
| 19 | #include <unistd.h> |
| 20 | #include <limits> |
| 21 | |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 22 | #include "src/tint/utils/ice/ice.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 23 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 24 | namespace tint { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
| 28 | std::string TmpFilePath(std::string ext) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 29 | char const* dir = getenv("TMPDIR"); |
| 30 | if (dir == nullptr) { |
| 31 | dir = "/tmp"; |
| 32 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 33 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 34 | // 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 Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 39 | TINT_ASSERT(ext.length() <= static_cast<size_t>(std::numeric_limits<int>::max())); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 51 | TmpFile::TmpFile(std::string extension) : path_(TmpFilePath(std::move(extension))) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 52 | |
| 53 | TmpFile::~TmpFile() { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 54 | if (!path_.empty()) { |
| 55 | remove(path_.c_str()); |
| 56 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool TmpFile::Append(const void* data, size_t size) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 60 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 66 | } |
| 67 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 68 | } // namespace tint |