|
| 1 | +extern crate libc; |
| 2 | + |
| 3 | +use std::ffi::CString; |
| 4 | +use std::fs::File; |
| 5 | +use std::io::{Error, ErrorKind, Result}; |
| 6 | +use std::mem; |
| 7 | +use std::os::unix::ffi::OsStrExt; |
| 8 | +use std::os::unix::fs::MetadataExt; |
| 9 | +use std::os::unix::io::{AsRawFd, FromRawFd}; |
| 10 | +use std::path::Path; |
| 11 | + |
| 12 | +use FsStats; |
| 13 | + |
| 14 | +pub fn duplicate(file: &File) -> Result<File> { |
| 15 | + unsafe { |
| 16 | + let fd = libc::dup(file.as_raw_fd()); |
| 17 | + |
| 18 | + if fd < 0 { |
| 19 | + Err(Error::last_os_error()) |
| 20 | + } else { |
| 21 | + Ok(File::from_raw_fd(fd)) |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +pub fn lock_shared(file: &File) -> Result<()> { |
| 27 | + flock(file, libc::LOCK_SH) |
| 28 | +} |
| 29 | + |
| 30 | +pub fn lock_exclusive(file: &File) -> Result<()> { |
| 31 | + flock(file, libc::LOCK_EX) |
| 32 | +} |
| 33 | + |
| 34 | +pub fn try_lock_shared(file: &File) -> Result<()> { |
| 35 | + flock(file, libc::LOCK_SH | libc::LOCK_NB) |
| 36 | +} |
| 37 | + |
| 38 | +pub fn try_lock_exclusive(file: &File) -> Result<()> { |
| 39 | + flock(file, libc::LOCK_EX | libc::LOCK_NB) |
| 40 | +} |
| 41 | + |
| 42 | +pub fn unlock(file: &File) -> Result<()> { |
| 43 | + flock(file, libc::LOCK_UN) |
| 44 | +} |
| 45 | + |
| 46 | +pub fn lock_error() -> Error { |
| 47 | + Error::from_raw_os_error(libc::EWOULDBLOCK) |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(not(target_os = "solaris"))] |
| 51 | +fn flock(file: &File, flag: libc::c_int) -> Result<()> { |
| 52 | + let ret = unsafe { libc::flock(file.as_raw_fd(), flag) }; |
| 53 | + if ret < 0 { |
| 54 | + Err(Error::last_os_error()) |
| 55 | + } else { |
| 56 | + Ok(()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(target_os = "solaris")] |
| 61 | +fn flock(file: &File, flag: libc::c_int) -> Result<()> { |
| 62 | + let mut fl = libc::flock { |
| 63 | + l_whence: 0, |
| 64 | + l_start: 0, |
| 65 | + l_len: 0, |
| 66 | + l_type: 0, |
| 67 | + l_pad: [0; 4], |
| 68 | + l_pid: 0, |
| 69 | + l_sysid: 0, |
| 70 | + }; |
| 71 | + |
| 72 | + let (cmd, operation) = match flag & libc::LOCK_NB { |
| 73 | + 0 => (libc::F_SETLKW, flag), |
| 74 | + _ => (libc::F_SETLK, flag & !libc::LOCK_NB), |
| 75 | + }; |
| 76 | + |
| 77 | + match operation { |
| 78 | + libc::LOCK_SH => fl.l_type |= libc::F_RDLCK, |
| 79 | + libc::LOCK_EX => fl.l_type |= libc::F_WRLCK, |
| 80 | + libc::LOCK_UN => fl.l_type |= libc::F_UNLCK, |
| 81 | + _ => return Err(Error::from_raw_os_error(libc::EINVAL)), |
| 82 | + } |
| 83 | + |
| 84 | + let ret = unsafe { libc::fcntl(file.as_raw_fd(), cmd, &fl) }; |
| 85 | + match ret { |
| 86 | + -1 => match Error::last_os_error().raw_os_error() { |
| 87 | + Some(libc::EACCES) => Err(lock_error()), |
| 88 | + _ => Err(Error::last_os_error()), |
| 89 | + }, |
| 90 | + _ => Ok(()), |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +pub fn allocated_size(file: &File) -> Result<u64> { |
| 95 | + file.metadata().map(|m| m.blocks() as u64 * 512) |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(any( |
| 99 | + target_os = "linux", |
| 100 | + target_os = "freebsd", |
| 101 | + target_os = "android" |
| 102 | +))] |
| 103 | +pub fn allocate(file: &File, len: u64) -> Result<()> { |
| 104 | + let ret = unsafe { libc::posix_fallocate(file.as_raw_fd(), 0, len as libc::off_t) }; |
| 105 | + if ret == 0 { |
| 106 | + Ok(()) |
| 107 | + } else { |
| 108 | + Err(Error::last_os_error()) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(any( |
| 113 | + target_os = "macos", |
| 114 | + target_os = "ios", |
| 115 | + target_os = "tvos", |
| 116 | + target_os = "watchos", |
| 117 | + target_os = "visionos" |
| 118 | +))] |
| 119 | +pub fn allocate(file: &File, len: u64) -> Result<()> { |
| 120 | + let stat = file.metadata()?; |
| 121 | + |
| 122 | + if len > stat.blocks() as u64 * 512 { |
| 123 | + let mut fstore = libc::fstore_t { |
| 124 | + fst_flags: libc::F_ALLOCATECONTIG, |
| 125 | + fst_posmode: libc::F_PEOFPOSMODE, |
| 126 | + fst_offset: 0, |
| 127 | + fst_length: len as libc::off_t, |
| 128 | + fst_bytesalloc: 0, |
| 129 | + }; |
| 130 | + |
| 131 | + let ret = unsafe { libc::fcntl(file.as_raw_fd(), libc::F_PREALLOCATE, &fstore) }; |
| 132 | + if ret == -1 { |
| 133 | + fstore.fst_flags = libc::F_ALLOCATEALL; |
| 134 | + let ret = unsafe { libc::fcntl(file.as_raw_fd(), libc::F_PREALLOCATE, &fstore) }; |
| 135 | + if ret == -1 { |
| 136 | + return Err(Error::last_os_error()); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if len > stat.size() as u64 { |
| 142 | + file.set_len(len) |
| 143 | + } else { |
| 144 | + Ok(()) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +#[cfg(any( |
| 149 | + target_os = "openbsd", |
| 150 | + target_os = "netbsd", |
| 151 | + target_os = "dragonfly", |
| 152 | + target_os = "solaris", |
| 153 | + target_os = "haiku" |
| 154 | +))] |
| 155 | +pub fn allocate(file: &File, len: u64) -> Result<()> { |
| 156 | + if len > file.metadata()?.len() as u64 { |
| 157 | + file.set_len(len) |
| 158 | + } else { |
| 159 | + Ok(()) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +pub fn statvfs(path: &Path) -> Result<FsStats> { |
| 164 | + let cstr = match CString::new(path.as_os_str().as_bytes()) { |
| 165 | + Ok(cstr) => cstr, |
| 166 | + Err(..) => return Err(Error::new(ErrorKind::InvalidInput, "path contained a null")), |
| 167 | + }; |
| 168 | + |
| 169 | + unsafe { |
| 170 | + let mut stat: libc::statvfs = mem::zeroed(); |
| 171 | + if libc::statvfs(cstr.as_ptr() as *const _, &mut stat) != 0 { |
| 172 | + Err(Error::last_os_error()) |
| 173 | + } else { |
| 174 | + Ok(FsStats { |
| 175 | + free_space: stat.f_frsize as u64 * stat.f_bfree as u64, |
| 176 | + available_space: stat.f_frsize as u64 * stat.f_bavail as u64, |
| 177 | + total_space: stat.f_frsize as u64 * stat.f_blocks as u64, |
| 178 | + allocation_granularity: stat.f_frsize as u64, |
| 179 | + }) |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments