-
Notifications
You must be signed in to change notification settings - Fork 0
/
FuseFile.h
73 lines (58 loc) · 1.83 KB
/
FuseFile.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
72
73
#pragma once
#include <errno.h>
#include <iterator>
#include <unistd.h>
#include <mutex>
#include <functional>
namespace simplyfuse {
struct FuseFS;
// subclass this struct to implement functionality represented by a file
struct FuseFile {
FuseFile() = default;
FuseFile(FuseFile const&) = delete;
FuseFile(FuseFile &&) = delete;
FuseFile operator=(FuseFile const&) = delete;
FuseFile operator=(FuseFile&&) = delete;
virtual ~FuseFile();
virtual int onOpen();
virtual int onClose();
virtual int onRead(char* buf, std::size_t size, off_t offset);
virtual int onWrite(const char* buf, std::size_t size, off_t offset);
virtual std::size_t getSize();
virtual int onTruncate(off_t offset);
virtual int getFilePermissions();
friend class FuseFS;
protected:
FuseFS* fuseFS {nullptr};
};
struct SimpleROFile : FuseFile {
using FuseFile::FuseFile;
using ReadCallback = std::function<std::string(void)>;
SimpleROFile(std::string const& _content="", ReadCallback readCallback={}) : content{_content}, readCB{readCallback} {};
virtual ~SimpleROFile() = default;
int onRead(char* buf, std::size_t size, off_t offset) override;
int getFilePermissions() override;
void setContent(std::string const& content);
std::string const& getContent() const;
std::unique_lock<std::mutex> lock() const {
return std::unique_lock{mutex};
}
protected:
mutable std::mutex mutex;
std::string content;
ReadCallback readCB{};
};
struct SimpleWOFile : FuseFile {
using FuseFile::FuseFile;
virtual ~SimpleWOFile() = default;
int onTruncate(off_t offset);
int getFilePermissions() override;
};
struct SimpleRWFile : SimpleROFile {
using SimpleROFile::SimpleROFile;
virtual ~SimpleRWFile() = default;
int onWrite(const char* buf, std::size_t size, off_t offset);
int onTruncate(off_t offset);
int getFilePermissions() override;
};
} /* namespace simplyfile */