diff --git a/proxmox-sys/src/fs/file.rs b/proxmox-sys/src/fs/file.rs index 8820335f..180a35f2 100644 --- a/proxmox-sys/src/fs/file.rs +++ b/proxmox-sys/src/fs/file.rs @@ -423,3 +423,27 @@ pub fn open_file_locked>( Err(err) => bail!("Unable to acquire lock {:?} - {}", path, err), } } + +/// Get an iterator over lines of a file, skipping empty lines and comments (lines starting with a +/// `#`). +pub fn file_get_non_comment_lines>( + path: P, +) -> Result>, Error> { + let path = path.as_ref(); + + Ok(io::BufReader::new( + File::open(path).map_err(|err| format_err!("error opening {:?}: {}", path, err))?, + ) + .lines() + .filter_map(|line| match line { + Ok(line) => { + let line = line.trim(); + if line.is_empty() || line.starts_with('#') { + None + } else { + Some(Ok(line.to_string())) + } + } + Err(err) => Some(Err(err)), + })) +} diff --git a/proxmox-sys/src/fs/read_dir.rs b/proxmox-sys/src/fs/read_dir.rs index 1c06d8f4..020f924d 100644 --- a/proxmox-sys/src/fs/read_dir.rs +++ b/proxmox-sys/src/fs/read_dir.rs @@ -1,9 +1,6 @@ use std::borrow::{Borrow, BorrowMut}; -use std::fs::File; -use std::io::{self, BufRead}; use std::ops::{Deref, DerefMut}; use std::os::unix::io::{AsRawFd, RawFd}; -use std::path::Path; use anyhow::{bail, format_err, Error}; use nix::dir; @@ -305,27 +302,3 @@ fn do_lock_dir_noblock( Ok(handle) } - -/// Get an iterator over lines of a file, skipping empty lines and comments (lines starting with a -/// `#`). -pub fn file_get_non_comment_lines>( - path: P, -) -> Result>, Error> { - let path = path.as_ref(); - - Ok(io::BufReader::new( - File::open(path).map_err(|err| format_err!("error opening {:?}: {}", path, err))?, - ) - .lines() - .filter_map(|line| match line { - Ok(line) => { - let line = line.trim(); - if line.is_empty() || line.starts_with('#') { - None - } else { - Some(Ok(line.to_string())) - } - } - Err(err) => Some(Err(err)), - })) -}