Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
thirteenowls committed May 26, 2024
1 parent 289a5dc commit 0951c68
Show file tree
Hide file tree
Showing 12 changed files with 578 additions and 178 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ async-std = { version = "1", optional = true }
smol = { version = "2", optional = true }
tokio = { version = "1", optional = true, default-features = false, features = ["fs"] }


[dev-dependencies]
async-std = { version = "1", features = ["attributes"] }
smol-potat = "1.1"
tempdir = "0.3"
tokio = { version = "1", features = ["full"]}
tokio = { version = "1", features = ["full"] }
libc = "0.2"

[package.metadata.docs.rs]
Expand Down
3 changes: 1 addition & 2 deletions src/file_ext/async_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ macro_rules! async_file_ext {
/// platform-specific behavior. File locks are implemented with
/// [`flock(2)`](http://man7.org/linux/man-pages/man2/flock.2.html) on Unix and
/// [`LockFile`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365202(v=vs.85).aspx)
/// on Windows.
/// on Windows.
pub trait AsyncFileExt {

/// Returns the amount of physical space allocated for a file.
Expand Down Expand Up @@ -96,4 +96,3 @@ cfg_smol! {
cfg_tokio! {
pub(crate) mod tokio_impl;
}

102 changes: 81 additions & 21 deletions src/file_ext/async_impl/async_std_impl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use async_std::fs::File;
#[cfg(unix)]
use crate::unix::async_impl::async_std_impl as sys;
#[cfg(windows)]
use crate::windows::async_impl::async_std_impl as sys;
use async_std::fs::File;

async_file_ext!(File, "async_std::fs::File");

Expand All @@ -12,26 +12,51 @@ mod test {
extern crate tempdir;
extern crate test;

use crate::{
allocation_granularity, async_std::AsyncFileExt, available_space, free_space,
lock_contended_error, total_space,
};
use async_std::fs;
use crate::{allocation_granularity, available_space, async_std::AsyncFileExt, free_space, lock_contended_error, total_space};

/// Tests shared file lock operations.
#[async_std::test]
async fn lock_shared() {
let tempdir = tempdir::TempDir::new("fs4").unwrap();
let path = tempdir.path().join("fs4");
let file1 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file2 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file3 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file1 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let file2 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let file3 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();

// Concurrent shared access is OK, but not shared and exclusive.
file1.lock_shared().unwrap();
file2.lock_shared().unwrap();
assert_eq!(file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(
file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind()
);
file1.unlock().unwrap();
assert_eq!(file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(
file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind()
);

// Once all shared file locks are dropped, an exclusive lock may be created;
file2.unlock().unwrap();
Expand All @@ -43,15 +68,31 @@ mod test {
async fn lock_exclusive() {
let tempdir = tempdir::TempDir::new("fs4").unwrap();
let path = tempdir.path().join("fs4");
let file1 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file2 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file1 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let file2 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();

// No other access is possible once an exclusive lock is created.
file1.lock_exclusive().unwrap();
assert_eq!(file2.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(
file2.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind()
);
assert_eq!(
file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind()
);

// Once the exclusive lock is dropped, the second file is able to create a lock.
file1.unlock().unwrap();
Expand All @@ -63,12 +104,26 @@ mod test {
async fn lock_cleanup() {
let tempdir = tempdir::TempDir::new("fs4").unwrap();
let path = tempdir.path().join("fs4");
let file1 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file2 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file1 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let file2 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();

file1.lock_exclusive().unwrap();
assert_eq!(file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(
file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind()
);

// Drop file1; the lock should be released.
drop(file1);
Expand All @@ -80,7 +135,12 @@ mod test {
async fn allocate() {
let tempdir = tempdir::TempDir::new("fs4").unwrap();
let path = tempdir.path().join("fs4");
let file = fs::OpenOptions::new().write(true).create(true).open(&path).await.unwrap();
let file = fs::OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let blksize = allocation_granularity(&path).unwrap();

// New files are created with no allocated size.
Expand Down Expand Up @@ -114,4 +174,4 @@ mod test {
assert!(total_space > available_space);
assert!(available_space <= free_space);
}
}
}
102 changes: 81 additions & 21 deletions src/file_ext/async_impl/smol_impl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use smol::fs::File;
#[cfg(unix)]
use crate::unix::async_impl::smol_impl as sys;
#[cfg(windows)]
use crate::windows::async_impl::smol_impl as sys;
use smol::fs::File;

async_file_ext!(File, "smol::fs::File");

Expand All @@ -12,26 +12,51 @@ mod test {
extern crate tempdir;
extern crate test;

use crate::{
allocation_granularity, available_space, free_space, lock_contended_error,
smol::AsyncFileExt, total_space,
};
use smol::fs;
use crate::{allocation_granularity, available_space, smol::AsyncFileExt, free_space, lock_contended_error, total_space};

/// Tests shared file lock operations.
#[smol_potat::test]
async fn lock_shared() {
let tempdir = tempdir::TempDir::new("fs4").unwrap();
let path = tempdir.path().join("fs4");
let file1 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file2 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file3 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file1 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let file2 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let file3 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();

// Concurrent shared access is OK, but not shared and exclusive.
file1.lock_shared().unwrap();
file2.lock_shared().unwrap();
assert_eq!(file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(
file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind()
);
file1.unlock().unwrap();
assert_eq!(file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(
file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind()
);

// Once all shared file locks are dropped, an exclusive lock may be created;
file2.unlock().unwrap();
Expand All @@ -43,15 +68,31 @@ mod test {
async fn lock_exclusive() {
let tempdir = tempdir::TempDir::new("fs4").unwrap();
let path = tempdir.path().join("fs4");
let file1 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file2 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file1 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let file2 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();

// No other access is possible once an exclusive lock is created.
file1.lock_exclusive().unwrap();
assert_eq!(file2.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(
file2.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind()
);
assert_eq!(
file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind()
);

// Once the exclusive lock is dropped, the second file is able to create a lock.
file1.unlock().unwrap();
Expand All @@ -63,12 +104,26 @@ mod test {
async fn lock_cleanup() {
let tempdir = tempdir::TempDir::new("fs4").unwrap();
let path = tempdir.path().join("fs4");
let file1 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file2 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).await.unwrap();
let file1 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let file2 = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.await
.unwrap();

file1.lock_exclusive().unwrap();
assert_eq!(file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(
file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind()
);

// Drop file1; the lock should be released.
drop(file1);
Expand All @@ -80,7 +135,12 @@ mod test {
async fn allocate() {
let tempdir = tempdir::TempDir::new("fs4").unwrap();
let path = tempdir.path().join("fs4");
let file = fs::OpenOptions::new().write(true).create(true).open(&path).await.unwrap();
let file = fs::OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.await
.unwrap();
let blksize = allocation_granularity(&path).unwrap();

// New files are created with no allocated size.
Expand Down Expand Up @@ -114,4 +174,4 @@ mod test {
assert!(total_space > available_space);
assert!(available_space <= free_space);
}
}
}
Loading

0 comments on commit 0951c68

Please sign in to comment.