github.com/hanwen/go-fuse@v1.0.0/splice/pair.go (about)

     1  // Copyright 2016 the Go-FUSE Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package splice
     6  
     7  import (
     8  	"fmt"
     9  	"syscall"
    10  )
    11  
    12  type Pair struct {
    13  	r, w int
    14  	size int
    15  }
    16  
    17  func (p *Pair) MaxGrow() {
    18  	for p.Grow(2*p.size) == nil {
    19  	}
    20  }
    21  
    22  func (p *Pair) Grow(n int) error {
    23  	if n <= p.size {
    24  		return nil
    25  	}
    26  	if !resizable {
    27  		return fmt.Errorf("splice: want %d bytes, but not resizable", n)
    28  	}
    29  	if n > maxPipeSize {
    30  		return fmt.Errorf("splice: want %d bytes, max pipe size %d", n, maxPipeSize)
    31  	}
    32  
    33  	newsize, errNo := fcntl(uintptr(p.r), F_SETPIPE_SZ, n)
    34  	if errNo != 0 {
    35  		return fmt.Errorf("splice: fcntl returned %v", errNo)
    36  	}
    37  	p.size = newsize
    38  	return nil
    39  }
    40  
    41  func (p *Pair) Cap() int {
    42  	return p.size
    43  }
    44  
    45  func (p *Pair) Close() error {
    46  	err1 := syscall.Close(p.r)
    47  	err2 := syscall.Close(p.w)
    48  	if err1 != nil {
    49  		return err1
    50  	}
    51  	return err2
    52  }
    53  
    54  func (p *Pair) Read(d []byte) (n int, err error) {
    55  	return syscall.Read(p.r, d)
    56  }
    57  
    58  func (p *Pair) Write(d []byte) (n int, err error) {
    59  	return syscall.Write(p.w, d)
    60  }
    61  
    62  func (p *Pair) ReadFd() uintptr {
    63  	return uintptr(p.r)
    64  }
    65  
    66  func (p *Pair) WriteFd() uintptr {
    67  	return uintptr(p.w)
    68  }