changeset 410:d7e590d58c85 api-inversion

Pavlovian response to compiler warnings... ... attempt to squash them. For now we can get most of the way by writing a simple write_or_goto_error() macro for write(), and the equivalent for read(). One of the warnings, for the return value of chdir(), is silly, because we're already in an error case, and we really can't do anything sensible if the chdir fails. Try to deal with it anyway.
author mas01cr
date Thu, 11 Dec 2008 08:54:01 +0000
parents 99e6cbad7f76
children ad2206c24986
files audioDB-internals.h create.cpp dump.cpp insert.cpp l2norm.cpp
diffstat 5 files changed, 61 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB-internals.h	Tue Dec 09 22:48:30 2008 +0000
+++ b/audioDB-internals.h	Thu Dec 11 08:54:01 2008 +0000
@@ -32,6 +32,20 @@
     } \
   }
 
+#define write_or_goto_error(fd, buffer, size) \
+  { ssize_t tmp = size; \
+    if(write(fd, buffer, size) != tmp) { \
+      goto error; \
+    } \
+  }
+
+#define read_or_goto_error(fd, buffer, size) \
+  { ssize_t tmp = size; \
+    if(read(fd, buffer, size) != tmp) { \
+      goto error; \
+    } \
+  }
+
 static inline int audiodb_sync_header(adb_t *adb) {
   off_t pos;
   pos = lseek(adb->fd, (off_t) 0, SEEK_CUR);
--- a/create.cpp	Tue Dec 09 22:48:30 2008 +0000
+++ b/create.cpp	Thu Dec 11 08:54:01 2008 +0000
@@ -2,6 +2,8 @@
 extern "C" {
 #include "audioDB_API.h"
 }
+#include "audioDB-internals.h"
+
 /* Make a new database.
 
 IF size(featuredata) < O2_LARGE_ADB_SIZE
@@ -102,9 +104,7 @@
     header->dbSize = header->l2normTableOffset;
   }
 
-  if (write(fd, header, O2_HEADERSIZE) != O2_HEADERSIZE) {
-    goto error;
-  }
+  write_or_goto_error(fd, header, O2_HEADERSIZE);
 
   // go to the location corresponding to the last byte
   if (lseek (fd, header->dbSize - 1, SEEK_SET) == -1) {
@@ -112,9 +112,7 @@
   }
 
   // write a dummy byte at the last location
-  if (write (fd, "", 1) != 1) {
-      goto error;
-  }
+  write_or_goto_error(fd, "", 1);
 
   free(header);
   return audiodb_open(path, O_RDWR);
--- a/dump.cpp	Tue Dec 09 22:48:30 2008 +0000
+++ b/dump.cpp	Thu Dec 11 08:54:01 2008 +0000
@@ -135,9 +135,7 @@
       if ((ffd = open(fName, O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
         goto error;
       }
-      if ((write(ffd, &(adb->header->dim), sizeof(uint32_t))) < 0) {
-        goto error;
-      }
+      write_or_goto_error(ffd, &(adb->header->dim), sizeof(uint32_t));
       
       /* FIXME: this repeated malloc()/free() of data buffers is
 	 inefficient. */
@@ -155,9 +153,7 @@
         goto error;
       }
       
-      if ((write(ffd, data_buffer, data_buffer_size)) < 0) {
-        goto error;
-      }
+      write_or_goto_error(ffd, data_buffer, data_buffer_size);
       
       free(data_buffer);
       
