Blame SOURCES/0027-Delete-files-when-upload-fails.patch

90e381
From 6224ecc5ac209323baa775880c0602c3fde3590a Mon Sep 17 00:00:00 2001
90e381
From: Martin Sehnoutka <msehnout@redhat.com>
90e381
Date: Thu, 17 Nov 2016 13:10:41 +0100
90e381
Subject: [PATCH 27/59] Delete files when upload fails.
90e381
90e381
Previously the uploaded file wasn't removed when the network was
90e381
disconnected. Now it is successfully deleted.
90e381
---
90e381
 ftpcodes.h  |  3 ++-
90e381
 ftpdataio.c |  8 ++++++++
90e381
 main.c      |  2 +-
90e381
 postlogin.c |  9 ++++++++-
90e381
 session.h   |  1 +
90e381
 sysutil.c   | 10 ++++++++++
90e381
 sysutil.h   |  1 +
90e381
 7 files changed, 31 insertions(+), 3 deletions(-)
90e381
90e381
diff --git a/ftpcodes.h b/ftpcodes.h
90e381
index 81e25c5..54dfae7 100644
90e381
--- a/ftpcodes.h
90e381
+++ b/ftpcodes.h
90e381
@@ -15,7 +15,8 @@
90e381
 #define FTP_PBSZOK            200
90e381
 #define FTP_PROTOK            200
90e381
 #define FTP_OPTSOK            200
90e381
-#define FTP_ALLOOK            202
90e381
+#define FTP_ALLOOK            200
90e381
+#define FTP_ALLOIGN           202
90e381
 #define FTP_FEAT              211
90e381
 #define FTP_STATOK            211
90e381
 #define FTP_SIZEOK            213
90e381
diff --git a/ftpdataio.c b/ftpdataio.c
90e381
index 00f9021..c859d80 100644
90e381
--- a/ftpdataio.c
90e381
+++ b/ftpdataio.c
90e381
@@ -242,6 +242,10 @@ init_data_sock_params(struct vsf_session* p_sess, int sock_fd)
90e381
   /* Start the timeout monitor */
90e381
   vsf_sysutil_install_io_handler(handle_io, p_sess);
90e381
   start_data_alarm(p_sess);
90e381
+  if(tunable_delete_failed_uploads)
90e381
+  {
90e381
+    vsf_sysutil_rcvtimeo(sock_fd);
90e381
+  }
90e381
 }
90e381
 
90e381
 static void
90e381
@@ -615,6 +619,10 @@ do_file_recv(struct vsf_session* p_sess, int file_fd, int is_ascii)
90e381
     else if (retval == 0 && !prev_cr)
90e381
     {
90e381
       /* Transfer done, nifty */
90e381
+      if (tunable_delete_failed_uploads &&
90e381
+          !is_ascii && p_sess->upload_size > 0 &&
90e381
+          p_sess->upload_size != ret_struct.transferred)
90e381
+        ret_struct.retval = -2;
90e381
       return ret_struct;
90e381
     }
90e381
     num_to_write = (unsigned int) retval;
90e381
diff --git a/main.c b/main.c
90e381
index f1e2f69..f039081 100644
90e381
--- a/main.c
90e381
+++ b/main.c
90e381
@@ -44,7 +44,7 @@ main(int argc, const char* argv[])
90e381
     /* Login */
90e381
     1, 0, INIT_MYSTR, INIT_MYSTR,
90e381
     /* Protocol state */
90e381
-    0, 1, INIT_MYSTR, 0, 0,
90e381
+    0, 0, 1, INIT_MYSTR, 0, 0,
90e381
     /* HTTP hacks */
90e381
     0, INIT_MYSTR,
90e381
     /* Session state */
90e381
diff --git a/postlogin.c b/postlogin.c
90e381
index 29958c0..e473c34 100644
90e381
--- a/postlogin.c
90e381
+++ b/postlogin.c
90e381
@@ -356,7 +356,14 @@ process_post_login(struct vsf_session* p_sess)
90e381
     }
90e381
     else if (str_equal_text(&p_sess->ftp_cmd_str, "ALLO"))
90e381
     {
90e381
-      vsf_cmdio_write(p_sess, FTP_ALLOOK, "ALLO command ignored.");
90e381
+      if (tunable_delete_failed_uploads && !p_sess->is_ascii)
90e381
+      {
90e381
+        p_sess->upload_size = (filesize_t)vsf_sysutil_atoi(str_getbuf(&p_sess->ftp_cmd_str)+5);
90e381
+        vsf_cmdio_write(p_sess, FTP_ALLOOK, "The filesize has been allocated.");
90e381
+      }
90e381
+      else {
90e381
+        vsf_cmdio_write(p_sess, FTP_ALLOIGN, "ALLO command ignored.");
90e381
+      }
90e381
     }
90e381
     else if (str_equal_text(&p_sess->ftp_cmd_str, "REIN"))
90e381
     {
90e381
diff --git a/session.h b/session.h
90e381
index 3e8fdd5..4eccf46 100644
90e381
--- a/session.h
90e381
+++ b/session.h
90e381
@@ -41,6 +41,7 @@ struct vsf_session
90e381
   struct mystr anon_pass_str;
90e381
 
90e381
   /* Details of the FTP protocol state */
90e381
+  filesize_t upload_size;
90e381
   filesize_t restart_pos;
90e381
   int is_ascii;
90e381
   struct mystr rnfr_filename_str;
90e381
diff --git a/sysutil.c b/sysutil.c
90e381
index 099748f..42bcdf8 100644
90e381
--- a/sysutil.c
90e381
+++ b/sysutil.c
90e381
@@ -680,6 +680,16 @@ vsf_sysutil_activate_keepalive(int fd)
90e381
   }
90e381
 }
90e381
 
90e381
+void
90e381
+vsf_sysutil_rcvtimeo(int fd)
90e381
+{
90e381
+  struct timeval tv;
90e381
+
90e381
+  tv.tv_sec = tunable_data_connection_timeout;
90e381
+  tv.tv_usec = 0;
90e381
+  setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
90e381
+}
90e381
+
90e381
 void
90e381
 vsf_sysutil_activate_reuseaddr(int fd)
90e381
 {
90e381
diff --git a/sysutil.h b/sysutil.h
90e381
index 13153cd..2886bbc 100644
90e381
--- a/sysutil.h
90e381
+++ b/sysutil.h
90e381
@@ -266,6 +266,7 @@ void vsf_sysutil_dns_resolve(struct vsf_sysutil_sockaddr** p_sockptr,
90e381
                              const char* p_name);
90e381
 /* Option setting on sockets */
90e381
 void vsf_sysutil_activate_keepalive(int fd);
90e381
+void vsf_sysutil_rcvtimeo(int fd);
90e381
 void vsf_sysutil_set_iptos_throughput(int fd);
90e381
 void vsf_sysutil_activate_reuseaddr(int fd);
90e381
 void vsf_sysutil_set_nodelay(int fd);
90e381
-- 
90e381
2.14.4
90e381