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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_arch_file_io.h"
18 #include "apr_file_io.h"
20 #include "apr_strings.h"
22 #include "apr_arch_inherit.h"
24 static apr_status_t file_dup(apr_file_t **new_file, apr_file_t *old_file, apr_pool_t *p)
29 if (*new_file == NULL) {
30 dup_file = (apr_file_t *)apr_palloc(p, sizeof(apr_file_t));
32 if (dup_file == NULL) {
36 dup_file->filedes = -1;
42 rv = DosDupHandle(old_file->filedes, &dup_file->filedes);
45 return APR_FROM_OS_ERROR(rv);
48 dup_file->fname = apr_pstrdup(dup_file->pool, old_file->fname);
49 dup_file->buffered = old_file->buffered;
50 dup_file->isopen = old_file->isopen;
51 dup_file->flags = old_file->flags & ~APR_INHERIT;
52 /* TODO - dup pipes correctly */
53 dup_file->pipe = old_file->pipe;
55 if (*new_file == NULL) {
56 apr_pool_cleanup_register(dup_file->pool, dup_file, apr_file_cleanup,
57 apr_pool_cleanup_null);
66 APR_DECLARE(apr_status_t) apr_file_dup(apr_file_t **new_file, apr_file_t *old_file, apr_pool_t *p)
69 apr_file_close(*new_file);
70 (*new_file)->filedes = -1;
73 return file_dup(new_file, old_file, p);
78 APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file, apr_file_t *old_file, apr_pool_t *p)
80 return file_dup(&new_file, old_file, p);
85 APR_DECLARE(apr_status_t) apr_file_setaside(apr_file_t **new_file,
89 *new_file = (apr_file_t *)apr_palloc(p, sizeof(apr_file_t));
90 memcpy(*new_file, old_file, sizeof(apr_file_t));
91 (*new_file)->pool = p;
93 if (old_file->buffered) {
94 (*new_file)->buffer = apr_palloc(p, APR_FILE_BUFSIZE);
96 if (old_file->direction == 1) {
97 memcpy((*new_file)->buffer, old_file->buffer, old_file->bufpos);
100 memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
103 if (old_file->mutex) {
104 apr_thread_mutex_create(&((*new_file)->mutex),
105 APR_THREAD_MUTEX_DEFAULT, p);
106 apr_thread_mutex_destroy(old_file->mutex);
110 if (old_file->fname) {
111 (*new_file)->fname = apr_pstrdup(p, old_file->fname);
114 if (!(old_file->flags & APR_FILE_NOCLEANUP)) {
115 apr_pool_cleanup_register(p, (void *)(*new_file),
120 old_file->filedes = -1;
121 apr_pool_cleanup_kill(old_file->pool, (void *)old_file,