jens@0
|
1 |
/*
|
jens@0
|
2 |
* File.h
|
jens@0
|
3 |
* Ottoman
|
jens@0
|
4 |
*
|
jens@0
|
5 |
* Created by Jens Alfke on 8/20/09.
|
jens@0
|
6 |
* Copyright 2009 Jens Alfke. All rights reserved.
|
jens@0
|
7 |
* BSD-Licensed: See the file "LICENSE.txt" for details.
|
jens@0
|
8 |
*/
|
jens@0
|
9 |
|
jens@0
|
10 |
#ifndef _MOOSEYARD_FILE_
|
jens@0
|
11 |
#define _MOOSEYARD_FILE_
|
jens@0
|
12 |
#include "Base.h"
|
jens@0
|
13 |
|
jens@0
|
14 |
namespace Mooseyard {
|
jens@0
|
15 |
|
jens@0
|
16 |
struct Blob;
|
jens@0
|
17 |
class MemoryMap;
|
jens@0
|
18 |
|
jens@0
|
19 |
/** An open file on disk; a thin wrapper around a Unix file descriptor. */
|
jens@0
|
20 |
class File {
|
jens@0
|
21 |
public:
|
jens@0
|
22 |
|
jens@0
|
23 |
struct Error {
|
jens@0
|
24 |
const int code;
|
jens@0
|
25 |
const char* const message;
|
jens@0
|
26 |
|
jens@0
|
27 |
Error(int c, const char *m) :code(c), message(m) { }
|
jens@0
|
28 |
Error(const char *m);
|
jens@0
|
29 |
};
|
jens@0
|
30 |
static const int kEOF = 10001;
|
jens@0
|
31 |
|
jens@0
|
32 |
|
jens@9
|
33 |
File (const char *filename, int oflag, bool locked =false) throw(Error);
|
jens@0
|
34 |
~File();
|
jens@0
|
35 |
|
jens@0
|
36 |
off_t length() const throw(Error);
|
jens@0
|
37 |
void setLength (off_t) throw(Error);
|
jens@0
|
38 |
|
jens@0
|
39 |
off_t position() const throw(Error);
|
jens@0
|
40 |
void setPosition (off_t) throw(Error);
|
jens@0
|
41 |
off_t setPositionToEnd (off_t bytesBefore =0) throw(Error);
|
jens@0
|
42 |
|
jens@0
|
43 |
void read (void *dst, size_t) throw(Error);
|
jens@0
|
44 |
size_t write (const void *src, size_t) throw(Error);
|
jens@0
|
45 |
|
jens@0
|
46 |
void readFrom (off_t where, void *dst, size_t) throw(Error);
|
jens@0
|
47 |
void writeTo (off_t where, const void *src, size_t) throw(Error);
|
jens@0
|
48 |
|
jens@0
|
49 |
size_t writeMultiple (Blob blobs[], int count) throw(Error);
|
jens@0
|
50 |
size_t writePadding (int alignment); // alignment should be a power of 2
|
jens@0
|
51 |
|
jens@0
|
52 |
MemoryMap* map();
|
jens@0
|
53 |
const MemoryMap* map() const {return const_cast<File*>(this)->map();}
|
jens@0
|
54 |
void mapRegion (off_t position, size_t length);
|
jens@0
|
55 |
const void* mappedPosition (off_t position) const;
|
jens@0
|
56 |
|
jens@0
|
57 |
void flush() throw(Error); // Regular fsync call
|
jens@0
|
58 |
void flushDisk() throw(Error); // Expensive F_FULLFSYNC call
|
jens@0
|
59 |
|
jens@0
|
60 |
template <typename T>
|
jens@0
|
61 |
void read (T& t) throw(Error) {read(&t,sizeof(t));}
|
jens@0
|
62 |
template <typename T>
|
jens@0
|
63 |
size_t write (const T& t) throw(Error) {return write(&t,sizeof(t));}
|
jens@0
|
64 |
template <typename T>
|
jens@0
|
65 |
void readFrom (off_t where, T &t) throw(Error) {readFrom(where,&t,sizeof(t));}
|
jens@0
|
66 |
template <typename T>
|
jens@0
|
67 |
void writeTo (off_t where, const T &t) throw(Error) {writeTo(where,&t,sizeof(t));}
|
jens@0
|
68 |
|
jens@0
|
69 |
bool hasPath (const char *path) const throw(Error);
|
jens@0
|
70 |
|
jens@0
|
71 |
static void unlink (const char *filename) throw(Error);
|
jens@0
|
72 |
static void rename (const char *srcFilename, const char *dstFilename) throw(Error);
|
jens@0
|
73 |
|
jens@0
|
74 |
|
jens@0
|
75 |
class Lock {
|
jens@0
|
76 |
public:
|
jens@0
|
77 |
Lock (File*, bool block =true) throw(Error);
|
jens@0
|
78 |
~Lock();
|
jens@0
|
79 |
bool isLocked() const {return _locked;}
|
jens@0
|
80 |
operator bool() const {return _locked;}
|
jens@0
|
81 |
bool retry();
|
jens@0
|
82 |
private:
|
jens@0
|
83 |
UNCOPYABLE(Lock);
|
jens@0
|
84 |
File *_file;
|
jens@0
|
85 |
int _mode;
|
jens@0
|
86 |
bool _locked;
|
jens@0
|
87 |
};
|
jens@0
|
88 |
|
jens@0
|
89 |
private:
|
jens@0
|
90 |
static int _open (const char *filename, int posixMode) throw(Error);
|
jbm@8
|
91 |
static int _check (int result) throw(Error);
|
jens@0
|
92 |
static void _checkRead (ssize_t result, size_t expectedSize) throw(Error);
|
jens@0
|
93 |
bool _lock (bool block);
|
jens@0
|
94 |
void _unlock();
|
jens@0
|
95 |
UNCOPYABLE(File);
|
jens@0
|
96 |
|
jens@0
|
97 |
const int _fd;
|
jens@0
|
98 |
mutable MemoryMap *_memoryMap;
|
jens@0
|
99 |
int _locked;
|
jens@0
|
100 |
friend class Lock;
|
jens@0
|
101 |
friend class MemoryMap;
|
jens@0
|
102 |
};
|
jens@0
|
103 |
|
jens@0
|
104 |
}
|
jens@0
|
105 |
|
jbm@8
|
106 |
#endif /* _MOOSEYARD_FILE_ */
|