Exemple #1
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 WriteTarWithLongFilenameTest()
        {
            using (Stream expected = File.OpenRead(@"IO/largefilename.tar"))
                using (Stream actual = new MemoryStream())
                    using (Stream output = new ValidatingCompositeStream(null, actual, expected))
                    {
                        var directories = new string[]
                        {
                            "./",
                            "./usr/",
                            "./usr/lib/",
                            "./usr/lib/mono/",
                            "./usr/lib/mono/gac/",
                            "./usr/lib/mono/gac/System.Runtime.InteropServices.RuntimeInformation/",
                            "./usr/lib/mono/gac/System.Runtime.InteropServices.RuntimeInformation/4.0.0.0__b03f5f7f11d50a3a/",
                        };

                        foreach (var directory in directories)
                        {
                            TarFileCreator.WriteEntry(
                                output,
                                new ArchiveEntry()
                            {
                                Mode       = LinuxFileMode.S_IXOTH | LinuxFileMode.S_IROTH | LinuxFileMode.S_IXGRP | LinuxFileMode.S_IRGRP | LinuxFileMode.S_IXUSR | LinuxFileMode.S_IWUSR | LinuxFileMode.S_IRUSR | LinuxFileMode.S_IFDIR,
                                TargetPath = directory,
                                Group      = "root",
                                Owner      = "root",
                                Modified   = new DateTimeOffset(2016, 12, 15, 10, 58, 56, TimeSpan.Zero)
                            },
                                null);
                        }

                        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("This file has a very long name.")))
                        {
                            TarFileCreator.WriteEntry(
                                output,
                                new ArchiveEntry()
                            {
                                Mode       = (LinuxFileMode)0x81FF,
                                TargetPath = "./usr/lib/mono/gac/System.Runtime.InteropServices.RuntimeInformation/4.0.0.0__b03f5f7f11d50a3a/System.Runtime.InteropServices.RuntimeInformation.txt",
                                Modified   = new DateTimeOffset(2017, 11, 11, 12, 37, 58, TimeSpan.Zero)
                            },
                                stream);
                        }

                        TarFileCreator.WriteTrailer(output);
                    }
        }
Exemple #3
0
        private static void WriteControl(Stream targetStream, DebPackage pkg, List <ArchiveEntry> entries)
        {
            var controlTar = new MemoryStream();

            WriteControlEntry(controlTar, "./");
            WriteControlEntry(
                controlTar,
                "./control",
                string.Join("\n", pkg.ControlFile
                            .OrderByDescending(x => x.Key == "Package").ThenBy(x => x.Key)
                            .Select(x => $"{x.Key}: {x.Value}")) + "\n");

            WriteControlEntry(
                controlTar,
                "./md5sums",
                string.Join("\n", pkg.Md5Sums.Select(x => $"{x.Value}  {x.Key}")) + "\n");

            var execMode = LinuxFileMode.S_IRUSR | LinuxFileMode.S_IWUSR | LinuxFileMode.S_IXUSR |
                           LinuxFileMode.S_IRGRP | LinuxFileMode.S_IROTH;

            if (!string.IsNullOrWhiteSpace(pkg.PreInstallScript))
            {
                WriteControlEntry(controlTar, "./preinst", $"#!/bin/sh\n{pkg.PreInstallScript}\n", execMode);
            }

            if (!string.IsNullOrWhiteSpace(pkg.PostInstallScript))
            {
                WriteControlEntry(controlTar, "./postinst", $"#!/bin/sh\n{pkg.PostInstallScript}\n", execMode);
            }

            if (!string.IsNullOrWhiteSpace(pkg.PreRemoveScript))
            {
                WriteControlEntry(controlTar, "./prerm", $"#!/bin/sh\n{pkg.PreRemoveScript}\n", execMode);
            }

            if (!string.IsNullOrWhiteSpace(pkg.PostRemoveScript))
            {
                WriteControlEntry(controlTar, "./postrm", $"#!/bin/sh\n{pkg.PostRemoveScript}\n", execMode);
            }

            var confFiles = entries
                            .Where(e => e.Mode.HasFlag(LinuxFileMode.S_IFREG) && e.TargetPath.StartsWith("/etc/"))
                            .Select(e => e.TargetPath).ToList();

            if (confFiles.Any())
            {
                WriteControlEntry(controlTar, "./conffiles", string.Join("\n", confFiles) + "\n");
            }

            TarFileCreator.WriteTrailer(controlTar);
            controlTar.Seek(0, SeekOrigin.Begin);

            var controlTarGz = new MemoryStream();

            using (var gzStream = new GZipStream(controlTarGz, CompressionMode.Compress, true))
            {
                controlTar.CopyTo(gzStream);
            }

            controlTarGz.Seek(0, SeekOrigin.Begin);
            ArFileCreator.WriteEntry(targetStream, "control.tar.gz", ArFileMode, controlTarGz);
        }