Skip to content

Commit 5701f42

Browse files
committed
Add tarring capabilities
This commit adds [tar-cs](http://code.google.com/p/tar-cs/) to LibGit2Sharp without any modifications to the original source code.
1 parent 2eb72b5 commit 5701f42

11 files changed

+982
-0
lines changed

LibGit2Sharp/Core/DataWriter.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* BSD License
3+
*
4+
* Copyright (c) 2009, Vladimir Vasiltsov
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11+
* * Names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*/
15+
16+
using System.IO;
17+
18+
namespace tar_cs
19+
{
20+
internal class DataWriter : IArchiveDataWriter
21+
{
22+
private readonly long size;
23+
private long remainingBytes;
24+
private bool canWrite = true;
25+
private readonly Stream stream;
26+
27+
public DataWriter(Stream data, long dataSizeInBytes)
28+
{
29+
size = dataSizeInBytes;
30+
remainingBytes = size;
31+
stream = data;
32+
}
33+
34+
public int Write(byte[] buffer, int count)
35+
{
36+
if(remainingBytes == 0)
37+
{
38+
canWrite = false;
39+
return -1;
40+
}
41+
int bytesToWrite;
42+
if(remainingBytes - count < 0)
43+
{
44+
bytesToWrite = (int)remainingBytes;
45+
}
46+
else
47+
{
48+
bytesToWrite = count;
49+
}
50+
stream.Write(buffer,0,bytesToWrite);
51+
remainingBytes -= bytesToWrite;
52+
return bytesToWrite;
53+
}
54+
55+
public bool CanWrite
56+
{
57+
get
58+
{
59+
return canWrite;
60+
}
61+
}
62+
}
63+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* BSD License
3+
*
4+
* Copyright (c) 2009, Vladimir Vasiltsov
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11+
* * Names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*/
15+
16+
namespace tar_cs
17+
{
18+
public interface IArchiveDataWriter
19+
{
20+
/// <summary>
21+
/// Write `length` bytes of data from `buffer` to corresponding archive.
22+
/// </summary>
23+
/// <param name="buffer">data storage</param>
24+
/// <param name="count">how many bytes to be written to the corresponding archive</param>
25+
int Write(byte[] buffer, int count);
26+
bool CanWrite { get; }
27+
}
28+
public delegate void WriteDataDelegate(IArchiveDataWriter writer);
29+
}

LibGit2Sharp/Core/ITarHeader.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* BSD License
3+
*
4+
* Copyright (c) 2009, Vladimir Vasiltsov
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11+
* * Names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*/
15+
16+
using System;
17+
18+
namespace tar_cs
19+
{
20+
public interface ITarHeader
21+
{
22+
string FileName { get; set; }
23+
int Mode { get; set; }
24+
int UserId { get; set; }
25+
string UserName { get; set; }
26+
int GroupId { get; set; }
27+
string GroupName { get; set; }
28+
long SizeInBytes { get; set; }
29+
DateTime LastModification { get; set; }
30+
int HeaderSize { get; }
31+
}
32+
}

LibGit2Sharp/Core/LegacyTarWriter.cs

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* BSD License
3+
*
4+
* Copyright (c) 2009, Vladimir Vasiltsov
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11+
* * Names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*/
15+
16+
using System;
17+
using System.IO;
18+
using System.Threading;
19+
using tar_cs;
20+
21+
namespace tar_cs
22+
{
23+
public class LegacyTarWriter : IDisposable
24+
{
25+
private readonly Stream outStream;
26+
protected byte[] buffer = new byte[1024];
27+
private bool isClosed;
28+
public bool ReadOnZero = true;
29+
30+
/// <summary>
31+
/// Writes tar (see GNU tar) archive to a stream
32+
/// </summary>
33+
/// <param name="writeStream">stream to write archive to</param>
34+
public LegacyTarWriter(Stream writeStream)
35+
{
36+
outStream = writeStream;
37+
}
38+
39+
protected virtual Stream OutStream
40+
{
41+
get { return outStream; }
42+
}
43+
44+
#region IDisposable Members
45+
46+
public void Dispose()
47+
{
48+
Close();
49+
}
50+
51+
#endregion
52+
53+
public void Write(string fileName)
54+
{
55+
using (FileStream file = File.OpenRead(fileName))
56+
{
57+
Write(file, file.Length, fileName, 61, 61, 511, File.GetLastWriteTime(file.Name));
58+
}
59+
}
60+
61+
public void Write(FileStream file)
62+
{
63+
string path = Path.GetFullPath(file.Name).Replace(Path.GetPathRoot(file.Name),string.Empty);
64+
path = path.Replace(Path.DirectorySeparatorChar, '/');
65+
Write(file, file.Length, path, 61, 61, 511, File.GetLastWriteTime(file.Name));
66+
}
67+
68+
public void Write(Stream data, long dataSizeInBytes, string name)
69+
{
70+
Write(data, dataSizeInBytes, name, 61, 61, 511, DateTime.Now);
71+
}
72+
73+
public virtual void Write(string name, long dataSizeInBytes, int userId, int groupId, int mode, DateTime lastModificationTime, WriteDataDelegate writeDelegate)
74+
{
75+
IArchiveDataWriter writer = new DataWriter(OutStream, dataSizeInBytes);
76+
WriteHeader(name, lastModificationTime, dataSizeInBytes, userId, groupId, mode);
77+
while(writer.CanWrite)
78+
{
79+
writeDelegate(writer);
80+
}
81+
AlignTo512(dataSizeInBytes, false);
82+
}
83+
84+
public virtual void Write(Stream data, long dataSizeInBytes, string name, int userId, int groupId, int mode,
85+
DateTime lastModificationTime)
86+
{
87+
if(isClosed)
88+
throw new TarException("Can not write to the closed writer");
89+
WriteHeader(name, lastModificationTime, dataSizeInBytes, userId, groupId, mode);
90+
WriteContent(dataSizeInBytes, data);
91+
AlignTo512(dataSizeInBytes,false);
92+
}
93+
94+
protected void WriteContent(long count, Stream data)
95+
{
96+
while (count > 0 && count > buffer.Length)
97+
{
98+
int bytesRead = data.Read(buffer, 0, buffer.Length);
99+
if (bytesRead < 0)
100+
throw new IOException("LegacyTarWriter unable to read from provided stream");
101+
if (bytesRead == 0)
102+
{
103+
if (ReadOnZero)
104+
Thread.Sleep(100);
105+
else
106+
break;
107+
}
108+
OutStream.Write(buffer, 0, bytesRead);
109+
count -= bytesRead;
110+
}
111+
if (count > 0)
112+
{
113+
int bytesRead = data.Read(buffer, 0, (int) count);
114+
if (bytesRead < 0)
115+
throw new IOException("LegacyTarWriter unable to read from provided stream");
116+
if (bytesRead == 0)
117+
{
118+
while (count > 0)
119+
{
120+
OutStream.WriteByte(0);
121+
--count;
122+
}
123+
}
124+
else
125+
OutStream.Write(buffer, 0, bytesRead);
126+
}
127+
}
128+
129+
protected virtual void WriteHeader(string name, DateTime lastModificationTime, long count, int userId, int groupId, int mode)
130+
{
131+
var header = new TarHeader
132+
{
133+
FileName = name,
134+
LastModification = lastModificationTime,
135+
SizeInBytes = count,
136+
UserId = userId,
137+
GroupId = groupId,
138+
Mode = mode
139+
};
140+
OutStream.Write(header.GetHeaderValue(), 0, header.HeaderSize);
141+
}
142+
143+
144+
public void AlignTo512(long size,bool acceptZero)
145+
{
146+
size = size%512;
147+
if (size == 0 && !acceptZero) return;
148+
while (size < 512)
149+
{
150+
OutStream.WriteByte(0);
151+
size++;
152+
}
153+
}
154+
155+
public virtual void Close()
156+
{
157+
if (isClosed) return;
158+
AlignTo512(0,true);
159+
AlignTo512(0,true);
160+
isClosed = true;
161+
}
162+
}
163+
}

LibGit2Sharp/Core/TarException.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* BSD License
3+
*
4+
* Copyright (c) 2009, Vladimir Vasiltsov
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11+
* * Names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*/
15+
16+
using System;
17+
18+
namespace tar_cs
19+
{
20+
public class TarException : Exception
21+
{
22+
public TarException(string message) : base(message)
23+
{
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)