diff options
Diffstat (limited to 'src/unstable/myriadfs.cpp')
-rw-r--r-- | src/unstable/myriadfs.cpp | 737 |
1 files changed, 737 insertions, 0 deletions
diff --git a/src/unstable/myriadfs.cpp b/src/unstable/myriadfs.cpp new file mode 100644 index 0000000..ab9ca74 --- /dev/null +++ b/src/unstable/myriadfs.cpp | |||
@@ -0,0 +1,737 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2023 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/config.h" | ||
9 | #include "bu/myriadfs.h" | ||
10 | #include "bu/myriadstream.h" | ||
11 | |||
12 | #include <string.h> | ||
13 | #include <unistd.h> | ||
14 | #include <time.h> | ||
15 | |||
16 | #include "bu/sio.h" | ||
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.exists( 1 ) ) | ||
36 | { | ||
37 | // Check to see if this is a MyriadFs stream. | ||
38 | Bu::MyriadStream ms = mStore.open( 1, Bu::Myriad::Read ); | ||
39 | char sMagic[4]; | ||
40 | if( ms.read( sMagic, 4 ) < 4 ) | ||
41 | throw MyriadFsException("The provided stream does not appear to be " | ||
42 | "a MyriadFs stream."); | ||
43 | if( ::strncmp( sMagic, Myriad_Fs_MAGIC_CODE, 4 ) ) | ||
44 | throw MyriadFsException("The provided stream does not appear to be " | ||
45 | "a MyriadFs stream."); | ||
46 | |||
47 | int8_t iVer; | ||
48 | ms.read( &iVer, 1 ); | ||
49 | |||
50 | int32_t iNumNodes; | ||
51 | ms.read( &iNumNodes, 4 ); | ||
52 | for( int32_t j = 0; j < iNumNodes; j++ ) | ||
53 | { | ||
54 | int32_t iNode; | ||
55 | int32_t iPos; | ||
56 | ms.read( &iNode, 4 ); | ||
57 | ms.read( &iPos, 4 ); | ||
58 | hNodeIndex.insert( iNode, iPos ); | ||
59 | } | ||
60 | } | ||
61 | else | ||
62 | { | ||
63 | // Create initial header stream | ||
64 | { | ||
65 | Bu::MyriadStream ms = mStore.open( | ||
66 | 1, Bu::Myriad::WriteNew|Bu::Myriad::Exclusive ); | ||
67 | ms.write( Myriad_Fs_MAGIC_CODE, 4 ); | ||
68 | int8_t iVer = 1; | ||
69 | int32_t iTmp = 1; | ||
70 | ms.write( &iVer, 1 ); | ||
71 | ms.write( &iTmp, 4 ); // iNumNodes | ||
72 | iTmp = 0; | ||
73 | ms.write( &iTmp, 4 ); // iInode | ||
74 | ms.write( &iTmp, 4 ); // iPosition | ||
75 | hNodeIndex.insert( 0, 0 ); | ||
76 | } | ||
77 | |||
78 | // Create initial inode stream, with one root node. | ||
79 | { | ||
80 | Bu::MyriadStream ms = mStore.open( | ||
81 | 2, Bu::Myriad::WriteNew|Bu::Myriad::Exclusive ); | ||
82 | RawStat rs; | ||
83 | rs.iNode = 0; | ||
84 | rs.iUser = iUser; | ||
85 | rs.iGroup = iGroup; | ||
86 | rs.uPerms = 0755|typeDir; | ||
87 | rs.iLinks = 1; | ||
88 | rs.uStreamIndex = 3; | ||
89 | rs.iCTime = rs.iMTime = rs.iATime = time(NULL); | ||
90 | ms.write( &rs, sizeof(RawStat) ); | ||
91 | } | ||
92 | |||
93 | // Create inode 0's storage stream. | ||
94 | { | ||
95 | Bu::MyriadStream ms = mStore.open( | ||
96 | 3, Bu::Myriad::WriteNew|Bu::Myriad::Exclusive ); | ||
97 | int32_t iTmp32 = 0; | ||
98 | ms.write( &iTmp32, 4 ); // iChildCount | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | |||
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.open( 2, Bu::Myriad::Read ); | ||
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.open( 2, Bu::Myriad::ReadWrite ); | ||
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.open( | ||
318 | rs.uStreamIndex, Bu::Myriad::Read | ||
319 | ); | ||
320 | int32_t iCount; | ||
321 | msDir.read( &iCount, 4 ); | ||
322 | if( iCount > 0 ) | ||
323 | { | ||
324 | throw Bu::MyriadFsException("Directory not empty."); | ||
325 | } | ||
326 | } | ||
327 | if( --rs.iLinks == 0 ) | ||
328 | { | ||
329 | destroyNode( (*i).iNode ); | ||
330 | } | ||
331 | else | ||
332 | { | ||
333 | writeInode( rs ); | ||
334 | } | ||
335 | lDir.erase( i ); | ||
336 | break; | ||
337 | } | ||
338 | } | ||
339 | |||
340 | Bu::MyriadStream ms = openByInode( iParent ); | ||
341 | int32_t iNumChildren = lDir.getSize(); | ||
342 | ms.write( &iNumChildren, 4 ); | ||
343 | for( Dir::iterator i = lDir.begin(); i; i++ ) | ||
344 | { | ||
345 | ms.write( &(*i).iNode, 4 ); | ||
346 | uint8_t iSize = (*i).sName.getSize(); | ||
347 | ms.write( &iSize, 1 ); | ||
348 | ms.write( (*i).sName.getStr(), iSize ); | ||
349 | } | ||
350 | ms.setSize( ms.tell() ); | ||
351 | } | ||
352 | |||
353 | void Bu::MyriadFs::setFileSize( const Bu::String &sPath, int32_t iSize ) | ||
354 | { | ||
355 | int32_t iParent = -1; | ||
356 | int32_t iNode; | ||
357 | iNode = lookupInode( sPath, iParent ); | ||
358 | MyriadStream ms = openByInode( iNode ); | ||
359 | ms.setSize( iSize ); | ||
360 | } | ||
361 | |||
362 | void Bu::MyriadFs::rename( const Bu::String &sFrom, const Bu::String &sTo ) | ||
363 | { | ||
364 | mkHardLink( sFrom, sTo ); | ||
365 | unlink( sFrom ); | ||
366 | } | ||
367 | |||
368 | dev_t Bu::MyriadFs::devToSys( uint32_t uDev ) | ||
369 | { | ||
370 | return (((uDev&0xFFFF0000)>>8)&0xFF00) | ((uDev&0xFF)); | ||
371 | } | ||
372 | |||
373 | uint32_t Bu::MyriadFs::sysToDev( dev_t uDev ) | ||
374 | { | ||
375 | return (((uint32_t)uDev&0xFF00)<<8) | ((uint32_t)uDev&0xFF); | ||
376 | } | ||
377 | |||
378 | int32_t Bu::MyriadFs::lookupInode( const Bu::String &sPath, int32_t &iParent ) | ||
379 | { | ||
380 | if( sPath == "/" ) | ||
381 | { | ||
382 | return 0; | ||
383 | } | ||
384 | if( sPath[0] == '/' ) | ||
385 | { | ||
386 | // Absolute lookup | ||
387 | return lookupInode( sPath.begin()+1, 0, iParent ); | ||
388 | } | ||
389 | else | ||
390 | { | ||
391 | // Relative lookup | ||
392 | throw Bu::ExceptionBase( | ||
393 | "Relative lookups in MyriadFs are not working yet."); | ||
394 | } | ||
395 | } | ||
396 | |||
397 | int32_t Bu::MyriadFs::lookupInode( Bu::String::const_iterator iStart, | ||
398 | int32_t iNode, int32_t &iParent ) | ||
399 | { | ||
400 | iParent = iNode; | ||
401 | |||
402 | Bu::String::const_iterator iEnd = iStart.find('/'); | ||
403 | Bu::String sTok( iStart, iEnd ); | ||
404 | |||
405 | // sio << "Direcotry component: " << sTok << sio.nl; | ||
406 | |||
407 | Dir lDir = readDir( iNode ); | ||
408 | |||
409 | for( Dir::iterator i = lDir.begin(); i; i++ ) | ||
410 | { | ||
411 | if( (*i).sName == sTok ) | ||
412 | { | ||
413 | // We found an item | ||
414 | if( !iEnd ) | ||
415 | { | ||
416 | // It's the last one in the requested path, return it | ||
417 | return (*i).iNode; | ||
418 | } | ||
419 | else | ||
420 | { | ||
421 | // Not the last one in our path, double check it's a dir | ||
422 | if( ((*i).uPerms&typeMask) == typeDir ) | ||
423 | { | ||
424 | return lookupInode( iEnd+1, (*i).iNode, iParent ); | ||
425 | } | ||
426 | else | ||
427 | { | ||
428 | iParent = -1; | ||
429 | throw Bu::MyriadFsException( | ||
430 | "Element '%s' in given path is not a directory.", | ||
431 | sTok.getStr() ); | ||
432 | } | ||
433 | } | ||
434 | } | ||
435 | } | ||
436 | |||
437 | if( iEnd ) | ||
438 | throw Bu::MyriadFsException( 1, "Path not found"); | ||
439 | else | ||
440 | throw Bu::MyriadFsException( 2, "Path not found"); | ||
441 | } | ||
442 | |||
443 | void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs ) | ||
444 | { | ||
445 | rIs.setPos( hNodeIndex.get( iNode )*sizeof(RawStat) ); | ||
446 | if( rIs.read( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) ) | ||
447 | throw Bu::MyriadFsException("Filesystem corruption detected."); | ||
448 | if( rs.iNode != iNode ) | ||
449 | throw Bu::MyriadFsException("Filesystem corruption detected."); | ||
450 | } | ||
451 | |||
452 | void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs ) | ||
453 | { | ||
454 | MyriadStream ms = mStore.open( 2, Bu::Myriad::Read ); | ||
455 | readInode( iNode, rs, ms ); | ||
456 | } | ||
457 | |||
458 | void Bu::MyriadFs::writeInode( const RawStat &rs, | ||
459 | MyriadStream &rOs ) | ||
460 | { | ||
461 | rOs.setSize( hNodeIndex.getSize()*sizeof(RawStat) ); | ||
462 | rOs.setPos( hNodeIndex.get( rs.iNode )*sizeof(RawStat) ); | ||
463 | if( rOs.write( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) ) | ||
464 | throw Bu::MyriadFsException("Error writing inode to header stream."); | ||
465 | } | ||
466 | |||
467 | void Bu::MyriadFs::writeInode( const RawStat &rs ) | ||
468 | { | ||
469 | MyriadStream ms = mStore.open( 2, Bu::Myriad::Write ); | ||
470 | writeInode( rs, ms ); | ||
471 | } | ||
472 | |||
473 | Bu::MyriadFs::Dir Bu::MyriadFs::readDir( int32_t iNode ) | ||
474 | { | ||
475 | Bu::MyriadStream ms = openByInode( iNode ); | ||
476 | int32_t iNumChildren = 0; | ||
477 | ms.read( &iNumChildren, 4 ); | ||
478 | |||
479 | Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::Read ); | ||
480 | Dir lDir; | ||
481 | // sio << "Reading dir " << iNode << ", " << iNumChildren << " entries:" << sio.nl; | ||
482 | for( int32_t j = 0; j < iNumChildren; j++ ) | ||
483 | { | ||
484 | int32_t iChildNode = 0; | ||
485 | if( ms.read( &iChildNode, 4 ) < 4 ) | ||
486 | { | ||
487 | throw Bu::MyriadFsException( | ||
488 | "Failed to read iChildNode from directory."); | ||
489 | } | ||
490 | Stat s; | ||
491 | stat( iChildNode, s, is ); | ||
492 | uint8_t uLen; | ||
493 | if( ms.read( &uLen, 1 ) < 1 ) | ||
494 | { | ||
495 | throw Bu::MyriadFsException( | ||
496 | "Failed to read uLen from directory."); | ||
497 | } | ||
498 | s.sName.setSize( uLen ); | ||
499 | if( ms.read( s.sName.getStr(), uLen ) < uLen ) | ||
500 | { | ||
501 | throw Bu::MyriadFsException( | ||
502 | "Failed to read sName from directory."); | ||
503 | } | ||
504 | lDir.append( s ); | ||
505 | |||
506 | // sio << " " << s.sName << sio.nl; | ||
507 | } | ||
508 | |||
509 | return lDir; | ||
510 | } | ||
511 | |||
512 | Bu::MyriadStream Bu::MyriadFs::openByInode( int32_t iNode ) | ||
513 | { | ||
514 | RawStat rs; | ||
515 | readInode( iNode, rs ); | ||
516 | switch( (rs.uPerms&typeMask) ) | ||
517 | { | ||
518 | case typeDir: | ||
519 | case typeSymLink: | ||
520 | case typeRegFile: | ||
521 | return mStore.open( rs.uStreamIndex, Bu::Myriad::ReadWrite ); | ||
522 | |||
523 | default: | ||
524 | throw Bu::MyriadFsException( | ||
525 | "inode incorrect type for low-level openByInode."); | ||
526 | } | ||
527 | } | ||
528 | |||
529 | void Bu::MyriadFs::addToDir( int32_t iDir, int32_t iNode, | ||
530 | const Bu::String &sName ) | ||
531 | { | ||
532 | if( sName.getSize() > 255 ) | ||
533 | { | ||
534 | throw Bu::MyriadFsException("Filename too long, max is 255 bytes."); | ||
535 | } | ||
536 | Bu::MyriadStream ms = openByInode( iDir ); | ||
537 | int32_t iNumChildren = 0; | ||
538 | ms.read( &iNumChildren, 4 ); | ||
539 | iNumChildren++; | ||
540 | ms.setPos( 0 ); | ||
541 | ms.write( &iNumChildren, 4 ); | ||
542 | ms.setPosEnd( 0 ); | ||
543 | ms.write( &iNode, 4 ); | ||
544 | uint8_t uLen = sName.getSize(); | ||
545 | ms.write( &uLen, 1 ); | ||
546 | ms.write( sName.getStr(), uLen ); | ||
547 | } | ||
548 | |||
549 | int32_t Bu::MyriadFs::create( int32_t iParent, const Bu::String &sName, | ||
550 | uint16_t uPerms, uint32_t uSpecial ) | ||
551 | { | ||
552 | int32_t iNode = allocInode( uPerms, uSpecial ); | ||
553 | addToDir( iParent, iNode, sName ); | ||
554 | return iNode; | ||
555 | } | ||
556 | |||
557 | int32_t Bu::MyriadFs::allocInode( uint16_t uPerms, uint32_t uSpecial ) | ||
558 | { | ||
559 | int32_t iNode = 0; | ||
560 | for(; iNode < 0xfffffff; iNode++ ) | ||
561 | { | ||
562 | if( !hNodeIndex.has( iNode ) ) | ||
563 | { | ||
564 | hNodeIndex.insert( iNode, hNodeIndex.getSize() ); | ||
565 | RawStat rs; | ||
566 | rs.iNode = iNode; | ||
567 | rs.iUser = iUser; | ||
568 | rs.iGroup = iGroup; | ||
569 | rs.uPerms = uPerms; | ||
570 | rs.iLinks = 1; | ||
571 | switch( (uPerms&typeMask) ) | ||
572 | { | ||
573 | case typeRegFile: | ||
574 | case typeSymLink: | ||
575 | { | ||
576 | Bu::MyriadStream ms = mStore.create( | ||
577 | Bu::Myriad::Create | ||
578 | ); | ||
579 | rs.uStreamIndex = ms.getId(); | ||
580 | } | ||
581 | break; | ||
582 | |||
583 | case typeDir: | ||
584 | { | ||
585 | Bu::MyriadStream ms = mStore.create( | ||
586 | Bu::Myriad::Create | ||
587 | ); | ||
588 | rs.uStreamIndex = ms.getId(); | ||
589 | } | ||
590 | // sio << "Creating directory node, storage: " | ||
591 | // << rs.uStreamIndex << sio.nl; | ||
592 | { | ||
593 | Bu::MyriadStream msDir = mStore.open( | ||
594 | rs.uStreamIndex, Bu::Myriad::Write | ||
595 | ); | ||
596 | uint32_t uSize = 0; | ||
597 | msDir.write( &uSize, 4 ); | ||
598 | } | ||
599 | break; | ||
600 | |||
601 | case typeChrDev: | ||
602 | case typeBlkDev: | ||
603 | rs.uStreamIndex = uSpecial; | ||
604 | break; | ||
605 | |||
606 | default: | ||
607 | rs.uStreamIndex = 0; | ||
608 | break; | ||
609 | } | ||
610 | rs.iATime = time(NULL); | ||
611 | rs.iMTime = time(NULL); | ||
612 | rs.iCTime = time(NULL); | ||
613 | writeInode( rs ); | ||
614 | |||
615 | return iNode; | ||
616 | } | ||
617 | } | ||
618 | |||
619 | throw Bu::MyriadFsException( | ||
620 | "No inode could be allocated. You've run out!"); | ||
621 | } | ||
622 | |||
623 | void Bu::MyriadFs::stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs ) | ||
624 | { | ||
625 | RawStat rs; | ||
626 | readInode( iNode, rs, rIs ); | ||
627 | rBuf.iNode = iNode; | ||
628 | rBuf.iUser = rs.iUser; | ||
629 | rBuf.iGroup = rs.iGroup; | ||
630 | rBuf.uPerms = rs.uPerms; | ||
631 | rBuf.iLinks = rs.iLinks; | ||
632 | rBuf.iATime = rs.iATime; | ||
633 | rBuf.iMTime = rs.iMTime; | ||
634 | rBuf.iCTime = rs.iCTime; | ||
635 | rBuf.uDev = 0; | ||
636 | rBuf.iSize = 0; | ||
637 | switch( (rBuf.uPerms&typeMask) ) | ||
638 | { | ||
639 | case typeRegFile: | ||
640 | case typeSymLink: | ||
641 | rBuf.iSize = mStore.getSize( rs.uStreamIndex ); | ||
642 | break; | ||
643 | |||
644 | case typeChrDev: | ||
645 | case typeBlkDev: | ||
646 | rBuf.uDev = rs.uStreamIndex; | ||
647 | break; | ||
648 | |||
649 | default: | ||
650 | rBuf.iSize = 0; | ||
651 | break; | ||
652 | } | ||
653 | } | ||
654 | |||
655 | void Bu::MyriadFs::writeHeader() | ||
656 | { | ||
657 | Bu::MyriadStream ms = mStore.open( 1, Bu::Myriad::Write ); | ||
658 | ms.write( Myriad_Fs_MAGIC_CODE, 4 ); | ||
659 | int8_t iVer = 1; | ||
660 | int32_t iNumNodes = hNodeIndex.getSize(); | ||
661 | ms.write( &iVer, 1 ); | ||
662 | ms.write( &iNumNodes, 4 ); // iNumNodes | ||
663 | for( NodeIndex::iterator i = hNodeIndex.begin(); i; i++ ) | ||
664 | { | ||
665 | int32_t iNode = i.getKey(); | ||
666 | int32_t iPosition = i.getValue(); | ||
667 | ms.write( &iNode, 4 ); | ||
668 | ms.write( &iPosition, 4 ); | ||
669 | } | ||
670 | |||
671 | // Truncate the stream afterwards so we don't use up too much space. | ||
672 | ms.setSize( ms.tell() ); | ||
673 | } | ||
674 | |||
675 | void Bu::MyriadFs::setTimes( int32_t iNode, int64_t iATime, int64_t iMTime ) | ||
676 | { | ||
677 | RawStat rs; | ||
678 | Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::ReadWrite ); | ||
679 | |||
680 | readInode( iNode, rs, is ); | ||
681 | rs.iATime = iATime; | ||
682 | rs.iMTime = iMTime; | ||
683 | writeInode( rs, is ); | ||
684 | } | ||
685 | |||
686 | void Bu::MyriadFs::destroyNode( int32_t iNode ) | ||
687 | { | ||
688 | if( iNode == 0 ) | ||
689 | throw Bu::MyriadFsException("You cannot destroy the root."); | ||
690 | |||
691 | uint32_t iPosition; | ||
692 | RawStat rsOld; | ||
693 | |||
694 | Bu::MyriadStream is = mStore.open( 2, Bu::Myriad::ReadWrite ); | ||
695 | |||
696 | // This will be overwritten with the last node | ||
697 | iPosition = hNodeIndex.get( iNode ); | ||
698 | readInode( iNode, rsOld, is ); | ||
699 | |||
700 | switch( (rsOld.uPerms&typeMask) ) | ||
701 | { | ||
702 | case typeRegFile: | ||
703 | case typeDir: | ||
704 | case typeSymLink: | ||
705 | mStore.erase( rsOld.uStreamIndex ); | ||
706 | break; | ||
707 | } | ||
708 | |||
709 | hNodeIndex.erase( iNode ); | ||
710 | |||
711 | // Read the last node, can't use the helpers, because we don't know the | ||
712 | // iNode yet. | ||
713 | if( iPosition != hNodeIndex.getSize() ) | ||
714 | { | ||
715 | // If this is the last node, then we don't need to do anything, but | ||
716 | // this case handles what to do if we aren't on the last node | ||
717 | RawStat rs; | ||
718 | is.setPos( (hNodeIndex.getSize())*sizeof(RawStat) ); | ||
719 | is.read( &rs, sizeof(RawStat) ); | ||
720 | |||
721 | hNodeIndex.get( rs.iNode ) = iPosition; | ||
722 | writeInode( rs, is ); | ||
723 | } | ||
724 | |||
725 | is.setSize( hNodeIndex.getSize() * sizeof(RawStat) ); | ||
726 | } | ||
727 | |||
728 | Bu::String Bu::MyriadFs::filePart( const Bu::String &sPath ) | ||
729 | { | ||
730 | Bu::String::const_iterator iStart = sPath.begin(); | ||
731 | if( *iStart == '/' ) | ||
732 | iStart++; | ||
733 | for( Bu::String::const_iterator iEnd = iStart.find('/'); iEnd; | ||
734 | iStart = iEnd+1, iEnd = iStart.find('/') ) { } | ||
735 | return Bu::String( iStart, sPath.end() ); | ||
736 | } | ||
737 | |||