|
| 1 | +package org.cryptomator.macos.revealpath; |
| 2 | + |
| 3 | +import org.cryptomator.integrations.common.OperatingSystem; |
| 4 | +import org.cryptomator.integrations.common.Priority; |
| 5 | +import org.cryptomator.integrations.revealpath.RevealFailedException; |
| 6 | +import org.cryptomator.integrations.revealpath.RevealPathService; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.LinkOption; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.attribute.BasicFileAttributes; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +@Priority(100) |
| 17 | +@OperatingSystem(OperatingSystem.Value.MAC) |
| 18 | +public class OpenCmdRevealPathService implements RevealPathService { |
| 19 | + |
| 20 | + @Override |
| 21 | + public void reveal(Path p) throws RevealFailedException { |
| 22 | + try { |
| 23 | + var attrs = Files.readAttributes(p, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); |
| 24 | + ProcessBuilder pb = new ProcessBuilder().command("open", attrs.isDirectory() ? "" : "-R", p.toString()); |
| 25 | + var process = pb.start(); |
| 26 | + try (var reader = process.errorReader()) { |
| 27 | + if (process.waitFor(5000, TimeUnit.MILLISECONDS)) { |
| 28 | + int exitValue = process.exitValue(); |
| 29 | + if (process.exitValue() != 0) { |
| 30 | + String error = reader.lines().collect(Collectors.joining()); |
| 31 | + throw new RevealFailedException("open command exited with value " + exitValue + " and error message: " + error); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } catch (IOException e) { |
| 36 | + throw new RevealFailedException(e); |
| 37 | + } catch (InterruptedException e) { |
| 38 | + Thread.currentThread().interrupt(); |
| 39 | + throw new RevealFailedException(e); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean isSupported() { |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments