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

0.6.1 by Andreas Barth
Import upstream version 2.2.3
1
/* Copyright 2000-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
#include "apr_lib.h"
20
#include "apr_strings.h"
21
#include <string.h>
22
#include "apr_arch_inherit.h"
23
24
static apr_status_t file_dup(apr_file_t **new_file, apr_file_t *old_file, apr_pool_t *p)
25
{
26
    int rv;
27
    apr_file_t *dup_file;
28
29
    if (*new_file == NULL) {
30
        dup_file = (apr_file_t *)apr_palloc(p, sizeof(apr_file_t));
31
32
        if (dup_file == NULL) {
33
            return APR_ENOMEM;
34
        }
35
36
        dup_file->filedes = -1;
37
    } else {
38
      dup_file = *new_file;
39
    }
40
41
    dup_file->pool = p;
42
    rv = DosDupHandle(old_file->filedes, &dup_file->filedes);
43
44
    if (rv) {
45
        return APR_FROM_OS_ERROR(rv);
46
    }
47
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;
54
55
    if (*new_file == NULL) {
56
        apr_pool_cleanup_register(dup_file->pool, dup_file, apr_file_cleanup,
57
                            apr_pool_cleanup_null);
58
        *new_file = dup_file;
59
    }
60
61
    return APR_SUCCESS;
62
}
63
64
65
66
APR_DECLARE(apr_status_t) apr_file_dup(apr_file_t **new_file, apr_file_t *old_file, apr_pool_t *p)
67
{
68
  if (*new_file) {
69
      apr_file_close(*new_file);
70
      (*new_file)->filedes = -1;
71
  }
72
73
  return file_dup(new_file, old_file, p);
74
}
75
76
77
78
APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file, apr_file_t *old_file, apr_pool_t *p)
79
{
80
  return file_dup(&new_file, old_file, p);
81
}
82
83
84
85
APR_DECLARE(apr_status_t) apr_file_setaside(apr_file_t **new_file,
86
                                            apr_file_t *old_file,
87
                                            apr_pool_t *p)
88
{
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;
92
93
    if (old_file->buffered) {
94
        (*new_file)->buffer = apr_palloc(p, APR_FILE_BUFSIZE);
95
96
        if (old_file->direction == 1) {
97
            memcpy((*new_file)->buffer, old_file->buffer, old_file->bufpos);
98
        }
99
        else {
100
            memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
101
        }
102
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);
107
        }
108
    }
109
110
    if (old_file->fname) {
111
        (*new_file)->fname = apr_pstrdup(p, old_file->fname);
112
    }
113
114
    if (!(old_file->flags & APR_FILE_NOCLEANUP)) {
115
        apr_pool_cleanup_register(p, (void *)(*new_file), 
116
                                  apr_file_cleanup,
117
                                  apr_file_cleanup);
118
    }
119
120
    old_file->filedes = -1;
121
    apr_pool_cleanup_kill(old_file->pool, (void *)old_file,
122
                          apr_file_cleanup);
123
124
    return APR_SUCCESS;
125
}