diff options
author | Mike Buland <mike@xagasoft.com> | 2024-08-27 13:37:36 -0700 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2024-08-27 13:37:36 -0700 |
commit | f1e3f25d9b7a12cdedb99e4cb0bfa66157a1a972 (patch) | |
tree | c8414b8040cdcd38bd98471d96a01908cdef49ad /src/unstable | |
parent | caee572ff94822ca2ed354fcb79ca04ed9adf388 (diff) | |
download | libbu++-f1e3f25d9b7a12cdedb99e4cb0bfa66157a1a972.tar.gz libbu++-f1e3f25d9b7a12cdedb99e4cb0bfa66157a1a972.tar.bz2 libbu++-f1e3f25d9b7a12cdedb99e4cb0bfa66157a1a972.tar.xz libbu++-f1e3f25d9b7a12cdedb99e4cb0bfa66157a1a972.zip |
Making progress.
Diffstat (limited to 'src/unstable')
-rw-r--r-- | src/unstable/myriadcache.cpp | 8 | ||||
-rw-r--r-- | src/unstable/myriadcache.h | 140 | ||||
-rw-r--r-- | src/unstable/myriadfs.cpp | 722 | ||||
-rw-r--r-- | src/unstable/myriadfs.h | 205 |
4 files changed, 0 insertions, 1075 deletions
diff --git a/src/unstable/myriadcache.cpp b/src/unstable/myriadcache.cpp deleted file mode 100644 index c9eb9c4..0000000 --- a/src/unstable/myriadcache.cpp +++ /dev/null | |||
@@ -1,8 +0,0 @@ | |||
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 deleted file mode 100644 index 24002b0..0000000 --- a/src/unstable/myriadcache.h +++ /dev/null | |||
@@ -1,140 +0,0 @@ | |||
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.openStream( 1 ); | ||
32 | Bu::Archive ar( ms, Bu::Archive::load ); | ||
33 | uint8_t uVer; | ||
34 | ar >> uVer; | ||
35 | switch( uVer ) | ||
36 | { | ||
37 | case 0: | ||
38 | ar >> hIndex; | ||
39 | break; | ||
40 | } | ||
41 | } | ||
42 | catch(...) | ||
43 | { | ||
44 | if( mStore.createStreamWithId( 1 ) != 1 ) | ||
45 | throw Bu::ExceptionBase("Error creating index stream."); | ||
46 | |||
47 | _sync(); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | virtual ~MyriadCache() | ||
52 | { | ||
53 | Bu::CacheBase<keytype,obtype>::sync(); | ||
54 | } | ||
55 | |||
56 | using typename Bu::CacheBase<keytype,obtype>::KeyList; | ||
57 | using typename Bu::CacheBase<keytype,obtype>::ObjectType; | ||
58 | |||
59 | virtual typename Bu::CacheBase<keytype,obtype>::KeyList getKeys() const | ||
60 | { | ||
61 | Bu::ReadWriteMutex::ReadLocker rl( rwStore ); | ||
62 | return hIndex.getKeys(); | ||
63 | } | ||
64 | |||
65 | virtual int getSize() const | ||
66 | { | ||
67 | Bu::ReadWriteMutex::ReadLocker rl( rwStore ); | ||
68 | return hIndex.getSize(); | ||
69 | } | ||
70 | |||
71 | protected: | ||
72 | virtual bool _has( const keytype &key ) | ||
73 | { | ||
74 | Bu::ReadWriteMutex::ReadLocker rl( rwStore ); | ||
75 | return hIndex.has( key ); | ||
76 | } | ||
77 | |||
78 | virtual void _create( const obtype *o ) | ||
79 | { | ||
80 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); | ||
81 | hIndex.insert( o->getKey(), mStore.createStream() ); | ||
82 | _save( o ); | ||
83 | |||
84 | bStructureChanged = true; | ||
85 | } | ||
86 | |||
87 | virtual void _erase( const keytype &k ) | ||
88 | { | ||
89 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); | ||
90 | mStore.deleteStream( hIndex.get( k ) ); | ||
91 | hIndex.erase( k ); | ||
92 | |||
93 | bStructureChanged = true; | ||
94 | } | ||
95 | |||
96 | virtual obtype *_load( | ||
97 | typename Bu::CacheObject<keytype, obtype>::Initializer &initObj, | ||
98 | const keytype &k | ||
99 | ) | ||
100 | { | ||
101 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) ); | ||
102 | return _cacheObjectLoad<keytype, obtype>( initObj, k, ms ); | ||
103 | } | ||
104 | |||
105 | virtual void _save( const obtype *o ) | ||
106 | { | ||
107 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) ); | ||
108 | _cacheObjectSave( ms, o ); | ||
109 | ms.setSize( ms.tell() ); | ||
110 | |||
111 | mStore.sync(); | ||
112 | } | ||
113 | |||
114 | virtual void _sync() | ||
115 | { | ||
116 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); | ||
117 | if( !bStructureChanged ) | ||
118 | return; | ||
119 | |||
120 | Bu::MyriadStream ms = mStore.openStream( 1 ); | ||
121 | Bu::Archive ar( ms, Bu::Archive::save ); | ||
122 | ar << (uint8_t)0 << hIndex; | ||
123 | ar.close(); | ||
124 | ms.setSize( ms.tell() ); | ||
125 | |||
126 | bStructureChanged = false; | ||
127 | |||
128 | mStore.sync(); | ||
129 | } | ||
130 | |||
131 | private: | ||
132 | Bu::Stream &sStore; | ||
133 | Bu::Myriad mStore; | ||
134 | Bu::Hash<keytype, int> hIndex; | ||
135 | mutable Bu::ReadWriteMutex rwStore; | ||
136 | bool bStructureChanged; | ||
137 | }; | ||
138 | } | ||
139 | |||
140 | #endif | ||
diff --git a/src/unstable/myriadfs.cpp b/src/unstable/myriadfs.cpp deleted file mode 100644 index 991c21d..0000000 --- a/src/unstable/myriadfs.cpp +++ /dev/null | |||
@@ -1,722 +0,0 @@ | |||
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" | ||
17 | using Bu::sio; | ||
18 | using Bu::Fmt; | ||
19 | |||
20 | namespace Bu { subExceptionDef( MyriadFsException ) } | ||
21 | |||
22 | #define Myriad_Fs_MAGIC_CODE ((char *)"\xa7\x18\x8b\x39") | ||
23 | |||
24 | Bu::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.hasStream( 1 ) ) | ||
36 | { | ||
37 | // Check to see if this is a MyriadFs stream. | ||
38 | Bu::MyriadStream ms = mStore.openStream( 1 ); | ||
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 | mStore.createStream( 1 ); | ||
66 | Bu::MyriadStream ms = mStore.openStream( 1 ); | ||
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 | mStore.createStream( 2 ); | ||
81 | Bu::MyriadStream ms = mStore.openStream( 2 ); | ||
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 | mStore.createStream( 3 ); | ||
96 | Bu::MyriadStream ms = mStore.openStream( 3 ); | ||
97 | int32_t iTmp32 = 0; | ||
98 | ms.write( &iTmp32, 4 ); // iChildCount | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | |||
103 | Bu::MyriadFs::~MyriadFs() | ||
104 | { | ||
105 | writeHeader(); | ||
106 | } | ||
107 | |||
108 | void 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.openStream( 2 ); | ||
113 | stat( iNode, rBuf, is ); | ||
114 | } | ||
115 | |||
116 | Bu::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 | |||
153 | void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms ) | ||
154 | { | ||
155 | create( sPath, iPerms, 0 ); | ||
156 | } | ||
157 | |||
158 | void 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 | |||
164 | void 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 | |||
195 | void Bu::MyriadFs::mkDir( const Bu::String &sPath, uint16_t iPerms ) | ||
196 | { | ||
197 | create( sPath, (iPerms&permMask)|typeDir, 0 ); | ||
198 | } | ||
199 | |||
200 | void 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 | |||
232 | void 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.openStream( 2 ); | ||
261 | RawStat rs; | ||
262 | readInode( iNode, rs, is ); | ||
263 | rs.iLinks++; | ||
264 | writeInode( rs, is ); | ||
265 | } | ||
266 | } | ||
267 | |||
268 | Bu::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 | |||
280 | Bu::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 | |||
287 | void 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 | |||
298 | void 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.openStream( rs.uStreamIndex ); | ||
318 | int32_t iCount; | ||
319 | msDir.read( &iCount, 4 ); | ||
320 | if( iCount > 0 ) | ||
321 | { | ||
322 | throw Bu::MyriadFsException("Directory not empty."); | ||
323 | } | ||
324 | } | ||
325 | if( --rs.iLinks == 0 ) | ||
326 | { | ||
327 | destroyNode( (*i).iNode ); | ||
328 | } | ||
329 | else | ||
330 | { | ||
331 | writeInode( rs ); | ||
332 | } | ||
333 | lDir.erase( i ); | ||
334 | break; | ||
335 | } | ||
336 | } | ||
337 | |||
338 | Bu::MyriadStream ms = openByInode( iParent ); | ||
339 | int32_t iNumChildren = lDir.getSize(); | ||
340 | ms.write( &iNumChildren, 4 ); | ||
341 | for( Dir::iterator i = lDir.begin(); i; i++ ) | ||
342 | { | ||
343 | ms.write( &(*i).iNode, 4 ); | ||
344 | uint8_t iSize = (*i).sName.getSize(); | ||
345 | ms.write( &iSize, 1 ); | ||
346 | ms.write( (*i).sName.getStr(), iSize ); | ||
347 | } | ||
348 | ms.setSize( ms.tell() ); | ||
349 | } | ||
350 | |||
351 | void Bu::MyriadFs::setFileSize( const Bu::String &sPath, int32_t iSize ) | ||
352 | { | ||
353 | int32_t iParent = -1; | ||
354 | int32_t iNode; | ||
355 | iNode = lookupInode( sPath, iParent ); | ||
356 | MyriadStream ms = openByInode( iNode ); | ||
357 | ms.setSize( iSize ); | ||
358 | } | ||
359 | |||
360 | void Bu::MyriadFs::rename( const Bu::String &sFrom, const Bu::String &sTo ) | ||
361 | { | ||
362 | mkHardLink( sFrom, sTo ); | ||
363 | unlink( sFrom ); | ||
364 | } | ||
365 | |||
366 | dev_t Bu::MyriadFs::devToSys( uint32_t uDev ) | ||
367 | { | ||
368 | return (((uDev&0xFFFF0000)>>8)&0xFF00) | ((uDev&0xFF)); | ||
369 | } | ||
370 | |||
371 | uint32_t Bu::MyriadFs::sysToDev( dev_t uDev ) | ||
372 | { | ||
373 | return (((uint32_t)uDev&0xFF00)<<8) | ((uint32_t)uDev&0xFF); | ||
374 | } | ||
375 | |||
376 | int32_t Bu::MyriadFs::lookupInode( const Bu::String &sPath, int32_t &iParent ) | ||
377 | { | ||
378 | if( sPath == "/" ) | ||
379 | { | ||
380 | return 0; | ||
381 | } | ||
382 | if( sPath[0] == '/' ) | ||
383 | { | ||
384 | // Absolute lookup | ||
385 | return lookupInode( sPath.begin()+1, 0, iParent ); | ||
386 | } | ||
387 | else | ||
388 | { | ||
389 | // Relative lookup | ||
390 | throw Bu::ExceptionBase( | ||
391 | "Relative lookups in MyriadFs are not working yet."); | ||
392 | } | ||
393 | } | ||
394 | |||
395 | int32_t Bu::MyriadFs::lookupInode( Bu::String::const_iterator iStart, | ||
396 | int32_t iNode, int32_t &iParent ) | ||
397 | { | ||
398 | iParent = iNode; | ||
399 | |||
400 | Bu::String::const_iterator iEnd = iStart.find('/'); | ||
401 | Bu::String sTok( iStart, iEnd ); | ||
402 | |||
403 | // sio << "Direcotry component: " << sTok << sio.nl; | ||
404 | |||
405 | Dir lDir = readDir( iNode ); | ||
406 | |||
407 | for( Dir::iterator i = lDir.begin(); i; i++ ) | ||
408 | { | ||
409 | if( (*i).sName == sTok ) | ||
410 | { | ||
411 | // We found an item | ||
412 | if( !iEnd ) | ||
413 | { | ||
414 | // It's the last one in the requested path, return it | ||
415 | return (*i).iNode; | ||
416 | } | ||
417 | else | ||
418 | { | ||
419 | // Not the last one in our path, double check it's a dir | ||
420 | if( ((*i).uPerms&typeMask) == typeDir ) | ||
421 | { | ||
422 | return lookupInode( iEnd+1, (*i).iNode, iParent ); | ||
423 | } | ||
424 | else | ||
425 | { | ||
426 | iParent = -1; | ||
427 | throw Bu::MyriadFsException( | ||
428 | "Element '%s' in given path is not a directory.", | ||
429 | sTok.getStr() ); | ||
430 | } | ||
431 | } | ||
432 | } | ||
433 | } | ||
434 | |||
435 | if( iEnd ) | ||
436 | throw Bu::MyriadFsException( 1, "Path not found"); | ||
437 | else | ||
438 | throw Bu::MyriadFsException( 2, "Path not found"); | ||
439 | } | ||
440 | |||
441 | void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs ) | ||
442 | { | ||
443 | rIs.setPos( hNodeIndex.get( iNode )*sizeof(RawStat) ); | ||
444 | if( rIs.read( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) ) | ||
445 | throw Bu::MyriadFsException("Filesystem corruption detected."); | ||
446 | if( rs.iNode != iNode ) | ||
447 | throw Bu::MyriadFsException("Filesystem corruption detected."); | ||
448 | } | ||
449 | |||
450 | void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs ) | ||
451 | { | ||
452 | MyriadStream ms = mStore.openStream( 2 ); | ||
453 | readInode( iNode, rs, ms ); | ||
454 | } | ||
455 | |||
456 | void Bu::MyriadFs::writeInode( const RawStat &rs, | ||
457 | MyriadStream &rOs ) | ||
458 | { | ||
459 | rOs.setSize( hNodeIndex.getSize()*sizeof(RawStat) ); | ||
460 | rOs.setPos( hNodeIndex.get( rs.iNode )*sizeof(RawStat) ); | ||
461 | if( rOs.write( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) ) | ||
462 | throw Bu::MyriadFsException("Error writing inode to header stream."); | ||
463 | } | ||
464 | |||
465 | void Bu::MyriadFs::writeInode( const RawStat &rs ) | ||
466 | { | ||
467 | MyriadStream ms = mStore.openStream( 2 ); | ||
468 | writeInode( rs, ms ); | ||
469 | } | ||
470 | |||
471 | Bu::MyriadFs::Dir Bu::MyriadFs::readDir( int32_t iNode ) | ||
472 | { | ||
473 | Bu::MyriadStream ms = openByInode( iNode ); | ||
474 | int32_t iNumChildren = 0; | ||
475 | ms.read( &iNumChildren, 4 ); | ||
476 | |||
477 | Bu::MyriadStream is = mStore.openStream( 2 ); | ||
478 | Dir lDir; | ||
479 | // sio << "Reading dir " << iNode << ", " << iNumChildren << " entries:" << sio.nl; | ||
480 | for( int32_t j = 0; j < iNumChildren; j++ ) | ||
481 | { | ||
482 | int32_t iChildNode = 0; | ||
483 | if( ms.read( &iChildNode, 4 ) < 4 ) | ||
484 | { | ||
485 | throw Bu::MyriadFsException( | ||
486 | "Failed to read iChildNode from directory."); | ||
487 | } | ||
488 | Stat s; | ||
489 | stat( iChildNode, s, is ); | ||
490 | uint8_t uLen; | ||
491 | if( ms.read( &uLen, 1 ) < 1 ) | ||
492 | { | ||
493 | throw Bu::MyriadFsException( | ||
494 | "Failed to read uLen from directory."); | ||
495 | } | ||
496 | s.sName.setSize( uLen ); | ||
497 | if( ms.read( s.sName.getStr(), uLen ) < uLen ) | ||
498 | { | ||
499 | throw Bu::MyriadFsException( | ||
500 | "Failed to read sName from directory."); | ||
501 | } | ||
502 | lDir.append( s ); | ||
503 | |||
504 | // sio << " " << s.sName << sio.nl; | ||
505 | } | ||
506 | |||
507 | return lDir; | ||
508 | } | ||
509 | |||
510 | Bu::MyriadStream Bu::MyriadFs::openByInode( int32_t iNode ) | ||
511 | { | ||
512 | RawStat rs; | ||
513 | readInode( iNode, rs ); | ||
514 | switch( (rs.uPerms&typeMask) ) | ||
515 | { | ||
516 | case typeDir: | ||
517 | case typeSymLink: | ||
518 | case typeRegFile: | ||
519 | return mStore.openStream( rs.uStreamIndex ); | ||
520 | |||
521 | default: | ||
522 | throw Bu::MyriadFsException( | ||
523 | "inode incorrect type for low-level openByInode."); | ||
524 | } | ||
525 | } | ||
526 | |||
527 | void Bu::MyriadFs::addToDir( int32_t iDir, int32_t iNode, | ||
528 | const Bu::String &sName ) | ||
529 | { | ||
530 | if( sName.getSize() > 255 ) | ||
531 | { | ||
532 | throw Bu::MyriadFsException("Filename too long, max is 255 bytes."); | ||
533 | } | ||
534 | Bu::MyriadStream ms = openByInode( iDir ); | ||
535 | int32_t iNumChildren = 0; | ||
536 | ms.read( &iNumChildren, 4 ); | ||
537 | iNumChildren++; | ||
538 | ms.setPos( 0 ); | ||
539 | ms.write( &iNumChildren, 4 ); | ||
540 | ms.setPosEnd( 0 ); | ||
541 | ms.write( &iNode, 4 ); | ||
542 | uint8_t uLen = sName.getSize(); | ||
543 | ms.write( &uLen, 1 ); | ||
544 | ms.write( sName.getStr(), uLen ); | ||
545 | } | ||
546 | |||
547 | int32_t Bu::MyriadFs::create( int32_t iParent, const Bu::String &sName, | ||
548 | uint16_t uPerms, uint32_t uSpecial ) | ||
549 | { | ||
550 | int32_t iNode = allocInode( uPerms, uSpecial ); | ||
551 | addToDir( iParent, iNode, sName ); | ||
552 | return iNode; | ||
553 | } | ||
554 | |||
555 | int32_t Bu::MyriadFs::allocInode( uint16_t uPerms, uint32_t uSpecial ) | ||
556 | { | ||
557 | int32_t iNode = 0; | ||
558 | for(; iNode < 0xfffffff; iNode++ ) | ||
559 | { | ||
560 | if( !hNodeIndex.has( iNode ) ) | ||
561 | { | ||
562 | hNodeIndex.insert( iNode, hNodeIndex.getSize() ); | ||
563 | RawStat rs; | ||
564 | rs.iNode = iNode; | ||
565 | rs.iUser = iUser; | ||
566 | rs.iGroup = iGroup; | ||
567 | rs.uPerms = uPerms; | ||
568 | rs.iLinks = 1; | ||
569 | switch( (uPerms&typeMask) ) | ||
570 | { | ||
571 | case typeRegFile: | ||
572 | case typeSymLink: | ||
573 | rs.uStreamIndex = mStore.createStream(); | ||
574 | break; | ||
575 | |||
576 | case typeDir: | ||
577 | rs.uStreamIndex = mStore.createStream(); | ||
578 | // sio << "Creating directory node, storage: " | ||
579 | // << rs.uStreamIndex << sio.nl; | ||
580 | { | ||
581 | Bu::MyriadStream msDir = mStore.openStream( | ||
582 | rs.uStreamIndex | ||
583 | ); | ||
584 | uint32_t uSize = 0; | ||
585 | msDir.write( &uSize, 4 ); | ||
586 | } | ||
587 | break; | ||
588 | |||
589 | case typeChrDev: | ||
590 | case typeBlkDev: | ||
591 | rs.uStreamIndex = uSpecial; | ||
592 | break; | ||
593 | |||
594 | default: | ||
595 | rs.uStreamIndex = 0; | ||
596 | break; | ||
597 | } | ||
598 | rs.iATime = time(NULL); | ||
599 | rs.iMTime = time(NULL); | ||
600 | rs.iCTime = time(NULL); | ||
601 | writeInode( rs ); | ||
602 | |||
603 | return iNode; | ||
604 | } | ||
605 | } | ||
606 | |||
607 | throw Bu::MyriadFsException( | ||
608 | "No inode could be allocated. You've run out!"); | ||
609 | } | ||
610 | |||
611 | void Bu::MyriadFs::stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs ) | ||
612 | { | ||
613 | RawStat rs; | ||
614 | readInode( iNode, rs, rIs ); | ||
615 | rBuf.iNode = iNode; | ||
616 | rBuf.iUser = rs.iUser; | ||
617 | rBuf.iGroup = rs.iGroup; | ||
618 | rBuf.uPerms = rs.uPerms; | ||
619 | rBuf.iLinks = rs.iLinks; | ||
620 | rBuf.iATime = rs.iATime; | ||
621 | rBuf.iMTime = rs.iMTime; | ||
622 | rBuf.iCTime = rs.iCTime; | ||
623 | rBuf.uDev = 0; | ||
624 | rBuf.iSize = 0; | ||
625 | switch( (rBuf.uPerms&typeMask) ) | ||
626 | { | ||
627 | case typeRegFile: | ||
628 | case typeSymLink: | ||
629 | rBuf.iSize = mStore.getStreamSize( rs.uStreamIndex ); | ||
630 | break; | ||
631 | |||
632 | case typeChrDev: | ||
633 | case typeBlkDev: | ||
634 | rBuf.uDev = rs.uStreamIndex; | ||
635 | break; | ||
636 | |||
637 | default: | ||
638 | rBuf.iSize = 0; | ||
639 | break; | ||
640 | } | ||
641 | } | ||
642 | |||
643 | void Bu::MyriadFs::writeHeader() | ||
644 | { | ||
645 | Bu::MyriadStream ms = mStore.openStream( 1 ); | ||
646 | ms.write( Myriad_Fs_MAGIC_CODE, 4 ); | ||
647 | int8_t iVer = 1; | ||
648 | int32_t iNumNodes = hNodeIndex.getSize(); | ||
649 | ms.write( &iVer, 1 ); | ||
650 | ms.write( &iNumNodes, 4 ); // iNumNodes | ||
651 | for( NodeIndex::iterator i = hNodeIndex.begin(); i; i++ ) | ||
652 | { | ||
653 | int32_t iNode = i.getKey(); | ||
654 | int32_t iPosition = i.getValue(); | ||
655 | ms.write( &iNode, 4 ); | ||
656 | ms.write( &iPosition, 4 ); | ||
657 | } | ||
658 | |||
659 | // Truncate the stream afterwards so we don't use up too much space. | ||
660 | ms.setSize( ms.tell() ); | ||
661 | } | ||
662 | |||
663 | void Bu::MyriadFs::setTimes( int32_t iNode, int64_t iATime, int64_t iMTime ) | ||
664 | { | ||
665 | RawStat rs; | ||
666 | Bu::MyriadStream is = mStore.openStream( 2 ); | ||
667 | |||
668 | readInode( iNode, rs, is ); | ||
669 | rs.iATime = iATime; | ||
670 | rs.iMTime = iMTime; | ||
671 | writeInode( rs, is ); | ||
672 | } | ||
673 | |||
674 | void Bu::MyriadFs::destroyNode( int32_t iNode ) | ||
675 | { | ||
676 | if( iNode == 0 ) | ||
677 | throw Bu::MyriadFsException("You cannot destroy the root."); | ||
678 | |||
679 | Bu::MyriadStream is = mStore.openStream( 2 ); | ||
680 | |||
681 | // This will be overwritten with the last node | ||
682 | uint32_t iPosition = hNodeIndex.get( iNode ); | ||
683 | RawStat rsOld; | ||
684 | readInode( iNode, rsOld, is ); | ||
685 | switch( (rsOld.uPerms&typeMask) ) | ||
686 | { | ||
687 | case typeRegFile: | ||
688 | case typeDir: | ||
689 | case typeSymLink: | ||
690 | mStore.deleteStream( rsOld.uStreamIndex ); | ||
691 | break; | ||
692 | } | ||
693 | |||
694 | hNodeIndex.erase( iNode ); | ||
695 | |||
696 | // Read the last node, can't use the helpers, because we don't know the | ||
697 | // iNode yet. | ||
698 | if( iPosition != hNodeIndex.getSize() ) | ||
699 | { | ||
700 | // If this is the last node, then we don't need to do anything, but | ||
701 | // this case handles what to do if we aren't on the last node | ||
702 | RawStat rs; | ||
703 | is.setPos( (hNodeIndex.getSize())*sizeof(RawStat) ); | ||
704 | is.read( &rs, sizeof(RawStat) ); | ||
705 | |||
706 | hNodeIndex.get( rs.iNode ) = iPosition; | ||
707 | writeInode( rs, is ); | ||
708 | } | ||
709 | |||
710 | is.setSize( hNodeIndex.getSize() * sizeof(RawStat) ); | ||
711 | } | ||
712 | |||
713 | Bu::String Bu::MyriadFs::filePart( const Bu::String &sPath ) | ||
714 | { | ||
715 | Bu::String::const_iterator iStart = sPath.begin(); | ||
716 | if( *iStart == '/' ) | ||
717 | iStart++; | ||
718 | for( Bu::String::const_iterator iEnd = iStart.find('/'); iEnd; | ||
719 | iStart = iEnd+1, iEnd = iStart.find('/') ) { } | ||
720 | return Bu::String( iStart, sPath.end() ); | ||
721 | } | ||
722 | |||
diff --git a/src/unstable/myriadfs.h b/src/unstable/myriadfs.h deleted file mode 100644 index ff14292..0000000 --- a/src/unstable/myriadfs.h +++ /dev/null | |||
@@ -1,205 +0,0 @@ | |||
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 | |||
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 = 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 | ||