Skip to content

Commit b63c4e6

Browse files
committed
Add support for renames/moves
1 parent 3899227 commit b63c4e6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static engineering.swat.watch.impl.mac.apis.FileSystemEvents.FSEventStreamEventFlag.ITEM_INODE_META_MOD;
3434
import static engineering.swat.watch.impl.mac.apis.FileSystemEvents.FSEventStreamEventFlag.ITEM_MODIFIED;
3535
import static engineering.swat.watch.impl.mac.apis.FileSystemEvents.FSEventStreamEventFlag.ITEM_REMOVED;
36+
import static engineering.swat.watch.impl.mac.apis.FileSystemEvents.FSEventStreamEventFlag.ITEM_RENAMED;
3637
import static engineering.swat.watch.impl.mac.apis.FileSystemEvents.FSEventStreamEventFlag.MUST_SCAN_SUB_DIRS;
3738
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
3839
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
@@ -41,6 +42,7 @@
4142

4243
import java.io.Closeable;
4344
import java.io.IOException;
45+
import java.nio.file.Files;
4446
import java.nio.file.Path;
4547
import java.util.Arrays;
4648

@@ -153,6 +155,19 @@ public void callback(Pointer streamRef, Pointer clientCallBackInfo,
153155
if (any(flags[i], MUST_SCAN_SUB_DIRS.mask)) {
154156
handler.handle(OVERFLOW, null);
155157
}
158+
if (any(flags[i], ITEM_RENAMED.mask)) {
159+
// For now, check if the file exists to determine if the
160+
// event pertains to the target of the rename (if it
161+
// exists) or to the source (else). This is an
162+
// approximation. It might be more accurate to maintain
163+
// an internal index (but getting the concurrency right
164+
// requires care).
165+
if (Files.exists(Path.of(paths[i]))) {
166+
handler.handle(ENTRY_CREATE, context);
167+
} else {
168+
handler.handle(ENTRY_DELETE, context);
169+
}
170+
}
156171
}
157172
}
158173

0 commit comments

Comments
 (0)