-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfile.go
65 lines (57 loc) · 1.84 KB
/
file.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
65
// Package styxfile provides helper routines and interfaces
// for serving 9P files from Go types.
package styxfile
import (
"errors"
"fmt"
"io"
)
// 9P read/write requests contain an offset. This makes them
// well-suited to the io.ReaderAt and io.WriterAt interfaces.
// However to keep our APIs flexible, we do the heavy lifting,
// accepting broader interfaces and forcing them into what we
// need.
// ErrNotSupported is returned when a given type does not
// implement the necessary functionality to complete a given
// read/write operation.
var ErrNotSupported = errors.New("not supported")
// Interface describes the methods a type must implement to
// be used as a file by a 9P file server. The New function converts
// types that implement some, but not all of these methods into
// types that do.
type Interface interface {
io.ReaderAt
io.WriterAt
io.Closer
}
type interfaceWithoutClose interface {
io.ReaderAt
io.WriterAt
}
type nopCloser struct {
interfaceWithoutClose
}
func (nopCloser) Close() error { return nil }
// New creates a new Interface that reads and writes to and from
// rwc. The type of rwc determines the implementation selected
// by New; if rwc already implements Interface, it is used as-is. If
// some methods are missing, wrapper types are used to implement
// missing functionality. If the provided type cannot be adapted into
// an Interface, New returns a non-nil error.
func New(rwc interface{}) (Interface, error) {
switch rwc := rwc.(type) {
case Interface:
return rwc, nil
case interfaceWithoutClose:
return nopCloser{rwc}, nil
case io.Seeker:
return &seekerAt{rwc: rwc}, nil
case io.ReadWriter:
return &dumbPipe{rwc: rwc}, nil
case io.Reader:
return &dumbPipe{rwc: rwc}, nil
case io.Writer:
return &dumbPipe{rwc: rwc}, nil
}
return nil, fmt.Errorf("Cannot convert type %T into a styxfile.Interface")
}