Skip to content

Commit

Permalink
Add a touch method (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset authored Dec 21, 2024
1 parent 2cbd272 commit a9e3bc7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion raugeas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
//! The main differences with the C API are:
//!
//! * Add `clear` and `clearm` methods clear values (instead of passing an `Option` to `set`/`setm`).
//! * Add `touch` method to create a node if it does not exist.
//! * Use a `Span` struct to represent the span of a node in a file.
//! * Use a `Position` enum to indicate where to insert a new node.
//! * Use an `Attr` struct to represent the attributes of a node.
//! * Use a `SaveMode` enum to indicate how to save changes.
//!
//! The main references:
//!
//! * https://github.com/hercules-team/augeas/blob/master/src/augeas.h
//! * https://github.com/hercules-team/ruby-augeas/tree/master/lib
//!
//! ## Usage
//!
//! In `Cargo.toml`:
Expand Down Expand Up @@ -542,6 +548,20 @@ impl Augeas {
Ok(())
}

/// Create the `path` with empty value if it doesn't exist.
pub fn touch<T: AsRef<OsStr>>(&mut self, path: T) -> Result<()> {
let path = path.as_ref();
let path_c = CString::new(path.as_bytes())?;

let no_matches = self.matches_os(path)?.is_empty();
if no_matches {
unsafe { aug_set(self.ptr, path_c.as_ptr(), ptr::null()) };
}
self.check_error()?;

Ok(())
}

/// Insert a new node: find the node matching `path` and create a new
/// sibling for it with the given `label`. The sibling is created
/// before or after the node `path`, depending on the value of `pos`.
Expand Down Expand Up @@ -1452,13 +1472,28 @@ mod tests {
}

#[test]
fn set_clear() {
fn clear_test() {
let mut aug = Augeas::init(Some("tests/test_root"), "", Flags::NONE).unwrap();
aug.clear("etc/passwd/root/uid").unwrap();
let uid = aug.get("etc/passwd/root/uid").unwrap();
assert_eq!(None, uid);
}

#[test]
fn touch_test() {
let mut aug = Augeas::init(Some("tests/test_root"), "", Flags::NONE).unwrap();
let exists = aug.matches("etc/passwd/epicaste/uid").unwrap();
assert_eq!(exists.len(), 0);
// Creates missing nodes.
aug.touch("etc/passwd/epicaste/uid").unwrap();
let exists = aug.matches("etc/passwd/epicaste/uid").unwrap();
assert_eq!(exists.len(), 1);
// Does not overwrite existing nodes.
aug.touch("etc/passwd/root/uid").unwrap();
let uid = aug.get("etc/passwd/root/uid").unwrap().unwrap();
assert_eq!("0", uid);
}

#[test]
fn get_nonutf8_test() {
let invalid: &[u8; 2] = &[0xc3, 0x28];
Expand Down
2 changes: 1 addition & 1 deletion raugeas/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ mod tests {

let hello = "Hello, World!";
let s = CString::new(hello).unwrap();
libc::fprintf(file, s.as_ptr() as *const i8);
libc::fprintf(file, s.as_ptr());

let res = read_memstream(&mut buf, &mut size, file).unwrap();
assert_eq!(res, hello);
Expand Down

0 comments on commit a9e3bc7

Please sign in to comment.