lionkov / go9p

Package for implementing clients and servers of the 9P and 9P2000 distributed resource protocols in Go.
Other
20 stars 7 forks source link

add dirread9p to srv #27

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
dirread9p is very useful for Read calls on directories. an example in go is 
below, as i have used in my fileserver. it could just be called srv.Dirread.

type Dirgen func(uint64, interface{}) *p.Dir

func Dirread9p(req *srv.Req, gen Dirgen, i interface{}) {
    var start uint64
    var count uint32

    p.InitRread(req.Rc, req.Tc.Count)

    if req.Tc.Offset == 0 {
        start = 0
    } else {
        start = req.Fid.Diroffset
    }

    b := req.Rc.Data

    for len(b) > 0 {
        d := gen(start, i)
        if d == nil {
            break
        }
        sz := p.PackDir(d, b, req.Conn.Dotu)
        if sz == 0 {
            break
        }

        b = b[sz:]
        count += uint32(sz)
        start++
    }

    req.Fid.Diroffset = start

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

Original issue reported on code.google.com by mischief@offblast.org on 10 Mar 2014 at 12:34