diff options
Diffstat (limited to 'src/unstable')
| -rw-r--r-- | src/unstable/bitstring.cpp | 12 | ||||
| -rw-r--r-- | src/unstable/bitstring.h | 10 | ||||
| -rw-r--r-- | src/unstable/myriadcache.cpp | 8 | ||||
| -rw-r--r-- | src/unstable/myriadcache.h | 150 | ||||
| -rw-r--r-- | src/unstable/myriadfs.cpp | 739 | ||||
| -rw-r--r-- | src/unstable/myriadfs.h | 211 |
6 files changed, 1126 insertions, 4 deletions
diff --git a/src/unstable/bitstring.cpp b/src/unstable/bitstring.cpp index 21c1316..b80c073 100644 --- a/src/unstable/bitstring.cpp +++ b/src/unstable/bitstring.cpp | |||
| @@ -209,7 +209,7 @@ void Bu::BitString::flipBit( long iBit ) | |||
| 209 | caData[iBit/8] ^= (1<<(iBit%8)); | 209 | caData[iBit/8] ^= (1<<(iBit%8)); |
| 210 | } | 210 | } |
| 211 | 211 | ||
| 212 | bool Bu::BitString::getBit( long iBit ) | 212 | bool Bu::BitString::getBit( long iBit ) const |
| 213 | { | 213 | { |
| 214 | if( iBit >= iBits || iBit < 0 ) return false; | 214 | if( iBit >= iBits || iBit < 0 ) return false; |
| 215 | if( (caData[iBit/8] & (1<<(iBit%8))) == 0 ) | 215 | if( (caData[iBit/8] & (1<<(iBit%8))) == 0 ) |
| @@ -224,7 +224,7 @@ long Bu::BitString::getBitLength() | |||
| 224 | return iBits; | 224 | return iBits; |
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | long Bu::BitString::getSize() | 227 | long Bu::BitString::getSize() const |
| 228 | { | 228 | { |
| 229 | return iBits; | 229 | return iBits; |
| 230 | } | 230 | } |
| @@ -311,6 +311,14 @@ void Bu::BitString::clear() | |||
| 311 | } | 311 | } |
| 312 | } | 312 | } |
| 313 | 313 | ||
| 314 | void Bu::BitString::fill() | ||
| 315 | { | ||
| 316 | if( caData != NULL ) | ||
| 317 | { | ||
| 318 | memset( caData, 0xff, iBytes ); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 314 | bool Bu::BitString::setBitLength( long iLength, bool bClear ) | 322 | bool Bu::BitString::setBitLength( long iLength, bool bClear ) |
| 315 | { | 323 | { |
| 316 | return setSize( iLength, bClear ); | 324 | return setSize( iLength, bClear ); |
diff --git a/src/unstable/bitstring.h b/src/unstable/bitstring.h index afc22fb..70ba822 100644 --- a/src/unstable/bitstring.h +++ b/src/unstable/bitstring.h | |||
| @@ -88,7 +88,7 @@ namespace Bu | |||
| 88 | *@param iBit The index of the bit to test. | 88 | *@param iBit The index of the bit to test. |
| 89 | *@returns True for a 1, false for a 0. | 89 | *@returns True for a 1, false for a 0. |
| 90 | */ | 90 | */ |
| 91 | bool getBit( long iBit ); | 91 | bool getBit( long iBit ) const; |
| 92 | 92 | ||
| 93 | /** | 93 | /** |
| 94 | * Inverts the entire BitString, in effect this calls flipBit on every | 94 | * Inverts the entire BitString, in effect this calls flipBit on every |
| @@ -106,7 +106,7 @@ namespace Bu | |||
| 106 | DEPRECATED | 106 | DEPRECATED |
| 107 | long getBitLength(); | 107 | long getBitLength(); |
| 108 | 108 | ||
| 109 | long getSize(); | 109 | long getSize() const; |
| 110 | 110 | ||
| 111 | /** | 111 | /** |
| 112 | * Sets the entire BitString to zeros, but it does it very quickly. | 112 | * Sets the entire BitString to zeros, but it does it very quickly. |
| @@ -115,6 +115,12 @@ namespace Bu | |||
| 115 | void clear(); | 115 | void clear(); |
| 116 | 116 | ||
| 117 | /** | 117 | /** |
| 118 | * Sets the entire BitString to ones, but it does it very quickly. | ||
| 119 | * This operation runs in O(N). | ||
| 120 | */ | ||
| 121 | void fill(); | ||
| 122 | |||
| 123 | /** | ||
| 118 | * Gets another BitString that is autonomous of the current one | 124 | * Gets another BitString that is autonomous of the current one |
| 119 | * (contains a copy of the memory, not a pointer) and contains a subset | 125 | * (contains a copy of the memory, not a pointer) and contains a subset |
| 120 | * of the data in the current BitString. This is an inclusive | 126 | * of the data in the current BitString. This is an inclusive |
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..f71f9b5 --- /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 | |||
| 17 | namespace 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::Myriad::StreamId id = mStore.allocate(); | ||
| 90 | hIndex.insert( o->getKey(), id ); | ||
| 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..f748a53 --- /dev/null +++ b/src/unstable/myriadfs.cpp | |||
| @@ -0,0 +1,739 @@ | |||
| 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 | #include "bu/mutexlocker.h" | ||
| 12 | |||
| 13 | #include <string.h> | ||
| 14 | #include <unistd.h> | ||
| 15 | #include <time.h> | ||
| 16 | |||
| 17 | #include "bu/sio.h" | ||
| 18 | using Bu::sio; | ||
| 19 | using Bu::Fmt; | ||
| 20 | |||
| 21 | namespace Bu { subExceptionDef( MyriadFsException ) } | ||
| 22 | |||
| 23 | #define Myriad_Fs_MAGIC_CODE ((char *)"\xa7\x18\x8b\x39") | ||
| 24 | |||
| 25 | Bu::MyriadFs::MyriadFs( Bu::Stream &rStore, int iBlockSize ) : | ||
| 26 | rStore( rStore ), | ||
| 27 | mStore( rStore, iBlockSize ), | ||
| 28 | iUser( 0 ), | ||
| 29 | iGroup( 0 ) | ||
| 30 | { | ||
| 31 | #ifndef WIN32 | ||
| 32 | iUser = getuid(); | ||
| 33 | iGroup = getgid(); | ||
| 34 | #endif | ||
| 35 | |||
| 36 | if( mStore.exists( 1 ) ) | ||
| 37 | { | ||
| 38 | // Check to see if this is a MyriadFs stream. | ||
| 39 | Bu::MyriadStream ms = mStore.open( 1, Bu::Myriad::Read ); | ||
| 40 | char sMagic[4]; | ||
| 41 | if( ms.read( sMagic, 4 ) < 4 ) | ||
| 42 | throw MyriadFsException("The provided stream does not appear to be " | ||
| 43 | "a MyriadFs stream."); | ||
| 44 | if( ::strncmp( sMagic, Myriad_Fs_MAGIC_CODE, 4 ) ) | ||
| 45 | throw MyriadFsException("The provided stream does not appear to be " | ||
| 46 | "a MyriadFs stream."); | ||
| 47 | |||
| 48 | int8_t iVer; | ||
| 49 | ms.read( &iVer, 1 ); | ||
| 50 | |||
| 51 | int32_t iNumNodes; | ||
| 52 | ms.read( &iNumNodes, 4 ); | ||
| 53 | for( int32_t j = 0; j < iNumNodes; j++ ) | ||
| 54 | { | ||
| 55 | int32_t iNode; | ||
| 56 | int32_t iPos; | ||
| 57 | ms.read( &iNode, 4 ); | ||
| 58 | ms.read( &iPos, 4 ); | ||
| 59 | hNodeIndex.insert( iNode, iPos ); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | // Create initial header stream | ||
| 65 | { | ||
| 66 | Bu::MyriadStream ms = mStore.open( | ||
| 67 | 1, Bu::Myriad::WriteNew|Bu::Myriad::Exclusive ); | ||
| 68 | ms.write( Myriad_Fs_MAGIC_CODE, 4 ); | ||
| 69 | int8_t iVer = 1; | ||
| 70 | int32_t iTmp = 1; | ||
| 71 | ms.write( &iVer, 1 ); | ||
| 72 | ms.write( &iTmp, 4 ); // iNumNodes | ||
| 73 | iTmp = 0; | ||
| 74 | ms.write( &iTmp, 4 ); // iInode | ||
| 75 | ms.write( &iTmp, 4 ); // iPosition | ||
| 76 | hNodeIndex.insert( 0, 0 ); | ||
| 77 | } | ||
| 78 | |||
| 79 | // Create initial inode stream, with one root node. | ||
| 80 | { | ||
| 81 | Bu::MyriadStream ms = mStore.open( | ||
| 82 | 2, Bu::Myriad::WriteNew|Bu::Myriad::Exclusive ); | ||
| 83 | RawStat rs; | ||
| 84 | rs.iNode = 0; | ||
| 85 | rs.iUser = iUser; | ||
| 86 | rs.iGroup = iGroup; | ||
| 87 | rs.uPerms = 0755|typeDir; | ||
| 88 | rs.iLinks = 1; | ||
| 89 | rs.uStreamIndex = 3; | ||
| 90 | rs.iCTime = rs.iMTime = rs.iATime = time(NULL); | ||
| 91 | ms.write( &rs, sizeof(RawStat) ); | ||
| 92 | } | ||
| 93 | |||
| 94 | // Create inode 0's storage stream. | ||
| 95 | { | ||
| 96 | Bu::MyriadStream ms = mStore.open( | ||
| 97 | 3, Bu::Myriad::WriteNew|Bu::Myriad::Exclusive ); | ||
| 98 | int32_t iTmp32 = 0; | ||
| 99 | ms.write( &iTmp32, 4 ); // iChildCount | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | Bu::MyriadFs::~MyriadFs() | ||
| 105 | { | ||
| 106 | writeHeader(); | ||
| 107 | } | ||
| 108 | |||
| 109 | void Bu::MyriadFs::stat( const Bu::String &sPath, Bu::MyriadFs::Stat &rBuf ) | ||
| 110 | { | ||
| 111 | Bu::MutexLocker lLock( mAccess ); | ||
| 112 | int32_t iParent; | ||
| 113 | int32_t iNode = lookupInode( sPath, iParent ); | ||
| 114 | Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::Read ); | ||
| 115 | stat( iNode, rBuf, is ); | ||
| 116 | } | ||
| 117 | |||
| 118 | Bu::MyriadStream Bu::MyriadFs::open( const Bu::String &sPath, int iMode, | ||
| 119 | uint16_t uPerms ) | ||
| 120 | { | ||
| 121 | Bu::MutexLocker lLock( mAccess ); | ||
| 122 | int32_t iParent = -1; | ||
| 123 | int32_t iNode; | ||
| 124 | try | ||
| 125 | { | ||
| 126 | iNode = lookupInode( sPath, iParent ); | ||
| 127 | // sio << "File found." << sio.nl; | ||
| 128 | // The file was found | ||
| 129 | Bu::MyriadStream ms = openByInode( iNode ); | ||
| 130 | if( (iMode&Truncate) ) | ||
| 131 | { | ||
| 132 | ms.setSize( 0 ); | ||
| 133 | } | ||
| 134 | return ms; | ||
| 135 | } | ||
| 136 | catch( Bu::MyriadFsException &e ) | ||
| 137 | { | ||
| 138 | if( iParent < 0 ) | ||
| 139 | throw; | ||
| 140 | |||
| 141 | // This means that an intermediate path component couldn't be found | ||
| 142 | if( e.getErrorCode() == 1 ) | ||
| 143 | throw; | ||
| 144 | |||
| 145 | // The file wasn't found, but the path leading up to it was. | ||
| 146 | // first, figure out the final path element... | ||
| 147 | Bu::String sName = filePart( sPath ); | ||
| 148 | // sio << "End filename: " << sName << sio.nl; | ||
| 149 | // sio << "Parent inode: " << iParent << sio.nl; | ||
| 150 | iNode = create( iParent, sName, (uPerms&permMask)|typeRegFile, 0 ); | ||
| 151 | // sio << "New iNode: " << iNode << sio.nl; | ||
| 152 | return openByInode( iNode ); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms ) | ||
| 157 | { | ||
| 158 | create( sPath, iPerms, 0 ); | ||
| 159 | } | ||
| 160 | |||
| 161 | void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms, | ||
| 162 | uint16_t iDevHi, uint16_t iDevLo ) | ||
| 163 | { | ||
| 164 | create( sPath, iPerms, ((uint32_t)iDevHi<<16)|(uint32_t)iDevLo ); | ||
| 165 | } | ||
| 166 | |||
| 167 | void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms, | ||
| 168 | uint32_t uSpecial ) | ||
| 169 | { | ||
| 170 | Bu::MutexLocker lLock( mAccess ); | ||
| 171 | int32_t iParent = -1; | ||
| 172 | // int32_t iNode; | ||
| 173 | try | ||
| 174 | { | ||
| 175 | /*iNode =*/ lookupInode( sPath, iParent ); | ||
| 176 | // sio << "File found." << sio.nl; | ||
| 177 | } | ||
| 178 | catch( Bu::MyriadFsException &e ) | ||
| 179 | { | ||
| 180 | if( iParent < 0 ) | ||
| 181 | throw; | ||
| 182 | |||
| 183 | // This means that an intermediate path component couldn't be found | ||
| 184 | if( e.getErrorCode() == 1 ) | ||
| 185 | throw; | ||
| 186 | |||
| 187 | // The file wasn't found, but the path leading up to it was. | ||
| 188 | // first, figure out the final path element... | ||
| 189 | Bu::String sName = filePart( sPath ); | ||
| 190 | // sio << "End filename: " << sName << sio.nl; | ||
| 191 | // sio << "Parent inode: " << iParent << sio.nl; | ||
| 192 | /*iNode =*/ create( iParent, sName, iPerms, uSpecial ); | ||
| 193 | // sio << "New iNode: " << iNode << sio.nl; | ||
| 194 | } | ||
| 195 | // The file was found | ||
| 196 | //throw Bu::MyriadFsException("Path already exists."); | ||
| 197 | } | ||
| 198 | |||
| 199 | void Bu::MyriadFs::mkDir( const Bu::String &sPath, uint16_t iPerms ) | ||
| 200 | { | ||
| 201 | create( sPath, (iPerms&permMask)|typeDir, 0 ); | ||
| 202 | } | ||
| 203 | |||
| 204 | void Bu::MyriadFs::mkSymLink( const Bu::String &sTarget, | ||
| 205 | const Bu::String &sPath ) | ||
| 206 | { | ||
| 207 | Bu::MutexLocker lLock( mAccess ); | ||
| 208 | int32_t iParent = -1; | ||
| 209 | int32_t iNode; | ||
| 210 | try | ||
| 211 | { | ||
| 212 | iNode = lookupInode( sPath, iParent ); | ||
| 213 | } | ||
| 214 | catch( Bu::MyriadFsException &e ) | ||
| 215 | { | ||
| 216 | if( iParent < 0 ) | ||
| 217 | throw; | ||
| 218 | |||
| 219 | // This means that an intermediate path component couldn't be found | ||
| 220 | if( e.getErrorCode() == 1 ) | ||
| 221 | throw; | ||
| 222 | |||
| 223 | // The file wasn't found, but the path leading up to it was. | ||
| 224 | // first, figure out the final path element... | ||
| 225 | Bu::String sName = filePart( sPath ); | ||
| 226 | // sio << "End filename: " << sName << sio.nl; | ||
| 227 | // sio << "Parent inode: " << iParent << sio.nl; | ||
| 228 | iNode = create( iParent, sName, 0777|typeSymLink, 0 ); | ||
| 229 | // sio << "New iNode: " << iNode << sio.nl; | ||
| 230 | MyriadStream ms = openByInode( iNode ); | ||
| 231 | ms.write( sTarget ); | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | throw Bu::MyriadFsException("Path already exists."); | ||
| 235 | } | ||
| 236 | |||
| 237 | void Bu::MyriadFs::mkHardLink( const Bu::String &sTarget, | ||
| 238 | const Bu::String &sPath ) | ||
| 239 | { | ||
| 240 | Bu::MutexLocker lLock( mAccess ); | ||
| 241 | int32_t iParent = -1; | ||
| 242 | int32_t iNode; | ||
| 243 | |||
| 244 | iNode = lookupInode( sTarget, iParent ); | ||
| 245 | |||
| 246 | try | ||
| 247 | { | ||
| 248 | lookupInode( sPath, iParent ); | ||
| 249 | throw Bu::MyriadFsException("Path already exists."); | ||
| 250 | } | ||
| 251 | catch( Bu::MyriadFsException &e ) | ||
| 252 | { | ||
| 253 | if( iParent < 0 ) | ||
| 254 | throw; | ||
| 255 | |||
| 256 | // This means that an intermediate path component couldn't be found | ||
| 257 | if( e.getErrorCode() == 1 ) | ||
| 258 | throw; | ||
| 259 | |||
| 260 | // The file wasn't found, but the path leading up to it was. | ||
| 261 | // first, figure out the final path element... | ||
| 262 | Bu::String sName = filePart( sPath ); | ||
| 263 | // sio << "End filename: " << sName << sio.nl; | ||
| 264 | // sio << "Parent inode: " << iParent << sio.nl; | ||
| 265 | addToDir( iParent, iNode, sName ); | ||
| 266 | MyriadStream is = mStore.open( 2, Bu::Myriad::ReadWrite ); | ||
| 267 | RawStat rs; | ||
| 268 | readInode( iNode, rs, is ); | ||
| 269 | rs.iLinks++; | ||
| 270 | writeInode( rs, is ); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | Bu::String Bu::MyriadFs::readSymLink( const Bu::String &sPath ) | ||
| 275 | { | ||
| 276 | Bu::MutexLocker lLock( mAccess ); | ||
| 277 | int32_t iParent = -1; | ||
| 278 | int32_t iNode; | ||
| 279 | iNode = lookupInode( sPath, iParent ); | ||
| 280 | MyriadStream ms = openByInode( iNode ); | ||
| 281 | Bu::String sRet; | ||
| 282 | sRet.setSize( ms.getSize() ); | ||
| 283 | ms.read( sRet.getStr(), ms.getSize() ); | ||
| 284 | return sRet; | ||
| 285 | } | ||
| 286 | |||
| 287 | Bu::MyriadFs::Dir Bu::MyriadFs::readDir( const Bu::String &sPath ) | ||
| 288 | { | ||
| 289 | Bu::MutexLocker lLock( mAccess ); | ||
| 290 | int32_t iParent = -1; | ||
| 291 | int32_t iNode = lookupInode( sPath, iParent ); | ||
| 292 | return readDir( iNode ); | ||
| 293 | } | ||
| 294 | |||
| 295 | void Bu::MyriadFs::setTimes( const Bu::String &sPath, int64_t iATime, | ||
| 296 | int64_t iMTime ) | ||
| 297 | { | ||
| 298 | Bu::MutexLocker lLock( mAccess ); | ||
| 299 | int32_t iParent = -1; | ||
| 300 | int32_t iNode; | ||
| 301 | |||
| 302 | iNode = lookupInode( sPath, iParent ); | ||
| 303 | |||
| 304 | setTimes( iNode, iATime, iMTime ); | ||
| 305 | } | ||
| 306 | |||
| 307 | void Bu::MyriadFs::unlink( const Bu::String &sPath ) | ||
| 308 | { | ||
| 309 | Bu::MutexLocker lLock( mAccess ); | ||
| 310 | int32_t iParent = -1; | ||
| 311 | // int32_t iNode; | ||
| 312 | |||
| 313 | /*iNode =*/ lookupInode( sPath, iParent ); | ||
| 314 | |||
| 315 | Dir lDir = readDir( iParent ); | ||
| 316 | |||
| 317 | Bu::String sName = filePart( sPath ); | ||
| 318 | |||
| 319 | for( Dir::iterator i = lDir.begin(); i; i++ ) | ||
| 320 | { | ||
| 321 | if( sName == (*i).sName ) | ||
| 322 | { | ||
| 323 | RawStat rs; | ||
| 324 | readInode( (*i).iNode, rs ); | ||
| 325 | if( (rs.uPerms&typeMask) == typeDir ) | ||
| 326 | { | ||
| 327 | MyriadStream msDir = mStore.open( | ||
| 328 | rs.uStreamIndex, Bu::Myriad::Read | ||
| 329 | ); | ||
| 330 | int32_t iCount; | ||
| 331 | msDir.read( &iCount, 4 ); | ||
| 332 | if( iCount > 0 ) | ||
| 333 | { | ||
| 334 | throw Bu::MyriadFsException("Directory not empty."); | ||
| 335 | } | ||
| 336 | } | ||
| 337 | if( --rs.iLinks == 0 ) | ||
| 338 | { | ||
| 339 | destroyNode( (*i).iNode ); | ||
| 340 | } | ||
| 341 | else | ||
| 342 | { | ||
| 343 | writeInode( rs ); | ||
| 344 | } | ||
| 345 | lDir.erase( i ); | ||
| 346 | break; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | Bu::MyriadStream ms = openByInode( iParent ); | ||
| 351 | int32_t iNumChildren = lDir.getSize(); | ||
| 352 | ms.write( &iNumChildren, 4 ); | ||
| 353 | for( Dir::iterator i = lDir.begin(); i; i++ ) | ||
| 354 | { | ||
| 355 | ms.write( &(*i).iNode, 4 ); | ||
| 356 | uint8_t iSize = (*i).sName.getSize(); | ||
| 357 | ms.write( &iSize, 1 ); | ||
| 358 | ms.write( (*i).sName.getStr(), iSize ); | ||
| 359 | } | ||
| 360 | ms.setSize( ms.tell() ); | ||
| 361 | } | ||
| 362 | |||
| 363 | void Bu::MyriadFs::setFileSize( const Bu::String &sPath, int32_t iSize ) | ||
| 364 | { | ||
| 365 | Bu::MutexLocker lLock( mAccess ); | ||
| 366 | int32_t iParent = -1; | ||
| 367 | int32_t iNode; | ||
| 368 | iNode = lookupInode( sPath, iParent ); | ||
| 369 | MyriadStream ms = openByInode( iNode ); | ||
| 370 | ms.setSize( iSize ); | ||
| 371 | } | ||
| 372 | |||
| 373 | void Bu::MyriadFs::rename( const Bu::String &sFrom, const Bu::String &sTo ) | ||
| 374 | { | ||
| 375 | Bu::MutexLocker lLock( mAccess ); | ||
| 376 | mkHardLink( sFrom, sTo ); | ||
| 377 | unlink( sFrom ); | ||
| 378 | } | ||
| 379 | |||
| 380 | dev_t Bu::MyriadFs::devToSys( uint32_t uDev ) | ||
| 381 | { | ||
| 382 | return (((uDev&0xFFFF0000)>>8)&0xFF00) | ((uDev&0xFF)); | ||
| 383 | } | ||
| 384 | |||
| 385 | uint32_t Bu::MyriadFs::sysToDev( dev_t uDev ) | ||
| 386 | { | ||
| 387 | return (((uint32_t)uDev&0xFF00)<<8) | ((uint32_t)uDev&0xFF); | ||
| 388 | } | ||
| 389 | |||
| 390 | int32_t Bu::MyriadFs::lookupInode( const Bu::String &sPath, int32_t &iParent ) | ||
| 391 | { | ||
| 392 | if( sPath == "/" ) | ||
| 393 | { | ||
| 394 | return 0; | ||
| 395 | } | ||
| 396 | if( sPath[0] == '/' ) | ||
| 397 | { | ||
| 398 | // Absolute lookup | ||
| 399 | return lookupInode( sPath.begin()+1, 0, iParent ); | ||
| 400 | } | ||
| 401 | else | ||
| 402 | { | ||
| 403 | // Relative lookup | ||
| 404 | throw Bu::ExceptionBase( | ||
| 405 | "Relative lookups in MyriadFs are not working yet."); | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | int32_t Bu::MyriadFs::lookupInode( Bu::String::const_iterator iStart, | ||
| 410 | int32_t iNode, int32_t &iParent ) | ||
| 411 | { | ||
| 412 | iParent = iNode; | ||
| 413 | |||
| 414 | Bu::String::const_iterator iEnd = iStart.find('/'); | ||
| 415 | Bu::String sTok( iStart, iEnd ); | ||
| 416 | |||
| 417 | // sio << "Direcotry component: " << sTok << sio.nl; | ||
| 418 | |||
| 419 | Dir lDir = readDir( iNode ); | ||
| 420 | |||
| 421 | for( Dir::iterator i = lDir.begin(); i; i++ ) | ||
| 422 | { | ||
| 423 | if( (*i).sName == sTok ) | ||
| 424 | { | ||
| 425 | // We found an item | ||
| 426 | if( !iEnd ) | ||
| 427 | { | ||
| 428 | // It's the last one in the requested path, return it | ||
| 429 | return (*i).iNode; | ||
| 430 | } | ||
| 431 | else | ||
| 432 | { | ||
| 433 | // Not the last one in our path, double check it's a dir | ||
| 434 | if( ((*i).uPerms&typeMask) == typeDir ) | ||
| 435 | { | ||
| 436 | return lookupInode( iEnd+1, (*i).iNode, iParent ); | ||
| 437 | } | ||
| 438 | else | ||
| 439 | { | ||
| 440 | iParent = -1; | ||
| 441 | throw Bu::MyriadFsException( | ||
| 442 | "Element '%s' in given path is not a directory.", | ||
| 443 | sTok.getStr() ); | ||
| 444 | } | ||
| 445 | } | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | if( iEnd ) | ||
| 450 | throw Bu::MyriadFsException( 1, "Path not found"); | ||
| 451 | else | ||
| 452 | throw Bu::MyriadFsException( 2, "Path not found"); | ||
| 453 | } | ||
| 454 | |||
| 455 | void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs ) | ||
| 456 | { | ||
| 457 | rIs.setPos( hNodeIndex.get( iNode )*sizeof(RawStat) ); | ||
| 458 | if( rIs.read( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) ) | ||
| 459 | throw Bu::MyriadFsException("Filesystem corruption detected."); | ||
| 460 | if( rs.iNode != iNode ) | ||
| 461 | throw Bu::MyriadFsException("Filesystem corruption detected."); | ||
| 462 | } | ||
| 463 | |||
| 464 | void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs ) | ||
| 465 | { | ||
| 466 | MyriadStream ms = mStore.open( 2, Bu::Myriad::Read ); | ||
| 467 | readInode( iNode, rs, ms ); | ||
| 468 | } | ||
| 469 | |||
| 470 | void Bu::MyriadFs::writeInode( const RawStat &rs, | ||
| 471 | MyriadStream &rOs ) | ||
| 472 | { | ||
| 473 | rOs.setSize( hNodeIndex.getSize()*sizeof(RawStat) ); | ||
| 474 | rOs.setPos( hNodeIndex.get( rs.iNode )*sizeof(RawStat) ); | ||
| 475 | if( rOs.write( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) ) | ||
| 476 | throw Bu::MyriadFsException("Error writing inode to header stream."); | ||
| 477 | } | ||
| 478 | |||
| 479 | void Bu::MyriadFs::writeInode( const RawStat &rs ) | ||
| 480 | { | ||
| 481 | MyriadStream ms = mStore.open( 2, Bu::Myriad::Write ); | ||
| 482 | writeInode( rs, ms ); | ||
| 483 | } | ||
| 484 | |||
| 485 | Bu::MyriadFs::Dir Bu::MyriadFs::readDir( int32_t iNode ) | ||
| 486 | { | ||
| 487 | Bu::MyriadStream ms = openByInode( iNode ); | ||
| 488 | int32_t iNumChildren = 0; | ||
| 489 | ms.read( &iNumChildren, 4 ); | ||
| 490 | |||
| 491 | Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::Read ); | ||
| 492 | Dir lDir; | ||
| 493 | // sio << "Reading dir " << iNode << ", " << iNumChildren << " entries:" << sio.nl; | ||
| 494 | for( int32_t j = 0; j < iNumChildren; j++ ) | ||
| 495 | { | ||
| 496 | int32_t iChildNode = 0; | ||
| 497 | if( ms.read( &iChildNode, 4 ) < 4 ) | ||
| 498 | { | ||
| 499 | throw Bu::MyriadFsException( | ||
| 500 | "Failed to read iChildNode from directory."); | ||
| 501 | } | ||
| 502 | Stat s; | ||
| 503 | stat( iChildNode, s, is ); | ||
| 504 | uint8_t uLen; | ||
| 505 | if( ms.read( &uLen, 1 ) < 1 ) | ||
| 506 | { | ||
| 507 | throw Bu::MyriadFsException( | ||
| 508 | "Failed to read uLen from directory."); | ||
| 509 | } | ||
| 510 | s.sName.setSize( uLen ); | ||
| 511 | if( ms.read( s.sName.getStr(), uLen ) < uLen ) | ||
| 512 | { | ||
| 513 | throw Bu::MyriadFsException( | ||
| 514 | "Failed to read sName from directory."); | ||
| 515 | } | ||
| 516 | lDir.append( s ); | ||
| 517 | |||
| 518 | // sio << " " << s.sName << sio.nl; | ||
| 519 | } | ||
| 520 | |||
| 521 | return lDir; | ||
| 522 | } | ||
| 523 | |||
| 524 | Bu::MyriadStream Bu::MyriadFs::openByInode( int32_t iNode ) | ||
| 525 | { | ||
| 526 | RawStat rs; | ||
| 527 | readInode( iNode, rs ); | ||
| 528 | switch( (rs.uPerms&typeMask) ) | ||
| 529 | { | ||
| 530 | case typeDir: | ||
| 531 | case typeSymLink: | ||
| 532 | case typeRegFile: | ||
| 533 | return mStore.open( rs.uStreamIndex, Bu::Myriad::ReadWrite ); | ||
| 534 | |||
| 535 | default: | ||
| 536 | throw Bu::MyriadFsException( | ||
| 537 | "inode incorrect type for low-level openByInode."); | ||
| 538 | } | ||
| 539 | } | ||
| 540 | |||
| 541 | void Bu::MyriadFs::addToDir( int32_t iDir, int32_t iNode, | ||
| 542 | const Bu::String &sName ) | ||
| 543 | { | ||
| 544 | if( sName.getSize() > 255 ) | ||
| 545 | { | ||
| 546 | throw Bu::MyriadFsException("Filename too long, max is 255 bytes."); | ||
| 547 | } | ||
| 548 | Bu::MyriadStream ms = openByInode( iDir ); | ||
| 549 | int32_t iNumChildren = 0; | ||
| 550 | ms.read( &iNumChildren, 4 ); | ||
| 551 | iNumChildren++; | ||
| 552 | ms.setPos( 0 ); | ||
| 553 | ms.write( &iNumChildren, 4 ); | ||
| 554 | ms.setPosEnd( 0 ); | ||
| 555 | ms.write( &iNode, 4 ); | ||
| 556 | uint8_t uLen = sName.getSize(); | ||
| 557 | ms.write( &uLen, 1 ); | ||
| 558 | ms.write( sName.getStr(), uLen ); | ||
| 559 | } | ||
| 560 | |||
| 561 | int32_t Bu::MyriadFs::create( int32_t iParent, const Bu::String &sName, | ||
| 562 | uint16_t uPerms, uint32_t uSpecial ) | ||
| 563 | { | ||
| 564 | int32_t iNode = allocInode( uPerms, uSpecial ); | ||
| 565 | addToDir( iParent, iNode, sName ); | ||
| 566 | return iNode; | ||
| 567 | } | ||
| 568 | |||
| 569 | int32_t Bu::MyriadFs::allocInode( uint16_t uPerms, uint32_t uSpecial ) | ||
| 570 | { | ||
| 571 | int32_t iNode = 0; | ||
| 572 | for(; iNode < 0xfffffff; iNode++ ) | ||
| 573 | { | ||
| 574 | if( !hNodeIndex.has( iNode ) ) | ||
| 575 | { | ||
| 576 | hNodeIndex.insert( iNode, hNodeIndex.getSize() ); | ||
| 577 | RawStat rs; | ||
| 578 | rs.iNode = iNode; | ||
| 579 | rs.iUser = iUser; | ||
| 580 | rs.iGroup = iGroup; | ||
| 581 | rs.uPerms = uPerms; | ||
| 582 | rs.iLinks = 1; | ||
| 583 | switch( (uPerms&typeMask) ) | ||
| 584 | { | ||
| 585 | case typeRegFile: | ||
| 586 | case typeSymLink: | ||
| 587 | rs.uStreamIndex = mStore.allocate(); | ||
| 588 | break; | ||
| 589 | |||
| 590 | case typeDir: | ||
| 591 | // sio << "Creating directory node, storage: " | ||
| 592 | // << rs.uStreamIndex << sio.nl; | ||
| 593 | { | ||
| 594 | Bu::MyriadStream msDir = mStore.create( | ||
| 595 | Bu::Myriad::Write | ||
| 596 | ); | ||
| 597 | rs.uStreamIndex = msDir.getId(); | ||
| 598 | uint32_t uSize = 0; | ||
| 599 | msDir.write( &uSize, 4 ); | ||
| 600 | } | ||
| 601 | break; | ||
| 602 | |||
| 603 | case typeChrDev: | ||
| 604 | case typeBlkDev: | ||
| 605 | rs.uStreamIndex = uSpecial; | ||
| 606 | break; | ||
| 607 | |||
| 608 | default: | ||
| 609 | rs.uStreamIndex = 0; | ||
| 610 | break; | ||
| 611 | } | ||
| 612 | rs.iATime = time(NULL); | ||
| 613 | rs.iMTime = time(NULL); | ||
| 614 | rs.iCTime = time(NULL); | ||
| 615 | writeInode( rs ); | ||
| 616 | |||
| 617 | return iNode; | ||
| 618 | } | ||
| 619 | } | ||
| 620 | |||
| 621 | throw Bu::MyriadFsException( | ||
| 622 | "No inode could be allocated. You've run out!"); | ||
| 623 | } | ||
| 624 | |||
| 625 | void Bu::MyriadFs::stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs ) | ||
| 626 | { | ||
| 627 | RawStat rs; | ||
| 628 | readInode( iNode, rs, rIs ); | ||
| 629 | rBuf.iNode = iNode; | ||
| 630 | rBuf.iUser = rs.iUser; | ||
| 631 | rBuf.iGroup = rs.iGroup; | ||
| 632 | rBuf.uPerms = rs.uPerms; | ||
| 633 | rBuf.iLinks = rs.iLinks; | ||
| 634 | rBuf.iATime = rs.iATime; | ||
| 635 | rBuf.iMTime = rs.iMTime; | ||
| 636 | rBuf.iCTime = rs.iCTime; | ||
| 637 | rBuf.uDev = 0; | ||
| 638 | rBuf.iSize = 0; | ||
| 639 | switch( (rBuf.uPerms&typeMask) ) | ||
| 640 | { | ||
| 641 | case typeRegFile: | ||
| 642 | case typeSymLink: | ||
| 643 | rBuf.iSize = mStore.getSize( rs.uStreamIndex ); | ||
| 644 | break; | ||
| 645 | |||
| 646 | case typeChrDev: | ||
| 647 | case typeBlkDev: | ||
| 648 | rBuf.uDev = rs.uStreamIndex; | ||
| 649 | break; | ||
| 650 | |||
| 651 | default: | ||
| 652 | rBuf.iSize = 0; | ||
| 653 | break; | ||
| 654 | } | ||
| 655 | } | ||
| 656 | |||
| 657 | void Bu::MyriadFs::writeHeader() | ||
| 658 | { | ||
| 659 | Bu::MyriadStream ms = mStore.open( 1, Bu::Myriad::Write ); | ||
| 660 | ms.write( Myriad_Fs_MAGIC_CODE, 4 ); | ||
| 661 | int8_t iVer = 1; | ||
| 662 | int32_t iNumNodes = hNodeIndex.getSize(); | ||
| 663 | ms.write( &iVer, 1 ); | ||
| 664 | ms.write( &iNumNodes, 4 ); // iNumNodes | ||
| 665 | for( NodeIndex::iterator i = hNodeIndex.begin(); i; i++ ) | ||
| 666 | { | ||
| 667 | int32_t iNode = i.getKey(); | ||
| 668 | int32_t iPosition = i.getValue(); | ||
| 669 | ms.write( &iNode, 4 ); | ||
| 670 | ms.write( &iPosition, 4 ); | ||
| 671 | } | ||
| 672 | |||
| 673 | // Truncate the stream afterwards so we don't use up too much space. | ||
| 674 | ms.setSize( ms.tell() ); | ||
| 675 | } | ||
| 676 | |||
| 677 | void Bu::MyriadFs::setTimes( int32_t iNode, int64_t iATime, int64_t iMTime ) | ||
| 678 | { | ||
| 679 | RawStat rs; | ||
| 680 | Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::ReadWrite ); | ||
| 681 | |||
| 682 | readInode( iNode, rs, is ); | ||
| 683 | rs.iATime = iATime; | ||
| 684 | rs.iMTime = iMTime; | ||
| 685 | writeInode( rs, is ); | ||
| 686 | } | ||
| 687 | |||
| 688 | void Bu::MyriadFs::destroyNode( int32_t iNode ) | ||
| 689 | { | ||
| 690 | if( iNode == 0 ) | ||
| 691 | throw Bu::MyriadFsException("You cannot destroy the root."); | ||
| 692 | |||
| 693 | uint32_t iPosition; | ||
| 694 | RawStat rsOld; | ||
| 695 | |||
| 696 | Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::ReadWrite ); | ||
| 697 | |||
| 698 | // This will be overwritten with the last node | ||
| 699 | iPosition = hNodeIndex.get( iNode ); | ||
| 700 | readInode( iNode, rsOld, is ); | ||
| 701 | |||
| 702 | switch( (rsOld.uPerms&typeMask) ) | ||
| 703 | { | ||
| 704 | case typeRegFile: | ||
| 705 | case typeDir: | ||
| 706 | case typeSymLink: | ||
| 707 | mStore.erase( rsOld.uStreamIndex ); | ||
| 708 | break; | ||
| 709 | } | ||
| 710 | |||
| 711 | hNodeIndex.erase( iNode ); | ||
| 712 | |||
| 713 | // Read the last node, can't use the helpers, because we don't know the | ||
| 714 | // iNode yet. | ||
| 715 | if( iPosition != hNodeIndex.getSize() ) | ||
| 716 | { | ||
| 717 | // If this is the last node, then we don't need to do anything, but | ||
| 718 | // this case handles what to do if we aren't on the last node | ||
| 719 | RawStat rs; | ||
| 720 | is.setPos( (hNodeIndex.getSize())*sizeof(RawStat) ); | ||
| 721 | is.read( &rs, sizeof(RawStat) ); | ||
| 722 | |||
| 723 | hNodeIndex.get( rs.iNode ) = iPosition; | ||
| 724 | writeInode( rs, is ); | ||
| 725 | } | ||
| 726 | |||
| 727 | is.setSize( hNodeIndex.getSize() * sizeof(RawStat) ); | ||
| 728 | } | ||
| 729 | |||
| 730 | Bu::String Bu::MyriadFs::filePart( const Bu::String &sPath ) | ||
| 731 | { | ||
| 732 | Bu::String::const_iterator iStart = sPath.begin(); | ||
| 733 | if( *iStart == '/' ) | ||
| 734 | iStart++; | ||
| 735 | for( Bu::String::const_iterator iEnd = iStart.find('/'); iEnd; | ||
| 736 | iStart = iEnd+1, iEnd = iStart.find('/') ) { } | ||
| 737 | return Bu::String( iStart, sPath.end() ); | ||
| 738 | } | ||
| 739 | |||
diff --git a/src/unstable/myriadfs.h b/src/unstable/myriadfs.h new file mode 100644 index 0000000..e3008bc --- /dev/null +++ b/src/unstable/myriadfs.h | |||
| @@ -0,0 +1,211 @@ | |||
| 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/debugmutex.h" | ||
| 15 | |||
| 16 | namespace 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 = 0x40, ///< 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 | /** | ||
| 176 | * Lookup inode. | ||
| 177 | */ | ||
| 178 | int32_t lookupInode( const Bu::String &sPath, int32_t &iParent ); | ||
| 179 | /** | ||
| 180 | * Lookup inode. | ||
| 181 | */ | ||
| 182 | int32_t lookupInode( Bu::String::const_iterator iStart, | ||
| 183 | int32_t iNode, int32_t &iParent ); | ||
| 184 | void readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs ); | ||
| 185 | void readInode( int32_t iNode, RawStat &rs ); | ||
| 186 | void writeInode( const RawStat &rs ); | ||
| 187 | void writeInode( const RawStat &rs, MyriadStream &rOs ); | ||
| 188 | Dir readDir( int32_t iNode ); | ||
| 189 | MyriadStream openByInode( int32_t iNode ); | ||
| 190 | void addToDir( int32_t iDir, int32_t iNode, const Bu::String &sName ); | ||
| 191 | int32_t create( int32_t iParent, const Bu::String &sName, | ||
| 192 | uint16_t uPerms, uint32_t uSpecial ); | ||
| 193 | int32_t allocInode( uint16_t uPerms, uint32_t uSpecial ); | ||
| 194 | void stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs ); | ||
| 195 | void writeHeader(); | ||
| 196 | void setTimes( int32_t iNode, int64_t iATime, int64_t iMTime ); | ||
| 197 | void destroyNode( int32_t iNode ); | ||
| 198 | |||
| 199 | static Bu::String filePart( const Bu::String &sPath ); | ||
| 200 | |||
| 201 | private: | ||
| 202 | Bu::Stream &rStore; | ||
| 203 | Bu::Myriad mStore; | ||
| 204 | Bu::DebugMutex mAccess; | ||
| 205 | NodeIndex hNodeIndex; | ||
| 206 | int32_t iUser; | ||
| 207 | int32_t iGroup; | ||
| 208 | }; | ||
| 209 | }; | ||
| 210 | |||
| 211 | #endif | ||
