コード例 #1
0
ファイル: p9.go プロジェクト: flynn/bake
// Create creates a new file.
func (fs *fileSystem) Create(req *go9p.SrvReq) {
	aux := req.Fid.Aux.(*Aux)
	if err := aux.stat(); err != nil {
		req.RespondError(err)
		return
	}

	path := aux.path + "/" + req.Tc.Name

	var file *os.File
	var err error
	switch {
	case req.Tc.Perm&go9p.DMDIR != 0:
		err = os.Mkdir(path, os.FileMode(req.Tc.Perm&0777))

	case req.Tc.Perm&go9p.DMSYMLINK != 0:
		err = os.Symlink(req.Tc.Ext, path)

	case req.Tc.Perm&go9p.DMLINK != 0:
		n, err := strconv.ParseUint(req.Tc.Ext, 10, 0)
		if err != nil {
			break
		}

		ofid := req.Conn.FidGet(uint32(n))
		if ofid == nil {
			req.RespondError(go9p.Eunknownfid)
			return
		}

		err = os.Link(ofid.Aux.(*Aux).path, path)
		ofid.DecRef()

	case req.Tc.Perm&go9p.DMNAMEDPIPE != 0:
	case req.Tc.Perm&go9p.DMDEVICE != 0:
		req.RespondError(&go9p.Error{"not implemented", go9p.EIO})
		return

	default:
		var mode uint32 = req.Tc.Perm & 0777
		if req.Conn.Dotu {
			if req.Tc.Perm&go9p.DMSETUID > 0 {
				mode |= syscall.S_ISUID
			}
			if req.Tc.Perm&go9p.DMSETGID > 0 {
				mode |= syscall.S_ISGID
			}
		}
		file, err = os.OpenFile(path, omode2uflags(req.Tc.Mode)|os.O_CREATE, os.FileMode(mode))
	}

	if file == nil && err == nil {
		file, err = os.OpenFile(path, omode2uflags(req.Tc.Mode), 0)
	}

	if err != nil {
		req.RespondError(toError(err))
		return
	}

	aux.path = path
	aux.file = file

	// Save file to writeset.
	fs.addToWriteset(aux.rootID, path)

	if err := aux.stat(); err != nil {
		req.RespondError(err)
		return
	}

	req.RespondRcreate(aux.qid(), 0)
}