| 1 | using System.Text; |
| 2 | |
| 3 | namespace Zipelo.Core; |
| 4 | |
| 5 | public enum FileNamePolicy { Windows, Linux } |
| 6 | |
| 7 | public static class OutputPathPolicy |
| 8 | { |
| 9 | public static FileNamePolicy CurrentPolicy => OperatingSystem.IsWindows() ? FileNamePolicy.Windows : |
| 10 | OperatingSystem.IsLinux() ? FileNamePolicy.Linux : throw new PlatformNotSupportedException(); |
| 11 | |
| 12 | public static StringComparison Comparison(FileNamePolicy policy) => policy switch |
| 13 | { |
| 14 | FileNamePolicy.Windows => StringComparison.OrdinalIgnoreCase, |
| 15 | FileNamePolicy.Linux => StringComparison.Ordinal, |
| 16 | _ => throw new ArgumentOutOfRangeException(nameof(policy)) |
| 17 | }; |
| 18 | |
| 19 | public static StringComparer Comparer(FileNamePolicy policy) => policy switch |
| 20 | { |
| 21 | FileNamePolicy.Windows => StringComparer.OrdinalIgnoreCase, |
| 22 | FileNamePolicy.Linux => StringComparer.Ordinal, |
| 23 | _ => throw new ArgumentOutOfRangeException(nameof(policy)) |
| 24 | }; |
| 25 | |
| 26 | public static IReadOnlyList<string> Validate(string path, FileNamePolicy policy) |
| 27 | { |
| 28 | if (policy == FileNamePolicy.Windows) { return Validate(path); } |
| 29 | if (policy != FileNamePolicy.Linux) { throw new ArgumentOutOfRangeException(nameof(policy)); } |
| 30 | if (string.IsNullOrEmpty(path) || path.Length > 4096 || path.Contains('\0')) |
| 31 | { |
| 32 | throw new ArchiveException(ArchiveError.UnsafePath); |
| 33 | } |
| 34 | var segments = path.Split('/'); |
| 35 | var encoding = new UTF8Encoding(false, true); |
| 36 | foreach (var segment in segments) |
| 37 | { |
| 38 | if (segment.Length == 0 || segment is "." or "..") |
| 39 | { |
| 40 | throw new ArchiveException(ArchiveError.UnsafePath); |
| 41 | } |
| 42 | try |
| 43 | { |
| 44 | if (encoding.GetByteCount(segment) > 255) { throw new ArchiveException(ArchiveError.UnsafePath); } |
| 45 | } |
| 46 | catch (EncoderFallbackException) { throw new ArchiveException(ArchiveError.UnsafePath); } |
| 47 | } |
| 48 | return Array.AsReadOnly(segments); |
| 49 | } |
| 50 | |
| 51 | // 기존 단일 인수 API는 Windows 사전 정책을 유지함. |
| 52 | // 문자열 검사는 사전 정책임. 파일시스템의 링크나 경쟁 상태를 검사한 결과가 아님. |
| 53 | public static IReadOnlyList<string> Validate(string path) |
| 54 | { |
| 55 | if (string.IsNullOrWhiteSpace(path) || path.Length > 4096) |
| 56 | { |
| 57 | throw new ArchiveException(ArchiveError.UnsafePath); |
| 58 | } |
| 59 | |
| 60 | var segments = path.Replace('\\', '/').Split('/'); |
| 61 | foreach (var segment in segments) |
| 62 | { |
| 63 | if (segment.Length is 0 or > 255 || segment is "." or ".." || |
| 64 | segment.EndsWith(' ') || segment.EndsWith('.') || |
| 65 | !segment.IsNormalized(NormalizationForm.FormC) || |
| 66 | segment.Any(c => char.IsControl(c) || "<>:\"|?*".Contains(c))) |
| 67 | { |
| 68 | throw new ArchiveException(ArchiveError.UnsafePath); |
| 69 | } |
| 70 | |
| 71 | var stem = segment.Split('.')[0].TrimEnd(' ').ToUpperInvariant(); |
| 72 | if (stem is "CON" or "PRN" or "AUX" or "NUL" or "CONIN$" or "CONOUT$" || |
| 73 | (stem.Length == 4 && (stem.StartsWith("COM") || stem.StartsWith("LPT")) && |
| 74 | "123456789¹²³".Contains(stem[3]))) |
| 75 | { |
| 76 | throw new ArchiveException(ArchiveError.UnsafePath); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return Array.AsReadOnly(segments); |
| 81 | } |
| 82 | } |