-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathgenerated_output.cc
More file actions
335 lines (305 loc) · 10.4 KB
/
Copy pathgenerated_output.cc
File metadata and controls
335 lines (305 loc) · 10.4 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "generated_output.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <fstream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#ifdef _WIN32
# include <direct.h>
# include <process.h>
# include <windows.h>
# define sc_getpid _getpid
# define sc_rmdir _rmdir
#else
# include <unistd.h>
# define sc_getpid getpid
# define sc_rmdir rmdir
#endif
namespace {
const char manifestName[] = ".exp2cxx-manifest";
const char manifestHeader[] = "exp2cxx generated output manifest v1";
struct OutputRecord {
std::string temporary;
};
class OutputManager {
std::map<std::string, OutputRecord> _outputs;
std::map<FILE *, std::string> _openFiles;
unsigned long _sequence;
bool _active;
bool _failed;
static bool safePath( const std::string & path ) {
if( path.empty() || path[0] == '/' || path[0] == '\\' ||
path.find( ':' ) != std::string::npos ) {
return false;
}
size_t start = 0;
while( start <= path.size() ) {
const size_t end = path.find_first_of( "/\\", start );
const std::string part = path.substr(
start, end == std::string::npos ? end : end - start );
if( part.empty() || part == "." || part == ".." ) {
return false;
}
if( end == std::string::npos ) {
break;
}
start = end + 1;
}
return true;
}
std::string temporaryName( const std::string & target ) {
std::ostringstream name;
name << target << ".exp2cxx-tmp-" << sc_getpid() << '-'
<< ++_sequence;
return name.str();
}
static bool copyFile( const std::string & source,
const std::string & destination ) {
std::ifstream input( source.c_str(), std::ios::binary );
std::ofstream output( destination.c_str(), std::ios::binary );
if( !output ) {
return false;
}
if( input ) {
output << input.rdbuf();
}
return output.good();
}
static bool sameContents( const std::string & first,
const std::string & second ) {
std::ifstream left( first.c_str(), std::ios::binary );
std::ifstream right( second.c_str(), std::ios::binary );
if( !left || !right ) {
return false;
}
char leftBuffer[65536];
char rightBuffer[65536];
do {
left.read( leftBuffer, sizeof( leftBuffer ) );
right.read( rightBuffer, sizeof( rightBuffer ) );
const std::streamsize leftCount = left.gcount();
const std::streamsize rightCount = right.gcount();
if( leftCount != rightCount ||
memcmp( leftBuffer, rightBuffer,
static_cast<size_t>( leftCount ) ) ) {
return false;
}
} while( left );
return right.peek() == std::ifstream::traits_type::eof();
}
static bool replaceFile( const std::string & temporary,
const std::string & target ) {
#ifdef _WIN32
return MoveFileExA( temporary.c_str(), target.c_str(),
MOVEFILE_REPLACE_EXISTING |
MOVEFILE_WRITE_THROUGH ) != 0;
#else
return rename( temporary.c_str(), target.c_str() ) == 0;
#endif
}
static std::set<std::string> readManifest() {
std::set<std::string> paths;
std::ifstream input( manifestName );
std::string path;
if( !std::getline( input, path ) || path != manifestHeader ) {
return paths;
}
while( std::getline( input, path ) ) {
if( safePath( path ) ) {
paths.insert( path );
}
}
return paths;
}
static bool writeStableManifest( const std::string & contents ) {
std::ostringstream temporary;
temporary << manifestName << ".exp2cxx-tmp-" << sc_getpid();
const std::string temporaryName = temporary.str();
{
std::ofstream output( temporaryName.c_str(), std::ios::binary );
output.write( contents.data(),
static_cast<std::streamsize>( contents.size() ) );
if( !output.good() ) {
remove( temporaryName.c_str() );
return false;
}
}
if( sameContents( temporaryName, manifestName ) ) {
remove( temporaryName.c_str() );
return true;
}
if( !replaceFile( temporaryName, manifestName ) ) {
remove( temporaryName.c_str() );
return false;
}
return true;
}
static void recordParentDirectories(
const std::string & path, std::set<std::string> & directories ) {
size_t slash = path.find_last_of( "/\\" );
while( slash != std::string::npos ) {
const std::string parent = path.substr( 0, slash );
if( safePath( parent ) ) {
directories.insert( parent );
}
slash = parent.find_last_of( "/\\" );
}
}
public:
OutputManager() : _sequence( 0 ), _active( false ), _failed( false ) {
}
~OutputManager() {
for( std::map<std::string, OutputRecord>::const_iterator i =
_outputs.begin(); i != _outputs.end(); ++i ) {
remove( i->second.temporary.c_str() );
}
}
void Begin() {
for( std::map<std::string, OutputRecord>::const_iterator i =
_outputs.begin(); i != _outputs.end(); ++i ) {
remove( i->second.temporary.c_str() );
}
_outputs.clear();
_openFiles.clear();
_sequence = 0;
_active = true;
_failed = false;
}
FILE * Open( const char * filename, bool append ) {
const std::string target = filename ? filename : "";
if( !_active || !safePath( target ) ) {
errno = EINVAL;
_failed = true;
return 0;
}
std::map<std::string, OutputRecord>::iterator found =
_outputs.find( target );
if( append ) {
if( found == _outputs.end() ) {
OutputRecord record;
record.temporary = temporaryName( target );
if( !copyFile( target, record.temporary ) ) {
_failed = true;
return 0;
}
found = _outputs.insert(
std::make_pair( target, record ) ).first;
}
} else {
if( found != _outputs.end() ) {
remove( found->second.temporary.c_str() );
_outputs.erase( found );
}
OutputRecord record;
record.temporary = temporaryName( target );
found = _outputs.insert( std::make_pair( target, record ) ).first;
}
FILE * output = fopen( found->second.temporary.c_str(),
append ? "a" : "w" );
if( !output ) {
_failed = true;
if( !append ) {
_outputs.erase( found );
}
return 0;
}
_openFiles[output] = target;
return output;
}
void Close( FILE * output ) {
if( !output ) {
return;
}
_openFiles.erase( output );
const int flushResult = fflush( output );
const int streamError = ferror( output );
const int closeResult = fclose( output );
if( flushResult || streamError || closeResult ) {
_failed = true;
}
}
int Write( const char * filename, const char * data, size_t size ) {
FILE * output = Open( filename, false );
if( !output ) {
return 0;
}
const bool written = fwrite( data, 1, size, output ) == size;
if( !written ) {
_failed = true;
}
Close( output );
return written ? 1 : 0;
}
int Finish() {
if( !_active || _failed || !_openFiles.empty() ) {
return 0;
}
const std::set<std::string> previous = readManifest();
std::ostringstream manifest;
manifest << manifestHeader << '\n';
for( std::map<std::string, OutputRecord>::iterator i =
_outputs.begin(); i != _outputs.end(); ++i ) {
manifest << i->first << '\n';
if( sameContents( i->second.temporary, i->first ) ) {
remove( i->second.temporary.c_str() );
} else if( !replaceFile( i->second.temporary, i->first ) ) {
return 0;
}
i->second.temporary.clear();
}
std::set<std::string> directories;
for( std::set<std::string>::const_iterator i = previous.begin();
i != previous.end(); ++i ) {
if( _outputs.find( *i ) == _outputs.end() ) {
if( remove( i->c_str() ) != 0 && errno != ENOENT ) {
return 0;
}
recordParentDirectories( *i, directories );
}
}
std::vector<std::string> orderedDirectories(
directories.begin(), directories.end() );
std::sort( orderedDirectories.begin(), orderedDirectories.end(),
[]( const std::string & left, const std::string & right ) {
return left.size() > right.size();
} );
for( size_t i = 0; i < orderedDirectories.size(); ++i ) {
sc_rmdir( orderedDirectories[i].c_str() );
}
if( !writeStableManifest( manifest.str() ) ) {
return 0;
}
_outputs.clear();
_active = false;
return 1;
}
};
OutputManager & manager() {
static OutputManager outputManager;
return outputManager;
}
}
extern "C" void GENERATEDbegin( void ) {
manager().Begin();
}
extern "C" FILE * GENERATEDopen( const char * filename ) {
return manager().Open( filename, false );
}
extern "C" FILE * GENERATEDappend( const char * filename ) {
return manager().Open( filename, true );
}
extern "C" void GENERATEDclose( FILE * file ) {
manager().Close( file );
}
extern "C" int GENERATEDwrite(
const char * filename, const char * data, size_t size ) {
return manager().Write( filename, data, size );
}
extern "C" int GENERATEDfinish( void ) {
return manager().Finish();
}