]> git.proxmox.com Git - mirror_edk2.git/blobdiff - FatPkg/EnhancedFatDxe/Delete.c
Add EDK II Prime FatPkg New Feature: Support both Unicode Collation and Unicode Colla...
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Delete.c
diff --git a/FatPkg/EnhancedFatDxe/Delete.c b/FatPkg/EnhancedFatDxe/Delete.c
new file mode 100644 (file)
index 0000000..b76fe45
--- /dev/null
@@ -0,0 +1,138 @@
+/*++\r
+\r
+Copyright (c) 2005, Intel Corporation\r
+All rights reserved. This program and the accompanying materials are licensed and made available\r
+under the terms and conditions of the BSD License which accompanies this\r
+distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+\r
+Module Name:\r
+\r
+  delete.c\r
+\r
+Abstract:\r
+\r
+  Function that deletes a file\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+#include "Fat.h"\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+FatDelete (\r
+  IN EFI_FILE  *FHand\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Deletes the file & Closes the file handle.\r
+\r
+Arguments:\r
+\r
+  FHand                    - Handle to the file to delete.\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS              - Delete the file successfully.\r
+  EFI_WARN_DELETE_FAILURE  - Fail to delete the file.\r
+\r
+--*/\r
+{\r
+  FAT_IFILE   *IFile;\r
+  FAT_OFILE   *OFile;\r
+  FAT_DIRENT  *DirEnt;\r
+  EFI_STATUS  Status;\r
+  UINTN       Round;\r
+\r
+  IFile = IFILE_FROM_FHAND (FHand);\r
+  OFile = IFile->OFile;\r
+\r
+  //\r
+  // Lock the volume\r
+  //\r
+  FatAcquireLock ();\r
+\r
+  //\r
+  // If the file is read-only, then don't delete it\r
+  //\r
+  if (IFile->ReadOnly) {\r
+    Status = EFI_WRITE_PROTECTED;\r
+    goto Done;\r
+  }\r
+  //\r
+  // If the file is the root dir, then don't delete it\r
+  //\r
+  if (OFile->Parent == NULL) {\r
+    Status = EFI_ACCESS_DENIED;\r
+    goto Done;\r
+  }\r
+  //\r
+  // If the file has a permanant error, skip the delete\r
+  //\r
+  Status = OFile->Error;\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // If this is a directory, make sure it's empty before\r
+    // allowing it to be deleted\r
+    //\r
+    if (OFile->ODir != NULL) {\r
+      //\r
+      // We do not allow to delete nonempty directory\r
+      //\r
+      FatResetODirCursor (OFile);\r
+      for (Round = 0; Round < 3; Round++) {\r
+        Status = FatGetNextDirEnt (OFile, &DirEnt);\r
+        if ((EFI_ERROR (Status)) ||\r
+            ((Round < 2) && (DirEnt == NULL || !FatIsDotDirEnt (DirEnt))) ||\r
+            ((Round == 2) && (DirEnt != NULL))\r
+            ) {\r
+          Status = EFI_ACCESS_DENIED;\r
+          goto Done;\r
+        }\r
+      }\r
+    }\r
+    //\r
+    // Return the file's space by setting its size to 0\r
+    //\r
+    FatTruncateOFile (OFile, 0);\r
+    //\r
+    // Free the directory entry for this file\r
+    //\r
+    Status = FatRemoveDirEnt (OFile->Parent, OFile->DirEnt);\r
+    if (EFI_ERROR (Status)) {\r
+      goto Done;\r
+    }\r
+    //\r
+    // Set a permanent error for this OFile in case there\r
+    // are still opened IFiles attached\r
+    //\r
+    OFile->Error = EFI_NOT_FOUND;\r
+  } else if (OFile->Error == EFI_NOT_FOUND) {\r
+    Status = EFI_SUCCESS;\r
+  }\r
+\r
+Done:\r
+  //\r
+  // Always close the handle\r
+  //\r
+  FatIFileClose (IFile);\r
+  //\r
+  // Done\r
+  //\r
+  Status = FatCleanupVolume (OFile->Volume, NULL, Status);\r
+  FatReleaseLock ();\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_WARN_DELETE_FAILURE;\r
+  }\r
+\r
+  return Status;\r
+}\r