* have select acting like it would accept \n000\n as terminator for the end of blobs
authorWilfried Göesgens <willi@citadel.org>
Tue, 21 Apr 2009 20:33:36 +0000 (20:33 +0000)
committerWilfried Göesgens <willi@citadel.org>
Tue, 21 Apr 2009 20:33:36 +0000 (20:33 +0000)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index 12be21c1c6c14dc90114a147b52910c385a9e2de..fe8e0d09aa5f535ea3aa1a6ad959ea3ed009cd72 100644 (file)
@@ -235,12 +235,15 @@ long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue);
 
 int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error);
 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error);
+#define NNN_TERM 1
+#define O_TERM 0
 int StrBufReadBLOBBuffered(StrBuf *Buf, 
                           StrBuf *IOBuf, 
                           const char **BufPos,
                           int *fd, 
                           int append, 
                           long nBytes, 
+                          int check, 
                           const char **Error);
 int StrBufTCP_read_buffered_line(StrBuf *Line, 
                                 StrBuf *buf, 
index 7db6017d600028d35fd366bb37272740f0bd2f31..1b3116c864e5d61231e6c7639336c7faf47f13b5 100644 (file)
@@ -1524,12 +1524,15 @@ int StrBufReadBLOBBuffered(StrBuf *Buf,
                           int *fd, 
                           int append, 
                           long nBytes, 
+                          int check, 
                           const char **Error)
 {
+       int nSelects = 0;
+       int SelRes;
         fd_set wset;
         int fdflags;
        int len, rlen, slen;
-       int nRead;
+       int nRead = 0;
        char *ptr;
 
        if ((Buf == NULL) || (*fd == -1) || (IOBuf == NULL))
@@ -1569,24 +1572,43 @@ int StrBufReadBLOBBuffered(StrBuf *Buf,
 
        fdflags = fcntl(*fd, F_GETFL);
 
+       SelRes = 1;
        while (nRead < nBytes) {
-               if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
+               if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
                         FD_ZERO(&wset);
                         FD_SET(*fd, &wset);
-                        if (select(*fd + 1, NULL, &wset, NULL, NULL) == -1) {
-                               *Error = strerror(errno);
-                                return -1;
-                        }
-                }
-
-                if ((rlen = read(*fd, 
-                                ptr,
-                                nBytes - nRead)) == -1) {
-                       close(*fd);
-                       *fd = -1;
+                       SelRes = select(*fd + 1, NULL, &wset, NULL, NULL);
+               }
+               if (SelRes == -1) {
                        *Error = strerror(errno);
-                        return rlen;
-                }
+                       return -1;
+               }
+               else if (SelRes) {
+                       nSelects = 0;
+                       if ((rlen = read(*fd, 
+                                        ptr,
+                                        nBytes - nRead)) == -1) {
+                               close(*fd);
+                               *fd = -1;
+                               *Error = strerror(errno);
+                               return rlen;
+                       }
+               }
+               else {
+                       nSelects ++;
+                       if ((check == NNN_TERM) && 
+                           (nRead > 5) &&
+                           (strncmp(Buf->buf + Buf->BufUsed - 5, "\n000\n", 5) == 0)) 
+                       {
+                               StrBufPlain(IOBuf, HKEY("\n000\n"));
+                               StrBufCutRight(Buf, 5);
+                               return Buf->BufUsed;
+                       }
+                       if (nSelects > 10) {
+                               FlushStrBuf(Buf);
+                               return -1;
+                       }
+               }
                nRead += rlen;
                ptr += rlen;
                Buf->BufUsed += rlen;