diff options
Diffstat (limited to '')
-rw-r--r-- | src/unstable/myriadfs.cpp | 739 |
1 files changed, 739 insertions, 0 deletions
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 | |||