Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions code/cfile/cfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "cfile/cfile.h"
#include "cfile/cfilearchive.h"
#include "cfile/cfilesystem.h"
#include "cfile/mod_bootstrap.h"
#include "osapi/osapi.h"
#include "parse/encrypt.h"
#include "cfilesystem.h"
Expand Down Expand Up @@ -183,41 +184,31 @@ int cfile_init(const char *exe_dir, const char *cdrom_dir)
return 0;
}

char buf[CFILE_ROOT_DIRECTORY_LEN];

#ifndef __ANDROID__
if (const auto bootstrap = mod_bootstrap_get()) {
// Keep relative settings paths anchored to the launch directory.
strcpy_s(Cfile_root_dir, bootstrap->workfolder.c_str());
} else {
char buf[CFILE_ROOT_DIRECTORY_LEN];
#ifndef __ANDROID__
strncpy(buf, exe_dir, CFILE_ROOT_DIRECTORY_LEN - 1);
#else
#else
(void)exe_dir;
strncpy(buf, os_android_get_working_folder_path().c_str(), CFILE_ROOT_DIRECTORY_LEN - 1);
#endif

buf[CFILE_ROOT_DIRECTORY_LEN - 1] = '\0';
#endif
buf[CFILE_ROOT_DIRECTORY_LEN - 1] = '\0';
if (cfile_in_root_dir(buf)) {
os::dialogs::Message(os::dialogs::MESSAGEBOX_ERROR, "FreeSpace2/Fred2 cannot be run from a drive root directory!");
return 1;
}

// are we in a root directory?
if(cfile_in_root_dir(buf)){
os::dialogs::Message(os::dialogs::MESSAGEBOX_ERROR, "FreeSpace2/Fred2 cannot be run from a drive root directory!");
return 1;
}
// Remove the executable name to get the game directory.
*strrchr(buf, DIR_SEPARATOR_CHAR) = '\0';
cfile_chdir(buf);
strcpy_s(Cfile_root_dir, buf);
}

// This needs to be set here because cf_build_secondary_filelist assumes it to be true
// The index builder requires CFILE to be marked initialized.
cfile_inited = 1;

/*
* Determine the executable's directory. Note that DIR_SEPARATOR_CHAR
* is guaranteed to be found in the string else cfile_in_root_dir()
* would have failed.
*/

char *p;

p = strrchr(buf, DIR_SEPARATOR_CHAR);
*p = '\0';

cfile_chdir(buf);

// set root directory
strcpy_s(Cfile_root_dir, buf);
strcpy_s(Cfile_user_dir, os_get_config_path().c_str());

// Initialize the block list with proper data
Expand Down Expand Up @@ -467,7 +458,9 @@ int cf_delete(const char *filename, int path_type, uint32_t location_flags)

Assert(CF_TYPE_SPECIFIED(path_type));

cf_create_default_path_string(longname, path_type, filename, location_flags);
if (!cf_create_default_path_string(longname, path_type, filename, location_flags, true)) {
return 0;
}

return (_unlink(longname.c_str()) != -1);
}
Expand Down Expand Up @@ -531,7 +524,9 @@ void cf_attrib(const char *filename, int set, int clear, int dir_type)

Assert( CF_TYPE_SPECIFIED(dir_type) );

cf_create_default_path_string(longname, dir_type, filename);
if (!cf_create_default_path_string(longname, dir_type, filename, static_cast<uint32_t>(CF_LOCATION_ALL), true)) {
return;
}

