forked from cloudius-systems/osv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.hh
72 lines (60 loc) · 1.53 KB
/
fs.hh
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
/*
* Copyright (C) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#ifndef FS_HH
#define FS_HH
#include <dirent.h>
#include <string>
#include <cstdint>
#include <boost/intrusive_ptr.hpp>
#include <osv/file.h>
#include <utility>
static inline void intrusive_ptr_add_ref(file *fp)
{
fhold(fp);
}
static inline void intrusive_ptr_release(file *fp)
{
fdrop(fp);
}
typedef boost::intrusive_ptr<file> fileref;
namespace std {
template<>
struct hash<fileref> {
size_t operator()(const fileref& fp) const {
return hash<file*>()(fp.get());
}
};
}
fileref fileref_from_fd(int fd);
fileref fileref_from_fname(std::string name);
uint64_t size(fileref f);
void read(fileref f, void *buffer, uint64_t offset, uint64_t len);
void write(fileref f, const void* buffer, uint64_t offset, uint64_t len);
template <class file_type, typename... args>
fileref
make_file(args&&... a)
{
file* fp = new (std::nothrow) file_type(std::forward<args>(a)...);
if (!fp) {
throw ENOMEM;
}
return fileref(fp, false);
}
class fdesc {
public:
explicit fdesc() : _fd(-1) {}
explicit fdesc(int fd) : _fd(fd) {}
explicit fdesc(file* f);
explicit fdesc(const fileref& f) : fdesc(f.get()) {}
// fdesc(fileref&& f) would be nice, but we can't implement it
~fdesc();
int get() { return _fd; }
int release() { int fd = -1; std::swap(fd, _fd); return fd; }
private:
int _fd;
};
#endif