Exemple #1
0
        public void ReadTarFileTest()
        {
            using (Stream stream = File.OpenRead("Deb/libplist3_1.12-3.1_amd64.deb"))
                using (ArFile arFile = new ArFile(stream, leaveOpen: true))
                {
                    // Skip the debian version
                    arFile.Read();

                    // This is the tar file
                    arFile.Read();

                    Collection <string>    filenames = new Collection <string>();
                    Collection <string>    contents  = new Collection <string>();
                    Collection <TarHeader> headers   = new Collection <TarHeader>();

                    using (Stream entryStream = arFile.Open())
                        using (GZipDecompressor decompressedStream = new GZipDecompressor(entryStream, leaveOpen: true))
                            using (TarFile tarFile = new TarFile(decompressedStream, leaveOpen: true))
                            {
                                while (tarFile.Read())
                                {
                                    filenames.Add(tarFile.FileName);
                                    headers.Add((TarHeader)tarFile.FileHeader);

                                    using (Stream data = tarFile.Open())
                                        using (StreamReader reader = new StreamReader(data))
                                        {
                                            contents.Add(reader.ReadToEnd());
                                        }
                                }
                            }
                }
        }
Exemple #2
0
        public void WriteTarFileTest()
        {
            using (Stream original = File.OpenRead(@"IO/test.tar"))
                using (Stream expected = File.OpenRead(@"IO/test.tar"))
                    using (Stream actual = new MemoryStream())
                        using (Stream output = new ValidatingCompositeStream(null, actual, expected))
                        {
                            var tar = new TarFile(original, true);
                            while (tar.Read())
                            {
                                Stream data = new MemoryStream();
                                if (tar.FileHeader.FileMode == LinuxFileMode.S_IFDIR)
                                {
                                    tar.Skip();
                                }
                                else
                                {
                                    data = tar.Open();
                                }

                                var clonedHeader = this.CloneHeader((TarHeader)tar.FileHeader);
                                this.AssertCompareClonedHeader((TarHeader)tar.FileHeader, clonedHeader);
                                using (data)
                                {
                                    TarFileCreator.WriteEntry(output, this.CloneHeader(clonedHeader), data);
                                }
                            }

                            TarFileCreator.WriteTrailer(output);
                        }
        }
        public void ReadDebPackageTest()
        {
            using (Stream stream = File.OpenRead("Deb/libplist3_1.12-3.1_amd64.deb"))
            {
                var package = DebPackageReader.Read(stream);

                Assert.Equal(new Version(2, 0), package.PackageFormatVersion);

                Assert.NotNull(package.ControlFile);
                Assert.Equal(13, package.ControlFile.Count);

                Assert.Equal("libplist3", package.ControlFile["Package"]);
                Assert.Equal("libplist", package.ControlFile["Source"]);
                Assert.Equal("1.12-3.1", package.ControlFile["Version"]);
                Assert.Equal("amd64", package.ControlFile["Architecture"]);

                stream.Seek(0, SeekOrigin.Begin);
                using (var payload = DebPackageReader.GetPayloadStream(stream))
                    using (var tarFile = new TarFile(payload, leaveOpen: true))
                    {
                        while (tarFile.Read())
                        {
                            var tarHeader = (TarHeader)tarFile.FileHeader;
                            Assert.Equal(tarHeader.Checksum, tarHeader.ComputeChecksum());
                            if (tarHeader.TypeFlag != TarTypeFlag.RegType)
                            {
                                tarFile.Skip();
                            }
                            else
                            {
                                var fname = tarFile.FileName;
                                Assert.StartsWith("./", fname);
                                fname = fname.Substring(2);
                                if (!package.Md5Sums.TryGetValue(fname, out var sum))
                                {
                                    throw new Exception($"Checksum for {fname} not found");
                                }

                                string hash;
                                using (var fileStream = tarFile.Open())
                                    using (var md5 = MD5.Create())
                                        hash = BitConverter.ToString(md5.ComputeHash(fileStream)).Replace("-", string.Empty)
                                               .ToLower();
                                Assert.Equal(sum, hash);
                            }
                        }
                    }
            }
        }
        private static async Task ReadControlArchive(ArFile archive, DebPackage package)
        {
            package.ControlExtras = new Dictionary <string, DebPackageControlFileData>();
            package.Md5Sums       = new Dictionary <string, string>();
            using (Stream stream = archive.Open())
                using (GZipDecompressor decompressedStream = new GZipDecompressor(stream, leaveOpen: true))
                    using (TarFile tarFile = new TarFile(decompressedStream, leaveOpen: true))
                    {
                        while (tarFile.Read())
                        {
                            switch (tarFile.FileName)
                            {
                            case "./control":
                            case "control":
                                using (Stream controlFile = tarFile.Open())
                                {
                                    package.ControlFile = ControlFileParser.Read(controlFile);
                                }

                                break;

                            case "./md5sums":
                                using (var sums = new StreamReader(tarFile.Open()))
                                {
                                    string line;
                                    while ((line = await sums.ReadLineAsync()) != null)
                                    {
                                        var s = line.Split(new[] { "  " }, 2, StringSplitOptions.None);
                                        package.Md5Sums[s[1]] = s[0];
                                    }
                                }

                                break;

                            case "./preinst":
                                package.PreInstallScript = tarFile.ReadAsUtf8String();
                                break;

                            case "./postinst":
                                package.PostInstallScript = tarFile.ReadAsUtf8String();
                                break;

                            case "./prerm":
                                package.PreRemoveScript = tarFile.ReadAsUtf8String();
                                break;

                            case "./postrm":
                                package.PostRemoveScript = tarFile.ReadAsUtf8String();
                                break;

                            case "./":
                                tarFile.Skip();
                                break;

                            default:
                                package.ControlExtras[tarFile.FileName] = new DebPackageControlFileData
                                {
                                    Mode     = tarFile.FileHeader.FileMode,
                                    Contents = tarFile.ReadAsUtf8String()
                                };
                                break;
                            }
                        }
                    }
        }