/// <summary>
 /// Convenience method that will modify an entry's name directly
 /// in place in an entry header buffer byte array.
 /// </summary>
 /// <param name="buffer">
 /// The buffer containing the entry header to modify.
 /// </param>
 /// <param name="newName">
 /// The new name to place into the header buffer.
 /// </param>
 static public void AdjustEntryName(byte[] buffer, string newName)
 {
     TarHeader.GetNameBytes(newName, buffer, 0, TarHeader.NAMELEN);
 }
        /// <summary>
        /// Fill in a TarHeader with information from a File.
        /// </summary>
        /// <param name="header">
        /// The TarHeader to fill in.
        /// </param>
        /// <param name="file">
        /// The file from which to get the header information.
        /// </param>
        public void GetFileTarHeader(TarHeader header, string file)
        {
            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }

            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            this.file = file;

            // bugfix from torhovl from #D forum:
            string name = file;

            // 23-Jan-2004 GnuTar allows device names in path where the name is not local to the current directory
            if (name.IndexOf(Directory.GetCurrentDirectory(), StringComparison.Ordinal) == 0)
            {
                name = name.Substring(Directory.GetCurrentDirectory().Length);
            }

            /*
             *                      if (Path.DirectorySeparatorChar == '\\')
             *                      {
             *                              // check if the OS is Windows
             *                              // Strip off drive letters!
             *                              if (name.Length > 2)
             *                              {
             *                                      char ch1 = name[0];
             *                                      char ch2 = name[1];
             *
             *                                      if (ch2 == ':' && Char.IsLetter(ch1))
             *                                      {
             *                                              name = name.Substring(2);
             *                                      }
             *                              }
             *                      }
             */

            name = name.Replace(Path.DirectorySeparatorChar, '/');

            // No absolute pathnames
            // Windows (and Posix?) paths can start with UNC style "\\NetworkDrive\",
            // so we loop on starting /'s.
            while (name.StartsWith("/", StringComparison.Ordinal))
            {
                name = name.Substring(1);
            }

            header.LinkName = String.Empty;
            header.Name     = name;

            if (Directory.Exists(file))
            {
                header.Mode     = 1003;             // Magic number for security access for a UNIX filesystem
                header.TypeFlag = TarHeader.LF_DIR;
                if ((header.Name.Length == 0) || header.Name[header.Name.Length - 1] != '/')
                {
                    header.Name = header.Name + "/";
                }

                header.Size = 0;
            }
            else
            {
                header.Mode     = 33216;             // Magic number for security access for a UNIX filesystem
                header.TypeFlag = TarHeader.LF_NORMAL;
                header.Size     = new FileInfo(file.Replace('/', Path.DirectorySeparatorChar)).Length;
            }

            header.ModTime  = System.IO.File.GetLastWriteTime(file.Replace('/', Path.DirectorySeparatorChar)).ToUniversalTime();
            header.DevMajor = 0;
            header.DevMinor = 0;
        }
 /// <summary>
 /// Initialise a default instance of <see cref="TarEntry"/>.
 /// </summary>
 private TarEntry()
 {
     header = new TarHeader();
 }
Beispiel #4
0
        /// <summary>
        /// Parse TarHeader information from a header buffer.
        /// </summary>
        /// <param name = "header">
        /// The tar entry header buffer to get information from.
        /// </param>
        public void ParseBuffer(byte[] header)
        {
            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }

            int offset = 0;

            name    = ParseName(header, offset, NAMELEN).ToString();
            offset += NAMELEN;

            mode    = (int)ParseOctal(header, offset, MODELEN);
            offset += MODELEN;

            UserId  = (int)ParseOctal(header, offset, UIDLEN);
            offset += UIDLEN;

            GroupId = (int)ParseOctal(header, offset, GIDLEN);
            offset += GIDLEN;

            Size    = ParseBinaryOrOctal(header, offset, SIZELEN);
            offset += SIZELEN;

            ModTime = GetDateTimeFromCTime(ParseOctal(header, offset, MODTIMELEN));
            offset += MODTIMELEN;

            checksum = (int)ParseOctal(header, offset, CHKSUMLEN);
            offset  += CHKSUMLEN;

            TypeFlag = header[offset++];

            LinkName = ParseName(header, offset, NAMELEN).ToString();
            offset  += NAMELEN;

            Magic   = ParseName(header, offset, MAGICLEN).ToString();
            offset += MAGICLEN;

            if (Magic == "ustar")
            {
                Version = ParseName(header, offset, VERSIONLEN).ToString();
                offset += VERSIONLEN;

                UserName = ParseName(header, offset, UNAMELEN).ToString();
                offset  += UNAMELEN;

                GroupName = ParseName(header, offset, GNAMELEN).ToString();
                offset   += GNAMELEN;

                DevMajor = (int)ParseOctal(header, offset, DEVLEN);
                offset  += DEVLEN;

                DevMinor = (int)ParseOctal(header, offset, DEVLEN);
                offset  += DEVLEN;

                string prefix = ParseName(header, offset, PREFIXLEN).ToString();
                if (!string.IsNullOrEmpty(prefix))
                {
                    Name = prefix + '/' + Name;
                }
            }

            isChecksumValid = Checksum == TarHeader.MakeCheckSum(header);
        }