コード例 #1
0
        public void Constructor_ConversionFromUstar_From_UnseekableTarReader(TarEntryFormat writerFormat)
        {
            using MemoryStream source         = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.ustar, "file");
            using WrappedStream wrappedSource = new WrappedStream(source, canRead: true, canWrite: false, canSeek: false);

            using TarReader sourceReader = new TarReader(wrappedSource, leaveOpen: true);
            UstarTarEntry ustarEntry = sourceReader.GetNextEntry(copyData: false) as UstarTarEntry;
            V7TarEntry    v7Entry    = new V7TarEntry(other: ustarEntry); // Convert, and avoid advancing wrappedSource position

            using MemoryStream destination = new MemoryStream();
            using (TarWriter writer = new TarWriter(destination, writerFormat, leaveOpen: true))
            {
                writer.WriteEntry(v7Entry); // Write DataStream exactly where the wrappedSource position was left
            }

            destination.Position = 0; // Rewind
            using (TarReader destinationReader = new TarReader(destination, leaveOpen: false))
            {
                V7TarEntry resultEntry = destinationReader.GetNextEntry() as V7TarEntry;
                Assert.NotNull(resultEntry);
                using (StreamReader streamReader = new StreamReader(resultEntry.DataStream))
                {
                    Assert.Equal("Hello file", streamReader.ReadToEnd());
                }
            }
        }
コード例 #2
0
        public void SupportedEntryType_Directory()
        {
            V7TarEntry directory = new V7TarEntry(TarEntryType.Directory, InitialEntryName);

            SetDirectory(directory);
            VerifyDirectory(directory);
        }
コード例 #3
0
        public void SupportedEntryType_SymbolicLink()
        {
            V7TarEntry symbolicLink = new V7TarEntry(TarEntryType.SymbolicLink, InitialEntryName);

            SetSymbolicLink(symbolicLink);
            VerifySymbolicLink(symbolicLink);
        }
コード例 #4
0
        public void SupportedEntryType_HardLink()
        {
            V7TarEntry hardLink = new V7TarEntry(TarEntryType.HardLink, InitialEntryName);

            SetHardLink(hardLink);
            VerifyHardLink(hardLink);
        }
コード例 #5
0
        public async Task Write_V7RegularFileEntry_In_OtherFormatsWriter_Async(TarEntryFormat writerFormat)
        {
            using MemoryStream archive = new MemoryStream();
            TarWriter writer = new TarWriter(archive, format: writerFormat, leaveOpen: true);

            await using (writer)
            {
                V7TarEntry entry = new V7TarEntry(TarEntryType.V7RegularFile, InitialEntryName);

                // Should be written in the format of the entry
                await writer.WriteEntryAsync(entry);
            }

            archive.Seek(0, SeekOrigin.Begin);
            TarReader reader = new TarReader(archive);

            await using (reader)
            {
                TarEntry entry = await reader.GetNextEntryAsync();

                Assert.NotNull(entry);
                Assert.Equal(TarEntryFormat.V7, entry.Format);
                Assert.True(entry is V7TarEntry);

                Assert.Null(await reader.GetNextEntryAsync());
            }
        }
コード例 #6
0
        public void SupportedEntryType_V7RegularFile()
        {
            V7TarEntry oldRegularFile = new V7TarEntry(TarEntryType.V7RegularFile, InitialEntryName);

            SetRegularFile(oldRegularFile);
            VerifyRegularFile(oldRegularFile, isWritable: true);
        }
コード例 #7
0
        public async Task VerifyChecksumV7_Async()
        {
            await using (MemoryStream archive = new MemoryStream())
            {
                await using (TarWriter writer = new TarWriter(archive, TarEntryFormat.V7, leaveOpen: true))
                {
                    V7TarEntry entry = new V7TarEntry(
                        // '\0' = 0
                        TarEntryType.V7RegularFile,
                        // 'a.b' = 97 + 46 + 98 = 241
                        entryName: "a.b");

                    // '0000744\0' = 48 + 48 + 48 + 48 + 55 + 52 + 52 + 0 = 351
                    entry.Mode = AssetMode; // octal 744 = u+rxw, g+r, o+r

                    // '0017351\0' = 48 + 48 + 49 + 55 + 51 + 53 + 49 + 0 = 353
                    entry.Uid = AssetUid; // decimal 7913, octal 17351

                    // '0006773\0' = 48 + 48 + 48 + 54 + 55 + 55 + 51 + 0 = 359
                    entry.Gid = AssetGid; // decimal 3579, octal 6773

                    // '14164217674\0' = 49 + 52 + 49 + 54 + 52 + 50 + 49 + 55 + 54 + 55 + 52 + 0 = 571
                    DateTimeOffset mtime = new DateTimeOffset(2022, 1, 2, 3, 45, 00, TimeSpan.Zero); // ToUnixTimeSeconds() = decimal 1641095100, octal 14164217674
                    entry.ModificationTime = mtime;

                    entry.DataStream = new MemoryStream();
                    byte[] buffer = new byte[] { 72, 101, 108, 108, 111 };

                    // '0000000005\0' = 48 + 48 + 48 + 48 + 48 + 48 + 48 + 48 + 48 + 48 + 53 + 0 = 533
                    await entry.DataStream.WriteAsync(buffer);  // Data length: decimal 5

                    entry.DataStream.Seek(0, SeekOrigin.Begin); // Rewind to ensure it gets written from the beginning

                    // Sum so far: 0 + 241 + 351 + 353 + 359 + 571 + 533 = decimal 2408
                    // Add 8 spaces to the sum: 2408 + (8 x 32) = octal 5150, decimal 2664 (final)
                    // Checksum: '005150\0 '

                    await writer.WriteEntryAsync(entry);

                    Assert.Equal(2664, entry.Checksum);
                }

                archive.Seek(0, SeekOrigin.Begin);
                await using (TarReader reader = new TarReader(archive))
                {
                    TarEntry entry = await reader.GetNextEntryAsync();

                    Assert.Equal(2664, entry.Checksum);
                }
            }
        }
