Skip to content

Commit dd9c4cb

Browse files
committed
WIP
1 parent 5309aa6 commit dd9c4cb

File tree

4 files changed

+15
-55
lines changed

4 files changed

+15
-55
lines changed

src/main/java/engineering/swat/watch/impl/mac/NativeEventStream.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,9 @@
2626
*/
2727
package engineering.swat.watch.impl.mac;
2828

29-
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
30-
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
31-
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
32-
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
33-
3429
import java.io.Closeable;
3530
import java.io.IOException;
3631
import java.nio.file.Path;
37-
import java.util.function.BiConsumer;
38-
39-
import org.checkerframework.checker.nullness.qual.Nullable;
40-
41-
import engineering.swat.watch.impl.mac.jni.FileSystemEvents;
4232

4333
// Note: This file is designed to be the only place in this package where JNA is
4434
// used and/or the native APIs are invoked. If the need to do so arises outside
@@ -58,6 +48,10 @@
5848
* </p>
5949
*/
6050
class NativeEventStream implements Closeable {
51+
static {
52+
NativeLibrary.load();
53+
}
54+
6155
private final Path path;
6256
private final NativeEventHandler handler;
6357
private volatile boolean closed;
@@ -76,7 +70,7 @@ public synchronized void open() {
7670
closed = false;
7771
}
7872

79-
nativeWatch = FileSystemEvents.start(path.toString(), handler);
73+
nativeWatch = NativeLibrary.start(path.toString(), handler);
8074
}
8175

8276
// -- Closeable --
@@ -89,6 +83,6 @@ public synchronized void close() {
8983
closed = true;
9084
}
9185

92-
FileSystemEvents.stop(nativeWatch);
86+
NativeLibrary.stop(nativeWatch);
9387
}
9488
}

src/main/java/engineering/swat/watch/impl/mac/jni/NativeLibrary.java renamed to src/main/java/engineering/swat/watch/impl/mac/NativeLibrary.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2525
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626
*/
27-
package engineering.swat.watch.impl.mac.jni;
27+
package engineering.swat.watch.impl.mac;
2828

2929
import static java.nio.file.attribute.PosixFilePermission.*;
3030

@@ -36,6 +36,10 @@
3636
import java.util.Set;
3737

3838
public class NativeLibrary {
39+
40+
public static native long start(String path, NativeEventHandler handler);
41+
public static native void stop(long watchId);
42+
3943
private static boolean isMac() {
4044
var os = System.getProperty("os.name");
4145
return os != null && (os.toLowerCase().contains("mac") || os.toLowerCase().contains("darwin"));
@@ -74,7 +78,7 @@ public static void load() {
7478

7579
private static void loadLibrary(String path) {
7680
try {
77-
var localFile = FileSystemEvents.class.getResource(path);
81+
var localFile = NativeLibrary.class.getResource(path);
7882
if (localFile != null && localFile.getProtocol().equals("file")) {
7983
System.load(localFile.getPath());
8084
return;
@@ -83,7 +87,7 @@ private static void loadLibrary(String path) {
8387
// so we have to copy it out and load that file instead
8488
var localCopy = Files.createTempFile("watch", ".dylib"/* , PRIVATE_FILE*/);
8589
localCopy.toFile().deleteOnExit();
86-
try (var libStream = FileSystemEvents.class.getResourceAsStream(path)) {
90+
try (var libStream = NativeLibrary.class.getResourceAsStream(path)) {
8791
if (libStream != null) {
8892
try (var writer = Files.newOutputStream(localCopy, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
8993
libStream.transferTo(writer);

src/main/java/engineering/swat/watch/impl/mac/jni/FileSystemEvents.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/rust/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl HandlerExecutor {
4646

4747
#[unsafe(no_mangle)]
4848
#[allow(unused_variables)]
49-
pub extern "system" fn Java_engineering_swat_watch_impl_mac_jni_FileSystemEvents_start<'local>(
49+
pub extern "system" fn Java_engineering_swat_watch_impl_mac_NativeLibrary_start<'local>(
5050
mut env: JNIEnv<'local>,
5151
class: JClass<'local>,
5252
path: JString<'local>,
@@ -63,7 +63,7 @@ pub extern "system" fn Java_engineering_swat_watch_impl_mac_jni_FileSystemEvents
6363

6464
#[unsafe(no_mangle)]
6565
#[allow(unused_variables)]
66-
pub extern "system" fn Java_engineering_swat_watch_impl_mac_jni_FileSystemEvents_stop<'local>(
66+
pub extern "system" fn Java_engineering_swat_watch_impl_mac_NativeLibrary_stop<'local>(
6767
env: JNIEnv<'local>,
6868
class: JClass<'local>,
6969
stream: jlong,

0 commit comments

Comments
 (0)