浏览代码

raise errors properly

David Rose 17 年之前
父节点
当前提交
5e838f0406
共有 2 个文件被更改,包括 38 次插入20 次删除
  1. 37 19
      dtool/src/dtoolutil/pandaFileStreamBuf.cxx
  2. 1 1
      dtool/src/dtoolutil/pandaFileStreamBuf.h

+ 37 - 19
dtool/src/dtoolutil/pandaFileStreamBuf.cxx

@@ -337,19 +337,31 @@ seekpos(streampos pos, ios_openmode which) {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 int PandaFileStreamBuf::
 int PandaFileStreamBuf::
 overflow(int ch) {
 overflow(int ch) {
+  bool okflag = true;
+
   size_t n = pptr() - pbase();
   size_t n = pptr() - pbase();
   if (n != 0) {
   if (n != 0) {
-    write_chars(pbase(), n);
-    pbump(-(int)n);
+    size_t wrote = write_chars(pbase(), n);
+    pbump(-(int)wrote);
+    if (wrote != n) {
+      okflag = false;
+    }
   }
   }
 
 
-  if (ch != EOF) {
-    // Store the extra character back in the buffer.
-    assert(pptr() != epptr());
-    *(pptr()) = ch;
-    pbump(1);
+  if (okflag && ch != EOF) {
+    if (pptr() != epptr()) {
+      // Store the extra character back in the buffer.
+      *(pptr()) = ch;
+      pbump(1);
+    } else {
+      // No room to store ch.
+      okflag = false;
+    }
   }
   }
 
 
+  if (!okflag) {
+    return EOF;
+  }
   return 0;
   return 0;
 }
 }
 
 
@@ -363,9 +375,12 @@ int PandaFileStreamBuf::
 sync() {
 sync() {
   size_t n = pptr() - pbase();
   size_t n = pptr() - pbase();
 
 
-  write_chars(pbase(), n);
-  pbump(-(int)n);
+  size_t wrote = write_chars(pbase(), n);
+  pbump(-(int)wrote);
 
 
+  if (n != wrote) {
+    return EOF;
+  }
   return 0;
   return 0;
 }
 }
 
 
@@ -485,11 +500,11 @@ read_chars(char *start, size_t length) {
 //  Description: Outputs the indicated stream of characters to the
 //  Description: Outputs the indicated stream of characters to the
 //               current file position.
 //               current file position.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-void PandaFileStreamBuf::
+size_t PandaFileStreamBuf::
 write_chars(const char *start, size_t length) {
 write_chars(const char *start, size_t length) {
   if (length == 0) {
   if (length == 0) {
     // Trivial no-op.
     // Trivial no-op.
-    return;
+    return 0;
   }
   }
 
 
   // Make sure the read buffer is flushed.
   // Make sure the read buffer is flushed.
@@ -518,7 +533,7 @@ write_chars(const char *start, size_t length) {
         << "Error writing " << length
         << "Error writing " << length
         << " bytes to " << _filename << ", windows error code 0x" << hex
         << " bytes to " << _filename << ", windows error code 0x" << hex
         << error << dec << ".  Disk full?\n";
         << error << dec << ".  Disk full?\n";
-      return;
+      return bytes_written;
     }
     }
     success = GetOverlappedResult(_handle, &overlapped, &bytes_written, false);
     success = GetOverlappedResult(_handle, &overlapped, &bytes_written, false);
   }
   }
@@ -531,26 +546,29 @@ write_chars(const char *start, size_t length) {
     if (lseek(_fd, _ppos, SEEK_SET) == -1) {
     if (lseek(_fd, _ppos, SEEK_SET) == -1) {
       cerr
       cerr
         << "Error seeking to position " << _ppos << " in " << _filename << "\n";
         << "Error seeking to position " << _ppos << " in " << _filename << "\n";
-      return;
+      return 0;
     }
     }
   }
   }
-  
-  while (length > 0) {
-    ssize_t result = ::write(_fd, start, length);
+
+  size_t remaining = length;
+  while (remaining > 0) {
+    ssize_t result = ::write(_fd, start, remaining);
     if (result < 0) {
     if (result < 0) {
       if (errno == EAGAIN) {
       if (errno == EAGAIN) {
         thread_yield();
         thread_yield();
       } else {
       } else {
         cerr
         cerr
-          << "Error writing " << length << " bytes to " << _filename << "\n";
-        return;
+          << "Error writing " << remaining << " bytes to " << _filename << "\n";
+        return length - remaining;
       }
       }
       continue;
       continue;
     }
     }
     
     
     start += result;
     start += result;
-    length -= result;
+    remaining -= result;
     _ppos += result;
     _ppos += result;
   }
   }
 #endif  // _WIN32
 #endif  // _WIN32
+
+  return length;
 }
 }

+ 1 - 1
dtool/src/dtoolutil/pandaFileStreamBuf.h

@@ -46,7 +46,7 @@ protected:
 
 
 private:
 private:
   size_t read_chars(char *start, size_t length);
   size_t read_chars(char *start, size_t length);
-  void write_chars(const char *start, size_t length);
+  size_t write_chars(const char *start, size_t length);
 
 
 private:
 private:
   string _filename;
   string _filename;