private static void ValidateFlags(PathFlags flags) { if (flags.HasFlag(PathFlags.UseDefaultsForGivenPath)) throw new ArgumentException("A path cannot have the UseDefaultsForGivenPath flag set."); if (flags.HasFlag(PathFlags.UseDefaultsFromUtility)) throw new ArgumentException("A path cannot have the UseDefaultsFromUtility flag set."); }
private static void ValidateFlags(PathFlags flags) { if (flags.HasFlag(PathFlags.UseDefaultsForGivenPath)) { throw new ArgumentException("A path cannot have the UseDefaultsForGivenPath flag set."); } if (flags.HasFlag(PathFlags.UseDefaultsFromUtility)) { throw new ArgumentException("A path cannot have the UseDefaultsFromUtility flag set."); } }
public bool GetFullPath(Source src, string dir, ref string path, PathFlags flags = 0) { if (string.IsNullOrEmpty(path)) { if (flags.HasFlag(PathFlags.AllowNonExistingPath)) { return(true); } Log.Error(src, null, "Expected non-empty path"); return(false); } if (!path.IsValidPath()) { Log.Error(src, null, "Invalid path " + path.Quote()); return(false); } var isAbsolute = Path.IsPathRooted(path); if (!isAbsolute && path.Contains('\\')) { Log.Warning(src, null, "Inconsistent separators in path " + path.Quote() + ". In Uno, paths should be separated using '/'"); } if (isAbsolute && !flags.HasFlag(PathFlags.AllowAbsolutePath)) { Log.Warning(src, null, "Absolute path " + path.Quote() + ". In Uno, paths should be relative to specifying file"); } path = path.StartsWith("@//", StringComparison.InvariantCulture) ? path.Substring(3).UnixToNative().ToFullPath(src.Package.SourceDirectory) : path.UnixToNative().ToFullPath(dir); if (flags.HasFlag(PathFlags.AllowNonExistingPath)) { return(true); } if (!flags.HasFlag(PathFlags.IsDirectory) && !File.Exists(path) || flags.HasFlag(PathFlags.IsDirectory) && !Directory.Exists(path)) { var msg = (flags.HasFlag(PathFlags.IsDirectory) ? "Directory" : "File") + " " + path.Quote() + " does not exist"; if (flags.HasFlag(PathFlags.WarnIfNonExistingPath)) { Log.Warning(src, null, msg); } else { Log.Error(src, null, msg); } return(false); } return(true); }
public static bool TryParsePathSpec(string path, out PathSpec pathSpec, out string error, PathFlags flags = PathFlags.UseDefaultsForGivenPath) { if (flags.HasFlag(PathFlags.UseDefaultsFromUtility) && flags.HasFlag(PathFlags.UseDefaultsForGivenPath)) throw new ArgumentException("Cannot specify both PathFlags.UseDefaultsFromUtility and PathFlags.UseDefaultsForGivenPath"); if (flags.HasFlag(PathFlags.UseDefaultsFromUtility)) flags = GetDefaultFlagsForThisEnvironment(); error = string.Empty; pathSpec = null; var isWindowsStyle = path.Contains("\\") || path.Contains(":"); var isUnixStyle = path.Contains("/"); if (isWindowsStyle && isUnixStyle) { error = "Cannot mix slashes and backslashes in the same path"; return false; } if (isWindowsStyle) { if (flags.HasFlag(PathFlags.UseDefaultsForGivenPath)) flags = PathFlags.None; if (path.Length > 1 && path.EndsWith("\\")) path = path.Substring(0, path.Length - 1); var colonIdx = path.LastIndexOf(':'); if (colonIdx > -1 && (colonIdx != 1 || !char.IsLetter(path[0]) || (path.Length > 2 && path[2] != '\\'))) { error = "A Windows path may not contain a : character, except as part of the drive specifier."; return false; } var isAbsolute = path.IsAbsoluteWindowsPath(); if (isAbsolute) { var components = path.Split('\\').ToList(); components.RemoveWhere((i, str) => i != 0 && str == "."); if (components.Any(String.IsNullOrEmpty)) { error = "Must not contain any directories that have empty names"; return false; } if (components.IsAncestorOfRoot()) { error = "Must not point to an ancestor of the filesystem root"; return false; } pathSpec = new PathSpec(flags, "\\", components); } else if (path.StartsWith(".")) { var components = path.Split('\\').ToList(); components.RemoveWhere((i, str) => i != 0 && str == "."); if (components.Any(String.IsNullOrEmpty)) { error = "Must not contain any directories that have empty names"; return false; } if (components.IsAncestorOfRoot()) { error = "Must not point to an ancestor of the filesystem root"; return false; } pathSpec = new PathSpec(flags, "\\", components); } else if (path.StartsWith("\\\\")) { var components = "\\\\".ItemConcat(path.Substring(2).Split('\\')).ToList(); components.RemoveWhere((i, str) => i != 0 && str == "."); if (components.Any(String.IsNullOrEmpty)) { error = "Must not contain any directories that have empty names"; return false; } if (components.IsAncestorOfRoot()) { error = "Must not point to an ancestor of the filesystem root"; return false; } pathSpec = new PathSpec(flags, "\\", components); } else if (path.StartsWith("\\")) { var components = "\\".ItemConcat(path.Substring(1).Split('\\')).ToList(); components.RemoveWhere((i, str) => i != 0 && str == "."); if (components.Any(String.IsNullOrEmpty)) { error = "Must not contain any directories that have empty names"; return false; } if (components.IsAncestorOfRoot()) { error = "Must not point to an ancestor of the filesystem root"; return false; } pathSpec = new PathSpec(flags, "\\", components); } else { var components = ".".ItemConcat(path.Split('\\')).ToList(); components.RemoveWhere((i, str) => i != 0 && str == "."); if (components.Any(String.IsNullOrEmpty)) { error = "Must not contain any directories that have empty names"; return false; } if (components.IsAncestorOfRoot()) { error = "Must not point to an ancestor of the filesystem root"; return false; } pathSpec = new PathSpec(flags, "\\", components); } return true; } if (isUnixStyle) { if (flags.HasFlag(PathFlags.UseDefaultsForGivenPath)) flags = PathFlags.CaseSensitive; if (path.Length > 1 && path.EndsWith("/")) path = path.Substring(0, path.Length - 1); if (path.Contains(":")) { error = "A Unix path may not contain a : character."; return false; } var isAbsolute = path.IsAbsoluteUnixPath(); if (isAbsolute) { var components = "/".ItemConcat(path.Substring(1).Split('/')).ToList(); components.RemoveWhere((i, str) => i != 0 && str == "."); if (components.Any(String.IsNullOrEmpty)) { error = "Must not contain any directories that have empty names"; return false; } if (components.IsAncestorOfRoot()) { error = "Must not point to an ancestor of the filesystem root"; return false; } pathSpec = new PathSpec(flags, "/", components); } else if (path.StartsWith(".")) { var components = path.Split('/').ToList(); components.RemoveWhere((i, str) => i != 0 && str == "."); if (components.Any(String.IsNullOrEmpty)) { error = "Must not contain any directories that have empty names"; return false; } if (components.IsAncestorOfRoot()) { error = "Must not point to an ancestor of the filesystem root"; return false; } pathSpec = new PathSpec(flags, "/", components); } else { var components = ".".ItemConcat(path.Split('/')).ToList(); components.RemoveWhere((i, str) => i != 0 && str == "."); if (components.Any(String.IsNullOrEmpty)) { error = "Must not contain any directories that have empty names"; return false; } if (components.IsAncestorOfRoot()) { error = "Must not point to an ancestor of the filesystem root"; return false; } pathSpec = new PathSpec(flags, "/", components); } return true; } // If we reach this point, there are no backslashes or slashes in the path, meaning that it's a // path with one element. if (flags.HasFlag(PathFlags.UseDefaultsForGivenPath)) flags = GetDefaultFlagsForThisEnvironment(); if (path == ".." || path == ".") pathSpec = new PathSpec(flags, DefaultDirectorySeparator, path); else pathSpec = new PathSpec(flags, DefaultDirectorySeparator, ".", path); return true; }