Re[2]: Пример кода
От: real_sba http://cellwar.xyz/
Дата: 21.11.12 10:04
Оценка:
Здравствуйте, Mazay, Вы писали:

M>А покажи код вместе с вызовом read.

CDirMonitor::CDirMonitor() :
  m_Inotify(-1)
{ }

CDirMonitor::~CDirMonitor() { }

bool CDirMonitor::Init() {
  m_Inotify = inotify_init1(IN_NONBLOCK);
  if (m_Inotify == -1) {
    LOG_SYSTEM_ERROR;
    return false;
  }

  return true;
}

void CDirMonitor::AddDirectory(const std::string& dirname, uint32_t mask/*, const THandler& handler*/) {
  using namespace boost;

  int wd = inotify_add_watch(m_Inotify, dirname.c_str(), mask);
  if (wd == -1) {
    boost::system::system_error e(
      boost::system::error_code(
        errno,
        boost::system::get_system_category()),
        "CDirMonitor::AddDirectory: inotify_add_watch failed"
    );

    BOOST_THROW_EXCEPTION(e);
  }

  mutex::scoped_lock lock(m_Mutex);
  m_WatchDescriptors.insert(std::make_pair(wd, dirname));
}

void CDirMonitor::RemoveDirectory(const std::string& dirname) { }

bool CDirMonitor::Process() {
  char buf[4096];
  ssize_t len = sizeof(buf);

  while (len == sizeof(buf)) {
    len = read(m_Inotify, buf, sizeof(buf));
    if (len <= 0) {
      break;
    }
    m_ReadBuff += std::string(buf, len);
  }

  if (len < 0 && errno != EAGAIN) {
    LOG_SYSTEM_ERROR;
    return false;
  }

  while (m_ReadBuff.size() > sizeof(inotify_event)) {
    const inotify_event *iev = reinterpret_cast<const inotify_event*>(m_ReadBuff.data());

    TWatchDescriptors::const_iterator it = m_WatchDescriptors.find(iev->wd);
    if (it != m_WatchDescriptors.end()) {
      LOG_DEBUG << it->second << " | " << iev->name << " | " << iev->mask;
    }

    m_ReadBuff.erase(0, sizeof(inotify_event) + iev->len);
  }
}


int main (int argc, char *argv[]) {
  CDirMonitor dm;
  dm.Init();
  dm.AddDirectory("/home/sba/projects/temp", IN_ALL_EVENTS);

  while (1) {
    dm.Process();
    sleep(1);
  }

  return 0;
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.