1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
//! POSIX-style filesystem functions which operate on bare paths.
use crate::fd::OwnedFd;
#[cfg(not(target_os = "espidf"))]
use crate::fs::Access;
#[cfg(not(any(
solarish,
target_os = "espidf",
target_os = "haiku",
target_os = "netbsd",
target_os = "nto",
target_os = "redox",
target_os = "wasi",
)))]
use crate::fs::StatFs;
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
use crate::fs::StatVfs;
use crate::fs::{Mode, OFlags, Stat};
use crate::{backend, io, path};
#[cfg(feature = "alloc")]
use {
crate::ffi::{CStr, CString},
crate::path::SMALL_PATH_BUFFER_SIZE,
alloc::vec::Vec,
};
/// `open(path, oflags, mode)`—Opens a file.
///
/// POSIX guarantees that `open` will use the lowest unused file descriptor,
/// however it is not safe in general to rely on this, as file descriptors may
/// be unexpectedly allocated on other threads or in libraries.
///
/// The `Mode` argument is only significant when creating a file.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html
/// [Linux]: https://man7.org/linux/man-pages/man2/open.2.html
#[inline]
pub fn open<P: path::Arg>(path: P, flags: OFlags, mode: Mode) -> io::Result<OwnedFd> {
path.into_with_c_str(|path| backend::fs::syscalls::open(path, flags, mode))
}
/// `chmod(path, mode)`—Sets file or directory permissions.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
/// [Linux]: https://man7.org/linux/man-pages/man2/chmod.2.html
#[cfg(not(target_os = "wasi"))]
#[inline]
pub fn chmod<P: path::Arg>(path: P, mode: Mode) -> io::Result<()> {
path.into_with_c_str(|path| backend::fs::syscalls::chmod(path, mode))
}
/// `stat(path)`—Queries metadata for a file or directory.
///
/// [`Mode::from_raw_mode`] and [`FileType::from_raw_mode`] may be used to
/// interpret the `st_mode` field.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html
/// [Linux]: https://man7.org/linux/man-pages/man2/stat.2.html
/// [`Mode::from_raw_mode`]: crate::fs::Mode::from_raw_mode
/// [`FileType::from_raw_mode`]: crate::fs::FileType::from_raw_mode
#[inline]
pub fn stat<P: path::Arg>(path: P) -> io::Result<Stat> {
path.into_with_c_str(backend::fs::syscalls::stat)
}
/// `lstat(path)`—Queries metadata for a file or directory, without following
/// symlinks.
///
/// [`Mode::from_raw_mode`] and [`FileType::from_raw_mode`] may be used to
/// interpret the `st_mode` field.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/lstat.html
/// [Linux]: https://man7.org/linux/man-pages/man2/lstat.2.html
/// [`Mode::from_raw_mode`]: crate::fs::Mode::from_raw_mode
/// [`FileType::from_raw_mode`]: crate::fs::FileType::from_raw_mode
#[inline]
pub fn lstat<P: path::Arg>(path: P) -> io::Result<Stat> {
path.into_with_c_str(backend::fs::syscalls::lstat)
}
/// `readlink(path)`—Reads the contents of a symlink.
///
/// If `reuse` is non-empty, reuse its buffer to store the result if possible.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html
/// [Linux]: https://man7.org/linux/man-pages/man2/readlink.2.html
#[cfg(feature = "alloc")]
#[inline]
pub fn readlink<P: path::Arg, B: Into<Vec<u8>>>(path: P, reuse: B) -> io::Result<CString> {
path.into_with_c_str(|path| _readlink(path, reuse.into()))
}
#[cfg(feature = "alloc")]
fn _readlink(path: &CStr, mut buffer: Vec<u8>) -> io::Result<CString> {
// This code would benefit from having a better way to read into
// uninitialized memory, but that requires `unsafe`.
buffer.clear();
buffer.reserve(SMALL_PATH_BUFFER_SIZE);
buffer.resize(buffer.capacity(), 0_u8);
loop {
let nread = backend::fs::syscalls::readlink(path, &mut buffer)?;
let nread = nread as usize;
assert!(nread <= buffer.len());
if nread < buffer.len() {
buffer.resize(nread, 0_u8);
return Ok(CString::new(buffer).unwrap());
}
buffer.reserve(1); // use `Vec` reallocation strategy to grow capacity exponentially
buffer.resize(buffer.capacity(), 0_u8);
}
}
/// `rename(old_path, new_path)`—Renames a file or directory.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
/// [Linux]: https://man7.org/linux/man-pages/man2/rename.2.html
#[inline]
pub fn rename<P: path::Arg, Q: path::Arg>(old_path: P, new_path: Q) -> io::Result<()> {
old_path.into_with_c_str(|old_path| {
new_path.into_with_c_str(|new_path| backend::fs::syscalls::rename(old_path, new_path))
})
}
/// `unlink(path)`—Unlinks a file.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html
/// [Linux]: https://man7.org/linux/man-pages/man2/unlink.2.html
#[inline]
pub fn unlink<P: path::Arg>(path: P) -> io::Result<()> {
path.into_with_c_str(backend::fs::syscalls::unlink)
}
/// `rmdir(path)`—Removes a directory.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/rmdir.html
/// [Linux]: https://man7.org/linux/man-pages/man2/rmdir.2.html
#[inline]
pub fn rmdir<P: path::Arg>(path: P) -> io::Result<()> {
path.into_with_c_str(backend::fs::syscalls::rmdir)
}
/// `link(old_path, new_path)`—Creates a hard link.
///
/// POSIX leaves it implementation-defined whether `link` follows a symlink in
/// `old_path`, or creates a new link to the symbolic link itself. On platforms
/// which have it, [`linkat`] avoids this problem since it has an [`AtFlags`]
/// paramter and the [`AtFlags::SYMLINK_FOLLOW`] flag determines whether
/// symlinks should be followed.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/link.html
/// [Linux]: https://man7.org/linux/man-pages/man2/link.2.html
/// [`linkat`]: crate::fs::linkat
/// [`AtFlags`]: crate::fs::AtFlags
/// [`AtFlags::SYMLINK_FOLLOW`]: crate::fs::AtFlags::SYMLINK_FOLLOW
#[inline]
pub fn link<P: path::Arg, Q: path::Arg>(old_path: P, new_path: Q) -> io::Result<()> {
old_path.into_with_c_str(|old_path| {
new_path.into_with_c_str(|new_path| backend::fs::syscalls::link(old_path, new_path))
})
}
/// `symlink(old_path, new_path)`—Creates a symlink.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html
/// [Linux]: https://man7.org/linux/man-pages/man2/symlink.2.html
#[inline]
pub fn symlink<P: path::Arg, Q: path::Arg>(old_path: P, new_path: Q) -> io::Result<()> {
old_path.into_with_c_str(|old_path| {
new_path.into_with_c_str(|new_path| backend::fs::syscalls::symlink(old_path, new_path))
})
}
/// `mkdir(path, mode)`—Creates a directory.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html
/// [Linux]: https://man7.org/linux/man-pages/man2/mkdir.2.html
#[inline]
pub fn mkdir<P: path::Arg>(path: P, mode: Mode) -> io::Result<()> {
path.into_with_c_str(|path| backend::fs::syscalls::mkdir(path, mode))
}
/// `access(path, access)`—Tests permissions for a file or directory.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html
/// [Linux]: https://man7.org/linux/man-pages/man2/access.2.html
#[cfg(not(target_os = "espidf"))]
#[inline]
pub fn access<P: path::Arg>(path: P, access: Access) -> io::Result<()> {
path.into_with_c_str(|path| backend::fs::syscalls::access(path, access))
}
/// `statfs`—Queries filesystem metadata.
///
/// Compared to [`statvfs`], this function often provides more information,
/// though it's less portable.
///
/// # References
/// - [Linux]
///
/// [Linux]: https://man7.org/linux/man-pages/man2/statfs.2.html
#[cfg(not(any(
solarish,
target_os = "espidf",
target_os = "haiku",
target_os = "netbsd",
target_os = "nto",
target_os = "redox",
target_os = "wasi",
)))]
#[inline]
pub fn statfs<P: path::Arg>(path: P) -> io::Result<StatFs> {
path.into_with_c_str(backend::fs::syscalls::statfs)
}
/// `statvfs`—Queries filesystem metadata, POSIX version.
///
/// Compared to [`statfs`], this function often provides less information,
/// but it is more portable. But even so, filesystems are very diverse and not
/// all the fields are meaningful for every filesystem. And `f_fsid` doesn't
/// seem to have a clear meaning anywhere.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/statvfs.html
/// [Linux]: https://man7.org/linux/man-pages/man2/statvfs.2.html
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
#[inline]
pub fn statvfs<P: path::Arg>(path: P) -> io::Result<StatVfs> {
path.into_with_c_str(backend::fs::syscalls::statvfs)
}