Esempio n. 1
0
func (f *fs) handleRead(r *fuse.ReadRequest) {
	log.Println("Inside handleRead")
	log.Println(r)
	resp := &fuse.ReadResponse{Data: make([]byte, r.Size)}
	if r.Dir {
		// handle directory listing
		data := f.handles.getReadCache(r.Handle)
		if data == nil {
			d, err := f.rpc.api.ReadDirAll(f.getContext(), &pb.ReadDirAllRequest{Inode: uint64(r.Node)})
			log.Println(d)
			if err != nil {
				log.Fatalf("Read on dir failed: %v", err)
			}
			data = fuse.AppendDirent(data, fuse.Dirent{
				Name:  ".",
				Inode: uint64(r.Node),
				Type:  fuse.DT_Dir,
			})
			data = fuse.AppendDirent(data, fuse.Dirent{
				Name: "..",
				Type: fuse.DT_Dir,
			})
			for _, de := range d.DirEntries {
				log.Println(de)
				data = fuse.AppendDirent(data, fuse.Dirent{
					Name:  de.Name,
					Inode: de.Attr.Inode,
					Type:  fuse.DT_Dir,
				})
			}
			for _, fe := range d.FileEntries {
				log.Println(fe)
				data = fuse.AppendDirent(data, fuse.Dirent{
					Name:  fe.Name,
					Inode: fe.Attr.Inode,
					Type:  fuse.DT_File,
				})
			}
			f.handles.cacheRead(r.Handle, data)
		}
		fuseutil.HandleRead(r, resp, data)
		r.Respond(resp)
		return
	} else {
		// handle file read
		data, err := f.rpc.api.Read(f.getContext(), &pb.ReadRequest{
			Inode:  uint64(r.Node),
			Offset: int64(r.Offset),
			Size:   int64(r.Size),
		})
		if err != nil {
			log.Fatal("Read on file failed: ", err)
		}
		copy(resp.Data, data.Payload)
		r.Respond(resp)
	}
}
Esempio n. 2
0
func (sc *serveConn) readDir(req *fuse.ReadRequest) {
	inode := uint64(req.Header.Node)
	resp := &fuse.ReadResponse{make([]byte, 0, req.Size)}
	var dirs []fuse.Dirent
	file, err := sc.db.FileByInode(inode)
	if err != nil {
		fuse.Debug(fmt.Sprintf("FileByInode(%d): %v", inode, err))
		req.RespondError(fuse.EIO)
		return
	}

	for _, inode := range file.Children {
		f, err := sc.db.FileByInode(inode)
		if err != nil {
			fuse.Debug(fmt.Sprintf("child: FileByInode(%d): %v", inode, err))
			req.RespondError(fuse.EIO)
			return
		}
		childType := fuse.DT_File
		if f.MimeType == driveFolderMimeType {
			childType = fuse.DT_Dir
		}
		dirs = append(dirs, fuse.Dirent{Inode: f.Inode, Name: f.Title, Type: childType})
	}
	fuse.Debug(fmt.Sprintf("%+v", dirs))
	var data []byte
	for _, dir := range dirs {
		data = fuse.AppendDirent(data, dir)
	}
	fuseutil.HandleRead(req, resp, data)
	req.Respond(resp)
}
Esempio n. 3
0
func (sc *serveConn) read(req *fuse.ReadRequest) {
	inode := uint64(req.Header.Node)
	resp := &fuse.ReadResponse{}
	// Lookup which fileId this request refers to
	f, err := sc.db.FileByInode(inode)
	if err != nil {
		debug.Printf("FileByInode(%d): %v", inode, err)
		req.RespondError(fuse.EIO)
		return
	}
	debug.Printf("Read(title: %s, offset: %d, size: %d)\n", f.Title, req.Offset, req.Size)
	resp.Data, err = sc.db.ReadFiledata(f.Id, req.Offset, int64(req.Size), f.FileSize)
	if err != nil && err != io.EOF {
		debug.Printf("driveCache.Read (..%v..): %v", req.Offset, err)
		req.RespondError(fuse.EIO)
		return
	}
	req.Respond(resp)
}