@@ -189,12 +185,8 @@
 	if ((pfd = open(fName, O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
           goto error;
 	}
-	if ((write(pfd, &one, sizeof(uint32_t))) < 0) {
-          goto error;
-	}
-	if ((write(pfd, powerTable + pos, trackTable[k] * sizeof(double))) < 0) {
-          goto error;
-	}
+        write_or_goto_error(pfd, &one, sizeof(uint32_t));
+        write_or_goto_error(pfd, powerTable + pos, trackTable[k] * sizeof(double));
 	fprintf(pLFile, "%s\n", fName);
 	close(pfd);
         pfd = 0;
@@ -294,7 +286,8 @@
   maybe_munmap(powerFileNameTable, fileTableLength);
 
   if(directory_changed) {
-    chdir(cwd);
+    int gcc_warning_workaround = chdir(cwd);
+    directory_changed = gcc_warning_workaround;
   }
   return 1;
 }
--- a/insert.cpp	Tue Dec 09 22:48:30 2008 +0000
+++ b/insert.cpp	Thu Dec 11 08:54:01 2008 +0000
@@ -81,7 +81,7 @@
 static int audiodb_insert_datum_internal(adb_t *adb, adb_datum_internal_t *datum) {
 
   off_t size, offset, nfiles;
-  double *l2norm_buffer, *lp, *dp;
+  double *l2norm_buffer = NULL, *lp, *dp;  
 
   /* 1. check write permission; */
   if(!(adb->flags & O_RDWR)) {
@@ -133,48 +133,50 @@
   offset = adb->header->length;
   nfiles = adb->header->numFiles;
 
-  /* FIXME: checking for all these lseek()s and write()s */
+  /* FIXME: checking for all these lseek()s */
   lseek(adb->fd, adb->header->fileTableOffset + nfiles * O2_FILETABLE_ENTRY_SIZE, SEEK_SET);
-  write(adb->fd, datum->key, strlen(datum->key)+1);
+  write_or_goto_error(adb->fd, datum->key, strlen(datum->key)+1);
   lseek(adb->fd, adb->header->trackTableOffset + nfiles * O2_TRACKTABLE_ENTRY_SIZE, SEEK_SET);
-  write(adb->fd, &datum->nvectors, O2_TRACKTABLE_ENTRY_SIZE);
+  write_or_goto_error(adb->fd, &datum->nvectors, O2_TRACKTABLE_ENTRY_SIZE);
   if(adb->header->flags & O2_FLAG_LARGE_ADB) {
     char cwd[PATH_MAX];
     char slash = '/';
 
-    getcwd(cwd, PATH_MAX);
+    if(!getcwd(cwd, PATH_MAX)) {
+      goto error;
+    }
     lseek(adb->fd, adb->header->dataOffset + nfiles * O2_FILETABLE_ENTRY_SIZE, SEEK_SET);
     if(*((char *) datum->data) != '/') {
-      write(adb->fd, cwd, strlen(cwd));
-      write(adb->fd, &slash, 1);
+      write_or_goto_error(adb->fd, cwd, strlen(cwd));
+      write_or_goto_error(adb->fd, &slash, 1);
     }
-    write(adb->fd, datum->data, strlen((const char *) datum->data)+1);
+    write_or_goto_error(adb->fd, datum->data, strlen((const char *) datum->data)+1);
     if(datum->power) {
       lseek(adb->fd, adb->header->powerTableOffset + nfiles * O2_FILETABLE_ENTRY_SIZE, SEEK_SET);
       if(*((char *) datum->power) != '/') {
-        write(adb->fd, cwd, strlen(cwd));
-        write(adb->fd, &slash, 1);
+        write_or_goto_error(adb->fd, cwd, strlen(cwd));
+        write_or_goto_error(adb->fd, &slash, 1);
       }
-      write(adb->fd, datum->power, strlen((const char *) datum->power)+1);
+      write_or_goto_error(adb->fd, datum->power, strlen((const char *) datum->power)+1);
     }
     if(datum->times) {
       lseek(adb->fd, adb->header->timesTableOffset + nfiles * O2_FILETABLE_ENTRY_SIZE, SEEK_SET);
       if(*((char *) datum->times) != '/') {
-        write(adb->fd, cwd, strlen(cwd));
-        write(adb->fd, &slash, 1);
+        write_or_goto_error(adb->fd, cwd, strlen(cwd));
+        write_or_goto_error(adb->fd, &slash, 1);
       }
-      write(adb->fd, datum->times, strlen((const char *) datum->times)+1);
+      write_or_goto_error(adb->fd, datum->times, strlen((const char *) datum->times)+1);
     }
   } else {
     lseek(adb->fd, adb->header->dataOffset + offset, SEEK_SET);
-    write(adb->fd, datum->data, sizeof(double) * datum->nvectors * datum->dim);
+    write_or_goto_error(adb->fd, datum->data, sizeof(double) * datum->nvectors * datum->dim);
     if(datum->power) {
       lseek(adb->fd, adb->header->powerTableOffset + offset / datum->dim, SEEK_SET);
-      write(adb->fd, datum->power, sizeof(double) * datum->nvectors);
+      write_or_goto_error(adb->fd, datum->power, sizeof(double) * datum->nvectors);
     }
     if(datum->times) {
       lseek(adb->fd, adb->header->timesTableOffset + offset / datum->dim * 2, SEEK_SET);
-      write(adb->fd, datum->times, sizeof(double) * datum->nvectors * 2);
+      write_or_goto_error(adb->fd, datum->times, sizeof(double) * datum->nvectors * 2);
     }
   }
 
@@ -197,8 +199,9 @@
       lp++;
     }
     lseek(adb->fd, adb->header->l2normTableOffset + offset / datum->dim, SEEK_SET);
-    write(adb->fd, l2norm_buffer, sizeof(double) * datum->nvectors);
+    write_or_goto_error(adb->fd, l2norm_buffer, sizeof(double) * datum->nvectors);
     free(l2norm_buffer);
+    l2norm_buffer = NULL;
   }
 
   /* 8. update adb->keys and adb->header; */
@@ -210,6 +213,9 @@
   return audiodb_sync_header(adb);
 
  error:
+  if(l2norm_buffer) {
+    free(l2norm_buffer);
+  }
   return 1;
 }
 
