chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.IO;
using OpenTap;
 
namespace Tap.Shared
{
    internal class PathUtils
    {
        public class PathComparer : IEqualityComparer<string>
        {
            public bool Equals(string x, string y)
            {
                return NormalizePath(x) == NormalizePath(y);
            }
 
            public int GetHashCode(string obj)
            {
                return NormalizePath(obj).GetHashCode();
            }
        }
 
        public static string NormalizePath(string path)
        {
            var newPath = Path.GetFullPath(path).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
 
            switch (Environment.OSVersion.Platform)
            {
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                    return newPath.ToUpperInvariant();
            }
 
            return newPath;
        }
 
        public static bool AreEqual(string path1, string path2)
        {
            return NormalizePath(path1) == NormalizePath(path2);
        }
 
        /// <summary>
        /// Similar to Directory.EnumerateFiles but will ignore any UnauthorizedAccessException or PathTooLongException that occur while walking the directory tree.
        /// </summary>
        public static IEnumerable<string> IterateDirectories(string rootPath, string patternMatch, SearchOption searchOption)
        {
            if (searchOption == SearchOption.AllDirectories)
            {
                IEnumerable<string> subDirs = Array.Empty<string>();
                try
                {
                    subDirs = Directory.EnumerateDirectories(rootPath);
                }
                catch (UnauthorizedAccessException) { }
                catch (PathTooLongException) { }
 
                foreach (var dir in subDirs)
                {
                    foreach (var f in IterateDirectories(dir, patternMatch, searchOption))
                        yield return f;
                }
            }
 
            IEnumerable<string> files = Array.Empty<string>();
            try
            {
                files = Directory.EnumerateFiles(rootPath, patternMatch);
            }
            catch (UnauthorizedAccessException) { }
 
            foreach (var file in files)
                yield return file;
        }
 
        static bool compareFileStreams(FileStream f1, FileStream f2)
        {
            if (f1.Length != f2.Length) return false;
            const int bufferSize = 4096;
            const int u64len1 = bufferSize / 8;
            byte[] buffer1 = new byte[bufferSize];
            byte[] buffer2 = new byte[bufferSize];
            while (true)
            {
                int count = f1.Read(buffer1, 0, bufferSize);
                if (count == 0) return true;
                f2.Read(buffer2, 0, bufferSize);
 
                int u64len = u64len1;
                if (count < bufferSize)
                    u64len = (count / 8 + 1);
 
                for (int i = 0; i < u64len; i++)
                {
                    if (BitConverter.ToInt64(buffer1, i * 8) != BitConverter.ToInt64(buffer2, i * 8))
                        return false;
                }
            }
        }
 
        public static bool CompareFiles(string file1, string file2)
        {
            if (PathUtils.AreEqual(file1, file2))
                return true;
            try
            {
                using (var f1 = File.Open(file1, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (var f2 = File.Open(file2, FileMode.Open, FileAccess.Read, FileShare.Read))
                    return compareFileStreams(f1, f2);
            }
            catch
            {
                return false;
            }
        }
 
 
        static string openTapLocation = null;
        /// <summary> Get the location of OpenTAP (OpenTAP.dll) </summary>
        public static string OpenTapDir =>
            openTapLocation ?? (openTapLocation = Path.GetDirectoryName(typeof(TestPlan).Assembly.Location));
 
        public static string GetTempFileName(string extension)
        {
            if (!extension.StartsWith(".")) extension = "." + extension;
            //use instead of Path.GetTempFileName()
            return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + extension);
        }
 
        /// <summary>
        /// Checks if the relative path has any ".OpenTapIgnore" file in parent directory chain. Throws argument error eventually if input is an absolute path and no ".OpenTapIgnore" file is present in parent directory chain.
        /// </summary>
        /// <param name="location">Relative path of file</param>
        /// <returns>Whether an .OpenTapIgnore file exists in folders.</returns>
        internal static bool DecendsFromOpenTapIgnore(string location)
        {
            string dir = Path.GetDirectoryName(location);
            if (dir == null) return false;
            if (File.Exists(Path.Combine(dir, ".OpenTapIgnore")))
                return true;
            if (string.IsNullOrWhiteSpace(dir))
                return false;
            return DecendsFromOpenTapIgnore(dir);
        }
    }
}