apr  1.7.0
About: APR (Apache Portable Runtime) project offers software libraries that provide a predictable and consistent interface to underlying platform-specific implementations (APR core library).
  Fossies Dox: apr-1.7.0.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

copy.c
Go to the documentation of this file.
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,
25 {
26  apr_file_t *s, *d;
28  apr_finfo_t finfo;
30 
31  /* Open source file. */
32  status = apr_file_open(&s, from_path, APR_FOPEN_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  apr_file_perms_set(to_path, perms); /* ignore any failure */
45  }
46  else
47  perms = to_perms;
48 
49  /* Open dest file. */
50  status = apr_file_open(&d, to_path, flags, perms, pool);
51  if (status) {
52  apr_file_close(s); /* toss any error */
53  return status;
54  }
55 
56 #if BUFSIZ > APR_FILE_DEFAULT_BUFSIZE
57 #define COPY_BUFSIZ BUFSIZ
58 #else
59 #define COPY_BUFSIZ APR_FILE_DEFAULT_BUFSIZE
60 #endif
61 
62  /* Copy bytes till the cows come home. */
63  while (1) {
64  char buf[COPY_BUFSIZ];
65  apr_size_t bytes_this_time = sizeof(buf);
66  apr_status_t read_err;
67  apr_status_t write_err;
68 
69  /* Read 'em. */
70  read_err = apr_file_read(s, buf, &bytes_this_time);
71  if (read_err && !APR_STATUS_IS_EOF(read_err)) {
72  apr_file_close(s); /* toss any error */
73  apr_file_close(d); /* toss any error */
74  return read_err;
75  }
76 
77  /* Write 'em. */
78  write_err = apr_file_write_full(d, buf, bytes_this_time, NULL);
79  if (write_err) {
80  apr_file_close(s); /* toss any error */
81  apr_file_close(d); /* toss any error */
82  return write_err;
83  }
84 
85  if (read_err && APR_STATUS_IS_EOF(read_err)) {
86  status = apr_file_close(s);
87  if (status) {
88  apr_file_close(d); /* toss any error */
89  return status;
90  }
91 
92  /* return the results of this close: an error, or success */
93  return apr_file_close(d);
94  }
95  }
96  /* NOTREACHED */
97 }
98 
99 APR_DECLARE(apr_status_t) apr_file_copy(const char *from_path,
100  const char *to_path,
102  apr_pool_t *pool)
103 {
104  return apr_file_transfer_contents(from_path, to_path,
106  perms,
107  pool);
108 }
109 
110 APR_DECLARE(apr_status_t) apr_file_append(const char *from_path,
111  const char *to_path,
113  apr_pool_t *pool)
114 {
115  return apr_file_transfer_contents(from_path, to_path,
117  perms,
118  pool);
119 }
APR_INCOMPLETE
#define APR_INCOMPLETE
Definition: apr_errno.h:452
pool
const char int apr_pool_t * pool
Definition: apr_cstr.h:82
APR_STATUS_IS_EOF
#define APR_STATUS_IS_EOF(s)
Definition: apr_errno.h:567
apr_fileperms_t
apr_int32_t apr_fileperms_t
Definition: apr_file_info.h:125
APR_FOPEN_TRUNCATE
#define APR_FOPEN_TRUNCATE
Definition: apr_file_io.h:58
status
int int status
Definition: apr_thread_proc.h:769
to_path
const char * to_path
Definition: apr_file_io.h:308
APR_FOPEN_READ
#define APR_FOPEN_READ
Definition: apr_file_io.h:54
apr_file_transfer_contents
static apr_status_t apr_file_transfer_contents(const char *from_path, const char *to_path, apr_int32_t flags, apr_fileperms_t to_perms, apr_pool_t *pool)
Definition: copy.c:20
APR_FOPEN_WRITE
#define APR_FOPEN_WRITE
Definition: apr_file_io.h:55
APR_DECLARE
APR_DECLARE(apr_status_t)
Definition: copy.c:99
perms
const char apr_fileperms_t perms
Definition: apr_file_io.h:333
buf
char * buf
Definition: apr_errno.h:52
APR_FOPEN_APPEND
#define APR_FOPEN_APPEND
Definition: apr_file_io.h:57
s
const char * s
Definition: apr_strings.h:95
APR_FINFO_PROT
#define APR_FINFO_PROT
Definition: apr_file_info.h:166
apr_finfo_t
Definition: apr_file_info.h:174
COPY_BUFSIZ
#define COPY_BUFSIZ
apr_file_t
Definition: apr_arch_file_io.h:97
apr_pool_t
Definition: apr_pools.c:577
apr_file_io.h
APR File I/O Handling.
APR_FILE_SOURCE_PERMS
#define APR_FILE_SOURCE_PERMS
Definition: apr_file_info.h:113
apr_status_t
int apr_status_t
Definition: apr_errno.h:44
APR_SUCCESS
#define APR_SUCCESS
Definition: apr_errno.h:225
flags
const char apr_ssize_t int flags
Definition: apr_encode.h:162
APR_FOPEN_CREATE
#define APR_FOPEN_CREATE
Definition: apr_file_io.h:56
apr_finfo_t::protection
apr_fileperms_t protection
Definition: apr_file_info.h:181
APR_OS_DEFAULT
#define APR_OS_DEFAULT
Definition: apr_file_info.h:112