2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/** @file fileio.cpp Standard In/Out file operations */
|
2007-03-01 01:24:44 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2008-08-31 10:50:05 +00:00
|
|
|
#include "fileio_func.h"
|
2021-04-14 15:20:39 +00:00
|
|
|
#include "spriteloader/spriteloader.hpp"
|
2007-02-08 23:46:25 +00:00
|
|
|
#include "debug.h"
|
2007-03-12 15:25:33 +00:00
|
|
|
#include "fios.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-06-24 09:05:24 +00:00
|
|
|
#include "tar_type.h"
|
2018-12-09 01:28:14 +00:00
|
|
|
#ifdef _WIN32
|
2007-06-17 15:48:57 +00:00
|
|
|
#include <windows.h>
|
2011-05-28 03:23:34 +00:00
|
|
|
# define access _taccess
|
2010-02-17 16:45:55 +00:00
|
|
|
#elif defined(__HAIKU__)
|
|
|
|
#include <Path.h>
|
|
|
|
#include <storage/FindDirectory.h>
|
2007-06-17 15:48:57 +00:00
|
|
|
#else
|
2010-01-16 22:01:33 +00:00
|
|
|
#include <unistd.h>
|
2007-03-18 18:02:24 +00:00
|
|
|
#include <pwd.h>
|
2007-03-12 15:25:33 +00:00
|
|
|
#endif
|
2007-09-15 15:21:17 +00:00
|
|
|
#include <sys/stat.h>
|
2020-06-22 12:21:11 +00:00
|
|
|
#include <sstream>
|
2023-06-02 16:13:51 +00:00
|
|
|
#include <filesystem>
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2010-07-16 19:32:39 +00:00
|
|
|
/** Whether the working directory should be scanned. */
|
|
|
|
static bool _do_scan_working_directory = true;
|
|
|
|
|
2020-12-06 20:11:42 +00:00
|
|
|
extern std::string _config_file;
|
|
|
|
extern std::string _highscore_file;
|
2010-07-19 17:11:09 +00:00
|
|
|
|
2011-08-24 13:33:49 +00:00
|
|
|
static const char * const _subdirs[] = {
|
2007-06-17 15:48:57 +00:00
|
|
|
"",
|
|
|
|
"save" PATHSEP,
|
|
|
|
"save" PATHSEP "autosave" PATHSEP,
|
|
|
|
"scenario" PATHSEP,
|
|
|
|
"scenario" PATHSEP "heightmap" PATHSEP,
|
|
|
|
"gm" PATHSEP,
|
|
|
|
"data" PATHSEP,
|
2011-11-14 21:34:27 +00:00
|
|
|
"baseset" PATHSEP,
|
|
|
|
"newgrf" PATHSEP,
|
2009-01-12 17:11:45 +00:00
|
|
|
"lang" PATHSEP,
|
|
|
|
"ai" PATHSEP,
|
2009-01-14 15:20:06 +00:00
|
|
|
"ai" PATHSEP "library" PATHSEP,
|
2011-12-19 20:54:37 +00:00
|
|
|
"game" PATHSEP,
|
2011-12-19 20:56:59 +00:00
|
|
|
"game" PATHSEP "library" PATHSEP,
|
2012-12-09 16:52:43 +00:00
|
|
|
"screenshot" PATHSEP,
|
2007-06-17 15:48:57 +00:00
|
|
|
};
|
2020-12-27 10:44:22 +00:00
|
|
|
static_assert(lengthof(_subdirs) == NUM_SUBDIRS);
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2020-12-06 20:11:43 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
|
2021-04-30 13:41:58 +00:00
|
|
|
std::vector<Searchpath> _valid_searchpaths;
|
2020-12-06 20:11:50 +00:00
|
|
|
std::array<TarList, NUM_SUBDIRS> _tar_list;
|
2011-11-14 21:28:43 +00:00
|
|
|
TarFileList _tar_filelist[NUM_SUBDIRS];
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
typedef std::map<std::string, std::string> TarLinkList;
|
2011-11-14 21:28:43 +00:00
|
|
|
static TarLinkList _tar_linklist[NUM_SUBDIRS]; ///< List of directory links
|
2008-07-13 16:09:00 +00:00
|
|
|
|
2023-05-30 20:35:00 +00:00
|
|
|
extern bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb);
|
2023-01-29 08:28:24 +00:00
|
|
|
|
2020-12-06 20:11:43 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the given search path is a valid search path
|
|
|
|
* @param sp the search path to check
|
|
|
|
* @return true if the search path is valid
|
|
|
|
*/
|
2021-04-30 13:41:58 +00:00
|
|
|
static bool IsValidSearchPath(Searchpath sp)
|
2020-12-06 20:11:43 +00:00
|
|
|
{
|
|
|
|
return sp < _searchpaths.size() && !_searchpaths[sp].empty();
|
|
|
|
}
|
|
|
|
|
2021-06-10 21:13:34 +00:00
|
|
|
static void FillValidSearchPaths(bool only_local_path)
|
2021-04-30 13:41:58 +00:00
|
|
|
{
|
|
|
|
_valid_searchpaths.clear();
|
2023-10-09 21:38:13 +00:00
|
|
|
|
|
|
|
std::set<std::string> seen{};
|
2021-04-30 13:41:58 +00:00
|
|
|
for (Searchpath sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) {
|
2021-06-10 21:13:34 +00:00
|
|
|
if (only_local_path) {
|
|
|
|
switch (sp) {
|
|
|
|
case SP_WORKING_DIR: // Can be influence by "-c" option.
|
|
|
|
case SP_BINARY_DIR: // Most likely contains all the language files.
|
|
|
|
case SP_AUTODOWNLOAD_DIR: // Otherwise we cannot download in-game content.
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 21:38:13 +00:00
|
|
|
if (IsValidSearchPath(sp)) {
|
|
|
|
if (seen.count(_searchpaths[sp]) != 0) continue;
|
|
|
|
seen.insert(_searchpaths[sp]);
|
|
|
|
_valid_searchpaths.emplace_back(sp);
|
|
|
|
}
|
2021-04-30 13:41:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-12 15:40:12 +00:00
|
|
|
/**
|
|
|
|
* Check whether the given file exists
|
2011-08-01 19:36:11 +00:00
|
|
|
* @param filename the file to try for existence.
|
2007-06-17 15:48:57 +00:00
|
|
|
* @param subdir the subdirectory to look in
|
2007-03-12 15:40:12 +00:00
|
|
|
* @return true if and only if the file can be opened
|
|
|
|
*/
|
2020-12-06 20:11:45 +00:00
|
|
|
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
|
2007-03-12 15:40:12 +00:00
|
|
|
{
|
2020-12-06 20:11:47 +00:00
|
|
|
FILE *f = FioFOpenFile(filename, "rb", subdir);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f == nullptr) return false;
|
2007-03-12 15:40:12 +00:00
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
FioFCloseFile(f);
|
2007-03-12 15:40:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-27 21:42:51 +00:00
|
|
|
/**
|
2011-08-01 19:36:11 +00:00
|
|
|
* Test whether the given filename exists.
|
2011-05-27 21:42:51 +00:00
|
|
|
* @param filename the file to test.
|
|
|
|
* @return true if and only if the file exists.
|
|
|
|
*/
|
2020-12-06 20:11:45 +00:00
|
|
|
bool FileExists(const std::string &filename)
|
2011-05-27 21:42:51 +00:00
|
|
|
{
|
2021-02-21 16:03:19 +00:00
|
|
|
return access(OTTD2FS(filename).c_str(), 0) == 0;
|
2011-05-27 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
/**
|
|
|
|
* Close a file in a safe way.
|
|
|
|
*/
|
|
|
|
void FioFCloseFile(FILE *f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2011-07-16 18:34:04 +00:00
|
|
|
/**
|
|
|
|
* Find a path to the filename in one of the search directories.
|
|
|
|
* @param subdir Subdirectory to try.
|
|
|
|
* @param filename Filename to look for.
|
2020-12-06 20:11:44 +00:00
|
|
|
* @return String containing the path if the path was found, else an empty string.
|
2011-07-16 18:34:04 +00:00
|
|
|
*/
|
2023-04-18 20:21:17 +00:00
|
|
|
std::string FioFindFullPath(Subdirectory subdir, const std::string &filename)
|
2007-06-17 15:48:57 +00:00
|
|
|
{
|
|
|
|
assert(subdir < NUM_SUBDIRS);
|
|
|
|
|
2021-04-30 13:41:58 +00:00
|
|
|
for (Searchpath sp : _valid_searchpaths) {
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string buf = FioGetDirectory(sp, subdir);
|
|
|
|
buf += filename;
|
2020-12-06 20:11:45 +00:00
|
|
|
if (FileExists(buf)) return buf;
|
2018-12-09 01:28:14 +00:00
|
|
|
#if !defined(_WIN32)
|
2009-12-23 09:08:52 +00:00
|
|
|
/* Be, as opening files, aware that sometimes the filename
|
|
|
|
* might be in uppercase when it is in lowercase on the
|
2011-08-01 19:36:11 +00:00
|
|
|
* disk. Of course Windows doesn't care about casing. */
|
2020-12-06 20:11:45 +00:00
|
|
|
if (strtolower(buf, _searchpaths[sp].size() - 1) && FileExists(buf)) return buf;
|
2009-12-23 09:08:52 +00:00
|
|
|
#endif
|
2007-06-17 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 20:11:44 +00:00
|
|
|
return {};
|
2007-06-17 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string FioGetDirectory(Searchpath sp, Subdirectory subdir)
|
2007-06-17 15:48:57 +00:00
|
|
|
{
|
|
|
|
assert(subdir < NUM_SUBDIRS);
|
|
|
|
assert(sp < NUM_SEARCHPATHS);
|
|
|
|
|
2020-12-06 20:11:44 +00:00
|
|
|
return _searchpaths[sp] + _subdirs[subdir];
|
2007-06-17 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string FioFindDirectory(Subdirectory subdir)
|
2007-06-17 15:48:57 +00:00
|
|
|
{
|
|
|
|
/* Find and return the first valid directory */
|
2021-04-30 13:41:58 +00:00
|
|
|
for (Searchpath sp : _valid_searchpaths) {
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string ret = FioGetDirectory(sp, subdir);
|
2020-12-06 20:11:45 +00:00
|
|
|
if (FileExists(ret)) return ret;
|
2007-06-17 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Could not find the directory, fall back to a base path */
|
2020-12-06 20:11:44 +00:00
|
|
|
return _personal_dir;
|
2007-06-17 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 20:11:47 +00:00
|
|
|
static FILE *FioFOpenFileSp(const std::string &filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
|
2007-06-17 15:48:57 +00:00
|
|
|
{
|
2021-02-21 19:04:24 +00:00
|
|
|
#if defined(_WIN32)
|
2007-06-17 15:48:57 +00:00
|
|
|
/* fopen is implemented as a define with ellipses for
|
|
|
|
* Unicode support (prepend an L). As we are not sending
|
|
|
|
* a string, but a variable, it 'renames' the variable,
|
|
|
|
* so make that variable to makes it compile happily */
|
|
|
|
wchar_t Lmode[5];
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode, lengthof(Lmode));
|
|
|
|
#endif
|
2019-04-10 21:07:06 +00:00
|
|
|
FILE *f = nullptr;
|
2020-12-06 20:11:43 +00:00
|
|
|
std::string buf;
|
2005-12-10 11:16:45 +00:00
|
|
|
|
2007-06-17 20:09:05 +00:00
|
|
|
if (subdir == NO_DIRECTORY) {
|
2020-12-06 20:11:43 +00:00
|
|
|
buf = filename;
|
2007-06-08 21:16:17 +00:00
|
|
|
} else {
|
2020-12-06 20:11:43 +00:00
|
|
|
buf = _searchpaths[sp] + _subdirs[subdir] + filename;
|
2007-04-04 12:03:10 +00:00
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
2018-12-09 01:28:14 +00:00
|
|
|
#if defined(_WIN32)
|
2021-02-21 16:03:19 +00:00
|
|
|
if (mode[0] == 'r' && GetFileAttributes(OTTD2FS(buf).c_str()) == INVALID_FILE_ATTRIBUTES) return nullptr;
|
2007-09-24 19:15:42 +00:00
|
|
|
#endif
|
|
|
|
|
2020-12-06 20:11:43 +00:00
|
|
|
f = fopen(buf.c_str(), mode);
|
2018-12-09 01:28:14 +00:00
|
|
|
#if !defined(_WIN32)
|
2020-12-06 20:11:43 +00:00
|
|
|
if (f == nullptr && strtolower(buf, subdir == NO_DIRECTORY ? 0 : _searchpaths[sp].size() - 1) ) {
|
|
|
|
f = fopen(buf.c_str(), mode);
|
2005-12-10 11:16:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f != nullptr && filesize != nullptr) {
|
2007-09-13 18:46:29 +00:00
|
|
|
/* Find the size of the file */
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
*filesize = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
}
|
2007-06-17 15:48:57 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2011-08-01 19:36:11 +00:00
|
|
|
/**
|
|
|
|
* Opens a file from inside a tar archive.
|
|
|
|
* @param entry The entry to open.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param[out] filesize If not \c nullptr, size of the opened file.
|
|
|
|
* @return File handle of the opened file, or \c nullptr if the file is not available.
|
2011-08-01 19:36:11 +00:00
|
|
|
* @note The file is read from within the tar file, and may not return \c EOF after reading the whole file.
|
|
|
|
*/
|
2020-12-06 20:11:47 +00:00
|
|
|
FILE *FioFOpenFileTar(const TarFileListEntry &entry, size_t *filesize)
|
2007-09-14 22:25:00 +00:00
|
|
|
{
|
2020-12-06 20:11:50 +00:00
|
|
|
FILE *f = fopen(entry.tar_filename.c_str(), "rb");
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f == nullptr) return f;
|
2007-09-14 22:25:00 +00:00
|
|
|
|
2020-12-06 20:11:47 +00:00
|
|
|
if (fseek(f, entry.position, SEEK_SET) < 0) {
|
2013-11-23 13:15:07 +00:00
|
|
|
fclose(f);
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2013-11-23 13:15:07 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 20:11:47 +00:00
|
|
|
if (filesize != nullptr) *filesize = entry.size;
|
2007-09-16 18:10:52 +00:00
|
|
|
return f;
|
2007-09-14 22:25:00 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 19:36:11 +00:00
|
|
|
/**
|
|
|
|
* Opens a OpenTTD file somewhere in a personal or global directory.
|
|
|
|
* @param filename Name of the file to open.
|
|
|
|
* @param subdir Subdirectory to open.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return File handle of the opened file, or \c nullptr if the file is not available.
|
2011-08-01 19:36:11 +00:00
|
|
|
*/
|
2020-12-06 20:11:47 +00:00
|
|
|
FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
|
2007-06-17 15:48:57 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
FILE *f = nullptr;
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2007-06-17 20:09:05 +00:00
|
|
|
assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2021-04-30 13:41:58 +00:00
|
|
|
for (Searchpath sp : _valid_searchpaths) {
|
2007-09-13 18:50:42 +00:00
|
|
|
f = FioFOpenFileSp(filename, mode, sp, subdir, filesize);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f != nullptr || subdir == NO_DIRECTORY) break;
|
2007-06-17 15:48:57 +00:00
|
|
|
}
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2007-09-14 22:25:00 +00:00
|
|
|
/* We can only use .tar in case of data-dir, and read-mode */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f == nullptr && mode[0] == 'r' && subdir != NO_DIRECTORY) {
|
2007-09-16 18:10:52 +00:00
|
|
|
/* Filenames in tars are always forced to be lowercase */
|
2020-12-06 20:11:47 +00:00
|
|
|
std::string resolved_name = filename;
|
2008-07-13 16:09:00 +00:00
|
|
|
strtolower(resolved_name);
|
|
|
|
|
2020-06-22 12:21:11 +00:00
|
|
|
/* Resolve ".." */
|
|
|
|
std::istringstream ss(resolved_name);
|
|
|
|
std::vector<std::string> tokens;
|
|
|
|
std::string token;
|
|
|
|
while (std::getline(ss, token, PATHSEPCHAR)) {
|
|
|
|
if (token == "..") {
|
|
|
|
if (tokens.size() < 2) return nullptr;
|
|
|
|
tokens.pop_back();
|
2021-03-25 20:29:18 +00:00
|
|
|
} else if (token == ".") {
|
|
|
|
/* Do nothing. "." means current folder, but you can create tar files with "." in the path.
|
|
|
|
* This confuses our file resolver. So, act like this folder doesn't exist. */
|
2020-06-22 12:21:11 +00:00
|
|
|
} else {
|
|
|
|
tokens.push_back(token);
|
|
|
|
}
|
|
|
|
}
|
2020-12-06 20:11:47 +00:00
|
|
|
|
|
|
|
resolved_name.clear();
|
2020-06-22 12:21:11 +00:00
|
|
|
bool first = true;
|
|
|
|
for (const std::string &token : tokens) {
|
|
|
|
if (!first) {
|
2020-12-06 20:11:47 +00:00
|
|
|
resolved_name += PATHSEP;
|
2020-06-22 12:21:11 +00:00
|
|
|
}
|
2020-12-06 20:11:47 +00:00
|
|
|
resolved_name += token;
|
2020-06-22 12:21:11 +00:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
/* Resolve ONE directory link */
|
2023-05-07 11:09:54 +00:00
|
|
|
for (const auto &link : _tar_linklist[subdir]) {
|
|
|
|
const std::string &src = link.first;
|
2008-08-13 14:52:54 +00:00
|
|
|
size_t len = src.length();
|
2020-12-06 20:11:47 +00:00
|
|
|
if (resolved_name.length() >= len && resolved_name[len - 1] == PATHSEPCHAR && src.compare(0, len, resolved_name, 0, len) == 0) {
|
2008-07-13 16:09:00 +00:00
|
|
|
/* Apply link */
|
2023-05-07 11:09:54 +00:00
|
|
|
resolved_name.replace(0, len, link.second);
|
2008-07-13 16:09:00 +00:00
|
|
|
break; // Only resolve one level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-14 21:28:43 +00:00
|
|
|
TarFileList::iterator it = _tar_filelist[subdir].find(resolved_name);
|
|
|
|
if (it != _tar_filelist[subdir].end()) {
|
2020-12-06 20:11:47 +00:00
|
|
|
f = FioFOpenFileTar(it->second, filesize);
|
2007-09-14 22:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
2007-12-04 18:42:08 +00:00
|
|
|
/* Sometimes a full path is given. To support
|
|
|
|
* the 'subdirectory' must be 'removed'. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f == nullptr && subdir != NO_DIRECTORY) {
|
2011-11-14 21:34:27 +00:00
|
|
|
switch (subdir) {
|
|
|
|
case BASESET_DIR:
|
|
|
|
f = FioFOpenFile(filename, mode, OLD_GM_DIR, filesize);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f != nullptr) break;
|
2017-08-13 18:38:42 +00:00
|
|
|
FALLTHROUGH;
|
2011-11-14 21:34:27 +00:00
|
|
|
case NEWGRF_DIR:
|
|
|
|
f = FioFOpenFile(filename, mode, OLD_DATA_DIR, filesize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
f = FioFOpenFile(filename, mode, NO_DIRECTORY, filesize);
|
|
|
|
break;
|
|
|
|
}
|
2007-12-04 18:42:08 +00:00
|
|
|
}
|
|
|
|
|
2005-12-10 11:16:45 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2007-03-12 15:25:33 +00:00
|
|
|
/**
|
|
|
|
* Create a directory with the given name
|
2020-02-15 12:30:16 +00:00
|
|
|
* If the parent directory does not exist, it will try to create that as well.
|
2007-03-12 15:25:33 +00:00
|
|
|
* @param name the new name of the directory
|
|
|
|
*/
|
2020-12-06 20:11:45 +00:00
|
|
|
void FioCreateDirectory(const std::string &name)
|
2007-03-12 15:25:33 +00:00
|
|
|
{
|
2020-12-06 20:11:45 +00:00
|
|
|
auto p = name.find_last_of(PATHSEPCHAR);
|
|
|
|
if (p != std::string::npos) {
|
|
|
|
std::string dirname = name.substr(0, p);
|
|
|
|
DIR *dir = ttd_opendir(dirname.c_str());
|
2020-02-15 12:30:16 +00:00
|
|
|
if (dir == nullptr) {
|
|
|
|
FioCreateDirectory(dirname); // Try creating the parent directory, if we couldn't open it
|
|
|
|
} else {
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-23 13:15:07 +00:00
|
|
|
/* Ignore directory creation errors; they'll surface later on, and most
|
|
|
|
* of the time they are 'directory already exists' errors anyhow. */
|
2018-12-09 01:28:14 +00:00
|
|
|
#if defined(_WIN32)
|
2021-02-21 16:03:19 +00:00
|
|
|
CreateDirectory(OTTD2FS(name).c_str(), nullptr);
|
2007-03-12 15:25:33 +00:00
|
|
|
#else
|
2021-02-21 16:03:19 +00:00
|
|
|
mkdir(OTTD2FS(name).c_str(), 0755);
|
2007-03-12 15:25:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends, if necessary, the path separator character to the end of the string.
|
|
|
|
* It does not add the path separator to zero-sized strings.
|
2014-04-23 21:23:21 +00:00
|
|
|
* @param buf string to append the separator to
|
2010-05-10 09:49:02 +00:00
|
|
|
* @return true iff the operation succeeded
|
2007-03-12 15:25:33 +00:00
|
|
|
*/
|
2020-12-06 20:11:44 +00:00
|
|
|
void AppendPathSeparator(std::string &buf)
|
2007-03-12 15:25:33 +00:00
|
|
|
{
|
2020-12-06 20:11:44 +00:00
|
|
|
if (buf.empty()) return;
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2020-12-06 20:11:44 +00:00
|
|
|
if (buf.back() != PATHSEPCHAR) buf.push_back(PATHSEPCHAR);
|
2007-03-12 15:25:33 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 21:28:43 +00:00
|
|
|
static void TarAddLink(const std::string &srcParam, const std::string &destParam, Subdirectory subdir)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-01-16 14:56:33 +00:00
|
|
|
std::string src = srcParam;
|
|
|
|
std::string dest = destParam;
|
|
|
|
/* Tar internals assume lowercase */
|
|
|
|
std::transform(src.begin(), src.end(), src.begin(), tolower);
|
|
|
|
std::transform(dest.begin(), dest.end(), dest.begin(), tolower);
|
|
|
|
|
2011-11-14 21:28:43 +00:00
|
|
|
TarFileList::iterator dest_file = _tar_filelist[subdir].find(dest);
|
|
|
|
if (dest_file != _tar_filelist[subdir].end()) {
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Link to file. Process the link like the destination file. */
|
2011-11-14 21:28:43 +00:00
|
|
|
_tar_filelist[subdir].insert(TarFileList::value_type(src, dest_file->second));
|
2009-01-12 17:11:45 +00:00
|
|
|
} else {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Destination file not found. Assume 'link to directory'
|
|
|
|
* Append PATHSEPCHAR to 'src' and 'dest' if needed */
|
2009-01-12 17:11:45 +00:00
|
|
|
const std::string src_path = ((*src.rbegin() == PATHSEPCHAR) ? src : src + PATHSEPCHAR);
|
|
|
|
const std::string dst_path = (dest.length() == 0 ? "" : ((*dest.rbegin() == PATHSEPCHAR) ? dest : dest + PATHSEPCHAR));
|
2011-11-14 21:28:43 +00:00
|
|
|
_tar_linklist[subdir].insert(TarLinkList::value_type(src_path, dst_path));
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
/**
|
|
|
|
* Simplify filenames from tars.
|
2011-08-01 19:36:11 +00:00
|
|
|
* Replace '/' by #PATHSEPCHAR, and force 'name' to lowercase.
|
2008-07-13 16:09:00 +00:00
|
|
|
* @param name Filename to process.
|
|
|
|
*/
|
2023-06-02 16:13:51 +00:00
|
|
|
static void SimplifyFileName(std::string &name)
|
2008-07-13 16:09:00 +00:00
|
|
|
{
|
2023-06-02 16:13:51 +00:00
|
|
|
for (char &c : name) {
|
|
|
|
/* Force lowercase */
|
|
|
|
c = std::tolower(c);
|
2008-07-13 16:09:00 +00:00
|
|
|
#if (PATHSEPCHAR != '/')
|
2023-06-02 16:13:51 +00:00
|
|
|
/* Tar-files always have '/' path-separator, but we want our PATHSEPCHAR */
|
|
|
|
if (c == '/') c = PATHSEPCHAR;
|
2008-07-13 16:09:00 +00:00
|
|
|
#endif
|
2023-06-02 16:13:51 +00:00
|
|
|
}
|
2008-07-13 16:09:00 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 21:28:43 +00:00
|
|
|
/**
|
|
|
|
* Perform the scanning of a particular subdirectory.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param sd The subdirectory to scan.
|
2011-11-14 21:28:43 +00:00
|
|
|
* @return The number of found tar files.
|
|
|
|
*/
|
|
|
|
uint TarScanner::DoScan(Subdirectory sd)
|
2010-11-18 22:26:29 +00:00
|
|
|
{
|
2011-11-14 21:28:43 +00:00
|
|
|
_tar_filelist[sd].clear();
|
|
|
|
_tar_list[sd].clear();
|
2011-11-14 21:34:27 +00:00
|
|
|
uint num = this->Scan(".tar", sd, false);
|
|
|
|
if (sd == BASESET_DIR || sd == NEWGRF_DIR) num += this->Scan(".tar", OLD_DATA_DIR, false);
|
|
|
|
return num;
|
2011-11-14 21:28:43 +00:00
|
|
|
}
|
2010-11-18 22:26:29 +00:00
|
|
|
|
2011-11-14 21:30:37 +00:00
|
|
|
/* static */ uint TarScanner::DoScan(TarScanner::Mode mode)
|
2011-11-14 21:28:43 +00:00
|
|
|
{
|
2022-12-15 18:25:06 +00:00
|
|
|
Debug(misc, 2, "Scanning for tars");
|
2010-05-10 09:37:17 +00:00
|
|
|
TarScanner fs;
|
2011-11-14 21:30:37 +00:00
|
|
|
uint num = 0;
|
2011-11-14 21:34:27 +00:00
|
|
|
if (mode & TarScanner::BASESET) {
|
2011-11-14 21:32:10 +00:00
|
|
|
num += fs.DoScan(BASESET_DIR);
|
2011-11-14 21:34:27 +00:00
|
|
|
}
|
|
|
|
if (mode & TarScanner::NEWGRF) {
|
2011-11-14 21:30:37 +00:00
|
|
|
num += fs.DoScan(NEWGRF_DIR);
|
|
|
|
}
|
|
|
|
if (mode & TarScanner::AI) {
|
|
|
|
num += fs.DoScan(AI_DIR);
|
|
|
|
num += fs.DoScan(AI_LIBRARY_DIR);
|
|
|
|
}
|
2011-12-19 20:54:37 +00:00
|
|
|
if (mode & TarScanner::GAME) {
|
|
|
|
num += fs.DoScan(GAME_DIR);
|
2011-12-19 20:56:59 +00:00
|
|
|
num += fs.DoScan(GAME_LIBRARY_DIR);
|
2011-12-19 20:54:37 +00:00
|
|
|
}
|
2011-11-14 21:30:37 +00:00
|
|
|
if (mode & TarScanner::SCENARIO) {
|
|
|
|
num += fs.DoScan(SCENARIO_DIR);
|
2012-02-25 08:42:39 +00:00
|
|
|
num += fs.DoScan(HEIGHTMAP_DIR);
|
2011-11-14 21:30:37 +00:00
|
|
|
}
|
2022-12-15 18:25:06 +00:00
|
|
|
Debug(misc, 2, "Scan complete, found {} files", num);
|
2010-05-10 09:37:17 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2011-11-16 16:54:37 +00:00
|
|
|
/**
|
|
|
|
* Add a single file to the scanned files of a tar, circumventing the scanning code.
|
|
|
|
* @param sd The sub directory the file is in.
|
|
|
|
* @param filename The name of the file to add.
|
|
|
|
* @return True if the additions went correctly.
|
|
|
|
*/
|
2020-12-06 20:11:50 +00:00
|
|
|
bool TarScanner::AddFile(Subdirectory sd, const std::string &filename)
|
2011-11-16 16:54:37 +00:00
|
|
|
{
|
|
|
|
this->subdir = sd;
|
|
|
|
return this->AddFile(filename, 0);
|
|
|
|
}
|
|
|
|
|
2023-06-02 16:13:51 +00:00
|
|
|
/**
|
|
|
|
* 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 also do not create a string
|
|
|
|
* of the buffer length as that makes the string contain essentially garbage.
|
|
|
|
* @param buffer The buffer to read from.
|
|
|
|
* @param buffer_length The length of the buffer to read from.
|
|
|
|
* @return The string data.
|
|
|
|
*/
|
|
|
|
static std::string ExtractString(char *buffer, size_t buffer_length)
|
|
|
|
{
|
|
|
|
size_t length = 0;
|
|
|
|
for (; length < buffer_length && buffer[length] != '\0'; length++) {}
|
|
|
|
return StrMakeValid(std::string(buffer, length));
|
|
|
|
}
|
|
|
|
|
2023-09-16 20:20:53 +00:00
|
|
|
bool TarScanner::AddFile(const std::string &filename, size_t, [[maybe_unused]] const std::string &tar_filename)
|
2007-09-14 22:25:00 +00:00
|
|
|
{
|
2011-09-08 09:48:52 +00:00
|
|
|
/* No tar within tar. */
|
2020-12-06 20:11:50 +00:00
|
|
|
assert(tar_filename.empty());
|
2011-09-08 09:48:52 +00:00
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
/* The TAR-header, repeated for every file */
|
2013-11-23 21:31:00 +00:00
|
|
|
struct TarHeader {
|
2007-09-16 18:10:52 +00:00
|
|
|
char name[100]; ///< Name of the file
|
|
|
|
char mode[8];
|
|
|
|
char uid[8];
|
|
|
|
char gid[8];
|
2023-06-02 16:13:51 +00:00
|
|
|
char size[12]; ///< Size of the file, in ASCII octals
|
2007-09-16 18:10:52 +00:00
|
|
|
char mtime[12];
|
|
|
|
char chksum[8];
|
|
|
|
char typeflag;
|
|
|
|
char linkname[100];
|
|
|
|
char magic[6];
|
|
|
|
char version[2];
|
|
|
|
char uname[32];
|
|
|
|
char gname[32];
|
|
|
|
char devmajor[8];
|
|
|
|
char devminor[8];
|
|
|
|
char prefix[155]; ///< Path of the file
|
|
|
|
|
|
|
|
char unused[12];
|
2013-11-23 21:31:00 +00:00
|
|
|
};
|
2007-09-16 18:10:52 +00:00
|
|
|
|
|
|
|
/* Check if we already seen this file */
|
2011-11-14 21:28:43 +00:00
|
|
|
TarList::iterator it = _tar_list[this->subdir].find(filename);
|
|
|
|
if (it != _tar_list[this->subdir].end()) return false;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2020-12-06 20:11:50 +00:00
|
|
|
FILE *f = fopen(filename.c_str(), "rb");
|
2010-03-15 17:44:54 +00:00
|
|
|
/* Although the file has been found there can be
|
|
|
|
* a number of reasons we cannot open the file.
|
|
|
|
* Most common case is when we simply have not
|
|
|
|
* been given read access. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f == nullptr) return false;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2020-12-06 20:11:50 +00:00
|
|
|
_tar_list[this->subdir][filename] = std::string{};
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
TarLinkList links; ///< Temporary list to collect links
|
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
TarHeader th;
|
2008-05-08 13:20:54 +00:00
|
|
|
size_t num = 0, pos = 0;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
|
|
|
/* Make a char of 512 empty bytes */
|
|
|
|
char empty[512];
|
|
|
|
memset(&empty[0], 0, sizeof(empty));
|
|
|
|
|
2008-07-12 14:49:43 +00:00
|
|
|
for (;;) { // Note: feof() always returns 'false' after 'fseek()'. Cool, isn't it?
|
|
|
|
size_t num_bytes_read = fread(&th, 1, 512, f);
|
|
|
|
if (num_bytes_read != 512) break;
|
|
|
|
pos += num_bytes_read;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
|
|
|
/* Check if we have the new tar-format (ustar) or the old one (a lot of zeros after 'link' field) */
|
|
|
|
if (strncmp(th.magic, "ustar", 5) != 0 && memcmp(&th.magic, &empty[0], 512 - offsetof(TarHeader, magic)) != 0) {
|
|
|
|
/* If we have only zeros in the block, it can be an end-of-file indicator */
|
|
|
|
if (memcmp(&th, &empty[0], 512) == 0) continue;
|
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 0, "The file '{}' isn't a valid tar-file", filename);
|
2013-11-24 15:02:05 +00:00
|
|
|
fclose(f);
|
2007-09-16 18:10:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-02 16:13:51 +00:00
|
|
|
std::string name;
|
2013-11-24 13:00:06 +00:00
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
/* The prefix contains the directory-name */
|
|
|
|
if (th.prefix[0] != '\0') {
|
2023-06-02 16:13:51 +00:00
|
|
|
name = ExtractString(th.prefix, lengthof(th.prefix));
|
|
|
|
name += PATHSEP;
|
2007-09-16 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the name of the file in a safe way at the end of 'name' */
|
2023-06-02 16:13:51 +00:00
|
|
|
name += ExtractString(th.name, lengthof(th.name));
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2023-06-02 16:13:51 +00:00
|
|
|
/* The size of the file, for some strange reason, this is stored as a string in octals. */
|
|
|
|
std::string size = ExtractString(th.size, lengthof(th.size));
|
|
|
|
size_t skip = size.empty() ? 0 : std::stoul(size, nullptr, 8);
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
switch (th.typeflag) {
|
|
|
|
case '\0':
|
|
|
|
case '0': { // regular file
|
2023-06-02 16:13:51 +00:00
|
|
|
if (name.empty()) break;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
/* Store this entry in the list */
|
|
|
|
TarFileListEntry entry;
|
2020-12-06 20:11:50 +00:00
|
|
|
entry.tar_filename = filename;
|
2008-07-23 21:42:55 +00:00
|
|
|
entry.size = skip;
|
|
|
|
entry.position = pos;
|
2008-07-13 16:09:00 +00:00
|
|
|
|
|
|
|
/* Convert to lowercase and our PATHSEPCHAR */
|
|
|
|
SimplifyFileName(name);
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 6, "Found file in tar: {} ({} bytes, {} offset)", name, skip, pos);
|
2011-11-14 21:28:43 +00:00
|
|
|
if (_tar_filelist[this->subdir].insert(TarFileList::value_type(name, entry)).second) num++;
|
2008-07-13 16:09:00 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '1': // hard links
|
|
|
|
case '2': { // symbolic links
|
|
|
|
/* Copy the destination of the link in a safe way at the end of 'linkname' */
|
2023-06-02 16:13:51 +00:00
|
|
|
std::string link = ExtractString(th.linkname, lengthof(th.linkname));
|
2008-07-13 16:09:00 +00:00
|
|
|
|
2023-06-02 16:13:51 +00:00
|
|
|
if (name.empty() || link.empty()) break;
|
2008-07-13 16:09:00 +00:00
|
|
|
|
|
|
|
/* Convert to lowercase and our PATHSEPCHAR */
|
|
|
|
SimplifyFileName(name);
|
|
|
|
SimplifyFileName(link);
|
|
|
|
|
|
|
|
/* Only allow relative links */
|
|
|
|
if (link[0] == PATHSEPCHAR) {
|
2022-12-15 18:25:06 +00:00
|
|
|
Debug(misc, 5, "Ignoring absolute link in tar: {} -> {}", name, link);
|
2008-07-13 16:09:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process relative path.
|
|
|
|
* Note: The destination of links must not contain any directory-links. */
|
2023-06-02 16:13:51 +00:00
|
|
|
std::string dest = (std::filesystem::path(name).remove_filename() /= link).lexically_normal().string();
|
|
|
|
if (dest[0] == PATHSEPCHAR || StrStartsWith(dest, "..")) {
|
|
|
|
Debug(misc, 5, "Ignoring link pointing outside of data directory: {} -> {}", name, link);
|
|
|
|
break;
|
2008-07-13 16:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Store links in temporary list */
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 6, "Found link in tar: {} -> {}", name, dest);
|
2008-07-13 16:09:00 +00:00
|
|
|
links.insert(TarLinkList::value_type(name, dest));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
case '5': // directory
|
|
|
|
/* Convert to lowercase and our PATHSEPCHAR */
|
|
|
|
SimplifyFileName(name);
|
|
|
|
|
|
|
|
/* Store the first directory name we detect */
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 6, "Found dir in tar: {}", name);
|
2020-12-06 20:11:50 +00:00
|
|
|
if (_tar_list[this->subdir][filename].empty()) _tar_list[this->subdir][filename] = name;
|
2009-01-12 17:11:45 +00:00
|
|
|
break;
|
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
default:
|
|
|
|
/* Ignore other types */
|
|
|
|
break;
|
|
|
|
}
|
2007-09-16 18:10:52 +00:00
|
|
|
|
|
|
|
/* Skip to the next block.. */
|
2007-11-19 20:40:14 +00:00
|
|
|
skip = Align(skip, 512);
|
2013-11-23 13:15:07 +00:00
|
|
|
if (fseek(f, skip, SEEK_CUR) < 0) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 0, "The file '{}' can't be read as a valid tar-file", filename);
|
2013-11-23 13:15:07 +00:00
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-16 18:10:52 +00:00
|
|
|
pos += skip;
|
2007-09-14 22:25:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 18:25:06 +00:00
|
|
|
Debug(misc, 4, "Found tar '{}' with {} new files", filename, num);
|
2007-09-16 18:10:52 +00:00
|
|
|
fclose(f);
|
2007-09-14 22:25:00 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
/* Resolve file links and store directory links.
|
|
|
|
* We restrict usage of links to two cases:
|
|
|
|
* 1) Links to directories:
|
|
|
|
* Both the source path and the destination path must NOT contain any further links.
|
|
|
|
* When resolving files at most one directory link is resolved.
|
|
|
|
* 2) Links to files:
|
|
|
|
* The destination path must NOT contain any links.
|
|
|
|
* The source path may contain one directory link.
|
|
|
|
*/
|
2023-05-07 11:09:54 +00:00
|
|
|
for (auto &it : links) {
|
|
|
|
TarAddLink(it.first, it.second, this->subdir);
|
2008-07-13 16:09:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-14 22:25:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-26 00:00:55 +00:00
|
|
|
/**
|
|
|
|
* Extract the tar with the given filename in the directory
|
|
|
|
* where the tar resides.
|
|
|
|
* @param tar_filename the name of the tar to extract.
|
2011-11-14 21:28:43 +00:00
|
|
|
* @param subdir The sub directory the tar is in.
|
2010-02-26 00:00:55 +00:00
|
|
|
* @return false on failure.
|
|
|
|
*/
|
2020-12-06 20:11:47 +00:00
|
|
|
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir)
|
2010-02-26 00:00:55 +00:00
|
|
|
{
|
2011-11-14 21:28:43 +00:00
|
|
|
TarList::iterator it = _tar_list[subdir].find(tar_filename);
|
2010-02-26 00:00:55 +00:00
|
|
|
/* We don't know the file. */
|
2011-11-14 21:28:43 +00:00
|
|
|
if (it == _tar_list[subdir].end()) return false;
|
2010-02-26 00:00:55 +00:00
|
|
|
|
2020-12-06 20:11:50 +00:00
|
|
|
const auto &dirname = (*it).second;
|
2010-02-26 00:00:55 +00:00
|
|
|
|
|
|
|
/* The file doesn't have a sub directory! */
|
2020-12-06 20:11:50 +00:00
|
|
|
if (dirname.empty()) {
|
2022-12-15 18:25:06 +00:00
|
|
|
Debug(misc, 3, "Extracting {} failed; archive rejected, the contents must be in a sub directory", tar_filename);
|
2020-07-28 16:22:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-26 00:00:55 +00:00
|
|
|
|
2020-12-06 20:11:47 +00:00
|
|
|
std::string filename = tar_filename;
|
|
|
|
auto p = filename.find_last_of(PATHSEPCHAR);
|
2010-02-26 00:00:55 +00:00
|
|
|
/* The file's path does not have a separator? */
|
2020-12-06 20:11:47 +00:00
|
|
|
if (p == std::string::npos) return false;
|
2010-02-26 00:00:55 +00:00
|
|
|
|
2020-12-06 20:11:47 +00:00
|
|
|
filename.replace(p + 1, std::string::npos, dirname);
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 8, "Extracting {} to directory {}", tar_filename, filename);
|
2010-02-26 00:00:55 +00:00
|
|
|
FioCreateDirectory(filename);
|
|
|
|
|
2023-05-07 11:09:54 +00:00
|
|
|
for (auto &it2 : _tar_filelist[subdir]) {
|
|
|
|
if (tar_filename != it2.second.tar_filename) continue;
|
2010-02-26 00:00:55 +00:00
|
|
|
|
2023-05-07 11:09:54 +00:00
|
|
|
filename.replace(p + 1, std::string::npos, it2.first);
|
2010-02-26 00:00:55 +00:00
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 9, " extracting {}", filename);
|
2010-02-26 00:00:55 +00:00
|
|
|
|
|
|
|
/* First open the file in the .tar. */
|
|
|
|
size_t to_copy = 0;
|
2023-05-07 11:09:54 +00:00
|
|
|
std::unique_ptr<FILE, FileDeleter> in(FioFOpenFileTar(it2.second, &to_copy));
|
2020-12-06 20:11:47 +00:00
|
|
|
if (!in) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 6, "Extracting {} failed; could not open {}", filename, tar_filename);
|
2010-02-26 00:00:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now open the 'output' file. */
|
2020-12-06 20:11:47 +00:00
|
|
|
std::unique_ptr<FILE, FileDeleter> out(fopen(filename.c_str(), "wb"));
|
|
|
|
if (!out) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 6, "Extracting {} failed; could not open {}", filename, filename);
|
2010-02-26 00:00:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now read from the tar and write it into the file. */
|
|
|
|
char buffer[4096];
|
|
|
|
size_t read;
|
|
|
|
for (; to_copy != 0; to_copy -= read) {
|
2021-01-08 10:16:18 +00:00
|
|
|
read = fread(buffer, 1, std::min(to_copy, lengthof(buffer)), in.get());
|
2020-12-06 20:11:47 +00:00
|
|
|
if (read <= 0 || fwrite(buffer, 1, read, out.get()) != read) break;
|
2010-02-26 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (to_copy != 0) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 6, "Extracting {} failed; still {} bytes to copy", filename, to_copy);
|
2010-02-26 00:00:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 9, " extraction successful");
|
2010-02-26 00:00:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:28:14 +00:00
|
|
|
#if defined(_WIN32)
|
2007-09-15 15:21:17 +00:00
|
|
|
/**
|
|
|
|
* Determine the base (personal dir and game data dir) paths
|
|
|
|
* @param exe the path from the current path to the executable
|
2023-06-16 12:23:55 +00:00
|
|
|
* @note defined in the OS related files (win32.cpp, unix.cpp etc)
|
2007-09-15 15:21:17 +00:00
|
|
|
*/
|
|
|
|
extern void DetermineBasePaths(const char *exe);
|
2018-12-09 01:28:14 +00:00
|
|
|
#else /* defined(_WIN32) */
|
2007-09-15 15:21:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param exe the path to the executable
|
|
|
|
*/
|
2011-05-15 17:18:46 +00:00
|
|
|
static bool ChangeWorkingDirectoryToExecutable(const char *exe)
|
2007-09-15 15:21:17 +00:00
|
|
|
{
|
2023-06-09 14:44:42 +00:00
|
|
|
std::string path = exe;
|
2017-06-22 17:29:53 +00:00
|
|
|
|
2007-09-15 15:21:17 +00:00
|
|
|
#ifdef WITH_COCOA
|
2023-06-09 14:44:42 +00:00
|
|
|
for (size_t pos = path.find_first_of('.'); pos != std::string::npos; pos = path.find_first_of('.', pos + 1)) {
|
|
|
|
if (StrEqualsIgnoreCase(path.substr(pos, 4), ".app")) {
|
|
|
|
path.erase(pos);
|
|
|
|
break;
|
2011-05-15 17:18:46 +00:00
|
|
|
}
|
2007-09-15 15:21:17 +00:00
|
|
|
}
|
2023-06-09 14:44:42 +00:00
|
|
|
#endif /* WITH_COCOA */
|
|
|
|
|
|
|
|
size_t pos = path.find_last_of(PATHSEPCHAR);
|
|
|
|
if (pos == std::string::npos) return false;
|
|
|
|
|
|
|
|
path.erase(pos);
|
|
|
|
|
|
|
|
if (chdir(path.c_str()) != 0) {
|
|
|
|
Debug(misc, 0, "Directory with the binary does not exist?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2007-09-15 15:21:17 +00:00
|
|
|
}
|
|
|
|
|
2010-07-16 19:32:39 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @return true if it should be scanned.
|
|
|
|
*/
|
|
|
|
bool DoScanWorkingDirectory()
|
|
|
|
{
|
|
|
|
/* No working directory, so nothing to do. */
|
2020-12-06 20:11:43 +00:00
|
|
|
if (_searchpaths[SP_WORKING_DIR].empty()) return false;
|
2010-07-16 19:32:39 +00:00
|
|
|
|
|
|
|
/* Working directory is root, so do nothing. */
|
2020-12-06 20:11:43 +00:00
|
|
|
if (_searchpaths[SP_WORKING_DIR] == PATHSEP) return false;
|
2010-07-16 19:32:39 +00:00
|
|
|
|
|
|
|
/* No personal/home directory, so the working directory won't be that. */
|
2020-12-06 20:11:43 +00:00
|
|
|
if (_searchpaths[SP_PERSONAL_DIR].empty()) return true;
|
2010-07-16 19:32:39 +00:00
|
|
|
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string tmp = _searchpaths[SP_WORKING_DIR] + PERSONAL_DIR;
|
|
|
|
AppendPathSeparator(tmp);
|
|
|
|
|
2020-12-06 20:11:43 +00:00
|
|
|
return _searchpaths[SP_PERSONAL_DIR] != tmp;
|
2010-07-16 19:32:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 18:49:28 +00:00
|
|
|
/**
|
|
|
|
* Gets the home directory of the user.
|
|
|
|
* May return an empty string in the unlikely scenario that the home directory cannot be found.
|
|
|
|
* @return User's home directory
|
|
|
|
*/
|
|
|
|
static std::string GetHomeDir()
|
|
|
|
{
|
|
|
|
#ifdef __HAIKU__
|
|
|
|
BPath path;
|
|
|
|
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
|
|
|
|
return std::string(path.Path());
|
|
|
|
#else
|
2021-07-10 20:32:35 +00:00
|
|
|
const char *home_env = std::getenv("HOME"); // Stack var, shouldn't be freed
|
2020-12-06 18:49:28 +00:00
|
|
|
if (home_env != nullptr) return std::string(home_env);
|
|
|
|
|
|
|
|
const struct passwd *pw = getpwuid(getuid());
|
|
|
|
if (pw != nullptr) return std::string(pw->pw_dir);
|
|
|
|
#endif
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2007-03-12 15:25:33 +00:00
|
|
|
/**
|
|
|
|
* Determine the base (personal dir and game data dir) paths
|
2007-03-17 11:36:04 +00:00
|
|
|
* @param exe the path to the executable
|
2007-03-12 15:25:33 +00:00
|
|
|
*/
|
2007-03-17 11:36:04 +00:00
|
|
|
void DetermineBasePaths(const char *exe)
|
|
|
|
{
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string tmp;
|
2020-12-06 18:49:28 +00:00
|
|
|
const std::string homedir = GetHomeDir();
|
|
|
|
#ifdef USE_XDG
|
2021-07-10 20:32:35 +00:00
|
|
|
const char *xdg_data_home = std::getenv("XDG_DATA_HOME");
|
2020-12-06 18:49:28 +00:00
|
|
|
if (xdg_data_home != nullptr) {
|
|
|
|
tmp = xdg_data_home;
|
|
|
|
tmp += PATHSEP;
|
|
|
|
tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
|
|
|
AppendPathSeparator(tmp);
|
|
|
|
_searchpaths[SP_PERSONAL_DIR_XDG] = tmp;
|
2021-01-03 10:10:56 +00:00
|
|
|
|
|
|
|
tmp += "content_download";
|
|
|
|
AppendPathSeparator(tmp);
|
|
|
|
_searchpaths[SP_AUTODOWNLOAD_PERSONAL_DIR_XDG] = tmp;
|
2020-12-06 18:49:28 +00:00
|
|
|
} else if (!homedir.empty()) {
|
|
|
|
tmp = homedir;
|
|
|
|
tmp += PATHSEP ".local" PATHSEP "share" PATHSEP;
|
|
|
|
tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
|
|
|
AppendPathSeparator(tmp);
|
|
|
|
_searchpaths[SP_PERSONAL_DIR_XDG] = tmp;
|
2021-01-03 10:10:56 +00:00
|
|
|
|
|
|
|
tmp += "content_download";
|
|
|
|
AppendPathSeparator(tmp);
|
|
|
|
_searchpaths[SP_AUTODOWNLOAD_PERSONAL_DIR_XDG] = tmp;
|
2020-12-06 18:49:28 +00:00
|
|
|
} else {
|
|
|
|
_searchpaths[SP_PERSONAL_DIR_XDG].clear();
|
2021-01-03 10:10:56 +00:00
|
|
|
_searchpaths[SP_AUTODOWNLOAD_PERSONAL_DIR_XDG].clear();
|
2007-03-17 11:36:04 +00:00
|
|
|
}
|
2010-02-17 16:45:55 +00:00
|
|
|
#endif
|
2007-03-17 11:36:04 +00:00
|
|
|
|
2023-06-16 12:23:55 +00:00
|
|
|
#if !defined(WITH_PERSONAL_DIR)
|
2020-12-06 18:49:28 +00:00
|
|
|
_searchpaths[SP_PERSONAL_DIR].clear();
|
|
|
|
#else
|
|
|
|
if (!homedir.empty()) {
|
2020-12-06 20:11:44 +00:00
|
|
|
tmp = homedir;
|
|
|
|
tmp += PATHSEP;
|
|
|
|
tmp += PERSONAL_DIR;
|
|
|
|
AppendPathSeparator(tmp);
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_PERSONAL_DIR] = tmp;
|
2021-01-03 10:10:56 +00:00
|
|
|
|
|
|
|
tmp += "content_download";
|
|
|
|
AppendPathSeparator(tmp);
|
|
|
|
_searchpaths[SP_AUTODOWNLOAD_PERSONAL_DIR] = tmp;
|
2013-11-25 21:50:54 +00:00
|
|
|
} else {
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_PERSONAL_DIR].clear();
|
2021-01-03 10:10:56 +00:00
|
|
|
_searchpaths[SP_AUTODOWNLOAD_PERSONAL_DIR].clear();
|
2013-11-25 21:50:54 +00:00
|
|
|
}
|
2007-06-17 15:48:57 +00:00
|
|
|
#endif
|
2008-01-13 17:45:29 +00:00
|
|
|
|
|
|
|
#if defined(WITH_SHARED_DIR)
|
2020-12-06 20:11:44 +00:00
|
|
|
tmp = SHARED_DIR;
|
|
|
|
AppendPathSeparator(tmp);
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_SHARED_DIR] = tmp;
|
2008-01-13 17:45:29 +00:00
|
|
|
#else
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_SHARED_DIR].clear();
|
2008-01-13 17:45:29 +00:00
|
|
|
#endif
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2020-12-13 19:54:20 +00:00
|
|
|
char cwd[MAX_PATH];
|
|
|
|
if (getcwd(cwd, MAX_PATH) == nullptr) *cwd = '\0';
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2020-12-06 20:11:42 +00:00
|
|
|
if (_config_file.empty()) {
|
2020-12-13 19:54:20 +00:00
|
|
|
/* Get the path to working directory of OpenTTD. */
|
2020-12-06 20:11:44 +00:00
|
|
|
tmp = cwd;
|
|
|
|
AppendPathSeparator(tmp);
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_WORKING_DIR] = tmp;
|
2020-12-13 19:54:20 +00:00
|
|
|
|
|
|
|
_do_scan_working_directory = DoScanWorkingDirectory();
|
|
|
|
} else {
|
|
|
|
/* Use the folder of the config file as working directory. */
|
2020-12-06 20:11:43 +00:00
|
|
|
size_t end = _config_file.find_last_of(PATHSEPCHAR);
|
|
|
|
if (end == std::string::npos) {
|
2020-12-13 19:54:20 +00:00
|
|
|
/* _config_file is not in a folder, so use current directory. */
|
2020-12-06 20:11:44 +00:00
|
|
|
tmp = cwd;
|
|
|
|
AppendPathSeparator(tmp);
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_WORKING_DIR] = tmp;
|
2020-12-13 19:54:20 +00:00
|
|
|
} else {
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_WORKING_DIR] = _config_file.substr(0, end + 1);
|
2020-12-13 19:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-16 19:32:39 +00:00
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
/* Change the working directory to that one of the executable */
|
2011-05-15 17:18:46 +00:00
|
|
|
if (ChangeWorkingDirectoryToExecutable(exe)) {
|
2020-12-06 20:11:44 +00:00
|
|
|
char buf[MAX_PATH];
|
|
|
|
if (getcwd(buf, lengthof(buf)) == nullptr) {
|
|
|
|
tmp.clear();
|
|
|
|
} else {
|
|
|
|
tmp = buf;
|
|
|
|
}
|
|
|
|
AppendPathSeparator(tmp);
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_BINARY_DIR] = tmp;
|
2011-05-15 17:18:46 +00:00
|
|
|
} else {
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_BINARY_DIR].clear();
|
2011-05-15 17:18:46 +00:00
|
|
|
}
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2020-12-13 19:54:20 +00:00
|
|
|
if (cwd[0] != '\0') {
|
2009-10-03 14:51:30 +00:00
|
|
|
/* Go back to the current working directory. */
|
2020-12-13 19:54:20 +00:00
|
|
|
if (chdir(cwd) != 0) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 0, "Failed to return to working directory!");
|
2011-05-15 17:18:46 +00:00
|
|
|
}
|
2009-10-03 14:51:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 15:47:34 +00:00
|
|
|
#if !defined(GLOBAL_DATA_DIR)
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_INSTALLATION_DIR].clear();
|
2007-06-21 14:32:27 +00:00
|
|
|
#else
|
2020-12-06 20:11:44 +00:00
|
|
|
tmp = GLOBAL_DATA_DIR;
|
|
|
|
AppendPathSeparator(tmp);
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_INSTALLATION_DIR] = tmp;
|
2007-06-21 14:32:27 +00:00
|
|
|
#endif
|
2007-06-17 15:48:57 +00:00
|
|
|
#ifdef WITH_COCOA
|
2020-12-26 15:07:47 +00:00
|
|
|
extern void CocoaSetApplicationBundleDir();
|
|
|
|
CocoaSetApplicationBundleDir();
|
2007-06-17 15:48:57 +00:00
|
|
|
#else
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_APPLICATION_BUNDLE_DIR].clear();
|
2007-06-17 15:48:57 +00:00
|
|
|
#endif
|
2007-03-17 11:36:04 +00:00
|
|
|
}
|
2018-12-09 01:28:14 +00:00
|
|
|
#endif /* defined(_WIN32) */
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2020-12-06 20:11:42 +00:00
|
|
|
std::string _personal_dir;
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2007-03-12 15:25:33 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2007-06-17 15:48:57 +00:00
|
|
|
* @param exe the path from the current path to the executable
|
2021-06-10 21:13:34 +00:00
|
|
|
* @param only_local_path Whether we shouldn't fill searchpaths with global folders.
|
2007-03-12 15:25:33 +00:00
|
|
|
*/
|
2021-06-10 21:13:34 +00:00
|
|
|
void DeterminePaths(const char *exe, bool only_local_path)
|
2007-03-12 15:25:33 +00:00
|
|
|
{
|
2007-03-17 11:36:04 +00:00
|
|
|
DetermineBasePaths(exe);
|
2021-06-10 21:13:34 +00:00
|
|
|
FillValidSearchPaths(only_local_path);
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2020-12-06 18:49:28 +00:00
|
|
|
#ifdef USE_XDG
|
|
|
|
std::string config_home;
|
|
|
|
const std::string homedir = GetHomeDir();
|
2021-07-10 20:32:35 +00:00
|
|
|
const char *xdg_config_home = std::getenv("XDG_CONFIG_HOME");
|
2020-12-06 18:49:28 +00:00
|
|
|
if (xdg_config_home != nullptr) {
|
|
|
|
config_home = xdg_config_home;
|
|
|
|
config_home += PATHSEP;
|
|
|
|
config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
|
|
|
} else if (!homedir.empty()) {
|
|
|
|
/* Defaults to ~/.config */
|
|
|
|
config_home = homedir;
|
|
|
|
config_home += PATHSEP ".config" PATHSEP;
|
|
|
|
config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
|
|
|
}
|
2020-12-06 20:11:44 +00:00
|
|
|
AppendPathSeparator(config_home);
|
2013-11-13 19:32:37 +00:00
|
|
|
#endif
|
|
|
|
|
2021-04-30 13:41:58 +00:00
|
|
|
for (Searchpath sp : _valid_searchpaths) {
|
2010-07-16 19:32:39 +00:00
|
|
|
if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue;
|
2022-12-15 18:25:06 +00:00
|
|
|
Debug(misc, 3, "{} added as search path", _searchpaths[sp]);
|
2010-07-16 19:32:39 +00:00
|
|
|
}
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2020-12-06 20:11:42 +00:00
|
|
|
std::string config_dir;
|
|
|
|
if (!_config_file.empty()) {
|
2020-12-13 19:54:20 +00:00
|
|
|
config_dir = _searchpaths[SP_WORKING_DIR];
|
2007-06-17 21:26:57 +00:00
|
|
|
} else {
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string personal_dir = FioFindFullPath(BASE_DIR, "openttd.cfg");
|
|
|
|
if (!personal_dir.empty()) {
|
|
|
|
auto end = personal_dir.find_last_of(PATHSEPCHAR);
|
|
|
|
if (end != std::string::npos) personal_dir.erase(end + 1);
|
2020-12-06 20:11:42 +00:00
|
|
|
config_dir = personal_dir;
|
2007-06-17 21:26:57 +00:00
|
|
|
} else {
|
2020-12-06 18:49:28 +00:00
|
|
|
#ifdef USE_XDG
|
2013-11-13 19:32:37 +00:00
|
|
|
/* No previous configuration file found. Use the configuration folder from XDG. */
|
|
|
|
config_dir = config_home;
|
|
|
|
#else
|
2007-06-17 21:26:57 +00:00
|
|
|
static const Searchpath new_openttd_cfg_order[] = {
|
|
|
|
SP_PERSONAL_DIR, SP_BINARY_DIR, SP_WORKING_DIR, SP_SHARED_DIR, SP_INSTALLATION_DIR
|
|
|
|
};
|
|
|
|
|
2020-12-06 20:11:42 +00:00
|
|
|
config_dir.clear();
|
2007-06-17 21:26:57 +00:00
|
|
|
for (uint i = 0; i < lengthof(new_openttd_cfg_order); i++) {
|
|
|
|
if (IsValidSearchPath(new_openttd_cfg_order[i])) {
|
2020-12-06 20:11:42 +00:00
|
|
|
config_dir = _searchpaths[new_openttd_cfg_order[i]];
|
2007-06-17 21:26:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 19:32:37 +00:00
|
|
|
#endif
|
2007-06-17 21:26:57 +00:00
|
|
|
}
|
2020-12-06 20:11:42 +00:00
|
|
|
_config_file = config_dir + "openttd.cfg";
|
2007-06-17 15:48:57 +00:00
|
|
|
}
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2022-12-15 18:25:06 +00:00
|
|
|
Debug(misc, 1, "{} found as config directory", config_dir);
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2020-12-06 20:11:42 +00:00
|
|
|
_highscore_file = config_dir + "hs.dat";
|
|
|
|
extern std::string _hotkeys_file;
|
|
|
|
_hotkeys_file = config_dir + "hotkeys.cfg";
|
|
|
|
extern std::string _windows_file;
|
|
|
|
_windows_file = config_dir + "windows.cfg";
|
2021-06-28 14:39:48 +00:00
|
|
|
extern std::string _private_file;
|
|
|
|
_private_file = config_dir + "private.cfg";
|
|
|
|
extern std::string _secrets_file;
|
|
|
|
_secrets_file = config_dir + "secrets.cfg";
|
2013-11-13 19:32:37 +00:00
|
|
|
|
2020-12-06 18:49:28 +00:00
|
|
|
#ifdef USE_XDG
|
2013-11-13 19:32:37 +00:00
|
|
|
if (config_dir == config_home) {
|
|
|
|
/* We are using the XDG configuration home for the config file,
|
|
|
|
* then store the rest in the XDG data home folder. */
|
|
|
|
_personal_dir = _searchpaths[SP_PERSONAL_DIR_XDG];
|
2021-06-10 21:13:34 +00:00
|
|
|
if (only_local_path) {
|
|
|
|
/* In case of XDG and we only want local paths and we detected that
|
|
|
|
* the user either manually indicated the XDG path or didn't use
|
|
|
|
* "-c" option, we change the working-dir to the XDG personal-dir,
|
|
|
|
* as this is most likely what the user is expecting. */
|
|
|
|
_searchpaths[SP_WORKING_DIR] = _searchpaths[SP_PERSONAL_DIR_XDG];
|
|
|
|
}
|
2013-11-13 19:32:37 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
_personal_dir = config_dir;
|
|
|
|
}
|
2007-06-17 15:48:57 +00:00
|
|
|
|
|
|
|
/* Make the necessary folders */
|
2020-12-06 20:11:45 +00:00
|
|
|
FioCreateDirectory(config_dir);
|
2020-12-08 22:11:47 +00:00
|
|
|
#if defined(WITH_PERSONAL_DIR)
|
2020-12-06 20:11:45 +00:00
|
|
|
FioCreateDirectory(_personal_dir);
|
2007-11-18 18:28:32 +00:00
|
|
|
#endif
|
|
|
|
|
2022-12-15 18:25:06 +00:00
|
|
|
Debug(misc, 1, "{} found as personal directory", _personal_dir);
|
2013-11-13 19:32:37 +00:00
|
|
|
|
2009-03-04 14:25:53 +00:00
|
|
|
static const Subdirectory default_subdirs[] = {
|
2012-12-09 16:52:43 +00:00
|
|
|
SAVE_DIR, AUTOSAVE_DIR, SCENARIO_DIR, HEIGHTMAP_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR, SCREENSHOT_DIR
|
2009-03-04 14:25:53 +00:00
|
|
|
};
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2009-03-04 14:25:53 +00:00
|
|
|
for (uint i = 0; i < lengthof(default_subdirs); i++) {
|
2020-12-06 20:11:45 +00:00
|
|
|
FioCreateDirectory(_personal_dir + _subdirs[default_subdirs[i]]);
|
2009-03-04 14:25:53 +00:00
|
|
|
}
|
2009-01-16 16:32:48 +00:00
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
/* If we have network we make a directory for the autodownloading of content */
|
2020-12-06 20:11:43 +00:00
|
|
|
_searchpaths[SP_AUTODOWNLOAD_DIR] = _personal_dir + "content_download" PATHSEP;
|
2022-12-15 18:25:06 +00:00
|
|
|
Debug(misc, 3, "{} added as search path", _searchpaths[SP_AUTODOWNLOAD_DIR]);
|
2020-12-06 20:11:45 +00:00
|
|
|
FioCreateDirectory(_searchpaths[SP_AUTODOWNLOAD_DIR]);
|
2021-06-10 21:13:34 +00:00
|
|
|
FillValidSearchPaths(only_local_path);
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
/* Create the directory for each of the types of content */
|
2011-12-19 20:56:59 +00:00
|
|
|
const Subdirectory dirs[] = { SCENARIO_DIR, HEIGHTMAP_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR };
|
2009-01-17 16:53:32 +00:00
|
|
|
for (uint i = 0; i < lengthof(dirs); i++) {
|
2020-12-06 20:11:45 +00:00
|
|
|
FioCreateDirectory(FioGetDirectory(SP_AUTODOWNLOAD_DIR, dirs[i]));
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
2010-07-19 17:46:53 +00:00
|
|
|
|
2020-05-17 21:32:10 +00:00
|
|
|
extern std::string _log_file;
|
2020-12-06 20:11:42 +00:00
|
|
|
_log_file = _personal_dir + "openttd.log";
|
2007-03-12 15:25:33 +00:00
|
|
|
}
|
2007-06-12 15:46:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitizes a filename, i.e. removes all illegal characters from it.
|
2023-05-25 16:02:59 +00:00
|
|
|
* @param filename the filename
|
2007-06-12 15:46:34 +00:00
|
|
|
*/
|
2023-05-25 16:02:59 +00:00
|
|
|
void SanitizeFilename(std::string &filename)
|
2007-06-12 15:46:34 +00:00
|
|
|
{
|
2023-05-25 16:02:59 +00:00
|
|
|
for (auto &c : filename) {
|
|
|
|
switch (c) {
|
2007-06-12 15:46:34 +00:00
|
|
|
/* The following characters are not allowed in filenames
|
|
|
|
* on at least one of the supported operating systems: */
|
|
|
|
case ':': case '\\': case '*': case '?': case '/':
|
2007-06-22 19:30:59 +00:00
|
|
|
case '<': case '>': case '|': case '"':
|
2023-05-25 16:02:59 +00:00
|
|
|
c = '_';
|
2007-06-12 15:46:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-25 11:26:07 +00:00
|
|
|
|
2011-08-01 19:36:11 +00:00
|
|
|
/**
|
|
|
|
* Load a file into memory.
|
|
|
|
* @param filename Name of the file to load.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param[out] lenp Length of loaded data.
|
2011-08-01 19:36:11 +00:00
|
|
|
* @param maxsize Maximum size to load.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return Pointer to new memory containing the loaded data, or \c nullptr if loading failed.
|
2011-08-01 19:36:11 +00:00
|
|
|
* @note If \a maxsize less than the length of the file, loading fails.
|
|
|
|
*/
|
2021-03-27 23:12:32 +00:00
|
|
|
std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize)
|
2007-12-25 11:26:07 +00:00
|
|
|
{
|
2020-12-06 20:11:48 +00:00
|
|
|
FILE *in = fopen(filename.c_str(), "rb");
|
2019-04-10 21:07:06 +00:00
|
|
|
if (in == nullptr) return nullptr;
|
2007-12-25 11:26:07 +00:00
|
|
|
|
2020-12-06 20:11:48 +00:00
|
|
|
FileCloser fc(in);
|
|
|
|
|
2007-12-25 11:26:07 +00:00
|
|
|
fseek(in, 0, SEEK_END);
|
2009-02-24 20:59:17 +00:00
|
|
|
size_t len = ftell(in);
|
2007-12-25 11:26:07 +00:00
|
|
|
fseek(in, 0, SEEK_SET);
|
2020-12-06 20:11:48 +00:00
|
|
|
if (len > maxsize) return nullptr;
|
|
|
|
|
2021-03-27 23:12:32 +00:00
|
|
|
std::unique_ptr<char[]> mem = std::make_unique<char[]>(len + 1);
|
2020-12-06 20:11:48 +00:00
|
|
|
|
|
|
|
mem.get()[len] = 0;
|
|
|
|
if (fread(mem.get(), len, 1, in) != 1) return nullptr;
|
2007-12-25 11:26:07 +00:00
|
|
|
|
2020-12-06 20:11:48 +00:00
|
|
|
lenp = len;
|
2007-12-25 11:26:07 +00:00
|
|
|
return mem;
|
|
|
|
}
|
2008-08-31 08:38:30 +00:00
|
|
|
|
2011-08-25 10:24:49 +00:00
|
|
|
/**
|
|
|
|
* Helper to see whether a given filename matches the extension.
|
|
|
|
* @param extension The extension to look for.
|
|
|
|
* @param filename The filename to look in for the extension.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return True iff the extension is nullptr, or the filename ends with it.
|
2011-08-25 10:24:49 +00:00
|
|
|
*/
|
|
|
|
static bool MatchesExtension(const char *extension, const char *filename)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (extension == nullptr) return true;
|
2011-08-25 10:24:49 +00:00
|
|
|
|
|
|
|
const char *ext = strrchr(filename, extension[0]);
|
2023-04-27 09:47:08 +00:00
|
|
|
return ext != nullptr && StrEqualsIgnoreCase(ext, extension);
|
2011-08-25 10:24:49 +00:00
|
|
|
}
|
2008-08-31 08:38:30 +00:00
|
|
|
|
|
|
|
/**
|
2010-04-12 14:12:47 +00:00
|
|
|
* Scan a single directory (and recursively its children) and add
|
2008-08-31 08:38:30 +00:00
|
|
|
* any graphics sets that are found.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param fs the file scanner to add the files to
|
2008-08-31 08:38:30 +00:00
|
|
|
* @param extension the extension of files to search for.
|
|
|
|
* @param path full path we're currently at
|
|
|
|
* @param basepath_length from where in the path are we 'based' on the search path
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param recursive whether to recursively search the sub directories
|
2008-08-31 08:38:30 +00:00
|
|
|
*/
|
2009-03-04 00:17:51 +00:00
|
|
|
static uint ScanPath(FileScanner *fs, const char *extension, const char *path, size_t basepath_length, bool recursive)
|
2008-08-31 08:38:30 +00:00
|
|
|
{
|
|
|
|
uint num = 0;
|
|
|
|
struct stat sb;
|
|
|
|
struct dirent *dirent;
|
|
|
|
DIR *dir;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (path == nullptr || (dir = ttd_opendir(path)) == nullptr) return 0;
|
2008-08-31 08:38:30 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
while ((dirent = readdir(dir)) != nullptr) {
|
2021-02-21 16:03:19 +00:00
|
|
|
std::string d_name = FS2OTTD(dirent->d_name);
|
2008-08-31 08:38:30 +00:00
|
|
|
|
|
|
|
if (!FiosIsValidFile(path, dirent, &sb)) continue;
|
|
|
|
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string filename(path);
|
|
|
|
filename += d_name;
|
2008-08-31 08:38:30 +00:00
|
|
|
|
2008-11-26 01:07:49 +00:00
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
2008-08-31 08:38:30 +00:00
|
|
|
/* Directory */
|
2009-03-04 00:17:51 +00:00
|
|
|
if (!recursive) continue;
|
2021-02-21 16:03:19 +00:00
|
|
|
if (d_name == "." || d_name == "..") continue;
|
2020-12-06 20:11:44 +00:00
|
|
|
AppendPathSeparator(filename);
|
|
|
|
num += ScanPath(fs, extension, filename.c_str(), basepath_length, recursive);
|
2008-11-26 01:07:49 +00:00
|
|
|
} else if (S_ISREG(sb.st_mode)) {
|
2008-08-31 08:38:30 +00:00
|
|
|
/* File */
|
2020-12-06 20:11:50 +00:00
|
|
|
if (MatchesExtension(extension, filename.c_str()) && fs->AddFile(filename, basepath_length, {})) num++;
|
2008-08-31 08:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan the given tar and add graphics sets when it finds one.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param fs the file scanner to scan for
|
2008-08-31 08:38:30 +00:00
|
|
|
* @param extension the extension of files to search for.
|
|
|
|
* @param tar the tar to search in.
|
|
|
|
*/
|
2021-04-29 23:13:50 +00:00
|
|
|
static uint ScanTar(FileScanner *fs, const char *extension, const TarFileList::value_type &tar)
|
2008-08-31 08:38:30 +00:00
|
|
|
{
|
|
|
|
uint num = 0;
|
2021-04-29 23:13:50 +00:00
|
|
|
const auto &filename = tar.first;
|
2008-08-31 08:38:30 +00:00
|
|
|
|
2021-04-29 23:13:50 +00:00
|
|
|
if (MatchesExtension(extension, filename.c_str()) && fs->AddFile(filename, 0, tar.second.tar_filename)) num++;
|
2008-08-31 08:38:30 +00:00
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-08 22:46:42 +00:00
|
|
|
* Scan for files with the given extension in the given search path.
|
2008-08-31 08:38:30 +00:00
|
|
|
* @param extension the extension of files to search for.
|
2008-10-14 19:27:08 +00:00
|
|
|
* @param sd the sub directory to search in.
|
2008-08-31 08:38:30 +00:00
|
|
|
* @param tars whether to search in the tars too.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param recursive whether to search recursively
|
2008-08-31 08:38:30 +00:00
|
|
|
* @return the number of found files, i.e. the number of times that
|
|
|
|
* AddFile returned true.
|
|
|
|
*/
|
2009-03-04 00:17:51 +00:00
|
|
|
uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool recursive)
|
2008-08-31 08:38:30 +00:00
|
|
|
{
|
2011-08-25 10:31:35 +00:00
|
|
|
this->subdir = sd;
|
|
|
|
|
2008-08-31 08:38:30 +00:00
|
|
|
uint num = 0;
|
|
|
|
|
2021-04-30 13:41:58 +00:00
|
|
|
for (Searchpath sp : _valid_searchpaths) {
|
2010-07-16 19:32:39 +00:00
|
|
|
/* Don't search in the working directory */
|
|
|
|
if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue;
|
|
|
|
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string path = FioGetDirectory(sp, sd);
|
|
|
|
num += ScanPath(this, extension, path.c_str(), path.size(), recursive);
|
2008-08-31 08:38:30 +00:00
|
|
|
}
|
2009-01-16 16:14:01 +00:00
|
|
|
|
2011-11-14 21:28:43 +00:00
|
|
|
if (tars && sd != NO_DIRECTORY) {
|
2021-04-29 23:13:50 +00:00
|
|
|
for (const auto &tar : _tar_filelist[sd]) {
|
2009-01-16 16:14:01 +00:00
|
|
|
num += ScanTar(this, extension, tar);
|
|
|
|
}
|
2008-08-31 08:38:30 +00:00
|
|
|
}
|
|
|
|
|
2011-11-15 00:26:06 +00:00
|
|
|
switch (sd) {
|
|
|
|
case BASESET_DIR:
|
|
|
|
num += this->Scan(extension, OLD_GM_DIR, tars, recursive);
|
2017-08-13 18:38:42 +00:00
|
|
|
FALLTHROUGH;
|
2011-11-15 00:26:06 +00:00
|
|
|
case NEWGRF_DIR:
|
|
|
|
num += this->Scan(extension, OLD_DATA_DIR, tars, recursive);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2008-08-31 08:38:30 +00:00
|
|
|
return num;
|
|
|
|
}
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
/**
|
2013-01-08 22:46:42 +00:00
|
|
|
* Scan for files with the given extension in the given search path.
|
2009-01-17 16:53:32 +00:00
|
|
|
* @param extension the extension of files to search for.
|
|
|
|
* @param directory the sub directory to search in.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param recursive whether to search recursively
|
2009-01-17 16:53:32 +00:00
|
|
|
* @return the number of found files, i.e. the number of times that
|
|
|
|
* AddFile returned true.
|
|
|
|
*/
|
2023-05-06 11:22:16 +00:00
|
|
|
uint FileScanner::Scan(const char *extension, const std::string &directory, bool recursive)
|
2009-01-17 16:53:32 +00:00
|
|
|
{
|
2020-12-06 20:11:44 +00:00
|
|
|
std::string path(directory);
|
|
|
|
AppendPathSeparator(path);
|
|
|
|
return ScanPath(this, extension, path.c_str(), path.size(), recursive);
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|