]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/stringbuf.c
FileSendChunked(): replace sendfile by splice()
[citadel.git] / libcitadel / lib / stringbuf.c
index 1d24342771ee10998ef1e251379474cde22d8e89..79b5f5d2f3ab2116e0d2cd6604bf3db6e60a0f5e 100644 (file)
@@ -1345,6 +1345,23 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
        return ReducedBy;
 }
 
+int StrBufExtract_tokenFromStr(StrBuf *dest, const char *Source, long SourceLen, int parmnum, char separator)
+{
+       const StrBuf Temp = {
+               (char*)Source,
+               SourceLen,
+               SourceLen,
+               1
+#ifdef SIZE_DEBUG
+               ,
+               0,
+               "",
+               ""
+#endif
+       };
+
+       return StrBufExtract_token(dest, &Temp, parmnum, separator);
+}
 
 /**
  * @ingroup StrBuf_Tokenizer
@@ -3869,7 +3886,7 @@ void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize)
        FDB->ChunkSize = 
                FDB->TotalSendSize = TotalSendSize;
        FDB->IOB = IO;
-#ifndef LINUX_SENDFILE
+#ifndef LINUX_SPLICE
        FDB->ChunkBuffer = NewStrBufPlain(NULL, TotalSendSize + 1);
 #else
        pipe(FDB->SplicePipe);
@@ -3879,7 +3896,7 @@ void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize)
 
 void FDIOBufferDelete(FDIOBuffer *FDB)
 {
-#ifndef LINUX_SENDFILE
+#ifndef LINUX_SPLICE
        FreeStrBuf(&FDB->ChunkBuffer);
 #else
        close(FDB->SplicePipe[0]);
@@ -3892,18 +3909,38 @@ void FDIOBufferDelete(FDIOBuffer *FDB)
 
 int FileSendChunked(FDIOBuffer *FDB, const char **Err)
 {
-
-#ifdef LINUX_SENDFILE
-       ssize_t sent;
-       sent = sendfile(FDB->IOB->fd, FDB->OtherFD, &FDB->TotalSentAlready, FDB->ChunkSendRemain);
+       ssize_t sent, pipesize;
+#ifdef LINUX_SPLICE
+       if (FDB->PipeSize == 0)
+       {
+               pipesize = splice(FDB->OtherFD,
+                                 &FDB->TotalSentAlready, 
+                                 FDB->SplicePipe[1],
+                                 NULL, 
+                                 FDB->ChunkSendRemain, 
+                                 SPLICE_F_MOVE);
+       
+               if (pipesize == -1)
+               {
+                       *Err = strerror(errno);
+                       return pipesize;
+               }
+               FDB->PipeSize = pipesize;
+       }
+       sent =  splice(FDB->SplicePipe[0],
+                      NULL, 
+                      FDB->IOB->fd,
+                      NULL, 
+                      FDB->PipeSize,
+                      SPLICE_F_MORE | SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
        if (sent == -1)
        {
                *Err = strerror(errno);
                return sent;
        }
+       FDB->PipeSize -= sent;
        FDB->ChunkSendRemain -= sent;
-       FDB->TotalSentAlready += sent;
-       return FDB->ChunkSendRemain;
+       return sent;
 #else
 
        char *pRead;
@@ -3939,26 +3976,37 @@ int FileRecvChunked(FDIOBuffer *FDB, const char **Err)
 {
        ssize_t sent, pipesize;
 
-#ifdef LINUX_SENDFILE
-
-       pipesize = splice(FDB->IOB->fd, NULL, 
-                         FDB->SplicePipe[1], NULL, 
-                         FDB->ChunkSendRemain, 
-                         SPLICE_F_MORE | SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
-       if (pipesize == -1)
+#ifdef LINUX_SPLICE
+       if (FDB->PipeSize == 0)
        {
-               *Err = strerror(errno);
-               return 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;
+               }
+               FDB->PipeSize = pipesize;
        }
        
-       sent = splice(FDB->SplicePipe[0], NULL, 
-                     FDB->OtherFD, &FDB->TotalSentAlready, 
-                     pipesize, SPLICE_F_MORE | SPLICE_F_MOVE);
+       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->PipeSize -= sent;
        FDB->ChunkSendRemain -= sent;
        return sent;
 #else