github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/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 // For compatibility with windows, the fifo must not exist already. 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 { 21 return nil, fmt.Errorf("error creating fifo %v: %v", path, err) 22 } 23 24 return func() (io.ReadCloser, error) { 25 return OpenReader(path) 26 }, nil 27 } 28 29 func OpenReader(path string) (io.ReadCloser, error) { 30 return os.OpenFile(path, unix.O_RDONLY, os.ModeNamedPipe) 31 } 32 33 // OpenWriter opens a fifo file for writer, assuming it already exists, returns io.WriteCloser 34 func OpenWriter(path string) (io.WriteCloser, error) { 35 return os.OpenFile(path, unix.O_WRONLY, os.ModeNamedPipe) 36 } 37 38 // Remove a fifo that already exists at a given path 39 func Remove(path string) error { 40 return os.Remove(path) 41 } 42 43 func IsClosedErr(err error) bool { 44 err2, ok := err.(*os.PathError) 45 if ok { 46 return err2.Err == os.ErrClosed 47 } 48 return false 49 } 50 51 func mkfifo(path string, mode uint32) (err error) { 52 return unix.Mkfifo(path, mode) 53 }