-
Notifications
You must be signed in to change notification settings - Fork 38
/
MyFilePath.java
235 lines (212 loc) · 9.64 KB
/
MyFilePath.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* The MIT License
*
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
* Eric Lefevre-Ardant, Erik Ramfelt, Michael B. Donohue, Alan Harder,
* Manufacture Française des Pneumatiques Michelin, Romain Seguy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.michelin.cio.hudson.plugins.copytoslave;
import hudson.FilePath;
import hudson.FilePath.FileCallable;
import hudson.FilePath.TarCompression;
import hudson.Functions;
import hudson.Util;
import hudson.model.Hudson;
import hudson.org.apache.tools.tar.TarInputStream;
import hudson.remoting.Future;
import hudson.remoting.Pipe;
import hudson.remoting.VirtualChannel;
import hudson.util.IOException2;
import hudson.util.IOUtils;
import hudson.util.io.Archiver;
import hudson.util.io.ArchiverFactory;
import static hudson.util.jna.GNUCLibrary.LIBC;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.concurrent.ExecutionException;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Chmod;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.tar.TarEntry;
/**
* Complements Hudson's {@link FilePath} to enhance the {@code copyRecursiveTo()}
* method so that it offer a possibility to flatten dirs (cf. HUDSON-8220) and
* to not use Ant's default excludes (cf. HUDSON-7999).
*
* <p>This class also fixes HUDSON-8155.</p>
*/
public class MyFilePath implements Serializable {
private static final long serialVersionUID = 1; // HUDSON-8274
/**
* Enhances Hudson's {@link FilePath#copyRecursiveTo} until I patch Hudson
* core and upgrade the plugin to the corresponding Hudson version.
*
* <p>This method supports only local to local and local to remote copies.</p>
*/
public static int copyRecursiveTo(
final FilePath source,
final String includes, final String excludes,
final boolean flatten, final boolean includeAntExcludes,
final FilePath target) throws IOException, InterruptedException {
if(source.getChannel() == target.getChannel()) {
// --- local --> local copy ---
return new FileCallable<Integer>() {
public Integer invoke(File sourceBaseDir, VirtualChannel channel) throws IOException {
if(!sourceBaseDir.exists()) {
return 0;
}
try {
class CopyImpl extends Copy {
private int copySize;
public CopyImpl() {
setProject(new org.apache.tools.ant.Project());
}
@Override
protected void doFileOperations() {
copySize = super.fileCopyMap.size();
super.doFileOperations();
}
public int getNumCopied() {
return copySize;
}
}
FileSet fs = Util.createFileSet(sourceBaseDir, includes, excludes);
if(includeAntExcludes) {
fs.setDefaultexcludes(false); // HUDSON-7999
}
CopyImpl copyTask = new CopyImpl();
copyTask.setTodir(new File(target.getRemote()));
copyTask.addFileset(fs);
copyTask.setOverwrite(true);
copyTask.setIncludeEmptyDirs(false);
copyTask.setFlatten(flatten);
copyTask.execute();
return copyTask.getNumCopied();
} catch (BuildException e) {
throw new IOException2("Failed to copy from "+sourceBaseDir+" to "+target,e);
}
}
}.invoke(new File(source.getRemote()), Hudson.MasterComputer.localChannel);
}
else {
// --- local -> remote copy ---
final Pipe pipe = Pipe.createLocalToRemote();
Future<Void> future = target.actAsync(new FileCallable<Void>() {
private static final long serialVersionUID = 1; // HUDSON-8274
public Void invoke(File f, VirtualChannel channel) throws IOException {
try {
readFromTar(f, flatten, TarCompression.GZIP.extract(pipe.getIn()));
return null;
} finally {
pipe.getIn().close();
}
}
});
int r = writeToTar(new File(source.getRemote()), includes, excludes, includeAntExcludes, TarCompression.GZIP.compress(pipe.getOut()));
try {
future.get();
} catch (ExecutionException e) {
throw new IOException2(e);
}
return r;
}
}
/**
* Full copy/paste of Hudson's {@link FilePath#readFromTar} method with
* some tweaking (mainly the flatten behavior).
*
* @see hudson.FilePath#readFromTar(java.lang.String, java.io.File, java.io.InputStream)
*/
public static void readFromTar(File baseDir, boolean flatten, InputStream in) throws IOException {
Chmod chmodTask = null; // HUDSON-8155
TarInputStream t = new TarInputStream(in);
try {
TarEntry tarEntry;
while ((tarEntry = t.getNextEntry()) != null) {
File f = null;
if(!flatten || (!tarEntry.getName().contains("/") && !tarEntry.getName().contains("\\"))) {
f = new File(baseDir, tarEntry.getName());
}
else {
String fileName = StringUtils.substringAfterLast(tarEntry.getName(), "/");
if(StringUtils.isBlank(fileName)) {
fileName = StringUtils.substringAfterLast(tarEntry.getName(), "\\");
}
f = new File(baseDir, fileName);
}
// dir processing
if(!flatten && tarEntry.isDirectory()) {
f.mkdirs();
}
// file processing
else {
if(!flatten && f.getParentFile() != null) {
f.getParentFile().mkdirs();
}
IOUtils.copy(t, f);
f.setLastModified(tarEntry.getModTime().getTime());
// chmod
int mode = tarEntry.getMode()&0777;
if(mode!=0 && !Functions.isWindows()) // be defensive
try {
LIBC.chmod(f.getPath(), mode);
} catch (NoClassDefFoundError ncdfe) {
// be defensive. see http://www.nabble.com/-3.0.6--Site-copy-problem%3A-hudson.util.IOException2%3A--java.lang.NoClassDefFoundError%3A-Could-not-initialize-class--hudson.util.jna.GNUCLibrary-td23588879.html
} catch (UnsatisfiedLinkError ule) {
// HUDSON-8155: use Ant's chmod task
if(chmodTask == null) {
chmodTask = new Chmod();
}
chmodTask.setProject(new Project());
chmodTask.setFile(f);
chmodTask.setPerm(Integer.toOctalString(mode));
chmodTask.execute();
}
}
}
} catch(IOException e) {
throw new IOException2("Failed to extract to "+baseDir.getAbsolutePath(),e);
} finally {
t.close();
}
}
/**
* Full copy/paste of Hudson's {@link FilePath#writeToTar} method with some
* tweaking (added an includeAntExcludes parameter).
*
* @see hudson.FilePath#writeToTar(java.lang.String, java.io.File, java.io.InputStream)
*/
public static Integer writeToTar(File baseDir, String includes, String excludes, boolean includeAntExcludes, OutputStream out) throws IOException {
Archiver tw = ArchiverFactory.TAR.create(out);
try {
new MyGlobDirScanner(includes, excludes, includeAntExcludes).scan(baseDir, tw); // HUDSON-7999
} finally {
tw.close();
}
return tw.countEntries();
}
}