• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H19-May-2021-

examples/tmpfile-cwd/H19-May-2021-

.golangci.ymlH A D19-May-2021668

LICENSEH A D19-May-20211 KiB

LICENSE.golangH A D19-May-20211.4 KiB

README.mdH A D19-May-20211.8 KiB

doc.goH A D19-May-2021501

doc_test.goH A D19-May-2021494

go.modH A D19-May-2021106

go.sumH A D19-May-2021207

tmpfile.goH A D19-May-2021730

tmpfile_notexists_test.goH A D19-May-2021271

tmpfile_notexists_windows_test.goH A D19-May-2021454

tmpfile_test.goH A D19-May-20215 KiB

tmpfile_windows.goH A D19-May-20212.4 KiB

README.md

1[![Documentation][godoc.badge]][godoc]
2[![Test Status][workflow.tests.badge]][workflow.tests]
3
4# Cross Platform Temporary Files
5
6This library attempts to bridge the gap between the what is provided in
7[ioutil.TempFile][ioutil.tempfile] and the best practice of ensuring temporary
8files are ***always*** deleted when the application exits.
9
10The normal way to do this on a POSIX system is to use the behavior of
11[unlink][posix.unlink] to immediately remove the directory entry for the
12temporary file. The OS then ensures that when all open file handles on the file
13are close that the file resources are removed. Unfortunately, despite Go having
14[os.Remove][os.remove] this does not work on Windows because on Windows it is
15necessary to open the files with special flags
16([FILE_SHARE_DELETE][windows.flags.share],
17[FILE_FLAG_DELETE_ON_CLOSE][windows.flags.on_close]) to enable removing a file
18that is open (and ioutil does not do this).
19
20Example usage:
21
22```go
23package main
24
25import "github.com/calebcase/tmpfile"
26
27func main() {
28	f, err := tmpfile.New("", "example-*")
29	if err != nil {
30		panic(err)
31	}
32	defer f.Close()
33
34	f.WriteString("Example Data")
35}
36```
37
38---
39
40[godoc.badge]: https://godoc.org/github.com/calebcase/tmpfile?status.svg
41[godoc]: https://godoc.org/github.com/calebcase/tmpfile
42[ioutil.tempfile]: https://golang.org/pkg/io/ioutil/#TempFile
43[os.remove]: https://golang.org/pkg/os/#Remove
44[posix.unlink]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html
45[windows.flags.on_close]: https://github.com/golang/sys/blob/master/windows/types_windows.go#L108
46[windows.flags.share]: https://github.com/golang/sys/blob/master/windows/types_windows.go#L71
47[workflow.tests.badge]: https://github.com/calebcase/tmpfile/workflows/tests/badge.svg
48[workflow.tests]: https://github.com/calebcase/tmpfile/actions?query=workflow%3Atests
49