Initial commit

Proof-of-concept implementation. Bugs will occur.
This commit is contained in:
2026-02-12 01:18:46 +03:00
commit 13ac06c14b
553 changed files with 253003 additions and 0 deletions

72
vendor/github.com/hanwen/go-fuse/v2/splice/copy.go generated vendored Normal file
View File

@@ -0,0 +1,72 @@
//go:build linux
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package splice
import (
"io"
"os"
)
func SpliceCopy(dst *os.File, src *os.File, p *Pair) (int64, error) {
total := int64(0)
for {
n, err := p.LoadFrom(src.Fd(), p.size)
if err != nil {
return total, err
}
if n == 0 {
break
}
m, err := p.WriteTo(dst.Fd(), n)
total += int64(m)
if err != nil {
return total, err
}
if m < n {
return total, err
}
if int(n) < p.size {
break
}
}
return total, nil
}
// Argument ordering follows io.Copy.
func CopyFile(dstName string, srcName string, mode int) error {
src, err := os.Open(srcName)
if err != nil {
return err
}
defer src.Close()
dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(mode))
if err != nil {
return err
}
defer dst.Close()
return CopyFds(dst, src)
}
func CopyFds(dst *os.File, src *os.File) (err error) {
p, err := splicePool.get()
if p != nil {
p.Grow(256 * 1024)
_, err := SpliceCopy(dst, src, p)
splicePool.done(p)
return err
} else {
_, err = io.Copy(dst, src)
}
if err == io.EOF {
err = nil
}
return err
}

70
vendor/github.com/hanwen/go-fuse/v2/splice/pair.go generated vendored Normal file
View File

@@ -0,0 +1,70 @@
//go:build linux
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package splice
import (
"fmt"
"syscall"
)
type Pair struct {
r, w int
size int
}
func (p *Pair) MaxGrow() {
for p.Grow(2*p.size) == nil {
}
}
func (p *Pair) Grow(n int) error {
if n <= p.size {
return nil
}
if !resizable {
return fmt.Errorf("splice: want %d bytes, but not resizable", n)
}
if n > maxPipeSize {
return fmt.Errorf("splice: want %d bytes, max pipe size %d", n, maxPipeSize)
}
newsize, errNo := fcntl(uintptr(p.r), F_SETPIPE_SZ, n)
if errNo != 0 {
return fmt.Errorf("splice: fcntl returned %v", errNo)
}
p.size = newsize
return nil
}
func (p *Pair) Cap() int {
return p.size
}
func (p *Pair) Close() error {
err1 := syscall.Close(p.r)
err2 := syscall.Close(p.w)
if err1 != nil {
return err1
}
return err2
}
func (p *Pair) Read(d []byte) (n int, err error) {
return syscall.Read(p.r, d)
}
func (p *Pair) Write(d []byte) (n int, err error) {
return syscall.Write(p.w, d)
}
func (p *Pair) ReadFd() uintptr {
return uintptr(p.r)
}
func (p *Pair) WriteFd() uintptr {
return uintptr(p.w)
}

View File

@@ -0,0 +1,54 @@
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package splice
import (
"fmt"
"log"
"os"
"syscall"
)
func (p *Pair) LoadFromAt(fd uintptr, sz int, off int64) (int, error) {
n, err := syscall.Splice(int(fd), &off, p.w, nil, sz, 0)
return int(n), err
}
func (p *Pair) LoadFrom(fd uintptr, sz int) (int, error) {
if sz > p.size {
return 0, fmt.Errorf("LoadFrom: not enough space %d, %d",
sz, p.size)
}
n, err := syscall.Splice(int(fd), nil, p.w, nil, sz, 0)
if err != nil {
err = os.NewSyscallError("Splice load from", err)
}
return int(n), err
}
func (p *Pair) WriteTo(fd uintptr, n int) (int, error) {
m, err := syscall.Splice(p.r, nil, int(fd), nil, int(n), 0)
if err != nil {
err = os.NewSyscallError("Splice write", err)
}
return int(m), err
}
const _SPLICE_F_NONBLOCK = 0x2
func (p *Pair) discard() {
_, err := syscall.Splice(p.r, nil, devNullFD(), nil, int(p.size), _SPLICE_F_NONBLOCK)
if err == syscall.EAGAIN {
// all good.
} else if err != nil {
errR := syscall.Close(p.r)
errW := syscall.Close(p.w)
// This can happen if something closed our fd
// inadvertently (eg. double close)
log.Panicf("splicing into /dev/null: %v (close R %d '%v', close W %d '%v')", err, p.r, errR, p.w, errW)
}
}

