upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / unix / copy.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  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_fileperms_t perms;
29
30     /* Open source file. */
31     status = apr_file_open(&s, from_path, APR_READ | APR_LARGEFILE, 
32                            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 #if defined(HAVE_FSTAT64) && defined(O_LARGEFILE) && SIZEOF_OFF_T == 4
39         struct stat64 st;
40
41         if (fstat64(s->filedes, &st) != 0)
42             return errno;
43
44         perms = apr_unix_mode2perms(st.st_mode);  
45 #else
46         apr_finfo_t finfo;
47
48         status = apr_file_info_get(&finfo, APR_FINFO_PROT, s);
49         if (status != APR_SUCCESS && status != APR_INCOMPLETE) {
50             apr_file_close(s);  /* toss any error */
51             return status;
52         }
53         perms = finfo.protection;
54 #endif
55     }
56     else
57         perms = to_perms;
58
59     /* Open dest file. */
60     status = apr_file_open(&d, to_path, flags, perms, pool);
61     if (status) {
62         apr_file_close(s);  /* toss any error */
63         return status;
64     }
65
66 #if BUFSIZ > APR_FILE_DEFAULT_BUFSIZE
67 #define COPY_BUFSIZ BUFSIZ
68 #else
69 #define COPY_BUFSIZ APR_FILE_DEFAULT_BUFSIZE
70 #endif
71
72     /* Copy bytes till the cows come home. */
73     while (1) {
74         char buf[COPY_BUFSIZ];
75         apr_size_t bytes_this_time = sizeof(buf);
76         apr_status_t read_err;
77         apr_status_t write_err;
78
79         /* Read 'em. */
80         read_err = apr_file_read(s, buf, &bytes_this_time);
81         if (read_err && !APR_STATUS_IS_EOF(read_err)) {
82             apr_file_close(s);  /* toss any error */
83             apr_file_close(d);  /* toss any error */
84             return read_err;
85         }
86
87         /* Write 'em. */
88         write_err = apr_file_write_full(d, buf, bytes_this_time, NULL);
89         if (write_err) {
90             apr_file_close(s);  /* toss any error */
91             apr_file_close(d);  /* toss any error */
92             return write_err;
93         }
94
95         if (read_err && APR_STATUS_IS_EOF(read_err)) {
96             status = apr_file_close(s);
97             if (status) {
98                 apr_file_close(d);  /* toss any error */
99                 return status;
100             }
101
102             /* return the results of this close: an error, or success */
103             return apr_file_close(d);
104         }
105     }
106     /* NOTREACHED */
107 }
108
109 APR_DECLARE(apr_status_t) apr_file_copy(const char *from_path,
110                                         const char *to_path,
111                                         apr_fileperms_t perms,
112                                         apr_pool_t *pool)
113 {
114     return apr_file_transfer_contents(from_path, to_path,
115                                       (APR_WRITE | APR_CREATE | APR_TRUNCATE
116                                        | APR_LARGEFILE),
117                                       perms,
118                                       pool);
119 }
120
121 APR_DECLARE(apr_status_t) apr_file_append(const char *from_path,
122                                           const char *to_path,
123                                           apr_fileperms_t perms,
124                                           apr_pool_t *pool)
125 {
126     return apr_file_transfer_contents(from_path, to_path,
127                                       (APR_WRITE | APR_CREATE | APR_APPEND 
128                                        | APR_LARGEFILE),
129                                       perms,
130                                       pool);
131 }