1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
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
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 "win32/apr_arch_file_io.h"
18
#include "apr_file_io.h"
19
#include "apr_general.h"
20
#include "apr_strings.h"
22
#include "apr_arch_inherit.h"
24
APR_DECLARE(apr_status_t) apr_file_dup(apr_file_t **new_file,
25
apr_file_t *old_file, apr_pool_t *p)
30
HANDLE hproc = GetCurrentProcess();
31
HANDLE newhand = NULL;
33
if (!DuplicateHandle(hproc, old_file->filehand,
34
hproc, &newhand, 0, FALSE,
35
DUPLICATE_SAME_ACCESS)) {
36
return apr_get_os_error();
39
(*new_file) = (apr_file_t *) apr_pcalloc(p, sizeof(apr_file_t));
40
(*new_file)->filehand = newhand;
41
(*new_file)->flags = old_file->flags & ~APR_INHERIT;
42
(*new_file)->pool = p;
43
(*new_file)->fname = apr_pstrdup(p, old_file->fname);
44
(*new_file)->append = old_file->append;
45
(*new_file)->buffered = FALSE;
46
(*new_file)->ungetchar = old_file->ungetchar;
49
if (old_file->mutex) {
50
apr_thread_mutex_create(&((*new_file)->mutex),
51
APR_THREAD_MUTEX_DEFAULT, p);
55
apr_pool_cleanup_register((*new_file)->pool, (void *)(*new_file), file_cleanup,
56
apr_pool_cleanup_null);
58
/* Create a pollset with room for one descriptor. */
59
/* ### check return codes */
60
(void) apr_pollset_create(&(*new_file)->pollset, 1, p, 0);
63
#endif /* !defined(_WIN32_WCE) */
66
#define stdin_handle 0x01
67
#define stdout_handle 0x02
68
#define stderr_handle 0x04
70
APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
71
apr_file_t *old_file, apr_pool_t *p)
77
HANDLE hproc = GetCurrentProcess();
78
HANDLE newhand = NULL;
81
/* dup2 is not supported literaly with native Windows handles.
82
* We can, however, emulate dup2 for the standard i/o handles,
83
* and close and replace other handles with duped handles.
84
* The os_handle will change, however.
86
if (new_file->filehand == GetStdHandle(STD_ERROR_HANDLE)) {
87
stdhandle |= stderr_handle;
89
if (new_file->filehand == GetStdHandle(STD_OUTPUT_HANDLE)) {
90
stdhandle |= stdout_handle;
92
if (new_file->filehand == GetStdHandle(STD_INPUT_HANDLE)) {
93
stdhandle |= stdin_handle;
97
if (!DuplicateHandle(hproc, old_file->filehand,
99
TRUE, DUPLICATE_SAME_ACCESS)) {
100
return apr_get_os_error();
102
if (((stdhandle & stderr_handle) && !SetStdHandle(STD_ERROR_HANDLE, newhand)) ||
103
((stdhandle & stdout_handle) && !SetStdHandle(STD_OUTPUT_HANDLE, newhand)) ||
104
((stdhandle & stdin_handle) && !SetStdHandle(STD_INPUT_HANDLE, newhand))) {
105
return apr_get_os_error();
107
newflags = old_file->flags | APR_INHERIT;
110
if (!DuplicateHandle(hproc, old_file->filehand,
112
FALSE, DUPLICATE_SAME_ACCESS)) {
113
return apr_get_os_error();
115
newflags = old_file->flags & ~APR_INHERIT;
118
if (new_file->filehand && (new_file->filehand != INVALID_HANDLE_VALUE)) {
119
CloseHandle(new_file->filehand);
122
new_file->flags = newflags;
123
new_file->filehand = newhand;
124
new_file->fname = apr_pstrdup(new_file->pool, old_file->fname);
125
new_file->append = old_file->append;
126
new_file->buffered = FALSE;
127
new_file->ungetchar = old_file->ungetchar;
130
if (old_file->mutex) {
131
apr_thread_mutex_create(&(new_file->mutex),
132
APR_THREAD_MUTEX_DEFAULT, p);
137
#endif /* !defined(_WIN32_WCE) */
140
APR_DECLARE(apr_status_t) apr_file_setaside(apr_file_t **new_file,
141
apr_file_t *old_file,
144
*new_file = (apr_file_t *)apr_palloc(p, sizeof(apr_file_t));
145
memcpy(*new_file, old_file, sizeof(apr_file_t));
146
(*new_file)->pool = p;
147
if (old_file->buffered) {
148
(*new_file)->buffer = apr_palloc(p, APR_FILE_BUFSIZE);
149
if (old_file->direction == 1) {
150
memcpy((*new_file)->buffer, old_file->buffer, old_file->bufpos);
153
memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
156
if (old_file->mutex) {
157
apr_thread_mutex_create(&((*new_file)->mutex),
158
APR_THREAD_MUTEX_DEFAULT, p);
159
apr_thread_mutex_destroy(old_file->mutex);
161
if (old_file->fname) {
162
(*new_file)->fname = apr_pstrdup(p, old_file->fname);
164
if (!(old_file->flags & APR_FILE_NOCLEANUP)) {
165
apr_pool_cleanup_register(p, (void *)(*new_file),
170
old_file->filehand = INVALID_HANDLE_VALUE;
171
apr_pool_cleanup_kill(old_file->pool, (void *)old_file,
174
/* Create a pollset with room for one descriptor. */
175
/* ### check return codes */
176
(void) apr_pollset_create(&(*new_file)->pollset, 1, p, 0);