Example #1
0
func (f *NullFS) Walk(req *srv.Req) {
	tc := req.Tc

	if len(tc.Wname) > 1 && tc.Qid.Type != ninep.QTDIR {
		req.RespondError(ninep.ENOENT)
		return
	}

	// The most common case is walking from '.', so we initialize to '.' and fix it up
	// later if needed.
	if req.Newfid.Aux == nil {
		req.Newfid.Aux = &Fid{NullFile: NullFile{Name: ".", Qid: dirQids["."]}}
	}

	if len(tc.Wname) == 0 {
		req.RespondRwalk([]ninep.Qid{})
		return
	}

	if q, ok := dirQids[tc.Wname[0]]; !ok {
		req.RespondError(ninep.ENOENT)
	} else {
		req.Newfid.Aux.(*Fid).Name = tc.Wname[0]
		req.Newfid.Aux.(*Fid).Qid = q
		req.RespondRwalk([]ninep.Qid{*q})
	}
}
Example #2
0
func (*NullFS) Attach(req *srv.Req) {

	if req.Afid != nil {
		req.RespondError(srv.Enoauth)
		return
	}

	req.Fid.Aux = &Fid{NullFile: NullFile{Name: "."}}

	req.RespondRattach(dirQids["."])
}
Example #3
0
func (*Ufs) Stat(req *srv.Req) {
	fid := req.Fid.Aux.(*Fid)
	if err := fid.stat(); err != nil {
		req.RespondError(err)
		return
	}

	st, err := dir2Dir(fid.path, fid.st, req.Conn.Dotu, req.Conn.Srv.Upool)
	if err != nil {
		req.RespondError(err)
		return
	}
	req.RespondRstat(st)
}
Example #4
0
func (*Ufs) Remove(req *srv.Req) {
	fid := req.Fid.Aux.(*Fid)
	err := fid.stat()
	if err != nil {
		req.RespondError(err)
		return
	}

	e := os.Remove(fid.path)
	if e != nil {
		req.RespondError(toError(e))
		return
	}

	req.RespondRremove()
}
Example #5
0
func (*Ufs) Write(req *srv.Req) {
	fid := req.Fid.Aux.(*Fid)
	tc := req.Tc
	err := fid.stat()
	if err != nil {
		req.RespondError(err)
		return
	}

	n, e := fid.file.WriteAt(tc.Data, int64(tc.Offset))
	if e != nil {
		req.RespondError(toError(e))
		return
	}

	req.RespondRwrite(uint32(n))
}
Example #6
0
func (f *NullFS) Read(r *srv.Req) {
	var count int

	ninep.InitRread(r.Rc, r.Tc.Count)
	fid := r.Fid.Aux.(*Fid)

	if fid.Qid.Path == Qroot {
		var dirents []byte
		for path, v := range dirQids {
			d := &ninep.Dir{
				Qid:  *v,
				Type: uint16(v.Type),
				Mode: uint32(v.Type) | v.Version,
				Name: path,
				Uid:  "root",
				Gid:  "root",
			}
			b := ninep.PackDir(d, true)
			dirents = append(dirents, b...)
			count += len(b)
		}

		// TODO: put this boilerplate into a helper function.
		switch {
		case r.Tc.Offset > uint64(len(dirents)):
			count = 0
		case len(dirents[r.Tc.Offset:]) > int(r.Tc.Count):
			count = int(r.Tc.Count)
		default:
			count = len(dirents[r.Tc.Offset:])
		}

		if count == 0 && int(r.Tc.Offset) < len(dirents) && len(dirents) > 0 {
			r.RespondError(&ninep.Error{Err: "too small read size for dir entry", Errornum: ninep.EINVAL})
			return
		}
		copy(r.Rc.Data, dirents[r.Tc.Offset:int(r.Tc.Offset)+count])
	} else {
		if fid.Qid.Path == Qzero {
			count = int(r.Tc.Count)
		}
	}
	ninep.SetRreadCount(r.Rc, uint32(count))
	r.Respond()
}
Example #7
0
func (*Ufs) Open(req *srv.Req) {
	fid := req.Fid.Aux.(*Fid)
	tc := req.Tc
	err := fid.stat()
	if err != nil {
		req.RespondError(err)
		return
	}

	var e error
	fid.file, e = os.OpenFile(fid.path, omode2uflags(tc.Mode), 0)
	if e != nil {
		req.RespondError(toError(e))
		return
	}

	req.RespondRopen(dir2Qid(fid.st), 0)
}
Example #8
0
func (*Ufs) Walk(req *srv.Req) {
	fid := req.Fid.Aux.(*Fid)
	tc := req.Tc

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

	if req.Newfid.Aux == nil {
		req.Newfid.Aux = new(Fid)
	}

	nfid := req.Newfid.Aux.(*Fid)
	wqids := make([]ninep.Qid, len(tc.Wname))
	path := fid.path
	i := 0
	for ; i < len(tc.Wname); i++ {
		p := path + "/" + tc.Wname[i]
		st, err := os.Lstat(p)
		if err != nil {
			if i == 0 {
				req.RespondError(Enoent)
				return
			}

			break
		}

		wqids[i] = *dir2Qid(st)
		path = p
	}

	nfid.path = path
	req.RespondRwalk(wqids[0:i])
}
Example #9
0
func (u *Ufs) Attach(req *srv.Req) {
	if req.Afid != nil {
		req.RespondError(srv.Enoauth)
		return
	}

	tc := req.Tc
	fid := new(Fid)

	// You can think of the ufs.Root as a 'chroot' of a sort.
	// client attaches are not allowed to go outside the
	// directory represented by ufs.Root
	fid.path = path.Join(u.Root, tc.Aname)

	req.Fid.Aux = fid
	err := fid.stat()
	if err != nil {
		req.RespondError(err)
		return
	}

	qid := dir2Qid(fid.st)
	req.RespondRattach(qid)
}
Example #10
0
func (f *NullFS) Wstat(r *srv.Req) {
	r.RespondError(srv.Eperm)
}
Example #11
0
func (f *NullFS) Remove(r *srv.Req) {
	r.RespondError(srv.Eperm)
}
Example #12
0
func (f *NullFS) Create(r *srv.Req) {
	r.RespondError(srv.Eperm)
}
Example #13
0
func (u *Ufs) Wstat(req *srv.Req) {
	var changed bool
	fid := req.Fid.Aux.(*Fid)
	err := fid.stat()
	if err != nil {
		req.RespondError(err)
		return
	}

	dir := &req.Tc.Dir
	if dir.Mode != 0xFFFFFFFF {
		changed = true
		mode := dir.Mode & 0777
		if req.Conn.Dotu {
			if dir.Mode&ninep.DMSETUID > 0 {
				mode |= syscall.S_ISUID
			}
			if dir.Mode&ninep.DMSETGID > 0 {
				mode |= syscall.S_ISGID
			}
		}
		e := os.Chmod(fid.path, os.FileMode(mode))
		if e != nil {
			req.RespondError(toError(e))
			return
		}
	}

	uid, gid := ninep.NOUID, ninep.NOUID
	if req.Conn.Dotu {
		uid = dir.Uidnum
		gid = dir.Gidnum
	}

	// Try to find local uid, gid by name.
	if (dir.Uid != "" || dir.Gid != "") && !req.Conn.Dotu {
		changed = true
		uid, err = lookup(dir.Uid, false)
		if err != nil {
			req.RespondError(err)
			return
		}

		// BUG(akumar): Lookup will never find gids
		// corresponding to group names, because
		// it only operates on user names.
		gid, err = lookup(dir.Gid, true)
		if err != nil {
			req.RespondError(err)
			return
		}
	}

	if uid != ninep.NOUID || gid != ninep.NOUID {
		changed = true
		e := os.Chown(fid.path, int(uid), int(gid))
		if e != nil {
			req.RespondError(toError(e))
			return
		}
	}

	if dir.Name != "" {
		changed = true
		// If we path.Join dir.Name to / before adding it to
		// the fid path, that ensures nobody gets to walk out of the
		// root of this server.
		newname := path.Join(path.Dir(fid.path), path.Join("/", dir.Name))

		// absolute renaming. Ufs can do this, so let's support it.
		// We'll allow an absolute path in the Name and, if it is,
		// we will make it relative to root. This is a gigantic performance
		// improvement in systems that allow it.
		if filepath.IsAbs(dir.Name) {
			newname = path.Join(u.Root, dir.Name)
		}

		err := syscall.Rename(fid.path, newname)
		if err != nil {
			req.RespondError(toError(err))
			return
		}
		fid.path = newname
	}

	if dir.Length != 0xFFFFFFFFFFFFFFFF {
		changed = true
		e := os.Truncate(fid.path, int64(dir.Length))
		if e != nil {
			req.RespondError(toError(e))
			return
		}
	}

	// If either mtime or atime need to be changed, then
	// we must change both.
	if dir.Mtime != ^uint32(0) || dir.Atime != ^uint32(0) {
		changed = true
		mt, at := time.Unix(int64(dir.Mtime), 0), time.Unix(int64(dir.Atime), 0)
		if cmt, cat := (dir.Mtime == ^uint32(0)), (dir.Atime == ^uint32(0)); cmt || cat {
			st, e := os.Stat(fid.path)
			if e != nil {
				req.RespondError(toError(e))
				return
			}
			switch cmt {
			case true:
				mt = st.ModTime()
			default:
				at = atime(st.Sys().(*syscall.Stat_t))
			}
		}
		e := os.Chtimes(fid.path, at, mt)
		if e != nil {
			req.RespondError(toError(e))
			return
		}
	}

	if !changed && fid.file != nil {
		fid.file.Sync()
	}
	req.RespondRwstat()
}
Example #14
0
func (u *Ufs) Read(req *srv.Req) {
	dbg := u.Debuglevel&srv.DbgLogFcalls != 0
	fid := req.Fid.Aux.(*Fid)
	tc := req.Tc
	rc := req.Rc
	err := fid.stat()
	if err != nil {
		req.RespondError(err)
		return
	}

	ninep.InitRread(rc, tc.Count)
	var count int
	var e error
	if fid.st.IsDir() {
		if tc.Offset == 0 {
			var e error
			// If we got here, it was open. Can't really seek
			// in most cases, just close and reopen it.
			fid.file.Close()
			if fid.file, e = os.OpenFile(fid.path, omode2uflags(req.Fid.Omode), 0); e != nil {
				req.RespondError(toError(e))
				return
			}

			if fid.dirs, e = fid.file.Readdir(-1); e != nil {
				req.RespondError(toError(e))
				return
			}

			if dbg {
				log.Printf("Read: read %d entries", len(fid.dirs))
			}
			fid.dirents = nil
			fid.direntends = nil
			for i := 0; i < len(fid.dirs); i++ {
				path := fid.path + "/" + fid.dirs[i].Name()
				st, err := dir2Dir(path, fid.dirs[i], req.Conn.Dotu, req.Conn.Srv.Upool)
				if err != nil {
					if dbg {
						log.Printf("dbg: stat of %v: %v", path, err)
					}
					continue
				}
				if dbg {
					log.Printf("Stat: %v is %v", path, st)
				}
				b := ninep.PackDir(st, req.Conn.Dotu)
				fid.dirents = append(fid.dirents, b...)
				count += len(b)
				fid.direntends = append(fid.direntends, count)
				if dbg {
					log.Printf("fid.direntends is %v\n", fid.direntends)
				}
			}
		}

		switch {
		case tc.Offset > uint64(len(fid.dirents)):
			count = 0
		case len(fid.dirents[tc.Offset:]) > int(tc.Count):
			count = int(tc.Count)
		default:
			count = len(fid.dirents[tc.Offset:])
		}

		if dbg {
			log.Printf("readdir: count %v @ offset %v", count, tc.Offset)
		}
		nextend := sort.SearchInts(fid.direntends, int(tc.Offset)+count)
		if nextend < len(fid.direntends) {
			if fid.direntends[nextend] > int(tc.Offset)+count {
				if nextend > 0 {
					count = fid.direntends[nextend-1] - int(tc.Offset)
				} else {
					count = 0
				}
			}
		}
		if dbg {
			log.Printf("readdir: count adjusted %v @ offset %v", count, tc.Offset)
		}
		if count == 0 && int(tc.Offset) < len(fid.dirents) && len(fid.dirents) > 0 {
			req.RespondError(&ninep.Error{"too small read size for dir entry", ninep.EINVAL})
			return
		}
		copy(rc.Data, fid.dirents[tc.Offset:int(tc.Offset)+count])
	} else {
		count, e = fid.file.ReadAt(rc.Data, int64(tc.Offset))
		if e != nil && e != io.EOF {
			req.RespondError(toError(e))
			return
		}
	}

	ninep.SetRreadCount(rc, uint32(count))
	req.Respond()
}
Example #15
0
func (*Ufs) Create(req *srv.Req) {
	fid := req.Fid.Aux.(*Fid)
	tc := req.Tc
	err := fid.stat()
	if err != nil {
		req.RespondError(err)
		return
	}

	path := fid.path + "/" + tc.Name
	var e error = nil
	var file *os.File = nil
	switch {
	case tc.Perm&ninep.DMDIR != 0:
		e = os.Mkdir(path, os.FileMode(tc.Perm&0777))

	case tc.Perm&ninep.DMSYMLINK != 0:
		e = os.Symlink(tc.Ext, path)

	case tc.Perm&ninep.DMLINK != 0:
		n, e := strconv.ParseUint(tc.Ext, 10, 0)
		if e != nil {
			break
		}

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

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

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

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

	if file == nil && e == nil {
		file, e = os.OpenFile(path, omode2uflags(tc.Mode), 0)
	}

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

	fid.path = path
	fid.file = file
	err = fid.stat()
	if err != nil {
		req.RespondError(err)
		return
	}

	req.RespondRcreate(dir2Qid(fid.st), 0)
}