[FFmpeg-devel] [PATCH] FTP graceful close data connection to avoid server abort

Camille Gonnet camille.gonnet at free.fr
Thu Jun 2 14:29:47 CEST 2016


When writing files to FTP, if the data connection is closed before the
control connection, the server may handle it as an aborted file transfer
and create and leave the file empty.

---
 libavformat/ftp.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/libavformat/ftp.c b/libavformat/ftp.c
index 0663b47..00747bb 100644
--- a/libavformat/ftp.c
+++ b/libavformat/ftp.c
@@ -220,15 +220,21 @@ static int ftp_send_command(FTPContext *s, const char
*command,

 static void ftp_close_data_connection(FTPContext *s)
 {
-    ffurl_closep(&s->conn_data);
+static const int close_codes[] = {225, 226, 0};
+
+    if (s->conn_data) {
+        ffurl_closep(&s->conn_data);
+        // Need to wait for status, or file transfer might be aborted on
server side
+        ftp_status(s, NULL, close_codes);
+    }
     s->position = 0;
     s->state = DISCONNECTED;
 }

 static void ftp_close_both_connections(FTPContext *s)
 {
-    ffurl_closep(&s->conn_control);
     ftp_close_data_connection(s);
+    ffurl_closep(&s->conn_control);
 }

 static int ftp_auth(FTPContext *s)
@@ -918,8 +924,8 @@ static int ftp_open_dir(URLContext *h)
     if (s->conn_data && s->state == LISTING_DIR)
         return 0;
   fail:
+    ftp_close_data_connection(s);
     ffurl_closep(&s->conn_control);
-    ffurl_closep(&s->conn_data);
     return ret;
 }

@@ -1039,8 +1045,8 @@ static int ftp_close_dir(URLContext *h)
 {
     FTPContext *s = h->priv_data;
     av_freep(&s->dir_buffer);
+    ftp_close_data_connection(s);
     ffurl_closep(&s->conn_control);
-    ffurl_closep(&s->conn_data);
     return 0;
 }

-- 
2.7.4


More information about the ffmpeg-devel mailing list