There's some places where this example could be better written. For example, this:
let oldrootfs = String::from(format!("{}/.oldrootfs", rootfs.clone()));
can be reduced to either of:
let oldrootfs = format!("{}/.oldrootfs", rootfs);
let oldrootfs = rootfs.clone() + "/.oldrootfs";
You could also do something like
Path::new(rootfs).join(".oldrootfs")
but I'm not entirely sure how to get that to a pointer for the FFI stuff. It seems like the smartest way would be to go through OsStr, and if one wanted to use Path instead (which seems like the appropriate type), then sys_pivot_root should probably be changed to accept them instead.
A lot of the complexity here is that you're interfacing with C, which is inherently unsafe, and cdecl is very simple in what can be passed. (And stuff like POSIX file paths are just hard to statically type around, because they're not text strings.) Normally, you'd write some wrappers (which the original author is well on the way to), and the rest of the code should look much simpler.
Similarly here:
create_dir(oldrootfs.clone());
The clone isn't needed; you can simply borrow oldrootfs:
create_dir(&oldrootfs);
If you change rootfs in both pivot_root and sys_pivot_root to a &str, you can then call it as just
pivot_root("/")
which is simpler than
pivot_root(String::from("/"))
(I generally find that taking &str is simpler than String, if you're not going to modify the String object.)
Sure, there is definitely a lot of possible improvements for the code examples, like avoiding `clone` on a heap allocated strings, etc. I had some really odd issues when passing a statically allocated strings to the `libc` functions (the function's arguments from the previous calls ended up concatenated in the later function's invocations, just use `strace` to observe that behavior).
A lot of the complexity here is that you're interfacing with C, which is inherently unsafe, and cdecl is very simple in what can be passed. (And stuff like POSIX file paths are just hard to statically type around, because they're not text strings.) Normally, you'd write some wrappers (which the original author is well on the way to), and the rest of the code should look much simpler.
Similarly here:
The clone isn't needed; you can simply borrow oldrootfs: If you change rootfs in both pivot_root and sys_pivot_root to a &str, you can then call it as just which is simpler than (I generally find that taking &str is simpler than String, if you're not going to modify the String object.)