Esempio n. 1
0
 public void PutNextEntry(TarEntry entry)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     if (entry.TarHeader.Name.Length >= 100)
     {
         TarHeader header = new TarHeader
         {
             TypeFlag = 0x4c,  
             UserId = 0, GroupId = 0, 
             GroupName = "", 
             UserName = "", 
             LinkName = "", 
             Size = entry.TarHeader.Name.Length
         };
         header.Name += "././@LongLink";
         header.WriteHeader(_blockBuffer);
         _buffer.WriteBlock(_blockBuffer);
         int nameOffset = 0;
         while (nameOffset < entry.TarHeader.Name.Length)
         {
             Array.Clear(_blockBuffer, 0, _blockBuffer.Length);
             TarHeader.GetAsciiBytes(entry.TarHeader.Name, nameOffset, _blockBuffer, 0, 0x200);
             nameOffset += 0x200;
             _buffer.WriteBlock(_blockBuffer);
         }
     }
     entry.WriteEntryHeader(_blockBuffer);
     _buffer.WriteBlock(_blockBuffer);
     _currBytes = 0L;
     _currSize = entry.IsDirectory ? 0L : entry.Size;
 }
Esempio n. 2
0
 public TarEntry[] GetDirectoryEntries()
 {
     if ((_file == null) || !Directory.Exists(_file))
     {
         return new TarEntry[0];
     }
     string[] fileSystemEntries = Directory.GetFileSystemEntries(_file);
     TarEntry[] entryArray = new TarEntry[fileSystemEntries.Length];
     for (int i = 0; i < fileSystemEntries.Length; i++)
     {
         entryArray[i] = CreateEntryFromFile(fileSystemEntries[i]);
     }
     return entryArray;
 }
