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

1.1     ! moko        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 * const IDENT="$Date: 2007-05-24 10:25:00 $";
        !             9: 
        !            10: #include "pa_file_io.h"
        !            11: 
        !            12: #include "pa_memory.h"
        !            13: #include "pa_os.h"
        !            14: 
        !            15: struct pa_file_t {
        !            16:     int handle;
        !            17: };
        !            18: 
        !            19: pa_status_t pa_file_open(pa_file_t **new_file, const char *fname,
        !            20:                                    pa_int32_t flag, pa_fileperms_t perm,
        !            21:                                                                   pa_pool_t *)
        !            22: {
        !            23:     int oflags = 0;
        !            24: #if PA_HAS_THREADS
        !            25:     pa_status_t rv;
        !            26: #endif
        !            27: 
        !            28:     (*new_file) = (pa_file_t*)pa_malloc_atomic(sizeof(pa_file_t));
        !            29: //    (*new_file)->flags = flag;
        !            30:     (*new_file)->handle = -1;
        !            31: 
        !            32:     if ((flag & PA_READ) && (flag & PA_WRITE)) {
        !            33:         oflags = O_RDWR;
        !            34:     }
        !            35:     else if (flag & PA_READ) {
        !            36:         oflags = O_RDONLY;
        !            37:     }
        !            38:     else if (flag & PA_WRITE) {
        !            39:         oflags = O_WRONLY;
        !            40:     }
        !            41:     else {
        !            42:         return PA_EACCES; 
        !            43:     }
        !            44: 
        !            45:        if (flag & PA_CREATE) {
        !            46:         oflags |= O_CREAT; 
        !            47:         if (flag & PA_EXCL) {
        !            48:             oflags |= O_EXCL;
        !            49:         }
        !            50:     }
        !            51:     if ((flag & PA_EXCL) && !(flag & PA_CREATE)) {
        !            52:         return PA_EACCES;
        !            53:     }   
        !            54: 
        !            55:     if (flag & PA_APPEND) {
        !            56:         oflags |= O_APPEND;
        !            57:     }
        !            58:     if (flag & PA_TRUNCATE) {
        !            59:         oflags |= O_TRUNC;
        !            60:     }
        !            61: #ifdef O_BINARY
        !            62:     if (flag & PA_BINARY) {
        !            63:         oflags |= O_BINARY;
        !            64:     }
        !            65: #endif
        !            66:     
        !            67:        if(((*new_file)->handle = open(fname, oflags, /*pa_unix_perms2mode*/(perm))) <0 ) 
        !            68:                return errno;
        !            69: 
        !            70:     return PA_SUCCESS;
        !            71: }
        !            72: 
        !            73: pa_status_t pa_file_close(pa_file_t *file)
        !            74: {
        !            75:        return close(file->handle);
        !            76: }
        !            77: 
        !            78: pa_status_t pa_file_lock(pa_file_t *file, int type)
        !            79: {
        !            80:        if(type & PA_FLOCK_NONBLOCK)
        !            81:                pa_lock_exclusive_blocking(file->handle);
        !            82: 
        !            83:        if ((type & PA_FLOCK_TYPEMASK) == PA_FLOCK_SHARED)
        !            84:                return pa_lock_shared_blocking(file->handle);
        !            85: 
        !            86:        return pa_lock_exclusive_blocking(file->handle);
        !            87: }
        !            88: 
        !            89: pa_status_t pa_file_unlock(pa_file_t *file)
        !            90: {
        !            91:        return pa_unlock(file->handle);
        !            92: }
        !            93: 
        !            94: pa_status_t pa_file_info_get(pa_finfo_t *finfo, 
        !            95:                                           pa_int32_t /*wanted*/,
        !            96:                                           pa_file_t *file)
        !            97: {
        !            98:     struct stat info;
        !            99: 
        !           100:     if (fstat(file->handle, &info) == 0) {
        !           101:                finfo->size=info.st_size;
        !           102:         return PA_SUCCESS;
        !           103:     }
        !           104:     else {
        !           105:         return errno;
        !           106:     }
        !           107: }
        !           108: 
        !           109: 
        !           110: pa_status_t pa_file_seek(pa_file_t *file, 
        !           111:                                    pa_seek_where_t where,
        !           112:                                    pa_off_t *offset)
        !           113: {
        !           114:     pa_off_t rv = lseek(file->handle, *offset, where);
        !           115:     *offset = rv;
        !           116:        return rv == -1? errno: PA_SUCCESS;
        !           117: }
        !           118: 
        !           119: 
        !           120: pa_status_t pa_file_read_full(pa_file_t *file, void *buf,
        !           121:                                         pa_size_t nbytes,
        !           122:                                         pa_size_t *p_bytes_read)
        !           123: {
        !           124:        int l_bytes_read = read(file->handle, buf, nbytes);
        !           125:     if (l_bytes_read == 0)
        !           126:         return PA_EOF;
        !           127:     else if (l_bytes_read == -1)
        !           128:         return errno;
        !           129: 
        !           130:        if(p_bytes_read)
        !           131:                *p_bytes_read=(pa_size_t)l_bytes_read;
        !           132: 
        !           133:        return PA_SUCCESS;
        !           134: }
        !           135: 
        !           136: 
        !           137: pa_status_t pa_file_write_full(pa_file_t *file, const void *buf,
        !           138:                                          pa_size_t nbytes, 
        !           139:                                          pa_size_t *bytes_written)
        !           140: {
        !           141:     pa_size_t rv;
        !           142:     do {
        !           143:         rv = write(file->handle, buf, nbytes);
        !           144:     } while (rv == (pa_size_t)-1 && errno == EINTR);
        !           145: 
        !           146:     if (rv == (pa_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 PA_SUCCESS;
        !           154: }

E-mail: