-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lock session.lock so other processes know that we currently read and write to the level. This is vanilla behavior. Test: Tested to start the a new server while on was locking the file, and got `Failed to lock level: AlreadyLocked("session.lock")` as expected
- Loading branch information
Showing
5 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use file_guard::{FileGuard, Lock}; | ||
|
||
use super::{LevelLocker, LockError}; | ||
|
||
use std::{fs::File, io::Write, sync::Arc}; | ||
|
||
pub struct AnvilLevelLocker { | ||
_lock: Option<FileGuard<Arc<File>>>, | ||
} | ||
|
||
const SESSION_LOCK_FILE_NAME: &str = "session.lock"; | ||
|
||
const SNOWMAN: &[u8] = "☃".as_bytes(); | ||
|
||
impl LevelLocker<Self> for AnvilLevelLocker { | ||
fn look(folder: &crate::level::LevelFolder) -> Result<Self, LockError> { | ||
let file_path = folder.root_folder.join(SESSION_LOCK_FILE_NAME); | ||
let mut file = File::options() | ||
.create(true) | ||
.truncate(false) | ||
.write(true) | ||
.open(file_path) | ||
.unwrap(); | ||
// im not joking, mojang writes a snowman into the lock file | ||
file.write_all(SNOWMAN) | ||
.map_err(|_| LockError::FailedWrite)?; | ||
let file_arc = Arc::new(file); | ||
let lock = file_guard::try_lock(file_arc, Lock::Exclusive, 0, 1) | ||
.map_err(|_| LockError::AlreadyLocked(SESSION_LOCK_FILE_NAME.to_string()))?; | ||
Ok(Self { _lock: Some(lock) }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use thiserror::Error; | ||
|
||
use crate::level::LevelFolder; | ||
|
||
pub mod anvil; | ||
|
||
// Gets unlocked when dropped | ||
pub trait LevelLocker<T>: Send + Sync { | ||
fn look(folder: &LevelFolder) -> Result<T, LockError>; | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum LockError { | ||
#[error("Oh no, Level is already locked by {0}")] | ||
AlreadyLocked(String), | ||
#[error("Failed to write into lock file")] | ||
FailedWrite, | ||
} |