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
|
/*
* Copyright (C) 2007-2011 Xagasoft, All rights reserved.
*
* This file is part of the libbu++ library and is released under the
* terms of the license contained in the file LICENSE.
*/
#include "bu/myriadfs.h"
#include "bu/myriadstream.h"
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "bu/sio.h"
using Bu::sio;
using Bu::Fmt;
namespace Bu { subExceptionDef( MyriadFsException ) }
#define Myriad_Fs_MAGIC_CODE ((char *)"\xa7\x18\x8b\x39")
Bu::MyriadFs::MyriadFs( Bu::Stream &rStore, int iBlockSize ) :
rStore( rStore ),
mStore( rStore, iBlockSize ),
iUser( 0 ),
iGroup( 0 )
{
#ifndef WIN32
iUser = getuid();
iGroup = getgid();
#endif
if( mStore.hasStream( 1 ) )
{
// Check to see if this is a MyriadFs stream.
Bu::MyriadStream ms = mStore.openStream( 1 );
char sMagic[4];
if( ms.read( sMagic, 4 ) < 4 )
throw MyriadFsException("The provided stream does not appear to be "
"a MyriadFs stream.");
if( ::strncmp( sMagic, Myriad_Fs_MAGIC_CODE, 4 ) )
throw MyriadFsException("The provided stream does not appear to be "
"a MyriadFs stream.");
int8_t iVer;
ms.read( &iVer, 1 );
int32_t iNumNodes;
ms.read( &iNumNodes, 4 );
for( int32_t j = 0; j < iNumNodes; j++ )
{
int32_t iNode;
int32_t iPos;
ms.read( &iNode, 4 );
ms.read( &iPos, 4 );
hNodeIndex.insert( iNode, iPos );
}
}
else
{
// Create initial header stream
{
mStore.createStream( 1 );
Bu::MyriadStream ms = mStore.openStream( 1 );
ms.write( Myriad_Fs_MAGIC_CODE, 4 );
int8_t iVer = 1;
int32_t iTmp = 1;
ms.write( &iVer, 1 );
ms.write( &iTmp, 4 ); // iNumNodes
iTmp = 0;
ms.write( &iTmp, 4 ); // iInode
ms.write( &iTmp, 4 ); // iPosition
hNodeIndex.insert( 0, 0 );
}
// Create initial inode stream, with one root node.
{
mStore.createStream( 2 );
Bu::MyriadStream ms = mStore.openStream( 2 );
RawStat rs;
rs.iNode = 0;
rs.iUser = iUser;
rs.iGroup = iGroup;
rs.uPerms = 0755|typeDir;
rs.iLinks = 1;
rs.uStreamIndex = 3;
rs.iCTime = rs.iMTime = rs.iATime = time(NULL);
ms.write( &rs, sizeof(RawStat) );
}
// Create inode 0's storage stream.
{
mStore.createStream( 3 );
Bu::MyriadStream ms = mStore.openStream( 3 );
int32_t iTmp32 = 0;
ms.write( &iTmp32, 4 ); // iChildCount
}
}
}
Bu::MyriadFs::~MyriadFs()
{
writeHeader();
}
void Bu::MyriadFs::stat( const Bu::String &sPath, Bu::MyriadFs::Stat &rBuf )
{
int32_t iParent;
int32_t iNode = lookupInode( sPath, iParent );
Bu::MyriadStream is = mStore.openStream( 2 );
stat( iNode, rBuf, is );
}
Bu::MyriadStream Bu::MyriadFs::open( const Bu::String &sPath, int iMode )
{
int32_t iParent = -1;
int32_t iNode;
try
{
iNode = lookupInode( sPath, iParent );
sio << "File found." << sio.nl;
// The file was found
return openByInode( iNode );
}
catch( Bu::MyriadFsException &e )
{
if( iParent < 0 )
throw;
// This means that an intermediate path component couldn't be found
if( e.getErrorCode() == 1 )
throw;
// The file wasn't found, but the path leading up to it was.
// first, figure out the final path element...
Bu::String::const_iterator iStart = sPath.begin();
if( *iStart == '/' )
iStart++;
sio << "Scanning for filename:" << sio.nl;
for( Bu::String::const_iterator iEnd = iStart.find('/'); iEnd;
iStart = iEnd+1, iEnd = iStart.find('/') ) { }
Bu::String sName( iStart, sPath.end() );
sio << "End filename: " << sName << sio.nl;
sio << "Parent inode: " << iParent << sio.nl;
iNode = create( iParent, sName, 0664|typeRegFile, 0 );
sio << "New iNode: " << iNode << sio.nl;
return openByInode( iNode );
}
}
void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms )
{
create( sPath, iPerms, 0 );
}
void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms,
uint16_t iDevHi, uint16_t iDevLo )
{
create( sPath, iPerms, ((uint32_t)iDevHi<<16)|(uint32_t)iDevLo );
}
void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms,
uint32_t uSpecial )
{
int32_t iParent = -1;
int32_t iNode;
try
{
iNode = lookupInode( sPath, iParent );
sio << "File found." << sio.nl;
// The file was found
throw Bu::MyriadFsException("Path already exists.");
}
catch( Bu::MyriadFsException &e )
{
if( iParent < 0 )
throw;
// This means that an intermediate path component couldn't be found
if( e.getErrorCode() == 1 )
throw;
// The file wasn't found, but the path leading up to it was.
// first, figure out the final path element...
Bu::String::const_iterator iStart = sPath.begin();
if( *iStart == '/' )
iStart++;
sio << "Scanning for filename:" << sio.nl;
for( Bu::String::const_iterator iEnd = iStart.find('/'); iEnd;
iStart = iEnd+1, iEnd = iStart.find('/') ) { }
Bu::String sName( iStart, sPath.end() );
sio << "End filename: " << sName << sio.nl;
sio << "Parent inode: " << iParent << sio.nl;
iNode = create( iParent, sName, iPerms, uSpecial );
sio << "New iNode: " << iNode << sio.nl;
}
}
void Bu::MyriadFs::mkDir( const Bu::String &sPath, uint16_t iPerms )
{
create( sPath, (iPerms&permMask)|typeDir, 0 );
}
Bu::MyriadFs::Dir Bu::MyriadFs::readDir( const Bu::String &sPath )
{
int32_t iParent = -1;
int32_t iNode = lookupInode( sPath, iParent );
return readDir( iNode );
}
void Bu::MyriadFs::setTimes( const Bu::String &sPath, int64_t iATime,
int64_t iMTime )
{
int32_t iParent = -1;
int32_t iNode;
iNode = lookupInode( sPath, iParent );
setTimes( iNode, iATime, iMTime );
}
dev_t Bu::MyriadFs::devToSys( uint32_t uDev )
{
return (((uDev&0xFFFF0000)>>8)&0xFF00) | ((uDev&0xFF));
}
uint32_t Bu::MyriadFs::sysToDev( dev_t uDev )
{
return (((uint32_t)uDev&0xFF00)<<8) | ((uint32_t)uDev&0xFF);
}
int32_t Bu::MyriadFs::lookupInode( const Bu::String &sPath, int32_t &iParent )
{
if( sPath == "/" )
{
return 0;
}
if( sPath[0] == '/' )
{
// Absolute lookup
return lookupInode( sPath.begin()+1, 0, iParent );
}
else
{
// Relative lookup
throw Bu::ExceptionBase(
"Relative lookups in MyriadFs are not working yet.");
}
}
int32_t Bu::MyriadFs::lookupInode( Bu::String::const_iterator iStart,
int32_t iNode, int32_t &iParent )
{
iParent = iNode;
Bu::String::const_iterator iEnd = iStart.find('/');
Bu::String sTok( iStart, iEnd );
sio << "Direcotry component: " << sTok << sio.nl;
Dir lDir = readDir( iNode );
for( Dir::iterator i = lDir.begin(); i; i++ )
{
if( (*i).sName == sTok )
{
// We found an item
if( !iEnd )
{
// It's the last one in the requested path, return it
return (*i).iNode;
}
else
{
// Not the last one in our path, double check it's a dir
if( ((*i).uPerms&typeMask) == typeDir )
{
return lookupInode( iEnd+1, (*i).iNode, iParent );
}
else
{
iParent = -1;
throw Bu::MyriadFsException(
"Element '%s' in given path is not a directory.",
sTok.getStr() );
}
}
}
}
if( iEnd )
throw Bu::MyriadFsException( 1, "Path not found");
else
throw Bu::MyriadFsException( 2, "Path not found");
}
Bu::MyriadFs::Dir Bu::MyriadFs::readDir( int32_t iNode )
{
Bu::MyriadStream ms = openByInode( iNode );
int32_t iNumChildren = 0;
ms.read( &iNumChildren, 4 );
Bu::MyriadStream is = mStore.openStream( 2 );
Dir lDir;
sio << "Reading dir, " << iNumChildren << " entries:" << sio.nl;
for( int32_t j = 0; j < iNumChildren; j++ )
{
int32_t iChildNode;
ms.read( &iChildNode, 4 );
Stat s;
stat( iChildNode, s, is );
uint8_t uLen;
ms.read( &uLen, 1 );
s.sName.setSize( uLen );
ms.read( s.sName.getStr(), uLen );
lDir.append( s );
sio << " " << s.sName << sio.nl;
}
return lDir;
}
Bu::MyriadStream Bu::MyriadFs::openByInode( int32_t iNode )
{
int32_t iIndex = hNodeIndex.get( iNode );
Bu::MyriadStream ms = mStore.openStream( 2 );
ms.setPos( iIndex*sizeof(RawStat) );
RawStat rs;
ms.read( &rs, sizeof(RawStat) );
switch( (rs.uPerms&typeMask) )
{
case typeDir:
case typeSymLink:
case typeRegFile:
return mStore.openStream( rs.uStreamIndex );
default:
throw Bu::MyriadFsException(
"inode incorrect type for low-level openByInode.");
}
}
int32_t Bu::MyriadFs::create( int32_t iParent, const Bu::String &sName,
uint16_t uPerms, uint32_t uSpecial )
{
if( sName.getSize() > 255 )
{
throw Bu::MyriadFsException("Filename too long, max is 255 bytes.");
}
Bu::MyriadStream ms = openByInode( iParent );
int32_t iNumChildren = 0;
ms.read( &iNumChildren, 4 );
iNumChildren++;
ms.setPos( 0 );
ms.write( &iNumChildren, 4 );
ms.setPosEnd( 0 );
int32_t iNode = allocInode( uPerms, uSpecial );
ms.write( &iNode, 4 );
uint8_t uLen = sName.getSize();
ms.write( &uLen, 1 );
ms.write( sName.getStr(), uLen );
return iNode;
}
int32_t Bu::MyriadFs::allocInode( uint16_t uPerms, uint32_t uSpecial )
{
int32_t iNode = 0;
for(; iNode < 0xfffffff; iNode++ )
{
if( !hNodeIndex.has( iNode ) )
{
Bu::MyriadStream is = mStore.openStream( 2 );
is.setSize( (hNodeIndex.getSize()+1)*sizeof(RawStat) );
is.setPos( hNodeIndex.getSize()*sizeof(RawStat) );
hNodeIndex.insert( iNode, hNodeIndex.getSize() );
RawStat rs;
rs.iNode = iNode;
rs.iUser = iUser;
rs.iGroup = iGroup;
rs.uPerms = uPerms;
rs.iLinks = 1;
switch( (uPerms&typeMask) )
{
case typeRegFile:
case typeSymLink:
rs.uStreamIndex = mStore.createStream();
break;
case typeDir:
rs.uStreamIndex = mStore.createStream();
sio << "Creating directory node, storage: "
<< rs.uStreamIndex << sio.nl;
{
Bu::MyriadStream msDir = mStore.openStream(
rs.uStreamIndex
);
uint32_t uSize = 0;
msDir.write( &uSize, 4 );
}
break;
case typeChrDev:
case typeBlkDev:
rs.uStreamIndex = uSpecial;
break;
default:
rs.uStreamIndex = 0;
break;
}
rs.iATime = time(NULL);
rs.iMTime = time(NULL);
rs.iCTime = time(NULL);
is.write( &rs, sizeof(RawStat) );
return iNode;
}
}
throw Bu::MyriadFsException(
"No inode could be allocated. You've run out!");
}
void Bu::MyriadFs::stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs )
{
rIs.setPos( hNodeIndex.get( iNode )*sizeof(RawStat) );
RawStat rs;
rIs.read( &rs, sizeof(RawStat) );
if( rs.iNode != iNode )
throw Bu::MyriadFsException("Filesystem corruption detected.");
rBuf.iNode = iNode;
rBuf.iUser = rs.iUser;
rBuf.iGroup = rs.iGroup;
rBuf.uPerms = rs.uPerms;
rBuf.iLinks = rs.iLinks;
rBuf.iATime = rs.iATime;
rBuf.iMTime = rs.iMTime;
rBuf.iCTime = rs.iCTime;
rBuf.uDev = 0;
rBuf.iSize = 0;
switch( (rBuf.uPerms&typeMask) )
{
case typeRegFile:
case typeSymLink:
rBuf.iSize = mStore.getStreamSize( rs.uStreamIndex );
break;
case typeChrDev:
case typeBlkDev:
rBuf.uDev = rs.uStreamIndex;
break;
default:
rBuf.iSize = 0;
break;
}
}
void Bu::MyriadFs::unlink( int32_t iNode )
{
}
void Bu::MyriadFs::writeHeader()
{
Bu::MyriadStream ms = mStore.openStream( 1 );
ms.write( Myriad_Fs_MAGIC_CODE, 4 );
int8_t iVer = 1;
int32_t iNumNodes = hNodeIndex.getSize();
ms.write( &iVer, 1 );
ms.write( &iNumNodes, 4 ); // iNumNodes
for( NodeIndex::iterator i = hNodeIndex.begin(); i; i++ )
{
int32_t iNode = i.getKey();
int32_t iPosition = i.getValue();
ms.write( &iNode, 4 );
ms.write( &iPosition, 4 );
}
// Truncate the stream afterwards so we don't use up too much space.
ms.setSize( ms.tell() );
}
void Bu::MyriadFs::setTimes( int32_t iNode, int64_t iATime, int64_t iMTime )
{
RawStat rs;
Bu::MyriadStream is = mStore.openStream( 2 );
is.setPos( hNodeIndex.get(iNode)*sizeof(RawStat) );
is.read( &rs, sizeof(RawStat) );
rs.iATime = iATime;
rs.iMTime = iMTime;
is.setPos( hNodeIndex.get(iNode)*sizeof(RawStat) );
is.write( &rs, sizeof(RawStat) );
}
|