9
9
package mysql
10
10
11
11
import (
12
- "database/sql/driver"
13
12
"fmt"
14
13
"io"
15
14
"os"
@@ -86,6 +85,16 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
86
85
rdr = handler ()
87
86
if rdr != nil {
88
87
data = make ([]byte , 4 + mc .maxWriteSize )
88
+
89
+ if rdc , ok := rdr .(io.ReadCloser ); ok {
90
+ defer func () {
91
+ if err == nil {
92
+ err = rdc .Close ()
93
+ } else {
94
+ rdc .Close ()
95
+ }
96
+ }()
97
+ }
89
98
} else {
90
99
err = fmt .Errorf ("Reader '%s' is <nil>" , name )
91
100
}
@@ -99,6 +108,15 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
99
108
var fi os.FileInfo
100
109
101
110
if file , err = os .Open (name ); err == nil {
111
+ defer func () {
112
+ if err == nil {
113
+ err = file .Close ()
114
+ } else {
115
+ file .Close ()
116
+ }
117
+ }()
118
+
119
+ // get file size
102
120
if fi , err = file .Stat (); err == nil {
103
121
rdr = file
104
122
if fileSize := int (fi .Size ()); fileSize <= mc .maxWriteSize {
@@ -115,45 +133,28 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
115
133
}
116
134
}
117
135
118
- if rdc , ok := rdr .(io.ReadCloser ); ok {
119
- defer func () {
120
- if err == nil {
121
- err = rdc .Close ()
122
- } else {
123
- rdc .Close ()
124
- }
125
- }()
126
- }
127
-
128
136
// send content packets
129
- var ioErr error
130
137
if err == nil {
131
138
var n int
132
- for err == nil && ioErr == nil {
139
+ for err == nil {
133
140
n , err = rdr .Read (data [4 :])
134
141
if n > 0 {
135
- ioErr = mc .writePacket (data [:4 + n ])
142
+ if ioErr := mc .writePacket (data [:4 + n ]); ioErr != nil {
143
+ return ioErr
144
+ }
136
145
}
137
146
}
138
147
if err == io .EOF {
139
148
err = nil
140
149
}
141
- if ioErr != nil {
142
- errLog .Print (ioErr .Error ())
143
- return driver .ErrBadConn
144
- }
145
150
}
146
151
147
152
// send empty packet (termination)
148
- ioErr = mc .writePacket ([]byte {
149
- 0x00 ,
150
- 0x00 ,
151
- 0x00 ,
152
- mc .sequence ,
153
- })
154
- if ioErr != nil {
155
- errLog .Print (ioErr .Error ())
156
- return driver .ErrBadConn
153
+ if data == nil {
154
+ data = make ([]byte , 4 )
155
+ }
156
+ if ioErr := mc .writePacket (data [:4 ]); ioErr != nil {
157
+ return ioErr
157
158
}
158
159
159
160
// read OK packet
0 commit comments