]> Repositorios git - scryer-prolog.git/commitdiff
ADDED: file_creation_time/2 and file_access_time/2.
authorMarkus Triska <[email protected]>
Fri, 17 Jul 2020 16:48:39 +0000 (18:48 +0200)
committerMarkus Triska <[email protected]>
Fri, 17 Jul 2020 16:48:39 +0000 (18:48 +0200)
The code can be simplified once if- and while-let-chains are available:

    https://github.com/rust-lang/rust/issues/53667

src/clause_types.rs
src/lib/files.pl
src/machine/system_calls.rs

index 97aac77a508ca5a26a30cd26081c109c9c461dfe..f43b30fdccada6011eb3264e4053906c66398ae6 100644 (file)
@@ -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)),
index adaa1e944dbcc3f15eb896598d0082a6d8082b4b..da511d06be389e4253c34797e11445e45a3d1ea5 100644 (file)
@@ -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).
index 7763e9632103ff4e7b9600c91062f4a75b30c9b2..3d1501070b00fe733b2e3c4096735b62d5f1d338 100644 (file)
@@ -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(());