title: Using read with inotify
body: <p>I have been studying inotify call, but I still a bit flaky when it comes to the read interface. These are the most relevant resourses I could find regarding how to properly interface with inotify using read(2):</p>
  
  <ul>
  <li><a href="http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html" rel="noreferrer">http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html</a></li>
  <li><a href="http://www.linuxjournal.com/article/8478" rel="noreferrer">http://www.linuxjournal.com/article/8478</a></li>
  </ul>
  
  <p>They both implement it in the same way, they first define the following sizes:</p>
  
  <pre><code>#define EVENT_SIZE  ( sizeof (struct inotify_event) )
  #define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 )
  </code></pre>
  
  <p>And then they use them in this manner:</p>
  
  <pre><code>length = read( fd, buffer, BUF_LEN );  
  
  if ( length &lt; 0 ) {
      perror( "read" );
  }  
  
  while ( i &lt; length ) {
      struct inotify_event *event = ( struct inotify_event * ) &amp;buffer[ i ];
      /* some processing */
      i += EVENT_SIZE + event-&gt;len;
  }
  </code></pre>
  
  <p>Now, we know name is part of <code>struct inotify_event</code> and that it has variable length. So, couldn't the last inotify_event in buffer be truncated?</p>
  
  <p>Suppose there is 1023 inotify_events with a path of 16 bytes and one with a path of 32 bytes. What will happen then? Will the later truncated? Or will the kernel see that it won't fit in the buffer and leave it all altogether?</p>