annotate libtests/0001/prog1.c @ 392:78fed0d4c108 api-inversion

Include some necessary information in struct adb. Now the struct adb contains a database fd, the flags used to open that fd (so that we can later tell if it was for write or not) and a database header pointer. audiodb_open() is now responsible for filling in all of that information. To do that, it needs to take an open(2) flag; that's good, because it means that the call to open(2) is no longer invoking undefined behaviour. (Also, the previous version of audiodb_open() leaked an fd). Unfortunately, that means we have broken ABI and API compatibility. (Fortunately, we have fewer than 12 users). Use audiodb_open() in audioDB::initDBHeader(). We've temporarily(?) put acquire_lock(int, bool) in the API header; that means we need to include <stdbool.h> and compile C files with -std=c99. Do so. Make audiodb_close() free resources allocated by audiodb_open(). Include a struct adb * field in the audioDB C++ object... ... which lets us actually implement memory-correctness, by audiodb_close()ing the database in audioDB::cleanup(). [ The lock is, I think, correctly disposed of; man fcntl(2) on Linux says that the locks are released once any file descriptor relating to the file is closed, and we close the fd in audiodb_close(). ]
author mas01cr
date Mon, 24 Nov 2008 15:42:15 +0000
parents 94c18f128ce8
children e072aa1611f5
rev   line source
mas01ik@355 1 #include <stdio.h>
mas01ik@355 2 #include <stdlib.h>
mas01ik@355 3 #include <string.h>
mas01ik@355 4 #include <sysexits.h>
mas01ik@355 5 #include <fcntl.h>
mas01ik@355 6 #include <dirent.h>
mas01ik@355 7 #include <unistd.h>
mas01ik@355 8 #include <sys/stat.h>
mas01ik@355 9 #include <errno.h>
mas01ik@355 10 /*
mas01ik@355 11 * * #define NDEBUG
mas01ik@355 12 * * */
mas01ik@355 13 #include <assert.h>
mas01ik@355 14
mas01ik@355 15 #include "../../audioDB_API.h"
mas01ik@355 16 #include "../test_utils_lib.h"
mas01ik@355 17
mas01ik@355 18
mas01ik@355 19 int main(int argc, char **argv){
mas01ik@355 20
mas01ik@355 21 int returnval=0;
mas01ik@355 22 adb_ptr mydbp={0};
mas01ik@355 23 adb_ptr mydbp2={0};
mas01ik@355 24 struct stat statbuf;
mas01ik@355 25 int statval=0;
mas01ik@355 26
mas01ik@355 27 char * databasename="testdb";
mas01ik@355 28
mas01ik@355 29 //if [ -f testdb ]; then rm -f testdb; fi
mas01ik@355 30 /* remove old directory */
mas01ik@355 31 clean_remove_db(databasename);
mas01ik@355 32
mas01ik@355 33 /* create new db */
mas01ik@355 34 //# creation
mas01ik@355 35 //${AUDIODB} -N -d testdb
mas01cr@392 36 mydbp=audiodb_open(databasename,O_RDWR);
mas01ik@355 37
mas01ik@355 38
mas01ik@355 39 /* open should fail (return NULL), so create a new db */
mas01ik@355 40 if (!mydbp){
mas01ik@355 41 mydbp=audiodb_create(databasename,0,0,0);
mas01ik@355 42 }
mas01ik@355 43
mas01ik@355 44
mas01ik@355 45
mas01ik@355 46 if (!mydbp){
mas01ik@355 47 printf("fail\n");
mas01ik@355 48 returnval=-1;
mas01ik@355 49 }
mas01ik@355 50
mas01ik@355 51
mas01ik@355 52 /* stat testdb - let's make sure that it is there */
mas01ik@355 53 //stat testdb
mas01ik@355 54 statval=stat(databasename, &statbuf);
mas01ik@355 55
mas01ik@355 56 if (statval){
mas01ik@355 57 returnval=-1;
mas01ik@355 58 }
mas01ik@355 59
mas01ik@355 60 audiodb_close(mydbp);
mas01ik@355 61
mas01ik@355 62 /* try to create should fail, because db exists now */
mas01ik@355 63 mydbp2=audiodb_create(databasename,0,0,0);
mas01ik@355 64
mas01ik@355 65 if (mydbp2){
mas01ik@355 66 returnval=-1;
mas01ik@355 67 }
mas01ik@355 68
mas01ik@355 69
mas01ik@355 70 /* should pass now - db exists */
mas01ik@355 71 //expect_clean_error_exit ${AUDIODB} -N -d testdb
mas01cr@392 72 mydbp2=audiodb_open(databasename, O_RDONLY);
mas01ik@355 73 if (!mydbp2){
mas01ik@355 74 returnval=-1;
mas01ik@355 75 }
mas01ik@355 76
mas01ik@355 77 //this test would fail at compile time because of the API interface
mas01ik@355 78 //# should fail (no db given)
mas01ik@355 79 //expect_clean_error_exit ${AUDIODB} -N
mas01ik@355 80
mas01ik@355 81
mas01ik@355 82 audiodb_close(mydbp2);
mas01ik@355 83
mas01ik@355 84 // printf("returnval:%d\n",returnval);
mas01ik@355 85
mas01ik@355 86 return(returnval);
mas01ik@355 87 }
mas01ik@355 88