2011-07-01 00:58:53 +02:00
|
|
|
/** @file
|
2015-01-13 02:04:07 +01:00
|
|
|
Defines file-path manipulation functions.
|
2011-07-01 00:58:53 +02:00
|
|
|
|
2017-09-19 05:01:56 +02:00
|
|
|
Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
|
2011-07-01 00:58:53 +02:00
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php.
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
**/
|
2015-01-13 02:04:07 +01:00
|
|
|
#include <Library/BaseMemoryLib.h>
|
|
|
|
#include <Library/BaseLib.h>
|
2011-07-01 00:58:53 +02:00
|
|
|
|
|
|
|
/**
|
2016-11-17 07:07:54 +01:00
|
|
|
Removes the last directory or file entry in a path. For a path which is
|
|
|
|
like L"fs0:startup.nsh", it's converted to L"fs0:".
|
2011-07-01 00:58:53 +02:00
|
|
|
|
2015-01-13 02:04:07 +01:00
|
|
|
@param[in,out] Path A pointer to the path to modify.
|
2011-07-01 00:58:53 +02:00
|
|
|
|
|
|
|
@retval FALSE Nothing was found to remove.
|
|
|
|
@retval TRUE A directory or file was removed.
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
EFIAPI
|
|
|
|
PathRemoveLastItem(
|
|
|
|
IN OUT CHAR16 *Path
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CHAR16 *Walker;
|
|
|
|
CHAR16 *LastSlash;
|
|
|
|
//
|
|
|
|
// get directory name from path... ('chop' off extra)
|
|
|
|
//
|
|
|
|
for ( Walker = Path, LastSlash = NULL
|
|
|
|
; Walker != NULL && *Walker != CHAR_NULL
|
|
|
|
; Walker++
|
|
|
|
){
|
2016-11-17 07:07:54 +01:00
|
|
|
if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {
|
|
|
|
LastSlash = Walker+1;
|
|
|
|
} else if (*Walker == L':' && *(Walker + 1) != L'\\' && *(Walker + 1) != CHAR_NULL) {
|
2011-07-01 00:58:53 +02:00
|
|
|
LastSlash = Walker+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (LastSlash != NULL) {
|
|
|
|
*LastSlash = CHAR_NULL;
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-13 02:04:07 +01:00
|
|
|
Function to clean up paths.
|
|
|
|
|
2011-07-01 00:58:53 +02:00
|
|
|
- Single periods in the path are removed.
|
|
|
|
- Double periods in the path are removed along with a single parent directory.
|
|
|
|
- Forward slashes L'/' are converted to backward slashes L'\'.
|
|
|
|
|
2015-01-13 02:04:07 +01:00
|
|
|
This will be done inline and the existing buffer may be larger than required
|
2011-07-01 00:58:53 +02:00
|
|
|
upon completion.
|
|
|
|
|
|
|
|
@param[in] Path The pointer to the string containing the path.
|
|
|
|
|
2015-01-13 02:04:07 +01:00
|
|
|
@return Returns Path, otherwise returns NULL to indicate that an error has occured.
|
2011-07-01 00:58:53 +02:00
|
|
|
**/
|
|
|
|
CHAR16*
|
|
|
|
EFIAPI
|
|
|
|
PathCleanUpDirectories(
|
|
|
|
IN CHAR16 *Path
|
2016-12-21 08:21:32 +01:00
|
|
|
)
|
2011-07-01 00:58:53 +02:00
|
|
|
{
|
|
|
|
CHAR16 *TempString;
|
2015-01-13 02:04:07 +01:00
|
|
|
|
2016-12-21 08:21:32 +01:00
|
|
|
if (Path == NULL) {
|
|
|
|
return NULL;
|
2011-07-01 00:58:53 +02:00
|
|
|
}
|
2016-12-21 08:21:32 +01:00
|
|
|
|
2011-07-01 00:58:53 +02:00
|
|
|
//
|
2016-12-21 08:21:32 +01:00
|
|
|
// Replace the '/' with '\'
|
2011-07-01 00:58:53 +02:00
|
|
|
//
|
2016-12-21 08:21:32 +01:00
|
|
|
for (TempString = Path; *TempString != CHAR_NULL; TempString++) {
|
2011-07-01 00:58:53 +02:00
|
|
|
if (*TempString == L'/') {
|
|
|
|
*TempString = L'\\';
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 08:21:32 +01:00
|
|
|
|
2011-07-01 00:58:53 +02:00
|
|
|
//
|
2016-12-21 08:21:32 +01:00
|
|
|
// Remove all the "\.". E.g.: fs0:\abc\.\def\.
|
2011-07-01 00:58:53 +02:00
|
|
|
//
|
2016-12-21 08:21:32 +01:00
|
|
|
while ((TempString = StrStr (Path, L"\\.\\")) != NULL) {
|
|
|
|
CopyMem (TempString, TempString + 2, StrSize (TempString + 2));
|
2011-07-01 00:58:53 +02:00
|
|
|
}
|
2017-09-19 05:01:56 +02:00
|
|
|
if ((StrLen (Path) >= 2) && (StrCmp (Path + StrLen (Path) - 2, L"\\.") == 0)) {
|
2016-12-21 08:21:32 +01:00
|
|
|
Path[StrLen (Path) - 1] = CHAR_NULL;
|
2011-07-01 00:58:53 +02:00
|
|
|
}
|
2016-12-21 08:21:32 +01:00
|
|
|
|
2011-07-01 00:58:53 +02:00
|
|
|
//
|
2016-12-21 08:21:32 +01:00
|
|
|
// Remove all the "\..". E.g.: fs0:\abc\..\def\..
|
2011-07-01 00:58:53 +02:00
|
|
|
//
|
2016-12-21 08:21:32 +01:00
|
|
|
while (((TempString = StrStr(Path, L"\\..")) != NULL) &&
|
|
|
|
((*(TempString + 3) == L'\\') || (*(TempString + 3) == CHAR_NULL))
|
|
|
|
) {
|
2011-09-20 23:01:34 +02:00
|
|
|
*(TempString + 1) = CHAR_NULL;
|
2016-12-21 08:21:32 +01:00
|
|
|
PathRemoveLastItem(Path);
|
|
|
|
CopyMem (Path + StrLen (Path), TempString + 3, StrSize (TempString + 3));
|
2011-07-01 00:58:53 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 08:21:32 +01:00
|
|
|
//
|
|
|
|
// Replace the "\\" with "\"
|
|
|
|
//
|
|
|
|
while ((TempString = StrStr (Path, L"\\\\")) != NULL) {
|
|
|
|
CopyMem (TempString, TempString + 1, StrSize (TempString + 1));
|
2014-04-11 21:15:02 +02:00
|
|
|
}
|
2011-07-01 00:58:53 +02:00
|
|
|
|
2016-12-21 08:21:32 +01:00
|
|
|
return Path;
|
2011-07-01 00:58:53 +02:00
|
|
|
}
|
|
|
|
|