github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/java/save_archive_to_tmp.go (about) 1 package java 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "path/filepath" 8 9 "github.com/anchore/syft/internal/log" 10 ) 11 12 func saveArchiveToTmp(archiveVirtualPath string, reader io.Reader) (string, string, func(), error) { 13 name := filepath.Base(archiveVirtualPath) 14 tempDir, err := os.MkdirTemp("", "syft-archive-contents-") 15 if err != nil { 16 return "", "", func() {}, fmt.Errorf("unable to create tempdir for archive processing: %w", err) 17 } 18 19 cleanupFn := func() { 20 err = os.RemoveAll(tempDir) 21 if err != nil { 22 log.Errorf("unable to cleanup archive tempdir: %+v", err) 23 } 24 } 25 26 archivePath := filepath.Join(tempDir, "archive-"+name) 27 contentDir := filepath.Join(tempDir, "contents") 28 29 err = os.Mkdir(contentDir, 0755) 30 if err != nil { 31 return contentDir, "", cleanupFn, fmt.Errorf("unable to create processing tempdir: %w", err) 32 } 33 34 archiveFile, err := os.Create(archivePath) 35 if err != nil { 36 return contentDir, "", cleanupFn, fmt.Errorf("unable to create archive: %w", err) 37 } 38 defer archiveFile.Close() 39 40 _, err = io.Copy(archiveFile, reader) 41 if err != nil { 42 return contentDir, archivePath, cleanupFn, fmt.Errorf("unable to copy archive: %w", err) 43 } 44 45 return contentDir, archivePath, cleanupFn, nil 46 }