コード例 #8
0
        public void Constructor_Name_FullPath_DestinationDirectory_Match_AdditionalSubdirectory_Throws()
        {
            using TempDirectory root = new TempDirectory();

            string fullPath = Path.Join(root.Path, "dir", "file.txt");

            V7TarEntry entry = new V7TarEntry(TarEntryType.V7RegularFile, fullPath);

            entry.DataStream = new MemoryStream();
            entry.DataStream.Write(new byte[] { 0x1 });
            entry.DataStream.Seek(0, SeekOrigin.Begin);

            Assert.Throws <IOException>(() => entry.ExtractToFile(root.Path, overwrite: false));

            Assert.False(File.Exists(fullPath));
        }
コード例 #9
0
        public void ExtractToFile_Link_Throws(TarEntryType entryType)
        {
            using TempDirectory root = new TempDirectory();
            string fileName = "mylink";
            string fullPath = Path.Join(root.Path, fileName);

            string linkTarget = PlatformDetection.IsWindows ? @"C:\Windows\system32\notepad.exe" : "/usr/bin/nano";

            V7TarEntry entry = new V7TarEntry(entryType, fileName);

            entry.LinkName = linkTarget;

            Assert.Throws <InvalidOperationException>(() => entry.ExtractToFile(fileName, overwrite: false));

            Assert.Equal(0, Directory.GetFileSystemEntries(root.Path).Count());
        }
コード例 #10
0
        public void WriteDirectory()
        {
            using MemoryStream archiveStream = new MemoryStream();
            using (TarWriter writer = new TarWriter(archiveStream, TarFormat.V7, leaveOpen: true))
            {
                V7TarEntry directory = new V7TarEntry(TarEntryType.Directory, InitialEntryName);
                SetDirectory(directory);
                VerifyDirectory(directory);
                writer.WriteEntry(directory);
            }

            archiveStream.Position = 0;
            using (TarReader reader = new TarReader(archiveStream))
            {
                V7TarEntry directory = reader.GetNextEntry() as V7TarEntry;
                VerifyDirectory(directory);
            }
        }
コード例 #11
0
        public void WriteRegularFile()
        {
            using MemoryStream archiveStream = new MemoryStream();
            using (TarWriter writer = new TarWriter(archiveStream, TarFormat.V7, leaveOpen: true))
            {
                V7TarEntry oldRegularFile = new V7TarEntry(TarEntryType.V7RegularFile, InitialEntryName);
                SetRegularFile(oldRegularFile);
                VerifyRegularFile(oldRegularFile, isWritable: true);
                writer.WriteEntry(oldRegularFile);
            }

            archiveStream.Position = 0;
            using (TarReader reader = new TarReader(archiveStream))
            {
                V7TarEntry oldRegularFile = reader.GetNextEntry() as V7TarEntry;
                VerifyRegularFile(oldRegularFile, isWritable: false);
            }
        }
コード例 #12
0
        public void WriteHardLink()
        {
            using MemoryStream archiveStream = new MemoryStream();
            using (TarWriter writer = new TarWriter(archiveStream, TarFormat.V7, leaveOpen: true))
            {
                V7TarEntry hardLink = new V7TarEntry(TarEntryType.HardLink, InitialEntryName);
                SetHardLink(hardLink);
                VerifyHardLink(hardLink);
                writer.WriteEntry(hardLink);
            }

            archiveStream.Position = 0;
            using (TarReader reader = new TarReader(archiveStream))
            {
                V7TarEntry hardLink = reader.GetNextEntry() as V7TarEntry;
                VerifyHardLink(hardLink);
            }
        }
