-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.go
More file actions
282 lines (261 loc) · 10.1 KB
/
Copy pathio.go
File metadata and controls
282 lines (261 loc) · 10.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package simplecsv
import (
"encoding/csv"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// SimpleCsv is the type for simple csv
type SimpleCsv [][]string
// CreateEmptyCsv creates an empty CSV with the headers passed as a slice.
// The headers are copied, changes to the original slice don't affect the csv.
// At least one header is required. Header names must be unique, like database
// columns: if columnNames is empty or contains duplicates the returned error
// is non-nil and the returned csv is nil.
func CreateEmptyCsv(columnNames []string) (SimpleCsv, error) {
if len(columnNames) == 0 {
return nil, fmt.Errorf("simplecsv: at least one header is required")
}
if hasDuplicateHeaders(columnNames) {
return nil, fmt.Errorf("simplecsv: duplicate header names are not allowed: %v", columnNames)
}
a := make([][]string, 1)
a[0] = copyRow(columnNames)
return a, nil
}
// CreateEmpyCsv creates an empty CSV with the headers passed as a slice
//
// Deprecated: use CreateEmptyCsv, this function was kept for compatibility
// with the misspelled name
func CreateEmpyCsv(columnNames []string) (SimpleCsv, error) {
return CreateEmptyCsv(columnNames)
}
// MustCreateEmptyCsv is like CreateEmptyCsv but panics if the headers are
// invalid (empty or duplicated). It is handy in tests and small scripts
// where the header list is part of the program, not a runtime condition:
//
// people := simplecsv.MustCreateEmptyCsv([]string{"id", "name"})
func MustCreateEmptyCsv(headers []string) SimpleCsv {
s, err := CreateEmptyCsv(headers)
if err != nil {
panic(err)
}
return s
}
// ReadCsvFile reads a file and returns a SimpleCsv.
// The second value is false if there's an error reading the file or parsing the CSV.
func ReadCsvFile(filename string) (SimpleCsv, bool) {
s, err := ReadCsvFileE(filename)
return s, err == nil
}
// ReadCsvFileE reads a file and returns a SimpleCsv or an error.
func ReadCsvFileE(filename string) (SimpleCsv, error) {
return ReadCsvFileComma(filename, ',')
}
// ReadCsvFileComma reads a delimited file (for example a semicolon or tab
// separated file) and returns a SimpleCsv or an error.
func ReadCsvFileComma(filename string, comma rune) (SimpleCsv, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("simplecsv: cannot open %q: %w", filename, err)
}
defer file.Close()
return ReadCsv(file, comma)
}
// ReadCsv reads all the records from r using comma as the field separator
// and returns a SimpleCsv or an error.
// The entire input is read into memory; use ReadCsvLimit for bounded reads.
//
// The first record is treated as the header row. Header names must be unique,
// like database columns: a file with duplicate headers in the first row is
// rejected with an error. All rows must have the same number of fields as the
// header row: a ragged file is rejected with an error. This makes uniform row
// width an invariant of a SimpleCsv, alongside the unique-headers invariant.
//
// A UTF-8 byte order mark (BOM, written by Excel and many Windows tools at
// the start of the file) is stripped from the first cell of the first row, so
// the first header is "id" and not "\ufeffid".
func ReadCsv(r io.Reader, comma rune) (SimpleCsv, error) {
reader := csv.NewReader(r)
reader.Comma = comma
// FieldsPerRecord 0 (the encoding/csv default) records the field count of
// the first record and rejects any subsequent record with a different field
// count. Set it explicitly: ragged files are rejected, never accepted.
reader.FieldsPerRecord = 0
allRecords, err := reader.ReadAll()
if err != nil {
return nil, fmt.Errorf("simplecsv: cannot parse csv: %w", err)
}
if len(allRecords) == 0 {
return allRecords, nil
}
stripBOM(allRecords)
if hasDuplicateHeaders(allRecords[0]) {
return nil, fmt.Errorf("simplecsv: duplicate header names are not allowed: %v", allRecords[0])
}
return allRecords, nil
}
// ReadCsvLimit reads records from r using comma as the field separator and
// returns a SimpleCsv or an error. maxRecords is the maximum number of CSV
// records, including the header row, and maxBytes is the maximum number of
// input bytes passed to the CSV parser. A zero limit disables that limit;
// negative limits are rejected. If a limit is exceeded, the returned csv is
// nil.
//
// Unlike ReadCsv, this function does not use csv.Reader.ReadAll. Use positive
// limits when reading input that may be large or attacker-controlled.
//
// Like ReadCsv, a UTF-8 byte order mark (BOM) is stripped from the first
// cell of the first row if present.
func ReadCsvLimit(r io.Reader, comma rune, maxRecords, maxBytes int64) (SimpleCsv, error) {
if maxRecords < 0 || maxBytes < 0 {
return nil, fmt.Errorf("simplecsv: read limits must not be negative")
}
input := io.Reader(r)
var byteLimit *io.LimitedReader
if maxBytes > 0 {
byteLimit = &io.LimitedReader{R: r, N: maxBytes}
input = byteLimit
}
reader := csv.NewReader(input)
reader.Comma = comma
// Keep the same uniform-width invariant as ReadCsv.
reader.FieldsPerRecord = 0
records := make(SimpleCsv, 0)
for {
if maxRecords > 0 && int64(len(records)) >= maxRecords {
_, err := reader.Read()
if err == nil {
return nil, fmt.Errorf("simplecsv: csv exceeds maximum of %d records", maxRecords)
}
if err == io.EOF {
if exceeded, limitErr := csvByteLimitStatus(byteLimit, r); limitErr != nil {
return nil, limitErr
} else if exceeded {
return nil, fmt.Errorf("simplecsv: csv exceeds maximum of %d bytes", maxBytes)
}
break
}
if exceeded, limitErr := csvByteLimitStatus(byteLimit, r); limitErr != nil {
return nil, limitErr
} else if exceeded {
return nil, fmt.Errorf("simplecsv: csv exceeds maximum of %d bytes", maxBytes)
}
return nil, fmt.Errorf("simplecsv: cannot parse csv: %w", err)
}
record, err := reader.Read()
if err == io.EOF {
if exceeded, limitErr := csvByteLimitStatus(byteLimit, r); limitErr != nil {
return nil, limitErr
} else if exceeded {
return nil, fmt.Errorf("simplecsv: csv exceeds maximum of %d bytes", maxBytes)
}
break
}
if err != nil {
if exceeded, limitErr := csvByteLimitStatus(byteLimit, r); limitErr != nil {
return nil, limitErr
} else if exceeded {
return nil, fmt.Errorf("simplecsv: csv exceeds maximum of %d bytes", maxBytes)
}
return nil, fmt.Errorf("simplecsv: cannot parse csv: %w", err)
}
records = append(records, record)
}
if len(records) == 0 {
return records, nil
}
stripBOM(records)
if hasDuplicateHeaders(records[0]) {
return nil, fmt.Errorf("simplecsv: duplicate header names are not allowed: %v", records[0])
}
return records, nil
}
// stripBOM removes a UTF-8 byte order mark from the first cell of the first
// row if present. Excel and many Windows tools write one at the start of the
// file; without stripping, the first header becomes "\ufeffid" and every
// name-based lookup silently fails. Only the first cell of the first row is
// touched: a BOM elsewhere in the file is left verbatim.
func stripBOM(records SimpleCsv) {
if len(records) == 0 || len(records[0]) == 0 {
return
}
records[0][0] = strings.TrimPrefix(records[0][0], "\ufeff")
}
// csvByteLimitStatus checks whether the limited reader stopped at its byte
// limit rather than because the source reached EOF. The one-byte probe is
// necessary because io.LimitedReader cannot distinguish those cases itself.
func csvByteLimitStatus(limit *io.LimitedReader, source io.Reader) (bool, error) {
if limit == nil || limit.N > 0 {
return false, nil
}
var probe [1]byte
n, err := source.Read(probe[:])
if n > 0 {
return true, nil
}
if err == nil || err == io.EOF {
return false, nil
}
return false, fmt.Errorf("simplecsv: cannot read csv: %w", err)
}
// WriteCsvFile writes the SimpleCsv to a file.
// Returns false if there's an error creating or writing the file.
func (s SimpleCsv) WriteCsvFile(filename string) bool {
return s.WriteCsvFileE(filename) == nil
}
// WriteCsvFileE writes the SimpleCsv to a file and returns an error on failure.
func (s SimpleCsv) WriteCsvFileE(filename string) error {
return s.WriteCsvFileComma(filename, ',')
}
// WriteCsvFileComma writes the SimpleCsv to a file using comma as the field
// separator and returns an error on failure.
//
// The write is atomic: the csv is first written to a temporary file in the
// same directory as filename and then renamed over it, so a failed or
// interrupted write cannot truncate or corrupt the destination. The temporary
// file (and therefore the destination after the rename) is created with mode
// 0600 (owner-readable only), so written files are not world-readable by
// default; callers that need different permissions can os.Chmod the result.
// Because the destination is replaced with os.Rename rather than opened, a
// symlink planted at filename is replaced instead of followed: the write does
// not follow a symlink at the destination.
func (s SimpleCsv) WriteCsvFileComma(filename string, comma rune) error {
dir := filepath.Dir(filename)
tmp, err := os.CreateTemp(dir, ".simplecsv-*")
if err != nil {
return fmt.Errorf("simplecsv: cannot create temp file for %q: %w", filename, err)
}
tmpName := tmp.Name()
// os.CreateTemp opens with 0600 on Unix; chmod explicitly so the final
// file is owner-readable only by default regardless of CreateTemp's mode.
defer os.Remove(tmpName) // no-op once the temp has been renamed
if cerr := os.Chmod(tmpName, 0600); cerr != nil {
tmp.Close()
return fmt.Errorf("simplecsv: cannot set permissions on temp file for %q: %w", filename, cerr)
}
werr := s.WriteTo(tmp, comma)
cerr := tmp.Close()
if werr != nil {
return fmt.Errorf("simplecsv: cannot write %q: %w", filename, werr)
}
if cerr != nil {
return fmt.Errorf("simplecsv: cannot close temp file for %q: %w", filename, cerr)
}
if rerr := os.Rename(tmpName, filename); rerr != nil {
return fmt.Errorf("simplecsv: cannot rename temp file to %q: %w", filename, rerr)
}
return nil
}
// WriteTo writes the SimpleCsv to w using comma as the field separator
// and returns an error on failure.
func (s SimpleCsv) WriteTo(w io.Writer, comma rune) error {
cw := csv.NewWriter(w)
cw.Comma = comma
if err := cw.WriteAll(s); err != nil {
return fmt.Errorf("simplecsv: cannot write csv: %w", err)
}
return nil
}