----- Original Message ----- From: "Dennis Jenkins" <[EMAIL PROTECTED]>
To: <sqlite-users@sqlite.org>
Sent: Friday, August 26, 2005 1:12 PM
Subject: Re: [sqlite] checking the database status


Robert Simpson wrote:

sqlite3_open() doesn't create/open a file for exclusive access off the bat.

If you want to atomically open a file that already exists,
open it yourself and while its open, call sqlite3_open(), then close your own handle to it.

If you want to atomically create a file and only open it if you created it, create it yourself and while its open, call sqlite3_open() then close your handle.

#1 should not work in all cases on Unix. Someone could unlink the file after you open it the first time and before you open it the second time. The first file will be removed when all open file handles to that file are closed.

So? If you open the file, that's 1 handle open. Someone unlinks it, but a handle is still open. sqlite3_open() then opens the file, that's 2 handles. You then close your handle and there's still 1 handle open until sqlite is done with it. I'm not a *nix programmer, so maybe I am missing something obvious.

#2.. Would that work if you opened the file exclusively? If you don't open it exclusively, theoretically someone else could open it too.

On Windows, sqlite3_open() calls the CreateFile() API with the OPEN_ALWAYS flag, which means if the file doesn't exist, then create it -- in either case, always open the file. To atomically create a file and make sqlite3 open it (at least in Windows) you would call CreateFile() with the CREATE_NEW flag, which will atomically fail if the file already exists. If it creates a new file however, you can then pass the filename to sqlite3_open() and then subsequently close your handle.

Unless I'm missing something, the only way to atomically create or open a file is to ask the OS to do that operation only once.

Maybe Dr. Hipp can elaborate on why we can pass the file open/creation flags into the sqlite3_open() function.

Maybe these flags do not exist on all platforms (in which case, why not just ignore the flags)? ?

Robert


Reply via email to