@@ -252,14 +258,14 @@
   if(fstat(fd, &st)) {
     goto error;
   }
-  read(fd, &(datum->dim), sizeof(uint32_t));
+  read_or_goto_error(fd, &(datum->dim), sizeof(uint32_t));
   size = st.st_size - sizeof(uint32_t);
   datum->nvectors = size / (sizeof(double) * datum->dim);
   datum->data = (double *) malloc(size);
   if(!datum->data) {
     goto error;
   }
-  read(fd, datum->data, size);
+  read_or_goto_error(fd, datum->data, size);
   close(fd);
   fd = 0;
   if(insert->power) {
@@ -273,7 +279,7 @@
     if((st.st_size - sizeof(uint32_t)) != (size / datum->dim)) {
       goto error;
     }
-    read(fd, &dim, sizeof(uint32_t));
+    read_or_goto_error(fd, &dim, sizeof(uint32_t));
     if(dim != 1) {
       goto error;
     }
@@ -281,7 +287,7 @@
     if(!datum->power) {
       goto error;
     }
-    read(fd, datum->power, size / datum->dim);
+    read_or_goto_error(fd, datum->power, size / datum->dim);
     close(fd);
   }
   if(insert->times) {
@@ -337,10 +343,11 @@
       return 1;
     }
     if(fstat(fd, &st)) {
-      return 1;
+      goto error;
     }
-    read(fd, &(d.dim), sizeof(uint32_t));
+    read_or_goto_error(fd, &(d.dim), sizeof(uint32_t));
     close(fd);
+    fd = 0;
     size = st.st_size - sizeof(uint32_t);
     d.nvectors = size / (sizeof(double) * d.dim);
     d.data = (void *) insert->features;
@@ -364,6 +371,11 @@
     } else {
       return err;
     }
+  error:
+    if(fd) {
+      close(fd);
+    }
+    return 1;
   } else {
     adb_datum_t datum;
     int err;
--- a/l2norm.cpp	Tue Dec 09 22:48:30 2008 +0000
+++ b/l2norm.cpp	Thu Dec 11 08:54:01 2008 +0000
@@ -34,9 +34,7 @@
   if(lseek(adb->fd, adb->header->l2normTableOffset, SEEK_SET) == (off_t) -1) {
     goto error;
   }
-  if(write(adb->fd, l2norm_buffer, nvectors * sizeof(double)) != (ssize_t) (nvectors * sizeof(double))) {
-    goto error;
-  }
+  write_or_goto_error(adb->fd, l2norm_buffer, nvectors * sizeof(double));
 
   munmap(data_buffer, data_buffer_size);
   free(l2norm_buffer);