Skip to content

Commit

Permalink
fix init on windows failed
Browse files Browse the repository at this point in the history
The failure is due to /dev/null is not existed on windows.
  • Loading branch information
winglq committed Nov 13, 2024
1 parent 6f79eb4 commit 9799dd1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
30 changes: 0 additions & 30 deletions splice/splice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ package splice

import (
"fmt"
"io/ioutil"
"log"
"os"
"syscall"
)

Expand All @@ -33,33 +30,6 @@ const DefaultPipeSize = 16 * 4096
// We empty pipes by splicing to /dev/null.
var devNullFD uintptr

func init() {
content, err := ioutil.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()

fd, err := syscall.Open("/dev/null", os.O_WRONLY, 0)
if err != nil {
log.Panicf("splice: %v", err)
}

devNullFD = uintptr(fd)
}

const F_SETPIPE_SZ = 1031
const F_GETPIPE_SZ = 1032

Expand Down
30 changes: 30 additions & 0 deletions splice/splice_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,39 @@ package splice

import (
"fmt"
"io/ioutil"
"log"
"os"
"syscall"
)

func init() {
content, err := ioutil.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()

fd, err := syscall.Open("/dev/null", os.O_WRONLY, 0)
if err != nil {
log.Panicf("splice: %v", err)
}

devNullFD = uintptr(fd)
}

// 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))
Expand Down
35 changes: 34 additions & 1 deletion splice/splice_linux.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
package splice

import "syscall"
import (
"fmt"
"io/ioutil"
"log"
"os"
"syscall"
)

func init() {
content, err := ioutil.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()

fd, err := syscall.Open("/dev/null", os.O_WRONLY, 0)
if err != nil {
log.Panicf("splice: %v", err)
}

devNullFD = uintptr(fd)
}

// copy & paste from syscall.
func fcntl(fd uintptr, cmd int, arg int) (val int, errno syscall.Errno) {
Expand Down
3 changes: 3 additions & 0 deletions splice/splice_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"syscall"
)

func init() {
}

func osPipe() (int, int, error) {
return 0, 0, fmt.Errorf("not implemented")
}
Expand Down

0 comments on commit 9799dd1

Please sign in to comment.