aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/stable/myriad.cpp21
-rw-r--r--src/stable/myriad.h24
-rw-r--r--src/stable/myriadstream.cpp6
-rw-r--r--src/stable/myriadstream.h1
-rw-r--r--src/tools/myriadfs.cpp269
-rw-r--r--src/unstable/myriadcache.cpp8
-rw-r--r--src/unstable/myriadcache.h150
-rw-r--r--src/unstable/myriadfs.cpp737
-rw-r--r--src/unstable/myriadfs.h205
9 files changed, 1418 insertions, 3 deletions
diff --git a/src/stable/myriad.cpp b/src/stable/myriad.cpp
index 1bf2301..0bd2ff2 100644
--- a/src/stable/myriad.cpp
+++ b/src/stable/myriad.cpp
@@ -74,7 +74,7 @@ Bu::MyriadStream Bu::Myriad::create( Bu::Myriad::Mode eMode,
74 mhStream.unlock(); 74 mhStream.unlock();
75 bStructureChanged = true; 75 bStructureChanged = true;
76 76
77 return Bu::MyriadStream( *this, pStream, (Mode)(eMode&ReadWrite) ); 77 return Bu::MyriadStream( *this, pStream, eMode&ReadWrite );
78} 78}
79 79
80Bu::MyriadStream Bu::Myriad::open( Bu::Myriad::StreamId iStream, 80Bu::MyriadStream Bu::Myriad::open( Bu::Myriad::StreamId iStream,
@@ -144,6 +144,18 @@ void Bu::Myriad::setSize( Bu::Myriad::StreamId iStream,
144 pStream->setSize( iNewSize ); 144 pStream->setSize( iNewSize );
145} 145}
146 146
147int32_t Bu::Myriad::getSize( StreamId iStream ) const
148{
149 Bu::MutexLocker l( mhStream );
150 return hStream.get( iStream )->getSize();
151}
152
153bool Bu::Myriad::exists( StreamId iStream ) const
154{
155 Bu::MutexLocker l( mhStream );
156 return hStream.has( iStream );
157}
158
147Bu::String Bu::Myriad::getLocation() const 159Bu::String Bu::Myriad::getLocation() const
148{ 160{
149 Bu::MutexLocker l( mAccess ); 161 Bu::MutexLocker l( mAccess );
@@ -218,6 +230,11 @@ int32_t Bu::Myriad::getTotalUnusedBytes(int32_t iAssumeBlockSize ) const
218 return iTotal; 230 return iTotal;
219} 231}
220 232
233void Bu::Myriad::sync()
234{
235 writeHeader();
236}
237
221bool Bu::Myriad::loadMyriad() 238bool Bu::Myriad::loadMyriad()
222{ 239{
223 Bu::println("Load myriad!"); 240 Bu::println("Load myriad!");
@@ -417,12 +434,12 @@ void Bu::Myriad::createMyriad( int32_t iBlockSize, int32_t iPreallocateBlocks )
417 434
418void Bu::Myriad::writeHeader() 435void Bu::Myriad::writeHeader()
419{ 436{
437 Bu::MutexLocker l( mAccess );
420 if( !rBacking.isWritable() ) 438 if( !rBacking.isWritable() )
421 return; 439 return;
422 Bu::println("Writing stream breakdown:"); 440 Bu::println("Writing stream breakdown:");
423 Bu::MemBuf mbHeader; 441 Bu::MemBuf mbHeader;
424 { 442 {
425 Bu::MutexLocker l( mAccess );
426 Bu::MutexLocker l2( mhStream ); 443 Bu::MutexLocker l2( mhStream );
427 444
428 int32_t iHdrStreamSize = __calcHeaderSize(); 445 int32_t iHdrStreamSize = __calcHeaderSize();
diff --git a/src/stable/myriad.h b/src/stable/myriad.h
index 58f700a..c3f682d 100644
--- a/src/stable/myriad.h
+++ b/src/stable/myriad.h
@@ -32,7 +32,7 @@ namespace Bu
32 { 32 {
33 public: 33 public:
34 typedef int32_t StreamId; 34 typedef int32_t StreamId;
35 enum Mode { 35 enum Mode : int32_t {
36 None = 0x00, 36 None = 0x00,
37 37
38 // Flags 38 // Flags
@@ -75,6 +75,8 @@ namespace Bu
75 MyriadStream open( StreamId iStream, Mode eMode ); 75 MyriadStream open( StreamId iStream, Mode eMode );
76 void erase( StreamId iStream ); 76 void erase( StreamId iStream );
77 void setSize( StreamId iStream, int32_t iNewSize ); 77 void setSize( StreamId iStream, int32_t iNewSize );
78 int32_t getSize( StreamId iStream ) const;
79 bool exists( StreamId iStream ) const;
78 Bu::String getLocation() const; 80 Bu::String getLocation() const;
79 int32_t getBlockSize() const; 81 int32_t getBlockSize() const;
80 int32_t getTotalBlocks() const; 82 int32_t getTotalBlocks() const;
@@ -84,6 +86,12 @@ namespace Bu
84 int32_t getTotalUsedBytes() const; 86 int32_t getTotalUsedBytes() const;
85 int32_t getTotalUnusedBytes( int32_t iAssumeBlockSize=-1 ) const; 87 int32_t getTotalUnusedBytes( int32_t iAssumeBlockSize=-1 ) const;
86 88
89 /**
90 * Flush all caches to the backing stream, write all structural and
91 * header changes.
92 */
93 void sync();
94
87 private: 95 private:
88 bool loadMyriad(); 96 bool loadMyriad();
89 void createMyriad( int32_t iBlockSize, int32_t iPreallocateBlocks ); 97 void createMyriad( int32_t iBlockSize, int32_t iPreallocateBlocks );
@@ -175,6 +183,20 @@ namespace Bu
175 IndexList lFreeBlocks; 183 IndexList lFreeBlocks;
176 StreamId iLastUsedIndex; 184 StreamId iLastUsedIndex;
177 }; 185 };
186 constexpr Myriad::Mode operator&( Myriad::Mode a, Myriad::Mode b )
187 {
188 return static_cast<Myriad::Mode>(
189 static_cast<std::underlying_type<Myriad::Mode>::type>(a) &
190 static_cast<std::underlying_type<Myriad::Mode>::type>(b)
191 );
192 }
193 constexpr Myriad::Mode operator|( Myriad::Mode a, Myriad::Mode b )
194 {
195 return static_cast<Myriad::Mode>(
196 static_cast<std::underlying_type<Myriad::Mode>::type>(a) |
197 static_cast<std::underlying_type<Myriad::Mode>::type>(b)
198 );
199 }
178}; 200};
179 201
180#endif 202#endif
diff --git a/src/stable/myriadstream.cpp b/src/stable/myriadstream.cpp
index 9ea2e17..eaf91a5 100644
--- a/src/stable/myriadstream.cpp
+++ b/src/stable/myriadstream.cpp
@@ -166,3 +166,9 @@ Bu::String Bu::MyriadStream::getLocation() const
166 return pStream->getLocation(); 166 return pStream->getLocation();
167} 167}
168 168
169Bu::Myriad::StreamId Bu::MyriadStream::getId() const
170{
171 Bu::MutexLocker l( mAccess );
172 return pStream->getStreamId();
173}
174
diff --git a/src/stable/myriadstream.h b/src/stable/myriadstream.h
index b86dbd7..27a15d5 100644
--- a/src/stable/myriadstream.h
+++ b/src/stable/myriadstream.h
@@ -39,6 +39,7 @@ namespace Bu
39 virtual size getSize() const; 39 virtual size getSize() const;
40 virtual size getBlockSize() const; 40 virtual size getBlockSize() const;
41 virtual Bu::String getLocation() const; 41 virtual Bu::String getLocation() const;
42 Myriad::StreamId getId() const;
42 43
43 private: 44 private:
44 mutable Bu::Mutex mAccess; 45 mutable Bu::Mutex mAccess;
diff --git a/src/tools/myriadfs.cpp b/src/tools/myriadfs.cpp
new file mode 100644
index 0000000..587bc89
--- /dev/null
+++ b/src/tools/myriadfs.cpp
@@ -0,0 +1,269 @@
1/*
2 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/sio.h"
9#include "bu/streamstack.h"
10#include "bu/file.h"
11#include "bu/myriadfs.h"
12#include "bu/myriadstream.h"
13#include "bu/optparser.h"
14
15enum Mode
16{
17 modeList,
18 modeCat,
19 modeCopyIn,
20 modeCopyOut,
21 modeMkdir,
22 modeInitialize,
23 modeRm,
24
25 modeNone
26};
27
28Bu::Formatter &operator>>( Bu::Formatter &f, Mode & /*e*/ )
29{
30 Bu::sio << "Uh oh, the formatter was called..." << Bu::sio.nl;
31 return f;
32}
33
34class Options : public Bu::OptParser
35{
36public:
37 Options( int argc, char *argv[] ) :
38 eMode( modeNone ),
39 iBlockSize( 64 )
40 {
41 addHelpBanner("Options:");
42 addOption( sFile, 'f', "file", "Myriadfs file");
43 addOption( iBlockSize, 'b', "block-size",
44 "Specify the block size when initializing a new MyriadFs");
45
46 setNonOption( Bu::slot( this, &Options::nonOption ) );
47
48 addHelpOption();
49
50 parse( argc, argv );
51 }
52
53 int nonOption( Bu::Array<Bu::String> aParams )
54 {
55 if( eMode == modeNone )
56 {
57 //Bu::println("Checking mode");
58 // First param, must be the mode
59 if( aParams[0] == "ls" )
60 eMode = modeList;
61 else if( aParams[0] == "cat" )
62 eMode = modeCat;
63 else if( aParams[0] == "cp-in" )
64 eMode = modeCopyIn;
65 else if( aParams[0] == "cp-out" )
66 eMode = modeCopyOut;
67 else if( aParams[0] == "mkdir" )
68 eMode = modeMkdir;
69 else if( aParams[0] == "initialize" )
70 eMode = modeInitialize;
71 else if( aParams[0] == "rm" )
72 eMode = modeRm;
73 else
74 Bu::println("Unknown command, try --help");
75 return 0;
76 } else {
77 lParams.append( aParams[0] );
78 }
79 //Bu::println("Got option: \"%1\"").arg( aParams[0] );
80 return 0;
81 }
82
83 Mode eMode;
84 Bu::String sFile;
85 Bu::StringList lParams;
86 int iBlockSize;
87};
88
89int main( int argc, char *argv[] )
90{
91 Options opt( argc, argv );
92
93 if( opt.sFile.isEmpty() )
94 {
95 Bu::println("You must specify a MyriadFs stream (see -f).\n");
96 return 1;
97 }
98
99 if( opt.eMode == modeNone )
100 {
101 Bu::println("Specify an operation to perfrom.\n");
102 return 1;
103 }
104
105 int iFlags = Bu::File::ReadWrite;
106
107 if( opt.eMode == modeInitialize )
108 {
109 iFlags |= Bu::File::Create|Bu::File::Truncate;
110 }
111
112 Bu::File fFs( opt.sFile, iFlags );
113 Bu::MyriadFs mFs( fFs, opt.iBlockSize );
114
115 switch( opt.eMode )
116 {
117 case modeList:
118 {
119 Bu::String sPath = "/";
120 if( opt.lParams.getSize() > 0 )
121 {
122 sPath = opt.lParams.first();
123 }
124 Bu::MyriadFs::Dir lEnt = mFs.readDir( sPath );
125 for( Bu::MyriadFs::Dir::iterator i = lEnt.begin(); i; i++ )
126 {
127 Bu::String sPerm;
128 switch( (*i).uPerms&Bu::MyriadFs::typeMask )
129 {
130 case Bu::MyriadFs::typeDir: sPerm += "d"; break;
131 case Bu::MyriadFs::typeChrDev: sPerm += "c"; break;
132 case Bu::MyriadFs::typeBlkDev: sPerm += "b"; break;
133 case Bu::MyriadFs::typeSymLink: sPerm += "l"; break;
134 case Bu::MyriadFs::typeSocket: sPerm += "s"; break;
135 default: sPerm += "-"; break;
136 }
137 sPerm += ((*i).uPerms&Bu::MyriadFs::permUsrR)?"r":"-";
138 sPerm += ((*i).uPerms&Bu::MyriadFs::permUsrW)?"w":"-";
139 sPerm += ((*i).uPerms&Bu::MyriadFs::permUsrX)?"x":"-";
140 sPerm += ((*i).uPerms&Bu::MyriadFs::permGrpR)?"r":"-";
141 sPerm += ((*i).uPerms&Bu::MyriadFs::permGrpW)?"w":"-";
142 sPerm += ((*i).uPerms&Bu::MyriadFs::permGrpX)?"x":"-";
143 sPerm += ((*i).uPerms&Bu::MyriadFs::permOthR)?"r":"-";
144 sPerm += ((*i).uPerms&Bu::MyriadFs::permOthW)?"w":"-";
145 sPerm += ((*i).uPerms&Bu::MyriadFs::permOthX)?"x":"-";
146 Bu::println("%1 %2 %3:%4 %5 %6")
147 .arg( sPerm )
148 .arg( (*i).iNode )
149 .arg( (*i).iUser )
150 .arg( (*i).iGroup )
151 .arg( (*i).iSize )
152 .arg( (*i).sName );
153 }
154 }
155 break;
156
157 case modeCat:
158 {
159 if( opt.lParams.isEmpty() )
160 {
161 Bu::println("Specify at least one file.");
162 return 1;
163 }
164 int iBlockSize = 1024*1024;
165 char *pBuf = new char[iBlockSize];
166 for( Bu::StringList::iterator i = opt.lParams.begin(); i; i++ )
167 {
168 Bu::MyriadStream ms = mFs.open( *i, Bu::MyriadFs::Read );
169 int iRead = 0;
170 do
171 {
172 iRead = ms.read( pBuf, iBlockSize );
173 if( iRead > 0 )
174 {
175 Bu::sioRaw.write( pBuf, iRead );
176 }
177 } while( iRead == iBlockSize );
178 }
179 delete[] pBuf;
180 }
181 break;
182
183 case modeCopyIn:
184 {
185 if( opt.lParams.getSize() != 2 )
186 {
187 Bu::println("Specify a source file and destination file.");
188 return 1;
189 }
190 int iBlockSize = 1024*1024;
191 char *pBuf = new char[iBlockSize];
192 Bu::File fs = Bu::File(
193 opt.lParams.first(), Bu::File::Read );
194 Bu::MyriadStream ms = mFs.open(
195 opt.lParams.last(), Bu::MyriadFs::WriteNew );
196 int iRead = 0;
197 do
198 {
199 iRead = fs.read( pBuf, iBlockSize );
200 if( iRead > 0 )
201 {
202 ms.write( pBuf, iRead );
203 }
204 } while( iRead == iBlockSize );
205 delete[] pBuf;
206 }
207 break;
208
209 case modeCopyOut:
210 {
211 if( opt.lParams.getSize() != 2 )
212 {
213 Bu::println("Specify a source file and destination file.");
214 return 1;
215 }
216 int iBlockSize = 1024*1024;
217 char *pBuf = new char[iBlockSize];
218 Bu::MyriadStream ms = mFs.open(
219 opt.lParams.first(), Bu::MyriadFs::Read );
220 Bu::File fs = Bu::File(
221 opt.lParams.last(), Bu::File::WriteNew );
222 int iRead = 0;
223 do
224 {
225 iRead = ms.read( pBuf, iBlockSize );
226 if( iRead > 0 )
227 {
228 fs.write( pBuf, iRead );
229 }
230 } while( iRead == iBlockSize );
231 delete[] pBuf;
232 }
233 break;
234
235 case modeMkdir:
236 {
237 if( opt.lParams.isEmpty() )
238 {
239 Bu::println("Specify at least one directory.");
240 return 1;
241 }
242 for( Bu::StringList::iterator i = opt.lParams.begin(); i; i++ )
243 {
244 mFs.mkDir( *i, 0777 );
245 }
246 }
247 break;
248
249 case modeInitialize:
250 Bu::println("MyriadFs initialized.\n");
251 break;
252
253 case modeRm:
254 {
255 if( opt.lParams.getSize() != 1 )
256 {
257 Bu::println("Specify a file path.");
258 return 1;
259 }
260 mFs.unlink( opt.lParams.first() );
261 }
262 break;
263
264 case modeNone:
265 break;
266 }
267
268 return 0;
269}
diff --git a/src/unstable/myriadcache.cpp b/src/unstable/myriadcache.cpp
new file mode 100644
index 0000000..c9eb9c4
--- /dev/null
+++ b/src/unstable/myriadcache.cpp
@@ -0,0 +1,8 @@
1/*
2 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/myriadcache.h"
diff --git a/src/unstable/myriadcache.h b/src/unstable/myriadcache.h
new file mode 100644
index 0000000..d6842a5
--- /dev/null
+++ b/src/unstable/myriadcache.h
@@ -0,0 +1,150 @@
1/*
2 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_MYRIAD_CACHE_H
9#define BU_MYRIAD_CACHE_H
10
11#include "bu/cachebase.h"
12#include "bu/myriad.h"
13#include "bu/myriadstream.h"
14#include "bu/file.h"
15#include "bu/streamstack.h"
16
17namespace Bu
18{
19 template<typename keytype, typename obtype>
20 class MyriadCache : public Bu::CacheBase<keytype, obtype>
21 {
22 public:
23 MyriadCache( Bu::Stream &sStore, int iBlockSize=512, int iPreallocate=8 ) :
24 sStore( sStore ),
25 mStore( sStore, iBlockSize, iPreallocate ),
26 bStructureChanged( false )
27 {
28 try
29 {
30 Bu::ReadWriteMutex::ReadLocker l( rwStore );
31 Bu::MyriadStream ms = mStore.open(
32 1, Bu::Myriad::WriteNew|Bu::Myriad::Read
33 );
34 Bu::Archive ar( ms, Bu::Archive::load );
35 uint8_t uVer;
36 ar >> uVer;
37 switch( uVer )
38 {
39 case 0:
40 ar >> hIndex;
41 break;
42 }
43 }
44 catch(...)
45 {
46 try
47 {
48 mStore.open( 1, Bu::Myriad::Create|Bu::Myriad::ReadWrite );
49 _sync();
50 }
51 catch(...)
52 {
53 throw Bu::ExceptionBase("Error creating index stream.");
54 }
55 }
56 }
57
58 virtual ~MyriadCache()
59 {
60 Bu::CacheBase<keytype,obtype>::sync();
61 }
62
63 using typename Bu::CacheBase<keytype,obtype>::KeyList;
64 using typename Bu::CacheBase<keytype,obtype>::ObjectType;
65
66 virtual typename Bu::CacheBase<keytype,obtype>::KeyList getKeys() const
67 {
68 Bu::ReadWriteMutex::ReadLocker rl( rwStore );
69 return hIndex.getKeys();
70 }
71
72 virtual int getSize() const
73 {
74 Bu::ReadWriteMutex::ReadLocker rl( rwStore );
75 return hIndex.getSize();
76 }
77
78 protected:
79 virtual bool _has( const keytype &key )
80 {
81 Bu::ReadWriteMutex::ReadLocker rl( rwStore );
82 return hIndex.has( key );
83 }
84
85 virtual void _create( const obtype *o )
86 {
87 Bu::ReadWriteMutex::WriteLocker wl( rwStore );
88 {
89 Bu::MyriadStream ms = mStore.create( Bu::Myriad::Create );
90 hIndex.insert( o->getKey(), ms.getId() );
91 }
92 _save( o );
93
94 bStructureChanged = true;
95 }
96
97 virtual void _erase( const keytype &k )
98 {
99 Bu::ReadWriteMutex::WriteLocker wl( rwStore );
100 mStore.erase( hIndex.get( k ) );
101 hIndex.erase( k );
102
103 bStructureChanged = true;
104 }
105
106 virtual obtype *_load(
107 typename Bu::CacheObject<keytype, obtype>::Initializer &initObj,
108 const keytype &k
109 )
110 {
111 Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) );
112 return _cacheObjectLoad<keytype, obtype>( initObj, k, ms );
113 }
114
115 virtual void _save( const obtype *o )
116 {
117 Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) );
118 _cacheObjectSave( ms, o );
119 ms.setSize( ms.tell() );
120
121 mStore.sync();
122 }
123
124 virtual void _sync()
125 {
126 Bu::ReadWriteMutex::WriteLocker wl( rwStore );
127 if( !bStructureChanged )
128 return;
129
130 Bu::MyriadStream ms = mStore.openStream( 1 );
131 Bu::Archive ar( ms, Bu::Archive::save );
132 ar << (uint8_t)0 << hIndex;
133 ar.close();
134 ms.setSize( ms.tell() );
135
136 bStructureChanged = false;
137
138 mStore.sync();
139 }
140
141 private:
142 Bu::Stream &sStore;
143 Bu::Myriad mStore;
144 Bu::Hash<keytype, int> hIndex;
145 mutable Bu::ReadWriteMutex rwStore;
146 bool bStructureChanged;
147 };
148}
149
150#endif
diff --git a/src/unstable/myriadfs.cpp b/src/unstable/myriadfs.cpp
new file mode 100644
index 0000000..ab9ca74
--- /dev/null
+++ b/src/unstable/myriadfs.cpp
@@ -0,0 +1,737 @@
1/*
2 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/config.h"
9#include "bu/myriadfs.h"
10#include "bu/myriadstream.h"
11
12#include <string.h>
13#include <unistd.h>
14#include <time.h>
15
16#include "bu/sio.h"
17using Bu::sio;
18using Bu::Fmt;
19
20namespace Bu { subExceptionDef( MyriadFsException ) }
21
22#define Myriad_Fs_MAGIC_CODE ((char *)"\xa7\x18\x8b\x39")
23
24Bu::MyriadFs::MyriadFs( Bu::Stream &rStore, int iBlockSize ) :
25 rStore( rStore ),
26 mStore( rStore, iBlockSize ),
27 iUser( 0 ),
28 iGroup( 0 )
29{
30#ifndef WIN32
31 iUser = getuid();
32 iGroup = getgid();
33#endif
34
35 if( mStore.exists( 1 ) )
36 {
37 // Check to see if this is a MyriadFs stream.
38 Bu::MyriadStream ms = mStore.open( 1, Bu::Myriad::Read );
39 char sMagic[4];
40 if( ms.read( sMagic, 4 ) < 4 )
41 throw MyriadFsException("The provided stream does not appear to be "
42 "a MyriadFs stream.");
43 if( ::strncmp( sMagic, Myriad_Fs_MAGIC_CODE, 4 ) )
44 throw MyriadFsException("The provided stream does not appear to be "
45 "a MyriadFs stream.");
46
47 int8_t iVer;
48 ms.read( &iVer, 1 );
49
50 int32_t iNumNodes;
51 ms.read( &iNumNodes, 4 );
52 for( int32_t j = 0; j < iNumNodes; j++ )
53 {
54 int32_t iNode;
55 int32_t iPos;
56 ms.read( &iNode, 4 );
57 ms.read( &iPos, 4 );
58 hNodeIndex.insert( iNode, iPos );
59 }
60 }
61 else
62 {
63 // Create initial header stream
64 {
65 Bu::MyriadStream ms = mStore.open(
66 1, Bu::Myriad::WriteNew|Bu::Myriad::Exclusive );
67 ms.write( Myriad_Fs_MAGIC_CODE, 4 );
68 int8_t iVer = 1;
69 int32_t iTmp = 1;
70 ms.write( &iVer, 1 );
71 ms.write( &iTmp, 4 ); // iNumNodes
72 iTmp = 0;
73 ms.write( &iTmp, 4 ); // iInode
74 ms.write( &iTmp, 4 ); // iPosition
75 hNodeIndex.insert( 0, 0 );
76 }
77
78 // Create initial inode stream, with one root node.
79 {
80 Bu::MyriadStream ms = mStore.open(
81 2, Bu::Myriad::WriteNew|Bu::Myriad::Exclusive );
82 RawStat rs;
83 rs.iNode = 0;
84 rs.iUser = iUser;
85 rs.iGroup = iGroup;
86 rs.uPerms = 0755|typeDir;
87 rs.iLinks = 1;
88 rs.uStreamIndex = 3;
89 rs.iCTime = rs.iMTime = rs.iATime = time(NULL);
90 ms.write( &rs, sizeof(RawStat) );
91 }
92
93 // Create inode 0's storage stream.
94 {
95 Bu::MyriadStream ms = mStore.open(
96 3, Bu::Myriad::WriteNew|Bu::Myriad::Exclusive );
97 int32_t iTmp32 = 0;
98 ms.write( &iTmp32, 4 ); // iChildCount
99 }
100 }
101}
102
103Bu::MyriadFs::~MyriadFs()
104{
105 writeHeader();
106}
107
108void Bu::MyriadFs::stat( const Bu::String &sPath, Bu::MyriadFs::Stat &rBuf )
109{
110 int32_t iParent;
111 int32_t iNode = lookupInode( sPath, iParent );
112 Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::Read );
113 stat( iNode, rBuf, is );
114}
115
116Bu::MyriadStream Bu::MyriadFs::open( const Bu::String &sPath, int iMode,
117 uint16_t uPerms )
118{
119 int32_t iParent = -1;
120 int32_t iNode;
121 try
122 {
123 iNode = lookupInode( sPath, iParent );
124// sio << "File found." << sio.nl;
125 // The file was found
126 Bu::MyriadStream ms = openByInode( iNode );
127 if( (iMode&Truncate) )
128 {
129 ms.setSize( 0 );
130 }
131 return ms;
132 }
133 catch( Bu::MyriadFsException &e )
134 {
135 if( iParent < 0 )
136 throw;
137
138 // This means that an intermediate path component couldn't be found
139 if( e.getErrorCode() == 1 )
140 throw;
141
142 // The file wasn't found, but the path leading up to it was.
143 // first, figure out the final path element...
144 Bu::String sName = filePart( sPath );
145// sio << "End filename: " << sName << sio.nl;
146// sio << "Parent inode: " << iParent << sio.nl;
147 iNode = create( iParent, sName, (uPerms&permMask)|typeRegFile, 0 );
148// sio << "New iNode: " << iNode << sio.nl;
149 return openByInode( iNode );
150 }
151}
152
153void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms )
154{
155 create( sPath, iPerms, 0 );
156}
157
158void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms,
159 uint16_t iDevHi, uint16_t iDevLo )
160{
161 create( sPath, iPerms, ((uint32_t)iDevHi<<16)|(uint32_t)iDevLo );
162}
163
164void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms,
165 uint32_t uSpecial )
166{
167 int32_t iParent = -1;
168// int32_t iNode;
169 try
170 {
171 /*iNode =*/ lookupInode( sPath, iParent );
172// sio << "File found." << sio.nl;
173 }
174 catch( Bu::MyriadFsException &e )
175 {
176 if( iParent < 0 )
177 throw;
178
179 // This means that an intermediate path component couldn't be found
180 if( e.getErrorCode() == 1 )
181 throw;
182
183 // The file wasn't found, but the path leading up to it was.
184 // first, figure out the final path element...
185 Bu::String sName = filePart( sPath );
186// sio << "End filename: " << sName << sio.nl;
187// sio << "Parent inode: " << iParent << sio.nl;
188 /*iNode =*/ create( iParent, sName, iPerms, uSpecial );
189// sio << "New iNode: " << iNode << sio.nl;
190 }
191 // The file was found
192 //throw Bu::MyriadFsException("Path already exists.");
193}
194
195void Bu::MyriadFs::mkDir( const Bu::String &sPath, uint16_t iPerms )
196{
197 create( sPath, (iPerms&permMask)|typeDir, 0 );
198}
199
200void Bu::MyriadFs::mkSymLink( const Bu::String &sTarget,
201 const Bu::String &sPath )
202{
203 int32_t iParent = -1;
204 int32_t iNode;
205 try
206 {
207 iNode = lookupInode( sPath, iParent );
208 }
209 catch( Bu::MyriadFsException &e )
210 {
211 if( iParent < 0 )
212 throw;
213
214 // This means that an intermediate path component couldn't be found
215 if( e.getErrorCode() == 1 )
216 throw;
217
218 // The file wasn't found, but the path leading up to it was.
219 // first, figure out the final path element...
220 Bu::String sName = filePart( sPath );
221// sio << "End filename: " << sName << sio.nl;
222// sio << "Parent inode: " << iParent << sio.nl;
223 iNode = create( iParent, sName, 0777|typeSymLink, 0 );
224// sio << "New iNode: " << iNode << sio.nl;
225 MyriadStream ms = openByInode( iNode );
226 ms.write( sTarget );
227 return;
228 }
229 throw Bu::MyriadFsException("Path already exists.");
230}
231
232void Bu::MyriadFs::mkHardLink( const Bu::String &sTarget,
233 const Bu::String &sPath )
234{
235 int32_t iParent = -1;
236 int32_t iNode;
237
238 iNode = lookupInode( sTarget, iParent );
239
240 try
241 {
242 lookupInode( sPath, iParent );
243 throw Bu::MyriadFsException("Path already exists.");
244 }
245 catch( Bu::MyriadFsException &e )
246 {
247 if( iParent < 0 )
248 throw;
249
250 // This means that an intermediate path component couldn't be found
251 if( e.getErrorCode() == 1 )
252 throw;
253
254 // The file wasn't found, but the path leading up to it was.
255 // first, figure out the final path element...
256 Bu::String sName = filePart( sPath );
257// sio << "End filename: " << sName << sio.nl;
258// sio << "Parent inode: " << iParent << sio.nl;
259 addToDir( iParent, iNode, sName );
260 MyriadStream is = mStore.open( 2, Bu::Myriad::ReadWrite );
261 RawStat rs;
262 readInode( iNode, rs, is );
263 rs.iLinks++;
264 writeInode( rs, is );
265 }
266}
267
268Bu::String Bu::MyriadFs::readSymLink( const Bu::String &sPath )
269{
270 int32_t iParent = -1;
271 int32_t iNode;
272 iNode = lookupInode( sPath, iParent );
273 MyriadStream ms = openByInode( iNode );
274 Bu::String sRet;
275 sRet.setSize( ms.getSize() );
276 ms.read( sRet.getStr(), ms.getSize() );
277 return sRet;
278}
279
280Bu::MyriadFs::Dir Bu::MyriadFs::readDir( const Bu::String &sPath )
281{
282 int32_t iParent = -1;
283 int32_t iNode = lookupInode( sPath, iParent );
284 return readDir( iNode );
285}
286
287void Bu::MyriadFs::setTimes( const Bu::String &sPath, int64_t iATime,
288 int64_t iMTime )
289{
290 int32_t iParent = -1;
291 int32_t iNode;
292
293 iNode = lookupInode( sPath, iParent );
294
295 setTimes( iNode, iATime, iMTime );
296}
297
298void Bu::MyriadFs::unlink( const Bu::String &sPath )
299{
300 int32_t iParent = -1;
301// int32_t iNode;
302
303 /*iNode =*/ lookupInode( sPath, iParent );
304
305 Dir lDir = readDir( iParent );
306
307 Bu::String sName = filePart( sPath );
308
309 for( Dir::iterator i = lDir.begin(); i; i++ )
310 {
311 if( sName == (*i).sName )
312 {
313 RawStat rs;
314 readInode( (*i).iNode, rs );
315 if( (rs.uPerms&typeMask) == typeDir )
316 {
317 MyriadStream msDir = mStore.open(
318 rs.uStreamIndex, Bu::Myriad::Read
319 );
320 int32_t iCount;
321 msDir.read( &iCount, 4 );
322 if( iCount > 0 )
323 {
324 throw Bu::MyriadFsException("Directory not empty.");
325 }
326 }
327 if( --rs.iLinks == 0 )
328 {
329 destroyNode( (*i).iNode );
330 }
331 else
332 {
333 writeInode( rs );
334 }
335 lDir.erase( i );
336 break;
337 }
338 }
339
340 Bu::MyriadStream ms = openByInode( iParent );
341 int32_t iNumChildren = lDir.getSize();
342 ms.write( &iNumChildren, 4 );
343 for( Dir::iterator i = lDir.begin(); i; i++ )
344 {
345 ms.write( &(*i).iNode, 4 );
346 uint8_t iSize = (*i).sName.getSize();
347 ms.write( &iSize, 1 );
348 ms.write( (*i).sName.getStr(), iSize );
349 }
350 ms.setSize( ms.tell() );
351}
352
353void Bu::MyriadFs::setFileSize( const Bu::String &sPath, int32_t iSize )
354{
355 int32_t iParent = -1;
356 int32_t iNode;
357 iNode = lookupInode( sPath, iParent );
358 MyriadStream ms = openByInode( iNode );
359 ms.setSize( iSize );
360}
361
362void Bu::MyriadFs::rename( const Bu::String &sFrom, const Bu::String &sTo )
363{
364 mkHardLink( sFrom, sTo );
365 unlink( sFrom );
366}
367
368dev_t Bu::MyriadFs::devToSys( uint32_t uDev )
369{
370 return (((uDev&0xFFFF0000)>>8)&0xFF00) | ((uDev&0xFF));
371}
372
373uint32_t Bu::MyriadFs::sysToDev( dev_t uDev )
374{
375 return (((uint32_t)uDev&0xFF00)<<8) | ((uint32_t)uDev&0xFF);
376}
377
378int32_t Bu::MyriadFs::lookupInode( const Bu::String &sPath, int32_t &iParent )
379{
380 if( sPath == "/" )
381 {
382 return 0;
383 }
384 if( sPath[0] == '/' )
385 {
386 // Absolute lookup
387 return lookupInode( sPath.begin()+1, 0, iParent );
388 }
389 else
390 {
391 // Relative lookup
392 throw Bu::ExceptionBase(
393 "Relative lookups in MyriadFs are not working yet.");
394 }
395}
396
397int32_t Bu::MyriadFs::lookupInode( Bu::String::const_iterator iStart,
398 int32_t iNode, int32_t &iParent )
399{
400 iParent = iNode;
401
402 Bu::String::const_iterator iEnd = iStart.find('/');
403 Bu::String sTok( iStart, iEnd );
404
405// sio << "Direcotry component: " << sTok << sio.nl;
406
407 Dir lDir = readDir( iNode );
408
409 for( Dir::iterator i = lDir.begin(); i; i++ )
410 {
411 if( (*i).sName == sTok )
412 {
413 // We found an item
414 if( !iEnd )
415 {
416 // It's the last one in the requested path, return it
417 return (*i).iNode;
418 }
419 else
420 {
421 // Not the last one in our path, double check it's a dir
422 if( ((*i).uPerms&typeMask) == typeDir )
423 {
424 return lookupInode( iEnd+1, (*i).iNode, iParent );
425 }
426 else
427 {
428 iParent = -1;
429 throw Bu::MyriadFsException(
430 "Element '%s' in given path is not a directory.",
431 sTok.getStr() );
432 }
433 }
434 }
435 }
436
437 if( iEnd )
438 throw Bu::MyriadFsException( 1, "Path not found");
439 else
440 throw Bu::MyriadFsException( 2, "Path not found");
441}
442
443void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs )
444{
445 rIs.setPos( hNodeIndex.get( iNode )*sizeof(RawStat) );
446 if( rIs.read( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) )
447 throw Bu::MyriadFsException("Filesystem corruption detected.");
448 if( rs.iNode != iNode )
449 throw Bu::MyriadFsException("Filesystem corruption detected.");
450}
451
452void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs )
453{
454 MyriadStream ms = mStore.open( 2, Bu::Myriad::Read );
455 readInode( iNode, rs, ms );
456}
457
458void Bu::MyriadFs::writeInode( const RawStat &rs,
459 MyriadStream &rOs )
460{
461 rOs.setSize( hNodeIndex.getSize()*sizeof(RawStat) );
462 rOs.setPos( hNodeIndex.get( rs.iNode )*sizeof(RawStat) );
463 if( rOs.write( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) )
464 throw Bu::MyriadFsException("Error writing inode to header stream.");
465}
466
467void Bu::MyriadFs::writeInode( const RawStat &rs )
468{
469 MyriadStream ms = mStore.open( 2, Bu::Myriad::Write );
470 writeInode( rs, ms );
471}
472
473Bu::MyriadFs::Dir Bu::MyriadFs::readDir( int32_t iNode )
474{
475 Bu::MyriadStream ms = openByInode( iNode );
476 int32_t iNumChildren = 0;
477 ms.read( &iNumChildren, 4 );
478
479 Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::Read );
480 Dir lDir;
481 // sio << "Reading dir " << iNode << ", " << iNumChildren << " entries:" << sio.nl;
482 for( int32_t j = 0; j < iNumChildren; j++ )
483 {
484 int32_t iChildNode = 0;
485 if( ms.read( &iChildNode, 4 ) < 4 )
486 {
487 throw Bu::MyriadFsException(
488 "Failed to read iChildNode from directory.");
489 }
490 Stat s;
491 stat( iChildNode, s, is );
492 uint8_t uLen;
493 if( ms.read( &uLen, 1 ) < 1 )
494 {
495 throw Bu::MyriadFsException(
496 "Failed to read uLen from directory.");
497 }
498 s.sName.setSize( uLen );
499 if( ms.read( s.sName.getStr(), uLen ) < uLen )
500 {
501 throw Bu::MyriadFsException(
502 "Failed to read sName from directory.");
503 }
504 lDir.append( s );
505
506// sio << " " << s.sName << sio.nl;
507 }
508
509 return lDir;
510}
511
512Bu::MyriadStream Bu::MyriadFs::openByInode( int32_t iNode )
513{
514 RawStat rs;
515 readInode( iNode, rs );
516 switch( (rs.uPerms&typeMask) )
517 {
518 case typeDir:
519 case typeSymLink:
520 case typeRegFile:
521 return mStore.open( rs.uStreamIndex, Bu::Myriad::ReadWrite );
522
523 default:
524 throw Bu::MyriadFsException(
525 "inode incorrect type for low-level openByInode.");
526 }
527}
528
529void Bu::MyriadFs::addToDir( int32_t iDir, int32_t iNode,
530 const Bu::String &sName )
531{
532 if( sName.getSize() > 255 )
533 {
534 throw Bu::MyriadFsException("Filename too long, max is 255 bytes.");
535 }
536 Bu::MyriadStream ms = openByInode( iDir );
537 int32_t iNumChildren = 0;
538 ms.read( &iNumChildren, 4 );
539 iNumChildren++;
540 ms.setPos( 0 );
541 ms.write( &iNumChildren, 4 );
542 ms.setPosEnd( 0 );
543 ms.write( &iNode, 4 );
544 uint8_t uLen = sName.getSize();
545 ms.write( &uLen, 1 );
546 ms.write( sName.getStr(), uLen );
547}
548
549int32_t Bu::MyriadFs::create( int32_t iParent, const Bu::String &sName,
550 uint16_t uPerms, uint32_t uSpecial )
551{
552 int32_t iNode = allocInode( uPerms, uSpecial );
553 addToDir( iParent, iNode, sName );
554 return iNode;
555}
556
557int32_t Bu::MyriadFs::allocInode( uint16_t uPerms, uint32_t uSpecial )
558{
559 int32_t iNode = 0;
560 for(; iNode < 0xfffffff; iNode++ )
561 {
562 if( !hNodeIndex.has( iNode ) )
563 {
564 hNodeIndex.insert( iNode, hNodeIndex.getSize() );
565 RawStat rs;
566 rs.iNode = iNode;
567 rs.iUser = iUser;
568 rs.iGroup = iGroup;
569 rs.uPerms = uPerms;
570 rs.iLinks = 1;
571 switch( (uPerms&typeMask) )
572 {
573 case typeRegFile:
574 case typeSymLink:
575 {
576 Bu::MyriadStream ms = mStore.create(
577 Bu::Myriad::Create
578 );
579 rs.uStreamIndex = ms.getId();
580 }
581 break;
582
583 case typeDir:
584 {
585 Bu::MyriadStream ms = mStore.create(
586 Bu::Myriad::Create
587 );
588 rs.uStreamIndex = ms.getId();
589 }
590// sio << "Creating directory node, storage: "
591// << rs.uStreamIndex << sio.nl;
592 {
593 Bu::MyriadStream msDir = mStore.open(
594 rs.uStreamIndex, Bu::Myriad::Write
595 );
596 uint32_t uSize = 0;
597 msDir.write( &uSize, 4 );
598 }
599 break;
600
601 case typeChrDev:
602 case typeBlkDev:
603 rs.uStreamIndex = uSpecial;
604 break;
605
606 default:
607 rs.uStreamIndex = 0;
608 break;
609 }
610 rs.iATime = time(NULL);
611 rs.iMTime = time(NULL);
612 rs.iCTime = time(NULL);
613 writeInode( rs );
614
615 return iNode;
616 }
617 }
618
619 throw Bu::MyriadFsException(
620 "No inode could be allocated. You've run out!");
621}
622
623void Bu::MyriadFs::stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs )
624{
625 RawStat rs;
626 readInode( iNode, rs, rIs );
627 rBuf.iNode = iNode;
628 rBuf.iUser = rs.iUser;
629 rBuf.iGroup = rs.iGroup;
630 rBuf.uPerms = rs.uPerms;
631 rBuf.iLinks = rs.iLinks;
632 rBuf.iATime = rs.iATime;
633 rBuf.iMTime = rs.iMTime;
634 rBuf.iCTime = rs.iCTime;
635 rBuf.uDev = 0;
636 rBuf.iSize = 0;
637 switch( (rBuf.uPerms&typeMask) )
638 {
639 case typeRegFile:
640 case typeSymLink:
641 rBuf.iSize = mStore.getSize( rs.uStreamIndex );
642 break;
643
644 case typeChrDev:
645 case typeBlkDev:
646 rBuf.uDev = rs.uStreamIndex;
647 break;
648
649 default:
650 rBuf.iSize = 0;
651 break;
652 }
653}
654
655void Bu::MyriadFs::writeHeader()
656{
657 Bu::MyriadStream ms = mStore.open( 1, Bu::Myriad::Write );
658 ms.write( Myriad_Fs_MAGIC_CODE, 4 );
659 int8_t iVer = 1;
660 int32_t iNumNodes = hNodeIndex.getSize();
661 ms.write( &iVer, 1 );
662 ms.write( &iNumNodes, 4 ); // iNumNodes
663 for( NodeIndex::iterator i = hNodeIndex.begin(); i; i++ )
664 {
665 int32_t iNode = i.getKey();
666 int32_t iPosition = i.getValue();
667 ms.write( &iNode, 4 );
668 ms.write( &iPosition, 4 );
669 }
670
671 // Truncate the stream afterwards so we don't use up too much space.
672 ms.setSize( ms.tell() );
673}
674
675void Bu::MyriadFs::setTimes( int32_t iNode, int64_t iATime, int64_t iMTime )
676{
677 RawStat rs;
678 Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::ReadWrite );
679
680 readInode( iNode, rs, is );
681 rs.iATime = iATime;
682 rs.iMTime = iMTime;
683 writeInode( rs, is );
684}
685
686void Bu::MyriadFs::destroyNode( int32_t iNode )
687{
688 if( iNode == 0 )
689 throw Bu::MyriadFsException("You cannot destroy the root.");
690
691 uint32_t iPosition;
692 RawStat rsOld;
693
694 Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::ReadWrite );
695
696 // This will be overwritten with the last node
697 iPosition = hNodeIndex.get( iNode );
698 readInode( iNode, rsOld, is );
699
700 switch( (rsOld.uPerms&typeMask) )
701 {
702 case typeRegFile:
703 case typeDir:
704 case typeSymLink:
705 mStore.erase( rsOld.uStreamIndex );
706 break;
707 }
708
709 hNodeIndex.erase( iNode );
710
711 // Read the last node, can't use the helpers, because we don't know the
712 // iNode yet.
713 if( iPosition != hNodeIndex.getSize() )
714 {
715 // If this is the last node, then we don't need to do anything, but
716 // this case handles what to do if we aren't on the last node
717 RawStat rs;
718 is.setPos( (hNodeIndex.getSize())*sizeof(RawStat) );
719 is.read( &rs, sizeof(RawStat) );
720
721 hNodeIndex.get( rs.iNode ) = iPosition;
722 writeInode( rs, is );
723 }
724
725 is.setSize( hNodeIndex.getSize() * sizeof(RawStat) );
726}
727
728Bu::String Bu::MyriadFs::filePart( const Bu::String &sPath )
729{
730 Bu::String::const_iterator iStart = sPath.begin();
731 if( *iStart == '/' )
732 iStart++;
733 for( Bu::String::const_iterator iEnd = iStart.find('/'); iEnd;
734 iStart = iEnd+1, iEnd = iStart.find('/') ) { }
735 return Bu::String( iStart, sPath.end() );
736}
737
diff --git a/src/unstable/myriadfs.h b/src/unstable/myriadfs.h
new file mode 100644
index 0000000..ff14292
--- /dev/null
+++ b/src/unstable/myriadfs.h
@@ -0,0 +1,205 @@
1/*
2 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef MYRIAD_FS_H
9#define MYRIAD_FS_H
10
11#include <sys/types.h>
12
13#include "bu/myriad.h"
14#include "bu/readwritemutex.h"
15
16namespace Bu
17{
18 class Stream;
19
20 subExceptionDecl( MyriadFsException );
21
22 /**
23 * A POSIX compliant, node based filesystem built on top of Myriad.
24 *
25 * A header is placed into stream 1.
26 * Header format:
27 * int32_t iMagicHeader (A7188B39)
28 * int8_t iVersion (1)
29 * int32_t iNumNodes
30 * NodeLookup[iNumNodes] nNode
31 *
32 * Node lookup:
33 * int32_t iInode
34 * int32_t iPosition
35 *
36 * The node headers or inode structures have a base size of 44 bytes.
37 * The name is stored in the directory format.
38 * Basic node header format:
39 * int32_t iNode
40 * int32_t iUser
41 * int32_t iGroup
42 * uint16_t uPerms
43 * int16_t iLinks
44 * uint32_t uStreamIndex
45 * int64_t iATime
46 * int64_t iMTime
47 * int64_t iCTime
48 *
49 * Some types get special formats for their assosiated data stream, or
50 * other special considerations, here's a list:
51 *
52 * - typeFifo: No stream, uStreamIndex unused (probably)
53 * - typeChrDev: No stream, uStreamIndex is device hi/lo
54 * - typeDir: The stream contains a directory contents listing, described
55 * below
56 * - typeBlkDev: No stream, uStreamIndex is device hi/lo
57 * - typeRegFile: The stream is the file data
58 * - typeSymLink: The stream is the destination of the symlink
59 * - typeSocket: No steram, uStreamIndex unused (probably)
60 *
61 * Directory streams have this simple listing format. They contain a list
62 * of all child elements, with no particular order at the moment. The . and
63 * .. entries are not listed, they are implicit:
64 * int32_t iNumNodes
65 * NodeTable[iNumNodes] nChildren
66 *
67 * NodeTable:
68 * int32_t iInode
69 * uint8_t uNameSize
70 * char[uNameSize] sName
71 */
72 class MyriadFs
73 {
74 public:
75 MyriadFs( Bu::Stream &rStore, int iBlockSize=512 );
76 virtual ~MyriadFs();
77
78 enum
79 {
80 permOthX = 0000001,
81 permOthW = 0000002,
82 permOthR = 0000004,
83 permGrpX = 0000010,
84 permGrpW = 0000020,
85 permGrpR = 0000040,
86 permUsrX = 0000100,
87 permUsrW = 0000200,
88 permUsrR = 0000400,
89 permSticky = 0001000,
90 permSetGid = 0002000,
91 permSetUid = 0004000,
92 permMask = 0007777,
93 typeFifo = 0010000,
94 typeChrDev = 0020000,
95 typeDir = 0040000,
96 typeBlkDev = 0060000,
97 typeRegFile = 0100000,
98 typeSymLink = 0120000,
99 typeSocket = 0140000,
100 typeMask = 0170000
101 };
102
103 enum
104 {
105 Read = 0x01, ///< Open file for reading
106 Write = 0x02, ///< Open file for writing
107 Create = 0x04, ///< Create file if it doesn't exist
108 Truncate = 0x08, ///< Truncate file if it does exist
109 Append = 0x10, ///< Always append on every write
110 NonBlock = 0x20, ///< Open file in non-blocking mode
111 Exclusive = 0x44, ///< Create file, if it exists then fail
112
113 // Helpful mixes
114 ReadWrite = 0x03, ///< Open for reading and writing
115 WriteNew = 0x0E ///< Create a file (or truncate) for writing.
116 /// Same as Write|Create|Truncate
117 };
118
119 class Stat
120 {
121 public:
122 int32_t iNode;
123 int32_t iUser;
124 int32_t iGroup;
125 uint16_t uPerms;
126 int16_t iLinks;
127 int64_t iATime;
128 int64_t iMTime;
129 int64_t iCTime;
130 int32_t iSize;
131 uint32_t uDev;
132 Bu::String sName;
133 };
134 typedef Bu::List<Stat> Dir;
135
136 void stat( const Bu::String &sPath, Stat &rBuf );
137 MyriadStream open( const Bu::String &sPath, int iMode,
138 uint16_t uPerms=0664 );
139 void create( const Bu::String &sPath, uint16_t iPerms );
140 void create( const Bu::String &sPath, uint16_t iPerms,
141 uint16_t iDevHi, uint16_t iDevLo );
142 void create( const Bu::String &sPath, uint16_t iPerms,
143 uint32_t uSpecial );
144 void mkDir( const Bu::String &sPath, uint16_t iPerms );
145 void mkSymLink( const Bu::String &sTarget, const Bu::String &sPath );
146 void mkHardLink( const Bu::String &sTarget, const Bu::String &sPath );
147 Bu::String readSymLink( const Bu::String &sPath );
148 Dir readDir( const Bu::String &sPath );
149 void setTimes( const Bu::String &sPath, int64_t iATime,
150 int64_t iMTime );
151 void unlink( const Bu::String &sPath );
152 void setFileSize( const Bu::String &sPath, int32_t iSize );
153 void rename( const Bu::String &sFrom, const Bu::String &sTo );
154
155 static dev_t devToSys( uint32_t uDev );
156 static uint32_t sysToDev( dev_t uDev );
157
158 private:
159 class RawStat
160 {
161 public:
162 int32_t iNode;
163 int32_t iUser;
164 int32_t iGroup;
165 uint16_t uPerms;
166 int16_t iLinks;
167 uint32_t uStreamIndex;
168 int64_t iATime;
169 int64_t iMTime;
170 int64_t iCTime;
171 };
172 typedef Bu::Hash<int32_t, int32_t> NodeIndex;
173
174 private:
175 int32_t lookupInode( const Bu::String &sPath, int32_t &iParent );
176 int32_t lookupInode( Bu::String::const_iterator iStart,
177 int32_t iNode, int32_t &iParent );
178 void readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs );
179 void readInode( int32_t iNode, RawStat &rs );
180 void writeInode( const RawStat &rs );
181 void writeInode( const RawStat &rs, MyriadStream &rOs );
182 Dir readDir( int32_t iNode );
183 MyriadStream openByInode( int32_t iNode );
184 void addToDir( int32_t iDir, int32_t iNode, const Bu::String &sName );
185 int32_t create( int32_t iParent, const Bu::String &sName,
186 uint16_t uPerms, uint32_t uSpecial );
187 int32_t allocInode( uint16_t uPerms, uint32_t uSpecial );
188 void stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs );
189 void writeHeader();
190 void setTimes( int32_t iNode, int64_t iATime, int64_t iMTime );
191 void destroyNode( int32_t iNode );
192
193 Bu::String filePart( const Bu::String &sPath );
194
195 private:
196 Bu::Stream &rStore;
197 Bu::Myriad mStore;
198 Bu::ReadWriteMutex mNodeIndex;
199 NodeIndex hNodeIndex;
200 int32_t iUser;
201 int32_t iGroup;
202 };
203};
204
205#endif