-
Notifications
You must be signed in to change notification settings - Fork 71
/
path.cc
515 lines (414 loc) · 13.2 KB
/
path.cc
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* -*-mode:c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
#include <stdexcept>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <libgen.h>
#include <deque>
#include <climits>
#include <vector>
#include <string>
#include <sstream>
#include <memory>
#include <errno.h>
#include "path.hh"
#include "exception.hh"
#include "tokenize.hh"
#include "file_descriptor.hh"
#include "temp_file.hh"
using namespace std;
namespace roost {
Directory::Directory( const string & path )
: fd_( CheckSystemCall( "open directory (" + path + ")",
open( path.c_str(),
O_DIRECTORY | O_CLOEXEC ) ) )
{}
Directory::Directory( const Directory & parent, const string & path )
: fd_( CheckSystemCall( "openat directory (" + path + ")",
openat( parent.num(), path.c_str(),
O_DIRECTORY | O_CLOEXEC ) ) )
{}
Directory::~Directory()
{
try {
CheckSystemCall( "close", close( fd_ ) );
}
catch ( const exception & e ) {}
}
path::path()
: path_()
{}
path::path( const char * pathn )
: path::path( ::string( pathn ) )
{}
path::path( const std::string & pathn )
: path_( pathn )
{}
path path::lexically_normal() const
{
vector<std::string> components_vector = path_components();
vector<std::string> normalized;
if ( components_vector.empty() ) {
return std::string();
}
if ( components_vector.at( 0 ).empty() ) {
normalized.push_back( "" );
}
for ( const std::string & component : components_vector ) {
if ( component == ".." ) {
if ( normalized.size() == 0 or normalized.back() == ".." ) {
normalized.push_back( component );
}
else if ( !normalized.back().empty() ) {
normalized.pop_back();
}
}
else if ( component != "." and not component.empty() ) {
normalized.push_back( component );
}
}
ostringstream path_oss;
for ( const std::string & component : normalized ) {
path_oss << component << "/";
}
std::string p = path_oss.str();
if ( p.length() > 1 and p.back() == '/' ) {
p.erase( p.length() - 1 );
}
return p;
}
const string & path::string() const
{
return path_;
}
bool path::operator==( const path & other ) const
{
return other.path_ == path_;
}
bool exists( const path & pathn )
{
return not access( pathn.string().c_str(), F_OK );
}
/* XXX need to be careful about race conditions if file size
changes between when this is called, and later copy */
/* maybe could have a thunk sanity check at the end, making sure
all sizes match the objects in the gg directory? */
off_t file_size( const path & pathn )
{
struct stat file_info;
CheckSystemCall( "stat " + pathn.string(),
stat( pathn.string().c_str(), &file_info ) );
return file_info.st_size;
}
path canonical( const path & pathn )
{
char canonical_file_name[ PATH_MAX ];
if ( nullptr == realpath( pathn.string().c_str(), canonical_file_name ) ) {
throw unix_error( "realpath", errno );
}
return string( canonical_file_name );
}
path dirname( const path & pathn )
{
char path_cstr[ PATH_MAX ];
strcpy( path_cstr, pathn.string().c_str() );
return ::dirname( path_cstr );
}
path rbasename( const path & pathn )
{
char path_cstr[ PATH_MAX ];
strcpy( path_cstr, pathn.string().c_str() );
return ::basename( path_cstr );
}
void symlink( const path & target, const path & linkpath )
{
CheckSystemCall( "symlink", ::symlink( target.string().c_str(),
linkpath.string().c_str() ) );
}
path current_working_directory()
{
char path_cstr[ PATH_MAX ];
if ( getcwd( path_cstr, sizeof( path_cstr ) ) == nullptr ) {
throw unix_error( "cannot get current working directory" );
}
return { path_cstr };
}
void move_file( const path & src, const path & dst )
{
/* attempt simple rename (will work if on same filesystem) */
const int rename_result = ::rename( src.string().c_str(),
dst.string().c_str() );
if ( rename_result == 0 ) {
return;
}
else if ( errno != EXDEV ) {
throw unix_error( "rename()" );
}
/* failed, so make copy onto target filesystem first */
copy_then_rename( src, dst );
remove( src );
}
void atomic_create( const string & contents, const path & dst,
const bool set_mode, const mode_t target_mode )
{
string tmp_file_name;
{
UniqueFile tmp_file { dst.string() };
tmp_file_name = tmp_file.name();
if ( contents.size() > 0 ) {
tmp_file.fd().write( contents );
}
if ( set_mode ) {
CheckSystemCall( "fchmod", fchmod( tmp_file.fd().fd_num(), target_mode ) );
}
/* allow block to end so the UniqueFile gets closed() before rename. */
/* not 100% sure readers will see fully-written file appear atomically otherwise */
}
rename( tmp_file_name, dst.string() );
}
void copy_then_rename( const path & src, const path & dst,
const bool set_mode, const mode_t target_mode )
{
/* read input file into memory */
FileDescriptor src_file { CheckSystemCall( "open (" + src.string() + ")",
open( src.string().c_str(), O_RDONLY ) ) };
struct stat src_info;
CheckSystemCall( "fstat", fstat( src_file.fd_num(), &src_info ) );
if ( not S_ISREG( src_info.st_mode ) ) {
throw runtime_error( src.string() + " is not a regular file" );
}
const string contents = src_file.read_exactly( src_info.st_size );
/* write out to new file */
atomic_create( contents, dst, true, set_mode ? target_mode : src_info.st_mode );
}
path operator/( const path & prefix, const path & suffix )
{
if ( ( not prefix.string().empty() and prefix.string().back() == '/' ) or
( not suffix.string().empty() and suffix.string().front() == '/') ) {
return prefix.string() + suffix.string();
}
else {
return prefix.string() + "/" + suffix.string();
}
}
void create_directories_relative( const Directory & parent_directory,
const vector<string>::const_iterator & begin,
const vector<string>::const_iterator & end )
{
if ( begin == end ) {
return;
}
/* empty path component -> skip */
if ( begin->empty() ) {
return create_directories_relative( parent_directory, begin + 1, end );
}
try {
CheckSystemCall( "mkdirat (" + *begin + ")",
mkdirat( parent_directory.num(),
begin->c_str(),
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ) );
}
catch ( const unix_error & e ) {
if ( e.error_code() == EEXIST ) {
/* okay */
}
else {
throw;
}
}
create_directories_relative( Directory( parent_directory, *begin ), begin + 1, end );
}
void create_directories( const path & pathn )
{
vector<string> components = pathn.path_components();
if ( components.empty() ) {
return;
}
if ( components.front().empty() ) {
components.front() = "/";
}
create_directories_relative( Directory( "." ), components.begin(), components.end() );
}
bool is_directory( const path & pathn )
{
struct stat file_info;
CheckSystemCall( "stat " + pathn.string(),
stat( pathn.string().c_str(), &file_info ) );
return S_ISDIR( file_info.st_mode );
}
bool is_directory_at( const Directory & parent_directory, const path & pathn )
{
struct stat file_info;
CheckSystemCall( "fstatat " + pathn.string(),
fstatat( parent_directory.num(), pathn.string().c_str(),
&file_info, AT_SYMLINK_NOFOLLOW ) );
return S_ISDIR( file_info.st_mode );
}
bool exists_and_is_directory( const path & pathn )
{
struct stat file_info;
if ( stat( pathn.string().c_str(), &file_info ) ) {
return false;
}
return S_ISDIR( file_info.st_mode );
}
vector<string> path::path_components() const
{
return split( path_, "/" );
}
bool remove( const path & pathn )
{
CheckSystemCall( "remove " + pathn.string(),
::remove( pathn.string().c_str() ) );
return true;
}
bool remove_at( const Directory & parent_directory, const path & pathn,
const bool is_directory )
{
CheckSystemCall( "unlinkat " + pathn.string(),
unlinkat( parent_directory.num(),
pathn.string().c_str(),
is_directory ? AT_REMOVEDIR : 0 ) );
return true;
}
void empty_directory_recursive( const Directory & parent_directory,
const string & pathn )
{
Directory directory( parent_directory, pathn );
shared_ptr<DIR> dir { fdopendir( directory.num() ), closedir };
if ( dir.get() == NULL ) {
throw unix_error( "fdopendir" );
}
struct dirent * entry = NULL;
while ( ( errno = 0, entry = readdir( dir.get() ) ) != NULL ) {
string name { entry->d_name };
if ( name == "." or name == ".." ) {
continue;
}
bool is_dir = is_directory_at( directory, name );
if ( is_dir ) {
empty_directory_recursive( directory, name );
}
remove_at( directory, name, is_dir );
}
if ( errno ) {
throw unix_error( "readdir" );
}
}
void remove_directory( const path & pathn )
{
Directory directory { pathn.string() };
empty_directory_recursive( directory, "." );
remove( pathn );
}
void empty_directory( const path & pathn )
{
Directory directory { pathn.string() };
shared_ptr<DIR> dir { fdopendir( directory.num() ), closedir };
if ( dir.get() == nullptr ) {
throw unix_error( "fdopendir" );
}
struct dirent * entry = NULL;
while ( ( errno = 0, entry = readdir( dir.get() ) ) != NULL ) {
string name { entry->d_name };
if ( name == "." or name == ".." ) {
continue;
}
bool is_dir = is_directory_at( directory, name );
if ( not is_dir ) {
remove_at( directory, name, is_dir );
}
}
if ( errno ) {
throw unix_error( "readdir" );
}
}
vector<string> list_directory( const path & pathn )
{
shared_ptr<DIR> dir { opendir( pathn.string().c_str() ), closedir };
struct dirent * entry = NULL;
if ( not dir ) {
throw runtime_error( "cannot open directory: " + pathn.string() );
}
vector<string> output;
while ( ( entry = readdir( dir.get() ) ) != NULL ) {
output.emplace_back( entry->d_name );
}
return output;
}
void rename( const path & oldpath, const path & newpath )
{
CheckSystemCall( "rename", ::rename( oldpath.string().c_str(),
newpath.string().c_str() ) );
}
vector<string> get_directory_listing( const path & pathn )
{
Directory directory( pathn.string() );
shared_ptr<DIR> dir { fdopendir( directory.num() ), closedir };
if ( dir.get() == NULL ) {
throw unix_error( "fdopendir" );
}
vector<string> result;
struct dirent * entry = NULL;
while ( ( errno = 0, entry = readdir( dir.get() ) ) != NULL ) {
string name { entry->d_name };
if ( name != ".." and name != "." ) {
result.push_back( name );
}
}
if ( errno ) {
throw unix_error( "readdir" );
}
return result;
}
void chmod( const path & pathn, mode_t mode )
{
CheckSystemCall( "chmod", ::chmod( pathn.string().c_str(), mode ) );
}
void chdir( const path & pathn )
{
CheckSystemCall( "chdir", ::chdir( pathn.string().c_str() ) );
}
string read_file( const path & pathn )
{
/* read input file into memory */
FileDescriptor in_file { CheckSystemCall( "open (" + pathn.string() + ")",
open( pathn.string().c_str(), O_RDONLY ) ) };
struct stat pathn_info;
CheckSystemCall( "fstat", fstat( in_file.fd_num(), &pathn_info ) );
if ( not S_ISREG( pathn_info.st_mode ) ) {
throw runtime_error( pathn.string() + " is not a regular file" );
}
return in_file.read_exactly( pathn_info.st_size );
}
void make_executable( const path & pathn )
{
struct stat file_info;
CheckSystemCall( "stat " + pathn.string(),
stat( pathn.string().c_str(), &file_info ) );
chmod( pathn, file_info.st_mode | S_IXUSR );
}
bool is_executable( const path & pathn )
{
struct stat file_info;
CheckSystemCall( "stat", stat( pathn.string().c_str(), &file_info ) );
return file_info.st_mode & S_IXUSR;
}
string readlink( const path & pathn )
{
char result[ PATH_MAX ];
ssize_t len = CheckSystemCall( "readlink", ::readlink( pathn.string().c_str(), result, sizeof( result ) ) );
result[ len ] = '\0';
return { result };
}
bool is_absolute( const path & pathn )
{
if ( pathn.string().length() == 0 ) {
return false;
}
return ( pathn.string().at( 0 ) == '/' );
}
}