Improve error handling around fchown

Address the feedback from the prior commit:
- Change the sense of return value testing to match more common
comparison idiom
- Test result of fchmod as well as fchown
- Change sense of return value testing around wrename as well
- Include errno where possible in error message
This commit is contained in:
Jeff Kowalski 2016-03-12 12:26:01 -08:00
parent 46b9f263ac
commit a7012648fe
2 changed files with 11 additions and 7 deletions

View File

@ -866,9 +866,10 @@ bool env_universal_t::sync(callback_data_list_t *callbacks)
struct stat sbuf;
if (wstat(vars_path, &sbuf) >= 0)
{
if (0 > fchown(private_fd, sbuf.st_uid, sbuf.st_gid))
if (fchown(private_fd, sbuf.st_uid, sbuf.st_gid) == -1)
UNIVERSAL_LOG("fchown() failed");
fchmod(private_fd, sbuf.st_mode);
if (fchmod(private_fd, sbuf.st_mode) == -1)
UNIVERSAL_LOG("fchmod() failed");
}
/* Linux by default stores the mtime with low precision, low enough that updates that occur in quick succession may

View File

@ -1453,16 +1453,19 @@ bool history_t::save_internal_via_rewrite()
if (wstat(new_name, &sbuf) >= 0)
{
/* Success */
if (0 > fchown(out_fd, sbuf.st_uid, sbuf.st_gid))
if (fchown(out_fd, sbuf.st_uid, sbuf.st_gid) == -1)
{
debug(2, L"Error when changing ownership of history file");
debug(2, L"Error %d when changing ownership of history file", errno);
}
if (fchmod(out_fd, sbuf.st_mode) == -1)
{
debug(2, L"Error %d when changing mode of history file", errno);
}
fchmod(out_fd, sbuf.st_mode);
}
if (0 > wrename(tmp_name, new_name))
if (wrename(tmp_name, new_name) == -1)
{
debug(2, L"Error when renaming history file");
debug(2, L"Error %d when renaming history file", errno);
}
}
close(out_fd);