]>
Commit | Line | Data |
---|---|---|
ab94587a | 1 | /** @file\r |
ae591c14 | 2 | Defines file-path manipulation functions.\r |
ab94587a | 3 | \r |
9fdf3178 | 4 | Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>\r |
7177be0b | 5 | Copyright (c) 2018, Dell Technologies. All rights reserved.<BR>\r |
9344f092 | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
ab94587a | 7 | **/\r |
ae591c14 DM |
8 | #include <Library/BaseMemoryLib.h>\r |
9 | #include <Library/BaseLib.h>\r | |
ab94587a | 10 | \r |
11 | /**\r | |
6a623094 HW |
12 | Removes the last directory or file entry in a path. For a path which is\r |
13 | like L"fs0:startup.nsh", it's converted to L"fs0:".\r | |
ab94587a | 14 | \r |
ae591c14 | 15 | @param[in,out] Path A pointer to the path to modify.\r |
ab94587a | 16 | \r |
17 | @retval FALSE Nothing was found to remove.\r | |
18 | @retval TRUE A directory or file was removed.\r | |
19 | **/\r | |
20 | BOOLEAN\r | |
21 | EFIAPI\r | |
22 | PathRemoveLastItem(\r | |
23 | IN OUT CHAR16 *Path\r | |
24 | )\r | |
25 | {\r | |
26 | CHAR16 *Walker;\r | |
27 | CHAR16 *LastSlash;\r | |
28 | //\r | |
29 | // get directory name from path... ('chop' off extra)\r | |
30 | //\r | |
31 | for ( Walker = Path, LastSlash = NULL\r | |
32 | ; Walker != NULL && *Walker != CHAR_NULL\r | |
33 | ; Walker++\r | |
34 | ){\r | |
6a623094 HW |
35 | if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r |
36 | LastSlash = Walker+1;\r | |
37 | } else if (*Walker == L':' && *(Walker + 1) != L'\\' && *(Walker + 1) != CHAR_NULL) {\r | |
ab94587a | 38 | LastSlash = Walker+1;\r |
39 | }\r | |
40 | }\r | |
41 | if (LastSlash != NULL) {\r | |
42 | *LastSlash = CHAR_NULL;\r | |
43 | return (TRUE);\r | |
44 | }\r | |
45 | return (FALSE);\r | |
46 | }\r | |
47 | \r | |
48 | /**\r | |
ae591c14 DM |
49 | Function to clean up paths.\r |
50 | \r | |
ab94587a | 51 | - Single periods in the path are removed.\r |
52 | - Double periods in the path are removed along with a single parent directory.\r | |
53 | - Forward slashes L'/' are converted to backward slashes L'\'.\r | |
54 | \r | |
ae591c14 | 55 | This will be done inline and the existing buffer may be larger than required\r |
ab94587a | 56 | upon completion.\r |
57 | \r | |
58 | @param[in] Path The pointer to the string containing the path.\r | |
59 | \r | |
ae591c14 | 60 | @return Returns Path, otherwise returns NULL to indicate that an error has occured.\r |
ab94587a | 61 | **/\r |
62 | CHAR16*\r | |
63 | EFIAPI\r | |
64 | PathCleanUpDirectories(\r | |
65 | IN CHAR16 *Path\r | |
bb99e328 | 66 | )\r |
ab94587a | 67 | {\r |
68 | CHAR16 *TempString;\r | |
ae591c14 | 69 | \r |
bb99e328 RN |
70 | if (Path == NULL) {\r |
71 | return NULL;\r | |
ab94587a | 72 | }\r |
bb99e328 | 73 | \r |
ab94587a | 74 | //\r |
bb99e328 | 75 | // Replace the '/' with '\'\r |
ab94587a | 76 | //\r |
bb99e328 | 77 | for (TempString = Path; *TempString != CHAR_NULL; TempString++) {\r |
ab94587a | 78 | if (*TempString == L'/') {\r |
79 | *TempString = L'\\';\r | |
80 | }\r | |
81 | }\r | |
bb99e328 | 82 | \r |
b08b045c JD |
83 | //\r |
84 | // Replace the "\\" with "\"\r | |
85 | //\r | |
86 | while ((TempString = StrStr (Path, L"\\\\")) != NULL) {\r | |
87 | CopyMem (TempString, TempString + 1, StrSize (TempString + 1));\r | |
88 | }\r | |
89 | \r | |
ab94587a | 90 | //\r |
bb99e328 | 91 | // Remove all the "\.". E.g.: fs0:\abc\.\def\.\r |
ab94587a | 92 | //\r |
bb99e328 RN |
93 | while ((TempString = StrStr (Path, L"\\.\\")) != NULL) {\r |
94 | CopyMem (TempString, TempString + 2, StrSize (TempString + 2));\r | |
ab94587a | 95 | }\r |
9fdf3178 | 96 | if ((StrLen (Path) >= 2) && (StrCmp (Path + StrLen (Path) - 2, L"\\.") == 0)) {\r |
bb99e328 | 97 | Path[StrLen (Path) - 1] = CHAR_NULL;\r |
ab94587a | 98 | }\r |
bb99e328 | 99 | \r |
ab94587a | 100 | //\r |
bb99e328 | 101 | // Remove all the "\..". E.g.: fs0:\abc\..\def\..\r |
ab94587a | 102 | //\r |
bb99e328 RN |
103 | while (((TempString = StrStr(Path, L"\\..")) != NULL) &&\r |
104 | ((*(TempString + 3) == L'\\') || (*(TempString + 3) == CHAR_NULL))\r | |
105 | ) {\r | |
1fc3749d | 106 | *(TempString + 1) = CHAR_NULL;\r |
bb99e328 | 107 | PathRemoveLastItem(Path);\r |
7177be0b JD |
108 | if (*(TempString + 3) != CHAR_NULL) {\r |
109 | CopyMem (Path + StrLen (Path), TempString + 4, StrSize (TempString + 4));\r | |
110 | }\r | |
ab94587a | 111 | }\r |
112 | \r | |
bb99e328 | 113 | return Path;\r |
ab94587a | 114 | }\r |
115 | \r |