1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
22
23 public class UnixCmds {
24 private static final String SU = "su";
25 private static final String MKDIR = "mkdir";
26 private static final String RM = "rm";
27 private static final String CHOWN = "chown";
28 private static final String CHMOD = "chmod";
29 private static final String HOSTNAME = "hostname";
30 private static final String PS = "ps";
31 private static final String KILL = "kill";
32 private static final String NOHUP = "nohup";
33 private static final String CP = "cp";
34
35 public String cp(String src, String dst) {
36 return cp(null, src, dst, false);
37 }
38
39 public String cp(String src, String dst, boolean bypassAnyAliases) {
40 return cp(null, src, dst, bypassAnyAliases);
41 }
42
43 public String cp(List<String> options, String src, String dst, boolean bypassAnyAliases) {
44 Assert.hasText(src, "src has no text");
45 Assert.hasText(dst, "dst has no text");
46 List<String> args = new ArrayList<String>();
47 args.addAll(CollectionUtils.toEmptyList(options));
48 args.add(src);
49 args.add(dst);
50 return cmd(CP, args, bypassAnyAliases);
51 }
52
53 public String nohup(String command) {
54 return nohup(command, new ArrayList<String>());
55 }
56
57 public String nohup(String command, List<String> nohupArgs) {
58 Assert.hasText(command);
59 List<String> args = new ArrayList<String>();
60 args.add(command);
61 args.addAll(nohupArgs);
62 return cmd(NOHUP, args);
63 }
64
65 public String kill(int pid) {
66 return kill(Arrays.asList(pid));
67 }
68
69 public String kill(List<Integer> pids) {
70 Assert.notEmpty(pids);
71 List<String> args = new ArrayList<String>();
72 for (Integer pid : pids) {
73 args.add(pid.toString());
74 }
75 return cmd(KILL, args);
76 }
77
78 public String ps() {
79 return cmd(PS);
80 }
81
82 public String ps(String user) {
83 Assert.hasText(user);
84 return cmd(PS, Arrays.asList("-u", user));
85 }
86
87 public String psf(String user) {
88 Assert.hasText(user);
89 List<String> args = new ArrayList<String>();
90 args.add("-u");
91 args.add(user);
92 args.add("-f");
93 return cmd(PS, args);
94 }
95
96
97
98
99 public String hostname() {
100 return cmd(HOSTNAME);
101 }
102
103
104
105
106 public String hostname(String hostname) {
107 return cmd(HOSTNAME, Arrays.asList(hostname));
108 }
109
110 public String chmod(String mode, String path) {
111 Assert.notBlank(path);
112 return chmod(mode, Collections.singletonList(path));
113 }
114
115 public String chmod(String mode, List<String> paths) {
116 return chmod(null, mode, paths);
117
118 }
119
120 public String chmod(List<String> options, String mode, List<String> paths) {
121 Assert.hasLength(mode);
122 Assert.notEmpty(paths);
123 return cmd(CHMOD, CollectionUtils.combineStrings(options, mode, paths));
124 }
125
126 public String mkdirp(String path) {
127 Assert.notBlank(path);
128 return mkdirp(null, Collections.singletonList(path));
129 }
130
131 public String mkdirp(List<String> paths) {
132 return mkdirp(null, paths);
133 }
134
135 public String mkdirp(List<String> options, List<String> paths) {
136 List<String> parents = Arrays.asList("-p");
137 if (options == null) {
138 return mkdir(parents, paths);
139 } else {
140 return mkdir(CollectionUtils.combineStringsUniquely(options, parents), paths);
141 }
142
143 }
144
145 public String mkdir(String path) {
146 Assert.notBlank(path);
147 return mkdir(null, Collections.singletonList(path));
148 }
149
150 public String mkdir(List<String> options, List<String> paths) {
151 Assert.notEmpty(paths);
152 return cmd(MKDIR, CollectionUtils.combineStrings(options, paths));
153 }
154
155 public String su(String login, String command) {
156 return su(null, login, command);
157 }
158
159 public String su(List<String> options, String login, String command) {
160 return su(options, login, Arrays.asList("--command", command));
161 }
162
163 public String su(List<String> options, String login, List<String> args) {
164 List<String> list2 = login == null ? null : Arrays.asList("-", login);
165 return cmd(SU, CollectionUtils.combineStrings(options, list2, args));
166 }
167
168 public String rmrf(List<String> paths) {
169 return rmrf(null, paths);
170 }
171
172 public String rmrf(String path) {
173 Assert.notBlank(path);
174 return rmrf(null, Collections.singletonList(path));
175 }
176
177 public String rmrf(List<String> options, List<String> paths) {
178 List<String> recursiveSilent = Arrays.asList("-r", "-f");
179 if (options == null) {
180 return rm(recursiveSilent, paths);
181 } else {
182 return rm(CollectionUtils.combineStringsUniquely(options, recursiveSilent), paths);
183 }
184 }
185
186 public String rm(List<String> paths) {
187 return rm(null, paths);
188 }
189
190 public String rm(String path) {
191 Assert.notBlank(path);
192 return rm(null, Collections.singletonList(path));
193 }
194
195 public String rm(List<String> options, List<String> paths) {
196 Assert.notEmpty(paths);
197 return cmd(RM, CollectionUtils.combineStrings(options, paths));
198 }
199
200 public String chownr(String owner, String group, String path) {
201 Assert.notBlank(path);
202 return chownr(owner, group, Collections.singletonList(path));
203 }
204
205 public String chownr(String owner, String group, List<String> paths) {
206 return chownr(null, owner, group, paths);
207 }
208
209 public String chownr(List<String> options, String owner, String group, List<String> paths) {
210 List<String> recursive = Arrays.asList("-R");
211 if (options == null) {
212 return chown(recursive, owner, group, paths);
213 } else {
214 return chown(CollectionUtils.combineStringsUniquely(options, recursive), owner, group, paths);
215 }
216 }
217
218 public String chown(List<String> options, String owner, String group, String path) {
219 Assert.notBlank(path);
220 return chown(options, owner, group, Collections.singletonList(path));
221 }
222
223 public String chown(List<String> options, String owner, String group, List<String> paths) {
224 Assert.notEmpty(paths);
225 Assert.noBlanks(owner, group);
226 return cmd(CHOWN, CollectionUtils.combineStrings(options, owner + ":" + group, paths));
227 }
228
229 public String chown(String owner, String group, String path) {
230 Assert.notBlank(path);
231 return chown(null, owner, group, Collections.singletonList(path));
232 }
233
234 public String cmd(String executable) {
235 return cmd(executable, null);
236 }
237
238 public String cmd(String executable, List<String> args) {
239 return cmd(executable, args, false);
240 }
241
242 public String cmd(String executable, List<String> args, boolean bypassAnyAliases) {
243 StringBuilder sb = new StringBuilder();
244 if (bypassAnyAliases) {
245 sb.append("\"");
246 }
247 sb.append(executable);
248 if (!CollectionUtils.isEmpty(args)) {
249 sb.append(" ");
250 sb.append(CollectionUtils.getSpaceSeparatedString(args));
251 }
252 return sb.toString();
253 }
254 }