File: stream.go

package info (click to toggle)
relic 7.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,108 kB
  • sloc: sh: 230; makefile: 10
file content (163 lines) | stat: -rw-r--r-- 4,315 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// Copyright (c) SAS Institute Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package comdoc

import (
	"errors"
	"fmt"
	"io"
)

type streamReader struct {
	remaining  uint32
	nextSector SecID
	sat        []SecID
	sectorSize int
	readSector func(SecID, []byte) (int, error)
	buf, saved []byte
}

// Open a stream for reading
func (r *ComDoc) ReadStream(e *DirEnt) (io.Reader, error) {
	if e.Type != DirStream {
		return nil, errors.New("not a stream")
	}
	sr := &streamReader{
		remaining:  e.StreamSize,
		nextSector: e.NextSector,
	}
	if e.StreamSize < r.Header.MinStdStreamSize {
		sr.sectorSize = r.ShortSectorSize
		sr.sat = r.SSAT
		sr.readSector = r.readShortSector
	} else {
		sr.sectorSize = r.SectorSize
		sr.sat = r.SAT
		sr.readSector = r.readSector
	}
	sr.buf = make([]byte, sr.sectorSize)
	return sr, nil
}

func (sr *streamReader) Read(d []byte) (copied int, err error) {
	if sr.remaining == 0 {
		return 0, io.EOF
	} else if len(d) == 0 {
		return 0, nil
	}
	if int64(len(d)) > int64(sr.remaining) {
		d = d[:int(sr.remaining)]
	}
	// read from previously buffered sector
	if len(sr.saved) > 0 {
		n := copy(d, sr.saved)
		d = d[n:]
		sr.saved = sr.saved[n:]
		copied += n
		sr.remaining -= uint32(n)
	}
	// read whole sectors
	for len(d) >= sr.sectorSize {
		if sr.nextSector < 0 {
			return copied, errors.New("unexpected end to stream")
		}
		n, err := sr.readSector(sr.nextSector, d[:sr.sectorSize])
		if n > 0 {
			d = d[n:]
			copied += n
			sr.remaining -= uint32(n)
		}
		if err != io.EOF && err != nil {
			return copied, err
		} else if n < sr.sectorSize && sr.remaining > 0 {
			return copied, fmt.Errorf("short read of sector %d: expected %d bytes but got %d", sr.nextSector, sr.sectorSize, n)
		}
		sr.nextSector = sr.sat[sr.nextSector]
	}
	// read partial sector and buffer the rest
	if len(d) > 0 {
		if sr.nextSector < 0 {
			return copied, errors.New("unexpected end to stream")
		}
		// read the full sector
		sectorN, err := sr.readSector(sr.nextSector, sr.buf)
		if sectorN > 0 {
			// fill the rest of the result
			copyN := copy(d, sr.buf)
			copied += copyN
			sr.remaining -= uint32(copyN)
		}
		if err != io.EOF && err != nil {
			return copied, err
		} else if sectorN < sr.sectorSize && sr.remaining > 0 {
			// it's ok if the final sector is truncated if there are no more bytes in the stream
			return copied, fmt.Errorf("short read of sector %d: expected %d bytes but got %d", sr.nextSector, sr.remaining, sectorN)
		}
		// save the remainder, if anything
		sr.saved = sr.buf[len(d):]
		sr.nextSector = sr.sat[sr.nextSector]
	}
	return copied, nil
}

// Store a blob as a chain of sectors, updating the sector table (or
// short-sector table if "short" is set)  and return the first sector ID
func (r *ComDoc) addStream(contents []byte, short bool) (SecID, error) {
	var sectorSize int
	var sat, freeList []SecID
	if short {
		sectorSize = int(r.ShortSectorSize)
		needSectors := (len(contents) + sectorSize - 1) / sectorSize
		freeList = r.makeFreeSectors(needSectors, true)
		sat = r.SSAT
	} else {
		sectorSize = int(r.SectorSize)
		needSectors := (len(contents) + sectorSize - 1) / sectorSize
		freeList = r.makeFreeSectors(needSectors, false)
		sat = r.SAT
	}
	first := SecIDEndOfChain
	previous := first
	for _, i := range freeList {
		if previous == SecIDEndOfChain {
			first = i
		} else {
			sat[previous] = i
		}
		previous = i
		// write to file
		n := sectorSize
		if n > len(contents) {
			n = len(contents)
		}
		var err error
		if short {
			err = r.writeShortSector(i, contents[:n])
		} else {
			err = r.writeSector(i, contents[:n])
		}
		if err != nil {
			return 0, err
		}
		contents = contents[n:]
	}
	sat[previous] = SecIDEndOfChain
	if len(contents) > 0 {
		panic("didn't allocate enough sectors")
	}
	return first, nil
}