-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenfiles.cpp
More file actions
75 lines (62 loc) · 2.5 KB
/
openfiles.cpp
File metadata and controls
75 lines (62 loc) · 2.5 KB
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
#include "openfiles.hpp"
#include "util/synchronized.hpp"
#include "exception.hpp"
namespace Springy{
OpenFiles::OpenFiles(){}
OpenFiles::~OpenFiles(){}
int OpenFiles::add(boost::filesystem::path volumeFile, Springy::Volume::IVolume *volume, int internalFd, int flags, mode_t mode){
Synchronized syncOpenFiles(this->openFiles);
int fd = 0;
int *syncToken = NULL;
openFiles_set::index<of_idx_volumeFile>::type &idx = this->openFiles.get<of_idx_volumeFile>();
openFiles_set::index<of_idx_volumeFile>::type::iterator it = idx.find(volumeFile);
if (it != idx.end()) {
syncToken = it->o.syncToken;
} else {
syncToken = new int;
}
openFiles_set::index<of_idx_fd>::type &fdidx = this->openFiles.get<of_idx_fd>();
openFiles_set::index<of_idx_fd>::type::iterator fdit = fdidx.begin();
for(;fdit!=fdidx.end();fdit++, fd++){
if(fdit->o.fd != fd){
break;
}
}
OpenFiles::openFile of;
of.volumeFile = volumeFile;
of.volume = volume;
of.fd = internalFd;
of.flags = flags;
of.syncToken = syncToken;
openFileSetEntry ofse;
ofse.o = of;
ofse.fd = fd;
ofse.valid = true;
this->openFiles.insert(ofse);
return fd;
}
OpenFiles::openFile OpenFiles::getByDescriptor(int fd){
Synchronized syncOpenFiles(this->openFiles);
openFiles_set::index<of_idx_fd>::type &idx = this->openFiles.get<of_idx_fd>();
openFiles_set::index<of_idx_fd>::type::iterator it = idx.find(fd);
if (it == idx.end()) {
throw Springy::Exception(__FILE__, __PRETTY_FUNCTION__, __LINE__) << "bad descriptor";
}
return it->o;
}
void OpenFiles::remove(int fd){
Synchronized syncOpenFiles(this->openFiles);
openFiles_set::index<of_idx_fd>::type &idx = this->openFiles.get<of_idx_fd>();
openFiles_set::index<of_idx_fd>::type::iterator it = idx.find(fd);
if (it == idx.end()) {
throw Springy::Exception(__FILE__, __PRETTY_FUNCTION__, __LINE__) << "bad descriptor";
}
int *syncToken = it->o.syncToken;
boost::filesystem::path volumeFile = it->o.volumeFile;
idx.erase(it);
openFiles_set::index<of_idx_volumeFile>::type &vidx = this->openFiles.get<of_idx_volumeFile>();
if (vidx.find(volumeFile) == vidx.end()) {
delete syncToken;
}
}
}