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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
|
/*
* Copyright (C) 2007-2013 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/config.h"
#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*/,
uint16_t uPerms )
{
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 sName = filePart( sPath );
sio << "End filename: " << sName << sio.nl;
sio << "Parent inode: " << iParent << sio.nl;
iNode = create( iParent, sName, (uPerms&permMask)|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 sName = filePart( sPath );
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 );
}
void Bu::MyriadFs::mkSymLink( const Bu::String &sTarget,
const Bu::String &sPath )
{
int32_t iParent = -1;
int32_t iNode;
try
{
iNode = lookupInode( sPath, iParent );
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 sName = filePart( sPath );
sio << "End filename: " << sName << sio.nl;
sio << "Parent inode: " << iParent << sio.nl;
iNode = create( iParent, sName, 0777|typeSymLink, 0 );
sio << "New iNode: " << iNode << sio.nl;
MyriadStream ms = openByInode( iNode );
ms.write( sTarget );
}
}
void Bu::MyriadFs::mkHardLink( const Bu::String &sTarget,
const Bu::String &sPath )
{
int32_t iParent = -1;
int32_t iNode;
iNode = lookupInode( sTarget, iParent );
try
{
lookupInode( sPath, iParent );
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 sName = filePart( sPath );
sio << "End filename: " << sName << sio.nl;
sio << "Parent inode: " << iParent << sio.nl;
addToDir( iParent, iNode, sName );
MyriadStream is = mStore.openStream( 2 );
RawStat rs;
readInode( iNode, rs, is );
rs.iLinks++;
writeInode( rs, is );
}
}
Bu::String Bu::MyriadFs::readSymLink( const Bu::String &sPath )
{
int32_t iParent = -1;
int32_t iNode;
iNode = lookupInode( sPath, iParent );
MyriadStream ms = openByInode( iNode );
Bu::String sRet;
sRet.setSize( ms.getSize() );
ms.read( sRet.getStr(), ms.getSize() );
return sRet;
}
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 );
}
void Bu::MyriadFs::unlink( const Bu::String &sPath )
{
int32_t iParent = -1;
// int32_t iNode;
/*iNode =*/ lookupInode( sPath, iParent );
Dir lDir = readDir( iParent );
Bu::String sName = filePart( sPath );
for( Dir::iterator i = lDir.begin(); i; i++ )
{
if( sName == (*i).sName )
{
RawStat rs;
readInode( (*i).iNode, rs );
if( (rs.uPerms&typeMask) == typeDir )
{
MyriadStream msDir = mStore.openStream( rs.uStreamIndex );
int32_t iCount;
msDir.read( &iCount, 4 );
if( iCount > 0 )
{
throw Bu::MyriadFsException("Directory not empty.");
}
}
if( --rs.iLinks == 0 )
{
destroyNode( (*i).iNode );
}
else
{
writeInode( rs );
}
lDir.erase( i );
break;
}
}
Bu::MyriadStream ms = openByInode( iParent );
int32_t iNumChildren = lDir.getSize();
ms.write( &iNumChildren, 4 );
for( Dir::iterator i = lDir.begin(); i; i++ )
{
ms.write( &(*i).iNode, 4 );
uint8_t iSize = (*i).sName.getSize();
ms.write( &iSize, 1 );
ms.write( (*i).sName.getStr(), iSize );
}
ms.setSize( ms.tell() );
}
void Bu::MyriadFs::setFileSize( const Bu::String &sPath, int32_t iSize )
{
int32_t iParent = -1;
int32_t iNode;
iNode = lookupInode( sPath, iParent );
MyriadStream ms = openByInode( iNode );
ms.setSize( iSize );
}
void Bu::MyriadFs::rename( const Bu::String &sFrom, const Bu::String &sTo )
{
mkHardLink( sFrom, sTo );
unlink( sFrom );
}
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");
}
void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs )
{
rIs.setPos( hNodeIndex.get( iNode )*sizeof(RawStat) );
if( rIs.read( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) )
throw Bu::MyriadFsException("Filesystem corruption detected.");
if( rs.iNode != iNode )
throw Bu::MyriadFsException("Filesystem corruption detected.");
}
void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs )
{
MyriadStream ms = mStore.openStream( 2 );
readInode( iNode, rs, ms );
}
void Bu::MyriadFs::writeInode( const RawStat &rs,
MyriadStream &rOs )
{
rOs.setSize( hNodeIndex.getSize()*sizeof(RawStat) );
rOs.setPos( hNodeIndex.get( rs.iNode )*sizeof(RawStat) );
if( rOs.write( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) )
throw Bu::MyriadFsException("Error writing inode to header stream.");
}
void Bu::MyriadFs::writeInode( const RawStat &rs )
{
MyriadStream ms = mStore.openStream( 2 );
writeInode( rs, ms );
}
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 )
{
RawStat rs;
readInode( iNode, rs );
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.");
}
}
void Bu::MyriadFs::addToDir( int32_t iDir, int32_t iNode,
const Bu::String &sName )
{
if( sName.getSize() > 255 )
{
throw Bu::MyriadFsException("Filename too long, max is 255 bytes.");
}
Bu::MyriadStream ms = openByInode( iDir );
int32_t iNumChildren = 0;
ms.read( &iNumChildren, 4 );
iNumChildren++;
ms.setPos( 0 );
ms.write( &iNumChildren, 4 );
ms.setPosEnd( 0 );
ms.write( &iNode, 4 );
uint8_t uLen = sName.getSize();
ms.write( &uLen, 1 );
ms.write( sName.getStr(), uLen );
}
int32_t Bu::MyriadFs::create( int32_t iParent, const Bu::String &sName,
uint16_t uPerms, uint32_t uSpecial )
{
int32_t iNode = allocInode( uPerms, uSpecial );
addToDir( iParent, iNode, sName );
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 ) )
{
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);
writeInode( rs );
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 )
{
RawStat rs;
readInode( iNode, rs, rIs );
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::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 );
readInode( iNode, rs, is );
rs.iATime = iATime;
rs.iMTime = iMTime;
writeInode( rs, is );
}
void Bu::MyriadFs::destroyNode( int32_t iNode )
{
if( iNode == 0 )
throw Bu::MyriadFsException("You cannot destroy the root.");
Bu::MyriadStream is = mStore.openStream( 2 );
// This will be overwritten with the last node
uint32_t iPosition = hNodeIndex.get( iNode );
RawStat rsOld;
readInode( iNode, rsOld, is );
switch( (rsOld.uPerms&typeMask) )
{
case typeRegFile:
case typeDir:
case typeSymLink:
mStore.deleteStream( rsOld.uStreamIndex );
break;
}
hNodeIndex.erase( iNode );
// Read the last node, can't use the helpers, because we don't know the
// iNode yet.
if( iPosition != hNodeIndex.getSize() )
{
// If this is the last node, then we don't need to do anything, but
// this case handles what to do if we aren't on the last node
RawStat rs;
is.setPos( (hNodeIndex.getSize())*sizeof(RawStat) );
is.read( &rs, sizeof(RawStat) );
hNodeIndex.get( rs.iNode ) = iPosition;
writeInode( rs, is );
}
is.setSize( hNodeIndex.getSize() * sizeof(RawStat) );
}
Bu::String Bu::MyriadFs::filePart( const Bu::String &sPath )
{
Bu::String::const_iterator iStart = sPath.begin();
if( *iStart == '/' )
iStart++;
for( Bu::String::const_iterator iEnd = iStart.find('/'); iEnd;
iStart = iEnd+1, iEnd = iStart.find('/') ) { }
return Bu::String( iStart, sPath.end() );
}
|