Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Posix_File.h
Go to the documentation of this file.
1#ifndef joedb_Posix_File_declared
2#define joedb_Posix_File_declared
3
5
6#include <fcntl.h>
7#include <unistd.h>
8#include <string>
9
10#ifndef F_OFD_SETLK
11#define JOEDB_HAS_BROKEN_POSIX_LOCKING
12#endif
13
14namespace joedb
15{
16 ////////////////////////////////////////////////////////////////////////////
17 class Posix_FD: public Abstract_File
18 ////////////////////////////////////////////////////////////////////////////
19 {
20 private:
21 int fd;
22 int lock(int command, short type, int64_t start, int64_t size);
23
24 protected:
25 bool try_exclusive_lock(int64_t start, int64_t size);
26
27 public:
28 static void throw_last_error(const char *action, const char *file_name);
29
30 Posix_FD(int fd, Open_Mode mode):
31 Abstract_File(mode),
32 fd(fd)
33 {
34 }
35
36 Posix_FD(const char *file_name, Open_Mode mode);
37
38 Posix_FD(const Posix_FD &) = delete;
39 Posix_FD &operator=(const Posix_FD &) = delete;
40
41 int64_t get_size() const override;
42 size_t pread(char *buffer, size_t size, int64_t offset) const override;
43 void pwrite(const char *buffer, size_t size, int64_t offset) override;
44
45 void sync() override;
46#if _POSIX_SYNCHRONIZED_IO > 0
47 void datasync() override;
48#endif
49
50 void shared_lock(int64_t start, int64_t size) override;
51 void exclusive_lock(int64_t start, int64_t size) override;
52 void unlock(int64_t start, int64_t size) noexcept override;
53
54 ~Posix_FD() override;
55 };
56
57 /// @ingroup journal
58 class Posix_File: public Posix_FD
59 {
60 public:
61 static constexpr bool lockable = true;
62#ifdef JOEDB_HAS_BROKEN_POSIX_LOCKING
63 static constexpr bool has_broken_posix_locking = true;
64#else
65 static constexpr bool has_broken_posix_locking = false;
66#endif
67
68 Posix_File(int fd, Open_Mode mode):
69 Posix_FD(fd, mode)
70 {
71 }
72
73 Posix_File(const char *file_name, Open_Mode mode);
74
75 Posix_File(const std::string &file_name, Open_Mode mode):
76 Posix_File(file_name.c_str(), mode)
77 {
78 }
79 };
80}
81
82#endif
virtual void datasync()
Write data durably (no file-size change)
~Posix_FD() override
size_t pread(char *buffer, size_t size, int64_t offset) const override
Read a range of bytes.
static void throw_last_error(const char *action, const char *file_name)
void pwrite(const char *buffer, size_t size, int64_t offset) override
Write a range of bytes. Extend file size if necessary.
Posix_FD & operator=(const Posix_FD &)=delete
bool try_exclusive_lock(int64_t start, int64_t size)
int64_t get_size() const override
Get the size of the file, or -1 if it is unknown.
void sync() override
Write data durably (including file-size change)
void unlock(int64_t start, int64_t size) noexcept override
Remove a lock. The range should match the range of a corresponding lock.
void shared_lock(int64_t start, int64_t size) override
Lock a range of bytes for reading (prevents writes, not reads)
void exclusive_lock(int64_t start, int64_t size) override
Lock a range of bytes for writing (prevents both writes and reads)
Posix_FD(const Posix_FD &)=delete
Posix_FD(int fd, Open_Mode mode)
Definition Posix_File.h:30
Posix_File(const std::string &file_name, Open_Mode mode)
Definition Posix_File.h:75
static constexpr bool lockable
Definition Posix_File.h:61
Posix_File(int fd, Open_Mode mode)
Definition Posix_File.h:68
static constexpr bool has_broken_posix_locking
Definition Posix_File.h:63
Open_Mode
Definition Open_Mode.h:8