From: Markus Triska Date: Fri, 17 Jul 2020 16:48:39 +0000 (+0200) Subject: ADDED: file_creation_time/2 and file_access_time/2. X-Git-Tag: v0.8.127~7^2 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=17dd9239b612761ff4a2191e54e4b4e42618e1f5;p=scryer-prolog.git ADDED: file_creation_time/2 and file_access_time/2. The code can be simplified once if- and while-let-chains are available: https://github.com/rust-lang/rust/issues/53667 --- diff --git a/src/clause_types.rs b/src/clause_types.rs index 97aac77a..f43b30fd 100644 --- a/src/clause_types.rs +++ b/src/clause_types.rs @@ -179,7 +179,7 @@ pub enum SystemClauseType { DeleteFile, WorkingDirectory, PathCanonical, - FileModificationTime, + FileTime, DeleteAttribute, DeleteHeadAttribute, DynamicModuleResolution(usize), @@ -348,7 +348,7 @@ impl SystemClauseType { &SystemClauseType::DeleteFile => clause_name!("$delete_file"), &SystemClauseType::WorkingDirectory => clause_name!("$working_directory"), &SystemClauseType::PathCanonical => clause_name!("$path_canonical"), - &SystemClauseType::FileModificationTime => clause_name!("$file_modification_time"), + &SystemClauseType::FileTime => clause_name!("$file_time"), &SystemClauseType::REPL(REPLCodePtr::CompileBatch) => clause_name!("$compile_batch"), &SystemClauseType::REPL(REPLCodePtr::UseModule) => clause_name!("$use_module"), &SystemClauseType::REPL(REPLCodePtr::UseQualifiedModule) => { @@ -686,7 +686,7 @@ impl SystemClauseType { ("$delete_file", 1) => Some(SystemClauseType::DeleteFile), ("$working_directory", 2) => Some(SystemClauseType::WorkingDirectory), ("$path_canonical", 2) => Some(SystemClauseType::PathCanonical), - ("$file_modification_time", 2) => Some(SystemClauseType::FileModificationTime), + ("$file_time", 3) => Some(SystemClauseType::FileTime), ("$use_module", 1) => Some(SystemClauseType::REPL(REPLCodePtr::UseModule)), ("$use_module_from_file", 1) => Some(SystemClauseType::REPL(REPLCodePtr::UseModuleFromFile)), diff --git a/src/lib/files.pl b/src/lib/files.pl index adaa1e94..da511d06 100644 --- a/src/lib/files.pl +++ b/src/lib/files.pl @@ -54,7 +54,9 @@ make_directory/1, working_directory/2, path_canonical/2, - file_modification_time/2]). + file_modification_time/2, + file_creation_time/2, + file_access_time/2]). :- use_module(library(error)). :- use_module(library(lists)). @@ -123,12 +125,21 @@ path_canonical(Ps, Cs) :- '$path_canonical'(Ps, Cs). /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - T is the modification time of File. + T is, respectively, the modification, access or creation time of File. T is a time stamp, suitable for use in format_time//2 in library(time). For two time stamps A and B, if A precedes B, then A @< B holds. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ file_modification_time(File, T) :- - '$file_modification_time'(File, T0), + file_time_(File, modification, T). + +file_access_time(File, T) :- + file_time_(File, access, T). + +file_creation_time(File, T) :- + file_time_(File, creation, T). + +file_time_(File, Which, T) :- + '$file_time'(File, Which, T0), read_term_from_chars(T0, T). diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index 7763e963..3d150107 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -943,13 +943,32 @@ impl MachineState { } } } - &SystemClauseType::FileModificationTime => { + &SystemClauseType::FileTime => { let file = self.heap_pstr_iter(self[temp_v!(1)]).to_string(); + let which = match self.store(self.deref(self[temp_v!(2)])) { + Addr::Con(h) if self.heap.atom_at(h) => { + if let HeapCellValue::Atom(ref atom, _) = &self.heap[h] { + atom.as_str() + } else { + unreachable!() + } + } + _ => { + unreachable!() + } + }; + if let Ok(md) = fs::metadata(file) { - if let Ok(time) = md.modified() { + if let Ok(time) = + match which { + "modification" => { md.modified() } + "access" => { md.accessed() } + "creation" => { md.created() } + _ => { unreachable!() } + } { let chars = self.systemtime_to_timestamp(time); - self.unify(self[temp_v!(2)], chars); + self.unify(self[temp_v!(3)], chars); } else { self.fail = true; return Ok(());