inotify and recursion

Artur Skawina art.08.09 at gmail.com
Fri Dec 27 06:13:52 PST 2013


On 12/27/13 14:28, David Eagen wrote:
> I had trouble getting the file name from the event. I think it's because the inotify module has name defined as char[0]. So I did this, which prints the name by using the extra data beyond the inotify_event struct itself:
> 
> 
>     void* buf = GC.malloc(bufsiz);
> 
>     /* wait for an event to occur */
>     size_t readlen = read(inotfd, buf, bufsiz);
>     inotify_event* event = cast(inotify_event*) (buf);
> 
>     /* process event struct here */
>     writeln("Received inotify event:");
>     writeln("Bytes read: ", readlen);
>     writeln("Length: ", event.len);
>     writeln("Name:", cast(char[])(buf[event.len..readlen]));
    
   writeln("Name:", (cast(char*)&event.name)[0..event.len-1]); // 2do: strip any extra trailing \0s.

It's probably easier (and safer) if you do it like this:

   struct MyInotifyEvent(size_t BS) {
      inotify_event event;
      char[BS] buffer;
      alias event this;
   }
   [...]
   enum bufsiz = inotify_event.sizeof + PATH_MAX + 1;
   auto event = cast(MyInotifyEvent!bufsiz*)malloc(bufsiz);
   [...]
   writeln("Name:", event.buffer[0..event.len-1]); // 2do: strip any extra trailing \0s.
  
artur


More information about the Digitalmars-d-learn mailing list