Skip to content

Commit

Permalink
scubainit: Avoid lifetimes on EntFile{Reader,Writer}
Browse files Browse the repository at this point in the history
Use into_inner().
  • Loading branch information
JonathonReinhart committed Dec 30, 2023
1 parent 1adca37 commit bc81d47
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
22 changes: 13 additions & 9 deletions scubainit/src/entfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,25 @@ impl<'a> EntLineParser<'a> {
////////////////////////////////////////////////////////////////////////////////
// EntFileReader

pub struct EntFileReader<'a, T> {
reader: BufReader<&'a File>,
pub struct EntFileReader<T> {
reader: BufReader<File>,
marker: PhantomData<T>, // T must be used
}

impl<T> EntFileReader<'_, T> {
pub fn new(file: &File) -> EntFileReader<T> {
impl<T> EntFileReader<T> {
pub fn new(file: File) -> EntFileReader<T> {
EntFileReader {
reader: BufReader::new(file),
marker: PhantomData,
}
}

pub fn into_inner(self) -> File {
self.reader.into_inner()
}
}

impl<T: Entry> Iterator for EntFileReader<'_, T> {
impl<T: Entry> Iterator for EntFileReader<T> {
type Item = Result<T, ReadEntryError>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -130,13 +134,13 @@ impl<T: Entry> Iterator for EntFileReader<'_, T> {
////////////////////////////////////////////////////////////////////////////////
// EntFileWriter

pub struct EntFileWriter<'a, T> {
file: &'a File,
pub struct EntFileWriter<T> {
file: File,
marker: PhantomData<T>, // T must be used
}

impl<T: Entry> EntFileWriter<'_, T> {
pub fn new(file: &File) -> EntFileWriter<T> {
impl<T: Entry> EntFileWriter<T> {
pub fn new(file: File) -> EntFileWriter<T> {
EntFileWriter {
file: file,
marker: PhantomData,
Expand Down
4 changes: 2 additions & 2 deletions scubainit/src/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub struct GroupEntry {
pub members: Vec<String>,
}

pub type GroupFileReader<'a> = EntFileReader<'a, GroupEntry>;
pub type GroupFileWriter<'a> = EntFileWriter<'a, GroupEntry>;
pub type GroupFileReader = EntFileReader<GroupEntry>;
pub type GroupFileWriter = EntFileWriter<GroupEntry>;

impl Entry for GroupEntry {
fn from_line(line: &str) -> Result<GroupEntry, ReadEntryError> {
Expand Down
18 changes: 12 additions & 6 deletions scubainit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl UserInfo {
let file = open_read_append(ETC_GROUP)?;

// Try to find a conflicting group (one matching name or gid).
let reader = groups::GroupFileReader::new(&file);
let reader = groups::GroupFileReader::new(file);
for grp in reader {
let grp = grp?;
let name_matches = grp.name.as_str() == group_name;
Expand All @@ -131,14 +131,16 @@ impl UserInfo {
}
}

let file = reader.into_inner();

// Okay, add group
let grp = groups::GroupEntry {
name: group_name.to_owned(),
passwd: INVALID_PASSWORD.to_owned(),
gid,
members: Vec::new(),
};
let mut writer = groups::GroupFileWriter::new(&file);
let mut writer = groups::GroupFileWriter::new(file);
Ok(writer.write(&grp)?)
}

Expand All @@ -150,7 +152,7 @@ impl UserInfo {
let file = open_read_append(ETC_PASSWD)?;

// Try to find a conflicting user (one matching name or uid).
let reader = passwd::PasswdFileReader::new(&file);
let reader = passwd::PasswdFileReader::new(file);
for pwd in reader {
let pwd = pwd?;
let name_matches = pwd.name.as_str() == user_name;
Expand All @@ -169,6 +171,8 @@ impl UserInfo {
}
}

let file = reader.into_inner();

// Okay, add user
let home_dir_path = self.home_dir();
let home_dir_str = home_dir_path.to_str().context("Invalid home_dir")?;
Expand All @@ -181,7 +185,7 @@ impl UserInfo {
home_dir: home_dir_str.to_owned(),
shell: DEFAULT_SHELL.to_owned(),
};
let mut writer = passwd::PasswdFileWriter::new(&file);
let mut writer = passwd::PasswdFileWriter::new(file);
Ok(writer.write(&user)?)
}

Expand All @@ -192,7 +196,7 @@ impl UserInfo {
let file = open_read_append(ETC_SHADOW)?;

// Try to find a conflicting user (one matching name).
let reader = shadow::ShadowFileReader::new(&file);
let reader = shadow::ShadowFileReader::new(file);
for sp in reader {
let sp = sp?;
if sp.name.as_str() == user_name {
Expand All @@ -201,6 +205,8 @@ impl UserInfo {
}
}

let file = reader.into_inner();

// Okay, add shadow entry
let entry = shadow::ShadowEntry {
name: user_name.to_owned(),
Expand All @@ -212,7 +218,7 @@ impl UserInfo {
inact_period: None,
expire_date: None,
};
let mut writer = shadow::ShadowFileWriter::new(&file);
let mut writer = shadow::ShadowFileWriter::new(file);
Ok(writer.write(&entry)?)
}

Expand Down
4 changes: 2 additions & 2 deletions scubainit/src/passwd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct PasswdEntry {
pub shell: String, // TODO: Option<String>
}

pub type PasswdFileReader<'a> = EntFileReader<'a, PasswdEntry>;
pub type PasswdFileWriter<'a> = EntFileWriter<'a, PasswdEntry>;
pub type PasswdFileReader = EntFileReader<PasswdEntry>;
pub type PasswdFileWriter = EntFileWriter<PasswdEntry>;

impl Entry for PasswdEntry {
fn from_line(line: &str) -> Result<PasswdEntry, ReadEntryError> {
Expand Down
4 changes: 2 additions & 2 deletions scubainit/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub struct ShadowEntry {
// reserved
}

pub type ShadowFileReader<'a> = EntFileReader<'a, ShadowEntry>;
pub type ShadowFileWriter<'a> = EntFileWriter<'a, ShadowEntry>;
pub type ShadowFileReader = EntFileReader<ShadowEntry>;
pub type ShadowFileWriter = EntFileWriter<ShadowEntry>;

impl Entry for ShadowEntry {
fn from_line(line: &str) -> Result<ShadowEntry, ReadEntryError> {
Expand Down

0 comments on commit bc81d47

Please sign in to comment.