Skip to content

Commit 4c5c710

Browse files
committed
Improve path distinction.
This commit prevents conflicts caused by naive path comparison with path canonicalization and case-insensitive comparison for Windows.
1 parent ae5bc9a commit 4c5c710

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

src/mpu/PathEqualityComparer.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#region -- License Terms --
2+
//
3+
// MessagePack for CLI
4+
//
5+
// Copyright (C) 2014 FUJIWARA, Yusuke
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
#endregion -- License Terms --
20+
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Globalization;
24+
using System.IO;
25+
26+
namespace mpu
27+
{
28+
internal abstract class PathEqualityComparer : IEqualityComparer<string>
29+
{
30+
private static readonly PathEqualityComparer _instance = DetermineInstance();
31+
32+
private static PathEqualityComparer DetermineInstance()
33+
{
34+
switch ( Environment.OSVersion.Platform )
35+
{
36+
case PlatformID.Win32NT:
37+
case PlatformID.Win32S:
38+
case PlatformID.Win32Windows:
39+
case PlatformID.WinCE:
40+
case PlatformID.Xbox:
41+
{
42+
return OrdinalPathEqualityComparer.CaseInsensitive;
43+
}
44+
case PlatformID.MacOSX: // Mac's NFD problem may not affect in assembly probling...
45+
case PlatformID.Unix:
46+
{
47+
return OrdinalPathEqualityComparer.CaseSensitive;
48+
}
49+
default:
50+
{
51+
throw new PlatformNotSupportedException(String.Format( CultureInfo.CurrentCulture, "Unknown platform '{0:G}({0:D})'.", Environment.OSVersion.Platform ));
52+
}
53+
}
54+
}
55+
56+
public static PathEqualityComparer Instance
57+
{
58+
get { return _instance; }
59+
}
60+
61+
62+
public bool Equals( string x, string y )
63+
{
64+
return EqualsCore( SafeGetFullPath( x ), SafeGetFullPath( y ) );
65+
}
66+
67+
protected abstract bool EqualsCore( string x, string y );
68+
69+
public int GetHashCode( string obj )
70+
{
71+
return SafeGetFullPath( obj ).GetHashCode();
72+
}
73+
74+
private static string SafeGetFullPath( string mayBePath )
75+
{
76+
if ( String.IsNullOrEmpty( mayBePath ) )
77+
{
78+
return String.Empty;
79+
}
80+
81+
return Path.GetFullPath( mayBePath.Trim() ).Replace( Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar );
82+
}
83+
84+
private sealed class OrdinalPathEqualityComparer : PathEqualityComparer
85+
{
86+
public static readonly OrdinalPathEqualityComparer CaseSensitive =
87+
new OrdinalPathEqualityComparer( StringComparer.Ordinal );
88+
89+
public static readonly OrdinalPathEqualityComparer CaseInsensitive =
90+
new OrdinalPathEqualityComparer( StringComparer.OrdinalIgnoreCase );
91+
92+
private readonly IEqualityComparer<string> _pathComparer;
93+
94+
private OrdinalPathEqualityComparer( IEqualityComparer<string> pathComparer )
95+
{
96+
this._pathComparer = pathComparer;
97+
}
98+
99+
protected override bool EqualsCore( string x, string y )
100+
{
101+
return this._pathComparer.Equals( x, y );
102+
}
103+
}
104+
}
105+
}

src/mpu/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private static int Execute( IEnumerable<string> args )
172172

173173
GenerateSerializers(
174174
sourceFilePathes,
175-
referenceAssemblies.Distinct().ToArray(),
175+
referenceAssemblies.Distinct( PathEqualityComparer.Instance ).ToArray(),
176176
sourceFileIsAssembly,
177177
includingPattern,
178178
excludingPattern,

src/mpu/mpu.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="AssetFileImporter.cs" />
5050
<Compile Include="ColorizedTextWriter.cs" />
5151
<Compile Include="Mono.Options\Options.cs" />
52+
<Compile Include="PathEqualityComparer.cs" />
5253
<Compile Include="Program.cs" />
5354
<Compile Include="Properties\AssemblyInfo.cs" />
5455
<Compile Include="SerializerCodeGenerator.cs" />

0 commit comments

Comments
 (0)