github.com/quay/claircore@v1.5.28/libindex/tempfile_windows.go (about) 1 package libindex 2 3 import ( 4 "errors" 5 "fmt" 6 "io/fs" 7 "os" 8 "path/filepath" 9 _ "unsafe" // linker tricks 10 ) 11 12 // This is a very rough port of src/os/tempfile.go that adds the magic 13 // autodelete flag. 14 15 //go:linkname fastrand runtime.fastrand 16 func fastrand() uint32 17 18 type tempFile struct { 19 *os.File 20 } 21 22 func openTemp(dir string) (*tempFile, error) { 23 // Copied out of golang.org/x/sys/windows 24 const FILE_FLAG_DELETE_ON_CLOSE = 0x04000000 25 for { 26 fn := fmt.Sprintf("fetch.%d", fastrand()) 27 f, err := os.OpenFile(filepath.Join(dir, fn), os.O_WRONLY|FILE_FLAG_DELETE_ON_CLOSE, 0644) 28 switch { 29 case errors.Is(err, nil): 30 return &tempFile{ 31 File: f, 32 }, nil 33 case errors.Is(err, fs.ErrExist): 34 // Continue 35 default: 36 return nil, err 37 } 38 } 39 } 40 41 func (t *tempFile) Reopen() (*os.File, error) { 42 return os.OpenFile(t.Name(), os.O_RDONLY, 0644) 43 }