]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg-UefiShellLib: Add a function to fully-qualify paths
authorJim.Dailey@dell.com <Jim.Dailey@dell.com>
Mon, 29 Oct 2018 21:14:36 +0000 (05:14 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Mon, 5 Nov 2018 02:52:40 +0000 (10:52 +0800)
Add a function to return a clean, fully-qualified version of some path.

This function handles a (possibly "dirty") input path that may or may
not include a file system reference.

If it does not include a file system reference, then if the input path
does not begin with a forward or backward slash, then the input path is
relative to the current working directory of the current file system.
Otherwise, it is an absolute path within the current file system.

If it does include a file system reference, it may be a reference to the
current or some other file system.  If the file system reference is not
immediately followed by a forward or backward slash, then the input path
is relative to the current working directory of the given file system.
Otherwise, it is an absolute path within the given file system.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jim Dailey <jim_dailey@dell.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
ShellPkg/Include/Library/ShellLib.h
ShellPkg/Library/UefiShellLib/UefiShellLib.c

index 92fddc50f5dd525f07deeb4568d64e8748328825..2ecc5ee0068b9e49f696c95309e4f50fd06b3ada 100644 (file)
@@ -2,6 +2,7 @@
   Provides interface to shell functionality for shell commands and applications.\r
 \r
   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  Copyright 2018 Dell Technologies.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
 extern EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;\r
 extern EFI_SHELL_PROTOCOL            *gEfiShellProtocol;\r
 \r
+/**\r
+  Return a clean, fully-qualified version of an input path.  If the return value\r
+  is non-NULL the caller must free the memory when it is no longer needed.\r
+\r
+  If asserts are disabled, and if the input parameter is NULL, NULL is returned.\r
+\r
+  If there is not enough memory available to create the fully-qualified path or\r
+  a copy of the input path, NULL is returned.\r
+\r
+  If there is no working directory, a clean copy of Path is returned.\r
+\r
+  Otherwise, the current file system or working directory (as appropriate) is\r
+  prepended to Path and the resulting path is cleaned and returned.\r
+\r
+  NOTE: If the input path is an empty string, then the current working directory\r
+  (if it exists) is returned.  In other words, an empty input path is treated\r
+  exactly the same as ".".\r
+\r
+  @param[in] Path  A pointer to some file or directory path.\r
+\r
+  @retval NULL          The input path is NULL or out of memory.\r
+\r
+  @retval non-NULL      A pointer to a clean, fully-qualified version of Path.\r
+                        If there is no working directory, then a pointer to a\r
+                        clean, but not necessarily fully-qualified version of\r
+                        Path.  The caller must free this memory when it is no\r
+                        longer needed.\r
+**/\r
+CHAR16*\r
+EFIAPI\r
+FullyQualifyPath(\r
+  IN     CONST CHAR16     *Path\r
+  );\r
+\r
 /**\r
   This function will retrieve the information about the file for the handle\r
   specified and store it in allocated pool memory.\r
index f04adbb63ffe650a9a0b77b06d5e324a57dbcc4b..580a1ee612d6c641d0e310b2ad7e630a5ad73732 100644 (file)
@@ -2,7 +2,7 @@
   Provides interface to shell functionality for shell commands and applications.\r
 \r
   (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
-  Copyright 2016 Dell Inc.\r
+  Copyright 2016-2018 Dell Technologies.<BR>\r
   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
@@ -36,6 +36,121 @@ EFI_HANDLE                    mEfiShellEnvironment2Handle;
 FILE_HANDLE_FUNCTION_MAP      FileFunctionMap;\r
 EFI_UNICODE_COLLATION_PROTOCOL  *mUnicodeCollationProtocol;\r
 \r
+/**\r
+  Return a clean, fully-qualified version of an input path.  If the return value\r
+  is non-NULL the caller must free the memory when it is no longer needed.\r
+\r
+  If asserts are disabled, and if the input parameter is NULL, NULL is returned.\r
+\r
+  If there is not enough memory available to create the fully-qualified path or\r
+  a copy of the input path, NULL is returned.\r
+\r
+  If there is no working directory, a clean copy of Path is returned.\r
+\r
+  Otherwise, the current file system or working directory (as appropriate) is\r
+  prepended to Path and the resulting path is cleaned and returned.\r
+\r
+  NOTE: If the input path is an empty string, then the current working directory\r
+  (if it exists) is returned.  In other words, an empty input path is treated\r
+  exactly the same as ".".\r
+\r
+  @param[in] Path  A pointer to some file or directory path.\r
+\r
+  @retval NULL          The input path is NULL or out of memory.\r
+\r
+  @retval non-NULL      A pointer to a clean, fully-qualified version of Path.\r
+                        If there is no working directory, then a pointer to a\r
+                        clean, but not necessarily fully-qualified version of\r
+                        Path.  The caller must free this memory when it is no\r
+                        longer needed.\r
+**/\r
+CHAR16*\r
+EFIAPI\r
+FullyQualifyPath(\r
+  IN     CONST CHAR16     *Path\r
+  )\r
+{\r
+  CONST CHAR16         *WorkingPath;\r
+  CONST CHAR16         *InputPath;\r
+  CHAR16               *InputFileSystem;\r
+  UINTN                FileSystemCharCount;\r
+  CHAR16               *FullyQualifiedPath;\r
+  UINTN                Size;\r
+\r
+  FullyQualifiedPath = NULL;\r
+\r
+  ASSERT(Path != NULL);\r
+  //\r
+  // Handle erroneous input when asserts are disabled.\r
+  //\r
+  if (Path == NULL) {\r
+    return NULL;\r
+  }\r
+  //\r
+  // In paths that contain ":", like fs0:dir/file.ext and fs2:\fqpath\file.ext,\r
+  // we  have to consider the file system part separately from the "path" part.\r
+  // If there is a file system in the path, we have to get the current working\r
+  // directory for that file system. Then we need to use the part of the path\r
+  // following the ":".  If a path does not contain ":", we use it as given.\r
+  //\r
+  InputPath = StrStr(Path, L":");\r
+  if (InputPath != NULL) {\r
+    InputPath++;\r
+    FileSystemCharCount = ((UINTN)InputPath - (UINTN)Path + sizeof(CHAR16)) / sizeof(CHAR16);\r
+    InputFileSystem = AllocateCopyPool(FileSystemCharCount * sizeof(CHAR16), Path);\r
+    if (InputFileSystem != NULL) {\r
+      InputFileSystem[FileSystemCharCount - 1] = CHAR_NULL;\r
+    }\r
+    WorkingPath = ShellGetCurrentDir(InputFileSystem);\r
+    SHELL_FREE_NON_NULL(InputFileSystem);\r
+  } else {\r
+    InputPath = Path;\r
+    WorkingPath = ShellGetEnvironmentVariable(L"cwd");\r
+  }\r
+\r
+  if (WorkingPath == NULL) {\r
+    //\r
+    // With no working directory, all we can do is copy and clean the input path.\r
+    //\r
+    FullyQualifiedPath = AllocateCopyPool(StrSize(Path), Path);\r
+  } else {\r
+    //\r
+    // Allocate space for both strings plus one more character.\r
+    //\r
+    Size = StrSize(WorkingPath) + StrSize(InputPath);\r
+    FullyQualifiedPath = AllocateZeroPool(Size);\r
+    if (FullyQualifiedPath == NULL) {\r
+      //\r
+      // Try to copy and clean just the input. No harm if not enough memory.\r
+      //\r
+      FullyQualifiedPath = AllocateCopyPool(StrSize(Path), Path);\r
+    } else {\r
+      if (*InputPath == L'\\' || *InputPath == L'/') {\r
+        //\r
+        // Absolute path: start with the current working directory, then\r
+        // truncate the new path after the file system part.\r
+        //\r
+        StrCpyS(FullyQualifiedPath, Size/sizeof(CHAR16), WorkingPath);\r
+        *(StrStr(FullyQualifiedPath, L":") + 1) = CHAR_NULL;\r
+      } else {\r
+        //\r
+        // Relative path: start with the working directory and append "\".\r
+        //\r
+        StrCpyS(FullyQualifiedPath, Size/sizeof(CHAR16), WorkingPath);\r
+        StrCatS(FullyQualifiedPath, Size/sizeof(CHAR16), L"\\");\r
+      }\r
+      //\r
+      // Now append the absolute or relative path.\r
+      //\r
+      StrCatS(FullyQualifiedPath, Size/sizeof(CHAR16), InputPath);\r
+    }\r
+  }\r
+\r
+  PathCleanUpDirectories(FullyQualifiedPath);\r
+\r
+  return FullyQualifiedPath;\r
+}\r
+\r
 /**\r
   Check if a Unicode character is a hexadecimal character.\r
 \r