19 # define access _taccess
20 #elif defined(__HAIKU__)
22 #include <storage/FindDirectory.h>
39 static const char *
const _subdirs[] = {
42 "save" PATHSEP
"autosave" PATHSEP,
44 "scenario" PATHSEP
"heightmap" PATHSEP,
51 "ai" PATHSEP
"library" PATHSEP,
53 "game" PATHSEP
"library" PATHSEP,
65 std::vector<Searchpath> _valid_searchpaths;
66 std::array<TarList, NUM_SUBDIRS> _tar_list;
69 typedef std::map<std::string, std::string> TarLinkList;
82 static void FillValidSearchPaths(
bool only_local_path)
84 _valid_searchpaths.clear();
85 for (
Searchpath sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) {
86 if (only_local_path) {
111 if (f ==
nullptr)
return false;
124 return access(
OTTD2FS(filename).c_str(), 0) == 0;
146 std::string buf = FioGetDirectory(sp, subdir);
163 assert(sp < NUM_SEARCHPATHS);
172 std::string ret = FioGetDirectory(sp, subdir);
180 static FILE *FioFOpenFileSp(
const std::string &filename,
const char *mode,
Searchpath sp,
Subdirectory subdir,
size_t *filesize)
188 MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode,
lengthof(Lmode));
200 if (mode[0] ==
'r' && GetFileAttributes(
OTTD2FS(buf).c_str()) == INVALID_FILE_ATTRIBUTES)
return nullptr;
203 f = fopen(buf.c_str(), mode);
206 f = fopen(buf.c_str(), mode);
209 if (f !=
nullptr && filesize !=
nullptr) {
211 fseek(f, 0, SEEK_END);
212 *filesize = ftell(f);
213 fseek(f, 0, SEEK_SET);
227 FILE *f = fopen(entry.tar_filename.c_str(),
"rb");
228 if (f ==
nullptr)
return f;
230 if (fseek(f, entry.position, SEEK_SET) < 0) {
235 if (filesize !=
nullptr) *filesize = entry.size;
252 f = FioFOpenFileSp(filename, mode, sp, subdir, filesize);
257 if (f ==
nullptr && mode[0] ==
'r' && subdir !=
NO_DIRECTORY) {
259 std::string resolved_name = filename;
263 std::istringstream ss(resolved_name);
264 std::vector<std::string> tokens;
266 while (std::getline(ss, token, PATHSEPCHAR)) {
268 if (tokens.size() < 2)
return nullptr;
270 }
else if (token ==
".") {
274 tokens.push_back(token);
278 resolved_name.clear();
280 for (
const std::string &token : tokens) {
282 resolved_name += PATHSEP;
284 resolved_name += token;
290 const std::string &src = link->first;
291 size_t len = src.length();
292 if (resolved_name.length() >= len && resolved_name[len - 1] == PATHSEPCHAR && src.compare(0, len, resolved_name, 0, len) == 0) {
294 resolved_name.replace(0, len, link->second);
299 TarFileList::iterator it = _tar_filelist[subdir].find(resolved_name);
300 if (it != _tar_filelist[subdir].end()) {
311 if (f !=
nullptr)
break;
333 auto p = name.find_last_of(PATHSEPCHAR);
334 if (p != std::string::npos) {
335 std::string dirname = name.substr(0, p);
337 if (dir ==
nullptr) {
347 CreateDirectory(
OTTD2FS(name).c_str(),
nullptr);
348 #elif defined(OS2) && !defined(__INNOTEK_LIBC__)
351 mkdir(
OTTD2FS(name).c_str(), 0755);
363 if (buf.empty())
return;
365 if (buf.back() != PATHSEPCHAR) buf.push_back(PATHSEPCHAR);
368 static void TarAddLink(
const std::string &srcParam,
const std::string &destParam,
Subdirectory subdir)
370 std::string src = srcParam;
371 std::string dest = destParam;
373 std::transform(src.begin(), src.end(), src.begin(), tolower);
374 std::transform(dest.begin(), dest.end(), dest.begin(), tolower);
376 TarFileList::iterator dest_file = _tar_filelist[subdir].find(dest);
377 if (dest_file != _tar_filelist[subdir].end()) {
379 _tar_filelist[subdir].insert(TarFileList::value_type(src, dest_file->second));
383 const std::string src_path = ((*src.rbegin() == PATHSEPCHAR) ? src : src + PATHSEPCHAR);
384 const std::string dst_path = (dest.length() == 0 ?
"" : ((*dest.rbegin() == PATHSEPCHAR) ? dest : dest + PATHSEPCHAR));
385 _tar_linklist[subdir].insert(TarLinkList::value_type(src_path, dst_path));
400 #if (PATHSEPCHAR != '/')
401 for (
char *n = name; *n !=
'\0'; n++)
if (*n ==
'/') *n = PATHSEPCHAR;
412 _tar_filelist[sd].clear();
413 _tar_list[sd].clear();
414 uint num = this->
Scan(
".tar", sd,
false);
421 Debug(misc, 1,
"Scanning for tars");
442 Debug(misc, 1,
"Scan complete, found {} files", num);
455 return this->
AddFile(filename, 0);
458 bool TarScanner::AddFile(
const std::string &filename,
size_t basepath_length,
const std::string &tar_filename)
461 assert(tar_filename.empty());
486 TarList::iterator it = _tar_list[this->
subdir].find(filename);
487 if (it != _tar_list[this->subdir].end())
return false;
489 FILE *f = fopen(filename.c_str(),
"rb");
494 if (f ==
nullptr)
return false;
496 _tar_list[this->
subdir][filename] = std::string{};
501 char buf[
sizeof(th.name) + 1], *end;
502 char name[
sizeof(th.prefix) + 1 +
sizeof(th.name) + 1];
503 char link[
sizeof(th.linkname) + 1];
504 char dest[
sizeof(th.prefix) + 1 +
sizeof(th.name) + 1 + 1 +
sizeof(th.linkname) + 1];
505 size_t num = 0, pos = 0;
509 memset(&empty[0], 0,
sizeof(empty));
512 size_t num_bytes_read = fread(&th, 1, 512, f);
513 if (num_bytes_read != 512)
break;
514 pos += num_bytes_read;
517 if (strncmp(th.magic,
"ustar", 5) != 0 && memcmp(&th.magic, &empty[0], 512 - offsetof(TarHeader, magic)) != 0) {
519 if (memcmp(&th, &empty[0], 512) == 0)
continue;
521 Debug(misc, 0,
"The file '{}' isn't a valid tar-file", filename);
529 if (th.prefix[0] !=
'\0') {
539 size_t skip = strtoul(buf, &end, 8);
541 switch (th.typeflag) {
544 if (strlen(name) == 0)
break;
548 entry.tar_filename = filename;
550 entry.position = pos;
555 Debug(misc, 6,
"Found file in tar: {} ({} bytes, {} offset)", name, skip, pos);
556 if (_tar_filelist[this->subdir].insert(TarFileList::value_type(name, entry)).second) num++;
566 if (strlen(name) == 0 || strlen(link) == 0)
break;
573 if (link[0] == PATHSEPCHAR) {
574 Debug(misc, 1,
"Ignoring absolute link in tar: {} -> {}", name, link);
581 char *destpos = strrchr(dest, PATHSEPCHAR);
582 if (destpos ==
nullptr) destpos = dest;
586 while (*pos !=
'\0') {
587 char *next = strchr(pos, PATHSEPCHAR);
588 if (next ==
nullptr) {
589 next = pos + strlen(pos);
595 if (strcmp(pos,
".") == 0) {
597 }
else if (strcmp(pos,
"..") == 0) {
599 if (dest[0] ==
'\0') {
600 Debug(misc, 1,
"Ignoring link pointing outside of data directory: {} -> {}", name, link);
606 destpos = strrchr(dest, PATHSEPCHAR);
607 if (destpos ==
nullptr) destpos = dest;
611 if (destpos != dest) destpos =
strecpy(destpos, PATHSEP,
lastof(dest));
615 if (destpos >=
lastof(dest)) {
616 Debug(misc, 0,
"The length of a link in tar-file '{}' is too large (malformed?)", filename);
625 Debug(misc, 6,
"Found link in tar: {} -> {}", name, dest);
626 links.insert(TarLinkList::value_type(name, dest));
636 Debug(misc, 6,
"Found dir in tar: {}", name);
637 if (_tar_list[this->subdir][filename].empty()) _tar_list[this->
subdir][filename] = name;
646 skip =
Align(skip, 512);
647 if (fseek(f, skip, SEEK_CUR) < 0) {
648 Debug(misc, 0,
"The file '{}' can't be read as a valid tar-file", filename);
655 Debug(misc, 1,
"Found tar '{}' with {} new files", filename, num);
667 for (TarLinkList::iterator link = links.begin(); link != links.end(); link++) {
668 const std::string &src = link->first;
669 const std::string &dest = link->second;
670 TarAddLink(src, dest, this->subdir);
685 TarList::iterator it = _tar_list[subdir].find(tar_filename);
687 if (it == _tar_list[subdir].end())
return false;
689 const auto &dirname = (*it).second;
692 if (dirname.empty()) {
693 Debug(misc, 1,
"Extracting {} failed; archive rejected, the contents must be in a sub directory", tar_filename);
697 std::string filename = tar_filename;
698 auto p = filename.find_last_of(PATHSEPCHAR);
700 if (p == std::string::npos)
return false;
702 filename.replace(p + 1, std::string::npos, dirname);
703 Debug(misc, 8,
"Extracting {} to directory {}", tar_filename, filename);
706 for (TarFileList::iterator it2 = _tar_filelist[subdir].begin(); it2 != _tar_filelist[subdir].end(); it2++) {
707 if (tar_filename != it2->second.tar_filename)
continue;
709 filename.replace(p + 1, std::string::npos, it2->first);
711 Debug(misc, 9,
" extracting {}", filename);
715 std::unique_ptr<FILE, FileDeleter> in(
FioFOpenFileTar(it2->second, &to_copy));
717 Debug(misc, 6,
"Extracting {} failed; could not open {}", filename, tar_filename);
722 std::unique_ptr<FILE, FileDeleter> out(fopen(filename.c_str(),
"wb"));
724 Debug(misc, 6,
"Extracting {} failed; could not open {}", filename, filename);
731 for (; to_copy != 0; to_copy -= read) {
732 read = fread(buffer, 1, std::min(to_copy,
lengthof(buffer)), in.get());
733 if (read <= 0 || fwrite(buffer, 1, read, out.get()) != read)
break;
737 Debug(misc, 6,
"Extracting {} failed; still {} bytes to copy", filename, to_copy);
742 Debug(misc, 9,
" extraction successful");
767 bool success =
false;
769 char *app_bundle = strchr(tmp,
'.');
770 while (app_bundle !=
nullptr && strncasecmp(app_bundle,
".app", 4) != 0) app_bundle = strchr(&app_bundle[1],
'.');
772 if (app_bundle !=
nullptr) *app_bundle =
'\0';
774 char *s = strrchr(tmp, PATHSEPCHAR);
777 if (chdir(tmp) != 0) {
778 Debug(misc, 0,
"Directory with the binary does not exist?");
822 find_directory(B_USER_SETTINGS_DIRECTORY, &path);
823 return std::string(path.Path());
825 const char *home_env = std::getenv(
"HOME");
826 if (home_env !=
nullptr)
return std::string(home_env);
828 const struct passwd *pw = getpwuid(getuid());
829 if (pw !=
nullptr)
return std::string(pw->pw_dir);
843 const char *xdg_data_home = std::getenv(
"XDG_DATA_HOME");
844 if (xdg_data_home !=
nullptr) {
847 tmp += PERSONAL_DIR[0] ==
'.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
851 tmp +=
"content_download";
854 }
else if (!homedir.empty()) {
856 tmp += PATHSEP
".local" PATHSEP
"share" PATHSEP;
857 tmp += PERSONAL_DIR[0] ==
'.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
861 tmp +=
"content_download";
870 #if defined(OS2) || !defined(WITH_PERSONAL_DIR)
873 if (!homedir.empty()) {
880 tmp +=
"content_download";
889 #if defined(WITH_SHARED_DIR)
898 if (getcwd(cwd, MAX_PATH) ==
nullptr) *cwd =
'\0';
910 if (end == std::string::npos) {
923 if (getcwd(buf,
lengthof(buf)) ==
nullptr) {
934 if (cwd[0] !=
'\0') {
936 if (chdir(cwd) != 0) {
937 Debug(misc, 0,
"Failed to return to working directory!");
941 #if !defined(GLOBAL_DATA_DIR)
944 tmp = GLOBAL_DATA_DIR;
949 extern void CocoaSetApplicationBundleDir();
950 CocoaSetApplicationBundleDir();
969 FillValidSearchPaths(only_local_path);
972 std::string config_home;
974 const char *xdg_config_home = std::getenv(
"XDG_CONFIG_HOME");
975 if (xdg_config_home !=
nullptr) {
976 config_home = xdg_config_home;
977 config_home += PATHSEP;
978 config_home += PERSONAL_DIR[0] ==
'.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
979 }
else if (!homedir.empty()) {
981 config_home = homedir;
982 config_home += PATHSEP
".config" PATHSEP;
983 config_home += PERSONAL_DIR[0] ==
'.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
993 std::string config_dir;
998 if (!personal_dir.empty()) {
999 auto end = personal_dir.find_last_of(PATHSEPCHAR);
1000 if (end != std::string::npos) personal_dir.erase(end + 1);
1001 config_dir = personal_dir;
1005 config_dir = config_home;
1007 static const Searchpath new_openttd_cfg_order[] = {
1012 for (uint i = 0; i <
lengthof(new_openttd_cfg_order); i++) {
1023 Debug(misc, 3,
"{} found as config directory", config_dir);
1026 extern std::string _hotkeys_file;
1027 _hotkeys_file = config_dir +
"hotkeys.cfg";
1036 if (config_dir == config_home) {
1040 if (only_local_path) {
1055 #if defined(WITH_PERSONAL_DIR)
1062 SAVE_DIR,
AUTOSAVE_DIR,
SCENARIO_DIR,
HEIGHTMAP_DIR,
BASESET_DIR,
NEWGRF_DIR,
AI_DIR,
AI_LIBRARY_DIR,
GAME_DIR,
GAME_LIBRARY_DIR,
SCREENSHOT_DIR
1065 for (uint i = 0; i <
lengthof(default_subdirs); i++) {
1073 FillValidSearchPaths(only_local_path);
1077 for (uint i = 0; i <
lengthof(dirs); i++) {
1091 for (; *filename !=
'\0'; filename++) {
1092 switch (*filename) {
1095 case ':':
case '\\':
case '*':
case '?':
case '/':
1096 case '<':
case '>':
case '|':
case '"':
1111 std::unique_ptr<char[]>
ReadFileToMem(
const std::string &filename,
size_t &lenp,
size_t maxsize)
1113 FILE *in = fopen(filename.c_str(),
"rb");
1114 if (in ==
nullptr)
return nullptr;
1118 fseek(in, 0, SEEK_END);
1119 size_t len = ftell(in);
1120 fseek(in, 0, SEEK_SET);
1121 if (len > maxsize)
return nullptr;
1123 std::unique_ptr<char[]> mem = std::make_unique<char[]>(len + 1);
1126 if (fread(mem.get(), len, 1, in) != 1)
return nullptr;
1140 if (extension ==
nullptr)
return true;
1142 const char *ext = strrchr(filename, extension[0]);
1143 return ext !=
nullptr && strcasecmp(ext, extension) == 0;
1155 static uint
ScanPath(
FileScanner *fs,
const char *extension,
const char *path,
size_t basepath_length,
bool recursive)
1157 extern bool FiosIsValidFile(
const char *path,
const struct dirent *ent,
struct stat *sb);
1161 struct dirent *dirent;
1164 if (path ==
nullptr || (dir =
ttd_opendir(path)) ==
nullptr)
return 0;
1166 while ((dirent = readdir(dir)) !=
nullptr) {
1167 std::string d_name =
FS2OTTD(dirent->d_name);
1169 if (!FiosIsValidFile(path, dirent, &sb))
continue;
1171 std::string filename(path);
1174 if (S_ISDIR(sb.st_mode)) {
1176 if (!recursive)
continue;
1177 if (d_name ==
"." || d_name ==
"..")
continue;
1179 num +=
ScanPath(fs, extension, filename.c_str(), basepath_length, recursive);
1180 }
else if (S_ISREG(sb.st_mode)) {
1200 const auto &filename = tar.first;
1202 if (
MatchesExtension(extension, filename.c_str()) && fs->
AddFile(filename, 0, tar.second.tar_filename)) num++;
1226 std::string path = FioGetDirectory(sp, sd);
1227 num +=
ScanPath(
this, extension, path.c_str(), path.size(), recursive);
1231 for (
const auto &tar : _tar_filelist[sd]) {
1232 num +=
ScanTar(
this, extension, tar);
1260 std::string path(directory);
1262 return ScanPath(
this, extension, path.c_str(), path.size(), recursive);