Remove double include; fcnctl seems to be around in several locations.
[citadel.git] / libcitadel / lib / stringbuf.c
index 08e904b315fc36f896823e6a9f0b174abf556a72..795dd604d734f881f0792d6398e44a3886cd3871 100644 (file)
@@ -16,6 +16,7 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
+#define _GNU_SOURCE
 #include "sysdep.h"
 #include <ctype.h>
 #include <errno.h>
@@ -3799,10 +3800,24 @@ void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize)
        FDB->IOB = IO;
 #ifndef LINUX_SENDFILE
        FDB->ChunkBuffer = NewStrBuf();
+#else
+       pipe(FDB->SplicePipe);
 #endif
        FDB->OtherFD = FD;
 }
 
+void FDIOBufferDelete(FDIOBuffer *FDB)
+{
+#ifndef LINUX_SENDFILE
+       FreeStrBuf(&FDB->ChunkBuffer);
+#else
+       close(FDB->SplicePipe[0]);
+       close(FDB->SplicePipe[1]);
+#endif
+       close(FDB->OtherFD);
+       memset(FDB, 0, sizeof(FDIOBuffer));     
+}
+
 int FileSendChunked(FDIOBuffer *FDB, const char **Err)
 {
 
@@ -3825,15 +3840,28 @@ int FileRecvChunked(FDIOBuffer *FDB, const char **Err)
 {
 
 #ifdef LINUX_SENDFILE
-       ssize_t sent;
-       sent = sendfile(FDB->OtherFD, FDB->IOB->fd, &FDB->TotalSentAlready, FDB->ChunkSendRemain);
+       ssize_t sent, pipesize;
+
+       pipesize = splice(FDB->IOB->fd, NULL, 
+                         FDB->SplicePipe[1], NULL, 
+                         FDB->ChunkSendRemain, 
+                         SPLICE_F_MORE | SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
+       if (pipesize == -1)
+       {
+               *Err = strerror(errno);
+               return pipesize;
+       }
+       
+       sent = splice(FDB->SplicePipe[0], NULL, 
+                     FDB->OtherFD, &FDB->TotalSentAlready, 
+                     pipesize, SPLICE_F_MORE | SPLICE_F_MOVE);
        if (sent == -1)
        {
                *Err = strerror(errno);
                return sent;
        }
        FDB->ChunkSendRemain -= sent;
-       return FDB->ChunkSendRemain;
+       return sent;
 #else
 #endif
        return 0;