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 package org.eclipse.jgit.archive;
44
45 import static org.eclipse.jgit.lib.Constants.CHARACTER_ENCODING;
46
47 import java.io.IOException;
48 import java.io.OutputStream;
49 import java.text.MessageFormat;
50 import java.util.Arrays;
51 import java.util.Collections;
52 import java.util.List;
53 import java.util.Map;
54
55 import org.apache.commons.compress.archivers.ArchiveOutputStream;
56 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
57 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
58 import org.apache.commons.compress.archivers.tar.TarConstants;
59 import org.eclipse.jgit.api.ArchiveCommand;
60 import org.eclipse.jgit.archive.internal.ArchiveText;
61 import org.eclipse.jgit.lib.FileMode;
62 import org.eclipse.jgit.lib.ObjectId;
63 import org.eclipse.jgit.lib.ObjectLoader;
64 import org.eclipse.jgit.revwalk.RevCommit;
65
66
67
68
69
70 public final class TarFormat extends BaseFormat implements
71 ArchiveCommand.Format<ArchiveOutputStream> {
72 private static final List<String> SUFFIXES = Collections
73 .unmodifiableList(Arrays.asList(".tar"));
74
75
76 @Override
77 public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
78 throws IOException {
79 return createArchiveOutputStream(s,
80 Collections.<String, Object> emptyMap());
81 }
82
83
84 @Override
85 public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
86 Map<String, Object> o) throws IOException {
87 TarArchiveOutputStream out = new TarArchiveOutputStream(s,
88 CHARACTER_ENCODING);
89 out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
90 out.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
91 return applyFormatOptions(out, o);
92 }
93
94
95 @Override
96 public void putEntry(ArchiveOutputStream out,
97 ObjectId tree, String path, FileMode mode, ObjectLoader loader)
98 throws IOException {
99 if (mode == FileMode.SYMLINK) {
100 final TarArchiveEntry entry = new TarArchiveEntry(
101 path, TarConstants.LF_SYMLINK);
102 entry.setLinkName(new String(
103 loader.getCachedBytes(100), CHARACTER_ENCODING));
104 out.putArchiveEntry(entry);
105 out.closeArchiveEntry();
106 return;
107 }
108
109
110
111 if (path.endsWith("/") && mode != FileMode.TREE)
112 throw new IllegalArgumentException(MessageFormat.format(
113 ArchiveText.get().pathDoesNotMatchMode, path, mode));
114 if (!path.endsWith("/") && mode == FileMode.TREE)
115 path = path + "/";
116
117 final TarArchiveEntry entry = new TarArchiveEntry(path);
118
119 if (tree instanceof RevCommit) {
120 long t = ((RevCommit) tree).getCommitTime() * 1000L;
121 entry.setModTime(t);
122 }
123
124 if (mode == FileMode.TREE) {
125 out.putArchiveEntry(entry);
126 out.closeArchiveEntry();
127 return;
128 }
129
130 if (mode == FileMode.REGULAR_FILE) {
131
132 } else if (mode == FileMode.EXECUTABLE_FILE) {
133 entry.setMode(mode.getBits());
134 } else {
135
136 throw new IllegalArgumentException(MessageFormat.format(
137 ArchiveText.get().unsupportedMode, mode));
138 }
139 entry.setSize(loader.getSize());
140 out.putArchiveEntry(entry);
141 loader.copyTo(out);
142 out.closeArchiveEntry();
143 }
144
145
146 @Override
147 public Iterable<String> suffixes() {
148 return SUFFIXES;
149 }
150
151
152 @Override
153 public boolean equals(Object other) {
154 return (other instanceof TarFormat);
155 }
156
157
158 @Override
159 public int hashCode() {
160 return getClass().hashCode();
161 }
162 }