From: Art Cancro Date: Tue, 2 Oct 2018 17:06:52 +0000 (-0400) Subject: Removed the chunked-io stuff from libcitadel. Nothing was using it and it isn't... X-Git-Tag: v939~340 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=3cfffe2479a77433a5e0801d24b902b33b0078f3 Removed the chunked-io stuff from libcitadel. Nothing was using it and it isn't written portably. We need to be better about making stuff work outside of Linux now that the kernel has been taken over by far-left social justice communists like C. Emkhe. --- diff --git a/citadel/utils/sendcommand.c b/citadel/utils/sendcommand.c index ba28d142d..001a4b1dc 100644 --- a/citadel/utils/sendcommand.c +++ b/citadel/utils/sendcommand.c @@ -30,10 +30,8 @@ #include "include/citadel_dirs.h" #include - int serv_sock = (-1); - int uds_connectsock(char *sockpath) { int s; @@ -94,7 +92,6 @@ void serv_write(char *buf, int nbytes) } - /* * input string from socket - implemented in terms of serv_read() */ @@ -134,8 +131,6 @@ void serv_puts(char *buf) } - - /* * Main loop. Do things and have fun. */ @@ -183,7 +178,6 @@ int main(int argc, char **argv) alarm(watchdog); serv_sock = uds_connectsock(file_citadel_admin_socket); - serv_gets(buf); fprintf(stderr, "%s\n", buf); @@ -203,83 +197,19 @@ int main(int argc, char **argv) xfermode = buf[0]; if ((xfermode == '4') || (xfermode == '8')) { /* send text */ - IOBuffer IOB; - FDIOBuffer FDIO; - const char *ErrStr; - - memset(&IOB, 0, sizeof(0)); - IOB.Buf = NewStrBufPlain(NULL, SIZ); - IOB.fd = serv_sock; - FDIOBufferInit(&FDIO, &IOB, fileno(stdin), -1); - - while (FileSendChunked(&FDIO, &ErrStr) >= 0) - alarm(watchdog); /* reset the watchdog timer */ - if (ErrStr != NULL) - fprintf(stderr, "Error while piping stuff: %s\n", ErrStr); - FDIOBufferDelete(&FDIO); - FreeStrBuf(&IOB.Buf); + while (fgets(buf, sizeof buf, stdin) > 0) { + if (buf[strlen(buf)-1] == '\n') { + buf[strlen(buf)-1] = 0; + } + serv_puts(buf); + } serv_puts("000"); } if ((xfermode == '1') || (xfermode == '8')) { /* receive text */ - IOBuffer IOB; - StrBuf *Line, *OutBuf; - int Finished = 0; - - memset(&IOB, 0, sizeof(IOB)); - IOB.Buf = NewStrBufPlain(NULL, SIZ); - IOB.fd = serv_sock; - Line = NewStrBufPlain(NULL, SIZ); - OutBuf = NewStrBufPlain(NULL, SIZ * 10); - - while (!Finished && (StrBuf_read_one_chunk_callback (serv_sock, 0, &IOB) >= 0)) - { - eReadState State; - - State = eReadSuccess; - while (!Finished && (State == eReadSuccess)) - { - if (IOBufferStrLength(&IOB) == 0) - { - State = eMustReadMore; - break; - } - State = StrBufChunkSipLine(Line, &IOB); - switch (State) - { - case eReadSuccess: - if (!strcmp(ChrPtr(Line), "000")) - { - Finished = 1; - break; - } - StrBufAppendBuf(OutBuf, Line, 0); - StrBufAppendBufPlain(OutBuf, HKEY("\n"), 0); - alarm(watchdog); /* reset the watchdog timer */ - break; - case eBufferNotEmpty: - break; - case eMustReadMore: - continue; - case eReadFail: - fprintf(stderr, "WTF? Exit!\n"); - exit(-1); - break; - } - if (StrLength(OutBuf) > 5*SIZ) - { - fwrite(ChrPtr(OutBuf), 1, StrLength(OutBuf), stdout); - FlushStrBuf(OutBuf); - } - } - } - if (StrLength(OutBuf) > 0) - { - fwrite(ChrPtr(OutBuf), 1, StrLength(OutBuf), stdout); + while(serv_gets(buf), strcmp(buf, "000")) { + printf("%s\n", buf); } - FreeStrBuf(&Line); - FreeStrBuf(&OutBuf); - FreeStrBuf(&IOB.Buf); } if (xfermode == '6') { /* receive binary */ diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 3dbea9588..46709311c 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -254,28 +254,6 @@ typedef struct _file_buffer { int nBlobBytesWanted; } IOBuffer; - -typedef struct __fd_iobuffer { - IOBuffer *IOB; - int OtherFD; - int SplicePipe[2]; - int PipeSize; - long TotalSendSize; - loff_t TotalSentAlready; - loff_t TotalReadAlready; - long ChunkSize; - long ChunkSendRemain; - StrBuf *ChunkBuffer; /* just used if we don't have sendfile */ -} FDIOBuffer; - - -void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize); -void FDIOBufferDelete(FDIOBuffer *FDB); -int FileSendChunked(FDIOBuffer *FDB, const char **Err); -int FileRecvChunked(FDIOBuffer *FDB, const char **Err); -int FileMoveChunked(FDIOBuffer *FDB, const char **Err); -eReadState WriteIOBAlreadyRead(FDIOBuffer *FDB, const char **Error); - long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB); int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 8398757de..770cb3eb5 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1987-2017 by the citadel.org team + * Copyright (c) 1987-2018 by the citadel.org team * * This program is open source software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -4506,421 +4506,6 @@ long IOBufferStrLength(IOBuffer *FB) return StrLength(FB->Buf) - (FB->ReadWritePointer - FB->Buf->buf); } -inline static void FDIOBufferFlush(FDIOBuffer *FDB) -{ - memset(FDB, 0, sizeof(FDIOBuffer)); - FDB->OtherFD = -1; - FDB->SplicePipe[0] = -1; - FDB->SplicePipe[1] = -1; -} - -void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize) -{ - FDIOBufferFlush(FDB); - - FDB->TotalSendSize = TotalSendSize; - if (TotalSendSize > 0) - FDB->ChunkSize = TotalSendSize; - else - { - TotalSendSize = SIZ * 10; - FDB->ChunkSize = TotalSendSize; - } - FDB->IOB = IO; - -#ifdef LINUX_SPLICE - if (EnableSplice) - pipe(FDB->SplicePipe); - else -#endif - FDB->ChunkBuffer = NewStrBufPlain(NULL, TotalSendSize+ 1); - - FDB->OtherFD = FD; -} - -void FDIOBufferDelete(FDIOBuffer *FDB) -{ -#ifdef LINUX_SPLICE - if (EnableSplice) - { - if (FDB->SplicePipe[0] > 0) - close(FDB->SplicePipe[0]); - if (FDB->SplicePipe[1] > 0) - close(FDB->SplicePipe[1]); - } - else -#endif - FreeStrBuf(&FDB->ChunkBuffer); - - if (FDB->OtherFD > 0) - close(FDB->OtherFD); - FDIOBufferFlush(FDB); -} - -int FileSendChunked(FDIOBuffer *FDB, const char **Err) -{ - ssize_t sent, pipesize; - - if (FDB->TotalSendSize > 0) - { -#ifdef LINUX_SPLICE - if (EnableSplice) - { - 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; - } - else -#endif - { - 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->ChunkBuffer->BufUsed - FDB->TotalSentAlready); - - if (nRead >= 0) { - FDB->TotalSentAlready += nRead; - FDB->ChunkSendRemain -= nRead; - return FDB->ChunkSendRemain; - } - else { - return nRead; - } - } - } - else - { -#ifdef LINUX_SPLICE - if (EnableSplice) - { - if (FDB->PipeSize == 0) - { - pipesize = splice(FDB->OtherFD, - &FDB->TotalSentAlready, - FDB->SplicePipe[1], - NULL, - SIZ * 10, - SPLICE_F_MOVE); - - if (pipesize == -1) - { - *Err = strerror(errno); - return pipesize; - } - FDB->PipeSize = pipesize; - if (pipesize == 0) - return -1; - } - 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; - } - else -#endif - { - char *pRead; - long nRead = 0; - - pRead = FDB->ChunkBuffer->buf; - while ((FDB->ChunkSendRemain == 0) && - (FDB->ChunkBuffer->BufUsed < FDB->ChunkBuffer->BufSize) && - (nRead >= 0)) - { - FDB->TotalSentAlready = 0; - nRead = read(FDB->OtherFD, pRead, FDB->ChunkBuffer->BufSize - FDB->ChunkBuffer->BufUsed); - if (nRead > 0) { - FDB->ChunkBuffer->BufUsed += nRead; - FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0'; - FDB->ChunkSendRemain += nRead; - } - else if (nRead == 0) - { - return -1; - } - else - { - *Err = strerror(errno); - return nRead; - } - } - - nRead = write(FDB->IOB->fd, - FDB->ChunkBuffer->buf + FDB->TotalSentAlready, - FDB->ChunkBuffer->BufUsed - FDB->TotalSentAlready); - - if (nRead >= 0) { - FDB->TotalSentAlready += nRead; - FDB->ChunkSendRemain -= nRead; - if (FDB->ChunkSendRemain == 0) - { - FDB->ChunkBuffer->BufUsed = 0; - FDB->TotalSentAlready = 0; - } - return FDB->ChunkSendRemain; - } - else { - return nRead; - } - } - } -} - -int FileRecvChunked(FDIOBuffer *FDB, const char **Err) -{ - ssize_t sent, pipesize; - -#ifdef LINUX_SPLICE - if (EnableSplice) - { - 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, - FDB->PipeSize, - SPLICE_F_MORE | SPLICE_F_MOVE); - - if (sent == -1) - { - *Err = strerror(errno); - return sent; - } - FDB->PipeSize -= sent; - FDB->ChunkSendRemain -= sent; - 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; - - 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; - } - - 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; - } - else -#endif - { - sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain); - if (sent > 0) { - int nWritten = 0; - int rc; - - 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; - - } - 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) -{ - int IsNonBlock; - int fdflags; - long rlen; - long should_write; - int nSuccessLess = 0; - struct timeval tv; - fd_set rfds; - - fdflags = fcntl(FDB->OtherFD, F_GETFL); - IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK; - - while ((FDB->IOB->ReadWritePointer - FDB->IOB->Buf->buf < FDB->IOB->Buf->BufUsed) && - (FDB->ChunkSendRemain > 0)) - { - if (IsNonBlock){ - tv.tv_sec = 1; /* selectresolution; */ - tv.tv_usec = 0; - - FD_ZERO(&rfds); - FD_SET(FDB->OtherFD, &rfds); - if (select(FDB->OtherFD + 1, NULL, &rfds, NULL, &tv) == -1) { - *Error = strerror(errno); - return eReadFail; - } - } - if (IsNonBlock && ! FD_ISSET(FDB->OtherFD, &rfds)) { - nSuccessLess ++; - continue; - } - - should_write = FDB->IOB->Buf->BufUsed - - (FDB->IOB->ReadWritePointer - FDB->IOB->Buf->buf); - if (should_write > FDB->ChunkSendRemain) - should_write = FDB->ChunkSendRemain; - - rlen = write(FDB->OtherFD, - FDB->IOB->ReadWritePointer, - should_write); - if (rlen < 1) { - *Error = strerror(errno); - - return eReadFail; - } - FDB->TotalSentAlready += rlen; - FDB->IOB->ReadWritePointer += rlen; - FDB->ChunkSendRemain -= rlen; - } - if (FDB->IOB->ReadWritePointer >= FDB->IOB->Buf->buf + FDB->IOB->Buf->BufUsed) - { - FlushStrBuf(FDB->IOB->Buf); - FDB->IOB->ReadWritePointer = NULL; - } - - if (FDB->ChunkSendRemain == 0) - return eReadSuccess; - else - return eMustReadMore; -} /******************************************************************************* * File I/O; Prefer buffered read since its faster! * @@ -4974,6 +4559,7 @@ int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error) return len - slen; } + /** * @ingroup StrBuf_BufferedIO * @brief Read a line from socket