FILE *fp = fopen(longname.c_str(), "rb");
if (fp) {
Expand All @@ -552,8 +547,11 @@ int cf_rename(const char *old_name, const char *name, int dir_type)
SCP_string old_longname;
SCP_string new_longname;

cf_create_default_path_string(old_longname, dir_type, old_name);
cf_create_default_path_string(new_longname, dir_type, name);
const auto flags = static_cast<uint32_t>(CF_LOCATION_ALL);
if (!cf_create_default_path_string(old_longname, dir_type, old_name, flags, true) ||
!cf_create_default_path_string(new_longname, dir_type, name, flags, true)) {
return CF_RENAME_FAIL_ACCESS;
}

ret_code = rename(old_longname.c_str(), new_longname.c_str());
if(ret_code != 0){
Expand Down Expand Up @@ -617,7 +615,9 @@ void cf_create_directory(int dir_type, uint32_t location_flags)
int i;

for (i=num_dirs-1; i>=0; i-- ) {
cf_create_default_path_string(longname, dir_tree[i], nullptr, location_flags);
if (!cf_create_default_path_string(longname, dir_tree[i], nullptr, location_flags, true)) {
return;
}
if (stat(longname.c_str(), &statbuf) != 0) {
mprintf(( "CFILE: Creating new directory '%s'\n", longname.c_str() ));
mkdir_recursive(longname.c_str());
Expand Down Expand Up @@ -674,7 +674,9 @@ CFILE* _cfopen(const char* source, int line, const char* file_path, const char*
// Create the directory if necessary
cf_create_directory(dir_type, location_flags);

cf_create_default_path_string(longname, dir_type, file_path, location_flags);
if (!cf_create_default_path_string(longname, dir_type, file_path, location_flags, true)) {
return nullptr;
}
}

// JOHN: TODO, you should create the path if it doesn't exist.
Expand Down
69 changes: 45 additions & 24 deletions code/cfile/cfilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "cfile/cfile.h"
#include "cfile/cfilesystem.h"
#include "cfile/mod_bootstrap.h"
#include "cmdline/cmdline.h"
#include "globalincs/pstypes.h"
#include "def_files/def_files.h"
Expand All @@ -57,6 +58,7 @@ typedef struct cf_root {
SCP_string path; // Contains something like c:\projects\freespace or c:\projects\freespace\freespace.vp
int roottype; // CF_ROOTTYPE_PATH = Path, CF_ROOTTYPE_PACK =Pack file, CF_ROOTTYPE_MEMORY=In memory
uint32_t location_flags;
bool read_only = false;

#ifdef SCP_UNIX
// map of existing case sensitive paths
Expand Down Expand Up @@ -569,6 +571,20 @@ void normalize_directory_separators(SCP_string &str)
std::replace(str.begin(), str.end(), bad_sep, DIR_SEPARATOR_CHAR);
}

static void cf_add_mod_root(SCP_string path, uint32_t location, bool read_only = false)
{
if (path.back() != DIR_SEPARATOR_CHAR) {
path += DIR_SEPARATOR_CHAR;
}
cf_root* root = cf_create_root();
root->path = std::move(path);
root->location_flags = location;
root->roottype = CF_ROOTTYPE_PATH;
root->read_only = read_only;
cf_init_root_pathtypes(root);
cf_build_pack_list(root);
}

static void cf_add_mod_roots(const char* rootDirectory, uint32_t basic_location)
{
if (Cmdline_mod)
Expand All @@ -594,18 +610,8 @@ static void cf_add_mod_roots(const char* rootDirectory, uint32_t basic_location)
// normalize the path to the native path format
normalize_directory_separators(rootPath);

cf_root* root = cf_create_root();

root->path = std::move(rootPath);
if (primary) {
root->location_flags = basic_location | CF_LOCATION_TYPE_PRIMARY_MOD;
} else {
root->location_flags = basic_location | CF_LOCATION_TYPE_SECONDARY_MODS;
}

root->roottype = CF_ROOTTYPE_PATH;
cf_init_root_pathtypes(root);
cf_build_pack_list(root);
const auto mod_type = primary ? CF_LOCATION_TYPE_PRIMARY_MOD : CF_LOCATION_TYPE_SECONDARY_MODS;
cf_add_mod_root(std::move(rootPath), basic_location | mod_type);

primary = false;
}
Expand All @@ -618,20 +624,24 @@ void cf_build_root_list(const char *cdrom_dir)
Num_path_roots = 0;

cf_root *root = nullptr;
const auto bootstrap = mod_bootstrap_get();
const bool has_mods = bootstrap ? !bootstrap->roots.empty() : Cmdline_mod && *Cmdline_mod;

if (os_is_legacy_mode())
{
// =========================================================================
#ifdef WIN32
// Nothing to do here, Windows uses the current directory as the base
#else
cf_add_mod_roots(os_get_legacy_user_dir(), CF_LOCATION_ROOT_USER);
if (!bootstrap) {
cf_add_mod_roots(os_get_legacy_user_dir(), CF_LOCATION_ROOT_USER);
}

root = cf_create_root();
root->path = os_get_legacy_user_dir();

root->location_flags |= CF_LOCATION_ROOT_USER | CF_LOCATION_TYPE_ROOT;
if (Cmdline_mod == nullptr || strlen(Cmdline_mod) <= 0) {
if (!has_mods) {
// If there are no mods then the root is the primary mod
root->location_flags |= CF_LOCATION_TYPE_PRIMARY_MOD;
}
Expand All @@ -653,7 +663,9 @@ void cf_build_root_list(const char *cdrom_dir)
{
// =========================================================================
// now look for mods under the users HOME directory to use before system ones
cf_add_mod_roots(Cfile_user_dir, CF_LOCATION_ROOT_USER);
if (!bootstrap) {
cf_add_mod_roots(Cfile_user_dir, CF_LOCATION_ROOT_USER);
}
// =========================================================================

// =========================================================================
Expand All @@ -662,7 +674,7 @@ void cf_build_root_list(const char *cdrom_dir)
root->path = Cfile_user_dir;

root->location_flags |= CF_LOCATION_ROOT_USER | CF_LOCATION_TYPE_ROOT;
if (Cmdline_mod == nullptr || strlen(Cmdline_mod) <= 0) {
if (!has_mods) {
// If there are no mods then the root is the primary mod
root->location_flags |= CF_LOCATION_TYPE_PRIMARY_MOD;
}
Expand All @@ -680,11 +692,11 @@ void cf_build_root_list(const char *cdrom_dir)
// =========================================================================
}

char working_directory[CF_MAX_PATHNAME_LENGTH];

if ( !_getcwd(working_directory, CF_MAX_PATHNAME_LENGTH ) ) {
char cwd[CF_MAX_PATHNAME_LENGTH];
if (!bootstrap && !_getcwd(cwd, sizeof(cwd))) {
Error(LOCATION, "Can't get current working directory -- %d", errno );
}
const char* working_directory = bootstrap ? Cfile_root_dir : cwd;

if (Cmdline_override_data) {

Expand All @@ -709,13 +721,21 @@ void cf_build_root_list(const char *cdrom_dir)
}

//now that the _override root is finished(or not), handle all mods
cf_add_mod_roots(working_directory, CF_LOCATION_ROOT_GAME);
if (bootstrap) {
auto mod_type = CF_LOCATION_TYPE_PRIMARY_MOD;
for (const auto& path : bootstrap->roots) {
cf_add_mod_root(path, CF_LOCATION_ROOT_GAME | mod_type, true);
mod_type = CF_LOCATION_TYPE_SECONDARY_MODS;
}
} else {
cf_add_mod_roots(working_directory, CF_LOCATION_ROOT_GAME);
}

//finally, handle the default root.
root = cf_create_root();

root->location_flags |= CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT;
if (Cmdline_mod == nullptr || strlen(Cmdline_mod) <= 0) {
if (!has_mods) {
// If there are no mods then the root is the primary mod
root->location_flags |= CF_LOCATION_TYPE_PRIMARY_MOD;
}
Expand Down Expand Up @@ -2399,7 +2419,8 @@ int cf_create_default_path_string(char* path, uint path_max, int pathtype, const
// filename - optional, if set, tacks the filename onto end of path.
// Output: path - Fully qualified pathname.
//Returns 0 if the result would be too long (invalid result)
int cf_create_default_path_string(SCP_string& path, int pathtype, const char* filename, uint32_t _location_flags)
int cf_create_default_path_string(SCP_string& path, int pathtype, const char* filename,
uint32_t _location_flags, bool for_write)
{
uint32_t location_flags = _location_flags;

Expand All @@ -2424,7 +2445,7 @@ int cf_create_default_path_string(SCP_string& path, int pathtype, const char* fi
for (auto i = 0; i < Num_roots; ++i) {
auto current_root = cf_get_root(i);

if (current_root->roottype != CF_ROOTTYPE_PATH) {
if (current_root->roottype != CF_ROOTTYPE_PATH || (for_write && current_root->read_only)) {
// We want a "real" path here so only path roots are valid
continue;
}
Expand All @@ -2437,7 +2458,7 @@ int cf_create_default_path_string(SCP_string& path, int pathtype, const char* fi
}

if (!root) {
if (filename == nullptr) {
if (filename == nullptr || (for_write && mod_bootstrap_get())) {
return 0;
}
path.assign(filename);
Expand Down
2 changes: 1 addition & 1 deletion code/cfile/cfilesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ bool cf_check_location_flags(uint32_t check_flags, uint32_t desired_flags);
int cf_create_default_path_string(char* path, uint path_max, int pathtype, const char* filename = nullptr,
uint32_t location_flags = CF_LOCATION_ALL);
int cf_create_default_path_string(SCP_string& path, int pathtype, const char* filename = nullptr,
uint32_t location_flags = CF_LOCATION_ALL);
uint32_t location_flags = CF_LOCATION_ALL, bool for_write = false);

#endif //_CFILESYSTEM_H
Loading
Loading