-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathTarArchiver.cs
143 lines (131 loc) · 5.23 KB
/
TarArchiver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Globalization;
using System.IO;
using System.Text;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// Logic for tar archiving (not the actual tar format, but the overal logic related to tar+git) is taken
/// from https://github.com/git/git/blob/master/archive-tar.c.
/// </summary>
internal class TarArchiver : ArchiverBase, IDisposable
{
private readonly TarWriter writer;
public TarArchiver(FileStream output)
{
writer = new TarWriter(output);
}
#region Overrides of ArchiverBase
public override void BeforeArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime)
{
if (oid == null)
{
return;
}
// Store the sha in the pax_global_header
using (var stream =
new MemoryStream(Encoding.ASCII.GetBytes(string.Format(CultureInfo.InvariantCulture,
"52 comment={0}\n",
oid.Sha))))
{
writer.Write("pax_global_header",
stream, modificationTime,
"666".OctalToInt32(),
"0",
"0",
'g',
"root",
"root",
"0",
"0",
oid.Sha,
false);
}
}
protected override void AddTreeEntry(string path, TreeEntry entry, DateTimeOffset modificationTime)
{
switch (entry.Mode)
{
case Mode.GitLink:
case Mode.Directory:
writer.Write(path + "/",
null,
modificationTime,
"775".OctalToInt32(),
"0",
"0",
'5',
"root",
"root",
"0",
"0",
entry.TargetId.Sha,
false);
break;
case Mode.ExecutableFile:
case Mode.NonExecutableFile:
case Mode.NonExecutableGroupWritableFile:
var blob = ((Blob)entry.Target);
WriteStream(path,
entry,
modificationTime,
() => blob.IsBinary
? blob.GetContentStream()
: blob.GetContentStream(new FilteringOptions(path)));
break;
case Mode.SymbolicLink:
using (Stream contentStream = ((Blob)entry.Target).GetContentStream(new FilteringOptions(path)))
{
writer.Write(path,
contentStream,
modificationTime,
"777".OctalToInt32(),
"0",
"0",
'2',
"root",
"root",
"0",
"0",
entry.TargetId.Sha,
true);
}
break;
default:
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
"Unsupported file mode: {0} (sha1: {1}).",
entry.Mode,
entry.TargetId.Sha));
}
}
private void WriteStream(string path, TreeEntry entry, DateTimeOffset modificationTime, Func<Stream> streamer)
{
using (Stream contentStream = streamer())
{
writer.Write(path,
contentStream,
modificationTime,
(entry.Mode == Mode.ExecutableFile)
? "775".OctalToInt32()
: "664".OctalToInt32(),
"0",
"0",
'0',
"root",
"root",
"0",
"0",
entry.TargetId.Sha,
false);
}
}
#endregion
#region Implementation of IDisposable
public void Dispose()
{
writer.Dispose();
}
#endregion
}
}