Joedb 9.5.0
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
9#ifndef F_OFD_SETLK
10#define JOEDB_HAS_BROKEN_POSIX_LOCKING
11#endif
12
13namespace joedb
14{
15 ////////////////////////////////////////////////////////////////////////////
16 class Posix_FD: public Buffered_File
17 ////////////////////////////////////////////////////////////////////////////
18 {
19 private:
20 int fd;
21 int lock(int command, short type, int64_t start, int64_t size);
22
23 protected:
24 bool try_exclusive_lock(int64_t start, int64_t size);
25
26 public:
27 static void throw_last_error(const char *action, const char *file_name);
28
29 Posix_FD(int fd, Open_Mode mode):
30 Buffered_File(mode),
31 fd(fd)
32 {
33 }
34
35 Posix_FD(const char *file_name, Open_Mode mode);
36
37 Posix_FD(const Posix_FD &) = delete;
38 Posix_FD &operator=(const Posix_FD &) = delete;
39
40 int64_t get_size() const override;
41 size_t pread(char *buffer, size_t size, int64_t offset) const override;
42 void pwrite(const char *buffer, size_t size, int64_t offset) override;
43
44 void sync() override;
45#if _POSIX_SYNCHRONIZED_IO > 0
46 void datasync() override;
47#endif
48
49 void shared_lock(int64_t start, int64_t size) override;
50 void exclusive_lock(int64_t start, int64_t size) override;
51 void unlock(int64_t start, int64_t size) noexcept override;
52
53 ~Posix_FD() override;
54 };
55
56 /// @ingroup journal
57 class Posix_File: public Posix_FD
58 {
59 public:
60 static constexpr bool lockable = true;
61
62 Posix_File(int fd, Open_Mode mode):
63 Posix_FD(fd, mode)
64 {
65 }
66
67 Posix_File(const char *file_name, Open_Mode mode);
68
69 Posix_File(const std::string &file_name, Open_Mode mode):
70 Posix_File(file_name.c_str(), mode)
71 {
72 }
73 };
74}
75
76#endif
virtual void datasync()
Write data durably to permanent storage.
~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 and meta-data (such as file size) durably to permanent storage.
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:29
Posix_File(const std::string &file_name, Open_Mode mode)
Definition Posix_File.h:69
static constexpr bool lockable
Definition Posix_File.h:60
Posix_File(int fd, Open_Mode mode)
Definition Posix_File.h:62
Open_Mode
Definition Open_Mode.h:8
Definition Blob.h:7