SolutionFramework /MSBuild/Microsoft/Build/Shared/FileUtilities.cs

Language C# Lines 378
MD5 Hash 0ead2fad621637fdee0e7d1e7113254e Estimated Cost $8,865 (why?)
Repository https://SolutionFramework.svn.codeplex.com/svn View Raw File
  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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
namespace Microsoft.Build.Shared
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading;

    internal static class FileUtilities
    {
        internal static string cacheDirectory = null;
        private static string executablePath;
        internal const string FileTimeFormat = "yyyy'-'MM'-'dd HH':'mm':'ss'.'fffffff";
        internal const int MaxPath = 260;

        internal static string AttemptToShortenPath(string path)
        {
            if ((path.Length >= Microsoft.Build.Shared.NativeMethodsShared.MAX_PATH) || (!IsRootedNoThrow(path) && (((Environment.CurrentDirectory.Length + path.Length) + 1) >= Microsoft.Build.Shared.NativeMethodsShared.MAX_PATH)))
            {
                path = GetFullPathNoThrow(path);
            }
            return path;
        }

        internal static void ClearCacheDirectory()
        {
            if (Directory.Exists(GetCacheDirectory()))
            {
                Directory.Delete(GetCacheDirectory(), true);
            }
        }

        private static Uri CreateUriFromPath(string path)
        {
            Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentLength(path, "path");
            Uri result = null;
            if (!Uri.TryCreate(path, UriKind.Absolute, out result))
            {
                result = new Uri(path, UriKind.Relative);
            }
            return result;
        }

        internal static void DeleteNoThrow(string path)
        {
            try
            {
                File.Delete(path);
            }
            catch (Exception exception)
            {
                if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
                {
                    throw;
                }
            }
        }

        internal static bool DirectoryExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);
            Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA lpFileInformation = new Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
            return (Microsoft.Build.Shared.NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref lpFileInformation) && ((lpFileInformation.fileAttributes & 0x10) != 0));
        }

        internal static bool EndsWithSlash(string fileSpec)
        {
            if (fileSpec.Length <= 0)
            {
                return false;
            }
            return IsSlash(fileSpec[fileSpec.Length - 1]);
        }

        internal static string EnsureNoLeadingSlash(string path)
        {
            if ((path.Length > 0) && IsSlash(path[0]))
            {
                path = path.Substring(1);
            }
            return path;
        }

        internal static string EnsureNoTrailingSlash(string path)
        {
            if (EndsWithSlash(path))
            {
                path = path.Substring(0, path.Length - 1);
            }
            return path;
        }

        internal static string EnsureTrailingSlash(string fileSpec)
        {
            if ((fileSpec.Length > 0) && !EndsWithSlash(fileSpec))
            {
                fileSpec = fileSpec + Path.DirectorySeparatorChar;
            }
            return fileSpec;
        }

        internal static bool FileExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);
            Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA lpFileInformation = new Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
            return (Microsoft.Build.Shared.NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref lpFileInformation) && ((lpFileInformation.fileAttributes & 0x10) == 0));
        }

        internal static bool FileOrDirectoryExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);
            Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA lpFileInformation = new Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
            return Microsoft.Build.Shared.NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref lpFileInformation);
        }

        internal static string GetCacheDirectory()
        {
            if (cacheDirectory == null)
            {
                cacheDirectory = Path.Combine(Path.GetTempPath(), string.Format(Thread.CurrentThread.CurrentUICulture, "MSBuild{0}", new object[] { Process.GetCurrentProcess().Id }));
            }
            return cacheDirectory;
        }

        internal static string GetDirectory(string fileSpec)
        {
            string directoryName = Path.GetDirectoryName(fileSpec);
            if (directoryName == null)
            {
                return fileSpec;
            }
            if ((directoryName.Length > 0) && !EndsWithSlash(directoryName))
            {
                directoryName = directoryName + Path.DirectorySeparatorChar;
            }
            return directoryName;
        }

        internal static string GetDirectoryNameOfFullPath(string fullPath)
        {
            if (fullPath == null)
            {
                return null;
            }
            int length = fullPath.Length;
            while (((length > 0) && (fullPath[--length] != Path.DirectorySeparatorChar)) && (fullPath[length] != Path.AltDirectorySeparatorChar))
            {
            }
            return fullPath.Substring(0, length);
        }

        internal static FileInfo GetFileInfoNoThrow(string filePath)
        {
            FileInfo info;
            filePath = AttemptToShortenPath(filePath);
            try
            {
                info = new FileInfo(filePath);
            }
            catch (Exception exception)
            {
                if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
                {
                    throw;
                }
                return null;
            }
            if (info.Exists)
            {
                return info;
            }
            return null;
        }

        internal static string GetFullPath(string fileSpec, string currentDirectory)
        {
            fileSpec = Microsoft.Build.Shared.EscapingUtilities.UnescapeAll(fileSpec);
            string str = Microsoft.Build.Shared.EscapingUtilities.Escape(NormalizePath(Path.Combine(currentDirectory, fileSpec)));
            if (EndsWithSlash(str))
            {
                return str;
            }
            Match match = Microsoft.Build.Shared.FileUtilitiesRegex.DrivePattern.Match(fileSpec);
            Match match2 = Microsoft.Build.Shared.FileUtilitiesRegex.UNCPattern.Match(str);
            if ((!match.Success || (match.Length != fileSpec.Length)) && (!match2.Success || (match2.Length != str.Length)))
            {
                return str;
            }
            return (str + Path.DirectorySeparatorChar);
        }

        internal static string GetFullPathNoThrow(string path)
        {
            try
            {
                path = NormalizePath(path);
            }
            catch (Exception exception)
            {
                if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
                {
                    throw;
                }
            }
            return path;
        }

        internal static bool HasExtension(string fileName, string[] allowedExtensions)
        {
            string extension = Path.GetExtension(fileName);
            foreach (string str2 in allowedExtensions)
            {
                if (string.Compare(extension, str2, true, CultureInfo.CurrentCulture) == 0)
                {
                    return true;
                }
            }
            return false;
        }

        internal static bool IsMetaprojectFilename(string filename)
        {
            return string.Equals(Path.GetExtension(filename), ".metaproj", StringComparison.OrdinalIgnoreCase);
        }

        internal static bool IsRootedNoThrow(string path)
        {
            try
            {
                return Path.IsPathRooted(path);
            }
            catch (Exception exception)
            {
                if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
                {
                    throw;
                }
                return false;
            }
        }

        internal static bool IsSlash(char c)
        {
            if (c != Path.DirectorySeparatorChar)
            {
                return (c == Path.AltDirectorySeparatorChar);
            }
            return true;
        }

        internal static bool IsSolutionFilename(string filename)
        {
            return string.Equals(Path.GetExtension(filename), ".sln", StringComparison.OrdinalIgnoreCase);
        }

        internal static bool IsVCProjFilename(string filename)
        {
            return string.Equals(Path.GetExtension(filename), ".vcproj", StringComparison.OrdinalIgnoreCase);
        }

        internal static string MakeRelative(string basePath, string path)
        {
            Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(basePath, "basePath");
            Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentLength(path, "path");
            if (basePath.Length == 0)
            {
                return path;
            }
            Uri baseUri = new Uri(EnsureTrailingSlash(basePath), UriKind.Absolute);
            Uri relativeUri = CreateUriFromPath(path);
            if (!relativeUri.IsAbsoluteUri)
            {
                relativeUri = new Uri(baseUri, relativeUri);
            }
            Uri uri3 = baseUri.MakeRelativeUri(relativeUri);
            return Uri.UnescapeDataString(uri3.IsAbsoluteUri ? uri3.LocalPath : uri3.ToString()).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
        }

        internal static string NormalizePath(string path)
        {
            Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentLength(path, "path");
            int errorCode = 0;
            int num2 = Microsoft.Build.Shared.NativeMethodsShared.MAX_PATH;
            StringBuilder buffer = new StringBuilder(num2 + 1);
            int num3 = Microsoft.Build.Shared.NativeMethodsShared.GetFullPathName(path, buffer.Capacity, buffer, IntPtr.Zero);
            errorCode = Marshal.GetLastWin32Error();
            if (num3 > num2)
            {
                num2 = num3;
                buffer = new StringBuilder(num2 + 1);
                num3 = Microsoft.Build.Shared.NativeMethodsShared.GetFullPathName(path, buffer.Capacity, buffer, IntPtr.Zero);
                errorCode = Marshal.GetLastWin32Error();
                Microsoft.Build.Shared.ErrorUtilities.VerifyThrow((num3 + 1) < buffer.Capacity, "Final buffer capacity should be sufficient for full path name and null terminator.");
            }
            if (num3 <= 0)
            {
                errorCode = -2147024896 | errorCode;
                Marshal.ThrowExceptionForHR(errorCode);
                return null;
            }
            string message = buffer.ToString();
            if (message.Length >= 260)
            {
                throw new PathTooLongException(message);
            }
            message = Path.Combine(message, string.Empty);
            if (message.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase))
            {
                int num4 = 2;
                while (num4 < message.Length)
                {
                    char ch = message[num4];
                    if (ch.Equals('\\'))
                    {
                        num4++;
                        break;
                    }
                    num4++;
                }
                if ((num4 == message.Length) || (message.IndexOf(@"\\?\globalroot", StringComparison.OrdinalIgnoreCase) != -1))
                {
                    message = Path.GetFullPath(message);
                }
            }
            if (string.Equals(message, path, StringComparison.Ordinal))
            {
                message = path;
            }
            return message;
        }

        internal static string TrimAndStripAnyQuotes(string path)
        {
            path = path.Trim();
            path = path.Trim(new char[] { '"' });
            return path;
        }

        internal static string CurrentExecutableConfigurationFilePath
        {
            get
            {
                return (CurrentExecutablePath + ".config");
            }
        }

        internal static string CurrentExecutableDirectory
        {
            get
            {
                return Path.GetDirectoryName(CurrentExecutablePath);
            }
        }

        internal static string CurrentExecutablePath
        {
            get
            {
                if (executablePath == null)
                {
                    StringBuilder buffer = new StringBuilder(Microsoft.Build.Shared.NativeMethodsShared.MAX_PATH);
                    if (Microsoft.Build.Shared.NativeMethodsShared.GetModuleFileName(Microsoft.Build.Shared.NativeMethodsShared.NullHandleRef, buffer, buffer.Capacity) == 0)
                    {
                        throw new Win32Exception();
                    }
                    executablePath = buffer.ToString();
                }
                return executablePath;
            }
        }
    }
}
Back to Top