This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
ZipFileHandle.go
64 lines (57 loc) · 2.2 KB
/
ZipFileHandle.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 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"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
"io"
"math/rand"
"sync"
"time"
)
// Encapsulates a file handle for a file inside a zip archive
type ZipFileHandle struct {
ContentStream io.ReadCloser
lock sync.Mutex
offset int64
}
// Ensure ZipFileHandle implements necesary fuse interface
var _ fs.Handle = (*ZipFileHandle)(nil)
var _ fs.HandleReleaser = (*ZipFileHandle)(nil)
var _ fs.HandleReader = (*ZipFileHandle)(nil)
// Creates new file handle
func NewZipFileHandle(contentStream io.ReadCloser) *ZipFileHandle {
return &ZipFileHandle{ContentStream: contentStream}
}
// Releases (closes) the handle
func (this *ZipFileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
return this.ContentStream.Close()
}
// Responds on FUSE Read request
func (this *ZipFileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
this.lock.Lock()
defer this.lock.Unlock()
for req.Offset != this.offset {
// Since file is opened in fuse.OpenNonSeekable mode, we expect kernel to issue sequential reads.
// However kernel might issue multiple read-ahead requests, one after another, but and they might be
// reordered by underlying bazil/fuse library because it fans out each request to a separate concurrent goroutine.
// If we got offset which isn't expected, this means that "wrong" goroutine grabbed the lock,
// in this case yielding for other instance of concurrent go-routine.
// This is a temporary workaround, we'll need to find better solution
// TODO: consider addressing this at bazil/fuse, by adding per-handle request serialization feature which preserves ordering
this.lock.Unlock()
time.Sleep(time.Duration(rand.Int31n(10)) * time.Millisecond)
this.lock.Lock()
}
// reading requested bytes
buffer := make([]byte, req.Size)
nr, err := io.ReadFull(this.ContentStream, buffer)
this.offset += int64(nr)
if err == io.EOF || err == io.ErrUnexpectedEOF {
// EOF isn't an error from the FUSE's point of view
err = nil
}
resp.Data = buffer[:nr]
return err
}