fifo: Require that fifos do not exist for create

Although this operation is safe on linux, it is not safe on Windows when
using the named pipe interface. To provide a ~reasonable common api
abstraction, here we switch to returning File exists errors on the unix
api.
This commit is contained in:
Danielle Lancashire 2019-06-28 13:35:41 +02:00
parent 0ff27cfc0f
commit 634ada671e
No known key found for this signature in database
GPG Key ID: 8D65584EF3DDF91B
1 changed files with 3 additions and 3 deletions

View File

@ -11,18 +11,18 @@ import (
)
// CreateAndRead creates a fifo at the given path, and returns an open function for reading.
// The fifo must not exist already, or that it's already a fifo file
// For compatibility with windows, the fifo must not exist already.
//
// It returns a reader open function that may block until a writer opens
// so it's advised to run it in a goroutine different from reader goroutine
func CreateAndRead(path string) (func() (io.ReadCloser, error), error) {
// create first
if err := mkfifo(path, 0600); err != nil && !os.IsExist(err) {
if err := mkfifo(path, 0600); err != nil {
return nil, fmt.Errorf("error creating fifo %v: %v", path, err)
}
return func() (io.ReadCloser, error) {
return os.OpenFile(path, unix.O_RDONLY, os.ModeNamedPipe)
return OpenReader(path)
}, nil
}