|
|
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.2 ! paf 8: static const char* IDENT="$Date: 2003/11/06 08:49:56 $";
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: public PA_Cleaned {
16: int handle;
17:
18: ~apr_file_t() {
19: if(handle>0) {
20: close(handle); handle=-1;
21: }
22: }
23: };
24:
25: APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new_file, const char *fname,
26: apr_int32_t flag, apr_fileperms_t perm,
27: apr_pool_t *cont)
28: {
29: int oflags = 0;
30: #if APR_HAS_THREADS
31: apr_status_t rv;
32: #endif
33:
34: (*new_file) = new apr_file_t;
35: // (*new_file)->flags = flag;
36: (*new_file)->handle = -1;
37:
38: if ((flag & APR_READ) && (flag & APR_WRITE)) {
39: oflags = O_RDWR;
40: }
41: else if (flag & APR_READ) {
42: oflags = O_RDONLY;
43: }
44: else if (flag & APR_WRITE) {
45: oflags = O_WRONLY;
46: }
47: else {
48: return APR_EACCES;
49: }
50:
51: if (flag & APR_CREATE) {
52: oflags |= O_CREAT;
53: if (flag & APR_EXCL) {
54: oflags |= O_EXCL;
55: }
56: }
57: if ((flag & APR_EXCL) && !(flag & APR_CREATE)) {
58: return APR_EACCES;
59: }
60:
61: if (flag & APR_APPEND) {
62: oflags |= O_APPEND;
63: }
64: if (flag & APR_TRUNCATE) {
65: oflags |= O_TRUNC;
66: }
67: #ifdef O_BINARY
68: if (flag & APR_BINARY) {
69: oflags |= O_BINARY;
70: }
71: #endif
72:
73: (*new_file)->handle = open(fname, oflags, /*apr_unix_perms2mode*/(perm));
74: return APR_SUCCESS;
75: }
76:
77: APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
78: {
79: delete file;
80: return APR_SUCCESS;
81: }
82:
83: APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *file, int type)
84: {
85: if(type & APR_FLOCK_NONBLOCK)
86: pa_lock_exclusive_nonblocking(file->handle);
87:
88: if ((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED)
89: return pa_lock_shared_blocking(file->handle);
90:
91: return pa_lock_exclusive_blocking(file->handle);
92: }
93:
94: APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *file)
95: {
96: return pa_unlock(file->handle);
97: }
98:
99: APR_DECLARE(apr_status_t) apr_file_info_get(apr_finfo_t *finfo,
100: apr_int32_t wanted,
101: apr_file_t *file)
102: {
1.2 ! paf 103: struct stat info;
! 104:
! 105: if (fstat(file->handle, &info) == 0) {
! 106: finfo->size=info.st_size;
! 107: return APR_SUCCESS;
! 108: }
! 109: else {
! 110: return errno;
! 111: }
1.1 paf 112: }
113:
114:
115: APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *file,
116: apr_seek_where_t where,
117: apr_off_t *offset)
118: {
1.2 ! paf 119: return lseek(file->handle, *offset, where);
1.1 paf 120: }
121:
122:
123: APR_DECLARE(apr_status_t) apr_file_read_full(apr_file_t *file, void *buf,
124: apr_size_t nbytes,
125: apr_size_t *bytes_read)
126: {
1.2 ! paf 127: int bytesread = read(file->handle, buf, nbytes);
! 128: if (bytesread == 0)
! 129: return APR_EOF;
! 130: else if (bytesread == -1)
! 131: return errno;
! 132:
1.1 paf 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: {
1.2 ! paf 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: *bytes_written = 0;
! 148: return errno;
! 149: }
! 150: *bytes_written=rv;
1.1 paf 151: return APR_SUCCESS;
152: }