Esempio n. 3
0
 public TarEntry GetNextEntry()
 {
     if (_hasHitEof)
     {
         return(null);
     }
     if (_currentEntry != null)
     {
         SkipToNextEntry();
     }
     byte[] block = _tarBuffer.ReadBlock();
     if (block == null)
     {
         _hasHitEof = true;
     }
     else if (TarBuffer.IsEndOfArchiveBlock(block))
     {
         _hasHitEof = true;
     }
     if (_hasHitEof)
     {
         _currentEntry = null;
     }
     else
     {
         try
         {
             TarHeader header = new TarHeader();
             header.ParseBuffer(block);
             if (!header.IsChecksumValid)
             {
                 throw new TarException("Header checksum is invalid");
             }
             _entryOffset = 0L;
             _entrySize   = header.Size;
             StringBuilder builder = null;
             if (header.TypeFlag == 0x4c)
             {
                 byte[] buffer = new byte[0x200];
                 long   size   = _entrySize;
                 builder = new StringBuilder();
                 while (size > 0L)
                 {
                     int length = Read(buffer, 0, (size > buffer.Length) ? buffer.Length : ((int)size));
                     if (length == -1)
                     {
                         throw new InvalidHeaderException("Failed to read long name entry");
                     }
                     builder.Append(TarHeader.ParseName(buffer, 0, length));
                     size -= length;
                 }
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             else if (header.TypeFlag == 0x67)
             {
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             else if (header.TypeFlag == 120)
             {
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             else if (header.TypeFlag == 0x56)
             {
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             else if (((header.TypeFlag != 0x30) && (header.TypeFlag != 0)) && (header.TypeFlag != 0x35))
             {
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             if (_entryFactory == null)
             {
                 _currentEntry = new TarEntry(block);
                 if (builder != null)
                 {
                     _currentEntry.Name = builder.ToString();
                 }
             }
             else
             {
                 _currentEntry = _entryFactory.CreateEntry(block);
             }
             _entryOffset = 0L;
             _entrySize   = _currentEntry.Size;
         }
         catch (InvalidHeaderException exception)
         {
             _entrySize    = 0L;
             _entryOffset  = 0L;
             _currentEntry = null;
             throw new InvalidHeaderException(string.Format("Bad header in record {0} block {1} {2}", _tarBuffer.CurrentRecord, _tarBuffer.CurrentBlock, exception.Message));
         }
     }
     return(_currentEntry);
 }
Esempio n. 4
0
 public TarEntry CreateEntryFromFile(string fileName)
 {
     return(TarEntry.CreateEntryFromFile(fileName));
 }
Esempio n. 5
0
 public TarEntry CreateEntry(string name)
 {
     return(TarEntry.CreateTarEntry(name));
 }
Esempio n. 6
0
 public TarEntry GetNextEntry()
 {
     if (_hasHitEof)
     {
         return null;
     }
     if (_currentEntry != null)
     {
         SkipToNextEntry();
     }
     byte[] block = _tarBuffer.ReadBlock();
     if (block == null)
     {
         _hasHitEof = true;
     }
     else if (TarBuffer.IsEndOfArchiveBlock(block))
     {
         _hasHitEof = true;
     }
     if (_hasHitEof)
     {
         _currentEntry = null;
     }
     else
     {
         try
         {
             TarHeader header = new TarHeader();
             header.ParseBuffer(block);
             if (!header.IsChecksumValid)
             {
                 throw new TarException("Header checksum is invalid");
             }
             _entryOffset = 0L;
             _entrySize = header.Size;
             StringBuilder builder = null;
             if (header.TypeFlag == 0x4c)
             {
                 byte[] buffer = new byte[0x200];
                 long size = _entrySize;
                 builder = new StringBuilder();
                 while (size > 0L)
                 {
                     int length = Read(buffer, 0, (size > buffer.Length) ? buffer.Length : ((int)size));
                     if (length == -1)
                     {
                         throw new InvalidHeaderException("Failed to read long name entry");
                     }
                     builder.Append(TarHeader.ParseName(buffer, 0, length));
                     size -= length;
                 }
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             else if (header.TypeFlag == 0x67)
             {
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             else if (header.TypeFlag == 120)
             {
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             else if (header.TypeFlag == 0x56)
             {
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             else if (((header.TypeFlag != 0x30) && (header.TypeFlag != 0)) && (header.TypeFlag != 0x35))
             {
                 SkipToNextEntry();
                 block = _tarBuffer.ReadBlock();
             }
             if (_entryFactory == null)
             {
                 _currentEntry = new TarEntry(block);
                 if (builder != null)
                 {
                     _currentEntry.Name = builder.ToString();
                 }
             }
             else
             {
                 _currentEntry = _entryFactory.CreateEntry(block);
             }
             _entryOffset = 0L;
             _entrySize = _currentEntry.Size;
         }
         catch (InvalidHeaderException exception)
         {
             _entrySize = 0L;
             _entryOffset = 0L;
             _currentEntry = null;
             throw new InvalidHeaderException(string.Format("Bad header in record {0} block {1} {2}", _tarBuffer.CurrentRecord, _tarBuffer.CurrentBlock, exception.Message));
         }
     }
     return _currentEntry;
 }
Esempio n. 7
0
 private void WriteEntryCore(TarEntry sourceEntry, bool recurse)
 {
     string path = null;
     string file = sourceEntry.File;
     TarEntry entry = (TarEntry)sourceEntry.Clone();
     if (_applyUserInfoOverrides)
     {
         entry.GroupId = _groupId;
         entry.GroupName = _groupName;
         entry.UserId = _userId;
         entry.UserName = _userName;
     }
     OnProgressMessageEvent(entry, null);
     if ((_asciiTranslate && !entry.IsDirectory) && !IsBinary(file))
     {
         path = Path.GetTempFileName();
         using (StreamReader reader = File.OpenText(file))
         {
             using (Stream stream = File.Create(path))
             {
                 Label_0088:
                 string str3 = reader.ReadLine();
                 if (str3 != null)
                 {
                     byte[] bytes = Encoding.ASCII.GetBytes(str3);
                     stream.Write(bytes, 0, bytes.Length);
                     stream.WriteByte(10);
                     goto Label_0088;
                 }
                 stream.Flush();
             }
         }
         entry.Size = new FileInfo(path).Length;
         file = path;
     }
     string str4 = null;
     if ((_rootPath != null) && entry.Name.StartsWith(_rootPath))
     {
         str4 = entry.Name.Substring(_rootPath.Length + 1);
     }
     if (_pathPrefix != null)
     {
         str4 = (str4 == null) ? (_pathPrefix + "/" + entry.Name) : (_pathPrefix + "/" + str4);
     }
     if (str4 != null)
     {
         entry.Name = str4;
     }
     _tarOut.PutNextEntry(entry);
     if (entry.IsDirectory)
     {
         if (recurse)
         {
             TarEntry[] directoryEntries = entry.GetDirectoryEntries();
             foreach (TarEntry t in directoryEntries)
             {
                 WriteEntryCore(t, true);
             }
             return;
         }
         return;
     }
     using (Stream stream2 = File.OpenRead(file))
     {
         byte[] buffer = new byte[0x8000];
         while (true)
         {
             int count = stream2.Read(buffer, 0, buffer.Length);
             if (count <= 0)
             {
                 goto Label_01F6;
             }
             _tarOut.Write(buffer, 0, count);
         }
     }
 Label_01F6:
     if (!string.IsNullOrEmpty(path))
     {
         File.Delete(path);
     }
     _tarOut.CloseEntry();
 }
Esempio n. 8
0
 public void WriteEntry(TarEntry sourceEntry, bool recurse)
 {
     if (sourceEntry == null)
     {
         throw new ArgumentNullException(nameof(sourceEntry));
     }
     if (_isDisposed)
     {
         throw new ObjectDisposedException("TarArchive");
     }
     try
     {
         if (recurse)
         {
             TarHeader.SetValueDefaults(sourceEntry.UserId, sourceEntry.UserName, sourceEntry.GroupId, sourceEntry.GroupName);
         }
         WriteEntryCore(sourceEntry, recurse);
     }
     finally
     {
         if (recurse)
         {
             TarHeader.RestoreSetValues();
         }
     }
 }
Esempio n. 9
0
 protected virtual void OnProgressMessageEvent(TarEntry entry, string message)
 {
     ProgressMessageHandler progressMessageEvent = ProgressMessageEvent;
     if (progressMessageEvent != null)
     {
         progressMessageEvent(this, entry, message);
     }
 }
Esempio n. 10
0
 private void ExtractEntry(string destDir, TarEntry entry)
 {
     int num;
     OnProgressMessageEvent(entry, null);
     string name = entry.Name;
     if (Path.IsPathRooted(name))
     {
         name = name.Substring(Path.GetPathRoot(name).Length);
     }
     name = name.Replace('/', Path.DirectorySeparatorChar);
     string directoryName = Path.Combine(destDir, name);
     if (entry.IsDirectory)
     {
         EnsureDirectoryExists(directoryName);
         return;
     }
     EnsureDirectoryExists(Path.GetDirectoryName(directoryName));
     bool flag = true;
     FileInfo info = new FileInfo(directoryName);
     if (info.Exists)
     {
         if (_keepOldFiles)
         {
             OnProgressMessageEvent(entry, "Destination file already exists");
             flag = false;
         }
         else if ((info.Attributes & FileAttributes.ReadOnly) != 0)
         {
             OnProgressMessageEvent(entry, "Destination file already exists, and is read-only");
             flag = false;
         }
     }
     if (!flag)
     {
         return;
     }
     bool flag2 = false;
     Stream stream = File.Create(directoryName);
     if (_asciiTranslate)
     {
         flag2 = !IsBinary(directoryName);
     }
     StreamWriter writer = null;
     if (flag2)
     {
         writer = new StreamWriter(stream);
     }
     byte[] buffer = new byte[0x8000];
 Label_00DF:
     num = _tarIn.Read(buffer, 0, buffer.Length);
     if (num > 0)
     {
         if (flag2)
         {
             int index = 0;
             for (int i = 0; i < num; i++)
             {
                 if (buffer[i] == 10)
                 {
                     string str4 = Encoding.ASCII.GetString(buffer, index, i - index);
                     writer.WriteLine(str4);
                     index = i + 1;
                 }
             }
         }
         else
         {
             stream.Write(buffer, 0, num);
         }
         goto Label_00DF;
     }
     if (flag2)
     {
         writer.Close();
     }
     else
     {
         stream.Close();
     }
 }
Esempio n. 11
0
        private void WriteEntryCore(TarEntry sourceEntry, bool recurse)
        {
            string   path  = null;
            string   file  = sourceEntry.File;
            TarEntry entry = (TarEntry)sourceEntry.Clone();

            if (_applyUserInfoOverrides)
            {
                entry.GroupId   = _groupId;
                entry.GroupName = _groupName;
                entry.UserId    = _userId;
                entry.UserName  = _userName;
            }
            OnProgressMessageEvent(entry, null);
            if ((_asciiTranslate && !entry.IsDirectory) && !IsBinary(file))
            {
                path = Path.GetTempFileName();
                using (StreamReader reader = File.OpenText(file))
                {
                    using (Stream stream = File.Create(path))
                    {
Label_0088:
                        string str3 = reader.ReadLine();
                        if (str3 != null)
                        {
                            byte[] bytes = Encoding.ASCII.GetBytes(str3);
                            stream.Write(bytes, 0, bytes.Length);
                            stream.WriteByte(10);
                            goto Label_0088;
                        }
                        stream.Flush();
                    }
                }
                entry.Size = new FileInfo(path).Length;
                file       = path;
            }
            string str4 = null;

            if ((_rootPath != null) && entry.Name.StartsWith(_rootPath))
            {
                str4 = entry.Name.Substring(_rootPath.Length + 1);
            }
            if (_pathPrefix != null)
            {
                str4 = (str4 == null) ? (_pathPrefix + "/" + entry.Name) : (_pathPrefix + "/" + str4);
            }
            if (str4 != null)
            {
                entry.Name = str4;
            }
            _tarOut.PutNextEntry(entry);
            if (entry.IsDirectory)
            {
                if (recurse)
                {
                    TarEntry[] directoryEntries = entry.GetDirectoryEntries();
                    foreach (TarEntry t in directoryEntries)
                    {
                        WriteEntryCore(t, true);
                    }
                    return;
                }
                return;
            }
            using (Stream stream2 = File.OpenRead(file))
            {
                byte[] buffer = new byte[0x8000];
                while (true)
                {
                    int count = stream2.Read(buffer, 0, buffer.Length);
                    if (count <= 0)
                    {
                        goto Label_01F6;
                    }
                    _tarOut.Write(buffer, 0, count);
                }
            }
Label_01F6:
            if (!string.IsNullOrEmpty(path))
            {
                File.Delete(path);
            }
            _tarOut.CloseEntry();
        }
Esempio n. 12
0
        private void ExtractEntry(string destDir, TarEntry entry)
        {
            int num;

            OnProgressMessageEvent(entry, null);
            string name = entry.Name;

            if (Path.IsPathRooted(name))
            {
                name = name.Substring(Path.GetPathRoot(name).Length);
            }
            name = name.Replace('/', Path.DirectorySeparatorChar);
            string directoryName = Path.Combine(destDir, name);

            if (entry.IsDirectory)
            {
                EnsureDirectoryExists(directoryName);
                return;
            }
            EnsureDirectoryExists(Path.GetDirectoryName(directoryName));
            bool     flag = true;
            FileInfo info = new FileInfo(directoryName);

            if (info.Exists)
            {
                if (_keepOldFiles)
                {
                    OnProgressMessageEvent(entry, "Destination file already exists");
                    flag = false;
                }
                else if ((info.Attributes & FileAttributes.ReadOnly) != 0)
                {
                    OnProgressMessageEvent(entry, "Destination file already exists, and is read-only");
                    flag = false;
                }
            }
            if (!flag)
            {
                return;
            }
            bool   flag2  = false;
            Stream stream = File.Create(directoryName);

            if (_asciiTranslate)
            {
                flag2 = !IsBinary(directoryName);
            }
            StreamWriter writer = null;

            if (flag2)
            {
                writer = new StreamWriter(stream);
            }
            byte[] buffer = new byte[0x8000];
Label_00DF:
            num = _tarIn.Read(buffer, 0, buffer.Length);
            if (num > 0)
            {
                if (flag2)
                {
                    int index = 0;
                    for (int i = 0; i < num; i++)
                    {
                        if (buffer[i] == 10)
                        {
                            string str4 = Encoding.ASCII.GetString(buffer, index, i - index);
                            writer.WriteLine(str4);
                            index = i + 1;
                        }
                    }
                }
                else
                {
                    stream.Write(buffer, 0, num);
                }
                goto Label_00DF;
            }
            if (flag2)
            {
                writer.Close();
            }
            else
            {
                stream.Close();
            }
        }
Esempio n. 13
0
 public static TarEntry CreateTarEntry(string name)
 {
     TarEntry entry = new TarEntry();
     NameTarHeader(entry._header, name);
     return entry;
 }
Esempio n. 14
0
 public static TarEntry CreateEntryFromFile(string fileName)
 {
     TarEntry entry = new TarEntry();
     entry.GetFileTarHeader(entry._header, fileName);
     return entry;
 }
Esempio n. 15
0
 public bool IsDescendent(TarEntry toTest)
 {
     if (toTest == null)
     {
         throw new ArgumentNullException(nameof(toTest));
     }
     return toTest.Name.StartsWith(Name);
 }