Repository: trafficserver
Updated Branches:
  refs/heads/master e25f0ac09 -> 7d2cf1d51


TS-3145: add additional textBuffer APIs

To make textBuffer more useful for formatting wawlls of text
  - fix textBuffer::enlargeBuffer so that it works with an initial size of 0
  - add textBuffer::format to append printf-formatted text
  - add textBuffer::chomp to remove any trailing newline
  - add textBuffer::release to remove the internal formatting buffer


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/1906c8b2
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/1906c8b2
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/1906c8b2

Branch: refs/heads/master
Commit: 1906c8b2d7d7165adc5d3ad4992975fbe38baeb3
Parents: e25f0ac
Author: James Peach <jpe...@apache.org>
Authored: Tue Oct 7 09:10:06 2014 -0700
Committer: James Peach <jpe...@apache.org>
Committed: Sat Oct 18 12:24:14 2014 -0700

----------------------------------------------------------------------
 lib/records/P_RecCore.cc |  2 +-
 lib/ts/TextBuffer.cc     | 67 ++++++++++++++++++++++++++++++++++++++-----
 lib/ts/TextBuffer.h      | 22 ++++++++------
 mgmt/Rollback.cc         |  6 ++--
 4 files changed, 78 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1906c8b2/lib/records/P_RecCore.cc
----------------------------------------------------------------------
diff --git a/lib/records/P_RecCore.cc b/lib/records/P_RecCore.cc
index 3afa600..d42bdfe 100644
--- a/lib/records/P_RecCore.cc
+++ b/lib/records/P_RecCore.cc
@@ -1001,7 +1001,7 @@ int RecWriteConfigFile(textBuffer *tb)
       break;
     }
 
