首页 > 其他分享 >golang: os.open函数

golang: os.open函数

时间:2023-01-17 08:11:51浏览次数:37  
标签:文件 name err syscall open golang int file os

  • Open函数
/*
    类似c里的fopen函数返回一个file的指针
    传入参数文件名(路径)
*/
func Open(name string) (*File, error) {
    /*
        调用OpenFile函数, 传入文件名,只读模式和权限0
        权限: linux的文件权限0777,8进制rwx
    */
	return OpenFile(name, O_RDONLY, 0)
}

  • OpenFile函数
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
	testlog.Open(name)
    // 打开文件*File返回并赋值给f
	f, err := openFileNolog(name, flag, perm)
	if err != nil {
		return nil, err
	}
	f.appendMode = flag&O_APPEND != 0

	return f, nil
}

  • openFileNolog函数
/* 
    openFileNolog是OpenFile的Unix实现
*/
func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
    // setSticky: false为不创建文件, true为创建文件
	setSticky := false
    /*
        默认值: supportsCreateWithStickyBit == false
        O_RDONLY & O_CREATE: 0x0 & 0x200 如果以只读模式传入,0和任意数位于皆为0,条件为假不继续判断,跳过
        ModeSticky: 1048576
    */
	if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
        // 如果打开文件的错误信息是"文件不存在"则修改setSticky的值为true
		if _, err := Stat(name); IsNotExist(err) {
			setSticky = true
		}
	}

	var r int
	for {
		var e error
		// 调用了syscall.Open,该函数类似c中open,返回fd
		r, e = syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
		if e == nil {
			break
		}

		// We have to check EINTR here, per issues 11180 and 39237.
		if e == syscall.EINTR {
			continue
		}

		return nil, &PathError{Op: "open", Path: name, Err: e}
	}

	// open(2) itself won't handle the sticky bit on *BSD and Solaris
	// 如果需要创建文件则修改打开模式
	if setSticky {
		setStickyBit(name)
	}

	// There's a race here with fork/exec, which we are
	// content to live with. See ../syscall/exec_unix.go.
	if !supportsCloseOnExec {
		syscall.CloseOnExec(r)
	}

	return newFile(uintptr(r), name, kindOpenFile), nil
}

  • newFile函数
func newFile(fd uintptr, name string, kind newFileKind) *File {
	fdi := int(fd)
	// 如果fd小于0,比如一般认为是-1为打开失败
	if fdi < 0 {
		return nil
	}
	// 创建File实例
	f := &File{&file{
		pfd: poll.FD{
			Sysfd:         fdi,
			IsStream:      true,
			ZeroReadIsEOF: true,
		},
		name:        name,
		stdoutOrErr: fdi == 1 || fdi == 2,
	}}
	/* 
		kind: (此时来自openFileNolog) 1
		kindOpenFile: 1 
		kindPipe: 2 
		kindNonBlock: 3 
	*/
	// pollable 为true
	pollable := kind == kindOpenFile || kind == kindPipe || kind == kindNonBlock

	// If the caller passed a non-blocking filedes (kindNonBlock),
	// we assume they know what they are doing so we allow it to be
	// used with kqueue.
	if kind == kindOpenFile {
		switch runtime.GOOS {
		case "darwin", "ios", "dragonfly", "freebsd", "netbsd", "openbsd":
			var st syscall.Stat_t
			err := ignoringEINTR(func() error {
				return syscall.Fstat(fdi, &st)
			})
			typ := st.Mode & syscall.S_IFMT
			// Don't try to use kqueue with regular files on *BSDs.
			// On FreeBSD a regular file is always
			// reported as ready for writing.
			// On Dragonfly, NetBSD and OpenBSD the fd is signaled
			// only once as ready (both read and write).
			// Issue 19093.
			// Also don't add directories to the netpoller.
			if err == nil && (typ == syscall.S_IFREG || typ == syscall.S_IFDIR) {
				pollable = false
			}

			// In addition to the behavior described above for regular files,
			// on Darwin, kqueue does not work properly with fifos:
			// closing the last writer does not cause a kqueue event
			// for any readers. See issue #24164.
			if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && typ == syscall.S_IFIFO {
				pollable = false
			}
		}
	}

	if err := f.pfd.Init("file", pollable); err != nil {
		// An error here indicates a failure to register
		// with the netpoll system. That can happen for
		// a file descriptor that is not supported by
		// epoll/kqueue; for example, disk files on
		// Linux systems. We assume that any real error
		// will show up in later I/O.
	} else if pollable {
		// We successfully registered with netpoll, so put
		// the file into nonblocking mode.
		if err := syscall.SetNonblock(fdi, true); err == nil {
			f.nonblock = true
		}
	}

	runtime.SetFinalizer(f.file, (*file).close)
	return f
}
  • File结构
type File struct {
	*file // os specific
}

  • file结构
type file struct {
	pfd         poll.FD   // 这个相当于c里open函数返回的文件描述符
	name        string   // 文件名
	dirinfo     *dirInfo // 目录信息,打开文件时值为nil, 打开文件时为非空值
	nonblock    bool     // 是否以无锁模式打开
	stdoutOrErr bool     // whether this is stdout or stderr
	appendMode  bool     // 是否是追加模式打开
}

  • poll.FD结构
// 文件描述符结构
type FD struct {
	// Lock sysfd and serialize access to Read and Write methods.
	fdmu fdMutex

	// 文件描述符,默认打开一个文件就是3, 0 标准输入,1标准输出,2标准错误输出
	Sysfd int

	// I/O 轮询器
	pd pollDesc

	// Writev 缓存
	iovecs *[]syscall.Iovec

	// 文件关闭时的信号量
	csema uint32

	// 是否阻塞模式: 如果文件处于阻塞模式,这个值非0
	isBlocking uint32

    // 是否是数据流套接字: 打开文件则为假,打开网络连接时则为真
	IsStream bool

    // 读取0个字节是否为EOF: 操作文件为真,操作套接字为假
	ZeroReadIsEOF bool

	// 是否文件: 文件为真,套接字为假
	isFile bool
}
  • 文件打开模式
const (
	// O_RDONLY, O_WRONLY, or O_RDWR这三个不能同时使用
	O_RDONLY int = syscall.O_RDONLY // == 0x0; 只读模式 
	O_WRONLY int = syscall.O_WRONLY // == 0x1; 只写模式
	O_RDWR   int = syscall.O_RDWR   // == 0x2; 读写模式
	O_APPEND int = syscall.O_APPEND // == 0x8; 追加模式
	O_CREATE int = syscall.O_CREAT  // == 0x200; 文件不存在则创建
	O_EXCL   int = syscall.O_EXCL   // == 0x800; 和O_CREATE一起使用时, 文件必须不存在
	O_SYNC   int = syscall.O_SYNC   // == 0x80; 打开文件使用同步IO
	O_TRUNC  int = syscall.O_TRUNC  // == 0x400; 截断文件,会清空文件
)

标签:文件,name,err,syscall,open,golang,int,file,os
From: https://www.cnblogs.com/ishmaelwanglin/p/17056881.html

相关文章