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