[edk2-devel] [PATCH v1 2/5] ShellPkg: add a helper function for getting a new file name

Joey Gouly joey.gouly at arm.com
Fri May 7 10:38:01 UTC 2021


From: Marc Moisson-Franckhauser <marc.moisson-franckhauser at arm.com>

Bugzilla: 3378 (https://bugzilla.tianocore.org/show_bug.cgi?id=3378)

This new helper will not overwrite existing files, by appending a number
to the end of the filename.

Signed-off-by: Marc Moisson-Franckhauser <marc.moisson-franckhauser at arm.com>
Signed-off-by: Joey Gouly <joey.gouly at arm.com>
---
 ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.h | 25 +++++-
 ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c | 80 ++++++++++++++++----
 2 files changed, 91 insertions(+), 14 deletions(-)

diff --git a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.h b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.h
index d5b95f5ee707de18be1879b3cd235d6c5db11d9f..ae8a67b7681033d66d068341ae489ded67de8b44 100644
--- a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.h
+++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.h
@@ -1,12 +1,13 @@
 /** @file
   Header file for AcpiView
 
-  Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.
+  Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.
   SPDX-License-Identifier: BSD-2-Clause-Patent
 **/
 
 #ifndef ACPIVIEW_H_
 #define ACPIVIEW_H_
+#include <Library/ShellLib.h>
 
 /**
   A macro to define the max file name length
@@ -23,6 +24,28 @@
 **/
 #define RSDP_LENGTH_OFFSET   20
 
+/**
+  This function finds a filename not already used by adding a number in between
+  the BaseFileName and the extension.
+
+  Make sure the buffer FileName is big enough before calling the function. A
+  size of MAX_FILE_NAME_LEN is recommended.
+
+  @param [in]      BaseFileName      Start of the desired file name.
+  @param [in]      Extension         Extension of the desired file name
+                                     (without '.').
+  @param [in, out] FileName          Preallocated buffer for the returned file
+                                     name.
+  @param [in]      FileNameBufferLen Size of FileName buffer..
+**/
+EFI_STATUS
+GetNewFileName (
+  IN     CONST CHAR16* BaseFileName,
+  IN     CONST CHAR16* Extension,
+  IN OUT       CHAR16* FileName,
+  IN           UINT32  FileNameBufferLen
+  );
+
 /**
   This function resets the ACPI table error counter to Zero.
 **/
diff --git a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
index a4242ba9d99b05d07c829520c4011439445aadb0..db7b2e2a30525cc85a333b93f5eb97ec3a517b37 100644
--- a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
+++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
@@ -27,8 +27,55 @@
 #include "Arm/SbbrValidator.h"
 #endif
 
-STATIC UINT32             mTableCount;
-STATIC UINT32             mBinTableCount;
+STATIC UINT32  mTableCount;
+
+/**
+  This function finds a filename not already used by adding a number in between
+  The BaseFileName and the extension.
+
+  Make sure the buffer FileName is big enough before calling the function. A
+  size of MAX_FILE_NAME_LEN is recommended.
+
+  @param [in]      BaseFileName      Start of the desired file name.
+  @param [in]      Extension         Extension of the desired file name
+                                     (without '.').
+  @param [in, out] FileName          Preallocated buffer for the returned file
+                                     name.
+  @param [in]      FileNameBufferLen Size of FileName buffer..
+**/
+EFI_STATUS
+GetNewFileName (
+  IN     CONST CHAR16* BaseFileName,
+  IN     CONST CHAR16* Extension,
+  IN OUT       CHAR16* FileName,
+  IN           UINT32  FileNameBufferLen
+  )
+{
+  UINT16            Index;
+  EFI_STATUS        Status;
+  SHELL_FILE_HANDLE tmpFileHandle;
+  for (Index = 0; Index <= 99; Index++) {
+    UnicodeSPrint(
+      FileName,
+      FileNameBufferLen,
+      L"%s%02d.%s",
+      BaseFileName,
+      Index,
+      Extension
+      );
+    Status = ShellOpenFileByName (
+               FileName,
+               &tmpFileHandle,
+               EFI_FILE_MODE_READ,
+               0
+               );
+    if (Status == EFI_NOT_FOUND) {
+      return EFI_SUCCESS;
+    }
+    ShellCloseFile (&tmpFileHandle);
+  }
+  return EFI_OUT_OF_RESOURCES;
+}
 
 /**
   This function dumps the ACPI table to a file.
@@ -46,19 +93,27 @@ DumpAcpiTableToFile (
   IN CONST UINTN   Length
   )
 {
-  CHAR16              FileNameBuffer[MAX_FILE_NAME_LEN];
-  UINTN               TransferBytes;
-  SELECTED_ACPI_TABLE *SelectedTable;
+  CHAR16               FileNameBuffer[MAX_FILE_NAME_LEN];
+  UINTN                TransferBytes;
+  EFI_STATUS           Status;
+  SELECTED_ACPI_TABLE* SelectedTable;
 
   GetSelectedAcpiTable (&SelectedTable);
 
-  UnicodeSPrint (
-    FileNameBuffer,
-    sizeof (FileNameBuffer),
-    L".\\%s%04d.bin",
-    SelectedTable->Name,
-    mBinTableCount++
-    );
+  Status = GetNewFileName (
+             SelectedTable->Name,
+             L"bin",
+             FileNameBuffer,
+             sizeof (FileNameBuffer)
+             );
+  if (EFI_ERROR (Status)) {
+    Print (
+      L"Error: Could not open bin file for %s table:\n"
+      L"Could not get a file name.",
+      SelectedTable->Name
+      );
+    return FALSE;
+  }
 
   Print (L"Dumping ACPI table to : %s ... ", FileNameBuffer);
 
@@ -207,7 +262,6 @@ AcpiView (
 
   // Reset Table counts
   mTableCount = 0;
-  mBinTableCount = 0;
 
   // Reset The error/warning counters
   ResetErrorCount ();
-- 
Guid("CE165669-3EF3-493F-B85D-6190EE5B9759")



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#74812): https://edk2.groups.io/g/devel/message/74812
Mute This Topic: https://groups.io/mt/82651827/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-





More information about the edk2-devel-archive mailing list