BTree realization
[tedtools.git] / flatdb.c
index 05dbe34..10e1a00 100644 (file)
--- a/flatdb.c
+++ b/flatdb.c
@@ -116,13 +116,21 @@ FDBOpen(FDB *db, char *file, int readonly) {
        if ( readonly ) {
                db->readonly=1;
                db->fd = open(file, O_RDONLY);
+               if ( db->fd < 0 ) {
+                       tlog(TL_CRIT,"FDBOpen: open failed: %s",strerror(errno));
+                       return FDB_ERROR;
+               }
                if ( flock( db->fd, LOCK_SH ) < 0 ) {
                        tlog(TL_CRIT,"FDBOpen: flock failed: %s",strerror(errno));
                        close(db->fd);
                        return FDB_ERROR;
                } 
        } else {
-               db->fd = open(file, O_CREAT | O_RDWR, 0666);
+               db->fd = open(file, O_CREAT | O_RDWR);
+               if ( db->fd < 0 ) {
+                       tlog(TL_CRIT,"FDBOpen: open failed: %s",strerror(errno));
+                       return FDB_ERROR;
+               }
                if ( flock( db->fd, LOCK_EX ) < 0 ) {
                        tlog(TL_CRIT,"FDBOpen: flock failed: %s",strerror(errno));
                        close(db->fd);
@@ -130,12 +138,6 @@ FDBOpen(FDB *db, char *file, int readonly) {
                } 
        }
 
-       if ( db->fd < 0 ) {
-               memset(db, 0, sizeof(FDB));
-               tlog(TL_CRIT,"FDBOpen: open failed: %s", strerror(errno));
-               return FDB_ERROR;
-       }
-
        rc = read(db->fd, &header, sizeof(FDBHeader));
 
        if ( rc<0 ) {