-
Notifications
You must be signed in to change notification settings - Fork 695
/
Copy pathWindowsUtil.java
122 lines (110 loc) · 5.18 KB
/
WindowsUtil.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
/*
* The MIT License
*
* Copyright (c) 2019 CloudBees, Inc.
*
* 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.
*/
//TODO: Have this added to core
//https://github.com/jenkinsci/ec2-plugin/pull/352/
package hudson.os;
import hudson.Functions;
import org.apache.commons.io.IOUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
// adapted from:
// https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
@Restricted(NoExternalUse.class)
public class WindowsUtil {
private static final Pattern NEEDS_QUOTING = Pattern.compile("[\\s\"]");
/**
* Quotes an argument while escaping special characters interpreted by CreateProcess.
*/
public static @Nonnull String quoteArgument(@Nonnull String argument) {
if (!NEEDS_QUOTING.matcher(argument).find()) return argument;
StringBuilder sb = new StringBuilder();
sb.append('"');
int end = argument.length();
for (int i = 0; i < end; i++) {
int nrBackslashes = 0;
while (i < end && argument.charAt(i) == '\\') {
i++;
nrBackslashes++;
}
if (i == end) {
// backslashes at the end of the argument must be escaped so the terminate quote isn't
nrBackslashes = nrBackslashes * 2;
} else if (argument.charAt(i) == '"') {
// backslashes preceding a quote all need to be escaped along with the quote
nrBackslashes = nrBackslashes * 2 + 1;
}
// else backslashes have no special meaning and don't need to be escaped here
for (int j = 0; j < nrBackslashes; j++) {
sb.append('\\');
}
if (i < end) {
sb.append(argument.charAt(i));
}
}
return sb.append('"').toString();
}
private static final Pattern CMD_METACHARS = Pattern.compile("[()%!^\"<>&|]");
/**
* Quotes an argument while escaping special characters suitable for use as an argument to {@code cmd.exe}.
*/
public static @Nonnull String quoteArgumentForCmd(@Nonnull String argument) {
return CMD_METACHARS.matcher(quoteArgument(argument)).replaceAll("^$0");
}
/**
* Executes a command and arguments using {@code cmd.exe /C ...}.
*/
public static @Nonnull Process execCmd(String... argv) throws IOException {
String command = Arrays.stream(argv).map(WindowsUtil::quoteArgumentForCmd).collect(Collectors.joining(" "));
return Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", command});
}
/**
* Creates an NTFS junction point if supported. Similar to symbolic links, NTFS provides junction points which
* provide different features than symbolic links.
* @param junction NTFS junction point to create
* @param target target directory to junction
* @return the newly created junction point
* @throws IOException if the call to mklink exits with a non-zero status code
* @throws InterruptedException if the call to mklink is interrupted before completing
* @throws UnsupportedOperationException if this method is called on a non-Windows platform
*/
public static @Nonnull File createJunction(@Nonnull File junction, @Nonnull File target) throws IOException, InterruptedException {
if(Functions.isWindows() == false) {
throw new UnsupportedOperationException("Can only be called on windows platform");
}
Process mklink = execCmd("mklink", "/J", junction.getAbsolutePath(), target.getAbsolutePath());
int result = mklink.waitFor();
if (result != 0) {
String stderr = IOUtils.toString(mklink.getErrorStream());
String stdout = IOUtils.toString(mklink.getInputStream());
throw new IOException("Process exited with " + result + "\nStandard Output:\n" + stdout + "\nError Output:\n" + stderr);
}
return junction;
}
}