| 1 | use std::io::{Write, Cursor}; |
| 2 | use std::fmt; |
| 3 | use std::str::from_utf8; |
| 4 | use std::ffi::{CString, CStr}; |
| 5 | use std::path::Path; |
| 6 | |
| 7 | use libc::{uid_t, gid_t, mode_t}; |
| 8 | use nix::mount::{MsFlags, mount}; |
| 9 | |
| 10 | use {OSError, Error}; |
| 11 | use util::{path_to_cstring, as_path}; |
| 12 | use explain::{Explainable, exists, user}; |
| 13 | |
| 14 | |
| 15 | #[derive(Debug, Clone, Copy)] |
| 16 | enum Size { |
| 17 | Auto, |
| 18 | Bytes(usize), |
| 19 | Blocks(usize), |
| 20 | } |
| 21 | |
| 22 | /// A tmpfs mount definition |
| 23 | /// |
| 24 | /// By default tmpfs is mounted with nosuid,nodev |
| 25 | #[derive(Debug, Clone)] |
| 26 | pub struct Tmpfs { |
| 27 | target: CString, |
| 28 | size: Size, |
| 29 | nr_inodes: Option<usize>, |
| 30 | mode: Option<mode_t>, |
| 31 | uid: Option<uid_t>, |
| 32 | gid: Option<gid_t>, |
| 33 | flags: MsFlags, |
| 34 | } |
| 35 | |
| 36 | impl Tmpfs { |
| 37 | /// New tmpfs mount point with target path and default settngs |
| 38 | pub fn new<P: AsRef<Path>>(path: P) -> Tmpfs { |
| 39 | Tmpfs { |
| 40 | target: path_to_cstring(path.as_ref()), |
| 41 | size: Size::Auto, |
| 42 | nr_inodes: None, |
| 43 | mode: None, |
| 44 | uid: None, |
| 45 | gid: None, |
| 46 | flags: MsFlags::MS_NOSUID|MsFlags::MS_NODEV, |
| 47 | } |
| 48 | } |
| 49 | /// Set size in bytes |
| 50 | pub fn size_bytes(mut self, size: usize) -> Tmpfs { |
| 51 | self.size = Size::Bytes(size); |
| 52 | self |
| 53 | } |
| 54 | /// Set size in blocks of PAGE_CACHE_SIZE |
| 55 | pub fn size_blocks(mut self, size: usize) -> Tmpfs { |
| 56 | self.size = Size::Blocks(size); |
| 57 | self |
| 58 | } |
| 59 | /// Maximum number of inodes |
| 60 | pub fn nr_inodes(mut self, num: usize) -> Tmpfs { |
| 61 | self.nr_inodes = Some(num); |
| 62 | self |
| 63 | } |
| 64 | /// Set initial permissions of the root directory |
| 65 | pub fn mode(mut self, mode: mode_t) -> Tmpfs { |
| 66 | self.mode = Some(mode); |
| 67 | self |
| 68 | } |
| 69 | /// Set initial owner of the root directory |
| 70 | pub fn uid(mut self, uid: uid_t) -> Tmpfs { |
| 71 | self.uid = Some(uid); |
| 72 | self |
| 73 | } |
| 74 | /// Set initial group of the root directory |
| 75 | pub fn gid(mut self, gid: gid_t) -> Tmpfs { |
| 76 | self.gid = Some(gid); |
| 77 | self |
| 78 | } |
| 79 | |
| 80 | fn format_options(&self) -> Vec<u8> { |
| 81 | let mut cur = Cursor::new(Vec::new()); |
| 82 | match self.size { |
| 83 | Size::Auto => {} |
| 84 | Size::Bytes(x) => write!(cur, "size={}", x).unwrap(), |
| 85 | Size::Blocks(x) => write!(cur, "nr_blocks={}", x).unwrap(), |
| 86 | } |
| 87 | if let Some(inodes) = self.nr_inodes { |
| 88 | if cur.position() != 0 { |
| 89 | cur.write(b",").unwrap(); |
| 90 | } |
| 91 | write!(cur, "nr_inodes={}", inodes).unwrap(); |
| 92 | } |
| 93 | if let Some(mode) = self.mode { |
| 94 | if cur.position() != 0 { |
| 95 | cur.write(b",").unwrap(); |
| 96 | } |
| 97 | write!(cur, "mode=0{:04o}", mode).unwrap(); |
| 98 | } |
| 99 | if let Some(uid) = self.uid { |
| 100 | if cur.position() != 0 { |
| 101 | cur.write(b",").unwrap(); |
| 102 | } |
| 103 | write!(cur, "uid={}", uid).unwrap(); |
| 104 | } |
| 105 | if let Some(gid) = self.gid { |
| 106 | if cur.position() != 0 { |
| 107 | cur.write(b",").unwrap(); |
| 108 | } |
| 109 | write!(cur, "gid={}", gid).unwrap(); |
| 110 | } |
| 111 | return cur.into_inner(); |
| 112 | } |
| 113 | |
| 114 | /// Mount the tmpfs |
| 115 | pub fn bare_mount(self) -> Result<(), OSError> { |
| 116 | let mut options = self.format_options(); |
| 117 | mount( |
| 118 | Some(CStr::from_bytes_with_nul(b"tmpfs\0").unwrap()), |
| 119 | &*self.target, |
| 120 | Some(CStr::from_bytes_with_nul(b"tmpfs\0").unwrap()), |
| 121 | self.flags, |
| 122 | Some(&*options) |
| 123 | ).map_err(|err| OSError::from_nix(err, Box::new(self))) |
| 124 | } |
| 125 | |
| 126 | /// Mount the tmpfs and explain error immediately |
| 127 | pub fn mount(self) -> Result<(), Error> { |
| 128 | self.bare_mount().map_err(OSError::explain) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | impl fmt::Display for Tmpfs { |
| 133 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 134 | let opts = self.format_options(); |
| 135 | write!(fmt, "tmpfs {} -> {:?}", from_utf8(&opts).unwrap(), |
| 136 | as_path(&self.target)) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | impl Explainable for Tmpfs { |
| 141 | fn explain(&self) -> String { |
| 142 | [ |
| 143 | format!("target: {}", exists(as_path(&self.target))), |
| 144 | format!("{}", user()), |
| 145 | ].join(", ") |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | mod test { |
| 151 | #[cfg(test)] |
| 152 | use super::Tmpfs; |
| 153 | |
| 154 | #[test] |
| 155 | fn test_tmpfs_options() { |
| 156 | let fs = Tmpfs::new("/tmp") |
| 157 | .size_bytes(1 << 20) |
| 158 | .nr_inodes(1024) |
| 159 | .mode(0o1777) |
| 160 | .uid(1000) |
| 161 | .gid(1000); |
| 162 | |
| 163 | assert_eq!(fs.format_options(), |
| 164 | "size=1048576,nr_inodes=1024,mode=01777,uid=1000,gid=1000".as_bytes()) |
| 165 | } |
| 166 | } |