-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsource.cpp
More file actions
177 lines (144 loc) · 4.68 KB
/
source.cpp
File metadata and controls
177 lines (144 loc) · 4.68 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
/*
* Copyright (c) 2022 Light Labs Inc.
* All Rights Reserved
* Licensed under the MIT license.
*/
#include "source.h"
#include <libgen.h>
#include <string>
void EntryLand::setAsRoot( Hierarchy * fs ) { root = fs; parent = nullptr; }
void EntryLand::setParent( PathEntry * dir ) { parent = dir; root = dir->root; }
int EntryLand::depth() const { return parent ? parent->depth() + 1 : 0; }
bool EntryStat::statFd( int fd ) { return fstat64( lastFd = fd, &stat ) >= 0; }
void EntryStat::closeFd() { close( lastFd ); lastFd = -1; }
// in fact, we might not have to call this function at all:
// openat() can be used for all or most practical purposes.
const char * Entry::nativePath() const { return absPath.c_str(); }
bool Entry::offerFd( const char * entry, bool relative )
{
auto flags = openFlags();
return offerFd( relative ? openat( parent->lastFd, entry, flags ) : open( entry, flags ) );
}
void Entry::setPath( const char * path, bool relative )
{
if( relative && parent )
{
absPath.append( parent->nativePath() );
absPath.push_back( '/' );
}
absPath.append( path );
}
void Entry::setName( const char * rawName ) { decoded = root->decoder->decode( rawName ); }
bool PathEntry::describe( int fd )
{
return statFd( fd ) && ( lastDir = fdopendir( fd ) );
}
void PathEntry::activate() { root->onFolder( this ); }
void PathEntry::insertFile( const char * path ) { placeChild( New<FileEntry>(), path, false ); }
void PathEntry::insertPath( const char * path ) { placeChild( New<PathEntry>(), path, false ); }
void PathEntry::appendFile( const char * path ) { placeChild( New<FileEntry>(), path, true ); }
void PathEntry::appendPath( const char * path ) { placeChild( New<PathEntry>(), path, true ); }
void PathEntry::insertStat( const char * path )
{
// It's not exactly efficient to stat the path at this point and re-stat it again, but
// passing an optional stat64 structure down the call flow would be a greater change.
// After all, insert* is used to process command line parameters,
// and we aren't expecting many of them. (Yes you can post this comment on TheDailyWTF.)
struct stat st;
if( lstat( path, &st ) >= 0 )
{
if( S_ISREG( st.st_mode ) ) { insertFile( path ); }
else if( S_ISDIR( st.st_mode ) ) { insertPath( path ); }
else
{
fprintf( stderr, "Unsupported file type (%08x): %s\n",
st.st_mode, path );
}
}
else { perror( path ); }
}
void PathEntry::placeChild( Ptr<Entry> child, const char * path, bool relative )
{
child->setParent( this );
const char* rpath = path;
std::string copy;
if( relative )
{
copy = path;
copy.push_back('\0');
rpath = basename( ©[0] );
}
child->setName( rpath );
PlaceEntry( child, path, relative );
}
void PlaceEntry( Ptr<Entry> child, const char * path, bool relative )
{
if( child->offerFd( path, relative ) )
{
child->setPath( path, relative );
if( child->parent ) { child->parent->entries.push_back( child ); }
child->activate();
}
}
bool PathEntry::isValidChild( const RawDirEnt & entry )
{
return strcmp( entry.d_name, "." )
&& strcmp( entry.d_name, ".." );
}
void PathEntry::traverse()
{
if( mute ) { return; }
RawDirEnt * ptr;
RawDirEnt entry;
// don't follow symbolic links: there are no in CFDS
// we don't support pipes, sockets or ch/blk devices
while( lastDir && ( readdir64_r( lastDir, &entry, &ptr ), ptr ) )
{
if( isValidChild( entry ) && root->useEntry( ptr ) )
{
switch( entry.d_type )
{
case DT_REG:
appendFile( entry.d_name );
break;
case DT_DIR:
appendPath( entry.d_name );
break;
}
}
}
}
void PathEntry::closeFd()
{
if( lastDir )
{
closedir( lastDir );
lastDir = nullptr;
lastFd = -1;
}
}
bool FileEntry::describe( int fd ) { return statFd( fd ); }
void FileEntry::activate() { root->onFileFd( this ); }
int FileEntry::fd() const { return lastFd < 0 ? lastFd = open( path(), O_RDONLY ) : lastFd; }
FileEntry::operator Extent()
{
Extent extent;
extent.medium = Temp<Medium>( this );
extent.offset = 0;
extent.length = stat.st_size;
return extent;
}
void Hierarchy::openRoot( const char * path, bool traverse )
{
fsRoot = New<PathEntry>();
fsRoot->mute = !traverse;
fsRoot->setAsRoot( this );
PlaceEntry( fsRoot, path, false );
}
void Hierarchy::fakeRoot()
{
fsRoot = New<PathEntry>();
fsRoot->mute = true;
fsRoot->setAsRoot( this );
onFolder( fsRoot.get() );
}