]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Uefi/Devices/Utility/Path.c
2 Device Abstraction: Path manipulation utilities.
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 #include <Library/BaseLib.h>
15 #include <LibConfig.h>
22 #include <Device/Device.h>
25 /** Identify the type of path pointed to by Path.
27 Paths are classified based upon their initial character sequences.
33 Mapping paths are broken into two parts at the ':'. The part to the left of the ':'
34 is the Map Name, pointed to by Path, and the part to the right of the ':' is pointed
37 If Path was not a Mapping Path, then NewPath is set to Path.
39 @param[in] Path Pointer to the path to be classified.
40 @param[out] NewPath Pointer to the path portion of a mapping path.
41 @param[out] Length Length of the Map Name portion of the path.
43 @retval PathAbsolute Path is an absolute path. NewPath points to the first '\'.
44 @retval PathRelative Path is a relative path. NewPath = Path.
45 @retval PathMapping Path is a mapping path. NewPath points to the character following ':'.
46 @retval PathError Path is NULL.
52 OUT
wchar_t ** NewPath
,
53 OUT
int * const Length
59 return PathError
; // Bad parameter
62 *NewPath
= Path
; // Setup default condition
64 if((*Path
== L
'\\') || (*Path
== L
'\0')) {
70 /* The easy stuff has been done, now see if this is a mapping path.
71 See if there is a ':' in Path that isn't the first character and is before
74 MapLen
= wcscspn(Path
, L
"\\:");
76 *Length
= (int)MapLen
;
78 /* MapLen == 0 means that the first character is a ':'
79 == PathLen means that there are no '\\' or ':'
80 Otherwise, Path[MapLen] == ':' for a mapping path
81 or '\\' for a relative path.
86 if(Path
[MapLen
] == L
':') {
88 *NewPath
= &Path
[MapLen
+ 1]; // Point to character after then ':'. Might be '\0'.
95 /* Normalize a narrow-character path and produce a wide-character path
96 that has forward slashes replaced with backslashes.
97 Backslashes are directory separators in UEFI File Paths.
99 It is the caller's responsibility to eventually free() the returned buffer.
101 @param[in] path A pointer to the narrow-character path to be normalized.
103 @return A pointer to a buffer containing the normalized, wide-character, path.
106 NormalizePath( const char *path
)
113 OldPath
= AsciiStrToUnicodeStr(path
, gMD
->UString
);
114 Length
= wcslen(OldPath
) + 1;
116 NewPath
= calloc(Length
, sizeof(wchar_t));
117 if(NewPath
!= NULL
) {
119 for( ; *OldPath
; ++OldPath
) {
120 if(*OldPath
== L
'/') {
131 EFIerrno
= RETURN_OUT_OF_RESOURCES
;
136 /** Process a wide character string representing a Mapping Path and extract the instance number.
138 The instance number is the sequence of decimal digits immediately to the left
139 of the ":" in the Map Name portion of a Mapping Path.
141 This function is called with a pointer to beginning of the Map Name.
142 Thus Path[Length] must be a ':' and Path[Length - 1] must be a decimal digit.
143 If either of these are not true, an instance value of 0 is returned.
145 If Path is NULL, an instance value of 0 is returned.
147 @param[in] Path Points to the beginning of a Mapping Path
148 @param[in] Length Number of valid characters to the left of the ':'
150 @return Returns either 0 or the value of the contiguous digits to the left of the ':'.
162 if((Path
!= NULL
) && (Path
[Length
] == L
':') && (Length
> 0)) {
163 for(temp
= __UNCONST(&Path
[Length
- 1]); Length
> 0; --Length
) {
164 if(!iswdigit(*temp
)) {
169 instance
= (int)wcstol(temp
+1, NULL
, 10);
174 /** Transform a relative path into an absolute path.
176 If Path is NULL, return NULL.
177 Otherwise, pre-pend the CWD to Path then process the resulting path to:
178 - Replace "/./" with "/"
179 - Replace "/<dirname>/../" with "/"
180 - Do not allow one to back up past the root, "/"
182 Also sets the Current Working Device to the Root Device.
184 Path must be a previously allocated buffer. PathAdjust will
185 allocate a new buffer to hold the results of the transformation
186 and free Path. A pointer to the newly allocated buffer is returned.
188 @param[in] Path A pointer to the path to be transformed. This buffer
189 will always be freed.
191 @return A pointer to a buffer containing the transformed path.
201 NewPath
= calloc(PATH_MAX
, sizeof(wchar_t));
202 if(NewPath
!= NULL
) {
203 wmemcpy(NewPath
, Path
, PATH_MAX
);
212 /** Replace the leading portion of Path with any aliases.
214 Aliases are read from /etc/fstab. If there is an associated device, the
215 Current Working Device is set to that device.
217 Path must be a previously allocated buffer. PathAlias will
218 allocate a new buffer to hold the results of the transformation
219 then free Path. A pointer to the newly allocated buffer is returned.
221 @param[in] Path A pointer to the original, unaliased, path. This
222 buffer is always freed.
223 @param[out] Node Filled in with a pointer to the Device Node describing
224 the device abstraction associated with this path.
226 @return A pointer to a buffer containing the aliased path.
237 NewPath
= calloc(PATH_MAX
, sizeof(wchar_t));
238 if(NewPath
!= NULL
) {
239 wmemcpy(NewPath
, Path
, PATH_MAX
);
249 /** Parse a path producing the target device, device instance, and file path.
251 It is the caller's responsibility to free() FullPath and MapPath when they
252 are no longer needed.
258 @param[out] MapPath OPTIONAL. If not NULL, it points to the place to save a pointer
259 to the extracted map name. If the path didn't have a map name,
260 then *MapPath is set to NULL.
262 @retval RETURN_SUCCESS The path was parsed successfully.
263 @retval RETURN_NOT_FOUND The path does not map to a valid device.
264 @retval RETURN_OUT_OF_RESOURCES Insufficient memory to calloc a MapName buffer.
265 The errno variable is set to ENOMEM.
266 @retval RETURN_INVALID_PARAMETER The path parameter is not valid.
267 The errno variable is set to EINVAL.
273 OUT
wchar_t **FullPath
,
274 OUT DeviceNode
**DevNode
,
276 OUT
wchar_t **MapPath
280 PATH_CLASS PathClass
;
282 wchar_t *WPath
= NULL
;
283 wchar_t *MPath
= NULL
;
284 DeviceNode
*Node
= NULL
;
285 RETURN_STATUS Status
= RETURN_NOT_FOUND
;
291 // Convert name from MBCS to WCS and change '/' to '\\'
292 WPath
= NormalizePath( path
);
293 PathClass
= ClassifyPath(WPath
, &NewPath
, &MapLen
);
299 if((NewPath
== NULL
) || (*NewPath
== L
'\0')) { /* Nothing after the ':' */
300 PathClass
= PathAbsolute
;
303 Instance
= PathInstance(WPath
, MapLen
);
304 PathClass
= ClassifyPath(NewPath
, NULL
, NULL
);
307 if(WPath
[MapLen
] == L
':') {
308 // Get the Map Name, including the trailing ':'. */
309 MPath
= calloc(MapLen
+2, sizeof(wchar_t));
311 wmemcpy(MPath
, WPath
, MapLen
+1);
315 Status
= RETURN_OUT_OF_RESOURCES
;
316 break; // Exit the switch(PathClass) statement.
319 if(WPath
!= NewPath
) {
320 /* Shift the RHS of the path down to the start of the buffer. */
321 wmemmove(WPath
, NewPath
, wcslen(NewPath
)+1);
326 /* Fall through to PathError if Remapped.
327 This means that the path looked like "foo:bar:something".
332 Status
= RETURN_INVALID_PARAMETER
;
336 /* Transform a relative path into an Absolute path.
337 Prepends CWD and handles ./ and ../ entries.
338 It is the caller's responsibility to free the space
341 WPath
= PathAdjust(NewPath
); // WPath was malloc()ed by PathAdjust
344 /* Perform any path aliasing. For example: /dev/foo -> { node.foo, "" }
345 The current volume and directory are updated in the path as needed.
346 It is the caller's responsibility to free the space
349 Status
= RETURN_SUCCESS
;
350 WPath
= PathAlias(WPath
, &Node
); // PathAlias frees its argument and malloc()s a new one.
353 if(!RETURN_ERROR(Status
)) {
356 if(MapPath
!= NULL
) {
359 else if(MPath
!= NULL
) {
360 free(MPath
); /* Caller doesn't want it so let MPath go free */
363 /* At this point, WPath is an absolute path,
364 MPath is either NULL or points to the Map Name,
365 and Instance is the instance number.
368 /* This is NOT a mapped path. */
370 Node
= daDefaultDevice
;
373 Status
= RETURN_SUCCESS
;
376 Status
= RETURN_NOT_FOUND
;
380 /* This is a mapped path. */
381 Status
= __DevSearch( MPath
, NULL
, &Node
);
382 if(Status
== RETURN_NOT_FOUND
) {
383 Node
= daDefaultDevice
;
386 Status
= RETURN_SUCCESS
;
390 if(DevNode
!= NULL
) {
398 Parses a normalized wide character path and returns a pointer to the entry
399 following the last \. If a \ is not found in the path the return value will
400 be the same as the input value. All error conditions return NULL.
402 The behavior when passing in a path that has not been normalized is undefined.
404 @param Path - A pointer to a wide character string containing a path to a
407 @return Pointer to the file name or terminal directory. NULL if an error is
412 GetFileNameFromPath (
422 Tail
= wcsrchr(Path
, L
'\\');
424 Tail
= (wchar_t *) Path
;
426 // Move to the next character after the '\\' to get the file name.