18 # define access _taccess
19 #elif defined(__HAIKU__)
21 #include <storage/FindDirectory.h>
33 #define FIO_BUFFER_SIZE 512
42 std::array<FILE *, MAX_FILE_SLOTS>
handles;
115 if (size == 0)
return 0;
117 return *
_fio.buffer++;
201 if (f ==
nullptr)
usererror(
"Cannot open file '%s'", filename.c_str());
203 if (pos < 0)
usererror(
"Cannot read file '%s'", filename.c_str());
210 auto t = filename.rfind(PATHSEPCHAR);
211 std::string sn = filename.substr(t != std::string::npos ? t + 1 : 0);
218 static const char *
const _subdirs[] = {
221 "save" PATHSEP
"autosave" PATHSEP,
223 "scenario" PATHSEP
"heightmap" PATHSEP,
230 "ai" PATHSEP
"library" PATHSEP,
232 "game" PATHSEP
"library" PATHSEP,
233 "screenshot" PATHSEP,
244 std::array<TarList, NUM_SUBDIRS> _tar_list;
247 typedef std::map<std::string, std::string> TarLinkList;
269 if (f ==
nullptr)
return false;
282 return access(
OTTD2FS(filename.c_str()), 0) == 0;
305 std::string buf = FioGetDirectory(sp, subdir);
322 assert(sp < NUM_SEARCHPATHS);
333 std::string ret = FioGetDirectory(sp, subdir);
341 static FILE *FioFOpenFileSp(
const std::string &filename,
const char *mode,
Searchpath sp,
Subdirectory subdir,
size_t *filesize)
349 MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode,
lengthof(Lmode));
361 if (mode[0] ==
'r' && GetFileAttributes(
OTTD2FS(buf.c_str())) == INVALID_FILE_ATTRIBUTES)
return nullptr;
364 f = fopen(buf.c_str(), mode);
367 f = fopen(buf.c_str(), mode);
370 if (f !=
nullptr && filesize !=
nullptr) {
372 fseek(f, 0, SEEK_END);
373 *filesize = ftell(f);
374 fseek(f, 0, SEEK_SET);
388 FILE *f = fopen(entry.tar_filename.c_str(),
"rb");
389 if (f ==
nullptr)
return f;
391 if (fseek(f, entry.position, SEEK_SET) < 0) {
396 if (filesize !=
nullptr) *filesize = entry.size;
414 f = FioFOpenFileSp(filename, mode, sp, subdir, filesize);
419 if (f ==
nullptr && mode[0] ==
'r' && subdir !=
NO_DIRECTORY) {
421 std::string resolved_name = filename;
425 std::istringstream ss(resolved_name);
426 std::vector<std::string> tokens;
428 while (std::getline(ss, token, PATHSEPCHAR)) {
430 if (tokens.size() < 2)
return nullptr;
433 tokens.push_back(token);
437 resolved_name.clear();
439 for (
const std::string &token : tokens) {
441 resolved_name += PATHSEP;
443 resolved_name += token;
449 const std::string &src = link->first;
450 size_t len = src.length();
451 if (resolved_name.length() >= len && resolved_name[len - 1] == PATHSEPCHAR && src.compare(0, len, resolved_name, 0, len) == 0) {
453 resolved_name.replace(0, len, link->second);
458 TarFileList::iterator it = _tar_filelist[subdir].find(resolved_name);
459 if (it != _tar_filelist[subdir].end()) {
470 if (f !=
nullptr)
break;
492 auto p = name.find_last_of(PATHSEPCHAR);
493 if (p != std::string::npos) {
494 std::string dirname = name.substr(0, p);
496 if (dir ==
nullptr) {
506 CreateDirectory(
OTTD2FS(name.c_str()),
nullptr);
507 #elif defined(OS2) && !defined(__INNOTEK_LIBC__)
510 mkdir(
OTTD2FS(name.c_str()), 0755);
522 if (buf.empty())
return;
524 if (buf.back() != PATHSEPCHAR) buf.push_back(PATHSEPCHAR);
527 static void TarAddLink(
const std::string &srcParam,
const std::string &destParam,
Subdirectory subdir)
529 std::string src = srcParam;
530 std::string dest = destParam;
532 std::transform(src.begin(), src.end(), src.begin(), tolower);
533 std::transform(dest.begin(), dest.end(), dest.begin(), tolower);
535 TarFileList::iterator dest_file = _tar_filelist[subdir].find(dest);
536 if (dest_file != _tar_filelist[subdir].end()) {
538 _tar_filelist[subdir].insert(TarFileList::value_type(src, dest_file->second));
542 const std::string src_path = ((*src.rbegin() == PATHSEPCHAR) ? src : src + PATHSEPCHAR);
543 const std::string dst_path = (dest.length() == 0 ?
"" : ((*dest.rbegin() == PATHSEPCHAR) ? dest : dest + PATHSEPCHAR));
544 _tar_linklist[subdir].insert(TarLinkList::value_type(src_path, dst_path));
559 #if (PATHSEPCHAR != '/')
560 for (
char *n = name; *n !=
'\0'; n++)
if (*n ==
'/') *n = PATHSEPCHAR;
571 _tar_filelist[sd].clear();
572 _tar_list[sd].clear();
573 uint num = this->
Scan(
".tar", sd,
false);
580 DEBUG(misc, 1,
"Scanning for tars");
601 DEBUG(misc, 1,
"Scan complete, found %d files", num);
614 return this->
AddFile(filename, 0);
617 bool TarScanner::AddFile(
const std::string &filename,
size_t basepath_length,
const std::string &tar_filename)
620 assert(tar_filename.empty());
645 TarList::iterator it = _tar_list[this->
subdir].find(filename);
646 if (it != _tar_list[this->subdir].end())
return false;
648 FILE *f = fopen(filename.c_str(),
"rb");
653 if (f ==
nullptr)
return false;
655 _tar_list[this->
subdir][filename] = std::string{};
660 char buf[
sizeof(th.name) + 1], *end;
661 char name[
sizeof(th.prefix) + 1 +
sizeof(th.name) + 1];
662 char link[
sizeof(th.linkname) + 1];
663 char dest[
sizeof(th.prefix) + 1 +
sizeof(th.name) + 1 + 1 +
sizeof(th.linkname) + 1];
664 size_t num = 0, pos = 0;
668 memset(&empty[0], 0,
sizeof(empty));
671 size_t num_bytes_read = fread(&th, 1, 512, f);
672 if (num_bytes_read != 512)
break;
673 pos += num_bytes_read;
676 if (strncmp(th.magic,
"ustar", 5) != 0 && memcmp(&th.magic, &empty[0], 512 - offsetof(TarHeader, magic)) != 0) {
678 if (memcmp(&th, &empty[0], 512) == 0)
continue;
680 DEBUG(misc, 0,
"The file '%s' isn't a valid tar-file", filename.c_str());
688 if (th.prefix[0] !=
'\0') {
698 size_t skip = strtoul(buf, &end, 8);
700 switch (th.typeflag) {
704 if (skip == 0)
break;
706 if (strlen(name) == 0)
break;
710 entry.tar_filename = filename;
712 entry.position = pos;
717 DEBUG(misc, 6,
"Found file in tar: %s (" PRINTF_SIZE
" bytes, " PRINTF_SIZE
" offset)", name, skip, pos);
718 if (_tar_filelist[this->subdir].insert(TarFileList::value_type(name, entry)).second) num++;
728 if (strlen(name) == 0 || strlen(link) == 0)
break;
735 if (link[0] == PATHSEPCHAR) {
736 DEBUG(misc, 1,
"Ignoring absolute link in tar: %s -> %s", name, link);
743 char *destpos = strrchr(dest, PATHSEPCHAR);
744 if (destpos ==
nullptr) destpos = dest;
748 while (*pos !=
'\0') {
749 char *next = strchr(pos, PATHSEPCHAR);
750 if (next ==
nullptr) {
751 next = pos + strlen(pos);
757 if (strcmp(pos,
".") == 0) {
759 }
else if (strcmp(pos,
"..") == 0) {
761 if (dest[0] ==
'\0') {
762 DEBUG(misc, 1,
"Ignoring link pointing outside of data directory: %s -> %s", name, link);
768 destpos = strrchr(dest, PATHSEPCHAR);
769 if (destpos ==
nullptr) destpos = dest;
773 if (destpos != dest) destpos =
strecpy(destpos, PATHSEP,
lastof(dest));
777 if (destpos >=
lastof(dest)) {
778 DEBUG(misc, 0,
"The length of a link in tar-file '%s' is too large (malformed?)", filename.c_str());
787 DEBUG(misc, 6,
"Found link in tar: %s -> %s", name, dest);
788 links.insert(TarLinkList::value_type(name, dest));
798 DEBUG(misc, 6,
"Found dir in tar: %s", name);
799 if (_tar_list[this->subdir][filename].empty()) _tar_list[this->
subdir][filename] = name;
808 skip =
Align(skip, 512);
809 if (fseek(f, skip, SEEK_CUR) < 0) {
810 DEBUG(misc, 0,
"The file '%s' can't be read as a valid tar-file", filename.c_str());
817 DEBUG(misc, 1,
"Found tar '%s' with " PRINTF_SIZE
" new files", filename.c_str(), num);
829 for (TarLinkList::iterator link = links.begin(); link != links.end(); link++) {
830 const std::string &src = link->first;
831 const std::string &dest = link->second;
832 TarAddLink(src, dest, this->subdir);
847 TarList::iterator it = _tar_list[subdir].find(tar_filename);
849 if (it == _tar_list[subdir].end())
return false;
851 const auto &dirname = (*it).second;
854 if (dirname.empty()) {
855 DEBUG(misc, 1,
"Extracting %s failed; archive rejected, the contents must be in a sub directory", tar_filename.c_str());
859 std::string filename = tar_filename;
860 auto p = filename.find_last_of(PATHSEPCHAR);
862 if (p == std::string::npos)
return false;
864 filename.replace(p + 1, std::string::npos, dirname);
865 DEBUG(misc, 8,
"Extracting %s to directory %s", tar_filename.c_str(), filename.c_str());
868 for (TarFileList::iterator it2 = _tar_filelist[subdir].begin(); it2 != _tar_filelist[subdir].end(); it2++) {
869 if (tar_filename != it2->second.tar_filename)
continue;
871 filename.replace(p + 1, std::string::npos, it2->first);
873 DEBUG(misc, 9,
" extracting %s", filename.c_str());
877 std::unique_ptr<FILE, FileDeleter> in(
FioFOpenFileTar(it2->second, &to_copy));
879 DEBUG(misc, 6,
"Extracting %s failed; could not open %s", filename.c_str(), tar_filename.c_str());
884 std::unique_ptr<FILE, FileDeleter> out(fopen(filename.c_str(),
"wb"));
886 DEBUG(misc, 6,
"Extracting %s failed; could not open %s", filename.c_str(), filename.c_str());
893 for (; to_copy != 0; to_copy -= read) {
894 read = fread(buffer, 1, std::min(to_copy,
lengthof(buffer)), in.get());
895 if (read <= 0 || fwrite(buffer, 1, read, out.get()) != read)
break;
899 DEBUG(misc, 6,
"Extracting %s failed; still %i bytes to copy", filename.c_str(), (
int)to_copy);
904 DEBUG(misc, 9,
" extraction successful");
929 bool success =
false;
931 char *app_bundle = strchr(tmp,
'.');
932 while (app_bundle !=
nullptr && strncasecmp(app_bundle,
".app", 4) != 0) app_bundle = strchr(&app_bundle[1],
'.');
934 if (app_bundle !=
nullptr) *app_bundle =
'\0';
936 char *s = strrchr(tmp, PATHSEPCHAR);
939 if (chdir(tmp) != 0) {
940 DEBUG(misc, 0,
"Directory with the binary does not exist?");
984 find_directory(B_USER_SETTINGS_DIRECTORY, &path);
985 return std::string(path.Path());
987 const char *home_env = getenv(
"HOME");
988 if (home_env !=
nullptr)
return std::string(home_env);
990 const struct passwd *pw = getpwuid(getuid());
991 if (pw !=
nullptr)
return std::string(pw->pw_dir);
1005 const char *xdg_data_home = getenv(
"XDG_DATA_HOME");
1006 if (xdg_data_home !=
nullptr) {
1007 tmp = xdg_data_home;
1009 tmp += PERSONAL_DIR[0] ==
'.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
1013 tmp +=
"content_download";
1016 }
else if (!homedir.empty()) {
1018 tmp += PATHSEP
".local" PATHSEP
"share" PATHSEP;
1019 tmp += PERSONAL_DIR[0] ==
'.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
1023 tmp +=
"content_download";
1032 #if defined(OS2) || !defined(WITH_PERSONAL_DIR)
1035 if (!homedir.empty()) {
1038 tmp += PERSONAL_DIR;
1042 tmp +=
"content_download";
1051 #if defined(WITH_SHARED_DIR)
1060 if (getcwd(cwd, MAX_PATH) ==
nullptr) *cwd =
'\0';
1072 if (end == std::string::npos) {
1085 if (getcwd(buf,
lengthof(buf)) ==
nullptr) {
1096 if (cwd[0] !=
'\0') {
1098 if (chdir(cwd) != 0) {
1099 DEBUG(misc, 0,
"Failed to return to working directory!");
1103 #if !defined(GLOBAL_DATA_DIR)
1106 tmp = GLOBAL_DATA_DIR;
1111 extern void CocoaSetApplicationBundleDir();
1112 CocoaSetApplicationBundleDir();
1132 std::string config_home;
1134 const char *xdg_config_home = getenv(
"XDG_CONFIG_HOME");
1135 if (xdg_config_home !=
nullptr) {
1136 config_home = xdg_config_home;
1137 config_home += PATHSEP;
1138 config_home += PERSONAL_DIR[0] ==
'.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
1139 }
else if (!homedir.empty()) {
1141 config_home = homedir;
1142 config_home += PATHSEP
".config" PATHSEP;
1143 config_home += PERSONAL_DIR[0] ==
'.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
1154 std::string config_dir;
1159 if (!personal_dir.empty()) {
1160 auto end = personal_dir.find_last_of(PATHSEPCHAR);
1161 if (end != std::string::npos) personal_dir.erase(end + 1);
1162 config_dir = personal_dir;
1166 config_dir = config_home;
1168 static const Searchpath new_openttd_cfg_order[] = {
1173 for (uint i = 0; i <
lengthof(new_openttd_cfg_order); i++) {
1184 DEBUG(misc, 3,
"%s found as config directory", config_dir.c_str());
1187 extern std::string _hotkeys_file;
1188 _hotkeys_file = config_dir +
"hotkeys.cfg";
1193 if (config_dir == config_home) {
1205 #if defined(WITH_PERSONAL_DIR)
1212 SAVE_DIR,
AUTOSAVE_DIR,
SCENARIO_DIR,
HEIGHTMAP_DIR,
BASESET_DIR,
NEWGRF_DIR,
AI_DIR,
AI_LIBRARY_DIR,
GAME_DIR,
GAME_LIBRARY_DIR,
SCREENSHOT_DIR
1215 for (uint i = 0; i <
lengthof(default_subdirs); i++) {
1225 for (uint i = 0; i <
lengthof(dirs); i++) {
1239 for (; *filename !=
'\0'; filename++) {
1240 switch (*filename) {
1243 case ':':
case '\\':
case '*':
case '?':
case '/':
1244 case '<':
case '>':
case '|':
case '"':
1259 std::unique_ptr<char>
ReadFileToMem(
const std::string &filename,
size_t &lenp,
size_t maxsize)
1261 FILE *in = fopen(filename.c_str(),
"rb");
1262 if (in ==
nullptr)
return nullptr;
1266 fseek(in, 0, SEEK_END);
1267 size_t len = ftell(in);
1268 fseek(in, 0, SEEK_SET);
1269 if (len > maxsize)
return nullptr;
1274 std::unique_ptr<char> mem(
static_cast<char *
>(::
operator new(len + 1)));
1277 if (fread(mem.get(), len, 1, in) != 1)
return nullptr;
1291 if (extension ==
nullptr)
return true;
1293 const char *ext = strrchr(filename, extension[0]);
1294 return ext !=
nullptr && strcasecmp(ext, extension) == 0;
1306 static uint
ScanPath(
FileScanner *fs,
const char *extension,
const char *path,
size_t basepath_length,
bool recursive)
1308 extern bool FiosIsValidFile(
const char *path,
const struct dirent *ent,
struct stat *sb);
1312 struct dirent *dirent;
1315 if (path ==
nullptr || (dir =
ttd_opendir(path)) ==
nullptr)
return 0;
1317 while ((dirent = readdir(dir)) !=
nullptr) {
1318 const char *d_name =
FS2OTTD(dirent->d_name);
1320 if (!FiosIsValidFile(path, dirent, &sb))
continue;
1322 std::string filename(path);
1325 if (S_ISDIR(sb.st_mode)) {
1327 if (!recursive)
continue;
1328 if (strcmp(d_name,
".") == 0 || strcmp(d_name,
"..") == 0)
continue;
1330 num +=
ScanPath(fs, extension, filename.c_str(), basepath_length, recursive);
1331 }
else if (S_ISREG(sb.st_mode)) {
1351 const auto &filename = (*tar).first;
1353 if (
MatchesExtension(extension, filename.c_str()) && fs->
AddFile(filename, 0, (*tar).second.tar_filename)) num++;
1372 TarFileList::iterator tar;
1379 std::string path = FioGetDirectory(sp, sd);
1380 num +=
ScanPath(
this, extension, path.c_str(), path.size(), recursive);
1384 FOR_ALL_TARS(tar, sd) {
1385 num +=
ScanTar(
this, extension, tar);
1413 std::string path(directory);
1415 return ScanPath(
this, extension, path.c_str(), path.size(), recursive);