Example #1
0
        private static IDirectoryInfo CopyDirectory(IDirectoryInfo source, IDirectoryInfo destination)
        {
            var targetDir = destination.GetDirectories().FirstOrDefault(dir => string.Equals(dir.Name, source.Name, StringComparison.OrdinalIgnoreCase))
                            ?? destination.CreateSubdirectory(source.Name);

            foreach (var sourceFile in source.GetFiles())
            {
                var name       = sourceFile.Name;
                var targetFile = targetDir.GetFiles().FirstOrDefault(item => string.Equals(item.Name, name, StringComparison.OrdinalIgnoreCase))
                                 ?? destination.DriveInfo.GetFileInfo(Path.Combine(targetDir.FullName, name));

                using (var input = sourceFile.Open(FileMode.Open, FileAccess.Read))
                    using (var output = targetFile.Open(FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        input.CopyTo(output);
                    }
            }

            foreach (var subSource in source.GetDirectories())
            {
                CopyDirectory(subSource, targetDir);
            }

            return(targetDir);
        }
Example #2
0
        private static IDirectoryInfo CopyDirectory(IDirectoryInfo source, IDirectoryInfo destination)
        {
            var targetDir = destination.GetDirectories().FirstOrDefault(dir => string.Equals(dir.Name, source.Name, StringComparison.OrdinalIgnoreCase))
                            ?? destination.CreateSubdirectory(source.Name);

            foreach (var sourceFile in source.GetFiles())
            {
                var name = sourceFile.Name;
                var targetFile = targetDir.GetFiles().FirstOrDefault(item => string.Equals(item.Name, name, StringComparison.OrdinalIgnoreCase))
                                 ?? destination.DriveInfo.GetFileInfo(Path.Combine(targetDir.FullName, name));

                using (var input = sourceFile.Open(FileMode.Open, FileAccess.Read))
                using (var output = targetFile.Open(FileMode.OpenOrCreate, FileAccess.Write))
                {
                    input.CopyTo(output);
                }
            }

            foreach (var subSource in source.GetDirectories())
            {
                CopyDirectory(subSource, targetDir);
            }

            return targetDir;
        }
Example #3
0
        private void When_creating_subdirectory_for_relative_path_it_must_succeed()
        {
            // Arrange
            const string path       = @"d:\some";
            const string folderName = @"with\nested\subtree";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(path)
                                     .Build();

            IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(path);

            // Act
            IDirectoryInfo subdirInfo = dirInfo.CreateSubdirectory(folderName);

            // Assert
            const string completePath = path + @"\" + folderName;

            subdirInfo.FullName.Should().Be(completePath);
            subdirInfo.Exists.Should().BeTrue();

            IDirectoryInfo topDirectory = subdirInfo.Parent.ShouldNotBeNull().Parent.ShouldNotBeNull().Parent.ShouldNotBeNull();

            topDirectory.FullName.Should().Be(dirInfo.FullName);

            fileSystem.Directory.Exists(completePath).Should().BeTrue();
        }
Example #4
0
 private void CopySubDirectories(IDirectoryInfo sourceDirectory, IDirectoryInfo destinationDirectory)
 {
     IDirectoryInfo[] subDirectories = sourceDirectory.GetDirectories();
     foreach (IDirectoryInfo subDirectory in subDirectories)
     {
         IDirectoryInfo destSubDirectory = destinationDirectory.CreateSubdirectory(subDirectory.Name);
         CopyDirectory(subDirectory, destSubDirectory);
     }
 }
Example #5
0
        public static IDirectoryInfo CreateItemFolderWithSingleFile(IFileSystem fileSystem, string steamUploadPath)
        {
            IDirectoryInfo itemFolder = fileSystem.DirectoryInfo.FromDirectoryName(steamUploadPath);

            itemFolder.Create();
            itemFolder.CreateSubdirectory("sub_dir");
            using StreamWriter streamWriter = fileSystem.File.CreateText(steamUploadPath + "/sub_dir/file.txt");
            streamWriter.Write("My awesome content!");
            streamWriter.Close();

            return(itemFolder);
        }
        private void CopyFiles(IDirectoryInfo source, IDirectoryInfo target)
        {
            foreach (var fi in source.GetFiles())
            {
                fi.CopyTo(this.fileSystem.Path.Combine(target.FullName, fi.Name));
            }

            foreach (var subDirectory in source.GetDirectories())
            {
                var nextTargetSubDir = target.CreateSubdirectory(subDirectory.Name);
                this.CopyFiles(subDirectory, nextTargetSubDir);
            }
        }
Example #7
0
        private IDirectoryInfo GetDirectoryPath(IDirectoryInfo directoryPath, string subDirectory)
        {
            if (string.IsNullOrEmpty(subDirectory))
            {
                return(directoryPath);
            }
            var target = directoryPath.GetDirectories(subDirectory, SearchOption.TopDirectoryOnly).FirstOrDefault();

            if (target == null || !target.Exists)
            {
                target = directoryPath.CreateSubdirectory(subDirectory);
            }
            return(target);
        }
Example #8
0
        private void CopyAll(IDirectoryInfo source, IDirectoryInfo destination)
        {
            // copy each file into destination
            foreach (var file in source.GetFiles())
            {
                file.CopyTo(_fileSystem.Path.Combine(destination.FullName, file.Name), true);
            }

            // copy each subdirectory using recursion
            foreach (var diSourceSubDir in source.GetDirectories())
            {
                var nextTargetSubDir = destination.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }
Example #9
0
        private void When_creating_subdirectory_for_invalid_characters_it_must_fail()
        {
            // Arrange
            const string path = @"d:\some";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(path)
                                     .Build();

            IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(path);

            // Act
            Action action = () => dirInfo.CreateSubdirectory("sub?");

            // Assert
            action.ShouldThrow <ArgumentException>().WithMessage("Illegal characters in path.*");
        }
Example #10
0
        private void When_creating_subdirectory_for_invalid_root_it_must_fail()
        {
            // Arrange
            const string path = @"d:\some";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(path)
                                     .Build();

            IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(path);

            // Act
            Action action = () => dirInfo.CreateSubdirectory("::");

            // Assert
            action.ShouldThrow <ArgumentException>().WithMessage("Second path fragment must not be a drive or UNC name.*");
        }
Example #11
0
        private void When_creating_subdirectory_for_too_many_parent_paths_below_directory_it_must_succeed()
        {
            // Arrange
            const string path = @"d:\some";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(path)
                                     .Build();

            IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(path);

            // Act
            IDirectoryInfo subdirInfo = dirInfo.CreateSubdirectory(@"..\..\..\..\..\..\..\..\..\some\other");

            // Assert
            subdirInfo.FullName.Should().Be(@"d:\some\other");
        }
Example #12
0
        private void When_creating_subdirectory_for_empty_string_it_must_fail()
        {
            // Arrange
            const string path = @"d:\some";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(path)
                                     .Build();

            IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(path);

            // Act
            Action action = () => dirInfo.CreateSubdirectory(string.Empty);

            // Assert
            action.ShouldThrow <ArgumentException>().WithMessage("Path cannot be the empty string or all whitespace.*");
        }
Example #13
0
        private void When_creating_subdirectory_for_whitespace_it_must_succeed()
        {
            // Arrange
            const string path = @"c:\some";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(path);

            // Act
            IDirectoryInfo resultDirInfo = dirInfo.CreateSubdirectory(" ");

            // Assert
            resultDirInfo.FullName.Should().Be(path);

            fileSystem.Directory.Exists(path).Should().BeTrue();
        }
Example #14
0
        private void When_creating_subdirectory_for_parent_path_next_to_directory_it_must_fail()
        {
            // Arrange
            const string path = @"d:\some";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(path)
                                     .Build();

            IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(path);

            // Act
            Action action = () => dirInfo.CreateSubdirectory(@"..\some2\more");

            // Assert
            action.ShouldThrow <ArgumentException>()
            .WithMessage(@"The directory specified, '..\some2\more', is not a subdirectory of 'd:\some'.*");
        }
Example #15
0
        private void When_creating_subdirectory_for_null_it_must_fail()
        {
            // Arrange
            const string path = @"d:\some";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(path)
                                     .Build();

            IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(path);

            // Act
            // ReSharper disable once AssignNullToNotNullAttribute
            Action action = () => dirInfo.CreateSubdirectory(null);

            // Assert
            action.ShouldThrow <ArgumentNullException>();
        }
Example #16
0
        public void CopyAll(IDirectoryInfo source, IDirectoryInfo target)
        {
            directory.CreateDirectory(target.FullName);

            // Copy each file into the new directory.
            foreach (IFileInfo fi in source.GetFiles())
            {
                fi.CopyTo(path.Combine(target.FullName, fi.Name), true);
            }

            // Copy each subdirectory using recursion.
            foreach (IDirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                IDirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }
Example #17
0
        private static void AssurePathExists(IDirectoryInfo dirInfo, Queue <string> nameSpaceParts)
        {
            if (!nameSpaceParts.Any())
            {
                return;
            }

            var nextPart       = nameSpaceParts.Dequeue();
            var subDirectories = dirInfo.GetDirectories();
            var nextDirectory  = subDirectories.SingleOrDefault(f => f.Name == nextPart);

            if (nextDirectory == null)
            {
                nextDirectory = dirInfo.CreateSubdirectory(nextPart);
            }

            AssurePathExists(nextDirectory, nameSpaceParts);
        }
Example #18
0
        private string GetLogDirectory()
        {
            if (_logDirectoryPath == null)
            {
                lock (_logDirectoryPathLock)
                {
                    if (_logDirectoryPath == null)
                    {
                        string         basePath          = _fileSystem.Path.Combine(_dataDirectory.ResolvedPath, InMemoryDirectory);
                        IDirectoryInfo baseDirectoryInfo = _fileSystem.DirectoryInfo.FromDirectoryName(basePath);
                        if (!baseDirectoryInfo.Exists)
                        {
                            baseDirectoryInfo.Create();
                        }

                        IDirectoryInfo[] existingLogDirectories = baseDirectoryInfo.GetDirectories("log*");
                        int existingSuffix = existingLogDirectories.Select(x =>
                        {
                            if (int.TryParse(x.Name.Substring(3), out int suffix))
                            {
                                return(suffix);
                            }

                            return(0);
                        })
                                             .DefaultIfEmpty()
                                             .Max();
                        int    newSuffix        = existingSuffix + 1;
                        string logDirectoryName = $"log{newSuffix:0000}";

                        if (_logger.IsDebug())
                        {
                            LogDebug.CreatingMemoryStoreLogDirectory(_logger, logDirectoryName,
                                                                     baseDirectoryInfo.FullName, null);
                        }
                        IDirectoryInfo logDirectory = baseDirectoryInfo.CreateSubdirectory(logDirectoryName);
                        _logDirectoryPath = logDirectory.FullName;
                    }
                }
            }

            return(_logDirectoryPath);
        }