|
27 | 27 | // System dependent filesystem routines |
28 | 28 |
|
29 | 29 | #include "../SDL_sysfilesystem.h" |
| 30 | +#include "../../SDL_hashtable.h" |
| 31 | +#include "../../events/SDL_events_c.h" |
30 | 32 |
|
31 | 33 | #include <stdio.h> |
32 | 34 | #include <string.h> |
|
39 | 41 | #include "../../core/android/SDL_android.h" |
40 | 42 | #endif |
41 | 43 |
|
| 44 | +#ifdef HAVE_INOTIFY |
| 45 | +#include <sys/inotify.h> |
| 46 | +#include <linux/limits.h> |
| 47 | +#endif |
42 | 48 |
|
43 | 49 | bool SDL_SYS_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback cb, void *userdata) |
44 | 50 | { |
@@ -410,6 +416,161 @@ bool SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info) |
410 | 416 | return true; |
411 | 417 | } |
412 | 418 |
|
| 419 | +#ifdef HAVE_INOTIFY |
| 420 | +static int inotify_fd = -1; |
| 421 | +static SDL_HashTable *watch_descriptor_table = NULL; // stores directory or file path for a watch descriptor |
| 422 | +static SDL_Mutex *file_watch_lock = NULL; |
| 423 | + |
| 424 | +#ifdef HAVE_INOTIFY_INIT1 |
| 425 | +static int SDL_inotify_init1(void) |
| 426 | +{ |
| 427 | + return inotify_init1(IN_NONBLOCK | IN_CLOEXEC); |
| 428 | +} |
| 429 | +#else |
| 430 | +static int SDL_inotify_init1(void) |
| 431 | +{ |
| 432 | + int fd = inotify_init(); |
| 433 | + if (fd < 0) { |
| 434 | + return -1; |
| 435 | + } |
| 436 | + fcntl(fd, F_SETFL, O_NONBLOCK); |
| 437 | + fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 438 | + return fd; |
| 439 | +} |
| 440 | +#endif // HAVE_INOTIFY_INIT1 |
| 441 | +#endif // HAVE_INOTIFY |
| 442 | + |
| 443 | +bool SDL_SYS_WatchFileForChanges(const char *path) |
| 444 | +{ |
| 445 | +#ifdef HAVE_INOTIFY |
| 446 | + if (!watch_descriptor_table) { |
| 447 | + watch_descriptor_table = SDL_CreateHashTable(0, false, SDL_HashID, SDL_KeyMatchID, SDL_DestroyHashValue, NULL); |
| 448 | + if (!watch_descriptor_table) { |
| 449 | + return false; |
| 450 | + } |
| 451 | + inotify_fd = SDL_inotify_init1(); |
| 452 | + if (inotify_fd == -1) { |
| 453 | + SDL_DestroyHashTable(watch_descriptor_table); |
| 454 | + watch_descriptor_table = NULL; |
| 455 | + return SDL_SetError("Could not initialize inotify: %s", strerror(errno)); |
| 456 | + } |
| 457 | + file_watch_lock = SDL_CreateMutex(); |
| 458 | + if (!file_watch_lock) { |
| 459 | + SDL_DestroyHashTable(watch_descriptor_table); |
| 460 | + watch_descriptor_table = NULL; |
| 461 | + close(inotify_fd); |
| 462 | + inotify_fd = -1; |
| 463 | + return false; |
| 464 | + } |
| 465 | + } |
| 466 | + |
| 467 | + char *p = SDL_strdup(path); |
| 468 | + if (!p) { |
| 469 | + return false; |
| 470 | + } |
| 471 | + // remove separator at the end of the path |
| 472 | + const size_t slen = SDL_strlen(p); |
| 473 | + if (p[slen - 1] == '/') { |
| 474 | + p[slen - 1] = '\0'; |
| 475 | + } |
| 476 | + |
| 477 | + SDL_LockMutex(file_watch_lock); |
| 478 | + int wd = inotify_add_watch(inotify_fd, p, IN_MODIFY); |
| 479 | + if (wd == -1) { |
| 480 | + SDL_UnlockMutex(file_watch_lock); |
| 481 | + SDL_free(p); |
| 482 | + return SDL_SetError("inotify_add_watch failed: %s", strerror(errno)); |
| 483 | + } |
| 484 | + if (!SDL_InsertIntoHashTable(watch_descriptor_table, (void *)(intptr_t)wd, p, false)) { |
| 485 | + inotify_rm_watch(inotify_fd, wd); |
| 486 | + SDL_UnlockMutex(file_watch_lock); |
| 487 | + SDL_free(p); |
| 488 | + return false; |
| 489 | + } |
| 490 | + SDL_UnlockMutex(file_watch_lock); |
| 491 | + return true; |
| 492 | +#else |
| 493 | + return SDL_Unsupported(); |
| 494 | +#endif // HAVE_INOTIFY |
| 495 | +} |
| 496 | + |
| 497 | +void SDL_SYS_UpdateFileWatch(void) |
| 498 | +{ |
| 499 | +#ifdef HAVE_INOTIFY |
| 500 | + if (inotify_fd >= 0) { |
| 501 | + SDL_LockMutex(file_watch_lock); |
| 502 | + union |
| 503 | + { |
| 504 | + struct inotify_event event; |
| 505 | + char storage[4096]; |
| 506 | + char enough_for_inotify[sizeof(struct inotify_event) + NAME_MAX + 1]; |
| 507 | + } buf; |
| 508 | + ssize_t bytes; |
| 509 | + size_t remain = 0; |
| 510 | + size_t len; |
| 511 | + char path[PATH_MAX]; |
| 512 | + |
| 513 | + bytes = read(inotify_fd, &buf, sizeof(buf)); |
| 514 | + |
| 515 | + if (bytes > 0) { |
| 516 | + remain = (size_t)bytes; |
| 517 | + } |
| 518 | + |
| 519 | + while (remain > 0) { |
| 520 | + const char *watched_path; |
| 521 | + if (SDL_FindInHashTable(watch_descriptor_table, (void *)(intptr_t)buf.event.wd, (const void **)&watched_path)) { |
| 522 | + const char *path_tmp; |
| 523 | + SDL_EventType event_type; |
| 524 | + bool post_event = true; |
| 525 | + if (buf.event.mask & IN_Q_OVERFLOW) { |
| 526 | + event_type = SDL_EVENT_FILE_WATCH_ERROR; |
| 527 | + path_tmp = NULL; |
| 528 | + } else if (buf.event.len != 0) { |
| 529 | + (void)SDL_snprintf(path, SDL_arraysize(path), "%s/%s", watched_path, buf.event.name); |
| 530 | + event_type = SDL_EVENT_FILE_CHANGED; |
| 531 | + path_tmp = SDL_CreateTemporaryString(path); |
| 532 | + post_event = (path_tmp != NULL); |
| 533 | + } else { |
| 534 | + event_type = SDL_EVENT_FILE_CHANGED; |
| 535 | + path_tmp = SDL_CreateTemporaryString(watched_path); |
| 536 | + post_event = (path_tmp != NULL); |
| 537 | + } |
| 538 | + if (post_event && SDL_EventEnabled(event_type)) { |
| 539 | + SDL_Event event; |
| 540 | + SDL_zero(event); |
| 541 | + event.type = event_type; |
| 542 | + event.common.timestamp = 0; |
| 543 | + event.file_watch.path = path_tmp; |
| 544 | + SDL_PushEvent(&event); |
| 545 | + } |
| 546 | + } |
| 547 | + |
| 548 | + len = sizeof(struct inotify_event) + buf.event.len; |
| 549 | + remain -= len; |
| 550 | + |
| 551 | + if (remain != 0) { |
| 552 | + SDL_memmove(&buf.storage[0], &buf.storage[len], remain); |
| 553 | + } |
| 554 | + } |
| 555 | + SDL_UnlockMutex(file_watch_lock); |
| 556 | + } |
| 557 | +#endif // HAVE_INOTIFY |
| 558 | +} |
| 559 | + |
| 560 | +void SDL_SYS_QuitFileWatch(void) |
| 561 | +{ |
| 562 | +#ifdef HAVE_INOTIFY |
| 563 | + if (inotify_fd >= 0) { |
| 564 | + close(inotify_fd); |
| 565 | + inotify_fd = -1; |
| 566 | + SDL_DestroyMutex(file_watch_lock); |
| 567 | + file_watch_lock = NULL; |
| 568 | + SDL_DestroyHashTable(watch_descriptor_table); |
| 569 | + watch_descriptor_table = NULL; |
| 570 | + } |
| 571 | +#endif // HAVE_INOTIFY |
| 572 | +} |
| 573 | + |
413 | 574 | // Note that this is actually part of filesystem, not fsops, but everything that uses posix fsops uses this implementation, even with separate filesystem code. |
414 | 575 | char *SDL_SYS_GetCurrentDirectory(void) |
415 | 576 | { |
|
0 commit comments