forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Buffer.cpp
151 lines (135 loc) · 3.37 KB
/
Buffer.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Buffer.cpp
*
*/
#include "Tools/Buffer.h"
#include "Processor/BaseMachine.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
bool BufferBase::rewind = false;
void BufferBase::setup(ifstream* f, int length, const string& filename,
const char* type, const string& field)
{
file = f;
tuple_length = length;
data_type = type;
field_type = field;
this->filename = filename;
}
bool BufferBase::is_pipe()
{
struct stat buf;
if (stat(filename.c_str(), &buf) == 0)
return S_ISFIFO(buf.st_mode);
else
return false;
}
void BufferBase::seekg(int pos)
{
assert(not is_pipe());
#ifdef DEBUG_BUFFER
if (pos != 0)
printf("seek %d %s thread %d\n", pos, filename.c_str(),
BaseMachine::thread_num);
#endif
if (not file)
{
if (pos == 0)
return;
else
file = open();
}
file->seekg(header_length + pos * tuple_length);
if (file->eof() || file->fail())
{
// let it go in case we don't need it anyway
if (pos != 0)
try_rewind();
}
#ifdef DEBUG_BUFFER
printf("seek %d %d thread %d\n", pos, int(file->tellg()),
BaseMachine::thread_num);
#endif
next = BUFFER_SIZE;
}
void BufferBase::try_rewind()
{
assert(not is_pipe());
#ifndef INSECURE
string type;
if (field_type.size() and data_type.size())
type = (string)" of " + field_type + " " + data_type;
throw not_enough_to_buffer(type, filename);
#endif
file->clear(); // unset EOF flag
file->seekg(header_length);
if (file->peek() == ifstream::traits_type::eof())
throw runtime_error("empty file: " + filename);
if (!rewind)
cerr << "REWINDING - ONLY FOR BENCHMARKING" << endl;
rewind = true;
eof = true;
}
void BufferBase::prune()
{
// only prune in secure mode
#ifdef INSECURE
return;
#endif
if (is_pipe())
return;
if (file and (not file->good() or file->peek() == EOF))
purge();
else if (file and file->tellg() != header_length)
{
#ifdef VERBOSE
cerr << "Pruning " << filename << endl;
#endif
string tmp_name = filename + ".new";
ofstream tmp(tmp_name.c_str());
size_t start = file->tellg();
char buf[header_length];
file->seekg(0);
file->read(buf, header_length);
tmp.write(buf, header_length);
file->seekg(start);
tmp << file->rdbuf();
if (tmp.fail())
throw runtime_error(
"problem writing to " + tmp_name + " from "
+ to_string(start) + " of " + filename);
tmp.close();
file->close();
rename(tmp_name.c_str(), filename.c_str());
file->open(filename.c_str(), ios::in | ios::binary);
}
#ifdef VERBOSE
else
{
cerr << "Not pruning " << filename << " because it's ";
if (file)
cerr << "closed";
else
cerr << "unused";
cerr << endl;
}
#endif
}
void BufferBase::purge()
{
if (file and not is_pipe())
{
#ifdef VERBOSE
cerr << "Removing " << filename << endl;
#endif
unlink(filename.c_str());
file->close();
file = 0;
}
}
void BufferBase::check_tuple_length(int tuple_length)
{
if (tuple_length != this->tuple_length)
throw Processor_Error("inconsistent tuple length");
}