changeset 366:521812d63516 gcc-4.3-cleanups

check return values of read() and write(), erroring out on an error return value or a short read/write. (A stylish implementation of CHECKED_FOO() would detect short reads/writes and retry the failed portion, but...)
author mas01cr
date Wed, 12 Nov 2008 15:23:29 +0000
parents 6ff688bac7b7
children 3b6cd1dfbddb
files audioDB.h create.cpp insert.cpp query.cpp sample.cpp
diffstat 5 files changed, 34 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB.h	Wed Nov 12 14:05:26 2008 +0000
+++ b/audioDB.h	Wed Nov 12 15:23:29 2008 +0000
@@ -149,6 +149,26 @@
     var = (type) tmp; \
   }
 
+#define CHECKED_READ(fd, buf, count) \
+  { size_t tmpcount = count; \
+    ssize_t tmp = read(fd, buf, tmpcount); \
+    if(tmp == -1) { \
+      error("read error", "", "read"); \
+    } else if((size_t) tmp != tmpcount) {	\
+      error("short read", ""); \
+    } \
+  }
+
+#define CHECKED_WRITE(fd, buf, count) \
+  { size_t tmpcount = count; \
+    ssize_t tmp = write(fd, buf, tmpcount); \
+    if(tmp == -1) { \
+      error("write error", "", "write"); \
+    } else if((size_t) tmp != tmpcount) {	\
+      error("short write", ""); \
+    } \
+  }
+
 #define VERB_LOG(vv, ...) \
   if(verbosity > vv) { \
     fprintf(stderr, __VA_ARGS__); \
--- a/create.cpp	Wed Nov 12 14:05:26 2008 +0000
+++ b/create.cpp	Wed Nov 12 15:23:29 2008 +0000
@@ -78,7 +78,7 @@
     dbH->dbSize = dbH->l2normTableOffset;
   } 
 
-  write(dbfid, dbH, O2_HEADERSIZE);
+  CHECKED_WRITE(dbfid, dbH, O2_HEADERSIZE);
 
   // go to the location corresponding to the last byte
   if (lseek (dbfid, dbH->dbSize - 1, SEEK_SET) == -1)
--- a/insert.cpp	Wed Nov 12 14:05:26 2008 +0000
+++ b/insert.cpp	Wed Nov 12 15:23:29 2008 +0000
@@ -15,8 +15,10 @@
 }
 
 void audioDB::insert_data_vectors(off_t offset, void *buffer, size_t size) {
-  lseek(dbfid, dbH->dataOffset + offset, SEEK_SET);
-  write(dbfid, buffer, size);
+  if(lseek(dbfid, dbH->dataOffset + offset, SEEK_SET) == (off_t) -1) {
+    error("error seeking to offset", "", "lseek");
+  }
+  CHECKED_WRITE(dbfid, buffer, size);
 }
 
 void audioDB::insert(const char* dbName, const char* inFile) {
--- a/query.cpp	Wed Nov 12 14:05:26 2008 +0000
+++ b/query.cpp	Wed Nov 12 15:23:29 2008 +0000
@@ -239,7 +239,7 @@
     }
   }
 
-  read(trkfid, *data_buffer_p, trackTable[track] * sizeof(double) * dbH->dim);
+  CHECKED_READ(trkfid, *data_buffer_p, trackTable[track] * sizeof(double) * dbH->dim);
 }
 
 // These names deserve some unpicking.  The names starting with a "q"
--- a/sample.cpp	Wed Nov 12 14:05:26 2008 +0000
+++ b/sample.cpp	Wed Nov 12 15:23:29 2008 +0000
@@ -109,11 +109,15 @@
 
     /* FIXME: this seeking, reading and distance calculation should
        share more code with the query loop */
-    lseek(dbfid, dbH->dataOffset + trackOffsetTable[track1] * sizeof(double) + i1 * dbH->dim * sizeof(double), SEEK_SET);
-    read(dbfid, v1, dbH->dim * sequenceLength * sizeof(double));
+    if(lseek(dbfid, dbH->dataOffset + trackOffsetTable[track1] * sizeof(double) + i1 * dbH->dim * sizeof(double), SEEK_SET) == (off_t) -1) {
+      error("seek failure", "", "lseek");
+    }
+    CHECKED_READ(dbfid, v1, dbH->dim * sequenceLength * sizeof(double));
 
-    lseek(dbfid, dbH->dataOffset + trackOffsetTable[track2] * sizeof(double) + i2 * dbH->dim * sizeof(double), SEEK_SET);
-    read(dbfid, v2, dbH->dim * sequenceLength * sizeof(double));
+    if(lseek(dbfid, dbH->dataOffset + trackOffsetTable[track2] * sizeof(double) + i2 * dbH->dim * sizeof(double), SEEK_SET) == (off_t) -1) {
+      error("seek failure", "", "lseek");
+    }
+    CHECKED_READ(dbfid, v2, dbH->dim * sequenceLength * sizeof(double));
 
     v1norm = 0;
     v2norm = 0;