Esempio n. 1
0
        public static Collection <string> Read(RpmPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }

            Collection <string> names = new Collection <string>();

            using (var payloadDecompressedStream = GetDecompressedPayloadStream(package))
                using (CpioFile file = new CpioFile(payloadDecompressedStream, leaveOpen: true))
                {
                    while (file.Read())
                    {
                        names.Add(file.FileName);

                        using (Stream stream = file.Open())
                        {
                            byte[] data = new byte[stream.Length];
                            stream.Read(data, 0, data.Length);
                        }
                    }
                }

            return(names);
        }
Esempio n. 2
0
        public void OpenCpioFileTests()
        {
            Collection <string>     entryNames   = new Collection <string>();
            Collection <CpioHeader> entryHeaders = new Collection <CpioHeader>();
            Collection <string>     entryHashes  = new Collection <string>();

            SHA256 sha = SHA256.Create();

            using (Stream stream = File.OpenRead(@"IO/test.cpio"))
                using (CpioFile file = new CpioFile(stream, true))
                {
                    while (file.Read())
                    {
                        entryNames.Add(file.FileName);
                        entryHeaders.Add(file.EntryHeader);

                        using (Stream entry = file.Open())
                        {
                            entryHashes.Add(BitConverter.ToString(sha.ComputeHash(entry)).Replace("-", string.Empty).ToLower());
                        }
                    }
                }

            Assert.Equal(3, entryHashes.Count);
            Assert.Equal(3, entryHeaders.Count);
            Assert.Equal(3, entryHashes.Count);

            Assert.Equal(".", entryNames[0]);
            Assert.Equal(0u, entryHeaders[0].Check);
            Assert.Equal(0xfcu, entryHeaders[0].DevMajor);
            Assert.Equal(0u, entryHeaders[0].DevMinor);
            Assert.Equal(0u, entryHeaders[0].FileSize);
            Assert.Equal(0u, entryHeaders[0].Gid);
            Assert.Equal(0x24fafu, entryHeaders[0].Ino);
            Assert.Equal((LinuxFileMode)0x41edu, entryHeaders[0].FileMode);
            Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(0x471c8630u), entryHeaders[0].LastModified);
            Assert.Equal(0x02u, entryHeaders[0].NameSize);
            Assert.Equal(0x8u, entryHeaders[0].Nlink);
            Assert.Equal(0u, entryHeaders[0].RDevMajor);
            Assert.Equal(0u, entryHeaders[0].RDevMinor);
            Assert.Equal("070701", entryHeaders[0].Signature);
            Assert.Equal(0u, entryHeaders[0].Uid);

            Assert.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", entryHashes[0]);
            Assert.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", entryHashes[1]);
            Assert.Equal("811ee67ae433927d702c675c53dfe53811b1479aa9eedd91666b548a4797a7bc", entryHashes[2]);
        }
Esempio n. 3
0
        public void WriteCpioFileTests()
        {
            using (Stream stream = File.OpenRead(@"IO/test.cpio"))
                using (CpioFile source = new CpioFile(stream, true))
                    using (MemoryStream output = new MemoryStream())
                        using (Stream expectedOutput = File.OpenRead(@"IO/test.cpio"))
                            using (Stream validatingOutput = new ValidatingCompositeStream(null, output, expectedOutput))
                                using (CpioFile target = new CpioFile(validatingOutput, true))
                                {
                                    int index = 0;

                                    while (source.Read())
                                    {
                                        using (Stream file = source.Open())
                                        {
                                            target.Write(source.EntryHeader, source.FileName, file);
                                            index++;
                                        }
                                    }

                                    target.WriteTrailer();
                                }
        }
        /// <summary>
        /// Extracts the <see cref="ArchiveEntry"/> objects from a CPIO file.
        /// </summary>
        /// <param name="file">
        /// The CPIO file from which to extract the entries.
        /// </param>
        /// <returns>
        /// A list of <see cref="ArchiveEntry"/> objects representing the data in the CPIO file.
        /// </returns>
        public List <ArchiveEntry> FromCpio(CpioFile file)
        {
            List <ArchiveEntry> value = new List <ArchiveEntry>();

            byte[] buffer     = new byte[1024];
            byte[] fileHeader = null;

            while (file.Read())
            {
                fileHeader = null;

                ArchiveEntry entry = new ArchiveEntry()
                {
                    FileSize       = file.EntryHeader.FileSize,
                    Group          = "root",
                    Owner          = "root",
                    Inode          = file.EntryHeader.Ino,
                    Mode           = file.EntryHeader.FileMode,
                    Modified       = file.EntryHeader.LastModified,
                    TargetPath     = file.FileName,
                    Type           = ArchiveEntryType.None,
                    LinkTo         = string.Empty,
                    Sha256         = Array.Empty <byte>(),
                    SourceFilename = null,
                    IsAscii        = true
                };

                if (entry.Mode.HasFlag(LinuxFileMode.S_IFREG) && !entry.Mode.HasFlag(LinuxFileMode.S_IFLNK))
                {
                    using (var fileStream = file.Open())
                        using (var hasher = IncrementalHash.CreateHash(HashAlgorithmName.SHA256))
                        {
                            int read;

                            while (true)
                            {
                                read = fileStream.Read(buffer, 0, buffer.Length);

                                if (fileHeader == null)
                                {
                                    fileHeader = new byte[read];
                                    Buffer.BlockCopy(buffer, 0, fileHeader, 0, read);
                                }

                                hasher.AppendData(buffer, 0, read);
                                entry.IsAscii = entry.IsAscii && fileHeader.All(c => c < 128);

                                if (read < buffer.Length)
                                {
                                    break;
                                }
                            }

                            entry.Sha256 = hasher.GetHashAndReset();
                        }

                    entry.Type = this.GetArchiveEntryType(fileHeader);
                }
                else if (entry.Mode.HasFlag(LinuxFileMode.S_IFLNK))
                {
                    using (var fileStrema = file.Open())
                        using (var reader = new StreamReader(fileStrema, Encoding.UTF8))
                        {
                            entry.LinkTo = reader.ReadToEnd();
                        }
                }
                else
                {
                    file.Skip();
                }

                if (entry.Mode.HasFlag(LinuxFileMode.S_IFDIR))
                {
                    entry.FileSize = 0x1000;
                }

                if (entry.TargetPath.StartsWith("."))
                {
                    entry.TargetPath = entry.TargetPath.Substring(1);
                }

                value.Add(entry);
            }

            return(value);
        }