| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use std::fs::{Metadata, OpenOptions}; |
| 7 |
use std::io; |
| 8 |
use std::path::Path; |
| 9 |
|
| 10 |
|
| 11 |
pub fn set_mode(p: &Path, mode: u32) -> io::Result<()> { |
| 12 |
#[cfg(unix)] |
| 13 |
{ |
| 14 |
use std::os::unix::fs::PermissionsExt; |
| 15 |
std::fs::set_permissions(p, std::fs::Permissions::from_mode(mode)) |
| 16 |
} |
| 17 |
#[cfg(not(unix))] |
| 18 |
{ |
| 19 |
let _ = (p, mode); |
| 20 |
Ok(()) |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
pub fn mode(md: &Metadata) -> u32 { |
| 26 |
#[cfg(unix)] |
| 27 |
{ |
| 28 |
use std::os::unix::fs::MetadataExt; |
| 29 |
md.mode() & 0o7777 |
| 30 |
} |
| 31 |
#[cfg(not(unix))] |
| 32 |
{ |
| 33 |
let base = if md.is_dir() { 0o755 } else { 0o644 }; |
| 34 |
if md.permissions().readonly() { |
| 35 |
base & !0o222 |
| 36 |
} else { |
| 37 |
base |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
pub fn private(o: &mut OpenOptions) -> &mut OpenOptions { |
| 44 |
#[cfg(unix)] |
| 45 |
{ |
| 46 |
use std::os::unix::fs::OpenOptionsExt; |
| 47 |
o.mode(0o600) |
| 48 |
} |
| 49 |
#[cfg(not(unix))] |
| 50 |
{ |
| 51 |
o |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
pub fn create_private_dir(p: &Path) -> io::Result<()> { |
| 57 |
#[cfg(unix)] |
| 58 |
{ |
| 59 |
std::os::unix::fs::DirBuilderExt::mode(std::fs::DirBuilder::new().recursive(true), 0o700).create(p) |
| 60 |
} |
| 61 |
#[cfg(not(unix))] |
| 62 |
{ |
| 63 |
std::fs::create_dir_all(p) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
pub fn disk_bytes(md: &Metadata) -> u64 { |
| 69 |
#[cfg(unix)] |
| 70 |
{ |
| 71 |
use std::os::unix::fs::MetadataExt; |
| 72 |
md.blocks() * 512 |
| 73 |
} |
| 74 |
#[cfg(not(unix))] |
| 75 |
{ |
| 76 |
md.len() |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
pub fn shared_identity(md: &Metadata) -> Option<(u64, u64)> { |
| 83 |
#[cfg(unix)] |
| 84 |
{ |
| 85 |
use std::os::unix::fs::MetadataExt; |
| 86 |
(md.nlink() > 1 && !md.is_dir()).then(|| (md.dev(), md.ino())) |
| 87 |
} |
| 88 |
#[cfg(not(unix))] |
| 89 |
{ |
| 90 |
let _ = md; |
| 91 |
None |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
pub fn symlink(target: &Path, link: &Path) -> io::Result<()> { |
| 98 |
#[cfg(unix)] |
| 99 |
{ |
| 100 |
std::os::unix::fs::symlink(target, link) |
| 101 |
} |
| 102 |
#[cfg(windows)] |
| 103 |
{ |
| 104 |
std::os::windows::fs::symlink_file(target, link) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
pub fn rename(from: &Path, to: &Path) -> io::Result<()> { |
| 112 |
#[cfg(windows)] |
| 113 |
{ |
| 114 |
use std::os::windows::ffi::OsStrExt; |
| 115 |
let wide = |p: &Path| p.as_os_str().encode_wide().chain(Some(0)).collect::<Vec<u16>>(); |
| 116 |
let (f, t) = (wide(from), wide(to)); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
let mut tries = 0; |
| 121 |
loop { |
| 122 |
|
| 123 |
if unsafe { windows_sys::Win32::Storage::FileSystem::MoveFileExW(f.as_ptr(), t.as_ptr(), 0) } != 0 { |
| 124 |
return Ok(()); |
| 125 |
} |
| 126 |
let e = io::Error::last_os_error(); |
| 127 |
tries += 1; |
| 128 |
if tries >= 30 || !matches!(e.raw_os_error(), Some(32) | Some(5)) { |
| 129 |
return Err(e); |
| 130 |
} |
| 131 |
std::thread::sleep(std::time::Duration::from_secs(1)); |
| 132 |
} |
| 133 |
} |
| 134 |
#[cfg(not(windows))] |
| 135 |
{ |
| 136 |
std::fs::rename(from, to) |
| 137 |
} |
| 138 |
} |