-
Notifications
You must be signed in to change notification settings - Fork 3
/
fsview_mkfs.cpp
148 lines (128 loc) · 4.97 KB
/
fsview_mkfs.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
/*
* Copyright (c) 2022 Light Labs Inc.
* All Rights Reserved
* Licensed under the MIT license.
*/
/* The architecture of the solution is to be re-documented.
* Outstanding issues that MAY affect the class chart:
* - "laning" (larger clusters on FAT32 drives achieved by shifting file
* extents by a sub-cluster offset);
* Notes:
* - objects are allocated on the heap and managed with shared pointers.
* - there is a thin abstraction layer over STL to keep the code simple.
* - POSIX and Linux-specific APIs are used in concrete implementations.
* - occasional POSIX types are used in interface specifications.
*/
#include "impl/rlimit.h"
#include "impl/extent.h"
#include "impl/strenc.h"
#include "impl/source.h"
#include "impl/device.h"
#include "impl/burner.h"
#include "impl/cd9660.h"
#include "impl/vfat32.h"
#include "impl/hfplus.h"
#include "conf/config.h"
#include "impl/unique.h"
#include <iostream>
#include <regex>
// should not be included by high-level app code,
// but only by concrete component implementations
// -include "allsys.h"
// - actually support multiple drives!
// - jam inodes (that is, remember ino_t and resolve inode conflicts)
// - actually crawl (close files, reopen on demand) [optional]
// - use memfds instead of reallocated mem
// - laaaaaning... (that's gonna take long)
//
int main( int argc, char ** argv )
{
MkfsConf cfg;
cfg.parse( argc, argv );
if( cfg.entries.size() ) // folder to index
{
if( !cfg.crawl_fds ) { RaiseFdLimit(); }
Original tree;
tree.gap = cfg.tolerance();
tree.decoder = New<UTF8Homebrew>();
if( cfg.ex.size() )
{
std::list<std::regex> patterns;
for( const char * expr : cfg.ex ) { patterns.emplace_back( expr ); }
tree.allowName = [patterns]( const char * name )
{
for( auto & pattern : patterns )
{ if( std::regex_match( name, pattern ) ) { return false; } }
return true;
};
}
// we allow running without cfg.target, simply to analyze the file geometry
if( !cfg.isTargetCopied() ) { tree.locator = New<ExtentIoc>( cfg ); }
auto itr = cfg.entries.begin();
tree.openRoot( *itr++ );
// supplementary files and folders
while( itr != cfg.entries.end() )
{ tree.fsRoot->insertStat( *itr++ ); }
printf( "Files: %lu\n", tree.fileTable.size() );
printf( "Backing devices: %lu\n", tree.plan.size() );
if( cfg.target )
{
Ptr<Burner> outImage, tmpImage;
if( cfg.isTargetMapped() && !cfg.zrControl )
{ printf( "DM without ZRam not yet supported\n" ); abort(); } // TODO
// differentiated based on whether the control node is provided!
if( cfg.zrControl && cfg.buffer )
{ tmpImage = New<ZRAMBurner>( cfg.buffer, cfg.zrControl ); }
else if( cfg.buffer && cfg.buffer[0] == '/' ) // ensure absolute
{ tmpImage = New<FileBurner>( cfg.buffer ); }
else // memfd
{ tmpImage = New<TempBurner>(); }
// differentiated based on whether the file path is absolute
if( cfg.isTargetMapped() )
{ outImage = New<DiskBurner>( cfg.target, cfg.dmControl ); }
else
{ outImage = New<FileBurner>( cfg.target ); }
auto tagVolume = [&cfg]( Volume & vol, MkfsConf::FSType type )
{
vol.setTitles( cfg.system, cfg.labels[type].c_str() );
};
CD::CD9660Out iso; tagVolume( iso, MkfsConf::FS_CDFS );
HP::HFPlusOut mac; tagVolume( mac, MkfsConf::FS_HFSX );
VF::VFat32Out fat; tagVolume( fat, MkfsConf::FS_Fat32 );
Volume * out;
if( cfg.fsType & MkfsConf::FS_CDFS )
{
if( cfg.fsType & MkfsConf::FS_HFSX )
{ iso.setHybrid( mac ); }
out = &iso;
}
else if( cfg.fsType & MkfsConf::FS_HFSX )
{ out = &mac; }
else if( cfg.fsType & MkfsConf::FS_Fat32 )
{
out = &fat;
// a mild version of bestBlkSize() in fsview_temp.cpp
if( !cfg.isTargetMapped() && out->blockSize() < 2048u )
{ out->setBlockSize( 2048u ); }
}
else if( !cfg.fsType )
{ printf( "No filesystem requested\n" ); abort(); }
else
{ printf( "Unsupported filesystem!\n" ); abort(); }
out->represent( tree, outImage, tmpImage );
for( std::pair<const char *, const char *> & props : cfg.setOnDone )
{
__system_property_set( props.first, props.second );
}
if( cfg.daemonize )
{
sigset_t t;
sigemptyset( &t );
sigaddset( &t, SIGTERM );
int real_sig;
sigwait( &t, &real_sig );
}
}
}
return 0;
}