github.com/quay/claircore@v1.5.28/libvuln/jsonblob/diskbuf_windows.go (about)

     1  package jsonblob
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     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  func diskBuf(_ context.Context) (*os.File, error) {
    19  	// Copied out of golang.org/x/sys/windows
    20  	const FILE_FLAG_DELETE_ON_CLOSE = 0x04000000
    21  	dir := os.TempDir()
    22  	for {
    23  		fn := fmt.Sprintf("jsonblob.%d.json", fastrand())
    24  		f, err := os.OpenFile(filepath.Join(dir, fn), os.O_RDWR|os.O_CREATE|os.O_EXCL|FILE_FLAG_DELETE_ON_CLOSE, 0600)
    25  		if errors.Is(err, os.ErrExist) {
    26  			continue
    27  		}
    28  		return f, err
    29  	}
    30  }