Skip to content

Commit e407919

Browse files
committed
Rearrange things to remove Windows build warnings and fmt better
1 parent dd13012 commit e407919

File tree

5 files changed

+52
-44
lines changed

5 files changed

+52
-44
lines changed

src/env.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
use cfg_if::cfg_if;
22

3+
fn emulate_freebsd_from_env() -> bool {
4+
use once_cell::sync::OnceCell;
5+
static CELL: OnceCell<bool> = OnceCell::new();
6+
*CELL.get_or_init(|| {
7+
let emulate = !matches!(
8+
std::env::var("POSSUM_EMULATE_FREEBSD"),
9+
Err(std::env::VarError::NotPresent)
10+
);
11+
if emulate {
12+
super::error!("emulating freebsd");
13+
}
14+
emulate
15+
})
16+
}
17+
18+
/// FreeBSD doesn't support file range locking or block cloning (yet). We can emulate FreeBSD on
19+
/// platforms that have flock().
320
pub(crate) fn emulate_freebsd() -> bool {
421
cfg_if! {
522
if #[cfg(target_os = "freebsd")] {
623
true
724
} else {
8-
use once_cell::sync::OnceCell;
9-
static CELL: OnceCell<bool> = OnceCell::new();
10-
*CELL.get_or_init(|| {
11-
let emulate = !matches!(
12-
std::env::var("POSSUM_EMULATE_FREEBSD"),
13-
Err(std::env::VarError::NotPresent)
14-
);
15-
if emulate {
16-
super::error!("emulating freebsd");
17-
}
18-
emulate
19-
})
25+
emulate_freebsd_from_env()
2026
}
2127
}
2228
}
2329

30+
/// Whether to use flock() instead of file segment locking. flock() is not available on Windows.
2431
pub(crate) fn flocking() -> bool {
2532
emulate_freebsd()
2633
}

src/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use shuttle::sync;
66
#[cfg(not(shuttle))]
77
use std::sync;
88

9-
pub use sync::*;
109
use sync::Mutex as InnerMutex;
1110
use sync::MutexGuard as InnerMutexGuard;
11+
pub use sync::*;
1212
// These types work in any sync context.
1313
use std::sync::{LockResult, PoisonError};
1414

src/sys/mod.rs

-29
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ pub use flock::*;
1515
pub(crate) use pathconf::*;
1616
pub use punchfile::*;
1717

18-
use crate::env::{emulate_freebsd, flocking};
19-
2018
cfg_if! {
2119
if #[cfg(windows)] {
2220
mod windows;
@@ -75,30 +73,3 @@ pub trait FileSystemFlags {
7573
pub trait DirMeta {
7674
fn file_system_flags(&self) -> io::Result<impl FileSystemFlags>;
7775
}
78-
79-
struct UnixFilesystemFlags {}
80-
81-
impl FileSystemFlags for UnixFilesystemFlags {
82-
fn supports_sparse_files(&self) -> bool {
83-
// AFAIK, all unix systems support sparse files on all filesystems.
84-
true
85-
}
86-
87-
fn supports_block_cloning(&self) -> Option<bool> {
88-
// AFAIK there's no way to check if a filesystem supports block cloning on non-Windows
89-
// platforms, and even then it depends on where you're copying to/from, sometimes even on
90-
// the same filesystem.
91-
if emulate_freebsd() {
92-
Some(false)
93-
} else {
94-
None
95-
}
96-
}
97-
}
98-
99-
#[cfg(not(windows))]
100-
impl DirMeta for File {
101-
fn file_system_flags(&self) -> io::Result<impl FileSystemFlags> {
102-
Ok(UnixFilesystemFlags {})
103-
}
104-
}

src/sys/unix.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
1+
use std::fs::File;
2+
use std::io;
13
use std::path::Path;
24

35
pub(crate) use nix::errno::errno;
6+
use crate::sys::{DirMeta, FileSystemFlags};
7+
8+
use crate::env::{emulate_freebsd};
49

510
pub fn path_disk_allocation(path: &Path) -> std::io::Result<u64> {
611
let metadata = std::fs::metadata(path)?;
712
use std::os::unix::fs::MetadataExt;
813
Ok(metadata.blocks() * 512)
914
}
15+
16+
struct UnixFilesystemFlags {}
17+
18+
impl FileSystemFlags for UnixFilesystemFlags {
19+
fn supports_sparse_files(&self) -> bool {
20+
// AFAIK, all unix systems support sparse files on all filesystems.
21+
true
22+
}
23+
24+
fn supports_block_cloning(&self) -> Option<bool> {
25+
// AFAIK there's no way to check if a filesystem supports block cloning on non-Windows
26+
// platforms, and even then it depends on where you're copying to/from, sometimes even on
27+
// the same filesystem.
28+
if emulate_freebsd() {
29+
Some(false)
30+
} else {
31+
None
32+
}
33+
}
34+
}
35+
36+
impl DirMeta for File {
37+
fn file_system_flags(&self) -> io::Result<impl FileSystemFlags> {
38+
Ok(UnixFilesystemFlags {})
39+
}
40+
}

src/tests.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use crate::testing::*;
1010

1111
pub(crate) fn check_concurrency(
1212
f: impl Fn() -> anyhow::Result<()> + Send + Sync + 'static,
13-
#[allow(unused_variables)]
14-
iterations_hint: usize,
13+
#[allow(unused_variables)] iterations_hint: usize,
1514
) -> anyhow::Result<()> {
1615
#[cfg(loom)]
1716
{

0 commit comments

Comments
 (0)