Skip to content

Commit d503c4b

Browse files
committed
infile: better buffer size calculation
1 parent a9d1410 commit d503c4b

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

infile.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,38 @@ func DeregisterReaderHandler(name string) {
7878

7979
func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
8080
var rdr io.Reader
81-
data := make([]byte, 4+mc.maxWriteSize)
81+
var data []byte
8282

8383
if strings.HasPrefix(name, "Reader::") { // io.Reader
8484
name = name[8:]
85-
handler, inMap := readerRegister[name]
86-
if handler != nil {
85+
if handler, inMap := readerRegister[name]; inMap {
8786
rdr = handler()
88-
}
89-
if rdr == nil {
90-
if !inMap {
91-
err = fmt.Errorf("Reader '%s' is not registered", name)
87+
if rdr != nil {
88+
data = make([]byte, 4+mc.maxWriteSize)
9289
} else {
9390
err = fmt.Errorf("Reader '%s' is <nil>", name)
9491
}
92+
} else {
93+
err = fmt.Errorf("Reader '%s' is not registered", name)
9594
}
9695
} else { // File
9796
name = strings.Trim(name, `"`)
9897
if mc.cfg.allowAllFiles || fileRegister[name] {
99-
rdr, err = os.Open(name)
98+
var file *os.File
99+
var fi os.FileInfo
100+
101+
if file, err = os.Open(name); err == nil {
102+
if fi, err = file.Stat(); err == nil {
103+
rdr = file
104+
if fileSize := int(fi.Size()); fileSize <= mc.maxWriteSize {
105+
data = make([]byte, 4+fileSize)
106+
} else if fileSize <= mc.maxPacketAllowed {
107+
data = make([]byte, 4+mc.maxWriteSize)
108+
} else {
109+
err = fmt.Errorf("Local File '%s' too large: Size: %d, Max: %d", name, fileSize, mc.maxPacketAllowed)
110+
}
111+
}
112+
}
100113
} else {
101114
err = fmt.Errorf("Local File '%s' is not registered. Use the DSN parameter 'allowAllFiles=true' to allow all files", name)
102115
}

0 commit comments

Comments
 (0)