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#define INCL_DOS
18#define INCL_DOSERRORS
19#include "apr_arch_file_io.h"
20#include "apr_file_io.h"
21#include "apr_lib.h"
22#include <sys/time.h>
23#include "apr_strings.h"
24
25
26static void FS3_to_finfo(apr_finfo_t *finfo, FILESTATUS3 *fstatus)
27{
28    finfo->protection = (fstatus->attrFile & FILE_READONLY) ? 0x555 : 0x777;
29
30    if (fstatus->attrFile & FILE_DIRECTORY)
31        finfo->filetype = APR_DIR;
32    else
33        finfo->filetype = APR_REG;
34    /* XXX: No other possible types from FS3? */
35
36    finfo->user = 0;
37    finfo->group = 0;
38    finfo->inode = 0;
39    finfo->device = 0;
40    finfo->size = fstatus->cbFile;
41    finfo->csize = fstatus->cbFileAlloc;
42    apr_os2_time_to_apr_time(&finfo->atime, fstatus->fdateLastAccess,
43                             fstatus->ftimeLastAccess );
44    apr_os2_time_to_apr_time(&finfo->mtime, fstatus->fdateLastWrite,
45                             fstatus->ftimeLastWrite );
46    apr_os2_time_to_apr_time(&finfo->ctime, fstatus->fdateCreation,
47                             fstatus->ftimeCreation );
48    finfo->valid = APR_FINFO_TYPE | APR_FINFO_PROT | APR_FINFO_SIZE
49                 | APR_FINFO_CSIZE | APR_FINFO_MTIME
50                 | APR_FINFO_CTIME | APR_FINFO_ATIME | APR_FINFO_LINK;
51}
52
53
54
55static apr_status_t handle_type(apr_filetype_e *ftype, HFILE file)
56{
57    ULONG filetype, fileattr, rc;
58
59    rc = DosQueryHType(file, &filetype, &fileattr);
60
61    if (rc == 0) {
62        switch (filetype & 0xff) {
63        case 0:
64            *ftype = APR_REG;
65            break;
66
67        case 1:
68            *ftype = APR_CHR;
69            break;
70
71        case 2:
72            *ftype = APR_PIPE;
73            break;
74
75        default:
76            /* Brian, is this correct???
77             */
78            *ftype = APR_UNKFILE;
79            break;
80        }
81
82        return APR_SUCCESS;
83    }
84    return APR_FROM_OS_ERROR(rc);
85}
86
87
88
89APR_DECLARE(apr_status_t) apr_file_info_get(apr_finfo_t *finfo, apr_int32_t wanted,
90                                   apr_file_t *thefile)
91{
92    ULONG rc;
93    FILESTATUS3 fstatus;
94
95    if (thefile->isopen) {
96        if (thefile->buffered) {
97            /* XXX: flush here is not mutex protected */
98            apr_status_t rv = apr_file_flush(thefile);
99
100            if (rv != APR_SUCCESS) {
101                return rv;
102            }
103        }
104
105        rc = DosQueryFileInfo(thefile->filedes, FIL_STANDARD, &fstatus, sizeof(fstatus));
106    }
107    else
108        rc = DosQueryPathInfo(thefile->fname, FIL_STANDARD, &fstatus, sizeof(fstatus));
109
110    if (rc == 0) {
111        FS3_to_finfo(finfo, &fstatus);
112        finfo->fname = thefile->fname;
113
114        if (finfo->filetype == APR_REG) {
115            if (thefile->isopen) {
116                return handle_type(&finfo->filetype, thefile->filedes);
117            }
118        } else {
119            return APR_SUCCESS;
120        }
121    }
122
123    finfo->protection = 0;
124    finfo->filetype = APR_NOFILE;
125    return APR_FROM_OS_ERROR(rc);
126}
127
128APR_DECLARE(apr_status_t) apr_file_perms_set(const char *fname, apr_fileperms_t perms)
129{
130    return APR_ENOTIMPL;
131}
132
133
134APR_DECLARE(apr_status_t) apr_stat(apr_finfo_t *finfo, const char *fname,
135                              apr_int32_t wanted, apr_pool_t *cont)
136{
137    ULONG rc;
138    FILESTATUS3 fstatus;
139
140    finfo->protection = 0;
141    finfo->filetype = APR_NOFILE;
142    finfo->name = NULL;
143    rc = DosQueryPathInfo(fname, FIL_STANDARD, &fstatus, sizeof(fstatus));
144
145    if (rc == 0) {
146        FS3_to_finfo(finfo, &fstatus);
147        finfo->fname = fname;
148
149        if (wanted & APR_FINFO_NAME) {
150            ULONG count = 1;
151            HDIR hDir = HDIR_SYSTEM;
152            FILEFINDBUF3 ffb;
153            rc = DosFindFirst(fname, &hDir,
154                              FILE_DIRECTORY|FILE_HIDDEN|FILE_SYSTEM|FILE_ARCHIVED,
155                              &ffb, sizeof(ffb), &count, FIL_STANDARD);
156            if (rc == 0 && count == 1) {
157                finfo->name = apr_pstrdup(cont, ffb.achName);
158                finfo->valid |= APR_FINFO_NAME;
159            }
160        }
161    } else if (rc == ERROR_INVALID_ACCESS) {
162        memset(finfo, 0, sizeof(apr_finfo_t));
163        finfo->valid = APR_FINFO_TYPE | APR_FINFO_PROT;
164        finfo->protection = 0666;
165        finfo->filetype = APR_CHR;
166
167        if (wanted & APR_FINFO_NAME) {
168            finfo->name = apr_pstrdup(cont, fname);
169            finfo->valid |= APR_FINFO_NAME;
170        }
171    } else {
172        return APR_FROM_OS_ERROR(rc);
173    }
174
175    return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS;
176}
177
178
179
180APR_DECLARE(apr_status_t) apr_file_attrs_set(const char *fname,
181                                             apr_fileattrs_t attributes,
182                                             apr_fileattrs_t attr_mask,
183                                             apr_pool_t *cont)
184{
185    FILESTATUS3 fs3;
186    ULONG rc;
187
188    /* Don't do anything if we can't handle the requested attributes */
189    if (!(attr_mask & (APR_FILE_ATTR_READONLY
190                       | APR_FILE_ATTR_HIDDEN)))
191        return APR_SUCCESS;
192
193    rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3));
194    if (rc == 0) {
195        ULONG old_attr = fs3.attrFile;
196
197        if (attr_mask & APR_FILE_ATTR_READONLY)
198        {
199            if (attributes & APR_FILE_ATTR_READONLY) {
200                fs3.attrFile |= FILE_READONLY;
201            } else {
202                fs3.attrFile &= ~FILE_READONLY;
203            }
204        }
205
206        if (attr_mask & APR_FILE_ATTR_HIDDEN)
207        {
208            if (attributes & APR_FILE_ATTR_HIDDEN) {
209                fs3.attrFile |= FILE_HIDDEN;
210            } else {
211                fs3.attrFile &= ~FILE_HIDDEN;
212            }
213        }
214
215        if (fs3.attrFile != old_attr) {
216            rc = DosSetPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3), 0);
217        }
218    }
219
220    return APR_FROM_OS_ERROR(rc);
221}
222
223
224/* ### Somebody please write this! */
225APR_DECLARE(apr_status_t) apr_file_mtime_set(const char *fname,
226                                              apr_time_t mtime,
227                                              apr_pool_t *pool)
228{
229    FILESTATUS3 fs3;
230    ULONG rc;
231    rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3));
232
233    if (rc) {
234        return APR_FROM_OS_ERROR(rc);
235    }
236
237    apr_apr_time_to_os2_time(&fs3.fdateLastWrite, &fs3.ftimeLastWrite, mtime);
238
239    rc = DosSetPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3), 0);
240    return APR_FROM_OS_ERROR(rc);
241}
242