Advertisement
decimusphostle

inotify asio

Mar 5th, 2013
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/asio.hpp>
  3. #include <boost/bind.hpp>
  4. #include <sys/inotify.h>
  5.  
  6. namespace asio = boost::asio;
  7.  
  8. struct test_inotify_asio
  9. {
  10.     asio::io_service io_svc;
  11.     asio::posix::stream_descriptor stream_desc;
  12.     std::string file_path;
  13.     int raw_fd;
  14.     int watch_fd;
  15.     asio::streambuf buf;
  16.  
  17.     test_inotify_asio(const std::string & file_path_)
  18.     : stream_desc(io_svc),
  19.       file_path(file_path_),
  20.       buf(1024)
  21.     {
  22.         if(0 < (raw_fd = inotify_init()))
  23.         {
  24.             if(0 > (watch_fd = inotify_add_watch
  25.                        (raw_fd,
  26.                         file_path.c_str(),
  27.                         IN_MODIFY | IN_ACCESS | IN_CREATE | IN_DELETE | IN_OPEN)))
  28.             {
  29.                 throw std::runtime_error("Unable to add watch on: " +
  30.                              file_path);
  31.             }
  32.         }
  33.         else
  34.         {
  35.             throw std::runtime_error("Unable to initialize inotify");
  36.         }
  37.  
  38.         stream_desc.assign(raw_fd);
  39.  
  40.         start_notify_handler();
  41.     };
  42.  
  43.     void start_notify_handler()
  44.     {
  45.       stream_desc
  46.       .async_read_some(buf.prepare(buf.max_size()),
  47.                        boost::bind
  48.                               (&test_inotify_asio::notify_handler,
  49.                                this,
  50.                                asio::placeholders::error,
  51.                                asio::placeholders::bytes_transferred));
  52.     }
  53.  
  54.     void notify_handler(const boost::system::error_code&,
  55.                         std::size_t transferred)
  56.     {
  57.       size_t processed = 0;
  58.  
  59.       while(transferred - processed >= sizeof(inotify_event))
  60.       {
  61.           const char* cdata = processed + asio::buffer_cast<const char*>(buf.data());
  62.  
  63.           const inotify_event* ievent = reinterpret_cast<const inotify_event*>(cdata);
  64.  
  65.           processed += sizeof(inotify_event) + ievent->len;
  66.  
  67.           if(ievent->len > 0 && ievent->mask & IN_MODIFY)
  68.           {
  69.               std::cout << "File modified: "
  70.                     << file_path
  71.                     << "[" << ievent->name << "]\n";
  72.           }
  73.           else if(ievent->len > 0 && ievent->mask & IN_ACCESS)
  74.           {
  75.               std::cout << "File accessed: "
  76.                     << file_path
  77.                     << "[" << ievent->name << "]\n";
  78.           }
  79.           else if(ievent->len > 0 && ievent->mask & IN_OPEN)
  80.           {
  81.                   std::cout << "File opened: "
  82.                     << file_path
  83.                     << "[" << ievent->name << "]\n";
  84.           }
  85.           else if(ievent->len > 0 && ievent->mask & IN_CREATE)
  86.           {
  87.               std::cout << "File created: "
  88.                     << file_path
  89.                     << "[" << ievent->name << "]\n";
  90.           }
  91.           else if(ievent->len > 0 && ievent->mask & IN_DELETE)
  92.           {
  93.                   std::cout << "File deleted: "
  94.                         << file_path
  95.                         << "[" << ievent->name << "]\n";
  96.           }
  97.       }
  98.  
  99.       start_notify_handler();
  100.     }
  101.  
  102.     void operator()()
  103.     {
  104.         io_svc.run();
  105.     }
  106.  
  107.     ~test_inotify_asio()
  108.     {
  109.         inotify_rm_watch(raw_fd, watch_fd);
  110.  
  111.         close(raw_fd);
  112.     }
  113. };
  114.  
  115. int main()
  116. {
  117.     test_inotify_asio tia("/abs/path/to/file");
  118.  
  119.     tia();
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement