-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcd9660.cpp
More file actions
342 lines (304 loc) · 13.5 KB
/
Copy pathcd9660.cpp
File metadata and controls
342 lines (304 loc) · 13.5 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
336
337
338
339
340
341
342
#include "cd9660.h"
#include "impl/datetm.h"
namespace
{
bool IsLittleEndian() { int word = 1; return( *( char * ) & word ); }
inline uint8_t QuarterOff( const struct tm & t ) { return ( int )( t.tm_gmtoff / 15 / 60 ) + 48; }
std::vector<bool> Allowed( const char * range )
{
std::vector<bool> retVal;
retVal.resize( UINT8_MAX + 1, 0 );
uint8_t allowed;
for( int i = 0; ( allowed = range[i] ); ++i ) { retVal[allowed] = true; }
return retVal;
}
#define DCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
#define ACHARS DCHARS "!\"%&'()*+,-./:;<=>?"
std::vector<bool> ARANGE = Allowed( ACHARS );
std::vector<bool> DRANGE = Allowed( DCHARS );
}
namespace CD
{
DateTime & DateTime::setTm( const tm & t, int centiseconds )
{
sprintf( buf, "%04d""%02d""%02d"
"%02d""%02d""%02d""%02d",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, centiseconds );
tzoff = QuarterOff( t ); // ... overwriting terminal 0
return *this;
}
DateTime & DateTime::operator=( const timespec & ts )
{ SetTime( ts, TmSet( this ) ); return *this; }
DateTime::DateTime() { SetNow( TmSet( this ) ); }
void DirEntryDtTime::setTm( const tm & t, int /*centis*/ )
{
year = t.tm_year;
month = t.tm_mon + 1;
day = t.tm_mday;
hour = t.tm_hour;
minute = t.tm_min;
second = t.tm_sec;
tzone = QuarterOff( t );
}
DirEntryDtTime & DirEntryDtTime::operator=( const timespec & ts )
{ SetTime( ts, TmSet( this ) ); return *this; }
void JolietDesc::CopyMeta( const PriVolDesc & priVol )
{
// origin
systemId.dilute<MSB<char16_t>>( priVol.systemId );
volumeId.dilute<MSB<char16_t>>( priVol.volumeId );
// textual
volumeSetId.dilute<MSB<char16_t>>( priVol.volumeSetId );
publisherId.dilute<MSB<char16_t>>( priVol.publisherId );
prepareById.dilute<MSB<char16_t>>( priVol.prepareById );
application.dilute<MSB<char16_t>>( priVol.application );
// file names
copyFile.dilute<MSB<char16_t>>( priVol.copyFile );
abstFile.dilute<MSB<char16_t>>( priVol.abstFile );
biblFile.dilute<MSB<char16_t>>( priVol.biblFile );
}
CD9660Out::CD9660Out( bool withUnicode )
{
// add primary
auto & priVol = _volumes[PrimaryVol];
priVol.vol = &_pri_vol;
priVol.rule = &pri_rule;
priVol.pack = &_ansi_pack;
if( withUnicode )
{
// add joliet
auto & joliet = _volumes[Supplement];
joliet.vol = &_sec_vol;
joliet.rule = &sec_rule;
joliet.pack = &_ucs2_pack;
}
}
Colonies CD9660Out::plan( const Original & tree, Planner & outPlanner, Planner & tmpPlanner )
{
// write zeroes, or what the slave wants
planReserved( tree, outPlanner, tmpPlanner, 0x8000 ); // Extent onReserved( Extent )
// printf( "After zeroes written: %lu (%lu)\n", outPlanner.offset(), tmpPlanner.offset() );
// write 3 descriptors - per-each-fs[PVD, SVD], end
outPlanner.append( tmpPlanner.wrapToGo( planHeaders( tmpPlanner ) ) );
outPlanner.autoPad(); // effectively a no-op because dm blocks <= zram blocks
// printf( "After headers written: %lu (%lu)\n", outPlanner.offset(), tmpPlanner.offset() );
// write files
Colonies srcToTrg = tree.writeFiles( outPlanner );
printf( "After files written: %lu (%lu)\n", outPlanner.offset(), tmpPlanner.offset() );
// write per-each-fs[folders, path-table[L], path-table[B]]
off64_t innerOff = tmpPlanner.offset();
off64_t tmpToOut = outPlanner.offset() - innerOff;
planVolumes( outPlanner, [&]( CD9660Out::FS & vol )
{
StdRand shuf;
std::map<PathEntry *, std::list<Later::Use>> parents;
struct FolDef
{
Extent extent;
Unicode conv;
std::string encName;
};
std::map<Entry *, FolDef> fsFolders;
const blksize_t blkSz = vol.vol->blkSz;
DirectoryEntry & dot = vol.vol->rootDirectory;
// we will reuse the field along the way; but the last one will be root!
for( auto itr = tree.pathTable.rbegin(); itr != tree.pathTable.rend(); ++itr )
{
PathEntry * pDir = *itr;
auto dirOffset = tmpPlanner.offset() + tmpToOut;
// we can replace the ctor with a factory method - just hide Later::Store() behind the facade method
Ptr<Burner> dirBurner = New<VectBurner>( blkSz ); // New<TempBurner>(blkSz)
dirBurner->reserve( blkSz );
auto writeEntry = [dirBurner, blkSz]( const DirectoryEntry & die, const std::string & enc )
{
off64_t cur = dirBurner->offset();
off64_t pad = dirBurner->padding( cur, blkSz );
if( die.entrySz > pad ) { dirBurner->append( ZeroExtent( pad ) ); }
cur = dirBurner->append( TempExtent( &die, sizeof( DirectoryEntry ) - sizeof( die.fileName.data ) ) );
dirBurner->append( 0, die.textSize(), New<TempMedium>( enc.data() ) );
return cur;
};
// for each folder, sort entries by names, write directories and file extents
// NOTE - first two entries are this (current) folder and the parent ..folder
dot.dateTime = pDir->stat.st_mtim;
dot.fileFlags |= XAttrFlags::Folder;
dot.extentLba = dirOffset / blkSz;
dot.fileName.data[0] = 0;
// specialEntry.length will be written later
off64_t ownOffset = dirBurner->append( TempExtent<DirectoryEntry>( dot ) );
Later::Use ownSize = Later::Store<decltype( dot.length )>( DtOf( dirBurner ),
ownOffset + offsetof( DirectoryEntry, length ), dot.length,
[]( decltype( dot.length )& lfield, const Range & range )
{
lfield = range.length;
} ); // for length
dot.fileName.data[0] = 1;
off64_t parentOffset = dirBurner->append( TempExtent<DirectoryEntry>( dot ) );
PathEntry * parent = pDir->parent ? pDir->parent : pDir;
parents[parent].push_back( Later::Store<DirectoryEntry>( DtOf( dirBurner ),
parentOffset, dot,
[parent, blkSz]( DirectoryEntry & lfield, const Range & range )
{
lfield.dateTime = parent->stat.st_mtim;
lfield.extentLba = range.offset / blkSz; // as above, tmpPlanner.offset() + tmpToOut
lfield.length = range.length;
} ) ); // for length
NamePool pool; // temporary, while a folder is being read
std::map<Unicomp, Entry *> entries; // names are unique at this point
for( Ptr<Entry> pEnt : pDir->entries )
{
entries[pool.fitName( pEnt->decoded, pEnt->isFile(), *vol.rule, shuf )] = pEnt.get();
}
for( auto & nEnt : entries )
{
const Unicomp & name = nEnt.first;
Entry * pEnt = nEnt.second;
DirectoryEntry die;
// Principal component: NAME
std::string encName;
vol.pack->compress( encName, name.conv );
die.fileName.size = encName.size();
die.entrySz = die.size();
// Principal component: DATE
die.dateTime = pEnt->stat.st_mtim;
if( pEnt->isFile() )
{
// Principal components: FLAG, LBA, LENGTH
off64_t length = pEnt->stat.st_size;
for( const Extent & xt : tree.layout.at( pEnt ) )
{
// the offset here is in source dev coordinates; adjust...
off64_t offset = srcToTrg.withinDisk( xt );
// the offset here is in target dev coordinates
die.extentLba = offset / blkSz;
if( length <= xt.length )
{ die.fileFlags &= ~XAttrFlags::TBCont; }
else
{ die.fileFlags |= XAttrFlags::TBCont; }
die.length = std::min( length, xt.length );
length -= xt.length;
writeEntry( die, encName );
}
}
else
{
// Principal components: FLAG, LBA, LENGTH
const Extent & xt = fsFolders[pEnt].extent;
die.fileFlags |= XAttrFlags::Folder;
// the offset is in target dev coordinates
die.extentLba = xt.offset / blkSz;
die.length = xt.length;
writeEntry( die, encName );
// put folder name for path table
fsFolders[pEnt].conv = name.conv;
fsFolders[pEnt].encName = encName;
}
}
tmpPlanner.append( WrapToGo( dirBurner ) ); // directory extent roundup
Extent ownExtent = Extent( dirOffset, dirBurner->offset(), dirBurner );
ownSize( ownExtent );
fsFolders[pDir].extent = ownExtent;
// propagate to children
auto catchup = parents.find( pDir );
if( catchup != parents.end() )
{
for( auto & use : catchup->second ) { use( ownExtent ); }
//1// parents.erase( catchup ); // MOREINFO will it speed up lookup?
}
}
dot.fileName.data[0] = 0; // root again
std::map<Unicode, Entry *> pTab;
wchar_t level = 1; // <= 8
wchar_t daddy = 1; // the parent directory of the root directory is the root directory
// traverse from top (Exposure::fsRoot)
Unicode order;
PathTableEntryPair pePair;
Ptr<Burner> ptLsb = New<VectBurner>( blkSz ), ptMsb = New<VectBurner>( blkSz ); // or can be new TempBurner?
order.resize( 2 );
order[0] = level;
order[1] = daddy;
order.push_back( L'\0' );
FolDef & rootData = fsFolders[tree.fsRoot.get()];
// now we can fill in the root directory size...
vol.vol->rootDirectory.length = rootData.extent.length;
// ...and proceed to the path tables.
rootData.encName.resize( 1, '\0' );
pTab[order] = tree.fsRoot.get();
auto itd = pTab.begin();
while( itd != pTab.end() && daddy < CD9660Out::PATHTB_SZ )
{
Unicode parentOrder = itd->first;
order[0] = parentOrder[0] + 1;
FolDef & parentData = fsFolders[itd->second];
order[1] = daddy++; // this is assigned by itn after sort
pePair.set( parentData.encName, parentData.extent.offset / blkSz, parentOrder[1] );
ptLsb->append( TempExtent( pePair.lsb ) );
ptMsb->append( TempExtent( pePair.msb ) );
Extent textExtent( 0, pePair.lsb.textSize(), New<TempMedium>( parentData.encName.c_str() ) );
ptLsb->append( textExtent );
ptMsb->append( textExtent );
//
PathEntry * pEnt = static_cast<PathEntry *>( itd->second );
for( Ptr<Entry> pSub : pEnt->entries )
{
if( pSub->isDir() )
{
order.resize( 2 );
FolDef & childData = fsFolders[pSub.get()];
order.append( childData.conv ); // this is going to be our key
pTab[order] = pSub.get(); // make sure the addition is after lst!
}
}
++itd; //++daddy;
}
// write two path tables and update their locations
vol.vol->pTabSz = ptLsb->offset();
off64_t lsbOff = tmpPlanner.append( WrapToGo( ptLsb ) );
off64_t msbOff = tmpPlanner.append( WrapToGo( ptMsb ) );
vol.vol->pTabLsb[0] = ( lsbOff + tmpToOut ) / blkSz;
vol.vol->pTabMsb[0] = ( msbOff + tmpToOut ) / blkSz;
} );
// metadata common to volumes
// frankly, should contain the serial number
// setTitles( "LIGHT_OS", "L16_MEDIA" ); // TODO: inject: systemLabel, volumeLabel
// TODO allow extent conversion if a real file is appended; make it a fallback
outPlanner.append( tmpPlanner.wrapToGo( innerOff ) );
outPlanner.autoPad(); // effectively a no-op because dm blocks <= zram blocks
setSize( outPlanner.offset() );
// printf( "After fsmeta written: %lu\n", outPlanner.offset() );
return srcToTrg;
}
void CD9660Out::setSize( off64_t size )
{
off64_t blk = size / blockSize();
_pri_vol.blocks = _sec_vol.blocks = blk;
_map_vol.blocks = size / Blocks::MAPPER_BS;
}
off64_t CD9660Out::planHeaders( IAppend & planner ) const
{
off64_t cur = planner.offset();
for( auto & vol : _volumes )
{
planner.append( TempExtent( * ( vol.second.vol ) ) );
planner.padTo( blockSize() );
}
// planner.append( TempExtent( _map_vol ) ); // check win!
// planner.padTo( blockSize() );
planner.append( TempExtent( _end_vol ) );
planner.padTo( blockSize() );
return cur;
}
off64_t CD9660Out::planVolumes( IAppend & planner, std::function<void( FS & )> fsGen )
{
off64_t cur = planner.offset();
for( auto & vol : _volumes ) { fsGen( vol.second ); }
return cur;
}
void CD9660Out::setLabels( const char * system, const char * volume )
{
_pri_vol.systemId = system;
_pri_vol.volumeId = volume;
_sec_vol.CopyMeta( _pri_vol );
}
}//namespace CD