Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ec0666d0e | |||
| 9e4eced44d |
Generated
+2
@@ -3259,12 +3259,14 @@ dependencies = [
|
|||||||
"clap 4.1.11",
|
"clap 4.1.11",
|
||||||
"clap_complete",
|
"clap_complete",
|
||||||
"clap_complete_fig",
|
"clap_complete_fig",
|
||||||
|
"filetime",
|
||||||
"log",
|
"log",
|
||||||
"pretty_env_logger",
|
"pretty_env_logger",
|
||||||
"semver 0.11.0",
|
"semver 0.11.0",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"vergen",
|
"vergen",
|
||||||
|
"walkdir",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -12,11 +12,13 @@ atty = "0.2"
|
|||||||
clap = { version = "4.0", features = ["derive"] }
|
clap = { version = "4.0", features = ["derive"] }
|
||||||
clap_complete = "4.0"
|
clap_complete = "4.0"
|
||||||
clap_complete_fig = "4.0"
|
clap_complete_fig = "4.0"
|
||||||
|
filetime = "0.2.20"
|
||||||
log = { workspace = true }
|
log = { workspace = true }
|
||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
semver = "0.11"
|
semver = "0.11"
|
||||||
serde = { workspace = true, features = ["derive"], optional = true }
|
serde = { workspace = true, features = ["derive"], optional = true }
|
||||||
serde_json = { workspace = true, optional = true }
|
serde_json = { workspace = true, optional = true }
|
||||||
|
walkdir = "2.3.3"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
vergen = { version = "=7.4.3", default-features = false, features = ["build", "git", "rustc", "cargo"] }
|
vergen = { version = "=7.4.3", default-features = false, features = ["build", "git", "rustc", "cargo"] }
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
io,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
|
use filetime::FileTime;
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
|
/// Keeps the most recently accessed directories in the given directory.
|
||||||
|
pub fn keep_recently_accessed_dirs<P: AsRef<Path>>(dir: P, num_to_keep: usize) -> io::Result<()> {
|
||||||
|
let mut dir_access_times: HashMap<PathBuf, FileTime> = HashMap::new();
|
||||||
|
|
||||||
|
for entry in std::fs::read_dir(dir)?.flatten() {
|
||||||
|
if entry.file_type().map(|e| e.is_dir()).unwrap_or(false) {
|
||||||
|
// For each sub directory, keep track of the most recently accessed time of any
|
||||||
|
// file inside that sub directory.
|
||||||
|
let sub_dir = entry.path();
|
||||||
|
let mut most_recent_access_time: Option<FileTime> = None;
|
||||||
|
|
||||||
|
for file_entry in WalkDir::new(&sub_dir)
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|e| e.ok())
|
||||||
|
.filter(|e| e.file_type().is_file())
|
||||||
|
{
|
||||||
|
let metadata = file_entry.metadata().unwrap();
|
||||||
|
let accessed_time = FileTime::from_last_access_time(&metadata);
|
||||||
|
|
||||||
|
if most_recent_access_time.map_or(true, |current| accessed_time > current) {
|
||||||
|
most_recent_access_time = Some(accessed_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(access_time) = most_recent_access_time {
|
||||||
|
dir_access_times.insert(sub_dir, access_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut sorted_dir_access_times: Vec<(PathBuf, FileTime)> =
|
||||||
|
dir_access_times.into_iter().collect();
|
||||||
|
sorted_dir_access_times.sort_unstable_by_key(|(_, accessed_time)| *accessed_time);
|
||||||
|
sorted_dir_access_times.reverse();
|
||||||
|
let dirs_to_remove = sorted_dir_access_times.split_off(num_to_keep);
|
||||||
|
|
||||||
|
for (path, _) in dirs_to_remove {
|
||||||
|
println!("Removing: {:?}", path);
|
||||||
|
if let Err(err) = std::fs::remove_dir_all(path.clone()) {
|
||||||
|
println!("Failed to remove {:?} due to: {:?}", path, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
pub mod build_information;
|
pub mod build_information;
|
||||||
pub mod completions;
|
pub mod completions;
|
||||||
|
pub mod file_handling;
|
||||||
pub mod logging;
|
pub mod logging;
|
||||||
pub mod output_format;
|
pub mod output_format;
|
||||||
pub mod version_checker;
|
pub mod version_checker;
|
||||||
|
|||||||
@@ -2,9 +2,11 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
use crate::commands::{ensure_config_version_compatibility, OverrideConfig};
|
use crate::commands::{ensure_config_version_compatibility, OverrideConfig};
|
||||||
|
use crate::config::{Config, MAX_NUMBER_OF_CONFIGS_TO_KEEP};
|
||||||
use crate::support::config::build_config;
|
use crate::support::config::build_config;
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
use nym_bin_common::output_format::OutputFormat;
|
use nym_bin_common::output_format::OutputFormat;
|
||||||
|
use nym_config::NymConfig;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@@ -113,7 +115,22 @@ fn special_addresses() -> Vec<&'static str> {
|
|||||||
vec!["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"]
|
vec!["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prune_old_configs() {
|
||||||
|
let default_root_directory = Config::default_root_directory();
|
||||||
|
if let Err(err) = nym_bin_common::file_handling::keep_recently_accessed_dirs(
|
||||||
|
default_root_directory,
|
||||||
|
MAX_NUMBER_OF_CONFIGS_TO_KEEP,
|
||||||
|
) {
|
||||||
|
eprintln!(
|
||||||
|
"Error encountered while pruning old configurations: {}",
|
||||||
|
err
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn execute(args: Run) -> Result<(), Box<dyn Error + Send + Sync>> {
|
pub async fn execute(args: Run) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
|
prune_old_configs();
|
||||||
|
|
||||||
let id = args.id.clone();
|
let id = args.id.clone();
|
||||||
eprintln!("Starting gateway {id}...");
|
eprintln!("Starting gateway {id}...");
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ const DEFAULT_MAXIMUM_CONNECTION_BUFFER_SIZE: usize = 128;
|
|||||||
const DEFAULT_STORED_MESSAGE_FILENAME_LENGTH: u16 = 16;
|
const DEFAULT_STORED_MESSAGE_FILENAME_LENGTH: u16 = 16;
|
||||||
const DEFAULT_MESSAGE_RETRIEVAL_LIMIT: i64 = 100;
|
const DEFAULT_MESSAGE_RETRIEVAL_LIMIT: i64 = 100;
|
||||||
|
|
||||||
|
pub const MAX_NUMBER_OF_CONFIGS_TO_KEEP: usize = 100_000;
|
||||||
|
|
||||||
pub fn missing_string_value() -> String {
|
pub fn missing_string_value() -> String {
|
||||||
MISSING_VALUE.to_string()
|
MISSING_VALUE.to_string()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user