github.com/smithx10/nomad@v0.9.1-rc1/client/lib/fifo/fifo_unix.go (about) 1 // +build !windows 2 3 package fifo 4 5 import ( 6 "fmt" 7 "io" 8 "os" 9 10 "golang.org/x/sys/unix" 11 ) 12 13 // CreateAndRead creates a fifo at the given path, and returns an open function for reading. 14 // The fifo must not exist already, or that it's already a fifo file 15 // 16 // It returns a reader open function that may block until a writer opens 17 // so it's advised to run it in a goroutine different from reader goroutine 18 func CreateAndRead(path string) (func() (io.ReadCloser, error), error) { 19 // create first 20 if err := mkfifo(path, 0600); err != nil && !os.IsExist(err) { 21 return nil, fmt.Errorf("error creating fifo %v: %v", path, err) 22 } 23 24 openFn := func() (io.ReadCloser, error) { 25 return os.OpenFile(path, unix.O_RDONLY, os.ModeNamedPipe) 26 } 27 28 return openFn, nil 29 } 30 31 // OpenWriter opens a fifo file for writer, assuming it already exists, returns io.WriteCloser 32 func OpenWriter(path string) (io.WriteCloser, error) { 33 return os.OpenFile(path, unix.O_WRONLY, os.ModeNamedPipe) 34 } 35 36 // Remove a fifo that already exists at a given path 37 func Remove(path string) error { 38 return os.Remove(path) 39 } 40 41 func IsClosedErr(err error) bool { 42 err2, ok := err.(*os.PathError) 43 if ok { 44 return err2.Err == os.ErrClosed 45 } 46 return false 47 } 48 49 func mkfifo(path string, mode uint32) (err error) { 50 return unix.Mkfifo(path, mode) 51 }