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

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: 
        !             8: static const char* IDENT="$Date: 2003/11/06 10:09:33 $";
        !             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,
        !            21:                                                                   apr_pool_t *cont)
        !            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:     
        !            67:        (*new_file)->handle = open(fname, oflags, /*apr_unix_perms2mode*/(perm));
        !            68:     return APR_SUCCESS;
        !            69: }
        !            70: 
        !            71: APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
        !            72: {
        !            73:        return close(file->handle);
        !            74: }
        !            75: 
        !            76: APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *file, int type)
        !            77: {
        !            78:        if(type & APR_FLOCK_NONBLOCK)
        !            79:                pa_lock_exclusive_nonblocking(file->handle);
        !            80: 
        !            81:        if ((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED)
        !            82:                return pa_lock_shared_blocking(file->handle);
        !            83: 
        !            84:        return pa_lock_exclusive_blocking(file->handle);
        !            85: }
        !            86: 
        !            87: APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *file)
        !            88: {
        !            89:        return pa_unlock(file->handle);
        !            90: }
        !            91: 
        !            92: APR_DECLARE(apr_status_t) apr_file_info_get(apr_finfo_t *finfo, 
        !            93:                                           apr_int32_t wanted,
        !            94:                                           apr_file_t *file)
        !            95: {
        !            96:     struct stat info;
        !            97: 
        !            98:     if (fstat(file->handle, &info) == 0) {
        !            99:                finfo->size=info.st_size;
        !           100:         return APR_SUCCESS;
        !           101:     }
        !           102:     else {
        !           103:         return errno;
        !           104:     }
        !           105: }
        !           106: 
        !           107: 
        !           108: APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *file, 
        !           109:                                    apr_seek_where_t where,
        !           110:                                    apr_off_t *offset)
        !           111: {
        !           112:        return lseek(file->handle, *offset, where);
        !           113: }
        !           114: 
        !           115: 
        !           116: APR_DECLARE(apr_status_t) apr_file_read_full(apr_file_t *file, void *buf,
        !           117:                                         apr_size_t nbytes,
        !           118:                                         apr_size_t *bytes_read)
        !           119: {
        !           120:        int bytesread = read(file->handle, buf, nbytes);
        !           121:     if (bytesread == 0)
        !           122:         return APR_EOF;
        !           123:     else if (bytesread == -1)
        !           124:         return errno;
        !           125: 
        !           126:        return APR_SUCCESS;
        !           127: }
        !           128: 
        !           129: 
        !           130: APR_DECLARE(apr_status_t) apr_file_write_full(apr_file_t *file, const void *buf,
        !           131:                                          apr_size_t nbytes, 
        !           132:                                          apr_size_t *bytes_written)
        !           133: {
        !           134:     apr_size_t rv;
        !           135:     do {
        !           136:         rv = write(file->handle, buf, nbytes);
        !           137:     } while (rv == (apr_size_t)-1 && errno == EINTR);
        !           138: 
        !           139:     if (rv == (apr_size_t)-1) {
        !           140:                if(bytes_written)
        !           141:                        *bytes_written = 0;
        !           142:         return errno;
        !           143:     }
        !           144:        if(bytes_written)
        !           145:                *bytes_written=rv;
        !           146:        return APR_SUCCESS;
        !           147: }

E-mail: