Search code examples
caprapache-portable-runtime

How can I create a file using apr_file_open()


I am making the following call to the Apache Portable Runtime library (version 1.4):

result = apr_file_open(
    &file, // new file handle
    pathname, // file name          
    APR_FOPEN_CREATE | // create file if not there 
    APR_FOPEN_EXCL | // error if file was there already
    APR_FOPEN_APPEND | // move to end of file on open
    APR_FOPEN_BINARY | // binary mode (ignored on UNIX)
    APR_FOPEN_XTHREAD | // allow multiple threads to use file
    0, // flags
    APR_OS_DEFAULT |
    0, // permissions
    pool // memory pool to use
);

if ( APR_SUCCESS != result ) {
    fprintf(
        stderr,
        "could not create file \"%s\": %s",
        pathname,
        apr_errorstr( result )
    );
}

and pathname contains the string /tmp/tempfile20110614091201.

I keep getting the error "Permission denied" (result code APR_EACCES) but I have permissions to read/write to /tmp - what could be causing this?


Solution

  • I needed the APR_FOPEN_WRITE flag. I had mistakenly assumed the APR_FOPEN_APPEND flag was enough.

    So, the call that worked was:

    
        result = apr_file_open(
            &file, // new file handle
            pathname, // file name          
            APR_FOPEN_CREATE | // create file if not there 
            APR_FOPEN_EXCL | // error if file was there already
            APR_FOPEN_WRITE | // open for writing
            APR_FOPEN_APPEND | // move to end of file on open
            APR_FOPEN_BINARY | // binary mode (ignored on UNIX)
            APR_FOPEN_XTHREAD | // allow multiple threads to use file
            0, // flags
            APR_OS_DEFAULT |
            0, // permissions
            pool // memory pool to use
        );