You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bc...@apache.org on 2018/03/08 19:19:13 UTC

[trafficserver] branch master updated: loop over read for readIntoBuffer

This is an automated email from the ASF dual-hosted git repository.

bcall pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 0732975  loop over read for readIntoBuffer
0732975 is described below

commit 0732975a4c70bafa095c65e4a799858d348cddb4
Author: Alan Wang <xf...@gmail.com>
AuthorDate: Wed Mar 7 11:53:00 2018 -0800

    loop over read for readIntoBuffer
---
 lib/ts/MatcherUtils.cc | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/lib/ts/MatcherUtils.cc b/lib/ts/MatcherUtils.cc
index f0421e5..d4c5fa7 100644
--- a/lib/ts/MatcherUtils.cc
+++ b/lib/ts/MatcherUtils.cc
@@ -51,8 +51,9 @@ readIntoBuffer(const char *file_path, const char *module_name, int *read_size_pt
 {
   int fd;
   struct stat file_info;
-  char *file_buf;
+  char *file_buf, *buf;
   int read_size = 0;
+  int file_size;
 
   if (read_size_ptr != nullptr) {
     *read_size_ptr = 0;
@@ -70,30 +71,49 @@ readIntoBuffer(const char *file_path, const char *module_name, int *read_size_pt
     return nullptr;
   }
 
-  if (file_info.st_size < 0) {
+  file_size = file_info.st_size; // number of bytes in file
+
+  if (file_size < 0) {
     Error("%s Can not get correct file size for %s file : %" PRId64 "", module_name, file_path, (int64_t)file_info.st_size);
     close(fd);
     return nullptr;
   }
+
+  ink_assert(file_size >= 0);
+
   // Allocate a buffer large enough to hold the entire file
   //   File size should be small and this makes it easy to
   //   do two passes on the file
-  file_buf = (char *)ats_malloc(file_info.st_size + 1);
+  file_buf = (char *)ats_malloc(file_size + 1);
   // Null terminate the buffer so that string operations will work
-  file_buf[file_info.st_size] = '\0';
+  file_buf[file_size] = '\0';
+
+  int ret = 0;
+  buf     = file_buf; // working pointer
+
+  // loop over read, trying to read in as much as we can each time.
+  while (file_size > read_size) {
+    ret = read(fd, buf, file_size - read_size);
+    if (ret <= 0) {
+      break;
+    }
+
+    buf += ret;
+    read_size += ret;
+  }
 
-  read_size = (file_info.st_size > 0) ? read(fd, file_buf, file_info.st_size) : 0;
+  buf = nullptr; // done with. don't want to accidentally use this instead of file_buf.
 
   // Check to make sure that we got the whole file
-  if (read_size < 0) {
+  if (ret < 0) {
     Error("%s Read of %s file failed : %s", module_name, file_path, strerror(errno));
     ats_free(file_buf);
     file_buf = nullptr;
-  } else if (read_size < file_info.st_size) {
+  } else if (read_size < file_size) {
     // Didn't get the whole file, drop everything. We don't want to return
     //   something partially read because, ie. with configs, the behaviour
     //   is undefined.
-    Error("%s Only able to read %d bytes out %d for %s file", module_name, read_size, (int)file_info.st_size, file_path);
+    Error("%s Only able to read %d bytes out %d for %s file", module_name, read_size, file_size, file_path);
     ats_free(file_buf);
     file_buf = nullptr;
   }

-- 
To stop receiving notification emails like this one, please contact
bcall@apache.org.