當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Inode.File方法代碼示例

本文整理匯總了Golang中github.com/jnwhiteh/minixfs/common.Inode.File方法的典型用法代碼示例。如果您正苦於以下問題:Golang Inode.File方法的具體用法?Golang Inode.File怎麽用?Golang Inode.File使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/jnwhiteh/minixfs/common.Inode的用法示例。


在下文中一共展示了Inode.File方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: do_open

func (fs *FileSystem) do_open(proc *Process, path string, oflags int, omode uint16) (common.Fd, error) {
	// Remap the bottom two bits of oflags
	bits := mode_map[oflags&common.O_ACCMODE]

	var err error
	var rip *common.Inode
	var exist bool = false

	// If O_CREATE is set, try to make the file
	if oflags&common.O_CREAT > 0 {
		// Create a new node by calling new_node()
		omode := common.I_REGULAR | (omode & common.ALL_MODES & proc.umask)
		dirp, newrip, _, err := fs.new_node(proc, path, omode, common.NO_ZONE)
		if err == nil {
			exist = false
		} else if err != common.EEXIST {
			return nil, err
		} else {
			exist = (oflags&common.O_EXCL == 0)
		}

		// we don't need the parent directory
		fs.itable.PutInode(dirp)
		rip = newrip
	} else {
		// grab the inode at the given path
		rip, err = fs.eatPath(proc, path)
		if err != nil {
			return nil, err
		}
	}

	// Find an available filp entry for the file descriptor
	fdindex := -1
	for i := 0; i < len(proc.files); i++ {
		if proc.files[i] == nil {
			fdindex = i
			break
		}
	}

	if fdindex == -1 {
		return nil, common.EMFILE
	}

	err = nil // we'll use this to set error codes

	if exist { // if the file existed already
		// TODO: Check permissions here
		switch rip.Type() {
		case common.I_REGULAR:
			if oflags&common.O_TRUNC > 0 {
				common.Truncate(rip, 0, fs.bcache)
				// Flush the inode so it gets written on next block cache
				fs.itable.FlushInode(rip)
			}
		case common.I_DIRECTORY:
			// Directories cannot be opened in this system
			err = common.EISDIR
		default:
			panic("NYI: Process.Open with non regular/directory")
		}
	}

	if err != nil {
		// Something went wrong, so release the inode
		fs.itable.PutInode(rip)
		return nil, err
	}

	// Make sure there is a 'File' server running
	if rip.File == nil {
		// Spawn a file process to handle reading/writing
		rip.File = file.NewFile(rip)
	}

	// Create a new 'filp' object to expose to the user
	filp := &filp{1, 0, rip.File, rip, bits, new(sync.Mutex)}
	proc.files[fdindex] = filp

	return filp, nil
}
開發者ID:jnwhiteh,項目名稱:minixfs,代碼行數:82,代碼來源:syscalls.go


注:本文中的github.com/jnwhiteh/minixfs/common.Inode.File方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。