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.
18 #include "apr_arch_file_io.h"
19 #include "apr_strings.h"
23 * Note that trailing spaces and trailing periods are never recorded
24 * in the file system, except by a very obscure bug where any file
25 * that is created with a trailing space or period, followed by the
26 * ':' stream designator on an NTFS volume can never be accessed again.
27 * In other words, don't ever accept them when designating a stream!
29 * An interesting side effect is that two or three periods are both
30 * treated as the parent directory, although the fourth and on are
31 * not [strongly suggest all trailing periods are trimmed off, or
32 * down to two if there are no other characters.]
34 * Leading spaces and periods are accepted, however.
35 * The * ? < > codes all have wildcard side effects
36 * The " / \ : are exclusively component separator tokens
37 * The system doesn't accept | for any (known) purpose
38 * Oddly, \x7f _is_ acceptable ;)
41 /* apr_c_is_fnchar[] maps Win32's file name and shell escape symbols
43 * element & 1 == valid file name character [excluding delimiters]
44 * element & 2 == character should be shell (caret) escaped from cmd.exe
46 * this must be in-sync with Apache httpd's gen_test_char.c for cgi escaping.
49 const char apr_c_is_fnchar[256] =
50 {/* Reject all ctrl codes... Escape \n and \r (ascii 10 and 13) */
51 0,0,0,0,0,0,0,0,0,0,2,0,0,2,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
52 /* ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
53 1,1,2,1,3,3,3,3,3,3,2,1,1,1,1,0, 1,1,1,1,1,1,1,1,1,1,0,3,2,1,2,2,
54 /* @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ */
55 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,3,2,3,3,1,
56 /* ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ */
57 3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,3,2,3,3,1,
58 /* High bit codes are accepted (subject to utf-8->Unicode xlation) */
59 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
60 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
61 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
62 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
66 apr_status_t filepath_root_test(char *path, apr_pool_t *p)
69 #if APR_HAS_UNICODE_FS
70 if (apr_os_level >= APR_WIN_NT)
72 apr_wchar_t wpath[APR_PATH_MAX];
73 if ((rv = utf8_to_unicode_path(wpath, sizeof(wpath)
74 / sizeof(apr_wchar_t), path)))
76 rv = GetDriveTypeW(wpath);
80 rv = GetDriveType(path);
82 if (rv == DRIVE_UNKNOWN || rv == DRIVE_NO_ROOT_DIR)
88 apr_status_t filepath_drive_get(char **rootpath, char drive,
89 apr_int32_t flags, apr_pool_t *p)
91 char path[APR_PATH_MAX];
92 #if APR_HAS_UNICODE_FS
96 apr_wchar_t wdrive[8];
97 apr_wchar_t wpath[APR_PATH_MAX];
99 /* ???: This needs review, apparently "\\?\d:." returns "\\?\d:"
100 * as if that is useful for anything.
102 wcscpy(wdrive, L"D:.");
103 wdrive[0] = (apr_wchar_t)(unsigned char)drive;
104 if (!GetFullPathNameW(wdrive, sizeof(wpath) / sizeof(apr_wchar_t), wpath, &ignored))
105 return apr_get_os_error();
106 if ((rv = unicode_to_utf8_path(path, sizeof(path), wpath)))
119 if (!GetFullPathName(drivestr, sizeof(path), path, &ignored))
120 return apr_get_os_error();
123 if (!(flags & APR_FILEPATH_NATIVE)) {
124 for (*rootpath = path; **rootpath; ++*rootpath) {
125 if (**rootpath == '\\')
129 *rootpath = apr_pstrdup(p, path);
134 apr_status_t filepath_root_case(char **rootpath, char *root, apr_pool_t *p)
136 #if APR_HAS_UNICODE_FS
139 apr_wchar_t *ignored;
140 apr_wchar_t wpath[APR_PATH_MAX];
142 apr_wchar_t wroot[APR_PATH_MAX];
143 /* ???: This needs review, apparently "\\?\d:." returns "\\?\d:"
144 * as if that is useful for anything.
146 if ((rv = utf8_to_unicode_path(wroot, sizeof(wroot)
147 / sizeof(apr_wchar_t), root)))
149 if (!GetFullPathNameW(wroot, sizeof(wpath) / sizeof(apr_wchar_t), wpath, &ignored))
150 return apr_get_os_error();
152 /* Borrow wroot as a char buffer (twice as big as necessary)
154 if ((rv = unicode_to_utf8_path((char*)wroot, sizeof(wroot), wpath)))
156 *rootpath = apr_pstrdup(p, (char*)wroot);
162 char path[APR_PATH_MAX];
164 if (!GetFullPathName(root, sizeof(path), path, &ignored))
165 return apr_get_os_error();
166 *rootpath = apr_pstrdup(p, path);
173 APR_DECLARE(apr_status_t) apr_filepath_get(char **rootpath, apr_int32_t flags,
176 char path[APR_PATH_MAX];
177 #if APR_HAS_UNICODE_FS
180 apr_wchar_t wpath[APR_PATH_MAX];
182 if (!GetCurrentDirectoryW(sizeof(wpath) / sizeof(apr_wchar_t), wpath))
183 return apr_get_os_error();
184 if ((rv = unicode_to_utf8_path(path, sizeof(path), wpath)))
191 if (!GetCurrentDirectory(sizeof(path), path))
192 return apr_get_os_error();
195 if (!(flags & APR_FILEPATH_NATIVE)) {
196 for (*rootpath = path; **rootpath; ++*rootpath) {
197 if (**rootpath == '\\')
201 *rootpath = apr_pstrdup(p, path);
206 APR_DECLARE(apr_status_t) apr_filepath_set(const char *rootpath,
209 #if APR_HAS_UNICODE_FS
212 apr_wchar_t wpath[APR_PATH_MAX];
214 if ((rv = utf8_to_unicode_path(wpath, sizeof(wpath)
215 / sizeof(apr_wchar_t), rootpath)))
217 if (!SetCurrentDirectoryW(wpath))
218 return apr_get_os_error();
224 if (!SetCurrentDirectory(rootpath))
225 return apr_get_os_error();