|
| 1 | +/* |
| 2 | + * BSD 2-Clause License |
| 3 | + * |
| 4 | + * Copyright (c) 2023, Swat.engineering |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | + * list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 20 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +package engineering.swat.watch.impl.mac; |
| 28 | + |
| 29 | +import static java.nio.file.attribute.PosixFilePermission.*; |
| 30 | + |
| 31 | +import java.nio.file.Files; |
| 32 | +import java.nio.file.StandardOpenOption; |
| 33 | +import java.nio.file.attribute.FileAttribute; |
| 34 | +import java.nio.file.attribute.PosixFilePermission; |
| 35 | +import java.nio.file.attribute.PosixFilePermissions; |
| 36 | +import java.util.Set; |
| 37 | + |
| 38 | +public class NativeLibrary { |
| 39 | + |
| 40 | + public static native long start(String path, NativeEventHandler handler); |
| 41 | + public static native void stop(long watchId); |
| 42 | + |
| 43 | + private static boolean isMac() { |
| 44 | + var os = System.getProperty("os.name"); |
| 45 | + return os != null && (os.toLowerCase().contains("mac") || os.toLowerCase().contains("darwin")); |
| 46 | + } |
| 47 | + |
| 48 | + private static boolean isAarch64() { |
| 49 | + var arch = System.getProperty("os.arch"); |
| 50 | + return arch != null && arch.toLowerCase().equals("aarch64"); |
| 51 | + } |
| 52 | + |
| 53 | + private static volatile boolean loaded = false; |
| 54 | + public static void load() { |
| 55 | + if (loaded) { |
| 56 | + return; |
| 57 | + } |
| 58 | + try { |
| 59 | + if (!isMac()) { |
| 60 | + throw new IllegalStateException("We should not be loading FileSystemEvents api on non mac machines"); |
| 61 | + } |
| 62 | + String path = "/engineering/swat/watch/jni/"; |
| 63 | + if (isAarch64()) { |
| 64 | + path += "macos-aarch64/"; |
| 65 | + } |
| 66 | + else { |
| 67 | + path += "macos-x64/"; |
| 68 | + } |
| 69 | + path += "librust_fsevents_jni.dylib"; |
| 70 | + |
| 71 | + loadLibrary(path); |
| 72 | + } finally { |
| 73 | + loaded = true; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static FileAttribute<Set<PosixFilePermission>> PRIVATE_FILE = PosixFilePermissions.asFileAttribute(Set.of(OWNER_READ, OWNER_WRITE , OWNER_EXECUTE)); |
| 78 | + |
| 79 | + private static void loadLibrary(String path) { |
| 80 | + try { |
| 81 | + var localFile = NativeLibrary.class.getResource(path); |
| 82 | + if (localFile != null && localFile.getProtocol().equals("file")) { |
| 83 | + System.load(localFile.getPath()); |
| 84 | + return; |
| 85 | + } |
| 86 | + // in most cases the file is inside of a jar |
| 87 | + // so we have to copy it out and load that file instead |
| 88 | + var localCopy = Files.createTempFile("watch", ".dylib"/* , PRIVATE_FILE*/); |
| 89 | + localCopy.toFile().deleteOnExit(); |
| 90 | + try (var libStream = NativeLibrary.class.getResourceAsStream(path)) { |
| 91 | + if (libStream != null) { |
| 92 | + try (var writer = Files.newOutputStream(localCopy, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { |
| 93 | + libStream.transferTo(writer); |
| 94 | + } |
| 95 | + System.load(localCopy.toString()); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + catch (Throwable e) { |
| 100 | + throw new IllegalStateException("We could not load: " + path, e); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments