]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Ls.c
ShellPkg/Ls: Return empty content for all empty folders
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Ls.c
index 1a65f60c3b44bd723e4870b95a75c982f453b325..da2b1acab47cf75dd76ffec2fea9fbed582bf449 100644 (file)
@@ -417,6 +417,8 @@ FileTimeToLocalTime (
   @param[in] Found          Set to TRUE, if anyone were found.\r
   @param[in] Count          The count of bits enabled in Attribs.\r
   @param[in] TimeZone       The current time zone offset.\r
+  @param[in] ListUnfiltered TRUE to request listing the directory contents\r
+                            unfiltered.\r
 \r
   @retval SHELL_SUCCESS     the printing was sucessful.\r
 **/\r
@@ -429,7 +431,8 @@ PrintLsOutput(
   IN CONST CHAR16  *SearchString,\r
   IN       BOOLEAN *Found,\r
   IN CONST UINTN   Count,\r
-  IN CONST INT16   TimeZone\r
+  IN CONST INT16   TimeZone,\r
+  IN CONST BOOLEAN ListUnfiltered\r
   )\r
 {\r
   EFI_STATUS            Status;\r
@@ -555,7 +558,7 @@ PrintLsOutput(
       HeaderPrinted = TRUE;\r
     }\r
 \r
-    if (!Sfo && ShellStatus != SHELL_ABORTED) {\r
+    if (!Sfo && ShellStatus != SHELL_ABORTED && HeaderPrinted) {\r
       PrintNonSfoFooter(FileCount, FileSize, DirCount);\r
     }\r
   }\r
@@ -602,7 +605,8 @@ PrintLsOutput(
             SearchString,\r
             &FoundOne,\r
             Count,\r
-            TimeZone);\r
+            TimeZone,\r
+            FALSE);\r
 \r
           //\r
           // Since it's running recursively, we have to break immediately when returned SHELL_ABORTED\r
@@ -619,7 +623,21 @@ PrintLsOutput(
   ShellCloseFileMetaArg(&ListHead);\r
 \r
   if (Found == NULL && !FoundOne) {\r
-    return (SHELL_NOT_FOUND);\r
+    if (ListUnfiltered) {\r
+      //\r
+      // When running "ls" without any filtering request, avoid outputing\r
+      // "File not found" when the directory is entirely empty, but print\r
+      // header and footer stating "0 File(s), 0 Dir(s)".\r
+      //\r
+      if (!Sfo) {\r
+        PrintNonSfoHeader (RootPath);\r
+        if (ShellStatus != SHELL_ABORTED) {\r
+          PrintNonSfoFooter (FileCount, FileSize, DirCount);\r
+        }\r
+      }\r
+    } else {\r
+      return (SHELL_NOT_FOUND);\r
+    }\r
   }\r
 \r
   if (Found != NULL) {\r
@@ -662,6 +680,7 @@ ShellCommandRunLs (
   UINTN         Size;\r
   EFI_TIME      TheTime;\r
   CHAR16        *SearchString;\r
+  BOOLEAN       ListUnfiltered;\r
 \r
   Size                = 0;\r
   FullPath            = NULL;\r
@@ -673,6 +692,7 @@ ShellCommandRunLs (
   SearchString        = NULL;\r
   CurDir              = NULL;\r
   Count               = 0;\r
+  ListUnfiltered      = FALSE;\r
 \r
   //\r
   // initialize the shell lib (we must be in non-auto-init...)\r
@@ -768,6 +788,7 @@ ShellCommandRunLs (
             ShellStatus = SHELL_NOT_FOUND;\r
             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"ls");\r
           }\r
+          ListUnfiltered = TRUE;\r
           //\r
           // Copy to the 2 strings for starting path and file search string\r
           //\r
@@ -808,6 +829,7 @@ ShellCommandRunLs (
               //\r
               // is listing ends with a directory, then we list all files in that directory\r
               //\r
+              ListUnfiltered = TRUE;\r
               StrnCatGrow(&SearchString, NULL, L"*", 0);\r
             } else {\r
               //\r
@@ -839,7 +861,8 @@ ShellCommandRunLs (
             SearchString,\r
             NULL,\r
             Count,\r
-            TheTime.TimeZone\r
+            TheTime.TimeZone,\r
+            ListUnfiltered\r
            );\r
           if (ShellStatus == SHELL_NOT_FOUND) {\r
             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LS_FILE_NOT_FOUND), gShellLevel2HiiHandle, L"ls", FullPath);\r