Use Result for write_to_fd return value (#10308)

This commit is contained in:
Bartłomiej Maryńczak 2024-03-10 04:29:50 +01:00 committed by GitHub
parent e6687dc61f
commit d5cde80447
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,7 +23,7 @@ use std::collections::hash_map::Entry;
use std::collections::HashSet; use std::collections::HashSet;
use std::ffi::CString; use std::ffi::CString;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use std::os::fd::{AsRawFd, OwnedFd, RawFd}; use std::os::fd::{AsFd, AsRawFd, OwnedFd, RawFd};
use std::os::unix::prelude::MetadataExt; use std::os::unix::prelude::MetadataExt;
// Pull in the O_EXLOCK constant if it is defined, otherwise set it to 0. // Pull in the O_EXLOCK constant if it is defined, otherwise set it to 0.
@ -533,11 +533,12 @@ impl EnvUniversal {
} }
/// Writes our state to the fd. path is provided only for error reporting. /// Writes our state to the fd. path is provided only for error reporting.
fn write_to_fd(&mut self, fd: RawFd, path: &wstr) -> bool { fn write_to_fd(&mut self, fd: impl AsFd, path: &wstr) -> std::io::Result<usize> {
assert!(fd >= 0); let fd = fd.as_fd();
let mut success = true;
let contents = Self::serialize_with_vars(&self.vars); let contents = Self::serialize_with_vars(&self.vars);
if let Err(err) = write_loop(&fd, &contents) { let res = write_loop(&fd, &contents);
if let Err(err) = res.as_ref() {
let error = Errno(err.raw_os_error().unwrap()); let error = Errno(err.raw_os_error().unwrap());
FLOG!( FLOG!(
error, error,
@ -547,14 +548,13 @@ impl EnvUniversal {
error.to_string() error.to_string()
), ),
); );
success = false;
} }
// Since we just wrote out this file, it matches our internal state; pretend we read from it. // Since we just wrote out this file, it matches our internal state; pretend we read from it.
self.last_read_file = file_id_for_fd(fd); self.last_read_file = file_id_for_fd(fd.as_raw_fd());
// We don't close the file. // We don't close the file.
success res
} }
fn move_new_vars_file_into_place(&mut self, src: &wstr, dst: &wstr) -> bool { fn move_new_vars_file_into_place(&mut self, src: &wstr, dst: &wstr) -> bool {
@ -768,7 +768,7 @@ impl EnvUniversal {
let private_file_path = &delete_pfp; let private_file_path = &delete_pfp;
// Write to it. // Write to it.
if !self.write_to_fd(private_fd.as_raw_fd(), &private_file_path) { if self.write_to_fd(&private_fd, private_file_path).is_err() {
FLOG!(uvar_file, "universal log write_to_fd() failed"); FLOG!(uvar_file, "universal log write_to_fd() failed");
return false; return false;
} }
@ -812,7 +812,7 @@ impl EnvUniversal {
} }
// Apply new file. // Apply new file.
if !self.move_new_vars_file_into_place(&private_file_path, &real_path) { if !self.move_new_vars_file_into_place(private_file_path, &real_path) {
FLOG!( FLOG!(
uvar_file, uvar_file,
"universal log move_new_vars_file_into_place() failed" "universal log move_new_vars_file_into_place() failed"