jeudi 17 octobre 2019

Is inotify a one shot only solution

ALL,

I have a program whiuch utilizes inotify.

What it does is it start watching the directory for the file being created. When it happens the program reads the content then deletes the file.

Then the user initiates an action which will create the same file again. However, inotify does not see that the file has been created a second time and the file is not processed.

Code is as follows:

m_wd1 = inotify_add_watch( m_fd, "/tmp", IN_CREATE );
if( m_wd1 == -1 )
{
}
else
{
    while( true )
    {
        poll_num = poll( &fds, nfds, -1 );
        if( poll_num == -1 )
        {
            if( errno == EINTR )
                continue;
            syslog( LOG_ERR, "Fail to run poll" );
            result = 1;
        }
        else if( poll_num > 0 && ( fds.revents & POLLIN ) )
        {
            syslog( LOG_DEBUG, "Polling is successful" );
            for( ;; )
            {
                len = read( m_fd, buf, sizeof( buf ) );
                if( len == -1 && errno != EAGAIN )
                {
                    syslog( LOG_ERR, "Failure to read the inotify event" );
                    result = 1;
                    break;
                }
                for( ptr = buf; ptr < buf + len; ptr += sizeof( struct inotify_event ) + event->len )
                {
                    event = (const struct inotify_event *) ptr;
                    if( event->mask & IN_CREATE )
                    {
                        std::string name( event->name );
                        if( name == "scan_results" )
                        {
                            fileCreated = true;
                            break;
                        }
                    }
                }
                if( fileCreated || result )
                    break;
            }
        }
        // process file

The while() loop runs only once. When the action happens second time I see a message "Polling is successful".

Should I add IN_MODIFY as a mask for inotify?

If it matters - this code is running inside std::thread.

TIA!

Aucun commentaire:

Enregistrer un commentaire