public DateTime GetTime(TarHeader.HeaderField field)
        {
            string paxValue = GetPaxValue(field.PaxAttribute);

            if (paxValue != null)
            {
                return(TarTime.FromPaxTime(paxValue));
            }

            long unixTime = GetOctalLong(field.WithoutPax);

            if (unixTime < TarTime.MinUnixTime)
            {
                unixTime = TarTime.MinUnixTime;
            }
            else if (unixTime > TarTime.MaxUnixTime)
            {
                unixTime = TarTime.MaxUnixTime;
            }

            return(TarTime.FromUnixTime(unixTime, 0));
        }
        public bool TryPutTime(DateTime time, TarHeader.HeaderField field)
        {
            uint nanoseconds = 0;
            long unixTime    = 0;

            if (time.Ticks != 0)
            {
                unixTime = TarTime.ToUnixTime(time, out nanoseconds);
            }

            if (TryPutOctal(unixTime, field.WithoutPax) && nanoseconds == 0)
            {
                return(true);
            }

            if (field.PaxAttribute != null && PaxAttributes != null)
            {
                PaxAttributes[field.PaxAttribute] = TarTime.ToPaxTime(time);
            }

            return(false);
        }
Example #3
0
        private static TarEntry ParseHeader(ref TarHeaderView header)
        {
            var entry = new TarEntry
            {
                Name         = header.GetString(TarHeader.Name),
                Mode         = header.GetOctal(TarHeader.Mode),
                UserID       = header.GetOctal(TarHeader.UserID),
                GroupID      = header.GetOctal(TarHeader.GroupID),
                Length       = header.GetOctalLong(TarHeader.Length),
                ModifiedTime = header.GetTime(TarHeader.ModifiedTime)
            };

            var checksum = header.GetOctal(TarHeader.Checksum);
            int signedChecksum;
            var unsignedChecksum = TarCommon.Checksum(header.Field(TarHeader.FullHeader), out signedChecksum);

            if (checksum != signedChecksum && checksum != unsignedChecksum)
            {
                throw new TarParseException("invalid tar checksum");
            }

            var typeFlag = header[TarHeader.TypeFlag.Offset];

            if (typeFlag == 0)
            {
                typeFlag = (byte)'0';
            }

            entry.Type = (TarEntryType)typeFlag;

            entry.LinkTarget = header.GetString(TarHeader.LinkTarget);
            var magic = header.GetString(TarHeader.FullMagic);

            if (magic == TarCommon.PosixMagic || magic == TarCommon.GnuMagic)
            {
                entry.UserName    = header.GetString(TarHeader.UserName);
                entry.GroupName   = header.GetString(TarHeader.GroupName);
                entry.DeviceMajor = header.GetOctal(TarHeader.DeviceMajor);
                entry.DeviceMinor = header.GetOctal(TarHeader.DeviceMinor);
                if (magic == TarCommon.PosixMagic)
                {
                    if (header.PaxAttributes == null || !header.PaxAttributes.ContainsKey(TarHeader.Name.PaxAttribute))
                    {
                        var prefix = header.GetString(TarHeader.Prefix);
                        if (prefix.Length > 0)
                        {
                            entry.Name = prefix + "/" + entry.Name;
                        }
                    }

                    string atime = header.GetPaxValue(TarCommon.PaxAtime);
                    if (atime != null)
                    {
                        entry.AccessTime = TarTime.FromPaxTime(atime);
                    }

                    string ctime = header.GetPaxValue(TarCommon.PaxCtime);
                    if (ctime != null)
                    {
                        entry.ChangeTime = TarTime.FromPaxTime(ctime);
                    }

                    string creationtime = header.GetPaxValue(TarCommon.PaxCreationTime);
                    if (creationtime != null)
                    {
                        entry.CreationTime = TarTime.FromPaxTime(creationtime);
                    }

                    string fileAttributes = header.GetPaxValue(TarCommon.PaxWindowsFileAttributes);
                    if (fileAttributes != null)
                    {
                        entry.FileAttributes = (FileAttributes)Convert.ToUInt32(fileAttributes);
                    }

                    entry.SecurityDescriptor = header.GetPaxValue(TarCommon.PaxWindowsSecurityDescriptor);
                    if (header.GetPaxValue(TarCommon.PaxWindowsMountPoint) != null)
                    {
                        entry.IsMountPoint = true;
                    }
                }
            }

            return(entry);
        }
