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