|
OpenTTD Source 20260421-master-gc2fbc6fdeb
|
Standard in/out file operations. More...
#include "stdafx.h"#include "core/string_consumer.hpp"#include "fileio_func.h"#include "spriteloader/spriteloader.hpp"#include "debug.h"#include "fios.h"#include "string_func.h"#include "tar_type.h"#include <unistd.h>#include <pwd.h>#include <sys/stat.h>#include <filesystem>#include "safeguards.h"Go to the source code of this file.
Functions | |
| static bool | IsValidSearchPath (Searchpath sp) |
| Checks whether the given search path is a valid search path. | |
| static void | FillValidSearchPaths (bool only_local_path) |
| bool | FioCheckFileExists (std::string_view filename, Subdirectory subdir) |
| Check whether the given file exists. | |
| bool | FileExists (std::string_view filename) |
| Test whether the given filename exists. | |
| std::string | FioFindFullPath (Subdirectory subdir, std::string_view filename) |
| Find a path to the filename in one of the search directories. | |
| std::string | FioGetDirectory (Searchpath sp, Subdirectory subdir) |
| std::string | FioFindDirectory (Subdirectory subdir) |
| static std::optional< FileHandle > | FioFOpenFileSp (std::string_view filename, std::string_view mode, Searchpath sp, Subdirectory subdir, size_t *filesize) |
| static std::optional< FileHandle > | FioFOpenFileTar (const TarFileListEntry &entry, size_t *filesize) |
| Opens a file from inside a tar archive. | |
| std::optional< FileHandle > | FioFOpenFile (std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize) |
| Opens a OpenTTD file somewhere in a personal or global directory. | |
| void | FioCreateDirectory (const std::string &name) |
| Create a directory with the given name If the parent directory does not exist, it will try to create that as well. | |
| bool | FioRemove (const std::string &filename) |
| Remove a file. | |
| void | AppendPathSeparator (std::string &buf) |
| Appends, if necessary, the path separator character to the end of the string. | |
| static void | SimplifyFileName (std::string &name) |
| Simplify filenames from tars. | |
| static std::string | ExtractString (std::span< char > buffer) |
| Helper to extract a string for the tar header. | |
| bool | ExtractTar (const std::string &tar_filename, Subdirectory subdir) |
| Extract the tar with the given filename in the directory where the tar resides. | |
| static bool | ChangeWorkingDirectoryToExecutable (std::string_view exe) |
| Changes the working directory to the path of the give executable. | |
| bool | DoScanWorkingDirectory () |
| Whether we should scan the working directory. | |
| static std::string | GetHomeDir () |
| Gets the home directory of the user. | |
| void | DetermineBasePaths (std::string_view exe) |
| Determine the base (personal dir and game data dir) paths. | |
| void | DeterminePaths (std::string_view exe, bool only_local_path) |
| Acquire the base paths (personal dir and game data dir), fill all other paths (save dir, autosave dir etc) and make the save and scenario directories. | |
| void | SanitizeFilename (std::string &filename) |
| Sanitizes a filename, i.e. | |
| std::unique_ptr< char[]> | ReadFileToMem (const std::string &filename, size_t &lenp, size_t maxsize) |
| Load a file into memory. | |
| static bool | MatchesExtension (std::string_view extension, const std::string &filename) |
| Helper to see whether a given filename matches the extension. | |
| static uint | ScanPath (FileScanner *fs, std::string_view extension, const std::filesystem::path &path, size_t basepath_length, bool recursive) |
| Scan a single directory (and recursively its children) and add any graphics sets that are found. | |
| static uint | ScanTar (FileScanner *fs, std::string_view extension, const TarFileList::value_type &tar) |
| Scan the given tar and add graphics sets when it finds one. | |
Variables | |
| static bool | _do_scan_working_directory = true |
| Whether the working directory should be scanned. | |
| std::string | _config_file |
| Configuration file of OpenTTD. | |
| std::string | _highscore_file |
| The file to store the highscore data in. | |
| static const EnumClassIndexContainer< std::array< std::string_view, to_underlying(Subdirectory::End)>, Subdirectory > | _subdirs |
| Subdirectory names. | |
| EnumClassIndexContainer< std::array< std::string, to_underlying(Searchpath::End)>, Searchpath > | _searchpaths |
| The search paths OpenTTD could search through. | |
| std::vector< Searchpath > | _valid_searchpaths |
| EnumClassIndexContainer< std::array< TarList, to_underlying(Subdirectory::End)>, Subdirectory > | _tar_list |
| List of tar files found in each subdirectory. | |
| EnumClassIndexContainer< std::array< TarFileList, to_underlying(Subdirectory::End)>, Subdirectory > | _tar_filelist |
| List of files within tar files found in each subdirectory. | |
| std::string | _personal_dir |
| custom directory for personal settings, saves, newgrf, etc. | |
Standard in/out file operations.
Definition in file fileio.cpp.
| void AppendPathSeparator | ( | std::string & | buf | ) |
Appends, if necessary, the path separator character to the end of the string.
It does not add the path separator to zero-sized strings.
| buf | String to append the separator to. |
Definition at line 351 of file fileio.cpp.
Referenced by DetermineBasePaths(), DeterminePaths(), DoScanWorkingDirectory(), MidiFile::GetSMFFile(), and FileScanner::Scan().
|
static |
Changes the working directory to the path of the give executable.
For OSX application bundles '.app' is the required extension of the bundle, so when we crop the path to there, when can remove the name of the bundle in the same way we remove the name from the executable name.
| exe | the path to the executable |
true iff the path to the executable was found. Definition at line 681 of file fileio.cpp.
References Debug, and StrEqualsIgnoreCase().
Referenced by DetermineBasePaths().
| void DetermineBasePaths | ( | std::string_view | exe | ) |
Determine the base (personal dir and game data dir) paths.
| exe | the path to the executable |
Definition at line 759 of file fileio.cpp.
References _config_file, _do_scan_working_directory, _searchpaths, AppendPathSeparator(), ApplicationBundleDir, AutodownloadPersonalDir, AutodownloadPersonalDirXdg, BinaryDir, ChangeWorkingDirectoryToExecutable(), Debug, DoScanWorkingDirectory(), FS2OTTD(), GetEnv(), GetHomeDir(), InstallationDir, lengthof, OTTD2FS(), PersonalDir, SharedDir, and WorkingDir.
Referenced by DeterminePaths().
| void DeterminePaths | ( | std::string_view | exe, |
| bool | only_local_path ) |
Acquire the base paths (personal dir and game data dir), fill all other paths (save dir, autosave dir etc) and make the save and scenario directories.
| exe | the path from the current path to the executable |
| only_local_path | Whether we shouldn't fill searchpaths with global folders. |
Definition at line 886 of file fileio.cpp.
References _config_file, _do_scan_working_directory, _favs_file, _highscore_file, _log_file, _personal_dir, _private_file, _searchpaths, _secrets_file, _subdirs, _windows_file, Ai, AiLibrary, AppendPathSeparator(), AutodownloadDir, Autosave, Base, Baseset, BinaryDir, Debug, DetermineBasePaths(), FioCreateDirectory(), FioFindFullPath(), GetEnv(), GetHomeDir(), Gs, GsLibrary, Heightmap, InstallationDir, IsValidSearchPath(), NewGrf, PersonalDir, Save, Scenario, Screenshot, SharedDir, SocialIntegration, and WorkingDir.
Referenced by openttd_main().
| bool DoScanWorkingDirectory | ( | ) |
Whether we should scan the working directory.
It should not be scanned if it's the root or the home directory as in both cases a big data directory can cause huge amounts of unrelated files scanned. Furthermore there are nearly no use cases for the home/root directory to have OpenTTD directories.
Definition at line 717 of file fileio.cpp.
References _searchpaths, AppendPathSeparator(), PersonalDir, and WorkingDir.
Referenced by DetermineBasePaths().
|
static |
Helper to extract a string for the tar header.
We must assume that the tar header contains garbage and is malicious. So, we cannot rely on the string being properly terminated. As such, do not use strlen to determine the actual length (explicitly or implicitly via the std::string constructor), but pass the buffer bounds explicitly.
| buffer | The buffer to read from. |
Definition at line 443 of file fileio.cpp.
References StrMakeValid().
Referenced by TarScanner::AddFile().
| bool ExtractTar | ( | const std::string & | tar_filename, |
| Subdirectory | subdir ) |
Extract the tar with the given filename in the directory where the tar resides.
There must be a directory in the .tar file, but the names of directories in the .tar will be replaced with the tar_filename without .tar extension.
| tar_filename | the name of the tar to extract. |
| subdir | The sub directory the tar is in. |
false on failure. Definition at line 598 of file fileio.cpp.
References _tar_filelist, _tar_list, Debug, FioCreateDirectory(), FioFOpenFileTar(), lengthof, and FileHandle::Open().
Referenced by ClientNetworkContentSocketHandler::AfterDownload().
| bool FileExists | ( | std::string_view | filename | ) |
Test whether the given filename exists.
| filename | the file to test. |
Definition at line 134 of file fileio.cpp.
References OTTD2FS().
Referenced by ConListDirs(), FioFindFullPath(), MidiFile::GetSMFFile(), ScriptInstance::LoadCompatibilityScript(), MakeScreenshotName(), NetworkHTTPInitialize(), and Win32FontCacheFactory::TryLoadFontFromFile().
|
static |
Definition at line 82 of file fileio.cpp.
| bool FioCheckFileExists | ( | std::string_view | filename, |
| Subdirectory | subdir ) |
Check whether the given file exists.
| filename | the file to try for existence. |
| subdir | the subdirectory to look in |
Definition at line 123 of file fileio.cpp.
References FioFOpenFile().
Referenced by ScriptScanner::AddFile(), ConSchedule(), FillGRFDetails(), FindGameManualFilePath(), GetMusicCatEntryData(), GetMusicCatEntryName(), GetTextfile(), GRFLoadConfig(), LoadNewGRF(), LoadTranslations(), TextfileWindow::NavigateToFile(), and SaveLoadWindow::OnTimeout().
| void FioCreateDirectory | ( | const std::string & | name | ) |
Create a directory with the given name If the parent directory does not exist, it will try to create that as well.
| name | the new name of the directory |
Definition at line 319 of file fileio.cpp.
References OTTD2FS().
Referenced by DeterminePaths(), ExtractTar(), and MidiFile::GetSMFFile().
| std::string FioFindDirectory | ( | Subdirectory | subdir | ) |
Definition at line 173 of file fileio.cpp.
| std::string FioFindFullPath | ( | Subdirectory | subdir, |
| std::string_view | filename ) |
Find a path to the filename in one of the search directories.
| subdir | Subdirectory to try. |
| filename | Filename to look for. |
Definition at line 146 of file fileio.cpp.
References _searchpaths, End, and FileExists().
Referenced by VideoDriver_SDL_Base::CreateMainWindow(), DeterminePaths(), GetDefaultTruetypeFontFile(), MidiFile::GetSMFFile(), FreeTypeFontCacheFactory::LoadFont(), DriverFactoryBase::MarkVideoDriverOperational(), DriverFactoryBase::SelectDriverImpl(), and Win32FontCacheFactory::TryLoadFontFromFile().
| std::optional< FileHandle > FioFOpenFile | ( | std::string_view | filename, |
| std::string_view | mode, | ||
| Subdirectory | subdir, | ||
| size_t * | filesize ) |
Opens a OpenTTD file somewhere in a personal or global directory.
| filename | Name of the file to open. | |
| mode | The fopen-mode to open the file. | |
| subdir | Subdirectory to open. | |
| [out] | filesize | Optional output for the size of the file. |
nullptr if the file is not available. Definition at line 246 of file fileio.cpp.
References _tar_filelist, Baseset, End, FioFOpenFile(), FioFOpenFileTar(), NewGrf, None, OldData, and OldGm.
Referenced by _ValidateSignatureFile(), ScenarioScanner::AddFile(), ScriptFileChecksumCreator::AddFile(), CalcGRFMD5Sum(), CalculateHashV1(), GraphicsSet::CheckMD5(), MD5File::CheckMD5(), ConExec(), DebugPrint(), FioCheckFileExists(), FioFOpenFile(), GetFileTitle(), MidiFile::LoadFile(), Squirrel::LoadFile(), TextfileWindow::LoadTextfile(), LoadTownData(), NetworkGameLoop(), IniFile::OpenFile(), RandomAccessFile::RandomAccessFile(), ReadHeightmapBMP(), ReadHeightmapPNG(), ReadRawLanguageStrings(), MidiFile::ReadSMFHeader(), SaveOrLoad(), DriverFactoryBase::SelectDriverImpl(), CrashLog::WriteCrashLog(), and MidiFile::WriteSMF().
|
static |
Definition at line 185 of file fileio.cpp.
|
static |
Opens a file from inside a tar archive.
| entry | The entry to open. | |
| [out] | filesize | If not nullptr, size of the opened file. |
nullptr if the file is not available. EOF after reading the whole file. Definition at line 225 of file fileio.cpp.
References FileHandle::Open().
Referenced by ExtractTar(), and FioFOpenFile().
| std::string FioGetDirectory | ( | Searchpath | sp, |
| Subdirectory | subdir ) |
Definition at line 165 of file fileio.cpp.
| bool FioRemove | ( | const std::string & | filename | ) |
Remove a file.
| filename | Filename to remove. |
Definition at line 331 of file fileio.cpp.
References Debug, and OTTD2FS().
Referenced by ClientNetworkContentSocketHandler::AfterDownload(), ConRemove(), DriverFactoryBase::MarkVideoDriverOperational(), and DriverFactoryBase::SelectDriverImpl().
|
static |
Gets the home directory of the user.
May return an empty string in the unlikely scenario that the home directory cannot be found.
Definition at line 739 of file fileio.cpp.
References GetEnv().
Referenced by DetermineBasePaths(), and DeterminePaths().
|
static |
Checks whether the given search path is a valid search path.
| sp | the search path to check |
Definition at line 77 of file fileio.cpp.
References _searchpaths, and to_underlying().
Referenced by DeterminePaths().
|
static |
Helper to see whether a given filename matches the extension.
| extension | The extension to look for. |
| filename | The filename to look in for the extension. |
Definition at line 1057 of file fileio.cpp.
References StrCompareIgnoreCase().
Referenced by ScanPath(), and ScanTar().
| std::unique_ptr< char[]> ReadFileToMem | ( | const std::string & | filename, |
| size_t & | lenp, | ||
| size_t | maxsize ) |
Load a file into memory.
| filename | Name of the file to load. | |
| [out] | lenp | Length of loaded data. |
| maxsize | Maximum size to load. |
nullptr if loading failed. Definition at line 1032 of file fileio.cpp.
References FileHandle::Open().
Referenced by ReadLanguagePack().
| void SanitizeFilename | ( | std::string & | filename | ) |
Sanitizes a filename, i.e.
removes all illegal characters from it.
| filename | the filename |
Definition at line 1010 of file fileio.cpp.
Referenced by GenerateDefaultSaveName().
|
static |
Scan a single directory (and recursively its children) and add any graphics sets that are found.
| fs | the file scanner to add the files to |
| extension | the extension of files to search for. |
| path | full path we're currently at |
| basepath_length | from where in the path are we 'based' on the search path |
| recursive | whether to recursively search the sub directories |
Definition at line 1076 of file fileio.cpp.
References FileScanner::AddFile(), Debug, FS2OTTD(), MatchesExtension(), and ScanPath().
Referenced by FileScanner::Scan(), FileScanner::Scan(), and ScanPath().
|
static |
Scan the given tar and add graphics sets when it finds one.
| fs | the file scanner to scan for |
| extension | the extension of files to search for. |
| tar | the tar to search in. |
Definition at line 1105 of file fileio.cpp.
References FileScanner::AddFile(), and MatchesExtension().
Referenced by FileScanner::Scan().
|
static |
Simplify filenames from tars.
Replace '/' by #PATHSEPCHAR, and force 'name' to lowercase.
| name | Filename to process. |
Definition at line 363 of file fileio.cpp.
Referenced by TarScanner::AddFile().
|
extern |
Configuration file of OpenTTD.
Definition at line 64 of file settings.cpp.
Referenced by DeleteGRFPresetFromConfig(), DetermineBasePaths(), DeterminePaths(), GetGRFPresetList(), LoadFromConfig(), LoadGRFPresetFromConfig(), openttd_main(), SaveGRFPresetToConfig(), and SaveToConfig().
|
static |
Whether the working directory should be scanned.
Definition at line 33 of file fileio.cpp.
Referenced by DetermineBasePaths(), DeterminePaths(), and FileScanner::Scan().
|
extern |
The file to store the highscore data in.
Definition at line 25 of file highscore.cpp.
Referenced by DeterminePaths(), LoadFromHighScore(), and SaveToHighScore().
| std::string _personal_dir |
custom directory for personal settings, saves, newgrf, etc.
Definition at line 877 of file fileio.cpp.
Referenced by CrashLog::CreateFileName(), DeterminePaths(), and MakeScreenshotName().
| EnumClassIndexContainer<std::array<std::string, to_underlying(Searchpath::End)>, Searchpath> _searchpaths |
The search paths OpenTTD could search through.
At least one of the slots has to be filled with a path. An empty string tells that there is no such path for the current operating system.
Definition at line 65 of file fileio.cpp.
Referenced by DetermineBasePaths(), DeterminePaths(), DoScanWorkingDirectory(), FioFindFullPath(), and IsValidSearchPath().
|
static |
Subdirectory names.
Definition at line 39 of file fileio.cpp.
Referenced by DeterminePaths().
| EnumClassIndexContainer<std::array<TarFileList, to_underlying(Subdirectory::End)>, Subdirectory> _tar_filelist |
List of files within tar files found in each subdirectory.
Definition at line 70 of file fileio.cpp.
Referenced by TarScanner::AddFile(), TarScanner::DoScan(), ExtractTar(), FioFOpenFile(), IsSameScript(), LoadTranslations(), and FileScanner::Scan().
| EnumClassIndexContainer<std::array<TarList, to_underlying(Subdirectory::End)>, Subdirectory> _tar_list |
List of tar files found in each subdirectory.
Definition at line 68 of file fileio.cpp.
Referenced by TarScanner::AddFile(), TarScanner::DoScan(), ExtractTar(), IsSameScript(), and LoadTranslations().
| std::vector<Searchpath> _valid_searchpaths |
Definition at line 66 of file fileio.cpp.