Hello,

I just started using the APR for the File-IO Part of a CGI-Application that
I'm working on.

I'm dealing with a couple of processes performing read/write accesses on
files concurrently, where the reading process locks the file by using
APR_FLOCK_SHARED and the writing process uses APR_FLOCK_EXCLUSIVE.

In my Log-Files I repeatedly spot an error reported by the read-method:
apr_file_read_full returns APR_EOF as status-value.

In the documentation I read the remark that this can happen, but I'm unsure
what could be the true cause and how to deal with this.

<code>

        if( ( rv = apr_file_open( &file, 
                                  session_file_path, 
                                  APR_CREATE | 
                                  APR_READ, 
                                  APR_OS_DEFAULT, p ) ) )
        {
            DEBUG_OUT_STR( "Konnte Datei nicht mit Lesezugriff oeffnen", 
                           session_file_path );
            DEBUG_OUT_STR( "APR-Error", apr_strerror( rv, error_buf, 1000 ) );

            break;
        }

        if( ( rv = apr_file_lock( file, APR_FLOCK_SHARED ) ) )
        {
            DEBUG_OUT_STR( "Konnte keinen shared lock auf Datei etablieren", 
                           session_file_path );
            DEBUG_OUT_STR( "APR-Error", apr_strerror( rv, error_buf, 1000 ) );

            break;
        }

        if( ( rv = apr_file_info_get( &finfo, APR_FINFO_SIZE, file ) ) )
        {

            DEBUG_OUT_STR( "Konnte die Dateigroesse nicht ermitteln", 
                           session_file_path );
            DEBUG_OUT_STR( "APR-Error", apr_strerror( rv, error_buf, 1000 ) );

            break;
        }

        if( finfo.size )
        {
            file_size = finfo.size;
            self->data = (char*) malloc( ( finfo.size + 1 ) * sizeof( char ) );
            if( ( rv =apr_file_read_full( file, self->data, finfo.size, 
&file_size
) ) ) 
            {
                //This is, where it happens
                DEBUG_OUT_STR( "Session-Fehler: Konnte aus Datei nicht lesen:",
                           session_file_path );
                DEBUG_OUT_STR( "APR-Error", apr_strerror( rv, error_buf, 1000 ) 
);

                break;
            }
            
            self->data[file_size] = 0;

        }
</code>

Is there
i.  any mistake in this code-snippet, that could lead to the EOF-Access or
ii. or, if not, a reasonable/unugly strategy to deal with this?

After all, the filesize is determined after establishing the lock, and even
though a problem is reported, the accessed file is not broken at all.

Greets
Sebastian

Reply via email to