public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Joey Gouly" <joey.gouly@arm.com>
To: <devel@edk2.groups.io>
Cc: <joey.gouly@arm.com>, <sami.mujawar@arm.com>, <ray.ni@intel.com>,
	<zhichao.gao@intel.com>, <nd@arm.com>
Subject: [PATCH v1 2/5] ShellPkg: add a helper function for getting a new file name
Date: Fri, 7 May 2021 11:38:01 +0100	[thread overview]
Message-ID: <20210507103804.8131-3-joey.gouly@arm.com> (raw)
In-Reply-To: <20210507103804.8131-1-joey.gouly@arm.com>

From: Marc Moisson-Franckhauser <marc.moisson-franckhauser@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@arm.com>
Signed-off-by: Joey Gouly <joey.gouly@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")


  parent reply	other threads:[~2021-05-07 10:38 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-07 10:37 [PATCH v1 0/5] Dot graph generator for PPTT Joey Gouly
2021-05-07 10:38 ` [PATCH v1 1/5] ShellPkg: Replace 'Trace' parameter with 'ParseFlags' Joey Gouly
2021-05-07 10:38 ` Joey Gouly [this message]
2021-05-07 10:38 ` [PATCH v1 3/5] ShellPkg: add a Graph option to the Parser Flags Joey Gouly
2021-05-07 10:38 ` [PATCH v1 4/5] ShellPkg: add dot file generator functions Joey Gouly
2021-05-07 10:38 ` [PATCH v1 5/5] ShellPkg: add PPTT dot file genration Joey Gouly
2021-05-07 11:47 ` [PATCH v1 0/5] Dot graph generator for PPTT Joey Gouly

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210507103804.8131-3-joey.gouly@arm.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox