INotifyPosix.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Util/INotify.h>
  6. #include <AnKi/Util/Logger.h>
  7. #include <sys/inotify.h>
  8. #include <poll.h>
  9. #include <unistd.h>
  10. namespace anki {
  11. Error INotify::initInternal()
  12. {
  13. ANKI_ASSERT(m_fd < 0 && m_watch < 0);
  14. Error err = Error::kNone;
  15. m_fd = inotify_init();
  16. if(m_fd < 0)
  17. {
  18. ANKI_UTIL_LOGE("inotify_init() failed: %s", strerror(errno));
  19. err = Error::kFunctionFailed;
  20. }
  21. if(!err)
  22. {
  23. m_watch = inotify_add_watch(m_fd, &m_path[0], IN_MODIFY | IN_CREATE | IN_DELETE | IN_IGNORED | IN_DELETE_SELF);
  24. if(m_watch < 0)
  25. {
  26. ANKI_UTIL_LOGE("inotify_add_watch() failed: %s", strerror(errno));
  27. err = Error::kFunctionFailed;
  28. }
  29. }
  30. if(err)
  31. {
  32. destroyInternal();
  33. }
  34. return err;
  35. }
  36. void INotify::destroyInternal()
  37. {
  38. if(m_watch >= 0)
  39. {
  40. int err = inotify_rm_watch(m_fd, m_watch);
  41. if(err < 0)
  42. {
  43. ANKI_UTIL_LOGE("inotify_rm_watch() failed: %s\n", strerror(errno));
  44. }
  45. m_watch = -1;
  46. }
  47. if(m_fd >= 0)
  48. {
  49. int err = close(m_fd);
  50. if(err < 0)
  51. {
  52. ANKI_UTIL_LOGE("close() failed: %s\n", strerror(errno));
  53. }
  54. m_fd = -1;
  55. }
  56. }
  57. Error INotify::pollEvents(Bool& modified)
  58. {
  59. ANKI_ASSERT(m_fd >= 0 && m_watch >= 0);
  60. Error err = Error::kNone;
  61. modified = false;
  62. while(true)
  63. {
  64. pollfd pfd = {m_fd, POLLIN, 0};
  65. int ret = poll(&pfd, 1, 0);
  66. if(ret < 0)
  67. {
  68. ANKI_UTIL_LOGE("poll() failed: %s", strerror(errno));
  69. err = Error::kFunctionFailed;
  70. break;
  71. }
  72. else if(ret == 0)
  73. {
  74. // No events, move on
  75. break;
  76. }
  77. else
  78. {
  79. // Process the new event
  80. Array<U8, 2_KB> readBuff;
  81. PtrSize nbytes = read(m_fd, &readBuff[0], sizeof(readBuff));
  82. if(nbytes > 0)
  83. {
  84. inotify_event* event = reinterpret_cast<inotify_event*>(&readBuff[0]);
  85. if(event->mask & IN_IGNORED)
  86. {
  87. // File was moved or deleted. Some editors on save they delete the file and move another file to
  88. // its place. In that case the m_fd and the m_watch need to be re-created.
  89. m_watch = -1; // Watch descriptor was removed implicitly
  90. destroyInternal();
  91. err = initInternal();
  92. if(err)
  93. {
  94. break;
  95. }
  96. }
  97. else
  98. {
  99. modified = true;
  100. }
  101. }
  102. else
  103. {
  104. ANKI_UTIL_LOGE("read() failed to read the expected size of data: %s", strerror(errno));
  105. err = Error::kFunctionFailed;
  106. break;
  107. }
  108. }
  109. }
  110. return err;
  111. }
  112. } // end namespace anki