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