00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanFileSystemManager.h"
00028
00029 #include "CeylanStandardFileSystemManager.h"
00030 #include "CeylanLibfatFileSystemManager.h"
00031
00032 #include "CeylanLogPlug.h"
00033 #include "CeylanFile.h"
00034 #include "CeylanOperators.h"
00035
00036
00037
00038 #ifdef CEYLAN_USES_CONFIG_H
00039 #include "CeylanConfig.h"
00040 #endif // CEYLAN_USES_CONFIG_H
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 using std::string ;
00074 using std::list ;
00075
00076 using namespace Ceylan::System ;
00077 using namespace Ceylan::Log ;
00078
00079
00080 const string FileSystemManager::DefaultAliasForCurrentDirectory = "." ;
00081 const string FileSystemManager::DefaultAliasForParentDirectory = ".." ;
00082
00083
00084 FileSystemManager * FileSystemManager::_CurrentDefaultFileSystemManager = 0 ;
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 bool FileSystemManager::ExistsAsEntry( const std::string & entryPath )
00102 {
00103
00104
00105 return GetAnyDefaultFileSystemManager().existsAsEntry( entryPath ) ;
00106
00107 }
00108
00109
00110
00111 void FileSystemManager::CreateSymbolicLink( const string & linkTarget,
00112 const string & linkName )
00113 {
00114
00115
00116 GetAnyDefaultFileSystemManager().createSymbolicLink( linkTarget,
00117 linkName ) ;
00118
00119 }
00120
00121
00122
00123 time_t FileSystemManager::GetEntryChangeTime( const string & entryPath )
00124 {
00125
00126
00127 return GetAnyDefaultFileSystemManager().getEntryChangeTime( entryPath ) ;
00128
00129 }
00130
00131
00132
00133
00134
00135
00136
00137 const string & FileSystemManager::GetRootDirectoryPrefix()
00138 {
00139
00140
00141 return GetAnyDefaultFileSystemManager().getRootDirectoryPrefix() ;
00142
00143 }
00144
00145
00146
00147 Ceylan::Latin1Char FileSystemManager::GetSeparator()
00148 {
00149
00150
00151 return GetAnyDefaultFileSystemManager().getSeparator() ;
00152
00153 }
00154
00155
00156
00157 string FileSystemManager::GetSeparatorAsString()
00158 {
00159
00160
00161 return GetAnyDefaultFileSystemManager().getSeparatorAsString() ;
00162
00163 }
00164
00165
00166
00167 const string & FileSystemManager::GetAliasForCurrentDirectory()
00168 {
00169
00170
00171 return GetAnyDefaultFileSystemManager().getAliasForCurrentDirectory() ;
00172
00173 }
00174
00175
00176
00177 const string & FileSystemManager::GetAliasForParentDirectory()
00178 {
00179
00180
00181 return GetAnyDefaultFileSystemManager().getAliasForParentDirectory() ;
00182
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 const std::string & FileSystemManager::getAliasForCurrentDirectory() const
00198 {
00199
00200
00201 return DefaultAliasForCurrentDirectory ;
00202
00203 }
00204
00205
00206
00207 const std::string & FileSystemManager::getAliasForParentDirectory() const
00208 {
00209
00210
00211 return DefaultAliasForParentDirectory ;
00212
00213 }
00214
00215
00216
00217 std::string FileSystemManager::getSeparatorAsString() const
00218 {
00219
00220 return Ceylan::toString( getSeparator() ) ;
00221
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 std::string FileSystemManager::transformIntoValidFilename(
00234 const string & rawFilename )
00235 {
00236
00237
00238
00239 string result ;
00240
00241 Ceylan::Uint32 characterCount = 0 ;
00242
00243
00244 while ( rawFilename[ characterCount ] == '.' )
00245 characterCount++ ;
00246
00247
00248
00249
00250
00251 StringSize nameSize = rawFilename.size() ;
00252
00253 for ( ; characterCount < nameSize ; characterCount++ )
00254 {
00255 switch( rawFilename[ characterCount ] )
00256 {
00257
00258 case ' ':
00259 result += "-" ;
00260 break ;
00261
00262 case '/':
00263 result += "-" ;
00264 break ;
00265
00266 case '\\':
00267 result += "-" ;
00268 break ;
00269
00270
00271
00272
00273
00274
00275
00276 case ':':
00277 result += "_" ;
00278 break ;
00279
00280 default:
00281 result += rawFilename[ characterCount ] ;
00282 break ;
00283
00284 }
00285
00286 }
00287
00288 return result ;
00289
00290 }
00291
00292
00293
00294 bool FileSystemManager::diff( const std::string & firstFilename,
00295 const std::string & secondFilename )
00296 {
00297
00298 try
00299 {
00300
00301
00302 if ( ! existsAsFileOrSymbolicLink( firstFilename ) )
00303 {
00304
00305 LogPlug::warning( "FileSystemManager::diff: first file '"
00306 + firstFilename + "' does not exist." ) ;
00307
00308 return false ;
00309
00310 }
00311
00312
00313 if ( ! existsAsFileOrSymbolicLink( secondFilename ) )
00314 {
00315
00316 LogPlug::warning( "FileSystemManager::diff: first file '"
00317 + secondFilename + "' does not exist." ) ;
00318
00319 return false ;
00320
00321 }
00322
00323 Size commonSize = getSize( firstFilename ) ;
00324
00325 if ( commonSize != getSize( secondFilename ) )
00326 {
00327
00328 LogPlug::warning( "FileSystemManager::diff: "
00329 "the two files do not have the same size." ) ;
00330
00331 return false ;
00332
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342 File & first = openFile( firstFilename ) ;
00343 File & second = openFile( secondFilename ) ;
00344
00345 for ( Size i = 0; i < commonSize; i++ )
00346 {
00347
00348 if ( first.readUint8() != second.readUint8() )
00349 return false ;
00350
00351 }
00352
00353 return true ;
00354
00355 }
00356 catch( const SystemException & e )
00357 {
00358
00359 throw FileDiffFailed( "FileSystemManager::diff failed: "
00360 + e.toString() ) ;
00361
00362 }
00363
00364
00365 }
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377 void FileSystemManager::removeLeadingSeparator( std::string & path )
00378 {
00379
00380 if ( path[ path.size() - 1 ] == getSeparator() )
00381 path.erase( path.size() - 1, 1 ) ;
00382
00383 }
00384
00385
00386
00387 list<string> FileSystemManager::splitPath( const string & path )
00388 {
00389
00390
00391
00392
00393
00394
00395
00396
00397 list<string> res ;
00398 string currentWord ;
00399
00400
00401
00402
00403
00404
00405
00406
00407 bool inEntry = true ;
00408
00409 Latin1Char separator = getSeparator() ;
00410
00411
00412 for( string::const_iterator it = path.begin(); it != path.end(); it++ )
00413 {
00414
00415 if ( (*it) == separator )
00416 {
00417
00418 if ( inEntry )
00419 res.push_back( currentWord ) ;
00420 inEntry = false ;
00421 currentWord.erase() ;
00422 }
00423 else
00424 {
00425
00426 currentWord += *it ;
00427 inEntry = true ;
00428 }
00429
00430 }
00431
00432
00433 if ( inEntry )
00434 res.push_back( currentWord ) ;
00435
00436 return res ;
00437
00438 }
00439
00440
00441
00442 string FileSystemManager::joinPath( const list<string> & pathElements )
00443 {
00444
00445 list<string>::const_iterator it = pathElements.begin() ;
00446 string res = (*it) ;
00447 it++ ;
00448
00449 while ( it != pathElements.end() )
00450 {
00451 res += getSeparatorAsString() + (*it ) ;
00452 it++ ;
00453 }
00454
00455 return res ;
00456
00457 }
00458
00459
00460
00461 string FileSystemManager::joinPath( const string & firstPath,
00462 const std::string & secondPath )
00463 {
00464
00465 return firstPath + getSeparatorAsString() + secondPath ;
00466
00467 }
00468
00469
00470
00471 void FileSystemManager::stripFilename( const string & path, string * base,
00472 string * file )
00473 {
00474
00475 if ( base != 0 )
00476 base->erase() ;
00477
00478 if ( file != 0 )
00479 file->erase() ;
00480
00481 string::size_type p ;
00482
00483 Latin1Char separator = getSeparator() ;
00484
00485
00486 if ( ( p = path.rfind( separator ) ) != string::npos )
00487 {
00488
00489 if ( base != 0 )
00490 base->assign( path, 0, p ) ;
00491
00492
00493 if ( file != 0 )
00494 file->assign( path, p+1, path.size() - p - 1 ) ;
00495 }
00496 else
00497 {
00498
00499 if ( file != 0 )
00500 *file = path ;
00501 }
00502
00503 }
00504
00505
00506
00507
00508
00509
00510
00511
00512 const string FileSystemManager::toString( Ceylan::VerbosityLevels level ) const
00513 {
00514
00515 return "Abstract filesystem manager" ;
00516
00517 }
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 bool FileSystemManager::IsDefaultFileSystemManagerSet()
00528 {
00529
00530 return ( _CurrentDefaultFileSystemManager != 0 ) ;
00531
00532 }
00533
00534
00535
00536 void FileSystemManager::SetDefaultFileSystemManager(
00537 FileSystemManager & newDefaultFileSystemManager,
00538 bool deallocatePreviousIfAny )
00539 {
00540
00541 if ( deallocatePreviousIfAny )
00542 {
00543
00544
00545
00546
00547
00548
00549
00550
00551 if ( _CurrentDefaultFileSystemManager != 0 )
00552 delete _CurrentDefaultFileSystemManager ;
00553
00554 }
00555
00556 _CurrentDefaultFileSystemManager = & newDefaultFileSystemManager ;
00557
00558 }
00559
00560
00561
00562 void FileSystemManager::SetDefaultFileSystemManagerToPlatformDefault()
00563 {
00564
00565
00566 if ( _CurrentDefaultFileSystemManager != 0 )
00567 delete _CurrentDefaultFileSystemManager ;
00568
00569 #if CEYLAN_ARCH_NINTENDO_DS
00570
00571 #ifdef CEYLAN_RUNS_ON_ARM7
00572
00573 throw FileSystemManagerException(
00574 "FileSystemManager::SetDefaultFileSystemManagerToPlatformDefault: "
00575 "only available on the ARM9." ) ;
00576
00577 #elif defined(CEYLAN_RUNS_ON_ARM9)
00578
00579
00580
00581
00582
00583
00584 _CurrentDefaultFileSystemManager =
00585 & LibfatFileSystemManager::GetLibfatFileSystemManager() ;
00586
00587 #endif // CEYLAN_RUNS_ON_ARM7
00588
00589
00590 #else // CEYLAN_ARCH_NINTENDO_DS
00591
00592
00593
00594
00595
00596
00597 _CurrentDefaultFileSystemManager =
00598 & StandardFileSystemManager::GetStandardFileSystemManager() ;
00599
00600 #endif // CEYLAN_ARCH_NINTENDO_DS
00601
00602 }
00603
00604
00605
00606 FileSystemManager & FileSystemManager::GetExistingDefaultFileSystemManager()
00607 {
00608
00609 if ( _CurrentDefaultFileSystemManager == 0 )
00610 throw FileSystemManagerException(
00611 "FileSystemManager::GetExistingDefaultFileSystemManager:"
00612 "no manager currently available" ) ;
00613
00614 return *_CurrentDefaultFileSystemManager ;
00615
00616 }
00617
00618
00619
00620 FileSystemManager & FileSystemManager::GetAnyDefaultFileSystemManager()
00621 {
00622
00623 if ( _CurrentDefaultFileSystemManager == 0 )
00624 SetDefaultFileSystemManagerToPlatformDefault() ;
00625
00626 return *_CurrentDefaultFileSystemManager ;
00627
00628 }
00629
00630
00631
00632 void FileSystemManager::RemoveDefaultFileSystemManager()
00633 {
00634
00635 if ( _CurrentDefaultFileSystemManager != 0 )
00636 {
00637
00638 delete _CurrentDefaultFileSystemManager ;
00639 _CurrentDefaultFileSystemManager = 0 ;
00640
00641 }
00642
00643 }
00644
00645
00646
00647
00648
00649
00650
00651 FileSystemManager::FileSystemManager()
00652 {
00653
00654 }
00655
00656
00657
00658 FileSystemManager::~FileSystemManager() throw()
00659 {
00660
00661 }
00662