summaryrefslogtreecommitdiffstats
path: root/openat/examples
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2023-01-27 13:58:10 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2023-01-27 13:58:10 +0000
commit6cef9f0fc159de4c9fd708050ec76adb4e74d390 (patch)
tree81856b3b4da6d18e10516b0be737a157e13f129b /openat/examples
parent9e8dd00da25273fba9f0cafccbde2236e04fb24e (diff)
downloadpam_usercg_rust-6cef9f0fc159de4c9fd708050ec76adb4e74d390.tar.gz
pam_usercg_rust-6cef9f0fc159de4c9fd708050ec76adb4e74d390.tar.xz
pam_usercg_rust-6cef9f0fc159de4c9fd708050ec76adb4e74d390.zip
openat variant
Diffstat (limited to 'openat/examples')
-rw-r--r--openat/examples/exchange.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/openat/examples/exchange.rs b/openat/examples/exchange.rs
new file mode 100644
index 0000000..21fdf0a
--- /dev/null
+++ b/openat/examples/exchange.rs
@@ -0,0 +1,39 @@
+extern crate argparse;
+extern crate openat;
+
+use std::process::exit;
+use std::path::PathBuf;
+
+use argparse::{ArgumentParser, Parse};
+use openat::Dir;
+
+#[cfg(not(target_os="linux"))]
+fn main() {
+ println!("Atomic exchange is not supported on this platform")
+}
+
+#[cfg(target_os="linux")]
+fn main() {
+ let mut path1 = PathBuf::new();
+ let mut path2 = PathBuf::new();
+ {
+ let mut ap = ArgumentParser::new();
+ ap.refer(&mut path1)
+ .add_argument("path1", Parse, "First path of exchange operation")
+ .required();
+ ap.refer(&mut path2)
+ .add_argument("path2", Parse, "Second path of exchange operation")
+ .required();
+ ap.parse_args_or_exit();
+ }
+ if path1.parent() != path2.parent() {
+ println!("Paths must be in the same directory");
+ exit(1);
+ }
+ let parent = path1.parent().expect("path must have parent directory");
+ let dir = Dir::open(parent).expect("can open directory");
+ dir.local_exchange(
+ path1.file_name().expect("path1 must have filename"),
+ path2.file_name().expect("path2 must have filename"),
+ ).expect("can rename");
+}