From 6242036e9f809cc7e6d21783db4ce22f39c6d944 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Fri, 21 May 2021 22:57:25 +0200
Subject: [PATCH 10/N] pacman: make file list comparisons between packages
 case insensitive

In case a package gets upgrade and a filename has changed case this allows
the upgrade to go through without any conflicts.

Similarly in case two different packages contain the same file with
a different case this makes it correctly detect a conflict.

This only handles ASCII parts of paths, so Unicode filenames still can
lead to invalid conflict detection.
---
 lib/libalpm/filelist.c | 47 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 42 insertions(+), 5 deletions(-)

diff --git a/lib/libalpm/filelist.c b/lib/libalpm/filelist.c
index 07239c3..9c044b5 100644
--- a/lib/libalpm/filelist.c
+++ b/lib/libalpm/filelist.c
@@ -25,6 +25,43 @@
 #include "filelist.h"
 #include "util.h"
 
+#ifdef __MSYS__
+
+#define TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' + 'a' : (c))
+
+static int _cmppath(const char *s1, const char *s2)
+{
+	int c1, c2;
+
+	while(*s1 && *s2)
+	{
+		c1 = TOLOWER(*s1);
+		c2 = TOLOWER(*s2);
+		if(c1 != c2)
+			return c1 - c2;
+		s1++;
+		s2++;
+	}
+
+	return *s1 - *s2;
+}
+
+static int _cmpchar(char c1, char c2)
+{
+	return TOLOWER(c1) - TOLOWER(c2);
+}
+#else
+static int _cmppath(const char *s1, const char *s2)
+{
+	return strcmp(s1, s2);
+}
+
+static int _cmpchar(char c1, char c2)
+{
+	return c1 - c2;
+}
+#endif
+
 /* Returns the difference of the provided two lists of files.
  * Pre-condition: both lists are sorted!
  * When done, free the list but NOT the contained data.
@@ -39,7 +76,7 @@ alpm_list_t *_alpm_filelist_difference(alpm_filelist_t *filesA,
 		char *strA = filesA->files[ctrA].name;
 		char *strB = filesB->files[ctrB].name;
 
-		int cmp = strcmp(strA, strB);
+		int cmp = _cmppath(strA, strB);
 		if(cmp < 0) {
 			/* item only in filesA, qualifies as a difference */
 			ret = alpm_list_add(ret, strA);
@@ -63,7 +100,7 @@ alpm_list_t *_alpm_filelist_difference(alpm_filelist_t *filesA,
 
 static int _alpm_filelist_pathcmp(const char *p1, const char *p2)
 {
-	while(*p1 && *p1 == *p2) {
+	while(*p1 && _cmpchar(*p1, *p2) == 0) {
 		p1++;
 		p2++;
 	}
@@ -75,7 +112,7 @@ static int _alpm_filelist_pathcmp(const char *p1, const char *p2)
 		p1++;
 	}
 
-	return *p1 - *p2;
+	return _cmpchar(*p1, *p2);
 }
 
 /* Returns the intersection of the provided two lists of files.
@@ -115,7 +152,7 @@ static int _alpm_files_cmp(const void *f1, const void *f2)
 {
 	const alpm_file_t *file1 = f1;
 	const alpm_file_t *file2 = f2;
-	return strcmp(file1->name, file2->name);
+	return _cmppath(file1->name, file2->name);
 }
 
 alpm_file_t SYMEXPORT *alpm_filelist_contains(alpm_filelist_t *filelist,
@@ -137,7 +174,7 @@ void _alpm_filelist_sort(alpm_filelist_t *filelist)
 {
 	size_t i;
 	for(i = 1; i < filelist->count; i++) {
-		if(strcmp(filelist->files[i - 1].name, filelist->files[i].name) > 0) {
+		if(_cmppath(filelist->files[i - 1].name, filelist->files[i].name) > 0) {
 			/* filelist is not pre-sorted */
 			qsort(filelist->files, filelist->count,
 					sizeof(alpm_file_t), _alpm_files_cmp);
-- 
2.33.0

