From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=68.232.153.90; helo=esa1.dell-outbound.iphmx.com; envelope-from=jim.dailey@dell.com; receiver=edk2-devel@lists.01.org Received: from esa1.dell-outbound.iphmx.com (esa1.dell-outbound.iphmx.com [68.232.153.90]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 8039621B02822 for ; Mon, 29 Oct 2018 14:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=dell.com; i=@dell.com; q=dns/txt; s=smtpout; t=1540847680; x=1572383680; h=from:to:cc:subject:date:message-id: content-transfer-encoding:mime-version; bh=NqMprEwkcif4euOj96t1/duxmu4fUM2+lOo1i5FoCU8=; b=gl55LAxYWULm7q7JCYhdaS7KXLlN8amRFIgmuL9mg+Lz107rrsEUd+Lk GcFjFR8eF+QHV6Ov3I4H9imeQ90gltSzoLisqu9fT9RfySajMknMerVE9 D9rXYs/Pd2rZvlgF/hBr8XeBxb39lBTgdWVeP12rlOT74HLUxO7VD0Jb9 s=; X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A2EtAABmd9dbhyWd50MYAkocAQEBBAE?= =?us-ascii?q?BBwQBAYFRBwEBCwEBg1YSKAqMA1+kZxSBZgsBAYRsg1E0DQ0BAwEBAgEBAgE?= =?us-ascii?q?BAhABAQEKCwkIKS+CNiKCZhYoPxIBPmgBBA4FCBqCf4ICjHiRbgKBEIlYAQE?= =?us-ascii?q?BghuKGoIuhVaCRoM0gRGHVQRvhSMCiHeFeZAZBwKQfCCQR5QzgkICBAIEBQI?= =?us-ascii?q?UgUOCDnCDPII0jiNviliBLoEfAQE?= X-IPAS-Result: =?us-ascii?q?A2EtAABmd9dbhyWd50MYAkocAQEBBAEBBwQBAYFRBwEBC?= =?us-ascii?q?wEBg1YSKAqMA1+kZxSBZgsBAYRsg1E0DQ0BAwEBAgEBAgEBAhABAQEKCwkIK?= =?us-ascii?q?S+CNiKCZhYoPxIBPmgBBA4FCBqCf4ICjHiRbgKBEIlYAQEBghuKGoIuhVaCR?= =?us-ascii?q?oM0gRGHVQRvhSMCiHeFeZAZBwKQfCCQR5QzgkICBAIEBQIUgUOCDnCDPII0j?= =?us-ascii?q?iNviliBLoEfAQE?= Received: from mx0b-00154901.pphosted.com (HELO mx0a-00154901.pphosted.com) ([67.231.157.37]) by esa1.dell-outbound.iphmx.com with ESMTP/TLS/AES256-SHA256; 29 Oct 2018 16:14:39 -0500 Received: from pps.filterd (m0089484.ppops.net [127.0.0.1]) by mx0b-00154901.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w9TL8dWN051266 for ; Mon, 29 Oct 2018 17:14:38 -0400 Received: from esa2.dell-outbound2.iphmx.com (esa2.dell-outbound2.iphmx.com [68.232.153.202]) by mx0b-00154901.pphosted.com with ESMTP id 2ne86q0je3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Mon, 29 Oct 2018 17:14:38 -0400 From: Received: from ausxippc106.us.dell.com ([143.166.85.156]) by esa2.dell-outbound2.iphmx.com with ESMTP/TLS/DHE-RSA-AES256-SHA256; 30 Oct 2018 03:14:17 +0600 X-LoopCount0: from 10.166.136.217 X-IronPort-AV: E=Sophos;i="5.54,441,1534827600"; d="scan'208";a="312435025" To: CC: , Thread-Topic: [PATCH v2 1/2] ShellPkg-UefiShellLib: Add a function to fully-qualify paths Thread-Index: AdRvyvFMWA6pVzosSjCUose/JKCJhg== Date: Mon, 29 Oct 2018 21:14:36 +0000 Message-ID: <517a9546603441cc868554cb350e8afe@ausx13mps335.AMER.DELL.COM> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: MSIP_Label_17cb76b2-10b8-4fe1-93d4-2202842406cd_Enabled=True; MSIP_Label_17cb76b2-10b8-4fe1-93d4-2202842406cd_SiteId=945c199a-83a2-4e80-9f8c-5a91be5752dd; MSIP_Label_17cb76b2-10b8-4fe1-93d4-2202842406cd_Owner=Jim_Dailey@Dell.com; MSIP_Label_17cb76b2-10b8-4fe1-93d4-2202842406cd_SetDate=2018-10-29T21:03:22.9674761Z; MSIP_Label_17cb76b2-10b8-4fe1-93d4-2202842406cd_Name=External Public; MSIP_Label_17cb76b2-10b8-4fe1-93d4-2202842406cd_Application=Microsoft Azure Information Protection; MSIP_Label_17cb76b2-10b8-4fe1-93d4-2202842406cd_Extended_MSFT_Method=Manual; Sensitivity=External Public x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.143.18.86] MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-10-29_12:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=708 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1810290190 Subject: [PATCH v2 1/2] ShellPkg-UefiShellLib: Add a function to fully-qualify paths X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 29 Oct 2018 21:14:41 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable 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 --- ShellPkg/Include/Library/ShellLib.h | 35 ++++++ ShellPkg/Library/UefiShellLib/UefiShellLib.c | 124 ++++++++++++++++++- 2 files changed, 158 insertions(+), 1 deletion(-) diff --git a/ShellPkg/Include/Library/ShellLib.h b/ShellPkg/Include/Library= /ShellLib.h index 92fddc50f5..2ecc5ee006 100644 --- a/ShellPkg/Include/Library/ShellLib.h +++ b/ShellPkg/Include/Library/ShellLib.h @@ -2,6 +2,7 @@ Provides interface to shell functionality for shell commands and applica= tions. =20 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+ Copyright 2018 Dell Technologies.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License which accompanies this distribution. The full text of the license may b= e found at @@ -35,6 +36,40 @@ extern EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol; extern EFI_SHELL_PROTOCOL *gEfiShellProtocol; =20 +/** + Return a clean, fully-qualified version of an input path. If the return= value + is non-NULL the caller must free the memory when it is no longer needed. + + If asserts are disabled, and if the input parameter is NULL, NULL is ret= urned. + + If there is not enough memory available to create the fully-qualified pa= th or + a copy of the input path, NULL is returned. + + If there is no working directory, a clean copy of Path is returned. + + Otherwise, the current file system or working directory (as appropriate)= is + prepended to Path and the resulting path is cleaned and returned. + + NOTE: If the input path is an empty string, then the current working dir= ectory + (if it exists) is returned. In other words, an empty input path is trea= ted + exactly the same as ".". + + @param[in] Path A pointer to some file or directory path. + + @retval NULL The input path is NULL or out of memory. + + @retval non-NULL A pointer to a clean, fully-qualified version of P= ath. + If there is no working directory, then a pointer t= o a + clean, but not necessarily fully-qualified version= of + Path. The caller must free this memory when it is= no + longer needed. +**/ +CHAR16* +EFIAPI +FullyQualifyPath( + IN CONST CHAR16 *Path + ); + /** This function will retrieve the information about the file for the handl= e specified and store it in allocated pool memory. diff --git a/ShellPkg/Library/UefiShellLib/UefiShellLib.c b/ShellPkg/Librar= y/UefiShellLib/UefiShellLib.c index f04adbb63f..8467eb8953 100644 --- a/ShellPkg/Library/UefiShellLib/UefiShellLib.c +++ b/ShellPkg/Library/UefiShellLib/UefiShellLib.c @@ -2,7 +2,7 @@ Provides interface to shell functionality for shell commands and applica= tions. =20 (C) Copyright 2016 Hewlett Packard Enterprise Development LP
- Copyright 2016 Dell Inc. + Copyright 2016-2018 Dell Technologies.
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License @@ -36,6 +36,128 @@ EFI_HANDLE mEfiShellEnvironment2Hand= le; FILE_HANDLE_FUNCTION_MAP FileFunctionMap; EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationProtocol; =20 +/** + Return a clean, fully-qualified version of an input path. If the return= value + is non-NULL the caller must free the memory when it is no longer needed. + + If asserts are disabled, and if the input parameter is NULL, NULL is ret= urned. + + If there is not enough memory available to create the fully-qualified pa= th or + a copy of the input path, NULL is returned. + + If there is no working directory, a clean copy of Path is returned. + + Otherwise, the current file system or working directory (as appropriate)= is + prepended to Path and the resulting path is cleaned and returned. + + NOTE: If the input path is an empty string, then the current working dir= ectory + (if it exists) is returned. In other words, an empty input path is trea= ted + exactly the same as ".". + + @param[in] Path A pointer to some file or directory path. + + @retval NULL The input path is NULL or out of memory. + + @retval non-NULL A pointer to a clean, fully-qualified version of P= ath. + If there is no working directory, then a pointer t= o a + clean, but not necessarily fully-qualified version= of + Path. The caller must free this memory when it is= no + longer needed. +**/ +CHAR16* +EFIAPI +FullyQualifyPath( + IN CONST CHAR16 *Path + ) +{ + CONST CHAR16 *WorkingPath; + CONST CHAR16 *InputPath; + CHAR16 *InputFileSystem; + UINTN FileSystemCharCount; + CHAR16 *FullyQualifiedPath; + UINTN Size; + + FullyQualifiedPath =3D NULL; + + ASSERT(Path !=3D NULL); + // + // Handle erroneous input when asserts are disabled. + // + if (Path =3D=3D NULL) { + return NULL; + } + // + // In paths that contain ":", like fs0:dir/file.ext and fs2:\fqpath\file= .ext, + // we have to consider the file system part separately from the "path" = part. + // If there is a file system in the path, we have to get the current wor= king + // directory for that file system. Then we need to use the part of the p= ath + // following the ":". If a path does not contain ":", we use it as give= n. + // + InputPath =3D StrStr(Path, L":"); + if (InputPath !=3D NULL) { + InputPath++; + FileSystemCharCount =3D ((UINTN)InputPath - (UINTN)Path + sizeof(CHAR1= 6)) / sizeof(CHAR16); + InputFileSystem =3D AllocateCopyPool(FileSystemCharCount * sizeof(CHAR= 16), Path); + if (InputFileSystem !=3D NULL) { + InputFileSystem[FileSystemCharCount - 1] =3D CHAR_NULL; + } + WorkingPath =3D ShellGetCurrentDir(InputFileSystem); + SHELL_FREE_NON_NULL(InputFileSystem); + // + // Handle the degenerate case where Path was only a file system refere= nce. + // In that case we return the current working directory of the file sy= stem. + // + if (InputPath =3D=3D NULL) { + InputPath =3D L""; + } + } else { + InputPath =3D Path; + WorkingPath =3D ShellGetEnvironmentVariable(L"cwd"); + } + + if (WorkingPath =3D=3D NULL) { + // + // With no working directory, all we can do is copy and clean the inpu= t path. + // + FullyQualifiedPath =3D AllocateCopyPool(StrSize(Path), Path); + } else { + // + // Allocate space for both strings plus one more character. + // + Size =3D StrSize(WorkingPath) + StrSize(InputPath); + FullyQualifiedPath =3D AllocateZeroPool(Size); + if (FullyQualifiedPath =3D=3D NULL) { + // + // Try to copy and clean just the input. No harm if not enough memor= y. + // + FullyQualifiedPath =3D AllocateCopyPool(StrSize(Path), Path); + } else { + if (*InputPath =3D=3D L'\\' || *InputPath =3D=3D L'/') { + // + // Absolute path: start with the current working directory, then + // truncate the new path after the file system part. + // + StrCpyS(FullyQualifiedPath, Size/sizeof(CHAR16), WorkingPath); + *(StrStr(FullyQualifiedPath, L":") + 1) =3D CHAR_NULL; + } else { + // + // Relative path: start with the working directory and append "\". + // + StrCpyS(FullyQualifiedPath, Size/sizeof(CHAR16), WorkingPath); + StrCatS(FullyQualifiedPath, Size/sizeof(CHAR16), L"\\"); + } + // + // Now append the absolute or relative path. + // + StrCatS(FullyQualifiedPath, Size/sizeof(CHAR16), InputPath); + } + } + + PathCleanUpDirectories(FullyQualifiedPath); + + return FullyQualifiedPath; +} + /** Check if a Unicode character is a hexadecimal character. =20 --=20 2.17.0.windows.1