~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/unix/seek.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include "apr_arch_file_io.h"
 
18
 
 
19
static apr_status_t setptr(apr_file_t *thefile, apr_off_t pos )
 
20
{
 
21
    apr_off_t newbufpos;
 
22
    apr_status_t rv;
 
23
 
 
24
    if (thefile->direction == 1) {
 
25
        rv = apr_file_flush(thefile);
 
26
        if (rv) {
 
27
            return rv;
 
28
        }
 
29
        thefile->bufpos = thefile->direction = thefile->dataRead = 0;
 
30
    }
 
31
 
 
32
    newbufpos = pos - (thefile->filePtr - thefile->dataRead);
 
33
    if (newbufpos >= 0 && newbufpos <= thefile->dataRead) {
 
34
        thefile->bufpos = newbufpos;
 
35
        rv = APR_SUCCESS;
 
36
    } 
 
37
    else {
 
38
        if (lseek(thefile->filedes, pos, SEEK_SET) != -1) {
 
39
            thefile->bufpos = thefile->dataRead = 0;
 
40
            thefile->filePtr = pos;
 
41
            rv = APR_SUCCESS;
 
42
        }
 
43
        else {
 
44
            rv = errno;
 
45
        }
 
46
    }
 
47
 
 
48
    return rv;
 
49
}
 
50
 
 
51
 
 
52
APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
 
53
{
 
54
    apr_off_t rv;
 
55
 
 
56
    thefile->eof_hit = 0;
 
57
 
 
58
    if (thefile->buffered) {
 
59
        int rc = EINVAL;
 
60
        apr_finfo_t finfo;
 
61
 
 
62
        switch (where) {
 
63
        case APR_SET:
 
64
            rc = setptr(thefile, *offset);
 
65
            break;
 
66
 
 
67
        case APR_CUR:
 
68
            rc = setptr(thefile, thefile->filePtr - thefile->dataRead + thefile->bufpos + *offset);
 
69
            break;
 
70
 
 
71
        case APR_END:
 
72
            rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
 
73
            if (rc == APR_SUCCESS)
 
74
                rc = setptr(thefile, finfo.size + *offset);
 
75
            break;
 
76
        }
 
77
 
 
78
        *offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
 
79
        return rc;
 
80
    }
 
81
    else {
 
82
        rv = lseek(thefile->filedes, *offset, where);
 
83
        if (rv == -1) {
 
84
            *offset = -1;
 
85
            return errno;
 
86
        }
 
87
        else {
 
88
            *offset = rv;
 
89
            return APR_SUCCESS;
 
90
        }
 
91
    }
 
92
}
 
93
 
 
94
apr_status_t apr_file_trunc(apr_file_t *fp, apr_off_t offset)
 
95
{
 
96
    if (ftruncate(fp->filedes, offset) == -1) {
 
97
        return errno;
 
98
    }
 
99
    return setptr(fp, offset);
 
100
}