Page MenuHomeDevCentral

D4150.diff
No OneTemporary

D4150.diff

diff --git a/Cargo.toml b/Cargo.toml
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,6 +24,7 @@
[dependencies.tokio]
version = "1.53.1"
features = [
+ "fs",
"macros",
"process",
"rt-multi-thread",
diff --git a/src/actions.rs b/src/actions.rs
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -49,7 +49,7 @@
let status = recipes.run_recipe(&site, action).await;
if action == "init" && status == RecipeStatus::Success {
- db.set_initialized(&site.name);
+ db.set_initialized(&site.name).await;
}
Ok(status)
diff --git a/src/db.rs b/src/db.rs
--- a/src/db.rs
+++ b/src/db.rs
@@ -5,13 +5,12 @@
// License: BSD-2-Clause
// -------------------------------------------------------------
-use std::fs;
-use std::fs::OpenOptions;
use std::io::Error as IOError;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use log::warn;
+use tokio::fs::{self, OpenOptions};
use crate::config::AlkaneConfig;
@@ -37,12 +36,12 @@
self.get_initialized_path(site_name).exists()
}
- pub fn set_initialized(&self, site_name: &str) -> bool {
+ pub async fn set_initialized(&self, site_name: &str) -> bool {
let path = self.get_initialized_path(site_name);
- if !path.exists() {
- match ensure_parent_directory_exists(&path) {
- Ok(_) => match touch(&path) {
+ match fs::try_exists(&path).await {
+ Ok(false) => match ensure_parent_directory_exists(&path).await {
+ Ok(_) => match touch(&path).await {
Ok(_) => true,
Err(error) => {
warn!("Can't mark site {} as initialized: {:?}", site_name, error);
@@ -55,9 +54,16 @@
false
}
+ },
+ Ok(true) => true,
+ Err(error) => {
+ warn!(
+ "Can't check initialization state for {}: {:?}",
+ site_name, error
+ );
+
+ false
}
- } else {
- true
}
}
@@ -68,21 +74,19 @@
/// Creates an empty file, similar to the touch command
/// Ignores existing files.
-fn touch(path: &PathBuf) -> Result<(), IOError> {
+async fn touch(path: &Path) -> Result<(), IOError> {
let mut options = OpenOptions::new();
options.create(true).write(true);
- options.open(path).map(|_| ())
+ options.open(path).await.map(|_| ())
}
-fn ensure_parent_directory_exists(path: &PathBuf) -> Result<(), IOError> {
+async fn ensure_parent_directory_exists(path: &Path) -> Result<(), IOError> {
let parent = path
.parent()
.ok_or_else(|| IOError::new(ErrorKind::InvalidInput, "Invalid path"))?;
- if !parent.exists() {
- fs::create_dir_all(parent)?;
- }
+ fs::create_dir_all(parent).await?;
Ok(())
}
@@ -93,24 +97,24 @@
#[cfg(test)]
mod tests {
- use std::fs;
-
use super::*;
- #[test]
- pub fn test_touch() {
+ #[tokio::test]
+ pub async fn test_touch() {
let path = Path::new("tmp-touch.empty");
assert!(
!path.exists(),
"Temporary file tmp-touch.empty shouldn't exist when test starts"
);
- touch(&path.to_path_buf()).expect("File can't be created");
+ touch(path).await.expect("File can't be created");
assert!(
path.exists(),
"Function touch returned Ok but temporary file does NOT exist."
);
- fs::remove_file(path).expect("Can't remove file after having created it.")
+ fs::remove_file(path)
+ .await
+ .expect("Can't remove file after having created it.")
}
}

File Metadata

Mime Type
text/plain
Expires
Mon, Aug 10, 22:07 (22 h, 35 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3983291
Default Alt Text
D4150.diff (3 KB)

Event Timeline