improve preferences file save/lod robustness

This commit is contained in:
cy384 2020-10-04 13:40:19 -04:00
parent f8a6cd1043
commit ee0c4dcf87
1 changed files with 17 additions and 10 deletions

View File

@ -43,14 +43,14 @@ int save_prefs(void)
if (ok) if (ok)
{ {
e = FSMakeFSSpec(foundVRefNum, foundDirID, PREFERENCES_FILENAME, &pref_file); e = FSMakeFSSpec(foundVRefNum, foundDirID, PREFERENCES_FILENAME, &pref_file);
if (e == fnfErr) // file doesn't exist, but is a valid path
{ // if the file exists, delete it
// so make the file if (e == noErr) FSpDelete(&pref_file);
// and then make it
e = FSpCreate(&pref_file, creator_type, pref_type, smSystemScript); e = FSpCreate(&pref_file, creator_type, pref_type, smSystemScript);
if (e != noErr) ok = 0; if (e != noErr) ok = 0;
} }
else if (e != noErr) ok = 0;
}
// open the file // open the file
if (ok) if (ok)
@ -80,7 +80,14 @@ int save_prefs(void)
snprintf(output_buffer+i, prefs.port[0]+1, "%s", prefs.port+1); i += prefs.port[0]; snprintf(output_buffer+i, prefs.port[0]+1, "%s", prefs.port+1); i += prefs.port[0];
i += snprintf(output_buffer+i, write_length-i, "\n"); i += snprintf(output_buffer+i, write_length-i, "\n");
if (prefs.privkey_path && prefs.privkey_path[0] != '\0')
{
i += snprintf(output_buffer+i, write_length-i, "%s\n%s\n", prefs.privkey_path, prefs.pubkey_path); i += snprintf(output_buffer+i, write_length-i, "%s\n%s\n", prefs.privkey_path, prefs.pubkey_path);
}
else
{
i += snprintf(output_buffer+i, write_length-i, "\n\n");
}
// tell it to write all bytes // tell it to write all bytes
long int bytes = i; long int bytes = i;
@ -1129,6 +1136,7 @@ void* read_thread(void* arg)
} }
else else
{ {
printf_i("failed!\r\n");
if (rc == LIBSSH2_ERROR_AUTHENTICATION_FAILED && prefs.auth_type == USE_PASSWORD) StopAlert(ALRT_PW_FAIL, nil); if (rc == LIBSSH2_ERROR_AUTHENTICATION_FAILED && prefs.auth_type == USE_PASSWORD) StopAlert(ALRT_PW_FAIL, nil);
else if (rc == LIBSSH2_ERROR_FILE) StopAlert(ALRT_FILE_FAIL, nil); else if (rc == LIBSSH2_ERROR_FILE) StopAlert(ALRT_FILE_FAIL, nil);
else if (rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED) else if (rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED)
@ -1145,8 +1153,7 @@ void* read_thread(void* arg)
} }
} }
// if we logged in, save the connection preferences, since we know they're ok save_prefs();
if (ok) save_prefs();
// if we logged in, open and set up the tty // if we logged in, open and set up the tty
if (ok) if (ok)