|
| 1 | +// Package spool provides utilities for managing "spool files". |
| 2 | +// |
| 3 | +// Files returned by this package can be counted on to be removed when all open descriptors are closed. |
| 4 | +package spool |
| 5 | + |
| 6 | +import ( |
| 7 | + "cmp" |
| 8 | + "io/fs" |
| 9 | + "math/rand/v2" |
| 10 | + "os" |
| 11 | + "runtime" |
| 12 | + "strconv" |
| 13 | +) |
| 14 | + |
| 15 | +// Root is the location all the spool files go into. |
| 16 | +// |
| 17 | +// This is initialized in os-specific files. |
| 18 | +var root *os.Root |
| 19 | + |
| 20 | +// Mkname generates a unique file name based on the provided prefix. |
| 21 | +func mkname(prefix string) string { |
| 22 | + return cmp.Or(prefix, "tmp") + "." + |
| 23 | + strconv.FormatUint(uint64(rand.Uint32()), 10) |
| 24 | +} |
| 25 | + |
| 26 | +// OpenFile opens a temporary file with the provided prefix. |
| 27 | +// |
| 28 | +// If "prefix" is not provided, "tmp" will be used. |
| 29 | +// Returned files cannot be opened by path. Callers should use [Reopen]. |
| 30 | +func OpenFile(prefix string, flag int, perm fs.FileMode) (*os.File, error) { |
| 31 | + name := osAdjustName(mkname(prefix)) |
| 32 | + flag = osAdjustFlag(flag) |
| 33 | + f, err := root.OpenFile(name, flag, perm) |
| 34 | + if f != nil { |
| 35 | + osAddCleanup(f) |
| 36 | + } |
| 37 | + return f, err |
| 38 | +} |
| 39 | + |
| 40 | +// Create returns an [*os.File] that cannot be opened by path and will be |
| 41 | +// removed when closed. |
| 42 | +func Create() (*os.File, error) { |
| 43 | + return OpenFile("", os.O_CREATE|os.O_RDWR, 0o600) |
| 44 | +} |
| 45 | + |
| 46 | +// Mkdir creates a directory with the provided prefix. |
| 47 | +// |
| 48 | +// The directory will have its contents removed when the returned [*os.Root] is |
| 49 | +// garbage collected. |
| 50 | +func Mkdir(prefix string, perm fs.FileMode) (*os.Root, error) { |
| 51 | + name := mkname(prefix) |
| 52 | + if err := root.Mkdir(name, perm); err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + r, err := root.OpenRoot(name) |
| 56 | + if err == nil { // NB If successful |
| 57 | + runtime.AddCleanup(r, func(name string) { |
| 58 | + root.RemoveAll(name) |
| 59 | + }, name) |
| 60 | + } |
| 61 | + return r, err |
| 62 | +} |
| 63 | + |
| 64 | +/* |
| 65 | +This package needs a few parts implemented in OS-specific ways. |
| 66 | +Below is a quick rundown of them, along with a ready-to-use documentation comment. |
| 67 | +
|
| 68 | +# Exported |
| 69 | +
|
| 70 | +The documentation for these implementations should add additional paragraphs explaining the OS-specific parts starting "The ${OS} implementation [...]". |
| 71 | +
|
| 72 | +See the Linux implementations in os_linux.go for an example. |
| 73 | +
|
| 74 | + // Reopen provides or emulates re-opening a file and obtaining an independent file description. |
| 75 | + func Reopen(f *os.File, flag int) (*os.File, error) |
| 76 | +
|
| 77 | +The [Reopen] API is not possible with dup(2), which returns another file descriptor to the same file description. |
| 78 | +An implementation using dup(2) would not provide independent offsets. |
| 79 | +
|
| 80 | +# Unexported |
| 81 | +
|
| 82 | + osAdjustName(string) string |
| 83 | +
|
| 84 | +AdjustName should modify the passed file name as needed and return the result. |
| 85 | +
|
| 86 | + osAdjustFlag(int) int |
| 87 | +
|
| 88 | +AdjustFlag should modify the passed flags as needed and return the result. |
| 89 | +
|
| 90 | + osAddCleanup(*os.File) |
| 91 | +
|
| 92 | +AddCleanup should use [runtime.AddCleanup] to attach any needed cleanup functions to the passed [*os.File]. |
| 93 | +An implementation will not be called with a nil pointer. |
| 94 | +*/ |
0 commit comments