FDIO: make splice runtime optional
[citadel.git] / libcitadel / lib / stringbuf.c
index 0d88f7ca14f9c2e9925b744300194791c3334f7f..963c55f6a7fbe1a398751e6f4893bf31d7f9df81 100644 (file)
@@ -50,6 +50,7 @@ int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen,
                           const Bytef * source, uLong sourceLen, int level);
 #endif
 int BaseStrBufSize = 64;
+int EnableSplice = 0;
 
 const char *StrBufNOTNULL = ((char*) NULL) - 1;
 
@@ -1345,6 +1346,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,70 +3887,99 @@ void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize)
        FDB->ChunkSize = 
                FDB->TotalSendSize = TotalSendSize;
        FDB->IOB = IO;
-#ifndef LINUX_SPLICE
-       FDB->ChunkBuffer = NewStrBufPlain(NULL, TotalSendSize + 1);
-#else
-       pipe(FDB->SplicePipe);
+#ifdef LINUX_SPLICE
+       if (EnableSplice)
+               pipe(FDB->SplicePipe);
+       else
 #endif
+               FDB->ChunkBuffer = NewStrBufPlain(NULL, TotalSendSize + 1);
+
        FDB->OtherFD = FD;
 }
 
 void FDIOBufferDelete(FDIOBuffer *FDB)
 {
-#ifndef LINUX_SPLICE
-       FreeStrBuf(&FDB->ChunkBuffer);
-#else
-       close(FDB->SplicePipe[0]);
-       close(FDB->SplicePipe[1]);
-       
+#ifdef LINUX_SPLICE
+       if (EnableSplice)
+       {
+               close(FDB->SplicePipe[0]);
+               close(FDB->SplicePipe[1]);
+       }
+       else
 #endif
+               FreeStrBuf(&FDB->ChunkBuffer);
+       
        close(FDB->OtherFD);
        memset(FDB, 0, sizeof(FDIOBuffer));     
 }
 
 int FileSendChunked(FDIOBuffer *FDB, const char **Err)
 {
-
+       ssize_t sent, pipesize;
 #ifdef LINUX_SPLICE
-       ssize_t sent;
-       sent = sendfile(FDB->IOB->fd, FDB->OtherFD, &FDB->TotalSentAlready, FDB->ChunkSendRemain);
-       if (sent == -1)
+       if (EnableSplice)
        {
-               *Err = strerror(errno);
+               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;
                return sent;
        }
-       FDB->ChunkSendRemain -= sent;
-       FDB->TotalSentAlready += sent;
-       return FDB->ChunkSendRemain;
-#else
-
-       char *pRead;
-       long nRead = 0;
-
-       pRead = FDB->ChunkBuffer->buf;
-       while ((FDB->ChunkBuffer->BufUsed < FDB->TotalSendSize) && (nRead >= 0))
+       else
+#endif
        {
-               nRead = read(FDB->OtherFD, pRead, FDB->TotalSendSize - FDB->ChunkBuffer->BufUsed);
-               if (nRead > 0) {
-                       FDB->ChunkBuffer->BufUsed += nRead;
-                       FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0';
-               }
-               else if (nRead == 0) {}
-               else return nRead;
+               char *pRead;
+               long nRead = 0;
+
+               pRead = FDB->ChunkBuffer->buf;
+               while ((FDB->ChunkBuffer->BufUsed < FDB->TotalSendSize) && (nRead >= 0))
+               {
+                       nRead = read(FDB->OtherFD, pRead, FDB->TotalSendSize - FDB->ChunkBuffer->BufUsed);
+                       if (nRead > 0) {
+                               FDB->ChunkBuffer->BufUsed += nRead;
+                               FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0';
+                       }
+                       else if (nRead == 0) {}
+                       else return nRead;
                
-       }
+               }
 
-       nRead = write(FDB->IOB->fd, FDB->ChunkBuffer->buf + FDB->TotalSentAlready, FDB->ChunkSendRemain);
+               nRead = write(FDB->IOB->fd, FDB->ChunkBuffer->buf + FDB->TotalSentAlready, FDB->ChunkSendRemain);
 
-       if (nRead >= 0) {
-               FDB->TotalSentAlready += nRead;
-               FDB->ChunkSendRemain -= nRead;
-               return FDB->ChunkSendRemain;
-       }
-       else {
-               return nRead;
+               if (nRead >= 0) {
+                       FDB->TotalSentAlready += nRead;
+                       FDB->ChunkSendRemain -= nRead;
+                       return FDB->ChunkSendRemain;
+               }
+               else {
+                       return nRead;
+               }
        }
-#endif
 }
 
 int FileRecvChunked(FDIOBuffer *FDB, const char **Err)
