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

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/unix/copy.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 2002-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
#include "apr_file_io.h"
 
19
 
 
20
static apr_status_t apr_file_transfer_contents(const char *from_path,
 
21
                                               const char *to_path,
 
22
                                               apr_int32_t flags,
 
23
                                               apr_fileperms_t to_perms,
 
24
                                               apr_pool_t *pool)
 
25
{
 
26
    apr_file_t *s, *d;
 
27
    apr_status_t status;
 
28
    apr_finfo_t finfo;
 
29
    apr_fileperms_t perms;
 
30
 
 
31
    /* Open source file. */
 
32
    status = apr_file_open(&s, from_path, APR_READ, APR_OS_DEFAULT, pool);
 
33
    if (status)
 
34
        return status;
 
35
 
 
36
    /* Maybe get its permissions. */
 
37
    if (to_perms == APR_FILE_SOURCE_PERMS) {
 
38
        status = apr_file_info_get(&finfo, APR_FINFO_PROT, s);
 
39
        if (status != APR_SUCCESS && status != APR_INCOMPLETE) {
 
40
            apr_file_close(s);  /* toss any error */
 
41
            return status;
 
42
        }
 
43
        perms = finfo.protection;
 
44
    }
 
45
    else
 
46
        perms = to_perms;
 
47
 
 
48
    /* Open dest file. */
 
49
    status = apr_file_open(&d, to_path, flags, perms, pool);
 
50
    if (status) {
 
51
        apr_file_close(s);  /* toss any error */
 
52
        return status;
 
53
    }
 
54
 
 
55
    /* Copy bytes till the cows come home. */
 
56
    while (1) {
 
57
        char buf[BUFSIZ];
 
58
        apr_size_t bytes_this_time = sizeof(buf);
 
59
        apr_status_t read_err;
 
60
        apr_status_t write_err;
 
61
 
 
62
        /* Read 'em. */
 
63
        read_err = apr_file_read(s, buf, &bytes_this_time);
 
64
        if (read_err && !APR_STATUS_IS_EOF(read_err)) {
 
65
            apr_file_close(s);  /* toss any error */
 
66
            apr_file_close(d);  /* toss any error */
 
67
            return read_err;
 
68
        }
 
69
 
 
70
        /* Write 'em. */
 
71
        write_err = apr_file_write_full(d, buf, bytes_this_time, NULL);
 
72
        if (write_err) {
 
73
            apr_file_close(s);  /* toss any error */
 
74
            apr_file_close(d);  /* toss any error */
 
75
            return write_err;
 
76
        }
 
77
 
 
78
        if (read_err && APR_STATUS_IS_EOF(read_err)) {
 
79
            status = apr_file_close(s);
 
80
            if (status) {
 
81
                apr_file_close(d);  /* toss any error */
 
82
                return status;
 
83
            }
 
84
 
 
85
            /* return the results of this close: an error, or success */
 
86
            return apr_file_close(d);
 
87
        }
 
88
    }
 
89
    /* NOTREACHED */
 
90
}
 
91
 
 
92
APR_DECLARE(apr_status_t) apr_file_copy(const char *from_path,
 
93
                                        const char *to_path,
 
94
                                        apr_fileperms_t perms,
 
95
                                        apr_pool_t *pool)
 
96
{
 
97
    return apr_file_transfer_contents(from_path, to_path,
 
98
                                      (APR_WRITE | APR_CREATE | APR_TRUNCATE),
 
99
                                      perms,
 
100
                                      pool);
 
101
}
 
102
 
 
103
APR_DECLARE(apr_status_t) apr_file_append(const char *from_path,
 
104
                                          const char *to_path,
 
105
                                          apr_fileperms_t perms,
 
106
                                          apr_pool_t *pool)
 
107
{
 
108
    return apr_file_transfer_contents(from_path, to_path,
 
109
                                      (APR_WRITE | APR_CREATE | APR_APPEND),
 
110
                                      perms,
 
111
                                      pool);
 
112
}