From 7f3f4db131008dce919ec399d989b867c46a5a86 Mon Sep 17 00:00:00 2001
From: maged <magedrifaat@gmail.com>
Date: Sat, 22 May 2021 21:31:19 +0200
Subject: [PATCH] Fixed case-insensitive name check in checkSketchFile

---
 arduino-core/src/processing/app/Sketch.java      |  4 ++--
 .../src/processing/app/helpers/FileUtils.java    | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/arduino-core/src/processing/app/Sketch.java b/arduino-core/src/processing/app/Sketch.java
index 6c417403ec9..4563297c82c 100644
--- a/arduino-core/src/processing/app/Sketch.java
+++ b/arduino-core/src/processing/app/Sketch.java
@@ -68,10 +68,10 @@ static public File checkSketchFile(File file) {
     if (pdeName.equals(fileName) || inoName.equals(fileName))
       return file;
 
-    if (altPdeFile.exists())
+    if (FileUtils.fileExistsCaseSensitive(altPdeFile, pdeName))
       return altPdeFile;
 
-    if (altInoFile.exists())
+    if (FileUtils.fileExistsCaseSensitive(altInoFile, inoName))
       return altInoFile;
 
     return null;
diff --git a/arduino-core/src/processing/app/helpers/FileUtils.java b/arduino-core/src/processing/app/helpers/FileUtils.java
index f2a1603b698..b8843a1284d 100644
--- a/arduino-core/src/processing/app/helpers/FileUtils.java
+++ b/arduino-core/src/processing/app/helpers/FileUtils.java
@@ -309,4 +309,20 @@ public static List<File> listFiles(File folder, boolean recursive,
     return result;
   }
 
+  /**
+   * Checks for the existence of a file with the given name but accounts
+   * for case-sensitivity on case-insensitive file systems.
+   * https://stackoverflow.com/a/34730781
+   *
+   * @param file The file being checked
+   * @param name The actual name the file is expected to have
+   */
+  public static boolean fileExistsCaseSensitive(File file, String name) {
+    try {
+        return file.exists() && file.getCanonicalFile().getName().equals(name);
+    } catch (IOException e) {
+        return false;
+    }
+  }
+
 }