#include <tDirectories.h>
Public Member Functions | |
bool | Open (std::ifstream &f, const char *filename) const |
bool | Open (std::ofstream &f, const char *filename, std::ios::openmode mode=std::ios::out, bool sensitive=false) const |
bool | Open (std::fstream &f, const char *filename) const |
tString | GetReadPath (const char *filename) const |
tString | GetWritePath (const char *filename) const |
tString | GetPaths (void) const |
Puts all paths into a tString for outputting to the user. | |
tPath () | |
virtual | ~tPath () |
Static Public Member Functions | |
static bool | IsValidPath (char const *filename) |
checks whether filename is valid, i.e. does not endanger system security. | |
Protected Member Functions | |
virtual void | Paths (tArray< tString > &paths) const =0 |
Definition at line 36 of file tDirectories.h.
tPath::tPath | ( | ) | [inline] |
virtual tPath::~tPath | ( | ) | [inline, virtual] |
bool tPath::Open | ( | std::ifstream & | f, | |
const char * | filename | |||
) | const |
Definition at line 729 of file tDirectories.cpp.
References IsValidPath(), GrowingArrayBase::Len(), and Paths().
Referenced by Load(), nServerInfo::Load(), tStatFile::open(), tTextFileRecorder::Open(), rModel::rModel(), and nServerInfo::Save().
00731 { 00732 if ( !tPath::IsValidPath( filename ) ) 00733 return false; 00734 00735 tArray< tString > paths; 00736 Paths( paths ); 00737 00738 for ( int prio = paths.Len() - 1; prio>=0; --prio ) 00739 { 00740 // std::ifstream test; 00741 00742 tString fullname; 00743 fullname << paths( prio ) << "/" << filename; 00744 00745 #ifdef PRINTSEARCH 00746 #endif 00747 00748 // test.open( fullname ); 00749 f.clear(); 00750 f.open( fullname ); 00751 00752 // if ( test ) 00753 if ( f && f.good() ) 00754 { 00755 #ifdef PRINTSEARCH 00756 std::cout << "Trying to open " << fullname << " succeeded."; 00757 #endif 00758 // f.open( fullname ); 00759 00760 // return f; 00761 return true; 00762 } 00763 00764 #ifdef PRINTSEARCH 00765 std::cout << "Trying to open " << fullname << " succeeded."; 00766 #endif 00767 } 00768 00769 return false; 00770 }
bool tPath::Open | ( | std::ofstream & | f, | |
const char * | filename, | |||
std::ios::openmode | mode = std::ios::out , |
|||
bool | sensitive = false | |||
) | const |
Definition at line 775 of file tDirectories.cpp.
References GetWritePath(), and IsValidPath().
00779 { 00780 if ( !tPath::IsValidPath( filename ) ) 00781 return false; 00782 00783 // tArray< tString > paths; 00784 // Paths( paths ); 00785 00786 tString fullname = GetWritePath(filename); 00787 00788 #ifndef WIN32 00789 mode_t oldmask=0; 00790 if(sensitive && st_protectFiles) 00791 { 00792 oldmask = umask(0600); 00793 } 00794 #endif 00795 f.open( fullname, mode ); 00796 #ifndef WIN32 00797 if(sensitive && st_protectFiles) 00798 { 00799 chmod( &fullname(0), 0600 ); 00800 umask(oldmask); 00801 } 00802 #endif 00803 00804 return ( f && f.good() ); 00805 }
bool tPath::Open | ( | std::fstream & | f, | |
const char * | filename | |||
) | const |
Definition at line 807 of file tDirectories.cpp.
References GetWritePath(), and in.
00808 { 00809 // std::cout << "open\n"; 00810 00811 tString fullname = GetWritePath(filename); 00812 f.open(fullname, std::ios::in | std::ios::out); 00813 00814 return ( f.good() ); 00815 }
tString tPath::GetReadPath | ( | const char * | filename | ) | const |
Definition at line 817 of file tDirectories.cpp.
References IsValidPath(), GrowingArrayBase::Len(), and Paths().
Referenced by rSurface::Create(), eSoundMixer::Init(), eWavData::Load(), tPlayList::LoadPlaylist(), eSoundMixer::LoadPlaylist(), tResourceManager::locateResource(), rFontContainer::New(), se_SoundInit(), sky_select(), and st_PrintPathInfo().
00818 { 00819 if ( !tPath::IsValidPath( filename ) ) 00820 return tString(); 00821 00822 tArray< tString > paths; 00823 Paths( paths ); 00824 00825 for ( int prio = paths.Len() - 1; prio>=0; --prio ) 00826 { 00827 tString fullname; 00828 fullname << paths( prio ) << "/" << filename; 00829 std::ifstream f; 00830 00831 //if (fullname != "./moviepack/sky.png") 00832 #ifdef PRINTSEARCH 00833 printf("Searching %s...", (const char *)fullname); 00834 #endif 00835 f.open( fullname ); 00836 00837 if ( f && f.good() ) 00838 { 00839 //if (fullname != "./moviepack/sky.png") 00840 #ifdef PRINTSEARCH 00841 printf("OK\n"); 00842 #endif 00843 return fullname; 00844 } 00845 //if (fullname != "./moviepack/sky.png") 00846 #ifdef PRINTSEARCH 00847 printf("nope\n"); 00848 #endif 00849 } 00850 00851 return tString(); 00852 }
tString tPath::GetWritePath | ( | const char * | filename | ) | const |
Reimplemented in tPathResource.
Definition at line 854 of file tDirectories.cpp.
References free, IsValidPath(), GrowingArrayBase::Len(), mkdir_recurse(), Paths(), strdup, and tERR_WARN.
Referenced by Open().
00855 { 00856 if ( !tPath::IsValidPath( filename ) ) 00857 return tString(); 00858 00859 tArray< tString > paths; 00860 Paths( paths ); 00861 00862 tString fullname; 00863 fullname << paths( paths.Len() -1 ) << "/" << filename; 00864 00865 { 00866 bool s; 00867 00868 char *fpmr = strdup( static_cast< char const * > ( fullname ) ); 00869 s = mkdir_recurse(fpmr, paths( paths.Len() -1 ).Len()); 00870 free(fpmr); 00871 00872 if (!s) 00873 { 00874 tERR_WARN( "Could not create path to " << fullname << ". Check your user's rights." ); 00875 return tString(); 00876 } 00877 } 00878 00879 return fullname; 00880 }
bool tPath::IsValidPath | ( | char const * | filename | ) | [static] |
checks whether filename is valid, i.e. does not endanger system security.
checks whether a user given path, to be prepended with one of the AA directories, is a valid path (does not use dirty filename tricks to access portions of the file system it isn't supposed to access). It is called by all path finding functions of tPath automatically, you should only call it from outside of tDirectories.cpp if you want to avoid redundant error messages if you try several path resolutions.
filename | the user given filename to check |
Definition at line 316 of file tDirectories.cpp.
References con, and st_IsPathDelimiter().
Referenced by _wrap_TPath_is_valid_path(), GetReadPath(), GetWritePath(), tPathResource::GetWritePath(), Open(), and st_Include().
00317 { 00318 // always check for empty paths 00319 if ( !filename || filename[0] == 0 ) 00320 { 00321 con << tOutput( "$directory_path_null" ); 00322 return false; 00323 } 00324 00325 // check for absolute paths (the system would not make them absolute, so no real 00326 // danger comes from them, but we check anyway) 00327 if ( st_checkPathAbsolute && ( st_IsPathDelimiter( filename[0] ) || strstr(filename,":") ) ) 00328 { 00329 con << tOutput( "$directory_path_absolute", filename ); 00330 return false; 00331 } 00332 00333 // check for relative paths. Those are the real danger. Search for .. and see 00334 // if it surrounded by path delimiters or string ends. 00335 if ( st_checkPathRelative ) 00336 { 00337 // traverse through occurences of .. 00338 char const * run = filename; 00339 while ( run && *run ) 00340 { 00341 // find next .. 00342 run = strstr(run,".."); 00343 00344 // check if before and after that come no path delimiters 00345 if ( run ) 00346 { 00347 if ( ( run == filename || st_IsPathDelimiter( run[-1] ) ) && 00348 ( run[2] == 0 || st_IsPathDelimiter( run[2] ) ) ) 00349 { 00350 con << tOutput( "$directory_path_relative", filename ); 00351 return false; 00352 } 00353 00354 // go on searching 00355 run ++; 00356 } 00357 } 00358 } 00359 00360 // hidden paths are a path delimiter followed by a ., but not ..<delimiter>. 00361 if ( st_checkPathHidden ) 00362 { 00363 // iterate path segments 00364 char const * run = filename; 00365 while ( run && *run ) 00366 { 00367 // check if it is a hidden file 00368 if ( run[0] == '.' ) 00369 { 00370 // don't give false alarm for relative paths 00371 if ( run[1] != '.' || ( !st_IsPathDelimiter( run[2] ) && run[2] != 0 ) ) 00372 { 00373 con << tOutput( "$directory_path_hidden", filename ); 00374 return false; 00375 } 00376 } 00377 00378 // proceed to next path segments: find next path delimiter 00379 while ( *run && !st_IsPathDelimiter( *run ) ) 00380 ++run; 00381 00382 // go to next character after that 00383 if ( *run ) 00384 ++run; 00385 } 00386 } 00387 00388 return true; 00389 }
tString tPath::GetPaths | ( | void | ) | const |
Puts all paths into a tString for outputting to the user.
Definition at line 1659 of file tDirectories.cpp.
References GrowingArrayBase::Len(), and Paths().
Referenced by st_PrintPathInfo().
01659 { 01660 tString ret; 01661 tArray<tString> paths; 01662 Paths(paths); 01663 for (int i = 0; i < paths.Len(); ++i) { 01664 if(i > 0 && paths[i - 1] == paths[i]) continue; 01665 ret << " - " << paths[i] << "\n"; 01666 } 01667 return ret; 01668 }
Implemented in SwigDirector_TPath, tPathConfig, tPathData, tPathMusic, tPathVar, tPathScreenshot, tPathResource, and tPathScripts.
Referenced by GetPaths(), GetReadPath(), GetWritePath(), and Open().