From c60e1046a7684f1981812fa35bbb48b054234b36 Mon Sep 17 00:00:00 2001
From: Vitor Santos Costa <vsc@dcc.fc.up.pt>
Date: Fri, 7 Jun 2013 18:00:27 +0100
Subject: [PATCH] use int64 fr file_time

---
 os/pl-files.c  | 78 +++++++++++++++++++++++++++++++++++++++++++++++++-
 pl/consult.yap |  6 ++--
 2 files changed, 80 insertions(+), 4 deletions(-)
 mode change 100644 => 100755 os/pl-files.c
 mode change 100644 => 100755 pl/consult.yap

diff --git a/os/pl-files.c b/os/pl-files.c
old mode 100644
new mode 100755
index 4642b8e68..bcd57b482
--- a/os/pl-files.c
+++ b/os/pl-files.c
@@ -105,12 +105,13 @@ LastModifiedFile(const char *name, double *tp)
     if ( rc )
     { double t;
 
+      fprintf(stderr, "wt.dwHighDateTime=%ld wt.dwLowDateTime=%ld\n",wt.dwHighDateTime,  wt.dwLowDateTime);
       t  = (double)wt.dwHighDateTime * (4294967296.0 * ntick nano);
       t += (double)wt.dwLowDateTime  * (ntick nano);
       t -= SEC_TO_UNIX_EPOCH;
 
       *tp = t;
-
+      fprintf(stderr, " t=%f\n", t);
       return TRUE;
     }
   }
@@ -130,6 +131,63 @@ LastModifiedFile(const char *name, double *tp)
 #endif
 }
 
+static int
+LastModifiedFile64(const char *name, int64_t *tp)
+{
+#ifdef __WINDOWS__
+  HANDLE hFile;
+  wchar_t wfile[MAXPATHLEN];
+
+#define nano * 0.000000001
+#define ntick 100.0
+#define SEC_TO_UNIX_EPOCH 11644473600.0
+
+  if ( !_xos_os_filenameW(name, wfile, MAXPATHLEN) )
+    return FALSE;
+
+  if ( (hFile=CreateFileW(wfile,
+			  0,
+			  FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,
+			  NULL,
+			  OPEN_EXISTING,
+			  FILE_FLAG_BACKUP_SEMANTICS,
+			  NULL)) != INVALID_HANDLE_VALUE )
+  { FILETIME wt;
+    int rc;
+
+    rc = GetFileTime(hFile, NULL, NULL, (FILETIME *)&wt);
+    CloseHandle(hFile);
+
+    if ( rc )
+    {
+      LARGE_INTEGER date, adjust;
+      date.HighPart = wt.dwHighDateTime;
+      date.LowPart = wt.dwLowDateTime;
+
+      adjust.QuadPart = 11644473600000 * 10000;
+      date.QuadPart -= adjust.QuadPart;
+      date.QuadPart /= 10000000;
+
+      *tp = date.QuadPart;
+      return TRUE;
+    }
+  }
+
+  set_posix_error(GetLastError());
+
+  return FALSE;
+#else
+  char tmp[MAXPATHLEN];
+  statstruct buf;
+
+  if ( statfunc(OsPath(name, tmp), &buf) < 0 )
+    return FALSE;
+
+  *tp = (int64_t)buf.st_mtime;
+  return TRUE;
+#endif
+}
+
 
 /** static int64_t SizeFile(const char *path)
 
@@ -602,6 +660,23 @@ PRED_IMPL("time_file", 2, time_file, 0)
   return FALSE;
 }
 
+static
+PRED_IMPL("time_file64", 2, time_file64, 0)
+{ char *fn;
+
+  if ( PL_get_file_name(A1, &fn, 0) )
+  { int64_t time;
+
+    if ( LastModifiedFile64(fn, &time) )
+      return PL_unify_int64(A2, time);
+
+    return PL_error(NULL, 0, NULL, ERR_FILE_OPERATION,
+		    ATOM_time, ATOM_file, A1);
+  }
+
+  return FALSE;
+}
+
 
 static
 PRED_IMPL("size_file", 2, size_file, 0)
@@ -1119,6 +1194,7 @@ BeginPredDefs(files)
   PRED_DEF("working_directory", 2, working_directory, 0)
   PRED_DEF("access_file", 2, access_file, 0)
   PRED_DEF("time_file", 2, time_file, 0)
+  PRED_DEF("time_file64", 2, time_file64, 0)
   PRED_DEF("size_file", 2, size_file, 0)
   PRED_DEF("read_link", 3, read_link, 0)
   PRED_DEF("exists_file", 1, exists_file, 0)
diff --git a/pl/consult.yap b/pl/consult.yap
old mode 100644
new mode 100755
index 8bf3b355f..71e3bb919
--- a/pl/consult.yap
+++ b/pl/consult.yap
@@ -561,8 +561,8 @@ prolog_load_context(term_position, '$stream_position'(0,Line,0,0,0)) :-
 	'$file_is_unchanged'(F, R, Age).
 
 '$file_is_unchanged'(F, R, Age) :-
-        time_file(F,CurrentAge),
-         ((CurrentAge = Age ; Age = -1)  -> true; erase(R), fail).
+        time_file64(F,CurrentAge),
+        ( (Age == CurrentAge ; Age = -1)  -> true; erase(R), fail).
 
 
 
@@ -645,7 +645,7 @@ remove_from_path(New) :- '$check_path'(New,Path),
 	Stream \= user_input,
 	'$file_name'(Stream,F),
 	( recorded('$lf_loaded','$lf_loaded'(F,M,_,_),R), erase(R), fail ; true ),
-	time_file(F,Age),
+	time_file64(F,Age),
 	recorda('$lf_loaded','$lf_loaded'(F,M,Age,Reconsult),_),
 	fail.
 '$record_loaded'(_, _, _).