-    if (nbytes != tb->spaceUsed()) {
+    if (nbytes != (int)tb->spaceUsed()) {
       RecLog(DL_Warning, "write to file: %s fail, disk maybe full", 
tmp_filename);
       result = REC_ERR_FAIL;
       break;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1906c8b2/lib/ts/TextBuffer.cc
----------------------------------------------------------------------
diff --git a/lib/ts/TextBuffer.cc b/lib/ts/TextBuffer.cc
index aeb2950..8c7c8a3 100644
--- a/lib/ts/TextBuffer.cc
+++ b/lib/ts/TextBuffer.cc
@@ -57,6 +57,17 @@ textBuffer::~textBuffer()
   ats_free(bufferStart);
 }
 
+char *
+textBuffer::release()
+{
+  char * ret = bufferStart;
+
+  bufferStart = nextAdd = NULL;
+  currentSize = spaceLeft = 0;
+
+  return ret;
+}
+
 // void textBuffer::reUse()
 //
 //   Sets the text buffer for reuse by repositioning the
@@ -81,7 +92,7 @@ textBuffer::reUse()
 //  Returns the number of bytes copies or
 //  -1 if there was insufficient memory
 int
-textBuffer::copyFrom(const void *source, int num_bytes)
+textBuffer::copyFrom(const void *source, unsigned num_bytes)
 {
 
   // Get more space if necessary
@@ -110,22 +121,23 @@ textBuffer::copyFrom(const void *source, int num_bytes)
 //  Returns -1 if insufficient memory,
 //    zero otherwise
 int
-textBuffer::enlargeBuffer(int N)
+textBuffer::enlargeBuffer(unsigned N)
 {
-  int addedSize = currentSize;
-  int newSize = currentSize * 2;
+  unsigned addedSize = 0;
+  unsigned newSize = (currentSize ? currentSize : 1) * 2;
   char *newSpace;
 
   if (spaceLeft < N) {
 
-    while (addedSize < N) {
-      addedSize += newSize;
+    while ((newSize - currentSize) < N) {
       newSize *= 2;
     }
 
+    addedSize = newSize - currentSize;
+
     newSpace = (char *)ats_realloc(bufferStart, newSize);
     if (newSpace != NULL) {
-      nextAdd = newSpace + (unsigned int) (nextAdd - bufferStart);
+      nextAdd = newSpace + (unsigned) (nextAdd - bufferStart);
       bufferStart = newSpace;
       spaceLeft += addedSize;
       currentSize = newSize;
@@ -212,3 +224,44 @@ textBuffer::bufPtr()
 {
   return bufferStart;
 }
+
+void
+textBuffer::format(const char * fmt, ...)
+{
+  va_list ap;
+  bool done = false;
+
+  do {
+    int num;
+
+    va_start(ap, fmt);
+    num = vsnprintf(this->nextAdd, this->spaceLeft, fmt, ap);
+    va_end(ap);
+
+    if ((unsigned)num < this->spaceLeft) {
+      // We had enough space to format including the NUL. Since the returned 
character
+      // count does not include the NUL, we can just increment and the next 
format will
+      // overwrite the previous NUL.
+      this->spaceLeft -= num;
+      this->nextAdd += num;
+      done = true;
+    } else {
+      if (enlargeBuffer(num + 1) == -1) {
+        return;
+      }
+    }
+
+  } while (!done);
+}
+
+void
+textBuffer::chomp()
+{
+  if (nextAdd > bufferStart) {
+    if (nextAdd[-1] == '\n') {
+      --nextAdd;
+      ++spaceLeft;
+      *nextAdd = '\0';
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1906c8b2/lib/ts/TextBuffer.h
----------------------------------------------------------------------
diff --git a/lib/ts/TextBuffer.h b/lib/ts/TextBuffer.h
index 35e729a..ab2fa40 100644
--- a/lib/ts/TextBuffer.h
+++ b/lib/ts/TextBuffer.h
@@ -39,21 +39,27 @@ class textBuffer
 {
 public:
   inkcoreapi textBuffer(int size);
-    inkcoreapi ~ textBuffer();
+  inkcoreapi ~ textBuffer();
   int rawReadFromFile(int fd);
   int readFromFD(int fd);
-  inkcoreapi int copyFrom(const void *, int num_bytes);
+  inkcoreapi int copyFrom(const void *, unsigned num_bytes);
   void reUse();
   inkcoreapi char *bufPtr();
-  int spaceUsed()
-  {
-    return (int) (nextAdd - bufferStart);
+
+  size_t spaceUsed() const {
+    return (size_t) (nextAdd - bufferStart);
   };
+
+  void chomp();
+  void format(const char * fmt, ...) TS_PRINTFLIKE(2, 3);
+
+  char * release();
+
 private:
   textBuffer(const textBuffer &);
-  int enlargeBuffer(int N);
-  int currentSize;
-  int spaceLeft;
+  int enlargeBuffer(unsigned N);
+  size_t currentSize;
+  size_t spaceLeft;
   char *bufferStart;
   char *nextAdd;
 };

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1906c8b2/mgmt/Rollback.cc
----------------------------------------------------------------------
diff --git a/mgmt/Rollback.cc b/mgmt/Rollback.cc
index 003d12f..56a4608 100644
--- a/mgmt/Rollback.cc
+++ b/mgmt/Rollback.cc
@@ -367,7 +367,7 @@ Rollback::internalUpdate(textBuffer * buf, version_t 
newVersion, bool notifyChan
   char *activeVersion;
   char *currentVersion_local;
   char *nextVersion;
-  int writeBytes;
+  ssize_t writeBytes;
   int diskFD;
   int ret;
   versionInfo *toRemove;
@@ -414,7 +414,7 @@ Rollback::internalUpdate(textBuffer * buf, version_t 
newVersion, bool notifyChan
   // Write the buffer into the new configuration file
   writeBytes = write(diskFD, buf->bufPtr(), buf->spaceUsed());
   ret = closeFile(diskFD, true);
-  if ((ret < 0) || (writeBytes != buf->spaceUsed())) {
+  if ((ret < 0) || ((size_t)writeBytes != buf->spaceUsed())) {
     mgmt_log(stderr, "[Rollback::internalUpdate] Unable to write new version 
of %s : %s\n", fileName, strerror(errno));
     returnCode = SYS_CALL_ERROR_ROLLBACK;
     goto UPDATE_CLEANUP;
@@ -564,7 +564,7 @@ Rollback::getVersion_ml(version_t version, textBuffer ** 
buffer)
     }
   } while (readResult > 0);
 
-  if (newBuffer->spaceUsed() != fileInfo.st_size) {
+  if ((off_t)newBuffer->spaceUsed() != fileInfo.st_size) {
     mgmt_log(stderr,
              "[Rollback::getVersion] Incorrect amount of data retrieved from 
%s version %d.  Expected: %d   Got: %d\n",
              fileName, version, fileInfo.st_size, newBuffer->spaceUsed());

Reply via email to