mkstemp (equivalent?) and berkely dbopen

mkstemp (equivalent?) and berkely dbopen

Post by Ollie Coo » Wed, 09 Jul 2003 04:00:55



Hi,

I would like to create a database using dbopen(3) with a random unused
filename, and to copy that over the 'live' database once it's been
completely flushed to disk, in an effort to always have the live
database containing valid data.

What is the safest way of achieving this? Obviously using mktemp(3) to
generate a filename, and then opening that file contains a race
condition and should be avoided.

Any pointers in the right direction would be greatfully received. I'm
programming in C on FreeBSD, but portable options would be preferred.

Cheers,

Ollie

--
Ollie Cook    Systems Administrator, Claranet UK

 
 
 

mkstemp (equivalent?) and berkely dbopen

Post by Jens Schweikhard » Wed, 09 Jul 2003 16:27:33




# Hi,
#
# I would like to create a database using dbopen(3) with a random unused
# filename, and to copy that over the 'live' database once it's been
# completely flushed to disk, in an effort to always have the live
# database containing valid data.
#
# What is the safest way of achieving this? Obviously using mktemp(3) to
# generate a filename, and then opening that file contains a race
# condition and should be avoided.

What exactly is the race for your app?

# Any pointers in the right direction would be greatfully received. I'm
# programming in C on FreeBSD, but portable options would be preferred.

Does mkstemp(3) solve your problem?

Regards,

        Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)

 
 
 

mkstemp (equivalent?) and berkely dbopen

Post by Ollie Coo » Wed, 09 Jul 2003 18:21:07



Quote:> What exactly is the race for your app?

Overwriting a db file causes processes which want to read it to not be
able to as the data obviously becomes inconsistent.

The data has to always be available to client applications so the
solution is to write to a temporary file and move that over the top
atomically.

Quote:> # Any pointers in the right direction would be greatfully received. I'm
> # programming in C on FreeBSD, but portable options would be preferred.

> Does mkstemp(3) solve your problem?

mkstemp is not suitable since it returns a file descriptor, not a DB
pointer. dbopen(3) explains the interface to dbopen.

Thanks for your reply.

Ollie

--
Ollie Cook    Systems Administrator, Claranet UK