PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 377 lines | 350 code | 27 blank | 0 comment | 59 complexity | 0ead2fad621637fdee0e7d1e7113254e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.Build.Shared
  2. {
  3. using System;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. internal static class FileUtilities
  13. {
  14. internal static string cacheDirectory = null;
  15. private static string executablePath;
  16. internal const string FileTimeFormat = "yyyy'-'MM'-'dd HH':'mm':'ss'.'fffffff";
  17. internal const int MaxPath = 260;
  18. internal static string AttemptToShortenPath(string path)
  19. {
  20. if ((path.Length >= Microsoft.Build.Shared.NativeMethodsShared.MAX_PATH) || (!IsRootedNoThrow(path) && (((Environment.CurrentDirectory.Length + path.Length) + 1) >= Microsoft.Build.Shared.NativeMethodsShared.MAX_PATH)))
  21. {
  22. path = GetFullPathNoThrow(path);
  23. }
  24. return path;
  25. }
  26. internal static void ClearCacheDirectory()
  27. {
  28. if (Directory.Exists(GetCacheDirectory()))
  29. {
  30. Directory.Delete(GetCacheDirectory(), true);
  31. }
  32. }
  33. private static Uri CreateUriFromPath(string path)
  34. {
  35. Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentLength(path, "path");
  36. Uri result = null;
  37. if (!Uri.TryCreate(path, UriKind.Absolute, out result))
  38. {
  39. result = new Uri(path, UriKind.Relative);
  40. }
  41. return result;
  42. }
  43. internal static void DeleteNoThrow(string path)
  44. {
  45. try
  46. {
  47. File.Delete(path);
  48. }
  49. catch (Exception exception)
  50. {
  51. if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
  52. {
  53. throw;
  54. }
  55. }
  56. }
  57. internal static bool DirectoryExistsNoThrow(string fullPath)
  58. {
  59. fullPath = AttemptToShortenPath(fullPath);
  60. Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA lpFileInformation = new Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
  61. return (Microsoft.Build.Shared.NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref lpFileInformation) && ((lpFileInformation.fileAttributes & 0x10) != 0));
  62. }
  63. internal static bool EndsWithSlash(string fileSpec)
  64. {
  65. if (fileSpec.Length <= 0)
  66. {
  67. return false;
  68. }
  69. return IsSlash(fileSpec[fileSpec.Length - 1]);
  70. }
  71. internal static string EnsureNoLeadingSlash(string path)
  72. {
  73. if ((path.Length > 0) && IsSlash(path[0]))
  74. {
  75. path = path.Substring(1);
  76. }
  77. return path;
  78. }
  79. internal static string EnsureNoTrailingSlash(string path)
  80. {
  81. if (EndsWithSlash(path))
  82. {
  83. path = path.Substring(0, path.Length - 1);
  84. }
  85. return path;
  86. }
  87. internal static string EnsureTrailingSlash(string fileSpec)
  88. {
  89. if ((fileSpec.Length > 0) && !EndsWithSlash(fileSpec))
  90. {
  91. fileSpec = fileSpec + Path.DirectorySeparatorChar;
  92. }
  93. return fileSpec;
  94. }
  95. internal static bool FileExistsNoThrow(string fullPath)
  96. {
  97. fullPath = AttemptToShortenPath(fullPath);
  98. Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA lpFileInformation = new Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
  99. return (Microsoft.Build.Shared.NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref lpFileInformation) && ((lpFileInformation.fileAttributes & 0x10) == 0));
  100. }
  101. internal static bool FileOrDirectoryExistsNoThrow(string fullPath)
  102. {
  103. fullPath = AttemptToShortenPath(fullPath);
  104. Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA lpFileInformation = new Microsoft.Build.Shared.NativeMethodsShared.WIN32_FILE_ATTRIBUTE_DATA();
  105. return Microsoft.Build.Shared.NativeMethodsShared.GetFileAttributesEx(fullPath, 0, ref lpFileInformation);
  106. }
  107. internal static string GetCacheDirectory()
  108. {
  109. if (cacheDirectory == null)
  110. {
  111. cacheDirectory = Path.Combine(Path.GetTempPath(), string.Format(Thread.CurrentThread.CurrentUICulture, "MSBuild{0}", new object[] { Process.GetCurrentProcess().Id }));
  112. }
  113. return cacheDirectory;
  114. }
  115. internal static string GetDirectory(string fileSpec)
  116. {
  117. string directoryName = Path.GetDirectoryName(fileSpec);
  118. if (directoryName == null)
  119. {
  120. return fileSpec;
  121. }
  122. if ((directoryName.Length > 0) && !EndsWithSlash(directoryName))
  123. {
  124. directoryName = directoryName + Path.DirectorySeparatorChar;
  125. }
  126. return directoryName;
  127. }
  128. internal static string GetDirectoryNameOfFullPath(string fullPath)
  129. {
  130. if (fullPath == null)
  131. {
  132. return null;
  133. }
  134. int length = fullPath.Length;
  135. while (((length > 0) && (fullPath[--length] != Path.DirectorySeparatorChar)) && (fullPath[length] != Path.AltDirectorySeparatorChar))
  136. {
  137. }
  138. return fullPath.Substring(0, length);
  139. }
  140. internal static FileInfo GetFileInfoNoThrow(string filePath)
  141. {
  142. FileInfo info;
  143. filePath = AttemptToShortenPath(filePath);
  144. try
  145. {
  146. info = new FileInfo(filePath);
  147. }
  148. catch (Exception exception)
  149. {
  150. if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
  151. {
  152. throw;
  153. }
  154. return null;
  155. }
  156. if (info.Exists)
  157. {
  158. return info;
  159. }
  160. return null;
  161. }
  162. internal static string GetFullPath(string fileSpec, string currentDirectory)
  163. {
  164. fileSpec = Microsoft.Build.Shared.EscapingUtilities.UnescapeAll(fileSpec);
  165. string str = Microsoft.Build.Shared.EscapingUtilities.Escape(NormalizePath(Path.Combine(currentDirectory, fileSpec)));
  166. if (EndsWithSlash(str))
  167. {
  168. return str;
  169. }
  170. Match match = Microsoft.Build.Shared.FileUtilitiesRegex.DrivePattern.Match(fileSpec);
  171. Match match2 = Microsoft.Build.Shared.FileUtilitiesRegex.UNCPattern.Match(str);
  172. if ((!match.Success || (match.Length != fileSpec.Length)) && (!match2.Success || (match2.Length != str.Length)))
  173. {
  174. return str;
  175. }
  176. return (str + Path.DirectorySeparatorChar);
  177. }
  178. internal static string GetFullPathNoThrow(string path)
  179. {
  180. try
  181. {
  182. path = NormalizePath(path);
  183. }
  184. catch (Exception exception)
  185. {
  186. if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
  187. {
  188. throw;
  189. }
  190. }
  191. return path;
  192. }
  193. internal static bool HasExtension(string fileName, string[] allowedExtensions)
  194. {
  195. string extension = Path.GetExtension(fileName);
  196. foreach (string str2 in allowedExtensions)
  197. {
  198. if (string.Compare(extension, str2, true, CultureInfo.CurrentCulture) == 0)
  199. {
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205. internal static bool IsMetaprojectFilename(string filename)
  206. {
  207. return string.Equals(Path.GetExtension(filename), ".metaproj", StringComparison.OrdinalIgnoreCase);
  208. }
  209. internal static bool IsRootedNoThrow(string path)
  210. {
  211. try
  212. {
  213. return Path.IsPathRooted(path);
  214. }
  215. catch (Exception exception)
  216. {
  217. if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
  218. {
  219. throw;
  220. }
  221. return false;
  222. }
  223. }
  224. internal static bool IsSlash(char c)
  225. {
  226. if (c != Path.DirectorySeparatorChar)
  227. {
  228. return (c == Path.AltDirectorySeparatorChar);
  229. }
  230. return true;
  231. }
  232. internal static bool IsSolutionFilename(string filename)
  233. {
  234. return string.Equals(Path.GetExtension(filename), ".sln", StringComparison.OrdinalIgnoreCase);
  235. }
  236. internal static bool IsVCProjFilename(string filename)
  237. {
  238. return string.Equals(Path.GetExtension(filename), ".vcproj", StringComparison.OrdinalIgnoreCase);
  239. }
  240. internal static string MakeRelative(string basePath, string path)
  241. {
  242. Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(basePath, "basePath");
  243. Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentLength(path, "path");
  244. if (basePath.Length == 0)
  245. {
  246. return path;
  247. }
  248. Uri baseUri = new Uri(EnsureTrailingSlash(basePath), UriKind.Absolute);
  249. Uri relativeUri = CreateUriFromPath(path);
  250. if (!relativeUri.IsAbsoluteUri)
  251. {
  252. relativeUri = new Uri(baseUri, relativeUri);
  253. }
  254. Uri uri3 = baseUri.MakeRelativeUri(relativeUri);
  255. return Uri.UnescapeDataString(uri3.IsAbsoluteUri ? uri3.LocalPath : uri3.ToString()).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
  256. }
  257. internal static string NormalizePath(string path)
  258. {
  259. Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentLength(path, "path");
  260. int errorCode = 0;
  261. int num2 = Microsoft.Build.Shared.NativeMethodsShared.MAX_PATH;
  262. StringBuilder buffer = new StringBuilder(num2 + 1);
  263. int num3 = Microsoft.Build.Shared.NativeMethodsShared.GetFullPathName(path, buffer.Capacity, buffer, IntPtr.Zero);
  264. errorCode = Marshal.GetLastWin32Error();
  265. if (num3 > num2)
  266. {
  267. num2 = num3;
  268. buffer = new StringBuilder(num2 + 1);
  269. num3 = Microsoft.Build.Shared.NativeMethodsShared.GetFullPathName(path, buffer.Capacity, buffer, IntPtr.Zero);
  270. errorCode = Marshal.GetLastWin32Error();
  271. Microsoft.Build.Shared.ErrorUtilities.VerifyThrow((num3 + 1) < buffer.Capacity, "Final buffer capacity should be sufficient for full path name and null terminator.");
  272. }
  273. if (num3 <= 0)
  274. {
  275. errorCode = -2147024896 | errorCode;
  276. Marshal.ThrowExceptionForHR(errorCode);
  277. return null;
  278. }
  279. string message = buffer.ToString();
  280. if (message.Length >= 260)
  281. {
  282. throw new PathTooLongException(message);
  283. }
  284. message = Path.Combine(message, string.Empty);
  285. if (message.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase))
  286. {
  287. int num4 = 2;
  288. while (num4 < message.Length)
  289. {
  290. char ch = message[num4];
  291. if (ch.Equals('\\'))
  292. {
  293. num4++;
  294. break;
  295. }
  296. num4++;
  297. }
  298. if ((num4 == message.Length) || (message.IndexOf(@"\\?\globalroot", StringComparison.OrdinalIgnoreCase) != -1))
  299. {
  300. message = Path.GetFullPath(message);
  301. }
  302. }
  303. if (string.Equals(message, path, StringComparison.Ordinal))
  304. {
  305. message = path;
  306. }
  307. return message;
  308. }
  309. internal static string TrimAndStripAnyQuotes(string path)
  310. {
  311. path = path.Trim();
  312. path = path.Trim(new char[] { '"' });
  313. return path;
  314. }
  315. internal static string CurrentExecutableConfigurationFilePath
  316. {
  317. get
  318. {
  319. return (CurrentExecutablePath + ".config");
  320. }
  321. }
  322. internal static string CurrentExecutableDirectory
  323. {
  324. get
  325. {
  326. return Path.GetDirectoryName(CurrentExecutablePath);
  327. }
  328. }
  329. internal static string CurrentExecutablePath
  330. {
  331. get
  332. {
  333. if (executablePath == null)
  334. {
  335. StringBuilder buffer = new StringBuilder(Microsoft.Build.Shared.NativeMethodsShared.MAX_PATH);
  336. if (Microsoft.Build.Shared.NativeMethodsShared.GetModuleFileName(Microsoft.Build.Shared.NativeMethodsShared.NullHandleRef, buffer, buffer.Capacity) == 0)
  337. {
  338. throw new Win32Exception();
  339. }
  340. executablePath = buffer.ToString();
  341. }
  342. return executablePath;
  343. }
  344. }
  345. }
  346. }