infile.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "fmt"
  11. "io"
  12. "os"
  13. "strings"
  14. )
  15. var (
  16. fileRegister map[string]bool
  17. readerRegister map[string]func() io.Reader
  18. )
  19. // RegisterLocalFile adds the given file to the file whitelist,
  20. // so that it can be used by "LOAD DATA LOCAL INFILE <filepath>".
  21. // Alternatively you can allow the use of all local files with
  22. // the DSN parameter 'allowAllFiles=true'
  23. //
  24. // filePath := "/home/gopher/data.csv"
  25. // mysql.RegisterLocalFile(filePath)
  26. // err := db.Exec("LOAD DATA LOCAL INFILE '" + filePath + "' INTO TABLE foo")
  27. // if err != nil {
  28. // ...
  29. //
  30. func RegisterLocalFile(filePath string) {
  31. // lazy map init
  32. if fileRegister == nil {
  33. fileRegister = make(map[string]bool)
  34. }
  35. fileRegister[strings.Trim(filePath, `"`)] = true
  36. }
  37. // DeregisterLocalFile removes the given filepath from the whitelist.
  38. func DeregisterLocalFile(filePath string) {
  39. delete(fileRegister, strings.Trim(filePath, `"`))
  40. }
  41. // RegisterReaderHandler registers a handler function which is used
  42. // to receive a io.Reader.
  43. // The Reader can be used by "LOAD DATA LOCAL INFILE Reader::<name>".
  44. // If the handler returns a io.ReadCloser Close() is called when the
  45. // request is finished.
  46. //
  47. // mysql.RegisterReaderHandler("data", func() io.Reader {
  48. // var csvReader io.Reader // Some Reader that returns CSV data
  49. // ... // Open Reader here
  50. // return csvReader
  51. // })
  52. // err := db.Exec("LOAD DATA LOCAL INFILE 'Reader::data' INTO TABLE foo")
  53. // if err != nil {
  54. // ...
  55. //
  56. func RegisterReaderHandler(name string, handler func() io.Reader) {
  57. // lazy map init
  58. if readerRegister == nil {
  59. readerRegister = make(map[string]func() io.Reader)
  60. }
  61. readerRegister[name] = handler
  62. }
  63. // DeregisterReaderHandler removes the ReaderHandler function with
  64. // the given name from the registry.
  65. func DeregisterReaderHandler(name string) {
  66. delete(readerRegister, name)
  67. }
  68. func deferredClose(err *error, closer io.Closer) {
  69. closeErr := closer.Close()
  70. if *err == nil {
  71. *err = closeErr
  72. }
  73. }
  74. func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
  75. var rdr io.Reader
  76. var data []byte
  77. if strings.HasPrefix(name, "Reader::") { // io.Reader
  78. name = name[8:]
  79. if handler, inMap := readerRegister[name]; inMap {
  80. rdr = handler()
  81. if rdr != nil {
  82. data = make([]byte, 4+mc.maxWriteSize)
  83. if cl, ok := rdr.(io.Closer); ok {
  84. defer deferredClose(&err, cl)
  85. }
  86. } else {
  87. err = fmt.Errorf("Reader '%s' is <nil>", name)
  88. }
  89. } else {
  90. err = fmt.Errorf("Reader '%s' is not registered", name)
  91. }
  92. } else { // File
  93. name = strings.Trim(name, `"`)
  94. if mc.cfg.allowAllFiles || fileRegister[name] {
  95. var file *os.File
  96. var fi os.FileInfo
  97. if file, err = os.Open(name); err == nil {
  98. defer deferredClose(&err, file)
  99. // get file size
  100. if fi, err = file.Stat(); err == nil {
  101. rdr = file
  102. if fileSize := int(fi.Size()); fileSize <= mc.maxWriteSize {
  103. data = make([]byte, 4+fileSize)
  104. } else if fileSize <= mc.maxPacketAllowed {
  105. data = make([]byte, 4+mc.maxWriteSize)
  106. } else {
  107. err = fmt.Errorf("Local File '%s' too large: Size: %d, Max: %d", name, fileSize, mc.maxPacketAllowed)
  108. }
  109. }
  110. }
  111. } else {
  112. err = fmt.Errorf("Local File '%s' is not registered. Use the DSN parameter 'allowAllFiles=true' to allow all files", name)
  113. }
  114. }
  115. // send content packets
  116. if err == nil {
  117. var n int
  118. for err == nil {
  119. n, err = rdr.Read(data[4:])
  120. if n > 0 {
  121. if ioErr := mc.writePacket(data[:4+n]); ioErr != nil {
  122. return ioErr
  123. }
  124. }
  125. }
  126. if err == io.EOF {
  127. err = nil
  128. }
  129. }
  130. // send empty packet (termination)
  131. if data == nil {
  132. data = make([]byte, 4)
  133. }
  134. if ioErr := mc.writePacket(data[:4]); ioErr != nil {
  135. return ioErr
  136. }
  137. // read OK packet
  138. if err == nil {
  139. return mc.readResultOK()
  140. } else {
  141. mc.readPacket()
  142. }
  143. return err
  144. }