diff --git a/source/net/filebot/Execute.java b/source/net/filebot/Execute.java index c87eb279..c4635be1 100644 --- a/source/net/filebot/Execute.java +++ b/source/net/filebot/Execute.java @@ -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); } diff --git a/source/net/filebot/ExecuteException.java b/source/net/filebot/ExecuteException.java index 2abb4af6..ca8e57c2 100644 --- a/source/net/filebot/ExecuteException.java +++ b/source/net/filebot/ExecuteException.java @@ -13,7 +13,7 @@ public class ExecuteException extends IOException { } public ExecuteException(List 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() { diff --git a/source/net/filebot/StandardRenameAction.java b/source/net/filebot/StandardRenameAction.java index 715f4396..fa36b681 100644 --- a/source/net/filebot/StandardRenameAction.java +++ b/source/net/filebot/StandardRenameAction.java @@ -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; diff --git a/source/net/filebot/platform/bsd/ExtAttrView.java b/source/net/filebot/platform/bsd/ExtAttrView.java index df09399d..f2db1179 100644 --- a/source/net/filebot/platform/bsd/ExtAttrView.java +++ b/source/net/filebot/platform/bsd/ExtAttrView.java @@ -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); - } - } - } diff --git a/source/net/filebot/util/ui/SwingUI.java b/source/net/filebot/util/ui/SwingUI.java index 951b97f0..9b40aeac 100644 --- a/source/net/filebot/util/ui/SwingUI.java +++ b/source/net/filebot/util/ui/SwingUI.java @@ -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); diff --git a/source/net/filebot/web/AcoustIDClient.java b/source/net/filebot/web/AcoustIDClient.java index 31510eee..18eda093 100644 --- a/source/net/filebot/web/AcoustIDClient.java +++ b/source/net/filebot/web/AcoustIDClient.java @@ -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 fpcalc(File file) throws IOException, InterruptedException { - Map output = new EnumMap(ChromaprintField.class); + Map fields = new EnumMap(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 {