@@ -3940,57 +3987,143 @@ int FileRecvChunked(FDIOBuffer *FDB, const char **Err)
        ssize_t sent, pipesize;
 
 #ifdef LINUX_SPLICE
-
-       pipesize = splice(FDB->IOB->fd, NULL, 
-                         FDB->SplicePipe[1], NULL, 
-                         FDB->ChunkSendRemain, 
-                         SPLICE_F_MORE | SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
-       if (pipesize == -1)
+       if (EnableSplice)
        {
-               *Err = strerror(errno);
-               return pipesize;
-       }
+               if (FDB->PipeSize == 0)
+               {
+                       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);
-       if (sent == -1)
-       {
-               *Err = strerror(errno);
+               sent = splice(FDB->SplicePipe[0],
+                             NULL, 
+                             FDB->OtherFD,
+                             &FDB->TotalSentAlready, 
+                             FDB->PipeSize,
+                             SPLICE_F_MORE | SPLICE_F_MOVE);
+
+               if (sent == -1)
+               {
+                       *Err = strerror(errno);
+                       return sent;
+               }
+               FDB->PipeSize -= sent;
+               FDB->ChunkSendRemain -= sent;
                return sent;
        }
-       FDB->ChunkSendRemain -= sent;
-       return sent;
-#else
-       
-       sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
-       if (sent > 0) {
-               int nWritten = 0;
-               int rc; 
+       else
+#endif
+       {
+               sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
+               if (sent > 0) {
+                       int nWritten = 0;
+                       int rc; 
                
-               FDB->ChunkBuffer->BufUsed = sent;
+                       FDB->ChunkBuffer->BufUsed = sent;
+
+                       while (nWritten < FDB->ChunkBuffer->BufUsed) {
+                               rc =  write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
+                               if (rc < 0) {
+                                       *Err = strerror(errno);
+                                       return rc;
+                               }
+                               nWritten += rc;
 
-               while (nWritten < FDB->ChunkBuffer->BufUsed) {
-                       rc =  write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
-                       if (rc < 0) {
-                               *Err = strerror(errno);
-                               return rc;
                        }
-                       nWritten += rc;
+                       FDB->ChunkBuffer->BufUsed = 0;
+                       FDB->TotalSentAlready += sent;
+                       FDB->ChunkSendRemain -= sent;
+                       return FDB->ChunkSendRemain;
+               }
+               else if (sent < 0) {
+                       *Err = strerror(errno);
+                       return sent;
+               }
+               return 0;
+       }
+}
+
+int FileMoveChunked(FDIOBuffer *FDB, const char **Err)
+{
+       ssize_t sent, pipesize;
 
+#ifdef LINUX_SPLICE
+       if (EnableSplice)
+       {
+               if (FDB->PipeSize == 0)
+               {
+                       pipesize = splice(FDB->IOB->fd,
+                                         &FDB->TotalReadAlready, 
+                                         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;
                }
-               FDB->ChunkBuffer->BufUsed = 0;
-               FDB->TotalSentAlready += sent;
+               
+               sent = splice(FDB->SplicePipe[0],
+                             NULL, 
+                             FDB->OtherFD,
+                             &FDB->TotalSentAlready, 
+                             FDB->PipeSize,
+                             SPLICE_F_MORE | SPLICE_F_MOVE);
+               
+               if (sent == -1)
+               {
+                       *Err = strerror(errno);
+                       return sent;
+               }
+               FDB->PipeSize -= sent;
                FDB->ChunkSendRemain -= sent;
-               return FDB->ChunkSendRemain;
-       }
-       else if (sent < 0) {
-               *Err = strerror(errno);
                return sent;
        }
+       else
+#endif 
+       {
+               sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
+               if (sent > 0) {
+                       int nWritten = 0;
+                       int rc; 
+               
+                       FDB->ChunkBuffer->BufUsed = sent;
 
-#endif
-       return 0;
+                       while (nWritten < FDB->ChunkBuffer->BufUsed) {
+                               rc =  write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
+                               if (rc < 0) {
+                                       *Err = strerror(errno);
+                                       return rc;
+                               }
+                               nWritten += rc;
+
+                       }
+                       FDB->ChunkBuffer->BufUsed = 0;
+                       FDB->TotalSentAlready += sent;
+                       FDB->ChunkSendRemain -= sent;
+                       return FDB->ChunkSendRemain;
+               }
+               else if (sent < 0) {
+                       *Err = strerror(errno);
+                       return sent;
+               }
+               return 0;
+       }
 }
 
 eReadState WriteIOBAlreadyRead(FDIOBuffer *FDB, const char **Error)