Example #4
0
        private static void GenerateHeader(ref TarHeaderView header, TarEntry entry, ArraySegment <byte> name)
        {
            var needsPath = false;

            header.PutNul(TarHeader.FullHeader);

            // Try to write the name, but don't write the PAX attribute yet in case we can
            // just write a split name later.
            if (name.Count > 0)
            {
                header.PutBytes(name, TarHeader.Name);
            }
            else if (!header.TryPutString(entry.Name, TarHeader.Name.WithoutPax))
            {
                needsPath = true;
            }

            header.TryPutOctal(entry.Mode, TarHeader.Mode);
            header.TryPutOctal(entry.UserID, TarHeader.UserID);
            header.TryPutOctal(entry.GroupID, TarHeader.GroupID);
            header.TryPutOctal(entry.Length, TarHeader.Length);
            header.TryPutTime(entry.ModifiedTime, TarHeader.ModifiedTime);

            header[TarHeader.ChecksumSpace.Offset] = (byte)' ';

            header[TarHeader.TypeFlag.Offset] = (byte)entry.Type;

            header.TryPutString(entry.LinkTarget, TarHeader.LinkTarget);

            header.TryPutString(TarCommon.PosixMagic, TarHeader.Magic);
            header[TarHeader.Version.Offset]     = (byte)'0';
            header[TarHeader.Version.Offset + 1] = (byte)'0';

            header.TryPutString(entry.UserName, TarHeader.UserName);
            header.TryPutString(entry.GroupName, TarHeader.GroupName);
            header.TryPutOctal(entry.DeviceMajor, TarHeader.DeviceMajor);
            header.TryPutOctal(entry.DeviceMinor, TarHeader.DeviceMinor);

            if (header.PaxAttributes != null)
            {
                if (entry.AccessTime.HasValue)
                {
                    header.PaxAttributes[TarCommon.PaxAtime] = TarTime.ToPaxTime(entry.AccessTime.Value);
                }

                if (entry.ChangeTime.HasValue)
                {
                    header.PaxAttributes[TarCommon.PaxCtime] = TarTime.ToPaxTime(entry.ChangeTime.Value);
                }

                if (entry.CreationTime.HasValue)
                {
                    header.PaxAttributes[TarCommon.PaxCreationTime] = TarTime.ToPaxTime(entry.CreationTime.Value);
                }

                if (entry.FileAttributes.HasValue)
                {
                    header.PaxAttributes[TarCommon.PaxWindowsFileAttributes] = Convert.ToString((uint)entry.FileAttributes.Value);
                }

                if (entry.IsMountPoint)
                {
                    header.PaxAttributes[TarCommon.PaxWindowsMountPoint] = "1";
                }

                if (entry.SecurityDescriptor != null)
                {
                    header.PaxAttributes[TarCommon.PaxWindowsSecurityDescriptor] = entry.SecurityDescriptor;
                }

                if (needsPath)
                {
                    int splitIndex;
                    if (header.PaxAttributes.Count == 0 && TrySplitPath(entry.Name, out splitIndex))
                    {
                        header.TryPutString(entry.Name.Substring(0, splitIndex), TarHeader.Prefix);
                        header.TryPutString(entry.Name.Substring(splitIndex + 1), TarHeader.Name);
                    }
                    else
                    {
                        header.PaxAttributes[TarHeader.Name.PaxAttribute] = entry.Name;
                    }
                }
            }

            int signedChecksum;
            var checksum = TarCommon.Checksum(header.Field(TarHeader.FullHeader), out signedChecksum);

            header.TryPutOctal(checksum, TarHeader.Checksum);
        }