public AnnoRDA.File AddFileToFolder(AnnoRDA.Folder folder, FileHeader file, IEnumerable <string> filePathComponents, AnnoRDA.BlockContentsSource blockContentsSource) { if (!filePathComponents.Any()) { throw new ArgumentException("filePathComponents cannot be empty", "filePathComponents"); } string currentName = filePathComponents.First(); IEnumerable <string> filePathComponentsRemaining = filePathComponents.Skip(1); if (filePathComponentsRemaining.Any()) { AnnoRDA.Folder currentFolder = folder.Folders.FirstOrDefault((f) => f.Name == currentName); if (currentFolder == null) { currentFolder = new Folder(currentName); folder.Add(currentFolder); } return(this.AddFileToFolder(currentFolder, file, filePathComponentsRemaining, blockContentsSource)); } else { AnnoRDA.File rdaFile = new File(currentName) { ModificationTimestamp = file.ModificationTimestamp, ContentsSource = new AnnoRDA.FileContentsSource(blockContentsSource, file.DataOffset, file.CompressedFileSize, file.UncompressedFileSize), }; folder.Add(rdaFile); return(rdaFile); } }
public void TestOverwriteFileInFileSystem() { var folderPath = new AnnoRDA.Folder("path"); { var folderTo = new AnnoRDA.Folder("to"); { folderTo.Add(new AnnoRDA.File("file.txt") { ModificationDate = new DateTime(), }); } folderPath.Add(folderTo); } var file = new AnnoRDA.FileEntities.FileHeader() { Path = "path/to/file.txt", DataOffset = 11111111, CompressedFileSize = 36960, UncompressedFileSize = 36960, ModificationTimestamp = 1448398881, }; var loader = new ContainerFileLoader(); AnnoRDA.FileSystem fileSystem; using (var context = new ContainerFileLoader.Context("dummy.rda", TestData.GetStream(), false, new PassThroughFileHeaderTransformer())) { fileSystem = context.FileSystem; fileSystem.Root.Add(folderPath); loader.AddFileToFileSystem(context, file, TestData.GetDummyBlockContentsSource()); } Assert.FolderAndFileCountAreEqual(1, 0, fileSystem.Root); Assert.FolderAndFileCountAreEqual(1, 0, fileSystem.Root.Folders.First()); Assert.FolderAndFileCountAreEqual(0, 1, fileSystem.Root.Folders.First().Folders.First()); Assert.ContainsFile(fileSystem, new Assert.FileSpec("path", "to", "file.txt") { CompressedFileSize = 36960, UncompressedFileSize = 36960, ModificationTimestamp = 1448398881, }); }
public void TestMergeFileSystemsWithFilesAndFolders() { AnnoRDA.FileSystem baseFS = new FileSystem(); baseFS.Root.Add(new File("root file")); var baseFSFolder1 = new AnnoRDA.Folder("Max Design"); { baseFSFolder1.Add(new File("1602") { ModificationDate = new DateTime(1998, 9, 24, 0, 0, 0, DateTimeKind.Utc) }); } baseFS.Root.Add(baseFSFolder1); var baseFSFolder2 = new AnnoRDA.Folder("Related Designs"); { baseFSFolder2.Add(new File("1701") { ModificationDate = new DateTime(2006, 10, 25, 0, 0, 0, DateTimeKind.Utc) }); baseFSFolder2.Add(new File("2070") { ModificationDate = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc) }); } baseFS.Root.Add(baseFSFolder2); AnnoRDA.FileSystem overwriteFS = new FileSystem(); var overwriteFSFolder1 = new AnnoRDA.Folder("Max Design"); { overwriteFSFolder1.Add(new File("1503") { ModificationDate = new DateTime(2003, 3, 23, 0, 0, 0, DateTimeKind.Utc) }); } overwriteFS.Root.Add(overwriteFSFolder1); var overwriteFSFolder2 = new AnnoRDA.Folder("Related Designs"); { overwriteFSFolder2.Add(new File("1404") { ModificationDate = new DateTime(2009, 6, 25, 0, 0, 0, DateTimeKind.Utc) }); overwriteFSFolder2.Add(new File("2070") { ModificationDate = new DateTime(2011, 11, 17, 0, 0, 0, DateTimeKind.Utc) }); } overwriteFS.Root.Add(overwriteFSFolder2); var overwriteFSFolder3 = new AnnoRDA.Folder("Blue Byte"); { overwriteFSFolder3.Add(new File("1404") { ModificationDate = new DateTime(2009, 6, 25, 0, 0, 0, DateTimeKind.Utc) }); overwriteFSFolder3.Add(new File("2070") { ModificationDate = new DateTime(2011, 11, 17, 0, 0, 0, DateTimeKind.Utc) }); overwriteFSFolder3.Add(new File("2205") { ModificationDate = new DateTime(2015, 11, 3, 0, 0, 0, DateTimeKind.Utc) }); } overwriteFS.Root.Add(overwriteFSFolder3); baseFS.OverwriteWith(overwriteFS, null, System.Threading.CancellationToken.None); Assert.FolderAndFileCountAreEqual(3, 1, baseFS); Assert.ContainsFile(baseFS, new Assert.FileSpec("root file")); Assert.ContainsFile(baseFS, new Assert.FileSpec("Max Design", "1503") { ModificationDate = new DateTime(2003, 3, 23, 0, 0, 0, DateTimeKind.Utc) }); Assert.ContainsFile(baseFS, new Assert.FileSpec("Max Design", "1602") { ModificationDate = new DateTime(1998, 9, 24, 0, 0, 0, DateTimeKind.Utc) }); Assert.ContainsFile(baseFS, new Assert.FileSpec("Related Designs", "1701") { ModificationDate = new DateTime(2006, 10, 25, 0, 0, 0, DateTimeKind.Utc) }); Assert.ContainsFile(baseFS, new Assert.FileSpec("Related Designs", "1404") { ModificationDate = new DateTime(2009, 6, 25, 0, 0, 0, DateTimeKind.Utc) }); Assert.ContainsFile(baseFS, new Assert.FileSpec("Related Designs", "2070") { ModificationDate = new DateTime(2011, 11, 17, 0, 0, 0, DateTimeKind.Utc) }); Assert.ContainsFile(baseFS, new Assert.FileSpec("Blue Byte", "1404") { ModificationDate = new DateTime(2009, 6, 25, 0, 0, 0, DateTimeKind.Utc) }); Assert.ContainsFile(baseFS, new Assert.FileSpec("Blue Byte", "2070") { ModificationDate = new DateTime(2011, 11, 17, 0, 0, 0, DateTimeKind.Utc) }); Assert.ContainsFile(baseFS, new Assert.FileSpec("Blue Byte", "2205") { ModificationDate = new DateTime(2015, 11, 3, 0, 0, 0, DateTimeKind.Utc) }); }