コード例 #13
0
        public async Task WriteDirectory_Async()
        {
            using MemoryStream archiveStream = new MemoryStream();
            await using (TarWriter writer = new TarWriter(archiveStream, TarEntryFormat.V7, leaveOpen: true))
            {
                V7TarEntry directory = new V7TarEntry(TarEntryType.Directory, InitialEntryName);
                SetDirectory(directory);
                VerifyDirectory(directory);
                await writer.WriteEntryAsync(directory);
            }

            archiveStream.Position = 0;
            await using (TarReader reader = new TarReader(archiveStream))
            {
                V7TarEntry directory = await reader.GetNextEntryAsync() as V7TarEntry;

                VerifyDirectory(directory);
            }
        }
コード例 #14
0
        public async Task WriteSymbolicLink_Async()
        {
            using MemoryStream archiveStream = new MemoryStream();
            await using (TarWriter writer = new TarWriter(archiveStream, TarEntryFormat.V7, leaveOpen: true))
            {
                V7TarEntry symbolicLink = new V7TarEntry(TarEntryType.SymbolicLink, InitialEntryName);
                SetSymbolicLink(symbolicLink);
                VerifySymbolicLink(symbolicLink);
                await writer.WriteEntryAsync(symbolicLink);
            }

            archiveStream.Position = 0;
            await using (TarReader reader = new TarReader(archiveStream))
            {
                V7TarEntry symbolicLink = await reader.GetNextEntryAsync() as V7TarEntry;

                VerifySymbolicLink(symbolicLink);
            }
        }
コード例 #15
0
        public void Write_V7RegularFileEntry_As_RegularFileEntry()
        {
            using MemoryStream archive = new MemoryStream();
            using (TarWriter writer = new TarWriter(archive, archiveFormat: TarEntryFormat.Pax, leaveOpen: true))
            {
                V7TarEntry entry = new V7TarEntry(TarEntryType.V7RegularFile, InitialEntryName);

                // Should be written as RegularFile
                writer.WriteEntry(entry);
            }

            archive.Seek(0, SeekOrigin.Begin);
            using (TarReader reader = new TarReader(archive))
            {
                PaxTarEntry entry = reader.GetNextEntry() as PaxTarEntry;
                Assert.NotNull(entry);
                Assert.Equal(TarEntryType.RegularFile, entry.EntryType);

                Assert.Null(reader.GetNextEntry());
            }
        }
コード例 #16
0
        public async Task WriteRegularFile_Async()
        {
            using MemoryStream archiveStream = new MemoryStream();
            TarWriter writer = new TarWriter(archiveStream, TarEntryFormat.V7, leaveOpen: true);

            await using (writer)
            {
                V7TarEntry oldRegularFile = new V7TarEntry(TarEntryType.V7RegularFile, InitialEntryName);
                SetRegularFile(oldRegularFile);
                VerifyRegularFile(oldRegularFile, isWritable: true);
                await writer.WriteEntryAsync(oldRegularFile);
            }

            archiveStream.Position = 0;
            TarReader reader = new TarReader(archiveStream);

            await using (reader)
            {
                V7TarEntry oldRegularFile = await reader.GetNextEntryAsync() as V7TarEntry;

                VerifyRegularFile(oldRegularFile, isWritable: false);
            }
        }
コード例 #17
0
        public async Task WriteHardLink_Async()
        {
            using MemoryStream archiveStream = new MemoryStream();
            TarWriter writer = new TarWriter(archiveStream, TarEntryFormat.V7, leaveOpen: true);

            await using (writer)
            {
                V7TarEntry hardLink = new V7TarEntry(TarEntryType.HardLink, InitialEntryName);
                SetHardLink(hardLink);
                VerifyHardLink(hardLink);
                await writer.WriteEntryAsync(hardLink);
            }

            archiveStream.Position = 0;
            TarReader reader = new TarReader(archiveStream);

            await using (reader)
            {
                V7TarEntry hardLink = await reader.GetNextEntryAsync() as V7TarEntry;

                VerifyHardLink(hardLink);
            }
        }
コード例 #18
0
 protected void SetDirectory(V7TarEntry directory) => SetCommonDirectory(directory);
コード例 #19
0
 protected void SetSymbolicLink(V7TarEntry symbolicLink) => SetCommonSymbolicLink(symbolicLink);
コード例 #20
0
 protected void VerifyRegularFile(V7TarEntry regularFile, bool isWritable) => VerifyCommonRegularFile(regularFile, isWritable, isV7RegularFile: true);
コード例 #21
0
 protected void VerifyDirectory(V7TarEntry directory) => VerifyCommonDirectory(directory);
コード例 #22
0
 protected void VerifyHardLink(V7TarEntry hardLink) => VerifyCommonHardLink(hardLink);
コード例 #23
0
 protected void VerifySymbolicLink(V7TarEntry symbolicLink) => VerifyCommonSymbolicLink(symbolicLink);
コード例 #24
0
 protected void SetRegularFile(V7TarEntry regularFile) => SetCommonRegularFile(regularFile, isV7RegularFile: true);
コード例 #25
0
 protected void SetHardLink(V7TarEntry hardLink) => SetCommonHardLink(hardLink);