Skip to content

Commit

Permalink
Fail more gracefully if sub-profile default.txt missing
Browse files Browse the repository at this point in the history
  • Loading branch information
IanSB committed Jan 19, 2021
1 parent d6a7b15 commit 515539d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
70 changes: 35 additions & 35 deletions src/filesystem.c
Expand Up @@ -438,6 +438,41 @@ unsigned int file_read_profile(char *profile_name, int saved_config_number, char
unsigned int num_written = 0;
init_filesystem();

if (updatecmd) {
char name[100];
sprintf(name, "/profile_%s.txt", cpld->name);
result = f_open(&file, name, FA_WRITE | FA_CREATE_ALWAYS);
if (result != FR_OK) {
log_warn("Failed to open %s (result = %d)", path, result);
close_filesystem();
return 0;
} else {

sprintf(cmdline, "profile=%s\r\n", profile_name);
int cmdlength = strlen(cmdline);
sprintf(cmdline + cmdlength, "saved_profile=%d\r\n", saved_config_number);
cmdlength += strlen(cmdline + cmdlength);
result = f_write(&file, cmdline, cmdlength, &num_written);

if (result != FR_OK) {
log_warn("Failed to write %s (result = %d)", path, result);
close_filesystem();
return 0;
} else if (num_written != cmdlength) {
log_warn("%s is incomplete (%d < %d bytes)", path, num_written, cmdlength);
close_filesystem();
return 0;
}

result = f_close(&file);
if (result != FR_OK) {
log_warn("Failed to close %s (result = %d)", path, result);
close_filesystem();
return 0;
}
}
}

if (saved_config_number == 0) {
if (sub_profile_name != NULL) {
sprintf(path, "%s/%s/%s/%s.txt", SAVED_PROFILE_BASE, cpld->name, profile_name, sub_profile_name);
Expand Down Expand Up @@ -487,41 +522,6 @@ unsigned int file_read_profile(char *profile_name, int saved_config_number, char
return 0;
}

if (updatecmd) {
char name[100];
sprintf(name, "/profile_%s.txt", cpld->name);
result = f_open(&file, name, FA_WRITE | FA_CREATE_ALWAYS);
if (result != FR_OK) {
log_warn("Failed to open %s (result = %d)", path, result);
close_filesystem();
return 0;
} else {

sprintf(cmdline, "profile=%s\r\n", profile_name);
int cmdlength = strlen(cmdline);
sprintf(cmdline + cmdlength, "saved_profile=%d\r\n", saved_config_number);
cmdlength += strlen(cmdline + cmdlength);
result = f_write(&file, cmdline, cmdlength, &num_written);

if (result != FR_OK) {
log_warn("Failed to write %s (result = %d)", path, result);
close_filesystem();
return 0;
} else if (num_written != cmdlength) {
log_warn("%s is incomplete (%d < %d bytes)", path, num_written, cmdlength);
close_filesystem();
return 0;
}

result = f_close(&file);
if (result != FR_OK) {
log_warn("Failed to close %s (result = %d)", path, result);
close_filesystem();
return 0;
}
}

}
close_filesystem();

log_debug("Profile loading complete");
Expand Down
21 changes: 12 additions & 9 deletions src/osd.c
Expand Up @@ -4175,15 +4175,18 @@ void load_profiles(int profile_number, int save_selected) {
sub_profile_buffers[0][0] = 0;
if (has_sub_profiles[profile_number]) {
bytes = file_read_profile(profile_names[profile_number], get_saved_config_number(), DEFAULT_STRING, save_selected, sub_default_buffer, MAX_BUFFER_SIZE - 4);
if (bytes) {
size_t count = 0;
scan_sub_profiles(sub_profile_names, profile_names[profile_number], &count);
if (count) {
features[F_SUBPROFILE].max = count - 1;
for (int i = 0; i < count; i++) {
file_read_profile(profile_names[profile_number], get_saved_config_number(), sub_profile_names[i], 0, sub_profile_buffers[i], MAX_BUFFER_SIZE - 4);
get_autoswitch_geometry(sub_profile_buffers[i], i);
}
if (!bytes) {
//if auto switching default.txt missing put a default value in buffer
strcpy(sub_default_buffer,"auto_switch=0\r\n\0");
log_info("Sub-profile default.txt missing, substituting %s", sub_default_buffer);
}
size_t count = 0;
scan_sub_profiles(sub_profile_names, profile_names[profile_number], &count);
if (count) {
features[F_SUBPROFILE].max = count - 1;
for (int i = 0; i < count; i++) {
file_read_profile(profile_names[profile_number], get_saved_config_number(), sub_profile_names[i], 0, sub_profile_buffers[i], MAX_BUFFER_SIZE - 4);
get_autoswitch_geometry(sub_profile_buffers[i], i);
}
}
} else {
Expand Down

0 comments on commit 515539d

Please sign in to comment.