createTempFile creates a temporary file with the given filename and writes the given data to it.
(filename string, rawData []byte)
| 104 | |
| 105 | // createTempFile creates a temporary file with the given filename and writes the given data to it. |
| 106 | func createTempFile(filename string, rawData []byte) (string, error) { |
| 107 | // Create a temporary directory with a random name to avoid collisions |
| 108 | tempDir, err := os.MkdirTemp("", "chainloop-inflight-dir-*") |
| 109 | if err != nil { |
| 110 | return "", fmt.Errorf("creating temporary directory: %w", err) |
| 111 | } |
| 112 | |
| 113 | // Create a temporary file with the same name as the original file |
| 114 | tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(filename))) |
| 115 | if err != nil { |
| 116 | return "", fmt.Errorf("creating temporary file: %w", err) |
| 117 | } |
| 118 | // Close the file when we are done |
| 119 | defer tempFile.Close() |
| 120 | |
| 121 | // Write the data to the temporary file |
| 122 | if _, err := tempFile.Write(rawData); err != nil { |
| 123 | return "", fmt.Errorf("writing to temporary file: %w", err) |
| 124 | } |
| 125 | |
| 126 | return tempFile.Name(), nil |
| 127 | } |
no test coverage detected