Fix failure to load a blend file behaving as if a new file is loaded

When a blend file failed to load from the command line,
blender would load a blend file with the path set to the location
that could not be loaded.

Now the command line argument is only treated as a new file
if the file does not already exist. Failure to load a file that
is found always exists with an error.
This commit is contained in:
Campbell Barton
2023-05-29 11:38:11 +10:00
parent 19a9941816
commit 2d81e65703

View File

@@ -2089,20 +2089,38 @@ static int arg_handle_load_file(int UNUSED(argc), const char **argv, void *data)
return -1;
}
if (BKE_blendfile_extension_check(filepath)) {
/* Just pretend a file was loaded, so the user can press Save and it'll
* save at the filepath from the CLI. */
STRNCPY(G_MAIN->filepath, filepath);
printf("... opened default scene instead; saving will write to: %s\n", filepath);
const char *error_msg = NULL;
if (BLI_exists(filepath)) {
/* When a file is found but can't be loaded, handling it as a new file
* could cause it to be unintentionally overwritten (data loss).
* Further this is almost certainly not that a user would expect or want.
* If they do, they can delete the file beforehand. */
error_msg = "file could not be loaded";
}
else {
fprintf(
stderr,
"Error: argument has no '.blend' file extension, not using as new file, exiting! %s\n",
filepath);
else if (!BKE_blendfile_extension_check(filepath)) {
/* Unrelated arguments should not be treated as new blend files. */
error_msg = "argument has no '.blend' file extension, not using as new file";
}
if (error_msg) {
fprintf(stderr, "Error: %s, exiting! %s\n", error_msg, filepath);
G.is_break = true;
WM_exit(C);
/* Unreachable, return for clarity. */
return -1;
}
/* Behave as if a file was loaded, calling "Save" will write to the `filepath` from the CLI.
*
* WARNING: The path referenced may be incorrect, no attempt is made to validate the path
* here or check that writing to it will work. If the users enters the path of a directory
* that doesn't exist (for e.g.) saving will fail.
* Attempting to create the file at this point is possible but likely to cause more
* trouble than it's worth (what with network drives), removable devices ... etc. */
STRNCPY(G_MAIN->filepath, filepath);
printf("... opened default scene instead; saving will write to: %s\n", filepath);
}
return 0;