ftp.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // Package ftp implements a FTP client as described in RFC 959.
  2. //
  3. // A textproto.Error is returned for errors at the protocol level.
  4. package ftp
  5. import (
  6. "bufio"
  7. "context"
  8. "crypto/tls"
  9. "errors"
  10. "io"
  11. "net"
  12. "net/textproto"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // EntryType describes the different types of an Entry.
  18. type EntryType int
  19. // The differents types of an Entry
  20. const (
  21. EntryTypeFile EntryType = iota
  22. EntryTypeFolder
  23. EntryTypeLink
  24. )
  25. // ServerConn represents the connection to a remote FTP server.
  26. // A single connection only supports one in-flight data connection.
  27. // It is not safe to be called concurrently.
  28. type ServerConn struct {
  29. options *dialOptions
  30. conn *textproto.Conn
  31. host string
  32. // Server capabilities discovered at runtime
  33. features map[string]string
  34. skipEPSV bool
  35. mlstSupported bool
  36. }
  37. // DialOption represents an option to start a new connection with Dial
  38. type DialOption struct {
  39. setup func(do *dialOptions)
  40. }
  41. // dialOptions contains all the options set by DialOption.setup
  42. type dialOptions struct {
  43. context context.Context
  44. dialer net.Dialer
  45. tlsConfig *tls.Config
  46. conn net.Conn
  47. disableEPSV bool
  48. location *time.Location
  49. debugOutput io.Writer
  50. dialFunc func(network, address string) (net.Conn, error)
  51. }
  52. // Entry describes a file and is returned by List().
  53. type Entry struct {
  54. Name string
  55. Target string // target of symbolic link
  56. Type EntryType
  57. Size uint64
  58. Time time.Time
  59. }
  60. // Response represents a data-connection
  61. type Response struct {
  62. conn net.Conn
  63. c *ServerConn
  64. closed bool
  65. }
  66. // Dial connects to the specified address with optional options
  67. func Dial(addr string, options ...DialOption) (*ServerConn, error) {
  68. do := &dialOptions{}
  69. for _, option := range options {
  70. option.setup(do)
  71. }
  72. if do.location == nil {
  73. do.location = time.UTC
  74. }
  75. tconn := do.conn
  76. if tconn == nil {
  77. var err error
  78. if do.dialFunc != nil {
  79. tconn, err = do.dialFunc("tcp", addr)
  80. } else if do.tlsConfig != nil {
  81. tconn, err = tls.DialWithDialer(&do.dialer, "tcp", addr, do.tlsConfig)
  82. } else {
  83. ctx := do.context
  84. if ctx == nil {
  85. ctx = context.Background()
  86. }
  87. tconn, err = do.dialer.DialContext(ctx, "tcp", addr)
  88. }
  89. if err != nil {
  90. return nil, err
  91. }
  92. }
  93. // Use the resolved IP address in case addr contains a domain name
  94. // If we use the domain name, we might not resolve to the same IP.
  95. remoteAddr := tconn.RemoteAddr().(*net.TCPAddr)
  96. var sourceConn io.ReadWriteCloser = tconn
  97. if do.debugOutput != nil {
  98. sourceConn = newDebugWrapper(tconn, do.debugOutput)
  99. }
  100. c := &ServerConn{
  101. options: do,
  102. features: make(map[string]string),
  103. conn: textproto.NewConn(sourceConn),
  104. host: remoteAddr.IP.String(),
  105. }
  106. _, _, err := c.conn.ReadResponse(StatusReady)
  107. if err != nil {
  108. c.Quit()
  109. return nil, err
  110. }
  111. err = c.feat()
  112. if err != nil {
  113. c.Quit()
  114. return nil, err
  115. }
  116. if _, mlstSupported := c.features["MLST"]; mlstSupported {
  117. c.mlstSupported = true
  118. }
  119. return c, nil
  120. }
  121. // DialWithTimeout returns a DialOption that configures the ServerConn with specified timeout
  122. func DialWithTimeout(timeout time.Duration) DialOption {
  123. return DialOption{func(do *dialOptions) {
  124. do.dialer.Timeout = timeout
  125. }}
  126. }
  127. // DialWithDialer returns a DialOption that configures the ServerConn with specified net.Dialer
  128. func DialWithDialer(dialer net.Dialer) DialOption {
  129. return DialOption{func(do *dialOptions) {
  130. do.dialer = dialer
  131. }}
  132. }
  133. // DialWithNetConn returns a DialOption that configures the ServerConn with the underlying net.Conn
  134. func DialWithNetConn(conn net.Conn) DialOption {
  135. return DialOption{func(do *dialOptions) {
  136. do.conn = conn
  137. }}
  138. }
  139. // DialWithDisabledEPSV returns a DialOption that configures the ServerConn with EPSV disabled
  140. // Note that EPSV is only used when advertised in the server features.
  141. func DialWithDisabledEPSV(disabled bool) DialOption {
  142. return DialOption{func(do *dialOptions) {
  143. do.disableEPSV = disabled
  144. }}
  145. }
  146. // DialWithLocation returns a DialOption that configures the ServerConn with specified time.Location
  147. // The location is used to parse the dates sent by the server which are in server's timezone
  148. func DialWithLocation(location *time.Location) DialOption {
  149. return DialOption{func(do *dialOptions) {
  150. do.location = location
  151. }}
  152. }
  153. // DialWithContext returns a DialOption that configures the ServerConn with specified context
  154. // The context will be used for the initial connection setup
  155. func DialWithContext(ctx context.Context) DialOption {
  156. return DialOption{func(do *dialOptions) {
  157. do.context = ctx
  158. }}
  159. }
  160. // DialWithTLS returns a DialOption that configures the ServerConn with specified TLS config
  161. //
  162. // If called together with the DialWithDialFunc option, the DialWithDialFunc function
  163. // will be used when dialing new connections but regardless of the function,
  164. // the connection will be treated as a TLS connection.
  165. func DialWithTLS(tlsConfig *tls.Config) DialOption {
  166. return DialOption{func(do *dialOptions) {
  167. do.tlsConfig = tlsConfig
  168. }}
  169. }
  170. // DialWithDebugOutput returns a DialOption that configures the ServerConn to write to the Writer
  171. // everything it reads from the server
  172. func DialWithDebugOutput(w io.Writer) DialOption {
  173. return DialOption{func(do *dialOptions) {
  174. do.debugOutput = w
  175. }}
  176. }
  177. // DialWithDialFunc returns a DialOption that configures the ServerConn to use the
  178. // specified function to establish both control and data connections
  179. //
  180. // If used together with the DialWithNetConn option, the DialWithNetConn
  181. // takes precedence for the control connection, while data connections will
  182. // be established using function specified with the DialWithDialFunc option
  183. func DialWithDialFunc(f func(network, address string) (net.Conn, error)) DialOption {
  184. return DialOption{func(do *dialOptions) {
  185. do.dialFunc = f
  186. }}
  187. }
  188. // Connect is an alias to Dial, for backward compatibility
  189. func Connect(addr string) (*ServerConn, error) {
  190. return Dial(addr)
  191. }
  192. // DialTimeout initializes the connection to the specified ftp server address.
  193. //
  194. // It is generally followed by a call to Login() as most FTP commands require
  195. // an authenticated user.
  196. func DialTimeout(addr string, timeout time.Duration) (*ServerConn, error) {
  197. return Dial(addr, DialWithTimeout(timeout))
  198. }
  199. // Login authenticates the client with specified user and password.
  200. //
  201. // "anonymous"/"anonymous" is a common user/password scheme for FTP servers
  202. // that allows anonymous read-only accounts.
  203. func (c *ServerConn) Login(user, password string) error {
  204. code, message, err := c.cmd(-1, "USER %s", user)
  205. if err != nil {
  206. return err
  207. }
  208. switch code {
  209. case StatusLoggedIn:
  210. case StatusUserOK:
  211. _, _, err = c.cmd(StatusLoggedIn, "PASS %s", password)
  212. if err != nil {
  213. return err
  214. }
  215. default:
  216. return errors.New(message)
  217. }
  218. // Switch to binary mode
  219. if _, _, err = c.cmd(StatusCommandOK, "TYPE I"); err != nil {
  220. return err
  221. }
  222. // Switch to UTF-8
  223. err = c.setUTF8()
  224. // If using implicit TLS, make data connections also use TLS
  225. if c.options.tlsConfig != nil {
  226. c.cmd(StatusCommandOK, "PBSZ 0")
  227. c.cmd(StatusCommandOK, "PROT P")
  228. }
  229. return err
  230. }
  231. // feat issues a FEAT FTP command to list the additional commands supported by
  232. // the remote FTP server.
  233. // FEAT is described in RFC 2389
  234. func (c *ServerConn) feat() error {
  235. code, message, err := c.cmd(-1, "FEAT")
  236. if err != nil {
  237. return err
  238. }
  239. if code != StatusSystem {
  240. // The server does not support the FEAT command. This is not an
  241. // error: we consider that there is no additional feature.
  242. return nil
  243. }
  244. lines := strings.Split(message, "\n")
  245. for _, line := range lines {
  246. if !strings.HasPrefix(line, " ") {
  247. continue
  248. }
  249. line = strings.TrimSpace(line)
  250. featureElements := strings.SplitN(line, " ", 2)
  251. command := featureElements[0]
  252. var commandDesc string
  253. if len(featureElements) == 2 {
  254. commandDesc = featureElements[1]
  255. }
  256. c.features[command] = commandDesc
  257. }
  258. return nil
  259. }
  260. // setUTF8 issues an "OPTS UTF8 ON" command.
  261. func (c *ServerConn) setUTF8() error {
  262. if _, ok := c.features["UTF8"]; !ok {
  263. return nil
  264. }
  265. code, message, err := c.cmd(-1, "OPTS UTF8 ON")
  266. if err != nil {
  267. return err
  268. }
  269. // Workaround for FTP servers, that does not support this option.
  270. if code == StatusBadArguments {
  271. return nil
  272. }
  273. // The ftpd "filezilla-server" has FEAT support for UTF8, but always returns
  274. // "202 UTF8 mode is always enabled. No need to send this command." when
  275. // trying to use it. That's OK
  276. if code == StatusCommandNotImplemented {
  277. return nil
  278. }
  279. if code != StatusCommandOK {
  280. return errors.New(message)
  281. }
  282. return nil
  283. }
  284. // epsv issues an "EPSV" command to get a port number for a data connection.
  285. func (c *ServerConn) epsv() (port int, err error) {
  286. _, line, err := c.cmd(StatusExtendedPassiveMode, "EPSV")
  287. if err != nil {
  288. return
  289. }
  290. start := strings.Index(line, "|||")
  291. end := strings.LastIndex(line, "|")
  292. if start == -1 || end == -1 {
  293. err = errors.New("invalid EPSV response format")
  294. return
  295. }
  296. port, err = strconv.Atoi(line[start+3 : end])
  297. return
  298. }
  299. // pasv issues a "PASV" command to get a port number for a data connection.
  300. func (c *ServerConn) pasv() (host string, port int, err error) {
  301. _, line, err := c.cmd(StatusPassiveMode, "PASV")
  302. if err != nil {
  303. return
  304. }
  305. // PASV response format : 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
  306. start := strings.Index(line, "(")
  307. end := strings.LastIndex(line, ")")
  308. if start == -1 || end == -1 {
  309. err = errors.New("invalid PASV response format")
  310. return
  311. }
  312. // We have to split the response string
  313. pasvData := strings.Split(line[start+1:end], ",")
  314. if len(pasvData) < 6 {
  315. err = errors.New("invalid PASV response format")
  316. return
  317. }
  318. // Let's compute the port number
  319. portPart1, err1 := strconv.Atoi(pasvData[4])
  320. if err1 != nil {
  321. err = err1
  322. return
  323. }
  324. portPart2, err2 := strconv.Atoi(pasvData[5])
  325. if err2 != nil {
  326. err = err2
  327. return
  328. }
  329. // Recompose port
  330. port = portPart1*256 + portPart2
  331. // Make the IP address to connect to
  332. host = strings.Join(pasvData[0:4], ".")
  333. return
  334. }
  335. // getDataConnPort returns a host, port for a new data connection
  336. // it uses the best available method to do so
  337. func (c *ServerConn) getDataConnPort() (string, int, error) {
  338. if !c.options.disableEPSV && !c.skipEPSV {
  339. if port, err := c.epsv(); err == nil {
  340. return c.host, port, nil
  341. }
  342. // if there is an error, skip EPSV for the next attempts
  343. c.skipEPSV = true
  344. }
  345. return c.pasv()
  346. }
  347. // openDataConn creates a new FTP data connection.
  348. func (c *ServerConn) openDataConn() (net.Conn, error) {
  349. host, port, err := c.getDataConnPort()
  350. if err != nil {
  351. return nil, err
  352. }
  353. addr := net.JoinHostPort(host, strconv.Itoa(port))
  354. if c.options.dialFunc != nil {
  355. return c.options.dialFunc("tcp", addr)
  356. }
  357. if c.options.tlsConfig != nil {
  358. conn, err := c.options.dialer.Dial("tcp", addr)
  359. if err != nil {
  360. return nil, err
  361. }
  362. return tls.Client(conn, c.options.tlsConfig), err
  363. }
  364. return c.options.dialer.Dial("tcp", addr)
  365. }
  366. // cmd is a helper function to execute a command and check for the expected FTP
  367. // return code
  368. func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, error) {
  369. _, err := c.conn.Cmd(format, args...)
  370. if err != nil {
  371. return 0, "", err
  372. }
  373. return c.conn.ReadResponse(expected)
  374. }
  375. // cmdDataConnFrom executes a command which require a FTP data connection.
  376. // Issues a REST FTP command to specify the number of bytes to skip for the transfer.
  377. func (c *ServerConn) cmdDataConnFrom(offset uint64, format string, args ...interface{}) (net.Conn, error) {
  378. conn, err := c.openDataConn()
  379. if err != nil {
  380. return nil, err
  381. }
  382. if offset != 0 {
  383. _, _, err := c.cmd(StatusRequestFilePending, "REST %d", offset)
  384. if err != nil {
  385. conn.Close()
  386. return nil, err
  387. }
  388. }
  389. _, err = c.conn.Cmd(format, args...)
  390. if err != nil {
  391. conn.Close()
  392. return nil, err
  393. }
  394. code, msg, err := c.conn.ReadResponse(-1)
  395. if err != nil {
  396. conn.Close()
  397. return nil, err
  398. }
  399. if code != StatusAlreadyOpen && code != StatusAboutToSend {
  400. conn.Close()
  401. return nil, &textproto.Error{Code: code, Msg: msg}
  402. }
  403. return conn, nil
  404. }
  405. // NameList issues an NLST FTP command.
  406. func (c *ServerConn) NameList(path string) (entries []string, err error) {
  407. conn, err := c.cmdDataConnFrom(0, "NLST %s", path)
  408. if err != nil {
  409. return
  410. }
  411. r := &Response{conn: conn, c: c}
  412. defer r.Close()
  413. scanner := bufio.NewScanner(r)
  414. for scanner.Scan() {
  415. entries = append(entries, scanner.Text())
  416. }
  417. if err = scanner.Err(); err != nil {
  418. return entries, err
  419. }
  420. return
  421. }
  422. // List issues a LIST FTP command.
  423. func (c *ServerConn) List(path string) (entries []*Entry, err error) {
  424. var cmd string
  425. var parser parseFunc
  426. if c.mlstSupported {
  427. cmd = "MLSD"
  428. parser = parseRFC3659ListLine
  429. } else {
  430. cmd = "LIST"
  431. parser = parseListLine
  432. }
  433. conn, err := c.cmdDataConnFrom(0, "%s %s", cmd, path)
  434. if err != nil {
  435. return
  436. }
  437. r := &Response{conn: conn, c: c}
  438. defer r.Close()
  439. scanner := bufio.NewScanner(r)
  440. now := time.Now()
  441. for scanner.Scan() {
  442. entry, err := parser(scanner.Text(), now, c.options.location)
  443. if err == nil {
  444. entries = append(entries, entry)
  445. }
  446. }
  447. if err := scanner.Err(); err != nil {
  448. return nil, err
  449. }
  450. return
  451. }
  452. // ChangeDir issues a CWD FTP command, which changes the current directory to
  453. // the specified path.
  454. func (c *ServerConn) ChangeDir(path string) error {
  455. _, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
  456. return err
  457. }
  458. // ChangeDirToParent issues a CDUP FTP command, which changes the current
  459. // directory to the parent directory. This is similar to a call to ChangeDir
  460. // with a path set to "..".
  461. func (c *ServerConn) ChangeDirToParent() error {
  462. _, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
  463. return err
  464. }
  465. // CurrentDir issues a PWD FTP command, which Returns the path of the current
  466. // directory.
  467. func (c *ServerConn) CurrentDir() (string, error) {
  468. _, msg, err := c.cmd(StatusPathCreated, "PWD")
  469. if err != nil {
  470. return "", err
  471. }
  472. start := strings.Index(msg, "\"")
  473. end := strings.LastIndex(msg, "\"")
  474. if start == -1 || end == -1 {
  475. return "", errors.New("unsuported PWD response format")
  476. }
  477. return msg[start+1 : end], nil
  478. }
  479. // FileSize issues a SIZE FTP command, which Returns the size of the file
  480. func (c *ServerConn) FileSize(path string) (int64, error) {
  481. _, msg, err := c.cmd(StatusFile, "SIZE %s", path)
  482. if err != nil {
  483. return 0, err
  484. }
  485. return strconv.ParseInt(msg, 10, 64)
  486. }
  487. // Retr issues a RETR FTP command to fetch the specified file from the remote
  488. // FTP server.
  489. //
  490. // The returned ReadCloser must be closed to cleanup the FTP data connection.
  491. func (c *ServerConn) Retr(path string) (*Response, error) {
  492. return c.RetrFrom(path, 0)
  493. }
  494. // RetrFrom issues a RETR FTP command to fetch the specified file from the remote
  495. // FTP server, the server will not send the offset first bytes of the file.
  496. //
  497. // The returned ReadCloser must be closed to cleanup the FTP data connection.
  498. func (c *ServerConn) RetrFrom(path string, offset uint64) (*Response, error) {
  499. conn, err := c.cmdDataConnFrom(offset, "RETR %s", path)
  500. if err != nil {
  501. return nil, err
  502. }
  503. return &Response{conn: conn, c: c}, nil
  504. }
  505. // Stor issues a STOR FTP command to store a file to the remote FTP server.
  506. // Stor creates the specified file with the content of the io.Reader.
  507. //
  508. // Hint: io.Pipe() can be used if an io.Writer is required.
  509. func (c *ServerConn) Stor(path string, r io.Reader) error {
  510. return c.StorFrom(path, r, 0)
  511. }
  512. // StorFrom issues a STOR FTP command to store a file to the remote FTP server.
  513. // Stor creates the specified file with the content of the io.Reader, writing
  514. // on the server will start at the given file offset.
  515. //
  516. // Hint: io.Pipe() can be used if an io.Writer is required.
  517. func (c *ServerConn) StorFrom(path string, r io.Reader, offset uint64) error {
  518. conn, err := c.cmdDataConnFrom(offset, "STOR %s", path)
  519. if err != nil {
  520. return err
  521. }
  522. _, err = io.Copy(conn, r)
  523. conn.Close()
  524. if err != nil {
  525. return err
  526. }
  527. _, _, err = c.conn.ReadResponse(StatusClosingDataConnection)
  528. return err
  529. }
  530. // Append issues a APPE FTP command to store a file to the remote FTP server.
  531. // If a file already exists with the given path, then the content of the
  532. // io.Reader is appended. Otherwise, a new file is created with that content.
  533. //
  534. // Hint: io.Pipe() can be used if an io.Writer is required.
  535. func (c *ServerConn) Append(path string, r io.Reader) error {
  536. conn, err := c.cmdDataConnFrom(0, "APPE %s", path)
  537. if err != nil {
  538. return err
  539. }
  540. _, err = io.Copy(conn, r)
  541. conn.Close()
  542. if err != nil {
  543. return err
  544. }
  545. _, _, err = c.conn.ReadResponse(StatusClosingDataConnection)
  546. return err
  547. }
  548. // Rename renames a file on the remote FTP server.
  549. func (c *ServerConn) Rename(from, to string) error {
  550. _, _, err := c.cmd(StatusRequestFilePending, "RNFR %s", from)
  551. if err != nil {
  552. return err
  553. }
  554. _, _, err = c.cmd(StatusRequestedFileActionOK, "RNTO %s", to)
  555. return err
  556. }
  557. // Delete issues a DELE FTP command to delete the specified file from the
  558. // remote FTP server.
  559. func (c *ServerConn) Delete(path string) error {
  560. _, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", path)
  561. return err
  562. }
  563. // RemoveDirRecur deletes a non-empty folder recursively using
  564. // RemoveDir and Delete
  565. func (c *ServerConn) RemoveDirRecur(path string) error {
  566. err := c.ChangeDir(path)
  567. if err != nil {
  568. return err
  569. }
  570. currentDir, err := c.CurrentDir()
  571. if err != nil {
  572. return err
  573. }
  574. entries, err := c.List(currentDir)
  575. if err != nil {
  576. return err
  577. }
  578. for _, entry := range entries {
  579. if entry.Name != ".." && entry.Name != "." {
  580. if entry.Type == EntryTypeFolder {
  581. err = c.RemoveDirRecur(currentDir + "/" + entry.Name)
  582. if err != nil {
  583. return err
  584. }
  585. } else {
  586. err = c.Delete(entry.Name)
  587. if err != nil {
  588. return err
  589. }
  590. }
  591. }
  592. }
  593. err = c.ChangeDirToParent()
  594. if err != nil {
  595. return err
  596. }
  597. err = c.RemoveDir(currentDir)
  598. return err
  599. }
  600. // MakeDir issues a MKD FTP command to create the specified directory on the
  601. // remote FTP server.
  602. func (c *ServerConn) MakeDir(path string) error {
  603. _, _, err := c.cmd(StatusPathCreated, "MKD %s", path)
  604. return err
  605. }
  606. // RemoveDir issues a RMD FTP command to remove the specified directory from
  607. // the remote FTP server.
  608. func (c *ServerConn) RemoveDir(path string) error {
  609. _, _, err := c.cmd(StatusRequestedFileActionOK, "RMD %s", path)
  610. return err
  611. }
  612. //Walk prepares the internal walk function so that the caller can begin traversing the directory
  613. func (c *ServerConn) Walk(root string) *Walker {
  614. w := new(Walker)
  615. w.serverConn = c
  616. if !strings.HasSuffix(root, "/") {
  617. root += "/"
  618. }
  619. w.root = root
  620. return w
  621. }
  622. // NoOp issues a NOOP FTP command.
  623. // NOOP has no effects and is usually used to prevent the remote FTP server to
  624. // close the otherwise idle connection.
  625. func (c *ServerConn) NoOp() error {
  626. _, _, err := c.cmd(StatusCommandOK, "NOOP")
  627. return err
  628. }
  629. // Logout issues a REIN FTP command to logout the current user.
  630. func (c *ServerConn) Logout() error {
  631. _, _, err := c.cmd(StatusReady, "REIN")
  632. return err
  633. }
  634. // Quit issues a QUIT FTP command to properly close the connection from the
  635. // remote FTP server.
  636. func (c *ServerConn) Quit() error {
  637. c.conn.Cmd("QUIT")
  638. return c.conn.Close()
  639. }
  640. // Read implements the io.Reader interface on a FTP data connection.
  641. func (r *Response) Read(buf []byte) (int, error) {
  642. return r.conn.Read(buf)
  643. }
  644. // Close implements the io.Closer interface on a FTP data connection.
  645. // After the first call, Close will do nothing and return nil.
  646. func (r *Response) Close() error {
  647. if r.closed {
  648. return nil
  649. }
  650. err := r.conn.Close()
  651. _, _, err2 := r.c.conn.ReadResponse(StatusClosingDataConnection)
  652. if err2 != nil {
  653. err = err2
  654. }
  655. r.closed = true
  656. return err
  657. }
  658. // SetDeadline sets the deadlines associated with the connection.
  659. func (r *Response) SetDeadline(t time.Time) error {
  660. return r.conn.SetDeadline(t)
  661. }