Пример #1
0
// implements nodefs.File
func (this *hookFile) Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status) {
	hook, hookEnabled := this.hook.(HookOnRead)
	var prehookBuf, posthookBuf []byte
	var prehookErr, posthookErr error
	var prehooked, posthooked bool
	var prehookCtx HookContext

	if hookEnabled {
		prehookBuf, prehookErr, prehooked, prehookCtx = hook.PreRead(this.name, int64(len(dest)), off)
		if prehooked {
			log.WithFields(log.Fields{
				"this": this,
				// "prehookBuf": prehookBuf,
				"prehookErr": prehookErr,
				"prehookCtx": prehookCtx,
			}).Debug("Read: Prehooked")
			return fuse.ReadResultData(prehookBuf), fuse.ToStatus(prehookErr)
		}
	}

	lowerRR, lowerCode := this.file.Read(dest, off)
	if hookEnabled {
		lowerRRBuf, lowerRRBufStatus := lowerRR.Bytes(make([]byte, lowerRR.Size()))
		if lowerRRBufStatus != fuse.OK {
			log.WithField("error", lowerRRBufStatus).Panic("lowerRR.Bytes() should not cause an error")
		}
		posthookBuf, posthookErr, posthooked = hook.PostRead(int32(lowerCode), lowerRRBuf, prehookCtx)
		if posthooked {
			if len(posthookBuf) != len(lowerRRBuf) {
				log.WithFields(log.Fields{
					"this": this,
					// "posthookBuf": posthookBuf,
					"posthookErr":    posthookErr,
					"posthookBufLen": len(posthookBuf),
					"lowerRRBufLen":  len(lowerRRBuf),
					"destLen":        len(dest),
				}).Warn("Read: Posthooked, but posthookBuf length != lowerrRRBuf length. You may get a strange behavior.")
			}

			log.WithFields(log.Fields{
				"this": this,
				// "posthookBuf": posthookBuf,
				"posthookErr": posthookErr,
			}).Debug("Read: Posthooked")
			return fuse.ReadResultData(posthookBuf), fuse.ToStatus(posthookErr)
		}
	}

	return lowerRR, lowerCode
}
Пример #2
0
func (node *AppendFSNode) Read(file nodefs.File, dest []byte, off int64, context *fuse.Context) (fuse.ReadResult, fuse.Status) {
	ret := fuse.OK
	dataFile, err := os.Open(node.fs.dataFilePath)
	if err != nil {
		ret = fuse.EIO
	}
	node.metadataMutex.RLock()
	start, end := int(off), int(off)+len(dest)-1
	entries := node.contentRanges.InRange(start, end)
	for _, entry := range entries {
		readStart, readEnd := max(entry.Min, start), min(entry.Max, end)+1
		blockStart, blockEnd := readStart-int(off), readEnd-int(off)
		blockDest := dest[blockStart:blockEnd]
		if fse, ok := entry.Data.(fileSegmentEntry); ok {
			readPos := int64(fse.base + readStart)
			//fmt.Printf("fileOffset: %d, blockStart: %d, blockEnd: %d, readPos: %d, min: %d, max: %d\n",
			//fse.fileOffset, blockStart, blockEnd, readPos, entry.Min, entry.Max)
			_, err = dataFile.ReadAt(blockDest, readPos)
			if err != nil {
				fmt.Printf("Read error\n")
				ret = fuse.EIO
			}

		}
	}
	node.metadataMutex.RUnlock()
	err = dataFile.Close()
	if err != nil {
		ret = fuse.EIO
	}
	if ret != fuse.OK {
		return nil, ret
	}
	return fuse.ReadResultData(dest), ret
}
Пример #3
0
func (n *nodeReadNode) Read(file File, dest []byte, off int64, context *fuse.Context) (fuse.ReadResult, fuse.Status) {
	e := off + int64(len(dest))
	if int(e) > len(n.data) {
		e = int64(len(n.data))
	}
	return fuse.ReadResultData(n.data[off:int(e)]), fuse.OK
}
Пример #4
0
Файл: fs.go Проект: hanwen/gitfs
func (f *memoryFile) Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status) {
	b := f.blob.Contents()
	end := off + int64(len(dest))
	if end > int64(len(b)) {
		end = int64(len(b))
	}
	return fuse.ReadResultData(b[off:end]), fuse.OK
}
Пример #5
0
func (f *MutableDataFile) Read(buf []byte, off int64) (fuse.ReadResult, fuse.Status) {
	end := int(off) + len(buf)
	if end > len(f.data) {
		end = len(f.data)
	}

	return fuse.ReadResultData(f.data[off:end]), fuse.OK
}
Пример #6
0
func (f *dataFile) Read(buf []byte, off int64) (res fuse.ReadResult, code fuse.Status) {
	end := int(off) + int(len(buf))
	if end > len(f.data) {
		end = len(f.data)
	}

	return fuse.ReadResultData(f.data[off:end]), fuse.OK
}
Пример #7
0
Файл: main.go Проект: c-fs/cfuse
func (f *CFile) Read(buf []byte, off int64) (res fuse.ReadResult, code fuse.Status) {
	log.Printf("Reading data from %s, len: %v", f.name, len(buf))
	l, buf, _, err := f.client.Read(ctx.TODO(), f.name, off, int64(len(buf)), 0)
	if err != nil {
		log.Fatalf("Failed to read")
	}
	log.Printf("Read Data from %s %v, %v", f.name, buf, l)
	return fuse.ReadResultData(buf), fuse.OK
}
Пример #8
0
func (m *MFile) Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status) {
	m.obj.Lock()
	defer m.obj.Unlock()
	n, e := m.file.ReadAt(dest, off)
	if e == io.EOF {
		e = nil
	}
	return fuse.ReadResultData(dest[:n]), fuse.ToStatus(e)
}
Пример #9
0
// Read - FUSE call
func (f *virtualFile) Read(buf []byte, off int64) (resultData fuse.ReadResult, status fuse.Status) {
	if off >= int64(len(f.content)) {
		return nil, fuse.OK
	}
	end := int(off) + len(buf)
	if end > len(f.content) {
		end = len(f.content)
	}
	return fuse.ReadResultData(f.content[off:end]), fuse.OK
}
Пример #10
0
func (node *inMemNode) Read(file nodefs.File, dest []byte, off int64, context *fuse.Context) (fuse.ReadResult, fuse.Status) {
	if int(off) > len(node.contents) {
		return nil, fuse.EIO
	}
	if len(dest) > len(node.contents)-int(off) {
		dest = dest[:len(node.contents)-int(off)]
	}
	copy(dest, node.contents[off:])
	return fuse.ReadResultData(dest), fuse.OK
}
Пример #11
0
func (f *file) Read(buf []byte, offset int64) (fuse.ReadResult, fuse.Status) {
	logrus.Debugf("Read: %s", string(f.kv.Value))

	end := int(offset) + len(buf)
	if end > len(f.kv.Value) {
		end = len(f.kv.Value)
	}

	copy(buf, f.kv.Value[offset:end])
	return fuse.ReadResultData(buf), fuse.OK
}
Пример #12
0
func (f *OssFile) Read(buf []byte, off int64) (fuse.ReadResult, fuse.Status) {
	log.Printf("Start to call read method!off:%d \n", off)
	f.lock.Lock()
	defer f.lock.Unlock()
	if f.bufFile == nil {
		f.bufFile = &BufFile{off: off, data: nil, size: int(f.size), endOff: int64(0), bucket: f.bucket,
			cacheSize: 1500000, lock: &sync.Mutex{}}
	}
	num, err := readFromBuf(f.bufFile, buf, off, f.size, f.ossClient, f.fileName, 0)

	log.Printf("Has read from Buf %d num bytes \n", num)
	if err != nil {
		return nil, fuse.EINVAL
	} else {
		if num <= len(buf) {
			readResultDatas := fuse.ReadResultData(buf[:num])
			return readResultDatas, fuse.OK
		} else {
			log.Printf("Error!!! num %d,len:%d,off:%d \n", num, len(buf), off)
			return fuse.ReadResultData(buf[:len(buf)]), fuse.OK
		}
	}
}
Пример #13
0
func (nfile nomsFile) Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status) {
	nfile.node.nLock.Lock()
	defer nfile.node.nLock.Unlock()

	file := nfile.node.inode.Get("contents")

	d.Chk.Equal(nodeType(nfile.node.inode), "File")

	ref := file.(types.Struct).Get("data").(types.Ref)
	blob := nfile.fs.ds.Database().ReadValue(ref.TargetHash()).(types.Blob)

	br := blob.Reader()

	_, err := br.Seek(off, 0)
	if err != nil {
		return nil, fuse.EIO
	}
	n, err := br.Read(dest)
	if err != nil {
		return fuse.ReadResultData(dest[:n]), fuse.EIO
	}

	return fuse.ReadResultData(dest[:n]), fuse.OK
}
Пример #14
0
func (f *etcdFile) Read(buf []byte, off int64) (fuse.ReadResult, fuse.Status) {
	res, err := f.etcdClient.Get(f.path, false, false)

	if err != nil {
		log.Println("Error:", err)
		return nil, fuse.EIO
	}

	end := int(off) + int(len(buf))
	if end > len(res.Node.Value) {
		end = len(res.Node.Value)
	}

	data := []byte(res.Node.Value)
	return fuse.ReadResultData(data[off:end]), fuse.OK
}
Пример #15
0
func (f *androidFile) Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status) {
	if off > f.node.Size {
		// ENXIO = no such address.
		return nil, fuse.Status(int(syscall.ENXIO))
	}

	if off+int64(len(dest)) > f.node.Size {
		dest = dest[:f.node.Size-off]
	}
	b := bytes.NewBuffer(dest[:0])
	err := f.node.fs.dev.AndroidGetPartialObject64(f.node.Handle(), b, off, uint32(len(dest)))
	if err != nil {
		log.Println("AndroidGetPartialObject64 failed:", err)
		return nil, fuse.EIO
	}

	return fuse.ReadResultData(dest[:b.Len()]), fuse.OK
}
Пример #16
0
func (constor *Constor) Read(input *fuse.ReadIn, buf []byte) (fuse.ReadResult, fuse.Status) {
	constor.log("%d", input.Fh)
	ptr := uintptr(input.Fh)
	inode, err := constor.inodemap.findInode(input.NodeId)
	if err != nil {
		constor.error("%s", err)
		return nil, fuse.ToStatus(err)
	}
	offset := input.Offset

	F := constor.getfd(ptr)

	if F == nil {
		constor.error("F == nil")
		return nil, fuse.EIO
	}

	if (F.layer != inode.layer) && (inode.layer == 0) {
		syscall.Close(F.fd)
		path, err := constor.dentrymap.getPath(inode.ino)
		if err != nil {
			constor.error("%s", err)
			return nil, fuse.ToStatus(err)
		}
		pathl := Path.Join(constor.layers[0] + path)
		fd, err := syscall.Open(pathl, F.flags, 0)
		if err != nil {
			constor.error("%s", err)
			return nil, fuse.ToStatus(err)
		}
		F.fd = fd
		F.layer = 0
		constor.log("reset fd for %s", path)
	}

	fd := F.fd
	_, err = syscall.Pread(fd, buf, int64(offset))
	if err != nil {
		constor.error("%s", err)
		return nil, fuse.ToStatus(err)
	}
	return fuse.ReadResultData(buf), fuse.OK
}
Пример #17
0
func (f *AdbFile) Read(buf []byte, off int64) (fuse.ReadResult, fuse.Status) {
	logEntry := f.startFileOperation("Read", formatArgsListForLog(buf, off))
	defer logEntry.FinishOperation()

	if !f.Flags.CanRead() {
		// This is not a user-permission denial, it's a filesystem config denial, so don't use EACCES.
		return readError(ErrNotPermitted, logEntry)
	}

	n, err := f.FileBuffer.ReadAt(buf, off)
	if err == io.EOF {
		err = nil
	}
	if err != nil {
		return readError(err, logEntry)
	}

	logEntry.Result("read %d bytes", n)
	return fuse.ReadResultData(buf[:n]), toFuseStatusLog(OK, logEntry)
}
Пример #18
0
// Read - FUSE call
func (f *file) Read(buf []byte, off int64) (resultData fuse.ReadResult, code fuse.Status) {
	cryptfs.Debug.Printf("ino%d: FUSE Read: offset=%d length=%d\n", f.ino, len(buf), off)

	if f.writeOnly {
		cryptfs.Warn.Printf("ino%d: Tried to read from write-only file\n", f.ino)
		return nil, fuse.EBADF
	}

	out, status := f.doRead(uint64(off), uint64(len(buf)))

	if status == fuse.EIO {
		cryptfs.Warn.Printf("ino%d: Read failed with EIO, offset=%d, length=%d\n", f.ino, len(buf), off)
	}
	if status != fuse.OK {
		return nil, status
	}

	cryptfs.Debug.Printf("ino%d: Read: status %v, returning %d bytes\n", f.ino, status, len(out))
	return fuse.ReadResultData(out), status
}
Пример #19
0
func (constor *Constor) Read(input *fuse.ReadIn, buf []byte) (fuse.ReadResult, fuse.Status) {
	constor.log("%d %d", input.Fh, len(buf))
	ptr := uintptr(input.Fh)
	inode := constor.inodemap.findInodePtr(input.NodeId)
	if inode == nil {
		constor.error("inode == nil")
		return nil, fuse.ENOENT
	}
	offset := input.Offset

	F := constor.getfd(ptr)

	if F == nil {
		constor.error("F == nil")
		return nil, fuse.EIO
	}

	if (F.layer != inode.layer) && (inode.layer == 0) {
		syscall.Close(F.fd)
		path := constor.getPath(0, inode.id)
		fd, err := syscall.Open(path, F.flags, 0)
		if err != nil {
			constor.error("open failed %s : %s", path, err)
			return nil, fuse.ToStatus(err)
		}
		F.fd = fd
		F.layer = 0
		constor.log("reset fd for %s", path)
	}
	if (F.layer != inode.layer) && (inode.layer >= 0) {
		constor.error("%s : %d", F.id, inode.layer)
		return nil, fuse.EBADF
	}
	fd := F.fd
	n, err := syscall.Pread(fd, buf, int64(offset))
	if err != nil && err != io.EOF {
		constor.error("%s", err)
		return nil, fuse.ToStatus(err)
	}
	return fuse.ReadResultData(buf[:n]), fuse.OK
}
Пример #20
0
// Read - FUSE call
func (rf *reverseFile) Read(buf []byte, ioff int64) (resultData fuse.ReadResult, status fuse.Status) {
	length := uint64(len(buf))
	off := uint64(ioff)
	var out bytes.Buffer
	var header []byte

	// Synthesize file header
	if off < contentenc.HeaderLen {
		header = rf.header.Pack()
		// Truncate to requested part
		end := int(off) + len(buf)
		if end > len(header) {
			end = len(header)
		}
		header = header[off:end]
		// Write into output buffer and adjust offsets
		out.Write(header)
		hLen := uint64(len(header))
		off += hLen
		length -= hLen
	}

	// Read actual file data
	if length > 0 {
		fileData, err := rf.readBackingFile(off, length)
		if err != nil {
			return nil, fuse.ToStatus(err)
		}
		if len(fileData) == 0 {
			// If we could not read any actual data, we also don't want to
			// return the file header. An empty file stays empty in encrypted
			// form.
			return nil, fuse.OK
		}
		out.Write(fileData)
	}

	return fuse.ReadResultData(out.Bytes()), fuse.OK
}
Пример #21
0
// Read - FUSE call
func (f *file) Read(buf []byte, off int64) (resultData fuse.ReadResult, code fuse.Status) {
	f.fdLock.RLock()
	defer f.fdLock.RUnlock()

	tlog.Debug.Printf("ino%d: FUSE Read: offset=%d length=%d", f.devIno.ino, len(buf), off)

	if f.writeOnly {
		tlog.Warn.Printf("ino%d: Tried to read from write-only file", f.devIno.ino)
		return nil, fuse.EBADF
	}

	out, status := f.doRead(uint64(off), uint64(len(buf)))

	if status == fuse.EIO {
		tlog.Warn.Printf("ino%d: Read: returning EIO, offset=%d, length=%d", f.devIno.ino, len(buf), off)
	}
	if status != fuse.OK {
		return nil, status
	}

	tlog.Debug.Printf("ino%d: Read: status %v, returning %d bytes", f.devIno.ino, status, len(out))
	return fuse.ReadResultData(out), status
}
Пример #22
0
func (f *devNullFile) Read(buf []byte, off int64) (fuse.ReadResult, fuse.Status) {
	return fuse.ReadResultData(nil), fuse.OK
}
Пример #23
0
func (f *FileObj) Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status) {
	n, e := f.Data.ReadAt(dest, off)
	return fuse.ReadResultData(dest[:n]), fuse.ToStatus(e)
}