Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115(* Takes the SHARED lock sqlite3 takes to read the database named by argv(1), in the steps of os_unix.c unixLock: a read lock on the PENDING byte, a read lock on the SHARED range, then the PENDING byte let go. sqlite3 takes it before every read transaction, in rollback mode and in WAL mode alike, so a process refused it is refused both reading and writing the file.
A separate executable because POSIX record locks do not exclude within a process: only another process meets the locks this engine's handle holds.
The exit status is sqlite3's own result code for what it met: [0] the SHARED lock was granted (SQLITE_OK), [5] it was refused (SQLITE_BUSY). With [hold] as argv(2), a granted lock is kept: the process writes [held] on stdout and keeps it until its stdin ends, as a sqlite3 reader inside a read transaction keeps it. With [reserved], it goes on to take RESERVED, as a sqlite3 process beginning a write does, and answers the same way; with [reserved hold], it keeps RESERVED as [hold] keeps SHARED. With [probe N], it asks only for a read lock on the byte at offset [N], as unixLock's first step to SHARED asks for one on the PENDING byte, and answers the same way: [5] where another process holds a write lock over that byte, [0] where none does. *)
(* sqlite/src/os.h: PENDING_BYTE is 0x40000000, RESERVED_BYTE the byte after it, and the SHARED range the 510 bytes after that. *)let pending_byte = Btree.Pager.pending_bytelet shared_first = pending_byte + 2let shared_size = 510
let read_lock fd ~at ~len = ignore (Unix.lseek fd at Unix.SEEK_SET : int); Unix.lockf fd Unix.F_TRLOCK len
let unlock fd ~at ~len = ignore (Unix.lseek fd at Unix.SEEK_SET : int); Unix.lockf fd Unix.F_ULOCK len
let shared path ~hold = let fd = Unix.openfile path [ Unix.O_RDWR ] 0 in match read_lock fd ~at:pending_byte ~len:1; read_lock fd ~at:shared_first ~len:shared_size; unlock fd ~at:pending_byte ~len:1 with | () -> if hold then begin print_endline "held"; try ignore (input_line stdin : string) with End_of_file -> () end; exit 0 | exception Unix.Unix_error ((Unix.EAGAIN | Unix.EACCES), _, _) -> exit 5
(* SHARED, then RESERVED as os_unix.c unixLock takes it on the way to a write: a write lock on the RESERVED byte, which coexists with other processes' SHARED and with nothing that holds RESERVED or more. *)let reserved ?(hold = false) path ~pending = let fd = Unix.openfile path [ Unix.O_RDWR ] 0 in match read_lock fd ~at:pending_byte ~len:1; read_lock fd ~at:shared_first ~len:shared_size; unlock fd ~at:pending_byte ~len:1; ignore (Unix.lseek fd (pending_byte + 1) Unix.SEEK_SET : int); Unix.lockf fd Unix.F_TLOCK 1; if hold && not pending then begin print_endline "held"; try ignore (input_line stdin : string) with End_of_file -> () end; if pending then begin ignore (Unix.lseek fd pending_byte Unix.SEEK_SET : int); Unix.lockf fd Unix.F_TLOCK 1; print_endline "held"; try ignore (input_line stdin : string) with End_of_file -> () end with | () -> exit 0 | exception Unix.Unix_error ((Unix.EAGAIN | Unix.EACCES), _, _) -> exit 5
let probe path offset = let fd = Unix.openfile path [ Unix.O_RDONLY ] 0 in match read_lock fd ~at:offset ~len:1 with | () -> exit 0 | exception Unix.Unix_error ((Unix.EAGAIN | Unix.EACCES), _, _) -> exit 5
(* lockingv3 section 3: EXCLUSIVE spans PENDING, RESERVED and SHARED. *)let exclusive path = let fd = Unix.openfile path [ Unix.O_RDWR ] 0 in match ignore (Unix.lseek fd pending_byte Unix.SEEK_SET : int); Unix.lockf fd Unix.F_TLOCK (shared_size + 2) with | () -> print_endline "held"; (try ignore (input_line stdin : string) with End_of_file -> ()); Unix.close fd; exit 0 | exception Unix.Unix_error ((Unix.EAGAIN | Unix.EACCES), _, _) -> exit 5
let usage () = prerr_endline "usage: db_shared.exe PATH [hold | reserved [hold] | pending | exclusive | \ probe OFFSET]"; exit 2
let () = match Sys.argv with | [| _; path |] -> shared path ~hold:false | [| _; path; "hold" |] -> shared path ~hold:true | [| _; path; "reserved" |] -> reserved path ~pending:false | [| _; path; "reserved"; "hold" |] -> reserved ~hold:true path ~pending:false | [| _; path; "pending" |] -> reserved path ~pending:true | [| _; path; "exclusive" |] -> exclusive path | [| _; path; "probe"; offset |] -> ( match int_of_string_opt offset with | Some offset -> probe path offset | None -> usage ()) | _ -> usage ()