Пример #1
0
        /// <summary>
        /// Locate a file at the given path by directly mapping path segments to physical directories.
        /// </summary>
        /// <param name="subpath">A path under the root directory</param>
        /// <returns>The file information. Caller must check Exists property. </returns>
        public IFileInfo GetFileInfo(string subpath)
        {
            if (string.IsNullOrEmpty(subpath))
            {
                return(new NotFoundFileInfo(subpath));
            }

            // Relative paths starting with a leading slash okay
            if (subpath.StartsWith("/", StringComparison.Ordinal))
            {
                subpath = subpath.Substring(1);
            }

            // Absolute paths not permitted.
            if (Path.IsPathRooted(subpath))
            {
                return(new NotFoundFileInfo(subpath));
            }

            var fullPath = GetFullPath(subpath);

            if (fullPath == null)
            {
                return(new NotFoundFileInfo(subpath));
            }

            var fileInfo = new FileInfo(fullPath);

            if (FileSystemInfoHelper.IsHiddenFile(fileInfo))
            {
                return(new NotFoundFileInfo(subpath));
            }

            return(new PhysicalFileInfo(_filesWatcher, fileInfo));
        }
Пример #2
0
        /// <summary>
        /// Enumerate a directory at the given path, if any.
        /// </summary>
        /// <param name="subpath">A path under the root directory</param>
        /// <returns>Contents of the directory. Caller must check Exists property.</returns>
        public IDirectoryContents GetDirectoryContents(string subpath)
        {
            try
            {
                if (subpath == null)
                {
                    return(new NotFoundDirectoryContents());
                }

                // Relative paths starting with a leading slash okay
                if (subpath.StartsWith("/", StringComparison.Ordinal))
                {
                    subpath = subpath.Substring(1);
                }

                // Absolute paths not permitted.
                if (Path.IsPathRooted(subpath))
                {
                    return(new NotFoundDirectoryContents());
                }

                var fullPath = GetFullPath(subpath);
                if (fullPath != null)
                {
                    var directoryInfo = new DirectoryInfo(fullPath);
                    if (!directoryInfo.Exists)
                    {
                        return(new NotFoundDirectoryContents());
                    }

                    var physicalInfos = directoryInfo
                                        .EnumerateFileSystemInfos()
                                        .Where(info => !FileSystemInfoHelper.IsHiddenFile(info));
                    var virtualInfos = new List <IFileInfo>();
                    foreach (var fileSystemInfo in physicalInfos)
                    {
                        var fileInfo = fileSystemInfo as FileInfo;
                        if (fileInfo != null)
                        {
                            virtualInfos.Add(new PhysicalFileInfo(_filesWatcher, fileInfo));
                        }
                        else
                        {
                            virtualInfos.Add(new PhysicalDirectoryInfo((DirectoryInfo)fileSystemInfo));
                        }
                    }

                    return(new EnumerableDirectoryContents(virtualInfos));
                }
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (IOException)
            {
            }
            return(new NotFoundDirectoryContents());
        }
Пример #3
0
        private void OnFileSystemEntryChange(string fullPath)
        {
            var fileSystemInfo = new FileInfo(fullPath);

            if (FileSystemInfoHelper.IsHiddenFile(fileSystemInfo))
            {
                return;
            }

            var relativePath = fullPath.Substring(_root.Length);

            if (_tokenCache.ContainsKey(relativePath))
            {
                ReportChangeForMatchedEntries(relativePath);
            }
            else
            {
                foreach (var token in _tokenCache.Values.Where(t => t.IsMatch(relativePath)))
                {
                    ReportChangeForMatchedEntries(token.Pattern);
                }
            }
        }