Cleanup: simplify file saving logic

Revert part of the fix from 073669dd85
that initialized the file-path on first save as it's no longer needed.

Also remove relbase argument to BLI_path_normalize as the destination
file paths shouldn't use relative locations.
This commit is contained in:
Campbell Barton 2021-12-16 16:17:26 +11:00
parent 4e98d974b5
commit 15c3617009
3 changed files with 24 additions and 21 deletions

View File

@ -60,11 +60,6 @@ struct BlendFileWriteParams {
uint use_save_versions : 1;
/** On write, restore paths after editing them (see #BLO_WRITE_PATH_REMAP_RELATIVE). */
uint use_save_as_copy : 1;
/**
* Saving from the UI writes into the #Main.filepath, so a check for the `filepath`
* not having been set is needed.
*/
uint use_save_first_time : 1;
uint use_userdef : 1;
const struct BlendThumbnail *thumb;
};

View File

@ -1318,6 +1318,8 @@ bool BLO_write_file(Main *mainvar,
const struct BlendFileWriteParams *params,
ReportList *reports)
{
BLI_assert(!BLI_path_is_rel(filepath));
char tempname[FILE_MAX + 1];
WriteWrap ww;
@ -1326,8 +1328,7 @@ bool BLO_write_file(Main *mainvar,
const bool use_save_as_copy = params->use_save_as_copy;
const bool use_userdef = params->use_userdef;
const BlendThumbnail *thumb = params->thumb;
const bool relbase_valid = (mainvar->filepath[0] != '\0') &&
(params->use_save_first_time == false);
const bool relbase_valid = (mainvar->filepath[0] != '\0');
/* path backup/restore */
void *path_list_backup = NULL;
@ -1351,6 +1352,13 @@ bool BLO_write_file(Main *mainvar,
return 0;
}
if (remap_mode == BLO_WRITE_PATH_REMAP_ABSOLUTE) {
/* Paths will already be absolute, no remapping to do. */
if (relbase_valid == false) {
remap_mode = BLO_WRITE_PATH_REMAP_NONE;
}
}
/* Remapping of relative paths to new file location. */
if (remap_mode != BLO_WRITE_PATH_REMAP_NONE) {
@ -1361,14 +1369,20 @@ bool BLO_write_file(Main *mainvar,
}
}
/* The source path only makes sense to set if the file was saved (`relbase_valid`). */
char dir_src[FILE_MAX];
char dir_dst[FILE_MAX];
BLI_split_dir_part(mainvar->filepath, dir_src, sizeof(dir_src));
BLI_split_dir_part(filepath, dir_dst, sizeof(dir_dst));
/* Just in case there is some subtle difference. */
BLI_path_normalize(mainvar->filepath, dir_dst);
BLI_path_normalize(mainvar->filepath, dir_src);
/* Normalize the paths in case there is some subtle difference (so they can be compared). */
if (relbase_valid) {
BLI_split_dir_part(mainvar->filepath, dir_src, sizeof(dir_src));
BLI_path_normalize(NULL, dir_src);
}
else {
dir_src[0] = '\0';
}
BLI_split_dir_part(filepath, dir_dst, sizeof(dir_dst));
BLI_path_normalize(NULL, dir_dst);
/* Only for relative, not relative-all, as this means making existing paths relative. */
if (remap_mode == BLO_WRITE_PATH_REMAP_RELATIVE) {
@ -1394,6 +1408,7 @@ bool BLO_write_file(Main *mainvar,
switch (remap_mode) {
case BLO_WRITE_PATH_REMAP_RELATIVE:
/* Saved, make relative paths relative to new location (if possible). */
BLI_assert(relbase_valid);
BKE_bpath_relative_rebase(mainvar, dir_src, dir_dst, NULL);
break;
case BLO_WRITE_PATH_REMAP_RELATIVE_ALL:
@ -1402,6 +1417,7 @@ bool BLO_write_file(Main *mainvar,
break;
case BLO_WRITE_PATH_REMAP_ABSOLUTE:
/* Make all absolute (when requested or unsaved). */
BLI_assert(relbase_valid);
BKE_bpath_absolute_convert(mainvar, dir_src, NULL);
break;
case BLO_WRITE_PATH_REMAP_NONE:

View File

@ -1813,24 +1813,16 @@ static bool wm_file_write(bContext *C,
ED_editors_flush_edits(bmain);
/* First time saving. */
/* XXX(ton): temp solution to solve bug, real fix coming. */
const bool relbase_valid = (bmain->filepath[0] != '\0');
if ((relbase_valid == false) && (use_save_as_copy == false)) {
STRNCPY(bmain->filepath, filepath);
}
/* XXX(ton): temp solution to solve bug, real fix coming. */
bmain->recovered = 0;
if (BLO_write_file(CTX_data_main(C),
if (BLO_write_file(bmain,
filepath,
fileflags,
&(const struct BlendFileWriteParams){
.remap_mode = remap_mode,
.use_save_versions = true,
.use_save_as_copy = use_save_as_copy,
.use_save_first_time = !relbase_valid,
.thumb = thumb,
},
reports)) {