Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for --changed-before and --changed-with for modification … #339

Merged
merged 2 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ num_cpus = "1.8"
regex = "1.0.0"
regex-syntax = "0.6"
ctrlc = "3.1"
humantime = "1.1.1"

[dependencies.clap]
version = "2.31.2"
Expand All @@ -51,3 +52,4 @@ libc = "0.2"
[dev-dependencies]
diff = "0.1"
tempdir = "0.3"
filetime = "0.2.1"
22 changes: 22 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ pub fn build_app() -> App<'static, 'static> {
.takes_value(true)
.hidden(true),
)
.arg(
arg("changed-within")
.long("changed-within")
.takes_value(true)
.number_of_values(1),
)
.arg(
arg("changed-before")
.long("changed-before")
.takes_value(true)
.number_of_values(1),
)
.arg(arg("pattern"))
.arg(arg("path").multiple(true))
}
Expand Down Expand Up @@ -292,5 +304,15 @@ fn usage() -> HashMap<&'static str, Help> {
'mi': mebibytes\n \
'gi': gibibytes\n \
'ti': tebibytes");
doc!(h, "changed-before"
, "Limit results based on modification time older than duration or date provided."
, "Limit results based on modification time older than duration provided:\n \
using a duration: <NUM>d <NUM>h <NUM>m <NUM>s (e.g. 10h, 1d, 35min...)\n \
or a date and time: YYYY-MM-DD HH:MM:SS");
doc!(h, "changed-within"
, "Limit results based on modification time within the duration provided or between date provided and now."
, "Limit results based on modification time within the duration provided:\n \
using a duration: <NUM>d <NUM>h <NUM>m <NUM>s (e.g. 10h, 1d, 35min...)\n \
or a date and time: YYYY-MM-DD HH:MM:SS");
h
}
100 changes: 99 additions & 1 deletion src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use std::ffi::OsString;
use std::path::PathBuf;
use std::process;
use std::time;
use std::time::{self, SystemTime};

use exec::CommandTemplate;
use lscolors::LsColors;
Expand Down Expand Up @@ -100,6 +100,38 @@ impl SizeFilter {
}
}

/// Filter based on time ranges
#[derive(Debug, PartialEq)]
pub enum TimeFilter {
Before(SystemTime),
After(SystemTime),
}

impl TimeFilter {
fn from_str(ref_time: &SystemTime, s: &str) -> Option<SystemTime> {
use humantime;
humantime::parse_duration(s)
.map(|duration| *ref_time - duration)
.or_else(|_| humantime::parse_rfc3339_weak(s))
.ok()
}

pub fn before(ref_time: &SystemTime, s: &str) -> Option<TimeFilter> {
TimeFilter::from_str(ref_time, s).map(TimeFilter::Before)
}

pub fn after(ref_time: &SystemTime, s: &str) -> Option<TimeFilter> {
TimeFilter::from_str(ref_time, s).map(TimeFilter::After)
}

pub fn applies_to(&self, t: &SystemTime) -> bool {
match self {
TimeFilter::Before(limit) => t <= limit,
TimeFilter::After(limit) => t >= limit,
}
}
}

/// Configuration options for *fd*.
pub struct FdOptions {
/// Whether the search is case-sensitive or case-insensitive.
Expand Down Expand Up @@ -162,6 +194,9 @@ pub struct FdOptions {

/// The given constraints on the size of returned files
pub size_constraints: Vec<SizeFilter>,

/// Constraints on last modification time of files
pub time_constraints: Vec<TimeFilter>,
}

/// Print error message to stderr.
Expand Down Expand Up @@ -468,4 +503,67 @@ mod tests {
let f = SizeFilter::from_string("+1K").unwrap();
assert!(f.is_within(1000));
}

#[test]
fn is_time_filter_applicable() {
use humantime;

let ref_time = humantime::parse_rfc3339("2010-10-10T10:10:10Z").unwrap();
assert!(
TimeFilter::after(&ref_time, "1min")
.unwrap()
.applies_to(&ref_time)
);
assert!(
!TimeFilter::before(&ref_time, "1min")
.unwrap()
.applies_to(&ref_time)
);

let t1m_ago = ref_time - time::Duration::from_secs(60);
assert!(
!TimeFilter::after(&ref_time, "30sec")
.unwrap()
.applies_to(&t1m_ago)
);
assert!(
TimeFilter::after(&ref_time, "2min")
.unwrap()
.applies_to(&t1m_ago)
);

assert!(
TimeFilter::before(&ref_time, "30sec")
.unwrap()
.applies_to(&t1m_ago)
);
assert!(
!TimeFilter::before(&ref_time, "2min")
.unwrap()
.applies_to(&t1m_ago)
);

let t10s_before = "2010-10-10 10:10:00";
assert!(
!TimeFilter::before(&ref_time, t10s_before)
.unwrap()
.applies_to(&ref_time)
);
assert!(
TimeFilter::before(&ref_time, t10s_before)
.unwrap()
.applies_to(&t1m_ago)
);

assert!(
TimeFilter::after(&ref_time, t10s_before)
.unwrap()
.applies_to(&ref_time)
);
assert!(
!TimeFilter::after(&ref_time, t10s_before)
.unwrap()
.applies_to(&t1m_ago)
);
}
}
21 changes: 20 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern crate clap;
extern crate ignore;
#[macro_use]
extern crate lazy_static;
extern crate humantime;
#[cfg(all(unix, not(target_os = "redox")))]
extern crate libc;
extern crate num_cpus;
Expand Down Expand Up @@ -40,7 +41,7 @@ use regex::{RegexBuilder, RegexSetBuilder};
use exec::CommandTemplate;
use internal::{
pattern_has_uppercase_char, print_error_and_exit, transform_args_with_exec, FdOptions,
FileTypes, SizeFilter,
FileTypes, SizeFilter, TimeFilter,
};
use lscolors::LsColors;

Expand Down Expand Up @@ -150,6 +151,23 @@ fn main() {
})
.unwrap_or_else(|| vec![]);

let now = time::SystemTime::now();
let mut time_constraints: Vec<TimeFilter> = Vec::new();
if let Some(t) = matches.value_of("changed-within") {
if let Some(f) = TimeFilter::after(&now, t) {
time_constraints.push(f);
} else {
print_error_and_exit(&format!("Error: {} is not a valid time.", t));
}
}
if let Some(t) = matches.value_of("changed-before") {
if let Some(f) = TimeFilter::before(&now, t) {
time_constraints.push(f);
} else {
print_error_and_exit(&format!("Error: {} is not a valid time.", t));
}
}

let config = FdOptions {
case_sensitive,
search_full_path: matches.is_present("full-path"),
Expand Down Expand Up @@ -226,6 +244,7 @@ fn main() {
.map(|vs| vs.map(PathBuf::from).collect())
.unwrap_or_else(|| vec![]),
size_constraints: size_limits,
time_constraints,
};

match RegexBuilder::new(&pattern_regex)
Expand Down
18 changes: 18 additions & 0 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,24 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) {
}
}

// Filter out unwanted modification times
if !config.time_constraints.is_empty() {
let mut matched = false;
if entry_path.is_file() {
if let Ok(metadata) = entry_path.metadata() {
if let Ok(modified) = metadata.modified() {
matched = config
.time_constraints
.iter()
.all(|tf| tf.applies_to(&modified));
}
}
}
if !matched {
return ignore::WalkState::Continue;
}
}

let search_str_o = if config.search_full_path {
match fshelper::path_absolute_form(entry_path) {
Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()),
Expand Down
Loading