- Add lock file
This commit is contained in:
Ivan Bushchik 2023-06-09 09:01:47 +03:00
parent 85eaa42ad8
commit 9c6d1a8190
No known key found for this signature in database
GPG key ID: 9F6DDABE11A2674D
3 changed files with 19 additions and 5 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "aliurl" name = "aliurl"
version = "0.3.2" version = "0.3.3"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/ivabus/aliurl" repository = "https://github.com/ivabus/aliurl"

View file

@ -50,7 +50,17 @@ struct Alias {
redirect_with_ad: Option<bool>, redirect_with_ad: Option<bool>,
} }
fn lock() {
while std::path::Path::new("./alias.json.lock").exists() {}
std::fs::File::create("./alias.json.lock").unwrap();
}
fn unlock() {
std::fs::remove_file("./alias.json.lock").unwrap()
}
fn read_aliases() -> Vec<Alias> { fn read_aliases() -> Vec<Alias> {
lock();
if !std::path::Path::new("./alias.json").exists() { if !std::path::Path::new("./alias.json").exists() {
let mut file = std::fs::File::create("./alias.json").unwrap(); let mut file = std::fs::File::create("./alias.json").unwrap();
file.write_all(b"[]").unwrap(); file.write_all(b"[]").unwrap();
@ -66,6 +76,7 @@ fn read_aliases() -> Vec<Alias> {
let mut contents = String::new(); let mut contents = String::new();
buf_reader.read_to_string(&mut contents).unwrap(); buf_reader.read_to_string(&mut contents).unwrap();
let aliases_list: Vec<Alias> = serde_json::from_str(&contents).unwrap(); let aliases_list: Vec<Alias> = serde_json::from_str(&contents).unwrap();
unlock();
aliases_list aliases_list
} }

View file

@ -71,7 +71,7 @@ fn check_access_key(key: Option<String>) -> Result<String, (Status, RawJson<Stri
} }
#[post("/api/create_alias", data = "<data>")] #[post("/api/create_alias", data = "<data>")]
pub fn create_alias(data: &RawStr) -> (Status, RawJson<String>) { pub async fn create_alias(data: &RawStr) -> (Status, RawJson<String>) {
let data: CreateAliasRequest = match serde_json::from_str(&data.to_string()) { let data: CreateAliasRequest = match serde_json::from_str(&data.to_string()) {
Ok(req) => req, Ok(req) => req,
Err(e) => { Err(e) => {
@ -82,8 +82,8 @@ pub fn create_alias(data: &RawStr) -> (Status, RawJson<String>) {
if let Err(e) = check_access_key(data.access_key) { if let Err(e) = check_access_key(data.access_key) {
return e; return e;
} }
let mut aliases_list = read_aliases(); let mut aliases_list = read_aliases();
lock();
let mut file = std::fs::File::options().write(true).open("./alias.json").unwrap(); let mut file = std::fs::File::options().write(true).open("./alias.json").unwrap();
let alias = match data.alias { let alias = match data.alias {
None => { None => {
@ -128,12 +128,13 @@ pub fn create_alias(data: &RawStr) -> (Status, RawJson<String>) {
file.write_all(serde_json::to_string(&aliases_list).unwrap().as_bytes()).unwrap(); file.write_all(serde_json::to_string(&aliases_list).unwrap().as_bytes()).unwrap();
file.sync_all().unwrap(); file.sync_all().unwrap();
unlock();
return (Status::Ok, RawJson(serde_json::to_string(&alias).unwrap())); return (Status::Ok, RawJson(serde_json::to_string(&alias).unwrap()));
} }
#[post("/api/get_aliases", data = "<data>")] #[post("/api/get_aliases", data = "<data>")]
pub fn get_aliases(data: &RawStr) -> (Status, RawJson<String>) { pub async fn get_aliases(data: &RawStr) -> (Status, RawJson<String>) {
let data: GetAliasesRequest = match serde_json::from_str(&data.to_string()) { let data: GetAliasesRequest = match serde_json::from_str(&data.to_string()) {
Ok(req) => req, Ok(req) => req,
Err(e) => { Err(e) => {
@ -149,7 +150,7 @@ pub fn get_aliases(data: &RawStr) -> (Status, RawJson<String>) {
} }
#[post("/api/remove_alias", data = "<data>")] #[post("/api/remove_alias", data = "<data>")]
pub fn remove_alias(data: &RawStr) -> (Status, RawJson<String>) { pub async fn remove_alias(data: &RawStr) -> (Status, RawJson<String>) {
let data: RemoveAliasRequest = match serde_json::from_str(&data.to_string()) { let data: RemoveAliasRequest = match serde_json::from_str(&data.to_string()) {
Ok(req) => req, Ok(req) => req,
Err(e) => { Err(e) => {
@ -161,6 +162,7 @@ pub fn remove_alias(data: &RawStr) -> (Status, RawJson<String>) {
} }
let mut aliases_list = read_aliases(); let mut aliases_list = read_aliases();
let mut removed_aliases: Vec<Alias> = vec![]; let mut removed_aliases: Vec<Alias> = vec![];
lock();
let mut file = std::fs::File::options().write(true).open("./alias.json").unwrap(); let mut file = std::fs::File::options().write(true).open("./alias.json").unwrap();
for i in (0..aliases_list.len()).rev() { for i in (0..aliases_list.len()).rev() {
@ -172,6 +174,7 @@ pub fn remove_alias(data: &RawStr) -> (Status, RawJson<String>) {
file.write_all(&aliases_list.as_bytes()).unwrap(); file.write_all(&aliases_list.as_bytes()).unwrap();
file.set_len(aliases_list.as_bytes().len() as u64).unwrap(); file.set_len(aliases_list.as_bytes().len() as u64).unwrap();
file.sync_all().unwrap(); file.sync_all().unwrap();
unlock();
return (Status::Ok, RawJson(serde_json::to_string(&removed_aliases).unwrap())); return (Status::Ok, RawJson(serde_json::to_string(&removed_aliases).unwrap()));
} }