107
vendor/github.com/hanwen/go-fuse/v2/splice/pool.go generated vendored Normal file
View File

@@ -0,0 +1,107 @@
//go:build linux
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package splice
import (
"sync"
)
var splicePool *pairPool
type pairPool struct {
sync.Mutex
unused []*Pair
usedCount int
}
func ClearSplicePool() {
splicePool.clear()
}
func Get() (*Pair, error) {
return splicePool.get()
}
func Total() int {
return splicePool.total()
}
func Used() int {
return splicePool.used()
}
// Done returns the pipe pair to pool.
func Done(p *Pair) {
splicePool.done(p)
}
// Closes and discards pipe pair.
func Drop(p *Pair) {
splicePool.drop(p)
}
func newSplicePairPool() *pairPool {
return &pairPool{}
}
func (pp *pairPool) clear() {
pp.Lock()
for _, p := range pp.unused {
p.Close()
}
pp.unused = pp.unused[:0]
pp.Unlock()
}
func (pp *pairPool) used() (n int) {
pp.Lock()
n = pp.usedCount
pp.Unlock()
return n
}
func (pp *pairPool) total() int {
pp.Lock()
n := pp.usedCount + len(pp.unused)
pp.Unlock()
return n
}
func (pp *pairPool) drop(p *Pair) {
p.Close()
pp.Lock()
pp.usedCount--
pp.Unlock()
}
func (pp *pairPool) get() (p *Pair, err error) {
pp.Lock()
defer pp.Unlock()
pp.usedCount++
l := len(pp.unused)
if l > 0 {
p := pp.unused[l-1]
pp.unused = pp.unused[:l-1]
return p, nil
}
return newSplicePair()
}
func (pp *pairPool) done(p *Pair) {
p.discard()
pp.Lock()
pp.usedCount--
pp.unused = append(pp.unused, p)
pp.Unlock()
}
func init() {
splicePool = newSplicePairPool()
}

106
vendor/github.com/hanwen/go-fuse/v2/splice/splice.go generated vendored Normal file
View File

@@ -0,0 +1,106 @@
//go:build linux
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package splice
// Routines for efficient file to file copying.
import (
"fmt"
"log"
"os"
"sync"
"syscall"
)
var maxPipeSize int
var resizable bool
func Resizable() bool {
return resizable
}
func MaxPipeSize() int {
return maxPipeSize
}
// From manpage on ubuntu Lucid:
//
// Since Linux 2.6.11, the pipe capacity is 65536 bytes.
const DefaultPipeSize = 16 * 4096
var (
devNullFDOnce sync.Once
devNullFDValue int
)
func init() {
content, err := os.ReadFile("/proc/sys/fs/pipe-max-size")
if err != nil {
maxPipeSize = DefaultPipeSize
} else {
fmt.Sscan(string(content), &maxPipeSize)
}
r, w, err := os.Pipe()
if err != nil {
log.Panicf("cannot create pipe: %v", err)
}
sz, errNo := fcntl(r.Fd(), F_GETPIPE_SZ, 0)
resizable = (errNo == 0)
_, errNo = fcntl(r.Fd(), F_SETPIPE_SZ, 2*sz)
resizable = resizable && (errNo == 0)
r.Close()
w.Close()
}
// We empty pipes by splicing to /dev/null.
func devNullFD() int {
devNullFDOnce.Do(func() {
fd, err := syscall.Open("/dev/null", os.O_WRONLY, 0)
if err != nil {
panic(fmt.Sprintf("failed to open /dev/null: %s", err))
}
devNullFDValue = fd
})
return devNullFDValue
}
// copy & paste from syscall.
func fcntl(fd uintptr, cmd int, arg int) (val int, errno syscall.Errno) {
r0, _, e1 := syscall.Syscall(syscall.SYS_FCNTL, fd, uintptr(cmd), uintptr(arg))
val = int(r0)
errno = syscall.Errno(e1)
return
}
const F_SETPIPE_SZ = 1031
const F_GETPIPE_SZ = 1032
func osPipe() (int, int, error) {
var fds [2]int
err := syscall.Pipe2(fds[:], syscall.O_NONBLOCK)
return fds[0], fds[1], err
}
func newSplicePair() (p *Pair, err error) {
p = &Pair{}
p.r, p.w, err = osPipe()
if err != nil {
return nil, err
}
var errNo syscall.Errno
p.size, errNo = fcntl(uintptr(p.r), F_GETPIPE_SZ, 0)
if err == syscall.EINVAL {
p.size = DefaultPipeSize
return p, nil
}
if errNo != 0 {
p.Close()
return nil, fmt.Errorf("fcntl getsize: %v", errNo)
}
return p, nil
}