Ejemplo n.º 1
0
        /// <summary>
        /// Converts the specified path to an absolute path (by adding a leading `/`). If the path is already absolute, returns the input.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>An absolute path.</returns>
        /// <exception cref="ArgumentNullException">if path is <see cref="UPath.IsNull"/></exception>
        public static UPath ToAbsolute(this UPath path)
        {
            path.AssertNotNull();

            if (path.IsAbsolute)
            {
                return(path);
            }

            return(path.IsEmpty ? UPath.Root : UPath.Root / path);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts the specified path to a relative path (by removing the leading `/`). If the path is already relative, returns the input.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>A relative path.</returns>
        /// <exception cref="ArgumentNullException">if path is <see cref="UPath.IsNull"/></exception>
        public static UPath ToRelative(this UPath path)
        {
            path.AssertNotNull();

            if (path.IsRelative)
            {
                return(path);
            }

            return(path.FullName == "/" ? UPath.Empty : new UPath(path.FullName.Substring(1), true));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the first directory of the specified path and return the remaining path (/a/b/c, first directory: /a, remaining: b/c)
        /// </summary>
        /// <param name="path">The path to extract the first directory and remaining.</param>
        /// <param name="remainingPath">The remaining relative path after the first directory</param>
        /// <returns>The first directory of the path.</returns>
        /// <exception cref="ArgumentNullException">if path is <see cref="UPath.IsNull"/></exception>
        public static UPath GetFirstPart(this UPath path)
        {
            path.AssertNotNull();
            string pathString = path.FullName;
            var    index      = pathString.IndexOf(UPath.DirectorySeparator, 1);

            if (index < 0)
            {
                return(pathString);
            }
            return(pathString.Substring(0, index));
        }
Ejemplo n.º 4
0
        public FileChangedEventArgs(IFileSystem fileSystem, WatcherChangeTypes changeType, UPath fullPath)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }

            fullPath.AssertNotNull(nameof(fullPath));
            fullPath.AssertAbsolute(nameof(fullPath));

            FileSystem = fileSystem;
            ChangeType = changeType;
            FullPath   = fullPath;
            Name       = fullPath.GetName();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Checks if the path is in the given directory. Does not check if the paths exist.
        /// </summary>
        /// <param name="path">The path to check.</param>
        /// <param name="directory">The directory to check the path against.</param>
        /// <param name="recursive">True to check if it is anywhere in the directory, false to check if it is directly in the directory.</param>
        /// <returns>True when the path is in the given directory.</returns>
        public static bool IsInDirectory(this UPath path, UPath directory, bool recursive)
        {
            path.AssertNotNull();
            directory.AssertNotNull(nameof(directory));

            if (path.IsAbsolute != directory.IsAbsolute)
            {
                throw new ArgumentException("Cannot mix absolute and relative paths", nameof(directory));
            }

            var target = path.FullName;
            var dir    = directory.FullName;

            if (target.Length < dir.Length || !target.StartsWith(dir))
            {
                return(false);
            }

            if (target.Length == dir.Length)
            {
                // exact match due to the StartsWith above
                // the directory parameter is interpreted as a directory so trailing separator isn't important
                return(true);
            }

            var dirHasTrailingSeparator = dir[dir.Length - 1] == UPath.DirectorySeparator;

            if (!recursive)
            {
                // need to check if the directory part terminates
                var lastSeparatorInTarget = target.LastIndexOf(UPath.DirectorySeparator);
                var expectedLastSeparator = dir.Length - (dirHasTrailingSeparator ? 1 : 0);

                if (lastSeparatorInTarget != expectedLastSeparator)
                {
                    return(false);
                }
            }

            if (!dirHasTrailingSeparator)
            {
                // directory is missing ending slash, check that target has it
                return(target.Length > dir.Length && target[dir.Length] == UPath.DirectorySeparator);
            }

            return(true);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Attempts to find information about the mount that a given path maps to.
        /// </summary>
        /// <param name="path">The path to search for.</param>
        /// <param name="name">The mount name that the <paramref name="path"/> belongs to.</param>
        /// <param name="fileSystem">The mounted filesystem that the <paramref name="path"/> is located in.</param>
        /// <param name="fileSystemPath">The path inside of <paramref name="fileSystem"/> that refers to the file at <paramref name="path"/>.</param>
        /// <returns>True if the <paramref name="path"/> was found in a mounted filesystem.</returns>
        /// <exception cref="System.ArgumentNullException">The <paramref name="path"/> must not be null.</exception>
        /// <exception cref="System.ArgumentException">The <paramref name="path"/> must be absolute.</exception>
        public bool TryGetMount(UPath path, out UPath name, out IFileSystem fileSystem, out UPath fileSystemPath)
        {
            path.AssertNotNull();
            path.AssertAbsolute();

            var fs = TryGetMountOrNext(ref path, out name);

            if (fs == null || name.IsNull)
            {
                name           = null;
                fileSystem     = null;
                fileSystemPath = null;
                return(false);
            }

            fileSystem     = fs;
            fileSystemPath = path;
            return(true);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the directory of the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The directory of the path.</returns>
        /// <exception cref="ArgumentNullException">if path is <see cref="UPath.IsNull"/></exception>
        public static UPath GetDirectory(this UPath path)
        {
            path.AssertNotNull();

            var fullname = path.FullName;

            if (fullname == "/")
            {
                return(new UPath());
            }

            var lastIndex = fullname.LastIndexOf(UPath.DirectorySeparator);

            if (lastIndex > 0)
            {
                return(fullname.Substring(0, lastIndex));
            }
            return(lastIndex == 0 ? UPath.Root : UPath.Empty);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets the absolute sub directory from within a given root.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="root">The root from which the sub directory starts.</param>
        /// <returns>The absolute sub directory.</returns>
        public static UPath GetSubDirectory(this UPath path, UPath root)
        {
            path.AssertNotNull();
            root.AssertNotNull(nameof(root));

            if (path.IsAbsolute != root.IsAbsolute)
            {
                throw new ArgumentException("Cannot mix absolute and relative paths", nameof(root));
            }

            var pathFullName = path.FullName;
            var rootFullName = root.FullName;

            if (!pathFullName.StartsWith(rootFullName))
            {
                throw new ArgumentException("Path must start with the given root.", nameof(path));
            }

            return(((UPath)pathFullName.Substring(rootFullName.Length)).ToAbsolute());
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets the first directory of the specified path and return the remaining path (/a/b/c, first directory: /a, remaining: b/c)
        /// </summary>
        /// <param name="path">The path to extract the first directory and remaining.</param>
        /// <param name="remainingPath">The remaining relative path after the first directory</param>
        /// <returns>The first directory of the path.</returns>
        /// <exception cref="ArgumentNullException">if path is <see cref="UPath.IsNull"/></exception>
        public static string GetFirstDirectory(this UPath path, out UPath remainingPath)
        {
            path.AssertNotNull();
            remainingPath = UPath.Empty;

            string firstDirectory;
            var    fullname = path.FullName;
            var    index    = fullname.IndexOf(UPath.DirectorySeparator, 1);

            if (index < 0)
            {
                firstDirectory = fullname.Substring(1, fullname.Length - 1);
            }
            else
            {
                firstDirectory = fullname.Substring(1, index - 1);
                if (index + 1 < fullname.Length)
                {
                    remainingPath = fullname.Substring(index + 1);
                }
            }
            return(firstDirectory);
        }