Exemple #1
0
        /// <summary>
        /// Mounts a filesystem for the specified mount name.
        /// </summary>
        /// <param name="name">The mount name.</param>
        /// <param name="fileSystem">The file system.</param>
        /// <exception cref="System.ArgumentNullException">fileSystem</exception>
        /// <exception cref="System.ArgumentException">
        /// Cannot recursively mount the filesystem to self - <paramref name="fileSystem"/>
        /// or
        /// There is already a mount with the same name: `{name}` - <paramref name="name"/>
        /// </exception>
        public void Mount(UPath name, IFileSystem fileSystem)
        {
            if (fileSystem is null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }
            if (fileSystem == this)
            {
                throw new ArgumentException("Cannot recursively mount the filesystem to self", nameof(fileSystem));
            }
            ValidateMountName(name);

            if (_mounts.ContainsKey(name))
            {
                throw new ArgumentException($"There is already a mount with the same name: `{name}`", nameof(name));
            }
            _mounts.Add(name, fileSystem);

            foreach (var watcher in _watchers)
            {
                if (!IsMountIncludedInWatch(name, watcher.Path, out var remainingPath))
                {
                    continue;
                }

                if (fileSystem.CanWatch(remainingPath))
                {
                    var internalWatcher = fileSystem.Watch(remainingPath);
                    watcher.Add(new WrapWatcher(fileSystem, name, remainingPath, internalWatcher));
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Adds a filesystem to this aggregate view. The Last filesystem as more priority than the previous one when an overrides
        /// of a file occurs.
        /// </summary>
        /// <param name="fs">The filesystem to add to this aggregate.</param>
        /// <exception cref="System.ArgumentNullException">fs</exception>
        /// <exception cref="System.ArgumentException">Cannot add this instance as an aggregate delegate of itself</exception>
        /// <exception cref="System.ArgumentException">The filesystem is already added</exception>
        public virtual void AddFileSystem(IFileSystem fs)
        {
            if (fs is null)
            {
                throw new ArgumentNullException(nameof(fs));
            }
            if (fs == this)
            {
                throw new ArgumentException("Cannot add this instance as an aggregate delegate of itself");
            }

            if (!_fileSystems.Contains(fs))
            {
                _fileSystems.Add(fs);

                foreach (var watcher in _watchers)
                {
                    if (fs.CanWatch(watcher.Path))
                    {
                        var newWatcher = fs.Watch(watcher.Path);
                        watcher.Add(newWatcher);
                    }
                }
            }
            else
            {
                throw new ArgumentException("The filesystem is already added");
            }
        }
Exemple #3
0
        /// <summary>
        /// Tries to watch the specified path. If watching the file system or path is not supported, returns null.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The path to watch for changes.</param>
        /// <returns>An <see cref="IFileSystemWatcher"/> instance or null if not supported.</returns>
        public static IFileSystemWatcher?TryWatch(this IFileSystem fileSystem, UPath path)
        {
            if (!fileSystem.CanWatch(path))
            {
                return(null);
            }

            return(fileSystem.Watch(path));
        }
Exemple #4
0
        /// <summary>
        /// Mounts a filesystem for the specified mount name.
        /// </summary>
        /// <param name="name">The mount name.</param>
        /// <param name="fileSystem">The file system.</param>
        /// <exception cref="System.ArgumentNullException">fileSystem</exception>
        /// <exception cref="System.ArgumentException">
        /// Cannot recursively mount the filesystem to self - <paramref name="fileSystem"/>
        /// or
        /// There is already a mount with the same name: `{name}` - <paramref name="name"/>
        /// </exception>
        public void Mount(UPath name, IFileSystem fileSystem)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }
            if (fileSystem == this)
            {
                throw new ArgumentException("Cannot recursively mount the filesystem to self", nameof(fileSystem));
            }
            AssertMountName(name);

            lock (_mounts)
            {
                if (_mounts.ContainsKey(name))
                {
                    throw new ArgumentException($"There is already a mount with the same name: `{name}`", nameof(name));
                }
                _mounts.Add(name, fileSystem);

                lock (_aggregateWatchers)
                {
                    foreach (var watcher in _aggregateWatchers)
                    {
                        var remainingPath = GetRemainingForWatch(watcher.Path, name);
                        if (remainingPath.IsNull)
                        {
                            continue;
                        }

                        if (fileSystem.CanWatch(remainingPath))
                        {
                            var internalWatcher = fileSystem.Watch(remainingPath);
                            watcher.Add(new Watcher(this, name, remainingPath, internalWatcher));
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Tries to watch the specified path. If watching the file system or path is not supported, returns null.
 /// </summary>
 /// <param name="fileSystem">The file system.</param>
 /// <param name="path">The path to watch for changes.</param>
 /// <returns>An <see cref="IFileSystemWatcher"/> instance or null if not supported.</returns>
 public static IFileSystemWatcher TryWatch(this IFileSystem fileSystem, UPath path)
 {
     return(!fileSystem.CanWatch(path) ? null : fileSystem.Watch(path));
 }