Refactor system exec

This commit is contained in:
Reinhard Pointner 2019-03-15 14:33:16 +07:00
parent 3fa0530da9
commit 078543d1e9
6 changed files with 17 additions and 51 deletions

View File

@ -57,6 +57,10 @@ public class Execute {
}
}
public static void system(String... command) throws IOException {
system(asList(command), null);
}
public static void system(String[] command, File directory) throws IOException {
system(asList(command), directory);
}

View File

@ -13,7 +13,7 @@ public class ExecuteException extends IOException {
}
public ExecuteException(List<String> command, int exitCode) {
this(String.format("%s failed with exit code %d", command, exitCode), exitCode);
this(String.format("%s failed (%d)", command, exitCode), exitCode);
}
public int getExitCode() {

View File

@ -2,6 +2,7 @@ package net.filebot;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import static net.filebot.Execute.*;
import static net.filebot.Logging.*;
import static net.filebot.UserFiles.*;
import static net.filebot.util.FileUtilities.*;
@ -70,22 +71,12 @@ public enum StandardRenameAction implements RenameAction {
File dest = resolveDestination(from, to);
// clonefile or reflink requires filesystem that supports copy-on-write (e.g. apfs or btrfs)
ProcessBuilder process = new ProcessBuilder();
if (Platform.isMac()) {
// -c copy files using clonefile
process.command("cp", "-c", "-f", from.getPath(), dest.getPath());
system("cp", "-c", "-f", from.getPath(), dest.getPath());
} else {
// --reflink copy files using reflink
process.command("cp", "--reflink", "--force", from.isDirectory() ? "--recursive" : "--no-target-directory", from.getPath(), dest.getPath());
}
process.directory(from.getParentFile());
process.inheritIO();
int exitCode = process.start().waitFor();
if (exitCode != 0) {
throw new IOException(String.format("%s failed (%d)", process.command(), exitCode));
system("cp", "--reflink", "--force", from.isDirectory() ? "--recursive" : "--no-target-directory", from.getPath(), dest.getPath());
}
return dest;

View File

@ -1,17 +1,13 @@
package net.filebot.platform.bsd;
import static java.nio.charset.StandardCharsets.*;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import static net.filebot.Logging.*;
import static net.filebot.Execute.*;
import static net.filebot.util.RegularExpressions.*;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.Path;
import java.util.List;
import net.filebot.util.ByteBufferOutputStream;
import net.filebot.util.XattrView;
public class ExtAttrView implements XattrView {
@ -42,27 +38,4 @@ public class ExtAttrView implements XattrView {
execute("rmextattr", "-q", "user", key, path);
}
protected CharSequence execute(String... command) throws IOException {
Process process = new ProcessBuilder(command).redirectError(Redirect.INHERIT).start();
try (ByteBufferOutputStream bb = new ByteBufferOutputStream(8 * 1024)) {
bb.transferFully(process.getInputStream());
int returnCode = process.waitFor();
String output = UTF_8.decode(bb.getByteBuffer()).toString();
// DEBUG
debug.fine(format("Execute: %s", asList(command)));
debug.finest(output);
if (returnCode == 0) {
return output;
} else {
throw new IOException(String.format("%s failed with exit code %d", command[0], returnCode));
}
} catch (InterruptedException e) {
throw new IOException(String.format("%s timed out", command[0]), e);
}
}
}

View File

@ -2,6 +2,7 @@ package net.filebot.util.ui;
import static java.util.Collections.*;
import static javax.swing.JOptionPane.*;
import static net.filebot.Execute.*;
import static net.filebot.Logging.*;
import static net.filebot.Settings.*;
@ -72,8 +73,7 @@ public final class SwingUI {
Desktop.getDesktop().browse(URI.create(uri));
} else {
// JDK BUG: Desktop.browse() doesn't work in snap environment but xdg-open works just fine
ProcessBuilder p = new ProcessBuilder("xdg-open", uri);
p.inheritIO().start();
system("xdg-open", uri);
}
} catch (Exception e) {
log.log(Level.SEVERE, "Failed to open URI: " + uri, e);

View File

@ -1,6 +1,7 @@
package net.filebot.web;
import static java.nio.charset.StandardCharsets.*;
import static net.filebot.Execute.*;
import static net.filebot.Logging.*;
import static net.filebot.util.JsonUtilities.*;
import static net.filebot.util.RegularExpressions.*;
@ -8,8 +9,6 @@ import static net.filebot.web.WebRequest.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Collection;
@ -194,17 +193,16 @@ public class AcoustIDClient implements MusicIdentificationService {
}
public Map<ChromaprintField, String> fpcalc(File file) throws IOException, InterruptedException {
Map<ChromaprintField, String> output = new EnumMap<ChromaprintField, String>(ChromaprintField.class);
Map<ChromaprintField, String> fields = new EnumMap<ChromaprintField, String>(ChromaprintField.class);
ProcessBuilder command = new ProcessBuilder(getChromaprintCommand(), file.getCanonicalPath());
Process process = command.redirectError(Redirect.INHERIT).start();
CharSequence output = execute(getChromaprintCommand(), file.getCanonicalPath());
try (Scanner scanner = new Scanner(new InputStreamReader(process.getInputStream(), UTF_8))) {
try (Scanner scanner = new Scanner(output.toString())) {
while (scanner.hasNextLine()) {
String[] value = EQUALS.split(scanner.nextLine(), 2);
if (value.length == 2) {
try {
output.put(ChromaprintField.valueOf(value[0]), value[1]);
fields.put(ChromaprintField.valueOf(value[0]), value[1]);
} catch (Exception e) {
debug.warning(e::toString);
}
@ -212,7 +210,7 @@ public class AcoustIDClient implements MusicIdentificationService {
}
}
return output;
return fields;
}
private static class MostFieldsNotNull implements Comparator<Object> {