示例#1
0
文件: vufs.go 项目: postfix/vufs
func (u *VuFs) Read(req *srv.Req) {
	fid := req.Fid.Aux.(*Fid)
	tc := req.Tc
	rc := req.Rc
	st, err := os.Stat(fid.path)
	if err != nil {
		req.RespondError(err)
		return
	}

	p.InitRread(rc, tc.Count)
	var count int
	var e error
	if st.IsDir() {
		// Simpler to treat non-zero offset as an error for directories.
		if tc.Offset != 0 {
			req.RespondError(srv.Ebadoffset)
			return
		}

		dirs, e := fid.file.Readdir(-1)

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

		// Bytes/one packed dir = 49 + len(name) + len(uid) + len(gid) + len(muid)
		// Estimate 49 + 20 + 20 + 20 + 11
		// From ../../lionkov/go9p/p/p9.go:421,427
		dirents := make([]byte, 0, 120*len(dirs))
		for i := 0; i < len(dirs); i++ {
			path := fid.path + "/" + dirs[i].Name()
			st, err := dir2Dir(path, dirs[i], req.Conn.Srv.Upool)
			if err != nil {
				req.RespondError(toError(err))
				return
			}
			b := p.PackDir(st, false)
			dirents = append(dirents, b...)
		}

		if len(dirents) > int(tc.Count) {
			req.RespondError(srv.Etoolarge)
			return
		}

		copy(rc.Data, dirents)

		count = len(dirents)

	} else {
		count, e = fid.file.ReadAt(rc.Data, int64(tc.Offset))
		if e != nil && e != io.EOF {
			req.RespondError(toError(e))
			return
		}
	}
	p.SetRreadCount(rc, uint32(count))
	req.Respond()
}
示例#2
0
文件: file.go 项目: edahlgren/go9p
func (*Fsrv) Read(req *Req) {
	var i, n int
	var err error

	fid := req.Fid.Aux.(*FFid)
	f := fid.F
	tc := req.Tc
	rc := req.Rc
	p.InitRread(rc, tc.Count)

	if f.Mode&p.DMDIR != 0 {
		// directory
		if tc.Offset == 0 {
			var g *File
			f.Lock()
			for n, g = 0, f.cfirst; g != nil; n, g = n+1, g.next {
			}

			fid.dirs = make([]*File, n)
			for n, g = 0, f.cfirst; g != nil; n, g = n+1, g.next {
				fid.dirs[n] = g
			}
			f.Unlock()
		}

		n = 0
		b := rc.Data
		// only return whole entries.
		for i = 0; i < len(fid.dirs); i++ {
			g := fid.dirs[i]
			g.Lock()
			if (g.flags & Fremoved) != 0 {
				g.Unlock()
				continue
			}

			nd := p.PackDir(&g.Dir, req.Conn.Dotu)
			g.Unlock()

			if len(nd) > len(b) {
				break
			}
			copy(b, nd)
			b = b[len(nd):]
			n += len(nd)
		}
		fid.dirs = fid.dirs[i:]
	} else {
		// file
		if rop, ok := f.Ops.(FReadOp); ok {
			n, err = rop.Read(fid, rc.Data, tc.Offset)
			if err != nil {
				req.RespondError(err)
				return
			}
		} else {
			req.RespondError(Eperm)
			return
		}
	}

	p.SetRreadCount(rc, uint32(n))
	req.Respond()
}
示例#3
0
文件: ufs.go 项目: rjkroege/go9p
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
	}

	p.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 := p.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(&p.Error{"too small read size for dir entry", p.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
		}
	}

	p.SetRreadCount(rc, uint32(count))
	req.Respond()
}