OpenTTD Source 20241224-master-gf74b0cf984
fileio.cpp File Reference

Standard In/Out file operations. More...

#include "stdafx.h"
#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 <charconv>
#include <sys/stat.h>
#include <sstream>
#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 (const std::string &filename, Subdirectory subdir)
 Check whether the given file exists.
 
bool FileExists (const std::string &filename)
 Test whether the given filename exists.
 
std::string FioFindFullPath (Subdirectory subdir, const std::string &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< FileHandleFioFOpenFileSp (const std::string &filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
 
static std::optional< FileHandleFioFOpenFileTar (const TarFileListEntry &entry, size_t *filesize)
 Opens a file from inside a tar archive.
 
std::optional< FileHandleFioFOpenFile (const std::string &filename, const char *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 (const char *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 (const char *exe)
 Determine the base (personal dir and game data dir) paths.
 
void DeterminePaths (const char *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 char *const _subdirs []
 
std::array< std::string, NUM_SEARCHPATHS > _searchpaths
 The search paths OpenTTD could search through.
 
std::vector< Searchpath_valid_searchpaths
 
std::array< TarList, NUM_SUBDIRS_tar_list
 
TarFileList _tar_filelist [NUM_SUBDIRS]
 
std::string _personal_dir
 custom directory for personal settings, saves, newgrf, etc.
 

Detailed Description

Standard In/Out file operations.

Definition in file fileio.cpp.

Function Documentation

◆ AppendPathSeparator()

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.

Parameters
bufstring to append the separator to
Returns
true iff the operation succeeded

Definition at line 346 of file fileio.cpp.

Referenced by DetermineBasePaths(), DeterminePaths(), DoScanWorkingDirectory(), MidiFile::GetSMFFile(), and FileScanner::Scan().

◆ ChangeWorkingDirectoryToExecutable()

static bool ChangeWorkingDirectoryToExecutable ( const char *  exe)
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.

Parameters
exethe path to the executable

Definition at line 672 of file fileio.cpp.

References Debug, and StrEqualsIgnoreCase().

Referenced by DetermineBasePaths().

◆ DetermineBasePaths()

void DetermineBasePaths ( const char *  exe)

◆ DeterminePaths()

void DeterminePaths ( const char *  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.

Parameters
exethe path from the current path to the executable
only_local_pathWhether we shouldn't fill searchpaths with global folders.

Definition at line 878 of file fileio.cpp.

References _config_file, _do_scan_working_directory, _favs_file, _highscore_file, _log_file, _personal_dir, _private_file, _searchpaths, _secrets_file, _windows_file, AI_DIR, AI_LIBRARY_DIR, AppendPathSeparator(), AUTOSAVE_DIR, BASE_DIR, BASESET_DIR, Debug, DetermineBasePaths(), FioCreateDirectory(), FioFindFullPath(), GAME_DIR, GAME_LIBRARY_DIR, GetHomeDir(), HEIGHTMAP_DIR, IsValidSearchPath(), NEWGRF_DIR, SAVE_DIR, SCENARIO_DIR, SCREENSHOT_DIR, SOCIAL_INTEGRATION_DIR, SP_AUTODOWNLOAD_DIR, SP_BINARY_DIR, SP_INSTALLATION_DIR, SP_PERSONAL_DIR, SP_SHARED_DIR, and SP_WORKING_DIR.

Referenced by openttd_main().

◆ DoScanWorkingDirectory()

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.

Returns
true if it should be scanned.

Definition at line 708 of file fileio.cpp.

References _searchpaths, AppendPathSeparator(), SP_PERSONAL_DIR, and SP_WORKING_DIR.

Referenced by DetermineBasePaths().

◆ ExtractString()

static std::string ExtractString ( std::span< char >  buffer)
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 implictly via the std::string constructor), but pass the buffer bounds explicitly.

Parameters
bufferThe buffer to read from.
Returns
The string data.

Definition at line 433 of file fileio.cpp.

References StrMakeValid().

Referenced by TarScanner::AddFile().

◆ ExtractTar()

bool ExtractTar ( const std::string &  tar_filename,
Subdirectory  subdir 
)

Extract the tar with the given filename in the directory where the tar resides.

Parameters
tar_filenamethe name of the tar to extract.
subdirThe sub directory the tar is in.
Returns
false on failure.

Definition at line 590 of file fileio.cpp.

References Debug, FioCreateDirectory(), FioFOpenFileTar(), lengthof, and FileHandle::Open().

Referenced by ClientNetworkContentSocketHandler::AfterDownload().

◆ FileExists()

bool FileExists ( const std::string &  filename)

Test whether the given filename exists.

Parameters
filenamethe file to test.
Returns
true if and only if the file exists.

Definition at line 132 of file fileio.cpp.

References OTTD2FS().

Referenced by DEF_CONSOLE_CMD(), FioFindFullPath(), MidiFile::GetSMFFile(), ScriptInstance::LoadCompatibilityScripts(), MakeScreenshotName(), and NetworkHTTPInitialize().

◆ FillValidSearchPaths()

static void FillValidSearchPaths ( bool  only_local_path)
static

Definition at line 80 of file fileio.cpp.

◆ FioCheckFileExists()

bool FioCheckFileExists ( const std::string &  filename,
Subdirectory  subdir 
)

Check whether the given file exists.

Parameters
filenamethe file to try for existence.
subdirthe subdirectory to look in
Returns
true if and only if the file can be opened

Definition at line 121 of file fileio.cpp.

References FioFOpenFile().

Referenced by ScriptScanner::AddFile(), FillGRFDetails(), FindGameManualFilePath(), GetMusicCatEntryData(), GetMusicCatEntryName(), GetTextfile(), GRFLoadConfig(), LoadNewGRF(), LoadTranslations(), TextfileWindow::NavigateToFile(), and SaveLoadWindow::OnTimeout().

◆ FioCreateDirectory()

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.

Parameters
namethe new name of the directory

Definition at line 316 of file fileio.cpp.

References OTTD2FS().

Referenced by DeterminePaths(), ExtractTar(), and MidiFile::GetSMFFile().

◆ FioFindDirectory()

std::string FioFindDirectory ( Subdirectory  subdir)

Definition at line 171 of file fileio.cpp.

◆ FioFindFullPath()

std::string FioFindFullPath ( Subdirectory  subdir,
const std::string &  filename 
)

Find a path to the filename in one of the search directories.

Parameters
subdirSubdirectory to try.
filenameFilename to look for.
Returns
String containing the path if the path was found, else an empty string.

Definition at line 144 of file fileio.cpp.

References _searchpaths, FileExists(), and NUM_SUBDIRS.

Referenced by VideoDriver_SDL_Base::CreateMainWindow(), DeterminePaths(), GetDefaultTruetypeFontFile(), MidiFile::GetSMFFile(), LoadFreeTypeFont(), DriverFactoryBase::MarkVideoDriverOperational(), and DriverFactoryBase::SelectDriverImpl().

◆ FioFOpenFile()

std::optional< FileHandle > FioFOpenFile ( const std::string &  filename,
const char *  mode,
Subdirectory  subdir,
size_t *  filesize 
)

◆ FioFOpenFileSp()

static std::optional< FileHandle > FioFOpenFileSp ( const std::string &  filename,
const char *  mode,
Searchpath  sp,
Subdirectory  subdir,
size_t *  filesize 
)
static

Definition at line 183 of file fileio.cpp.

◆ FioFOpenFileTar()

static std::optional< FileHandle > FioFOpenFileTar ( const TarFileListEntry entry,
size_t *  filesize 
)
static

Opens a file from inside a tar archive.

Parameters
entryThe entry to open.
[out]filesizeIf not nullptr, size of the opened file.
Returns
File handle of the opened file, or nullptr if the file is not available.
Note
The file is read from within the tar file, and may not return EOF after reading the whole file.

Definition at line 223 of file fileio.cpp.

References FileHandle::Open().

Referenced by ExtractTar(), and FioFOpenFile().

◆ FioGetDirectory()

std::string FioGetDirectory ( Searchpath  sp,
Subdirectory  subdir 
)

Definition at line 163 of file fileio.cpp.

◆ FioRemove()

bool FioRemove ( const std::string &  filename)

Remove a file.

Parameters
filenameFilename to remove.
Returns
true iff the file was removed.

Definition at line 328 of file fileio.cpp.

References Debug, and OTTD2FS().

Referenced by ClientNetworkContentSocketHandler::AfterDownload(), FiosDelete(), DriverFactoryBase::MarkVideoDriverOperational(), and DriverFactoryBase::SelectDriverImpl().

◆ GetHomeDir()

static std::string GetHomeDir ( )
static

Gets the home directory of the user.

May return an empty string in the unlikely scenario that the home directory cannot be found.

Returns
User's home directory

Definition at line 730 of file fileio.cpp.

Referenced by DetermineBasePaths(), and DeterminePaths().

◆ IsValidSearchPath()

static bool IsValidSearchPath ( Searchpath  sp)
static

Checks whether the given search path is a valid search path.

Parameters
spthe search path to check
Returns
true if the search path is valid

Definition at line 75 of file fileio.cpp.

References _searchpaths.

Referenced by DeterminePaths().

◆ MatchesExtension()

static bool MatchesExtension ( std::string_view  extension,
const std::string &  filename 
)
static

Helper to see whether a given filename matches the extension.

Parameters
extensionThe extension to look for.
filenameThe filename to look in for the extension.
Returns
True iff the extension is nullptr, or the filename ends with it.

Definition at line 1050 of file fileio.cpp.

References StrCompareIgnoreCase().

Referenced by ScanPath(), and ScanTar().

◆ ReadFileToMem()

std::unique_ptr< char[]> ReadFileToMem ( const std::string &  filename,
size_t &  lenp,
size_t  maxsize 
)

Load a file into memory.

Parameters
filenameName of the file to load.
[out]lenpLength of loaded data.
maxsizeMaximum size to load.
Returns
Pointer to new memory containing the loaded data, or nullptr if loading failed.
Note
If maxsize less than the length of the file, loading fails.

Definition at line 1025 of file fileio.cpp.

References FileHandle::Open().

Referenced by ReadLanguagePack().

◆ SanitizeFilename()

void SanitizeFilename ( std::string &  filename)

Sanitizes a filename, i.e.

removes all illegal characters from it.

Parameters
filenamethe filename

Definition at line 1003 of file fileio.cpp.

Referenced by GenerateDefaultSaveName().

◆ ScanPath()

static uint ScanPath ( FileScanner fs,
std::string_view  extension,
const std::filesystem::path &  path,
size_t  basepath_length,
bool  recursive 
)
static

Scan a single directory (and recursively its children) and add any graphics sets that are found.

Parameters
fsthe file scanner to add the files to
extensionthe extension of files to search for.
pathfull path we're currently at
basepath_lengthfrom where in the path are we 'based' on the search path
recursivewhether to recursively search the sub directories

Definition at line 1068 of file fileio.cpp.

References FileScanner::AddFile(), Debug, FS2OTTD(), MatchesExtension(), and ScanPath().

Referenced by FileScanner::Scan(), FileScanner::Scan(), and ScanPath().

◆ ScanTar()

static uint ScanTar ( FileScanner fs,
std::string_view  extension,
const TarFileList::value_type &  tar 
)
static

Scan the given tar and add graphics sets when it finds one.

Parameters
fsthe file scanner to scan for
extensionthe extension of files to search for.
tarthe tar to search in.

Definition at line 1096 of file fileio.cpp.

References FileScanner::AddFile(), and MatchesExtension().

Referenced by FileScanner::Scan().

◆ SimplifyFileName()

static void SimplifyFileName ( std::string &  name)
static

Simplify filenames from tars.

Replace '/' by PATHSEPCHAR, and force 'name' to lowercase.

Parameters
nameFilename to process.

Definition at line 358 of file fileio.cpp.

Referenced by TarScanner::AddFile().

Variable Documentation

◆ _config_file

std::string _config_file
extern

Configuration file of OpenTTD.

Definition at line 60 of file settings.cpp.

Referenced by DetermineBasePaths(), and DeterminePaths().

◆ _do_scan_working_directory

bool _do_scan_working_directory = true
static

Whether the working directory should be scanned.

Definition at line 34 of file fileio.cpp.

Referenced by DetermineBasePaths(), DeterminePaths(), and FileScanner::Scan().

◆ _highscore_file

std::string _highscore_file
extern

The file to store the highscore data in.

Definition at line 24 of file highscore.cpp.

Referenced by DeterminePaths(), LoadFromHighScore(), and SaveToHighScore().

◆ _personal_dir

std::string _personal_dir

custom directory for personal settings, saves, newgrf, etc.

Definition at line 869 of file fileio.cpp.

Referenced by CrashLog::CreateFileName(), DeterminePaths(), and MakeScreenshotName().

◆ _searchpaths

std::array<std::string, NUM_SEARCHPATHS> _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().

◆ _subdirs

const char* const _subdirs[]
static
Initial value:
= {
"",
"save" PATHSEP,
"save" PATHSEP "autosave" PATHSEP,
"scenario" PATHSEP,
"scenario" PATHSEP "heightmap" PATHSEP,
"gm" PATHSEP,
"data" PATHSEP,
"baseset" PATHSEP,
"newgrf" PATHSEP,
"lang" PATHSEP,
"ai" PATHSEP,
"ai" PATHSEP "library" PATHSEP,
"game" PATHSEP,
"game" PATHSEP "library" PATHSEP,
"screenshot" PATHSEP,
"social_integration" PATHSEP,
}

Definition at line 39 of file fileio.cpp.

◆ _tar_filelist

TarFileList _tar_filelist[NUM_SUBDIRS]

Definition at line 68 of file fileio.cpp.

◆ _tar_list

std::array<TarList, NUM_SUBDIRS> _tar_list

Definition at line 67 of file fileio.cpp.

◆ _valid_searchpaths

std::vector<Searchpath> _valid_searchpaths

Definition at line 66 of file fileio.cpp.