| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "dxxsconf.h" |
| 9 |
#include "args.h" |
| 10 |
#include "compiler-poison.h" |
| 11 |
#include "console.h" |
| 12 |
#include "d_enumerate.h" |
| 13 |
#include "ignorecase.h" |
| 14 |
#include "newdemo.h" |
| 15 |
#include "physfs_list.h" |
| 16 |
#include "physfsx.h" |
| 17 |
#include "strutil.h" |
| 18 |
|
| 19 |
namespace dcx { |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
constexpr std::array<file_extension_t, 1> archive_exts{{"dxa"}}; |
| 24 |
|
| 25 |
std::pair<RAIINamedPHYSFS_File, PHYSFS_ErrorCode> PHYSFSX_openReadBuffered_internal(char *const filename, const char *const reported_filename) |
| 26 |
{ |
| 27 |
if (PHYSFSEXT_locateCorrectCase(filename) != PHYSFSX_case_search_result::success) |
| 28 |
return {RAIINamedPHYSFS_File{}, PHYSFS_ERR_NOT_FOUND}; |
| 29 |
/* Use the specified filename in any error messages. This may be the |
| 30 |
* modified filename or may be the original, depending on whether the |
| 31 |
* modified filename is a temporary. Even if the original filename is |
| 32 |
* used, it should be close enough that the user should be able to identify |
| 33 |
* the bad file, and avoids the need to allocate and return storage for the |
| 34 |
* filename. In almost all cases, the filename will never be seen, since |
| 35 |
* it is only shown if an error occurs when reading the file. |
| 36 |
*/ |
| 37 |
RAIIPHYSFS_File fp{PHYSFS_openRead(filename)}; |
| 38 |
if (!fp) |
| 39 |
return {RAIINamedPHYSFS_File{}, PHYSFS_getLastErrorCode()}; |
| 40 |
PHYSFS_uint64 bufSize; |
| 41 |
bufSize = PHYSFS_fileLength(fp); |
| 42 |
while (!PHYSFS_setBuffer(fp, bufSize) && bufSize) |
| 43 |
bufSize /= 2; // even if the error isn't memory full, for a 20MB file it'll only do this 8 times |
| 44 |
return {RAIINamedPHYSFS_File{std::move(fp), reported_filename}, PHYSFS_ERR_OK}; |
| 45 |
} |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
PHYSFSX_fgets_t::result PHYSFSX_fgets_t::get(const std::span<char> buf, PHYSFS_File *const fp) |
| 50 |
{ |
| 51 |
/* Tell PHYSFS not to use the last byte of the buffer, so that this |
| 52 |
* function can always write to the byte after the last byte read from the |
| 53 |
* file. |
| 54 |
*/ |
| 55 |
const PHYSFS_sint64 r{PHYSFSX_readBytes(fp, buf.data(), buf.size() - 1)}; |
| 56 |
if (r <= 0) |
| 57 |
{ |
| 58 |
DXX_POISON_MEMORY(buf, 0xcc); |
| 59 |
return {}; |
| 60 |
} |
| 61 |
const auto bb{buf.begin()}; |
| 62 |
auto p{bb}; |
| 63 |
std::ptrdiff_t buffer_end_adjustment{0}; |
| 64 |
for (const auto e{std::next(bb, r)}; p != e; ++p) |
| 65 |
{ |
| 66 |
if (const char c{*p}; c == 0 || c == '\n') |
| 67 |
{ |
| 68 |
/* Do nothing - and skip the `continue` below that handles most |
| 69 |
* characters. |
| 70 |
*/ |
| 71 |
} |
| 72 |
else if (c == '\r') |
| 73 |
{ |
| 74 |
/* If a Carriage Return is found, stop here. If the next byte is a |
| 75 |
* newline, then update `p` to consider the newline included in the |
| 76 |
* string, so that the seek does not cause the newline to replay on |
| 77 |
* the next call. Otherwise, leave `p` such that the seek will |
| 78 |
* cause that next byte to replay on the next call. |
| 79 |
*/ |
| 80 |
*p = 0; |
| 81 |
if (const auto p2{std::next(p)}; p2 != e && *p2 == '\n') |
| 82 |
{ |
| 83 |
p = p2; |
| 84 |
/* Increment `buffer_end_adjustment` so that the returned span does |
| 85 |
* not include the null that was previously a Carriage Return. |
| 86 |
*/ |
| 87 |
++ buffer_end_adjustment; |
| 88 |
} |
| 89 |
} |
| 90 |
else |
| 91 |
continue; |
| 92 |
PHYSFS_seek(fp, PHYSFS_tell(fp) + p - e + 1); |
| 93 |
break; |
| 94 |
} |
| 95 |
*p = 0; |
| 96 |
DXX_POISON_MEMORY(buf.subspan((p + 1) - bb), 0xcc); |
| 97 |
return {bb, p - buffer_end_adjustment}; |
| 98 |
} |
| 99 |
|
| 100 |
const file_extension_t *PHYSFSX_checkMatchingExtension(const char *filename, const std::ranges::subrange<const file_extension_t *> range) |
| 101 |
{ |
| 102 |
const char *ext{strrchr(filename, '.')}; |
| 103 |
if (!ext) |
| 104 |
return nullptr; |
| 105 |
++ext; |
| 106 |
// see if the file is of a type we want |
| 107 |
for (auto &k : range) |
| 108 |
{ |
| 109 |
if (!d_stricmp(ext, k)) |
| 110 |
return &k; |
| 111 |
} |
| 112 |
return nullptr; |
| 113 |
} |
| 114 |
|
| 115 |
// Add a searchpath, but that searchpath is relative to an existing searchpath |
| 116 |
// It will add the first one it finds and return a PhysFS error code, if it doesn't find any it returns PHYSFS_ERR_OK |
| 117 |
PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(char *const relname2, std::array<char, PATH_MAX> &pathname, physfs_search_path add_to_end) |
| 118 |
{ |
| 119 |
if (PHYSFSEXT_locateCorrectCase(relname2) != PHYSFSX_case_search_result::success || |
| 120 |
!PHYSFSX_getRealPath(relname2, pathname)) |
| 121 |
{ |
| 122 |
/* This failure is not reported as an error, because callers |
| 123 |
* probe for files that users may not have, and do not need. |
| 124 |
*/ |
| 125 |
con_printf(CON_DEBUG, "PHYSFS: ignoring map request: no canonical path for relative name \"%s\"", relname2); |
| 126 |
return PHYSFS_ERR_OK; |
| 127 |
} |
| 128 |
|
| 129 |
auto r = PHYSFS_mount(pathname.data(), nullptr, static_cast<int>(add_to_end)); |
| 130 |
const auto action = add_to_end != physfs_search_path::prepend ? "append" : "insert"; |
| 131 |
if (r) |
| 132 |
{ |
| 133 |
con_printf(CON_DEBUG, "PHYSFS: %s canonical directory \"%s\" to search path from relative name \"%s\"", action, pathname.data(), relname2); |
| 134 |
return PHYSFS_ERR_OK; |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
const auto err = PHYSFS_getLastErrorCode(); |
| 139 |
con_printf(CON_VERBOSE, "PHYSFS: failed to %s canonical directory \"%s\" to search path from relative name \"%s\": \"%s\"", action, pathname.data(), relname2, PHYSFS_getErrorByCode(err)); |
| 140 |
return err; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void PHYSFSX_removeRelFromSearchPath(char *const relname) |
| 145 |
{ |
| 146 |
std::array<char, PATH_MAX> pathname; |
| 147 |
if (PHYSFSEXT_locateCorrectCase(relname) != PHYSFSX_case_search_result::success || |
| 148 |
!PHYSFSX_getRealPath(relname, pathname)) |
| 149 |
{ |
| 150 |
con_printf(CON_DEBUG, "PHYSFS: ignoring unmap request: no canonical path for relative name \"%s\"", relname); |
| 151 |
return; |
| 152 |
} |
| 153 |
auto r = PHYSFS_unmount(pathname.data()); |
| 154 |
if (r) |
| 155 |
con_printf(CON_DEBUG, "PHYSFS: unmap canonical directory \"%s\" (relative name \"%s\")", pathname.data(), relname); |
| 156 |
else |
| 157 |
con_printf(CON_VERBOSE, "PHYSFS: failed to unmap canonical directory \"%s\" (relative name \"%s\"): \"%s\"", pathname.data(), relname, PHYSFS_getLastError()); |
| 158 |
} |
| 159 |
|
| 160 |
int PHYSFSX_fsize(char *const filename) |
| 161 |
{ |
| 162 |
if (PHYSFSEXT_locateCorrectCase(filename) != PHYSFSX_case_search_result::success) |
| 163 |
{ |
| 164 |
/* Fall through to the final return. */ |
| 165 |
} |
| 166 |
else if (RAIIPHYSFS_File fp{PHYSFS_openRead(filename)}) |
| 167 |
return PHYSFS_fileLength(fp); |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
void PHYSFSX_listSearchPathContent() |
| 172 |
{ |
| 173 |
if (!(CON_DEBUG <= CGameArg.DbgVerbose)) |
| 174 |
/* Skip retrieving the path contents if the verbosity level will |
| 175 |
* prevent showing the results. |
| 176 |
*/ |
| 177 |
return; |
| 178 |
con_puts(CON_DEBUG, "PHYSFS: Listing contents of Search Path."); |
| 179 |
if (const auto s{PHYSFSX_uncounted_list{PHYSFS_getSearchPath()}}) |
| 180 |
for (const auto &&[idx, entry] : enumerate(s)) |
| 181 |
con_printf(CON_DEBUG, "PHYSFS: search path entry #%" DXX_PRI_size_type ": %s", idx, entry); |
| 182 |
if (const auto s{PHYSFSX_uncounted_list{PHYSFS_enumerateFiles("")}}) |
| 183 |
for (const auto &&[idx, entry] : enumerate(s)) |
| 184 |
con_printf(CON_DEBUG, "PHYSFS: found file #%" DXX_PRI_size_type ": %s", idx, entry); |
| 185 |
} |
| 186 |
|
| 187 |
int PHYSFSX_getRealPath(const char *stdPath, std::array<char, PATH_MAX> &realPath) |
| 188 |
{ |
| 189 |
DXX_POISON_MEMORY(std::span(realPath), 0xdd); |
| 190 |
const char *realDir = PHYSFS_getRealDir(stdPath); |
| 191 |
if (!realDir) |
| 192 |
{ |
| 193 |
realDir = PHYSFS_getWriteDir(); |
| 194 |
if (!realDir) |
| 195 |
return 0; |
| 196 |
} |
| 197 |
const auto realDirSize = strlen(realDir); |
| 198 |
if (realDirSize >= realPath.size()) |
| 199 |
return 0; |
| 200 |
auto mountpoint = PHYSFS_getMountPoint(realDir); |
| 201 |
if (!mountpoint) |
| 202 |
return 0; |
| 203 |
std::copy_n(realDir, realDirSize + 1, realPath.begin()); |
| 204 |
#ifdef _unix__ |
| 205 |
auto &sep = "/"; |
| 206 |
assert(!strcmp(PHYSFS_getDirSeparator(), sep)); |
| 207 |
#else |
| 208 |
const auto sep = PHYSFS_getDirSeparator(); |
| 209 |
#endif |
| 210 |
const auto sepSize = strlen(sep); |
| 211 |
auto realPathUsed = realDirSize; |
| 212 |
if (realDirSize >= sepSize) |
| 213 |
{ |
| 214 |
const auto p = std::next(realPath.begin(), realDirSize - sepSize); |
| 215 |
if (strcmp(p, sep)) // no sep at end of realPath |
| 216 |
{ |
| 217 |
realPathUsed += sepSize; |
| 218 |
std::copy_n(sep, sepSize, &realPath[realDirSize]); |
| 219 |
} |
| 220 |
} |
| 221 |
if (*mountpoint == '/') |
| 222 |
++mountpoint; |
| 223 |
if (*stdPath == '/') |
| 224 |
++stdPath; |
| 225 |
const auto ml = strlen(mountpoint); |
| 226 |
if (!strncmp(mountpoint, stdPath, ml)) |
| 227 |
stdPath += ml; |
| 228 |
else |
| 229 |
{ |
| 230 |
/* Virtual path is not under the virtual mount point that |
| 231 |
* provides the path. |
| 232 |
*/ |
| 233 |
assert(false); |
| 234 |
} |
| 235 |
const auto stdPathLen = strlen(stdPath) + 1; |
| 236 |
if (realPathUsed + stdPathLen >= realPath.size()) |
| 237 |
return 0; |
| 238 |
#ifdef __unix__ |
| 239 |
/* Separator is "/" and physfs internal separator is "/". Copy |
| 240 |
* through. |
| 241 |
*/ |
| 242 |
std::copy_n(stdPath, stdPathLen, &realPath[realPathUsed]); |
| 243 |
#else |
| 244 |
/* Separator might be / on non-unix, but the fallback path works |
| 245 |
* regardless of whether separator is "/". |
| 246 |
*/ |
| 247 |
const auto csep = *sep; |
| 248 |
const auto a = [csep](char c) { |
| 249 |
return c == '/' ? csep : c; |
| 250 |
}; |
| 251 |
std::transform(stdPath, &stdPath[stdPathLen], &realPath[realPathUsed], a); |
| 252 |
#endif |
| 253 |
return 1; |
| 254 |
} |
| 255 |
|
| 256 |
int PHYSFSX_rename(const char *oldpath, const char *newpath) |
| 257 |
{ |
| 258 |
std::array<char, PATH_MAX> old, n; |
| 259 |
if (!PHYSFSX_getRealPath(oldpath, old) || !PHYSFSX_getRealPath(newpath, n)) |
| 260 |
return -1; |
| 261 |
return (rename(old.data(), n.data()) == 0); |
| 262 |
} |
| 263 |
|
| 264 |
static PHYSFSX_uncounted_list trim_physfs_list(PHYSFSX_uncounted_list list, char **const iter_first_unused) |
| 265 |
{ |
| 266 |
*iter_first_unused = nullptr; |
| 267 |
const auto old_begin = list.get(); |
| 268 |
const auto r = reinterpret_cast<char **>(realloc(old_begin, (iter_first_unused - old_begin + 1) * sizeof(char *))); // save a bit of memory (or a lot?) |
| 269 |
/* iter_first_unused, old_begin are now invalid since realloc may have moved the block */ |
| 270 |
return r ? PHYSFSX_uncounted_list{(list.release(), r)} : std::move(list); |
| 271 |
} |
| 272 |
|
| 273 |
static PHYSFSX_uncounted_list PHYSFSX_findPredicateFiles(const char *path, auto &&predicate) |
| 274 |
{ |
| 275 |
PHYSFSX_uncounted_list list{PHYSFS_enumerateFiles(path)}; |
| 276 |
if (!list) |
| 277 |
return nullptr; // out of memory: not so good |
| 278 |
char **j = list.get(); |
| 279 |
for (const auto i : list) |
| 280 |
{ |
| 281 |
if (predicate(i)) |
| 282 |
*j++ = i; |
| 283 |
else |
| 284 |
free(i); |
| 285 |
} |
| 286 |
return trim_physfs_list(std::move(list), j); |
| 287 |
} |
| 288 |
|
| 289 |
// Find files at path that have an extension listed in exts |
| 290 |
// The extension list exts must be NULL-terminated, with each ext beginning with a '.' |
| 291 |
PHYSFSX_uncounted_list PHYSFSX_findFiles(const char *path, const std::ranges::subrange<const file_extension_t *> exts) |
| 292 |
{ |
| 293 |
const auto predicate = [&](const char *i) { |
| 294 |
return PHYSFSX_checkMatchingExtension(i, exts); |
| 295 |
}; |
| 296 |
return PHYSFSX_findPredicateFiles(path, predicate); |
| 297 |
} |
| 298 |
|
| 299 |
// Same function as above but takes a real directory as second argument, only adding files originating from this directory. |
| 300 |
// This can be used to further seperate files in search path but it must be made sure realpath is properly formatted. |
| 301 |
PHYSFSX_uncounted_list PHYSFSX_findabsoluteFiles(const char *path, const char *realpath, const std::ranges::subrange<const file_extension_t *> exts) |
| 302 |
{ |
| 303 |
const auto predicate = [&](const char *i) { |
| 304 |
return PHYSFSX_checkMatchingExtension(i, exts) && (!strcmp(PHYSFS_getRealDir(i), realpath)); |
| 305 |
}; |
| 306 |
return PHYSFSX_findPredicateFiles(path, predicate); |
| 307 |
} |
| 308 |
|
| 309 |
int PHYSFSX_exists_ignorecase(char *filename) |
| 310 |
{ |
| 311 |
return PHYSFSEXT_locateCorrectCase(filename) == PHYSFSX_case_search_result::success; |
| 312 |
} |
| 313 |
|
| 314 |
//Open a file for reading, set up a buffer |
| 315 |
std::pair<RAIINamedPHYSFS_File, PHYSFS_ErrorCode> PHYSFSX_openReadBuffered_updateCase(char *const filename) |
| 316 |
{ |
| 317 |
/* The caller provided the filename buffer, so use that buffer for the |
| 318 |
* reported filename. |
| 319 |
*/ |
| 320 |
return PHYSFSX_openReadBuffered_internal(filename, filename); |
| 321 |
} |
| 322 |
|
| 323 |
//Open a file for reading, set up a buffer |
| 324 |
std::pair<RAIINamedPHYSFS_File, PHYSFS_ErrorCode> PHYSFSX_openReadBuffered(const char *filename) |
| 325 |
{ |
| 326 |
char filename2[PATH_MAX]; |
| 327 |
snprintf(filename2, sizeof(filename2), "%s", filename); |
| 328 |
/* Use the original filename in any error messages. This is close enough |
| 329 |
* that the user should be able to identify the bad file, and avoids the |
| 330 |
* need to allocate and return storage for the filename. In almost all |
| 331 |
* cases, the filename will never be seen, since it is only shown if an |
| 332 |
* error occurs when reading the file. |
| 333 |
*/ |
| 334 |
return PHYSFSX_openReadBuffered_internal(filename2, filename); |
| 335 |
} |
| 336 |
|
| 337 |
//Open a file for writing, set up a buffer |
| 338 |
std::pair<RAIIPHYSFS_File, PHYSFS_ErrorCode> PHYSFSX_openWriteBuffered(const char *filename) |
| 339 |
{ |
| 340 |
PHYSFS_uint64 bufSize = 1024*1024; // hmm, seems like an OK size. |
| 341 |
RAIIPHYSFS_File fp{PHYSFS_openWrite(filename)}; |
| 342 |
if (!fp) |
| 343 |
return {nullptr, PHYSFS_getLastErrorCode()}; |
| 344 |
while (!PHYSFS_setBuffer(fp, bufSize) && bufSize) |
| 345 |
bufSize /= 2; |
| 346 |
return {std::move(fp), PHYSFS_ERR_OK}; |
| 347 |
} |
| 348 |
|
| 349 |
static uint8_t add_archives_to_search_path() |
| 350 |
{ |
| 351 |
uint8_t content_updated{}; |
| 352 |
// find files in Searchpath ... |
| 353 |
// if found, add them... |
| 354 |
const auto s{PHYSFSX_findFiles("", archive_exts)}; |
| 355 |
if (!s) |
| 356 |
return content_updated; |
| 357 |
for (const auto i : s) |
| 358 |
{ |
| 359 |
std::array<char, PATH_MAX> realfile; |
| 360 |
if (PHYSFSX_getRealPath(i, realfile) && PHYSFS_mount(realfile.data(), nullptr, 0)) |
| 361 |
{ |
| 362 |
con_printf(CON_DEBUG, "PHYSFS: Added %s to Search Path",realfile.data()); |
| 363 |
content_updated = 1; |
| 364 |
} |
| 365 |
} |
| 366 |
return content_updated; |
| 367 |
} |
| 368 |
|
| 369 |
static uint8_t add_demo_files_to_search_path(uint8_t content_updated) |
| 370 |
{ |
| 371 |
// find files in DEMO_DIR ... |
| 372 |
// if found, add them... |
| 373 |
for (const auto i : PHYSFSX_findFiles(DEMO_DIR, archive_exts)) |
| 374 |
{ |
| 375 |
char demofile[PATH_MAX]; |
| 376 |
snprintf(demofile, sizeof(demofile), DEMO_DIR "%s", i); |
| 377 |
std::array<char, PATH_MAX> realfile; |
| 378 |
if (PHYSFSX_getRealPath(demofile, realfile) && PHYSFS_mount(realfile.data(), DEMO_DIR, 0)) |
| 379 |
{ |
| 380 |
con_printf(CON_DEBUG, "PHYSFS: Added %s to " DEMO_DIR, realfile.data()); |
| 381 |
content_updated = 1; |
| 382 |
} |
| 383 |
} |
| 384 |
return content_updated; |
| 385 |
} |
| 386 |
|
| 387 |
/* |
| 388 |
* Add archives to the game. |
| 389 |
* 1) archives from Sharepath/Data to extend/replace builtin game content |
| 390 |
* 2) archived demos |
| 391 |
*/ |
| 392 |
void PHYSFSX_addArchiveContent() |
| 393 |
{ |
| 394 |
con_puts(CON_DEBUG, "PHYSFS: Adding archives to the game."); |
| 395 |
auto content_updated{add_archives_to_search_path()}; |
| 396 |
content_updated = add_demo_files_to_search_path(content_updated); |
| 397 |
if (content_updated) |
| 398 |
{ |
| 399 |
con_puts(CON_DEBUG, "Game content updated!"); |
| 400 |
PHYSFSX_listSearchPathContent(); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
// Removes content added above when quitting game |
| 405 |
void PHYSFSX_removeArchiveContent() |
| 406 |
{ |
| 407 |
// find files in Searchpath ... |
| 408 |
// if found, remove them... |
| 409 |
for (const auto i : PHYSFSX_findFiles("", archive_exts)) |
| 410 |
{ |
| 411 |
std::array<char, PATH_MAX> realfile; |
| 412 |
if (PHYSFSX_getRealPath(i, realfile)) |
| 413 |
PHYSFS_unmount(realfile.data()); |
| 414 |
} |
| 415 |
// find files in DEMO_DIR ... |
| 416 |
// if found, remove them... |
| 417 |
for (const auto i : PHYSFSX_findFiles(DEMO_DIR, archive_exts)) |
| 418 |
{ |
| 419 |
char demofile[PATH_MAX]; |
| 420 |
snprintf(demofile, sizeof(demofile), DEMO_DIR "%s", i); |
| 421 |
std::array<char, PATH_MAX> realfile; |
| 422 |
if (PHYSFSX_getRealPath(demofile, realfile)) |
| 423 |
PHYSFS_unmount(realfile.data()); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
void PHYSFSX_read_helper_report_error(const char *const filename, const unsigned line, const char *const func, const NamedPHYSFS_File file) |
| 428 |
{ |
| 429 |
(Error)(filename, line, func, "reading file %s at %lu", file.filename, static_cast<unsigned long>((PHYSFS_tell)(file.fp))); |
| 430 |
} |
| 431 |
|
| 432 |
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *const name, physfs_search_path position) |
| 433 |
{ |
| 434 |
auto pathname = std::make_unique<std::array<char, PATH_MAX>>(); |
| 435 |
if (PHYSFSX_addRelToSearchPath(name, *pathname.get(), position) == PHYSFS_ERR_OK) |
| 436 |
return RAIIPHYSFS_ComputedPathMount(std::move(pathname)); |
| 437 |
return nullptr; |
| 438 |
} |
| 439 |
|
| 440 |
} |