-
Notifications
You must be signed in to change notification settings - Fork 80
/
SCMFile.java
462 lines (432 loc) · 18 KB
/
SCMFile.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* The MIT License
*
* Copyright (c) 2011-2016, CloudBees, Inc., Stephen Connolly.
*
* 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 jenkins.scm.api;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.WebApp;
/**
* A file/directory inspected by {@link SCMFileSystem}.
*/
public abstract class SCMFile {
/**
* The parent {@link SCMFile} or {@code null} for the root.
*/
@CheckForNull
private final SCMFile parent;
/**
* The name of this {@link SCMFile}
*/
private final String name;
/**
* Cache of the file type information, to allow repeated calls to minimize the number of network round trips.
* We cache the type information because too many people think that
* {@code File f; if (f.exists() && f.isFile()) ...} is a good code pattern, ignoring the fact that
* {@link File#isFile()} includes and existence check. We provide {@link #getType()} to avoid the need for
* such calling styles, but when people are replicating local code it can be easier to keep the logic
* the same as the code you are replicating - which is why we cache this here.
*/
private Type type;
/**
* Constructor for the root entry.
*
* @since 2.0
*/
protected SCMFile() {
this.parent = null;
this.name = "";
}
/**
* Constructor for any entry that is not the root.
*
* @param parent the parent reference or {@code null} if this is the root object.
* @param name the name of this entry (cannot contain '/').
* @since 2.0
*/
protected SCMFile(@NonNull SCMFile parent, String name) {
if (name.indexOf('/') != -1) {
throw new IllegalArgumentException("Name cannot contain '/'");
}
this.parent = parent;
this.name = name;
}
/**
* Gets the file name of this file without any path portion, such as just "foo.txt"
* <p>This method is the equivalent of {@link File#getName()}.</p>
*
* @return the file name of this file without any path portion.
*/
@NonNull
public final String getName() {
return name;
}
/**
* Gets the file name including the path portion, such as "foo/bar/manchu.txt". Will never end in {@code /}.
*
* @return the pathname of this file.
* @since 2.0
*/
@NonNull
public String getPath() {
if (parent == null) {
// root node
return "";
}
List<String> names = new ArrayList<>();
SCMFile ptr = this;
while (ptr != null && !ptr.isRoot()) {
names.add(ptr.getName());
ptr = ptr.parent();
}
Collections.reverse(names);
return StringUtils.join(names, "/");
}
/**
* Tests if this instance is the root of the filesystem.
*
* @return {@code true} if this instance is the root of the filesystem.
* @since 2.0
*/
public final boolean isRoot() {
return parent == null;
}
/**
* Retrieves the parent {@link SCMFile} instance.
*
* @return the parent or {@code null} if this instance is the root already.
* @since 2.0
*/
@CheckForNull
public SCMFile parent() {
return parent;
}
/**
* Constructs a child/descendant {@link SCMFile} instance path relative from this object.
*
* @param path Relative path of the child to return.
* @return The instance.
*/
@NonNull
public SCMFile child(String path) {
int index = path.indexOf('/');
if (index == -1) {
if (".".equals(path)) {
return this;
}
if ("..".equals(path)) {
SCMFile parent = parent();
return parent == null ? this : parent;
}
return newChild(path, false);
}
String name = path.substring(0, index);
SCMFile next;
if (".".equals(name)) {
next = this;
} else if ("..".equals(name)) {
SCMFile parent = parent();
next = parent == null ? this : parent;
} else {
next = newChild(name, true);
}
String restOfPath = path.substring(index + 1);
return StringUtils.isBlank(restOfPath) ? next : next.child(restOfPath);
}
/**
* Constructs an immediate child with the supplied type hint.
*
* @param name the name of the immediate child, never {@code null}, never empty and never containing
* a {@code /}.
* @param assumeIsDirectory {@code true} if it this entry is being used as an intermediate in a multi-segment path
* and should thus be assumed to be a directory.
* @return the instance.
* @since 2.0.1
*/
@NonNull
protected abstract SCMFile newChild(@NonNull String name, boolean assumeIsDirectory);
/**
* If this object represents a directory, lists up all the immediate children.
* <p>This method is the equivalent of {@link File#listFiles()}.</p>
*
* @return Always non-null. If this method is not a directory, this method returns
* an empty iterable.
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
@NonNull
public abstract Iterable<SCMFile> children() throws IOException, InterruptedException;
/**
* Returns the time that the {@link SCMFile} was last modified.
*
* @return A <code>long</code> value representing the time the file was last modified, measured in milliseconds
* since the epoch (00:00:00 GMT, January 1, 1970) or {@code 0L} if the operation is unsupported.
* @throws FileNotFoundException if this {@link SCMFile} instance does not exist in the remote system (e.g. if you
* created a nonexistent instance via {@link #child(String)})
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
public abstract long lastModified() throws IOException, InterruptedException;
/**
* Returns true if this object represents something that exists.
* <p>This method is the equivalent of {@link File#exists()}.</p>
* <p>NOTE: Typically to minimize round trips, {@link #getType()} would be preferred</p>
*
* @return true if this object represents something that exists.
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
* @see #getType()
*/
public final boolean exists() throws IOException, InterruptedException {
return !Type.NONEXISTENT.equals(getType());
}
/**
* Returns true if this object represents a file.
* <p>This method is the equivalent of {@link File#isFile()}.</p>
* <p>NOTE: Typically to minimize round trips, {@link #getType()} would be preferred</p>
*
* @return true if this object represents a file.
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
* @see #getType()
*/
public final boolean isFile() throws IOException, InterruptedException {
return Type.REGULAR_FILE.equals(getType());
}
/**
* Returns true if this object represents a directory.
* <p>This method is the equivalent of {@link File#isDirectory()}.</p>
* <p>NOTE: Typically to minimize round trips, {@link #getType()} would be preferred</p>
*
* @return true if this object represents a directory.
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
* @see #getType()
*/
public final boolean isDirectory() throws IOException, InterruptedException {
return Type.DIRECTORY.equals(getType());
}
/**
* The type of this object.
*
* @return the {@link Type} of this object, specifically {@link Type#NONEXISTENT} if this {@link SCMFile} instance
* does not exist in the remote system (e.g. if you created a nonexistent instance via {@link #child(String)})
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
@NonNull
public final Type getType() throws IOException, InterruptedException {
return type != null ? type : (type = type());
}
/**
* Proactively seeds the type information where that has been already obtained in a different request.
*
* @param type the type of this object.
* @since 2.0
*/
protected final void type(@NonNull Type type) {
this.type = type;
}
/**
* The type of this object.
*
* @return the {@link Type} of this object, specifically {@link Type#NONEXISTENT} if this {@link SCMFile} instance
* does not exist in the remote system (e.g. if you created a nonexistent instance via {@link #child(String)})
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
* @since 2.0
*/
@NonNull
protected abstract Type type() throws IOException, InterruptedException;
/**
* Reads the content of this file.
*
* @return an open stream to read the file content. The caller must close the stream.
* @throws FileNotFoundException if this {@link SCMFile} instance does not exist in the remote system (e.g. if you
* created a nonexistent instance via {@link #child(String)})
* @throws IOException if this object represents a directory or if an error occurs while performing the
* operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
@NonNull
public abstract InputStream content() throws IOException, InterruptedException;
/**
* A convenience method that reads the content and then turns it into a byte array.
*
* @return the file content as a byte array.
* @throws FileNotFoundException if this {@link SCMFile} instance does not exist in the remote system (e.g. if you
* created a nonexistent instance via {@link #child(String)})
* @throws IOException if this object represents a directory or if an error occurs while performing the
* operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
@NonNull
public byte[] contentAsBytes() throws IOException, InterruptedException {
final InputStream is = content();
try {
return IOUtils.toByteArray(is);
} finally {
IOUtils.closeQuietly(is);
}
}
/**
* A convenience method that reads the content and then turns it into a string.
*
* @return the file content as a string.
* @throws FileNotFoundException if this {@link SCMFile} instance does not exist in the remote system (e.g. if you
* created a nonexistent instance via {@link #child(String)})
* @throws IOException if this object represents a directory or if an error occurs while performing the
* operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
@NonNull
public String contentAsString() throws IOException, InterruptedException {
final InputStream is = content();
try {
return IOUtils.toString(is, contentEncoding());
} finally {
IOUtils.closeQuietly(is);
}
}
/**
* Returns the MIME type of this file.
* <p>The default implementation infers this based on the file name, but
* sophisticated server might provide this information from different sources,
* such as "svn:mime-type" in Subversion.</p>
*
* @return the MIME type of this file.
* @throws FileNotFoundException if this {@link SCMFile} instance does not exist in the remote system (e.g. if you
* created a nonexistent instance via {@link #child(String)})
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
@NonNull
public String contentMimeType() throws IOException, InterruptedException {
return getMimeType(getName());
}
/**
* Checks if this file is a binary file.
* <p>What exactly is a binary file is up to the implementation. Some SCMs (such as Subversion)
* has a way of letting users mark files as binaries.</p>
*
* @return true if this file is a binary file.
* @throws FileNotFoundException if this {@link SCMFile} instance does not exist in the remote system (e.g. if you
* created a nonexistent instance via {@link #child(String)})
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
public boolean isContentBinary() throws IOException, InterruptedException {
return !isContentText();
}
/**
* The opposite of {@link #isContentBinary()}
*
* @return true if this file is not a binary file.
* @throws FileNotFoundException if this {@link SCMFile} instance does not exist in the remote system (e.g. if you
* created a nonexistent instance via {@link #child(String)})
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
public boolean isContentText() throws IOException, InterruptedException {
return StringUtils.startsWithIgnoreCase(contentMimeType(), "text/");
}
/**
* Encoding of this file.
* <p>This is used to interpret text files.</p>
* <p>Some SCM implementations allow users to mark content encoding of files, and this method
* may provide those. As a fallback, the default implementation returns the platform
* default encoding.</p>
*
* @return the encoding of this file.
* @throws FileNotFoundException if this {@link SCMFile} instance does not exist in the remote system (e.g. if you
* created a nonexistent instance via {@link #child(String)})
* @throws IOException if an error occurs while performing the operation.
* @throws InterruptedException if interrupted while performing the operation.
*/
@NonNull
public Charset contentEncoding() throws IOException, InterruptedException {
return Charset.defaultCharset();
}
/**
* Looks up the servlet container's mime type mapping for the provided filename.
*
* @param fileName the file name.
* @return the mime type.
*/
@NonNull
private static String getMimeType(@NonNull String fileName) {
int idx = fileName.lastIndexOf('/');
fileName = fileName.substring(idx + 1);
idx = fileName.lastIndexOf('\\');
fileName = fileName.substring(idx + 1);
WebApp webApp = WebApp.getCurrent();
String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
String mimeType = webApp.mimeTypes.get(extension);
if (mimeType == null) {
mimeType = webApp.context.getMimeType(fileName);
}
if (mimeType == null) {
mimeType = "application/octet-stream";
}
if (webApp.defaultEncodingForStaticResources.containsKey(mimeType)) {
mimeType += ";charset=" + webApp.defaultEncodingForStaticResources.get(mimeType);
}
return mimeType;
}
/**
* Represents the type of a {@link SCMFile}.
*
* @since 2.0
*/
public enum Type {
/**
* The {@link SCMFile} does not exist.
*/
NONEXISTENT,
/**
* The {@link SCMFile} is a regular file.
*/
REGULAR_FILE,
/**
* The {@link SCMFile} is a regular directory.
*/
DIRECTORY,
/**
* The {@link SCMFile} is a link.
*/
LINK,
/**
* The {@link SCMFile} is something else, but it exists
*/
OTHER
}
}