本文整理汇总了Golang中os.NewFile函数的典型用法代码示例。如果您正苦于以下问题:Golang NewFile函数的具体用法?Golang NewFile怎么用?Golang NewFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: listenStream
func listenStream(netw, addr string) (l net.Listener, err error) {
var (
file *os.File
)
fd, err := listen(netw, addr)
if err != nil {
return nil, err
}
// Set backlog size to the maximum
if err = syscall.Listen(fd, syscall.SOMAXCONN); err != nil {
syscall.Close(fd)
return nil, err
}
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
if l, err = net.FileListener(file); err != nil {
syscall.Close(fd)
return nil, err
}
if err = file.Close(); err != nil {
syscall.Close(fd)
l.Close()
return nil, err
}
return l, err
}
示例2: newfile
func newfile(fd uintptr, name string) *os.File {
p, err := winPath(name)
if err != nil {
return os.NewFile(fd, name)
}
return os.NewFile(fd, p)
}
示例3: newPipeFd
func newPipeFd() (*Pipe, error) {
r2pipeIn := os.Getenv("R2PIPE_IN")
r2pipeOut := os.Getenv("R2PIPE_OUT")
if r2pipeIn == "" || r2pipeOut == "" {
return nil, errors.New("missing R2PIPE_{IN,OUT} vars")
}
r2pipeInFd, err := strconv.Atoi(r2pipeIn)
if err != nil {
return nil, err
}
r2pipeOutFd, err := strconv.Atoi(r2pipeOut)
if err != nil {
return nil, err
}
stdout := os.NewFile(uintptr(r2pipeInFd), "R2PIPE_IN")
stdin := os.NewFile(uintptr(r2pipeOutFd), "R2PIPE_OUT")
r2p := &Pipe{
File: "",
r2cmd: nil,
stdin: stdin,
stdout: stdout,
}
return r2p, nil
}
示例4: newSocketPair
func newSocketPair(name string) (*os.File, *os.File, error) {
pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, nil, err
}
return os.NewFile(uintptr(pair[0]), name), os.NewFile(uintptr(pair[1]), name), nil
}
示例5: Pty
// Pty returns a UNIX 98 pseudoterminal device.
// Pty returns a pair of fds representing the master and slave pair.
func Pty() (*os.File, *os.File, error) {
open := func(path string) (uintptr, error) {
fd, err := syscall.Open(path, syscall.O_NOCTTY|syscall.O_RDWR|syscall.O_CLOEXEC, 0666)
if err != nil {
return 0, fmt.Errorf("unable to open %q: %v", path, err)
}
return uintptr(fd), nil
}
ptm, err := open("/dev/ptmx")
if err != nil {
return nil, nil, err
}
sname, err := ptsname(ptm)
if err != nil {
return nil, nil, err
}
err = grantpt(ptm)
if err != nil {
return nil, nil, err
}
err = unlockpt(ptm)
if err != nil {
return nil, nil, err
}
pts, err := open(sname)
if err != nil {
return nil, nil, err
}
return os.NewFile(uintptr(ptm), "ptm"), os.NewFile(uintptr(pts), sname), nil
}
示例6: Pty
// Pty returns a UNIX 98 pseudoterminal device.
// Pty returns a pair of fds representing the master and slave pair.
func Pty() (*os.File, *os.File, error) {
ptm, err := open_pty_master()
if err != nil {
return nil, nil, err
}
sname, err := Ptsname(ptm)
if err != nil {
return nil, nil, err
}
err = grantpt(ptm)
if err != nil {
return nil, nil, err
}
err = unlockpt(ptm)
if err != nil {
return nil, nil, err
}
pts, err := open_device(sname)
if err != nil {
return nil, nil, err
}
return os.NewFile(uintptr(ptm), "ptm"), os.NewFile(uintptr(pts), sname), nil
}
示例7: ForkPty
// Opens a pseudo-terminal, forks, sets up the pty slave as the new child process's controlling terminal and
// stdin/stdout/stderr, and execs the given command in the child process. Returns the master
// terminal control, the child pid, and an Error if any.
func ForkPty(name string, argv []string, attr *Attributes, size *WindowSize) (*Terminal, int, os.Error) {
master, slave, err := OpenPty(attr, size)
if err != nil {
return nil, -1, err
}
procattr := &os.ProcAttr{
Dir: "",
Env: nil,
Files: []*os.File{
os.NewFile(slave.Fd(), "/dev/stdin"),
os.NewFile(slave.Fd(), "/dev/stdout"),
os.NewFile(slave.Fd(), "/dev/stderr"),
},
Sys: &syscall.SysProcAttr{
Setsid: true,
Setctty: true,
},
}
proc, err := os.StartProcess(name, argv, procattr)
if err != nil {
return nil, -1, err
}
return master, proc.Pid, nil
}
示例8: newPipe
func newPipe() (parent *os.File, child *os.File, err error) {
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
if err != nil {
return nil, nil, err
}
return os.NewFile(uintptr(fds[1]), "parent"), os.NewFile(uintptr(fds[0]), "child"), nil
}
示例9: newFakeVTYServer
func newFakeVTYServer() (*fakeVTYServer, error) {
fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, err
}
vtyClientFile := os.NewFile(uintptr(fds[1]), "vty-client")
vtyClientConn, err := net.FileConn(vtyClientFile)
if err != nil {
return nil, err
}
vtyServerFile := os.NewFile(uintptr(fds[0]), "vty-server")
vtyServerConn, err := net.FileConn(vtyServerFile)
if err != nil {
return nil, err
}
syscall.SetNonblock(fds[0], false)
vs := &fakeVTYServer{
clientConn: vtyClientConn,
serverConn: vtyServerConn,
clientFile: vtyClientFile,
serverFile: vtyServerFile,
send: make(chan []byte),
received: make(chan []byte),
}
go vs.read()
go vs.write()
return vs, nil
}
示例10: createAttachment
func createAttachment(stream *spdystream.Stream) (*os.File, error) {
if stream.IsFinished() {
return nil, fmt.Errorf("stream already finished")
}
socketFds, socketErr := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.FD_CLOEXEC, 0)
if socketErr != nil {
return nil, socketErr
}
pipe := os.NewFile(uintptr(socketFds[1]), "")
defer pipe.Close()
conn, connErr := net.FileConn(pipe)
if connErr != nil {
return nil, connErr
}
go func() {
io.Copy(conn, stream)
conn.Close()
}()
go func() {
io.Copy(stream, conn)
}()
return os.NewFile(uintptr(socketFds[0]), ""), nil
}
示例11: socketPair
func socketPair() (sock1 *os.File, sock2 *os.File, err error) {
var fds [2]int
fds, err = syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
if err != nil {
fmt.Println(err.Error())
}
return os.NewFile(uintptr(fds[0]), "|0"), os.NewFile(uintptr(fds[1]), "|1"), err
}
示例12: mount
func mount(dir string, ready chan<- struct{}, errp *error) (fusefd *os.File, err error) {
// linux mount is never delayed
close(ready)
fds, err := syscall.Socketpair(syscall.AF_FILE, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, fmt.Errorf("socketpair error: %v", err)
}
defer syscall.Close(fds[0])
defer syscall.Close(fds[1])
cmd := exec.Command("fusermount", "--", dir)
cmd.Env = append(os.Environ(), "_FUSE_COMMFD=3")
writeFile := os.NewFile(uintptr(fds[0]), "fusermount-child-writes")
defer writeFile.Close()
cmd.ExtraFiles = []*os.File{writeFile}
out, err := cmd.CombinedOutput()
if len(out) > 0 || err != nil {
return nil, fmt.Errorf("fusermount: %q, %v", out, err)
}
readFile := os.NewFile(uintptr(fds[1]), "fusermount-parent-reads")
defer readFile.Close()
c, err := net.FileConn(readFile)
if err != nil {
return nil, fmt.Errorf("FileConn from fusermount socket: %v", err)
}
defer c.Close()
uc, ok := c.(*net.UnixConn)
if !ok {
return nil, fmt.Errorf("unexpected FileConn type; expected UnixConn, got %T", c)
}
buf := make([]byte, 32) // expect 1 byte
oob := make([]byte, 32) // expect 24 bytes
_, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
return nil, fmt.Errorf("ParseSocketControlMessage: %v", err)
}
if len(scms) != 1 {
return nil, fmt.Errorf("expected 1 SocketControlMessage; got scms = %#v", scms)
}
scm := scms[0]
gotFds, err := syscall.ParseUnixRights(&scm)
if err != nil {
return nil, fmt.Errorf("syscall.ParseUnixRights: %v", err)
}
if len(gotFds) != 1 {
return nil, fmt.Errorf("wanted 1 fd; got %#v", gotFds)
}
f := os.NewFile(uintptr(gotFds[0]), "/dev/fuse")
return f, nil
}
示例13: newCtlSockets
func newCtlSockets() (*os.File, *os.File, error) {
fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_SEQPACKET, 0)
if err != nil {
return nil, nil, err
}
f1 := os.NewFile(uintptr(fds[0]), "ctl")
f2 := os.NewFile(uintptr(fds[1]), "ctl")
return f1, f2, nil
}
示例14: unixgramSocketpair
func unixgramSocketpair() (l, r *os.File, err error) {
fd, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_SEQPACKET, 0)
if err != nil {
return nil, nil, os.NewSyscallError("socketpair",
err.(syscall.Errno))
}
l = os.NewFile(uintptr(fd[0]), "socketpair-half1")
r = os.NewFile(uintptr(fd[1]), "socketpair-half2")
return
}
示例15: initPipe
// either gets a pipe off the pool or returns a brand new one
func initPipe() (p *pipe, err error) {
var pp [2]int32
_, _, e := syscall.RawSyscall(syscall.SYS_PIPE2, uintptr(unsafe.Pointer(&pp)), syscall.O_CLOEXEC, 0)
if e != 0 {
return nil, os.NewSyscallError("pipe", e)
}
fmt.Printf("Got new pipe, read fd %d, write fd %d\n", pp[0], pp[1])
return &pipe{os.NewFile(uintptr(pp[0]), "|0"), os.NewFile(uintptr(pp[1]), "|1"), 0}, nil
}