Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Portable_File.cpp
Go to the documentation of this file.
3
4namespace joedb::detail
5{
6 /////////////////////////////////////////////////////////////////////////////
7 Portable_File_Buffer::Portable_File_Buffer
8 /////////////////////////////////////////////////////////////////////////////
9 (
10 const char * const file_name,
11 const Open_Mode mode
12 )
13 {
14 constexpr auto in = std::ios::binary | std::ios::in;
15
16 if (mode == Open_Mode::read_existing)
17 filebuf.open(file_name, in);
18 else if (mode == Open_Mode::write_existing)
19 filebuf.open(file_name, in | std::ios::out);
20 else if (mode == Open_Mode::create_new)
21 {
22 if (filebuf.open(file_name, in))
23 throw Exception("File already exists: " + std::string(file_name));
24 filebuf.open(file_name, in | std::ios::out | std::ios::trunc);
25 }
26 else if
27 (
31 )
32 {
33 filebuf.open(file_name, in | std::ios::out) ||
34 filebuf.open(file_name, in | std::ios::out | std::ios::trunc);
35 }
36 else if (mode == Open_Mode::truncate)
37 filebuf.open(file_name, in | std::ios::out | std::ios::trunc);
38 else
39 {
40 throw Exception
41 (
42 std::string(file_name) + ": unsupported mode for Portable_File"
43 );
44 }
45
46 if (!filebuf.is_open())
47 throw Exception("Cannot open file: " + std::string(file_name));
48 }
49}
Open_Mode
Definition Open_Mode.h:8
@ truncate
create new file, or truncate existing file, and locks the file
@ create_new
fails if already exists, locks the file for writing
@ write_existing
fails if does not exist or locked, locks the file for writing
@ shared_write
like write_existing_or_create_new, but does not lock the file, and does not fail if locked
@ write_existing_or_create_new
either write_existing or create_new depending on whether the file exists. Racy in Posix,...
@ write_lock
like write_existing_or_create_new, but waits instead of failing if already locked
@ read_existing
fails if does not exist