- if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
- "tdb_open: failed to get open lock on %s: %s\n",
- name, strerror(errno));
- goto fail; /* errno set by tdb_brlock */
- }
-
- if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
- || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
- if (!(open_flags & O_CREAT)
- || tdb_new_database(tdb, seed, &hdr) == -1) {
- if (errno == 0) {
- errno = EIO; /* ie bad format or something */
- }
+ ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
+ if (ecode != TDB_SUCCESS) {
+ goto fail;
+ }
+
+ /* If they used O_TRUNC, read will return 0. */
+ rlen = read(tdb->fd, &hdr, sizeof(hdr));
+ if (rlen == 0 && (open_flags & O_CREAT)) {
+ ecode = tdb_new_database(tdb, seed, &hdr);
+ if (ecode != TDB_SUCCESS) {