-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
171 lines (134 loc) · 3.21 KB
/
writer.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
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
164
165
166
167
168
169
170
171
package psx
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"sync"
)
var (
errDuplicateName = errors.New("duplicate name")
errInvalidLength = errors.New("invalid length")
errNoFreeSpace = errors.New("no free space")
)
type fileWriter struct {
buf *bytes.Buffer
w *Writer
}
func (w *fileWriter) maxSize() int {
return binary.Size(directoryFrame{}) + numBlocks*blockSize
}
func (w *fileWriter) Write(p []byte) (int, error) {
if len(p)+w.buf.Len() > w.maxSize() {
// Would exceed the maximum size
return 0, errInvalidLength
}
return w.buf.Write(p) //nolint:wrapcheck
}
//nolint:cyclop,funlen
func (w *fileWriter) Close() error {
w.w.mu.Lock()
defer w.w.mu.Unlock()
delete(w.w.fw, w)
mc := w.w.mc
df := new(directoryFrame)
if err := binary.Read(w.buf, binary.LittleEndian, df); err != nil {
return fmt.Errorf("unable to read header: %w", err)
}
for _, x := range mc.HeaderBlock.DirectoryFrame {
if !x.isFirst() {
continue
}
if x.filename() == df.filename() {
return errDuplicateName
}
}
if w.buf.Len()%blockSize != 0 || w.buf.Len() != int(df.Size) {
return errInvalidLength
}
blocks := w.buf.Len() / blockSize
if w.w.i+blocks > numBlocks {
return errNoFreeSpace
}
for i := 0; i < blocks; i++ {
frame := w.w.i + i
if i == 0 {
mc.HeaderBlock.DirectoryFrame[frame] = *df
}
lo := uint16(frame + 1)
if i+1 == blocks {
lo = lastLink
}
ab := blockMiddleLink
if i == 0 {
ab = blockFirstLink
} else if i+1 == blocks && blocks > 1 {
ab = blockLastLink
}
mc.HeaderBlock.DirectoryFrame[frame].LinkOrder = lo
mc.HeaderBlock.DirectoryFrame[frame].AvailableBlocks = ab
if _, err := io.ReadFull(w.buf, mc.DataBlock[frame][:]); err != nil {
return fmt.Errorf("unable to read data block: %w", err)
}
}
w.w.i += blocks
return mc.checksum()
}
// A Writer is used for creating a new memory card image with files written to
// it.
type Writer struct {
mu sync.Mutex
w io.Writer
mc *memoryCard
fw map[*fileWriter]struct{}
i int
}
// Create returns an io.WriteCloser for writing a new file on the memory card.
// The file should consist of a 128 byte header followed by one or more 8 KiB
// blocks as indicated in the header.
func (w *Writer) Create() (io.WriteCloser, error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.i == numBlocks {
return nil, errNoFreeSpace
}
fw := &fileWriter{new(bytes.Buffer), w}
w.fw[fw] = struct{}{}
return fw, nil
}
// Close writes out the memory card to the underlying io.Writer. Any in-flight
// open memory card files are closed first.
func (w *Writer) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
for fw := range w.fw {
if err := fw.Close(); err != nil {
return err
}
delete(w.fw, fw)
}
b, err := w.mc.MarshalBinary()
if err != nil {
return err
}
if n, err := w.w.Write(b); err != nil || n != w.mc.size() {
if err != nil {
return fmt.Errorf("unable to write memory card: %w", err)
}
return errInvalidLength
}
return nil
}
// NewWriter returns a Writer that will write a new memory card to w.
func NewWriter(w io.Writer) (*Writer, error) {
mc, err := newMemoryCard()
if err != nil {
return nil, err
}
return &Writer{
w: w,
mc: mc,
fw: make(map[*fileWriter]struct{}),
}, nil
}