I found this in mod_perl but the issue can be demonstrated without Perl.

Attempting to open a file for append using the following flags:

   APR_BUFFERED | APR_BINARY | APR_CREATE | APR_APPEND


will not work. The apr_file_open() function returns APR_EACCES. If the following flags are used:

   APR_BUFFERED | APR_BINARY | APR_CREATE | APR_WRITE | APR_APPEND


it works fine. In a vacuum this behavior is debatable. On the one hand, APR_APPEND could be seen to imply APR_WRITE. On the other hand, it might be argued that the first case is incomplete flag-wise.

In the context of Perl, however, particularly when using the APR PerlIO filter, this becomes problematic. Perl uses special character sequences which are converted to the proper flags down underneath the covers. So in Perl '>' (write to a new file) converts properly but '>>' (append to an existing file or create a new one if necessary) does not. There is no message either, AFAIK, it just fails silently.

I'm attaching a couple of my test files. They demonstrate the problem but don't show why it happens. I instrumented a copy of file_io/unix/open.c to figure out what was happening. It fails in this code:

   if ((flag & APR_READ) && (flag & APR_WRITE)) {
       oflags = O_RDWR;
   }
   else if (flag & APR_READ) {
       oflags = O_RDONLY;
   }
   else if (flag & APR_WRITE) {
       oflags = O_WRONLY;
   }
   else {
       printf("fails here...\n");
       return APR_EACCES;
   }

I might suggest checking for APR_WRITE /or/ APR_APPEND, but there may be some reason why it's done this way.

Has anyone else stumbled on this or am I doing something that breaks the warranty? Is there a work-around in Perl?

--
Marc M. Adkins
Software Development Engineer
520 Pike Street, Suite 500
Seattle, WA  98101
P: 206-331-3508
F: 206.331.3695
E: madk...@marchex.com

Marchex Inc.
www.marchex.com

This e-mail message and any attachments are solely for
intended recipients, and may contain information that is
privileged and confidential.  If you are not the intended
recipient, any dissemination, distribution or copying is
strictly prohibited.  If you believe that you may have
received this message in error, please immediately notify
the sender by replying to this e-mail message.
#include <stdio.h>
#include <stdlib.h>

typedef long long off64_t;

#include <apr_general.h>
#include <apr_file_io.h>
#include <apr_pools.h>

char *text = "Nevermore will we sing\n";

int main (int argc, const char *argv[]) {
    apr_file_t     *file;
    apr_int32_t     flag;
    apr_pool_t     *pool;
    apr_size_t      size;
    apr_status_t    stat;
    
    stat = apr_initialize();
    
    if (stat != APR_SUCCESS) {
        printf("Unable to initialize\n");
        return -1;
    }
    
    printf("Initialized!\n");
    
    apr_pool_create(&pool, NULL);
    
//  flag = APR_BUFFERED | APR_BINARY | APR_CREATE | APR_WRITE | APR_TRUNCATE;
    flag = APR_BUFFERED | APR_BINARY | APR_CREATE | APR_APPEND;
//  flag = APR_BUFFERED | APR_BINARY | APR_CREATE | APR_WRITE | APR_APPEND;
    stat = apr_file_open(&file, "test.txt", flag, APR_OS_DEFAULT, pool);
    
    if (stat == APR_SUCCESS) {
        size = strlen(text);
        stat = apr_file_write(file, text, &size);
        
        if (stat != APR_SUCCESS) {
            printf("Error writing to file (%d)\n", stat);
        }
    } else {
        printf("Error opening file (%d)\n", stat);
    }
    
    apr_pool_destroy(pool);
    apr_terminate();
    
    return 0;
}

Attachment: perlio-apr.pl
Description: Perl program

Reply via email to