From 836b8b3beb35dd7afa637c5a50f3424216d87a49 Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Fri, 26 Sep 2008 08:24:20 -0400
Subject: [PATCH] BUG: Fix SharedForward in-tree detection

To detect when the launcher is running from the build tree we now test
if the directory containing it is the same as the build-tree directory
using an inode test instead of string comparison.  This makes it more
robust on case-insensitive filesystems and other quirky situations.
---
 SharedForward.h.in | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/SharedForward.h.in b/SharedForward.h.in
index 7a15de8..9b72453 100644
--- a/SharedForward.h.in
+++ b/SharedForward.h.in
@@ -159,6 +159,7 @@
 # include <process.h>
 #else
 # include <unistd.h>
+# include <sys/stat.h>
 #endif
 
 /*--------------------------------------------------------------------------*/
@@ -283,6 +284,37 @@ static int kwsys_shared_forward_realpath(const char* in_path, char* out_path)
 #endif
 }
 
+/*--------------------------------------------------------------------------*/
+static int kwsys_shared_forward_samepath(const char* file1, const char* file2)
+{
+#if defined(_WIN32)
+  int result = 0;
+  HANDLE h1 = CreateFile(file1, GENERIC_READ, FILE_SHARE_READ, NULL,
+                         OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
+  HANDLE h2 = CreateFile(file2, GENERIC_READ, FILE_SHARE_READ, NULL,
+                         OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
+  if(h1 != INVALID_HANDLE_VALUE && h2 != INVALID_HANDLE_VALUE)
+    {
+    BY_HANDLE_FILE_INFORMATION fi1;
+    BY_HANDLE_FILE_INFORMATION fi2;
+    GetFileInformationByHandle(h1, &fi1);
+    GetFileInformationByHandle(h2, &fi2);
+    result = (fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber &&
+              fi1.nFileIndexHigh == fi2.nFileIndexHigh &&
+              fi1.nFileIndexLow == fi2.nFileIndexLow);
+    }
+   CloseHandle(h1);
+   CloseHandle(h2);
+   return result;
+#else
+  struct stat fs1, fs2;
+  return (stat(file1, &fs1) == 0 && stat(file2, &fs2) == 0 &&
+          memcmp(&fs2.st_dev, &fs1.st_dev, sizeof(fs1.st_dev)) == 0 &&
+          memcmp(&fs2.st_ino, &fs1.st_ino, sizeof(fs1.st_ino)) == 0 &&
+          fs2.st_size == fs1.st_size);
+#endif
+}
+
 /*--------------------------------------------------------------------------*/
 /* Function to report a system error message.  */
 static void kwsys_shared_forward_strerror(char* message)
@@ -535,7 +567,7 @@ static int kwsys_shared_forward_get_settings(const char* self_path,
 
   /* Check whether we are running in the build tree or an install tree.  */
   if(kwsys_shared_forward_realpath(build_path, build_path_real) &&
-     strcmp(self_path_real, build_path_real) == 0)
+     kwsys_shared_forward_samepath(self_path_real, build_path_real))
     {
     /* Running in build tree.  Use the build path and exe.  */
     search_path = search_path_build;
-- 
GitLab