-
Notifications
You must be signed in to change notification settings - Fork 9
/
file.hpp
101 lines (94 loc) · 2.72 KB
/
file.hpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <linux/fs.h>
#include <memory>
#include <string>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
class FileHandle {
private:
int fd_ = -1;
public:
FileHandle(int fd)
: fd_(fd) {};
FileHandle(const FileHandle&) = delete;
static std::shared_ptr<FileHandle> open(const std::string& path, int flags = O_RDONLY, mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
{
auto fd = ::open(path.c_str(), flags, mode);
if (fd < 0) {
std::cerr << "Failed to open " << path << " " << strerror(errno) << std::endl;
return nullptr;
}
return std::make_shared<FileHandle>(fd);
}
virtual ~FileHandle() { close(); }
void close()
{
if (fd_ > 0) {
::close(fd_);
fd_ = -1;
}
}
bool is_open() const { return fd_ >= 0; }
struct stat stat() const
{
struct stat st;
std::memset(&st, 0, sizeof(st));
if (0 != ::fstat(fd_, &st)) {
std::cerr << "fstat error" << std::endl;
}
if (S_ISBLK(st.st_mode)) {
uint64_t size;
if (0 > ioctl(fd_, BLKGETSIZE64, &size)) {
std::cerr << "ioctl error" << std::endl;
}
st.st_blksize = 512;
st.st_blocks = size / st.st_blksize;
st.st_size = st.st_blksize * st.st_blocks;
}
return st;
}
uint64_t size() const { return stat().st_size; }
bool seek(off64_t offset) { return offset == ::lseek64(fd_, offset, SEEK_SET); }
bool truncate(off64_t offset = 0) { return 0 == ::ftruncate(fd_, offset); }
bool lock(int operation) { return 0 == ::flock(fd_, operation); }
bool sync() { return 0 == ::syncfs(fd_); }
int read(uint8_t* data, size_t size)
{
auto tsize = size;
while (size) {
auto rsize = ::read(fd_, data, size);
if (rsize == 0) {
return tsize - size;
} else if (rsize < 0) {
return -1;
}
size -= rsize, data += rsize;
}
return tsize;
}
int write(const uint8_t* data, size_t size)
{
auto tsize = size;
while (size) {
auto wsize = ::write(fd_, data, size);
if (wsize < 0) {
return tsize > size ? tsize - size : -1;
}
size -= wsize, data += wsize;
}
return tsize;
}
int write(const std::string& buffer) { return ::write(fd_, buffer.c_str(), buffer.size()); }
int fd() { return fd_; }
int release()
{
auto fd = fd_;
fd_ = -1;
return fd;
}
};