In Go, when is SyscallConn() useful for regular *os.File?

王林
Release: 2024-02-05 23:30:03
forward
793 people have browsed it

在 Go 中,什么时候 SyscallConn() 对常规 *os.File 有用?

Question content

I am reading some instructions on using SyscallConn() (on *os.File defined function) Go code (from https://github.com/KarpelesLab/reflink):

// reflinkInternal performs the actual reflink action without worrying about fallback
func reflinkInternal(d, s *os.File) error {
    ss, err := s.SyscallConn()
    if err != nil {
        return err
    }
    sd, err := d.SyscallConn()
    if err != nil {
        return err
    }
    var err2, err3 error
    err = sd.Control(func(dfd uintptr) {
        err2 = ss.Control(func(sfd uintptr) {
            // int ioctl(int dest_fd, FICLONE, int src_fd);
            err3 = unix.IoctlFileClone(int(dfd), int(sfd))
        })
    })
    if err != nil {
        // sd.Control failed
        return err
    }
    if err2 != nil {
        // ss.Control failed
        return err2
    }
    if err3 != nil && errors.Is(err3, unix.ENOTSUP) {
        return ErrReflinkFailed
    }
    // err3 is ioctl() response
    return err3
}
Copy after login

In this example, use these Control() functions instead of directly using d.Fd() and s.Fd() Is there any Advantage? More generally, what is func func (*os.File) SyscallConn() (syscall.RawConn, error) used for?


Correct answer


##file.Fd() Returns a file descriptor, and it causes the file descriptor to run in blocking mode (occupying a thread for blocking operations). SyscallConn does not do this. In fact, it was created specifically to get a file descriptor without blocking it. See this question for more information.

The above is the detailed content of In Go, when is SyscallConn() useful for regular *os.File?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!