|
|
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.4 ! paf 8: static const char * const IDENT="$Date: 2003/11/20 16:34:25 $";
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:
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,
1.2 paf 93: apr_int32_t /*wanted*/,
1.1 paf 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: {
1.4 ! paf 112: apr_off_t rv = lseek(file->handle, *offset, where);
! 113: *offset = rv;
! 114: return rv == -1? errno: APR_SUCCESS;
1.1 paf 115: }
116:
117:
118: APR_DECLARE(apr_status_t) apr_file_read_full(apr_file_t *file, void *buf,
119: apr_size_t nbytes,
1.2 paf 120: apr_size_t *p_bytes_read)
1.1 paf 121: {
1.2 paf 122: int l_bytes_read = read(file->handle, buf, nbytes);
123: if (l_bytes_read == 0)
1.1 paf 124: return APR_EOF;
1.2 paf 125: else if (l_bytes_read == -1)
1.1 paf 126: return errno;
1.2 paf 127:
128: if(p_bytes_read)
129: *p_bytes_read=(apr_size_t)l_bytes_read;
1.1 paf 130:
131: return APR_SUCCESS;
132: }
133:
134:
135: APR_DECLARE(apr_status_t) apr_file_write_full(apr_file_t *file, const void *buf,
136: apr_size_t nbytes,
137: apr_size_t *bytes_written)
138: {
139: apr_size_t rv;
140: do {
141: rv = write(file->handle, buf, nbytes);
142: } while (rv == (apr_size_t)-1 && errno == EINTR);
143:
144: if (rv == (apr_size_t)-1) {
145: if(bytes_written)
146: *bytes_written = 0;
147: return errno;
148: }
149: if(bytes_written)
150: *bytes_written=rv;
151: return APR_SUCCESS;
152: }