Example #1
0
        protected virtual void CleanOrphanedTempFolders()
        {
            // Clean up orphaned temp folders
            IDirectoryInformation rootInfo = this.fileServiceProvider.GetPathInfo(this.rootTempFolder) as IDirectoryInformation;

            if (rootInfo != null)
            {
                try
                {
                    var flagFiles =
                        from directory in rootInfo.EnumerateDirectories()
                        from file in directory.EnumerateFiles(FileCleaner.XTaskFlagFileName)
                        select new { Directory = directory.Path, File = file.Path };

                    foreach (var flagFile in flagFiles.ToArray())
                    {
                        try
                        {
                            // If we can't delete the flag file (open handle) we'll throw and move on
                            this.fileServiceProvider.DeleteFile(flagFile.File);
                            this.fileServiceProvider.DeleteDirectory(flagFile.Directory, deleteChildren: true);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                catch (Exception)
                {
                    // Ignoring orphan cleanup errors as the DotNet file service chokes on long paths
                }
            }
        }
Example #2
0
        public void EnumerateDirectoriesPassesCorrectArguments()
        {
            IDirectoryInformation info = Substitute.For <IDirectoryInformation>();

            info.EnumerateDirectories("foo", SearchOption.AllDirectories, 0);
            info.Received(1).EnumerateChildren(ChildType.Directory, "foo", SearchOption.AllDirectories, 0);
        }
 /// <summary>
 /// Walk directories in the current directory. Allows filtering by attributes. Folders with the given attributes will be skipped as well.
 /// </summary>
 /// <param name="fileSystemInformation">If the information is not a directory, returns an empty enumerable.</param>
 public static IEnumerable <IDirectoryInformation> EnumerateDirectories(
     this IDirectoryInformation directoryInformation,
     string searchPattern             = "*",
     SearchOption searchOption        = SearchOption.TopDirectoryOnly,
     FileAttributes excludeAttributes = FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReparsePoint)
 {
     return(directoryInformation.EnumerateChildren(ChildType.Directory, searchPattern, searchOption, excludeAttributes).OfType <IDirectoryInformation>());
 }
Example #4
0
        /// <summary>
        /// Gets directory info for the specified path.
        /// </summary>
        /// <exception cref="FileExistsException">Thrown if a file exists in the given path.</exception>
        public static IDirectoryInformation GetDirectoryInfo(this IFileService fileService, string path)
        {
            IDirectoryInformation directoryInformation = fileService.GetPathInfo(path) as IDirectoryInformation;

            if (directoryInformation == null)
            {
                throw new FileExistsException(XTaskStrings.ErrorNotADirectory, path);
            }
            return(directoryInformation);
        }
        public void GetFileInfoThrowsForDirectory()
        {
            IFileService          service = Substitute.For <IFileService>();
            IDirectoryInformation info    = Substitute.For <IDirectoryInformation>();

            service.GetPathInfo("").ReturnsForAnyArgs(info);

            Action action = () => service.GetFileInfo("Foo");

            action.ShouldThrow <FileExistsException>();
        }
Example #6
0
        public IDirectoryInformation[] GetDirectorySubdirectories(string dirPath)
        {
            DirectoryInfo dir = new DirectoryInfo(dirPath);

            DirectoryInfo[]         dirInfos    = dir.GetDirectories();
            IDirectoryInformation[] winDirInfos = new IDirectoryInformation[dirInfos.Length];

            for (int i = 0; i < dirInfos.Length; i++)
            {
                winDirInfos[i] = new WindowsDirectoryInformation(dirInfos[i]);
            }

            return(winDirInfos);
        }
Example #7
0
        public void CleanupHandlesDirectoryExceptions()
        {
            IFileService fileServiceProvider = Substitute.For <IFileService>();
            MemoryStream memoryStream        = new MemoryStream();

            fileServiceProvider.CreateFileStream("").ReturnsForAnyArgs(memoryStream);
            IDirectoryInformation directoryInfo = Substitute.For <IDirectoryInformation>();
            IFileInformation      fileInfo      = Substitute.For <IFileInformation>();

            directoryInfo.EnumerateChildren().ReturnsForAnyArgs(new IFileInformation[] { fileInfo });
            fileServiceProvider.GetPathInfo("").ReturnsForAnyArgs(directoryInfo);

            using (FileCleaner cleaner = new FileCleaner("Test", fileServiceProvider))
            {
                fileServiceProvider.WhenForAnyArgs(f => f.DeleteDirectory("")).Do(a => { throw new Exception("TestException"); });
            }
        }
Example #8
0
        private void SetSubdirectoriesExpectations(IFileSystem fileSystem)
        {
            foreach (KeyValuePair <string, List <string> > dirPair in dirsByDirs)
            {
                string dirPath = dirPair.Key;

                List <IDirectoryInformation> dirInfos = new List <IDirectoryInformation>();

                foreach (string dirInDir in dirPair.Value)
                {
                    IDirectoryInformation dirInfo = MockRepository.GenerateStub <IDirectoryInformation>();
                    dirInfo.Stub(x => x.FullName).Return(Path.Combine(dirPath, dirInDir));
                    dirInfos.Add(dirInfo);
                }

                fileSystem.Stub(x => x.GetDirectorySubdirectories(dirPath)).Return(dirInfos.ToArray());
            }
        }
Example #9
0
        protected override ExitCode ExecuteFileTask()
        {
            var fileService = this.GetService <IFileService>();

            Table output = Table.Create(
                new ColumnFormat(1),
                new ColumnFormat(1),
                new ColumnFormat(1, ContentVisibility.Default, Justification.Right),
                new ColumnFormat(4));

            output.HasHeader = false;

            int   directoryCount = 0;
            int   fileCount      = 0;
            ulong totalSize      = 0;

            IDirectoryInformation directory = fileService.GetDirectoryInfo(GetFullTargetPath());

            foreach (var subdir in directory.EnumerateDirectories().OrderBy(i => i.Name))
            {
                directoryCount++;
                this.AddToTable(output, subdir);
            }

            foreach (var file in directory.EnumerateFiles().OrderBy(i => i.Name))
            {
                fileCount++;
                totalSize += file.Length;
                this.AddToTable(output, file);
            }

            this.Loggers[LoggerType.Status].WriteLine($" Directory of {directory.Path}");
            this.Loggers[LoggerType.Status].WriteLine();
            this.Loggers[LoggerType.Result].Write(output);
            this.Loggers[LoggerType.Status].WriteLine();
            this.Loggers[LoggerType.Status].WriteLine($" {directoryCount} Dir(s) {fileCount} File(s) {totalSize:N0} bytes");
            return(ExitCode.Success);
        }
Example #10
0
 public override void Refresh()
 {
     this.md5Hash = null;
     this.directoryInformation = null;
     base.Refresh();
 }
Example #11
0
 public override void Refresh()
 {
     _md5Hash = null;
     _directoryInformation = null;
     base.Refresh();
 }
Example #12
0
 public override void Refresh()
 {
     _md5Hash = null;
     _directoryInformation = null;
     base.Refresh();
 }
Example #13
0
 public override void Refresh()
 {
     this.md5Hash = null;
     this.directoryInformation = null;
     base.Refresh();
 }
Example #14
0
 protected override void Refresh(bool fromAttributes)
 {
     this.md5Hash = null;
     this.directoryInformation = null;
     base.Refresh();
 }