Annotation of parser3/src/lib/sdbm/apr_file_io.C, revision 1.5

1.1       paf         1: /** @file
                      2:        Parser: implementation of apr functions.
                      3: 
                      4:        Copyright(c) 2003 ArtLebedev Group (http://www.artlebedev.com)
                      5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
1.5     ! paf         8: static const char * const IDENT="$Date: 2005/11/18 10:04:03 $";
1.1       paf         9: 
                     10: #include "apr_file_io.h"
                     11: 
                     12: #include "pa_memory.h"
                     13: #include "pa_os.h"
                     14: 
                     15: struct apr_file_t {
                     16:     int handle;
                     17: };
                     18: 
                     19: APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new_file, const char *fname,
                     20:                                    apr_int32_t flag, apr_fileperms_t perm,
1.2       paf        21:                                                                   apr_pool_t *)
1.1       paf        22: {
                     23:     int oflags = 0;
                     24: #if APR_HAS_THREADS
                     25:     apr_status_t rv;
                     26: #endif
                     27: 
                     28:     (*new_file) = (apr_file_t*)pa_malloc_atomic(sizeof(apr_file_t));
                     29: //    (*new_file)->flags = flag;
                     30:     (*new_file)->handle = -1;
                     31: 
                     32:     if ((flag & APR_READ) && (flag & APR_WRITE)) {
                     33:         oflags = O_RDWR;
                     34:     }
                     35:     else if (flag & APR_READ) {
                     36:         oflags = O_RDONLY;
                     37:     }
                     38:     else if (flag & APR_WRITE) {
                     39:         oflags = O_WRONLY;
                     40:     }
                     41:     else {
                     42:         return APR_EACCES; 
                     43:     }
                     44: 
                     45:        if (flag & APR_CREATE) {
                     46:         oflags |= O_CREAT; 
                     47:         if (flag & APR_EXCL) {
                     48:             oflags |= O_EXCL;
                     49:         }
                     50:     }
                     51:     if ((flag & APR_EXCL) && !(flag & APR_CREATE)) {
                     52:         return APR_EACCES;
                     53:     }   
                     54: 
                     55:     if (flag & APR_APPEND) {
                     56:         oflags |= O_APPEND;
                     57:     }
                     58:     if (flag & APR_TRUNCATE) {
                     59:         oflags |= O_TRUNC;
                     60:     }
                     61: #ifdef O_BINARY
                     62:     if (flag & APR_BINARY) {
                     63:         oflags |= O_BINARY;
                     64:     }
                     65: #endif
                     66:     
1.5     ! paf        67:        if(((*new_file)->handle = open(fname, oflags, /*apr_unix_perms2mode*/(perm))) <0 ) 
        !            68:                return errno;
        !            69: 
1.1       paf        70:     return APR_SUCCESS;
                     71: }
                     72: 
                     73: APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
                     74: {
                     75:        return close(file->handle);
                     76: }
                     77: 
                     78: APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *file, int type)
                     79: {
                     80:        if(type & APR_FLOCK_NONBLOCK)
                     81:                pa_lock_exclusive_nonblocking(file->handle);
                     82: 
                     83:        if ((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED)
                     84:                return pa_lock_shared_blocking(file->handle);
                     85: 
                     86:        return pa_lock_exclusive_blocking(file->handle);
                     87: }
                     88: 
                     89: APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *file)
                     90: {
                     91:        return pa_unlock(file->handle);
                     92: }
                     93: 
                     94: APR_DECLARE(apr_status_t) apr_file_info_get(apr_finfo_t *finfo, 
1.2       paf        95:                                           apr_int32_t /*wanted*/,
1.1       paf        96:                                           apr_file_t *file)
                     97: {
                     98:     struct stat info;
                     99: 
                    100:     if (fstat(file->handle, &info) == 0) {
                    101:                finfo->size=info.st_size;
                    102:         return APR_SUCCESS;
                    103:     }
                    104:     else {
                    105:         return errno;
                    106:     }
                    107: }
                    108: 
                    109: 
                    110: APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *file, 
                    111:                                    apr_seek_where_t where,
                    112:                                    apr_off_t *offset)
                    113: {
1.4       paf       114:     apr_off_t rv = lseek(file->handle, *offset, where);
                    115:     *offset = rv;
                    116:        return rv == -1? errno: APR_SUCCESS;
1.1       paf       117: }
                    118: 
                    119: 
                    120: APR_DECLARE(apr_status_t) apr_file_read_full(apr_file_t *file, void *buf,
                    121:                                         apr_size_t nbytes,
1.2       paf       122:                                         apr_size_t *p_bytes_read)
1.1       paf       123: {
1.2       paf       124:        int l_bytes_read = read(file->handle, buf, nbytes);
                    125:     if (l_bytes_read == 0)
1.1       paf       126:         return APR_EOF;
1.2       paf       127:     else if (l_bytes_read == -1)
1.1       paf       128:         return errno;
1.2       paf       129: 
                    130:        if(p_bytes_read)
                    131:                *p_bytes_read=(apr_size_t)l_bytes_read;
1.1       paf       132: 
                    133:        return APR_SUCCESS;
                    134: }
                    135: 
                    136: 
                    137: APR_DECLARE(apr_status_t) apr_file_write_full(apr_file_t *file, const void *buf,
                    138:                                          apr_size_t nbytes, 
                    139:                                          apr_size_t *bytes_written)
                    140: {
                    141:     apr_size_t rv;
                    142:     do {
                    143:         rv = write(file->handle, buf, nbytes);
                    144:     } while (rv == (apr_size_t)-1 && errno == EINTR);
                    145: 
                    146:     if (rv == (apr_size_t)-1) {
                    147:                if(bytes_written)
                    148:                        *bytes_written = 0;
                    149:         return errno;
                    150:     }
                    151:        if(bytes_written)
                    152:                *bytes_written=rv;
                    153:        return APR_SUCCESS;
                    154: }

E-mail: