-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.h
71 lines (54 loc) · 2.07 KB
/
file.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Copyright (C) 2024 Patrick Rotsaert
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "locktypes.hpp"
#include <filesystem>
#include <memory>
#include <sys/stat.h>
namespace bitcask {
class file final
{
class impl;
std::unique_ptr<impl> pimpl_;
public:
explicit file(int fd, const std::filesystem::path& path);
~file() noexcept; // closes the descriptor
file(file&&) = delete;
file& operator=(file&&) = delete;
file(const file&) = delete;
file& operator=(const file&) = delete;
static std::unique_ptr<file> open(const std::filesystem::path& path, int flags, mode_t mode);
void reopen(int flags, mode_t mode);
const std::filesystem::path& path() const noexcept;
enum class read_mode
{
any,
zero_or_count,
count
};
// These methods lock the mutex and release it on return.
std::size_t read(void* buf, std::size_t count, read_mode mode) const;
void write(const void* buf, std::size_t count) const;
off64_t seek(off64_t offset, int whence) const;
off64_t seek(off64_t offset) const;
off64_t position() const;
off64_t size() const;
// Lock this instance.
// Use this lock if you need to perform several dependent operations. For example,
// to perform a seek and a write, first get a lock, then pass that lock to locked_seek and locked_write.
// For as long as the returned lock exists, do not call the non-locked methods obove. This would deadlock.
lock_type lock() const;
// Locked methods.
// Useful only if compiled with BITCASK_THREAD_SAFE
std::size_t locked_read(const lock_type&, void* buf, std::size_t count, read_mode mode) const;
void locked_write(const lock_type&, const void* buf, std::size_t count) const;
off64_t locked_seek(const lock_type&, off64_t offset, int whence) const;
off64_t locked_seek(const lock_type&, off64_t offset) const;
off64_t locked_position(const lock_type&) const;
off64_t locked_size(const lock_type&) const;
};
} // namespace bitcask