2018-09-17 17:15:48 +00:00
|
|
|
package fifo
|
|
|
|
|
|
|
|
import (
|
2019-06-28 11:49:07 +00:00
|
|
|
"fmt"
|
2018-09-17 17:15:48 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
winio "github.com/Microsoft/go-winio"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PipeBufferSize is the size of the input and output buffers for the windows
|
|
|
|
// named pipe
|
|
|
|
const PipeBufferSize = int32(^uint16(0))
|
|
|
|
|
|
|
|
type winFIFO struct {
|
|
|
|
listener net.Listener
|
|
|
|
conn net.Conn
|
|
|
|
connLock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2019-07-02 11:12:54 +00:00
|
|
|
func (f *winFIFO) ensureConn() (net.Conn, error) {
|
2018-09-17 17:15:48 +00:00
|
|
|
f.connLock.Lock()
|
2019-07-02 11:12:54 +00:00
|
|
|
defer f.connLock.Unlock()
|
2018-09-17 17:15:48 +00:00
|
|
|
if f.conn == nil {
|
|
|
|
c, err := f.listener.Accept()
|
|
|
|
if err != nil {
|
2019-07-02 11:12:54 +00:00
|
|
|
return nil, err
|
2018-09-17 17:15:48 +00:00
|
|
|
}
|
|
|
|
f.conn = c
|
|
|
|
}
|
2019-07-02 11:12:54 +00:00
|
|
|
|
|
|
|
return f.conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *winFIFO) Read(p []byte) (n int, err error) {
|
|
|
|
conn, err := f.ensureConn()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-09-17 17:15:48 +00:00
|
|
|
|
|
|
|
// If the connection is closed then we need to close the listener
|
|
|
|
// to emulate unix fifo behavior
|
2019-07-02 11:12:54 +00:00
|
|
|
n, err = conn.Read(p)
|
2018-09-17 17:15:48 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
f.listener.Close()
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *winFIFO) Write(p []byte) (n int, err error) {
|
2019-07-02 11:12:54 +00:00
|
|
|
conn, err := f.ensureConn()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2018-09-17 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the connection is closed then we need to close the listener
|
|
|
|
// to emulate unix fifo behavior
|
2019-07-02 11:12:54 +00:00
|
|
|
n, err = conn.Write(p)
|
2018-09-17 17:15:48 +00:00
|
|
|
if err == io.EOF {
|
2019-07-02 11:12:54 +00:00
|
|
|
conn.Close()
|
2018-09-17 17:15:48 +00:00
|
|
|
f.listener.Close()
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *winFIFO) Close() error {
|
2019-06-28 14:27:10 +00:00
|
|
|
f.connLock.Lock()
|
|
|
|
if f.conn != nil {
|
|
|
|
f.conn.Close()
|
|
|
|
}
|
|
|
|
f.connLock.Unlock()
|
2018-09-17 17:15:48 +00:00
|
|
|
return f.listener.Close()
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:56:43 +00:00
|
|
|
// CreateAndRead creates a fifo at the given path and returns an io.ReadCloser open for it.
|
2019-03-31 16:50:06 +00:00
|
|
|
// The fifo must not already exist
|
2019-04-01 19:56:43 +00:00
|
|
|
func CreateAndRead(path string) (func() (io.ReadCloser, error), error) {
|
2018-09-17 17:15:48 +00:00
|
|
|
l, err := winio.ListenPipe(path, &winio.PipeConfig{
|
|
|
|
InputBufferSize: PipeBufferSize,
|
|
|
|
OutputBufferSize: PipeBufferSize,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-06-28 11:49:07 +00:00
|
|
|
return nil, fmt.Errorf("failed to create fifo: %v", err)
|
2018-09-17 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 12:56:24 +00:00
|
|
|
return func() (io.ReadCloser, error) {
|
2019-03-31 16:50:06 +00:00
|
|
|
return &winFIFO{
|
|
|
|
listener: l,
|
|
|
|
}, nil
|
2019-06-18 12:56:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenReader(path string) (io.ReadCloser, error) {
|
2019-06-20 17:13:05 +00:00
|
|
|
l, err := winio.ListenOnlyPipe(path, nil)
|
2019-06-18 12:56:24 +00:00
|
|
|
if err != nil {
|
2019-06-28 11:49:07 +00:00
|
|
|
return nil, fmt.Errorf("failed to open fifo listener: %v", err)
|
2019-03-31 16:50:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 17:13:05 +00:00
|
|
|
return &winFIFO{listener: l}, nil
|
2018-09-17 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 16:50:06 +00:00
|
|
|
// OpenWriter opens a fifo that already exists and returns an io.WriteCloser for it
|
2019-04-01 19:56:43 +00:00
|
|
|
func OpenWriter(path string) (io.WriteCloser, error) {
|
2018-09-17 17:15:48 +00:00
|
|
|
return winio.DialPipe(path, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a fifo that already exists at a given path
|
|
|
|
func Remove(path string) error {
|
|
|
|
dur := 500 * time.Millisecond
|
|
|
|
conn, err := winio.DialPipe(path, &dur)
|
|
|
|
if err == nil {
|
|
|
|
return conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsClosedErr(err error) bool {
|
|
|
|
return err == winio.ErrFileClosed
|
|
|
|
}
|