// Rread returns up to c bytes from file fid starting at offset o
Rread returns up to c bytes from file fid starting at offset o
// Rread returns up to c bytes from file fid starting at offset o
func (fs *fileServer) Rread(fid protocol.FID, offset protocol.Offset, count protocol.Count) ([]byte, error) {
	f, err := fs.getFile(fid)
	if err != nil {
		return nil, err
	}
	ctx := context.Background()
	switch f.Type {
	case icore.TDirectory:
		entries, err := fs.ipfs.ReadDir(ctx, f)
		if err != nil {
			return nil, err
		}
		start := 0
		for start < int(offset) && len(entries) > 0 {
			var discard bytes.Buffer
			protocol.Marshaldir(&discard, *plan9Dir(&entries[0]))
			start += discard.Len()
			entries = entries[1:]
		}
		if len(entries) == 0 {
			return nil, nil
		}
		if start != int(offset) {
			return nil, ErrBadOffset
		}
		var b bytes.Buffer
		for b.Len() < int(count) && len(entries) > 0 {
			var tmp bytes.Buffer
			protocol.Marshaldir(&tmp, *plan9Dir(&entries[0]))
			entries = entries[1:]
			if b.Len()+tmp.Len() > int(count) {
				break
			}
			b.Write(tmp.Bytes())
		}
		return b.Bytes(), nil
	case icore.TFile:
		file, err := fs.ipfs.GetFile(ctx, f)
		if err != nil {
			return nil, err
		}
		defer file.Close()
		if _, err := file.Seek(int64(offset), 0); err != nil {
			return nil, err
		}
		b := make([]byte, count)
		n, err := file.Read(b)
		if err != nil && err != io.EOF {
			return nil, err
		}
		return b[:n], nil
	}
	log.Printf("fid %v file type %v is unknown", fid, f.Type)
	return nil, nil
}
func ( fs * fileServer ) Rread ( fid protocol . FID , offset protocol . Offset , count protocol . Count ) ( [ ] byte , error ) { f , err := fs . getFile ( fid ) if err != nil { return nil , err } ctx := context . Background ( ) switch f . Type { case icore . TDirectory : entries , err := fs . ipfs . ReadDir ( ctx , f ) if err != nil { return nil , err } start := 0 for start < int ( offset ) && len ( entries ) > 0 { var discard bytes . Buffer protocol . Marshaldir ( & discard , * plan9Dir ( & entries [ 0 ] ) ) start += discard . Len ( ) entries = entries [ 1 : ] } if len ( entries ) == 0 { return nil , nil } if start != int ( offset ) { return nil , ErrBadOffset } var b bytes . Buffer for b . Len ( ) < int ( count ) && len ( entries ) > 0 { var tmp bytes . Buffer protocol . Marshaldir ( & tmp , * plan9Dir ( & entries [ 0 ] ) ) entries = entries [ 1 : ] if b . Len ( ) + tmp . Len ( ) > int ( count ) { break } b . Write ( tmp . Bytes ( ) ) } return b . Bytes ( ) , nil case icore . TFile : file , err := fs . ipfs . GetFile ( ctx , f ) if err != nil { return nil , err } defer file . Close ( ) if _ , err := file . Seek ( int64 ( offset ) , 0 ) ; err != nil { return nil , err } b := make ( [ ] byte , count ) n , err := file . Read ( b ) if err != nil && err != io . EOF { return nil , err } return b [ : n ] , nil } log . Printf ( " " , fid , f . Type ) return nil , nil }
Rread returns up to c bytes from file fid starting at offset o
Rread returns up to c bytes from file fid starting at offset o