4

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?

PP.
  • 10,764
  • 7
  • 45
  • 59
  • 1
    I created this question and answer because I searched for it myself on the Internet for a while and could not find the answer. – PP. Jun 14 '11 at 09:54
  • Hello. Could you please tell me how can you append the HTTP request's parameters and their values into the file after this step? – weefwefwqg3 Nov 18 '16 at 18:34

1 Answers1

5

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
    );
PP.
  • 10,764
  • 7
  • 45
  • 59