MCPcopy
hub / github.com/rclone/rclone / Open

Method Open

fs/operations/reopen_test.go:42–86  ·  view source on GitHub ↗

Open opens the file for read. Call Close() on the returned io.ReadCloser This will break after reading the number of bytes in breaks

(ctx context.Context, options ...fs.OpenOption)

Source from the content-addressed store, hash-verified

40//
41// This will break after reading the number of bytes in breaks
42func (o *reOpenTestObject) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadCloser, error) {
43 // Lots of backends do this - make sure it works as it modifies options
44 fs.FixRangeOption(options, o.Size())
45 gotHash := false
46 gotRange := false
47 startPos := int64(0)
48 for _, option := range options {
49 switch x := option.(type) {
50 case *fs.HashesOption:
51 gotHash = true
52 case *fs.RangeOption:
53 gotRange = true
54 startPos = x.Start
55 if o.unknownSize {
56 assert.Equal(o.t, int64(-1), x.End)
57 }
58 case *fs.SeekOption:
59 startPos = x.Offset
60 }
61 }
62 assert.Equal(o.t, o.wantStart, startPos)
63 // Check if ranging, mustn't have hash if offset != 0
64 if gotHash && gotRange {
65 assert.Equal(o.t, int64(0), startPos)
66 }
67 rc, err := o.Object.Open(ctx, options...)
68 if err != nil {
69 return nil, err
70 }
71 if len(o.breaks) > 0 {
72 // Pop a breakpoint off
73 N := o.breaks[0]
74 o.breaks = o.breaks[1:]
75 o.wantStart += N
76 // If 0 then return an error immediately
77 if N == 0 {
78 return nil, errorTestError
79 }
80 // Read N bytes then an error
81 r := io.MultiReader(&io.LimitedReader{R: rc, N: N}, readers.ErrorReader{Err: errorTestError})
82 // Wrap with Close in a new readCloser
83 rc = readCloser{Reader: r, Closer: rc}
84 }
85 return rc, nil
86}
87
88func TestReOpen(t *testing.T) {
89 for _, testName := range []string{"Normal", "WithRangeOption", "WithSeekOption", "UnknownSize"} {

Callers

nothing calls this directly

Calls 4

FixRangeOptionFunction · 0.92
SizeMethod · 0.65
OpenMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected