File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\io\TarArchiver.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 package hudson.util.io;
26
27 import hudson.Functions;
28 import hudson.org.apache.tools.tar.TarOutputStream;
29 import hudson.os.PosixException;
30 import hudson.util.FileVisitor;
31 import hudson.util.IOException2;
32 import hudson.util.IOUtils;
33 import org.apache.tools.tar.TarEntry;
34
35 import java.io.BufferedOutputStream;
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.io.OutputStream;
40 import java.lang.reflect.Field;
41
42 import static org.apache.tools.tar.TarConstants.LF_SYMLINK;
43
44 /**
45 * {@link FileVisitor} that creates a tar archive.
46 *
47 * @see ArchiverFactory#TAR
48 */
49 final class TarArchiver extends Archiver {
50 private final byte[] buf = new byte[8192];
51 private final TarOutputStream tar;
52
53 TarArchiver(OutputStream out) {
54 tar = new TarOutputStream(new BufferedOutputStream(out) {
55 // TarOutputStream uses TarBuffer internally,
56 // which flushes the stream for each block. this creates unnecessary
57 // data stream fragmentation, and flush request to a remote, which slows things down.
58 @Override
59 public void flush() throws IOException {
60 // so don't do anything in flush
61 }
62 });
63 tar.setLongFileMode(TarOutputStream.LONGFILE_GNU);
64 }
65
66 @Override
67 public void visitSymlink(File link, String target, String relativePath) throws IOException {
68 TarEntry e = new TarEntry(relativePath, LF_SYMLINK);
69 try {
70 int mode = IOUtils.mode(link);
71 if (mode != -1) {
72 e.setMode(mode);
73 }
74 } catch (PosixException x) {
75 // ignore
76 }
77
78 try {
79 StringBuffer linkName = (StringBuffer) LINKNAME_FIELD.get(e);
80 linkName.setLength(0);
81 linkName.append(target);
82 } catch (IllegalAccessException x) {
83 throw new IOException2("Failed to set linkName", x);
84 }
85
86 tar.putNextEntry(e);
87 entriesWritten++;
88 }
89
90 @Override
91 public boolean understandsSymlink() {
92 return true;
93 }
94
95 public void visit(File file, String relativePath) throws IOException {
96 if(Functions.isWindows())
97 relativePath = relativePath.replace('\\','/');
98
99 if(file.isDirectory())
100 relativePath+='/';
101 TarEntry te = new TarEntry(relativePath);
102 int mode = IOUtils.mode(file);
103 if (mode!=-1) te.setMode(mode);
104 te.setModTime(file.lastModified());
105 if(!file.isDirectory())
106 te.setSize(file.length());
107
108 tar.putNextEntry(te);
109
110 if (!file.isDirectory()) {
111 FileInputStream in = new FileInputStream(file);
112 try {
113 int len;
114 while((len=in.read(buf))>=0)
115 tar.write(buf,0,len);
116 } finally {
117 in.close();
118 }
119 }
120
121 tar.closeEntry();
122 entriesWritten++;
123 }
124
125 public void close() throws IOException {
126 tar.close();
127 }
128
129 private static final Field LINKNAME_FIELD = getTarEntryLinkNameField();
130
131 private static Field getTarEntryLinkNameField() {
132 try {
133 Field f = TarEntry.class.getDeclaredField("linkName");
134 f.setAccessible(true);
135 return f;
136 } catch (SecurityException e) {
137 throw new AssertionError(e);
138 } catch (NoSuchFieldException e) {
139 throw new AssertionError(e);
140 }
141 }
142 }
143