MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / createTempFile

Function createTempFile

pkg/resourceloader/resourceloader.go:106–127  ·  view source on GitHub ↗

createTempFile creates a temporary file with the given filename and writes the given data to it.

(filename string, rawData []byte)

Source from the content-addressed store, hash-verified

104
105// createTempFile creates a temporary file with the given filename and writes the given data to it.
106func 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}

Callers 1

GetPathForResourceFunction · 0.85

Calls 4

CreateMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.45
NameMethod · 0.45

Tested by

no test coverage detected