forked from microsoft/hdfs-mount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHandleAsReadSeekCloser.go
48 lines (40 loc) · 1.47 KB
/
FileHandleAsReadSeekCloser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package main
import (
"bazil.org/fuse"
)
// Wraps FileHandle exposing it as ReadSeekCloser intrface
// Concurrency: not thread safe: at most on request at a time
type FileHandleAsReadSeekCloser struct {
FileHandle *FileHandle
Offset int64
}
// Verify that *FileHandleAsReadSeekCloser implements ReadSeekCloser
var _ ReadSeekCloser = (*FileHandleAsReadSeekCloser)(nil)
// Creates new adapter
func NewFileHandleAsReadSeekCloser(fileHandle *FileHandle) ReadSeekCloser {
return &FileHandleAsReadSeekCloser{FileHandle: fileHandle}
}
// Reads a chunk of data
func (this *FileHandleAsReadSeekCloser) Read(buffer []byte) (int, error) {
resp := fuse.ReadResponse{Data: buffer}
err := this.FileHandle.Read(nil, &fuse.ReadRequest{Offset: this.Offset, Size: len(buffer)}, &resp)
this.Offset += int64(len(resp.Data))
return len(resp.Data), err
}
// Seeks to a given position
func (this *FileHandleAsReadSeekCloser) Seek(pos int64) error {
// Note: seek is implemented as virtual operation, error checking will happen
// when a Read() is called after a problematic Seek()
this.Offset = pos
return nil
}
// Returns reading position
func (this *FileHandleAsReadSeekCloser) Position() (int64, error) {
return this.Offset, nil
}
// Closes the underlying file handle
func (this *FileHandleAsReadSeekCloser) Close() error {
return this.FileHandle.Release(nil, nil)
}